Disabling depth buffer writing and using it as SRV

Started by
2 comments, last by KaiserJohan 9 years, 5 months ago

Hello,

I am using deferred rendering, and I want to reconstruct view-space positions from depth so I bind it as an SRV, however at the same time I need to have the same depth buffer texture bound as a depth stencil view to use it for depth comparison.

In short, this:


// disable further depth writing
context->OMSetDepthStencilState(mDepthStencilState, 0);

context->PSSetShaderResources(DX11Texture::SHADER_TEXTURE_SLOT_DEPTH, 1, &mDepthSRV.p);
context->OMSetRenderTargets(1, &mBackbufferRTV.p, mDepthStencilView);

Upon which visual studio goes nuts and forces OMSetRenderTargets() to use NULL as DSV since the same texture is already bound as a shader resource, to prevent it from being written and read at the same time.

But I already disabled writing by setting my depth stencil state!

The only solution I've found is to copy the depth texture into a separate texture and use that as the SRV. I'm not sure how expensive operation this is however and maybe it is totally unnecessary because I've missed something; is there a more clean solution available?

Advertisement

Create a depth stencil view with read_only flag - this allows you to perform depth testing (can't write though) and read the depth buffer at the same time.

Cheers!

[edit] yes, you may copy the depth buffer to another texture too - I do it for other needs - and I can't say that it is expensive operation.

Cheers!

Note that using the read only flag limits you to D3D11 hardware. For 10 and 10.1 hardware you'll have to copy the depth buffer.

I'm not quite sure why it's so limiting, as you can do it all in D3D9 with vendor specific code.

So two depth stencil views - one for the geometry pass (writing to gbuffer), and then the read_only for all shading operations? Will try it.

This topic is closed to new replies.

Advertisement