41 blockSize_ = blockSize;
42 pageBytes_ = std::max(PAGE_BYTES, alignUp(blockSize));
47 const std::vector<u8> zeros(blockSize, 0);
55 for (
const Page& page : pages_) {
62 blockSize_ = pageBytes_ = cursor_ = used_ = 0;
82 if (!device_ || !bytes || size == 0 || size > blockSize_)
return {};
83 if (page_ < pages_.size() && cursor_ + blockSize_ > pageBytes_) {
87 if (page_ == pages_.size()) {
89 page.buffer = device_->createBuffer(
92 page.bytes.assign(pageBytes_, 0);
93 pages_.push_back(std::move(page));
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);
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;
118 if (!device_)
return;
119 if (range.
page >= pages_.size()) {
123 device_->setUniformBuffer(slot, pages_[range.
page].buffer, range.
offset, blockSize_);
129 if (device_) device_->setUniformBuffer(slot, zero_);
136 static constexpr u32 NONE = 0xFFFFFFFFu;
137 static constexpr u32 PAGE_BYTES = 128 * 1024;
138 static u32 alignUp(
u32 n) {
145 std::vector<u8> bytes;
146 u32 dirtyFrom = NONE;
150 GfxDevice* device_ =
nullptr;
153 std::vector<Page> pages_;
Abstract graphics device interface.
Core type definitions and utilities for ESEngine.
static constexpr GfxContent transient()
static constexpr GfxContent retained()
Abstract graphics device interface.
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.
static constexpr u32 GFX_UNIFORM_OFFSET_ALIGNMENT
What a uniform binding's offset must be a multiple of: the largest either API may ask.
std::uint32_t u32
Unsigned 32-bit integer.
The uniform blocks a draw rewrites immediately before it.
Where one draw's block was staged; a default one binds the zero block.