SPARK  0.1.0
A general purpose game engine written in C++.
Loading...
Searching...
No Matches
Buffer.h
1#pragma once
2
3#include "spark/render/DeviceMemory.h"
4#include "spark/render/Export.h"
5#include "spark/render/Mappable.h"
6#include "spark/render/StateResource.h"
7
8#include <format>
9
10namespace spark::render
11{
15 enum class BufferFormat
16 {
17 // ReSharper disable CppInconsistentNaming
18 None = 0x00000000,
19 X16F = 0x10000101,
20 X16I = 0x10000201,
21 X16U = 0x10000401,
22 XY16F = 0x10000102,
23 XY16I = 0x10000202,
24 XY16U = 0x10000402,
25 XYZ16F = 0x10000103,
26 XYZ16I = 0x10000203,
27 XYZ16U = 0x10000403,
28 XYZW16F = 0x10000104,
29 XYZW16I = 0x10000204,
30 XYZW16U = 0x10000404,
31 X32F = 0x20000101,
32 X32I = 0x20000201,
33 X32U = 0x20000401,
34 XY32F = 0x20000102,
35 XY32I = 0x20000202,
36 XY32U = 0x20000402,
37 XYZ32F = 0x20000103,
38 XYZ32I = 0x20000203,
39 XYZ32U = 0x20000403,
40 XYZW32F = 0x20000104,
41 XYZW32I = 0x20000204,
42 XYZW32U = 0x20000404
43 // ReSharper restore CppInconsistentNaming
44 };
45
49 enum class BufferType
50 {
52 Vertex = 0x00000001,
53
55 Index = 0x00000002,
56
59 Uniform = 0x00000003,
60
63 Storage = 0x00000004,
64
67 Texel = 0x00000005,
68
71 Other = 0x7FFFFFFF
72 };
73
86 enum class BufferUsage
87 {
92 Staging = 0x00000001,
93
97 Resource = 0x00000002,
98
102 Dynamic = 0x00000010,
103
105 Readback = 0x00000100
106 };
107
111 enum class AttributeSemantic
112 {
114 Binormal = 0x00000001,
115
117 BlendIndices = 0x00000002,
118
120 BlendWeight = 0x00000003,
121
123 Color = 0x00000004,
124
126 Normal = 0x00000005,
127
129 Position = 0x00000006,
130
132 TransformedPosition = 0x00000007,
133
135 PointSize = 0x00000008,
136
138 Tangent = 0x00000009,
139
141 TextureCoordinate = 0x0000000A,
142
144 Unknown = 0x7FFFFFFF
145 };
146
150 class SPARK_RENDER_EXPORT IBuffer : public IDeviceMemory, public IMappable, public virtual IStateResource
151 {
152 public:
153 ~IBuffer() noexcept override = default;
154
159 [[nodiscard]] virtual BufferType type() const = 0;
160 };
161
165 class SPARK_RENDER_EXPORT IBufferLayout
166 {
167 public:
168 virtual ~IBufferLayout() noexcept = default;
169
174 [[nodiscard]] virtual std::size_t elementSize() const noexcept = 0;
175
180 [[nodiscard]] virtual unsigned int binding() const noexcept = 0;
181
186 [[nodiscard]] virtual BufferType type() const noexcept = 0;
187 };
188
192 class SPARK_RENDER_EXPORT BufferAttribute final
193 {
194 public:
198 BufferAttribute() noexcept;
199
208 BufferAttribute(unsigned int location, unsigned int offset, BufferFormat format, AttributeSemantic semantic, unsigned int semantic_index) noexcept;
209 ~BufferAttribute() noexcept;
210
211 BufferAttribute(const BufferAttribute& other) = delete;
212 BufferAttribute(BufferAttribute&& other) noexcept;
213 BufferAttribute& operator=(const BufferAttribute& other) = delete;
214 BufferAttribute& operator=(BufferAttribute&& other) noexcept;
215
222 [[nodiscard]] unsigned int location() const noexcept;
223
228 [[nodiscard]] BufferFormat format() const noexcept;
229
234 [[nodiscard]] unsigned int offset() const noexcept;
235
242 [[nodiscard]] AttributeSemantic semantic() const noexcept;
243
250 [[nodiscard]] unsigned int semanticIndex() const noexcept;
251
252 private:
253 struct Impl;
254 std::unique_ptr<Impl> m_impl;
255 };
256}
257
258template <>
259struct std::formatter<spark::render::BufferFormat> : std::formatter<std::string_view>
260{
261 static constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
262
263 static constexpr auto format(const spark::render::BufferFormat format, auto& ctx)
264 {
265 switch (format)
266 {
267 case spark::render::BufferFormat::None:
268 return std::format_to(ctx.out(), "None");
269 case spark::render::BufferFormat::X16F:
270 return std::format_to(ctx.out(), "X16F");
271 case spark::render::BufferFormat::X16I:
272 return std::format_to(ctx.out(), "X16I");
273 case spark::render::BufferFormat::X16U:
274 return std::format_to(ctx.out(), "X16U");
275 case spark::render::BufferFormat::XY16F:
276 return std::format_to(ctx.out(), "XY16F");
277 case spark::render::BufferFormat::XY16I:
278 return std::format_to(ctx.out(), "XY16I");
279 case spark::render::BufferFormat::XY16U:
280 return std::format_to(ctx.out(), "XY16U");
281 case spark::render::BufferFormat::XYZ16F:
282 return std::format_to(ctx.out(), "XYZ16F");
283 case spark::render::BufferFormat::XYZ16I:
284 return std::format_to(ctx.out(), "XYZ16I");
285 case spark::render::BufferFormat::XYZ16U:
286 return std::format_to(ctx.out(), "XYZ16U");
287 case spark::render::BufferFormat::XYZW16F:
288 return std::format_to(ctx.out(), "XYZW16F");
289 case spark::render::BufferFormat::XYZW16I:
290 return std::format_to(ctx.out(), "XYZW16I");
291 case spark::render::BufferFormat::XYZW16U:
292 return std::format_to(ctx.out(), "XYZW16U");
293 case spark::render::BufferFormat::X32F:
294 return std::format_to(ctx.out(), "X32F");
295 case spark::render::BufferFormat::X32I:
296 return std::format_to(ctx.out(), "X32I");
297 case spark::render::BufferFormat::X32U:
298 return std::format_to(ctx.out(), "X32U");
299 case spark::render::BufferFormat::XY32F:
300 return std::format_to(ctx.out(), "XY32F");
301 case spark::render::BufferFormat::XY32I:
302 return std::format_to(ctx.out(), "XY32I");
303 case spark::render::BufferFormat::XY32U:
304 return std::format_to(ctx.out(), "XY32U");
305 case spark::render::BufferFormat::XYZ32F:
306 return std::format_to(ctx.out(), "XYZ32F");
307 case spark::render::BufferFormat::XYZ32I:
308 return std::format_to(ctx.out(), "XYZ32I");
309 case spark::render::BufferFormat::XYZ32U:
310 return std::format_to(ctx.out(), "XYZ32U");
311 case spark::render::BufferFormat::XYZW32F:
312 return std::format_to(ctx.out(), "XYZW32F");
313 case spark::render::BufferFormat::XYZW32I:
314 return std::format_to(ctx.out(), "XYZW32I");
315 case spark::render::BufferFormat::XYZW32U:
316 return std::format_to(ctx.out(), "XYZW32U");
317 }
318 return std::format_to(ctx.out(), "Unknown");
319 }
320};
321
322template <>
323struct std::formatter<spark::render::BufferType> : std::formatter<std::string_view>
324{
325 static constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
326
327 static constexpr auto format(const spark::render::BufferType type, auto& ctx)
328 {
329 switch (type)
330 {
331 case spark::render::BufferType::Vertex:
332 return std::format_to(ctx.out(), "Vertex");
333 case spark::render::BufferType::Index:
334 return std::format_to(ctx.out(), "Index");
335 case spark::render::BufferType::Uniform:
336 return std::format_to(ctx.out(), "Uniform");
337 case spark::render::BufferType::Storage:
338 return std::format_to(ctx.out(), "Storage");
339 case spark::render::BufferType::Texel:
340 return std::format_to(ctx.out(), "Texel");
341 case spark::render::BufferType::Other:
342 return std::format_to(ctx.out(), "Other");
343 }
344 return std::format_to(ctx.out(), "Unknown");
345 }
346};
Stores metadata about a buffer attribute, member or field of a descriptor or buffer.
Definition Buffer.h:193
Describes the layout of a buffer.
Definition Buffer.h:166
virtual std::size_t elementSize() const noexcept=0
Gets the size of a single element in the buffer.
Base interface for all buffers.
Definition Buffer.h:151
virtual BufferType type() const =0
Gets the type of the buffer.
Describes a chunk of memory on the GPU.
Definition DeviceMemory.h:13
Interface allowing data to be mapped into the object.
Definition Mappable.h:13
Interface for a state resource.
Definition StateResource.h:14