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 =========================================================================*/
14 /**
15  * @class   vtkShader
16  * @brief   encapsulate a glsl shader
17  *
18  * vtkShader represents a shader, vertex, fragment, geometry etc
19  */
20 
21 #ifndef vtkShader_h
22 #define vtkShader_h
23 
24 #include "vtkObject.h"
25 #include "vtkRenderingOpenGL2Module.h" // for export macro
26 
27 #include <string> // For member variables.
28 #include <vector> // For member variables.
29 
30 /**
31  * @brief Vertex or Fragment shader, combined into a ShaderProgram.
32  *
33  * This class creates a Vertex, Fragment or Geometry shader, that can be
34  * attached to a ShaderProgram in order to render geometry etc.
35  */
36 
37 class VTKRENDERINGOPENGL2_EXPORT vtkShader : public vtkObject
38 {
39 public:
40   static vtkShader* New();
41   vtkTypeMacro(vtkShader, vtkObject);
42   void PrintSelf(ostream& os, vtkIndent indent) override;
43 
44   /** Available shader types. */
45   enum Type
46   {
47     Vertex,   /**< Vertex shader */
48     Fragment, /**< Fragment shader */
49     Geometry, /**< Geometry shader */
50     Unknown   /**< Unknown (default) */
51   };
52 
53   /** Set the shader type. */
54   void SetType(Type type);
55 
56   /** Get the shader type, typically Vertex or Fragment. */
GetType()57   Type GetType() const { return this->ShaderType; }
58 
59   /** Set the shader source to the supplied string. */
60   void SetSource(const std::string& source);
61 
62   /** Get the source for the shader. */
GetSource()63   std::string GetSource() const { return this->Source; }
64 
65   /** Get the error message (empty if none) for the shader. */
GetError()66   std::string GetError() const { return this->Error; }
67 
68   /** Get the handle of the shader. */
GetHandle()69   int GetHandle() const { return this->Handle; }
70 
71   /** Compile the shader.
72    * @note A valid context must to current in order to compile the shader.
73    */
74   bool Compile();
75 
76   /** Delete the shader.
77    * @note This should only be done once the ShaderProgram is done with the
78    * Shader.
79    */
80   void Cleanup();
81 
82   class ReplacementSpec
83   {
84   public:
85     std::string OriginalValue;
86     vtkShader::Type ShaderType;
87     bool ReplaceFirst;
88     bool operator<(const ReplacementSpec& v1) const
89     {
90       if (this->OriginalValue != v1.OriginalValue)
91       {
92         return this->OriginalValue < v1.OriginalValue;
93       }
94       if (this->ShaderType != v1.ShaderType)
95       {
96         return this->ShaderType < v1.ShaderType;
97       }
98       return (this->ReplaceFirst < v1.ReplaceFirst);
99     }
100     bool operator>(const ReplacementSpec& v1) const
101     {
102       if (this->OriginalValue != v1.OriginalValue)
103       {
104         return this->OriginalValue > v1.OriginalValue;
105       }
106       if (this->ShaderType != v1.ShaderType)
107       {
108         return this->ShaderType > v1.ShaderType;
109       }
110       return (this->ReplaceFirst > v1.ReplaceFirst);
111     }
112   };
113   class ReplacementValue
114   {
115   public:
116     std::string Replacement;
117     bool ReplaceAll;
118   };
119 
120 protected:
121   vtkShader();
122   ~vtkShader() override;
123 
124   Type ShaderType;
125   int Handle;
126   bool Dirty;
127 
128   std::string Source;
129   std::string Error;
130 
131 private:
132   vtkShader(const vtkShader&) = delete;
133   void operator=(const vtkShader&) = delete;
134 };
135 
136 #endif
137