Search code examples
directxdirectx-11direct3ddirect3d11

DX11 triangle list is not rendering at all


I have a list of 4 verts loaded into a vert buffer, and an index loaded into a index buffer.

The issue I have is that while the LineList rendermode shows a quad just fine (see below) the TriangleList shows nothing (See below)

void BLX::Model::load(std::filesystem::path path, Model* model, ID3D11Device* d3dDevice, ID3D11DeviceContext* d3dContext)
{
    // tmp: just making a quad
    float num = 0.5f;
    std::vector<BLX::Vertex> vertices = {
        BLX::Vertex { DirectX::XMFLOAT3(-num, -num, 0.0f), DirectX::XMFLOAT3(0.0f, 0.0f, 0.5f), },  // 0 = TL
        BLX::Vertex { DirectX::XMFLOAT3(num, -num, 0.0f), DirectX::XMFLOAT3(0.0f, 0.5f, 0.0f), },   // 1 = TR
        BLX::Vertex { DirectX::XMFLOAT3(num, num, 0.0f), DirectX::XMFLOAT3(0.5f, 0.0f, 0.0f), }, // 2 = BR
        BLX::Vertex { DirectX::XMFLOAT3(-num, num, 0.0f), DirectX::XMFLOAT3(0.5f, 0.5f, 0.0f), },  // 3 = BL
    };

    // line list
    //std::vector<unsigned int> indices = { 0, 1, 1, 2, 2, 3, 3, 0 };

    // triangle list
    std::vector<unsigned int> indices = { 0, 1, 3, 3, 1, 2 };

    model->vertexCount = vertices.size();
    model->indexCount = indices.size();

    // Vertex Buffer

    D3D11_BUFFER_DESC vbd = {};
    vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vbd.Usage = D3D11_USAGE_DEFAULT;
    vbd.CPUAccessFlags = 0u;
    vbd.MiscFlags = 0u;
    vbd.ByteWidth = sizeof(BLX::Vertex) * model->vertexCount;

    vbd.StructureByteStride = sizeof(BLX::Vertex);

    D3D11_SUBRESOURCE_DATA vsd = {};
    vsd.pSysMem = &vertices[0];
    vsd.SysMemPitch = 0;
    vsd.SysMemSlicePitch = 0;

    d3dDevice->CreateBuffer(&vbd, &vsd, &model->vertexBuffer);

    /// Index Buffer
    
    D3D11_BUFFER_DESC ibd = {};
    ibd.Usage = D3D11_USAGE_DEFAULT;
    ibd.ByteWidth = sizeof(unsigned int) * model->indexCount;
    ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
    ibd.CPUAccessFlags = 0;
    ibd.MiscFlags = 0;

    D3D11_SUBRESOURCE_DATA isd = {};
    isd.pSysMem = &indices[0];
    isd.SysMemPitch = 0;
    isd.SysMemSlicePitch = 0;

    d3dDevice->CreateBuffer(&ibd, &isd, &model->indexBuffer);


    // IA = Input Assembly

    // pixel shader
    D3DReadFileToBlob(L"PixelShader2.cso", &model->pBlob);
    d3dDevice->CreatePixelShader(model->pBlob->GetBufferPointer(), model->pBlob->GetBufferSize(), nullptr, &model->pPixelShader);

    // Vertex Shader
    D3DReadFileToBlob(L"VertexShader2.cso", &model->pBlob);
    d3dDevice->CreateVertexShader(model->pBlob->GetBufferPointer(), model->pBlob->GetBufferSize(), nullptr, &model->pVertexShader);

    const D3D11_INPUT_ELEMENT_DESC ied[] =
    {
        // "Position" correcponds to Vertex Shader Semantic Name
                    // semantic index
                        // data type format
                                                  // Input slot
                                                     // Aligned byte offset
                                                        // Input slot class
                                                                                     // Instance data step rate
        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
        { "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    };
    // needs vertex shader blob
    d3dDevice->CreateInputLayout(ied, ARRAYSIZE(ied), model->pBlob->GetBufferPointer(), model->pBlob->GetBufferSize(), &model->pInputLayout);
}

void BLX::Model::render(ID3D11Device* d3dDevice, ID3D11DeviceContext* d3dContext, D3D11_VIEWPORT * vp)
{
    const UINT stride = sizeof(Vertex);
    const UINT offset[] = { 0u, 0u };

    d3dContext->IASetVertexBuffers(0u, 1u, vertexBuffer.GetAddressOf(), &stride, &offset[0]);
    d3dContext->IASetIndexBuffer(*indexBuffer.GetAddressOf(), DXGI_FORMAT_R32_UINT, offset[1]);

    d3dContext->PSSetShader(pPixelShader.Get(), nullptr, 0u);
    d3dContext->VSSetShader(pVertexShader.Get(), nullptr, 0u);

    d3dContext->IASetInputLayout(pInputLayout.Get());

    d3dContext->RSSetViewports(1u, vp);

    //d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY::D3D11_PRIMITIVE_TOPOLOGY_LINELIST);
    d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY::D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
    d3dContext->DrawIndexed(indexCount, 0, 0);
}

When using the LineList index and topology: enter image description here

When using the TriangleList index and topology: enter image description here

But when I was doing this:

    // tmp: just making a quad
    float num = 0.5f;
    std::vector<BLX::Vertex> vertices = {
        BLX::Vertex { DirectX::XMFLOAT3(0.0f, num, 0.0f), DirectX::XMFLOAT3(0.0f, 0.0f, 0.5f), },
        BLX::Vertex { DirectX::XMFLOAT3(num, -num, 0.0f), DirectX::XMFLOAT3(0.0f, 0.5f, 0.0f), },
        BLX::Vertex { DirectX::XMFLOAT3(-num, -num, 0.0f), DirectX::XMFLOAT3(0.5f, 0.0f, 0.0f), },
    };

    // triangle list
    std::vector<unsigned int> indices = { 0, 1, 2 };

(everything else the exact same) I got this: enter image description here

Just really curious what I'm not seeing or getting when trying to render two triangles to make up a quad


Solution

  • Your rectangle has indices organized in a clockwise manner, which are culled by the default rasterizer (since you do not specify one it culls clockwise primitives)

    Your triangle vertices order was counter clockwise, so the primitive was not culled.

    To solve it, two solutions:

    • Change your indices order :

        std::vector<unsigned int> indices = { 0, 3, 1, 3, 2, 1 };
      
    • Disable culling in the rasterizer state :

    First create one Rasterizer Description

    D3D11_RASTERIZER_DESC raster_desc;
    raster_desc.FillMode = D3D11_FILL_SOLID;
    raster_desc.CullMode= D3D11_CULL_NONE;
    raster_desc.FrontCounterClockwise = false;
    raster_desc.DepthBias = 0;
    raster_desc.DepthBiasClamp= 0.0f;
    raster_desc.SlopeScaledDepthBias= 0.0f;
    raster_desc.DepthClipEnable= true;
    raster_desc.ScissorEnable= false;
    raster_desc.MultisampleEnable= false;
    raster_desc.AntialiasedLineEnable= false;
    

    Then create a rasterizer state using your device:

       ID3D11RasterizerState* raster_state;
       HRESULT hr =d3dDevice->CreateRasterizerState(&raster_desc, &raster_state);
    

    Before the draw, assign your rasterizer state to your context:

       d3dContext->RSSetState(raster_state);