Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm rendering to a depth map in order to use it as a shader resource view, but when I sample the depth map in my shader, the red component has a value of 1 while all other channels have a value of 0.

The Texture2D I use to create the DepthStencilView is bound with the D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE flags, the DepthStencilView has the DXGI_FORMAT_D32_FLOAT format, and the ShaderResourceView's format is D3D11_SRV_DIMENSION_TEXTURE2D.

I'm setting the depth map render target, then i'm drawing my scene, and once that is done, I'm the back buffer render target and depth stencil are set on the output merger, and I'm using the depth map shader resource view as a texture in my shader, but the depth value in the red channel is constantly 1. I'm not getting any runtime errors from D3D, and no compile time warning or anything.

I'm not sure what I'm missing here at all. I have the impression the depth value is always being set to 1.

I have not set any depth/stencil states, and AFAICT depth writing is enabled by default. The geometry is being rendered correctly so I'm pretty sure depth writing is enabled.

The device is created with the appropriate debug flags;

#if defined(DEBUG) || defined(_DEBUG)
    deviceFlags |= D3D11_CREATE_DEVICE_DEBUG | D3D11_RLDO_DETAIL;
#endif

This is how I create my depth map. I've omitted error checking for the sake of brevity

D3D11_TEXTURE2D_DESC td;

td.Width     = width;
td.Height    = height;
td.MipLevels = 1;
td.ArraySize = 1;
td.Format    = DXGI_FORMAT_R32_TYPELESS;
td.SampleDesc.Count   = 1;  
td.SampleDesc.Quality = 0;  
td.Usage          = D3D11_USAGE_DEFAULT;
td.BindFlags      = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
td.CPUAccessFlags = 0; 
td.MiscFlags      = 0;
_device->CreateTexture2D(&texDesc, 0, &this->_depthMap);

D3D11_DEPTH_STENCIL_VIEW_DESC dsvd;
ZeroMemory(&dsvd, sizeof(dsvd));
dsvd.Format = DXGI_FORMAT_D32_FLOAT;
dsvd.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
dsvd.Texture2D.MipSlice = 0;
_device->CreateDepthStencilView(this->_depthMap, &dsvd, &this->_dmapDSV);

D3D11_SHADER_RESOURCE_VIEW_DESC srvd;
srvd.Format = DXGI_FORMAT_R32_FLOAT;
srvd.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvd.Texture2D.MipLevels = texDesc.MipLevels;
srvd.Texture2D.MostDetailedMip = 0;
_device->CreateShaderResourceView(this->_depthMap, &srvd, &this->_dmapSRV);
share|improve this question
1  
1 is a typical clear depth value -- are you sure that when your scene is rendered, you've actually got depth writing enabled? Can you post some code? And did you make sure to create the D3D11 device with the "debug" flag to get all the debug output? –  Josh Petrie Nov 15 '11 at 16:44
    
Don't forget to use PIX debugger, that could help to debug this kind of problem –  xoofx Nov 17 '11 at 1:46
    
I've used PIX to check the values in the buffer, same problem, all texel have a value of 1. –  dotminic Nov 24 '11 at 20:00

1 Answer 1

Depth-writing being enabled is definitely the first thing to check. If I had $5 for every time I thought something was definitely enabled by default...

So try explicitly enabling it just to be absolutely certain. Also double-check your depth range and ensure that it's not something like minz 1 and maxz 1 (which would give the same result even with depth writing enabled).

share|improve this answer
    
Depth-writing is enabled, and the min/max depth values for the viewport are 0/1. –  dotminic Nov 15 '11 at 20:35
    
Wow good call about min/max z, never would have thought to look for that, but they were both in fact set to 1. –  eodabash Dec 17 '13 at 6:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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