If this was just a quad you would have to invert the depths clear flag
GraphicsDevice.Clear(Color.Black);
GraphicsDevice.Clear(ClearOptions.DepthBuffer, Color.Black, 0f, 0);
Edit:
Ok… here we go…
// to make sure drawstring works right use greater equal.
public static DepthStencilState ds_depthtest_greater = new DepthStencilState()
{
DepthBufferEnable = true,
DepthBufferFunction = CompareFunction.GreaterEqual
};
…
// clear the back buffer
GraphicsDevice.Clear(Color.Black);
DepthStencilState ds;
if(flipDepth)
{
// clear the depth buffer to near clipping 0 instead of far 1.
GraphicsDevice.Clear(ClearOptions.DepthBuffer, Color.Black, 0f, 0);
ds = ds_depthtest_greater_equal;
}
else
{
ds = ds_depthtest_less_equal;
}
var rA = new Rectangle(100, 100, 100, 100);
var rB = new Rectangle(120, 100, 100, 100);
spriteBatch.Begin(SpriteSortMode.Texture, null, null, ds, null, null, null);
spriteBatch.Draw(texture, rA, texture.Bounds, Color.Blue, 0f, Vector2.Zero, SpriteEffects.None, .9f);
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.Texture, null, null, ds, null, null, null);
spriteBatch.Draw(texture, rB, texture.Bounds, Color.Red, 0f, Vector2.Zero, SpriteEffects.None, .3f);
spriteBatch.End();
Result : Blue sprite is drawn because it has a higher depth.
Worked for Immediate and Deferred mode. i deleted the test before i checked if it worked for texture but that is probably what you should use should be fine.
commenting out the clear option and setting the state to less than equal gives the reverse, the Red sprite is drawn in front. Both sprites are visible.
You don’t need sprite sort mode (either front to back or back to front), as the depth buffer is sorting by layer depth (alpha blend should work normally as well now). You can pick SpriteSortMode.Texture or immediate to batch on textures if you have manually batched them by texture. Then seperate spriteBatch begin end calls for different effects.