2012-03-01 18:14:55 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2005-02-24 21:33:35 +08:00
|
|
|
* Copyright (C) 2003-2005 3Dlabs Inc. Ltd.
|
2005-03-24 17:37:45 +08:00
|
|
|
* Copyright (C) 2004-2005 Nathan Cournia
|
2008-01-08 22:29:44 +08:00
|
|
|
* Copyright (C) 2008 Zebra Imaging
|
2010-11-23 22:50:31 +08:00
|
|
|
* Copyright (C) 2010 Vires Simulationstechnologie GmbH
|
2005-02-24 21:33:35 +08:00
|
|
|
*
|
2007-09-11 21:34:41 +08:00
|
|
|
* This application is open source and may be redistributed and/or modified
|
2010-11-22 19:22:03 +08:00
|
|
|
* freely and without restriction, both in commercial and non commercial
|
2005-02-24 21:33:35 +08:00
|
|
|
* applications, as long as this copyright notice is maintained.
|
2012-03-01 18:14:55 +08:00
|
|
|
*
|
2005-02-24 21:33:35 +08:00
|
|
|
* 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
|
2008-01-08 22:29:44 +08:00
|
|
|
* author: Mike Weiblen 2008-01-02
|
2010-11-23 22:50:31 +08:00
|
|
|
* Holger Helmich 2010-10-21
|
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
|
|
|
|
2011-02-14 20:54:21 +08:00
|
|
|
|
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
|
|
|
|
2007-01-05 00:49:58 +08:00
|
|
|
/** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
|
|
|
|
virtual void setThreadSafeRefUnref(bool threadSafe);
|
|
|
|
|
|
|
|
/** Compile program and associated shaders.*/
|
2005-04-30 15:02:02 +08:00
|
|
|
virtual void compileGLObjects(osg::State& state) const;
|
2005-02-24 21:33:35 +08:00
|
|
|
|
2007-01-04 22:11:51 +08:00
|
|
|
/** Resize any per context GLObject buffers to specified size. */
|
|
|
|
virtual void resizeGLObjectBuffers(unsigned int maxSize);
|
|
|
|
|
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
|
|
|
|
2015-10-22 21:42:19 +08:00
|
|
|
template<class T> bool addShader( const ref_ptr<T>& shader ) { return addShader(shader.get()); }
|
|
|
|
|
2009-01-30 18:55:28 +08:00
|
|
|
unsigned int getNumShaders() const { return static_cast<unsigned int>(_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
|
|
|
|
2015-10-22 21:42:19 +08:00
|
|
|
template<class T> bool removeShader( const ref_ptr<T>& shader ) { return removeShader(shader.get()); }
|
|
|
|
|
2008-01-08 22:29:44 +08:00
|
|
|
/** Set/get GL program parameters */
|
|
|
|
void setParameter( GLenum pname, GLint value );
|
|
|
|
GLint getParameter( GLenum pname ) const;
|
|
|
|
|
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 );
|
2012-03-01 18:14:55 +08:00
|
|
|
|
2007-09-11 21:34:41 +08:00
|
|
|
/** Remove an attribute location binding. */
|
2005-05-11 19:41:44 +08:00
|
|
|
void removeBindAttribLocation( const std::string& name );
|
|
|
|
|
2007-09-11 21:34:41 +08:00
|
|
|
/** 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 );
|
|
|
|
|
2010-11-30 01:43:27 +08:00
|
|
|
/** Add a uniform block binding to an index target. XXX This
|
|
|
|
* should not be an attribute of the program. It should be a
|
|
|
|
* pseudo-uniform that can live in StateSet objects because
|
|
|
|
* it is cheap to set. */
|
|
|
|
void addBindUniformBlock(const std::string& name, GLuint index);
|
|
|
|
|
|
|
|
/** Remove a uniform block binding. */
|
|
|
|
void removeBindUniformBlock(const std::string& name);
|
|
|
|
|
2015-01-07 01:12:51 +08:00
|
|
|
/** Remove a TransformFeedBackVarying. */
|
|
|
|
void removeTransformFeedBackVarying(const std::string& name)
|
|
|
|
{
|
|
|
|
for(std::vector<std::string>::iterator i=_feedbackout.begin(); i!=_feedbackout.end(); i++)
|
|
|
|
{
|
|
|
|
if (*i == name) {_feedbackout.erase(i);break; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Add a TransformFeedBack Varying Name. */
|
|
|
|
void addTransformFeedBackVarying(const std::string& outname)
|
|
|
|
{
|
|
|
|
_feedbackout.push_back(outname);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get number of TransformFeedBack Varyings. */
|
|
|
|
inline unsigned int getNumTransformFeedBackVaryings() const { return _feedbackout.size(); }
|
|
|
|
|
|
|
|
/** Get const TransformFeedBack Varying at index i. */
|
|
|
|
inline const std::string& getTransformFeedBackVarying(unsigned int i) const { return _feedbackout[i]; }
|
|
|
|
|
|
|
|
/** Set TransformFeedBack Mode. */
|
|
|
|
void setTransformFeedBackMode(GLenum e) {_feedbackmode=e;}
|
|
|
|
|
|
|
|
/** Get TransformFeedBack Mode. */
|
|
|
|
GLenum getTransformFeedBackMode() const {return _feedbackmode;}
|
|
|
|
|
2015-02-11 01:04:02 +08:00
|
|
|
/** Experimental. */
|
|
|
|
void setShaderDefines(const ShaderDefines& shaderDefs) { _shaderDefines = shaderDefs; }
|
|
|
|
ShaderDefines& getShaderDefines() { return _shaderDefines; }
|
|
|
|
const ShaderDefines& getShaderDefines() const { return _shaderDefines; }
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-02-14 23:01:56 +08:00
|
|
|
/** Simple class for wrapping up the data used in glProgramBinary and glGetProgramBinary.
|
|
|
|
* On the first run of your application Programs should be assigned an empty ProgramBinary.
|
|
|
|
* Before your application exits it should retrieve the program binary via
|
|
|
|
* Program::PerContextProgram::compileProgramBinary and save it to disk.
|
|
|
|
* When your application is run subsequently, load your binary from disk and use it to set
|
|
|
|
* the data of a ProgramBinary, and set the ProgramBinary on the associated Program.
|
|
|
|
* This will typically result in Program::compileGLObjects executing much faster.*/
|
|
|
|
class OSG_EXPORT ProgramBinary : public osg::Object
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
ProgramBinary();
|
|
|
|
|
|
|
|
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
|
|
|
|
ProgramBinary(const ProgramBinary& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
|
|
|
|
|
|
|
|
META_Object(osg, ProgramBinary);
|
|
|
|
|
2016-06-01 21:20:14 +08:00
|
|
|
/** Allocated a data buffer of specified size.*/
|
2011-02-14 23:01:56 +08:00
|
|
|
void allocate(unsigned int size);
|
|
|
|
|
|
|
|
/** Assign program binary data, copying the specified data into locally stored data buffer, the original data can then be deleted.*/
|
|
|
|
void assign(unsigned int size, const unsigned char* data);
|
|
|
|
|
|
|
|
/** Set the format of the program binary data.*/
|
|
|
|
void setFormat(GLenum format) {_format = format;}
|
|
|
|
|
|
|
|
/** Get the format of the program binary data.*/
|
|
|
|
GLenum getFormat() const {return _format;}
|
|
|
|
|
|
|
|
/** Get the size of the program binary data.*/
|
2013-05-15 00:12:21 +08:00
|
|
|
unsigned int getSize() const { return static_cast<unsigned int>(_data.size()); }
|
2011-02-14 23:01:56 +08:00
|
|
|
|
|
|
|
/** Get a ptr to the program binary data.*/
|
|
|
|
unsigned char* getData() { return _data.empty() ? 0 : &(_data.front()); }
|
|
|
|
|
|
|
|
/** Get a const ptr to the program binary data.*/
|
|
|
|
const unsigned char* getData() const { return _data.empty() ? 0 : &(_data.front()); }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::vector<unsigned char> _data;
|
|
|
|
GLenum _format;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-02-14 20:54:21 +08:00
|
|
|
/** Set the Program using a ProgramBinary. If a ProgramBinary is not yet
|
|
|
|
* available then setting an empty one signals that compileProgramBinary
|
|
|
|
* will be called later.*/
|
2012-03-01 18:14:55 +08:00
|
|
|
void setProgramBinary(ProgramBinary* programBinary) { _programBinary = programBinary; }
|
2011-02-14 20:54:21 +08:00
|
|
|
|
|
|
|
/** Get the Program's ProgramBinary, return NULL if none is assigned. */
|
2012-03-01 18:14:55 +08:00
|
|
|
ProgramBinary* getProgramBinary() { return _programBinary.get(); }
|
2011-02-14 20:54:21 +08:00
|
|
|
|
|
|
|
/** Get the const Program's ProgramBinary, return NULL if none is assigned. */
|
2012-03-01 18:14:55 +08:00
|
|
|
const ProgramBinary* getProgramBinary() const { return _programBinary.get(); }
|
2011-02-14 20:54:21 +08:00
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
typedef std::map<std::string,GLuint> AttribBindingList;
|
2007-09-11 21:34:41 +08:00
|
|
|
typedef std::map<std::string,GLuint> FragDataBindingList;
|
2010-11-30 01:43:27 +08:00
|
|
|
typedef std::map<std::string,GLuint> UniformBlockBindingList;
|
2005-05-11 19:41:44 +08:00
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
const AttribBindingList& getAttribBindingList() const { return _attribBindingList; }
|
2007-09-11 21:34:41 +08:00
|
|
|
const FragDataBindingList& getFragDataBindingList() const { return _fragDataBindingList; }
|
2010-11-30 01:43:27 +08:00
|
|
|
const UniformBlockBindingList& getUniformBlockBindingList() const { return _uniformBlockBindingList; }
|
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
|
|
|
|
2006-03-29 00:08:32 +08:00
|
|
|
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;
|
|
|
|
};
|
2010-11-25 20:30:38 +08:00
|
|
|
typedef std::map< unsigned int, ActiveVarInfo > ActiveUniformMap;
|
2006-03-29 00:08:32 +08:00
|
|
|
typedef std::map< std::string, ActiveVarInfo > ActiveVarInfoMap;
|
2015-02-11 01:04:02 +08:00
|
|
|
//const ActiveUniformMap& getActiveUniforms(unsigned int contextID) const;
|
|
|
|
//const ActiveVarInfoMap& getActiveAttribs(unsigned int contextID) const;
|
2010-11-30 01:43:27 +08:00
|
|
|
struct UniformBlockInfo
|
|
|
|
{
|
|
|
|
UniformBlockInfo() : _index(GL_INVALID_INDEX), _size(0) {}
|
|
|
|
UniformBlockInfo(GLuint index, GLsizei size)
|
|
|
|
: _index(index), _size(size)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
GLuint _index;
|
|
|
|
GLsizei _size;
|
|
|
|
};
|
|
|
|
typedef std::map<std::string, UniformBlockInfo> UniformBlockMap;
|
2015-02-11 01:04:02 +08:00
|
|
|
|
|
|
|
//const UniformBlockMap& getUniformBlocks(unsigned contextID) const;
|
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:
|
2013-07-02 18:32:53 +08:00
|
|
|
/** Use "0" as programHandle to let the PeContextProgram execute "glCreateProgram"and "glDeleteProgram" */
|
|
|
|
PerContextProgram(const Program* program, unsigned int contextID, GLuint programHandle=0);
|
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
|
|
|
|
2015-02-13 20:50:43 +08:00
|
|
|
const osg::Program* getProgram() const { return _program; }
|
|
|
|
|
2015-02-11 01:04:02 +08:00
|
|
|
void setDefineString(const std::string& defStr) { _defineStr = defStr; }
|
|
|
|
const std::string& getDefineString() const { return _defineStr; }
|
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
void requestLink();
|
2013-07-02 18:32:53 +08:00
|
|
|
virtual void linkProgram(osg::State& state);
|
|
|
|
virtual bool validateProgram();
|
2005-04-30 15:02:02 +08:00
|
|
|
bool needsLink() const {return _needsLink;}
|
|
|
|
bool isLinked() const {return _isLinked;}
|
2013-07-02 18:32:53 +08:00
|
|
|
virtual bool getInfoLog( std::string& infoLog ) const;
|
2005-03-24 17:37:45 +08:00
|
|
|
|
2011-02-14 20:54:21 +08:00
|
|
|
/** Was glProgramBinary called successfully? */
|
|
|
|
bool loadedBinary() const {return _loadedBinary;}
|
|
|
|
|
|
|
|
/** Compile a program binary. For this to work setProgramBinary must have
|
|
|
|
* been called on the osg::Program with an empty ProgramBinary prior to
|
|
|
|
* compileGLObjects being called.
|
|
|
|
* compileProgramBinary should be called after the program has been
|
|
|
|
* "exercised" by rendering with it. The ProgramBinary can then be saved
|
|
|
|
* to disk for faster subsequent compiling. */
|
2013-07-02 18:32:53 +08:00
|
|
|
virtual ProgramBinary* compileProgramBinary(osg::State& state);
|
2011-02-14 20:54:21 +08:00
|
|
|
|
2013-07-02 18:32:53 +08:00
|
|
|
virtual 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
|
|
|
{
|
2012-03-01 18:38:28 +08:00
|
|
|
_lastAppliedUniformList.clear();
|
2005-05-04 05:46:47 +08:00
|
|
|
}
|
2012-03-01 18:14:55 +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
|
|
|
{
|
2010-11-25 20:30:38 +08:00
|
|
|
GLint location = getUniformLocation(uniform.getNameID());
|
2005-05-04 05:46:47 +08:00
|
|
|
if (location>=0)
|
|
|
|
{
|
2008-06-19 19:58:34 +08:00
|
|
|
const Uniform* lastAppliedUniform = _lastAppliedUniformList[location].first.get();
|
2005-05-04 05:46:47 +08:00
|
|
|
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
|
|
|
|
2010-11-25 20:30:38 +08:00
|
|
|
const ActiveUniformMap& getActiveUniforms() const {return _uniformInfoMap;}
|
2006-03-29 00:08:32 +08:00
|
|
|
const ActiveVarInfoMap& getActiveAttribs() const {return _attribInfoMap;}
|
2010-11-30 01:43:27 +08:00
|
|
|
const UniformBlockMap& getUniformBlocks() const {return _uniformBlockMap; }
|
2010-11-25 20:30:38 +08:00
|
|
|
inline GLint getUniformLocation( unsigned int uniformNameID ) const { ActiveUniformMap::const_iterator itr = _uniformInfoMap.find(uniformNameID); return (itr!=_uniformInfoMap.end()) ? itr->second._location : -1; }
|
2010-12-12 17:22:09 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Alternative version of getUniformLocation( unsigned int uniformNameID )
|
|
|
|
* retrofited into OSG for backward compatibility with osgCal,
|
|
|
|
* after uniform ids were refactored from std::strings to GLints in OSG version 2.9.10.
|
2012-03-01 18:14:55 +08:00
|
|
|
*
|
|
|
|
* Drawbacks: This method is not particularly fast. It has to access mutexed static
|
2010-12-12 17:22:09 +08:00
|
|
|
* map of uniform ids. So don't overuse it or your app performance will suffer.
|
|
|
|
*/
|
|
|
|
inline GLint getUniformLocation( const std::string & uniformName ) const { return getUniformLocation( Uniform::getNameID( uniformName ) ); }
|
|
|
|
|
2006-03-29 00:08:32 +08:00
|
|
|
inline GLint getAttribLocation( const std::string& name ) const { ActiveVarInfoMap::const_iterator itr = _attribInfoMap.find(name); return (itr!=_attribInfoMap.end()) ? itr->second._location : -1; }
|
2005-04-30 15:02:02 +08:00
|
|
|
|
2007-12-11 19:21:13 +08:00
|
|
|
inline void addShaderToAttach(Shader *shader)
|
|
|
|
{
|
|
|
|
_shadersToAttach.push_back(shader);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void addShaderToDetach(Shader *shader)
|
|
|
|
{
|
|
|
|
_shadersToDetach.push_back(shader);
|
|
|
|
}
|
2012-03-01 18:14:55 +08:00
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
protected: /*methods*/
|
2013-07-02 18:32:53 +08:00
|
|
|
virtual ~PerContextProgram();
|
2005-04-30 15:02:02 +08:00
|
|
|
|
|
|
|
protected: /*data*/
|
|
|
|
/** Pointer to our parent Program */
|
|
|
|
const Program* _program;
|
|
|
|
/** Pointer to this context's extension functions */
|
2014-12-10 18:38:12 +08:00
|
|
|
osg::ref_ptr<GLExtensions> _extensions;
|
2015-02-11 01:04:02 +08:00
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
/** Handle to the actual OpenGL glProgram */
|
|
|
|
GLuint _glProgramHandle;
|
2015-02-11 01:04:02 +08:00
|
|
|
|
|
|
|
/** Define string passed on to Shaders to help configure them.*/
|
|
|
|
std::string _defineStr;
|
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
/** Does our glProgram need to be linked? */
|
|
|
|
bool _needsLink;
|
|
|
|
/** Is our glProgram successfully linked? */
|
|
|
|
bool _isLinked;
|
2011-02-14 20:54:21 +08:00
|
|
|
/** Was glProgramBinary called successfully? */
|
|
|
|
bool _loadedBinary;
|
2013-07-02 18:32:53 +08:00
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
const unsigned int _contextID;
|
2014-12-10 18:38:12 +08:00
|
|
|
|
2013-07-02 18:32:53 +08:00
|
|
|
/** Does the glProgram handle belongs to this class? */
|
|
|
|
bool _ownsProgramHandle;
|
2008-01-08 22:29:44 +08:00
|
|
|
|
2010-11-25 20:30:38 +08:00
|
|
|
ActiveUniformMap _uniformInfoMap;
|
2006-03-29 00:08:32 +08:00
|
|
|
ActiveVarInfoMap _attribInfoMap;
|
2010-11-30 01:43:27 +08:00
|
|
|
UniformBlockMap _uniformBlockMap;
|
2012-03-01 18:14:55 +08:00
|
|
|
|
2008-06-19 19:58:34 +08:00
|
|
|
typedef std::pair<osg::ref_ptr<const osg::Uniform>, unsigned int> UniformModifiedCountPair;
|
2012-03-01 18:38:28 +08:00
|
|
|
typedef std::map<unsigned int, UniformModifiedCountPair> LastAppliedUniformList;
|
2005-05-04 05:46:47 +08:00
|
|
|
mutable LastAppliedUniformList _lastAppliedUniformList;
|
2012-03-01 18:14:55 +08:00
|
|
|
|
2007-12-11 19:21:13 +08:00
|
|
|
typedef std::vector< ref_ptr<Shader> > ShaderList;
|
|
|
|
ShaderList _shadersToDetach;
|
|
|
|
ShaderList _shadersToAttach;
|
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
|
|
|
|
2015-09-23 17:47:34 +08:00
|
|
|
struct OSG_EXPORT ProgramObjects : public osg::GraphicsObject
|
2015-02-11 01:04:02 +08:00
|
|
|
{
|
|
|
|
typedef std::vector< osg::ref_ptr<PerContextProgram> > PerContextPrograms;
|
|
|
|
|
|
|
|
ProgramObjects(const Program* program, unsigned int contextID);
|
|
|
|
|
|
|
|
unsigned int _contextID;
|
|
|
|
const Program* _program;
|
|
|
|
mutable PerContextPrograms _perContextPrograms;
|
|
|
|
|
|
|
|
PerContextProgram* getPCP(const std::string& defineStr) const;
|
|
|
|
PerContextProgram* createPerContextProgram(const std::string& defineStr);
|
|
|
|
void requestLink();
|
|
|
|
void addShaderToAttach(Shader* shader);
|
|
|
|
void addShaderToDetach(Shader* shader);
|
|
|
|
bool getGlProgramInfoLog(std::string& log) const;
|
|
|
|
};
|
|
|
|
|
2005-04-30 15:02:02 +08:00
|
|
|
/** Get the PCP for a particular GL context */
|
2015-02-11 01:04:02 +08:00
|
|
|
PerContextProgram* getPCP(State& state) 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
|
|
|
|
2015-02-11 01:04:02 +08:00
|
|
|
mutable osg::buffered_value< osg::ref_ptr<ProgramObjects> > _pcpList;
|
2005-04-30 15:02:02 +08:00
|
|
|
AttribBindingList _attribBindingList;
|
2007-09-11 21:34:41 +08:00
|
|
|
FragDataBindingList _fragDataBindingList;
|
2010-11-30 01:43:27 +08:00
|
|
|
UniformBlockBindingList _uniformBlockBindingList;
|
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
|
|
|
|
2011-02-14 20:54:21 +08:00
|
|
|
osg::ref_ptr<ProgramBinary> _programBinary;
|
|
|
|
|
2008-01-08 22:29:44 +08:00
|
|
|
/** Parameters maintained with glProgramParameteriEXT */
|
|
|
|
GLint _geometryVerticesOut;
|
|
|
|
GLint _geometryInputType;
|
|
|
|
GLint _geometryOutputType;
|
|
|
|
|
2015-01-07 01:12:51 +08:00
|
|
|
/**TransformFeedBack**/
|
|
|
|
GLenum _feedbackmode;
|
|
|
|
std::vector<std::string> _feedbackout;
|
|
|
|
|
2015-02-11 01:04:02 +08:00
|
|
|
ShaderDefines _shaderDefines;
|
|
|
|
|
|
|
|
|
2005-02-24 21:33:35 +08:00
|
|
|
private:
|
2005-04-30 15:02:02 +08:00
|
|
|
Program& operator=(const Program&); // disallowed
|
2010-11-23 22:50:31 +08:00
|
|
|
|
2005-02-24 21:33:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|