- Home /
Is a shader with selective passes faster than a single-pass which uses lerp to enable/disable shader effects?
Been messing around with shaders a lot recently, both via code and via Shader Forge.
I noticed that in the standard and image effects shaders that Unity uses multiple shader pass declarations to contain functions of a shader (eg, different methods of blurring for bloom). The pass is then defined in the script which handles the processing.
From Unity's bloom script:
Graphics.Blit (rtDown4, quarterRezColor, screenBlend, 6);
And part of the relevant shader:
// 4: nicer & softer "screen" blend mode(cheapest)
Pass {
// ..... snip .....
}
// 5: "add" blend mode (cheapest)
Pass {
// ..... snip .....
}
// 6: used for "stable" downsampling (blur)
Pass {
CGPROGRAM
#pragma vertex vertMultiTap
#pragma fragment fragMultiTapBlur
ENDCG
}
Currently I've been achieving similar selective functionality by using Switches in Shader Forge (which essentially boil down to a lerp between two values based on a 0f/1f float you define in the shader parameters).
To (very, very simply) clarify how a switch works by adding a color to a texture:
//_TheTexture = texture property, value set by material
//_DoColoring = float, value of 0f or 1f set by material
float4 frag (VertexOutput i) : COLOR {
float4 outputColor = tex2D(_TheTexture, i.uv0);
float4 color = float4(1, 0, 0, 1); //red
outputColor = lerp(outputColor, outputColor * color, _DoColoring);
return outputColor;
}
Obviously that is a very crude example, but the A and B values of the lerp could have any amount of processing behind them.
Are the A and B values of the lerp both calculated if the lerp amount is 0 or 1, or is the alternate value (and processing) completely ignored? Obviously with a value between 0 and 1 both would need to be done...
I suppose there are other benefits to using selective passes (such as handling hardware support, being able to use cheaper versions of processing methods on slower systems, etc), but generally is that approach faster than leaving it all up to the GPU?
I don't know much about how unity manages shader code at runtime, but I think it's a stretch to think that whenever it sees your_FLAG is 0, it traces back through the code to ignore every path that only writes to that lerp function.
But even if this happened, it would make no sense to think it would be faster than using if statements in the shader. If statements aren't super fast in shaders, but if you can use them to hide more than trivial computations, you should.
I don't know how selective use of shader Passes works (and how fast that is), and am also interested in hearing about it.