[osg-users] Correct time to check shader/program info log

Wojciech Lewandowski w.p.lewandowski at gmail.com
Wed Feb 24 12:21:15 PST 2021


Hi James,

Its not direct answer to your questions but a code snippet overloading
Program which I used to change shader variant depending on compilation/link
result (from most advanced to more basic fallbacks).  This approach was
quite simple I tested Program compilation result including compilation/link
log obtained from PCP(PerContextProgram ?) by simply checking if my Program
was really applied after Program::apply() and if not adopted fallback to
less demanding shaders. Not sure if this will solve your problem but
perhaps will be a step forward to proper solution.

class MyProgram: public osg::Program
{
public:
    MyProgram( ): _shaderVariant( DefaultShaderVariant )
    {
        osg::Shader * vertShader = osgDB::readShaderFile(
osg::Shader::VERTEX, "MyVertexShader.glsl" );
        osg::Shader * fragShader = osgDB::readShaderFile(
osg::Shader::FRAGMENT, "MyFragmentShader.glsl" );

        addShader( vertShader );
        addShader( fragShader );

        notified.resize( DefaultShaderVariant + 1 );
    }

    void setDigitDefine( std::string MACRO, int DIGIT ) const
    {
          //  For brevity code removed but
          //  this function simply checks shader sources and
          //   finds all ocurences of
          // #define MACRO DEFAULT_DIGIT
          //  and replaces them  to
          // #define MACRO DIGIT
          // to select shader codepath dependant on macro value
    }

    void apply( osg::State& state ) const
    {
        while( _shaderVariant > 0 )
        {
            osg::Program::apply( state );

            // Break if program was applied ie its not null
            if( state.getLastAppliedProgramObject() != NULL )
            {
                if( !notified[0] )
                {
                    std::cout<< "INFO: MyProgram - GLSL Compilation
Succeeded." << std::endl;
                    std::cout<< "  Shader variant: " << _shaderVariant <<
std::endl;
                    std::cout<< "  GL Vendor:      " <<
glGetString(GL_VENDOR) << std::endl;
                    std::cout<< "  GL Renderer:    " <<
glGetString(GL_RENDERER) << std::endl;
                    std::cout<< "  GL Version:     " <<
glGetString(GL_VERSION) << std::endl;
                    notified[0] = true;
                }
                break;
            }

            if( !notified[_shaderVariant] )
            {
                std::string infoLog;
                getPCP( state )->getInfoLog( infoLog );
                std::cout<< "ERROR: MyProgram - GLSL Compilation Failed."
<< std::endl;
                std::cout<< "  Shader variant: " << _shaderVariant <<
std::endl;
                std::cout<< "  GL Vendor: " << glGetString(GL_VENDOR) <<
std::endl;
                std::cout<< "  GL Renderer: " << glGetString(GL_RENDERER)
<< std::endl;
                std::cout<< "  GL Version: " << glGetString(GL_VERSION) <<
std::endl;
                std::cout<< infoLog << std::endl;

                notified[_shaderVariant] = true;
            }

            // Switch to fallback variants if program link failed
            setDigitDefine( "SHADER_VARIANT", --_shaderVariant );
        }
    }

protected:
    mutable int _shaderVariant;
    static const int DefaultShaderVariant = 2;
    mutable std::vector< bool > notified;
};

Cheers,
Wojtek Lewandowski

śr., 24 lut 2021 o 10:30 James Turner <zakalawe at mac.com> napisał(a):

> Hello,
>
> I’m trying to extract shader compile and link info logs at runtime, so I
> can log+report them.
>
> I note osg::Program::getGlProgramInfoLog exists, obviously it takes a
> context ID since the PerContextProgram is what has the actual errors.
>
> Two things I need help with:
>
> 1) does the program log also contain shader compile errors, or is this
> only the link log? I don’t see a corresponding APi on osg::Shader, is why I
> ask
>
> 2) *when* can I call the log functions and expect to get valid results?
> Given the OSg drawing model, obviously the log won’t be available
> immediately. Do I need to use a DrawCallback to check the log after the
> first time the Program has been used?
>
> I looked for examples of using getGlProgramInfoLog but unfortunately
> couldn't find any, maybe pointing me at one would answer both of these
> points.
>
> Kind regards,
> James Turner
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20210224/9d4d65b4/attachment.html>


More information about the osg-users mailing list