OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Shader.h
Go to the documentation of this file.
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 
16 /* file: include/osg/Shader
17  * author: Mike Weiblen 2008-01-02
18  * Holger Helmich 2010-10-21
19 */
20 
21 #ifndef OSG_SHADER
22 #define OSG_SHADER 1
23 
24 
25 #include <osg/GLExtensions>
26 #include <osg/Object>
27 #include <osg/buffered_value>
28 
29 #include <set>
30 #include <map>
31 
32 namespace osg {
33 
34 class Program;
35 
36 // set of shader define strings that the shader is dependent upon.
37 typedef std::set<std::string> ShaderDefines;
38 
42 {
43  public:
44 
45  ShaderBinary();
46 
49 
51 
53  void allocate(unsigned int size);
54 
56  void assign(unsigned int size, const unsigned char* data);
57 
59  unsigned int getSize() const { return static_cast<unsigned int>(_data.size()); }
60 
62  unsigned char* getData() { return _data.empty() ? 0 : &(_data.front()); }
63 
65  const unsigned char* getData() const { return _data.empty() ? 0 : &(_data.front()); }
66 
69  static ShaderBinary* readShaderBinaryFile(const std::string& fileName);
70 
71  protected:
72 
73  typedef std::vector<unsigned char> Data;
74  Data _data;
75 };
76 
77 
79 
89 {
90  public:
91 
92  enum Type {
93  VERTEX = GL_VERTEX_SHADER,
94  TESSCONTROL = GL_TESS_CONTROL_SHADER,
95  TESSEVALUATION = GL_TESS_EVALUATION_SHADER,
96  GEOMETRY = GL_GEOMETRY_SHADER,
97  FRAGMENT = GL_FRAGMENT_SHADER,
98  COMPUTE = GL_COMPUTE_SHADER,
99  UNDEFINED = -1
100  };
101 
102  Shader(Type type = UNDEFINED);
103  Shader(Type type, const std::string& source );
104  Shader(Type type, ShaderBinary* shaderBinary );
105 
107  Shader(const Shader& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
108 
110 
111  int compare(const Shader& rhs) const;
112 
114  bool setType(Type t);
115 
117  inline Type getType() const { return _type; }
118 
120  const char* getTypename() const;
121 
122 
124  inline void setFileName(const std::string& fileName) { _shaderFileName = fileName; }
125 
127  inline const std::string& getFileName() const { return _shaderFileName; }
128 
129 
131  void setShaderSource(const std::string& sourceText);
132 
134  inline const std::string& getShaderSource() const { return _shaderSource; }
135 
136 
138  {
140  USE_MANUAL_SETTINGS
141  };
142 
143  void setShaderDefinesMode(ShaderDefinesMode sdm) { _shaderDefinesMode = sdm; }
144  ShaderDefinesMode getShaderDefinesMode() const { return _shaderDefinesMode; }
145 
146 
147  void setShaderDefines(const ShaderDefines& shaderDefs) { _shaderDefines = shaderDefs; }
148  ShaderDefines& getShaderDefines() { return _shaderDefines; }
149  const ShaderDefines& getShaderDefines() const { return _shaderDefines; }
150 
151  void setShaderRequirements(const ShaderDefines& shaderDefs) { _shaderRequirements = shaderDefs; }
152  ShaderDefines& getShaderRequirements() { return _shaderRequirements; }
153  const ShaderDefines& getShaderRequirements() const { return _shaderRequirements; }
154 
155 
157  void setShaderBinary(ShaderBinary* shaderBinary) { _shaderBinary = shaderBinary; }
158 
160  ShaderBinary* getShaderBinary() { return _shaderBinary.get(); }
161 
163  const ShaderBinary* getShaderBinary() const { return _shaderBinary.get(); }
164 
165 
168  static Shader* readShaderFile( Type type, const std::string& fileName );
169 
171  bool loadShaderSourceFromFile( const std::string& fileName );
172 
173 
175  typedef std::multimap<float, std::string> CodeInjectionMap;
176 
183  void addCodeInjection(float position, const std::string& code) { _codeInjectionMap.insert(CodeInjectionMap::value_type(position, code)); }
184 
186  CodeInjectionMap& getCodeInjectionMap() { return _codeInjectionMap; }
187 
189  const CodeInjectionMap& getCodeInjectionMap() const { return _codeInjectionMap; }
190 
191 
192 
194  virtual void resizeGLObjectBuffers(unsigned int maxSize);
195 
199  void releaseGLObjects(osg::State* state=0) const;
200 
203  void dirtyShader();
204 
206  void compileShader(osg::State& state) const;
207 
208 
212  static void deleteGlShader(unsigned int contextID, GLuint shader);
213 
216  static void flushDeletedGlShaders(unsigned int contextID,double currentTime, double& availableTime);
217 
221  static void discardDeletedGlShaders(unsigned int contextID);
222 
223  static Shader::Type getTypeId( const std::string& tname );
224 
225  public:
228  {
229  public:
230  PerContextShader(const Shader* shader, unsigned int contextID);
231 
232  void setDefineString(const std::string& defStr) { _defineStr = defStr; }
233  const std::string& getDefineString() const { return _defineStr; }
234 
235  GLuint getHandle() const {return _glShaderHandle;}
236 
237  void requestCompile();
238  void compileShader(osg::State& state);
239  bool needsCompile() const {return _needsCompile;}
240  bool isCompiled() const {return _isCompiled;}
241  bool getInfoLog( std::string& infoLog ) const;
242 
244  void attachShader(GLuint program) const;
245 
247  void detachShader(GLuint program) const;
248 
249  protected: /*methods*/
250  ~PerContextShader();
251 
252  protected: /*data*/
254  const Shader* _shader;
255 
258 
261 
263  std::string _defineStr;
264 
267 
270 
271  const unsigned int _contextID;
272 
273  private:
274  PerContextShader(); // disallowed
275  PerContextShader(const PerContextShader&); // disallowed
276  PerContextShader& operator=(const PerContextShader&); // disallowed
277  };
278 
279 
281  {
282  typedef std::vector< osg::ref_ptr<PerContextShader> > PerContextShaders;
283 
284  ShaderObjects(const Shader* shader, unsigned int contextID);
285 
286  unsigned int _contextID;
287  const Shader* _shader;
288  mutable PerContextShaders _perContextShaders;
289 
290  PerContextShader* getPCS(const std::string& defineStr) const;
291  PerContextShader* createPerContextShader(const std::string& defineStr);
292  void requestCompile();
293  };
294 
295  PerContextShader* getPCS(osg::State& state) const;
296 
297  protected: /*methods*/
298  virtual ~Shader();
299 
300 
301  friend class osg::Program;
302  bool addProgramRef( osg::Program* program );
303  bool removeProgramRef( osg::Program* program );
304 
305  void _computeShaderDefines();
306  void _parseShaderDefines(const std::string& str, ShaderDefines& defines);
307 
308 
309  protected: /*data*/
311  std::string _shaderFileName;
312  std::string _shaderSource;
314 
315  CodeInjectionMap _codeInjectionMap;
316 
317  // ShaderDefines variables
319  ShaderDefines _shaderDefines;
320  ShaderDefines _shaderRequirements;
321 
323  typedef std::set< osg::Program* > ProgramSet;
324  ProgramSet _programSet;
326 
327  private:
328  Shader& operator=(const Shader&); // disallowed
329 };
330 
331 
333 {
334  public:
335 
336  ShaderComponent();
338 
340 
341  unsigned int addShader(osg::Shader* shader);
342  void removeShader(unsigned int i);
343 
344  osg::Shader* getShader(unsigned int i) { return _shaders[i].get(); }
345  const osg::Shader* getShader(unsigned int i) const { return _shaders[i].get(); }
346 
347  unsigned int getNumShaders() const { return static_cast<unsigned int>(_shaders.size()); }
348 
349  virtual void compileGLObjects(State& state) const;
350  virtual void resizeGLObjectBuffers(unsigned int maxSize);
351  virtual void releaseGLObjects(State* state=0) const;
352 
353  protected:
354 
355  typedef std::vector< osg::ref_ptr<osg::Shader> > Shaders;
356  Shaders _shaders;
357 };
358 
359 
360 }
361 
362 #endif
void setShaderBinary(ShaderBinary *shaderBinary)
Definition: Shader.h:157
const std::string & getFileName() const
Definition: Shader.h:127
std::vector< osg::ref_ptr< osg::Shader > > Shaders
Definition: Shader.h:355
#define OSG_EXPORT
Definition: Export.h:43
void setShaderDefinesMode(ShaderDefinesMode sdm)
Definition: Shader.h:143
ShaderDefines _shaderDefines
Definition: Shader.h:319
CodeInjectionMap & getCodeInjectionMap()
Definition: Shader.h:186
#define GL_FRAGMENT_SHADER
Definition: GLDefines.h:90
void setShaderRequirements(const ShaderDefines &shaderDefs)
Definition: Shader.h:151
const ShaderBinary * getShaderBinary() const
Definition: Shader.h:163
void setDefineString(const std::string &defStr)
Definition: Shader.h:232
Type getType() const
Definition: Shader.h:117
#define GL_COMPUTE_SHADER
Definition: GLDefines.h:473
ShaderDefines & getShaderDefines()
Definition: Shader.h:148
const CodeInjectionMap & getCodeInjectionMap() const
Definition: Shader.h:189
ShaderDefines _shaderRequirements
Definition: Shader.h:320
ProgramSet _programSet
Definition: Shader.h:324
std::vector< osg::ref_ptr< PerContextShader > > PerContextShaders
Definition: Shader.h:282
const unsigned char * getData() const
Definition: Shader.h:65
#define GL_VERTEX_SHADER
Definition: GLDefines.h:91
const ShaderDefines & getShaderDefines() const
Definition: Shader.h:149
Type _type
Definition: Shader.h:310
std::set< std::string > ShaderDefines
Definition: Shader.h:34
osg::ref_ptr< ShaderBinary > _shaderBinary
Definition: Shader.h:313
std::vector< unsigned char > Data
Definition: Shader.h:73
unsigned int getNumShaders() const
Definition: Shader.h:347
Shaders _shaders
Definition: Shader.h:356
bool isCompiled() const
Definition: Shader.h:240
osg::buffered_value< osg::ref_ptr< ShaderObjects > > _pcsList
Definition: Shader.h:325
bool needsCompile() const
Definition: Shader.h:239
ShaderDefinesMode
Definition: Shader.h:137
const Shader * _shader
Definition: Shader.h:287
unsigned int getSize() const
Definition: Shader.h:59
std::set< osg::Program * > ProgramSet
Definition: Shader.h:323
#define META_Object(library, name)
Definition: Object.h:42
OSGDB_EXPORT osg::Shader * readShaderFile(const std::string &filename, const Options *options)
const std::string & getDefineString() const
Definition: Shader.h:233
GLuint getHandle() const
Definition: Shader.h:235
ShaderDefines & getShaderRequirements()
Definition: Shader.h:152
void setShaderDefines(const ShaderDefines &shaderDefs)
Definition: Shader.h:147
std::string _shaderFileName
Definition: Shader.h:311
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum GLenum type
Definition: GLU.h:71
CodeInjectionMap _codeInjectionMap
Definition: Shader.h:315
const std::string & getShaderSource() const
Definition: Shader.h:134
osg::Shader * getShader(unsigned int i)
Definition: Shader.h:344
osg::ref_ptr< osg::GLExtensions > _extensions
Definition: Shader.h:257
const osg::Shader * getShader(unsigned int i) const
Definition: Shader.h:345
#define GL_TESS_CONTROL_SHADER
Definition: GLDefines.h:240
const unsigned int _contextID
Definition: Shader.h:271
const Shader * _shader
Definition: Shader.h:254
unsigned int _contextID
Definition: Shader.h:286
Definition: AlphaFunc.h:19
ShaderDefinesMode _shaderDefinesMode
Definition: Shader.h:318
unsigned char * getData()
Definition: Shader.h:62
ShaderBinary * getShaderBinary()
Definition: Shader.h:160
void addCodeInjection(float position, const std::string &code)
Definition: Shader.h:183
std::string _shaderSource
Definition: Shader.h:312
const ShaderDefines & getShaderRequirements() const
Definition: Shader.h:153
#define GL_TESS_EVALUATION_SHADER
Definition: GLDefines.h:239
ShaderDefinesMode getShaderDefinesMode() const
Definition: Shader.h:144
std::multimap< float, std::string > CodeInjectionMap
Definition: Shader.h:175
void setFileName(const std::string &fileName)
Definition: Shader.h:124
PerContextShaders _perContextShaders
Definition: Shader.h:288
#define GL_GEOMETRY_SHADER
Definition: GLDefines.h:196