SpriteBatch and renderstates

SpriteBatch and renderstates

  • Comments 27

2010 update: if you are using a version of XNA Game Studio >= 4.0, see here instead.

If you are mixing 3D rendering with 2D objects using SpriteBatch, you may notice that your 3D graphics no longer draw correctly after you have rendered sprites. This is because the SpriteBatch changes several device renderstates to values that may not be appropriate for drawing in 3D.

An obvious way to avoid this problem is to pass SaveStateMode.SaveState when you call SpriteBatch.Begin, but there is a potential pitfall here. Saving and restoring device state can be slow, so if you do this a lot, your framerate will suffer.

A more efficient approach is to always set whatever renderstates you need before you draw any graphics. SpriteBatch will automatically set what it needs for drawing in 2D, so you just need to set these back to what you want before drawing in 3D.

So exactly which renderstates does SpriteBatch change? Here's the complete list:

    GraphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace;
GraphicsDevice.RenderState.DepthBufferEnable = false;

GraphicsDevice.RenderState.AlphaBlendEnable = true;
GraphicsDevice.RenderState.AlphaBlendOperation = BlendFunction.Add;
GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
GraphicsDevice.RenderState.SeparateAlphaBlendEnabled = false;

GraphicsDevice.RenderState.AlphaTestEnable = true;
GraphicsDevice.RenderState.AlphaFunction = CompareFunction.Greater;
GraphicsDevice.RenderState.ReferenceAlpha = 0;

GraphicsDevice.SamplerStates[0].AddressU = TextureAddressMode.Clamp;
GraphicsDevice.SamplerStates[0].AddressV = TextureAddressMode.Clamp;

GraphicsDevice.SamplerStates[0].MagFilter = TextureFilter.Linear;
GraphicsDevice.SamplerStates[0].MinFilter = TextureFilter.Linear;
GraphicsDevice.SamplerStates[0].MipFilter = TextureFilter.Linear;

GraphicsDevice.SamplerStates[0].MipMapLevelOfDetailBias = 0.0f;
GraphicsDevice.SamplerStates[0].MaxMipLevel = 0;

SpriteBatch also modifies the Vertices, Indices, VertexDeclaration, VertexShader, and PixelShader properties on the GraphicsDevice.

Many of these settings are fine for both 2D and 3D rendering, but before you draw anything in 3D you will probably want to reset these states:

    GraphicsDevice.RenderState.DepthBufferEnable = true;
GraphicsDevice.RenderState.AlphaBlendEnable = false;
GraphicsDevice.RenderState.AlphaTestEnable = false;

Depending on your 3D content, you may also want to set:

    GraphicsDevice.SamplerStates[0].AddressU = TextureAddressMode.Wrap;
GraphicsDevice.SamplerStates[0].AddressV = TextureAddressMode.Wrap;

 

  • PingBack from http://www.xnatutorial.com/?p=42

  • SpriteSortMode.Texture sorts sprites by texture. SpriteSortMode.BackToFront and SpriteSortMode.FrontToBack

  • I see SpriteBatch uses DestinationBlend = Blend.InverseSourceAlpha but after it's done it seems to leave that renderstate set as Blend.DestinationAlpha. Is this the case?

  • > I see SpriteBatch uses DestinationBlend = Blend.InverseSourceAlpha but after it's done it seems to leave that renderstate set as Blend.DestinationAlpha. Is this the case?

    No. SpriteBatch never sets the destination blend state to destination alpha.

  • Thanks, this is exactly what I needed.

  • Wow, this just saved my day! Been digging through my 3D rendering code for the last days, figuring it had to be something crazy going on there, but you hit the spot Shawn.

  • Five stars ! Another one saved by this article :-)

  • Thanks! This fixed my problem.

  • Thanks ! It helped me a lot ! There is another reason which can cause polygon glitch through !

    If the Plane values are too sensible, you will get  some polygon colision !

  • I spent hours trying to fix this problem, I hate how the hardest things can be fixed with just a few lines, thanks for making me feel like an idiot, lol

    honestly thank you.

  • thank you so much, I was tearing my hair out over what I was doing wrong...

  • Thanks for the info, never thought adding a simple 2D crosshair would cause so many problems...

  • Thanks, but in WP7. The RenderState is removed..... I do not know how to fix this..

    Do you have any suggestion??

  • Thanks, I thought the same that there's a hurt to perf when using SaveStates. You just confirm the right way.

  • thanks you so much!!!! I could not for the life of me figure out why my 3d models were not drawing correctly (eg some meshes were always drawn on top of other meshes no matter the orientation of the model). Turns out I was drawing some sprite batches first and than my 3d models and it messed up the drawing settings for my models. Those three lines fixed it right up!

Page 1 of 2 (27 items) 12
Leave a Comment
  • Please add 3 and 4 and type the answer here:
  • Post