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);
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 
248 protected:
250  ~vtkShaderProgram();
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
abstract base class for most VTK objects
Definition: vtkObject.h:59
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:41
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:39
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
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:40
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:36
The ShaderProgram uses one or more Shader objects.