SpriteBatch and renderstates

Originally posted to Shawn Hargreaves Blog on MSDN, Monday, November 13, 2006

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;

Blog index   -   Back to my homepage