VTK
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 =========================================================================*/
24 #ifndef vtkShaderProgram_h
25 #define vtkShaderProgram_h
26 
27 #include "vtkRenderingOpenGL2Module.h" // for export macro
28 #include "vtkObject.h"
29 
30 #include <string> // For member variables.
31 #include <map> // For member variables.
32 
33 class vtkMatrix3x3;
34 class vtkMatrix4x4;
36 class vtkShader;
37 class VertexArrayObject;
38 class vtkWindow;
39 
47 class VTKRENDERINGOPENGL2_EXPORT vtkShaderProgram : public vtkObject
48 {
49 public:
50  static vtkShaderProgram *New();
51  vtkTypeMacro(vtkShaderProgram, vtkObject);
52  void PrintSelf(ostream& os, vtkIndent indent) override;
53 
55 
58  vtkGetObjectMacro(VertexShader, vtkShader);
61 
63 
66  vtkGetObjectMacro(FragmentShader, vtkShader);
69 
71 
74  vtkGetObjectMacro(GeometryShader, vtkShader);
77 
79 
82  vtkGetObjectMacro(TransformFeedback, vtkTransformFeedback);
85 
87 
90  vtkGetMacro(Compiled, bool);
91  vtkSetMacro(Compiled, bool);
92  vtkBooleanMacro(Compiled, bool);
94 
98  std::string GetMD5Hash() const { return this->MD5Hash; }
99  void SetMD5Hash(const std::string &hash) { this->MD5Hash = hash; }
100 
101 
114  NoNormalize
115  };
116 
117 
122  bool isBound() const { return this->Bound; }
123 
128 
130  int GetHandle() const { return Handle; }
131 
133  std::string GetError() const { return Error; }
134 
139  bool EnableAttributeArray(const char *name);
140 
145  bool DisableAttributeArray(const char *name);
146 
162  bool UseAttributeArray(const char *name, int offset, size_t stride,
163  int elementType, int elementTupleSize,
164  NormalizeOption normalize);
165 
183  template <class T>
184  bool SetAttributeArray(const char *name, const T &array,
185  int tupleSize, NormalizeOption normalize);
186 
188  bool SetUniformi(const char *name, int v);
189  bool SetUniformf(const char *name, float v);
190  bool SetUniform2i(const char *name, const int v[2]);
191  bool SetUniform2f(const char *name, const float v[2]);
192  bool SetUniform3f(const char *name, const float v[3]);
193  bool SetUniform3f(const char *name, const double v[3]);
194  bool SetUniform4f(const char *name, const float v[4]);
195  bool SetUniform3uc(const char *name, const unsigned char v[3]); // maybe remove
196  bool SetUniform4uc(const char *name, const unsigned char v[4]); // maybe remove
197  bool SetUniformMatrix(const char *name, vtkMatrix3x3 *v);
198  bool SetUniformMatrix(const char *name, vtkMatrix4x4 *v);
199  bool SetUniformMatrix3x3(const char *name, float *v);
200  bool SetUniformMatrix4x4(const char *name, float *v);
201 
203  bool SetUniform1iv(const char *name, const int count, const int *f);
204  bool SetUniform1fv(const char *name, const int count, const float *f);
205  bool SetUniform2fv(const char *name, const int count, const float (*f)[2]);
206  bool SetUniform3fv(const char *name, const int count, const float (*f)[3]);
207  bool SetUniform4fv(const char *name, const int count, const float (*f)[4]);
208  bool SetUniformMatrix4x4v(const char *name, const int count, float *v);
209 
210  // How many outputs does this program produce
211  // only valid for OpenGL 3.2 or later
212  vtkSetMacro(NumberOfOutputs,unsigned int);
213 
225  static bool Substitute(
227  const std::string &search,
228  const std::string &replace,
229  bool all = true);
230 
242  static bool Substitute(
243  vtkShader* shader,
244  const std::string &search,
245  const std::string &replace,
246  bool all = true);
247 
253  bool IsUniformUsed(const char *);
254 
259  bool IsAttributeUsed(const char *name);
260 
261  // maps of std::string are super slow when calling find
262  // with a string literal or const char * as find
263  // forces construction/copy/destruction of a
264  // std::string copy of the const char *
265  // In spite of the doubters this can really be a
266  // huge CPU hog.
267  struct cmp_str
268  {
269  bool operator()(const char *a, const char *b) const
270  {
271  return strcmp(a, b) < 0;
272  }
273  };
274 
276 
293  vtkSetStringMacro(FileNamePrefixForDebugging);
294  vtkGetStringMacro(FileNamePrefixForDebugging);
296 
298 
306  UserGroup, // always will be last
307  };
311 
312  // returns the location for a uniform or attribute in
313  // this program. Is cached for performance.
314  int FindUniform(const char *name);
315  int FindAttributeArray(const char *name);
316 
317 protected:
319  ~vtkShaderProgram() override;
320 
321  /***************************************************************
322  * The following functions are only for use by the shader cache
323  * which is why they are protected and that class is a friend
324  * you need to use the shader cache to compile/link/bind your shader
325  * do not try to do it yourself as it will screw up the cache
326  ***************************************************************/
327  friend class vtkOpenGLShaderCache;
328 
335  bool AttachShader(const vtkShader *shader);
336 
342  bool DetachShader(const vtkShader *shader);
343 
347  virtual int CompileShader();
348 
354  bool Link();
355 
360  bool Bind();
361 
363  void Release();
364 
365  /************* end **************************************/
366 
371 
372  // hash of the shader program
374 
375  bool SetAttributeArrayInternal(const char *name, void *buffer,
376  int type, int tupleSize,
377  NormalizeOption normalize);
378  int Handle;
382 
383  bool Linked;
384  bool Bound;
385  bool Compiled;
386 
387  // for glsl 1.5 or later, how many outputs
388  // does this shader create
389  // they will be bound in order to
390  // fragOutput0 fragOutput1 etc...
391  unsigned int NumberOfOutputs;
392 
394 
395  // since we are using const char * arrays we have to
396  // free our memory :-)
397  void ClearMaps();
398  std::map<const char *, int, cmp_str> AttributeLocs;
399  std::map<const char *, int, cmp_str> UniformLocs;
400 
401  std::map<int, vtkMTimeType> UniformGroupMTimes;
402 
403  friend class VertexArrayObject;
404 
405 private:
406  vtkShaderProgram(const vtkShaderProgram&) = delete;
407  void operator=(const vtkShaderProgram&) = delete;
408 
409  char* FileNamePrefixForDebugging;
410 };
411 
412 
413 #endif
vtkShaderProgram::SetUniform2fv
bool SetUniform2fv(const char *name, const int count, const float(*f)[2])
vtkShaderProgram::SetUniform3f
bool SetUniform3f(const char *name, const double v[3])
vtkShaderProgram::~vtkShaderProgram
~vtkShaderProgram() override
vtkShaderProgram::GeometryShaderHandle
int GeometryShaderHandle
Definition: vtkShaderProgram.h:381
vtkShaderProgram::CompileShader
virtual int CompileShader()
Compile this shader program and attached shaders.
vtkShaderProgram::Compiled
bool Compiled
Definition: vtkShaderProgram.h:385
vtkShaderProgram::SetUniformGroupUpdateTime
void SetUniformGroupUpdateTime(int, vtkMTimeType tm)
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:98
vtkShaderProgram::UniformGroupMTimes
std::map< int, vtkMTimeType > UniformGroupMTimes
Definition: vtkShaderProgram.h:401
vtkShaderProgram::FragmentShaderHandle
int FragmentShaderHandle
Definition: vtkShaderProgram.h:380
vtkShaderProgram::SetUniformMatrix4x4
bool SetUniformMatrix4x4(const char *name, float *v)
vtkX3D::type
@ type
Definition: vtkX3D.h:516
vtkShaderProgram::NormalizeOption
NormalizeOption
Options for attribute normalization.
Definition: vtkShaderProgram.h:103
vtkShaderProgram::Link
bool Link()
Attempt to link the shader program.
vtkShaderProgram::SetUniform4f
bool SetUniform4f(const char *name, const float v[4])
vtkShaderProgram::Bound
bool Bound
Definition: vtkShaderProgram.h:384
vtkShaderProgram::GetUniformGroupUpdateTime
vtkMTimeType GetUniformGroupUpdateTime(int)
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:368
vtkShaderProgram::cmp_str
Definition: vtkShaderProgram.h:268
vtkShaderProgram::CameraGroup
@ CameraGroup
Definition: vtkShaderProgram.h:304
vtkShaderProgram::GetError
std::string GetError() const
Get the error message (empty if none) for the shader program.
Definition: vtkShaderProgram.h:133
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:60
vtkShaderProgram::SetTransformFeedback
void SetTransformFeedback(vtkTransformFeedback *tfc)
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.
vtkShaderProgram::AttributeLocs
std::map< const char *, int, cmp_str > AttributeLocs
Definition: vtkShaderProgram.h:398
vtkMatrix3x3
represent and manipulate 3x3 transformation matrices
Definition: vtkMatrix3x3.h:37
vtkShaderProgram::Handle
int Handle
Definition: vtkShaderProgram.h:378
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:38
vtkShaderProgram::SetAttributeArrayInternal
bool SetAttributeArrayInternal(const char *name, void *buffer, int type, int tupleSize, NormalizeOption normalize)
vtkShaderProgram::Linked
bool Linked
Definition: vtkShaderProgram.h:383
source
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
Definition: vtkBoostGraphAdapter.h:825
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 *)
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:438
vtkShaderProgram
The ShaderProgram uses one or more Shader objects.
Definition: vtkShaderProgram.h:48
vtkShaderProgram::SetVertexShader
void SetVertexShader(vtkShader *)
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:40
vtkMatrix4x4
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:42
vtkShaderProgram::UserGroup
@ UserGroup
Definition: vtkShaderProgram.h:306
vtkShaderProgram::UniformGroups
UniformGroups
Set/Get times that can be used to track when a set of uniforms was last updated.
Definition: vtkShaderProgram.h:303
vtkShaderProgram::Normalize
@ Normalize
The values range across the limits of the numeric type.
Definition: vtkShaderProgram.h:112
vtkShaderProgram::SetUniformi
bool SetUniformi(const char *name, int v)
Set the name uniform value to int v.
vtkShaderProgram::TransformFeedback
vtkTransformFeedback * TransformFeedback
Definition: vtkShaderProgram.h:370
vtkShaderProgram::isBound
bool isBound() const
Check if the program is currently bound, or not.
Definition: vtkShaderProgram.h:122
vtkShader
Vertex or Fragment shader, combined into a ShaderProgram.
Definition: vtkShader.h:41
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:130
vtkX3D::name
@ name
Definition: vtkX3D.h:219
vtkShaderProgram::LightingGroup
@ LightingGroup
Definition: vtkShaderProgram.h:305
vtkObject.h
vtkShaderProgram::cmp_str::operator()
bool operator()(const char *a, const char *b) const
Definition: vtkShaderProgram.h:269
vtkShaderProgram::NumberOfOutputs
unsigned int NumberOfOutputs
Definition: vtkShaderProgram.h:391
vtkX3D::string
@ string
Definition: vtkX3D.h:490
vtkShaderProgram::Error
std::string Error
Definition: vtkShaderProgram.h:393
vtkShaderProgram::SetUniformMatrix4x4v
bool SetUniformMatrix4x4v(const char *name, const int count, float *v)
vtkShaderProgram::SetFragmentShader
void SetFragmentShader(vtkShader *)
vtkShaderProgram::EnableAttributeArray
bool EnableAttributeArray(const char *name)
Enable the named attribute array.
vtkShaderProgram::SetMD5Hash
void SetMD5Hash(const std::string &hash)
Definition: vtkShaderProgram.h:99
vtkShaderProgram::SetUniform2f
bool SetUniform2f(const char *name, const float v[2])
vtkShaderProgram::MD5Hash
std::string MD5Hash
Definition: vtkShaderProgram.h:373
vtkShaderProgram::SetUniform3fv
bool SetUniform3fv(const char *name, const int count, const float(*f)[3])
vtkShaderProgram::UniformLocs
std::map< const char *, int, cmp_str > UniformLocs
Definition: vtkShaderProgram.h:399
vtkShaderProgram::SetUniformMatrix3x3
bool SetUniformMatrix3x3(const char *name, float *v)
vtkShaderProgram::VertexShader
vtkShader * VertexShader
Definition: vtkShaderProgram.h:367
vtkShaderProgram::GeometryShader
vtkShader * GeometryShader
Definition: vtkShaderProgram.h:369
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:379
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:302
vtkTransformFeedback
Manages a TransformFeedback buffer.
Definition: vtkTransformFeedback.h:41
vtkShaderProgram::FindAttributeArray
int FindAttributeArray(const char *name)