VTK  9.2.6
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 =========================================================================*/
24 #ifndef vtkShaderProgram_h
25 #define vtkShaderProgram_h
26 
27 #include "vtkObject.h"
28 #include "vtkRenderingOpenGL2Module.h" // for export macro
29 
30 #include <map> // For member variables.
31 #include <string> // For member variables.
32 
33 class vtkMatrix3x3;
34 class vtkMatrix4x4;
36 class vtkShader;
37 class VertexArrayObject;
38 class vtkWindow;
39 
47 class VTKRENDERINGOPENGL2_EXPORT vtkShaderProgram : public vtkObject
48 {
49 public:
50  static vtkShaderProgram* New();
51  vtkTypeMacro(vtkShaderProgram, vtkObject);
52  void PrintSelf(ostream& os, vtkIndent indent) override;
53 
55 
58  vtkGetObjectMacro(VertexShader, vtkShader);
59  void SetVertexShader(vtkShader*);
61 
63 
66  vtkGetObjectMacro(FragmentShader, vtkShader);
67  void SetFragmentShader(vtkShader*);
69 
71 
74  vtkGetObjectMacro(GeometryShader, vtkShader);
75  void SetGeometryShader(vtkShader*);
77 
79 
82  vtkGetObjectMacro(TransformFeedback, vtkTransformFeedback);
83  void SetTransformFeedback(vtkTransformFeedback* tfc);
85 
87 
90  vtkGetMacro(Compiled, bool);
91  vtkSetMacro(Compiled, bool);
92  vtkBooleanMacro(Compiled, bool);
94 
98  std::string GetMD5Hash() const { return this->MD5Hash; }
99  void SetMD5Hash(const std::string& hash) { this->MD5Hash = hash; }
100 
103  {
114  NoNormalize
115  };
116 
121  bool isBound() const { return this->Bound; }
122 
126  void ReleaseGraphicsResources(vtkWindow* win);
127 
129  int GetHandle() const { return Handle; }
130 
132  std::string GetError() const { return Error; }
133 
138  bool EnableAttributeArray(const char* name);
139 
144  bool DisableAttributeArray(const char* name);
145 
161  bool UseAttributeArray(const char* name, int offset, size_t stride, int elementType,
162  int elementTupleSize, NormalizeOption normalize);
163 
181  template <class T>
182  bool SetAttributeArray(
183  const char* name, const T& array, int tupleSize, NormalizeOption normalize);
184 
186  bool SetUniformi(const char* name, int v);
187  bool SetUniformf(const char* name, float v);
188  bool SetUniform2i(const char* name, const int v[2]);
189  bool SetUniform2f(const char* name, const float v[2]);
190  bool SetUniform3f(const char* name, const float v[3]);
191  bool SetUniform3f(const char* name, const double v[3]);
192  bool SetUniform4f(const char* name, const float v[4]);
193  bool SetUniform3uc(const char* name, const unsigned char v[3]); // maybe remove
194  bool SetUniform4uc(const char* name, const unsigned char v[4]); // maybe remove
195  bool SetUniformMatrix(const char* name, vtkMatrix3x3* v);
196  bool SetUniformMatrix(const char* name, vtkMatrix4x4* v);
197  bool SetUniformMatrix3x3(const char* name, float* v);
198  bool SetUniformMatrix4x4(const char* name, float* v);
199 
201  bool SetUniform1iv(const char* name, const int count, const int* f);
202  bool SetUniform1fv(const char* name, const int count, const float* f);
203  bool SetUniform2fv(const char* name, const int count, const float* f);
204  bool SetUniform2fv(const char* name, const int count, const float (*f)[2]);
205  bool SetUniform3fv(const char* name, const int count, const float* f);
206  bool SetUniform3fv(const char* name, const int count, const float (*f)[3]);
207  bool SetUniform4fv(const char* name, const int count, const float* f);
208  bool SetUniform4fv(const char* name, const int count, const float (*f)[4]);
209  bool SetUniformMatrix4x4v(const char* name, const int count, float* v);
210 
211  // How many outputs does this program produce
212  // only valid for OpenGL 3.2 or later
213  vtkSetMacro(NumberOfOutputs, unsigned int);
214 
226  static bool Substitute(
227  std::string& source, const std::string& search, const std::string& replace, bool all = true);
228 
240  static bool Substitute(
241  vtkShader* shader, const std::string& search, const std::string& replace, bool all = true);
242 
248  bool IsUniformUsed(const char*);
249 
254  bool IsAttributeUsed(const char* name);
255 
256  // maps of std::string are super slow when calling find
257  // with a string literal or const char * as find
258  // forces construction/copy/destruction of a
259  // std::string copy of the const char *
260  // In spite of the doubters this can really be a
261  // huge CPU hog.
262  struct cmp_str
263  {
264  bool operator()(const char* a, const char* b) const { return strcmp(a, b) < 0; }
265  };
266 
268 
285  vtkSetFilePathMacro(FileNamePrefixForDebugging);
286  vtkGetFilePathMacro(FileNamePrefixForDebugging);
288 
290 
296  {
299  UserGroup, // always will be last
300  };
301  void SetUniformGroupUpdateTime(int, vtkMTimeType tm);
302  vtkMTimeType GetUniformGroupUpdateTime(int);
304 
305  // returns the location for a uniform or attribute in
306  // this program. Is cached for performance.
307  int FindUniform(const char* name);
308  int FindAttributeArray(const char* name);
309 
310 protected:
312  ~vtkShaderProgram() override;
313 
314  /***************************************************************
315  * The following functions are only for use by the shader cache
316  * which is why they are protected and that class is a friend
317  * you need to use the shader cache to compile/link/bind your shader
318  * do not try to do it yourself as it will screw up the cache
319  ***************************************************************/
320  friend class vtkOpenGLShaderCache;
321 
328  bool AttachShader(const vtkShader* shader);
329 
335  bool DetachShader(const vtkShader* shader);
336 
340  virtual int CompileShader();
341 
347  bool Link();
348 
353  bool Bind();
354 
356  void Release();
357 
358  /************* end **************************************/
359 
364 
365  // hash of the shader program
367 
368  bool SetAttributeArrayInternal(
369  const char* name, void* buffer, int type, int tupleSize, NormalizeOption normalize);
370  int Handle;
374 
375  bool Linked;
376  bool Bound;
377  bool Compiled;
378 
379  // for glsl 1.5 or later, how many outputs
380  // does this shader create
381  // they will be bound in order to
382  // fragOutput0 fragOutput1 etc...
383  unsigned int NumberOfOutputs;
384 
386 
387  // since we are using const char * arrays we have to
388  // free our memory :-)
389  void ClearMaps();
390  std::map<const char*, int, cmp_str> AttributeLocs;
391  std::map<const char*, int, cmp_str> UniformLocs;
392 
393  std::map<int, vtkMTimeType> UniformGroupMTimes;
394 
395  friend class VertexArrayObject;
396 
397 private:
398  vtkShaderProgram(const vtkShaderProgram&) = delete;
399  void operator=(const vtkShaderProgram&) = delete;
400 
401  char* FileNamePrefixForDebugging;
402 };
403 
404 #endif
std::map< const char *, int, cmp_str > UniformLocs
int GetHandle() const
Get the handle of the shader program.
abstract base class for most VTK objects
Definition: vtkObject.h:62
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:41
std::map< const char *, int, cmp_str > AttributeLocs
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkTransformFeedback * TransformFeedback
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:287
unsigned int NumberOfOutputs
manage Shader Programs within a context
bool isBound() const
Check if the program is currently bound, or not.
vtkShader * VertexShader
The values range across the limits of the numeric type.
bool operator()(const char *a, const char *b) const
void SetMD5Hash(const std::string &hash)
window superclass for vtkRenderWindow
Definition: vtkWindow.h:38
a simple class to control print indentation
Definition: vtkIndent.h:39
vtkShader * GeometryShader
Manages a TransformFeedback buffer.
vtkShader * FragmentShader
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
NormalizeOption
Options for attribute normalization.
std::string GetMD5Hash() const
Set/Get the md5 hash of this program.
Vertex or Fragment shader, combined into a ShaderProgram.
Definition: vtkShader.h:37
std::map< int, vtkMTimeType > UniformGroupMTimes
std::string GetError() const
Get the error message (empty if none) for the shader program.
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
UniformGroups
Set/Get times that can be used to track when a set of uniforms was last updated.
The ShaderProgram uses one or more Shader objects.