Skip to main content

UniformValueMap.ixx File

Mapping of uniform names to values used by shader programs. More...

Included Headers

#include <array> #include <optional> #include <variant> #include <helios.math.types> #include <helios.rendering.shader.UniformSemantics>

Namespaces Index

namespacehelios
namespacerendering

Graphics rendering infrastructure. More...

namespaceshader

Shader program management. More...

Classes Index

classUniformValueMap

Maps uniform semantics to their values for shader parameter binding. More...

Description

Mapping of uniform names to values used by shader programs.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file UniformValueMap.ixx
3 * @brief Mapping of uniform names to values used by shader programs.
4 */
5module;
6
7#include <array>
8#include <optional>
9#include <variant>
10
11export module helios.rendering.shader.UniformValueMap;
12
13import helios.rendering.shader.UniformSemantics;
14
15import helios.math.types;
16
17export namespace helios::rendering::shader {
18
19 /**
20 * @brief A type alias for the variant containing all supported
21 * uniform types.
22 *
23 * @see [Str22, 100]
24 */
25 using UniformValue = std::variant<
28 float,
29 int
30 >;
31
32 /**
33 * @brief Maps uniform semantics to their values for shader parameter binding.
34 *
35 * `UniformValueMap` stores uniform values (matrices, vectors, scalars) indexed by
36 * their `UniformSemantics` identifier. This allows the rendering pipeline to
37 * efficiently pass data to shaders without string-based lookups.
38 *
39 * ## Supported Types
40 *
41 * - `mat4f` – 4x4 transformation matrices
42 * - `vec4f` – 4-component vectors (colors, positions)
43 * - `float` – Scalar values (roughness, time)
44 * - `int` – Integer values (texture units)
45 *
46 * ## Usage
47 *
48 * ```cpp
49 * UniformValueMap uniforms;
50 *
51 * // Set transform matrices
52 * uniforms.set(UniformSemantics::ModelMatrix, modelMat);
53 * uniforms.set(UniformSemantics::ViewMatrix, viewMat);
54 *
55 * // Set material properties
56 * uniforms.set(UniformSemantics::MaterialBaseColor, vec4f{1.0f, 0.0f, 0.0f, 1.0f});
57 *
58 * // Retrieve raw pointer for OpenGL
59 * const float* modelPtr = uniforms.mat4f_ptr(UniformSemantics::ModelMatrix);
60 * ```
61 *
62 * @todo UniformMap must allow only one index for all data structures,
63 * i.e. if map_[semantics] contains a mat4f, other types should not be allowed
64 * for the same semantics.
65 *
66 * @todo UniformValueMaps should be scoped, e.g. per frame (world matrix,
67 * projection, view...), per material (colors, emissive...), per object (world matrix...),
68 * which allows for better handling of assigning uniforms: per-frame values change once per frame,
69 * while per-object values change per object.
70 */
72
73 private:
74 /**
75 * @brief Internal data structure storing the association between a uniform semantic and
76 * a concrete value.
77 */
78 std::array<std::optional<UniformValue>, std::to_underlying(UniformSemantics::size_)> map_;
79
80 public:
81
82 /**
83 * @brief Default destructor.
84 */
85 ~UniformValueMap() = default;
86
87 /**
88 * @brief Default constructor.
89 *
90 * Initializes an empty uniform value map with no values set.
91 */
92 UniformValueMap() = default;
93
94 /**
95 * @brief Sets or updates a `helios::math::mat4f` uniform value for a given semantic.
96 *
97 * @param uniformSemantics The `UniformSemantics` identifier for the uniform.
98 * @param mat4f A const ref to the mat4f value to set.
99 */
100 void set(UniformSemantics uniformSemantics, const helios::math::mat4f& mat4f) noexcept {
101 map_[std::to_underlying(uniformSemantics)].emplace(mat4f);
102 }
103
104 /**
105 * @brief Returns a raw const pointer to the `mat4f` for the specified uniform semantics.
106 *
107 * @param uniformSemantics The `UniformSemantics` identifier for the uniform.
108 *
109 * @return A raw const pointer to the associated mat4f, or `nullptr` if no mat4f is
110 * associated with this semantics.
111 */
112 [[nodiscard]] const float* mat4f_ptr(UniformSemantics uniformSemantics) const noexcept {
113
114 const auto index = std::to_underlying(uniformSemantics);
115
116 if (index >= map_.size()) {
117 return nullptr;
118 }
119
120 if (const auto& el = map_[index]; el.has_value()) {
121 if (const auto* it = std::get_if<helios::math::mat4f>(&el.value())) {
122 return helios::math::value_ptr(*it);
123 }
124 }
125
126 return nullptr;
127 }
128
129 /**
130 * @brief Returns a const pointer to the `mat4f` value for the specified uniform semantics.
131 *
132 * Unlike `mat4f_ptr()`, this returns a pointer to the `mat4f` object itself rather than
133 * its underlying float array.
134 *
135 * @param uniformSemantics The `UniformSemantics` identifier for the uniform.
136 *
137 * @return A const pointer to the associated `mat4f`, or `nullptr` if no mat4f is
138 * associated with this semantics.
139 */
140 [[nodiscard]] const helios::math::mat4f* mat4f(UniformSemantics uniformSemantics) const noexcept {
141
142 const auto index = std::to_underlying(uniformSemantics);
143
144 if (index >= map_.size()) {
145 return nullptr;
146 }
147
148 if (const auto& el = map_[index]; el.has_value()) {
149 if (const auto* it = std::get_if<helios::math::mat4f>(&el.value())) {
150 return it;
151 }
152 }
153
154 return nullptr;
155 }
156
157 /**
158 * @brief Sets or updates a `helios::math::vec4f` uniform value for a given semantic.
159 *
160 * @param uniformSemantics The `UniformSemantics` identifier for the uniform.
161 * @param vec4f A const ref to the vec4f value to set.
162 */
163 void set(UniformSemantics uniformSemantics, const helios::math::vec4f& vec4f) noexcept {
164 map_[std::to_underlying(uniformSemantics)].emplace(vec4f);
165 }
166
167 /**
168 * @brief Returns a raw const pointer to the `vec4f` for the specified uniform semantics.
169 *
170 * @param uniformSemantics The `UniformSemantics` identifier for the uniform.
171 * @return A raw const pointer to the associated vec4f, or `nullptr` if no vec4f is
172 * associated with this semantics.
173 */
174 [[nodiscard]] const float* vec4f_ptr(UniformSemantics uniformSemantics) const noexcept {
175
176 const auto index = std::to_underlying(uniformSemantics);
177
178 if (index >= map_.size()) {
179 return nullptr;
180 }
181
182 if (const auto& el = map_[index]; el.has_value()) {
183 if (const auto* it = std::get_if<helios::math::vec4f>(&el.value())) {
184 return helios::math::value_ptr(*it);
185 }
186 }
187
188 return nullptr;
189 }
190
191 /**
192 * @brief Sets or updates an integer uniform value for a given semantic.
193 *
194 * Typically used for texture unit indices.
195 *
196 * @param uniformSemantics The `UniformSemantics` identifier for the uniform.
197 * @param value The integer value to set.
198 */
199 void set(UniformSemantics uniformSemantics, int value) noexcept {
200 map_[std::to_underlying(uniformSemantics)].emplace(value);
201 }
202
203 /**
204 * @brief Sets or updates a float uniform value for a given semantic.
205 *
206 * @param uniformSemantics The `UniformSemantics` identifier for the uniform.
207 * @param value The float value to set.
208 */
209 void set(UniformSemantics uniformSemantics, float value) noexcept {
210 map_[std::to_underlying(uniformSemantics)].emplace(value);
211 }
212
213 /**
214 * @brief Returns a raw const pointer to the float value for the specified uniform semantics.
215 *
216 * This method retrieves the float value associated with a given `UniformSemantics` identifier.
217 * If no float value is associated with the specified semantics, the method returns `nullptr`.
218 *
219 * @param uniformSemantics The `UniformSemantics` identifier for the uniform.
220 *
221 * @return A raw const pointer to the associated float value, or `nullptr` if no float value
222 * is associated with this semantics.
223 */
224 [[nodiscard]] const float* float_ptr(UniformSemantics uniformSemantics) const noexcept {
225
226 const auto index = std::to_underlying(uniformSemantics);
227
228 if (index >= map_.size()) {
229 return nullptr;
230 }
231
232 if (const auto& el = map_[index]; el.has_value()) {
233 if (const auto* floatPtr = std::get_if<float>(&el.value())) {
234 return floatPtr;
235 }
236 }
237
238 return nullptr;
239 }
240
241 /**
242 * @brief Returns a raw const pointer to the integer value for the specified uniform semantics.
243 *
244 * @param uniformSemantics The `UniformSemantics` identifier for the uniform.
245 *
246 * @return A raw const pointer to the associated integer value, or `nullptr` if no integer
247 * value is associated with this semantics.
248 */
249 [[nodiscard]] const int* int_ptr(UniformSemantics uniformSemantics) const noexcept {
250
251 const auto index = std::to_underlying(uniformSemantics);
252
253 if (index >= map_.size()) {
254 return nullptr;
255 }
256
257 if (const auto& el = map_[index]; el.has_value()) {
258 if (const auto* intPtr = std::get_if<int>(&el.value())) {
259 return intPtr;
260 }
261 }
262
263 return nullptr;
264 }
265
266 };
267
268}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.