Jump to content

  • Log In with Google      Sign In   
  • Create Account

14 years ago on June 15th Gamedev.net was first launched! We want to thank all of you for being part of our community and hope the best years are ahead of us. Happy birthday Gamedev.net!

ZBuffer trouble :/


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Rasmus   Members   -  Reputation: 101

Like
0Likes
Like

Posted 16 April 2012 - 03:46 PM

Hello

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);
if(FAILED(hr))
{
  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)
Posted Image

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

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


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
------------------------------Check out me work at www.dmtribute.webs.com

Sponsor:

#2 iedoc   Members   -  Reputation: 499

Like
0Likes
Like

Posted 17 April 2012 - 02:57 AM

you say:

// Create a depth stencil state which turns off the Z buffer reading

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

stencilDesc.DepthEnable = true;

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?
Braynzar Soft - DirectX Lessons & Game Programming Resources!

#3 Rasmus   Members   -  Reputation: 101

Like
0Likes
Like

Posted 17 April 2012 - 03:24 AM

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..
------------------------------Check out me work at www.dmtribute.webs.com




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS