4
\$\begingroup\$

I need to read depth buffer from GPU and write it to texture. How this can be done? Here is how texture for depth buffer is created:

depthBufferDesc.Width = screenWidth;
depthBufferDesc.Height = screenHeight;
depthBufferDesc.MipLevels = 1;
depthBufferDesc.ArraySize = 1;
depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthBufferDesc.SampleDesc.Count = 1;
depthBufferDesc.SampleDesc.Quality = 0;
depthBufferDesc.Usage = D3D10_USAGE_DEFAULT;
depthBufferDesc.BindFlags = D3D10_BIND_DEPTH_STENCIL;
depthBufferDesc.CPUAccessFlags = 0;
depthBufferDesc.MiscFlags = 0;

m_device->CreateTexture2D(&depthBufferDesc, NULL, m_depthStencilBuffer);

Also, I've got another question: is it possible to bind depth buffer texture as sampler to the pixel shader?

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

You bind that texture to a depth stencil view and also a shader resource view to sample from it in another shader.

You need to add D3D10_BIND_SHADER_RESOURCE to the depthBufferDesc.BindFlags field.

\$\endgroup\$
2
  • \$\begingroup\$ I have following settings for stencil depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; and this how I try to obtain resource D3D10_SHADER_RESOURCE_VIEW_DESC srDesc; srDesc.Format = DXGI_FORMAT_R32_FLOAT; srDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D; srDesc.Texture2D.MostDetailedMip = 0; srDesc.Texture2D.MipLevels = 1; ID3D10ShaderResourceView* Resource; m_device->CreateShaderResourceView( m_depthStencilBuffer, &srDesc, &Resource ); \$\endgroup\$ – innochenti Mar 18 '12 at 18:30
  • \$\begingroup\$ You need to make sure that your formats match up - in this case use DXGI_FORMAT_R24G8_TYPELESS for the SRV desc. \$\endgroup\$ – Maximus Minimus Jun 16 '12 at 19:45

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.