4#ifndef GREM_GRAPHICS_MESH_HPP
5#define GREM_GRAPHICS_MESH_HPP
33struct TextureImplementation;
35struct MeshImplementation;
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;
119 ~MeshBase() =
default;
122 MeshBase(
const MeshBase&) =
default;
125 MeshBase(MeshBase&&)
noexcept =
default;
128 MeshBase& operator=(
const MeshBase&) =
default;
131 MeshBase& operator=(MeshBase&&)
noexcept =
default;
142 return implementation;
153 [[nodiscard]] MeshImplementation*
get()
noexcept {
154 return implementation.get();
165 [[nodiscard]]
const MeshImplementation*
get()
const noexcept {
166 return implementation.get();
180 size_t vertexStride);
183 void uploadIndices(
Span<const byte> indexData, uint32_t indexCount,
size_t indexStride);
208template <
typename Vertex,
typename Index = NoIndex,
typename Parameters = NoParameters,
typename Instance = NoInstance>
209class Mesh :
public detail::MeshBase {
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.");
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)) {}
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)) {
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));
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));
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));
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));
376 uploadVertices(
asBytes(newVertices),
static_cast<uint32_t
>(newVertices.size()), detail::VERTEX_ATTRIBUTE_DESCRIPTIONS<Vertex>,
sizeof(Vertex));
394 template <
typename... AttributeArrays>
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));
415 uploadIndices(
asBytes(newIndices),
static_cast<uint32_t
>(newIndices.size()),
sizeof(Index));
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));
451 template <
typename... AttributeArrays>
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));
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;
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>;
487 uploadParameters(bytes, textures);
#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
constexpr reference front() const noexcept
Definition Span.hpp:261
constexpr size_type size() const noexcept
Definition Span.hpp:207
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
Parameters parameters_type
Definition Mesh.hpp:223
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 ¶meters, 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
Instance instance_type
Definition Mesh.hpp:226
void setParameters(const Parameters &newParameters) noexcept
Upload new parameter values to the uniform buffer, overriding any previous contents.
Definition Mesh.hpp:475
Vertex vertex_type
Definition Mesh.hpp:217
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 ¶meters, 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 ¶meters, VertexAttributeMask activeVertexAttributes=~VertexAttributeMask{})
Constructor for meshes that have a vertex buffer and a uniform buffer.
Definition Mesh.hpp:327
Index index_type
Definition Mesh.hpp:220
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 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
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