Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
Buffer.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
15
16#include "./Buffer.hpp"
17#include "./GfxDevice.hpp"
18#include "../../core/Log.hpp"
19
20namespace esengine {
21
23 switch (type) {
24 case ShaderDataType::Float: return 4;
25 case ShaderDataType::Float2: return 4 * 2;
26 case ShaderDataType::Float3: return 4 * 3;
27 case ShaderDataType::Float4: return 4 * 4;
28 case ShaderDataType::Int: return 4;
29 case ShaderDataType::Int2: return 4 * 2;
30 case ShaderDataType::Int3: return 4 * 3;
31 case ShaderDataType::Int4: return 4 * 4;
32 case ShaderDataType::Bool: return 1;
33 case ShaderDataType::UByte4N: return 4;
34 default: return 0;
35 }
36}
37
39 switch (type) {
40 case ShaderDataType::Float: return 1;
41 case ShaderDataType::Float2: return 2;
42 case ShaderDataType::Float3: return 3;
43 case ShaderDataType::Float4: return 4;
44 case ShaderDataType::Int: return 1;
45 case ShaderDataType::Int2: return 2;
46 case ShaderDataType::Int3: return 3;
47 case ShaderDataType::Int4: return 4;
48 case ShaderDataType::Bool: return 1;
49 case ShaderDataType::UByte4N: return 4;
50 default: return 0;
51 }
52}
53
54
55// ========================================
56// VertexBuffer
57// ========================================
58
60 if (handle_ != BufferHandle::Invalid && device_) {
61 device_->deleteBuffer(handle_);
62 }
63}
64
66 : device_(other.device_), handle_(other.handle_), layout_(std::move(other.layout_)) {
67 other.handle_ = BufferHandle::Invalid;
68}
69
71 if (this != &other) {
72 if (handle_ != BufferHandle::Invalid && device_) {
73 device_->deleteBuffer(handle_);
74 }
75 device_ = other.device_;
76 handle_ = other.handle_;
77 layout_ = std::move(other.layout_);
78 other.handle_ = BufferHandle::Invalid;
79 }
80 return *this;
81}
82
83Unique<VertexBuffer> VertexBuffer::createRaw(GfxDevice& device, GfxContent content, const void* data, u32 sizeBytes) {
84 auto buffer = makeUnique<VertexBuffer>();
85 buffer->device_ = &device;
86 buffer->handle_ = device.createBuffer({GfxBufferUsage::Vertex, sizeBytes, /*dynamic=*/false}, content, data);
87 return buffer;
88}
89
91 auto buffer = makeUnique<VertexBuffer>();
92 buffer->device_ = &device;
93 buffer->handle_ = device.createBuffer({GfxBufferUsage::Vertex, size, /*dynamic=*/true}, content, nullptr);
94 return buffer;
95}
96
97void VertexBuffer::setDataRaw(const void* data, u32 sizeBytes) {
98 if (!device_) return;
99 device_->updateBuffer(handle_, 0, data, sizeBytes);
100}
101
102void VertexBuffer::setSubDataRaw(const void* data, u32 sizeBytes, u32 offsetBytes) {
103 if (!device_) return;
104 device_->updateBuffer(handle_, offsetBytes, data, sizeBytes);
105}
106
107// ========================================
108// IndexBuffer
109// ========================================
110
112 if (handle_ != BufferHandle::Invalid && device_) {
113 device_->deleteBuffer(handle_);
114 }
115}
116
118 : device_(other.device_), handle_(other.handle_), count_(other.count_), is16Bit_(other.is16Bit_) {
119 other.handle_ = BufferHandle::Invalid;
120 other.count_ = 0;
121}
122
124 if (this != &other) {
125 if (handle_ != BufferHandle::Invalid && device_) {
126 device_->deleteBuffer(handle_);
127 }
128 device_ = other.device_;
129 handle_ = other.handle_;
130 count_ = other.count_;
131 is16Bit_ = other.is16Bit_;
132 other.handle_ = BufferHandle::Invalid;
133 other.count_ = 0;
134 }
135 return *this;
136}
137
138Unique<IndexBuffer> IndexBuffer::create(GfxDevice& device, GfxContent content, const u32* indices, u32 count) {
139 auto buffer = makeUnique<IndexBuffer>();
140 buffer->device_ = &device;
141 buffer->count_ = count;
142 buffer->is16Bit_ = false;
143 buffer->handle_ = device.createBuffer(
144 {GfxBufferUsage::Index, count * static_cast<u32>(sizeof(u32)), /*dynamic=*/false}, content, indices);
145 return buffer;
146}
147
148Unique<IndexBuffer> IndexBuffer::create(GfxDevice& device, GfxContent content, const u16* indices, u32 count) {
149 auto buffer = makeUnique<IndexBuffer>();
150 buffer->device_ = &device;
151 buffer->count_ = count;
152 buffer->is16Bit_ = true;
153 buffer->handle_ = device.createBuffer(
154 {GfxBufferUsage::Index, count * static_cast<u32>(sizeof(u16)), /*dynamic=*/false}, content, indices);
155 return buffer;
156}
157
158} // namespace esengine
GPU buffer abstractions for vertex and index data.
Abstract graphics device interface.
Logging 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 createBuffer(const BufferDesc &desc, GfxContent content, const void *initialData)
Creates a buffer with fixed capacity and optional initial contents.
IndexBuffer & operator=(const IndexBuffer &)=delete
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
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
void setDataRaw(const void *data, u32 sizeBytes)
Updates buffer data from raw pointer (internal use).
Definition Buffer.cpp:97
VertexBuffer & operator=(const VertexBuffer &)=delete
static Unique< VertexBuffer > create(GfxDevice &device, GfxContent content, std::span< const T > data)
Creates a static buffer from a span.
Definition Buffer.hpp:211
constexpr Unique< T > makeUnique(Args &&... args)
Creates a Unique pointer with the given arguments.
Definition Types.hpp:99
ShaderDataType
Vertex attribute data types.
Definition Buffer.hpp:45
@ UByte4N
4 unsigned bytes, normalized to [0,1] vec4
Definition Buffer.hpp:56
@ Int
Single 32-bit integer.
Definition Buffer.hpp:51
@ Int4
ivec4 (4 ints)
Definition Buffer.hpp:54
@ Float
Single 32-bit float.
Definition Buffer.hpp:47
@ 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