GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Mesh.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_GRAPHICS_MESH_HPP
5#define GREM_GRAPHICS_MESH_HPP
6
8
19#include <GREM/core/math.hpp>
24
25#include <type_traits> // std::is_empty_v, std::remove_cvref_t
26#include <typeindex> // std::type_index
27#include <typeinfo> // IWYU pragma: keep // typeid
28#include <utility> // std::declval
29
30namespace grem::graphics {
31
32class Device; // Forward declaration, to avoid a circular include of Device.hpp.
33struct TextureImplementation; // Forward declaration, to avoid including Texture.hpp.
34
35struct MeshImplementation;
36
42template <typename T>
44
48struct NoIndex {};
49
54enum class MeshIndexType : uint8_t {
57};
58
59namespace detail {
60
61template <typename T>
62inline constexpr Optional<MeshIndexType> MESH_INDEX_TYPE{};
63
64// clang-format off
65template <> inline constexpr Optional<MeshIndexType> MESH_INDEX_TYPE<NoIndex>{};
66template <> inline constexpr Optional<MeshIndexType> MESH_INDEX_TYPE<uint16_t> = MeshIndexType::U16;
67template <> inline constexpr Optional<MeshIndexType> MESH_INDEX_TYPE<uint32_t> = MeshIndexType::U32;
68// clang-format on
69
70} // namespace detail
71
77template <typename T>
78concept mesh_index = //
82
86struct NoParameters {};
87
93template <typename T>
95
99struct NoInstance {};
100
106template <typename T>
108
113
114namespace detail {
115
116class MeshBase {
117public:
119 ~MeshBase() = default;
120
122 MeshBase(const MeshBase&) = default;
123
125 MeshBase(MeshBase&&) noexcept = default;
126
128 MeshBase& operator=(const MeshBase&) = default;
129
131 MeshBase& operator=(MeshBase&&) noexcept = default;
132
141 [[nodiscard]] SharedPointer<MeshImplementation> lock() const {
142 return implementation;
143 }
144
153 [[nodiscard]] MeshImplementation* get() noexcept { // NOLINT(readability-make-member-function-const)
154 return implementation.get();
155 }
156
165 [[nodiscard]] const MeshImplementation* get() const noexcept {
166 return implementation.get();
167 }
168
169protected:
170 GREM_API(graphics)
171 MeshBase(Device& device, std::type_index meshTypeIndex, VertexAttributeMask activeVertexAttributes, Span<const VertexAttributeDescription> vertexAttributeDescriptions,
172 size_t vertexStride, Optional<MeshIndexType> indexType, size_t indexStride, Span<const ParameterDescription> parameterDescriptions, size_t parameterStride,
173 size_t textureParameterCount, Span<const FieldDescription> instanceAttributeDescriptions, size_t instanceStride);
174
175 GREM_API(graphics)
176 void uploadVertices(Span<const byte> vertexData, uint32_t vertexCount, Span<const VertexAttributeDescription> vertexAttributeDescriptions, size_t vertexStride);
177
178 GREM_API(graphics)
179 void uploadVertexAttributes(Span<const Span<const byte>> vertexAttributeData, uint32_t vertexCount, Span<const VertexAttributeDescription> vertexAttributeDescriptions,
180 size_t vertexStride);
181
182 GREM_API(graphics)
183 void uploadIndices(Span<const byte> indexData, uint32_t indexCount, size_t indexStride);
184
185 GREM_API(graphics)
186 void uploadParameters(Span<const byte> newParameterValuesBytes, Span<SharedPointer<TextureImplementation>> newTextures);
187
188private:
189 SharedPointer<MeshImplementation> implementation{};
190};
191
192} // namespace detail
193
208template <typename Vertex, typename Index = NoIndex, typename Parameters = NoParameters, typename Instance = NoInstance>
209class Mesh : public detail::MeshBase {
210public:
211 static_assert(mesh_vertex<Vertex>, "Mesh template parameter \"Vertex\" must be a valid vertex type.");
212 static_assert(mesh_index<Index>, "Mesh template parameter \"Index\" must be a valid index type.");
213 static_assert(mesh_parameters<Parameters>, "Mesh template parameter \"Parameters\" must be a valid parameter struct type.");
214 static_assert(mesh_instance<Instance>, "Mesh template parameter \"Instance\" must be a valid instance type.");
215
217 using vertex_type = Vertex;
218
220 using index_type = Index;
221
223 using parameters_type = Parameters;
224
226 using instance_type = Instance;
227
241 Mesh(Device& device, VertexAttributeMask activeVertexAttributes = ~VertexAttributeMask{}) requires(std::is_empty_v<Parameters>)
242 : detail::MeshBase(device, typeid(Mesh), activeVertexAttributes, detail::VERTEX_ATTRIBUTE_DESCRIPTIONS<Vertex>, sizeof(Vertex), detail::MESH_INDEX_TYPE<Index>,
243 (same_as<Index, NoIndex>) ? 0 : sizeof(Index), {}, 0, 0, detail::FIELD_DESCRIPTIONS<Instance>, (std::is_empty_v<Instance>) ? 0 : sizeof(Instance)) {}
244
260 Mesh(Device& device, const Parameters& parameters, VertexAttributeMask activeVertexAttributes = ~VertexAttributeMask{}) requires(!std::is_empty_v<Parameters>)
261 : detail::MeshBase(device, typeid(Mesh), activeVertexAttributes, detail::VERTEX_ATTRIBUTE_DESCRIPTIONS<Vertex>, sizeof(Vertex), detail::MESH_INDEX_TYPE<Index>,
262 (same_as<Index, NoIndex>) ? 0 : sizeof(Index), detail::PARAMETER_DESCRIPTIONS<Parameters>, detail::PARAMETER_VALUES_BYTES_SIZE<Parameters>,
263 detail::PARAMETER_TEXTURES_COUNT<Parameters>, detail::FIELD_DESCRIPTIONS<Instance>, (std::is_empty_v<Instance>) ? 0 : sizeof(Instance)) {
264 setParameters(parameters);
265 }
266
281 Mesh(Device& device, Span<const Vertex> vertices, VertexAttributeMask activeVertexAttributes = ~VertexAttributeMask{})
282 requires(same_as<Index, NoIndex> && std::is_empty_v<Parameters>)
283 : detail::MeshBase(device, typeid(Mesh), activeVertexAttributes, detail::VERTEX_ATTRIBUTE_DESCRIPTIONS<Vertex>, sizeof(Vertex), {}, 0, {}, 0, 0,
284 detail::FIELD_DESCRIPTIONS<Instance>, (std::is_empty_v<Instance>) ? 0 : sizeof(Instance)) {
285 uploadVertices(asBytes(vertices), static_cast<uint32_t>(vertices.size()), detail::VERTEX_ATTRIBUTE_DESCRIPTIONS<Vertex>, sizeof(Vertex));
286 }
287
303 Mesh(Device& device, Span<const Vertex> vertices, Span<const Index> indices, VertexAttributeMask activeVertexAttributes = ~VertexAttributeMask{})
304 requires(!same_as<Index, NoIndex> && std::is_empty_v<Parameters>)
305 : detail::MeshBase(device, typeid(Mesh), activeVertexAttributes, detail::VERTEX_ATTRIBUTE_DESCRIPTIONS<Vertex>, sizeof(Vertex), detail::MESH_INDEX_TYPE<Index>,
306 sizeof(Index), {}, 0, 0, detail::FIELD_DESCRIPTIONS<Instance>, (std::is_empty_v<Instance>) ? 0 : sizeof(Instance)) {
307 uploadVertices(asBytes(vertices), static_cast<uint32_t>(vertices.size()), detail::VERTEX_ATTRIBUTE_DESCRIPTIONS<Vertex>, sizeof(Vertex));
308 uploadIndices(asBytes(indices), static_cast<uint32_t>(indices.size()), sizeof(Index));
309 }
310
327 Mesh(Device& device, Span<const Vertex> vertices, const Parameters& parameters, VertexAttributeMask activeVertexAttributes = ~VertexAttributeMask{})
328 requires(same_as<Index, NoIndex> && !std::is_empty_v<Parameters>)
329 : detail::MeshBase(device, typeid(Mesh), activeVertexAttributes, detail::VERTEX_ATTRIBUTE_DESCRIPTIONS<Vertex>, sizeof(Vertex), {}, 0,
330 detail::PARAMETER_DESCRIPTIONS<Parameters>, detail::PARAMETER_VALUES_BYTES_SIZE<Parameters>, detail::PARAMETER_TEXTURES_COUNT<Parameters>,
331 detail::FIELD_DESCRIPTIONS<Instance>, (std::is_empty_v<Instance>) ? 0 : sizeof(Instance)) {
332 uploadVertices(asBytes(vertices), static_cast<uint32_t>(vertices.size()), detail::VERTEX_ATTRIBUTE_DESCRIPTIONS<Vertex>, sizeof(Vertex));
333 setParameters(parameters);
334 }
335
354 Mesh(Device& device, Span<const Vertex> vertices, Span<const Index> indices, const Parameters& parameters, VertexAttributeMask activeVertexAttributes = ~VertexAttributeMask{})
355 requires(!same_as<Index, NoIndex> && !std::is_empty_v<Parameters>)
356 : detail::MeshBase(device, typeid(Mesh), activeVertexAttributes, detail::VERTEX_ATTRIBUTE_DESCRIPTIONS<Vertex>, sizeof(Vertex), detail::MESH_INDEX_TYPE<Index>,
357 sizeof(Index), detail::PARAMETER_DESCRIPTIONS<Parameters>, detail::PARAMETER_VALUES_BYTES_SIZE<Parameters>, detail::PARAMETER_TEXTURES_COUNT<Parameters>,
358 detail::FIELD_DESCRIPTIONS<Instance>, (std::is_empty_v<Instance>) ? 0 : sizeof(Instance)) {
359 uploadVertices(asBytes(vertices), static_cast<uint32_t>(vertices.size()), detail::VERTEX_ATTRIBUTE_DESCRIPTIONS<Vertex>, sizeof(Vertex));
360 uploadIndices(asBytes(indices), static_cast<uint32_t>(indices.size()), sizeof(Index));
361 setParameters(parameters);
362 }
363
375 void setVertices(Span<const Vertex> newVertices) noexcept {
376 uploadVertices(asBytes(newVertices), static_cast<uint32_t>(newVertices.size()), detail::VERTEX_ATTRIBUTE_DESCRIPTIONS<Vertex>, sizeof(Vertex));
377 }
378
394 template <typename... AttributeArrays>
395 void setVertexAttributes(const AttributeArrays&... vertexAttributes) noexcept requires(sizeof...(AttributeArrays) == meta::aggregate_size_v<Vertex>) {
396 static_assert(convertible_to<Tuple<std::remove_cvref_t<decltype(Span{vertexAttributes}.front())>...>, decltype(meta::getFields(std::declval<const Vertex&>()))>,
397 "The given vertex attributes must be convertible to the vertex fields of the mesh.");
398 size_t vertexCount = 0;
399 ((vertexCount = (vertexAttributes.empty()) ? vertexCount : vertexAttributes.size()), ...);
400 GREM_ASSERT(((vertexAttributes.empty() || vertexAttributes.size() == vertexCount) && ...));
401 uploadVertexAttributes({asBytes(Span{vertexAttributes})...}, static_cast<uint32_t>(vertexCount), detail::VERTEX_ATTRIBUTE_DESCRIPTIONS<Vertex>, sizeof(Vertex));
402 }
403
414 void setIndices(Span<const Index> newIndices) noexcept requires(!same_as<Index, NoIndex>) {
415 uploadIndices(asBytes(newIndices), static_cast<uint32_t>(newIndices.size()), sizeof(Index));
416 }
417
430 void setVerticesAndIndices(Span<const Vertex> newVertices, Span<const Index> newIndices) noexcept requires(!same_as<Index, NoIndex>) {
431 uploadVertices(asBytes(newVertices), static_cast<uint32_t>(newVertices.size()), detail::VERTEX_ATTRIBUTE_DESCRIPTIONS<Vertex>, sizeof(Vertex));
432 uploadIndices(asBytes(newIndices), static_cast<uint32_t>(newIndices.size()), sizeof(Index));
433 }
434
451 template <typename... AttributeArrays>
452 void setIndicesAndVertexAttributes(Span<const Index> newIndices, const AttributeArrays&... vertexAttributes) noexcept
453 requires(!same_as<Index, NoIndex> && sizeof...(AttributeArrays) == meta::aggregate_size_v<Vertex>) {
454 static_assert(convertible_to<Tuple<std::remove_cvref_t<decltype(Span{vertexAttributes}.front())>...>, decltype(meta::getFields(std::declval<const Vertex&>()))>,
455 "The given vertex attributes must be convertible to the vertex fields of the mesh.");
456 size_t vertexCount = 0;
457 ((vertexCount = (vertexAttributes.empty()) ? vertexCount : vertexAttributes.size()), ...);
458 GREM_ASSERT(((vertexAttributes.empty() || vertexAttributes.size() == vertexCount) && ...));
459 GREM_ASSERT(std::all_of(newIndices.begin(), newIndices.end(), [&](const Index& index) -> bool { return index < vertexCount; }));
460 uploadVertexAttributes({asBytes(Span{vertexAttributes})...}, static_cast<uint32_t>(vertexCount), detail::VERTEX_ATTRIBUTE_DESCRIPTIONS<Vertex>, sizeof(Vertex));
461 uploadIndices(asBytes(newIndices), static_cast<uint32_t>(newIndices.size()), sizeof(Index));
462 }
463
475 void setParameters(const Parameters& newParameters) noexcept requires(!std::is_empty_v<Parameters>) {
476 alignas(sizeof(float) * 4) Array<byte, detail::PARAMETER_VALUES_BYTES_SIZE<Parameters>> bytes;
477 Array<SharedPointer<TextureImplementation>, detail::PARAMETER_TEXTURES_COUNT<Parameters>> textures{};
478 size_t byteOffset = 0;
479 size_t textureIndex = 0;
480 meta::forEachField(newParameters, [&]<typename Wrapper>(const Wrapper& parameter) -> void { //
481 detail::alignParameter(byteOffset, detail::PARAMETER_TYPE_OF_PARAMETER_WRAPPER<Wrapper>, detail::ARRAY_ELEMENT_COUNT_OF_PARAMETER_WRAPPER<Wrapper>);
482 detail::writeParameter(bytes.data() + byteOffset, parameter);
483 detail::skipParameter(byteOffset, detail::PARAMETER_TYPE_OF_PARAMETER_WRAPPER<Wrapper>, detail::ARRAY_ELEMENT_COUNT_OF_PARAMETER_WRAPPER<Wrapper>);
484 detail::writeParameterTextures(textures.data() + textureIndex, parameter);
485 textureIndex += detail::TEXTURE_COUNT_OF_PARAMETER_WRAPPER<Wrapper>;
486 });
487 uploadParameters(bytes, textures);
488 }
489};
490
491} // namespace grem::graphics
492
493#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition BitArray.hpp:23
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
Definition SharedPointer.hpp:65
Definition Span.hpp:146
constexpr reference front() const noexcept
Definition Span.hpp:261
constexpr size_type size() const noexcept
Definition Span.hpp:207
Definition Tuple.hpp:210
Persistent system managing the rendering context of a Window.
Definition Device.hpp:68
Generic abstraction of a GPU vertex array object and its associated buffers.
Definition Mesh.hpp:209
void setIndicesAndVertexAttributes(Span< const Index > newIndices, const AttributeArrays &... vertexAttributes) noexcept
Upload new indices and deinterleaved vertex attributes to the index and vertex buffers,...
Definition Mesh.hpp:452
void setVertices(Span< const Vertex > newVertices) noexcept
Upload new vertices to the vertex buffer, overriding any previous contents.
Definition Mesh.hpp:375
void setVerticesAndIndices(Span< const Vertex > newVertices, Span< const Index > newIndices) noexcept
Upload new vertices and indices to the vertex and index buffers, overriding any previous contents.
Definition Mesh.hpp:430
Mesh(Device &device, Span< const Vertex > vertices, VertexAttributeMask activeVertexAttributes=~VertexAttributeMask{})
Constructor for meshes that only have a vertex buffer.
Definition Mesh.hpp:281
Mesh(Device &device, VertexAttributeMask activeVertexAttributes=~VertexAttributeMask{})
Construct an empty mesh.
Definition Mesh.hpp:241
Mesh(Device &device, const Parameters &parameters, VertexAttributeMask activeVertexAttributes=~VertexAttributeMask{})
Construct an empty mesh.
Definition Mesh.hpp:260
void setVertexAttributes(const AttributeArrays &... vertexAttributes) noexcept
Upload new deinterleaved vertex attributes to the vertex buffer, overriding any previous contents.
Definition Mesh.hpp:395
void setParameters(const Parameters &newParameters) noexcept
Upload new parameter values to the uniform buffer, overriding any previous contents.
Definition Mesh.hpp:475
Mesh(Device &device, Span< const Vertex > vertices, Span< const Index > indices, VertexAttributeMask activeVertexAttributes=~VertexAttributeMask{})
Constructor for meshes that have a vertex buffer and an index buffer.
Definition Mesh.hpp:303
Mesh(Device &device, Span< const Vertex > vertices, Span< const Index > indices, const Parameters &parameters, VertexAttributeMask activeVertexAttributes=~VertexAttributeMask{})
Constructor for meshes that have a vertex buffer, an index buffer and a uniform buffer.
Definition Mesh.hpp:354
Mesh(Device &device, Span< const Vertex > vertices, const Parameters &parameters, VertexAttributeMask activeVertexAttributes=~VertexAttributeMask{})
Constructor for meshes that have a vertex buffer and a uniform buffer.
Definition Mesh.hpp:327
void setIndices(Span< const Index > newIndices) noexcept
Upload new indices to the index buffer, overriding any previous contents.
Definition Mesh.hpp:414
Definition concepts.hpp:47
Concept that checks if a type is a valid field struct type for a shader-read buffer.
Definition FieldDescription.hpp:123
Concept that checks if a type is a valid index type.
Definition Mesh.hpp:78
Concept that checks if a type is a valid instance type.
Definition Mesh.hpp:107
Concept that checks if a type is a valid mesh parameter struct type.
Definition Mesh.hpp:94
Backend-specific implementation of Mesh.
Definition Mesh.hpp:43
Concept that checks if a type is a valid parameter struct type for a shader-read buffer.
Definition ParameterDescription.hpp:318
Concept that checks if a type is a valid vertex attribute struct type.
Definition VertexAttributeDescription.hpp:185
Definition concepts.hpp:35
Definition buffer_layouts.hpp:20
BitArray< 32 > VertexAttributeMask
Mask of active vertex attributes in a Mesh.
Definition Mesh.hpp:112
MeshIndexType
Specification of which type of indices is used in the index buffer of a particular Mesh.
Definition Mesh.hpp:54
@ U32
Unsigned 32-bit integer.
Definition Mesh.hpp:56
@ U16
Unsigned 16-bit integer.
Definition Mesh.hpp:55
constexpr void forEachField(Aggregate &&value, auto fn)
Execute a function for each field of an aggregate.
Definition metaprogramming.hpp:1297
constexpr auto getFields(auto &&value) noexcept
Get a tuple of references to each of the fields of an aggregate.
Definition metaprogramming.hpp:1178
constexpr std::size_t aggregate_size_v
The number of fields in a given aggregate type.
Definition metaprogramming.hpp:921
constexpr tuple_element_t< Index, Pair< T1, T2 > > & get(Pair< T1, T2 > &t) noexcept
Definition Pair.hpp:202
GREM_ALWAYS_INLINE void lock(Lockable1 &lock1, Lockable2 &lock2, LockableN &... lockN)
Definition synchronization.hpp:1268
auto asBytes(Span< T, N > s) noexcept
Definition Span.hpp:359
Definition Array.hpp:20
constexpr pointer data() noexcept
Definition Array.hpp:79
Tag type for specifying that a Mesh does not have an index buffer.
Definition Mesh.hpp:48
Tag type for specifying that a Mesh does not have any instance attributes.
Definition Mesh.hpp:99
Tag type for specifying that a Mesh does not have any parameters.
Definition Mesh.hpp:86