shader - GLSL break command -
currently learning how create shaders in glsl game engine working on, , have question regarding language puzzles me. have learned in shader versions lower 3.0 cannot use uniform variables in condition of loop. example following code not work in shader versions older 3.0.
for (int = 0; < unumlights; i++) { ............... }
but isn't possible replace loop fixed amount of iterations, containing conditional statement break loop if i, in case, greater unumlights?. ex :
for (int = 0; < max_lights; i++) { if(i >= unumlights) break; .............. }
aren't these equivalent? should latter work in older versions glsl? , if so, isn't more efficient , easy implement other techniques have read about, using different version of shader different number of lights?
know might silly question, beginner , cannot find reason why shouldn't work.
glsl can confusing insofar for()
suggests there must conditional branching, when there isn't because hardware unable @ (which applies if()
in same way).
what really happens on pre-sm3 hardware hal inside opengl implementation unroll loop, there no jump more. and, explains why has difficulties doing non-constants.
while technically possible non-constants anyway, implementation have recompile shader every time change uniform, , might run against maximum instruction count if you're allowed supply haphazard number.
that problem because... then? that's bad situation.
if supply big constant, give "too many instructions" compiler error when build shader. now, if supply silly number in uniform, , hal has produce new code , runs against limit, can opengl do?
validated program after compiling , linking, , queried shader info log, , opengl kept telling fine. is, in way, binding promise, cannot decide otherwise of sudden. therefore, must make sure situation cannot arise, , workable solution not allow uniforms in conditions on hardware generations don't support dynamic branching.
otherwise, there need form of validation inside gluniform
rejects bad values. however, since depends on successful (or unsuccessful) shader recompilation, mean have run synchronously, makes "no go" approach. also, consider gl_arb_uniform_buffer_object
exposed on sm2 hardware (for example geforce fx), means throw buffer object unpredictable content @ opengl , still expect work somehow! implementation have scan buffer's memory invalid values after unmap it, insane.
similar loop, if()
statement not branch on sm2 hardware, though looks it. instead, calculate both branches , conditional move.
Comments
Post a Comment