VTK  9.7.1
vtkShaderProgram.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
15 #ifndef vtkShaderProgram_h
16 #define vtkShaderProgram_h
17 
18 #include "vtkDeprecation.h" // VTK_DEPRECATED_IN_9_6_0()
19 #include "vtkObject.h"
20 #include "vtkRenderingOpenGL2Module.h" // for export macro
21 
22 #include <cstddef> // For std::size_t.
23 #include <map> // For member variables.
24 #include <string> // For member variables.
25 #include <vector> // For member variables.
26 
27 VTK_ABI_NAMESPACE_BEGIN
28 class vtkMatrix3x3;
29 class vtkMatrix4x4;
31 class vtkShader;
32 class VertexArrayObject;
33 class vtkWindow;
34 
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(ComputeShader, vtkShader);
80  void SetComputeShader(vtkShader*);
82 
84 
87  vtkGetObjectMacro(TessControlShader, vtkShader);
88  void SetTessControlShader(vtkShader*);
90 
92 
95  vtkGetObjectMacro(TessEvaluationShader, vtkShader);
96  void SetTessEvaluationShader(vtkShader*);
98 
100 
103  vtkGetObjectMacro(TransformFeedback, vtkTransformFeedback);
104  void SetTransformFeedback(vtkTransformFeedback* tfc);
106 
108 
111  vtkGetMacro(Compiled, bool);
112  vtkSetMacro(Compiled, bool);
113  vtkBooleanMacro(Compiled, bool);
115 
119  std::string GetHash() const { return this->MD5Hash; }
120  void SetHash(const std::string& hash) { this->MD5Hash = hash; }
121 
125  VTK_DEPRECATED_IN_9_6_0("Use GetHash instead.")
126  std::string GetMD5Hash() const { return this->MD5Hash; }
127  VTK_DEPRECATED_IN_9_6_0("Use SetHash instead.")
128  void SetMD5Hash(const std::string& hash) { this->MD5Hash = hash; }
129 
132  {
143  NoNormalize
144  };
145 
150  bool isBound() const { return this->Bound; }
151 
155  void ReleaseGraphicsResources(vtkWindow* win);
156 
158  int GetHandle() const { return Handle; }
159 
161  std::string GetError() const { return Error; }
162 
167  bool EnableAttributeArray(const char* name);
168 
173  bool DisableAttributeArray(const char* name);
174 
190  bool UseAttributeArray(const char* name, int offset, size_t stride, int elementType,
191  int elementTupleSize, NormalizeOption normalize);
192 
210  template <class T>
211  bool SetAttributeArray(
212  const char* name, const T& array, int tupleSize, NormalizeOption normalize);
213 
215  bool SetUniformi(const char* name, int v);
216  bool SetUniformf(const char* name, float v);
217  bool SetUniform2i(const char* name, const int v[2]);
218  bool SetUniform2f(const char* name, const float v[2]);
219  bool SetUniform3f(const char* name, const float v[3]);
220  bool SetUniform3f(const char* name, const double v[3]);
221  bool SetUniform4f(const char* name, const float v[4]);
222  bool SetUniform3uc(const char* name, const unsigned char v[3]); // maybe remove
223  bool SetUniform4uc(const char* name, const unsigned char v[4]); // maybe remove
224  bool SetUniformMatrix(const char* name, vtkMatrix3x3* v);
225  bool SetUniformMatrix(const char* name, vtkMatrix4x4* v);
226  bool SetUniformMatrix3x3(const char* name, float* v);
227  bool SetUniformMatrix4x4(const char* name, float* v);
228 
230  bool SetUniform1iv(const char* name, int count, const int* f);
231  bool SetUniform1fv(const char* name, int count, const float* f);
232  bool SetUniform2fv(const char* name, int count, const float* f);
233  bool SetUniform2fv(const char* name, int count, const float (*f)[2]);
234  bool SetUniform3fv(const char* name, int count, const float* f);
235  bool SetUniform3fv(const char* name, int count, const float (*f)[3]);
236  bool SetUniform4fv(const char* name, int count, const float* f);
237  bool SetUniform4fv(const char* name, int count, const float (*f)[4]);
238  bool SetUniformMatrix4x4v(const char* name, int count, float* v);
239 
241 
252  bool SetUniformi(int location, int v);
253  bool SetUniformf(int location, float v);
254  bool SetUniform3f(int location, const float v[3]);
255  bool SetUniform3f(int location, const double v[3]);
256  bool SetUniform4f(int location, const float v[4]);
257  bool SetUniform1fv(int location, int count, const float* f);
258  bool SetUniform4fv(int location, int count, const float* f);
259  bool SetUniformMatrix(int location, vtkMatrix3x3* v);
260  bool SetUniformMatrix(int location, vtkMatrix4x4* v);
261  bool SetUniformMatrix3x3(int location, float* v);
262  bool SetUniformMatrix4x4(int location, float* v);
263  bool SetUniformMatrix4x4v(int location, int count, float* v);
265 
272  unsigned int GetLinkCount() const { return this->LinkCount; }
273 
274  // How many outputs does this program produce
275  // only valid for OpenGL 3.2 or later
276  vtkSetMacro(NumberOfOutputs, unsigned int);
277 
289  static bool Substitute(
290  std::string& source, const std::string& search, const std::string& replace, bool all = true);
291 
303  static bool Substitute(
304  vtkShader* shader, const std::string& search, const std::string& replace, bool all = true);
305 
311  bool IsUniformUsed(const char*);
312 
317  bool IsAttributeUsed(const char* name);
318 
319  // maps of std::string are super slow when calling find
320  // with a string literal or const char * as find
321  // forces construction/copy/destruction of a
322  // std::string copy of the const char *
323  // In spite of the doubters this can really be a
324  // huge CPU hog.
325  struct cmp_str
326  {
327  bool operator()(const char* a, const char* b) const { return strcmp(a, b) < 0; }
328  };
329 
331 
348  vtkSetFilePathMacro(FileNamePrefixForDebugging);
349  vtkGetFilePathMacro(FileNamePrefixForDebugging);
351 
353 
359  {
362  UserGroup, // always will be last
363  };
364  void SetUniformGroupUpdateTime(int, vtkMTimeType tm);
365  vtkMTimeType GetUniformGroupUpdateTime(int);
367 
368  // returns the location for a uniform or attribute in
369  // this program. Is cached for performance.
370  int FindUniform(const char* name);
371  int FindAttributeArray(const char* name);
372 
373 protected:
375  ~vtkShaderProgram() override;
376 
377  /***************************************************************
378  * The following functions are only for use by the shader cache
379  * which is why they are protected and that class is a friend
380  * you need to use the shader cache to compile/link/bind your shader
381  * do not try to do it yourself as it will screw up the cache
382  ***************************************************************/
383  friend class vtkOpenGLShaderCache;
384 
391  bool AttachShader(const vtkShader* shader);
392 
398  bool DetachShader(const vtkShader* shader);
399 
403  virtual int CompileShader();
404 
410  bool Link();
411 
416  bool Bind();
417 
419  void Release();
420 
421  /************* end **************************************/
422 
430 
431  // hash of the shader program
433 
434  bool SetAttributeArrayInternal(
435  const char* name, void* buffer, int type, int tupleSize, NormalizeOption normalize);
436  int Handle;
443 
444  bool Linked;
445  bool Bound;
446  bool Compiled;
447 
448  // for glsl 1.5 or later, how many outputs
449  // does this shader create
450  // they will be bound in order to
451  // fragOutput0 fragOutput1 etc...
452  unsigned int NumberOfOutputs;
453 
455 
456  // since we are using const char * arrays we have to
457  // free our memory :-)
458  void ClearMaps();
459  std::map<const char*, int, cmp_str> AttributeLocs;
460  std::map<const char*, int, cmp_str> UniformLocs;
461 
462  std::map<int, vtkMTimeType> UniformGroupMTimes;
463 
464  friend class VertexArrayObject;
465 
466 private:
467  vtkShaderProgram(const vtkShaderProgram&) = delete;
468  void operator=(const vtkShaderProgram&) = delete;
469 
470  // print shader code and report error
471  void ReportShaderError(vtkShader* shader);
472 
485  bool UniformValueChanged(int location, const void* data, std::size_t nbytes);
486 
487  char* FileNamePrefixForDebugging;
488  std::vector<std::vector<unsigned char>> UniformValueCache;
489  // Bumped on every successful Link(); see GetLinkCount().
490  unsigned int LinkCount = 0;
491 };
492 
493 VTK_ABI_NAMESPACE_END
494 #endif
std::map< const char *, int, cmp_str > UniformLocs
int GetHandle() const
Get the handle of the shader program.
vtkShader * ComputeShader
abstract base class for most VTK objects
Definition: vtkObject.h:52
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:31
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:318
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
vtkShader * TessEvaluationShader
void SetHash(const std::string &hash)
window superclass for vtkRenderWindow
Definition: vtkWindow.h:41
#define VTK_DEPRECATED_IN_9_6_0(reason)
a simple class to control print indentation
Definition: vtkIndent.h:28
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 GetHash() const
Set/Get the hash of this program.
Vertex or Fragment shader, combined into a ShaderProgram.
Definition: vtkShader.h:30
std::map< int, vtkMTimeType > UniformGroupMTimes
std::string GetError() const
Get the error message (empty if none) for the shader program.
unsigned int GetLinkCount() const
Number of times this program has been (re)linked.
RealT tm
Definition: PyrC2Basis.h:35
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:26
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.
vtkShader * TessControlShader