Need help with optimized-out GLSL uniforms

Started by
5 comments, last by syskrank 9 years, 11 months ago

Hello again, everyone.

I'm hoping that someone could tell me what I am doing wrong.

Here's the plot. I am using 2 GLSL sources to create a shader program - vertex shader and fragment shader, respectively.

I'm using #version 400 core and, for non-standart part, I've implemented my own #require directive to have some sort of 'please include that file here' behavior.

The problem is, that after compilation and linkage ( which were successful ), I can't get uniforms locations, because all of them seems to be optimized-out. And I swear - I'm using the code, that was optimized-out ( or at least, I think so smile.png ).

Here how the shaders are organized:

File A - just some code to 'include' - my data structures ( for lights and material ) and math functions for Phong shading like that:


struct material_t {
    vec3 Kd; // diffuse reflectance [ color ], taken from diffuse map
    vec3 Ka; // ambient reflectance, taken from default
    vec3 Ks; // specular reflectance
    float specularPower;
    float emission; // for using the specular map for emission mapping too
    // params for parallax displacement mapping
    float displaceScale;
    float displaceBias;
};
...

vec3 PhongLight( baseLight_t light, material_t material,
                        vec3 lightDir, vec3 eyePos,
                        vec3 worldPos, vec3 normal ) {
    // calculate prerequisites
    vec3 dirToEye = normalize( eyePos - worldPos );
    vec3 reflectDir = normalize ( reflect( lightDir, normal ) );
    
    // calculate diffuse component
    vec3 Id = light.Ld * material.Kd * max ( dot ( dirToEye, normal ), 0 );
    
    // calculate specular component
    float dotSpecular = max ( dot( dirToEye, reflectDir ), 0 );
    float specularFactor = pow( dotSpecular, material.specularPower );
    vec3 Is = light.Ls * material.Ks * specularFactor;

    // calculate result frag color, taking an 'emissive' map in account
    vec3 resultColor = vec3( Id + Is +
                material.Ks * material.emission ) * light.intensity;
    return resultColor;
}

File B - another 'header' file filled with uniforms like MVP, M, eyePos, lights, and, the most tricky part - the control switches for 'if' statements:


uniform bool useDiffuseMap = true;
uniform bool useNormalMap = true;
uniform bool useSpecularMap = true;
uniform bool useDisplaceMap = true;
// entity's material uniform
material_t material;

File C - pretty standart vertex shader, all is good here ( no lost uniforms );

File D - the actual fragment shader, where I have something like that:


#version 400 core
#require <fwd-common-math.glsl>
#require <fwd-common-uniforms.glsl>

layout( location = 0 ) out vec4 fragColor;

void main () {    
vec2 tCoords = textureCoord.xy;
    if ( useDisplaceMap == true ) {
        tCoords = CalculateParallax( displaceSampler, textureCoord,
                                material, eyePos, worldPosition, TBN );
    }
    if ( useDiffuseMap == true ) {
        material.Kd = texture2D( diffuseSampler, tCoords.xy ).rgb;
    }
    if ( true == useSpecularMap )
        material.Ks = texture2D( specularSampler, tCoords.xy ).rgb;
    vec3 n = normal;
    if ( true == useNormalMap )
        n = CalculateNormalMap( normalSampler, tCoords.xy, TBN );
        
    
    vec3 color = DirectLight( directLight, material, eyePos, worldPosition, n );    
    fragColor = vec4( color, 1.0 ); 
}

In the fragment shader above all variables and functions were declared ( and implemented ) in the 'required' files , but the most part of uniforms seems to be optimized-out ( glGetUniformLocation() returns -1 ) and my Logger gives me the following:


Error: (1400251767320): Failed to find uniform 'material.Kd'.
Error: (1400251767320): Failed to find uniform 'material.Ks'.
Error: (1400251767320): Failed to find uniform 'material.Ka'.
Error: (1400251767320): Failed to find uniform 'material.specularPower'.
Error: (1400251767320): Failed to find uniform 'material.emission'.
Error: (1400251767320): Failed to find uniform 'material.displaceScale'.
Error: (1400251767320): Failed to find uniform 'material.displaceBias'.
Error: (1400251767320): Failed to find uniform 'La'.
Error: (1400251767320): Failed to find uniform 'displaceSampler'.
Error: (1400251767320): Failed to find uniform 'directLight.base.color'.
Error: (1400251767320): Failed to find uniform 'directLight.dir'.
Error: (1400251767320): Failed to find uniform 'pointLight.pos'.
Error: (1400251767320): Failed to find uniform 'pointLight.range'.
Error: (1400251767320): Failed to find uniform 'pointLight.base.color'.
Error: (1400251767320): Failed to find uniform 'pointLight.base.intensity'.
Error: (1400251767320): Failed to find uniform 'pointLight.constAtten'.
Error: (1400251767320): Failed to find uniform 'pointLight.linearAtten'.
Error: (1400251767320): Failed to find uniform 'pointLight.quadAtten'.
Error: (1400251767320): Failed to find uniform 'spotLight.pointLight.pos'.
Error: (1400251767320): Failed to find uniform 'spotLight.pointLight.range'.
Error: (1400251767320): Failed to find uniform 'spotLight.pointLight.base.color'.
Error: (1400251767320): Failed to find uniform 'spotLight.pointLight.base.intensity'.
Error: (1400251767320): Failed to find uniform 'spotLight.pointLight.constAtten'.
Error: (1400251767320): Failed to find uniform 'spotLight.pointLight.linearAtten'.
Error: (1400251767320): Failed to find uniform 'spotLight.pointLight.quadAtten'.
Error: (1400251767320): Failed to find uniform 'spotLight.dir'.
Error: (1400251767320): Failed to find uniform 'spotLight.cutoff'.

Note, that I've tried to use subroutines functionality here to avoid 'if' statements and all that boolean mess, and in result I've got the very similar errors with uniforms =(

Please, explain where the issue might be, or what I am doing wrong in my frag shader.

P.S.

'#require' works fine, I've tried without it by assembling resulting source by hand - same uniform errors are present.

Advertisement

I see nothing that catches my eyes immediately. Have you tries asking for the list of all available uniforms to see what is actually there? Are the function calls you didn't show, such as CalculateParallax and DirectLight, actually implemented to use the parameters? If they are, for example, just dummy functions that returns nothing or something constant for testing, then all parameters and thus most uniforms are effectively unused.

Hello, Brother Bob, and thanks for answer.


Have you tries asking for the list of all available uniforms to see what is actually there?

Nope, will do that now.


CalculateParallax and DirectLight, actually implemented to use the parameters?

They are independent from all of the uniforms.


// Calculate parallax mapping offset
vec2 CalculateParallax( sampler2D displaceSampler,
                            vec2 texCoord,    material_t material,
                            vec3 eyePos, vec3 worldPos, mat3 TBN ) {     
    vec3 dir = normalize( eyePos - worldPos );
    vec2 offset = ( dir * TBN ).xy; // go to tangent space
    float height = texture2D( displaceSampler, texCoord.xy ).r  *
                        material.displaceScale + material.displaceBias;
    return texCoord.xy + offset * height;
}

// directional light
vec3 DirectLight( directLight_t light, material_t material,
                        vec3 eyePos, vec3 worldPos, vec3 normal ) {
    return PhongLight( light.base, material, light.dir,
                            eyePos, worldPos, normal );                            
} 

So, as you can see, they are not using any uniforms at all, but own parameters only, and they are not dummy functions for testing.

P.S.

I am not sure about the variables scope - maybe I should rename those parameters so that their names won't match with the uniform names. How GLSL handles stuff like that ?

Digging available uniforms info like that:


    void Shader::GetDebufUniformInfo() {
        GLint numBlocks;
        glGetProgramiv( program, GL_ACTIVE_UNIFORM_BLOCKS, &numBlocks );
        core::stringVector_t names;
        names.reserve( numBlocks );
        for ( int i = 0; i < numBlocks; i++ ) {
            GLint namelen;
            glGetActiveUniformBlockiv( program, i, GL_UNIFORM_BLOCK_NAME_LENGTH, &namelen );

            std::vector< GLchar > name;
            name.resize( namelen );
            glGetActiveUniformBlockName( program, i, namelen, 0, &name[ 0 ] );
            blockName.assign( name.begin(), name.end() - 1 );
            names.push_back( blockName );
        }

        core::Logger::WriteLog( core::LOG_S_MESSAGE, "Available uniforms: " );
        for ( unsigned i = 0; i < names.size(); i++ )
            core::Logger::WriteLog( core::LOG_S_MESSAGE, names[ i ] );
    }

...Getting just emptiness.

And yeah - the strange thing is that some uniforms are not reported to be unreachable, but they are not present in 'available' list.

Have you tried querying with glGetActiveUniform and not the block-variant? I haven't used uniform blocks yet, but it doesn't look to me that you're using uniform blocks so the block-variants may not be useful to you.

You seem to be missing the uniform qualifier when you declare your material uniform :) it should be
uniform material_t material;

Well, I've fixed that, thank you, guys :)


Have you tried querying with glGetActiveUniform and not the block-variant? I haven't used uniform blocks yet, but it doesn't look to me that you're using uniform blocks so the block-variants may not be useful to you.

That's my mistake here, it's a shame, because I'm not even using uniform blocks - damn that haste :)


You seem to be missing the uniform qualifier when you declare your material uniform smile.png

Here it is again. Thank you, always good to take a fresh look at the source. Missed that 'uniform'.

Now ( and after some other minor fixes) all compiles and locates OK. Will do some subroutines soon :)

Thanks again.

This topic is closed to new replies.

Advertisement