# XNA 4.0 Depth Test

This topic is 2577 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey folks,

I'm trying to render a scene with the depth buffer enabled, first using a BasicEffect instance to draw a model and then a call to DrawIndexedPrimitives to display a ground plane. I set up the depth buffer like this:

 // Initialise the depth & stencil buffers DepthStencilState depthStencilState = new DepthStencilState(); depthStencilState.DepthBufferEnable = true; depthStencilState.DepthBufferFunction = CompareFunction.LessEqual; depthStencilState.DepthBufferWriteEnable = true; device.DepthStencilState = DepthStencilState.Default; 

But the resulting image looks like this :

Must be doing something wrong here, but I'm not sure what... Do I have to set up a 2D render target and then use a sprite batch to draw the resulting texture for the depth buffer to kick in?

Cheers!

##### Share on other sites
Ah never mind - didn't realize you have to set the state on a per-frame basis

##### Share on other sites

Ah never mind - didn't realize you have to set the state on a per-frame basis

You shouldnt have to set in on a per frame basis, you have to set it again each time you change that particular state. It's a state machine so it remebers the state you last set in it. Did you set up a different state for another render call as that would invalidate the state.

##### Share on other sites

You shouldnt have to set in on a per frame basis, you have to set it again each time you change that particular state. It's a state machine so it remebers the state you last set in it. Did you set up a different state for another render call as that would invalidate the state.

I don't change the DepthStencilState directly, but each time I get to the rendering stage the GraphicDevice version is set to DepthStencilState.None

I'm not sure what would be clearing the depth stencil state, the only other thing I modify on the device are the Vertex & Index buffers when I'm rendering the ground plane or object bounding boxes

##### Share on other sites

[quote name='NightCreature83' timestamp='1321624843' post='4885308']
You shouldnt have to set in on a per frame basis, you have to set it again each time you change that particular state. It's a state machine so it remebers the state you last set in it. Did you set up a different state for another render call as that would invalidate the state.

I don't change the DepthStencilState directly, but each time I get to the rendering stage the GraphicDevice version is set to DepthStencilState.None

I'm not sure what would be clearing the depth stencil state, the only other thing I modify on the device are the Vertex & Index buffers when I'm rendering the ground plane or object bounding boxes

[/quote]
Do you set any state information in the effect file as that will affect the render state as well. These are the things you generally set before the lines that compile the vertex and pixel shaders.

##### Share on other sites

[quote name='NightCreature83' timestamp='1321624843' post='4885308']
You shouldnt have to set in on a per frame basis, you have to set it again each time you change that particular state. It's a state machine so it remebers the state you last set in it. Did you set up a different state for another render call as that would invalidate the state.

I don't change the DepthStencilState directly, but each time I get to the rendering stage the GraphicDevice version is set to DepthStencilState.None

I'm not sure what would be clearing the depth stencil state, the only other thing I modify on the device are the Vertex & Index buffers when I'm rendering the ground plane or object bounding boxes

[/quote]

A good rule of thumb is to only ever set the renderstates that you need when setting up a draw call. Between frames states don't change unless if you explicitly change them (as NightCreature explained), so you have to check your drawing code. If you're using BasicEffect, that won't be setting any renderstates on its own, but if you build your own effects you can set renderstates in the pass declarations.

Do you use SpriteBatch in any way or form? By default that uses DepthStencilState.None, so even if you aren't explicitly changing the state, spritebatch would. This sometimes is the culprit in these situations, and why it's a good idea to set the states you depend on before drawing different geometry since things like SpriteBatch or third party game components may change states without your knowledge or direct control.

Edit:

Additionally, a DepthStencilState doesn't define how to setup the depth buffer - it specifies how to use the depth buffer during drawing, e.g. not write to it, write to if depth is less than or greater than, etc. When you create the back buffer (in XNA, its closely tied to the graphics device) you can specify if you want a depth buffer, and if so its format. Same thing when creating render targets.

##### Share on other sites

Do you use SpriteBatch in any way or form? By default that uses DepthStencilState.None, so even if you aren't explicitly changing the state, spritebatch would. This sometimes is the culprit in these situations, and why it's a good idea to set the states you depend on before drawing different geometry since things like SpriteBatch or third party game components may change states without your knowledge or direct control.

Edit:

Additionally, a DepthStencilState doesn't define how to setup the depth buffer - it specifies how to use the depth buffer during drawing, e.g. not write to it, write to if depth is less than or greater than, etc. When you create the back buffer (in XNA, its closely tied to the graphics device) you can specify if you want a depth buffer, and if so its format. Same thing when creating render targets.

Ah the sprite batch will be what's doing it I'm sure - I use it to render line lists for both the ground plane and Model bounding boxes.

Cheers!

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633661
• Total Posts
3013221
×