libdonut  2.3.2
Application framework for cross-platform game development in C++20
ShaderParameter.hpp
Go to the documentation of this file.
1 #ifndef DONUT_GRAPHICS_SHADER_PARAMETER_HPP
2 #define DONUT_GRAPHICS_SHADER_PARAMETER_HPP
3 
4 #include <array> // std::array
5 #include <cstddef> // std::size_t
6 #include <cstdint> // std::int32_t
7 #include <string> // std::string, std::to_string
8 #include <utility> // std::index_sequence, std::make_index_sequence
9 
10 namespace donut::graphics {
11 
12 class ShaderProgram; // Forward declaration, to avoid a circular include of ShaderProgram.hpp.
13 
18 public:
28  ShaderParameter(const ShaderProgram& program, const char* name);
29 
35  [[nodiscard]] std::int32_t getLocation() const noexcept {
36  return location;
37  }
38 
39 private:
40  std::int32_t location;
41 };
42 
51 template <typename T, std::size_t N>
52 class ShaderArray {
53 public:
66  ShaderArray(const ShaderProgram& program, const char* name)
67  : array([&]<std::size_t... Indices>(std::index_sequence<Indices...>) -> std::array<T, N> {
68  return {(T{program, (std::string{name} + "[" + std::to_string(Indices) + "]").c_str()})...};
69  }(std::make_index_sequence<N>{})) {}
70 
77  [[nodiscard]] std::size_t size() const noexcept {
78  return array.size();
79  }
80 
89  [[nodiscard]] T& operator[](std::size_t i) {
90  return array[i];
91  }
92 
101  [[nodiscard]] const T& operator[](std::size_t i) const {
102  return array[i];
103  }
104 
110  [[nodiscard]] decltype(auto) begin() const noexcept {
111  return array.begin();
112  }
113 
119  [[nodiscard]] decltype(auto) end() const noexcept {
120  return array.end();
121  }
122 
123 private:
124  std::array<T, N> array;
125 };
126 
127 } // namespace donut::graphics
128 
129 #endif
Fixed-size array of uniform shader variable identifiers representing an array inside a ShaderProgram.
Definition: ShaderParameter.hpp:52
T & operator[](std::size_t i)
Access an element of the array.
Definition: ShaderParameter.hpp:89
decltype(auto) end() const noexcept
Get an iterator to the end of the array.
Definition: ShaderParameter.hpp:119
std::size_t size() const noexcept
Get the size of the array.
Definition: ShaderParameter.hpp:77
const T & operator[](std::size_t i) const
Access an element of the array.
Definition: ShaderParameter.hpp:101
ShaderArray(const ShaderProgram &program, const char *name)
Construct an array of uniform shader variable identifiers for a specific shader array.
Definition: ShaderParameter.hpp:66
decltype(auto) begin() const noexcept
Get an iterator to the beginning of the array.
Definition: ShaderParameter.hpp:110
Identifier for a uniform shader variable inside a ShaderProgram.
Definition: ShaderParameter.hpp:17
std::int32_t getLocation() const noexcept
Get the location of the variable in the shader program.
Definition: ShaderParameter.hpp:35
ShaderParameter(const ShaderProgram &program, const char *name)
Construct an identifier for a specific uniform shader variable.
Compiled and linked GPU shader program.
Definition: ShaderProgram.hpp:46
Definition: Buffer.hpp:7