'texture' function call on sampler2D in glsl makes entire screen gray

Viewed 242

I am trying to use both samplerBuffer and sampler2D in fragment shader.

With samplerBuffer alone it works fine and code looks like this

#version 460
out vec4 FragColor;
in vec2 texcoord;

uniform sampler2D background;
uniform samplerBuffer OctreeData;

...

void main()
{    
    Ray ray = initRay();
    /*this function does raytraycing in octree and retrieves data
    from samplerBuffer using texelFetch function.*/
    Contact res = iterativeHitNodes(ray);

    if(res.hit)
      FragColor = assignNodeColor(res); //sets color of the node
    else
      FragColor = getBackground(origin, ray);
}

Then if I add FragColor = texture(background, texcoord.xy); return; line at the beginning of main function, it successfully reads the image and displays it. (A)

But when texture is called in getBackground it shows gray screen. And turns out shader is not working at all — When I intentionally load heavy models framerate stays the same. But there are no shader compile errors.

And the biggest "what" is that screen remains gray even when texture is never called (B). For example when backgroundType is always 0 and getBackground function is:

vec3 getBackground(vec3 from, vec3 direction)
{
    if(backgroundType == 0)
        return getSkyBackground(from, direction);
    else if(backgroundType == 1)
    {
        return texture(background, texcoord.xy).xyz;
    }

    return vec3(0);
}

Seems like a sheer presence of texture call discards fragments.

P.S. In Debug it works as intended. For some reason, the problem appears in Release mode

P.P.S Most of the shader's code https://pastebin.com/SWCQ9HUw

1 Answers

See OpenGL Shading Language 4.60 Specification (HTML) - Texture Functions:

Some texture functions (non-“Lod” and non-“Grad” versions) may require implicit derivatives. Implicit derivatives are undefined within non-uniform control flow and for non-fragment shader texture fetches.

If you are using a texture and texture for texture lookup make sure that all invocations in the invocation group execute the same control-flow path:

vec3 getBackground(vec3 from, vec3 direction, vec3 backgroundColor)
{
    if (backgroundType == 0)
        return getSkyBackground(from, direction);
    else if (backgroundType == 1)
        return backgroundColor;

    return vec3(0);
}
void main()
{
    vec3 backgroundColor = texture(background, texcoord.xy).xyz;
    
    Ray ray = initRay();
    /*this function does raytraycing in octree and retrieves data
    from samplerBuffer using texelFetch function.*/
    Contact res = iterativeHitNodes(ray);

    if(res.hit)
      FragColor = assignNodeColor(res); //sets color of the node
    else
      FragColor = getBackground(origin, ray, backgroundColor);
}
Related