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 =========================================================================*/
20 #ifndef vtkShaderProgram_h
21 #define vtkShaderProgram_h
22 
23 #include "vtkRenderingOpenGL2Module.h" // for export macro
24 #include "vtkObject.h"
25 
26 #include <string> // For member variables.
27 #include <map> // For member variables.
28 
29 class vtkMatrix3x3;
30 class vtkMatrix4x4;
31 class vtkShader;
32 class VertexArrayObject;
33 class vtkWindow;
34 
42 class VTKRENDERINGOPENGL2_EXPORT vtkShaderProgram : public vtkObject
43 {
44 public:
45  static vtkShaderProgram *New();
46  vtkTypeMacro(vtkShaderProgram, vtkObject);
47  void PrintSelf(ostream& os, vtkIndent indent);
48 
50 
51  vtkGetObjectMacro(VertexShader, vtkShader);
52  void SetVertexShader(vtkShader*);
54 
56 
57  vtkGetObjectMacro(FragmentShader, vtkShader);
58  void SetFragmentShader(vtkShader*);
60 
62 
63  vtkGetObjectMacro(GeometryShader, vtkShader);
64  void SetGeometryShader(vtkShader*);
66 
68 
69  vtkGetMacro(Compiled, bool);
70  vtkSetMacro(Compiled, bool);
71  vtkBooleanMacro(Compiled, bool);
73 
75 
76  std::string GetMD5Hash() const { return this->MD5Hash; }
77  void SetMD5Hash(const std::string &hash) { this->MD5Hash = hash; }
79 
80 
93  NoNormalize
94  };
95 
96 
101  bool isBound() const { return this->Bound; }
102 
104  void ReleaseGraphicsResources(vtkWindow *win);
105 
107  int GetHandle() const { return Handle; }
108 
110  std::string GetError() const { return Error; }
111 
116  bool EnableAttributeArray(const char *name);
117 
122  bool DisableAttributeArray(const char *name);
123 
139  bool UseAttributeArray(const char *name, int offset, size_t stride,
140  int elementType, int elementTupleSize,
141  NormalizeOption normalize);
142 
160  template <class T>
161  bool SetAttributeArray(const char *name, const T &array,
162  int tupleSize, NormalizeOption normalize);
163 
165  bool SetUniformi(const char *name, int v);
166  bool SetUniformf(const char *name, float v);
167  bool SetUniform2i(const char *name, const int v[2]);
168  bool SetUniform2f(const char *name, const float v[2]);
169  bool SetUniform3f(const char *name, const float v[3]);
170  bool SetUniform4f(const char *name, const float v[4]);
171  bool SetUniform3uc(const char *name, const unsigned char v[3]); // maybe remove
172  bool SetUniform4uc(const char *name, const unsigned char v[4]); // maybe remove
173  bool SetUniformMatrix(const char *name, vtkMatrix3x3 *v);
174  bool SetUniformMatrix(const char *name, vtkMatrix4x4 *v);
175  bool SetUniformMatrix3x3(const char *name, float *v);
176  bool SetUniformMatrix4x4(const char *name, float *v);
177 
179  bool SetUniform1iv(const char *name, const int count, const int *f);
180  bool SetUniform1fv(const char *name, const int count, const float *f);
181  bool SetUniform2fv(const char *name, const int count, const float (*f)[2]);
182  bool SetUniform3fv(const char *name, const int count, const float (*f)[3]);
183  bool SetUniform4fv(const char *name, const int count, const float (*f)[4]);
184 
185  // How many outputs does this program produce
186  // only valid for OpenGL 3.2 or later
187  vtkSetMacro(NumberOfOutputs,unsigned int);
188 
189 //BTX
191 
195  static bool Substitute(
197  const std::string &search,
198  const std::string replace,
199  bool all = true);
201 
202 
203 
204 protected:
206  ~vtkShaderProgram();
207 
208  /***************************************************************
209  * The following functions are only for use by the shader cache
210  * which is why they are protected and that class is a friend
211  * you need to use the shader cache to compile/link/bind your shader
212  * do not try to do it yourself as it will screw up the cache
213  ***************************************************************/
214  friend class vtkOpenGLShaderCache;
215 
222  bool AttachShader(const vtkShader *shader);
223 
229  bool DetachShader(const vtkShader *shader);
230 
232  virtual int CompileShader();
233 
239  bool Link();
240 
245  bool Bind();
246 
248  void Release();
249 
250  /************* end **************************************/
251 
255 
256  // hash of the shader program
258 
259  bool SetAttributeArrayInternal(const char *name, void *buffer,
260  int type, int tupleSize,
261  NormalizeOption normalize);
262  int Handle;
266 
267  bool Linked;
268  bool Bound;
269  bool Compiled;
270 
271  // for glsl 1.5 or later, how many outputs
272  // does this shader create
273  // they will be bound in order to
274  // fragOutput0 fragOutput1 etc...
275  unsigned int NumberOfOutputs;
276 
278 
279  std::map<std::string, int> Attributes;
280 
281  friend class VertexArrayObject;
282 
283 private:
284  int FindAttributeArray(const char *name);
285  int FindUniform(const char *name);
286 
287  vtkShaderProgram(const vtkShaderProgram&); // Not implemented.
288  void operator=(const vtkShaderProgram&); // Not implemented.
289 //ETX
290 };
291 
292 
293 #endif
int GetHandle() const
Get the handle of the shader program.
abstract base class for most VTK objects
Definition: vtkObject.h:61
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:38
unsigned int NumberOfOutputs
manage Shader Programs within a context
bool isBound() const
Check if the program is currently bound, or not.
vtkShader * VertexShader
The values range across the limits of the numeric type.
std::map< std::string, int > Attributes
void SetMD5Hash(const std::string &hash)
window superclass for vtkRenderWindow
Definition: vtkWindow.h:36
virtual void PrintSelf(ostream &os, vtkIndent indent)
a simple class to control print indentation
Definition: vtkIndent.h:38
vtkShader * GeometryShader
vtkShader * FragmentShader
NormalizeOption
Options for attribute normalization.
std::string GetMD5Hash() const
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
Vertex or Fragment shader, combined into a ShaderProgram.
Definition: vtkShader.h:36
std::string GetError() const
Get the error message (empty if none) for the shader program.
std::string replace(std::string source, const std::string &search, const std::string replace, bool all)
static vtkObject * New()
represent and manipulate 3x3 transformation matrices
Definition: vtkMatrix3x3.h:35
The ShaderProgram uses one or more Shader objects.