Hyunan Kwon
Hyunan Kwon

Reputation: 575

GLSL : Considering not used variables

I wonder whether I should consider not used variables in glsl.

In next situation(which is just sample code for description). If "trigger" is true, "position" and "normal" are not used in the fragment shader. Then, are "position" and "normal" abandoned? or calculated by rasterizer?

Vertex shader :

layout(location = 0) in vec3 vertice;
layout(location = 1) in vec2 uv;
layout(location = 2) in vec3 normal;

out VertexData{
    out vec3 position;
    out vec2 uv;
    out vec3 normal;
    flat out bool trigger;
} VertexOut;

void main(){
    gl_Position = vertice;
    VertexOut.uv = uv;
    if(uv.x > 0){
        VertexOut.trigger = true;
    else{
        VertexOut.trigger = false;
        VertexOut.position = vertice;
        VertexOut.normal = normal;
    }
}

Fragment shdaer :

in VertexData{
    in vec3 position;
    in vec2 uv;
    in vec3 normal;
    flat in bool trigger;   
}VertexIn;

out vec4 color;

uniform sampler2D tex;

void main(){
    if(VertexIn.trigger){
        color = texture2D(tex, VertexIn.uv);
    }
    else{
        vec4 result = texture2D(tex, VertexIn.uv);
        /*
            Lighting with position and normal...
        */

        color = result;
    }
}

Upvotes: 0

Views: 184

Answers (1)

keltar
keltar

Reputation: 18389

Depends on hardware. On newer hardware smart compiler can avoid interpolating/fetching this values. Older hardware don't have this operations controllable by fragment shader, so it would be done anyway (interpolated from undefined value - which is still undefined).

Conditional operation isn't free though, especially if your fragment branches are highly divergent.

Upvotes: 1

Related Questions