3
\$\begingroup\$

I read a few thing about, that in GLSL both sides of the if-clause is executed, so do you have any idea for a workaround for this one:

uniform int uses_bones;
uniform mat4[] bone_transformations;


void main()
{

    vec4 worldPosition = transformationMatrix * vec4(position, 1);
    if(uses_bones == 1)
    worldPosition = transformationMatrix * vec4(position, 1) * (bone_transformations[bone_pointer] *  bone_intensity);
    vec4 pos =  projectionMatrix * worldPosition;

The int uses_bones represents a bool, because LWJGL does not support loading booleans to the shader. 1 = true, 0 = false

\$\endgroup\$
  • \$\begingroup\$ What problem are you trying to solve? Maybe it's fine as-is. (Yes, implementations of GLSL might execute both parts of the IF. ) You could profile it as-is, and then force always-bones and never-bones, and compare those results, to get a sense of how expensive it it. \$\endgroup\$ – david van brink Feb 14 '15 at 20:26
5
\$\begingroup\$

You can create two shaders and set the correct one from the application. Use a define to turn on/off features.

GLSL both sides of the if-clause is executed

Partially true.

One core execute a lot of (64) with SIMD paradigms. Think about them as a vector of data and one instruction pointer (IP). In program with a branch the core can't use two IP for different data. So in this case both branch are executed and the registry from the "wrong" branch ignored.

But if the branch depend only on uniform data all the data in the SIMD vector will follow the same branch, and the compiler add a real branch instruction. Older shader compiler didn't support this feature, but modern compiler and hardware does. In HLSL you can enforce the branch with [branch] before the if. I don't know if GLSL have something similar.

In any case, use branch in shader only if you don't have other alternative.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.