Generally if you're new to shaders, and you're using if or switch, then it can probably be optimized ;)
Rule 1 of shaders is
don't branch unless it's really necessary, or you can prove it's an optimization.
I haven't tested this, but it should be equivalent to your original GetLayer function (
assuming Color.x is a float and is always positive), but with the nasty branching replaced with 4 dots, 2 steps, some swizzling and an add.
float GetLayer(PixelShaderInput Input){ //generate all the possible results float4 result1234 = Tex(TerLayers); float2 result05 = float2(1.0, 0.0); //generate a 0/1 value for each case to say whether it can be true float4 case1234 = step( float4(1.0, 2.0, 3.0, 4.0), Color.x ); float2 case05 = step( float2(0.0, 4.004), Color.x ); //at this point more than one case may be true, because 'step' does >= instead of ==, so lets fix that. //1 can't be true if 2 is true //2 can't be true if 3 is true //3 can't be true if 4 is true //4 can't be true if 5 is true not1234 = float4( case1234.yzw, case05.y ); //0 can't be true if 1 is true //5 can always be true not05 = float4( case1234.x, 0.0 ); //AND the conditions with their 'nots' case1234 = dot( case1234, 1.0-not1234 ); case05 = dot( case05, 1.0-not05 ); //Now, case1234 and case05 should be all 0's with a single 1 somewhere in them. //multiply the conditions (0/1's) with their associated return values return dot( case1234, result1234 ) + dot( case05, result05 );}
[Edited by - Hodgman on July 17, 2010 9:39:09 AM]