openscenegraph
osg/Program
Go to the documentation of this file.
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
6 *
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.
10 *
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.
14*/
15
16/* file: include/osg/Program
17 * author: Mike Weiblen 2008-01-02
18 * Holger Helmich 2010-10-21
19*/
20
21#ifndef OSG_PROGRAM
22#define OSG_PROGRAM 1
23
24#include <string>
25#include <vector>
26#include <map>
27
28#include <osg/buffered_value>
29#include <osg/ref_ptr>
30#include <osg/Uniform>
31#include <osg/Shader>
32#include <osg/StateAttribute>
33
34namespace osg {
35
36class State;
37
38
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
48 * configuration.
49 */
50
51class OSG_EXPORT Program : public osg::StateAttribute
52{
53 public:
54 Program();
55
56 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
57 Program(const Program& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
58
59 META_StateAttribute(osg, Program, PROGRAM);
60
61 /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
62 virtual int compare(const osg::StateAttribute& sa) const;
63
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;
67
68 /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
69 virtual void setThreadSafeRefUnref(bool threadSafe);
70
71 /** Compile program and associated shaders.*/
72 virtual void compileGLObjects(osg::State& state) const;
73
74 /** Resize any per context GLObject buffers to specified size. */
75 virtual void resizeGLObjectBuffers(unsigned int maxSize);
76
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;
81
82 /** Mark our PCSOs as needing relink */
83 void dirtyProgram();
84
85 /** Attach an osg::Shader to this osg::Program.
86 * Mark Program as needing relink. Return true for success */
87 bool addShader( Shader* shader );
88
89 template<class T> bool addShader( const ref_ptr<T>& shader ) { return addShader(shader.get()); }
90
91 unsigned int getNumShaders() const { return static_cast<unsigned int>(_shaderList.size()); }
92
93 Shader* getShader( unsigned int i ) { return _shaderList[i].get(); }
94 const Shader* getShader( unsigned int i ) const { return _shaderList[i].get(); }
95
96 /** Remove osg::Shader from this osg::Program.
97 * Mark Program as needing relink. Return true for success */
98 bool removeShader( Shader* shader );
99
100 template<class T> bool removeShader( const ref_ptr<T>& shader ) { return removeShader(shader.get()); }
101
102 /** Set/get GL program parameters */
103 void setParameter( GLenum pname, GLint value );
104 GLint getParameter( GLenum pname ) const;
105
106 /** Add an attribute location binding. */
107 void addBindAttribLocation( const std::string& name, GLuint index );
108
109 /** Remove an attribute location binding. */
110 void removeBindAttribLocation( const std::string& name );
111
112 /** Add an frag data location binding. See EXT_gpu_shader4 for BindFragDataLocationEXT */
113 void addBindFragDataLocation( const std::string& name, GLuint index );
114
115 /** Remove an frag data location binding. */
116 void removeBindFragDataLocation( const std::string& name );
117
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);
123
124 /** Remove a uniform block binding. */
125 void removeBindUniformBlock(const std::string& name);
126
127 /** Remove a TransformFeedBackVarying. */
128 void removeTransformFeedBackVarying(const std::string& name)
129 {
130 for(std::vector<std::string>::iterator i=_feedbackout.begin(); i!=_feedbackout.end(); i++)
131 {
132 if (*i == name) {_feedbackout.erase(i);break; }
133 }
134 }
135
136 /** Add a TransformFeedBack Varying Name. */
137 void addTransformFeedBackVarying(const std::string& outname)
138 {
139 _feedbackout.push_back(outname);
140 }
141
142 /** Get number of TransformFeedBack Varyings. */
143 inline unsigned int getNumTransformFeedBackVaryings() const { return _feedbackout.size(); }
144
145 /** Get const TransformFeedBack Varying at index i. */
146 inline const std::string& getTransformFeedBackVarying(unsigned int i) const { return _feedbackout[i]; }
147
148 /** Set TransformFeedBack Mode. */
149 void setTransformFeedBackMode(GLenum e) {_feedbackmode=e;}
150
151 /** Get TransformFeedBack Mode. */
152 GLenum getTransformFeedBackMode() const {return _feedbackmode;}
153
154 /** Experimental. */
155 void setShaderDefines(const ShaderDefines& shaderDefs) { _shaderDefines = shaderDefs; }
156 ShaderDefines& getShaderDefines() { return _shaderDefines; }
157 const ShaderDefines& getShaderDefines() const { return _shaderDefines; }
158
159
160
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
169 {
170 public:
171
172 ProgramBinary();
173
174 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
175 ProgramBinary(const ProgramBinary& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
176
177 META_Object(osg, ProgramBinary);
178
179 /** Allocated a data buffer of specified size.*/
180 void allocate(unsigned int size);
181
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);
184
185 /** Set the format of the program binary data.*/
186 void setFormat(GLenum format) {_format = format;}
187
188 /** Get the format of the program binary data.*/
189 GLenum getFormat() const {return _format;}
190
191 /** Get the size of the program binary data.*/
192 unsigned int getSize() const { return static_cast<unsigned int>(_data.size()); }
193
194 /** Get a ptr to the program binary data.*/
195 unsigned char* getData() { return _data.empty() ? 0 : &(_data.front()); }
196
197 /** Get a const ptr to the program binary data.*/
198 const unsigned char* getData() const { return _data.empty() ? 0 : &(_data.front()); }
199
200 protected:
201 std::vector<unsigned char> _data;
202 GLenum _format;
203 };
204
205
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; }
210
211 /** Get the Program's ProgramBinary, return NULL if none is assigned. */
212 ProgramBinary* getProgramBinary() { return _programBinary.get(); }
213
214 /** Get the const Program's ProgramBinary, return NULL if none is assigned. */
215 const ProgramBinary* getProgramBinary() const { return _programBinary.get(); }
216
217 typedef std::map<std::string,GLuint> AttribBindingList;
218 typedef std::map<std::string,GLuint> FragDataBindingList;
219 typedef std::map<std::string,GLuint> UniformBlockBindingList;
220
221 const AttribBindingList& getAttribBindingList() const { return _attribBindingList; }
222 const FragDataBindingList& getFragDataBindingList() const { return _fragDataBindingList; }
223 const UniformBlockBindingList& getUniformBlockBindingList() const { return _uniformBlockBindingList; }
224
225 /** Return true if this Program represents "fixed-functionality" rendering */
226 bool isFixedFunction() const;
227
228 /** Query InfoLog from a glProgram */
229 bool getGlProgramInfoLog(unsigned int contextID, std::string& log) const;
230
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) {}
234 GLint _location;
235 GLenum _type;
236 GLint _size;
237 };
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
243 {
244 UniformBlockInfo() : _index(GL_INVALID_INDEX), _size(0) {}
245 UniformBlockInfo(GLuint index, GLsizei size)
246 : _index(index), _size(size)
247 {
248 }
249 GLuint _index;
250 GLsizei _size;
251 };
252 typedef std::map<std::string, UniformBlockInfo> UniformBlockMap;
253
254 //const UniformBlockMap& getUniformBlocks(unsigned contextID) const;
255 public:
256
257 // make PerContextProgram a friend to allow it access Program's protected
258 // methods and member variables.
259 class PerContextProgram;
260 friend class PerContextProgram;
261
262 /** PerContextProgram (PCP) is an OSG-internal encapsulation of glPrograms per-GL context. */
263 class OSG_EXPORT PerContextProgram : public osg::Referenced
264 {
265 public:
266 /** Use "0" as programHandle to let the PeContextProgram execute "glCreateProgram"and "glDeleteProgram" */
267 PerContextProgram(const Program* program, unsigned int contextID, GLuint programHandle=0);
268
269 GLuint getHandle() const {return _glProgramHandle;}
270
271 const osg::Program* getProgram() const { return _program; }
272
273 void setDefineString(const std::string& defStr) { _defineStr = defStr; }
274 const std::string& getDefineString() const { return _defineStr; }
275
276 void requestLink();
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;
282
283 /** Was glProgramBinary called successfully? */
284 bool loadedBinary() const {return _loadedBinary;}
285
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);
293
294 virtual void useProgram() const;
295
296 void resetAppliedUniforms() const
297 {
298 _lastAppliedUniformList.clear();
299 }
300
301
302 inline void apply(const Uniform& uniform) const
303 {
304 GLint location = getUniformLocation(uniform.getNameID());
305 if (location>=0)
306 {
307 const Uniform* lastAppliedUniform = _lastAppliedUniformList[location].first.get();
308 if (lastAppliedUniform != &uniform)
309 {
310 // new attribute
311 uniform.apply(_extensions.get(),location);
312 _lastAppliedUniformList[location].first = &uniform;
313 _lastAppliedUniformList[location].second = uniform.getModifiedCount();
314 }
315 else if (_lastAppliedUniformList[location].second != uniform.getModifiedCount())
316 {
317 // existing attribute has been modified
318 uniform.apply(_extensions.get(),location);
319 _lastAppliedUniformList[location].first = &uniform;
320 _lastAppliedUniformList[location].second = uniform.getModifiedCount();
321 }
322 }
323 }
324
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; }
329
330 /**
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.
334 *
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.
337 */
338 inline GLint getUniformLocation( const std::string & uniformName ) const { return getUniformLocation( Uniform::getNameID( uniformName ) ); }
339
340 inline GLint getAttribLocation( const std::string& name ) const { ActiveVarInfoMap::const_iterator itr = _attribInfoMap.find(name); return (itr!=_attribInfoMap.end()) ? itr->second._location : -1; }
341
342 inline void addShaderToAttach(Shader *shader)
343 {
344 _shadersToAttach.push_back(shader);
345 }
346
347 inline void addShaderToDetach(Shader *shader)
348 {
349 _shadersToDetach.push_back(shader);
350 }
351
352 protected: /*methods*/
353 virtual ~PerContextProgram();
354
355 protected: /*data*/
356 /** Pointer to our parent Program */
357 const Program* _program;
358 /** Pointer to this context's extension functions */
359 osg::ref_ptr<GLExtensions> _extensions;
360
361 /** Handle to the actual OpenGL glProgram */
362 GLuint _glProgramHandle;
363
364 /** Define string passed on to Shaders to help configure them.*/
365 std::string _defineStr;
366
367 /** Does our glProgram need to be linked? */
368 bool _needsLink;
369 /** Is our glProgram successfully linked? */
370 bool _isLinked;
371 /** Was glProgramBinary called successfully? */
372 bool _loadedBinary;
373
374 const unsigned int _contextID;
375
376 /** Does the glProgram handle belongs to this class? */
377 bool _ownsProgramHandle;
378
379 ActiveUniformMap _uniformInfoMap;
380 ActiveVarInfoMap _attribInfoMap;
381 UniformBlockMap _uniformBlockMap;
382
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;
386
387 typedef std::vector< ref_ptr<Shader> > ShaderList;
388 ShaderList _shadersToDetach;
389 ShaderList _shadersToAttach;
390
391 private:
392 PerContextProgram(); // disallowed
393 PerContextProgram(const PerContextProgram&); // disallowed
394 PerContextProgram& operator=(const PerContextProgram&); // disallowed
395 };
396
397 struct OSG_EXPORT ProgramObjects : public osg::GraphicsObject
398 {
399 typedef std::vector< osg::ref_ptr<PerContextProgram> > PerContextPrograms;
400
401 ProgramObjects(const Program* program, unsigned int contextID);
402
403 unsigned int _contextID;
404 const Program* _program;
405 mutable PerContextPrograms _perContextPrograms;
406
407 PerContextProgram* getPCP(const std::string& defineStr) const;
408 PerContextProgram* createPerContextProgram(const std::string& defineStr);
409 void requestLink();
410 void addShaderToAttach(Shader* shader);
411 void addShaderToDetach(Shader* shader);
412 bool getGlProgramInfoLog(std::string& log) const;
413 };
414
415 /** Get the PCP for a particular GL context */
416 PerContextProgram* getPCP(State& state) const;
417
418 protected: /*methods*/
419 virtual ~Program();
420
421 protected: /*data*/
422
423 mutable osg::buffered_value< osg::ref_ptr<ProgramObjects> > _pcpList;
424 AttribBindingList _attribBindingList;
425 FragDataBindingList _fragDataBindingList;
426 UniformBlockBindingList _uniformBlockBindingList;
427
428 typedef std::vector< ref_ptr<Shader> > ShaderList;
429 ShaderList _shaderList;
430
431 osg::ref_ptr<ProgramBinary> _programBinary;
432
433 /** Parameters maintained with glProgramParameteriEXT */
434 GLint _geometryVerticesOut;
435 GLint _geometryInputType;
436 GLint _geometryOutputType;
437
438 /**TransformFeedBack**/
439 GLenum _feedbackmode;
440 std::vector<std::string> _feedbackout;
441
442 ShaderDefines _shaderDefines;
443
444
445 private:
446 Program& operator=(const Program&); // disallowed
447
448};
449
450}
451
452#endif