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) 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 SetUniform3f(const char *name, const double v[3]);
191  bool SetUniform4f(const char *name, const float v[4]);
192  bool SetUniform3uc(const char *name, const unsigned char v[3]); // maybe remove
193  bool SetUniform4uc(const char *name, const unsigned char v[4]); // maybe remove
194  bool SetUniformMatrix(const char *name, vtkMatrix3x3 *v);
195  bool SetUniformMatrix(const char *name, vtkMatrix4x4 *v);
196  bool SetUniformMatrix3x3(const char *name, float *v);
197  bool SetUniformMatrix4x4(const char *name, float *v);
198 
200  bool SetUniform1iv(const char *name, const int count, const int *f);
201  bool SetUniform1fv(const char *name, const int count, const float *f);
202  bool SetUniform2fv(const char *name, const int count, const float (*f)[2]);
203  bool SetUniform3fv(const char *name, const int count, const float (*f)[3]);
204  bool SetUniform4fv(const char *name, const int count, const float (*f)[4]);
205  bool SetUniformMatrix4x4v(const char *name, const int count, float *v);
206 
207  // How many outputs does this program produce
208  // only valid for OpenGL 3.2 or later
209  vtkSetMacro(NumberOfOutputs,unsigned int);
210 
222  static bool Substitute(
224  const std::string &search,
225  const std::string &replace,
226  bool all = true);
227 
239  static bool Substitute(
240  vtkShader* shader,
241  const std::string &search,
242  const std::string &replace,
243  bool all = true);
244 
250  bool IsUniformUsed(const char *);
251 
256  bool IsAttributeUsed(const char *name);
257 
258  // maps of std::string are super slow when calling find
259  // with a string literal or const char * as find
260  // forces construction/copy/destruction of a
261  // std::string copy of the const char *
262  // In spite of the doubters this can really be a
263  // huge CPU hog.
264  struct cmp_str
265  {
266  bool operator()(const char *a, const char *b) const
267  {
268  return strcmp(a, b) < 0;
269  }
270  };
271 
273 
290  vtkSetStringMacro(FileNamePrefixForDebugging);
291  vtkGetStringMacro(FileNamePrefixForDebugging);
293 
295 
303  UserGroup, // always will be last
304  };
305  void SetUniformGroupUpdateTime(int, vtkMTimeType tm);
306  vtkMTimeType GetUniformGroupUpdateTime(int);
308 
309  // returns the location for a uniform or attribute in
310  // this program. Is cached for performance.
311  int FindUniform(const char *name);
312  int FindAttributeArray(const char *name);
313 
314 protected:
316  ~vtkShaderProgram() override;
317 
318  /***************************************************************
319  * The following functions are only for use by the shader cache
320  * which is why they are protected and that class is a friend
321  * you need to use the shader cache to compile/link/bind your shader
322  * do not try to do it yourself as it will screw up the cache
323  ***************************************************************/
324  friend class vtkOpenGLShaderCache;
325 
332  bool AttachShader(const vtkShader *shader);
333 
339  bool DetachShader(const vtkShader *shader);
340 
344  virtual int CompileShader();
345 
351  bool Link();
352 
357  bool Bind();
358 
360  void Release();
361 
362  /************* end **************************************/
363 
364  vtkShader *VertexShader;
365  vtkShader *FragmentShader;
366  vtkShader *GeometryShader;
367  vtkTransformFeedback *TransformFeedback;
368 
369  // hash of the shader program
370  std::string MD5Hash;
371 
372  bool SetAttributeArrayInternal(const char *name, void *buffer,
373  int type, int tupleSize,
374  NormalizeOption normalize);
375  int Handle;
376  int VertexShaderHandle;
377  int FragmentShaderHandle;
378  int GeometryShaderHandle;
379 
380  bool Linked;
381  bool Bound;
382  bool Compiled;
383 
384  // for glsl 1.5 or later, how many outputs
385  // does this shader create
386  // they will be bound in order to
387  // fragOutput0 fragOutput1 etc...
388  unsigned int NumberOfOutputs;
389 
390  std::string Error;
391 
392  // since we are using const char * arrays we have to
393  // free our memory :-)
394  void ClearMaps();
395  std::map<const char *, int, cmp_str> AttributeLocs;
396  std::map<const char *, int, cmp_str> UniformLocs;
397 
398  std::map<int, vtkMTimeType> UniformGroupMTimes;
399 
400  friend class VertexArrayObject;
401 
402 private:
403  vtkShaderProgram(const vtkShaderProgram&) = delete;
404  void operator=(const vtkShaderProgram&) = delete;
405 
406  char* FileNamePrefixForDebugging;
407 };
408 
409 
410 #endif
vtkShaderProgram::GetMD5Hash
std::string GetMD5Hash() const
Set/Get the md5 hash of this program.
Definition: vtkShaderProgram.h:95
vtkX3D::type
Definition: vtkX3D.h:516
vtkShaderProgram::NormalizeOption
NormalizeOption
Options for attribute normalization.
Definition: vtkShaderProgram.h:100
vtkObject::New
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on.
vtkShaderProgram::cmp_str
Definition: vtkShaderProgram.h:264
vtkShaderProgram::CameraGroup
Definition: vtkShaderProgram.h:301
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:53
vtkMatrix3x3
represent and manipulate 3x3 transformation matrices
Definition: vtkMatrix3x3.h:33
vtkEventDataAction::Release
vtkWindow
window superclass for vtkRenderWindow
Definition: vtkWindow.h:34
source
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
Definition: vtkBoostGraphAdapter.h:825
vtkX3D::offset
Definition: vtkX3D.h:438
vtkShaderProgram
The ShaderProgram uses one or more Shader objects.
Definition: vtkShaderProgram.h:44
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:33
vtkMatrix4x4
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:35
vtkShaderProgram::UserGroup
Definition: vtkShaderProgram.h:303
vtkShaderProgram::UniformGroups
UniformGroups
Set/Get times that can be used to track when a set of uniforms was last updated.
Definition: vtkShaderProgram.h:300
vtkShaderProgram::Normalize
The values range across the limits of the numeric type.
Definition: vtkShaderProgram.h:109
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:37
vtkObject::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkOpenGLShaderCache
manage Shader Programs within a context
Definition: vtkOpenGLShaderCache.h:35
vtkShaderProgram::GetHandle
int GetHandle() const
Get the handle of the shader program.
Definition: vtkShaderProgram.h:127
vtkX3D::name
Definition: vtkX3D.h:219
vtkShaderProgram::LightingGroup
Definition: vtkShaderProgram.h:302
vtkObject.h
vtkShaderProgram::cmp_str::operator()
bool operator()(const char *a, const char *b) const
Definition: vtkShaderProgram.h:266
vtkX3D::string
Definition: vtkX3D.h:490
vtkShaderProgram::SetMD5Hash
void SetMD5Hash(const std::string &hash)
Definition: vtkShaderProgram.h:96
vtkMTimeType
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:302
vtkTransformFeedback
Manages a TransformFeedback buffer.
Definition: vtkTransformFeedback.h:40