Parsing a GLSL Shader String to Find Variable Names in Android NDK

This is doozy. For the sake of proper explanation, let me explain what I'm trying to do. I will follow the list of codes and then explain the code at the stern.

purpose

I am trying to get the variable names in every GLSL shader file that I have. Right now, I have only one vertex shader, as well as a fragment shader in addition to it. The purpose of this is so that I can dynamically bind values ​​to shaders without having to enter each name of each variable.

the 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 a red pill

, , , . -, - , , ​​ , (, , ..) , , . , .

, , , . , , , keyLookupTable[], , iKey (.. , ). .

, , (, vec4 mat3). , , pos, pos , , char. - , .

, char* , line var.

, std::vector var , .

  • JNI , Java, JNI ++.
  • Unicode , : Shader Variable Found is: |uԯ|uԯ/
  • src const char * JNI env->GetStringUTFChars()

, , , std::stringstream - , , - - . , "" , .

, ?

+5

All Articles