VTK
vtkShaderProgram2.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkShaderProgram2.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
28 #ifndef vtkShaderProgram2_h
29 #define vtkShaderProgram2_h
30 
31 #include <cassert> // for templated functions
32 #include "vtkRenderingOpenGLModule.h" // for export macro
33 #include "vtkWeakPointer.h" // for weak ptr to rendering context
34 #include "vtkObject.h"
35 
36 class vtkRenderWindow;
40 
41 // Values for GetLastBuildStatus()
43 {
44  // one of the shaders failed to compile
46  // all the shaders compiled successfully but the link failed
48  // all the shaders compiled successfully and the link succeeded
50 };
51 
53 {
59 };
60 
62 {
66 };
67 
68 class VTKRENDERINGOPENGL_EXPORT vtkShaderProgram2 : public vtkObject
69 {
70 public:
71  static vtkShaderProgram2* New();
72  vtkTypeMacro(vtkShaderProgram2, vtkObject);
73  void PrintSelf(ostream& os, vtkIndent indent) VTK_OVERRIDE;
74 
79  static bool IsSupported(vtkRenderWindow *context);
80 
82 
90  vtkGetMacro(PrintErrors,bool);
91  vtkSetMacro(PrintErrors,bool);
93 
95 
101  void SetContext(vtkRenderWindow *context);
102  vtkRenderWindow *GetContext();
104 
106 
110  vtkGetObjectMacro(Shaders,vtkShader2Collection);
112 
118  bool HasVertexShaders();
119 
123  bool HasTessellationControlShaders();
124 
128  bool HasTessellationEvaluationShaders();
129 
133  bool HasGeometryShaders();
134 
141  bool HasFragmentShaders();
142 
149  bool IsValid();
150 
158  void Build();
159 
166  void SendUniforms();
167 
174  void PrintActiveUniformVariables(ostream &os,
175  vtkIndent indent);
176 
183  void PrintActiveUniformVariablesOnCout();
184 
190  bool IsUsed();
191 
200  void Use();
201 
207  void Restore();
208 
216  void RestoreFixedPipeline();
217 
219 
223  void UseProgram();
224  void UnuseProgram();
226 
236  int GetLastBuildStatus();
237 
243  const char *GetLastLinkLog();
244 
250  const char *GetLastValidateLog();
251 
255  virtual void ReleaseGraphicsResources();
256 
265  int GetAttributeLocation(const char *name);
266 
268 
272  vtkGetObjectMacro(UniformVariables,vtkUniformVariables);
273  virtual void SetUniformVariables(vtkUniformVariables *variables);
275 
285  bool DisplayListUnderCreationInCompileMode();
286 
288 
301  vtkSetMacro(GeometryTypeIn,int);
302  vtkGetMacro(GeometryTypeIn,int);
304 
306 
318  vtkSetMacro(GeometryVerticesOut,int);
319  vtkGetMacro(GeometryVerticesOut,int);
321 
323 
334  vtkSetMacro(GeometryTypeOut,int);
335  vtkGetMacro(GeometryTypeOut,int);
337 
338  /*
339  Low level api --
340  this is provided as a way to avoid some of the overhead in this
341  class's implementation of SendUniforms. One should use the
342  following API if performance is a concern (eg. uniforms are
343  set per primitive), or if the uniform management is not needed
344  (eg. variables are already managed in other vtkObjects)
345  */
346 
351  int GetUniformLocation(const char *name);
352 
360  void SetUniformf(const char *name, float val)
361  { this->SetUniform1f(name, &val); }
362  void SetUniform1f(const char *name, float *val)
363  { this->SetUniform1f(this->GetUniformLocation(name), val); }
364  void SetUniform2f(const char *name, float *val)
365  { this->SetUniform2f(this->GetUniformLocation(name), val); }
366  void SetUniform3f(const char *name, float *val)
367  { this->SetUniform3f(this->GetUniformLocation(name), val); }
368  void SetUniform4f(const char *name, float *val)
369  { this->SetUniform4f(this->GetUniformLocation(name), val); }
370 
371  void SetUniformi(const char *name, int val)
372  { this->SetUniform1i(name, &val); }
373  void SetUniform1i(const char *name, int *val)
374  { this->SetUniform1i(this->GetUniformLocation(name), val); }
375  void SetUniform2i(const char *name, int *val)
376  { this->SetUniform2i(this->GetUniformLocation(name), val); }
377  void SetUniform3i(const char *name, int *val)
378  { this->SetUniform3i(this->GetUniformLocation(name), val); }
379  void SetUniform4i(const char *name, int *val)
380  { this->SetUniform4i(this->GetUniformLocation(name), val); }
381 
382  void SetUniformf(int loc, float val)
383  { this->SetUniform1f(loc, &val); }
384  void SetUniform1f(int loc, float *val);
385  void SetUniform2f(int loc, float *val);
386  void SetUniform3f(int loc, float *val);
387  void SetUniform4f(int loc, float *val);
388 
389  void SetUniformi(int loc, int val)
390  { this->SetUniform1i(loc, &val); }
391  void SetUniform1i(int loc, int *val);
392  void SetUniform2i(int loc, int *val);
393  void SetUniform3i(int loc, int *val);
394  void SetUniform4i(int loc, int *val);
395 
397 
402  template<typename T> void SetUniform1it(const char *name, T *value);
403  template<typename T> void SetUniform2it(const char *name, T *value);
404  template<typename T> void SetUniform3it(const char *name, T *value);
405  template<typename T> void SetUniform4it(const char *name, T *value);
407 
408  template<typename T> void SetUniform1ft(const char *name, T *value);
409  template<typename T> void SetUniform2ft(const char *name, T *value);
410  template<typename T> void SetUniform3ft(const char *name, T *value);
411  template<typename T> void SetUniform4ft(const char *name, T *value);
412 
413  template<typename T> void SetUniform1it(int loc, T *value);
414  template<typename T> void SetUniform2it(int loc, T *value);
415  template<typename T> void SetUniform3it(int loc, T *value);
416  template<typename T> void SetUniform4it(int loc, T *value);
417 
418  template<typename T> void SetUniform1ft(int loc, T *value);
419  template<typename T> void SetUniform2ft(int loc, T *value);
420  template<typename T> void SetUniform3ft(int loc, T *value);
421  template<typename T> void SetUniform4ft(int loc, T *value);
422 
423 protected:
425  ~vtkShaderProgram2() VTK_OVERRIDE;
426 
430  bool LoadRequiredExtensions(vtkRenderWindow *context);
431 
441  int GetUniformLocationInternal(const char *name);
442 
443  unsigned int Id; // actually GLuint. Initial value is 0.
444  unsigned int SavedId;
445 
448 
451 
452  int LastBuildStatus; // Initial value is VTK_SHADER_PROGRAM2_COMPILE_FAILED
453 
454  char *LastLinkLog; // Initial value is the empty string ""='\0'
455  size_t LastLinkLogCapacity; // Initial value is 8.
456 
457  char *LastValidateLog; // Initial value is the empty string ""='\0'
458  size_t LastValidateLogCapacity; // Initial value is 8.
459 
460 
462 
465 
469 
470 private:
471  vtkShaderProgram2(const vtkShaderProgram2&) VTK_DELETE_FUNCTION;
472  void operator=(const vtkShaderProgram2&) VTK_DELETE_FUNCTION;
473 };
474 
475 // ----------------------------------------------------------------------------
476 
477 #define vtkShaderProgram2SetUniformCopyCastMacro(toLetter, toType, num) \
478 template<typename fromType> \
479 void vtkShaderProgram2::SetUniform##num##toLetter##t(const char *name, fromType *fvalues) \
480 { \
481  toType tvalues[num]; \
482  for (int i=0; i<num; ++i) \
483  { \
484  tvalues[i] = static_cast<toType>(fvalues[i]); \
485  } \
486  this->SetUniform##num##toLetter(name, tvalues); \
487 } \
488 template<typename fromType> \
489 void vtkShaderProgram2::SetUniform##num##toLetter##t(int location, fromType *fvalues) \
490 { \
491  assert(location!=-1); \
492  toType tvalues[num]; \
493  for (int i=0; i<num; ++i) \
494  { \
495  tvalues[i] = static_cast<toType>(fvalues[i]); \
496  } \
497  this->SetUniform##num##toLetter(location, tvalues); \
498 }
507 
508 #endif
void SetUniform2f(const char *name, float *val)
void SetUniform3f(const char *name, float *val)
abstract base class for most VTK objects
Definition: vtkObject.h:59
vtkTimeStamp LastLinkTime
void SetUniform1f(const char *name, float *val)
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkWeakPointer< vtkRenderWindow > Context
GLSL uniform variables.
record modification and/or execution time
Definition: vtkTimeStamp.h:35
void SetUniformi(const char *name, int val)
vtkUniformVariables * UniformVariables
an ordered list of Shader2 objects.
GLSL Program.
void SetUniform3i(const char *name, int *val)
a simple class to control print indentation
Definition: vtkIndent.h:39
vtkShaderProgram2GeometryInType
#define vtkShaderProgram2SetUniformCopyCastMacro(toLetter, toType, num)
void SetUniformf(const char *name, float val)
Set a uniform value directly.
void SetUniformf(int loc, float val)
void SetUniform4f(const char *name, float *val)
void SetUniform4i(const char *name, int *val)
vtkShaderProgram2GeometryOutType
Interface class for querying and using OpenGL extensions.
void SetUniform2i(const char *name, int *val)
vtkSetMacro(IgnoreDriverBugs, bool)
When set known driver bugs are ignored during driver feature detection.
void SetUniformi(int loc, int val)
create a window for renderers to draw into
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
void SetUniform1i(const char *name, int *val)
vtkShaderProgram2BuildStatus
vtkTimeStamp LastSendUniformsTime
vtkShader2Collection * Shaders