| 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 | /////////////////////////////////////////////////////////////////////////// |
|---|
| 34 | /** osg::Shader is an application-level abstraction of an OpenGL glShader. |
|---|
| 35 | * It is a container to load the shader source code text and manage its |
|---|
| 36 | * compilation. |
|---|
| 37 | * An osg::Shader may be attached to more than one osg::Program. |
|---|
| 38 | * Shader will automatically manage per-context instancing of the |
|---|
| 39 | * internal objects, if that is necessary for a particular display |
|---|
| 40 | * configuration. |
|---|
| 41 | */ |
|---|
| 42 | |
|---|
| 43 | class OSG_EXPORT Shader : public osg::Object |
|---|
| 44 | { |
|---|
| 45 | public: |
|---|
| 46 | |
|---|
| 47 | enum Type { |
|---|
| 48 | VERTEX = GL_VERTEX_SHADER, |
|---|
| 49 | FRAGMENT = GL_FRAGMENT_SHADER, |
|---|
| 50 | GEOMETRY = GL_GEOMETRY_SHADER_EXT, |
|---|
| 51 | UNDEFINED = -1 |
|---|
| 52 | }; |
|---|
| 53 | |
|---|
| 54 | Shader( Type type = UNDEFINED); |
|---|
| 55 | Shader( Type type, const std::string& source ); |
|---|
| 56 | |
|---|
| 57 | /** Copy constructor using CopyOp to manage deep vs shallow copy.*/ |
|---|
| 58 | Shader(const Shader& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); |
|---|
| 59 | |
|---|
| 60 | META_Object(osg, Shader); |
|---|
| 61 | |
|---|
| 62 | int compare(const Shader& rhs) const; |
|---|
| 63 | |
|---|
| 64 | bool setType( Type t ); |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | /** Load the Shader's source code text from a string. */ |
|---|
| 68 | void setShaderSource( const std::string& sourceText ); |
|---|
| 69 | |
|---|
| 70 | /** Read shader source from file and then constructor shader of specified type. |
|---|
| 71 | * Return the resulting Shader or 0 if no valid shader source code be read.*/ |
|---|
| 72 | static Shader* readShaderFile( Type type, const std::string& fileName ); |
|---|
| 73 | |
|---|
| 74 | /** Load the Shader's source code text from a file. */ |
|---|
| 75 | bool loadShaderSourceFromFile( const std::string& fileName ); |
|---|
| 76 | |
|---|
| 77 | /** Query the shader's source code text */ |
|---|
| 78 | inline const std::string& getShaderSource() const { return _shaderSource; } |
|---|
| 79 | |
|---|
| 80 | /** Get the Shader type as an enum. */ |
|---|
| 81 | inline Type getType() const { return _type; } |
|---|
| 82 | |
|---|
| 83 | /** Get the Shader type as a descriptive string. */ |
|---|
| 84 | const char* getTypename() const; |
|---|
| 85 | |
|---|
| 86 | /** Set file name for the shader source code. */ |
|---|
| 87 | inline void setFileName(const std::string& fileName) { _shaderFileName = fileName; } |
|---|
| 88 | |
|---|
| 89 | /** Get filename to which the shader source code belongs. */ |
|---|
| 90 | inline const std::string& getFileName() const { return _shaderFileName; } |
|---|
| 91 | |
|---|
| 92 | /** Resize any per context GLObject buffers to specified size. */ |
|---|
| 93 | virtual void resizeGLObjectBuffers(unsigned int maxSize); |
|---|
| 94 | |
|---|
| 95 | /** release OpenGL objects in specified graphics context if State |
|---|
| 96 | object is passed, otherwise release OpenGL objects for all graphics context if |
|---|
| 97 | State object pointer NULL.*/ |
|---|
| 98 | void releaseGLObjects(osg::State* state=0) const; |
|---|
| 99 | |
|---|
| 100 | /** Mark our PCSs as needing recompilation. |
|---|
| 101 | * Also mark Programs that depend on us as needing relink */ |
|---|
| 102 | void dirtyShader(); |
|---|
| 103 | |
|---|
| 104 | /** If needed, compile the PCS's glShader */ |
|---|
| 105 | void compileShader(osg::State& state) const; |
|---|
| 106 | |
|---|
| 107 | /** For a given GL context, attach a glShader to a glProgram */ |
|---|
| 108 | void attachShader(unsigned int contextID, GLuint program) const; |
|---|
| 109 | |
|---|
| 110 | /** For a given GL context, detach a glShader to a glProgram */ |
|---|
| 111 | void detachShader(unsigned int contextID, GLuint program) const; |
|---|
| 112 | |
|---|
| 113 | /** Query InfoLog from a glShader */ |
|---|
| 114 | bool getGlShaderInfoLog(unsigned int contextID, std::string& log) const; |
|---|
| 115 | |
|---|
| 116 | /** Mark internal glShader for deletion. |
|---|
| 117 | * Deletion requests are queued until they can be executed |
|---|
| 118 | * in the proper GL context. */ |
|---|
| 119 | static void deleteGlShader(unsigned int contextID, GLuint shader); |
|---|
| 120 | |
|---|
| 121 | /** flush all the cached glShaders which need to be deleted |
|---|
| 122 | * in the OpenGL context related to contextID.*/ |
|---|
| 123 | static void flushDeletedGlShaders(unsigned int contextID,double currentTime, double& availableTime); |
|---|
| 124 | |
|---|
| 125 | /** discard all the cached glShaders which need to be deleted in the OpenGL context related to contextID. |
|---|
| 126 | * Note, unlike flush no OpenGL calls are made, instead the handles are all removed. |
|---|
| 127 | * this call is useful for when an OpenGL context has been destroyed. */ |
|---|
| 128 | static void discardDeletedGlShaders(unsigned int contextID); |
|---|
| 129 | |
|---|
| 130 | static Shader::Type getTypeId( const std::string& tname ); |
|---|
| 131 | |
|---|
| 132 | protected: |
|---|
| 133 | /** PerContextShader (PCS) is an OSG-internal encapsulation of glShader per-GL context. */ |
|---|
| 134 | class PerContextShader : public osg::Referenced |
|---|
| 135 | { |
|---|
| 136 | public: |
|---|
| 137 | PerContextShader(const Shader* shader, unsigned int contextID); |
|---|
| 138 | |
|---|
| 139 | GLuint getHandle() const {return _glShaderHandle;} |
|---|
| 140 | |
|---|
| 141 | void requestCompile(); |
|---|
| 142 | void compileShader(osg::State& state); |
|---|
| 143 | bool needsCompile() const {return _needsCompile;} |
|---|
| 144 | bool isCompiled() const {return _isCompiled;} |
|---|
| 145 | bool getInfoLog( std::string& infoLog ) const; |
|---|
| 146 | |
|---|
| 147 | /** Attach our glShader to a glProgram */ |
|---|
| 148 | void attachShader(GLuint program) const; |
|---|
| 149 | |
|---|
| 150 | /** Detach our glShader from a glProgram */ |
|---|
| 151 | void detachShader(GLuint program) const; |
|---|
| 152 | |
|---|
| 153 | protected: /*methods*/ |
|---|
| 154 | ~PerContextShader(); |
|---|
| 155 | |
|---|
| 156 | protected: /*data*/ |
|---|
| 157 | /** Pointer to our parent osg::Shader */ |
|---|
| 158 | const Shader* _shader; |
|---|
| 159 | /** Pointer to this context's extension functions. */ |
|---|
| 160 | osg::ref_ptr<osg::GL2Extensions> _extensions; |
|---|
| 161 | /** Handle to the actual glShader. */ |
|---|
| 162 | GLuint _glShaderHandle; |
|---|
| 163 | /** Does our glShader need to be recompiled? */ |
|---|
| 164 | bool _needsCompile; |
|---|
| 165 | /** Is our glShader successfully compiled? */ |
|---|
| 166 | bool _isCompiled; |
|---|
| 167 | const unsigned int _contextID; |
|---|
| 168 | |
|---|
| 169 | private: |
|---|
| 170 | PerContextShader(); // disallowed |
|---|
| 171 | PerContextShader(const PerContextShader&); // disallowed |
|---|
| 172 | PerContextShader& operator=(const PerContextShader&); // disallowed |
|---|
| 173 | }; |
|---|
| 174 | |
|---|
| 175 | protected: /*methods*/ |
|---|
| 176 | virtual ~Shader(); |
|---|
| 177 | |
|---|
| 178 | PerContextShader* getPCS(unsigned int contextID) const; |
|---|
| 179 | |
|---|
| 180 | friend class osg::Program; |
|---|
| 181 | bool addProgramRef( osg::Program* program ); |
|---|
| 182 | bool removeProgramRef( osg::Program* program ); |
|---|
| 183 | |
|---|
| 184 | protected: /*data*/ |
|---|
| 185 | Type _type; |
|---|
| 186 | std::string _shaderSource; |
|---|
| 187 | std::string _shaderFileName; |
|---|
| 188 | /** osg::Programs that this osg::Shader is attached to */ |
|---|
| 189 | typedef std::set< osg::Program* > ProgramSet; |
|---|
| 190 | ProgramSet _programSet; |
|---|
| 191 | mutable osg::buffered_value< osg::ref_ptr<PerContextShader> > _pcsList; |
|---|
| 192 | |
|---|
| 193 | private: |
|---|
| 194 | Shader& operator=(const Shader&); // disallowed |
|---|
| 195 | }; |
|---|
| 196 | |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | #endif |
|---|