1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 * Copyright (C) 2003-2005 3Dlabs Inc. Ltd.
3 * Copyright (C) 2004-2005 Nathan Cournia
4 * Copyright (C) 2008 Zebra Imaging
5 * Copyright (C) 2010 Vires Simulationstechnologie GmbH
7 * This application is open source and may be redistributed and/or modified
8 * freely and without restriction, both in commercial and non commercial
9 * applications, as long as this copyright notice is maintained.
11 * This application is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16/* file: include/osg/Program
17 * author: Mike Weiblen 2008-01-02
18 * Holger Helmich 2010-10-21
28#include <osg/buffered_value>
32#include <osg/StateAttribute>
39///////////////////////////////////////////////////////////////////////////
40/** osg::Program is an application-level abstraction of an OpenGL glProgram.
41 * It is an osg::StateAttribute that, when applied, will activate a
42 * glProgram for subsequent rendering.
43 * osg::Shaders containing the actual shader source code are
44 * attached to a Program, which will then manage the compilation,
45 * linking, and activation of the GLSL program.
46 * osg::Program will automatically manage per-context instancing of the
47 * OpenGL glPrograms, if that is necessary for a particular display
51class OSG_EXPORT Program : public osg::StateAttribute
56 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
57 Program(const Program& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
59 META_StateAttribute(osg, Program, PROGRAM);
61 /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
62 virtual int compare(const osg::StateAttribute& sa) const;
64 /** If enabled, activate our program in the GL pipeline,
65 * performing any rebuild operations that might be pending. */
66 virtual void apply(osg::State& state) const;
68 /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
69 virtual void setThreadSafeRefUnref(bool threadSafe);
71 /** Compile program and associated shaders.*/
72 virtual void compileGLObjects(osg::State& state) const;
74 /** Resize any per context GLObject buffers to specified size. */
75 virtual void resizeGLObjectBuffers(unsigned int maxSize);
77 /** release OpenGL objects in specified graphics context if State
78 object is passed, otherwise release OpenGL objects for all graphics context if
79 State object pointer NULL.*/
80 virtual void releaseGLObjects(osg::State* state=0) const;
82 /** Mark our PCSOs as needing relink */
85 /** Attach an osg::Shader to this osg::Program.
86 * Mark Program as needing relink. Return true for success */
87 bool addShader( Shader* shader );
89 template<class T> bool addShader( const ref_ptr<T>& shader ) { return addShader(shader.get()); }
91 unsigned int getNumShaders() const { return static_cast<unsigned int>(_shaderList.size()); }
93 Shader* getShader( unsigned int i ) { return _shaderList[i].get(); }
94 const Shader* getShader( unsigned int i ) const { return _shaderList[i].get(); }
96 /** Remove osg::Shader from this osg::Program.
97 * Mark Program as needing relink. Return true for success */
98 bool removeShader( Shader* shader );
100 template<class T> bool removeShader( const ref_ptr<T>& shader ) { return removeShader(shader.get()); }
102 /** Set/get GL program parameters */
103 void setParameter( GLenum pname, GLint value );
104 GLint getParameter( GLenum pname ) const;
106 /** Add an attribute location binding. */
107 void addBindAttribLocation( const std::string& name, GLuint index );
109 /** Remove an attribute location binding. */
110 void removeBindAttribLocation( const std::string& name );
112 /** Add an frag data location binding. See EXT_gpu_shader4 for BindFragDataLocationEXT */
113 void addBindFragDataLocation( const std::string& name, GLuint index );
115 /** Remove an frag data location binding. */
116 void removeBindFragDataLocation( const std::string& name );
118 /** Add a uniform block binding to an index target. XXX This
119 * should not be an attribute of the program. It should be a
120 * pseudo-uniform that can live in StateSet objects because
121 * it is cheap to set. */
122 void addBindUniformBlock(const std::string& name, GLuint index);
124 /** Remove a uniform block binding. */
125 void removeBindUniformBlock(const std::string& name);
127 /** Remove a TransformFeedBackVarying. */
128 void removeTransformFeedBackVarying(const std::string& name)
130 for(std::vector<std::string>::iterator i=_feedbackout.begin(); i!=_feedbackout.end(); i++)
132 if (*i == name) {_feedbackout.erase(i);break; }
136 /** Add a TransformFeedBack Varying Name. */
137 void addTransformFeedBackVarying(const std::string& outname)
139 _feedbackout.push_back(outname);
142 /** Get number of TransformFeedBack Varyings. */
143 inline unsigned int getNumTransformFeedBackVaryings() const { return _feedbackout.size(); }
145 /** Get const TransformFeedBack Varying at index i. */
146 inline const std::string& getTransformFeedBackVarying(unsigned int i) const { return _feedbackout[i]; }
148 /** Set TransformFeedBack Mode. */
149 void setTransformFeedBackMode(GLenum e) {_feedbackmode=e;}
151 /** Get TransformFeedBack Mode. */
152 GLenum getTransformFeedBackMode() const {return _feedbackmode;}
155 void setShaderDefines(const ShaderDefines& shaderDefs) { _shaderDefines = shaderDefs; }
156 ShaderDefines& getShaderDefines() { return _shaderDefines; }
157 const ShaderDefines& getShaderDefines() const { return _shaderDefines; }
161 /** Simple class for wrapping up the data used in glProgramBinary and glGetProgramBinary.
162 * On the first run of your application Programs should be assigned an empty ProgramBinary.
163 * Before your application exits it should retrieve the program binary via
164 * Program::PerContextProgram::compileProgramBinary and save it to disk.
165 * When your application is run subsequently, load your binary from disk and use it to set
166 * the data of a ProgramBinary, and set the ProgramBinary on the associated Program.
167 * This will typically result in Program::compileGLObjects executing much faster.*/
168 class OSG_EXPORT ProgramBinary : public osg::Object
174 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
175 ProgramBinary(const ProgramBinary& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
177 META_Object(osg, ProgramBinary);
179 /** Allocated a data buffer of specified size.*/
180 void allocate(unsigned int size);
182 /** Assign program binary data, copying the specified data into locally stored data buffer, the original data can then be deleted.*/
183 void assign(unsigned int size, const unsigned char* data);
185 /** Set the format of the program binary data.*/
186 void setFormat(GLenum format) {_format = format;}
188 /** Get the format of the program binary data.*/
189 GLenum getFormat() const {return _format;}
191 /** Get the size of the program binary data.*/
192 unsigned int getSize() const { return static_cast<unsigned int>(_data.size()); }
194 /** Get a ptr to the program binary data.*/
195 unsigned char* getData() { return _data.empty() ? 0 : &(_data.front()); }
197 /** Get a const ptr to the program binary data.*/
198 const unsigned char* getData() const { return _data.empty() ? 0 : &(_data.front()); }
201 std::vector<unsigned char> _data;
206 /** Set the Program using a ProgramBinary. If a ProgramBinary is not yet
207 * available then setting an empty one signals that compileProgramBinary
208 * will be called later.*/
209 void setProgramBinary(ProgramBinary* programBinary) { _programBinary = programBinary; }
211 /** Get the Program's ProgramBinary, return NULL if none is assigned. */
212 ProgramBinary* getProgramBinary() { return _programBinary.get(); }
214 /** Get the const Program's ProgramBinary, return NULL if none is assigned. */
215 const ProgramBinary* getProgramBinary() const { return _programBinary.get(); }
217 typedef std::map<std::string,GLuint> AttribBindingList;
218 typedef std::map<std::string,GLuint> FragDataBindingList;
219 typedef std::map<std::string,GLuint> UniformBlockBindingList;
221 const AttribBindingList& getAttribBindingList() const { return _attribBindingList; }
222 const FragDataBindingList& getFragDataBindingList() const { return _fragDataBindingList; }
223 const UniformBlockBindingList& getUniformBlockBindingList() const { return _uniformBlockBindingList; }
225 /** Return true if this Program represents "fixed-functionality" rendering */
226 bool isFixedFunction() const;
228 /** Query InfoLog from a glProgram */
229 bool getGlProgramInfoLog(unsigned int contextID, std::string& log) const;
231 struct ActiveVarInfo {
232 ActiveVarInfo() : _location(-1), _type(Uniform::UNDEFINED), _size(-1) {}
233 ActiveVarInfo( GLint loc, GLenum type, GLint size ) : _location(loc), _type(type), _size(size) {}
238 typedef std::map< unsigned int, ActiveVarInfo > ActiveUniformMap;
239 typedef std::map< std::string, ActiveVarInfo > ActiveVarInfoMap;
240 //const ActiveUniformMap& getActiveUniforms(unsigned int contextID) const;
241 //const ActiveVarInfoMap& getActiveAttribs(unsigned int contextID) const;
242 struct UniformBlockInfo
244 UniformBlockInfo() : _index(GL_INVALID_INDEX), _size(0) {}
245 UniformBlockInfo(GLuint index, GLsizei size)
246 : _index(index), _size(size)
252 typedef std::map<std::string, UniformBlockInfo> UniformBlockMap;
254 //const UniformBlockMap& getUniformBlocks(unsigned contextID) const;
257 // make PerContextProgram a friend to allow it access Program's protected
258 // methods and member variables.
259 class PerContextProgram;
260 friend class PerContextProgram;
262 /** PerContextProgram (PCP) is an OSG-internal encapsulation of glPrograms per-GL context. */
263 class OSG_EXPORT PerContextProgram : public osg::Referenced
266 /** Use "0" as programHandle to let the PeContextProgram execute "glCreateProgram"and "glDeleteProgram" */
267 PerContextProgram(const Program* program, unsigned int contextID, GLuint programHandle=0);
269 GLuint getHandle() const {return _glProgramHandle;}
271 const osg::Program* getProgram() const { return _program; }
273 void setDefineString(const std::string& defStr) { _defineStr = defStr; }
274 const std::string& getDefineString() const { return _defineStr; }
277 virtual void linkProgram(osg::State& state);
278 virtual bool validateProgram();
279 bool needsLink() const {return _needsLink;}
280 bool isLinked() const {return _isLinked;}
281 virtual bool getInfoLog( std::string& infoLog ) const;
283 /** Was glProgramBinary called successfully? */
284 bool loadedBinary() const {return _loadedBinary;}
286 /** Compile a program binary. For this to work setProgramBinary must have
287 * been called on the osg::Program with an empty ProgramBinary prior to
288 * compileGLObjects being called.
289 * compileProgramBinary should be called after the program has been
290 * "exercised" by rendering with it. The ProgramBinary can then be saved
291 * to disk for faster subsequent compiling. */
292 virtual ProgramBinary* compileProgramBinary(osg::State& state);
294 virtual void useProgram() const;
296 void resetAppliedUniforms() const
298 _lastAppliedUniformList.clear();
302 inline void apply(const Uniform& uniform) const
304 GLint location = getUniformLocation(uniform.getNameID());
307 const Uniform* lastAppliedUniform = _lastAppliedUniformList[location].first.get();
308 if (lastAppliedUniform != &uniform)
311 uniform.apply(_extensions.get(),location);
312 _lastAppliedUniformList[location].first = &uniform;
313 _lastAppliedUniformList[location].second = uniform.getModifiedCount();
315 else if (_lastAppliedUniformList[location].second != uniform.getModifiedCount())
317 // existing attribute has been modified
318 uniform.apply(_extensions.get(),location);
319 _lastAppliedUniformList[location].first = &uniform;
320 _lastAppliedUniformList[location].second = uniform.getModifiedCount();
325 const ActiveUniformMap& getActiveUniforms() const {return _uniformInfoMap;}
326 const ActiveVarInfoMap& getActiveAttribs() const {return _attribInfoMap;}
327 const UniformBlockMap& getUniformBlocks() const {return _uniformBlockMap; }
328 inline GLint getUniformLocation( unsigned int uniformNameID ) const { ActiveUniformMap::const_iterator itr = _uniformInfoMap.find(uniformNameID); return (itr!=_uniformInfoMap.end()) ? itr->second._location : -1; }
331 * Alternative version of getUniformLocation( unsigned int uniformNameID )
332 * retrofited into OSG for backward compatibility with osgCal,
333 * after uniform ids were refactored from std::strings to GLints in OSG version 2.9.10.
335 * Drawbacks: This method is not particularly fast. It has to access mutexed static
336 * map of uniform ids. So don't overuse it or your app performance will suffer.
338 inline GLint getUniformLocation( const std::string & uniformName ) const { return getUniformLocation( Uniform::getNameID( uniformName ) ); }
340 inline GLint getAttribLocation( const std::string& name ) const { ActiveVarInfoMap::const_iterator itr = _attribInfoMap.find(name); return (itr!=_attribInfoMap.end()) ? itr->second._location : -1; }
342 inline void addShaderToAttach(Shader *shader)
344 _shadersToAttach.push_back(shader);
347 inline void addShaderToDetach(Shader *shader)
349 _shadersToDetach.push_back(shader);
352 protected: /*methods*/
353 virtual ~PerContextProgram();
356 /** Pointer to our parent Program */
357 const Program* _program;
358 /** Pointer to this context's extension functions */
359 osg::ref_ptr<GLExtensions> _extensions;
361 /** Handle to the actual OpenGL glProgram */
362 GLuint _glProgramHandle;
364 /** Define string passed on to Shaders to help configure them.*/
365 std::string _defineStr;
367 /** Does our glProgram need to be linked? */
369 /** Is our glProgram successfully linked? */
371 /** Was glProgramBinary called successfully? */
374 const unsigned int _contextID;
376 /** Does the glProgram handle belongs to this class? */
377 bool _ownsProgramHandle;
379 ActiveUniformMap _uniformInfoMap;
380 ActiveVarInfoMap _attribInfoMap;
381 UniformBlockMap _uniformBlockMap;
383 typedef std::pair<osg::ref_ptr<const osg::Uniform>, unsigned int> UniformModifiedCountPair;
384 typedef std::map<unsigned int, UniformModifiedCountPair> LastAppliedUniformList;
385 mutable LastAppliedUniformList _lastAppliedUniformList;
387 typedef std::vector< ref_ptr<Shader> > ShaderList;
388 ShaderList _shadersToDetach;
389 ShaderList _shadersToAttach;
392 PerContextProgram(); // disallowed
393 PerContextProgram(const PerContextProgram&); // disallowed
394 PerContextProgram& operator=(const PerContextProgram&); // disallowed
397 struct OSG_EXPORT ProgramObjects : public osg::GraphicsObject
399 typedef std::vector< osg::ref_ptr<PerContextProgram> > PerContextPrograms;
401 ProgramObjects(const Program* program, unsigned int contextID);
403 unsigned int _contextID;
404 const Program* _program;
405 mutable PerContextPrograms _perContextPrograms;
407 PerContextProgram* getPCP(const std::string& defineStr) const;
408 PerContextProgram* createPerContextProgram(const std::string& defineStr);
410 void addShaderToAttach(Shader* shader);
411 void addShaderToDetach(Shader* shader);
412 bool getGlProgramInfoLog(std::string& log) const;
415 /** Get the PCP for a particular GL context */
416 PerContextProgram* getPCP(State& state) const;
418 protected: /*methods*/
423 mutable osg::buffered_value< osg::ref_ptr<ProgramObjects> > _pcpList;
424 AttribBindingList _attribBindingList;
425 FragDataBindingList _fragDataBindingList;
426 UniformBlockBindingList _uniformBlockBindingList;
428 typedef std::vector< ref_ptr<Shader> > ShaderList;
429 ShaderList _shaderList;
431 osg::ref_ptr<ProgramBinary> _programBinary;
433 /** Parameters maintained with glProgramParameteriEXT */
434 GLint _geometryVerticesOut;
435 GLint _geometryInputType;
436 GLint _geometryOutputType;
438 /**TransformFeedBack**/
439 GLenum _feedbackmode;
440 std::vector<std::string> _feedbackout;
442 ShaderDefines _shaderDefines;
446 Program& operator=(const Program&); // disallowed