| 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 | * |
|---|
| 6 | * This application is open source and may be redistributed and/or modified |
|---|
| 7 | * freely and without restriction, both in commericial and non commericial |
|---|
| 8 | * applications, as long as this copyright notice is maintained. |
|---|
| 9 | * |
|---|
| 10 | * This application is distributed in the hope that it will be useful, |
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | /* file: include/osg/Shader |
|---|
| 16 | * author: Mike Weiblen 2008-01-02 |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #ifndef OSG_SHADER |
|---|
| 20 | #define OSG_SHADER 1 |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | #include <osg/GL2Extensions> |
|---|
| 24 | #include <osg/Object> |
|---|
| 25 | #include <osg/buffered_value> |
|---|
| 26 | |
|---|
| 27 | #include <set> |
|---|
| 28 | |
|---|
| 29 | namespace osg { |
|---|
| 30 | |
|---|
| 31 | class Program; |
|---|
| 32 | |
|---|
| 33 | /** Simple class for wrapping up the data used in OpenGL ES 2's glShaderBinary calls. |
|---|
| 34 | * ShaderBinary is set up with the binary data then assigned to one or more osg::Shader. */ |
|---|
| 35 | class OSG_EXPORT ShaderBinary : public osg::Object |
|---|
| 36 | { |
|---|
| 37 | public: |
|---|
| 38 | |
|---|
| 39 | ShaderBinary(); |
|---|
| 40 | |
|---|
| 41 | /** Copy constructor using CopyOp to manage deep vs shallow copy.*/ |
|---|
| 42 | ShaderBinary(const ShaderBinary& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); |
|---|
| 43 | |
|---|
| 44 | META_Object(osg, ShaderBinary); |
|---|
| 45 | |
|---|
| 46 | /** Allocated a data buffer of specified size/*/ |
|---|
| 47 | void allocate(unsigned int size); |
|---|
| 48 | |
|---|
| 49 | /** Assign shader binary data, copying the specified data into locally stored data buffer, the original data can then be deleted.*/ |
|---|
| 50 | void assign(unsigned int size, const unsigned char* data); |
|---|
| 51 | |
|---|
| 52 | /** Get the size of the shader binary data.*/ |
|---|
| 53 | unsigned int getSize() const { return _data.size(); } |
|---|
| 54 | |
|---|
| 55 | /** Get a ptr to the shader binary data.*/ |
|---|
| 56 | unsigned char* getData() { return _data.empty() ? 0 : &(_data.front()); } |
|---|
| 57 | |
|---|
| 58 | /** Get a const ptr to the shader binary data.*/ |
|---|
| 59 | const unsigned char* getData() const { return _data.empty() ? 0 : &(_data.front()); } |
|---|
| 60 | |
|---|
| 61 | protected: |
|---|
| 62 | |
|---|
| 63 | typedef std::vector<unsigned char> Data; |
|---|
| 64 | Data _data; |
|---|
| 65 | }; |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | /////////////////////////////////////////////////////////////////////////// |
|---|
| 69 | /** osg::Shader is an application-level abstraction of an OpenGL glShader. |
|---|
| 70 | * It is a container to load the shader source code text and manage its |
|---|
| 71 | * compilation. |
|---|
| 72 | * An osg::Shader may be attached to more than one osg::Program. |
|---|
| 73 | * Shader will automatically manage per-context instancing of the |
|---|
| 74 | * internal objects, if that is necessary for a particular display |
|---|
| 75 | * configuration. |
|---|
| 76 | */ |
|---|
| 77 | |
|---|
| 78 | class OSG_EXPORT Shader : public osg::Object |
|---|
| 79 | { |
|---|
| 80 | public: |
|---|
| 81 | |
|---|
| 82 | enum Type { |
|---|
| 83 | VERTEX = GL_VERTEX_SHADER, |
|---|
| 84 | FRAGMENT = GL_FRAGMENT_SHADER, |
|---|
| 85 | GEOMETRY = GL_GEOMETRY_SHADER_EXT, |
|---|
| 86 | UNDEFINED = -1 |
|---|
| 87 | }; |
|---|
| 88 | |
|---|
| 89 | Shader(Type type = UNDEFINED); |
|---|
| 90 | Shader(Type type, const std::string& source ); |
|---|
| 91 | Shader(Type type, ShaderBinary* shaderBinary ); |
|---|
| 92 | |
|---|
| 93 | /** Copy constructor using CopyOp to manage deep vs shallow copy.*/ |
|---|
| 94 | Shader(const Shader& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); |
|---|
| 95 | |
|---|
| 96 | META_Object(osg, Shader); |
|---|
| 97 | |
|---|
| 98 | int compare(const Shader& rhs) const; |
|---|
| 99 | |
|---|
| 100 | /** Set the Shader type as an enum. */ |
|---|
| 101 | bool setType(Type t); |
|---|
| 102 | |
|---|
| 103 | /** Get the Shader type as an enum. */ |
|---|
| 104 | inline Type getType() const { return _type; } |
|---|
| 105 | |
|---|
| 106 | /** Get the Shader type as a descriptive string. */ |
|---|
| 107 | const char* getTypename() const; |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | /** Set file name for the shader source code. */ |
|---|
| 111 | inline void setFileName(const std::string& fileName) { _shaderFileName = fileName; } |
|---|
| 112 | |
|---|
| 113 | /** Get filename to which the shader source code belongs. */ |
|---|
| 114 | inline const std::string& getFileName() const { return _shaderFileName; } |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | /** Set the Shader's source code text from a string. */ |
|---|
| 118 | void setShaderSource(const std::string& sourceText); |
|---|
| 119 | |
|---|
| 120 | /** Query the shader's source code text */ |
|---|
| 121 | inline const std::string& getShaderSource() const { return _shaderSource; } |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | /** Set the Shader using a ShaderBinary. */ |
|---|
| 125 | void setShaderBinary(ShaderBinary* shaderBinary) { _shaderBinary = shaderBinary; } |
|---|
| 126 | |
|---|
| 127 | /** Get the Shader's ShaderBinary, return NULL if none is assigned. */ |
|---|
| 128 | ShaderBinary* getShaderBinary() { return _shaderBinary.get(); } |
|---|
| 129 | |
|---|
| 130 | /** Get the const Shader's ShaderBinary, return NULL if none is assigned. */ |
|---|
| 131 | const ShaderBinary* getShaderBinary() const { return _shaderBinary.get(); } |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | /** Read shader source from file and then constructor shader of specified type. |
|---|
| 135 | * Return the resulting Shader or 0 if no valid shader source code be read.*/ |
|---|
| 136 | static Shader* readShaderFile( Type type, const std::string& fileName ); |
|---|
| 137 | |
|---|
| 138 | /** Load the Shader's source code text from a file. */ |
|---|
| 139 | bool loadShaderSourceFromFile( const std::string& fileName ); |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | /** Resize any per context GLObject buffers to specified size. */ |
|---|
| 144 | virtual void resizeGLObjectBuffers(unsigned int maxSize); |
|---|
| 145 | |
|---|
| 146 | /** release OpenGL objects in specified graphics context if State |
|---|
| 147 | object is passed, otherwise release OpenGL objects for all graphics context if |
|---|
| 148 | State object pointer NULL.*/ |
|---|
| 149 | void releaseGLObjects(osg::State* state=0) const; |
|---|
| 150 | |
|---|
| 151 | /** Mark our PCSs as needing recompilation. |
|---|
| 152 | * Also mark Programs that depend on us as needing relink */ |
|---|
| 153 | void dirtyShader(); |
|---|
| 154 | |
|---|
| 155 | /** If needed, compile the PCS's glShader */ |
|---|
| 156 | void compileShader(osg::State& state) const; |
|---|
| 157 | |
|---|
| 158 | /** For a given GL context, attach a glShader to a glProgram */ |
|---|
| 159 | void attachShader(unsigned int contextID, GLuint program) const; |
|---|
| 160 | |
|---|
| 161 | /** For a given GL context, detach a glShader to a glProgram */ |
|---|
| 162 | void detachShader(unsigned int contextID, GLuint program) const; |
|---|
| 163 | |
|---|
| 164 | /** Query InfoLog from a glShader */ |
|---|
| 165 | bool getGlShaderInfoLog(unsigned int contextID, std::string& log) const; |
|---|
| 166 | |
|---|
| 167 | /** Mark internal glShader for deletion. |
|---|
| 168 | * Deletion requests are queued until they can be executed |
|---|
| 169 | * in the proper GL context. */ |
|---|
| 170 | static void deleteGlShader(unsigned int contextID, GLuint shader); |
|---|
| 171 | |
|---|
| 172 | /** flush all the cached glShaders which need to be deleted |
|---|
| 173 | * in the OpenGL context related to contextID.*/ |
|---|
| 174 | static void flushDeletedGlShaders(unsigned int contextID,double currentTime, double& availableTime); |
|---|
| 175 | |
|---|
| 176 | /** discard all the cached glShaders which need to be deleted in the OpenGL context related to contextID. |
|---|
| 177 | * Note, unlike flush no OpenGL calls are made, instead the handles are all removed. |
|---|
| 178 | * this call is useful for when an OpenGL context has been destroyed. */ |
|---|
| 179 | static void discardDeletedGlShaders(unsigned int contextID); |
|---|
| 180 | |
|---|
| 181 | static Shader::Type getTypeId( const std::string& tname ); |
|---|
| 182 | |
|---|
| 183 | protected: |
|---|
| 184 | /** PerContextShader (PCS) is an OSG-internal encapsulation of glShader per-GL context. */ |
|---|
| 185 | class PerContextShader : public osg::Referenced |
|---|
| 186 | { |
|---|
| 187 | public: |
|---|
| 188 | PerContextShader(const Shader* shader, unsigned int contextID); |
|---|
| 189 | |
|---|
| 190 | GLuint getHandle() const {return _glShaderHandle;} |
|---|
| 191 | |
|---|
| 192 | void requestCompile(); |
|---|
| 193 | void compileShader(osg::State& state); |
|---|
| 194 | bool needsCompile() const {return _needsCompile;} |
|---|
| 195 | bool isCompiled() const {return _isCompiled;} |
|---|
| 196 | bool getInfoLog( std::string& infoLog ) const; |
|---|
| 197 | |
|---|
| 198 | /** Attach our glShader to a glProgram */ |
|---|
| 199 | void attachShader(GLuint program) const; |
|---|
| 200 | |
|---|
| 201 | /** Detach our glShader from a glProgram */ |
|---|
| 202 | void detachShader(GLuint program) const; |
|---|
| 203 | |
|---|
| 204 | protected: /*methods*/ |
|---|
| 205 | ~PerContextShader(); |
|---|
| 206 | |
|---|
| 207 | protected: /*data*/ |
|---|
| 208 | /** Pointer to our parent osg::Shader */ |
|---|
| 209 | const Shader* _shader; |
|---|
| 210 | /** Pointer to this context's extension functions. */ |
|---|
| 211 | osg::ref_ptr<osg::GL2Extensions> _extensions; |
|---|
| 212 | /** Handle to the actual glShader. */ |
|---|
| 213 | GLuint _glShaderHandle; |
|---|
| 214 | /** Does our glShader need to be recompiled? */ |
|---|
| 215 | bool _needsCompile; |
|---|
| 216 | /** Is our glShader successfully compiled? */ |
|---|
| 217 | bool _isCompiled; |
|---|
| 218 | const unsigned int _contextID; |
|---|
| 219 | |
|---|
| 220 | private: |
|---|
| 221 | PerContextShader(); // disallowed |
|---|
| 222 | PerContextShader(const PerContextShader&); // disallowed |
|---|
| 223 | PerContextShader& operator=(const PerContextShader&); // disallowed |
|---|
| 224 | }; |
|---|
| 225 | |
|---|
| 226 | protected: /*methods*/ |
|---|
| 227 | virtual ~Shader(); |
|---|
| 228 | |
|---|
| 229 | PerContextShader* getPCS(unsigned int contextID) const; |
|---|
| 230 | |
|---|
| 231 | friend class osg::Program; |
|---|
| 232 | bool addProgramRef( osg::Program* program ); |
|---|
| 233 | bool removeProgramRef( osg::Program* program ); |
|---|
| 234 | |
|---|
| 235 | protected: /*data*/ |
|---|
| 236 | Type _type; |
|---|
| 237 | std::string _shaderFileName; |
|---|
| 238 | std::string _shaderSource; |
|---|
| 239 | osg::ref_ptr<ShaderBinary> _shaderBinary; |
|---|
| 240 | |
|---|
| 241 | /** osg::Programs that this osg::Shader is attached to */ |
|---|
| 242 | typedef std::set< osg::Program* > ProgramSet; |
|---|
| 243 | ProgramSet _programSet; |
|---|
| 244 | mutable osg::buffered_value< osg::ref_ptr<PerContextShader> > _pcsList; |
|---|
| 245 | |
|---|
| 246 | private: |
|---|
| 247 | Shader& operator=(const Shader&); // disallowed |
|---|
| 248 | }; |
|---|
| 249 | |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | #endif |
|---|