Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7885183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:59:42+00:00 2026-06-03T04:59:42+00:00

This one is a doozy. For the sake of proper explanation let me explain

  • 0

This one is a doozy. For the sake of proper explanation let me explain what I’m trying to do. I’ll follow up with a code listing, and then explain the code aftwards.

The Goal

I’m trying to get the names of the variables in every GLSL shader file I have. Right now, I only have a single vertex shader, along with a fragment shader to complement it. The purpose of this is so I can dynamically bind values to the shaders without having to enter each and every single variable name.

Code

std::vector< const char* > GetShaderVariableNames( const Shader& shader )
    {
        Config::Log::info( "Getting shader variable names." );

        static const char* keyLookupTable[] =
        {
            "vec2", "vec3", "vec4",
            "mat2", "mat3", "mat4",
            "float", "int", "double"
        };

        std::vector< const char* >  keys;
        std::vector< std::string > lines;

        SplitIntoLines( &lines, std::string( shader.shaderSrc ) );

        for( int32_t iLines = 0; iLines < lines.size(); ++iLines )
        {
            const char* line = lines[ iLines ].c_str();

            int32_t index = 0;
            bool foundMatch = false;

            for( int32_t iKey = 0; iKey < sizeof( keyLookupTable ) / sizeof( char ); ++iKey )
            {
                if( strContains( lines[ iLines ], keyLookupTable[ iKey ] ) )
                {
                    index = iKey;
                    foundMatch = true;
                    break;
                }
            }

            if( foundMatch )
            {
                const int32_t pos = lines[ iLines ].find( keyLookupTable[ index ] );

                Config::Log::info( "Position found is %i", pos );

                const int32_t lineLen = strlen( line );

                char* var = new char[ lineLen - pos ];

                int32_t iLine = pos + strlen( keyLookupTable[ index ] );

                for( ; iLine < lineLen; ++iLine )
                {
                    var[ iLine ] = line[ iLine ];
                }

                Config::Log::info( "Shader Variable Found is: %s", var );

                keys.push_back( var );
            }
        }

        return keys;
    }

Taking the Red Pill

So, the idea is that there’s a key look up table containing the most commonly used variable types. First off, the Shader received is a class which holds information about the data, such as its handle, its type (Fragment, Vertex, Texture, etc.), and of course its source. I’m parsing these all from shader files, and not strings.

What happens is there is a grand-daddy loop which iterates over each line parsed in the shader file. In each and every line, if there is a match in the key lookup table, the second loop iterating over keyLookupTable[] will break with an index value taking on the value of iKey (i.e., the index in the array, where the match is found). The loop then breaks.

If a match is found, the position in the line where the match is found (e.g. vec4 or mat3) is taken. From there, using the position stored in pos, we use pos to act as a basis for the length of the variable name, which is done by specifying the required amount of characters in a char array. The required amount is the length of the line, minus the position.

From there a third and final loop then iterates over the line, using a char* to reference it, taking the values in line and copying them to the allocated var character array.

Finally, the std::vector keys inserts var and continues on in the loop, repeating the process.

Notable Concerns

  • I’m using the JNI to get the shader strings, as the shaders themselves are parsed via Java, and then sent through the JNI to C++.
  • Unicode may be of a concern, as I’ve been getting outputs such as this:
    Shader Variable Found is: |uԯ|uԯ/
  • The shader src is passed into a const char* from the JNI through env->GetStringUTFChars()

Conclusion

I’m sure there’s a better way to do this, maybe using std::stringstream or something, but I’m not very familiar with it and would like this algorithm to work somehow or someway. However, if this is the “naive” way to do it, I’m open to suggestion.

The Question

What is the best way to achieve this to get the parsing to work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:59:43+00:00Added an answer on June 3, 2026 at 4:59 am

    Are you sure that you need to do this yourself? GLSL already does this parsing for you, and if you want a list of all the input variables you can get them via glGetActiveAttrib / glGetActiveUniform.

    Just query the number of active attribs/uniforms of a linked shader, and then iterate over each index querying for the name of the input variable.

    http://www.opengl.org/sdk/docs/man/xhtml/glGetActiveAttrib.xml

    http://www.opengl.org/sdk/docs/man/xhtml/glGetActiveUniform.xml

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one is a doozy... My buddy just downloaded the iPhone SDK on his
Howdy fellas, this one is gonna be a doozy: So for awhile I've been
This one is a follow-up question. I filter the top level nodes of a
This one has been puzzling my for some time now. Let's imagine a class
this one is hard to explain! I am writing a python application to be
This one's tough to explain, so I'll try to show what I'm after using
This one is killing me. I'm trying to make a simple console app that
This one really puzzles me, as the code looks completely harmless. IE8 halts script
This one's really getting me down! :( I'm trying to make a nested model
This one is tough to Google. I'm trying to find a solution or API

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.