Search code examples
xnadirectxshaderhlslpixel-shader

Multiple Effect on the sprite


I have implement the effect and Transition using pixel shader files. When i apply effect and transition separately it working fine, but if i am applying both simultaneously it is not working. How can apply multiple shader to a sprite. Below is code what i am doing.

_effect = Effect.FromFile(_parentRVRenderer.Device, path, null, ShaderFlags.None, null);
_effect1 = Effect.FromFile(_parentRVRenderer.Device, path1, null, ShaderFlags.None, null);
_effect.Technique = "TransformTexture";
_effect1.Technique = "TransformTexture";

_effect1.Begin(0);
_effect1.BeginPass(0);
_effect.Begin(0);
_effect.BeginPass(0);
sprint.Begin()
Sprite.Draw();
....

Solution

  • Put your two pixel shader functions in the same shader, and use a two-pass technique that applies a different pixel shader in each pass.

    You still have to use two render targets to bounce the output from the first pass to the other as stated above, but it's better to use the two-pass approach than sending the render target to another shader technique.

    Pseudocode:

    RenderTarget2D[2] targets;
    
    // (Draw all your sprites to target 0)
    
    // target 1 will be empty, will be used in pass 0 (even pass)
    
    effect.Technique = "TwoPassTechnique";
    
    for (int i = 0; i < effect.Passes.Count; i++) 
    {
        // Even pass sets target 1, odd pass sets target 0
        GraphicsDevice.setRenderTarget(targets[1 - i % 2]);
        effect(i).BeginPass;
    
        // Even pass samples texture from target 0, odd pass uses target 1
        effect(i).Parameters["texture"].SetValue(targets[i % 2]);
    
        // Draw a 2D quad with extents (-1, -1), (1, 1) in screen space 
    }
    // Final contents are now stored in target 0
    
    // (Draw target 0's texture to the screen, using a sprite or another 2D quad)