Jump to content
  • Advertisement
Sign in to follow this  

ZBuffer trouble :/

This topic is 2401 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


I am using Direct3D 11 and are having som problem with the ZBuffer buffer..
What I want to do is to disable the Z read, and enable the Z write..
This is because I am doing a game that render huge amounts of terrain and I want certain parts (quads) of the terrain not to be rendered.
I solved this by pre writing a quad to the z buffer with a depth value that is just a little bit lower than the terrain was going to check..
After this is done I render the terrain and get a open space just where I want it to be..
My next goal was to render to the zbuffer once again with exactly the same quads but this time set the depth value to 1. By doing this I should be able to render everything I want in this open area...

This may sound alittle bit overkill, but because the terrain is rendered by using fractals and there are a couple of million triangles being rendered... I would save alot of fps by not having to insert some "if" commands in the shader code..

The only thing that doesn't work is when I disable the Z read, and enable the Z write.. I get some error like behavior on the screen as seen below. The color I finally try to render over the open space is (1.0f, 0.0f, 1.0f, 1.0f).. But all I get this...

This is the code for the stencil state I use when trying to do this:

// Create a depth stencil state which turns off the Z buffer reading
ZeroMemory(&stencilDesc, sizeof(stencilDesc));
stencilDesc.DepthEnable = true;
stencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
stencilDesc.DepthFunc = D3D11_COMPARISON_ALWAYS;
stencilDesc.StencilEnable = true;
stencilDesc.StencilReadMask = 0xFF;
stencilDesc.StencilWriteMask = 0xFF;
stencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
stencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
stencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
stencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
stencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
stencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
stencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
stencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
hr = g_pd3dDevice->CreateDepthStencilState(&stencilDesc, &m_pZReadDisabledStencilState);
return hr;

This is the code when enabling it:

g_pImmediateContext->OMSetDepthStencilState(m_pDepthEnableStencilState, 0);

And this is the shader code (All shaders are using the same vertexshader so that the vertex positions always will be the same)..

Code for render to the z buffer so that there will be an open space in the terrain (working) (shader 1):

float PS_Pre( PS_INPUT input) : SV_Depth
return (input.ZDepth.x / input.ZDepth.y) - 0.000001f;

Code for render to the z buffer so that other objects can be rendered in the same area (not working)(shader 2):

float PS_Post( PS_INPUT input) : SV_Depth
return 1.0f;

And final code that renders a quad in this exact same area (shader 3):

float PS( PS_INPUT input) : SV_Target
return float4(1.0f, 0.0f, 1.0f, 1.0f);

Here is a screenshot when only using shader 1: (as expected)

Here is a screenshot when only using shader 1 and 2: (not as expected)

Here is a screenshot when using shader 1, 2 and 3: (not as expected, but the color changed non the less)

Any help would be appreciated.. I usually manage to solve these problems myself, but this time it seems like there is something seriously wrong :S

Share this post

Link to post
Share on other sites
you say:

[color=#880000]// Create a depth stencil state which turns off the Z buffer reading

which means you don't want to read the zbuffer?

[color=#000000]stencilDesc[color=#666600].[color=#660066]DepthEnable[color=#000000] [color=#666600]=[color=#000000] [color=#000088]true[color=#666600];

You set the depth testing to true, which means you will read the depth (z) value of pixels. have you tried setting this to false instead?

Share this post

Link to post
Share on other sites
Thanks for your reply..

Yes I have tried doing that but the results are still the same...
I turn of the Z reading by setting
stencilDesc.DepthFunc = D3D11_COMPARISON_ALWAYS;

So that it always write over the previous z value..

But anyway, I seem to have found the problem..
I didn't set float 4 to shader 3 where I am rendering to an argb texture :S
So there became an truncation error..

Typically there are always some minor change needed to be done to fix the big problems..

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!