So I’ve been trying to wrap my head around shaders in 2D in XNA.
http://msdn.microsoft.com/en-us/library/bb313868(v=xnagamestudio.31).aspx
This link above stated that I needed to use SpriteSortMode.Immediate, which is a problem for me because I have developed a parallax system that relies on deferred rendering (SpriteSortMode.BackToFront).
Can someone explain to me the significance of SpriteSortMode in shaders, if Immediate is mandatory, anything I can do to maintain my current system, and anything else in general to help me understand this better?
In addition, my game runs off of multiple SpriteBatch.Begin()/End() calls (for drawing background, then the game, then the foreground and HUD and etc). I’ve noticed that the Bloom example does not work in that case, and I am guessing it has something to do with this.
In general, I’ve been having some trouble understanding these concepts. I know what a shader is and how it works, but I don’t know how it interacts with XNA and what goes on there. I would really appreciate some enlightenment. 🙂
Thanks SO!
The sort mode will matter if you are attempting to do something non-trivial like render layered transparency or make use of a depth buffer. I’m going to assume you want to do something non-trivial since you want to use a pixel shader to accomplish it.
SpriteSortMode.Immediatewill draw things in exactly the order of the draw calls. If you use another mode,SpriteBatchwill group the draw calls to the video card by texture if it can. This is for performance reasons.Keep in mind that every time you call
SpriteBatch.Beginyou are applying a new pixel shader and discarding the one previously set. (Even if the new one is just SpriteBatch’s standard pixel shader that applies aTintcolor.) Additionally, remember that by callingSpriteBatch.Endyou are telling the video card to execute all of the current SpriteBatch commands.This means that you could potentially keep your existing sorting method, if your fancy pixel shaders are of limited scope. In other words, draw your background with one
Effectand then your foreground and characters with another. Each Begin/End call to SpriteBatch can be treated separately.If your goal is to apply one
Effect(such as heat waves or bloom) to everything on the screen you have another option. You could choose to render all of your graphics onto aRenderTargetthat you create instead of directly to the video card’s backbuffer. If you do this, at the end of your rendering section you can callGraphicsDevice.SetRenderTarget(null)and paint your completed image to the backbuffer with a custom shader that applies to the entire scene.