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 =========================================================================*/
25 #ifndef vtkShaderProgram2_h
26 #define vtkShaderProgram2_h
27 
28 #include <cassert> // for templated functions
29 #include "vtkRenderingOpenGLModule.h" // for export macro
30 #include "vtkWeakPointer.h" // for weak ptr to rendering context
31 #include "vtkObject.h"
32 
33 class vtkRenderWindow;
37 
38 // Values for GetLastBuildStatus()
40 {
41  // one of the shaders failed to compile
43  // all the shaders compiled successfully but the link failed
45  // all the shaders compiled successfully and the link succeeded
47 };
48 
50 {
56 };
57 
59 {
63 };
64 
65 class VTKRENDERINGOPENGL_EXPORT vtkShaderProgram2 : public vtkObject
66 {
67 public:
68  static vtkShaderProgram2* New();
69  vtkTypeMacro(vtkShaderProgram2, vtkObject);
70  void PrintSelf(ostream& os, vtkIndent indent) VTK_OVERRIDE;
71 
76  static bool IsSupported(vtkRenderWindow *context);
77 
79 
87  vtkGetMacro(PrintErrors,bool);
88  vtkSetMacro(PrintErrors,bool);
90 
92 
98  void SetContext(vtkRenderWindow *context);
99  vtkRenderWindow *GetContext();
101 
103 
107  vtkGetObjectMacro(Shaders,vtkShader2Collection);
109 
115  bool HasVertexShaders();
116 
120  bool HasTessellationControlShaders();
121 
125  bool HasTessellationEvaluationShaders();
126 
130  bool HasGeometryShaders();
131 
138  bool HasFragmentShaders();
139 
146  bool IsValid();
147 
155  void Build();
156 
163  void SendUniforms();
164 
171  void PrintActiveUniformVariables(ostream &os,
172  vtkIndent indent);
173 
180  void PrintActiveUniformVariablesOnCout();
181 
187  bool IsUsed();
188 
197  void Use();
198 
204  void Restore();
205 
213  void RestoreFixedPipeline();
214 
216 
220  void UseProgram();
221  void UnuseProgram();
223 
233  int GetLastBuildStatus();
234 
240  const char *GetLastLinkLog();
241 
247  const char *GetLastValidateLog();
248 
252  virtual void ReleaseGraphicsResources();
253 
262  int GetAttributeLocation(const char *name);
263 
265 
269  vtkGetObjectMacro(UniformVariables,vtkUniformVariables);
270  virtual void SetUniformVariables(vtkUniformVariables *variables);
272 
282  bool DisplayListUnderCreationInCompileMode();
283 
285 
298  vtkSetMacro(GeometryTypeIn,int);
299  vtkGetMacro(GeometryTypeIn,int);
301 
303 
315  vtkSetMacro(GeometryVerticesOut,int);
316  vtkGetMacro(GeometryVerticesOut,int);
318 
320 
331  vtkSetMacro(GeometryTypeOut,int);
332  vtkGetMacro(GeometryTypeOut,int);
334 
335  /*
336  Low level api --
337  this is provided as a way to avoid some of the overhead in this
338  class's implementation of SendUniforms. One should use the
339  following API if performance is a concern (eg. uniforms are
340  set per primitive), or if the uniform management is not needed
341  (eg. variables are already managed in other vtkObjects)
342  */
343 
348  int GetUniformLocation(const char *name);
349 
357  void SetUniformf(const char *name, float val)
358  { this->SetUniform1f(name, &val); }
359  void SetUniform1f(const char *name, float *val)
360  { this->SetUniform1f(this->GetUniformLocation(name), val); }
361  void SetUniform2f(const char *name, float *val)
362  { this->SetUniform2f(this->GetUniformLocation(name), val); }
363  void SetUniform3f(const char *name, float *val)
364  { this->SetUniform3f(this->GetUniformLocation(name), val); }
365  void SetUniform4f(const char *name, float *val)
366  { this->SetUniform4f(this->GetUniformLocation(name), val); }
367 
368  void SetUniformi(const char *name, int val)
369  { this->SetUniform1i(name, &val); }
370  void SetUniform1i(const char *name, int *val)
371  { this->SetUniform1i(this->GetUniformLocation(name), val); }
372  void SetUniform2i(const char *name, int *val)
373  { this->SetUniform2i(this->GetUniformLocation(name), val); }
374  void SetUniform3i(const char *name, int *val)
375  { this->SetUniform3i(this->GetUniformLocation(name), val); }
376  void SetUniform4i(const char *name, int *val)
377  { this->SetUniform4i(this->GetUniformLocation(name), val); }
378 
379  void SetUniformf(int loc, float val)
380  { this->SetUniform1f(loc, &val); }
381  void SetUniform1f(int loc, float *val);
382  void SetUniform2f(int loc, float *val);
383  void SetUniform3f(int loc, float *val);
384  void SetUniform4f(int loc, float *val);
385 
386  void SetUniformi(int loc, int val)
387  { this->SetUniform1i(loc, &val); }
388  void SetUniform1i(int loc, int *val);
389  void SetUniform2i(int loc, int *val);
390  void SetUniform3i(int loc, int *val);
391  void SetUniform4i(int loc, int *val);
392 
394 
399  template<typename T> void SetUniform1it(const char *name, T *value);
400  template<typename T> void SetUniform2it(const char *name, T *value);
401  template<typename T> void SetUniform3it(const char *name, T *value);
402  template<typename T> void SetUniform4it(const char *name, T *value);
404 
405  template<typename T> void SetUniform1ft(const char *name, T *value);
406  template<typename T> void SetUniform2ft(const char *name, T *value);
407  template<typename T> void SetUniform3ft(const char *name, T *value);
408  template<typename T> void SetUniform4ft(const char *name, T *value);
409 
410  template<typename T> void SetUniform1it(int loc, T *value);
411  template<typename T> void SetUniform2it(int loc, T *value);
412  template<typename T> void SetUniform3it(int loc, T *value);
413  template<typename T> void SetUniform4it(int loc, T *value);
414 
415  template<typename T> void SetUniform1ft(int loc, T *value);
416  template<typename T> void SetUniform2ft(int loc, T *value);
417  template<typename T> void SetUniform3ft(int loc, T *value);
418  template<typename T> void SetUniform4ft(int loc, T *value);
419 
420 protected:
422  ~vtkShaderProgram2() VTK_OVERRIDE;
423 
427  bool LoadRequiredExtensions(vtkRenderWindow *context);
428 
438  int GetUniformLocationInternal(const char *name);
439 
440  unsigned int Id; // actually GLuint. Initial value is 0.
441  unsigned int SavedId;
442 
445 
448 
449  int LastBuildStatus; // Initial value is VTK_SHADER_PROGRAM2_COMPILE_FAILED
450 
451  char *LastLinkLog; // Initial value is the empty string ""='\0'
452  size_t LastLinkLogCapacity; // Initial value is 8.
453 
454  char *LastValidateLog; // Initial value is the empty string ""='\0'
455  size_t LastValidateLogCapacity; // Initial value is 8.
456 
457 
459 
462 
466 
467 private:
468  vtkShaderProgram2(const vtkShaderProgram2&) VTK_DELETE_FUNCTION;
469  void operator=(const vtkShaderProgram2&) VTK_DELETE_FUNCTION;
470 };
471 
472 // ----------------------------------------------------------------------------
473 
474 #define vtkShaderProgram2SetUniformCopyCastMacro(toLetter, toType, num) \
475 template<typename fromType> \
476 void vtkShaderProgram2::SetUniform##num##toLetter##t(const char *name, fromType *fvalues) \
477 { \
478  toType tvalues[num]; \
479  for (int i=0; i<num; ++i) \
480  { \
481  tvalues[i] = static_cast<toType>(fvalues[i]); \
482  } \
483  this->SetUniform##num##toLetter(name, tvalues); \
484 } \
485 template<typename fromType> \
486 void vtkShaderProgram2::SetUniform##num##toLetter##t(int location, fromType *fvalues) \
487 { \
488  assert(location!=-1); \
489  toType tvalues[num]; \
490  for (int i=0; i<num; ++i) \
491  { \
492  tvalues[i] = static_cast<toType>(fvalues[i]); \
493  } \
494  this->SetUniform##num##toLetter(location, tvalues); \
495 }
504 
505 #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:53
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:32
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:33
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