VTK  9.2.20230129
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 =========================================================================*/
103 #ifndef vtkShaderProgram_h
104 #define vtkShaderProgram_h
105 
106 #include "vtkObject.h"
107 #include "vtkRenderingOpenGL2Module.h" // for export macro
108 
109 #include <map> // For member variables.
110 #include <string> // For member variables.
111 
112 VTK_ABI_NAMESPACE_BEGIN
113 class vtkMatrix3x3;
114 class vtkMatrix4x4;
116 class vtkShader;
117 class VertexArrayObject;
118 class vtkWindow;
119 
127 class VTKRENDERINGOPENGL2_EXPORT vtkShaderProgram : public vtkObject
128 {
129 public:
131  vtkTypeMacro(vtkShaderProgram, vtkObject);
132  void PrintSelf(ostream& os, vtkIndent indent) override;
133 
135 
138  vtkGetObjectMacro(VertexShader, vtkShader);
141 
143 
146  vtkGetObjectMacro(FragmentShader, vtkShader);
149 
151 
154  vtkGetObjectMacro(GeometryShader, vtkShader);
157 
159 
162  vtkGetObjectMacro(TransformFeedback, vtkTransformFeedback);
165 
167 
170  vtkGetMacro(Compiled, bool);
171  vtkSetMacro(Compiled, bool);
172  vtkBooleanMacro(Compiled, bool);
174 
178  std::string GetMD5Hash() const { return this->MD5Hash; }
179  void SetMD5Hash(const std::string& hash) { this->MD5Hash = hash; }
180 
183  {
194  NoNormalize
195  };
196 
201  bool isBound() const { return this->Bound; }
202 
207 
209  int GetHandle() const { return Handle; }
210 
212  std::string GetError() const { return Error; }
213 
218  bool EnableAttributeArray(const char* name);
219 
224  bool DisableAttributeArray(const char* name);
225 
241  bool UseAttributeArray(const char* name, int offset, size_t stride, int elementType,
242  int elementTupleSize, NormalizeOption normalize);
243 
261  template <class T>
263  const char* name, const T& array, int tupleSize, NormalizeOption normalize);
264 
266  bool SetUniformi(const char* name, int v);
267  bool SetUniformf(const char* name, float v);
268  bool SetUniform2i(const char* name, const int v[2]);
269  bool SetUniform2f(const char* name, const float v[2]);
270  bool SetUniform3f(const char* name, const float v[3]);
271  bool SetUniform3f(const char* name, const double v[3]);
272  bool SetUniform4f(const char* name, const float v[4]);
273  bool SetUniform3uc(const char* name, const unsigned char v[3]); // maybe remove
274  bool SetUniform4uc(const char* name, const unsigned char v[4]); // maybe remove
275  bool SetUniformMatrix(const char* name, vtkMatrix3x3* v);
276  bool SetUniformMatrix(const char* name, vtkMatrix4x4* v);
277  bool SetUniformMatrix3x3(const char* name, float* v);
278  bool SetUniformMatrix4x4(const char* name, float* v);
279 
281  bool SetUniform1iv(const char* name, const int count, const int* f);
282  bool SetUniform1fv(const char* name, const int count, const float* f);
283  bool SetUniform2fv(const char* name, const int count, const float* f);
284  bool SetUniform2fv(const char* name, const int count, const float (*f)[2]);
285  bool SetUniform3fv(const char* name, const int count, const float* f);
286  bool SetUniform3fv(const char* name, const int count, const float (*f)[3]);
287  bool SetUniform4fv(const char* name, const int count, const float* f);
288  bool SetUniform4fv(const char* name, const int count, const float (*f)[4]);
289  bool SetUniformMatrix4x4v(const char* name, const int count, float* v);
290 
291  // How many outputs does this program produce
292  // only valid for OpenGL 3.2 or later
293  vtkSetMacro(NumberOfOutputs, unsigned int);
294 
306  static bool Substitute(
307  std::string& source, const std::string& search, const std::string& replace, bool all = true);
308 
320  static bool Substitute(
321  vtkShader* shader, const std::string& search, const std::string& replace, bool all = true);
322 
328  bool IsUniformUsed(const char*);
329 
334  bool IsAttributeUsed(const char* name);
335 
336  // maps of std::string are super slow when calling find
337  // with a string literal or const char * as find
338  // forces construction/copy/destruction of a
339  // std::string copy of the const char *
340  // In spite of the doubters this can really be a
341  // huge CPU hog.
342  struct cmp_str
343  {
344  bool operator()(const char* a, const char* b) const { return strcmp(a, b) < 0; }
345  };
346 
348 
365  vtkSetFilePathMacro(FileNamePrefixForDebugging);
366  vtkGetFilePathMacro(FileNamePrefixForDebugging);
368 
370 
376  {
379  UserGroup, // always will be last
380  };
384 
385  // returns the location for a uniform or attribute in
386  // this program. Is cached for performance.
387  int FindUniform(const char* name);
388  int FindAttributeArray(const char* name);
389 
390 protected:
392  ~vtkShaderProgram() override;
393 
394  /***************************************************************
395  * The following functions are only for use by the shader cache
396  * which is why they are protected and that class is a friend
397  * you need to use the shader cache to compile/link/bind your shader
398  * do not try to do it yourself as it will screw up the cache
399  ***************************************************************/
400  friend class vtkOpenGLShaderCache;
401 
408  bool AttachShader(const vtkShader* shader);
409 
415  bool DetachShader(const vtkShader* shader);
416 
420  virtual int CompileShader();
421 
427  bool Link();
428 
433  bool Bind();
434 
436  void Release();
437 
438  /************* end **************************************/
439 
444 
445  // hash of the shader program
447 
449  const char* name, void* buffer, int type, int tupleSize, NormalizeOption normalize);
450  int Handle;
454 
455  bool Linked;
456  bool Bound;
457  bool Compiled;
458 
459  // for glsl 1.5 or later, how many outputs
460  // does this shader create
461  // they will be bound in order to
462  // fragOutput0 fragOutput1 etc...
463  unsigned int NumberOfOutputs;
464 
466 
467  // since we are using const char * arrays we have to
468  // free our memory :-)
469  void ClearMaps();
470  std::map<const char*, int, cmp_str> AttributeLocs;
471  std::map<const char*, int, cmp_str> UniformLocs;
472 
473  std::map<int, vtkMTimeType> UniformGroupMTimes;
474 
475  friend class VertexArrayObject;
476 
477 private:
478  vtkShaderProgram(const vtkShaderProgram&) = delete;
479  void operator=(const vtkShaderProgram&) = delete;
480 
481  char* FileNamePrefixForDebugging;
482 };
483 
484 VTK_ABI_NAMESPACE_END
485 #endif
a simple class to control print indentation
Definition: vtkIndent.h:120
represent and manipulate 3x3 transformation matrices
Definition: vtkMatrix3x3.h:67
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:152
abstract base class for most VTK objects
Definition: vtkObject.h:83
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, const int count, const float *f)
void Release()
Releases the shader program from the current context.
bool SetUniform4fv(const char *name, const int count, const float(*f)[4])
vtkShader * FragmentShader
~vtkShaderProgram() override
void SetFragmentShader(vtkShader *)
Get the fragment shader for this program.
std::string GetError() const
Get the error message (empty if none) for the shader program.
bool SetUniform2fv(const char *name, const int count, const float *f)
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 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 SetUniform2f(const char *name, const float v[2])
bool SetUniformMatrix4x4v(const char *name, const int count, float *v)
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 SetUniform1iv(const char *name, const int count, const int *f)
Set the name uniform array to f with count elements.
bool AttachShader(const vtkShader *shader)
Attach the supplied shader to this program.
int FindUniform(const char *name)
bool SetAttributeArrayInternal(const char *name, void *buffer, int type, int tupleSize, NormalizeOption normalize)
int GetHandle() const
Get the handle of the shader program.
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
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 SetUniform3fv(const char *name, const int count, const float *f)
bool SetUniformf(const char *name, float v)
bool SetUniformMatrix4x4(const char *name, float *v)
bool SetUniform2fv(const char *name, const int count, const float(*f)[2])
vtkShader * GeometryShader
bool SetUniformMatrix3x3(const char *name, float *v)
bool DisableAttributeArray(const char *name)
Disable the named attribute array.
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 SetUniform1fv(const char *name, const int count, const float *f)
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 the geometry shader for this program.
std::map< int, vtkMTimeType > UniformGroupMTimes
bool SetUniform3fv(const char *name, const int count, const float(*f)[3])
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...
Vertex or Fragment shader, combined into a ShaderProgram.
Definition: vtkShader.h:39
Manages a TransformFeedback buffer.
window superclass for vtkRenderWindow
Definition: vtkWindow.h:40
@ type
Definition: vtkX3D.h:528
@ name
Definition: vtkX3D.h:231
@ offset
Definition: vtkX3D.h:450
@ string
Definition: vtkX3D.h:502
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:282