Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
CustomGeometry.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
13
14#include "./CustomGeometry.hpp"
15
16#include "../rhi/GfxDevice.hpp"
17#include "../../core/Log.hpp"
18
19namespace esengine {
20
21namespace {
22
23GfxDataType toGfxDataType(ShaderDataType type) {
24 switch (type) {
29 return GfxDataType::Int;
33 default:
34 return GfxDataType::Float;
35 }
36}
37
38} // namespace
39
40void CustomGeometry::init(GfxDevice& device, const f32* vertices, u32 vertexCount,
41 const VertexLayout& layout, bool dynamic) {
42 device_ = &device;
43 dynamic_ = dynamic;
44 stride_ = layout.getStride();
45 if (stride_ == 0 || layout.getAttributes().size() > MAX_VERTEX_ATTRIBUTES) {
46 // An empty vertex layout would make the divide below a divide-by-zero
47 // (a wasm trap). Leave the geometry uninitialized: layout_ stays Invalid,
48 // so isValid() is false and bind()/draw become no-ops.
49 ES_LOG_ERROR("CustomGeometry::init: unusable vertex layout (stride {}, {} attributes); skipping setup",
50 stride_, layout.getAttributes().size());
51 return;
52 }
53 vertexCount_ = vertexCount * sizeof(f32) / stride_;
54
55 if (dynamic) {
57 vertexCount * sizeof(f32)));
58 vbo_->setDataRaw(vertices, vertexCount * sizeof(f32));
59 } else {
61 vertexCount * sizeof(f32)));
62 }
63 vbo_->setLayout(layout);
64
66 desc.strides[0] = stride_;
67 for (const auto& attr : layout) {
69 out.location = desc.attributeCount;
70 out.components = static_cast<u8>(shaderDataTypeComponentCount(attr.type));
71 out.type = toGfxDataType(attr.type);
72 out.normalized = attr.normalized || attr.type == ShaderDataType::UByte4N;
73 out.offset = attr.offset;
74 out.bufferSlot = 0;
75 ++desc.attributeCount;
76 }
77 layout_ = device.createVertexLayout(desc);
78}
79
80void CustomGeometry::setIndices(const u16* indices, u32 indexCount) {
81 if (!isValid() || !device_) return;
82 ibo_ = Shared<IndexBuffer>(IndexBuffer::create(*device_, GfxContent::retained(), indices, indexCount));
83}
84
85void CustomGeometry::setIndices(const u32* indices, u32 indexCount) {
86 if (!isValid() || !device_) return;
87 ibo_ = Shared<IndexBuffer>(IndexBuffer::create(*device_, GfxContent::retained(), indices, indexCount));
88}
89
90void CustomGeometry::updateVertices(const f32* vertices, u32 vertexCount, u32 offset) {
91 if (!dynamic_ || !vbo_) {
92 ES_LOG_WARN("Cannot update non-dynamic geometry");
93 return;
94 }
95
96 vbo_->setSubDataRaw(vertices, vertexCount * sizeof(f32), offset * sizeof(f32));
97
98 u32 endOffset = offset + vertexCount;
99 u32 newVertexCount = endOffset * sizeof(f32) / stride_;
100 if (newVertexCount > vertexCount_) {
101 vertexCount_ = newVertexCount;
102 }
103}
104
105void CustomGeometry::bind(GfxDevice& device) const {
106 if (!isValid()) return;
107 device.setVertexBuffer(0, vbo_ ? vbo_->handle() : BufferHandle::Invalid, 0);
108 device.setIndexBuffer(ibo_ ? ibo_->handle() : BufferHandle::Invalid);
109}
110
112 if (ibo_) {
113 return ibo_->getCount();
114 }
115 return 0;
116}
117
119 return ibo_ != nullptr && ibo_->getCount() > 0;
120}
121
122// =============================================================================
123// GeometryManager Implementation
124// =============================================================================
125
127 GeometryHandle handle;
128
129 if (!freeList_.empty()) {
130 handle = freeList_.back();
131 freeList_.pop_back();
132 geometries_[handle - 1] = makeUnique<CustomGeometry>();
133 } else {
134 handle = nextHandle_++;
135 geometries_.push_back(makeUnique<CustomGeometry>());
136 }
137
138 return handle;
139}
140
142 if (handle == INVALID_HANDLE || handle > geometries_.size()) {
143 return nullptr;
144 }
145 return geometries_[handle - 1].get();
146}
147
149 if (handle == INVALID_HANDLE || handle > geometries_.size()) {
150 return nullptr;
151 }
152 return geometries_[handle - 1].get();
153}
154
156 if (handle == INVALID_HANDLE || handle > geometries_.size()) {
157 return;
158 }
159 geometries_[handle - 1].reset();
160 freeList_.push_back(handle);
161}
162
164 if (handle == INVALID_HANDLE || handle > geometries_.size()) {
165 return false;
166 }
167 return geometries_[handle - 1] != nullptr;
168}
169
170} // namespace esengine
Custom geometry for user-defined mesh rendering.
Abstract graphics device interface.
Logging utilities for ESEngine.
#define ES_LOG_WARN(...)
Logs a warning message.
Definition Log.hpp:294
#define ES_LOG_ERROR(...)
Logs an error message.
Definition Log.hpp:296
Custom geometry for user-defined mesh rendering.
void setIndices(const u16 *indices, u32 indexCount)
Sets indices for indexed rendering.
bool hasIndices() const
Checks if geometry uses indices.
void updateVertices(const f32 *vertices, u32 vertexCount, u32 offset=0)
Updates vertex data (only for dynamic geometry).
void init(GfxDevice &device, const f32 *vertices, u32 vertexCount, const VertexLayout &layout, bool dynamic=false)
Initializes geometry with vertex data and layout.
void bind(GfxDevice &device) const
Binds the geometry's buffers for the next draw.
u32 getIndexCount() const
Gets the index count.
bool isValid() const
Checks if geometry is initialized.
static constexpr GeometryHandle INVALID_HANDLE
bool isValid(GeometryHandle handle) const
Checks if handle is valid.
GeometryHandle create()
Creates a new geometry.
CustomGeometry * get(GeometryHandle handle)
Gets a geometry by handle.
void release(GeometryHandle handle)
Releases a geometry.
static constexpr GfxContent retained()
Abstract graphics device interface.
Definition GfxDevice.hpp:75
virtual void setVertexBuffer(u32 slot, BufferHandle buffer, u32 offsetBytes)=0
Binds a vertex buffer to a layout slot for subsequent draws.
virtual void setIndexBuffer(BufferHandle buffer)=0
Binds the index buffer for subsequent indexed draws.
VertexLayoutHandle createVertexLayout(const VertexLayoutDesc &desc)
Registers an immutable vertex layout; pipelines reference it by handle.
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< 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
static Unique< VertexBuffer > create(GfxDevice &device, GfxContent content, std::span< const T > data)
Creates a static buffer from a span.
Definition Buffer.hpp:211
Describes the layout of vertex data in a buffer.
Definition Buffer.hpp:126
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
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
@ Int2
ivec2 (2 ints)
Definition Buffer.hpp:52
@ Int3
ivec3 (3 ints)
Definition Buffer.hpp:53
@ Bool
Boolean (1 byte).
Definition Buffer.hpp:55
std::shared_ptr< T > Shared
Shared ownership smart pointer.
Definition Types.hpp:82
static constexpr u32 MAX_VERTEX_ATTRIBUTES
Definition GfxEnums.hpp:217
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::uint8_t u8
Unsigned 8-bit integer.
Definition Types.hpp:41
std::uint16_t u16
Unsigned 16-bit integer.
Definition Types.hpp:43
float f32
32-bit floating point
Definition Types.hpp:59
u32 offset
Byte offset within one element of its buffer slot.
Definition GfxEnums.hpp:225
Describes how vertex-buffer bytes map to shader attributes.
Definition GfxEnums.hpp:235
GfxVertexAttribute attributes[MAX_VERTEX_ATTRIBUTES]
Definition GfxEnums.hpp:236
u32 strides[MAX_VERTEX_BUFFER_SLOTS]
Definition GfxEnums.hpp:238