Counting the depth complexity / Get the stencilRef

Started by
2 comments, last by Tsus 11 years, 4 months ago
Hi,

i have following problem. I want to count my depth complexity and then render it with different colors

my approach was this depth_stencil_desc (but im not sure if it even is right)


D3D11_DEPTH_STENCIL_DESC colorDepth;
colorDepth.DepthEnable = true;
colorDepth.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO;
colorDepth.DepthFunc = D3D11_COMPARISON_LESS;

colorDepth.StencilEnable = true;
colorDepth.StencilReadMask = 0xff;
colorDepth.StencilWriteMask = 0xff;

colorDepth.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
colorDepth.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
colorDepth.FrontFace.StencilPassOp = D3D11_STENCIL_OP_INCR;
colorDepth.FrontFace.StencilFunc = D3D11_COMPARISON_EQUAL;

colorDepth.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
colorDepth.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
colorDepth.BackFace.StencilPassOp = D3D11_STENCIL_OP_INCR;
colorDepth.BackFace.StencilFunc = D3D11_COMPARISON_EQUAL;



and my draw depth_stencil_desc


D3D11_DEPTH_STENCIL_DESC drawDepth;
drawDepth.DepthEnable = true;
drawDepth.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
drawDepth.DepthFunc = D3D11_COMPARISON_LESS;
drawDepth.StencilEnable = true;

drawDepth.StencilReadMask = 0xff;
drawDepth.StencilWriteMask = 0xff;


drawDepth.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
drawDepth.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
drawDepth.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
drawDepth.FrontFace.StencilFunc = D3D11_COMPARISON_EQUAL;

drawDepth.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
drawDepth.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
drawDepth.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
drawDepth.BackFace.StencilFunc = D3D11_COMPARISON_EQUAL;


my idea was that always when two pixel are rendered on the same location the stencilRef increases

in the end this kinda works (somehow)

first i draw my whole scene with this depthStencil

then i redraw all again but all in a for loop (i think that this is not the right way, but it was the only idea how to solve it)


landAndWavesTech->GetDesc( &techDesc );
for(UINT p = 0; p < techDesc.Passes; ++p)
{
for(UINT i = 0; i < 5; i++)
{

md3dImmediateContext->IASetVertexBuffers(0, 1, &amp;mBoxVB, &amp;stride, &amp;offset);
md3dImmediateContext->IASetIndexBuffer(mBoxIB, DXGI_FORMAT_R32_UINT, 0);

Effects::BasicFX->SetMaterial(depth[i-1]);


md3dImmediateContext->OMSetDepthStencilState(RenderStates::DrawDepthDSS, i); // << here i try to go through all stencil refs


quesitons:

1) how can i get the count of how many stencilrefs i have? (how often did the stencilref increment ?)
2) is this the right way to do it - loop through all stencil refs? seems not very performant
3) how can i access the stencil ref if i implement the depth_stencil_state via hlsl?
4) it works fine but it doesnt include the water, any idea why?

look at the attach to understand what i mean

hope that someone can help :)

regards helgon

from time to time i find time

Advertisement
Hi Helgon,

What do you want to count? Both back and front faces? Probably just front faces, right? If so, try to set:

colorDepth.DepthEnable = true;
colorDepth.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO; // disable writes
colorDepth.DepthFunc = D3D11_COMPARISON_ALWAYS;
colorDepth.StencilEnable = true;
colorDepth.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK; // enable
colorDepth.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK; // enable
colorDepth.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
colorDepth.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
colorDepth.FrontFace.StencilPassOp = D3D11_STENCIL_OP_INCR; // also count fragments that didn't pass the depth test
colorDepth.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
colorDepth.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; // set to INCR, if you want to count both, back and front faces
colorDepth.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; // set to INCR, if you want to count both, back and front faces. (counts fragments that didn't pass the depth test)
colorDepth.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
colorDepth.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;


StencilFailOp is executed if the stencil test fails (it will never, since we set StencilFunc to always). StencilDepthFailOp happens if the stencil test passes and the depth test fails. The StencilPassOp operation is used when both tests pass. On frontfaces we increment, on backfaces we do nothing.

If you want to read the stencil value in the shader, then you need three depth stencil views, all viewing the same texture.
The first is used for rendering into it. Therefore, use the format DXGI_FORMAT_D24_UNORM_S8_UINT. Bind it to the OM, when you render the scene (OMSetRenderTargets).
The second SRV is bound to the OM, when you want to read the stencil buffer. Again, use the format DXGI_FORMAT_D24_UNORM_S8_UINT, but additionally set the flag D3D11_DSV_READ_ONLY_STENCIL. (Additionally, set a depthStencilState that disables stencil write operations, i.e., StencilWriteMask = 0)
The third SRV is bound to the PS (PSSetShaderResources). Now, use the format DXGI_FORMAT_X24_TYPELESS_G8_UINT.
In the shader code it is used like this:
Texture2D<uint> txStencil : register (t0); // set the corresponding register
float4 PSStencil(float4 pos: SV_Position) : SV_Target
{
uint stencil = txStencil.Load(int3(pos.xy, 0));

// debug output
if (stencil == 1) return float4(0,1,0,1);
else return float4(0,0,0,0);
}


Hope that helps! smile.png
Best regards!
Hey, thank your very much for the detailed answer.

Now it works perfect. The shader solution is still a little bit to sophisticated for me. so i just draw the whole scene with addtive blending and added a little bit of color for each rendered pixel (the colors accumulate and so i get a feeling of the depth) works fine enough for me.

But i still have a question:

How can i count the depth complexity. Of course i understand that if the depthFails or the pass it increments by one, but how can i directly access this value?
As i said my approach was to just for loop


for(UINT i = 0; i < 5; i++)
{
Effects::BasicFX->SetMaterial(depth[i-1]);

md3dImmediateContext->OMSetDepthStencilState(RenderStates::DrawDepthDSS, i); // << here


but this just works because i know how many depths i circa have. but if its get more complex it just can be 2 or 200 and that would not be performant, so how do i directly get the count of stencilRefs?

A exersise in the book is also: How can i count the number of pixels that pass the depth test and how can i count the number of pixels that fail the depth test.

thanks again for your answer, regards helgon

from time to time i find time

Hi Helgon,


How can i count the depth complexity. Of course i understand that if the depthFails or the pass it increments by one, but how can i directly access this value?
[...] so how do i directly get the count of stencilRefs?

If you want to access the count on the GPU (in a shader), then use the three DSVs (depth stencil views), as I have described in the previous post. The piece of shader code shows, how to access the stencil value, i.e., the “depth complexity”.
If you have any further questions, don't hestitate to ask. smile.png


A exersise in the book is also: How can i count the number of pixels that pass the depth test and how can i count the number of pixels that fail the depth test.

Counting the number of fragments that pass the depth test is done by:

colorDepth.DepthEnable = true;
colorDepth.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO;
colorDepth.DepthFunc = D3D11_COMPARISON_ALWAYS;
colorDepth.StencilEnable = true;
colorDepth.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK;
colorDepth.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK;
colorDepth.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
colorDepth.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
colorDepth.FrontFace.StencilPassOp = D3D11_STENCIL_OP_INCR; // only increment if depth and stencil test pass (stencil passes always)
colorDepth.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; // stencil test passes always
colorDepth.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
colorDepth.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
colorDepth.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
colorDepth.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;


Counting the number of fragments that fail the depth test is done by:

colorDepth.DepthEnable = true;
colorDepth.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO;
colorDepth.DepthFunc = D3D11_COMPARISON_ALWAYS;
colorDepth.StencilEnable = true;
colorDepth.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK;
colorDepth.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK;
colorDepth.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
colorDepth.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR; // only increment if depth test fails and stencil test passes (passes always)
colorDepth.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
colorDepth.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; // stencil test passes always
colorDepth.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
colorDepth.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
colorDepth.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
colorDepth.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;



Now it works perfect. The shader solution is still a little bit to sophisticated for me. so i just draw the whole scene with addtive blending and added a little bit of color for each rendered pixel (the colors accumulate and so i get a feeling of the depth) works fine enough for me.

As you suggested, you can use alpha blending for the counting as well, by outputting a 1/255.0 in the pixel shader.
Disabling the depth test gives you the total depth complexity (all fragments).
Enabling the depth test gives you the number of fragments that passed the depth test.
The difference of both gives you the number of fragments that failed the depth test. (Requires two passes, though. If you want to get this number in a single pass, then you should probably use the stencil buffer).

Or do you want to access the stencil value on the CPU? In that case, copy the texture into a staging texture (deviceContext->CopyResource(..)) and use deviceContext->Map(..) to get access to the content. In case you used the stencil buffer, cast the data to an uint4 array (those are the pixels row-by-row). The fourth component should be the stencil value. Otherwise cast it to whatever format you used in your render target.

Best regards!

This topic is closed to new replies.

Advertisement