• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
~Helgon

Counting the depth complexity / Get the stencilRef

3 posts in this topic

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)

[CODE]
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;
[/CODE]


and my draw depth_stencil_desc

[CODE]
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;
[/CODE]

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)

[CODE]
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
[/CODE]

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 Edited by ~Helgon
0

Share this post


Link to post
Share on other sites
Hi Helgon,

What do you want to count? Both back and front faces? Probably just front faces, right? If so, try to set:
[CODE]
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;
[/CODE]

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:
[CODE]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);
}[/CODE]

Hope that helps! [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
Best regards! Edited by Tsus
1

Share this post


Link to post
Share on other sites
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

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

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

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
0

Share this post


Link to post
Share on other sites
Hi Helgon,

[quote name='~Helgon' timestamp='1353860135' post='5003954']
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?
[/quote]
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. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

[quote name='~Helgon' timestamp='1353860135' post='5003954']
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.
[/quote]
Counting the number of fragments that pass the depth test is done by:
[CODE]
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;
[/CODE]

Counting the number of fragments that fail the depth test is done by:
[CODE]
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;
[/CODE]

[quote name='~Helgon' timestamp='1353860135' post='5003954']
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.
[/quote]
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!
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0