0
\$\begingroup\$

I'm currently working on a game for university in DirectX 11.

I have tried to add in a depth buffer to my project, but since adding it in none of my objects show.

//Describe the Depth/Stencil Buffer
D3D11_TEXTURE2D_DESC db;
db.Width = Width;
db.Height = Height;
db.MipLevels = 1;
db.ArraySize = 1;
db.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
db.SampleDesc.Count = 1;
db.SampleDesc.Quality = 0;
db.Usage = D3D11_USAGE_DEFAULT;
db.BindFlags = D3D11_BIND_DEPTH_STENCIL;
db.CPUAccessFlags = 0;
db.MiscFlags = 0;

//Create Depth View
hr = g_pd3dDevice->CreateTexture2D(&db, NULL, &g_depthBuffer);
if (FAILED(hr)) {
    MessageBox(NULL, L"Error.", L"Error", MB_OK);
}
g_pd3dDevice->CreateDepthStencilView(g_depthBuffer, NULL, &g_depthView);
//g_pd3dDevice->CreateDepthStencilState(&db, &g_depthStencil); //When uncommented, the '&db' shows the following error. 
//argument of type "D3D11_TEXTURE2D_DESC*" is incompatible with parameter of type "const D3D11_DEPTH_STENCIL_DESC*"


//Set Render Target
g_pImmediateContext->OMSetRenderTargets( 1, &g_pRenderTargetView, g_depthView );

    void Render()
{
    //Update our time
    static float t = 0.0f;
    if ( g_driverType == D3D_DRIVER_TYPE_REFERENCE )
    {
        t += ( float )XM_PI * 0.0125f;
    }
    else
    {
        static ULONGLONG timeStart = 0;
        ULONGLONG timeCur = GetTickCount64();
        if (timeStart == 0)
            timeStart = timeCur;
        t = (timeCur - timeStart) / 1000.0f;
    }

    //
    // Animate the cube
    //
    //g_World = XMMatrixRotationY( t );

    //
    // Clear the back buffer
    //
    g_pImmediateContext->ClearRenderTargetView( g_pRenderTargetView, Colors::MidnightBlue );
    //g_pImmediateContext->ClearDepthStencilView( g_depthView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0 );

    //
    // Update variables
    //
    ConstantBuffer cb;
        //cb.mWorld = cube1;
    cb.mWorld = XMMatrixTranspose( cube1 );

    cb.mView = XMMatrixTranspose( g_View );
    cb.mProjection = XMMatrixTranspose( g_Projection );
    g_pImmediateContext->UpdateSubresource( g_pConstantBuffer, 0, nullptr, &cb, 0, 0 );

    //
    // Draws first square
    //

    g_pImmediateContext->VSSetShader(g_pVertexShader, nullptr, 0);
    g_pImmediateContext->VSSetConstantBuffers(0, 1, &g_pConstantBuffer);
    g_pImmediateContext->PSSetShader(g_pPixelShader, nullptr, 0);
    g_pImmediateContext->DrawIndexed(36, 0, 0); //36 vertices needed for 12 triangles in a triangle list


    ConstantBuffer cb2;
        //cb2.mWorld = cube2;
    cb2.mWorld = XMMatrixTranspose(cube2);

    cb2.mView = XMMatrixTranspose(g_View);
    cb2.mProjection = XMMatrixTranspose(g_Projection);
    g_pImmediateContext->UpdateSubresource(g_pConstantBuffer, 0, nullptr, &cb2, 0, 0);

//  g_pImmediateContext->VSSetShader(g_pVertexShader, nullptr, 0);
//  g_pImmediateContext->VSSetConstantBuffers(0, 1, &g_pConstantBuffer);
//  g_pImmediateContext->PSSetShader(g_pPixelShader, nullptr, 0);
    g_pImmediateContext->DrawIndexed(36, 0, 0);

    //cube1 = XMMatrixIdentity();

    //
    // Present our back buffer to our front buffer
    //
    g_pSwapChain->Present( 0, 0 );
}

void Update()
{
    rot += 0.0005f;
    if (rot > 6.26f)
        rot = 0.0f;

    cube1 = XMMatrixIdentity();

    XMVECTOR rotaxis = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
    Rotation = XMMatrixRotationAxis(rotaxis, rot);
    Translation = XMMatrixTranslation(1.0f, 1.0f, 0.0f);

    cube1 = Translation * Rotation;


    cube2 = XMMatrixIdentity();


    Rotation = XMMatrixRotationAxis(rotaxis, -rot);
    Scale = XMMatrixScaling(0.5f, 0.5f, 0.5f);

    cube2 = Scale;
}

From what someone else had mentioned, that I'm either not creating a D3D11DepthStencilState or not clearing the depth stencil view. I've tried to fix the problem, but no luck so far.

\$\endgroup\$
1
\$\begingroup\$

It is important how you create the depth stencil state and what values you clear the depth buffer to. And you should always clear the depth buffer at least once per frame if you are writing to it (for optimal hardware behaviour).

I would try setting a depth stencil state where Depth is disabled, that should set you back to working state (if your app works when no depth buffer is bound). Then you can enable the depth test/depth write and see where it goes wrong. Also take note of your DepthFunc. The depthFunc should be generally COMPARISON_LESS_EQUAL if you are clearing your depth buffer to 1.0f, and GREATER_EQUAL if you are clearing to 0.0f values.

Also, you projection matrix near and far planes matter. If nearPlane < farPlane, then you should clear depth buffer to 1.0f. If farPlane < nearPlane, then clear to 0.0f.

Also, always enable debug layer (D3D11_CREATE_DEVICE_DEBUG) when looking for DirectX bugs. And try to use a graphics debugger, like the one built into visual studio, RenderDoc, or Nvidia Nsight.

Good luck!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.