candlewick 0.7.0-59-g23c6
A tiny cross-platform renderer based on SDL3
Loading...
Searching...
No Matches
MeshLayout.h
Go to the documentation of this file.
1#pragma once
2
3#include <SDL3/SDL_gpu.h>
4#include "math_types.h"
5#include <vector>
6
7inline bool operator==(const SDL_GPUVertexBufferDescription &lhs,
8 const SDL_GPUVertexBufferDescription &rhs) {
9#define _c(field) lhs.field == rhs.field
10 return _c(slot) && _c(pitch) && _c(input_rate) && _c(instance_step_rate);
11#undef _c
12}
13
14inline bool operator==(const SDL_GPUVertexAttribute &lhs,
15 const SDL_GPUVertexAttribute &rhs) {
16#define _c(field) lhs.field == rhs.field
17 return _c(location) && _c(buffer_slot) && _c(format) && _c(offset);
18#undef _c
19}
20
21inline std::strong_ordering
22operator<=>(const SDL_GPUVertexAttribute &lhs,
23 const SDL_GPUVertexAttribute &rhs) noexcept {
24 if (auto cmp = lhs.location <=> rhs.location; cmp != 0)
25 return cmp;
26 if (auto cmp = lhs.buffer_slot <=> rhs.buffer_slot; cmp != 0)
27 return cmp;
28 if (auto cmp = lhs.format <=> rhs.format; cmp != 0)
29 return cmp;
30 return lhs.offset <=> rhs.offset;
31}
32
33inline std::strong_ordering
34operator<=>(const SDL_GPUVertexBufferDescription &lhs,
35 const SDL_GPUVertexBufferDescription &rhs) noexcept {
36 if (auto cmp = lhs.slot <=> rhs.slot; cmp != 0)
37 return cmp;
38 if (auto cmp = lhs.pitch <=> rhs.pitch; cmp != 0)
39 return cmp;
40 if (auto cmp = lhs.input_rate <=> rhs.input_rate; cmp != 0)
41 return cmp;
42 return lhs.instance_step_rate <=> rhs.instance_step_rate;
43}
44
45namespace candlewick {
46
47constexpr Uint64 vertexElementSize(SDL_GPUVertexElementFormat format) {
48 switch (format) {
49 case SDL_GPU_VERTEXELEMENTFORMAT_INVALID:
50 return 0u;
51 case SDL_GPU_VERTEXELEMENTFORMAT_INT:
52 return sizeof(Sint32);
53 case SDL_GPU_VERTEXELEMENTFORMAT_INT2:
54 return sizeof(Sint32[2]);
55 case SDL_GPU_VERTEXELEMENTFORMAT_INT3:
56 return sizeof(Sint32[3]);
57 case SDL_GPU_VERTEXELEMENTFORMAT_INT4:
58 return sizeof(Sint32[4]);
59 case SDL_GPU_VERTEXELEMENTFORMAT_UINT:
60 return sizeof(Uint32);
61 case SDL_GPU_VERTEXELEMENTFORMAT_UINT2:
62 return sizeof(Uint32[2]);
63 case SDL_GPU_VERTEXELEMENTFORMAT_UINT3:
64 return sizeof(Uint32[3]);
65 case SDL_GPU_VERTEXELEMENTFORMAT_UINT4:
66 return sizeof(Uint32[4]);
67 case SDL_GPU_VERTEXELEMENTFORMAT_FLOAT:
68 return sizeof(float);
69 case SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2:
70 return sizeof(float[2]);
71 case SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3:
72 return sizeof(float[3]);
73 case SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4:
74 return sizeof(float[4]);
75 case SDL_GPU_VERTEXELEMENTFORMAT_BYTE2:
76 case SDL_GPU_VERTEXELEMENTFORMAT_UBYTE2:
77 case SDL_GPU_VERTEXELEMENTFORMAT_BYTE2_NORM:
78 case SDL_GPU_VERTEXELEMENTFORMAT_UBYTE2_NORM:
79 return 16u;
80 case SDL_GPU_VERTEXELEMENTFORMAT_BYTE4:
81 case SDL_GPU_VERTEXELEMENTFORMAT_UBYTE4:
82 case SDL_GPU_VERTEXELEMENTFORMAT_BYTE4_NORM:
83 case SDL_GPU_VERTEXELEMENTFORMAT_UBYTE4_NORM:
84 return 32u;
85 case SDL_GPU_VERTEXELEMENTFORMAT_SHORT2:
86 case SDL_GPU_VERTEXELEMENTFORMAT_USHORT2:
87 case SDL_GPU_VERTEXELEMENTFORMAT_SHORT2_NORM:
88 case SDL_GPU_VERTEXELEMENTFORMAT_USHORT2_NORM:
89 case SDL_GPU_VERTEXELEMENTFORMAT_HALF2:
90 return 32ul;
91 case SDL_GPU_VERTEXELEMENTFORMAT_SHORT4:
92 case SDL_GPU_VERTEXELEMENTFORMAT_USHORT4:
93 case SDL_GPU_VERTEXELEMENTFORMAT_SHORT4_NORM:
94 case SDL_GPU_VERTEXELEMENTFORMAT_USHORT4_NORM:
95 case SDL_GPU_VERTEXELEMENTFORMAT_HALF4:
96 return 64ul;
97 }
98}
99
114
123public:
124 MeshLayout() : m_bufferDescs{}, m_attrs{}, m_totalVertexSize(0) {}
125
135 MeshLayout &addBinding(Uint32 slot, Uint32 pitch) {
136 m_bufferDescs.emplace_back(slot, pitch, SDL_GPU_VERTEXINPUTRATE_VERTEX, 0u);
137 return *this;
138 }
139
147 constexpr MeshLayout &addAttribute(VertexAttrib loc, Uint32 binding,
148 SDL_GPUVertexElementFormat format,
149 Uint32 offset) {
150 const Uint16 _loc = static_cast<Uint16>(loc);
151 m_attrs.emplace_back(_loc, binding, format, offset);
152 const Uint32 attrSize =
153 math::roundUpTo16(Uint32(vertexElementSize(format)));
154 m_totalVertexSize = std::max(m_totalVertexSize, offset + attrSize);
155 return *this;
156 }
157
158 const SDL_GPUVertexAttribute *getAttribute(VertexAttrib loc) const noexcept {
159 for (Uint32 i = 0; i < numAttributes(); i++) {
160 if (m_attrs[i].location == static_cast<Uint16>(loc)) {
161 return &m_attrs[i];
162 }
163 }
164 return nullptr;
165 }
166
171 SDL_GPUVertexInputState toVertexInputState() const noexcept {
172 return {
173 m_bufferDescs.data(),
174 numBuffers(),
175 m_attrs.data(),
177 };
178 }
179
180 operator SDL_GPUVertexInputState() const noexcept {
181 return toVertexInputState();
182 }
183
184 std::strong_ordering operator<=>(const MeshLayout &other) const noexcept;
185
186 bool operator==(const MeshLayout &other) const noexcept = default;
187
189 Uint32 numBuffers() const { return Uint32(m_bufferDescs.size()); }
191 Uint32 numAttributes() const { return Uint32(m_attrs.size()); }
194 Uint32 vertexSize() const { return m_totalVertexSize; }
196 Uint32 indexSize() const { return sizeof(Uint32); }
197
198 std::vector<SDL_GPUVertexBufferDescription> m_bufferDescs;
199 std::vector<SDL_GPUVertexAttribute> m_attrs;
200
201private:
202 Uint32 m_totalVertexSize;
203};
204
205inline std::strong_ordering
206MeshLayout::operator<=>(const MeshLayout &other) const noexcept {
207 if (auto cmp = numBuffers() <=> other.numBuffers(); cmp != 0)
208 return cmp;
209 if (auto cmp = numAttributes() <=> other.numAttributes(); cmp != 0)
210 return cmp;
211 if (auto cmp = m_bufferDescs <=> other.m_bufferDescs; cmp != 0)
212 return cmp;
213 return m_attrs <=> other.m_attrs;
214}
215
218inline bool validateMeshLayout(const MeshLayout &layout) {
219 return (layout.numBuffers() > 0) && (layout.numAttributes() > 0);
220}
221
224template <typename V>
225concept IsVertexType = std::is_standard_layout_v<V> && (alignof(V) == 16);
226
227template <IsVertexType V> struct VertexTraits;
228
230template <IsVertexType V> MeshLayout meshLayoutFor() {
232}
233
234} // namespace candlewick
#define _c(field)
std::strong_ordering operator<=>(const SDL_GPUVertexAttribute &lhs, const SDL_GPUVertexAttribute &rhs) noexcept
Definition MeshLayout.h:22
bool operator==(const SDL_GPUVertexBufferDescription &lhs, const SDL_GPUVertexBufferDescription &rhs)
Definition MeshLayout.h:7
This class defines the layout of a mesh's vertices.
Definition MeshLayout.h:122
bool operator==(const MeshLayout &other) const noexcept=default
SDL_GPUVertexInputState toVertexInputState() const noexcept
Cast to the SDL_GPU vertex input state struct, used to create pipelines.
Definition MeshLayout.h:171
std::vector< SDL_GPUVertexBufferDescription > m_bufferDescs
Definition MeshLayout.h:198
Uint32 indexSize() const
Size of mesh indices (in bytes).
Definition MeshLayout.h:196
constexpr MeshLayout & addAttribute(VertexAttrib loc, Uint32 binding, SDL_GPUVertexElementFormat format, Uint32 offset)
Add a vertex attribute.
Definition MeshLayout.h:147
Uint32 vertexSize() const
Total size of a vertex (in bytes).
Definition MeshLayout.h:194
const SDL_GPUVertexAttribute * getAttribute(VertexAttrib loc) const noexcept
Definition MeshLayout.h:158
std::vector< SDL_GPUVertexAttribute > m_attrs
Definition MeshLayout.h:199
MeshLayout & addBinding(Uint32 slot, Uint32 pitch)
Add a binding (i.e. a vertex binding) for the mesh.
Definition MeshLayout.h:135
MeshLayout()
Definition MeshLayout.h:124
Uint32 numBuffers() const
Number of vertex buffers.
Definition MeshLayout.h:189
Uint32 numAttributes() const
Number of vertex attributes.
Definition MeshLayout.h:191
std::strong_ordering operator<=>(const MeshLayout &other) const noexcept
Definition MeshLayout.h:206
Basic concept checking if type V has the correct layout and alignment requirements to be a vertex ele...
Definition MeshLayout.h:225
constexpr Uint32 roundUpTo16(Uint32 value)
Definition math_types.h:160
Definition Camera.h:8
VertexAttrib
Fixed vertex attributes.
Definition MeshLayout.h:104
@ Color1
Definition MeshLayout.h:110
@ TexCoord0
Definition MeshLayout.h:111
@ Position
Definition MeshLayout.h:105
@ Tangent
Definition MeshLayout.h:107
@ Normal
Definition MeshLayout.h:106
@ Color0
Definition MeshLayout.h:109
@ TexCoord1
Definition MeshLayout.h:112
@ Bitangent
Definition MeshLayout.h:108
constexpr Uint64 vertexElementSize(SDL_GPUVertexElementFormat format)
Definition MeshLayout.h:47
bool validateMeshLayout(const MeshLayout &layout)
Validation function. Checks if a MeshLayout produces invalid data for a Mesh.
Definition MeshLayout.h:218
MeshLayout meshLayoutFor()
Shortcut for extracting layout from compile-time struct.
Definition MeshLayout.h:230
Definition MeshLayout.h:227