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.

  • \$\begingroup\$ Are you targeting XBox/PC or WP7 platforms? \$\endgroup\$
    – r2d2rigo
    Oct 15 '11 at 22:27
  • \$\begingroup\$ PC right now. If I ever expand, I'd probably only expand to the xbox because I do not have a Windows Phone. \$\endgroup\$ 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.


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

enter image description here

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,
            AlphaDestinationBlend = Blend.InverseSourceColor


enter image description here

  • \$\begingroup\$ I'll try this when I get a chance. \$\endgroup\$ 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.


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... :)
  • \$\begingroup\$ 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? \$\endgroup\$
    – Blau
    Oct 16 '11 at 0:46
  • \$\begingroup\$ 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. \$\endgroup\$ Oct 16 '11 at 3:42
  • \$\begingroup\$ aaah... ok... then you should choose the BlendState way... if not accurate, i'm sure you can get a similar effect. \$\endgroup\$
    – Blau
    Oct 16 '11 at 10:25
  • 2
    \$\begingroup\$ @CoreyOgburn you could try (source x Blend.Zero) + (destination x Blend.InverseSourceColor) - i.e. set up a BlendState as such. \$\endgroup\$ Oct 17 '11 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.