• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
syskrank

Need help with optimized-out GLSL uniforms

6 posts in this topic

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.

Edited by syskrank
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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 ?

Edited by syskrank
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites
You seem to be missing the uniform qualifier when you declare your material uniform :) it should be
uniform material_t material;
0

Share this post


Link to post
Share on other sites

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.
 

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0