سؤال

I'm trying to setup a Z-buffer with DirectX 11 but it's not working as expected. Without the Z-Buffer it shows the further objects on top of the closer ones, as you'd expect. With the buffer it shows a blank screen then flickers through bits when moving it around as shown in this video.

Here's my Z Buffer initialization.

// Create a Z buffer texture
D3D11_TEXTURE2D_DESC tex2dDesc;
ZeroMemory(&tex2dDesc, sizeof(tex2dDesc));

tex2dDesc.Width = width;
tex2dDesc.Height = height;
tex2dDesc.ArraySize = 1;
tex2dDesc.MipLevels = 1;
tex2dDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
tex2dDesc.SampleDesc.Count = sd.SampleDesc.Count;
tex2dDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
tex2dDesc.Usage = D3D11_USAGE_DEFAULT;

ID3D11Texture2D *pZBufferTexture;
hr = g_pD3DDevice->CreateTexture2D(&tex2dDesc, NULL, &pZBufferTexture);

if(FAILED(hr)) return hr;

// Create the Z buffer
D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc;
ZeroMemory(&dsvDesc, sizeof(dsvDesc));

dsvDesc.Format = tex2dDesc.Format;
dsvDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;

g_pD3DDevice->CreateDepthStencilView(pZBufferTexture, &dsvDesc, &g_pZBuffer);
pZBufferTexture->Release();


// Set the render target view
g_pImmediateContext->OMSetRenderTargets(1, &g_pBackBufferRTView, g_pZBuffer);

Here's what runs in the rendering.

g_pImmediateContext->ClearRenderTargetView(g_pBackBufferRTView, g_clear_colour);

g_pImmediateContext->ClearDepthStencilView
    (g_pZBuffer, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);

I'm still much of a beginner when it comes to DirectX 11 so any help would be greatly appreciated. If more code is required I can provide.

هل كانت مفيدة؟

المحلول

You should add the D3D11_CREATE_DEVICE_DEBUG flag at device creation. If something is wrong in the frame, you will find a verbose clue on the why in the output log.

also, do not create a stencil buffer if you do not use stencil. Modern GPU do not store the information in the depth buffer anyway, a D32 format will not waste 8 bits of precision for nothing.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top