How can I manually sample the depth buffer / Z-Buffer?


another noob question here. Can I sample the depth buffer at the current position manually in the HLSL code? Speaking obviously about the depth buffer created in the RenderTarget i am writing to.

Or would I need to create another RT with a Depth buffer which i then sample as a texture?

I didn’t find anything useful on Google…

Thanks :slight_smile:

To sample the depth buffer directly you need to create a shader resource view. See Unfortunately, these features are not exposed through the XNA API.

Yes, in XNA/MonoGame you can manually output the depth into a render target and sample from there.

You could also use depth stencil comparison functions and ZEnable = true; to avoid the cost of the rendertarget (Memory and pass if not using MRT)

or compute the depth in the vertex shader your effect uses one.

Thanks for the info, I guess I’ll use another RT in the lighting pass to output depth as well.

Since you guys seem knowledgable - can I pass structs to my GPU registers? I know it doesn’t work directly in XNA, does it work in Mono?

You mean using a struct in C# and mapping it to a GPU constant buffer? No. (The MonoGame API is still the same as in XNA. Maybe in a future release of MonoGame…)

I don’t know about passing a struct directly as effect parameter, but if this struct contains only float values, you could pass them as one vector4 or a matrix :wink: (I do that in my engine, to minimize the cost of setting effectparameter)

Ok thanks again, the heroes I need