How can I use DirectX write depth buffer to 1 with certain stencil test pass area

Started by
0 comments, last by MJP 8 years, 5 months ago

I am doing 3D rendering the recursive reflections between multiple mirrors. But before that, drawing only one mirror's reflection has a problem with depth buffer handling:

Here is the steps:

Drawing the whole scene objects except the mirror. The problem maker is the floor. The mirror is in the middle of the floor, which means there is a part of the floor behind the mirror. So when drawing the mirror's reflection, there is already drawn floor behind the mirror. When I draw the reflections, some part of the floor is not covered by the later drawn reflections due to less Z value(they should have the same Z value but the imprecisely calculation floating point Z may give the wrong number). So I have to clear the depth value in the reflection part. To be precisely, only clear the non-occluded mirror part depth value to 1. It is easy to do this with stencil buffer. In OpenGL, it is easy to do this:


gLColorMask(0,0,0,0);

gLDepthRange(1,1); 

gLDepthFunc(GL_ALWAYS);

gLStencilFunc(GL_EQUAL, 1, ~0); 

glStencilOp(GL_KEEP, GL_KEEP,GL_KEEP);

drawreflections();

then draw only the non-occluded mirror into depth buffer with value 1.

But In DirectX11, I found no way to only draw into depth buffer with specified depth value when self defined stencil test passed. Is there? (I hope I just missed it)

So I walked around this: first use stencil to record the non-occluded mirror part with stencil value 1. Then clear the whole depth buffer to the highest value--1. Then draw the reflections only pass the stencil test. And finally draw the mirror also according the stencil test. Since the stencil test keep the original depth order between the already drawn objects and mirror. Even the depth information lost after I clear the whole screen depth buffer, the stencil buffer keep the sort of depth information: all the stencil value = 1 means non-occluded mirror part, so the occluded part will not be drawn.

So in this walk around way, it works fine. Even with 2+ mirrors without reflecting the reflections, it works well. But when I approach to the 2+ mirrors recursive reflections, the clear whole depth buffer method does not work. In this way, I have to create a new depth buffer in every new level of the recursion to save old depth values. Only one depth buffer does not work this way.

Do you guys know about set depth value to specific area according to stencil test in DX11?

What do you do when render the unconstrained reflection?

According to NVIDIA's Mark J. Kilgard's 15+ years ago's article(Improving Shadows and reflections via the stencil buffer), he drew the mirror first that is because he can use openGL clear the partial depth buffer as 1 according to the stencil record. I have to draw all objects first except the current mirror, then use stencil to record the mirror and depth relations. But use this method to do recursion, the entire depth information will be cleared from previous level, so it does not work unless you create a new depth/stencil buffer in each recursion level.

?

Advertisement
If I understand you're usage glDepthRange correctly, you're basically trying to force a depth value of 1.0 to get written into the depth buffer regardless of the actual depth of the polygon. In D3D11, this functionality is part of the D3D11_VIEWPORT structure. If you set MinDepth and MaxDepth to 1.0, then the pixel depth value will always get mapped to 1.0 during the viewport transform.

This topic is closed to new replies.

Advertisement