libdonut  2.3.2
Application framework for cross-platform game development in C++20
ShaderProgram.hpp
Go to the documentation of this file.
1 #ifndef DONUT_GRAPHICS_SHADER_PROGRAM_HPP
2 #define DONUT_GRAPHICS_SHADER_PROGRAM_HPP
3 
4 #include <donut/UniqueHandle.hpp>
5 #include <donut/Variant.hpp>
8 #include <donut/math.hpp>
9 
10 #include <cstdint> // std::int32_t
11 #include <span> // std::span
12 #include <utility> // std::pair
13 #include <vector> // std::vector
14 
15 namespace donut::graphics {
16 
17 class Texture; // Forward declaration, to avoid including Texture.hpp.
18 class ShaderParameter; // Forward declaration, to avoid a circular include of ShaderParameter.hpp.
19 
28  const char* definitions = nullptr;
29 
34  const char* vertexShaderSourceCode = nullptr;
35 
40  const char* fragmentShaderSourceCode = nullptr;
41 };
42 
47 public:
51  using UniformValue = Variant< //
52  float, //
53  vec2, //
54  vec3, //
55  vec4, //
56  i32, //
57  i32vec2, //
58  i32vec3, //
59  i32vec4, //
60  u32, //
61  u32vec2, //
62  u32vec3, //
63  u32vec4, //
64  mat2, //
65  mat3, //
66  mat4>;
67 
78  explicit ShaderProgram(const ShaderProgramOptions& options);
79 
96  void setUniformFloat(const ShaderParameter& parameter, float value);
97 
114  void setUniformVec2(const ShaderParameter& parameter, vec2 value);
115 
132  void setUniformVec3(const ShaderParameter& parameter, vec3 value);
133 
150  void setUniformVec4(const ShaderParameter& parameter, vec4 value);
151 
168  void setUniformInt(const ShaderParameter& parameter, i32 value);
169 
186  void setUniformIVec2(const ShaderParameter& parameter, i32vec2 value);
187 
204  void setUniformIVec3(const ShaderParameter& parameter, i32vec3 value);
205 
222  void setUniformIVec4(const ShaderParameter& parameter, i32vec4 value);
223 
240  void setUniformUint(const ShaderParameter& parameter, u32 value);
241 
258  void setUniformUVec2(const ShaderParameter& parameter, u32vec2 value);
259 
276  void setUniformUVec3(const ShaderParameter& parameter, u32vec3 value);
277 
294  void setUniformUVec4(const ShaderParameter& parameter, u32vec4 value);
295 
312  void setUniformMat2(const ShaderParameter& parameter, const mat2& value);
313 
330  void setUniformMat3(const ShaderParameter& parameter, const mat3& value);
331 
348  void setUniformMat4(const ShaderParameter& parameter, const mat4& value);
349 
371  void setUniformSampler(const ShaderParameter& parameter, const Texture* texture);
372 
376  void clearUniformUploadQueue() noexcept {
377  uniformUploadQueue.clear();
378  }
379 
383  void clearTextureBindings() noexcept {
384  textureBindings.clear();
385  }
386 
395  [[nodiscard]] std::span<const std::pair<std::int32_t, UniformValue>> getUniformUploadQueue() const noexcept {
396  return uniformUploadQueue;
397  }
398 
408  [[nodiscard]] std::span<const std::pair<std::int32_t, const Texture*>> getTextureBindings() const noexcept {
409  return textureBindings;
410  }
411 
422  [[nodiscard]] Handle get() const noexcept {
423  return program.get();
424  }
425 
426 private:
427  struct ProgramDeleter {
428  void operator()(Handle handle) const noexcept;
429  };
430 
432  ShaderStage vertexShader;
433  ShaderStage fragmentShader;
434  std::vector<std::pair<std::int32_t, UniformValue>> uniformUploadQueue{};
435  std::vector<std::pair<std::int32_t, const Texture*>> textureBindings{};
436 };
437 
438 } // namespace donut::graphics
439 
440 #endif
constexpr Handle get() const noexcept
Get the value of the underlying resource handle.
Definition: UniqueHandle.hpp:152
Tagged union value type that holds a value of one of the given types.
Definition: Variant.hpp:291
Identifier for a uniform shader variable inside a ShaderProgram.
Definition: ShaderParameter.hpp:17
Compiled and linked GPU shader program.
Definition: ShaderProgram.hpp:46
void setUniformIVec2(const ShaderParameter &parameter, i32vec2 value)
Enqueue a new value to be set for a uniform shader variable of GLSL type ivec2.
void setUniformVec2(const ShaderParameter &parameter, vec2 value)
Enqueue a new value to be set for a uniform shader variable of GLSL type vec2.
void setUniformVec4(const ShaderParameter &parameter, vec4 value)
Enqueue a new value to be set for a uniform shader variable of GLSL type vec4.
void setUniformFloat(const ShaderParameter &parameter, float value)
Enqueue a new value to be set for a uniform shader variable of GLSL type float.
Handle get() const noexcept
Get an opaque handle to the GPU representation of the shader program.
Definition: ShaderProgram.hpp:422
void setUniformMat2(const ShaderParameter &parameter, const mat2 &value)
Enqueue a new value to be set for a uniform shader variable of GLSL type mat2.
ShaderProgram(const ShaderProgramOptions &options)
Compile and link a shader program.
std::span< const std::pair< std::int32_t, const Texture * > > getTextureBindings() const noexcept
Get the list of active texture bindings to be synchronized with the shader.
Definition: ShaderProgram.hpp:408
void setUniformMat3(const ShaderParameter &parameter, const mat3 &value)
Enqueue a new value to be set for a uniform shader variable of GLSL type mat3.
void setUniformUVec2(const ShaderParameter &parameter, u32vec2 value)
Enqueue a new value to be set for a uniform shader variable of GLSL type uvec2.
void setUniformSampler(const ShaderParameter &parameter, const Texture *texture)
Insert a new texture to be bound for a uniform shader variable of a GLSL sampler type.
void setUniformIVec4(const ShaderParameter &parameter, i32vec4 value)
Enqueue a new value to be set for a uniform shader variable of GLSL type ivec4.
void setUniformIVec3(const ShaderParameter &parameter, i32vec3 value)
Enqueue a new value to be set for a uniform shader variable of GLSL type ivec3.
std::span< const std::pair< std::int32_t, UniformValue > > getUniformUploadQueue() const noexcept
Get the list of new uniform shader variable values to be uploaded to the shader.
Definition: ShaderProgram.hpp:395
void setUniformVec3(const ShaderParameter &parameter, vec3 value)
Enqueue a new value to be set for a uniform shader variable of GLSL type vec3.
void setUniformUVec3(const ShaderParameter &parameter, u32vec3 value)
Enqueue a new value to be set for a uniform shader variable of GLSL type uvec3.
void setUniformUVec4(const ShaderParameter &parameter, u32vec4 value)
Enqueue a new value to be set for a uniform shader variable of GLSL type uvec4.
void setUniformUint(const ShaderParameter &parameter, u32 value)
Enqueue a new value to be set for a uniform shader variable of GLSL type uint.
void setUniformMat4(const ShaderParameter &parameter, const mat4 &value)
Enqueue a new value to be set for a uniform shader variable of GLSL type mat4.
void clearUniformUploadQueue() noexcept
Erase all entries from the queue of new uniform shader variable values.
Definition: ShaderProgram.hpp:376
void setUniformInt(const ShaderParameter &parameter, i32 value)
Enqueue a new value to be set for a uniform shader variable of GLSL type int.
void clearTextureBindings() noexcept
Erase all entries from the list of active texture bindings.
Definition: ShaderProgram.hpp:383
Storage for multidimensional data, such as 2D images, on the GPU, combined with a sampler configurati...
Definition: Texture.hpp:75
Definition: Buffer.hpp:7
std::uint32_t Handle
Generic GPU resource handle.
Definition: Handle.hpp:11
Configuration options for a ShaderProgram.
Definition: ShaderProgram.hpp:23
const char * definitions
Non-owning pointer to GLSL source code for constant definitions to add to the beginning of the source...
Definition: ShaderProgram.hpp:28
const char * fragmentShaderSourceCode
Non-owning pointer to the GLSL source code for the fragment shader stage, or nullptr to use the built...
Definition: ShaderProgram.hpp:40
const char * vertexShaderSourceCode
Non-owning pointer to the GLSL source code for the vertex shader stage, or nullptr to use the built-i...
Definition: ShaderProgram.hpp:34