11#include <SDL3/SDL_assert.h>
12#include <SDL3/SDL_gpu.h>
17 Derived &
derived() {
return static_cast<Derived &
>(*this); }
18 const Derived &
derived()
const {
return static_cast<const Derived &
>(*this); }
23 return static_cast<Uint32
>(
derived().indexData.size());
34 std::vector<char> m_vertexData;
37 MeshData(
const MeshData &) =
default;
48 template <IsVertexType VertexT>
58 MeshData &operator=(MeshData &&) noexcept = default;
59 MeshData &operator=(const MeshData &) noexcept = delete;
62 [[nodiscard]] static MeshData
copy(const MeshData &other) {
63 return MeshData{other};
71 Uint64
vertexBytes() const noexcept {
return m_vertexData.size(); }
76 template <
typename U> std::span<U>
viewAs() {
77 U *begin =
reinterpret_cast<U *
>(m_vertexData.data());
78 return std::span<U>(begin, m_numVertices);
82 template <
typename U> std::span<const U>
viewAs()
const {
83 const U *begin =
reinterpret_cast<const U *
>(m_vertexData.data());
84 return std::span<const U>(begin, m_numVertices);
92 const Uint32 stride =
layout.vertexSize();
93 auto ptr = m_vertexData.data() + attr.offset;
94 return strided_view(
reinterpret_cast<T *
>(ptr), m_numVertices, stride);
100 const Uint32 stride =
layout.vertexSize();
101 auto ptr = m_vertexData.data() + attr.offset;
102 return strided_view(
reinterpret_cast<const T *
>(ptr), m_numVertices,
106 template <
typename T>
108 if (
auto attr =
layout.getAttribute(loc)) {
112 std::format(
"Vertex attribute {:d} not found.", Uint16(loc)));
115 template <
typename T>
117 if (
auto attr =
layout.getAttribute(loc)) {
121 std::format(
"Vertex attribute {:d} not found.", Uint16(loc)));
124 std::span<const char>
vertexData()
const {
return m_vertexData; }
127template <IsVertexType VertexT>
132 std::vector<char>{
reinterpret_cast<char *
>(
vertexData.data()),
135 std::move(indexData)) {}
143 bool upload =
false);
149 SDL_GPUBuffer *vertexBuffer,
150 SDL_GPUBuffer *indexBuffer);
159 std::span<const MeshData> meshDatas,
172[[nodiscard]]
inline std::vector<PbrMaterial>
174 std::vector<PbrMaterial> out;
175 out.reserve(meshDatas.size());
176 for (
size_t i = 0; i < meshDatas.size(); i++) {
177 out.push_back(meshDatas[i].material);
A class to store type-erased vertex data and index data.
Definition MeshData.h:33
SDL_GPUPrimitiveType primitiveType
Definition MeshData.h:41
std::span< U > viewAs()
Obtain a typed view to the underlying vertex data.
Definition MeshData.h:76
Uint64 vertexBytes() const noexcept
Size of the overall vertex data, in bytes.
Definition MeshData.h:71
std::vector< IndexType > indexData
Definition MeshData.h:43
strided_view< const T > getAttribute(const SDL_GPUVertexAttribute &attr) const
Definition MeshData.h:99
std::span< const char > vertexData() const
Definition MeshData.h:124
strided_view< T > getAttribute(VertexAttrib loc)
Definition MeshData.h:107
Uint32 vertexSize() const noexcept
Size of each vertex, in bytes.
Definition MeshData.h:69
strided_view< const T > getAttribute(VertexAttrib loc) const
Definition MeshData.h:116
MeshData(SDL_GPUPrimitiveType primitiveType, const MeshLayout &layout, std::vector< char > vertexData, std::vector< IndexType > indexData={})
Uint32 IndexType
Definition MeshData.h:40
MeshLayout layout
Definition MeshData.h:42
PbrMaterial material
Definition MeshData.h:44
std::span< const U > viewAs() const
Obtain a typed view to the underlying vertex data.
Definition MeshData.h:82
MeshData(MeshData &&) noexcept=default
strided_view< T > getAttribute(const SDL_GPUVertexAttribute &attr)
Access an attribute. Use this when the underlying vertex data type is unknown.
Definition MeshData.h:91
Uint32 numVertices() const noexcept
Number of individual vertices.
Definition MeshData.h:67
static MeshData copy(const MeshData &other)
Explicit copy function, uses private copy ctor.
Definition MeshData.h:62
This class defines the layout of a mesh's vertices.
Definition MeshLayout.h:98
A view into a Mesh object.
Definition Mesh.h:22
Handle class for meshes (vertex buffers and an optional index buffer) on the GPU.
Definition Mesh.h:57
A strided view to data, allowing for type-erased data.
Definition StridedView.h:14
std::vector< PbrMaterial > extractMaterials(std::span< const MeshData > meshDatas)
Definition MeshData.h:173
void terminate_with_message(std::source_location location, std::string_view fmt, Ts &&...args)
Definition errors.h:51
VertexAttrib
Fixed vertex attributes.
Definition MeshLayout.h:80
Mesh createMeshFromBatch(const Device &device, std::span< const MeshData > meshDatas, bool upload)
Create a Mesh from a batch of MeshData.
Mesh createMesh(const Device &device, const MeshData &meshData, bool upload=false)
Convert MeshData to a GPU Mesh object. This creates the required vertex buffer and index buffer (if r...
void uploadMeshToDevice(const Device &device, const MeshView &meshView, const MeshData &meshData)
Upload the contents of a single, individual mesh to the GPU device.
strided_view(T *first, size_t, size_t) -> strided_view< T >
MeshLayout meshLayoutFor()
Shortcut for extracting layout from compile-time struct.
Definition MeshLayout.h:211
RAII wrapper for SDL_GPUDevice.
Definition Device.h:17
Derived & derived()
Definition MeshData.h:17
Uint32 numVertices() const
Definition MeshData.h:20
Uint32 numIndices() const
Definition MeshData.h:22
bool isIndexed() const
Definition MeshData.h:25
const Derived & derived() const
Definition MeshData.h:18
Tag type for non-initializing constructors (for e.g. RAII classes)
Definition Tags.h:6
PBR material for metallic-roughness workflow.
Definition MaterialUniform.h:8