candlewick 0.1.0
A renderer
Loading...
Searching...
No Matches
Mesh.h
Go to the documentation of this file.
1#pragma once
2
3#include "Core.h"
4#include "Tags.h"
5#include "MeshLayout.h"
6
7#include <vector>
8#include <span>
9#include <SDL3/SDL_assert.h>
10#include <entt/entity/registry.hpp>
11#include <entt/entity/handle.hpp>
12
13namespace candlewick {
14
22class MeshView {
23 friend class Mesh;
24 MeshView() noexcept = default;
25
26public:
28 std::vector<SDL_GPUBuffer *> vertexBuffers;
30 SDL_GPUBuffer *indexBuffer;
31
36
40 Uint32 indexCount;
41
42 bool isIndexed() const { return indexBuffer != nullptr; }
43
44 MeshView(const MeshView &parent, Uint32 subVertexOffset,
45 Uint32 vertexSubCount, Uint32 subIndexOffset, Uint32 indexSubCount);
46};
47
57class Mesh {
58 SDL_GPUDevice *m_device{nullptr};
59 std::vector<MeshView> m_views;
60 MeshLayout m_layout;
61
62public:
64 Uint32 indexCount{0u};
65
73 std::vector<SDL_GPUBuffer *> vertexBuffers;
74
78 SDL_GPUBuffer *indexBuffer{nullptr};
79
80 explicit Mesh(const Device &device, const MeshLayout &layout);
81
82 const MeshLayout &layout() const { return m_layout; }
83
85 Mesh(const Mesh &) = delete;
86 Mesh(Mesh &&other) noexcept;
87
88 Mesh &operator=(const Mesh &) = delete;
89 Mesh &operator=(Mesh &&other) noexcept;
90
91 const MeshView &view(size_t i) const { return m_views[i]; }
92 std::span<const MeshView> views() const { return m_views; }
93 size_t numViews() const { return m_views.size(); }
94
98 MeshView &addView(Uint32 vertexOffset, Uint32 vertexSubCount,
99 Uint32 indexOffset, Uint32 indexSubCount);
100
108 Mesh &bindVertexBuffer(Uint32 slot, SDL_GPUBuffer *buffer);
109
119 Mesh &setIndexBuffer(SDL_GPUBuffer *buffer);
120
122 Uint32 numVertexBuffers() const {
123 return static_cast<Uint32>(vertexBuffers.size());
124 }
125
126 bool isIndexed() const { return indexBuffer != nullptr; }
127
129 void release() noexcept;
130 ~Mesh() noexcept { release(); }
131
132 SDL_GPUBufferBinding getVertexBinding(Uint32 slot) const {
133 return {.buffer = vertexBuffers[slot], .offset = 0u};
134 }
135
136 SDL_GPUBufferBinding getIndexBinding() const {
137 return {.buffer = indexBuffer, .offset = 0u};
138 }
139};
140
144[[nodiscard]] inline bool validateMesh(const Mesh &mesh) {
145 if (!validateMeshLayout(mesh.layout()))
146 return false;
147 if (mesh.views().size() == 0)
148 return false;
149 for (size_t i = 0; i < mesh.numVertexBuffers(); i++) {
150 if (!mesh.vertexBuffers[i])
151 return false;
152 }
153 // check that if we are indexed iff we have positive index count.
154 return mesh.isIndexed() == (mesh.indexCount > 0);
155}
156
164[[nodiscard]] inline bool validateMeshView(const MeshView &view) {
165 for (auto vb : view.vertexBuffers) {
166 if (!vb)
167 return false;
168 }
169 // views of indexed meshes cannot have zero indices.
170 if (view.isIndexed() != (view.indexCount > 0))
171 return false;
172 return view.vertexCount > 0;
173}
174
175} // namespace candlewick
This class defines the layout of a mesh's vertices.
Definition MeshLayout.h:98
A view into a Mesh object.
Definition Mesh.h:22
Uint32 indexCount
Number of indices in the mesh view.
Definition Mesh.h:40
MeshView(const MeshView &parent, Uint32 subVertexOffset, Uint32 vertexSubCount, Uint32 subIndexOffset, Uint32 indexSubCount)
Uint32 indexOffset
Index offset (in elements).
Definition Mesh.h:38
std::vector< SDL_GPUBuffer * > vertexBuffers
Vertex buffers.
Definition Mesh.h:28
friend class Mesh
Definition Mesh.h:23
Uint32 vertexOffset
Vertex offsets, expressed in elements.
Definition Mesh.h:33
bool isIndexed() const
Definition Mesh.h:42
Uint32 vertexCount
Number of vertices in the mesh view.
Definition Mesh.h:35
SDL_GPUBuffer * indexBuffer
Index buffer.
Definition Mesh.h:30
Handle class for meshes (vertex buffers and an optional index buffer) on the GPU.
Definition Mesh.h:57
void release() noexcept
Release all owned vertex and index buffers in the Mesh object.
Uint32 indexCount
Definition Mesh.h:64
Uint32 vertexCount
Definition Mesh.h:63
SDL_GPUBufferBinding getIndexBinding() const
Definition Mesh.h:136
bool isIndexed() const
Definition Mesh.h:126
Mesh & bindVertexBuffer(Uint32 slot, SDL_GPUBuffer *buffer)
Bind an existing vertex buffer to a given slot of the Mesh.
size_t numViews() const
Definition Mesh.h:93
const MeshLayout & layout() const
Definition Mesh.h:82
SDL_GPUBuffer * indexBuffer
Definition Mesh.h:78
Uint32 numVertexBuffers() const
Number of vertex buffers.
Definition Mesh.h:122
std::vector< SDL_GPUBuffer * > vertexBuffers
Definition Mesh.h:73
MeshView & addView(Uint32 vertexOffset, Uint32 vertexSubCount, Uint32 indexOffset, Uint32 indexSubCount)
Add a stored MeshView object. The added view will be drawn when calling Renderer::draw() with a Mesh ...
Mesh(const Device &device, const MeshLayout &layout)
std::span< const MeshView > views() const
Definition Mesh.h:92
Mesh & setIndexBuffer(SDL_GPUBuffer *buffer)
Bind an existing index buffer for the Mesh.
SDL_GPUBufferBinding getVertexBinding(Uint32 slot) const
Definition Mesh.h:132
Mesh & operator=(const Mesh &)=delete
Mesh & operator=(Mesh &&other) noexcept
Mesh(Mesh &&other) noexcept
const MeshView & view(size_t i) const
Definition Mesh.h:91
Mesh(const Mesh &)=delete
Definition Camera.h:8
bool validateMeshView(const MeshView &view)
Validation for a MeshView object.
Definition Mesh.h:164
bool validateMeshLayout(const MeshLayout &layout)
Validation function. Checks if a MeshLayout produces invalid data for a Mesh.
Definition MeshLayout.h:199
bool validateMesh(const Mesh &mesh)
Check that all vertex buffers were set, and consistency in the "indexed/non-indexed" status.
Definition Mesh.h:144
RAII wrapper for SDL_GPUDevice.
Definition Device.h:17
Tag type for non-initializing constructors (for e.g. RAII classes)
Definition Tags.h:6