VTK
vtkShaderProgram.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4
5 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
6 All rights reserved.
7 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
8
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notice for more information.
12
13=========================================================================*/
21#ifndef vtkShaderProgram_h
22#define vtkShaderProgram_h
23
24#include "vtkRenderingOpenGL2Module.h" // for export macro
25#include "vtkObject.h"
26
27#include <string> // For member variables.
28#include <map> // For member variables.
29
30class vtkMatrix3x3;
31class vtkMatrix4x4;
33class vtkShader;
34class VertexArrayObject;
35class vtkWindow;
36
44class VTKRENDERINGOPENGL2_EXPORT vtkShaderProgram : public vtkObject
45{
46public:
49 void PrintSelf(ostream& os, vtkIndent indent);
50
52
55 vtkGetObjectMacro(VertexShader, vtkShader);
58
60
63 vtkGetObjectMacro(FragmentShader, vtkShader);
66
68
71 vtkGetObjectMacro(GeometryShader, vtkShader);
74
76
79 vtkGetObjectMacro(TransformFeedback, vtkTransformFeedback);
82
84
87 vtkGetMacro(Compiled, bool);
88 vtkSetMacro(Compiled, bool);
89 vtkBooleanMacro(Compiled, bool);
91
95 std::string GetMD5Hash() const { return this->MD5Hash; }
96 void SetMD5Hash(const std::string &hash) { this->MD5Hash = hash; }
97
98
111 NoNormalize
112 };
113
114
119 bool isBound() const { return this->Bound; }
120
125
127 int GetHandle() const { return Handle; }
128
130 std::string GetError() const { return Error; }
131
136 bool EnableAttributeArray(const char *name);
137
142 bool DisableAttributeArray(const char *name);
143
159 bool UseAttributeArray(const char *name, int offset, size_t stride,
160 int elementType, int elementTupleSize,
161 NormalizeOption normalize);
162
180 template <class T>
181 bool SetAttributeArray(const char *name, const T &array,
182 int tupleSize, NormalizeOption normalize);
183
185 bool SetUniformi(const char *name, int v);
186 bool SetUniformf(const char *name, float v);
187 bool SetUniform2i(const char *name, const int v[2]);
188 bool SetUniform2f(const char *name, const float v[2]);
189 bool SetUniform3f(const char *name, const float v[3]);
190 bool SetUniform4f(const char *name, const float v[4]);
191 bool SetUniform3uc(const char *name, const unsigned char v[3]); // maybe remove
192 bool SetUniform4uc(const char *name, const unsigned char v[4]); // maybe remove
193 bool SetUniformMatrix(const char *name, vtkMatrix3x3 *v);
194 bool SetUniformMatrix(const char *name, vtkMatrix4x4 *v);
195 bool SetUniformMatrix3x3(const char *name, float *v);
196 bool SetUniformMatrix4x4(const char *name, float *v);
197
199 bool SetUniform1iv(const char *name, const int count, const int *f);
200 bool SetUniform1fv(const char *name, const int count, const float *f);
201 bool SetUniform2fv(const char *name, const int count, const float (*f)[2]);
202 bool SetUniform3fv(const char *name, const int count, const float (*f)[3]);
203 bool SetUniform4fv(const char *name, const int count, const float (*f)[4]);
204 bool SetUniformMatrix4x4v(const char *name, const int count, float *v);
205
206 // How many outputs does this program produce
207 // only valid for OpenGL 3.2 or later
208 vtkSetMacro(NumberOfOutputs,unsigned int);
209
215 static bool Substitute(
217 const std::string &search,
218 const std::string &replace,
219 bool all = true);
220
226 bool IsUniformUsed(const char *);
227
232 bool IsAttributeUsed(const char *name);
233
234 // maps of std::string are super slow when calling find
235 // with a string literal or const char * as find
236 // forces construction/copy/destruction of a
237 // std::sting copy of the const char *
238 // In spite of the doubters this can really be a
239 // huge CPU hog.
240 struct cmp_str
241 {
242 bool operator()(const char *a, const char *b) const
243 {
244 return strcmp(a, b) < 0;
245 }
246 };
247
248protected:
251
252 /***************************************************************
253 * The following functions are only for use by the shader cache
254 * which is why they are protected and that class is a friend
255 * you need to use the shader cache to compile/link/bind your shader
256 * do not try to do it yourself as it will screw up the cache
257 ***************************************************************/
259
266 bool AttachShader(const vtkShader *shader);
267
273 bool DetachShader(const vtkShader *shader);
274
278 virtual int CompileShader();
279
285 bool Link();
286
291 bool Bind();
292
294 void Release();
295
296 /************* end **************************************/
297
302
303 // hash of the shader program
305
306 bool SetAttributeArrayInternal(const char *name, void *buffer,
307 int type, int tupleSize,
308 NormalizeOption normalize);
313
314 bool Linked;
315 bool Bound;
317
318 // for glsl 1.5 or later, how many outputs
319 // does this shader create
320 // they will be bound in order to
321 // fragOutput0 fragOutput1 etc...
322 unsigned int NumberOfOutputs;
323
325
326 // since we are using const char * arrays we have to
327 // free our memory :-)
328 void ClearMaps();
329 std::map<const char *, int, cmp_str> AttributeLocs;
330 std::map<const char *, int, cmp_str> UniformLocs;
331
332 friend class VertexArrayObject;
333
334private:
335 int FindAttributeArray(const char *name);
336 int FindUniform(const char *name);
337
338 vtkShaderProgram(const vtkShaderProgram&) VTK_DELETE_FUNCTION;
339 void operator=(const vtkShaderProgram&) VTK_DELETE_FUNCTION;
340
341};
342
343
344#endif
a simple class to control print indentation
Definition: vtkIndent.h:40
represent and manipulate 3x3 transformation matrices
Definition: vtkMatrix3x3.h:37
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:42
abstract base class for most VTK objects
Definition: vtkObject.h:60
manage Shader Programs within a context
The ShaderProgram uses one or more Shader objects.
bool Link()
Attempt to link the shader program.
void Release()
Releases the shader program from the current context.
bool SetUniform4fv(const char *name, const int count, const float(*f)[4])
vtkShader * FragmentShader
std::map< const char *, int, cmp_str > AttributeLocs
void SetFragmentShader(vtkShader *)
std::string GetError() const
Get the error message (empty if none) for the shader program.
bool IsUniformUsed(const char *)
methods to inquire as to what uniforms/attributes are used by this shader.
bool Bind()
Bind the program in order to use it.
bool SetAttributeArray(const char *name, const T &array, int tupleSize, NormalizeOption normalize)
Upload the supplied array of tightly packed values to the named attribute.
void SetVertexShader(vtkShader *)
bool SetUniform2i(const char *name, const int v[2])
vtkTransformFeedback * TransformFeedback
bool SetUniform3f(const char *name, const float v[3])
std::string GetMD5Hash() const
Set/Get the md5 hash of this program.
bool SetUniform4uc(const char *name, const unsigned char v[4])
bool SetUniform4f(const char *name, const float v[4])
bool SetUniform2f(const char *name, const float v[2])
bool SetUniformMatrix4x4v(const char *name, const int count, float *v)
bool SetUniformMatrix(const char *name, vtkMatrix4x4 *v)
bool SetUniform1iv(const char *name, const int count, const int *f)
Set the name uniform array to f with count elements.
bool AttachShader(const vtkShader *shader)
Attach the supplied shader to this program.
bool SetAttributeArrayInternal(const char *name, void *buffer, int type, int tupleSize, NormalizeOption normalize)
static vtkShaderProgram * New()
int GetHandle() const
Get the handle of the shader program.
NormalizeOption
Options for attribute normalization.
@ Normalize
The values range across the limits of the numeric type.
void SetMD5Hash(const std::string &hash)
bool EnableAttributeArray(const char *name)
Enable the named attribute array.
vtkShader * VertexShader
bool isBound() const
Check if the program is currently bound, or not.
bool SetUniformMatrix(const char *name, vtkMatrix3x3 *v)
bool DetachShader(const vtkShader *shader)
Detach the supplied shader from this program.
bool SetUniformf(const char *name, float v)
bool SetUniformMatrix4x4(const char *name, float *v)
bool SetUniform2fv(const char *name, const int count, const float(*f)[2])
vtkShader * GeometryShader
bool SetUniformMatrix3x3(const char *name, float *v)
bool DisableAttributeArray(const char *name)
Disable the named attribute array.
bool SetUniform3uc(const char *name, const unsigned char v[3])
bool IsAttributeUsed(const char *name)
Return true if the compiled and linked shader has an attribute matching name.
bool UseAttributeArray(const char *name, int offset, size_t stride, int elementType, int elementTupleSize, NormalizeOption normalize)
Use the named attribute array with the bound BufferObject.
void PrintSelf(ostream &os, vtkIndent indent)
Methods invoked by print to print information about the object including superclasses.
bool SetUniformi(const char *name, int v)
Set the name uniform value to int v.
unsigned int NumberOfOutputs
bool SetUniform1fv(const char *name, const int count, const float *f)
void SetGeometryShader(vtkShader *)
std::map< const char *, int, cmp_str > UniformLocs
bool SetUniform3fv(const char *name, const int count, const float(*f)[3])
void ReleaseGraphicsResources(vtkWindow *win)
release any graphics resources this class is using.
static bool Substitute(std::string &source, const std::string &search, const std::string &replace, bool all=true)
perform in place string substitutions, indicate if a substitution was done this is useful for buildin...
virtual int CompileShader()
Compile this shader program and attached shaders.
void SetTransformFeedback(vtkTransformFeedback *tfc)
Vertex or Fragment shader, combined into a ShaderProgram.
Definition: vtkShader.h:41
Manages a TransformFeedback buffer.
window superclass for vtkRenderWindow
Definition: vtkWindow.h:35
@ type
Definition: vtkX3D.h:516
@ name
Definition: vtkX3D.h:219
@ offset
Definition: vtkX3D.h:438
@ string
Definition: vtkX3D.h:490
std::string replace(std::string source, const std::string &search, const std::string &replace, bool all)
bool operator()(const char *a, const char *b) const
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
vtkSetMacro(IgnoreDriverBugs, bool)
Updates the extensions string.
vtkBooleanMacro(IgnoreDriverBugs, bool)
Updates the extensions string.