VTK  9.6.20260109
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
73
74#ifndef vtkShaderProgram_h
75#define vtkShaderProgram_h
76
77#include "vtkDeprecation.h" // VTK_DEPRECATED_IN_9_6_0()
78#include "vtkObject.h"
79#include "vtkRenderingOpenGL2Module.h" // for export macro
80
81#include <map> // For member variables.
82#include <string> // For member variables.
83
84VTK_ABI_NAMESPACE_BEGIN
85class vtkMatrix3x3;
86class vtkMatrix4x4;
88class vtkShader;
89class VertexArrayObject;
90class vtkWindow;
91
100
101class VTKRENDERINGOPENGL2_EXPORT vtkShaderProgram : public vtkObject
102{
103public:
106 void PrintSelf(ostream& os, vtkIndent indent) override;
107
109
112 vtkGetObjectMacro(VertexShader, vtkShader);
115
117
120 vtkGetObjectMacro(FragmentShader, vtkShader);
123
125
128 vtkGetObjectMacro(GeometryShader, vtkShader);
131
133
136 vtkGetObjectMacro(ComputeShader, vtkShader);
139
141
144 vtkGetObjectMacro(TessControlShader, vtkShader);
147
149
152 vtkGetObjectMacro(TessEvaluationShader, vtkShader);
155
157
163
165
168 vtkGetMacro(Compiled, bool);
169 vtkSetMacro(Compiled, bool);
170 vtkBooleanMacro(Compiled, bool);
172
176 std::string GetHash() const { return this->MD5Hash; }
177 void SetHash(const std::string& hash) { this->MD5Hash = hash; }
178
182 VTK_DEPRECATED_IN_9_6_0("Use GetHash instead.")
183 std::string GetMD5Hash() const { return this->MD5Hash; }
184 VTK_DEPRECATED_IN_9_6_0("Use SetHash instead.")
185 void SetMD5Hash(const std::string& hash) { this->MD5Hash = hash; }
186
202
207 bool isBound() const { return this->Bound; }
208
213
215 int GetHandle() const { return Handle; }
216
218 std::string GetError() const { return Error; }
219
224 bool EnableAttributeArray(const char* name);
225
230 bool DisableAttributeArray(const char* name);
231
247 bool UseAttributeArray(const char* name, int offset, size_t stride, int elementType,
248 int elementTupleSize, NormalizeOption normalize);
249
267 template <class T>
269 const char* name, const T& array, int tupleSize, NormalizeOption normalize);
270
272 bool SetUniformi(const char* name, int v);
273 bool SetUniformf(const char* name, float v);
274 bool SetUniform2i(const char* name, const int v[2]);
275 bool SetUniform2f(const char* name, const float v[2]);
276 bool SetUniform3f(const char* name, const float v[3]);
277 bool SetUniform3f(const char* name, const double v[3]);
278 bool SetUniform4f(const char* name, const float v[4]);
279 bool SetUniform3uc(const char* name, const unsigned char v[3]); // maybe remove
280 bool SetUniform4uc(const char* name, const unsigned char v[4]); // maybe remove
281 bool SetUniformMatrix(const char* name, vtkMatrix3x3* v);
282 bool SetUniformMatrix(const char* name, vtkMatrix4x4* v);
283 bool SetUniformMatrix3x3(const char* name, float* v);
284 bool SetUniformMatrix4x4(const char* name, float* v);
285
287 bool SetUniform1iv(const char* name, int count, const int* f);
288 bool SetUniform1fv(const char* name, int count, const float* f);
289 bool SetUniform2fv(const char* name, int count, const float* f);
290 bool SetUniform2fv(const char* name, int count, const float (*f)[2]);
291 bool SetUniform3fv(const char* name, int count, const float* f);
292 bool SetUniform3fv(const char* name, int count, const float (*f)[3]);
293 bool SetUniform4fv(const char* name, int count, const float* f);
294 bool SetUniform4fv(const char* name, int count, const float (*f)[4]);
295 bool SetUniformMatrix4x4v(const char* name, int count, float* v);
296
297 // How many outputs does this program produce
298 // only valid for OpenGL 3.2 or later
299 vtkSetMacro(NumberOfOutputs, unsigned int);
300
312 static bool Substitute(
313 std::string& source, const std::string& search, const std::string& replace, bool all = true);
314
326 static bool Substitute(
327 vtkShader* shader, const std::string& search, const std::string& replace, bool all = true);
328
334 bool IsUniformUsed(const char*);
335
340 bool IsAttributeUsed(const char* name);
341
342 // maps of std::string are super slow when calling find
343 // with a string literal or const char * as find
344 // forces construction/copy/destruction of a
345 // std::string copy of the const char *
346 // In spite of the doubters this can really be a
347 // huge CPU hog.
348 struct cmp_str
349 {
350 bool operator()(const char* a, const char* b) const { return strcmp(a, b) < 0; }
351 };
352
354
371 vtkSetFilePathMacro(FileNamePrefixForDebugging);
372 vtkGetFilePathMacro(FileNamePrefixForDebugging);
374
376
382 {
385 UserGroup, // always will be last
386 };
390
391 // returns the location for a uniform or attribute in
392 // this program. Is cached for performance.
393 int FindUniform(const char* name);
394 int FindAttributeArray(const char* name);
395
396protected:
399
400 /***************************************************************
401 * The following functions are only for use by the shader cache
402 * which is why they are protected and that class is a friend
403 * you need to use the shader cache to compile/link/bind your shader
404 * do not try to do it yourself as it will screw up the cache
405 ***************************************************************/
407
414 bool AttachShader(const vtkShader* shader);
415
421 bool DetachShader(const vtkShader* shader);
422
426 virtual int CompileShader();
427
433 bool Link();
434
439 bool Bind();
440
442 void Release();
443
444 /************* end **************************************/
445
453
454 // hash of the shader program
455 std::string MD5Hash;
456
458 const char* name, void* buffer, int type, int tupleSize, NormalizeOption normalize);
466
467 bool Linked;
468 bool Bound;
470
471 // for glsl 1.5 or later, how many outputs
472 // does this shader create
473 // they will be bound in order to
474 // fragOutput0 fragOutput1 etc...
475 unsigned int NumberOfOutputs;
476
477 std::string Error;
478
479 // since we are using const char * arrays we have to
480 // free our memory :-)
481 void ClearMaps();
482 std::map<const char*, int, cmp_str> AttributeLocs;
483 std::map<const char*, int, cmp_str> UniformLocs;
484
485 std::map<int, vtkMTimeType> UniformGroupMTimes;
486
487 friend class VertexArrayObject;
488
489private:
490 vtkShaderProgram(const vtkShaderProgram&) = delete;
491 void operator=(const vtkShaderProgram&) = delete;
492
493 // print shader code and report error
494 void ReportShaderError(vtkShader* shader);
495
496 char* FileNamePrefixForDebugging;
497};
498
499VTK_ABI_NAMESPACE_END
500#endif
RealT tm
Definition PyrC2Basis.h:35
a simple class to control print indentation
Definition vtkIndent.h:108
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.
std::string GetHash() const
Set/Get the hash of this program.
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 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.
void SetHash(const std::string &hash)
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:120
Manages a TransformFeedback buffer.
window superclass for vtkRenderWindow
Definition vtkWindow.h:48
bool operator()(const char *a, const char *b) const
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
#define VTK_DEPRECATED_IN_9_6_0(reason)
vtkTypeUInt32 vtkMTimeType
Definition vtkType.h:323