FragmentProgram 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSG_FRAGMENTPROGRAM
  14. #define OSG_FRAGMENTPROGRAM 1
  15. #include <osg/StateAttribute>
  16. #include <osg/Vec4>
  17. #include <osg/Matrix>
  18. #include <osg/buffered_value>
  19. #include <map>
  20. #include <string>
  21. // if not defined by gl.h use the definition found in:
  22. // http://oss.sgi.com/projects/ogl-sample/registry/ARB/fragment_program.txt
  23. #ifndef GL_ARB_fragment_program
  24. #define GL_FRAGMENT_PROGRAM_ARB 0x8804
  25. #define GL_PROGRAM_FORMAT_ASCII_ARB 0x8875
  26. #define GL_PROGRAM_LENGTH_ARB 0x8627
  27. #define GL_PROGRAM_FORMAT_ARB 0x8876
  28. #define GL_PROGRAM_BINDING_ARB 0x8677
  29. #define GL_PROGRAM_INSTRUCTIONS_ARB 0x88A0
  30. #define GL_MAX_PROGRAM_INSTRUCTIONS_ARB 0x88A1
  31. #define GL_PROGRAM_NATIVE_INSTRUCTIONS_ARB 0x88A2
  32. #define GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB 0x88A3
  33. #define GL_PROGRAM_TEMPORARIES_ARB 0x88A4
  34. #define GL_MAX_PROGRAM_TEMPORARIES_ARB 0x88A5
  35. #define GL_PROGRAM_NATIVE_TEMPORARIES_ARB 0x88A6
  36. #define GL_MAX_PROGRAM_NATIVE_TEMPORARIES_ARB 0x88A7
  37. #define GL_PROGRAM_PARAMETERS_ARB 0x88A8
  38. #define GL_MAX_PROGRAM_PARAMETERS_ARB 0x88A9
  39. #define GL_PROGRAM_NATIVE_PARAMETERS_ARB 0x88AA
  40. #define GL_MAX_PROGRAM_NATIVE_PARAMETERS_ARB 0x88AB
  41. #define GL_PROGRAM_ATTRIBS_ARB 0x88AC
  42. #define GL_MAX_PROGRAM_ATTRIBS_ARB 0x88AD
  43. #define GL_PROGRAM_NATIVE_ATTRIBS_ARB 0x88AE
  44. #define GL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB 0x88AF
  45. #define GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB 0x88B4
  46. #define GL_MAX_PROGRAM_ENV_PARAMETERS_ARB 0x88B5
  47. #define GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB 0x88B6
  48. #define GL_PROGRAM_ALU_INSTRUCTIONS_ARB 0x8805
  49. #define GL_PROGRAM_TEX_INSTRUCTIONS_ARB 0x8806
  50. #define GL_PROGRAM_TEX_INDIRECTIONS_ARB 0x8807
  51. #define GL_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB 0x8808
  52. #define GL_PROGRAM_NATIVE_TEX_INSTRUCTIONS_ARB 0x8809
  53. #define GL_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB 0x880A
  54. #define GL_MAX_PROGRAM_ALU_INSTRUCTIONS_ARB 0x880B
  55. #define GL_MAX_PROGRAM_TEX_INSTRUCTIONS_ARB 0x880C
  56. #define GL_MAX_PROGRAM_TEX_INDIRECTIONS_ARB 0x880D
  57. #define GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB 0x880E
  58. #define GL_MAX_PROGRAM_NATIVE_TEX_INSTRUCTIONS_ARB 0x880F
  59. #define GL_MAX_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB 0x8810
  60. #define GL_PROGRAM_STRING_ARB 0x8628
  61. #define GL_PROGRAM_ERROR_POSITION_ARB 0x864B
  62. #define GL_CURRENT_MATRIX_ARB 0x8641
  63. #define GL_TRANSPOSE_CURRENT_MATRIX_ARB 0x88B7
  64. #define GL_CURRENT_MATRIX_STACK_DEPTH_ARB 0x8640
  65. #define GL_MAX_PROGRAM_MATRICES_ARB 0x862F
  66. #define GL_MAX_PROGRAM_MATRIX_STACK_DEPTH_ARB 0x862E
  67. #define GL_MAX_TEXTURE_COORDS_ARB 0x8871
  68. #define GL_MAX_TEXTURE_IMAGE_UNITS_ARB 0x8872
  69. #define GL_PROGRAM_ERROR_STRING_ARB 0x8874
  70. #define GL_MATRIX0_ARB 0x88C0
  71. #define GL_MATRIX1_ARB 0x88C1
  72. #define GL_MATRIX2_ARB 0x88C2
  73. #define GL_MATRIX3_ARB 0x88C3
  74. #define GL_MATRIX4_ARB 0x88C4
  75. #define GL_MATRIX5_ARB 0x88C5
  76. #define GL_MATRIX6_ARB 0x88C6
  77. #define GL_MATRIX7_ARB 0x88C7
  78. #define GL_MATRIX8_ARB 0x88C8
  79. #define GL_MATRIX9_ARB 0x88C9
  80. #define GL_MATRIX10_ARB 0x88CA
  81. #define GL_MATRIX11_ARB 0x88CB
  82. #define GL_MATRIX12_ARB 0x88CC
  83. #define GL_MATRIX13_ARB 0x88CD
  84. #define GL_MATRIX14_ARB 0x88CE
  85. #define GL_MATRIX15_ARB 0x88CF
  86. #define GL_MATRIX16_ARB 0x88D0
  87. #define GL_MATRIX17_ARB 0x88D1
  88. #define GL_MATRIX18_ARB 0x88D2
  89. #define GL_MATRIX19_ARB 0x88D3
  90. #define GL_MATRIX20_ARB 0x88D4
  91. #define GL_MATRIX21_ARB 0x88D5
  92. #define GL_MATRIX22_ARB 0x88D6
  93. #define GL_MATRIX23_ARB 0x88D7
  94. #define GL_MATRIX24_ARB 0x88D8
  95. #define GL_MATRIX25_ARB 0x88D9
  96. #define GL_MATRIX26_ARB 0x88DA
  97. #define GL_MATRIX27_ARB 0x88DB
  98. #define GL_MATRIX28_ARB 0x88DC
  99. #define GL_MATRIX29_ARB 0x88DD
  100. #define GL_MATRIX30_ARB 0x88DE
  101. #define GL_MATRIX31_ARB 0x88DF
  102. #endif
  103. namespace osg {
  104. /** FragmentProgram - encapsulates the OpenGL ARB fragment program state.*/
  105. class OSG_EXPORT FragmentProgram : public StateAttribute
  106. {
  107. public:
  108. FragmentProgram();
  109. /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
  110. FragmentProgram(const FragmentProgram& vp,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  111. META_StateAttribute(osg, FragmentProgram, FRAGMENTPROGRAM);
  112. /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
  113. virtual int compare(const osg::StateAttribute& sa) const
  114. {
  115. // check the types are equal and then create the rhs variable
  116. // used by the COMPARE_StateAttribute_Parameter macros below.
  117. COMPARE_StateAttribute_Types(FragmentProgram,sa)
  118. // compare each parameter in turn against the rhs.
  119. COMPARE_StateAttribute_Parameter(_fragmentProgram)
  120. return 0; // passed all the above comparison macros, must be equal.
  121. }
  122. virtual bool getModeUsage(StateAttribute::ModeUsage& usage) const
  123. {
  124. usage.usesMode(GL_FRAGMENT_PROGRAM_ARB);
  125. return true;
  126. }
  127. // data access methods.
  128. /** Get the handle to the fragment program id for the current context.*/
  129. inline GLuint& getFragmentProgramID(unsigned int contextID) const
  130. {
  131. return _fragmentProgramIDList[contextID];
  132. }
  133. /** Set the fragment program using a C style string.*/
  134. inline void setFragmentProgram( const char* program )
  135. {
  136. _fragmentProgram = program;
  137. dirtyFragmentProgramObject();
  138. }
  139. /** Set the fragment program using C++ style string.*/
  140. inline void setFragmentProgram( const std::string& program )
  141. {
  142. _fragmentProgram = program;
  143. dirtyFragmentProgramObject();
  144. }
  145. /** Get the fragment program.*/
  146. inline const std::string& getFragmentProgram() const { return _fragmentProgram; }
  147. /** Set Program Parameters */
  148. inline void setProgramLocalParameter(const GLuint index, const Vec4& p)
  149. {
  150. _programLocalParameters[index] = p;
  151. }
  152. typedef std::map<GLuint,Vec4> LocalParamList;
  153. /** Set list of Program Parameters */
  154. inline void setLocalParameters(const LocalParamList& lpl) { _programLocalParameters = lpl; }
  155. /** Get list of Program Parameters */
  156. inline LocalParamList& getLocalParameters() { return _programLocalParameters; }
  157. /** Get const list of Program Parameters */
  158. inline const LocalParamList& getLocalParameters() const { return _programLocalParameters; }
  159. /** Matrix */
  160. inline void setMatrix(const GLenum mode, const Matrix& matrix)
  161. {
  162. _matrixList[mode] = matrix;
  163. }
  164. typedef std::map<GLenum,Matrix> MatrixList;
  165. /** Set list of Matrices */
  166. inline void setMatrices(const MatrixList& matrices) { _matrixList = matrices; }
  167. /** Get list of Matrices */
  168. inline MatrixList& getMatrices() { return _matrixList; }
  169. /** Get list of Matrices */
  170. inline const MatrixList& getMatrices() const { return _matrixList; }
  171. /** Force a recompile on next apply() of associated OpenGL vertex program objects.*/
  172. void dirtyFragmentProgramObject();
  173. virtual void apply(State& state) const;
  174. virtual void compileGLObjects(State& state) const { apply(state); }
  175. /** Resize any per context GLObject buffers to specified size. */
  176. virtual void resizeGLObjectBuffers(unsigned int maxSize);
  177. /** release an OpenGL objects in specified graphics context if State
  178. object is passed, otherwise release OpenGL objects for all graphics context if
  179. State object pointer == NULL.*/
  180. virtual void releaseGLObjects(State* state=0) const;
  181. protected:
  182. virtual ~FragmentProgram();
  183. typedef buffered_value<GLuint> FragmentProgramIDList;
  184. mutable FragmentProgramIDList _fragmentProgramIDList;
  185. std::string _fragmentProgram;
  186. LocalParamList _programLocalParameters;
  187. MatrixList _matrixList;
  188. };
  189. }
  190. #endif