Nothing rendered when DepthBuffer enabled

Started by
4 comments, last by Kiba2014 9 years, 5 months ago

Hello,

I try to learn Direct3D 11 from scratch by reading some tutorials and I managed to draw a simple cube in a window. But if a DepthStencil-Buffer is set as a render target, no pixels are drawn (and there is no output from the D3D debug-layer).

Can you please have a look at the code in the attached zip-file (it contains a VS2013 project). The error can be reproduced by commenting out the line 110 in d3d_wrapper.h and uncommenting line 111.

What is wrong with the code? Thanks in advance!

Advertisement

I'm at work so unfortunately I can't download your code.

Are you clearing the depth buffer before using it for rendering?

Your near plane is bad (0 never works). Use something like 0.1f for near and e.g. 10.0f for far.

Thank you, that was it. Looked at the code for hours, what are the strategies to find such bugs?

Definitively PIX or the graphics debugger (if you got a visual studio version coming with one). Alternatives are Cryteks RenderDoc, there are also tools from the different hardware vendors. Here you can inspect if you got the right buffers bound, what states are active, look at render targets, depth buffers, debug shaders if you must, etc. etc.

In this case a look at the the transformed vertices (in PIX post-VS or GS tab) and/or at the depth buffer would have pointed you in the right direction.

This is a nasty beginner bug, so I'll elaborate a bit. If you look at the math of the projection matrix and set znear = 0 it will end up with something like this:

sx 0 0 0
0 sy 0 0
0 0 1 1
0 0 0 0

So after projection, your clip-space vertices end up with w == z, after the perspective divide z == 1 (normalized device coordinates, this is now your vertex's depth value). So, all your vertices end up at the far plane. Since you don't explicitly set a depth stencil state, the default is used. This uses a depth comparison function of D3D11_COMPARISON_LESS and since your depth buffer is cleared to 1, well, guess what happens ;)

Edit: Wait, it's even worse, with this formula sx and sy end up being 0, too. So all your vertices end up at (0,0,1) tongue.png. I should have linked to this function instead. Sorry about the confusion.

Thank you for the explanation. I will try the debuggers you mentioned.

This topic is closed to new replies.

Advertisement