OpenSceneGraph 3.6.5
Shader
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
32namespace osg {
33
34class Program;
35
36// set of shader define strings that the shader is dependent upon.
37typedef std::set<std::string> ShaderDefines;
38
42{
43 public:
44
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;
75};
76
77
79
87
89{
90 public:
91
101
103 Shader(Type type, const std::string& source );
104 Shader(Type type, ShaderBinary* shaderBinary );
105
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
142
145
146
147 void setShaderDefines(const ShaderDefines& shaderDefs) { _shaderDefines = shaderDefs; }
150
151 void setShaderRequirements(const ShaderDefines& shaderDefs) { _shaderRequirements = shaderDefs; }
154
155
157 void setShaderBinary(ShaderBinary* shaderBinary) { _shaderBinary = shaderBinary; }
158
161
163 const ShaderBinary* getShaderBinary() const { return _shaderBinary.get(); }
164
165#ifdef OSG_USE_DEPRECATED_API
167 static Shader* readShaderFile( Type type, const std::string& fileName );
168
170 bool loadShaderSourceFromFile( const std::string& fileName );
171#endif
172
174 typedef std::multimap<float, std::string> CodeInjectionMap;
175
182 void addCodeInjection(float position, const std::string& code) { _codeInjectionMap.insert(CodeInjectionMap::value_type(position, code)); }
183
186
189
190
191
193 virtual void resizeGLObjectBuffers(unsigned int maxSize);
194
198 void releaseGLObjects(osg::State* state=0) const;
199
203
205 void compileShader(osg::State& state) const;
206
207 static Shader::Type getTypeId( const std::string& tname );
208
209 public:
212 {
213 public:
214 PerContextShader(const Shader* shader, unsigned int contextID);
215
216 void setDefineString(const std::string& defStr) { _defineStr = defStr; }
217 const std::string& getDefineString() const { return _defineStr; }
218
219 GLuint getHandle() const {return _glShaderHandle;}
220
223 bool needsCompile() const {return _needsCompile;}
224 bool isCompiled() const {return _isCompiled;}
225 bool getInfoLog( std::string& infoLog ) const;
226
228 void attachShader(GLuint program) const;
229
231 void detachShader(GLuint program) const;
232
233 protected: /*methods*/
235
236 protected: /*data*/
239
242
245
247 std::string _defineStr;
248
251
254
255 const unsigned int _contextID;
256
257 private:
258 PerContextShader(); // disallowed
259 PerContextShader(const PerContextShader&); // disallowed
260 PerContextShader& operator=(const PerContextShader&); // disallowed
261 };
262
263
265 {
266 typedef std::vector< osg::ref_ptr<PerContextShader> > PerContextShaders;
267
268 ShaderObjects(const Shader* shader, unsigned int contextID);
269
270 unsigned int _contextID;
273
274 PerContextShader* getPCS(const std::string& defineStr) const;
275 PerContextShader* createPerContextShader(const std::string& defineStr);
277 };
278
280
281 protected: /*methods*/
282 virtual ~Shader();
283
284
285 friend class osg::Program;
286 bool addProgramRef( osg::Program* program );
288
290 void _parseShaderDefines(const std::string& str, ShaderDefines& defines);
291
292
293 protected: /*data*/
295 std::string _shaderFileName;
296 std::string _shaderSource;
298
300
301 // ShaderDefines variables
305
307 typedef std::set< osg::Program* > ProgramSet;
309 OpenThreads::Mutex _programSetMutex;
311
312 private:
313 Shader& operator=(const Shader&); // disallowed
314};
315
316
318{
319 public:
320
323
325
326 unsigned int addShader(osg::Shader* shader);
327 void removeShader(unsigned int i);
328
329 osg::Shader* getShader(unsigned int i) { return _shaders[i].get(); }
330 const osg::Shader* getShader(unsigned int i) const { return _shaders[i].get(); }
331
332 unsigned int getNumShaders() const { return static_cast<unsigned int>(_shaders.size()); }
333
334 virtual void compileGLObjects(State& state) const;
335 virtual void resizeGLObjectBuffers(unsigned int maxSize);
336 virtual void releaseGLObjects(State* state=0) const;
337
338 protected:
339
340 typedef std::vector< osg::ref_ptr<osg::Shader> > Shaders;
342};
343
344
345}
346
347#endif
#define GL_FRAGMENT_SHADER
Definition GLDefines:92
#define GL_GEOMETRY_SHADER
Definition GLDefines:198
#define GL_TESS_CONTROL_SHADER
Definition GLDefines:242
#define GL_COMPUTE_SHADER
Definition GLDefines:477
#define GL_TESS_EVALUATION_SHADER
Definition GLDefines:241
#define GL_VERTEX_SHADER
Definition GLDefines:93
The core osg library provides the basic scene graph classes such as Nodes, State and Drawables,...
Definition AlphaFunc:19
std::set< std::string > ShaderDefines
Definition Shader:37
Implements a simple buffered value for values that need to be buffered on a per graphics context basi...
Definition buffered_value:27
Copy Op(erator) used to control whether shallow or deep copy is used during copy construction and clo...
Definition CopyOp:41
@ SHALLOW_COPY
Definition CopyOp:47
Base class/standard interface for objects which require IO support, cloning and reference counting.
Definition Object:61
osg::Program is an application-level abstraction of an OpenGL glProgram.
Definition Program:52
Smart pointer for handling referenced counted objects.
Definition ref_ptr:32
Base class for providing reference counted objects.
Definition Referenced:44
Simple class for wrapping up the data used in OpenGL ES 2's glShaderBinary calls.
Definition Shader:42
Data _data
Definition Shader:74
unsigned char * getData()
Get a ptr to the shader binary data.
Definition Shader:62
ShaderBinary(const ShaderBinary &rhs, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
Copy constructor using CopyOp to manage deep vs shallow copy.
META_Object(osg, ShaderBinary)
void allocate(unsigned int size)
Allocated a data buffer of specified size.
const unsigned char * getData() const
Get a const ptr to the shader binary data.
Definition Shader:65
static ShaderBinary * readShaderBinaryFile(const std::string &fileName)
Read shader binary from file.
unsigned int getSize() const
Get the size of the shader binary data.
Definition Shader:59
std::vector< unsigned char > Data
Definition Shader:73
void assign(unsigned int size, const unsigned char *data)
Assign shader binary data, copying the specified data into locally stored data buffer,...
osg::Shader is an application-level abstraction of an OpenGL glShader.
Definition Shader:89
ShaderDefines _shaderRequirements
Definition Shader:304
const std::string & getFileName() const
Get filename to which the shader source code belongs.
Definition Shader:127
Type _type
Definition Shader:294
Shader(const Shader &rhs, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
Copy constructor using CopyOp to manage deep vs shallow copy.
std::string _shaderFileName
Definition Shader:295
ShaderBinary * getShaderBinary()
Get the Shader's ShaderBinary, return NULL if none is assigned.
Definition Shader:160
Shader(Type type, ShaderBinary *shaderBinary)
const ShaderDefines & getShaderRequirements() const
Definition Shader:153
Shader(Type type, const std::string &source)
void _parseShaderDefines(const std::string &str, ShaderDefines &defines)
osg::buffered_value< osg::ref_ptr< ShaderObjects > > _pcsList
Definition Shader:310
bool setType(Type t)
Set the Shader type as an enum.
ShaderDefinesMode
Definition Shader:138
@ USE_SHADER_PRAGMA
Definition Shader:139
@ USE_MANUAL_SETTINGS
Definition Shader:140
META_Object(osg, Shader)
ShaderDefines _shaderDefines
Definition Shader:303
const ShaderBinary * getShaderBinary() const
Get the const Shader's ShaderBinary, return NULL if none is assigned.
Definition Shader:163
void releaseGLObjects(osg::State *state=0) const
release OpenGL objects in specified graphics context if State object is passed, otherwise release Ope...
ShaderDefines & getShaderDefines()
Definition Shader:148
virtual void resizeGLObjectBuffers(unsigned int maxSize)
Resize any per context GLObject buffers to specified size.
osg::ref_ptr< ShaderBinary > _shaderBinary
Definition Shader:297
std::string _shaderSource
Definition Shader:296
void _computeShaderDefines()
virtual ~Shader()
std::set< osg::Program * > ProgramSet
osg::Programs that this osg::Shader is attached to
Definition Shader:307
Type getType() const
Get the Shader type as an enum.
Definition Shader:117
ShaderDefinesMode getShaderDefinesMode() const
Definition Shader:144
void setShaderBinary(ShaderBinary *shaderBinary)
Set the Shader using a ShaderBinary.
Definition Shader:157
void setFileName(const std::string &fileName)
Set file name for the shader source code.
Definition Shader:124
const ShaderDefines & getShaderDefines() const
Definition Shader:149
ProgramSet _programSet
Definition Shader:308
void setShaderSource(const std::string &sourceText)
Set the Shader's source code text from a string.
bool addProgramRef(osg::Program *program)
void setShaderDefines(const ShaderDefines &shaderDefs)
Definition Shader:147
ShaderDefines & getShaderRequirements()
Definition Shader:152
PerContextShader * getPCS(osg::State &state) const
const char * getTypename() const
Get the Shader type as a descriptive string.
Type
Definition Shader:92
@ FRAGMENT
Definition Shader:97
@ GEOMETRY
Definition Shader:96
@ UNDEFINED
Definition Shader:99
@ TESSCONTROL
Definition Shader:94
@ COMPUTE
Definition Shader:98
@ TESSEVALUATION
Definition Shader:95
@ VERTEX
Definition Shader:93
void setShaderDefinesMode(ShaderDefinesMode sdm)
Definition Shader:143
OpenThreads::Mutex _programSetMutex
Definition Shader:309
bool removeProgramRef(osg::Program *program)
int compare(const Shader &rhs) const
Shader(Type type=UNDEFINED)
const std::string & getShaderSource() const
Query the shader's source code text.
Definition Shader:134
CodeInjectionMap & getCodeInjectionMap()
Get the code injection map.
Definition Shader:185
void setShaderRequirements(const ShaderDefines &shaderDefs)
Definition Shader:151
void addCodeInjection(float position, const std::string &code)
Add code injection that will be placed in the main shader to enable support for this shader.
Definition Shader:182
void compileShader(osg::State &state) const
If needed, compile the PCS's glShader.
void dirtyShader()
Mark our PCSs as needing recompilation.
const CodeInjectionMap & getCodeInjectionMap() const
Get the const code injection map.
Definition Shader:188
CodeInjectionMap _codeInjectionMap
Definition Shader:299
static Shader::Type getTypeId(const std::string &tname)
std::multimap< float, std::string > CodeInjectionMap
The code injection map used when generating the main shader during main shader composition.
Definition Shader:174
ShaderDefinesMode _shaderDefinesMode
Definition Shader:302
PerContextShader (PCS) is an OSG-internal encapsulation of glShader per-GL context.
Definition Shader:212
void attachShader(GLuint program) const
Attach our glShader to a glProgram.
bool needsCompile() const
Definition Shader:223
std::string _defineStr
Define string passed on to Shaders to help configure them.
Definition Shader:247
bool _needsCompile
Does our glShader need to be recompiled?
Definition Shader:250
const Shader * _shader
Pointer to our parent osg::Shader.
Definition Shader:238
bool getInfoLog(std::string &infoLog) const
void setDefineString(const std::string &defStr)
Definition Shader:216
GLuint _glShaderHandle
Handle to the actual glShader.
Definition Shader:244
bool isCompiled() const
Definition Shader:224
void compileShader(osg::State &state)
const std::string & getDefineString() const
Definition Shader:217
const unsigned int _contextID
Definition Shader:255
PerContextShader(const Shader *shader, unsigned int contextID)
GLuint getHandle() const
Definition Shader:219
bool _isCompiled
Is our glShader successfully compiled?
Definition Shader:253
osg::ref_ptr< osg::GLExtensions > _extensions
Pointer to this context's extension functions.
Definition Shader:241
void detachShader(GLuint program) const
Detach our glShader from a glProgram.
ShaderObjects(const Shader *shader, unsigned int contextID)
unsigned int _contextID
Definition Shader:270
PerContextShader * createPerContextShader(const std::string &defineStr)
PerContextShader * getPCS(const std::string &defineStr) const
PerContextShaders _perContextShaders
Definition Shader:272
std::vector< osg::ref_ptr< PerContextShader > > PerContextShaders
Definition Shader:266
const Shader * _shader
Definition Shader:271
unsigned int getNumShaders() const
Definition Shader:332
std::vector< osg::ref_ptr< osg::Shader > > Shaders
Definition Shader:340
virtual void releaseGLObjects(State *state=0) const
If State is non-zero, this function releases any associated OpenGL objects for the specified graphics...
virtual void compileGLObjects(State &state) const
const osg::Shader * getShader(unsigned int i) const
Definition Shader:330
ShaderComponent(const ShaderComponent &sc, const CopyOp &copyop=CopyOp::SHALLOW_COPY)
META_Object(osg, ShaderComponent)
Shaders _shaders
Definition Shader:341
osg::Shader * getShader(unsigned int i)
Definition Shader:329
void removeShader(unsigned int i)
virtual void resizeGLObjectBuffers(unsigned int maxSize)
Resize any per context GLObject buffers to specified size.
unsigned int addShader(osg::Shader *shader)
Encapsulates the current applied OpenGL modes, attributes and vertex arrays settings,...
Definition State:80
#define OSG_EXPORT
Definition Export:39

osg logo
Generated at Sun Jul 20 2025 00:00:00 for the OpenSceneGraph by doxygen 1.14.0.