2005-02-24 21:33:35 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield
|
|
|
|
* Copyright (C) 2003-2005 3Dlabs Inc. Ltd.
|
2005-03-24 17:37:45 +08:00
|
|
|
* Copyright (C) 2004-2005 Nathan Cournia
|
2005-02-24 21:33:35 +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.
|
|
|
|
*/
|
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
/* file: include/osg/Program
|
2005-07-14 18:27:00 +08:00
|
|
|
* author: Mike Weiblen 2005-07-01
|
2005-02-24 21:33:35 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSG_PROGRAM
|
|
|
|
#define OSG_PROGRAM 1
|
|
|
|
|
2005-03-24 17:37:45 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2005-04-04 18:08:15 +08:00
|
|
|
#include <map>
|
2005-03-24 17:37:45 +08:00
|
|
|
|
2005-02-24 21:33:35 +08:00
|
|
|
#include <osg/buffered_value>
|
|
|
|
#include <osg/ref_ptr>
|
|
|
|
#include <osg/Uniform>
|
2005-05-12 03:59:21 +08:00
|
|
|
#include <osg/Shader>
|
2005-03-24 17:37:45 +08:00
|
|
|
#include <osg/StateAttribute>
|
2005-02-24 21:33:35 +08:00
|
|
|
|
|
|
|
namespace osg {
|
|
|
|
|
2005-03-12 04:29:21 +08:00
|
|
|
class State;
|
2005-02-24 21:33:35 +08:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/** 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.
|
2005-03-24 17:37:45 +08:00
|
|
|
* osg::Program will automatically manage per-context instancing of the
|
|
|
|
* OpenGL glPrograms, if that is necessary for a particular display
|
2005-02-24 21:33:35 +08:00
|
|
|
* configuration.
|
|
|
|
*/
|
|
|
|
|
2005-04-12 01:14:17 +08:00
|
|
|
class OSG_EXPORT Program : public osg::StateAttribute
|
2005-02-24 21:33:35 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Program();
|
|
|
|
|
|
|
|
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
|
|
|
|
Program(const Program& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
|
|
|
|
|
2005-03-24 17:37:45 +08:00
|
|
|
META_StateAttribute(osg, Program, PROGRAM);
|
2005-02-24 21:33:35 +08:00
|
|
|
|
|
|
|
/** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
|
2005-03-24 17:37:45 +08:00
|
|
|
virtual int compare(const osg::StateAttribute& sa) const;
|
2005-02-24 21:33:35 +08:00
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
/** If enabled, activate our program in the GL pipeline,
|
|
|
|
* performing any rebuild operations that might be pending. */
|
|
|
|
virtual void apply(osg::State& state) const;
|
2005-02-24 21:33:35 +08:00
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
virtual void compileGLObjects(osg::State& state) const;
|
2005-02-24 21:33:35 +08:00
|
|
|
|
|
|
|
/** 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;
|
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
/** Mark our PCSOs as needing relink */
|
|
|
|
void dirtyProgram();
|
2005-02-24 21:33:35 +08:00
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
/** Attach an osg::Shader to this osg::Program.
|
|
|
|
* Mark Program as needing relink. Return true for success */
|
|
|
|
bool addShader( Shader* shader );
|
2005-02-24 21:33:35 +08:00
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
unsigned int getNumShaders() const { return _shaderList.size(); }
|
2005-04-08 04:23:58 +08:00
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
Shader* getShader( unsigned int i ) { return _shaderList[i].get(); }
|
|
|
|
const Shader* getShader( unsigned int i ) const { return _shaderList[i].get(); }
|
2005-04-08 04:23:58 +08:00
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
/** Remove osg::Shader from this osg::Program.
|
|
|
|
* Mark Program as needing relink. Return true for success */
|
|
|
|
bool removeShader( Shader* shader );
|
2005-02-24 21:33:35 +08:00
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
/** Add an attribute location binding. */
|
2005-05-11 19:41:44 +08:00
|
|
|
void addBindAttribLocation( const std::string& name, GLuint index );
|
|
|
|
|
|
|
|
/** Add an attribute location binding. */
|
|
|
|
void removeBindAttribLocation( const std::string& name );
|
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
typedef std::map<std::string,GLuint> AttribBindingList;
|
2005-05-11 19:41:44 +08:00
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
const AttribBindingList& getAttribBindingList() const { return _attribBindingList; }
|
2005-02-24 21:33:35 +08:00
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
/** Return true if this Program represents "fixed-functionality" rendering */
|
|
|
|
bool isFixedFunction() const;
|
2005-02-24 21:33:35 +08:00
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
/** Query InfoLog from a glProgram */
|
|
|
|
bool getGlProgramInfoLog(unsigned int contextID, std::string& log) const;
|
2005-03-24 17:37:45 +08:00
|
|
|
|
|
|
|
/** Mark internal glProgram for deletion.
|
2005-04-30 15:02:02 +08:00
|
|
|
* Deletion requests are queued until they can be executed
|
|
|
|
* in the proper GL context. */
|
2005-03-24 17:37:45 +08:00
|
|
|
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);
|
|
|
|
|
2005-05-06 17:58:49 +08:00
|
|
|
typedef std::map< std::string, std::pair<GLint,GLenum> > NameInfoMap;
|
|
|
|
const NameInfoMap& getActiveUniforms(unsigned int contextID) const;
|
|
|
|
const NameInfoMap& getActiveAttribs(unsigned int contextID) const;
|
2005-02-24 21:33:35 +08:00
|
|
|
|
2005-04-13 20:00:28 +08:00
|
|
|
public:
|
2005-04-11 16:15:43 +08:00
|
|
|
|
2005-04-11 23:22:29 +08:00
|
|
|
// make PerContextProgram a friend to allow it access Program's protected
|
|
|
|
// methods and member variables.
|
|
|
|
class PerContextProgram;
|
|
|
|
friend class PerContextProgram;
|
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
/** PerContextProgram (PCP) is an OSG-internal encapsulation of glPrograms per-GL context. */
|
|
|
|
class OSG_EXPORT PerContextProgram : public osg::Referenced
|
|
|
|
{
|
|
|
|
public:
|
2005-04-13 20:00:28 +08:00
|
|
|
|
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
PerContextProgram(const Program* program, unsigned int contextID);
|
2005-02-24 21:33:35 +08:00
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
GLuint getHandle() const {return _glProgramHandle;}
|
2005-02-24 21:33:35 +08:00
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
void requestLink();
|
|
|
|
void linkProgram();
|
2006-01-17 01:05:17 +08:00
|
|
|
bool validateProgram();
|
2005-04-30 15:02:02 +08:00
|
|
|
bool needsLink() const {return _needsLink;}
|
|
|
|
bool isLinked() const {return _isLinked;}
|
|
|
|
bool getInfoLog( std::string& infoLog ) const;
|
2005-03-24 17:37:45 +08:00
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
void useProgram() const;
|
2005-05-04 05:46:47 +08:00
|
|
|
|
2005-05-06 17:58:49 +08:00
|
|
|
void resetAppliedUniforms() const
|
2005-05-04 05:46:47 +08:00
|
|
|
{
|
|
|
|
for(LastAppliedUniformList::iterator itr=_lastAppliedUniformList.begin();
|
|
|
|
itr!=_lastAppliedUniformList.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
(*itr).first = 0;
|
|
|
|
(*itr).second = 0;
|
|
|
|
}
|
|
|
|
}
|
2005-04-13 22:12:06 +08:00
|
|
|
|
2005-05-04 05:46:47 +08:00
|
|
|
|
|
|
|
inline void apply(const Uniform& uniform) const
|
2005-04-13 22:12:06 +08:00
|
|
|
{
|
|
|
|
GLint location = getUniformLocation(uniform.getName());
|
2005-05-04 05:46:47 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2005-04-13 22:12:06 +08:00
|
|
|
}
|
2005-02-24 21:33:35 +08:00
|
|
|
|
2005-05-06 17:58:49 +08:00
|
|
|
const NameInfoMap& getActiveUniforms() const {return _uniformInfoMap;}
|
|
|
|
const NameInfoMap& getActiveAttribs() const {return _attribInfoMap;}
|
|
|
|
|
|
|
|
inline GLint getUniformLocation( const std::string& name ) const { NameInfoMap::const_iterator itr = _uniformInfoMap.find(name); return (itr!=_uniformInfoMap.end()) ? itr->second.first : -1; }
|
|
|
|
inline GLint getAttribLocation( const std::string& name ) const { NameInfoMap::const_iterator itr = _attribInfoMap.find(name); return (itr!=_attribInfoMap.end()) ? itr->second.first : -1; }
|
2005-04-30 15:02:02 +08:00
|
|
|
|
|
|
|
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;
|
2005-04-13 20:00:28 +08:00
|
|
|
|
2005-05-06 17:58:49 +08:00
|
|
|
NameInfoMap _uniformInfoMap;
|
|
|
|
NameInfoMap _attribInfoMap;
|
2005-05-04 05:46:47 +08:00
|
|
|
|
|
|
|
typedef std::pair<const osg::Uniform*, unsigned int> UniformModifiedCountPair;
|
|
|
|
typedef std::vector<UniformModifiedCountPair> LastAppliedUniformList;
|
|
|
|
mutable LastAppliedUniformList _lastAppliedUniformList;
|
2005-03-24 17:37:45 +08:00
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
private:
|
|
|
|
PerContextProgram(); // disallowed
|
|
|
|
PerContextProgram(const PerContextProgram&); // disallowed
|
|
|
|
PerContextProgram& operator=(const PerContextProgram&); // disallowed
|
|
|
|
};
|
2005-02-24 21:33:35 +08:00
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
/** Get the PCP for a particular GL context */
|
2005-04-13 20:00:28 +08:00
|
|
|
PerContextProgram* getPCP(unsigned int contextID) const;
|
2005-04-11 16:15:43 +08:00
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
protected: /*methods*/
|
2005-02-24 21:33:35 +08:00
|
|
|
virtual ~Program();
|
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
protected: /*data*/
|
2005-06-08 21:16:19 +08:00
|
|
|
|
2005-04-04 18:08:15 +08:00
|
|
|
mutable osg::buffered_value< osg::ref_ptr<PerContextProgram> > _pcpList;
|
2005-04-30 15:02:02 +08:00
|
|
|
AttribBindingList _attribBindingList;
|
2005-04-04 18:08:15 +08:00
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
typedef std::vector< ref_ptr<Shader> > ShaderList;
|
|
|
|
ShaderList _shaderList;
|
2005-04-04 18:08:15 +08:00
|
|
|
|
2005-02-24 21:33:35 +08:00
|
|
|
|
|
|
|
private:
|
2005-04-30 15:02:02 +08:00
|
|
|
Program& operator=(const Program&); // disallowed
|
2005-02-24 21:33:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*EOF*/
|