Spritebatch render states issues after Effect is applied

I have a problem with spritebatch , the render order is messed up after I call an effect, I know that spritebatch begin/end do not return to the original state after an effect is applied, but I tried to turn blend mode, rasterizer state and other fields back to the same as before and still is messed up.

I am using blend mode = Deferred, if I change to “immediate” everything renders properly.

This is the problem:

1. spritebatch.Begin( deferred .....+  all params here without effect)
<--- here all renders perfectly
spritebatch.end();

2. spritebatch,Begin( same params with effect)
<--- here all renders perfectly
spritebatch.end();

3. spritebatch.Begin( deferred ...+  all params same as first one without effect)
<--- here all drawings get messed up the order
spritebatch.end();

if I do not do the effect drawing skipping it ( step 2 in my list above), everything renders perfectly. I saw spritebatch becomes Opaque in #3 even if I set as Alphablend, any idea why it is not setting up the values I enter after my effect finished?

nevermind found the issue and solved it, so I will write it here so in case anyone else finds the same problem they can find a solution.

If you are using render targets to pass to your effect , never do :

SetRenderTarget( null );
spritebatch.End();

clearing the render target while still in the spritebatch seems to mess up everything , so you have to do

spritebatch.End();
SetRenderTarget( null ); // <- this has to be after spritebatch.End(); otherwise following spritebatch calls will not work properly.

in that order, I found one place where I was doing this in wrong order and because of that all the spritebatch rendering went in wrong order after, no matter what I set up on the graphic device or spritebatch as parameters.

You have the correct solution there. Just as a little more info, the reason this was happening is because the actual rendering call with a SpriteBatch happens when End is called, not on the actual Draw call unless you’ve specified immediate.

Under the hood, the Draw call basically passes all the required information to render into an internal Sprite Batcher class, which holds the list of everything you’ve called Draw on until it needs to flush out this list. If you’ve set immediate it happens right at the end of the draw call, but if you don’t specify immediate it waits until the End call is made and flushes out all the drawing at once.

So the issue you were seeing originally was happening because by clearing the render target before the SpriteBatch End call, it was just going to the backbuffer instead of your expected render target.

yes ,but on top, I didn’t expect it to affect any spritebatch batches that followed , even if those were already pushed to the graphic device. It failed to set the render states after my mistake properly.