Debian Codegraph @

Sources/nCine/Graphics/RHI/GXM/GxmShaderTypes.h

raw
1 #pragma once
2
3 #include "../../../../Shaders/Generated/ShaderCompilerTypes.h"
4
5 #include <cstddef>
6 #include <cstdint>
7 #include <cstring>
8
9 #include <Containers/SmallVector.h>
10
11 using namespace Death::Containers;
12
13 namespace nCine::RHI::GXM
14 {
15 class GxmShaderProgram;
16
17 namespace UniformTypeInfo
18 {
19 /** @brief Number of scalar components of a reflected uniform type (0 for aggregates) */
20 inline std::uint32_t ComponentCount(ShaderCompiler::UniformType type)
21 {
22 switch (type) {
23 case ShaderCompiler::UniformType::Float:
24 case ShaderCompiler::UniformType::Int:
25 case ShaderCompiler::UniformType::UInt:
26 case ShaderCompiler::UniformType::Bool:
27 case ShaderCompiler::UniformType::Sampler2D:
28 case ShaderCompiler::UniformType::Sampler3D:
29 case ShaderCompiler::UniformType::SamplerCube:
30 return 1;
31 case ShaderCompiler::UniformType::Vec2:
32 case ShaderCompiler::UniformType::IVec2:
33 case ShaderCompiler::UniformType::UVec2:
34 case ShaderCompiler::UniformType::BVec2:
35 return 2;
36 case ShaderCompiler::UniformType::Vec3:
37 case ShaderCompiler::UniformType::IVec3:
38 case ShaderCompiler::UniformType::UVec3:
39 case ShaderCompiler::UniformType::BVec3:
40 return 3;
41 case ShaderCompiler::UniformType::Vec4:
42 case ShaderCompiler::UniformType::IVec4:
43 case ShaderCompiler::UniformType::UVec4:
44 case ShaderCompiler::UniformType::BVec4:
45 return 4;
46 case ShaderCompiler::UniformType::Mat2: return 4;
47 case ShaderCompiler::UniformType::Mat3: return 9;
48 case ShaderCompiler::UniformType::Mat4: return 16;
49 default: return 0;
50 }
51 }
52
53 /** @brief Returns `true` if the basic component type of the uniform is floating point */
54 inline bool IsFloat(ShaderCompiler::UniformType type)
55 {
56 switch (type) {
57 case ShaderCompiler::UniformType::Float:
58 case ShaderCompiler::UniformType::Vec2:
59 case ShaderCompiler::UniformType::Vec3:
60 case ShaderCompiler::UniformType::Vec4:
61 case ShaderCompiler::UniformType::Mat2:
62 case ShaderCompiler::UniformType::Mat3:
63 case ShaderCompiler::UniformType::Mat4:
64 return true;
65 default:
66 return false;
67 }
68 }
69 }
70
71 /**
72 @brief Reflected metadata of a single active uniform (aliased as `RHI::Uniform`)
73
74 Holds everything the backend needs about one uniform imported from the offline reflection: its name,
75 scalar/vector/matrix type, array size, a synthetic sequential location and, for uniforms living inside
76 a block, the owning block index and std140 byte offset. It carries no value storage — a @ref
77 GxmUniformCache holds the value.
78 */
79 class GxmUniform
80 {
81 friend class GxmShaderProgram;
82 friend class GxmUniformBlock;
83
84 public:
85 /** @brief Maximum length of a uniform name, including the terminating null character */
86 static constexpr std::uint32_t MaxNameLength = 48;
87
88 GxmUniform()
89 : _type(ShaderCompiler::UniformType::Float), _size(0), _location(-1), _blockIndex(-1), _offset(0), _owner(nullptr)
90 {
91 _name[0] = '\0';
92 }
93 GxmUniform(GxmShaderProgram* owner, const char* name, ShaderCompiler::UniformType type, std::int32_t arraySize, std::int32_t location)
94 : _type(type), _size(arraySize > 0 ? arraySize : 1), _location(location), _blockIndex(-1), _offset(0), _owner(owner)
95 {
96 SetName(name);
97 }
98
99 inline std::int32_t GetLocation() const {
100 return _location;
101 }
102 inline std::int32_t GetBlockIndex() const {
103 return _blockIndex;
104 }
105 inline std::int32_t GetSize() const {
106 return _size;
107 }
108 /** @brief Returns the reflected (backend-neutral) type of the uniform */
109 inline ShaderCompiler::UniformType GetType() const {
110 return _type;
111 }
112 inline std::int32_t GetOffset() const {
113 return _offset;
114 }
115 inline const char* GetName() const {
116 return _name;
117 }
118 inline GxmShaderProgram* GetOwner() const {
119 return _owner;
120 }
121 inline std::uint32_t GetComponentCount() const {
122 return UniformTypeInfo::ComponentCount(_type);
123 }
124 inline bool IsFloat() const {
125 return UniformTypeInfo::IsFloat(_type);
126 }
127 inline std::uint32_t GetMemorySize() const {
128 return std::uint32_t(GetSize()) * GetComponentCount() * 4u;
129 }
130
131 private:
132 char _name[MaxNameLength];
133 ShaderCompiler::UniformType _type;
134 std::int32_t _size;
135 std::int32_t _location;
136 std::int32_t _blockIndex;
137 std::int32_t _offset;
138 GxmShaderProgram* _owner;
139
140 void SetName(const char* name) {
141 std::size_t length = std::strlen(name);
142 if (length >= MaxNameLength) {
143 length = MaxNameLength - 1;
144 }
145 std::memcpy(_name, name, length);
146 _name[length] = '\0';
147 }
148 };
149
150 /**
151 @brief Reflected metadata of a single active uniform block (aliased as `RHI::UniformBlock`)
152
153 Holds the block name, a synthetic sequential index and binding index, its std140 byte size and the
154 metadata of its member uniforms (each with a std140 offset). The backend does not pad the size to any
155 device alignment, so @ref GetAlignAmount() is always 0.
156 */
157 class GxmUniformBlock
158 {
159 friend class GxmShaderProgram;
160 friend class GxmUniformBlockCache;
161
162 public:
163 static constexpr std::uint32_t MaxNameLength = 48;
164
165 enum class DiscoverUniforms
166 {
167 Enabled,
168 Disabled
169 };
170
171 GxmUniformBlock()
172 : _index(0), _size(0), _alignAmount(0), _bindingIndex(-1)
173 {
174 _name[0] = '\0';
175 }
176 GxmUniformBlock(std::uint32_t index, const char* name, std::int32_t dataSize)
177 : _index(index), _size(dataSize), _alignAmount(0), _bindingIndex(-1)
178 {
179 SetName(name);
180 }
181
182 inline std::uint32_t GetIndex() const {
183 return _index;
184 }
185 inline std::int32_t GetBindingIndex() const {
186 return _bindingIndex;
187 }
188 inline std::int32_t GetSize() const {
189 return _size;
190 }
191 inline std::uint8_t GetAlignAmount() const {
192 return _alignAmount;
193 }
194 inline const char* GetName() const {
195 return _name;
196 }
197
198 /** @brief Returns the member uniform with the specified name, or `nullptr` if not found */
199 GxmUniform* GetUniform(const char* name) {
200 for (GxmUniform& u : _members) {
201 if (std::strcmp(u.GetName(), name) == 0) {
202 return &u;
203 }
204 }
205 return nullptr;
206 }
207 void SetBlockBinding(std::int32_t blockBinding) {
208 _bindingIndex = blockBinding;
209 }
210
211 private:
212 char _name[MaxNameLength];
213 std::uint32_t _index;
214 std::int32_t _size;
215 std::uint8_t _alignAmount;
216 std::int32_t _bindingIndex;
217 SmallVector<GxmUniform, 0> _members;
218
219 void SetName(const char* name) {
220 std::size_t length = std::strlen(name);
221 if (length >= MaxNameLength) {
222 length = MaxNameLength - 1;
223 }
224 std::memcpy(_name, name, length);
225 _name[length] = '\0';
226 }
227 };
228
229 /**
230 @brief Reflected metadata of a single active vertex attribute (aliased as `RHI::Attribute`)
231 */
232 class GxmAttribute
233 {
234 public:
235 static constexpr std::uint32_t MaxNameLength = 32;
236
237 GxmAttribute()
238 : _type(ShaderCompiler::UniformType::Float), _location(-1)
239 {
240 _name[0] = '\0';
241 }
242 GxmAttribute(const char* name, ShaderCompiler::UniformType type, std::int32_t location)
243 : _type(type), _location(location)
244 {
245 std::size_t length = std::strlen(name);
246 if (length >= MaxNameLength) {
247 length = MaxNameLength - 1;
248 }
249 std::memcpy(_name, name, length);
250 _name[length] = '\0';
251 }
252
253 inline std::int32_t GetLocation() const {
254 return _location;
255 }
256 inline ShaderCompiler::UniformType GetType() const {
257 return _type;
258 }
259 inline const char* GetName() const {
260 return _name;
261 }
262 inline std::int32_t GetComponentCount() const {
263 return std::int32_t(UniformTypeInfo::ComponentCount(_type));
264 }
265
266 private:
267 char _name[MaxNameLength];
268 ShaderCompiler::UniformType _type;
269 std::int32_t _location;
270 };
271 }