Why is my SparkSL shader code behaving differently with and without an if statement with an unreachable condition?

33 Views Asked by At

I'm writing a shader for Meta Spark Studio which creates a wavy pattern:


vec4 wavy(function<vec4(vec2)> tex, float waveAmplitude, float waveFrequency, float timeMultiplier) {
    vec2 coords = getVertexTexCoord();
    float time = getTime() * timeMultiplier;
    
    if(false && coords.x < -1.0){
        return tex(coords);
    }else{
        coords.y += cos(((coords.x * 3.14 * 5.0) + time * 3.14) * waveFrequency + time) * waveAmplitude * 1.0;
        return tex(coords);
    }
}

This code works as expected (I reached this weird state by trial and error), running the else clause and displaying correctly. The code block in if clause of the statement should never be executed anyway, so I can remove it and simplify as:

vec4 wavy(function<vec4(vec2)> tex, float waveAmplitude, float waveFrequency, float timeMultiplier) {
    vec2 coords = getVertexTexCoord();
    float time = getTime() * timeMultiplier;
    
    coords.y += cos(((coords.x * 3.14 * 5.0) + time * 3.14) * waveFrequency + time) * waveAmplitude * 1.0;
    return tex(coords);
}

However, the moment I do this, my effect behaves differently as if argument of cos is never multiplied by any number and the result is just different whatever I do.

Why is my code behaving only correctly whenever it's inside of an else block of an if statement with the if clause that always evaluates to false with no side effects, but incorrectly without that statement?

0

There are 0 best solutions below