Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
Buffer.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
16#pragma once
17
18// =============================================================================
19// Includes
20// =============================================================================
21
22// Project includes
23#include "../../core/Types.hpp"
24#include "./GfxContent.hpp"
25#include "./GfxEnums.hpp"
26
27// Standard library
28#include <span>
29#include <vector>
30
31namespace esengine {
32
33class GfxDevice;
34
35// =============================================================================
36// Shader Data Types
37// =============================================================================
38
58
65
72
73// =============================================================================
74// Vertex Attribute
75// =============================================================================
76
107
108// =============================================================================
109// Vertex Layout
110// =============================================================================
111
127public:
129 VertexLayout() = default;
130
135 VertexLayout(std::initializer_list<VertexAttribute> attributes)
136 : attributes_(attributes) {
137 calculateOffsetsAndStride();
138 }
139
140 VertexLayout(std::vector<VertexAttribute> attributes)
141 : attributes_(std::move(attributes)) {
142 calculateOffsetsAndStride();
143 }
144
149 const std::vector<VertexAttribute>& getAttributes() const { return attributes_; }
150
155 u32 getStride() const { return stride_; }
156
157 // Iterator support for range-based for loops
158 std::vector<VertexAttribute>::iterator begin() { return attributes_.begin(); }
159 std::vector<VertexAttribute>::iterator end() { return attributes_.end(); }
160 std::vector<VertexAttribute>::const_iterator begin() const { return attributes_.begin(); }
161 std::vector<VertexAttribute>::const_iterator end() const { return attributes_.end(); }
162
163private:
167 void calculateOffsetsAndStride() {
168 u32 offset = 0;
169 stride_ = 0;
170 for (auto& attr : attributes_) {
171 attr.offset = offset;
172 offset += attr.size;
173 stride_ += attr.size;
174 }
175 }
176
177 std::vector<VertexAttribute> attributes_;
178 u32 stride_ = 0;
179};
180
181// =============================================================================
182// Vertex Buffer
183// =============================================================================
184
189public:
190 VertexBuffer() = default;
192
193 // Non-copyable, movable
194 VertexBuffer(const VertexBuffer&) = delete;
196 VertexBuffer(VertexBuffer&& other) noexcept;
197 VertexBuffer& operator=(VertexBuffer&& other) noexcept;
198
199 // =========================================================================
200 // Type-Safe Creation Methods
201 // =========================================================================
202
210 template<typename T>
211 static Unique<VertexBuffer> create(GfxDevice& device, GfxContent content, std::span<const T> data) {
212 return createRaw(device, content, data.data(), static_cast<u32>(data.size_bytes()));
213 }
214
218 template<typename T>
219 static Unique<VertexBuffer> create(GfxDevice& device, GfxContent content, const std::vector<T>& data) {
220 return create(device, content, std::span<const T>(data));
221 }
222
226 template<typename T, usize N>
227 static Unique<VertexBuffer> create(GfxDevice& device, GfxContent content, const T (&data)[N]) {
228 return create(device, content, std::span<const T>(data, N));
229 }
230
239 static Unique<VertexBuffer> create(GfxDevice& device, GfxContent content, u32 sizeBytes);
240
241 // =========================================================================
242 // Operations
243 // =========================================================================
244
248 template<typename T>
249 void setData(std::span<const T> data) {
250 setDataRaw(data.data(), static_cast<u32>(data.size_bytes()));
251 }
252
256 template<typename T>
257 void setData(const std::vector<T>& data) {
258 setData(std::span<const T>(data));
259 }
260
264 template<typename T, usize N>
265 void setData(const T (&data)[N]) {
266 setData(std::span<const T>(data, N));
267 }
268
269 // =========================================================================
270 // Layout
271 // =========================================================================
272
277 void setLayout(const VertexLayout& layout) { layout_ = layout; }
278
283 const VertexLayout& getLayout() const { return layout_; }
284
286 BufferHandle handle() const { return handle_; }
287
288 // =========================================================================
289 // Raw API for internal use only
290 // =========================================================================
291
299 static Unique<VertexBuffer> createRaw(GfxDevice& device, GfxContent content, const void* data, u32 sizeBytes);
300
306 void setDataRaw(const void* data, u32 sizeBytes);
307
314 void setSubDataRaw(const void* data, u32 sizeBytes, u32 offsetBytes);
315
316private:
317 GfxDevice* device_ = nullptr;
319 VertexLayout layout_;
320};
321
322// =============================================================================
323// Index Buffer
324// =============================================================================
325
338public:
339 IndexBuffer() = default;
340 ~IndexBuffer();
341
342 // Non-copyable, movable
343 IndexBuffer(const IndexBuffer&) = delete;
345 IndexBuffer(IndexBuffer&& other) noexcept;
346 IndexBuffer& operator=(IndexBuffer&& other) noexcept;
347
348 // =========================================================================
349 // Type-Safe Creation Methods
350 // =========================================================================
351
353 static Unique<IndexBuffer> create(GfxDevice& device, GfxContent content, std::span<const u32> indices) {
354 return create(device, content, indices.data(), static_cast<u32>(indices.size()));
355 }
356
358 static Unique<IndexBuffer> create(GfxDevice& device, GfxContent content, std::span<const u16> indices) {
359 return create(device, content, indices.data(), static_cast<u32>(indices.size()));
360 }
361
363 static Unique<IndexBuffer> create(GfxDevice& device, GfxContent content, const std::vector<u32>& indices) {
364 return create(device, content, std::span<const u32>(indices));
365 }
366
368 static Unique<IndexBuffer> create(GfxDevice& device, GfxContent content, const std::vector<u16>& indices) {
369 return create(device, content, std::span<const u16>(indices));
370 }
371
373 static Unique<IndexBuffer> create(GfxDevice& device, GfxContent content, const u32* indices, u32 count);
374
380 static Unique<IndexBuffer> create(GfxDevice& device, GfxContent content, const u16* indices, u32 count);
381
382 // =========================================================================
383 // Operations
384 // =========================================================================
385
387 u32 getCount() const { return count_; }
388
390 BufferHandle handle() const { return handle_; }
391
393 bool is16Bit() const { return is16Bit_; }
394
395private:
396 GfxDevice* device_ = nullptr;
398 u32 count_ = 0;
399 bool is16Bit_ = false;
400};
401
402} // namespace esengine
Who restores a GPU object's contents after the device is lost.
Backend-agnostic graphics enumerations.
Core type definitions and utilities for ESEngine.
A content policy. provider names who refills a Sourced object and key what it refills; both are the p...
Abstract graphics device interface.
Definition GfxDevice.hpp:75
BufferHandle handle() const
Gets the device buffer handle.
Definition Buffer.hpp:390
IndexBuffer & operator=(const IndexBuffer &)=delete
static Unique< IndexBuffer > create(GfxDevice &device, GfxContent content, const std::vector< u16 > &indices)
Creates an index buffer from a vector of 16-bit indices.
Definition Buffer.hpp:368
static Unique< IndexBuffer > create(GfxDevice &device, GfxContent content, const std::vector< u32 > &indices)
Creates an index buffer from a vector of 32-bit indices.
Definition Buffer.hpp:363
static Unique< IndexBuffer > create(GfxDevice &device, GfxContent content, std::span< const u32 > indices)
Creates an index buffer from a span of 32-bit indices.
Definition Buffer.hpp:353
static Unique< IndexBuffer > create(GfxDevice &device, GfxContent content, std::span< const u16 > indices)
Creates an index buffer from a span of 16-bit indices.
Definition Buffer.hpp:358
u32 getCount() const
Gets the number of indices.
Definition Buffer.hpp:387
IndexBuffer(const IndexBuffer &)=delete
bool is16Bit() const
Returns true if using 16-bit indices.
Definition Buffer.hpp:393
void setSubDataRaw(const void *data, u32 sizeBytes, u32 offsetBytes)
Updates a portion of buffer data with offset (internal use).
Definition Buffer.cpp:102
static Unique< VertexBuffer > createRaw(GfxDevice &device, GfxContent content, const void *data, u32 sizeBytes)
Creates a buffer from raw data pointer for internal use.
Definition Buffer.cpp:83
VertexBuffer(const VertexBuffer &)=delete
void setDataRaw(const void *data, u32 sizeBytes)
Updates buffer data from raw pointer (internal use).
Definition Buffer.cpp:97
BufferHandle handle() const
Gets the device buffer handle.
Definition Buffer.hpp:286
VertexBuffer & operator=(const VertexBuffer &)=delete
void setData(const T(&data)[N])
Updates buffer data from a C array.
Definition Buffer.hpp:265
static Unique< VertexBuffer > create(GfxDevice &device, GfxContent content, const T(&data)[N])
Creates a static buffer from a C array.
Definition Buffer.hpp:227
const VertexLayout & getLayout() const
Gets the vertex layout.
Definition Buffer.hpp:283
static Unique< VertexBuffer > create(GfxDevice &device, GfxContent content, std::span< const T > data)
Creates a static buffer from a span.
Definition Buffer.hpp:211
void setData(std::span< const T > data)
Updates buffer data from a span.
Definition Buffer.hpp:249
void setLayout(const VertexLayout &layout)
Sets the vertex layout.
Definition Buffer.hpp:277
void setData(const std::vector< T > &data)
Updates buffer data from a vector.
Definition Buffer.hpp:257
static Unique< VertexBuffer > create(GfxDevice &device, GfxContent content, const std::vector< T > &data)
Creates a static buffer from a vector.
Definition Buffer.hpp:219
Describes the layout of vertex data in a buffer.
Definition Buffer.hpp:126
std::vector< VertexAttribute >::iterator end()
Definition Buffer.hpp:159
VertexLayout(std::vector< VertexAttribute > attributes)
Definition Buffer.hpp:140
std::vector< VertexAttribute >::const_iterator begin() const
Definition Buffer.hpp:160
u32 getStride() const
Gets the stride (total size) of one vertex.
Definition Buffer.hpp:155
const std::vector< VertexAttribute > & getAttributes() const
Gets all vertex attributes.
Definition Buffer.hpp:149
std::vector< VertexAttribute >::const_iterator end() const
Definition Buffer.hpp:161
VertexLayout(std::initializer_list< VertexAttribute > attributes)
Constructs layout from initializer list.
Definition Buffer.hpp:135
VertexLayout()=default
Default constructor (empty layout).
std::vector< VertexAttribute >::iterator begin()
Definition Buffer.hpp:158
ShaderDataType
Vertex attribute data types.
Definition Buffer.hpp:45
@ UByte4N
4 unsigned bytes, normalized to [0,1] vec4
Definition Buffer.hpp:56
@ Int4
ivec4 (4 ints)
Definition Buffer.hpp:54
@ Float4
vec4 (4 floats)
Definition Buffer.hpp:50
@ Int2
ivec2 (2 ints)
Definition Buffer.hpp:52
@ Int3
ivec3 (3 ints)
Definition Buffer.hpp:53
@ Float2
vec2 (2 floats)
Definition Buffer.hpp:48
@ Float3
vec3 (3 floats)
Definition Buffer.hpp:49
@ Bool
Boolean (1 byte).
Definition Buffer.hpp:55
u32 shaderDataTypeSize(ShaderDataType type)
Gets the size in bytes of a shader data type.
Definition Buffer.cpp:22
std::unique_ptr< T > Unique
Unique ownership smart pointer.
Definition Types.hpp:75
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
u32 shaderDataTypeComponentCount(ShaderDataType type)
Gets the component count of a shader data type.
Definition Buffer.cpp:38
std::uint16_t u16
Unsigned 16-bit integer.
Definition Types.hpp:43
VertexAttribute()=default
Default constructor.
VertexAttribute(ShaderDataType type, const std::string &name, bool normalized=false)
Constructs a vertex attribute.
Definition Buffer.hpp:104
u32 size
Size in bytes.
Definition Buffer.hpp:89
u32 offset
Byte offset within the vertex.
Definition Buffer.hpp:91
bool normalized
Whether to normalize integer data to [0,1] or [-1,1].
Definition Buffer.hpp:93
std::string name
Attribute name (for debugging).
Definition Buffer.hpp:85
ShaderDataType type
Data type of the attribute.
Definition Buffer.hpp:87