1
\$\begingroup\$

I imagine this is a problem that gets asked quite often, but reading up on it I can't seem to get a definitive answer on how to solve it.

I've got two HLSL shaders (I've simplified them in the example to just output a colour) that require RenderTargets. For example Blur shaders.

There are two balls spinning around a central point. As they spin, one passes between the other and the camera occluding the other ball and vice versa.

Correct Occlusion

If I render them both to the same rendertarget (using the same technique) they obviously share a depth buffer, and the depth testing happens correctly.

However, when I need to use different techniques to render each ball seperately (one ball to one rendertarget and the other ball to a different) and then try to composite them depth testing fails and they are just rendered on top of each other.

Incorrect

Attach is the code I'm using. I've simplified it down a lot, but it indicates the problem I'm having.

How do I go about rendering two different objects, using two different techniques to two different rendertargets and then composite them?

DepthTesting.cs

effect.fx

\$\endgroup\$
  • \$\begingroup\$ The depth buffer is disabled by default, do you enable it anywhere? stackoverflow.com/questions/3763475/… \$\endgroup\$ – ClassicThunder Dec 28 '13 at 22:00
  • \$\begingroup\$ I'm pretty sure the depth buffer is turned on - any idea how I would check? I'm calling GraphicsDevice.DepthStencilState = new DepthStencilState() { DepthBufferEnable = true, DepthBufferWriteEnable = true }; at the start of my draw method. \$\endgroup\$ – Chris Bampton Dec 28 '13 at 22:41
  • \$\begingroup\$ check the value of "GraphicsDevice.DepthStencilState.DepthBufferEnable" using the debugger. As mentioned it is off be default, so if you didn;t do it explicitly its off. \$\endgroup\$ – ClassicThunder Dec 28 '13 at 22:46
  • \$\begingroup\$ I've checked whilst debugging and Depth Testing IS enabled. \$\endgroup\$ – Chris Bampton Dec 29 '13 at 2:58
1
\$\begingroup\$

I ended up solving this problem by ordering the SpriteBatch draw calls by the distance from the camera to the object being drawn. This, combined with depth testing on the SpriteBatch seems to work.

\$\endgroup\$

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.