2004-11-10 17:56:03 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2004 Robert Osfield
|
|
|
|
* Copyright (C) 2003-2004 3Dlabs Inc. Ltd.
|
2003-07-15 18:45:46 +08:00
|
|
|
*
|
|
|
|
* 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/osgGL2/ProgramObject
|
2004-11-10 17:56:03 +08:00
|
|
|
* author: Mike Weiblen 2004-11-09
|
2003-07-15 18:45:46 +08:00
|
|
|
*
|
|
|
|
* See http://www.3dlabs.com/opengl2/ for more information regarding
|
|
|
|
* the OpenGL Shading Language.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef OSGGL2_PROGRAMOBJECT
|
|
|
|
#define OSGGL2_PROGRAMOBJECT 1
|
|
|
|
|
|
|
|
#include <osg/State>
|
|
|
|
#include <osg/StateAttribute>
|
|
|
|
#include <osg/buffered_value>
|
|
|
|
#include <osg/ref_ptr>
|
2003-10-05 19:30:54 +08:00
|
|
|
#include <osg/Vec2>
|
|
|
|
#include <osg/Vec3>
|
|
|
|
#include <osg/Vec4>
|
2003-07-15 18:45:46 +08:00
|
|
|
|
|
|
|
#include <osgGL2/Export>
|
|
|
|
#include <osgGL2/Extensions>
|
2003-10-05 19:30:54 +08:00
|
|
|
#include <osgGL2/UniformValue>
|
2003-07-15 18:45:46 +08:00
|
|
|
|
|
|
|
#include <string>
|
2003-10-05 19:30:54 +08:00
|
|
|
#include <vector>
|
2003-07-15 18:45:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
namespace osgGL2 {
|
|
|
|
|
2003-10-05 19:30:54 +08:00
|
|
|
class ProgramObject;
|
|
|
|
typedef osg::ref_ptr<ProgramObject> ProgramObjectPtr;
|
2003-07-15 18:45:46 +08:00
|
|
|
|
2003-10-05 19:30:54 +08:00
|
|
|
class ShaderObject;
|
|
|
|
typedef osg::ref_ptr<ShaderObject> ShaderObjectPtr;
|
2003-07-15 18:45:46 +08:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2004-01-03 17:06:52 +08:00
|
|
|
/** osgGL2::ProgramObject is an application-level abstraction of the OpenGL Shading Language glProgramObject.
|
|
|
|
* It is an osg::StateAttribute that, when applied, will install an OGLSL
|
|
|
|
* shader program for subsequent rendering.
|
|
|
|
* osgGL2::ShaderObjects containing the actual shader source code are
|
|
|
|
* attached to the ProgramObject, which will then manage the compilation,
|
|
|
|
* linking, and installation of the GL shader program.
|
|
|
|
* ProgramObject will automatically manage per-context instancing of the
|
|
|
|
* internal objects, if that is necessary for a particular display
|
|
|
|
* configuration.
|
|
|
|
*/
|
2003-07-15 18:45:46 +08:00
|
|
|
|
|
|
|
class OSGGL2_EXPORT ProgramObject : public osg::StateAttribute
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ProgramObject();
|
|
|
|
|
|
|
|
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
|
|
|
|
ProgramObject(const ProgramObject& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
|
|
|
|
|
|
|
|
META_StateAttribute(osgGL2, ProgramObject, PROGRAMOBJECT);
|
|
|
|
|
|
|
|
/** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
|
2004-07-29 03:56:22 +08:00
|
|
|
virtual int compare(const osg::StateAttribute& sa) const;
|
2003-07-15 18:45:46 +08:00
|
|
|
|
2004-01-03 17:06:52 +08:00
|
|
|
/** If enabled, install our shader program in the GL pipeline,
|
|
|
|
* performing any shader program rebuild operations that might
|
|
|
|
* be pending. */
|
2003-07-15 18:45:46 +08:00
|
|
|
virtual void apply(osg::State& state) const;
|
|
|
|
|
2004-11-10 17:56:03 +08:00
|
|
|
virtual void compileGLObjects(osg::State& state) const;
|
2004-07-20 13:37:59 +08:00
|
|
|
|
|
|
|
/** release an OpenGL objects in specified graphics context if State
|
|
|
|
object is passed, otherwise release OpenGL objexts for all graphics context if
|
|
|
|
State object pointer NULL.*/
|
|
|
|
virtual void releaseGLObjects(osg::State* state=0) const;
|
2003-07-15 18:45:46 +08:00
|
|
|
|
|
|
|
// data access methods.
|
|
|
|
|
2004-01-03 17:06:52 +08:00
|
|
|
/** Mark us as "dirty" and in need of relinking. */
|
2003-10-05 19:30:54 +08:00
|
|
|
void dirtyProgramObject();
|
|
|
|
|
2004-01-03 17:06:52 +08:00
|
|
|
/** Mark our attached ShaderObjects as "dirty" and in need of
|
|
|
|
* recompilation. */
|
2003-10-05 19:30:54 +08:00
|
|
|
void dirtyShaderObjects();
|
2003-07-15 18:45:46 +08:00
|
|
|
|
2004-01-03 17:06:52 +08:00
|
|
|
/** An override to control whether the shader program will
|
|
|
|
* actually be installed when OSG attempts to apply() */
|
2003-10-05 19:30:54 +08:00
|
|
|
void enable( bool enabled ) { _enabled = enabled; }
|
|
|
|
|
|
|
|
/** Attach a ShaderObject to this ProgramObject */
|
|
|
|
void addShader( ShaderObject* shadObj );
|
|
|
|
|
2004-01-03 17:06:52 +08:00
|
|
|
/** Assign a value to a ProgramObject's uniform variable */
|
2003-10-05 19:30:54 +08:00
|
|
|
void setUniform( const char* uniformName, int value );
|
|
|
|
void setUniform( const char* uniformName, float value );
|
|
|
|
void setUniform( const char* uniformName, osg::Vec2 value );
|
|
|
|
void setUniform( const char* uniformName, osg::Vec3 value );
|
|
|
|
void setUniform( const char* uniformName, osg::Vec4 value );
|
|
|
|
|
|
|
|
inline void setSampler( const char* uniformName, int value )
|
|
|
|
{
|
|
|
|
// emphatic alias for setUniform(int)
|
|
|
|
setUniform( uniformName, static_cast<int>(value) );
|
|
|
|
}
|
2003-07-16 17:52:43 +08:00
|
|
|
|
2004-01-03 17:06:52 +08:00
|
|
|
/** Mark internal GL objects for deletion.
|
|
|
|
* Deletion requests are queued until they can be executed
|
|
|
|
* in the proper GL context. */
|
2003-07-16 17:52:43 +08:00
|
|
|
static void deleteObject(unsigned int contextID, GLhandleARB handle);
|
|
|
|
|
|
|
|
/** flush all the cached glProgramObjects which need to be deleted
|
|
|
|
* in the OpenGL context related to contextID.*/
|
|
|
|
static void flushDeletedGL2Objects(unsigned int contextID,double currentTime, double& availableTime);
|
2003-07-15 18:45:46 +08:00
|
|
|
|
|
|
|
|
2003-10-05 19:30:54 +08:00
|
|
|
protected:
|
2004-01-03 17:06:52 +08:00
|
|
|
/** PCPO is an OSG-internal encapsulation of glProgramObjects per-GL context. */
|
2003-10-05 19:30:54 +08:00
|
|
|
class PerContextProgObj : public osg::Referenced
|
2003-07-15 18:45:46 +08:00
|
|
|
{
|
|
|
|
public:
|
2003-10-05 19:30:54 +08:00
|
|
|
PerContextProgObj(const ProgramObject* progObj, unsigned int contextID);
|
2003-07-15 18:45:46 +08:00
|
|
|
PerContextProgObj(const PerContextProgObj& rhs);
|
|
|
|
|
2003-10-05 19:30:54 +08:00
|
|
|
GLhandleARB& getHandle() {return _glProgObjHandle;}
|
2003-07-15 18:45:46 +08:00
|
|
|
|
|
|
|
bool isDirty() const {return _dirty;}
|
|
|
|
void markAsDirty() {_dirty = true; }
|
2003-10-05 19:30:54 +08:00
|
|
|
void build();
|
2003-07-15 18:45:46 +08:00
|
|
|
void use() const;
|
|
|
|
|
2004-01-03 17:06:52 +08:00
|
|
|
/** Add a list of UniformValues to our per-context queue */
|
2003-10-05 19:30:54 +08:00
|
|
|
void updateUniforms( const UniformValueList& univalList );
|
2003-07-15 18:45:46 +08:00
|
|
|
|
2004-01-03 17:06:52 +08:00
|
|
|
/** Apply our queue of pending UniformValue updates to the glProgramObjects */
|
|
|
|
void applyUniformValues();
|
2003-10-05 19:30:54 +08:00
|
|
|
|
|
|
|
protected: /*methods*/
|
|
|
|
PerContextProgObj();
|
2003-07-15 18:45:46 +08:00
|
|
|
~PerContextProgObj();
|
|
|
|
|
2003-10-05 19:30:54 +08:00
|
|
|
protected: /*data*/
|
2004-01-03 17:06:52 +08:00
|
|
|
/** Pointer to our parent ProgramObject */
|
2003-10-05 19:30:54 +08:00
|
|
|
const ProgramObject* _progObj;
|
2004-01-03 17:06:52 +08:00
|
|
|
/** Pointer to this context's extension functions */
|
2003-07-15 18:45:46 +08:00
|
|
|
osg::ref_ptr<Extensions> _extensions;
|
2004-01-03 17:06:52 +08:00
|
|
|
/** Handle to the actual glProgramObject */
|
2003-10-05 19:30:54 +08:00
|
|
|
GLhandleARB _glProgObjHandle;
|
2004-01-03 17:06:52 +08:00
|
|
|
/** Do we need to be linked? */
|
2003-07-15 18:45:46 +08:00
|
|
|
bool _dirty;
|
2004-01-03 17:06:52 +08:00
|
|
|
/** Queue of UniformValues awaiting assignment */
|
2003-10-05 19:30:54 +08:00
|
|
|
UniformValueList _univalList;
|
|
|
|
const unsigned int _contextID;
|
2003-07-15 18:45:46 +08:00
|
|
|
};
|
|
|
|
|
2003-10-05 19:30:54 +08:00
|
|
|
protected: /*methods*/
|
|
|
|
virtual ~ProgramObject();
|
2004-01-03 17:06:52 +08:00
|
|
|
/** Get the PCPO for a particular GL context */
|
2003-07-15 18:45:46 +08:00
|
|
|
PerContextProgObj* getPCPO(unsigned int contextID) const;
|
2004-01-03 17:06:52 +08:00
|
|
|
|
|
|
|
/** Per frame, copy the list of pending UniformValue updates to
|
|
|
|
* each of the PCPOs. */
|
2003-10-05 19:30:54 +08:00
|
|
|
void updateUniforms( int frameNumber ) const;
|
|
|
|
|
|
|
|
protected: /*data*/
|
|
|
|
bool _enabled;
|
2004-07-29 03:56:22 +08:00
|
|
|
|
|
|
|
typedef std::vector< ShaderObjectPtr > ShaderObjectList;
|
|
|
|
ShaderObjectList _shaderObjectList;
|
2003-10-05 19:30:54 +08:00
|
|
|
mutable osg::buffered_value< osg::ref_ptr<PerContextProgObj> > _pcpoList;
|
|
|
|
mutable int _frameNumberOfLastPCPOUpdate;
|
|
|
|
mutable UniformValueList _univalList;
|
2004-01-03 17:06:52 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
const ProgramObject& operator=(const ProgramObject&);
|
2003-07-15 18:45:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2004-01-03 17:06:52 +08:00
|
|
|
/** osgGL2::ShaderObject is an application-level abstraction of the OpenGL Shading Language glShaderObject.
|
|
|
|
* It is a container to load the shader source code text and manage its
|
|
|
|
* compilation.
|
|
|
|
* A ShaderObject may be attached to more than one osgGL2::ProgramObject.
|
|
|
|
* ShaderObject will automatically manage per-context instancing of the
|
|
|
|
* internal objects, if that is necessary for a particular display
|
|
|
|
* configuration.
|
|
|
|
*/
|
2003-07-15 18:45:46 +08:00
|
|
|
|
|
|
|
class OSGGL2_EXPORT ShaderObject : public osg::Object
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
enum Type {
|
|
|
|
VERTEX = GL_VERTEX_SHADER_ARB,
|
|
|
|
FRAGMENT = GL_FRAGMENT_SHADER_ARB,
|
|
|
|
UNKNOWN = -1
|
|
|
|
};
|
|
|
|
|
|
|
|
ShaderObject();
|
|
|
|
ShaderObject(Type type);
|
|
|
|
ShaderObject(Type type, const char* sourceText);
|
|
|
|
|
|
|
|
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
|
|
|
|
ShaderObject(const ShaderObject& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
|
|
|
|
META_Object(osgGL2, ShaderObject);
|
|
|
|
|
2004-07-29 03:56:22 +08:00
|
|
|
int compare(const ShaderObject& sa) const;
|
|
|
|
|
2003-07-15 18:45:46 +08:00
|
|
|
// data access methods.
|
|
|
|
|
2004-01-03 17:06:52 +08:00
|
|
|
/** Load the ShaderObject's source code text from a string. */
|
2003-07-15 18:45:46 +08:00
|
|
|
void setShaderSource( const char* sourceText );
|
2004-01-03 17:06:52 +08:00
|
|
|
|
|
|
|
/** Retreive the source code text */
|
2003-07-15 18:45:46 +08:00
|
|
|
inline const std::string& getShaderSource() const {return _shaderSource; }
|
2004-01-03 17:06:52 +08:00
|
|
|
|
|
|
|
/** Load the ShaderObject's source code text from a file. */
|
2003-07-15 18:45:46 +08:00
|
|
|
bool loadShaderSourceFromFile( const char* fileName );
|
2004-01-03 17:06:52 +08:00
|
|
|
|
|
|
|
/** Get the ShaderObject type as an enum. */
|
2003-10-05 19:30:54 +08:00
|
|
|
inline Type getType() const { return _type; }
|
2004-01-03 17:06:52 +08:00
|
|
|
|
|
|
|
/** Get the ShaderObject type as a descriptive string. */
|
2003-10-05 19:30:54 +08:00
|
|
|
const char* getTypename() const;
|
2003-07-15 18:45:46 +08:00
|
|
|
|
2004-01-03 17:06:52 +08:00
|
|
|
/** Mark us as "dirty" and in need of recompilation */
|
2003-07-15 18:45:46 +08:00
|
|
|
void dirtyShaderObject();
|
|
|
|
|
2004-01-03 17:06:52 +08:00
|
|
|
/** Perform a recompilation of all our PCSOs */
|
2003-10-05 19:30:54 +08:00
|
|
|
void build(unsigned int contextID) const;
|
2004-01-03 17:06:52 +08:00
|
|
|
|
|
|
|
/** For a given GL context, attach a glShaderObject to a glProgramObject */
|
2003-07-15 18:45:46 +08:00
|
|
|
void attach(unsigned int contextID, GLhandleARB progObj) const;
|
|
|
|
|
|
|
|
|
2003-10-05 19:30:54 +08:00
|
|
|
protected:
|
2004-01-03 17:06:52 +08:00
|
|
|
/** PCSO is an OSG-internal encapsulation of glShaderObjects per-GL context. */
|
2003-10-05 19:30:54 +08:00
|
|
|
class PerContextShaderObj : public osg::Referenced
|
2003-07-15 18:45:46 +08:00
|
|
|
{
|
|
|
|
public:
|
2003-10-05 19:30:54 +08:00
|
|
|
PerContextShaderObj(const ShaderObject* shadObj, unsigned int contextID);
|
2003-07-15 18:45:46 +08:00
|
|
|
PerContextShaderObj(const PerContextShaderObj& rhs);
|
|
|
|
|
2003-10-05 19:30:54 +08:00
|
|
|
GLhandleARB& getHandle() {return _glShaderObjHandle;}
|
2003-07-15 18:45:46 +08:00
|
|
|
|
|
|
|
bool isDirty() const {return _dirty;}
|
|
|
|
void markAsDirty() {_dirty = true; }
|
2003-10-05 19:30:54 +08:00
|
|
|
void build();
|
2003-07-15 18:45:46 +08:00
|
|
|
|
2004-01-03 17:06:52 +08:00
|
|
|
/** Attach our glShaderObject to a glProgramObject */
|
2003-10-05 19:30:54 +08:00
|
|
|
void attach(GLhandleARB progObj) const;
|
2003-07-15 18:45:46 +08:00
|
|
|
|
2003-10-05 19:30:54 +08:00
|
|
|
protected: /*methods*/
|
|
|
|
PerContextShaderObj();
|
2003-07-15 18:45:46 +08:00
|
|
|
~PerContextShaderObj();
|
|
|
|
|
2003-10-05 19:30:54 +08:00
|
|
|
protected: /*data*/
|
2004-01-03 17:06:52 +08:00
|
|
|
/** Pointer to our parent ShaderObject */
|
2003-10-05 19:30:54 +08:00
|
|
|
const ShaderObject* _shadObj;
|
2004-01-03 17:06:52 +08:00
|
|
|
/** Pointer to this context's extension functions. */
|
2003-07-15 18:45:46 +08:00
|
|
|
osg::ref_ptr<Extensions> _extensions;
|
2004-01-03 17:06:52 +08:00
|
|
|
/** Handle to the actual glShaderObject. */
|
2003-10-05 19:30:54 +08:00
|
|
|
GLhandleARB _glShaderObjHandle;
|
2004-01-03 17:06:52 +08:00
|
|
|
/** Do we need to be recompiled? */
|
2003-07-15 18:45:46 +08:00
|
|
|
bool _dirty;
|
2003-10-05 19:30:54 +08:00
|
|
|
const unsigned int _contextID;
|
2003-07-15 18:45:46 +08:00
|
|
|
};
|
|
|
|
|
2003-10-05 19:30:54 +08:00
|
|
|
protected: /*methods*/
|
|
|
|
virtual ~ShaderObject();
|
2003-07-15 18:45:46 +08:00
|
|
|
PerContextShaderObj* getPCSO(unsigned int contextID) const;
|
|
|
|
|
2003-10-05 19:30:54 +08:00
|
|
|
friend void ProgramObject::addShader( ShaderObject* shadObj ); // to access addProgObjRef()
|
|
|
|
void addProgObjRef( ProgramObject* progObj );
|
|
|
|
|
|
|
|
protected: /*data*/
|
|
|
|
Type _type;
|
|
|
|
std::string _shaderSource;
|
|
|
|
std::vector< ProgramObjectPtr > _programObjectList;
|
|
|
|
mutable osg::buffered_value< osg::ref_ptr<PerContextShaderObj> > _pcsoList;
|
2004-01-03 17:06:52 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
const ShaderObject& operator=(const ShaderObject&);
|
2003-07-15 18:45:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2004-01-03 17:06:52 +08:00
|
|
|
/*EOF*/
|