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 =========================================================================*/
21 #ifndef vtkShaderProgram_h
22 #define vtkShaderProgram_h
23 
24 #include "vtkObject.h"
25 #include "vtkRenderingOpenGL2Module.h" // for export macro
26 
27 #include <map> // For member variables.
28 #include <string> // For member variables.
29 
30 class vtkMatrix3x3;
31 class vtkMatrix4x4;
33 class vtkShader;
34 class VertexArrayObject;
35 class vtkWindow;
36 
44 class VTKRENDERINGOPENGL2_EXPORT vtkShaderProgram : public vtkObject
45 {
46 public:
47  static vtkShaderProgram* New();
48  vtkTypeMacro(vtkShaderProgram, vtkObject);
49  void PrintSelf(ostream& os, vtkIndent indent) override;
50 
52 
55  vtkGetObjectMacro(VertexShader, vtkShader);
56  void SetVertexShader(vtkShader*);
58 
60 
63  vtkGetObjectMacro(FragmentShader, vtkShader);
64  void SetFragmentShader(vtkShader*);
66 
68 
71  vtkGetObjectMacro(GeometryShader, vtkShader);
72  void SetGeometryShader(vtkShader*);
74 
76 
79  vtkGetObjectMacro(TransformFeedback, vtkTransformFeedback);
80  void SetTransformFeedback(vtkTransformFeedback* tfc);
82 
84 
87  vtkGetMacro(Compiled, bool);
88  vtkSetMacro(Compiled, bool);
89  vtkBooleanMacro(Compiled, bool);
91 
95  std::string GetMD5Hash() const { return this->MD5Hash; }
96  void SetMD5Hash(const std::string& hash) { this->MD5Hash = hash; }
97 
100  {
111  NoNormalize
112  };
113 
118  bool isBound() const { return this->Bound; }
119 
123  void ReleaseGraphicsResources(vtkWindow* win);
124 
126  int GetHandle() const { return Handle; }
127 
129  std::string GetError() const { return Error; }
130 
135  bool EnableAttributeArray(const char* name);
136 
141  bool DisableAttributeArray(const char* name);
142 
158  bool UseAttributeArray(const char* name, int offset, size_t stride, int elementType,
159  int elementTupleSize, NormalizeOption normalize);
160 
178  template <class T>
179  bool SetAttributeArray(
180  const char* name, const T& array, int tupleSize, NormalizeOption normalize);
181 
183  bool SetUniformi(const char* name, int v);
184  bool SetUniformf(const char* name, float v);
185  bool SetUniform2i(const char* name, const int v[2]);
186  bool SetUniform2f(const char* name, const float v[2]);
187  bool SetUniform3f(const char* name, const float v[3]);
188  bool SetUniform3f(const char* name, const double v[3]);
189  bool SetUniform4f(const char* name, const float v[4]);
190  bool SetUniform3uc(const char* name, const unsigned char v[3]); // maybe remove
191  bool SetUniform4uc(const char* name, const unsigned char v[4]); // maybe remove
192  bool SetUniformMatrix(const char* name, vtkMatrix3x3* v);
193  bool SetUniformMatrix(const char* name, vtkMatrix4x4* v);
194  bool SetUniformMatrix3x3(const char* name, float* v);
195  bool SetUniformMatrix4x4(const char* name, float* v);
196 
198  bool SetUniform1iv(const char* name, const int count, const int* f);
199  bool SetUniform1fv(const char* name, const int count, const float* f);
200  bool SetUniform2fv(const char* name, const int count, const float* f);
201  bool SetUniform2fv(const char* name, const int count, const float (*f)[2]);
202  bool SetUniform3fv(const char* name, const int count, const float* f);
203  bool SetUniform3fv(const char* name, const int count, const float (*f)[3]);
204  bool SetUniform4fv(const char* name, const int count, const float* f);
205  bool SetUniform4fv(const char* name, const int count, const float (*f)[4]);
206  bool SetUniformMatrix4x4v(const char* name, const int count, float* v);
207 
208  // How many outputs does this program produce
209  // only valid for OpenGL 3.2 or later
210  vtkSetMacro(NumberOfOutputs, unsigned int);
211 
223  static bool Substitute(
224  std::string& source, const std::string& search, const std::string& replace, bool all = true);
225 
237  static bool Substitute(
238  vtkShader* shader, const std::string& search, const std::string& replace, bool all = true);
239 
245  bool IsUniformUsed(const char*);
246 
251  bool IsAttributeUsed(const char* name);
252 
253  // maps of std::string are super slow when calling find
254  // with a string literal or const char * as find
255  // forces construction/copy/destruction of a
256  // std::string copy of the const char *
257  // In spite of the doubters this can really be a
258  // huge CPU hog.
259  struct cmp_str
260  {
261  bool operator()(const char* a, const char* b) const { return strcmp(a, b) < 0; }
262  };
263 
265 
282  vtkSetStringMacro(FileNamePrefixForDebugging);
283  vtkGetStringMacro(FileNamePrefixForDebugging);
285 
287 
293  {
296  UserGroup, // always will be last
297  };
298  void SetUniformGroupUpdateTime(int, vtkMTimeType tm);
299  vtkMTimeType GetUniformGroupUpdateTime(int);
301 
302  // returns the location for a uniform or attribute in
303  // this program. Is cached for performance.
304  int FindUniform(const char* name);
305  int FindAttributeArray(const char* name);
306 
307 protected:
309  ~vtkShaderProgram() override;
310 
311  /***************************************************************
312  * The following functions are only for use by the shader cache
313  * which is why they are protected and that class is a friend
314  * you need to use the shader cache to compile/link/bind your shader
315  * do not try to do it yourself as it will screw up the cache
316  ***************************************************************/
317  friend class vtkOpenGLShaderCache;
318 
325  bool AttachShader(const vtkShader* shader);
326 
332  bool DetachShader(const vtkShader* shader);
333 
337  virtual int CompileShader();
338 
344  bool Link();
345 
350  bool Bind();
351 
353  void Release();
354 
355  /************* end **************************************/
356 
357  vtkShader* VertexShader;
358  vtkShader* FragmentShader;
359  vtkShader* GeometryShader;
360  vtkTransformFeedback* TransformFeedback;
361 
362  // hash of the shader program
363  std::string MD5Hash;
364 
365  bool SetAttributeArrayInternal(
366  const char* name, void* buffer, int type, int tupleSize, NormalizeOption normalize);
367  int Handle;
368  int VertexShaderHandle;
369  int FragmentShaderHandle;
370  int GeometryShaderHandle;
371 
372  bool Linked;
373  bool Bound;
374  bool Compiled;
375 
376  // for glsl 1.5 or later, how many outputs
377  // does this shader create
378  // they will be bound in order to
379  // fragOutput0 fragOutput1 etc...
380  unsigned int NumberOfOutputs;
381 
382  std::string Error;
383 
384  // since we are using const char * arrays we have to
385  // free our memory :-)
386  void ClearMaps();
387  std::map<const char*, int, cmp_str> AttributeLocs;
388  std::map<const char*, int, cmp_str> UniformLocs;
389 
390  std::map<int, vtkMTimeType> UniformGroupMTimes;
391 
392  friend class VertexArrayObject;
393 
394 private:
395  vtkShaderProgram(const vtkShaderProgram&) = delete;
396  void operator=(const vtkShaderProgram&) = delete;
397 
398  char* FileNamePrefixForDebugging;
399 };
400 
401 #endif
vtkShaderProgram::GetMD5Hash
std::string GetMD5Hash() const
Set/Get the md5 hash of this program.
Definition: vtkShaderProgram.h:95
vtkX3D::type
Definition: vtkX3D.h:522
vtkShaderProgram::NormalizeOption
NormalizeOption
Options for attribute normalization.
Definition: vtkShaderProgram.h:99
vtkObject::New
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on.
vtkShaderProgram::cmp_str
Definition: vtkShaderProgram.h:259
vtkShaderProgram::CameraGroup
Definition: vtkShaderProgram.h:294
vtkShaderProgram::GetError
std::string GetError() const
Get the error message (empty if none) for the shader program.
Definition: vtkShaderProgram.h:129
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:53
vtkMatrix3x3
represent and manipulate 3x3 transformation matrices
Definition: vtkMatrix3x3.h:33
vtkEventDataAction::Release
vtkWindow
window superclass for vtkRenderWindow
Definition: vtkWindow.h:34
vtkX3D::offset
Definition: vtkX3D.h:444
vtkShaderProgram
The ShaderProgram uses one or more Shader objects.
Definition: vtkShaderProgram.h:44
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:33
vtkMatrix4x4
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:35
vtkShaderProgram::UserGroup
Definition: vtkShaderProgram.h:296
vtkShaderProgram::UniformGroups
UniformGroups
Set/Get times that can be used to track when a set of uniforms was last updated.
Definition: vtkShaderProgram.h:292
vtkShaderProgram::Normalize
The values range across the limits of the numeric type.
Definition: vtkShaderProgram.h:109
vtkShaderProgram::isBound
bool isBound() const
Check if the program is currently bound, or not.
Definition: vtkShaderProgram.h:118
vtkShader
Vertex or Fragment shader, combined into a ShaderProgram.
Definition: vtkShader.h:37
vtkObject::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkOpenGLShaderCache
manage Shader Programs within a context
Definition: vtkOpenGLShaderCache.h:35
vtkShaderProgram::GetHandle
int GetHandle() const
Get the handle of the shader program.
Definition: vtkShaderProgram.h:126
vtkX3D::name
Definition: vtkX3D.h:225
vtkShaderProgram::LightingGroup
Definition: vtkShaderProgram.h:295
vtkObject.h
vtkShaderProgram::cmp_str::operator()
bool operator()(const char *a, const char *b) const
Definition: vtkShaderProgram.h:261
vtkX3D::string
Definition: vtkX3D.h:496
vtkShaderProgram::SetMD5Hash
void SetMD5Hash(const std::string &hash)
Definition: vtkShaderProgram.h:96
source
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
Definition: vtkBoostGraphAdapter.h:959
vtkMTimeType
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:304
vtkTransformFeedback
Manages a TransformFeedback buffer.
Definition: vtkTransformFeedback.h:40