# How does one specify raster operations in XNA?

I'm looking for a way to add a sprite using a particular logic operation (like XOR). I can't find anything on Google and I'm not sure where to look in the documentation.

I've looked into SpriteBatch.Begin(...) and its Draw method and several options in the GraphicsDevice class, but I'm not recognizing anything capable of this. I'm still pretty new to XNA so I may just not have recognized the terminology to do this.

• Are you targeting XBox/PC or WP7 platforms? Oct 15 '11 at 22:27
• PC right now. If I ever expand, I'd probably only expand to the xbox because I do not have a Windows Phone. Oct 16 '11 at 3:38

The GraphicsDevice.RasterizerState property is how you alter raster settings.

There are three default states built in but I don't believe the RasterizerState is what you are looking for.

RasterizerState.CullCounterClockwise
RasterizerState.CullClockwise
RasterizerState.CullNone


I'm not sure what you mean by xor sprites together. However, from the below comment I'm assuming you want to use an overlay for a crosshair. If you do want to use an overlay you could use alter the BlendState as Jonathan Dickinson pointed out.

Aside from just another tool in the toolbelt, I'm looking into how to make a crosshair for targeting. An inverted crosshair (in my experience/opinion) is the best crosshair

Just set the below as the BlendState in SpriteBatch.Begin(). Also worth noting is that if the SortMode is SpriteSortMode.Immediate you can alter the all of the GraphicsDevice's properties without calling Spritebatch.Begin again.

        GraphicsDevice.BlendState = new BlendState {

ColorSourceBlend = Blend.Zero,
ColorDestinationBlend = Blend.InverseSourceColor,

AlphaSourceBlend = Blend.Zero,

};


• I'll try this when I get a chance. Feb 14 '12 at 16:37

One option may be defining a new BlendState...and choose the right BlendFunction and Factors to get the xor operation. I'm not sure it can be done, but it is the easier way and should be your first option.

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.blendstate.alphablendfunction.aspx

The second option is using a HLSL shader and rendertargets.

1. You draw to rendertargetA you sprites
2. if you want to do a xor operation..
3. you assign the texture source in your shader from rendertargetA
4. you draw with your xor shader to rendertargetB
5. exchange rendertarget reference, RendertargetA is now B, and viceversa
6. Draw from your rendertarget A to the backbuffer.

Or something similar... :)

• everyway xor operations with textures has not much sense in modern hardware, because it is expected to be a full redraw every frame... ¿are you sure you need this operation?
– Blau
Oct 16 '11 at 0:46
• Aside from just another tool in the toolbelt, I'm looking into how to make a crosshair for targeting. An inverted crosshair (in my experience/opinion) is the best crosshair. Oct 16 '11 at 3:42
• aaah... ok... then you should choose the BlendState way... if not accurate, i'm sure you can get a similar effect.
– Blau
Oct 16 '11 at 10:25
• @CoreyOgburn you could try (source x Blend.Zero) + (destination x Blend.InverseSourceColor) - i.e. set up a BlendState as such. Oct 17 '11 at 9:48