Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Question regarding Stencil Buffer in DirectX 9


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
5 replies to this topic

#1 N00bKefka   Members   -  Reputation: 120

Like
0Likes
Like

Posted 19 July 2012 - 01:14 AM

I heard I can specify the stencil mask using a black and white image file. How do I do that and how do I specify the area of maksing?

Sponsor:

#2 Nik02   Crossbones+   -  Reputation: 2883

Like
2Likes
Like

Posted 19 July 2012 - 04:45 AM

Enable the stencil write render state, render your mask texture and discard those pixels that you don't want to write in the stencil buffer by using the clip() pixel shader function. The pixel shader logic specifies the "area of masking".

In your case, you'd sample the mask texture, and feed its luminance - 0.5f (where 0.5 is the gray treshold) to the clip function as a parameter. The clip function discards the currently processed pixel if its parameter is less than zero, and effectively prevents it from being written to color targets, depth buffer or the stencil buffer.

Alternatively (in case you don't want to use pixel shader), enable alpha testing and set the alpha test treshold to 0.5. Then output the texture luminance (or alpha, if you have an actual alpha channel in the texture) as alpha in the texture stage cascade. Alpha test rejection effectively establishes the same thing as the clip() function.

Edited by Nik02, 19 July 2012 - 05:06 AM.

Niko Suni


#3 N00bKefka   Members   -  Reputation: 120

Like
0Likes
Like

Posted 20 July 2012 - 12:41 AM

I am not using lighting... I need the stencil buffer for minimap.

#4 Nik02   Crossbones+   -  Reputation: 2883

Like
0Likes
Like

Posted 20 July 2012 - 08:25 AM

Neither of the techniques I described are in any way related to lighting.

In order to write something to the stencil buffer (without card-specific hacks), you need to render something, because the standard stencil buffer formats are not lockable and therefore you can't get direct access to their surface memory from within your program. Rendering can be thought as an indirect write access.

Edited by Nik02, 20 July 2012 - 08:27 AM.

Niko Suni


#5 N00bKefka   Members   -  Reputation: 120

Like
0Likes
Like

Posted 21 July 2012 - 01:04 AM

Sorry my bad. When I saw the word Luminance, I thought there's some relation in lighting. Now this sentence make me even more confuse.

"Rendering can be thought as an indirect write access."

#6 Nik02   Crossbones+   -  Reputation: 2883

Like
1Likes
Like

Posted 21 July 2012 - 05:49 AM

Rendering something is the only standard way to write an image (such as a mask) to a stencil buffer, because you can't access the stencil buffer memory by locking it.

Some hardware and drivers do allow locking of stencil buffers when they are allocated in a special way, but in D3D9 this is always done via vendor specific extensions (thus forcing you to write custom code for each vendor such as AMD and NVidia). In contrast, the technique(s) I outlined only require that the target GPU supports pixel shaders or alpha testing (in addition to obviously supporting stencil buffers). Any GPU less than about a twelve years old can do that, in a standard way.

When I said "luminance", I meant the white level of the texture you want to use as the mask image. If you clip by (luminance - 0.5f), all pixels that have less than 50% white on the mask image will not be rendered to either the color targets or the depth/stencil buffer. The clip simply provides the means to control which pixels get written to the stencil buffer - after you've filled it, you can use it to mask subsequent drawing operations.

Note that you could also use an alpha mask texture directly instead of using a stencil buffer.

Niko Suni





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