VTK
/Users/kitware/Dashboards/MyTests/VTK_BLD_Release_docs/Utilities/Doxygen/dox/Rendering/OpenGL/vtkShaderProgram2.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkShaderProgram2.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00027 #ifndef vtkShaderProgram2_h
00028 #define vtkShaderProgram2_h
00029 
00030 #include <cassert> // for templated functions
00031 #include "vtkRenderingOpenGLModule.h" // for export macro
00032 #include "vtkWeakPointer.h" // for weak ptr to rendering context
00033 #include "vtkObject.h"
00034 
00035 class vtkRenderWindow;
00036 class vtkOpenGLExtensionManager;
00037 class vtkShader2Collection;
00038 class vtkUniformVariables;
00039 
00040 // Values for GetLastBuildStatus()
00041 enum vtkShaderProgram2BuildStatus
00042 {
00043   // one of the shaders failed to compile
00044   VTK_SHADER_PROGRAM2_COMPILE_FAILED=0,
00045   // all the shaders compiled successfully but the link failed
00046   VTK_SHADER_PROGRAM2_LINK_FAILED=1,
00047   // all the shaders compiled successfully and the link succeeded
00048   VTK_SHADER_PROGRAM2_LINK_SUCCEEDED=2
00049 };
00050 
00051 enum vtkShaderProgram2GeometryInType
00052 {
00053   VTK_GEOMETRY_SHADER_IN_TYPE_POINTS,
00054   VTK_GEOMETRY_SHADER_IN_TYPE_LINES,
00055   VTK_GEOMETRY_SHADER_IN_TYPE_LINES_ADJACENCY,
00056   VTK_GEOMETRY_SHADER_IN_TYPE_TRIANGLES,
00057   VTK_GEOMETRY_SHADER_IN_TYPE_TRIANGLES_ADJACENCY
00058 };
00059 
00060 enum vtkShaderProgram2GeometryOutType
00061 {
00062   VTK_GEOMETRY_SHADER_OUT_TYPE_POINTS,
00063   VTK_GEOMETRY_SHADER_OUT_TYPE_LINE_STRIP,
00064   VTK_GEOMETRY_SHADER_OUT_TYPE_TRIANGLE_STRIP
00065 };
00066 
00067 class VTKRENDERINGOPENGL_EXPORT vtkShaderProgram2 : public vtkObject
00068 {
00069 public:
00070   static vtkShaderProgram2* New();
00071   vtkTypeMacro(vtkShaderProgram2, vtkObject);
00072   void PrintSelf(ostream& os, vtkIndent indent);
00073 
00076   static bool IsSupported(vtkRenderWindow *context);
00077 
00079 
00084   vtkGetMacro(PrintErrors,bool);
00085   vtkSetMacro(PrintErrors,bool);
00087 
00089 
00093   void SetContext(vtkRenderWindow *context);
00094   vtkRenderWindow *GetContext();
00096 
00098 
00100   vtkGetObjectMacro(Shaders,vtkShader2Collection);
00102 
00106   bool HasVertexShaders();
00107 
00110   bool HasTessellationControlShaders();
00111 
00114   bool HasTessellationEvaluationShaders();
00115 
00117   bool HasGeometryShaders();
00118 
00122   bool HasFragmentShaders();
00123 
00128   bool IsValid();
00129 
00134   void Build();
00135 
00140   void SendUniforms();
00141 
00143 
00147   void PrintActiveUniformVariables(ostream &os,
00148                                    vtkIndent indent);
00150 
00155   void PrintActiveUniformVariablesOnCout();
00156 
00160   bool IsUsed();
00161 
00167   void Use();
00168 
00172   void Restore();
00173 
00178   void RestoreFixedPipeline();
00179 
00181 
00183   void UseProgram();
00184   void UnuseProgram();
00186 
00193   int GetLastBuildStatus();
00194 
00197   const char *GetLastLinkLog();
00198 
00201   const char *GetLastValidateLog();
00202 
00204   virtual void ReleaseGraphicsResources();
00205 
00211   int GetAttributeLocation(const char *name);
00212 
00214 
00216   vtkGetObjectMacro(UniformVariables,vtkUniformVariables);
00217   virtual void SetUniformVariables(vtkUniformVariables *variables);
00219 
00226   bool DisplayListUnderCreationInCompileMode();
00227 
00229 
00239   vtkSetMacro(GeometryTypeIn,int);
00240   vtkGetMacro(GeometryTypeIn,int);
00242 
00244 
00251   vtkSetMacro(GeometryVerticesOut,int);
00252   vtkGetMacro(GeometryVerticesOut,int);
00254 
00256 
00264   vtkSetMacro(GeometryTypeOut,int);
00265   vtkGetMacro(GeometryTypeOut,int);
00267 
00268   /*
00269   Low level api --
00270   this is provided as a way to avoid some of the overhead in this
00271   class's implementation of SendUniforms. One should use the
00272   following API if performance is a concern (eg. uniforms are
00273   set per primative), or if the uniform management is not needed
00274   (eg. variables are already managed in other vtkObjects)
00275   */
00276 
00278   int GetUniformLocation(const char *name);
00279 
00281 
00285   void SetUniformf(const char *name, float val)
00286     { this->SetUniform1f(name, &val); }
00287   void SetUniform1f(const char *name, float *val)
00288     { this->SetUniform1f(this->GetUniformLocation(name), val); }
00289   void SetUniform2f(const char *name, float *val)
00290     { this->SetUniform2f(this->GetUniformLocation(name), val); }
00291   void SetUniform3f(const char *name, float *val)
00292     { this->SetUniform3f(this->GetUniformLocation(name), val); }
00293   void SetUniform4f(const char *name, float *val)
00294     { this->SetUniform4f(this->GetUniformLocation(name), val); }
00296 
00297   void SetUniformi(const char *name, int val)
00298     { this->SetUniform1i(name, &val); }
00299   void SetUniform1i(const char *name, int *val)
00300     { this->SetUniform1i(this->GetUniformLocation(name), val); }
00301   void SetUniform2i(const char *name, int *val)
00302     { this->SetUniform2i(this->GetUniformLocation(name), val); }
00303   void SetUniform3i(const char *name, int *val)
00304     { this->SetUniform3i(this->GetUniformLocation(name), val); }
00305   void SetUniform4i(const char *name, int *val)
00306     { this->SetUniform4i(this->GetUniformLocation(name), val); }
00307 
00308   void SetUniformf(int loc, float val)
00309     { this->SetUniform1f(loc, &val); }
00310   void SetUniform1f(int loc, float *val);
00311   void SetUniform2f(int loc, float *val);
00312   void SetUniform3f(int loc, float *val);
00313   void SetUniform4f(int loc, float *val);
00314 
00315   void SetUniformi(int loc, int val)
00316     { this->SetUniform1i(loc, &val); }
00317   void SetUniform1i(int loc, int *val);
00318   void SetUniform2i(int loc, int *val);
00319   void SetUniform3i(int loc, int *val);
00320   void SetUniform4i(int loc, int *val);
00321 
00323 
00326   template<typename T> void SetUniform1it(const char *name, T *value);
00327   template<typename T> void SetUniform2it(const char *name, T *value);
00328   template<typename T> void SetUniform3it(const char *name, T *value);
00329   template<typename T> void SetUniform4it(const char *name, T *value);
00331 
00332   template<typename T> void SetUniform1ft(const char *name, T *value);
00333   template<typename T> void SetUniform2ft(const char *name, T *value);
00334   template<typename T> void SetUniform3ft(const char *name, T *value);
00335   template<typename T> void SetUniform4ft(const char *name, T *value);
00336 
00337   template<typename T> void SetUniform1it(int loc, T *value);
00338   template<typename T> void SetUniform2it(int loc, T *value);
00339   template<typename T> void SetUniform3it(int loc, T *value);
00340   template<typename T> void SetUniform4it(int loc, T *value);
00341 
00342   template<typename T> void SetUniform1ft(int loc, T *value);
00343   template<typename T> void SetUniform2ft(int loc, T *value);
00344   template<typename T> void SetUniform3ft(int loc, T *value);
00345   template<typename T> void SetUniform4ft(int loc, T *value);
00346 
00347 protected:
00348   vtkShaderProgram2();
00349   virtual ~vtkShaderProgram2();
00350 
00352   bool LoadRequiredExtensions(vtkRenderWindow *context);
00353 
00359   int GetUniformLocationInternal(const char *name);
00360 
00361   unsigned int Id; // actually GLuint. Initial value is 0.
00362   unsigned int SavedId;
00363 
00364   vtkTimeStamp LastLinkTime;
00365   vtkTimeStamp LastSendUniformsTime;
00366 
00367   vtkShader2Collection *Shaders;
00368   vtkUniformVariables *UniformVariables;
00369 
00370   int LastBuildStatus; // Initial value is VTK_SHADER_PROGRAM2_COMPILE_FAILED
00371 
00372   char *LastLinkLog; // Initial value is the empty string ""='\0'
00373   size_t LastLinkLogCapacity; // Initial value is 8.
00374 
00375   char *LastValidateLog; // Initial value is the empty string ""='\0'
00376   size_t LastValidateLogCapacity; // Initial value is 8.
00377 
00378 
00379   bool PrintErrors;
00380 
00381   vtkWeakPointer<vtkRenderWindow> Context;
00382   bool ExtensionsLoaded;
00383 
00384   int GeometryTypeIn;
00385   int GeometryTypeOut;
00386   int GeometryVerticesOut;
00387 
00388 private:
00389   vtkShaderProgram2(const vtkShaderProgram2&); // Not implemented.
00390   void operator=(const vtkShaderProgram2&); // Not implemented.
00391 };
00392 
00393 // ----------------------------------------------------------------------------
00394 //BTX
00395 #define vtkShaderProgram2SetUniformCopyCastMacro(toLetter, toType, num) \
00396 template<typename fromType> \
00397 void vtkShaderProgram2::SetUniform##num##toLetter##t(const char *name, fromType *fvalues) \
00398 { \
00399   toType tvalues[num]; \
00400   for (int i=0; i<num; ++i) \
00401     { \
00402     tvalues[i] = static_cast<toType>(fvalues[i]); \
00403     } \
00404   this->SetUniform##num##toLetter(name, tvalues); \
00405 } \
00406 template<typename fromType> \
00407 void vtkShaderProgram2::SetUniform##num##toLetter##t(int location, fromType *fvalues) \
00408 { \
00409   assert(location!=-1); \
00410   toType tvalues[num]; \
00411   for (int i=0; i<num; ++i) \
00412     { \
00413     tvalues[i] = static_cast<toType>(fvalues[i]); \
00414     } \
00415   this->SetUniform##num##toLetter(location, tvalues); \
00416 }
00417 vtkShaderProgram2SetUniformCopyCastMacro(f, float, 1)
00418 vtkShaderProgram2SetUniformCopyCastMacro(f, float, 2)
00419 vtkShaderProgram2SetUniformCopyCastMacro(f, float, 3)
00420 vtkShaderProgram2SetUniformCopyCastMacro(f, float, 4)
00421 vtkShaderProgram2SetUniformCopyCastMacro(i, int, 1)
00422 vtkShaderProgram2SetUniformCopyCastMacro(i, int, 2)
00423 vtkShaderProgram2SetUniformCopyCastMacro(i, int, 3)
00424 vtkShaderProgram2SetUniformCopyCastMacro(i, int, 4)
00425 //ETX
00426 
00427 
00428 #endif