Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
PerDrawBlocks.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
18#pragma once
19
20#include "../../core/Types.hpp"
21#include "./GfxDevice.hpp"
22
23#include <algorithm>
24#include <cstring>
25#include <vector>
26
27namespace esengine {
28
30public:
32 struct Range {
33 u32 page = NONE;
35 };
36
38 void init(GfxDevice& device, u32 blockSize) {
39 shutdown();
40 device_ = &device;
41 blockSize_ = blockSize;
42 pageBytes_ = std::max(PAGE_BYTES, alignUp(blockSize));
43 pages_.clear();
44 page_ = 0;
45 cursor_ = 0;
46 used_ = 0;
47 const std::vector<u8> zeros(blockSize, 0);
48 zero_ = device.createBuffer({GfxBufferUsage::Uniform, blockSize, /*dynamic=*/false},
49 GfxContent::retained(), zeros.data());
50 }
51
53 void shutdown() {
54 if (!device_) return;
55 for (const Page& page : pages_) {
56 if (page.buffer != BufferHandle::Invalid) device_->deleteBuffer(page.buffer);
57 }
58 if (zero_ != BufferHandle::Invalid) device_->deleteBuffer(zero_);
59 pages_.clear();
61 device_ = nullptr;
62 blockSize_ = pageBytes_ = cursor_ = used_ = 0;
63 page_ = 0;
64 }
65
67 void beginFrame() {
68 page_ = 0;
69 cursor_ = 0;
70 used_ = 0;
71 }
72
81 Range stage(const void* bytes, u32 size) {
82 if (!device_ || !bytes || size == 0 || size > blockSize_) return {};
83 if (page_ < pages_.size() && cursor_ + blockSize_ > pageBytes_) {
84 ++page_;
85 cursor_ = 0;
86 }
87 if (page_ == pages_.size()) {
88 Page page;
89 page.buffer = device_->createBuffer(
90 {GfxBufferUsage::Uniform, pageBytes_, /*dynamic=*/true}, GfxContent::transient(),
91 nullptr);
92 page.bytes.assign(pageBytes_, 0);
93 pages_.push_back(std::move(page));
94 }
95 Page& page = pages_[page_];
96 std::memcpy(page.bytes.data() + cursor_, bytes, size);
97 page.dirtyFrom = std::min(page.dirtyFrom, cursor_);
98 page.dirtyTo = std::max(page.dirtyTo, cursor_ + size);
99 const Range range{static_cast<u32>(page_), cursor_};
100 cursor_ += alignUp(size);
101 ++used_;
102 return range;
103 }
104
106 void upload() {
107 for (Page& page : pages_) {
108 if (page.dirtyFrom >= page.dirtyTo) continue;
109 device_->updateBuffer(page.buffer, page.dirtyFrom, page.bytes.data() + page.dirtyFrom,
110 page.dirtyTo - page.dirtyFrom);
111 page.dirtyFrom = NONE;
112 page.dirtyTo = 0;
113 }
114 }
115
117 void bind(u32 slot, Range range) const {
118 if (!device_) return;
119 if (range.page >= pages_.size()) {
120 bindZero(slot);
121 return;
122 }
123 device_->setUniformBuffer(slot, pages_[range.page].buffer, range.offset, blockSize_);
124 }
125
128 void bindZero(u32 slot) const {
129 if (device_) device_->setUniformBuffer(slot, zero_);
130 }
131
133 u32 used() const { return used_; }
134
135private:
136 static constexpr u32 NONE = 0xFFFFFFFFu;
137 static constexpr u32 PAGE_BYTES = 128 * 1024;
138 static u32 alignUp(u32 n) {
141 }
142
143 struct Page {
145 std::vector<u8> bytes;
146 u32 dirtyFrom = NONE;
147 u32 dirtyTo = 0;
148 };
149
150 GfxDevice* device_ = nullptr;
151 u32 blockSize_ = 0;
152 u32 pageBytes_ = 0;
153 std::vector<Page> pages_;
154 usize page_ = 0;
155 u32 cursor_ = 0;
156 u32 used_ = 0;
158};
159
173
174} // namespace esengine
Abstract graphics device interface.
Core type definitions and utilities for ESEngine.
static constexpr GfxContent transient()
static constexpr GfxContent retained()
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.
void beginFrame()
Hands every range back. Called once per FRAME — see the file note.
Range stage(const void *bytes, u32 size)
Copies size bytes of bytes into a range of this frame's, to reach the GPU at the next upload(); a blo...
void shutdown()
Releases every page and the zero block while the device is alive.
u32 used() const
Ranges this frame has handed out — a per-frame cost, for the profile.
void bind(u32 slot, Range range) const
Binds range to slot — the zero block for a range staging refused.
void upload()
Sends what was staged since the last upload, one write per page.
void bindZero(u32 slot) const
Binds what a draw with nothing to say reads: zeroed once and never written, so "none" is an object ra...
void init(GfxDevice &device, u32 blockSize)
Claims the pool for blocks of blockSize bytes, plus the zero one.
std::size_t usize
Size type for indexing and sizes.
Definition Types.hpp:64
static constexpr u32 GFX_UNIFORM_OFFSET_ALIGNMENT
What a uniform binding's offset must be a multiple of: the largest either API may ask.
Definition GfxDevice.hpp:47
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
The uniform blocks a draw rewrites immediately before it.
Where one draw's block was staged; a default one binds the zero block.