Sign in to follow this  

Using stencil operations

This topic is 2045 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 need someone to help me understand concepts around this topic.
I have read the documentation but not quite get it.

[quote]
D3DRS_STENCILFUNC
The comparison function is used to compare the reference value to a stencil buffer entry.
This comparison applies only to the bits in the reference value and stencil buffer entry that are set in the stencil mask (set by the D3DRS_STENCILMASK render state).
If TRUE, the stencil test passes.
[/quote]

Let me go step by step.

1.
First i use Clear function to clear BB & DS surface with stencil of 0.
Then i draw some object with:

[CODE]
D3DDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
D3DDevice->SetRenderState(D3DRS_STENCILREF, 1);
D3DDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
D3DDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
D3DDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
D3DDevice->SetRenderState(D3DRS_STENCILREF, 1);// this sets value of 1 to stencil where some object is drawn? Right?
sphere1->Draw();
[/CODE]

Then i draw some other object overlaping first one with:

[CODE]
D3DDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_EQUAL);
D3DDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
D3DDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
D3DDevice->SetRenderState(D3DRS_STENCILREF, 1);// value to compare with previous one? Also writes this one to stencil?
sphere2->Draw();
[/CODE]

2. Considering D3DRS_STENCILZFAIL, is this the case (where yellow color is drawn) in this picture?

[img]http://www.dodaj.rs/f/19/uf/tNlxuba/zfail.jpg[/img]

3. About D3DRS_STENCILMASK and D3DRS_STENCILWRITEMASK, how is this used? What is it "masking"/"write-masking"? I don't understand.
Can you please show me some example pseudo code?

Share this post


Link to post
Share on other sites
You're a bit vague about exactly what order the stuff in your drawing is drawn in, but I think you've got essentially the right idea. Just keep in mind that stencil ref doesn't do anything by itself, it just feeds the other operations. Your first block of states will set the stencil to 1 only where stencil and depth test pass. In the second block, you tell it to set the stencil to 1 again when both tests pass, which implies that stencil was already 1. So the second draw call will never change the stencil value. You also don't seem to be setting the stencil fail value, I don't know if that's intentional or not.

Check [url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb206123(v=vs.85).aspx"]this article[/url] and see if it helps clarify the masks. It basically does a bitwise operation on the stencil values to determine which bits are significant and which are ignored for the rest of the stencil operation.

Share this post


Link to post
Share on other sites
1.
Ok. I get this one:
[code]
(ref & mask) ComparisonOperation (oldValue & mask)
[/code]

But what about D3DRS_STENCILWRITEMASK? I don't see where it is used in this expression?
Docs says its default value is 0xFFFFFFFF same as mask.

2. From article you pointed:
[quote]
You can write your own formula for the value you want written into the stencil buffer as shown in the following example.
[code]
NewStencilBufferValue = (StencilBufferValue & ~StencilWriteMask) |
(StencilWriteMask & StencilOp(StencilBufferValue))
[/code]
[/quote]

How to set render states to get this formula?
This makes my brain hurt.[img]http://public.gamedev.net//public/style_emoticons/default/blink.png[/img]

3.
[quote]
You're a bit vague about exactly what order the stuff in your drawing is drawn in...
[/quote]

First i want to understand this a bit better as i wish later to do some decals/splatting on walls. So it means i first draw walls and then spheres to tag pixels that "intersect".

Share this post


Link to post
Share on other sites
Basically what it boils down to is the following code:
[code]
if (<Boolean epression setup by D3DRS_STENCILFUNC>)
{
//Here you specify what operation to do when the stencil test passes
stencilValue = <Equation setup by D3DRS_STENCILPASS>;
}
else
{
//Stencil test fails
stencilValue = <Equation setup by D3DRS_STENCILFAIL>;
}
[/code]

I explain this with a code example as this is what the hardware is actually executing and I find this easier to understand then just looking at the API calls.

The masks involved in these equations are only of interest if you want to only look at say the first few bits of the actual stencil value or write to those bits. So when these are set to 0xFFFFFFFF you will write and read from the whole stencil value, when set to 0xFF000000 you will only look at/write to the first 16 bits of the stencil value (this is a 32bit value, it gets converted to 8 bits later on I believe). Bear in mind that the read and write mask don't need to have the same value.

The actual stencil test equation is just a comparison and [url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb172517(v=vs.85).aspx"]D3DRS_STENCILFUNC[/url] sets which type of boolean expression is used to do the stencil test: "<", ">", "=", etc.
Then stencil [url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb172610(v=vs.85).aspx"]D3DRS_STENCIL[/url]FAIL or D3DRS_STENCILPASS defines what operation is done to the stencilValue: "keep", "+1", "-1", etc when the test passes or fails. Edited by NightCreature83

Share this post


Link to post
Share on other sites
I have another related problem. I tried to reder fullscreen quad for directional light, but i want to affect everything except sky, here is my setup (but its wrong):
[code]
// ---------------------- Draw g-buffer
setRenderState(D3DRS_STENCILENABLE, TRUE);
setRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
setRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);
setRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
setRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
setRenderState(D3DRS_STENCILREF, 5);
setRenderTarget(diffuseRT);
setRenderTarget(positionRT);
setRenderTarget(normalRT);
drawToGBuffer();
setRenderTarget(1, nullptr);
setRenderTarget(2, nullptr);

// ---------------------- Draw sky
setRenderState(D3DRS_STENCILFUNC, D3DCMP_GREATEREQUAL);
setRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);
setRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
setRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
setRenderState(D3DRS_STENCILREF, 3);
drawSkyBox();
setRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
setRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
setRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
drawCloudsLayers();
setRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
setRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
setRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO);

// ---------------------- Draw fsquad for dir light
setRenderState(D3DRS_STENCILFUNC, D3DCMP_EQUAL);
setRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);
setRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
setRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);// If equal to gbuffer (5) draw to it, else keep? But its not?
setRenderState(D3DRS_STENCILREF, 5);
setTexture(positionRT);
setTexture(normalRT);
drawFullscreenQuad();// dir light
[/code]

Please advice. Thanks for your time. Edited by belfegor

Share this post


Link to post
Share on other sites

This topic is 2045 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.

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