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 
30 class vtkMatrix3x3;
31 class vtkMatrix4x4;
33 class vtkShader;
34 class VertexArrayObject;
35 class vtkWindow;
36 
44 class VTKRENDERINGOPENGL2_EXPORT vtkShaderProgram : public vtkObject
45 {
46 public:
47  static vtkShaderProgram *New();
48  vtkTypeMacro(vtkShaderProgram, vtkObject);
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 
248 protected:
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  ***************************************************************/
258  friend class vtkOpenGLShaderCache;
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);
309  int Handle;
313 
314  bool Linked;
315  bool Bound;
316  bool Compiled;
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 
334 private:
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
vtkShaderProgram::SetUniform2fv
bool SetUniform2fv(const char *name, const int count, const float(*f)[2])
vtkShaderProgram::GeometryShaderHandle
int GeometryShaderHandle
Definition: vtkShaderProgram.h:312
vtkShaderProgram::CompileShader
virtual int CompileShader()
Compile this shader program and attached shaders.
vtkShaderProgram::Compiled
bool Compiled
Definition: vtkShaderProgram.h:316
vtkShaderProgram::Release
void Release()
Releases the shader program from the current context.
vtkShaderProgram::GetMD5Hash
std::string GetMD5Hash() const
Set/Get the md5 hash of this program.
Definition: vtkShaderProgram.h:95
vtkShaderProgram::FragmentShaderHandle
int FragmentShaderHandle
Definition: vtkShaderProgram.h:311
vtkShaderProgram::SetUniformMatrix4x4
bool SetUniformMatrix4x4(const char *name, float *v)
vtkX3D::type
@ type
Definition: vtkX3D.h:516
vtkShaderProgram::NormalizeOption
NormalizeOption
Options for attribute normalization.
Definition: vtkShaderProgram.h:100
vtkShaderProgram::Link
bool Link()
Attempt to link the shader program.
vtkShaderProgram::SetUniform4f
bool SetUniform4f(const char *name, const float v[4])
vtkShaderProgram::Bound
bool Bound
Definition: vtkShaderProgram.h:315
vtkShaderProgram::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent)
Methods invoked by print to print information about the object including superclasses.
vtkShaderProgram::SetAttributeArray
bool SetAttributeArray(const char *name, const T &array, int tupleSize, NormalizeOption normalize)
Upload the supplied array of tightly packed values to the named attribute.
vtkShaderProgram::FragmentShader
vtkShader * FragmentShader
Definition: vtkShaderProgram.h:299
vtkShaderProgram::cmp_str
Definition: vtkShaderProgram.h:241
vtkShaderProgram::GetError
std::string GetError() const
Get the error message (empty if none) for the shader program.
Definition: vtkShaderProgram.h:130
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:60
vtkShaderProgram::SetTransformFeedback
void SetTransformFeedback(vtkTransformFeedback *tfc)
vtkShaderProgram::vtkShaderProgram
vtkShaderProgram()
vtkShaderProgram::SetUniformf
bool SetUniformf(const char *name, float v)
vtkShaderProgram::New
static vtkShaderProgram * New()
vtkShaderProgram::UseAttributeArray
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.
vtkShaderProgram::AttributeLocs
std::map< const char *, int, cmp_str > AttributeLocs
Definition: vtkShaderProgram.h:329
vtkMatrix3x3
represent and manipulate 3x3 transformation matrices
Definition: vtkMatrix3x3.h:37
vtkShaderProgram::Handle
int Handle
Definition: vtkShaderProgram.h:309
vtkShaderProgram::IsAttributeUsed
bool IsAttributeUsed(const char *name)
Return true if the compiled and linked shader has an attribute matching name.
vtkShaderProgram::Bind
bool Bind()
Bind the program in order to use it.
vtkShaderProgram::DisableAttributeArray
bool DisableAttributeArray(const char *name)
Disable the named attribute array.
vtkWindow
window superclass for vtkRenderWindow
Definition: vtkWindow.h:35
vtkShaderProgram::SetAttributeArrayInternal
bool SetAttributeArrayInternal(const char *name, void *buffer, int type, int tupleSize, NormalizeOption normalize)
vtkShaderProgram::Linked
bool Linked
Definition: vtkShaderProgram.h:314
source
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
Definition: vtkBoostGraphAdapter.h:822
vtkShaderProgram::ClearMaps
void ClearMaps()
vtkShaderProgram::SetUniform1fv
bool SetUniform1fv(const char *name, const int count, const float *f)
vtkShaderProgram::DetachShader
bool DetachShader(const vtkShader *shader)
Detach the supplied shader from this program.
vtkShaderProgram::SetGeometryShader
void SetGeometryShader(vtkShader *)
vtkShaderProgram::SetUniform2i
bool SetUniform2i(const char *name, const int v[2])
vtkShaderProgram::SetUniformMatrix
bool SetUniformMatrix(const char *name, vtkMatrix4x4 *v)
vtkX3D::offset
@ offset
Definition: vtkX3D.h:438
vtkShaderProgram
The ShaderProgram uses one or more Shader objects.
Definition: vtkShaderProgram.h:45
vtkShaderProgram::SetVertexShader
void SetVertexShader(vtkShader *)
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:40
vtkMatrix4x4
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:42
vtkShaderProgram::Normalize
@ Normalize
The values range across the limits of the numeric type.
Definition: vtkShaderProgram.h:109
vtkShaderProgram::SetUniformi
bool SetUniformi(const char *name, int v)
Set the name uniform value to int v.
vtkShaderProgram::TransformFeedback
vtkTransformFeedback * TransformFeedback
Definition: vtkShaderProgram.h:301
vtkShaderProgram::isBound
bool isBound() const
Check if the program is currently bound, or not.
Definition: vtkShaderProgram.h:119
vtkShader
Vertex or Fragment shader, combined into a ShaderProgram.
Definition: vtkShader.h:41
vtkShaderProgram::SetUniform4fv
bool SetUniform4fv(const char *name, const int count, const float(*f)[4])
vtkShaderProgram::SetUniform3uc
bool SetUniform3uc(const char *name, const unsigned char v[3])
vtkShaderProgram::IsUniformUsed
bool IsUniformUsed(const char *)
methods to inquire as to what uniforms/attributes are used by this shader.
vtkShaderProgram::Substitute
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...
vtkOpenGLShaderCache
manage Shader Programs within a context
Definition: vtkOpenGLShaderCache.h:35
vtkShaderProgram::SetUniformMatrix
bool SetUniformMatrix(const char *name, vtkMatrix3x3 *v)
vtkShaderProgram::GetHandle
int GetHandle() const
Get the handle of the shader program.
Definition: vtkShaderProgram.h:127
vtkX3D::name
@ name
Definition: vtkX3D.h:219
vtkObject.h
vtkShaderProgram::cmp_str::operator()
bool operator()(const char *a, const char *b) const
Definition: vtkShaderProgram.h:242
vtkBooleanMacro
vtkBooleanMacro(IgnoreDriverBugs, bool)
Updates the extensions string.
vtkSetMacro
vtkSetMacro(IgnoreDriverBugs, bool)
Updates the extensions string.
vtkShaderProgram::NumberOfOutputs
unsigned int NumberOfOutputs
Definition: vtkShaderProgram.h:322
vtkX3D::string
@ string
Definition: vtkX3D.h:490
vtkShaderProgram::Error
std::string Error
Definition: vtkShaderProgram.h:324
vtkShaderProgram::SetUniformMatrix4x4v
bool SetUniformMatrix4x4v(const char *name, const int count, float *v)
vtkShaderProgram::SetFragmentShader
void SetFragmentShader(vtkShader *)
vtkShaderProgram::EnableAttributeArray
bool EnableAttributeArray(const char *name)
Enable the named attribute array.
vtkShaderProgram::SetMD5Hash
void SetMD5Hash(const std::string &hash)
Definition: vtkShaderProgram.h:96
vtkShaderProgram::SetUniform2f
bool SetUniform2f(const char *name, const float v[2])
vtkShaderProgram::MD5Hash
std::string MD5Hash
Definition: vtkShaderProgram.h:304
vtkShaderProgram::SetUniform3fv
bool SetUniform3fv(const char *name, const int count, const float(*f)[3])
vtkShaderProgram::UniformLocs
std::map< const char *, int, cmp_str > UniformLocs
Definition: vtkShaderProgram.h:330
vtkShaderProgram::SetUniformMatrix3x3
bool SetUniformMatrix3x3(const char *name, float *v)
vtkShaderProgram::VertexShader
vtkShader * VertexShader
Definition: vtkShaderProgram.h:298
vtkvolume::replace
std::string replace(std::string source, const std::string &search, const std::string &replace, bool all)
Definition: vtkVolumeShaderComposer.h:41
vtkShaderProgram::GeometryShader
vtkShader * GeometryShader
Definition: vtkShaderProgram.h:300
vtkShaderProgram::AttachShader
bool AttachShader(const vtkShader *shader)
Attach the supplied shader to this program.
vtkShaderProgram::SetUniform4uc
bool SetUniform4uc(const char *name, const unsigned char v[4])
vtkShaderProgram::~vtkShaderProgram
~vtkShaderProgram()
vtkShaderProgram::SetUniform3f
bool SetUniform3f(const char *name, const float v[3])
vtkShaderProgram::ReleaseGraphicsResources
void ReleaseGraphicsResources(vtkWindow *win)
release any graphics resources this class is using.
vtkShaderProgram::VertexShaderHandle
int VertexShaderHandle
Definition: vtkShaderProgram.h:310
vtkShaderProgram::SetUniform1iv
bool SetUniform1iv(const char *name, const int count, const int *f)
Set the name uniform array to f with count elements.
vtkTransformFeedback
Manages a TransformFeedback buffer.
Definition: vtkTransformFeedback.h:40