c++openscenegraph

How to disable State attribute in Openscenegraph geometry node?


I tried adding a fragment shader program to OSG::Geometry node as below.

    osg::ref_ptr<osg::Geometry> node = new osg::Geometry();
    osg::ref_ptr<osg::Program> m_program= new osg::Program;
    osg::ref_ptr<osg::Shader> fragShader = new osg::Shader(osg::Shader::FRAGMENT);
    //TODO Add LOG if shader failed to load
    if (!fragShader->loadShaderSourceFromFile("Shaders/Sel.frag"))
        return;
    m_program->addShader(fragShader);
    osg::StateSet* state = node->getOrCreateStateSet();
    state->setAttributeAndModes(m_program, osg::StateAttribute::ON);

At some point, I removed the program using the below method

state->removeAttribute(m_program);

After removing the attribute, the next immediate render frame loop frame() throws an exception as below.

enter image description here

I tried to debug the openscenegraph and found the map which is causing the issue.

Header file: State

Method name :

inline void State::applyAttributeList(AttributeMap& attributeMap,const StateSet::AttributeList& attributeList)

The variable which caused the exception.

attributeMap

if I am not removing the program state attribute, it is working fine. Only removing the attribute, causing the issue.


Solution

  • This is most likely due to the fact that you're updating the stateset while the rendering thread(s) is still using it to finish the dispatch of the previous frame. To make sure this is the cause of the crash, you can try to run the application with OSG SingleThreaded scheme (either set it via code on the viewer or set the OSG_THREADING env var).
    If that is the cause and you want to use the one of the other threading schemes, you can set the stateset's data variance to DYNAMIC - this will ensure that the rendering thread is done with your stateset before the next update callback is called for the new frame.
    This topic has been discussed many times in the osg-users mailing list, you may check the archives for further info.