Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
GfxResourceRegistry.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
13#pragma once
14
15#include "./GfxContent.hpp"
17#include "../../core/Hash.hpp"
18#include "./GfxEnums.hpp"
19#include "./PipelineState.hpp"
20
21#include <deque>
22#include <optional>
23#include <unordered_map>
24#include <functional>
25#include <string>
26#include <utility>
27#include <vector>
28
29namespace esengine {
30
33 enum class Type : u8 { None, Int, Float, Vec2, Vec3, Vec4, Mat3, Mat4 };
35 i32 i = 0;
36 f32 f[16] = {};
37};
38
41
45 std::vector<u8> bytes;
46 bool realized = false;
47 bool owed = false;
48};
49
69
72 std::string vertex;
73 std::string fragment;
74 std::vector<std::pair<u32, std::string>> attribBindings;
76 std::vector<std::string> uniformNames;
77 std::vector<GfxUniformValue> uniformValues;
78 std::vector<std::string> blockNames;
80 std::vector<u32> blockBindings;
81 bool realized = false;
83 std::function<void()> onLinked;
84};
85
86inline constexpr u32 GFX_UNBOUND_BLOCK = 0xFFFFFFFFu;
87
89 std::string source;
90 std::string entry;
91 bool realized = false;
92};
93
98
102
106
108 bool realized = false;
109};
110
119template <typename Record>
121public:
122 u32 insert(Record record) {
123 const u32 id = static_cast<u32>(slots_.size()) + 1;
124 slots_.emplace_back(std::move(record));
125 ++live_;
126 return id;
127 }
128
129 Record* find(u32 id) {
130 if (id == 0 || id > slots_.size()) return nullptr;
131 auto& slot = slots_[id - 1];
132 return slot ? &*slot : nullptr;
133 }
134
135 const Record* find(u32 id) const {
136 if (id == 0 || id > slots_.size()) return nullptr;
137 const auto& slot = slots_[id - 1];
138 return slot ? &*slot : nullptr;
139 }
140
141 bool erase(u32 id) {
142 if (id == 0 || id > slots_.size() || !slots_[id - 1]) return false;
143 slots_[id - 1].reset();
144 --live_;
145 return true;
146 }
147
148 u32 size() const { return live_; }
149
151 template <typename Slots, typename Ref>
152 class View {
153 public:
154 class iterator {
155 public:
156 iterator(Slots* slots, size_t at) : slots_(slots), at_(at) { skip(); }
157 // An lvalue, so `for (auto& [id, record] : table.all())` binds as it did over the map.
158 std::pair<u32, Ref>& operator*() const { current_.emplace(static_cast<u32>(at_ + 1), *(*slots_)[at_]); return *current_; }
159 iterator& operator++() { ++at_; skip(); return *this; }
160 bool operator!=(const iterator& other) const { return at_ != other.at_; }
161 bool operator==(const iterator& other) const { return at_ == other.at_; }
162 private:
163 void skip() { while (at_ < slots_->size() && !(*slots_)[at_]) ++at_; }
164 Slots* slots_;
165 size_t at_;
166 mutable std::optional<std::pair<u32, Ref>> current_;
167 };
168 explicit View(Slots* slots) : slots_(slots) {}
169 iterator begin() const { return iterator(slots_, 0); }
170 iterator end() const { return iterator(slots_, slots_->size()); }
171 private:
172 Slots* slots_;
173 };
174
177 return View<const std::deque<std::optional<Record>>, const Record&>(&slots_);
178 }
179
180private:
181 std::deque<std::optional<Record>> slots_;
182 u32 live_ = 0;
183};
184
187 size_t operator()(const PipelineDesc& d) const noexcept {
189 const auto mix = [&h](u64 v) { h = hash::fnv1aWord(h, v); };
190 mix(static_cast<u32>(d.program));
191 mix(static_cast<u32>(d.vertexLayout));
192 mix(static_cast<u32>(d.blend));
193 mix(static_cast<u32>(d.stencil));
194 mix(static_cast<u32>(d.topology));
195 mix(static_cast<u16>(d.depthBias));
196 mix((u64(d.blendEnabled) << 0) | (u64(d.depthTest) << 1) | (u64(d.depthWrite) << 2) | (u64(d.cullEnabled) << 3)
197 | (u64(d.cullFront) << 4) | (u64(d.alphaToCoverage) << 5) | (u64(d.gbuffer) << 6) | (u64(d.albedo) << 7)
198 | (u64(d.colorWrite) << 8) | (u64(d.depthEqual) << 9) | (u64(d.layerAlpha) << 10));
199 return static_cast<size_t>(h);
200 }
201};
202
211
213public:
222
224 u32 findPipeline(const PipelineDesc& desc) const {
225 const auto it = pipelineIndex_.find(desc);
226 return it != pipelineIndex_.end() ? it->second : 0;
227 }
228
231 if (const u32 existing = findPipeline(desc)) return existing;
232 const u32 id = pipelines.insert(GfxPipelineRecord{desc});
233 pipelineIndex_.emplace(desc, id);
234 return id;
235 }
236
237 std::vector<GfxOwedContent> owed() const {
238 std::vector<GfxOwedContent> out;
239 for (const auto& [id, record] : buffers.all()) {
240 if (record.owed) {
241 out.push_back({GfxOwedContent::Kind::Buffer, id,
242 record.content.provider(), record.content.key()});
243 }
244 }
245 for (const auto& [id, record] : textures.all()) {
246 if (record.owed) {
247 out.push_back({GfxOwedContent::Kind::Texture, id,
248 record.content.provider(), record.content.key()});
249 }
250 }
251 return out;
252 }
253
254private:
255 std::unordered_map<PipelineDesc, u32, PipelineDescHash> pipelineIndex_;
256};
257
258} // namespace esengine
Who restores a GPU object's contents after the device is lost.
Backend-agnostic graphics enumerations.
Immutable GPU pipeline state — the backend-agnostic unit of "how to draw".
What a resource's content is, as a number that changes whenever it does.
A content policy. provider names who refills a Sourced object and key what it refills; both are the p...
bool operator==(const iterator &other) const
std::pair< u32, Ref > & operator*() const
bool operator!=(const iterator &other) const
One kind of object, by id, in creation order.
View< const std::deque< std::optional< Record > >, const Record & > all() const
const Record * find(u32 id) const
View< std::deque< std::optional< Record > >, Record & > all()
GfxRecordTable< GfxFramebufferRecord > framebuffers
std::vector< GfxOwedContent > owed() const
GfxRecordTable< GfxTimerQueryRecord > timerQueries
GfxRecordTable< GfxComputeRecord > computes
u32 internPipeline(const PipelineDesc &desc)
The pipeline with this description, made if there is none. Pipelines are never erased,...
GfxRecordTable< GfxProgramRecord > programs
GfxRecordTable< GfxBufferRecord > buffers
GfxRecordTable< GfxPipelineRecord > pipelines
GfxRecordTable< GfxTextureRecord > textures
u32 findPipeline(const PipelineDesc &desc) const
An existing pipeline with this description, or 0. A hash lookup: a pipeline is asked for at every cha...
GfxRecordTable< GfxLayoutRecord > layouts
2D texture for GPU rendering
Definition Texture.hpp:116
constexpr u64 fnv1aWord(u64 h, u64 word)
Definition Hash.hpp:17
constexpr u64 kFnv64Offset
Definition Hash.hpp:12
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
GfxShaderLanguage
Shader source language a backend consumes.
Definition GfxEnums.hpp:654
GfxCompressedFormat
GPU-compressed texture internal formats for createCompressedTexture.
Definition GfxEnums.hpp:326
@ ETC2_RGBA8
GL_COMPRESSED_RGBA8_ETC2_EAC (core).
Definition GfxEnums.hpp:328
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
std::uint8_t u8
Unsigned 8-bit integer.
Definition Types.hpp:41
std::uint16_t u16
Unsigned 16-bit integer.
Definition Types.hpp:43
constexpr u32 GFX_UNBOUND_BLOCK
u64 nextRevision()
A revision no content has had before. Never 0, which reads as "none".
Definition Revision.hpp:24
float f32
32-bit floating point
Definition Types.hpp:59
Immutable creation parameters of a GPU buffer.
Definition GfxEnums.hpp:403
Attachments of an offscreen render target. The attach point of depthStencil follows its texture's pix...
Definition GfxEnums.hpp:451
std::vector< u8 > bytes
Retained content, or the initial bytes of a buffer created while the device was lost.
GfxBufferRecord(const BufferDesc &d, GfxContent c)
A Sourced object that has storage again and is waiting for its contents.
std::vector< std::string > uniformNames
A location handed out is an index here; the backend's own is resolved per generation.
std::vector< GfxUniformValue > uniformValues
std::vector< std::string > blockNames
std::function< void()> onLinked
What the owner of a program created with GfxShaderSource::deferLink does once it has linked (the bloc...
std::vector< std::pair< u32, std::string > > attribBindings
std::vector< u32 > blockBindings
Binding point per block name, or GFX_UNBOUND_BLOCK.
GfxCompressedFormat compressedFormat
GfxTextureRecord(const TextureDesc &d, GfxContent c)
bool externallyWritable
Attached for rendering or backed by a host texture.
The last value a program was given for one uniform, replayed after a relink.
size_t operator()(const PipelineDesc &d) const noexcept
Immutable description of a draw pipeline. Equality keys the pipeline cache, so two draws with the sam...
Immutable creation parameters of a 2D texture or 2D texture array.
Definition GfxEnums.hpp:414
Describes how vertex-buffer bytes map to shader attributes.
Definition GfxEnums.hpp:235