VTK  9.3.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
13 #ifndef vtkShaderProgram_h
14 #define vtkShaderProgram_h
15 
16 #include "vtkObject.h"
17 #include "vtkRenderingOpenGL2Module.h" // for export macro
18 
19 #include <map> // For member variables.
20 #include <string> // For member variables.
21 
22 VTK_ABI_NAMESPACE_BEGIN
23 class vtkMatrix3x3;
24 class vtkMatrix4x4;
26 class vtkShader;
27 class VertexArrayObject;
28 class vtkWindow;
29 
37 class VTKRENDERINGOPENGL2_EXPORT vtkShaderProgram : public vtkObject
38 {
39 public:
40  static vtkShaderProgram* New();
41  vtkTypeMacro(vtkShaderProgram, vtkObject);
42  void PrintSelf(ostream& os, vtkIndent indent) override;
43 
45 
48  vtkGetObjectMacro(VertexShader, vtkShader);
49  void SetVertexShader(vtkShader*);
51 
53 
56  vtkGetObjectMacro(FragmentShader, vtkShader);
57  void SetFragmentShader(vtkShader*);
59 
61 
64  vtkGetObjectMacro(GeometryShader, vtkShader);
65  void SetGeometryShader(vtkShader*);
67 
69 
72  vtkGetObjectMacro(TransformFeedback, vtkTransformFeedback);
73  void SetTransformFeedback(vtkTransformFeedback* tfc);
75 
77 
80  vtkGetMacro(Compiled, bool);
81  vtkSetMacro(Compiled, bool);
82  vtkBooleanMacro(Compiled, bool);
84 
88  std::string GetMD5Hash() const { return this->MD5Hash; }
89  void SetMD5Hash(const std::string& hash) { this->MD5Hash = hash; }
90 
93  {
104  NoNormalize
105  };
106 
111  bool isBound() const { return this->Bound; }
112 
116  void ReleaseGraphicsResources(vtkWindow* win);
117 
119  int GetHandle() const { return Handle; }
120 
122  std::string GetError() const { return Error; }
123 
128  bool EnableAttributeArray(const char* name);
129 
134  bool DisableAttributeArray(const char* name);
135 
151  bool UseAttributeArray(const char* name, int offset, size_t stride, int elementType,
152  int elementTupleSize, NormalizeOption normalize);
153 
171  template <class T>
172  bool SetAttributeArray(
173  const char* name, const T& array, int tupleSize, NormalizeOption normalize);
174 
176  bool SetUniformi(const char* name, int v);
177  bool SetUniformf(const char* name, float v);
178  bool SetUniform2i(const char* name, const int v[2]);
179  bool SetUniform2f(const char* name, const float v[2]);
180  bool SetUniform3f(const char* name, const float v[3]);
181  bool SetUniform3f(const char* name, const double v[3]);
182  bool SetUniform4f(const char* name, const float v[4]);
183  bool SetUniform3uc(const char* name, const unsigned char v[3]); // maybe remove
184  bool SetUniform4uc(const char* name, const unsigned char v[4]); // maybe remove
185  bool SetUniformMatrix(const char* name, vtkMatrix3x3* v);
186  bool SetUniformMatrix(const char* name, vtkMatrix4x4* v);
187  bool SetUniformMatrix3x3(const char* name, float* v);
188  bool SetUniformMatrix4x4(const char* name, float* v);
189 
191  bool SetUniform1iv(const char* name, int count, const int* f);
192  bool SetUniform1fv(const char* name, int count, const float* f);
193  bool SetUniform2fv(const char* name, int count, const float* f);
194  bool SetUniform2fv(const char* name, int count, const float (*f)[2]);
195  bool SetUniform3fv(const char* name, int count, const float* f);
196  bool SetUniform3fv(const char* name, int count, const float (*f)[3]);
197  bool SetUniform4fv(const char* name, int count, const float* f);
198  bool SetUniform4fv(const char* name, int count, const float (*f)[4]);
199  bool SetUniformMatrix4x4v(const char* name, int count, float* v);
200 
201  // How many outputs does this program produce
202  // only valid for OpenGL 3.2 or later
203  vtkSetMacro(NumberOfOutputs, unsigned int);
204 
216  static bool Substitute(
217  std::string& source, const std::string& search, const std::string& replace, bool all = true);
218 
230  static bool Substitute(
231  vtkShader* shader, const std::string& search, const std::string& replace, bool all = true);
232 
238  bool IsUniformUsed(const char*);
239 
244  bool IsAttributeUsed(const char* name);
245 
246  // maps of std::string are super slow when calling find
247  // with a string literal or const char * as find
248  // forces construction/copy/destruction of a
249  // std::string copy of the const char *
250  // In spite of the doubters this can really be a
251  // huge CPU hog.
252  struct cmp_str
253  {
254  bool operator()(const char* a, const char* b) const { return strcmp(a, b) < 0; }
255  };
256 
258 
275  vtkSetFilePathMacro(FileNamePrefixForDebugging);
276  vtkGetFilePathMacro(FileNamePrefixForDebugging);
278 
280 
286  {
289  UserGroup, // always will be last
290  };
291  void SetUniformGroupUpdateTime(int, vtkMTimeType tm);
292  vtkMTimeType GetUniformGroupUpdateTime(int);
294 
295  // returns the location for a uniform or attribute in
296  // this program. Is cached for performance.
297  int FindUniform(const char* name);
298  int FindAttributeArray(const char* name);
299 
300 protected:
302  ~vtkShaderProgram() override;
303 
304  /***************************************************************
305  * The following functions are only for use by the shader cache
306  * which is why they are protected and that class is a friend
307  * you need to use the shader cache to compile/link/bind your shader
308  * do not try to do it yourself as it will screw up the cache
309  ***************************************************************/
310  friend class vtkOpenGLShaderCache;
311 
318  bool AttachShader(const vtkShader* shader);
319 
325  bool DetachShader(const vtkShader* shader);
326 
330  virtual int CompileShader();
331 
337  bool Link();
338 
343  bool Bind();
344 
346  void Release();
347 
348  /************* end **************************************/
349 
354 
355  // hash of the shader program
357 
358  bool SetAttributeArrayInternal(
359  const char* name, void* buffer, int type, int tupleSize, NormalizeOption normalize);
360  int Handle;
364 
365  bool Linked;
366  bool Bound;
367  bool Compiled;
368 
369  // for glsl 1.5 or later, how many outputs
370  // does this shader create
371  // they will be bound in order to
372  // fragOutput0 fragOutput1 etc...
373  unsigned int NumberOfOutputs;
374 
376 
377  // since we are using const char * arrays we have to
378  // free our memory :-)
379  void ClearMaps();
380  std::map<const char*, int, cmp_str> AttributeLocs;
381  std::map<const char*, int, cmp_str> UniformLocs;
382 
383  std::map<int, vtkMTimeType> UniformGroupMTimes;
384 
385  friend class VertexArrayObject;
386 
387 private:
388  vtkShaderProgram(const vtkShaderProgram&) = delete;
389  void operator=(const vtkShaderProgram&) = delete;
390 
391  char* FileNamePrefixForDebugging;
392 };
393 
394 VTK_ABI_NAMESPACE_END
395 #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:51
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:30
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:270
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:27
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 GetMD5Hash() const
Set/Get the md5 hash of this program.
Vertex or Fragment shader, combined into a ShaderProgram.
Definition: vtkShader.h:27
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:25
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.