Program 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. /* file: include/osg/Program
  16. * author: Mike Weiblen 2008-01-02
  17. * Holger Helmich 2010-10-21
  18. */
  19. #ifndef OSG_PROGRAM
  20. #define OSG_PROGRAM 1
  21. #include <string>
  22. #include <vector>
  23. #include <map>
  24. #include <osg/buffered_value>
  25. #include <osg/ref_ptr>
  26. #include <osg/Uniform>
  27. #include <osg/Shader>
  28. #include <osg/StateAttribute>
  29. namespace osg {
  30. class State;
  31. ///////////////////////////////////////////////////////////////////////////
  32. /** osg::Program is an application-level abstraction of an OpenGL glProgram.
  33. * It is an osg::StateAttribute that, when applied, will activate a
  34. * glProgram for subsequent rendering.
  35. * osg::Shaders containing the actual shader source code are
  36. * attached to a Program, which will then manage the compilation,
  37. * linking, and activation of the GLSL program.
  38. * osg::Program will automatically manage per-context instancing of the
  39. * OpenGL glPrograms, if that is necessary for a particular display
  40. * configuration.
  41. */
  42. class OSG_EXPORT Program : public osg::StateAttribute
  43. {
  44. public:
  45. Program();
  46. /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
  47. Program(const Program& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  48. META_StateAttribute(osg, Program, PROGRAM);
  49. /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
  50. virtual int compare(const osg::StateAttribute& sa) const;
  51. /** If enabled, activate our program in the GL pipeline,
  52. * performing any rebuild operations that might be pending. */
  53. virtual void apply(osg::State& state) const;
  54. /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
  55. virtual void setThreadSafeRefUnref(bool threadSafe);
  56. /** Compile program and associated shaders.*/
  57. virtual void compileGLObjects(osg::State& state) const;
  58. /** Resize any per context GLObject buffers to specified size. */
  59. virtual void resizeGLObjectBuffers(unsigned int maxSize);
  60. /** release OpenGL objects in specified graphics context if State
  61. object is passed, otherwise release OpenGL objects for all graphics context if
  62. State object pointer NULL.*/
  63. virtual void releaseGLObjects(osg::State* state=0) const;
  64. /** Mark our PCSOs as needing relink */
  65. void dirtyProgram();
  66. /** Attach an osg::Shader to this osg::Program.
  67. * Mark Program as needing relink. Return true for success */
  68. bool addShader( Shader* shader );
  69. template<class T> bool addShader( const ref_ptr<T>& shader ) { return addShader(shader.get()); }
  70. unsigned int getNumShaders() const { return static_cast<unsigned int>(_shaderList.size()); }
  71. Shader* getShader( unsigned int i ) { return _shaderList[i].get(); }
  72. const Shader* getShader( unsigned int i ) const { return _shaderList[i].get(); }
  73. /** Remove osg::Shader from this osg::Program.
  74. * Mark Program as needing relink. Return true for success */
  75. bool removeShader( Shader* shader );
  76. template<class T> bool removeShader( const ref_ptr<T>& shader ) { return removeShader(shader.get()); }
  77. /** Set/get GL program parameters */
  78. void setParameter( GLenum pname, GLint value );
  79. GLint getParameter( GLenum pname ) const;
  80. /** Add an attribute location binding. */
  81. void addBindAttribLocation( const std::string& name, GLuint index );
  82. /** Remove an attribute location binding. */
  83. void removeBindAttribLocation( const std::string& name );
  84. /** Add an frag data location binding. See EXT_gpu_shader4 for BindFragDataLocationEXT */
  85. void addBindFragDataLocation( const std::string& name, GLuint index );
  86. /** Remove an frag data location binding. */
  87. void removeBindFragDataLocation( const std::string& name );
  88. /** Add a uniform block binding to an index target. XXX This
  89. * should not be an attribute of the program. It should be a
  90. * pseudo-uniform that can live in StateSet objects because
  91. * it is cheap to set. */
  92. void addBindUniformBlock(const std::string& name, GLuint index);
  93. /** Remove a uniform block binding. */
  94. void removeBindUniformBlock(const std::string& name);
  95. /** Remove a TransformFeedBackVarying. */
  96. void removeTransformFeedBackVarying(const std::string& name)
  97. {
  98. for(std::vector<std::string>::iterator i=_feedbackout.begin(); i!=_feedbackout.end(); i++)
  99. {
  100. if (*i == name) {_feedbackout.erase(i);break; }
  101. }
  102. }
  103. /** Add a TransformFeedBack Varying Name. */
  104. void addTransformFeedBackVarying(const std::string& outname)
  105. {
  106. _feedbackout.push_back(outname);
  107. }
  108. /** Get number of TransformFeedBack Varyings. */
  109. inline unsigned int getNumTransformFeedBackVaryings() const { return _feedbackout.size(); }
  110. /** Get const TransformFeedBack Varying at index i. */
  111. inline const std::string& getTransformFeedBackVarying(unsigned int i) const { return _feedbackout[i]; }
  112. /** Set TransformFeedBack Mode. */
  113. void setTransformFeedBackMode(GLenum e) {_feedbackmode=e;}
  114. /** Get TransformFeedBack Mode. */
  115. GLenum getTransformFeedBackMode() const {return _feedbackmode;}
  116. /** Experimental. */
  117. void setShaderDefines(const ShaderDefines& shaderDefs) { _shaderDefines = shaderDefs; }
  118. ShaderDefines& getShaderDefines() { return _shaderDefines; }
  119. const ShaderDefines& getShaderDefines() const { return _shaderDefines; }
  120. /** Simple class for wrapping up the data used in glProgramBinary and glGetProgramBinary.
  121. * On the first run of your application Programs should be assigned an empty ProgramBinary.
  122. * Before your application exits it should retrieve the program binary via
  123. * Program::PerContextProgram::compileProgramBinary and save it to disk.
  124. * When your application is run subsequently, load your binary from disk and use it to set
  125. * the data of a ProgramBinary, and set the ProgramBinary on the associated Program.
  126. * This will typically result in Program::compileGLObjects executing much faster.*/
  127. class OSG_EXPORT ProgramBinary : public osg::Object
  128. {
  129. public:
  130. ProgramBinary();
  131. /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
  132. ProgramBinary(const ProgramBinary& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  133. META_Object(osg, ProgramBinary);
  134. /** Allocated a data buffer of specified size.*/
  135. void allocate(unsigned int size);
  136. /** Assign program binary data, copying the specified data into locally stored data buffer, the original data can then be deleted.*/
  137. void assign(unsigned int size, const unsigned char* data);
  138. /** Set the format of the program binary data.*/
  139. void setFormat(GLenum format) {_format = format;}
  140. /** Get the format of the program binary data.*/
  141. GLenum getFormat() const {return _format;}
  142. /** Get the size of the program binary data.*/
  143. unsigned int getSize() const { return static_cast<unsigned int>(_data.size()); }
  144. /** Get a ptr to the program binary data.*/
  145. unsigned char* getData() { return _data.empty() ? 0 : &(_data.front()); }
  146. /** Get a const ptr to the program binary data.*/
  147. const unsigned char* getData() const { return _data.empty() ? 0 : &(_data.front()); }
  148. protected:
  149. std::vector<unsigned char> _data;
  150. GLenum _format;
  151. };
  152. /** Set the Program using a ProgramBinary. If a ProgramBinary is not yet
  153. * available then setting an empty one signals that compileProgramBinary
  154. * will be called later.*/
  155. void setProgramBinary(ProgramBinary* programBinary) { _programBinary = programBinary; }
  156. /** Get the Program's ProgramBinary, return NULL if none is assigned. */
  157. ProgramBinary* getProgramBinary() { return _programBinary.get(); }
  158. /** Get the const Program's ProgramBinary, return NULL if none is assigned. */
  159. const ProgramBinary* getProgramBinary() const { return _programBinary.get(); }
  160. typedef std::map<std::string,GLuint> AttribBindingList;
  161. typedef std::map<std::string,GLuint> FragDataBindingList;
  162. typedef std::map<std::string,GLuint> UniformBlockBindingList;
  163. const AttribBindingList& getAttribBindingList() const { return _attribBindingList; }
  164. const FragDataBindingList& getFragDataBindingList() const { return _fragDataBindingList; }
  165. const UniformBlockBindingList& getUniformBlockBindingList() const { return _uniformBlockBindingList; }
  166. /** Return true if this Program represents "fixed-functionality" rendering */
  167. bool isFixedFunction() const;
  168. /** Query InfoLog from a glProgram */
  169. bool getGlProgramInfoLog(unsigned int contextID, std::string& log) const;
  170. struct ActiveVarInfo {
  171. ActiveVarInfo() : _location(-1), _type(Uniform::UNDEFINED), _size(-1) {}
  172. ActiveVarInfo( GLint loc, GLenum type, GLint size ) : _location(loc), _type(type), _size(size) {}
  173. GLint _location;
  174. GLenum _type;
  175. GLint _size;
  176. };
  177. typedef std::map< unsigned int, ActiveVarInfo > ActiveUniformMap;
  178. typedef std::map< std::string, ActiveVarInfo > ActiveVarInfoMap;
  179. //const ActiveUniformMap& getActiveUniforms(unsigned int contextID) const;
  180. //const ActiveVarInfoMap& getActiveAttribs(unsigned int contextID) const;
  181. struct UniformBlockInfo
  182. {
  183. UniformBlockInfo() : _index(GL_INVALID_INDEX), _size(0) {}
  184. UniformBlockInfo(GLuint index, GLsizei size)
  185. : _index(index), _size(size)
  186. {
  187. }
  188. GLuint _index;
  189. GLsizei _size;
  190. };
  191. typedef std::map<std::string, UniformBlockInfo> UniformBlockMap;
  192. //const UniformBlockMap& getUniformBlocks(unsigned contextID) const;
  193. public:
  194. // make PerContextProgram a friend to allow it access Program's protected
  195. // methods and member variables.
  196. class PerContextProgram;
  197. friend class PerContextProgram;
  198. /** PerContextProgram (PCP) is an OSG-internal encapsulation of glPrograms per-GL context. */
  199. class OSG_EXPORT PerContextProgram : public osg::Referenced
  200. {
  201. public:
  202. /** Use "0" as programHandle to let the PeContextProgram execute "glCreateProgram"and "glDeleteProgram" */
  203. PerContextProgram(const Program* program, unsigned int contextID, GLuint programHandle=0);
  204. GLuint getHandle() const {return _glProgramHandle;}
  205. const osg::Program* getProgram() const { return _program; }
  206. void setDefineString(const std::string& defStr) { _defineStr = defStr; }
  207. const std::string& getDefineString() const { return _defineStr; }
  208. void requestLink();
  209. virtual void linkProgram(osg::State& state);
  210. virtual bool validateProgram();
  211. bool needsLink() const {return _needsLink;}
  212. bool isLinked() const {return _isLinked;}
  213. virtual bool getInfoLog( std::string& infoLog ) const;
  214. /** Was glProgramBinary called successfully? */
  215. bool loadedBinary() const {return _loadedBinary;}
  216. /** Compile a program binary. For this to work setProgramBinary must have
  217. * been called on the osg::Program with an empty ProgramBinary prior to
  218. * compileGLObjects being called.
  219. * compileProgramBinary should be called after the program has been
  220. * "exercised" by rendering with it. The ProgramBinary can then be saved
  221. * to disk for faster subsequent compiling. */
  222. virtual ProgramBinary* compileProgramBinary(osg::State& state);
  223. virtual void useProgram() const;
  224. void resetAppliedUniforms() const
  225. {
  226. _lastAppliedUniformList.clear();
  227. }
  228. inline void apply(const Uniform& uniform) const
  229. {
  230. GLint location = getUniformLocation(uniform.getNameID());
  231. if (location>=0)
  232. {
  233. const Uniform* lastAppliedUniform = _lastAppliedUniformList[location].first.get();
  234. if (lastAppliedUniform != &uniform)
  235. {
  236. // new attribute
  237. uniform.apply(_extensions.get(),location);
  238. _lastAppliedUniformList[location].first = &uniform;
  239. _lastAppliedUniformList[location].second = uniform.getModifiedCount();
  240. }
  241. else if (_lastAppliedUniformList[location].second != uniform.getModifiedCount())
  242. {
  243. // existing attribute has been modified
  244. uniform.apply(_extensions.get(),location);
  245. _lastAppliedUniformList[location].first = &uniform;
  246. _lastAppliedUniformList[location].second = uniform.getModifiedCount();
  247. }
  248. }
  249. }
  250. const ActiveUniformMap& getActiveUniforms() const {return _uniformInfoMap;}
  251. const ActiveVarInfoMap& getActiveAttribs() const {return _attribInfoMap;}
  252. const UniformBlockMap& getUniformBlocks() const {return _uniformBlockMap; }
  253. inline GLint getUniformLocation( unsigned int uniformNameID ) const { ActiveUniformMap::const_iterator itr = _uniformInfoMap.find(uniformNameID); return (itr!=_uniformInfoMap.end()) ? itr->second._location : -1; }
  254. /**
  255. * Alternative version of getUniformLocation( unsigned int uniformNameID )
  256. * retrofited into OSG for backward compatibility with osgCal,
  257. * after uniform ids were refactored from std::strings to GLints in OSG version 2.9.10.
  258. *
  259. * Drawbacks: This method is not particularly fast. It has to access mutexed static
  260. * map of uniform ids. So don't overuse it or your app performance will suffer.
  261. */
  262. inline GLint getUniformLocation( const std::string & uniformName ) const { return getUniformLocation( Uniform::getNameID( uniformName ) ); }
  263. inline GLint getAttribLocation( const std::string& name ) const { ActiveVarInfoMap::const_iterator itr = _attribInfoMap.find(name); return (itr!=_attribInfoMap.end()) ? itr->second._location : -1; }
  264. inline void addShaderToAttach(Shader *shader)
  265. {
  266. _shadersToAttach.push_back(shader);
  267. }
  268. inline void addShaderToDetach(Shader *shader)
  269. {
  270. _shadersToDetach.push_back(shader);
  271. }
  272. protected: /*methods*/
  273. virtual ~PerContextProgram();
  274. protected: /*data*/
  275. /** Pointer to our parent Program */
  276. const Program* _program;
  277. /** Pointer to this context's extension functions */
  278. osg::ref_ptr<GLExtensions> _extensions;
  279. /** Handle to the actual OpenGL glProgram */
  280. GLuint _glProgramHandle;
  281. /** Define string passed on to Shaders to help configure them.*/
  282. std::string _defineStr;
  283. /** Does our glProgram need to be linked? */
  284. bool _needsLink;
  285. /** Is our glProgram successfully linked? */
  286. bool _isLinked;
  287. /** Was glProgramBinary called successfully? */
  288. bool _loadedBinary;
  289. const unsigned int _contextID;
  290. /** Does the glProgram handle belongs to this class? */
  291. bool _ownsProgramHandle;
  292. ActiveUniformMap _uniformInfoMap;
  293. ActiveVarInfoMap _attribInfoMap;
  294. UniformBlockMap _uniformBlockMap;
  295. typedef std::pair<osg::ref_ptr<const osg::Uniform>, unsigned int> UniformModifiedCountPair;
  296. typedef std::map<unsigned int, UniformModifiedCountPair> LastAppliedUniformList;
  297. mutable LastAppliedUniformList _lastAppliedUniformList;
  298. typedef std::vector< ref_ptr<Shader> > ShaderList;
  299. ShaderList _shadersToDetach;
  300. ShaderList _shadersToAttach;
  301. private:
  302. PerContextProgram(); // disallowed
  303. PerContextProgram(const PerContextProgram&); // disallowed
  304. PerContextProgram& operator=(const PerContextProgram&); // disallowed
  305. };
  306. struct OSG_EXPORT ProgramObjects : public osg::GraphicsObject
  307. {
  308. typedef std::vector< osg::ref_ptr<PerContextProgram> > PerContextPrograms;
  309. ProgramObjects(const Program* program, unsigned int contextID);
  310. unsigned int _contextID;
  311. const Program* _program;
  312. mutable PerContextPrograms _perContextPrograms;
  313. PerContextProgram* getPCP(const std::string& defineStr) const;
  314. PerContextProgram* createPerContextProgram(const std::string& defineStr);
  315. void requestLink();
  316. void addShaderToAttach(Shader* shader);
  317. void addShaderToDetach(Shader* shader);
  318. bool getGlProgramInfoLog(std::string& log) const;
  319. };
  320. /** Get the PCP for a particular GL context */
  321. PerContextProgram* getPCP(State& state) const;
  322. protected: /*methods*/
  323. virtual ~Program();
  324. protected: /*data*/
  325. mutable osg::buffered_value< osg::ref_ptr<ProgramObjects> > _pcpList;
  326. AttribBindingList _attribBindingList;
  327. FragDataBindingList _fragDataBindingList;
  328. UniformBlockBindingList _uniformBlockBindingList;
  329. typedef std::vector< ref_ptr<Shader> > ShaderList;
  330. ShaderList _shaderList;
  331. osg::ref_ptr<ProgramBinary> _programBinary;
  332. /** Parameters maintained with glProgramParameteriEXT */
  333. GLint _geometryVerticesOut;
  334. GLint _geometryInputType;
  335. GLint _geometryOutputType;
  336. /**TransformFeedBack**/
  337. GLenum _feedbackmode;
  338. std::vector<std::string> _feedbackout;
  339. ShaderDefines _shaderDefines;
  340. private:
  341. Program& operator=(const Program&); // disallowed
  342. };
  343. }
  344. #endif