[osg-submissions] Fix for removing shaders from programs
Per Fahlberg
perfa at remograph.com
Mon Nov 19 11:53:07 PST 2007
Hi Robert,
Sorry about that, here are the files again.
Regards,
Per
Robert Osfield wrote:
> Hi Per,
>
> The headers came through inlined, could zip the files up and then reattach them.
>
> Cheers,
> Robert.
>
> On Nov 19, 2007 6:56 PM, Per Fahlberg <perfa at remograph.com> wrote:
>
>> Hi,
>>
>> Attached is a fix allowing removal of shaders from a program after it is
>> first compiled. It will also allow new shaders to be attached after the
>> program is first compiled.
>> The changes are made from svn revision 7594.
>>
>> Regards,
>> Per
>>
>>
>> /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
>> * Copyright (C) 2003-2005 3Dlabs Inc. Ltd.
>> * Copyright (C) 2004-2005 Nathan Cournia
>> *
>> * This application is open source and may be redistributed and/or modified
>> * freely and without restriction, both in commericial and non commericial
>> * applications, as long as this copyright notice is maintained.
>> *
>> * This application is distributed in the hope that it will be useful,
>> * but WITHOUT ANY WARRANTY; without even the implied warranty of
>> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
>> */
>>
>> /* file: include/osg/Program
>> * author: Mike Weiblen 2006-03-25
>> */
>>
>> #ifndef OSG_PROGRAM
>> #define OSG_PROGRAM 1
>>
>> #include <string>
>> #include <vector>
>> #include <map>
>>
>> #include <osg/buffered_value>
>> #include <osg/ref_ptr>
>> #include <osg/Uniform>
>> #include <osg/Shader>
>> #include <osg/StateAttribute>
>>
>> namespace osg {
>>
>> class State;
>>
>> ///////////////////////////////////////////////////////////////////////////
>> /** osg::Program is an application-level abstraction of an OpenGL glProgram.
>> * It is an osg::StateAttribute that, when applied, will activate a
>> * glProgram for subsequent rendering.
>> * osg::Shaders containing the actual shader source code are
>> * attached to a Program, which will then manage the compilation,
>> * linking, and activation of the GLSL program.
>> * osg::Program will automatically manage per-context instancing of the
>> * OpenGL glPrograms, if that is necessary for a particular display
>> * configuration.
>> */
>>
>> class OSG_EXPORT Program : public osg::StateAttribute
>> {
>> public:
>> Program();
>>
>> /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
>> Program(const Program& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
>>
>> META_StateAttribute(osg, Program, PROGRAM);
>>
>> /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
>> virtual int compare(const osg::StateAttribute& sa) const;
>>
>> /** If enabled, activate our program in the GL pipeline,
>> * performing any rebuild operations that might be pending. */
>> virtual void apply(osg::State& state) const;
>>
>> /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
>> virtual void setThreadSafeRefUnref(bool threadSafe);
>>
>> /** Compile program and associated shaders.*/
>> virtual void compileGLObjects(osg::State& state) const;
>>
>> /** Resize any per context GLObject buffers to specified size. */
>> virtual void resizeGLObjectBuffers(unsigned int maxSize);
>>
>> /** release OpenGL objects in specified graphics context if State
>> object is passed, otherwise release OpenGL objects for all graphics context if
>> State object pointer NULL.*/
>> virtual void releaseGLObjects(osg::State* state=0) const;
>>
>> /** Mark our PCSOs as needing relink */
>> void dirtyProgram();
>>
>> /** Attach an osg::Shader to this osg::Program.
>> * Mark Program as needing relink. Return true for success */
>> bool addShader( Shader* shader );
>>
>> unsigned int getNumShaders() const { return _shaderList.size(); }
>>
>> Shader* getShader( unsigned int i ) { return _shaderList[i].get(); }
>> const Shader* getShader( unsigned int i ) const { return _shaderList[i].get(); }
>>
>> /** Remove osg::Shader from this osg::Program.
>> * Mark Program as needing relink. Return true for success */
>> bool removeShader( Shader* shader );
>>
>> /** Add an attribute location binding. */
>> void addBindAttribLocation( const std::string& name, GLuint index );
>>
>> /** Remove an attribute location binding. */
>> void removeBindAttribLocation( const std::string& name );
>>
>> /** Add an frag data location binding. See EXT_gpu_shader4 for BindFragDataLocationEXT */
>> void addBindFragDataLocation( const std::string& name, GLuint index );
>>
>> /** Remove an frag data location binding. */
>> void removeBindFragDataLocation( const std::string& name );
>>
>> typedef std::map<std::string,GLuint> AttribBindingList;
>> typedef std::map<std::string,GLuint> FragDataBindingList;
>>
>> const AttribBindingList& getAttribBindingList() const { return _attribBindingList; }
>> const FragDataBindingList& getFragDataBindingList() const { return _fragDataBindingList; }
>>
>> /** Return true if this Program represents "fixed-functionality" rendering */
>> bool isFixedFunction() const;
>>
>> /** Query InfoLog from a glProgram */
>> bool getGlProgramInfoLog(unsigned int contextID, std::string& log) const;
>>
>> /** Mark internal glProgram for deletion.
>> * Deletion requests are queued until they can be executed
>> * in the proper GL context. */
>> static void deleteGlProgram(unsigned int contextID, GLuint program);
>>
>> /** flush all the cached glPrograms which need to be deleted
>> * in the OpenGL context related to contextID.*/
>> static void flushDeletedGlPrograms(unsigned int contextID,double currentTime, double& availableTime);
>>
>> struct ActiveVarInfo {
>> ActiveVarInfo() : _location(-1), _type(Uniform::UNDEFINED), _size(-1) {}
>> ActiveVarInfo( GLint loc, GLenum type, GLint size ) : _location(loc), _type(type), _size(size) {}
>> GLint _location;
>> GLenum _type;
>> GLint _size;
>> };
>> typedef std::map< std::string, ActiveVarInfo > ActiveVarInfoMap;
>> const ActiveVarInfoMap& getActiveUniforms(unsigned int contextID) const;
>> const ActiveVarInfoMap& getActiveAttribs(unsigned int contextID) const;
>>
>> public:
>>
>> // make PerContextProgram a friend to allow it access Program's protected
>> // methods and member variables.
>> class PerContextProgram;
>> friend class PerContextProgram;
>>
>> /** PerContextProgram (PCP) is an OSG-internal encapsulation of glPrograms per-GL context. */
>> class OSG_EXPORT PerContextProgram : public osg::Referenced
>> {
>> public:
>>
>>
>> PerContextProgram(const Program* program, unsigned int contextID);
>>
>> GLuint getHandle() const {return _glProgramHandle;}
>>
>> void requestLink();
>> void linkProgram();
>> bool validateProgram();
>> bool needsLink() const {return _needsLink;}
>> bool isLinked() const {return _isLinked;}
>> bool getInfoLog( std::string& infoLog ) const;
>>
>> void useProgram() const;
>>
>> void resetAppliedUniforms() const
>> {
>> for(LastAppliedUniformList::iterator itr=_lastAppliedUniformList.begin();
>> itr!=_lastAppliedUniformList.end();
>> ++itr)
>> {
>> (*itr).first = 0;
>> (*itr).second = 0;
>> }
>> }
>>
>>
>> inline void apply(const Uniform& uniform) const
>> {
>> GLint location = getUniformLocation(uniform.getName());
>> if (location>=0)
>> {
>> if ((unsigned int)location>=_lastAppliedUniformList.size()) _lastAppliedUniformList.resize(location+1);
>> const Uniform* lastAppliedUniform = _lastAppliedUniformList[location].first;
>> if (lastAppliedUniform != &uniform)
>> {
>> // new attribute
>> uniform.apply(_extensions.get(),location);
>> _lastAppliedUniformList[location].first = &uniform;
>> _lastAppliedUniformList[location].second = uniform.getModifiedCount();
>> }
>> else if (_lastAppliedUniformList[location].second != uniform.getModifiedCount())
>> {
>> // existing attribute has been modified
>> uniform.apply(_extensions.get(),location);
>> _lastAppliedUniformList[location].first = &uniform;
>> _lastAppliedUniformList[location].second = uniform.getModifiedCount();
>> }
>> }
>> }
>>
>> const ActiveVarInfoMap& getActiveUniforms() const {return _uniformInfoMap;}
>> const ActiveVarInfoMap& getActiveAttribs() const {return _attribInfoMap;}
>>
>> inline GLint getUniformLocation( const std::string& name ) const { ActiveVarInfoMap::const_iterator itr = _uniformInfoMap.find(name); return (itr!=_uniformInfoMap.end()) ? itr->second._location : -1; }
>> inline GLint getAttribLocation( const std::string& name ) const { ActiveVarInfoMap::const_iterator itr = _attribInfoMap.find(name); return (itr!=_attribInfoMap.end()) ? itr->second._location : -1; }
>>
>> inline void addShaderToAttach(Shader *shader)
>> {
>> _shadersToAttach.push_back(shader);
>> }
>>
>> inline void addShaderToDetach(Shader *shader)
>> {
>> _shadersToDetach.push_back(shader);
>> }
>>
>> protected: /*methods*/
>> ~PerContextProgram();
>>
>> protected: /*data*/
>> /** Pointer to our parent Program */
>> const Program* _program;
>> /** Pointer to this context's extension functions */
>> osg::ref_ptr<GL2Extensions> _extensions;
>> /** Handle to the actual OpenGL glProgram */
>> GLuint _glProgramHandle;
>> /** Does our glProgram need to be linked? */
>> bool _needsLink;
>> /** Is our glProgram successfully linked? */
>> bool _isLinked;
>> const unsigned int _contextID;
>>
>> ActiveVarInfoMap _uniformInfoMap;
>> ActiveVarInfoMap _attribInfoMap;
>>
>> typedef std::pair<const osg::Uniform*, unsigned int> UniformModifiedCountPair;
>> typedef std::vector<UniformModifiedCountPair> LastAppliedUniformList;
>> mutable LastAppliedUniformList _lastAppliedUniformList;
>>
>> typedef std::vector< ref_ptr<Shader> > ShaderList;
>> ShaderList _shadersToDetach;
>> ShaderList _shadersToAttach;
>>
>> private:
>> PerContextProgram(); // disallowed
>> PerContextProgram(const PerContextProgram&); // disallowed
>> PerContextProgram& operator=(const PerContextProgram&); // disallowed
>> };
>>
>> /** Get the PCP for a particular GL context */
>> PerContextProgram* getPCP(unsigned int contextID) const;
>>
>> protected: /*methods*/
>> virtual ~Program();
>>
>> protected: /*data*/
>>
>> mutable osg::buffered_value< osg::ref_ptr<PerContextProgram> > _pcpList;
>> AttribBindingList _attribBindingList;
>> FragDataBindingList _fragDataBindingList;
>>
>> typedef std::vector< ref_ptr<Shader> > ShaderList;
>> ShaderList _shaderList;
>>
>> private:
>> Program& operator=(const Program&); // disallowed
>> };
>>
>> }
>>
>> #endif
>>
>> /*EOF*/
>>
>> /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
>> * Copyright (C) 2003-2005 3Dlabs Inc. Ltd.
>> * Copyright (C) 2004-2005 Nathan Cournia
>> *
>> * This application is open source and may be redistributed and/or modified
>> * freely and without restriction, both in commericial and non commericial
>> * applications, as long as this copyright notice is maintained.
>> *
>> * This application is distributed in the hope that it will be useful,
>> * but WITHOUT ANY WARRANTY; without even the implied warranty of
>> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
>> */
>>
>> /* file: include/osg/Shader
>> * author: Mike Weiblen 2005-06-15
>> */
>>
>> #ifndef OSG_SHADER
>> #define OSG_SHADER 1
>>
>>
>> #include <osg/GL2Extensions>
>> #include <osg/Object>
>> #include <osg/buffered_value>
>>
>> #include <set>
>>
>> namespace osg {
>>
>> class Program;
>>
>> ///////////////////////////////////////////////////////////////////////////
>> /** osg::Shader is an application-level abstraction of an OpenGL glShader.
>> * It is a container to load the shader source code text and manage its
>> * compilation.
>> * An osg::Shader may be attached to more than one osg::Program.
>> * Shader will automatically manage per-context instancing of the
>> * internal objects, if that is necessary for a particular display
>> * configuration.
>> */
>>
>> class OSG_EXPORT Shader : public osg::Object
>> {
>> public:
>>
>> enum Type {
>> VERTEX = GL_VERTEX_SHADER,
>> FRAGMENT = GL_FRAGMENT_SHADER,
>> UNDEFINED = -1
>> };
>>
>> Shader( Type type = UNDEFINED);
>> Shader( Type type, const std::string& source );
>>
>> /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
>> Shader(const Shader& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
>>
>> META_Object(osg, Shader);
>>
>> int compare(const Shader& rhs) const;
>>
>> bool setType( Type t );
>>
>>
>> /** Load the Shader's source code text from a string. */
>> void setShaderSource( const std::string& sourceText );
>>
>> /** Read shader source from file and then constructor shader of specified type.
>> * Return the resulting Shader or 0 if no valid shader source code be read.*/
>> static Shader* readShaderFile( Type type, const std::string& fileName );
>>
>> /** Load the Shader's source code text from a file. */
>> bool loadShaderSourceFromFile( const std::string& fileName );
>>
>> /** Query the shader's source code text */
>> inline const std::string& getShaderSource() const { return _shaderSource; }
>>
>> /** Get the Shader type as an enum. */
>> inline Type getType() const { return _type; }
>>
>> /** Get the Shader type as a descriptive string. */
>> const char* getTypename() const;
>>
>> /** Resize any per context GLObject buffers to specified size. */
>> virtual void resizeGLObjectBuffers(unsigned int maxSize);
>>
>> /** release OpenGL objects in specified graphics context if State
>> object is passed, otherwise release OpenGL objects for all graphics context if
>> State object pointer NULL.*/
>> void releaseGLObjects(osg::State* state=0) const;
>>
>> /** Mark our PCSs as needing recompilation.
>> * Also mark Programs that depend on us as needing relink */
>> void dirtyShader();
>>
>> /** If needed, compile the PCS's glShader */
>> void compileShader(unsigned int contextID) const;
>>
>> /** For a given GL context, attach a glShader to a glProgram */
>> void attachShader(unsigned int contextID, GLuint program) const;
>>
>> /** For a given GL context, detach a glShader to a glProgram */
>> void detachShader(unsigned int contextID, GLuint program) const;
>>
>> /** Query InfoLog from a glShader */
>> bool getGlShaderInfoLog(unsigned int contextID, std::string& log) const;
>>
>> /** Mark internal glShader for deletion.
>> * Deletion requests are queued tuntil they can be executed
>> * in the proper GL context. */
>> static void deleteGlShader(unsigned int contextID, GLuint shader);
>>
>> /** flush all the cached glShaders which need to be deleted
>> * in the OpenGL context related to contextID.*/
>> static void flushDeletedGlShaders(unsigned int contextID,double currentTime, double& availableTime);
>>
>> static Shader::Type getTypeId( const std::string& tname );
>>
>> protected:
>> /** PerContextShader (PCS) is an OSG-internal encapsulation of glShader per-GL context. */
>> class PerContextShader : public osg::Referenced
>> {
>> public:
>> PerContextShader(const Shader* shader, unsigned int contextID);
>>
>> GLuint getHandle() const {return _glShaderHandle;}
>>
>> void requestCompile();
>> void compileShader();
>> bool needsCompile() const {return _needsCompile;}
>> bool isCompiled() const {return _isCompiled;}
>> bool getInfoLog( std::string& infoLog ) const;
>>
>> /** Attach our glShader to a glProgram */
>> void attachShader(GLuint program) const;
>>
>> /** Detach our glShader from a glProgram */
>> void detachShader(GLuint program) const;
>>
>> protected: /*methods*/
>> ~PerContextShader();
>>
>> protected: /*data*/
>> /** Pointer to our parent osg::Shader */
>> const Shader* _shader;
>> /** Pointer to this context's extension functions. */
>> osg::ref_ptr<osg::GL2Extensions> _extensions;
>> /** Handle to the actual glShader. */
>> GLuint _glShaderHandle;
>> /** Does our glShader need to be recompiled? */
>> bool _needsCompile;
>> /** Is our glShader successfully compiled? */
>> bool _isCompiled;
>> const unsigned int _contextID;
>>
>> private:
>> PerContextShader(); // disallowed
>> PerContextShader(const PerContextShader&); // disallowed
>> PerContextShader& operator=(const PerContextShader&); // disallowed
>> };
>>
>> protected: /*methods*/
>> virtual ~Shader();
>>
>> PerContextShader* getPCS(unsigned int contextID) const;
>>
>> friend class osg::Program;
>> bool addProgramRef( osg::Program* program );
>> bool removeProgramRef( osg::Program* program );
>>
>> protected: /*data*/
>> Type _type;
>> std::string _shaderSource;
>> /** osg::Programs that this osg::Shader is attached to */
>> typedef std::set< osg::Program* > ProgramSet;
>> ProgramSet _programSet;
>> mutable osg::buffered_value< osg::ref_ptr<PerContextShader> > _pcsList;
>>
>> private:
>> Shader& operator=(const Shader&); // disallowed
>> };
>>
>> }
>>
>> #endif
>>
>> /*EOF*/
>>
>> _______________________________________________
>> osg-submissions mailing list
>> osg-submissions at lists.openscenegraph.org
>> http://lists.openscenegraph.org/listinfo.cgi/osg-submissions-openscenegraph.org
>>
>>
>>
> _______________________________________________
> osg-submissions mailing list
> osg-submissions at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-submissions-openscenegraph.org
>
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: osg-shaderfix.tgz
Type: application/x-compressed-tar
Size: 17269 bytes
Desc: not available
Url : http://lists.openscenegraph.org/pipermail/osg-submissions-openscenegraph.org/attachments/20071119/ca7ceb50/attachment-0001.bin
More information about the osg-submissions
mailing list