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) VTK_OVERRIDE;
50 
52 
55  vtkGetObjectMacro(VertexShader, vtkShader);
56  void SetVertexShader(vtkShader*);
58 
60 
63  vtkGetObjectMacro(FragmentShader, vtkShader);
64  void SetFragmentShader(vtkShader*);
66 
68 
71  vtkGetObjectMacro(GeometryShader, vtkShader);
72  void SetGeometryShader(vtkShader*);
74 
76 
79  vtkGetObjectMacro(TransformFeedback, vtkTransformFeedback);
80  void SetTransformFeedback(vtkTransformFeedback *tfc);
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 
124  void ReleaseGraphicsResources(vtkWindow *win);
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 
249 
266  vtkSetStringMacro(FileNamePrefixForDebugging);
267  vtkGetStringMacro(FileNamePrefixForDebugging);
269 
270 protected:
272  ~vtkShaderProgram() VTK_OVERRIDE;
273 
274  /***************************************************************
275  * The following functions are only for use by the shader cache
276  * which is why they are protected and that class is a friend
277  * you need to use the shader cache to compile/link/bind your shader
278  * do not try to do it yourself as it will screw up the cache
279  ***************************************************************/
280  friend class vtkOpenGLShaderCache;
281 
288  bool AttachShader(const vtkShader *shader);
289 
295  bool DetachShader(const vtkShader *shader);
296 
300  virtual int CompileShader();
301 
307  bool Link();
308 
313  bool Bind();
314 
316  void Release();
317 
318  /************* end **************************************/
319 
324 
325  // hash of the shader program
327 
328  bool SetAttributeArrayInternal(const char *name, void *buffer,
329  int type, int tupleSize,
330  NormalizeOption normalize);
331  int Handle;
335 
336  bool Linked;
337  bool Bound;
338  bool Compiled;
339 
340  // for glsl 1.5 or later, how many outputs
341  // does this shader create
342  // they will be bound in order to
343  // fragOutput0 fragOutput1 etc...
344  unsigned int NumberOfOutputs;
345 
347 
348  // since we are using const char * arrays we have to
349  // free our memory :-)
350  void ClearMaps();
351  std::map<const char *, int, cmp_str> AttributeLocs;
352  std::map<const char *, int, cmp_str> UniformLocs;
353 
354  friend class VertexArrayObject;
355 
356 private:
357  int FindAttributeArray(const char *name);
358  int FindUniform(const char *name);
359 
360  vtkShaderProgram(const vtkShaderProgram&) VTK_DELETE_FUNCTION;
361  void operator=(const vtkShaderProgram&) VTK_DELETE_FUNCTION;
362 
363  char* FileNamePrefixForDebugging;
364 };
365 
366 
367 #endif
abstract base class for most VTK objects
Definition: vtkObject.h:53
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:35
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkTransformFeedback * TransformFeedback
bool isBound() const
Check if the program is currently bound, or not.
unsigned int NumberOfOutputs
manage Shader Programs within a context
vtkShader * VertexShader
The values range across the limits of the numeric type.
std::map< const char *, int, cmp_str > UniformLocs
int GetHandle() const
Get the handle of the shader program.
std::string replace(std::string source, const std::string &search, const std::string &replace, bool all)
void SetMD5Hash(const std::string &hash)
window superclass for vtkRenderWindow
Definition: vtkWindow.h:34
a simple class to control print indentation
Definition: vtkIndent.h:33
vtkShader * GeometryShader
bool operator()(const char *a, const char *b) const
Manages a TransformFeedback buffer.
std::string GetMD5Hash() const
Set/Get the md5 hash of this program.
vtkShader * FragmentShader
vtkGetStringMacro(ExtensionsString)
Returns a string listing all available extensions.
std::string GetError() const
Get the error message (empty if none) for the shader program.
vtkSetMacro(IgnoreDriverBugs, bool)
When set known driver bugs are ignored during driver feature detection.
NormalizeOption
Options for attribute normalization.
boost::graph_traits< vtkGraph *>::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
Vertex or Fragment shader, combined into a ShaderProgram.
Definition: vtkShader.h:37
std::map< const char *, int, cmp_str > AttributeLocs
vtkBooleanMacro(IgnoreDriverBugs, bool)
When set known driver bugs are ignored during driver feature detection.
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
represent and manipulate 3x3 transformation matrices
Definition: vtkMatrix3x3.h:33
The ShaderProgram uses one or more Shader objects.