VTK  9.3.20240329
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
74 #ifndef vtkShaderProgram_h
75 #define vtkShaderProgram_h
76 
77 #include "vtkObject.h"
78 #include "vtkRenderingOpenGL2Module.h" // for export macro
79 
80 #include <map> // For member variables.
81 #include <string> // For member variables.
82 
83 VTK_ABI_NAMESPACE_BEGIN
84 class vtkMatrix3x3;
85 class vtkMatrix4x4;
87 class vtkShader;
88 class VertexArrayObject;
89 class vtkWindow;
90 
100 class VTKRENDERINGOPENGL2_EXPORT vtkShaderProgram : public vtkObject
101 {
102 public:
104  vtkTypeMacro(vtkShaderProgram, vtkObject);
105  void PrintSelf(ostream& os, vtkIndent indent) override;
106 
108 
111  vtkGetObjectMacro(VertexShader, vtkShader);
114 
116 
119  vtkGetObjectMacro(FragmentShader, vtkShader);
122 
124 
127  vtkGetObjectMacro(GeometryShader, vtkShader);
130 
132 
135  vtkGetObjectMacro(ComputeShader, vtkShader);
138 
140 
143  vtkGetObjectMacro(TransformFeedback, vtkTransformFeedback);
146 
148 
151  vtkGetMacro(Compiled, bool);
152  vtkSetMacro(Compiled, bool);
153  vtkBooleanMacro(Compiled, bool);
155 
159  std::string GetMD5Hash() const { return this->MD5Hash; }
160  void SetMD5Hash(const std::string& hash) { this->MD5Hash = hash; }
161 
164  {
175  NoNormalize
176  };
177 
182  bool isBound() const { return this->Bound; }
183 
188 
190  int GetHandle() const { return Handle; }
191 
193  std::string GetError() const { return Error; }
194 
199  bool EnableAttributeArray(const char* name);
200 
205  bool DisableAttributeArray(const char* name);
206 
222  bool UseAttributeArray(const char* name, int offset, size_t stride, int elementType,
223  int elementTupleSize, NormalizeOption normalize);
224 
242  template <class T>
244  const char* name, const T& array, int tupleSize, NormalizeOption normalize);
245 
247  bool SetUniformi(const char* name, int v);
248  bool SetUniformf(const char* name, float v);
249  bool SetUniform2i(const char* name, const int v[2]);
250  bool SetUniform2f(const char* name, const float v[2]);
251  bool SetUniform3f(const char* name, const float v[3]);
252  bool SetUniform3f(const char* name, const double v[3]);
253  bool SetUniform4f(const char* name, const float v[4]);
254  bool SetUniform3uc(const char* name, const unsigned char v[3]); // maybe remove
255  bool SetUniform4uc(const char* name, const unsigned char v[4]); // maybe remove
256  bool SetUniformMatrix(const char* name, vtkMatrix3x3* v);
257  bool SetUniformMatrix(const char* name, vtkMatrix4x4* v);
258  bool SetUniformMatrix3x3(const char* name, float* v);
259  bool SetUniformMatrix4x4(const char* name, float* v);
260 
262  bool SetUniform1iv(const char* name, int count, const int* f);
263  bool SetUniform1fv(const char* name, int count, const float* f);
264  bool SetUniform2fv(const char* name, int count, const float* f);
265  bool SetUniform2fv(const char* name, int count, const float (*f)[2]);
266  bool SetUniform3fv(const char* name, int count, const float* f);
267  bool SetUniform3fv(const char* name, int count, const float (*f)[3]);
268  bool SetUniform4fv(const char* name, int count, const float* f);
269  bool SetUniform4fv(const char* name, int count, const float (*f)[4]);
270  bool SetUniformMatrix4x4v(const char* name, int count, float* v);
271 
272  // How many outputs does this program produce
273  // only valid for OpenGL 3.2 or later
274  vtkSetMacro(NumberOfOutputs, unsigned int);
275 
287  static bool Substitute(
288  std::string& source, const std::string& search, const std::string& replace, bool all = true);
289 
301  static bool Substitute(
302  vtkShader* shader, const std::string& search, const std::string& replace, bool all = true);
303 
309  bool IsUniformUsed(const char*);
310 
315  bool IsAttributeUsed(const char* name);
316 
317  // maps of std::string are super slow when calling find
318  // with a string literal or const char * as find
319  // forces construction/copy/destruction of a
320  // std::string copy of the const char *
321  // In spite of the doubters this can really be a
322  // huge CPU hog.
323  struct cmp_str
324  {
325  bool operator()(const char* a, const char* b) const { return strcmp(a, b) < 0; }
326  };
327 
329 
346  vtkSetFilePathMacro(FileNamePrefixForDebugging);
347  vtkGetFilePathMacro(FileNamePrefixForDebugging);
349 
351 
357  {
360  UserGroup, // always will be last
361  };
365 
366  // returns the location for a uniform or attribute in
367  // this program. Is cached for performance.
368  int FindUniform(const char* name);
369  int FindAttributeArray(const char* name);
370 
371 protected:
373  ~vtkShaderProgram() override;
374 
375  /***************************************************************
376  * The following functions are only for use by the shader cache
377  * which is why they are protected and that class is a friend
378  * you need to use the shader cache to compile/link/bind your shader
379  * do not try to do it yourself as it will screw up the cache
380  ***************************************************************/
381  friend class vtkOpenGLShaderCache;
382 
389  bool AttachShader(const vtkShader* shader);
390 
396  bool DetachShader(const vtkShader* shader);
397 
401  virtual int CompileShader();
402 
408  bool Link();
409 
414  bool Bind();
415 
417  void Release();
418 
419  /************* end **************************************/
420 
426 
427  // hash of the shader program
429 
431  const char* name, void* buffer, int type, int tupleSize, NormalizeOption normalize);
432  int Handle;
437 
438  bool Linked;
439  bool Bound;
440  bool Compiled;
441 
442  // for glsl 1.5 or later, how many outputs
443  // does this shader create
444  // they will be bound in order to
445  // fragOutput0 fragOutput1 etc...
446  unsigned int NumberOfOutputs;
447 
449 
450  // since we are using const char * arrays we have to
451  // free our memory :-)
452  void ClearMaps();
453  std::map<const char*, int, cmp_str> AttributeLocs;
454  std::map<const char*, int, cmp_str> UniformLocs;
455 
456  std::map<int, vtkMTimeType> UniformGroupMTimes;
457 
458  friend class VertexArrayObject;
459 
460 private:
461  vtkShaderProgram(const vtkShaderProgram&) = delete;
462  void operator=(const vtkShaderProgram&) = delete;
463 
464  // print shader code and report error
465  void ReportShaderError(vtkShader* shader);
466 
467  char* FileNamePrefixForDebugging;
468 };
469 
470 VTK_ABI_NAMESPACE_END
471 #endif
a simple class to control print indentation
Definition: vtkIndent.h:108
represent and manipulate 3x3 transformation matrices
Definition: vtkMatrix3x3.h:56
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:141
abstract base class for most VTK objects
Definition: vtkObject.h:162
manage Shader Programs within a context
The ShaderProgram uses one or more Shader objects.
bool Link()
Attempt to link the shader program.
bool SetUniform4fv(const char *name, int count, const float *f)
void Release()
Releases the shader program from the current context.
vtkShader * FragmentShader
~vtkShaderProgram() override
void SetFragmentShader(vtkShader *)
Get/set the fragment shader for this program.
std::string GetError() const
Get the error message (empty if none) for the shader program.
bool IsUniformUsed(const char *)
methods to inquire as to what uniforms/attributes are used by this shader.
bool Bind()
Bind the program in order to use it.
bool SetAttributeArray(const char *name, const T &array, int tupleSize, NormalizeOption normalize)
Upload the supplied array of tightly packed values to the named attribute.
vtkSetFilePathMacro(FileNamePrefixForDebugging)
When developing shaders, it's often convenient to tweak the shader and re-render incrementally.
void SetVertexShader(vtkShader *)
Get/set the vertex shader for this program.
bool SetUniform2i(const char *name, const int v[2])
vtkTransformFeedback * TransformFeedback
bool SetUniform3f(const char *name, const float v[3])
int FindAttributeArray(const char *name)
static vtkShaderProgram * New()
std::string GetMD5Hash() const
Set/Get the md5 hash of this program.
bool SetUniform4uc(const char *name, const unsigned char v[4])
std::map< const char *, int, cmp_str > UniformLocs
bool SetUniform4f(const char *name, const float v[4])
bool SetUniform3fv(const char *name, int count, const float *f)
bool SetUniform2f(const char *name, const float v[2])
bool SetUniformMatrix(const char *name, vtkMatrix4x4 *v)
UniformGroups
Set/Get times that can be used to track when a set of uniforms was last updated.
bool AttachShader(const vtkShader *shader)
Attach the supplied shader to this program.
bool SetUniform3fv(const char *name, int count, const float(*f)[3])
bool SetUniform4fv(const char *name, int count, const float(*f)[4])
int FindUniform(const char *name)
bool SetAttributeArrayInternal(const char *name, void *buffer, int type, int tupleSize, NormalizeOption normalize)
bool SetUniform2fv(const char *name, int count, const float(*f)[2])
int GetHandle() const
Get the handle of the shader program.
bool SetUniform1fv(const char *name, int count, const float *f)
NormalizeOption
Options for attribute normalization.
@ Normalize
The values range across the limits of the numeric type.
bool SetUniform3f(const char *name, const double v[3])
void SetMD5Hash(const std::string &hash)
bool EnableAttributeArray(const char *name)
Enable the named attribute array.
vtkShader * VertexShader
vtkShader * ComputeShader
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
bool isBound() const
Check if the program is currently bound, or not.
void SetUniformGroupUpdateTime(int, vtkMTimeType tm)
Set/Get times that can be used to track when a set of uniforms was last updated.
bool SetUniformMatrix(const char *name, vtkMatrix3x3 *v)
std::map< const char *, int, cmp_str > AttributeLocs
bool DetachShader(const vtkShader *shader)
Detach the supplied shader from this program.
bool SetUniform2fv(const char *name, int count, const float *f)
bool SetUniformf(const char *name, float v)
bool SetUniformMatrix4x4(const char *name, float *v)
vtkShader * GeometryShader
bool SetUniformMatrix3x3(const char *name, float *v)
bool DisableAttributeArray(const char *name)
Disable the named attribute array.
bool SetUniform1iv(const char *name, int count, const int *f)
Set the name uniform array to f with count elements.
bool SetUniform3uc(const char *name, const unsigned char v[3])
bool IsAttributeUsed(const char *name)
Return true if the compiled and linked shader has an attribute matching name.
bool UseAttributeArray(const char *name, int offset, size_t stride, int elementType, int elementTupleSize, NormalizeOption normalize)
Use the named attribute array with the bound BufferObject.
bool SetUniformi(const char *name, int v)
Set the name uniform value to int v.
unsigned int NumberOfOutputs
bool SetUniformMatrix4x4v(const char *name, int count, float *v)
vtkMTimeType GetUniformGroupUpdateTime(int)
Set/Get times that can be used to track when a set of uniforms was last updated.
vtkGetFilePathMacro(FileNamePrefixForDebugging)
When developing shaders, it's often convenient to tweak the shader and re-render incrementally.
void SetGeometryShader(vtkShader *)
Get/set the geometry shader for this program.
std::map< int, vtkMTimeType > UniformGroupMTimes
void ReleaseGraphicsResources(vtkWindow *win)
release any graphics resources this class is using.
static bool Substitute(std::string &source, const std::string &search, const std::string &replace, bool all=true)
perform in place string substitutions, indicate if a substitution was done this is useful for buildin...
virtual int CompileShader()
Compile this shader program and attached shaders.
void SetTransformFeedback(vtkTransformFeedback *tfc)
Get/Set a TransformFeedbackCapture object on this shader program.
static bool Substitute(vtkShader *shader, const std::string &search, const std::string &replace, bool all=true)
Perform in-place string substitutions on the shader source string and indicate if one or all substitu...
void SetComputeShader(vtkShader *)
Get/set the compute shader for this program.
Vertex or Fragment shader, combined into a ShaderProgram.
Definition: vtkShader.h:120
Manages a TransformFeedback buffer.
window superclass for vtkRenderWindow
Definition: vtkWindow.h:48
@ type
Definition: vtkX3D.h:516
@ name
Definition: vtkX3D.h:219
@ offset
Definition: vtkX3D.h:438
@ string
Definition: vtkX3D.h:490
bool operator()(const char *a, const char *b) const
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:270