VTK  9.1.0
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 =========================================================================*/
100 #ifndef vtkShaderProgram_h
101 #define vtkShaderProgram_h
102 
103 #include "vtkObject.h"
104 #include "vtkRenderingOpenGL2Module.h" // for export macro
105 
106 #include <map> // For member variables.
107 #include <string> // For member variables.
108 
109 class vtkMatrix3x3;
110 class vtkMatrix4x4;
112 class vtkShader;
113 class VertexArrayObject;
114 class vtkWindow;
115 
123 class VTKRENDERINGOPENGL2_EXPORT vtkShaderProgram : public vtkObject
124 {
125 public:
127  vtkTypeMacro(vtkShaderProgram, vtkObject);
128  void PrintSelf(ostream& os, vtkIndent indent) override;
129 
131 
134  vtkGetObjectMacro(VertexShader, vtkShader);
137 
139 
142  vtkGetObjectMacro(FragmentShader, vtkShader);
145 
147 
150  vtkGetObjectMacro(GeometryShader, vtkShader);
153 
155 
158  vtkGetObjectMacro(TransformFeedback, vtkTransformFeedback);
161 
163 
166  vtkGetMacro(Compiled, bool);
167  vtkSetMacro(Compiled, bool);
168  vtkBooleanMacro(Compiled, bool);
170 
174  std::string GetMD5Hash() const { return this->MD5Hash; }
175  void SetMD5Hash(const std::string& hash) { this->MD5Hash = hash; }
176 
179  {
190  NoNormalize
191  };
192 
197  bool isBound() const { return this->Bound; }
198 
203 
205  int GetHandle() const { return Handle; }
206 
208  std::string GetError() const { return Error; }
209 
214  bool EnableAttributeArray(const char* name);
215 
220  bool DisableAttributeArray(const char* name);
221 
237  bool UseAttributeArray(const char* name, int offset, size_t stride, int elementType,
238  int elementTupleSize, NormalizeOption normalize);
239 
257  template <class T>
259  const char* name, const T& array, int tupleSize, NormalizeOption normalize);
260 
262  bool SetUniformi(const char* name, int v);
263  bool SetUniformf(const char* name, float v);
264  bool SetUniform2i(const char* name, const int v[2]);
265  bool SetUniform2f(const char* name, const float v[2]);
266  bool SetUniform3f(const char* name, const float v[3]);
267  bool SetUniform3f(const char* name, const double v[3]);
268  bool SetUniform4f(const char* name, const float v[4]);
269  bool SetUniform3uc(const char* name, const unsigned char v[3]); // maybe remove
270  bool SetUniform4uc(const char* name, const unsigned char v[4]); // maybe remove
271  bool SetUniformMatrix(const char* name, vtkMatrix3x3* v);
272  bool SetUniformMatrix(const char* name, vtkMatrix4x4* v);
273  bool SetUniformMatrix3x3(const char* name, float* v);
274  bool SetUniformMatrix4x4(const char* name, float* v);
275 
277  bool SetUniform1iv(const char* name, const int count, const int* f);
278  bool SetUniform1fv(const char* name, const int count, const float* f);
279  bool SetUniform2fv(const char* name, const int count, const float* f);
280  bool SetUniform2fv(const char* name, const int count, const float (*f)[2]);
281  bool SetUniform3fv(const char* name, const int count, const float* f);
282  bool SetUniform3fv(const char* name, const int count, const float (*f)[3]);
283  bool SetUniform4fv(const char* name, const int count, const float* f);
284  bool SetUniform4fv(const char* name, const int count, const float (*f)[4]);
285  bool SetUniformMatrix4x4v(const char* name, const int count, float* v);
286 
287  // How many outputs does this program produce
288  // only valid for OpenGL 3.2 or later
289  vtkSetMacro(NumberOfOutputs, unsigned int);
290 
302  static bool Substitute(
303  std::string& source, const std::string& search, const std::string& replace, bool all = true);
304 
316  static bool Substitute(
317  vtkShader* shader, const std::string& search, const std::string& replace, bool all = true);
318 
324  bool IsUniformUsed(const char*);
325 
330  bool IsAttributeUsed(const char* name);
331 
332  // maps of std::string are super slow when calling find
333  // with a string literal or const char * as find
334  // forces construction/copy/destruction of a
335  // std::string copy of the const char *
336  // In spite of the doubters this can really be a
337  // huge CPU hog.
338  struct cmp_str
339  {
340  bool operator()(const char* a, const char* b) const { return strcmp(a, b) < 0; }
341  };
342 
344 
361  vtkSetFilePathMacro(FileNamePrefixForDebugging);
362  vtkGetFilePathMacro(FileNamePrefixForDebugging);
364 
366 
372  {
375  UserGroup, // always will be last
376  };
380 
381  // returns the location for a uniform or attribute in
382  // this program. Is cached for performance.
383  int FindUniform(const char* name);
384  int FindAttributeArray(const char* name);
385 
386 protected:
388  ~vtkShaderProgram() override;
389 
390  /***************************************************************
391  * The following functions are only for use by the shader cache
392  * which is why they are protected and that class is a friend
393  * you need to use the shader cache to compile/link/bind your shader
394  * do not try to do it yourself as it will screw up the cache
395  ***************************************************************/
396  friend class vtkOpenGLShaderCache;
397 
404  bool AttachShader(const vtkShader* shader);
405 
411  bool DetachShader(const vtkShader* shader);
412 
416  virtual int CompileShader();
417 
423  bool Link();
424 
429  bool Bind();
430 
432  void Release();
433 
434  /************* end **************************************/
435 
440 
441  // hash of the shader program
443 
445  const char* name, void* buffer, int type, int tupleSize, NormalizeOption normalize);
446  int Handle;
450 
451  bool Linked;
452  bool Bound;
453  bool Compiled;
454 
455  // for glsl 1.5 or later, how many outputs
456  // does this shader create
457  // they will be bound in order to
458  // fragOutput0 fragOutput1 etc...
459  unsigned int NumberOfOutputs;
460 
462 
463  // since we are using const char * arrays we have to
464  // free our memory :-)
465  void ClearMaps();
466  std::map<const char*, int, cmp_str> AttributeLocs;
467  std::map<const char*, int, cmp_str> UniformLocs;
468 
469  std::map<int, vtkMTimeType> UniformGroupMTimes;
470 
471  friend class VertexArrayObject;
472 
473 private:
474  vtkShaderProgram(const vtkShaderProgram&) = delete;
475  void operator=(const vtkShaderProgram&) = delete;
476 
477  char* FileNamePrefixForDebugging;
478 };
479 
480 #endif
vtkShaderProgram::SetUniform2fv
bool SetUniform2fv(const char *name, const int count, const float(*f)[2])
vtkShaderProgram::SetUniform2fv
bool SetUniform2fv(const char *name, const int count, const float *f)
vtkShaderProgram::SetUniform3f
bool SetUniform3f(const char *name, const double v[3])
vtkShaderProgram::~vtkShaderProgram
~vtkShaderProgram() override
vtkShaderProgram::GeometryShaderHandle
int GeometryShaderHandle
Definition: vtkShaderProgram.h:449
vtkShaderProgram::CompileShader
virtual int CompileShader()
Compile this shader program and attached shaders.
vtkShaderProgram::Compiled
bool Compiled
Definition: vtkShaderProgram.h:453
vtkShaderProgram::SetUniformGroupUpdateTime
void SetUniformGroupUpdateTime(int, vtkMTimeType tm)
Set/Get times that can be used to track when a set of uniforms was last updated.
vtkShaderProgram::Release
void Release()
Releases the shader program from the current context.
vtkShaderProgram::FindUniform
int FindUniform(const char *name)
vtkShaderProgram::GetMD5Hash
std::string GetMD5Hash() const
Set/Get the md5 hash of this program.
Definition: vtkShaderProgram.h:174
vtkShaderProgram::UniformGroupMTimes
std::map< int, vtkMTimeType > UniformGroupMTimes
Definition: vtkShaderProgram.h:469
vtkShaderProgram::FragmentShaderHandle
int FragmentShaderHandle
Definition: vtkShaderProgram.h:448
vtkShaderProgram::SetUniformMatrix4x4
bool SetUniformMatrix4x4(const char *name, float *v)
vtkX3D::type
@ type
Definition: vtkX3D.h:522
vtkShaderProgram::NormalizeOption
NormalizeOption
Options for attribute normalization.
Definition: vtkShaderProgram.h:179
vtkShaderProgram::Link
bool Link()
Attempt to link the shader program.
vtkShaderProgram::SetUniform4f
bool SetUniform4f(const char *name, const float v[4])
vtkShaderProgram::vtkSetFilePathMacro
vtkSetFilePathMacro(FileNamePrefixForDebugging)
When developing shaders, it's often convenient to tweak the shader and re-render incrementally.
vtkShaderProgram::Bound
bool Bound
Definition: vtkShaderProgram.h:452
vtkShaderProgram::GetUniformGroupUpdateTime
vtkMTimeType GetUniformGroupUpdateTime(int)
Set/Get times that can be used to track when a set of uniforms was last updated.
vtkShaderProgram::SetAttributeArray
bool SetAttributeArray(const char *name, const T &array, int tupleSize, NormalizeOption normalize)
Upload the supplied array of tightly packed values to the named attribute.
vtkShaderProgram::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkShaderProgram::Substitute
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...
vtkShaderProgram::FragmentShader
vtkShader * FragmentShader
Definition: vtkShaderProgram.h:437
vtkShaderProgram::cmp_str
Definition: vtkShaderProgram.h:339
vtkShaderProgram::CameraGroup
@ CameraGroup
Definition: vtkShaderProgram.h:373
vtkShaderProgram::GetError
std::string GetError() const
Get the error message (empty if none) for the shader program.
Definition: vtkShaderProgram.h:208
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:82
vtkShaderProgram::SetTransformFeedback
void SetTransformFeedback(vtkTransformFeedback *tfc)
Get/Set a TransformFeedbackCapture object on this shader program.
vtkShaderProgram::vtkGetFilePathMacro
vtkGetFilePathMacro(FileNamePrefixForDebugging)
When developing shaders, it's often convenient to tweak the shader and re-render incrementally.
vtkShaderProgram::vtkShaderProgram
vtkShaderProgram()
vtkShaderProgram::SetUniformf
bool SetUniformf(const char *name, float v)
vtkShaderProgram::New
static vtkShaderProgram * New()
vtkShaderProgram::UseAttributeArray
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.
vtkMatrix3x3
represent and manipulate 3x3 transformation matrices
Definition: vtkMatrix3x3.h:63
vtkShaderProgram::Handle
int Handle
Definition: vtkShaderProgram.h:446
vtkShaderProgram::IsAttributeUsed
bool IsAttributeUsed(const char *name)
Return true if the compiled and linked shader has an attribute matching name.
vtkShaderProgram::Bind
bool Bind()
Bind the program in order to use it.
vtkShaderProgram::DisableAttributeArray
bool DisableAttributeArray(const char *name)
Disable the named attribute array.
vtkWindow
window superclass for vtkRenderWindow
Definition: vtkWindow.h:39
vtkShaderProgram::UniformLocs
std::map< const char *, int, cmp_str > UniformLocs
Definition: vtkShaderProgram.h:467
vtkShaderProgram::SetAttributeArrayInternal
bool SetAttributeArrayInternal(const char *name, void *buffer, int type, int tupleSize, NormalizeOption normalize)
vtkShaderProgram::Linked
bool Linked
Definition: vtkShaderProgram.h:451
vtkShaderProgram::ClearMaps
void ClearMaps()
vtkShaderProgram::SetUniform1fv
bool SetUniform1fv(const char *name, const int count, const float *f)
vtkShaderProgram::DetachShader
bool DetachShader(const vtkShader *shader)
Detach the supplied shader from this program.
vtkShaderProgram::SetGeometryShader
void SetGeometryShader(vtkShader *)
Get the geometry shader for this program.
vtkShaderProgram::SetUniform2i
bool SetUniform2i(const char *name, const int v[2])
vtkShaderProgram::SetUniformMatrix
bool SetUniformMatrix(const char *name, vtkMatrix4x4 *v)
vtkX3D::offset
@ offset
Definition: vtkX3D.h:444
vtkShaderProgram
The ShaderProgram uses one or more Shader objects.
Definition: vtkShaderProgram.h:124
vtkShaderProgram::SetVertexShader
void SetVertexShader(vtkShader *)
Get the vertex shader for this program.
vtkShaderProgram::SetUniform4fv
bool SetUniform4fv(const char *name, const int count, const float *f)
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:113
vtkMatrix4x4
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:145
vtkShaderProgram::AttributeLocs
std::map< const char *, int, cmp_str > AttributeLocs
Definition: vtkShaderProgram.h:466
vtkShaderProgram::UserGroup
@ UserGroup
Definition: vtkShaderProgram.h:375
vtkShaderProgram::UniformGroups
UniformGroups
Set/Get times that can be used to track when a set of uniforms was last updated.
Definition: vtkShaderProgram.h:372
vtkShaderProgram::Normalize
@ Normalize
The values range across the limits of the numeric type.
Definition: vtkShaderProgram.h:188
vtkShaderProgram::SetUniformi
bool SetUniformi(const char *name, int v)
Set the name uniform value to int v.
vtkShaderProgram::TransformFeedback
vtkTransformFeedback * TransformFeedback
Definition: vtkShaderProgram.h:439
vtkShaderProgram::isBound
bool isBound() const
Check if the program is currently bound, or not.
Definition: vtkShaderProgram.h:197
vtkShader
Vertex or Fragment shader, combined into a ShaderProgram.
Definition: vtkShader.h:38
vtkShaderProgram::SetUniform4fv
bool SetUniform4fv(const char *name, const int count, const float(*f)[4])
vtkShaderProgram::SetUniform3uc
bool SetUniform3uc(const char *name, const unsigned char v[3])
vtkShaderProgram::IsUniformUsed
bool IsUniformUsed(const char *)
methods to inquire as to what uniforms/attributes are used by this shader.
vtkShaderProgram::Substitute
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...
vtkOpenGLShaderCache
manage Shader Programs within a context
Definition: vtkOpenGLShaderCache.h:36
vtkShaderProgram::SetUniformMatrix
bool SetUniformMatrix(const char *name, vtkMatrix3x3 *v)
vtkShaderProgram::GetHandle
int GetHandle() const
Get the handle of the shader program.
Definition: vtkShaderProgram.h:205
vtkX3D::name
@ name
Definition: vtkX3D.h:225
vtkShaderProgram::LightingGroup
@ LightingGroup
Definition: vtkShaderProgram.h:374
vtkObject.h
vtkShaderProgram::cmp_str::operator()
bool operator()(const char *a, const char *b) const
Definition: vtkShaderProgram.h:340
vtkShaderProgram::SetUniform3fv
bool SetUniform3fv(const char *name, const int count, const float *f)
vtkShaderProgram::NumberOfOutputs
unsigned int NumberOfOutputs
Definition: vtkShaderProgram.h:459
vtkX3D::string
@ string
Definition: vtkX3D.h:496
vtkShaderProgram::Error
std::string Error
Definition: vtkShaderProgram.h:461
vtkShaderProgram::SetUniformMatrix4x4v
bool SetUniformMatrix4x4v(const char *name, const int count, float *v)
vtkShaderProgram::SetFragmentShader
void SetFragmentShader(vtkShader *)
Get the fragment shader for this program.
vtkShaderProgram::EnableAttributeArray
bool EnableAttributeArray(const char *name)
Enable the named attribute array.
vtkShaderProgram::SetMD5Hash
void SetMD5Hash(const std::string &hash)
Definition: vtkShaderProgram.h:175
vtkShaderProgram::SetUniform2f
bool SetUniform2f(const char *name, const float v[2])
vtkShaderProgram::MD5Hash
std::string MD5Hash
Definition: vtkShaderProgram.h:442
vtkShaderProgram::SetUniform3fv
bool SetUniform3fv(const char *name, const int count, const float(*f)[3])
vtkShaderProgram::SetUniformMatrix3x3
bool SetUniformMatrix3x3(const char *name, float *v)
vtkShaderProgram::VertexShader
vtkShader * VertexShader
Definition: vtkShaderProgram.h:436
source
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
Definition: vtkBoostGraphAdapter.h:998
vtkShaderProgram::GeometryShader
vtkShader * GeometryShader
Definition: vtkShaderProgram.h:438
vtkShaderProgram::AttachShader
bool AttachShader(const vtkShader *shader)
Attach the supplied shader to this program.
vtkShaderProgram::SetUniform4uc
bool SetUniform4uc(const char *name, const unsigned char v[4])
vtkShaderProgram::SetUniform3f
bool SetUniform3f(const char *name, const float v[3])
vtkShaderProgram::ReleaseGraphicsResources
void ReleaseGraphicsResources(vtkWindow *win)
release any graphics resources this class is using.
vtkShaderProgram::VertexShaderHandle
int VertexShaderHandle
Definition: vtkShaderProgram.h:447
vtkShaderProgram::SetUniform1iv
bool SetUniform1iv(const char *name, const int count, const int *f)
Set the name uniform array to f with count elements.
vtkMTimeType
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:287
vtkTransformFeedback
Manages a TransformFeedback buffer.
Definition: vtkTransformFeedback.h:41
vtkShaderProgram::FindAttributeArray
int FindAttributeArray(const char *name)