VTK  9.4.20251007
vtkShaderProgram.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
14
15#ifndef vtkShaderProgram_h
16#define vtkShaderProgram_h
17
18#include "vtkObject.h"
19#include "vtkRenderingOpenGL2Module.h" // for export macro
20
21#include <map> // For member variables.
22#include <string> // For member variables.
23
24VTK_ABI_NAMESPACE_BEGIN
25class vtkMatrix3x3;
26class vtkMatrix4x4;
28class vtkShader;
29class VertexArrayObject;
30class vtkWindow;
31
40
41class VTKRENDERINGOPENGL2_EXPORT vtkShaderProgram : public vtkObject
42{
43public:
46 void PrintSelf(ostream& os, vtkIndent indent) override;
47
49
52 vtkGetObjectMacro(VertexShader, vtkShader);
55
57
60 vtkGetObjectMacro(FragmentShader, vtkShader);
63
65
68 vtkGetObjectMacro(GeometryShader, vtkShader);
71
73
76 vtkGetObjectMacro(ComputeShader, vtkShader);
79
81
84 vtkGetObjectMacro(TessControlShader, vtkShader);
87
89
92 vtkGetObjectMacro(TessEvaluationShader, vtkShader);
95
97
103
105
108 vtkGetMacro(Compiled, bool);
109 vtkSetMacro(Compiled, bool);
110 vtkBooleanMacro(Compiled, bool);
112
116 std::string GetMD5Hash() const { return this->MD5Hash; }
117 void SetMD5Hash(const std::string& hash) { this->MD5Hash = hash; }
118
134
139 bool isBound() const { return this->Bound; }
140
145
147 int GetHandle() const { return Handle; }
148
150 std::string GetError() const { return Error; }
151
156 bool EnableAttributeArray(const char* name);
157
162 bool DisableAttributeArray(const char* name);
163
179 bool UseAttributeArray(const char* name, int offset, size_t stride, int elementType,
180 int elementTupleSize, NormalizeOption normalize);
181
199 template <class T>
201 const char* name, const T& array, int tupleSize, NormalizeOption normalize);
202
204 bool SetUniformi(const char* name, int v);
205 bool SetUniformf(const char* name, float v);
206 bool SetUniform2i(const char* name, const int v[2]);
207 bool SetUniform2f(const char* name, const float v[2]);
208 bool SetUniform3f(const char* name, const float v[3]);
209 bool SetUniform3f(const char* name, const double v[3]);
210 bool SetUniform4f(const char* name, const float v[4]);
211 bool SetUniform3uc(const char* name, const unsigned char v[3]); // maybe remove
212 bool SetUniform4uc(const char* name, const unsigned char v[4]); // maybe remove
213 bool SetUniformMatrix(const char* name, vtkMatrix3x3* v);
214 bool SetUniformMatrix(const char* name, vtkMatrix4x4* v);
215 bool SetUniformMatrix3x3(const char* name, float* v);
216 bool SetUniformMatrix4x4(const char* name, float* v);
217
219 bool SetUniform1iv(const char* name, int count, const int* f);
220 bool SetUniform1fv(const char* name, int count, const float* f);
221 bool SetUniform2fv(const char* name, int count, const float* f);
222 bool SetUniform2fv(const char* name, int count, const float (*f)[2]);
223 bool SetUniform3fv(const char* name, int count, const float* f);
224 bool SetUniform3fv(const char* name, int count, const float (*f)[3]);
225 bool SetUniform4fv(const char* name, int count, const float* f);
226 bool SetUniform4fv(const char* name, int count, const float (*f)[4]);
227 bool SetUniformMatrix4x4v(const char* name, int count, float* v);
228
229 // How many outputs does this program produce
230 // only valid for OpenGL 3.2 or later
231 vtkSetMacro(NumberOfOutputs, unsigned int);
232
244 static bool Substitute(
245 std::string& source, const std::string& search, const std::string& replace, bool all = true);
246
258 static bool Substitute(
259 vtkShader* shader, const std::string& search, const std::string& replace, bool all = true);
260
266 bool IsUniformUsed(const char*);
267
272 bool IsAttributeUsed(const char* name);
273
274 // maps of std::string are super slow when calling find
275 // with a string literal or const char * as find
276 // forces construction/copy/destruction of a
277 // std::string copy of the const char *
278 // In spite of the doubters this can really be a
279 // huge CPU hog.
280 struct cmp_str
281 {
282 bool operator()(const char* a, const char* b) const { return strcmp(a, b) < 0; }
283 };
284
286
303 vtkSetFilePathMacro(FileNamePrefixForDebugging);
304 vtkGetFilePathMacro(FileNamePrefixForDebugging);
306
308
314 {
317 UserGroup, // always will be last
318 };
322
323 // returns the location for a uniform or attribute in
324 // this program. Is cached for performance.
325 int FindUniform(const char* name);
326 int FindAttributeArray(const char* name);
327
328protected:
331
332 /***************************************************************
333 * The following functions are only for use by the shader cache
334 * which is why they are protected and that class is a friend
335 * you need to use the shader cache to compile/link/bind your shader
336 * do not try to do it yourself as it will screw up the cache
337 ***************************************************************/
339
346 bool AttachShader(const vtkShader* shader);
347
353 bool DetachShader(const vtkShader* shader);
354
358 virtual int CompileShader();
359
365 bool Link();
366
371 bool Bind();
372
374 void Release();
375
376 /************* end **************************************/
377
385
386 // hash of the shader program
387 std::string MD5Hash;
388
390 const char* name, void* buffer, int type, int tupleSize, NormalizeOption normalize);
398
399 bool Linked;
400 bool Bound;
402
403 // for glsl 1.5 or later, how many outputs
404 // does this shader create
405 // they will be bound in order to
406 // fragOutput0 fragOutput1 etc...
407 unsigned int NumberOfOutputs;
408
409 std::string Error;
410
411 // since we are using const char * arrays we have to
412 // free our memory :-)
413 void ClearMaps();
414 std::map<const char*, int, cmp_str> AttributeLocs;
415 std::map<const char*, int, cmp_str> UniformLocs;
416
417 std::map<int, vtkMTimeType> UniformGroupMTimes;
418
419 friend class VertexArrayObject;
420
421private:
422 vtkShaderProgram(const vtkShaderProgram&) = delete;
423 void operator=(const vtkShaderProgram&) = delete;
424
425 // print shader code and report error
426 void ReportShaderError(vtkShader* shader);
427
428 char* FileNamePrefixForDebugging;
429};
430
431VTK_ABI_NAMESPACE_END
432#endif
RealT tm
Definition PyrC2Basis.h:35
a simple class to control print indentation
Definition vtkIndent.h:29
represent and manipulate 3x3 transformation matrices
represent and manipulate 4x4 transformation matrices
bool Link()
Attempt to link the shader program.
bool SetUniform4fv(const char *name, int count, const float *f)
void Release()
Releases the shader program from the current context.
void SetTessControlShader(vtkShader *)
Get/set the tess control shader for this program.
vtkShader * FragmentShader
~vtkShaderProgram() override
void SetFragmentShader(vtkShader *)
Get/set the fragment shader for this program.
std::string GetError() const
Get the error message (empty if none) for the shader program.
bool IsUniformUsed(const char *)
methods to inquire as to what uniforms/attributes are used by this shader.
bool Bind()
Bind the program in order to use it.
bool SetAttributeArray(const char *name, const T &array, int tupleSize, NormalizeOption normalize)
Upload the supplied array of tightly packed values to the named attribute.
vtkSetFilePathMacro(FileNamePrefixForDebugging)
When developing shaders, it's often convenient to tweak the shader and re-render incrementally.
void SetVertexShader(vtkShader *)
Get/set the vertex shader for this program.
bool SetUniform2i(const char *name, const int v[2])
vtkTransformFeedback * TransformFeedback
bool SetUniform3f(const char *name, const float v[3])
int FindAttributeArray(const char *name)
std::string GetMD5Hash() const
Set/Get the md5 hash of this program.
bool SetUniform4uc(const char *name, const unsigned char v[4])
std::map< const char *, int, cmp_str > UniformLocs
bool SetUniform4f(const char *name, const float v[4])
bool SetUniform3fv(const char *name, int count, const float *f)
bool SetUniform2f(const char *name, const float v[2])
bool SetUniformMatrix(const char *name, vtkMatrix4x4 *v)
UniformGroups
Set/Get times that can be used to track when a set of uniforms was last updated.
bool AttachShader(const vtkShader *shader)
Attach the supplied shader to this program.
bool SetUniform3fv(const char *name, int count, const float(*f)[3])
bool SetUniform4fv(const char *name, int count, const float(*f)[4])
int FindUniform(const char *name)
bool SetAttributeArrayInternal(const char *name, void *buffer, int type, int tupleSize, NormalizeOption normalize)
bool SetUniform2fv(const char *name, int count, const float(*f)[2])
static vtkShaderProgram * New()
int GetHandle() const
Get the handle of the shader program.
bool SetUniform1fv(const char *name, int count, const float *f)
NormalizeOption
Options for attribute normalization.
@ NoNormalize
The values should be used as-is. Do not perform any normalization.
@ Normalize
The values range across the limits of the numeric type.
bool SetUniform3f(const char *name, const double v[3])
vtkShader * TessControlShader
void SetMD5Hash(const std::string &hash)
bool EnableAttributeArray(const char *name)
Enable the named attribute array.
vtkShader * VertexShader
vtkShader * ComputeShader
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
bool isBound() const
Check if the program is currently bound, or not.
void SetUniformGroupUpdateTime(int, vtkMTimeType tm)
Set/Get times that can be used to track when a set of uniforms was last updated.
bool SetUniformMatrix(const char *name, vtkMatrix3x3 *v)
std::map< const char *, int, cmp_str > AttributeLocs
bool DetachShader(const vtkShader *shader)
Detach the supplied shader from this program.
bool SetUniform2fv(const char *name, int count, const float *f)
bool SetUniformf(const char *name, float v)
bool SetUniformMatrix4x4(const char *name, float *v)
vtkShader * GeometryShader
bool SetUniformMatrix3x3(const char *name, float *v)
bool DisableAttributeArray(const char *name)
Disable the named attribute array.
bool SetUniform1iv(const char *name, int count, const int *f)
Set the name uniform array to f with count elements.
vtkShader * TessEvaluationShader
bool SetUniform3uc(const char *name, const unsigned char v[3])
void SetTessEvaluationShader(vtkShader *)
Get/set the tess evaluation shader for this program.
bool IsAttributeUsed(const char *name)
Return true if the compiled and linked shader has an attribute matching name.
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.
bool SetUniformi(const char *name, int v)
Set the name uniform value to int v.
unsigned int NumberOfOutputs
bool SetUniformMatrix4x4v(const char *name, int count, float *v)
vtkMTimeType GetUniformGroupUpdateTime(int)
Set/Get times that can be used to track when a set of uniforms was last updated.
friend class VertexArrayObject
vtkGetFilePathMacro(FileNamePrefixForDebugging)
When developing shaders, it's often convenient to tweak the shader and re-render incrementally.
void SetGeometryShader(vtkShader *)
Get/set the geometry shader for this program.
std::map< int, vtkMTimeType > UniformGroupMTimes
void ReleaseGraphicsResources(vtkWindow *win)
release any graphics resources this class is using.
friend class vtkOpenGLShaderCache
static bool Substitute(std::string &source, const std::string &search, const std::string &replace, bool all=true)
perform in place string substitutions, indicate if a substitution was done this is useful for buildin...
virtual int CompileShader()
Compile this shader program and attached shaders.
void SetTransformFeedback(vtkTransformFeedback *tfc)
Get/Set a TransformFeedbackCapture object on this shader program.
static bool Substitute(vtkShader *shader, const std::string &search, const std::string &replace, bool all=true)
Perform in-place string substitutions on the shader source string and indicate if one or all substitu...
void SetComputeShader(vtkShader *)
Get/set the compute shader for this program.
Vertex or Fragment shader, combined into a ShaderProgram.
Definition vtkShader.h:31
Manages a TransformFeedback buffer.
window superclass for vtkRenderWindow
Definition vtkWindow.h:29
bool operator()(const char *a, const char *b) const
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
vtkTypeUInt32 vtkMTimeType
Definition vtkType.h:270