| 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 | |
|---|
| 14 | #ifndef OSG_VERTEXPROGRAM |
|---|
| 15 | #define OSG_VERTEXPROGRAM 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/StateAttribute> |
|---|
| 18 | #include <osg/Vec4> |
|---|
| 19 | #include <osg/Matrix> |
|---|
| 20 | #include <osg/buffered_value> |
|---|
| 21 | |
|---|
| 22 | #include <map> |
|---|
| 23 | #include <string> |
|---|
| 24 | |
|---|
| 25 | // if not defined by gl.h use the definition found in: |
|---|
| 26 | // http://oss.sgi.com/projects/ogl-sample/registry/ARB/vertex_program.txt |
|---|
| 27 | #ifndef GL_ARB_vertex_program |
|---|
| 28 | #define GL_VERTEX_PROGRAM_ARB 0x8620 |
|---|
| 29 | #define GL_VERTEX_PROGRAM_POINT_SIZE_ARB 0x8642 |
|---|
| 30 | #define GL_VERTEX_PROGRAM_TWO_SIDE_ARB 0x8643 |
|---|
| 31 | #define GL_COLOR_SUM_ARB 0x8458 |
|---|
| 32 | #define GL_PROGRAM_FORMAT_ASCII_ARB 0x8875 |
|---|
| 33 | #define GL_VERTEX_ATTRIB_ARRAY_ENABLED_ARB 0x8622 |
|---|
| 34 | #define GL_VERTEX_ATTRIB_ARRAY_SIZE_ARB 0x8623 |
|---|
| 35 | #define GL_VERTEX_ATTRIB_ARRAY_STRIDE_ARB 0x8624 |
|---|
| 36 | #define GL_VERTEX_ATTRIB_ARRAY_TYPE_ARB 0x8625 |
|---|
| 37 | #define GL_VERTEX_ATTRIB_ARRAY_NORMALIZED_ARB 0x886A |
|---|
| 38 | #define GL_CURRENT_VERTEX_ATTRIB_ARB 0x8626 |
|---|
| 39 | #define GL_VERTEX_ATTRIB_ARRAY_POINTER_ARB 0x8645 |
|---|
| 40 | #define GL_PROGRAM_LENGTH_ARB 0x8627 |
|---|
| 41 | #define GL_PROGRAM_FORMAT_ARB 0x8876 |
|---|
| 42 | #define GL_PROGRAM_BINDING_ARB 0x8677 |
|---|
| 43 | #define GL_PROGRAM_INSTRUCTIONS_ARB 0x88A0 |
|---|
| 44 | #define GL_MAX_PROGRAM_INSTRUCTIONS_ARB 0x88A1 |
|---|
| 45 | #define GL_PROGRAM_NATIVE_INSTRUCTIONS_ARB 0x88A2 |
|---|
| 46 | #define GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB 0x88A3 |
|---|
| 47 | #define GL_PROGRAM_TEMPORARIES_ARB 0x88A4 |
|---|
| 48 | #define GL_MAX_PROGRAM_TEMPORARIES_ARB 0x88A5 |
|---|
| 49 | #define GL_PROGRAM_NATIVE_TEMPORARIES_ARB 0x88A6 |
|---|
| 50 | #define GL_MAX_PROGRAM_NATIVE_TEMPORARIES_ARB 0x88A7 |
|---|
| 51 | #define GL_PROGRAM_PARAMETERS_ARB 0x88A8 |
|---|
| 52 | #define GL_MAX_PROGRAM_PARAMETERS_ARB 0x88A9 |
|---|
| 53 | #define GL_PROGRAM_NATIVE_PARAMETERS_ARB 0x88AA |
|---|
| 54 | #define GL_MAX_PROGRAM_NATIVE_PARAMETERS_ARB 0x88AB |
|---|
| 55 | #define GL_PROGRAM_ATTRIBS_ARB 0x88AC |
|---|
| 56 | #define GL_MAX_PROGRAM_ATTRIBS_ARB 0x88AD |
|---|
| 57 | #define GL_PROGRAM_NATIVE_ATTRIBS_ARB 0x88AE |
|---|
| 58 | #define GL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB 0x88AF |
|---|
| 59 | #define GL_PROGRAM_ADDRESS_REGISTERS_ARB 0x88B0 |
|---|
| 60 | #define GL_MAX_PROGRAM_ADDRESS_REGISTERS_ARB 0x88B1 |
|---|
| 61 | #define GL_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB 0x88B2 |
|---|
| 62 | #define GL_MAX_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB 0x88B3 |
|---|
| 63 | #define GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB 0x88B4 |
|---|
| 64 | #define GL_MAX_PROGRAM_ENV_PARAMETERS_ARB 0x88B5 |
|---|
| 65 | #define GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB 0x88B6 |
|---|
| 66 | #define GL_PROGRAM_STRING_ARB 0x8628 |
|---|
| 67 | #define GL_PROGRAM_ERROR_POSITION_ARB 0x864B |
|---|
| 68 | #define GL_CURRENT_MATRIX_ARB 0x8641 |
|---|
| 69 | #define GL_TRANSPOSE_CURRENT_MATRIX_ARB 0x88B7 |
|---|
| 70 | #define GL_CURRENT_MATRIX_STACK_DEPTH_ARB 0x8640 |
|---|
| 71 | #define GL_MAX_VERTEX_ATTRIBS_ARB 0x8869 |
|---|
| 72 | #define GL_MAX_PROGRAM_MATRICES_ARB 0x862F |
|---|
| 73 | #define GL_MAX_PROGRAM_MATRIX_STACK_DEPTH_ARB 0x862E |
|---|
| 74 | #define GL_PROGRAM_ERROR_STRING_ARB 0x8874 |
|---|
| 75 | #define GL_MATRIX0_ARB 0x88C0 |
|---|
| 76 | #define GL_MATRIX1_ARB 0x88C1 |
|---|
| 77 | #define GL_MATRIX2_ARB 0x88C2 |
|---|
| 78 | #define GL_MATRIX3_ARB 0x88C3 |
|---|
| 79 | #define GL_MATRIX4_ARB 0x88C4 |
|---|
| 80 | #define GL_MATRIX5_ARB 0x88C5 |
|---|
| 81 | #define GL_MATRIX6_ARB 0x88C6 |
|---|
| 82 | #define GL_MATRIX7_ARB 0x88C7 |
|---|
| 83 | #define GL_MATRIX8_ARB 0x88C8 |
|---|
| 84 | #define GL_MATRIX9_ARB 0x88C9 |
|---|
| 85 | #define GL_MATRIX10_ARB 0x88CA |
|---|
| 86 | #define GL_MATRIX11_ARB 0x88CB |
|---|
| 87 | #define GL_MATRIX12_ARB 0x88CC |
|---|
| 88 | #define GL_MATRIX13_ARB 0x88CD |
|---|
| 89 | #define GL_MATRIX14_ARB 0x88CE |
|---|
| 90 | #define GL_MATRIX15_ARB 0x88CF |
|---|
| 91 | #define GL_MATRIX16_ARB 0x88D0 |
|---|
| 92 | #define GL_MATRIX17_ARB 0x88D1 |
|---|
| 93 | #define GL_MATRIX18_ARB 0x88D2 |
|---|
| 94 | #define GL_MATRIX19_ARB 0x88D3 |
|---|
| 95 | #define GL_MATRIX20_ARB 0x88D4 |
|---|
| 96 | #define GL_MATRIX21_ARB 0x88D5 |
|---|
| 97 | #define GL_MATRIX22_ARB 0x88D6 |
|---|
| 98 | #define GL_MATRIX23_ARB 0x88D7 |
|---|
| 99 | #define GL_MATRIX24_ARB 0x88D8 |
|---|
| 100 | #define GL_MATRIX25_ARB 0x88D9 |
|---|
| 101 | #define GL_MATRIX26_ARB 0x88DA |
|---|
| 102 | #define GL_MATRIX27_ARB 0x88DB |
|---|
| 103 | #define GL_MATRIX28_ARB 0x88DC |
|---|
| 104 | #define GL_MATRIX29_ARB 0x88DD |
|---|
| 105 | #define GL_MATRIX30_ARB 0x88DE |
|---|
| 106 | #define GL_MATRIX31_ARB 0x88DF |
|---|
| 107 | #endif |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | namespace osg { |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | /** VertexProgram - encapsulates the OpenGL ARB vertex program state. */ |
|---|
| 115 | class OSG_EXPORT VertexProgram : public StateAttribute |
|---|
| 116 | { |
|---|
| 117 | public: |
|---|
| 118 | |
|---|
| 119 | VertexProgram(); |
|---|
| 120 | |
|---|
| 121 | /** Copy constructor using CopyOp to manage deep vs shallow copy. */ |
|---|
| 122 | VertexProgram(const VertexProgram& vp,const CopyOp& copyop=CopyOp::SHALLOW_COPY); |
|---|
| 123 | |
|---|
| 124 | META_StateAttribute(osg, VertexProgram, VERTEXPROGRAM); |
|---|
| 125 | |
|---|
| 126 | /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */ |
|---|
| 127 | virtual int compare(const osg::StateAttribute& sa) const |
|---|
| 128 | { |
|---|
| 129 | // check the types are equal and then create the rhs variable |
|---|
| 130 | // used by the COMPARE_StateAttribute_Parameter macros below. |
|---|
| 131 | COMPARE_StateAttribute_Types(VertexProgram,sa) |
|---|
| 132 | |
|---|
| 133 | // compare each parameter in turn against the rhs. |
|---|
| 134 | COMPARE_StateAttribute_Parameter(_vertexProgram) |
|---|
| 135 | |
|---|
| 136 | return 0; // passed all the above comparison macros, must be equal. |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | virtual bool getModeUsage(StateAttribute::ModeUsage& usage) const |
|---|
| 140 | { |
|---|
| 141 | usage.usesMode(GL_VERTEX_PROGRAM_ARB); |
|---|
| 142 | return true; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | // data access methods. |
|---|
| 146 | |
|---|
| 147 | /** Get the handle to the vertex program ID for the current context. */ |
|---|
| 148 | inline GLuint& getVertexProgramID(unsigned int contextID) const |
|---|
| 149 | { |
|---|
| 150 | return _vertexProgramIDList[contextID]; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | /** Set the vertex program using a C style string. */ |
|---|
| 155 | inline void setVertexProgram( const char* program ) |
|---|
| 156 | { |
|---|
| 157 | _vertexProgram = program; |
|---|
| 158 | dirtyVertexProgramObject(); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | /** Set the vertex program using C++ style string. */ |
|---|
| 162 | inline void setVertexProgram( const std::string& program ) |
|---|
| 163 | { |
|---|
| 164 | _vertexProgram = program; |
|---|
| 165 | dirtyVertexProgramObject(); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | /** Get the vertex program. */ |
|---|
| 169 | inline const std::string& getVertexProgram() const { return _vertexProgram; } |
|---|
| 170 | |
|---|
| 171 | /** Set Program Parameters */ |
|---|
| 172 | inline void setProgramLocalParameter(const GLuint index, const Vec4& p) |
|---|
| 173 | { |
|---|
| 174 | _programLocalParameters[index] = p; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | typedef std::map<GLuint,Vec4> LocalParamList; |
|---|
| 178 | |
|---|
| 179 | /** Set list of Program Parameters */ |
|---|
| 180 | inline void setLocalParameters(const LocalParamList& lpl) { _programLocalParameters = lpl; } |
|---|
| 181 | |
|---|
| 182 | /** Get list of Program Parameters */ |
|---|
| 183 | inline LocalParamList& getLocalParameters() { return _programLocalParameters; } |
|---|
| 184 | |
|---|
| 185 | /** Get const list of Program Parameters */ |
|---|
| 186 | inline const LocalParamList& getLocalParameters() const { return _programLocalParameters; } |
|---|
| 187 | |
|---|
| 188 | /** Matrix */ |
|---|
| 189 | inline void setMatrix(const GLenum mode, const Matrix& matrix) |
|---|
| 190 | { |
|---|
| 191 | _matrixList[mode] = matrix; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | typedef std::map<GLenum,Matrix> MatrixList; |
|---|
| 195 | |
|---|
| 196 | /** Set list of Matrices */ |
|---|
| 197 | inline void setMatrices(const MatrixList& matrices) { _matrixList = matrices; } |
|---|
| 198 | |
|---|
| 199 | /** Get list of Matrices */ |
|---|
| 200 | inline MatrixList& getMatrices() { return _matrixList; } |
|---|
| 201 | |
|---|
| 202 | /** Get list of Matrices */ |
|---|
| 203 | inline const MatrixList& getMatrices() const { return _matrixList; } |
|---|
| 204 | |
|---|
| 205 | /** Force a recompile on next apply() of associated OpenGL vertex program objects. */ |
|---|
| 206 | void dirtyVertexProgramObject(); |
|---|
| 207 | |
|---|
| 208 | /** Use deleteVertexProgramObject instead of glDeletePrograms to allow |
|---|
| 209 | * OpenGL Vertex Program objects to cached until they can be deleted |
|---|
| 210 | * by the OpenGL context in which they were created, specified |
|---|
| 211 | * by contextID. |
|---|
| 212 | */ |
|---|
| 213 | static void deleteVertexProgramObject(unsigned int contextID,GLuint handle); |
|---|
| 214 | |
|---|
| 215 | /** Flush all the cached vertex programs which need to be deleted |
|---|
| 216 | * in the OpenGL context related to contextID. |
|---|
| 217 | */ |
|---|
| 218 | static void flushDeletedVertexProgramObjects(unsigned int contextID,double currentTime, double& availableTime); |
|---|
| 219 | |
|---|
| 220 | /** discard all the cached vertex programs which need to be deleted |
|---|
| 221 | * in the OpenGL context related to contextID. |
|---|
| 222 | * Note, unlike flush no OpenGL calls are made, instead the handles are all removed. |
|---|
| 223 | * this call is useful for when an OpenGL context has been destroyed. |
|---|
| 224 | */ |
|---|
| 225 | static void discardDeletedVertexProgramObjects(unsigned int contextID); |
|---|
| 226 | |
|---|
| 227 | virtual void apply(State& state) const; |
|---|
| 228 | |
|---|
| 229 | virtual void compileGLObjects(State& state) const { apply(state); } |
|---|
| 230 | |
|---|
| 231 | /** Resize any per context GLObject buffers to specified size. */ |
|---|
| 232 | virtual void resizeGLObjectBuffers(unsigned int maxSize); |
|---|
| 233 | |
|---|
| 234 | /** Release any OpenGL objects in specified graphics context if State |
|---|
| 235 | * object is passed, otherwise release OpenGL objects for all graphics contexts if |
|---|
| 236 | * State object pointer is NULL. |
|---|
| 237 | */ |
|---|
| 238 | virtual void releaseGLObjects(State* state=0) const; |
|---|
| 239 | |
|---|
| 240 | /** Extensions class which encapsulates the querying of extensions and |
|---|
| 241 | * associated function pointers, and provide convenience wrappers to |
|---|
| 242 | * check for the extensions or use the associated functions. |
|---|
| 243 | */ |
|---|
| 244 | class OSG_EXPORT Extensions : public osg::Referenced |
|---|
| 245 | { |
|---|
| 246 | public: |
|---|
| 247 | Extensions(unsigned int contextID); |
|---|
| 248 | |
|---|
| 249 | Extensions(const Extensions& rhs); |
|---|
| 250 | |
|---|
| 251 | void lowestCommonDenominator(const Extensions& rhs); |
|---|
| 252 | |
|---|
| 253 | void setupGLExtensions(unsigned int contextID); |
|---|
| 254 | |
|---|
| 255 | void setVertexProgramSupported(bool flag) { _isVertexProgramSupported=flag; } |
|---|
| 256 | bool isVertexProgramSupported() const { return _isVertexProgramSupported; } |
|---|
| 257 | |
|---|
| 258 | void glBindProgram(GLenum target, GLuint id) const; |
|---|
| 259 | void glGenPrograms(GLsizei n, GLuint *programs) const; |
|---|
| 260 | void glDeletePrograms(GLsizei n, GLuint *programs) const; |
|---|
| 261 | void glProgramString(GLenum target, GLenum format, GLsizei len, const void *string) const; |
|---|
| 262 | void glProgramLocalParameter4fv(GLenum target, GLuint index, const GLfloat *params) const; |
|---|
| 263 | |
|---|
| 264 | protected: |
|---|
| 265 | |
|---|
| 266 | ~Extensions() {} |
|---|
| 267 | |
|---|
| 268 | bool _isVertexProgramSupported; |
|---|
| 269 | |
|---|
| 270 | typedef void (GL_APIENTRY * BindProgramProc) (GLenum target, GLuint id); |
|---|
| 271 | typedef void (GL_APIENTRY * GenProgramsProc) (GLsizei n, GLuint *programs); |
|---|
| 272 | typedef void (GL_APIENTRY * DeleteProgramsProc) (GLsizei n, GLuint *programs); |
|---|
| 273 | typedef void (GL_APIENTRY * ProgramStringProc) (GLenum target, GLenum format, GLsizei len, const void *string); |
|---|
| 274 | typedef void (GL_APIENTRY * ProgramLocalParameter4fvProc) (GLenum target, GLuint index, const GLfloat *params); |
|---|
| 275 | |
|---|
| 276 | BindProgramProc _glBindProgram; |
|---|
| 277 | GenProgramsProc _glGenPrograms; |
|---|
| 278 | DeleteProgramsProc _glDeletePrograms; |
|---|
| 279 | ProgramStringProc _glProgramString; |
|---|
| 280 | ProgramLocalParameter4fvProc _glProgramLocalParameter4fv; |
|---|
| 281 | |
|---|
| 282 | }; |
|---|
| 283 | |
|---|
| 284 | /** Function to call to get the extension of a specified context. |
|---|
| 285 | * If the Extension object for that context has not yet been created |
|---|
| 286 | * and the 'createIfNotInitalized' flag been set to false then returns NULL. |
|---|
| 287 | * If 'createIfNotInitalized' is true then the Extensions object is |
|---|
| 288 | * automatically created. However, in this case the extension object |
|---|
| 289 | * will only be created with the graphics context associated with ContextID. |
|---|
| 290 | */ |
|---|
| 291 | static Extensions* getExtensions(unsigned int contextID,bool createIfNotInitalized); |
|---|
| 292 | |
|---|
| 293 | /** The setExtensions method allows users to override the extensions across graphics contexts. |
|---|
| 294 | * Typically used when you have different extensions supported across graphics pipes |
|---|
| 295 | * but need to ensure that they all use the same low common denominator extensions. |
|---|
| 296 | */ |
|---|
| 297 | static void setExtensions(unsigned int contextID,Extensions* extensions); |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | protected: |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | virtual ~VertexProgram(); |
|---|
| 304 | |
|---|
| 305 | typedef buffered_value<GLuint> VertexProgramIDList; |
|---|
| 306 | mutable VertexProgramIDList _vertexProgramIDList; |
|---|
| 307 | |
|---|
| 308 | std::string _vertexProgram; |
|---|
| 309 | |
|---|
| 310 | LocalParamList _programLocalParameters; |
|---|
| 311 | |
|---|
| 312 | MatrixList _matrixList; |
|---|
| 313 | }; |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | #endif |
|---|
| 320 | |
|---|