In our GPS map app, as you zoom in closer to the ground, we toggle our shader to draw more expensive detail.
Currently, it’s kludged, we do it via one of the shader parameters turn on/off the extra detail technique (which shows contour lines based upon elevation). However, because the 'if" branch is inside the shader, it’s performing the cost of this “if” no matter what. I assume that this kludged method hurts performance enough that we ought to instead have two versions of the shader, and just swap “Shader Technique”.
Pixel Shader looks like this:
void PS()
{
vec4 diffColor = texture2D(sDiffMap, vTexCoord);
vec4 elevNorm = texture2D(sNormalMap, vElevCoord).rgba; // was ra
float hiByte = elevNorm.x * 65280.0;
float loByte = elevNorm.y * 255.0;
float elevFt = hiByte + loByte; // elevNorm * 65535.0;
elevFt -= cOwnshipAltitudeFt;
if (cHighlightAlpha > 0.01 && elevFt > -1500.0) // <<<<= I WANT TO DITCH THIS 'IF' branch
{
.... complex logic to show Altitude Highlights, and Contours ....
}
…
gl_FragColor = diffColor;
}
====
ALSO, related, we as the user pans/zooms, we like to do a QUICK fade-in (0.15 sec) new content and fade out the old content. This “fading” effect requires us to use “alpha” pass as follows;
<pass name=“alpha” depthwrite=“false” blend=“alpha” />
I would imagine that calling this the “alpha” pass requires extra CPU work to “sort” the objects in the alpha pass (back to front?), to make it work correctly. However, we ONLY need this during the 0.15 seconds where we are fading in/out.
So currently, we’re ALWAYS rendering these tiles on the “alpha” pass, so that we can do this fade in/out on-demand (each material has an “opacity” parameter).
So for efficiency, we’re wanting to swap in/out the technique used (from “alpha” password to “base” pass). What is the most efficient way to swap this techique?
Two cases:
- Case #1 - same pass, but added shader logic to show “contour lines” as you zoom way in.
- Case #2 - Want to toggle the pass between “alpha” and “base”, and for the “base” pass shader, we would omit use of the “opacity” shader parameter.
What is the best way to toggle techniques on an object to make it most efficient for the CPU/GPU?
Options that I can think of:
- One Material with advanced settings that allow you to programmatically tell the material which technique to use for rendering.
- Multiple simple materials, and just assign the material dynamically.
- One Material, but programmatically use “SetTechnique(…)” to set it’s technique.
I’m leaning towards #3 right now, but figured there may be a better way to do it.