Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
TransientBufferPool.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3#pragma once
4
6#include "./GfxDevice.hpp"
7
8#include <vector>
9#include <array>
10#include <utility>
11
12namespace esengine {
13
14// LayoutId / LAYOUT_COUNT now live in GfxEnums.hpp (a leaf header) so PipelineState can
15// reference LayoutId without pulling in the GfxDevice include cycle.
16
29public:
31
33 GfxDevice& device() { return device_; }
34
36 void init(u32 initialVertexBytes = 2 * 1024 * 1024,
37 u32 initialIndexCount = 256 * 1024);
38 void shutdown();
39
40 void beginFrame();
41
43 void swap(TransientBufferPool& other) noexcept {
44 std::swap(streams_, other.streams_);
45 std::swap(initial_vertex_bytes_, other.initial_vertex_bytes_);
46 std::swap(initial_index_count_, other.initial_index_count_);
47 std::swap(initialized_, other.initialized_);
48 }
49
50 u32 allocVertices(LayoutId layout, u32 byteSize);
51 u32 allocIndices(LayoutId layout, u32 count);
52
53 void writeVertices(LayoutId layout, u32 byteOffset, const void* data, u32 byteSize);
54 void writeIndices(LayoutId layout, u32 indexOffset, const u32* data, u32 count);
55
56 u32 appendVertices(LayoutId layout, const void* data, u32 byteSize);
57 u32 appendIndices(LayoutId layout, const u32* data, u32 count);
59 bool copyIndexRange(LayoutId layout, u32 offset, u32 count, u32& copiedAt);
60
62 void upload();
63
65 void bindLayout(LayoutId layout);
66
72 void bindInstanceLayout(LayoutId layout, u32 instanceByteOffset);
74 void bindInstanceBuffer(LayoutId layout, BufferHandle instances);
77 void reserveStream(LayoutId layout) { ensureStorage(layout); }
78
81
85 u32 vertexStride(LayoutId layout) const;
86
89 u8* vertexData(LayoutId layout);
90 u32 vertexBytesUsed(LayoutId layout) const;
91 u32 indicesUsed(LayoutId layout) const;
92 const u32* indexData(LayoutId layout) const;
94 BufferHandle indexBuffer(LayoutId layout) const;
95
97 TextureHandle instanceTexture() const { return stream(LayoutId::MeshInstance).texture; }
98
99private:
100 struct Stream {
101 BufferHandle vbo = BufferHandle::Invalid; // for ParticleInstance: the per-instance (streamed) buffer
104 BufferHandle quad_vbo = BufferHandle::Invalid; // ParticleInstance only: static unit-quad geometry (slot 0)
109 u32 texture_rows = 0;
110 std::vector<u8> texture_mirror;
111 u32 mirror_rows = 0;
112 u64 mirror_generation = 0;
113 std::vector<u8> vertex_staging;
114 std::vector<u32> index_staging; // 32-bit indices: a single Batch stream can exceed 65535 vertices
115 u32 vertex_stride = 0;
116 u32 vertex_write_pos = 0;
117 u32 index_write_pos = 0;
118 u32 vbo_capacity = 0;
119 u32 ebo_capacity = 0;
120 };
121
122 void setupStream(LayoutId layout);
123 void ensureStorage(LayoutId layout);
124 void uploadInstanceRows(Stream& s, u32& grows, u32& writes);
125 static constexpr u32 instanceRowBytes() { return MESH_INSTANCE_TEXTURE_WIDTH * 16; }
126 void growVertexStaging(Stream& s, u32 requiredBytes);
127 void growIndexStaging(Stream& s, u32 requiredCount);
128
129 Stream& stream(LayoutId layout);
130 const Stream& stream(LayoutId layout) const;
131
132 GfxDevice& device_;
133 std::array<Stream, LAYOUT_COUNT> streams_{};
134 u32 initial_vertex_bytes_ = 0;
135 u32 initial_index_count_ = 0;
136 bool initialized_ = false;
137};
138
139} // namespace esengine
Abstract graphics device interface.
Core type definitions and utilities for ESEngine.
Abstract graphics device interface.
Definition GfxDevice.hpp:75
VertexLayoutHandle layoutHandle(LayoutId layout) const
bool copyIndexRange(LayoutId layout, u32 offset, u32 count, u32 &copiedAt)
u32 appendVertices(LayoutId layout, const void *data, u32 byteSize)
const u32 * indexData(LayoutId layout) const
void swap(TransientBufferPool &other) noexcept
Trades what the two pools hold, buffers and staging both; they must share one device.
BufferHandle vertexBuffer(LayoutId layout) const
GfxDevice & device()
The device the pool's buffers live on, for a plugin that keeps geometry of its own on it.
void bindInstanceLayout(LayoutId layout, u32 instanceByteOffset)
u32 allocIndices(LayoutId layout, u32 count)
u32 vertexStride(LayoutId layout) const
BufferHandle indexBuffer(LayoutId layout) const
TextureHandle instanceTexture() const
The frame's per-object records. Invalid until a frame writes some.
void init(u32 initialVertexBytes=2 *1024 *1024, u32 initialIndexCount=256 *1024)
u32 vertexBytesUsed(LayoutId layout) const
u32 indicesUsed(LayoutId layout) const
u32 appendIndices(LayoutId layout, const u32 *data, u32 count)
void writeVertices(LayoutId layout, u32 byteOffset, const void *data, u32 byteSize)
u32 allocVertices(LayoutId layout, u32 byteSize)
void bindInstanceBuffer(LayoutId layout, BufferHandle instances)
void writeIndices(LayoutId layout, u32 indexOffset, const u32 *data, u32 count)
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
static constexpr u32 MESH_INSTANCE_TEXTURE_WIDTH
Definition GfxEnums.hpp:125
std::uint8_t u8
Unsigned 8-bit integer.
Definition Types.hpp:41
LayoutId
Identifies a transient vertex stream / vertex format. Each layout has its own VBO+EBO+VAO in Transien...
Definition GfxEnums.hpp:88