Jump to content

  • Log In with Google      Sign In   
  • Create Account

Depth Stencil Buffer


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 Rothgo Wooft   Members   -  Reputation: 100

Like
0Likes
Like

Posted 09 September 2009 - 10:49 AM

It appears my question is too basic to be in the FAQ, so here it is.. In XNA, how do I "merge" the depth and stencil buffers? eg, so that a sprite appears to have a depth in the 3D scene, obscured by things on top? The 3D depth buffer has a proper Z position, but the stencil buffer is just from 0 to 1. I've tried scaling the stencil Z position in to the 0 to 1 range, but this doesn't help. The two buffers work as separate things... I want to copy the data from the depth buffer to make up a stencil buffer before drawing my 2d sprites... Surely there is a function already in place to do this? Several hours looking haven't found it yet. Any nudge in the right direction is appriechiated. cheers, Dave.

Sponsor:

#2 Darg   Members   -  Reputation: 209

Like
0Likes
Like

Posted 09 September 2009 - 12:11 PM

This might help:

http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series2/Point_sprites.php

EDIT: Not sure how to get links to work right on these forums.

#3 Rothgo Wooft   Members   -  Reputation: 100

Like
0Likes
Like

Posted 09 September 2009 - 12:30 PM

Is there way without using point sprites?

I have a awful lot of them on the screen at once, and am guessing its quicker render using a normal 2D sprite batch? I could well be wrong...

If there's not much in it, I guess "Point Sprite" would be the magic word I was after, cheers!

I'll test it both ways and see what the effect is...

#4 Rothgo Wooft   Members   -  Reputation: 100

Like
0Likes
Like

Posted 09 September 2009 - 01:58 PM

Not bad! My rough test indicates there's not a lot in it at all.


#5 MJP   Moderators   -  Reputation: 11737

Like
0Likes
Like

Posted 09 September 2009 - 05:52 PM

Point sprites are a nice optimization when the hardware supports them, and when you can work within their limitations. The main benefit is that since you only need one vertex per sprite you can use a much smaller vertex buffer (with no index buffer necessary), which saves on memory, vertex processing load, and bandwidth. SpriteBatch just renders quads with a vertex buffer + index buffer, but is generally easier to use and gives you some more flexibility if you need it. Plus it doesn't require hardware support for point sprites, of course. If you're going to be rendering many many sprites (for something like a particle system), point sprites are a good choice if they're available.

Anyway your original question is unrelated to point sprites vs. SpriteBatch, it's simply an issue of using the depth buffer correctly. First of all, what you want is basic z-buffering behavior: you want to render all of your 3D geometry with z-writes and z-buffering enabled (RenderState.DepthBufferEnable = true and RenderState.DepthBufferWriteEnabled = true), and then render your sprites also with z-buffering enabled. As long as...

1. z-buffering is enabled
2. the depth buffer hasn't been cleared
3. you're setting a proper depth value for your sprites

...they should be tested for visibility just like 3D geometry. If you're not getting this behavior, I would suggest checking those 3 things.

Also on a side note, the stencil buffer doesn't hold "Z position"...it just holds an integer value. This value is typically 8-bits, can be 1-bit (or no bits) depending on the format of your DepthStencilBuffer. Either way it's just a value that's set depending on the values of RenderState.StencilFail and RenderState.StencilPass. If an incoming pixel passes the stencil test (specified by RenderState.StencilFunc) then StencilPass gets used, otherwise if it fails StencilFail gets used. At that point the stencil value doesn't get set to Z-position or anything like that...instead a simple operation is performed. It's either incremented, decremented, inverted, zeroed, or set to a value. See the documentation for StencilOperation for more details.

You also can't copy stencil data to Z or vice-versa. In fact you can't really directly manipulate the contents of the DepthStencilBuffer aside from clearing it...everything has to be done through render states and drawing geometry.

#6 Rothgo Wooft   Members   -  Reputation: 100

Like
0Likes
Like

Posted 10 September 2009 - 03:32 AM

It all makes a lot more sense now I've slept on it. Point sprites is probably the way to go, what with the lower bandwidth. But..

Are you saying that I should have seen my non-point sprites working with the stencil buffer automatically, if all was well? I'd like to get my head around it, even if I end up using point sprites. So, as the depth buffer is modified by 3D objects, it also updates the stencil buffer when altered (at lower depth resolution depending on bits used), or do they only work as separate things? If it should work, how does the stencil buffer scale itself from the depth buffer?

cheers,
Dave




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