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();
47  void PrintSelf(ostream& os, vtkIndent indent);
48 
50 
51  vtkGetObjectMacro(VertexShader, vtkShader);
54 
56 
57  vtkGetObjectMacro(FragmentShader, vtkShader);
60 
62 
63  vtkGetObjectMacro(GeometryShader, 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 
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:
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 
223 
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
vtkShaderProgram::SetUniform2fv
bool SetUniform2fv(const char *name, const int count, const float(*f)[2])
vtkGetObjectMacro
#define vtkGetObjectMacro(name, type)
Definition: vtkSetGet.h:232
vtkShaderProgram::GeometryShaderHandle
int GeometryShaderHandle
Definition: vtkShaderProgram.h:265
vtkShaderProgram::CompileShader
virtual int CompileShader()
vtkShaderProgram::Compiled
bool Compiled
Definition: vtkShaderProgram.h:269
vtkShaderProgram::Release
void Release()
Releases the shader program from the current context.
vtkShaderProgram::GetMD5Hash
std::string GetMD5Hash() const
Definition: vtkShaderProgram.h:76
vtkgl::count
GLuint GLuint GLsizei count
Definition: vtkgl.h:11315
vtkShaderProgram::FragmentShaderHandle
int FragmentShaderHandle
Definition: vtkShaderProgram.h:264
vtkShaderProgram::SetUniformMatrix4x4
bool SetUniformMatrix4x4(const char *name, float *v)
vtkShaderProgram::NormalizeOption
NormalizeOption
Options for attribute normalization.
Definition: vtkShaderProgram.h:82
vtkShaderProgram::Link
bool Link()
Attempt to link the shader program.
vtkShaderProgram::SetUniform4f
bool SetUniform4f(const char *name, const float v[4])
vtkShaderProgram::Bound
bool Bound
Definition: vtkShaderProgram.h:268
vtkShaderProgram::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent)
vtkShaderProgram::SetAttributeArray
bool SetAttributeArray(const char *name, const T &array, int tupleSize, NormalizeOption normalize)
Upload the supplied array of tightly packed values to the named attribute.
vtkShaderProgram::FragmentShader
vtkShader * FragmentShader
Definition: vtkShaderProgram.h:253
vtkTypeMacro
#define vtkTypeMacro(thisClass, superclass)
Definition: vtkSetGet.h:642
vtkShaderProgram::GetError
std::string GetError() const
Get the error message (empty if none) for the shader program.
Definition: vtkShaderProgram.h:110
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:62
vtkgl::v
const GLdouble * v
Definition: vtkgl.h:11595
vtkShaderProgram::vtkShaderProgram
vtkShaderProgram()
vtkShaderProgram::SetUniformf
bool SetUniformf(const char *name, float v)
vtkShaderProgram::New
static vtkShaderProgram * New()
vtkShaderProgram::UseAttributeArray
bool UseAttributeArray(const char *name, int offset, size_t stride, int elementType, int elementTupleSize, NormalizeOption normalize)
Use the named attribute array with the bound BufferObject.
vtkMatrix3x3
represent and manipulate 3x3 transformation matrices
Definition: vtkMatrix3x3.h:36
vtkShaderProgram::Handle
int Handle
Definition: vtkShaderProgram.h:262
vtkShaderProgram::Bind
bool Bind()
Bind the program in order to use it.
vtkShaderProgram::DisableAttributeArray
bool DisableAttributeArray(const char *name)
Disable the named attribute array.
vtkgl::stride
GLsizei stride
Definition: vtkgl.h:11728
vtkWindow
window superclass for vtkRenderWindow
Definition: vtkWindow.h:34
vtkShaderProgram::SetAttributeArrayInternal
bool SetAttributeArrayInternal(const char *name, void *buffer, int type, int tupleSize, NormalizeOption normalize)
vtkShaderProgram::Linked
bool Linked
Definition: vtkShaderProgram.h:267
source
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
Definition: vtkBoostGraphAdapter.h:821
vtkgl::name
GLuint const GLchar * name
Definition: vtkgl.h:11983
vtkShaderProgram::SetUniform1fv
bool SetUniform1fv(const char *name, const int count, const float *f)
vtkShaderProgram::DetachShader
bool DetachShader(const vtkShader *shader)
Detach the supplied shader from this program.
vtkShaderProgram::SetGeometryShader
void SetGeometryShader(vtkShader *)
vtkShaderProgram::SetUniform2i
bool SetUniform2i(const char *name, const int v[2])
vtkShaderProgram::SetUniformMatrix
bool SetUniformMatrix(const char *name, vtkMatrix4x4 *v)
vtkShaderProgram
The ShaderProgram uses one or more Shader objects.
Definition: vtkShaderProgram.h:43
vtkShaderProgram::SetVertexShader
void SetVertexShader(vtkShader *)
vtkvolume::replace
std::string replace(std::string source, const std::string &search, const std::string replace, bool all)
Definition: vtkVolumeShaderComposer.h:34
vtkgl::shader
GLuint shader
Definition: vtkgl.h:11982
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:39
vtkMatrix4x4
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:39
vtkShaderProgram::Normalize
@ Normalize
The values range across the limits of the numeric type.
Definition: vtkShaderProgram.h:91
vtkShaderProgram::SetUniformi
bool SetUniformi(const char *name, int v)
Set the name uniform value to int v.
vtkShaderProgram::isBound
bool isBound() const
Check if the program is currently bound, or not.
Definition: vtkShaderProgram.h:101
vtkShader
Vertex or Fragment shader, combined into a ShaderProgram.
Definition: vtkShader.h:37
vtkShaderProgram::SetUniform4fv
bool SetUniform4fv(const char *name, const int count, const float(*f)[4])
vtkgl::offset
GLintptr offset
Definition: vtkgl.h:11844
vtkShaderProgram::SetUniform3uc
bool SetUniform3uc(const char *name, const unsigned char v[3])
vtkGetMacro
#define vtkGetMacro(name, type)
Definition: vtkSetGet.h:93
vtkOpenGLShaderCache
manage Shader Programs within a context
Definition: vtkOpenGLShaderCache.h:33
vtkShaderProgram::SetUniformMatrix
bool SetUniformMatrix(const char *name, vtkMatrix3x3 *v)
vtkgl::f
GLclampf f
Definition: vtkgl.h:14181
vtkShaderProgram::GetHandle
int GetHandle() const
Get the handle of the shader program.
Definition: vtkShaderProgram.h:107
vtkObject.h
vtkBooleanMacro
vtkBooleanMacro(IgnoreDriverBugs, bool)
vtkSetMacro
vtkSetMacro(IgnoreDriverBugs, bool)
vtkShaderProgram::NumberOfOutputs
unsigned int NumberOfOutputs
Definition: vtkShaderProgram.h:275
vtkShaderProgram::Error
std::string Error
Definition: vtkShaderProgram.h:277
vtkgl::buffer
GLuint buffer
Definition: vtkgl.h:11839
vtkShaderProgram::SetFragmentShader
void SetFragmentShader(vtkShader *)
vtkShaderProgram::EnableAttributeArray
bool EnableAttributeArray(const char *name)
Enable the named attribute array.
vtkShaderProgram::SetMD5Hash
void SetMD5Hash(const std::string &hash)
Definition: vtkShaderProgram.h:77
vtkShaderProgram::SetUniform2f
bool SetUniform2f(const char *name, const float v[2])
vtkShaderProgram::MD5Hash
std::string MD5Hash
Definition: vtkShaderProgram.h:257
vtkShaderProgram::SetUniform3fv
bool SetUniform3fv(const char *name, const int count, const float(*f)[3])
vtkShaderProgram::SetUniformMatrix3x3
bool SetUniformMatrix3x3(const char *name, float *v)
vtkShaderProgram::VertexShader
vtkShader * VertexShader
Definition: vtkShaderProgram.h:252
vtkShaderProgram::GeometryShader
vtkShader * GeometryShader
Definition: vtkShaderProgram.h:254
vtkShaderProgram::AttachShader
bool AttachShader(const vtkShader *shader)
Attach the supplied shader to this program.
vtkShaderProgram::SetUniform4uc
bool SetUniform4uc(const char *name, const unsigned char v[4])
vtkgl::type
GLuint GLuint GLsizei GLenum type
Definition: vtkgl.h:11315
vtkShaderProgram::~vtkShaderProgram
~vtkShaderProgram()
vtkgl::string
GLsizei const GLchar ** string
Definition: vtkgl.h:12011
vtkShaderProgram::Substitute
static bool Substitute(std::string &source, const std::string &search, const std::string replace, bool all=true)
vtkShaderProgram::SetUniform3f
bool SetUniform3f(const char *name, const float v[3])
vtkShaderProgram::ReleaseGraphicsResources
void ReleaseGraphicsResources(vtkWindow *win)
vtkShaderProgram::VertexShaderHandle
int VertexShaderHandle
Definition: vtkShaderProgram.h:263
vtkShaderProgram::SetUniform1iv
bool SetUniform1iv(const char *name, const int count, const int *f)
Set the name uniform array to f with count elements.
vtkShaderProgram::Attributes
std::map< std::string, int > Attributes
Definition: vtkShaderProgram.h:279