2D depth sorting

It can be :stuck_out_tongue:

This is why I asked you how you render the final scene.

The best would be to render the shadows over the scene in one passage by blending, otherwise you need two steps, one for merging the inputs textures (shadows and scene) as you do it already only for stencil == 0, that is everywhere except the player. And another one for merging (but actually you’d render only the player itself) the inputs texture only for stencil == 1.

Small tip: switching FBOs is the most expensive operation, if you have perf issues, you should consider just switching color attachments