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)

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






