Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
RenderGraph.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
22#pragma once
23
24#include "../../core/Types.hpp"
26#include "../rhi/GfxEnums.hpp"
27#include "TargetPool.hpp"
28
29#include <functional>
30#include <memory>
31#include <string>
32#include <unordered_set>
33#include <vector>
34
35namespace esengine {
36
37class GfxDevice;
38
39namespace rg {
40
43constexpr ResourceId kNoResource = static_cast<ResourceId>(-1);
44
58struct TargetDesc {
59 f32 scale = 1.0f;
63 bool linearFilter = true;
64 bool depthStencil = false;
69 bool gbuffer = false;
71 bool storage = false;
72};
73
79 const std::vector<TextureHandle>* inputs = nullptr;
82
84 return (inputs && i < inputs->size()) ? (*inputs)[i] : TextureHandle::Invalid;
85 }
86};
87
95struct PassDesc {
96 std::string name;
97 std::vector<ResourceId> reads;
106 std::vector<ResourceId> dependencies;
112 bool compute = false;
114 bool keep = false;
116 bool discardDepth = false;
117 bool clear = false;
121 bool clearDepth = false;
122 f32 clearColor[4] = {0.0f, 0.0f, 0.0f, 1.0f};
125 bool clearViewportOnly = false;
138 std::function<void(const PassContext&)> execute;
140 bool asyncCompute = false;
142};
143
165public:
166 struct Entry {
167 const char* name;
169 };
170
171 explicit PassTimer(GfxDevice& device) : device_(device) {}
172
174 bool available();
175
177 void beginFrame();
178 void endFrame();
179
181 void begin(const std::string& name);
182 void end();
183
189 void segment(const std::string& name);
190
192 const std::vector<Entry>& last() const { return last_; }
193 f32 lastTotalMs() const { return lastTotal_; }
194
197 u32 framesRead() const { return framesRead_; }
198 u32 framesDropped() const { return framesDropped_; }
199 u32 framesDisjoint() const { return framesDisjoint_; }
201 u32 framesSkipped() const { return framesSkipped_; }
202
203private:
204 static constexpr u32 kFrames = 4;
205 struct Slot {
206 const char* name = nullptr;
207 u32 query = 0;
208 bool done = false;
209 f32 ms = 0.0f;
210 };
211 struct Frame {
212 std::vector<Slot> slots;
213 bool inflight = false;
214 };
215
216 static constexpr u32 kMaxPasses = 512;
217 u32 framesRead_ = 0, framesDropped_ = 0, framesDisjoint_ = 0, framesSkipped_ = 0;
218 u32 queryFor(u32 frame, u32 index);
219 bool poll(Frame& frame);
220
221 GfxDevice& device_;
222 int state_ = 0; // 0 = unprobed, 1 = available, 2 = no timer
223 bool arming_ = false;
224 bool open_ = false;
225 std::string pass_; // the open pass
226 Frame frames_[kFrames];
227 std::vector<u32> queries_;
228 std::unordered_set<std::string> names_;
229 u32 cur_ = 0;
230 u32 counter_ = 0;
231 u32 frameNo_[kFrames] = {0, 0, 0, 0};
232 u32 lastNo_ = 0;
234 bool timing_ = false;
235 static constexpr u32 kGiveUpFrames = 120;
236 std::vector<Entry> last_;
237 f32 lastTotal_ = -1.0f;
238};
239
241public:
245 explicit RenderGraph(GfxDevice& device);
247
248 RenderGraph(const RenderGraph&) = delete;
250
252 void begin(u32 refWidth, u32 refHeight);
253
255 ResourceId importTexture(TextureHandle texture, u32 width, u32 height);
256
268 ResourceId importTarget(FramebufferHandle target, u32 width, u32 height, bool output = true,
270
273
283
286
287 void addPass(PassDesc pass);
288
290 void execute();
291
293 void setPassTimer(PassTimer* timer) { timer_ = timer; }
294
298
302
307
310 void releasePool();
311
314 TargetPool& pool() { return pool_; }
315
317 u32 pooledTargetCount() const { return pool_.count(); }
318
321 u64 pooledTargetBytes() const { return pool_.bytes(); }
322
324 u32 lastExecutedPassCount() const { return executed_; }
325
326private:
327 enum class Kind : u8 { ImportedTexture, ImportedTarget, Transient };
328
329 struct Resource {
330 Kind kind = Kind::Transient;
331 TargetDesc desc;
334 u32 width = 0;
335 u32 height = 0;
337 TargetHandle pooled = kNoTarget;
338 i32 lastRead = -1;
339 };
340
341
342 void resolveSize(Resource& res) const;
343 TargetHandle acquire(const Resource& res);
344 void release(Resource& res);
345 void cull();
346
347 GfxDevice& device_;
349 Unique<TargetPool> ownedPool_;
350 TargetPool& pool_;
351 std::vector<Resource> resources_;
352 std::vector<PassDesc> passes_;
356 std::vector<TargetHandle> borrowed_;
357 std::vector<TextureHandle> inputScratch_;
358 std::vector<bool> live_;
359 std::vector<bool> wanted_;
360 std::vector<i32> last_writer_;
361 ResourceId finalTarget_ = kNoResource;
362 u32 refWidth_ = 0;
363 u32 refHeight_ = 0;
364 u32 executed_ = 0;
365 PassTimer* timer_ = nullptr;
366};
367
368} // namespace rg
369} // namespace esengine
GPU framebuffer abstraction for render-to-texture.
Backend-agnostic graphics enumerations.
The render targets a frame borrows from, keyed by shape — owned by the frame, not by any one graph th...
Core type definitions and utilities for ESEngine.
Abstract graphics device interface.
Definition GfxDevice.hpp:75
A frame's full-screen passes, compiled and run.
void begin(const std::string &name)
const std::vector< Entry > & last() const
u32 framesSkipped() const
Frames not timed because every slot was still waiting on the device.
void segment(const std::string &name)
PassTimer(GfxDevice &device)
TextureHandle albedoTextureOf(ResourceId id) const
The third attachment of a gbuffer target: the surface's diffuse colour (alpha 1 where one was written...
ResourceId importTexture(TextureHandle texture, u32 width, u32 height)
ResourceId importTarget(FramebufferHandle target, u32 width, u32 height, bool output=true, TextureHandle readable=TextureHandle::Invalid)
The framebuffer the graph must end in (the screen, an editor FBO).
void addPass(PassDesc pass)
void begin(u32 refWidth, u32 refHeight)
RenderGraph & operator=(const RenderGraph &)=delete
RenderGraph(GfxDevice &device, TargetPool &pool)
FramebufferHandle framebufferOf(ResourceId id) const
TextureHandle textureOf(ResourceId id) const
ResourceId createTarget(const TargetDesc &desc)
TextureHandle gbufferTextureOf(ResourceId id) const
void setPassTimer(PassTimer *timer)
RenderGraph(const RenderGraph &)=delete
TextureHandle depthTextureOf(ResourceId id) const
ResourceId createExternalTarget(const TargetDesc &desc)
A pooled target filled by a producer OUTSIDE any pass callback.
u32 TargetHandle
A borrowed target: a slot index, and the generation of what is in it.
constexpr ResourceId kNoResource
constexpr TargetHandle kNoTarget
FramebufferHandle
Handle 0 is the default framebuffer (backbuffer); it cannot be created or deleted.
Definition GfxEnums.hpp:41
AsyncComputePriority
Definition GfxEnums.hpp:23
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
std::unique_ptr< T > Unique
Unique ownership smart pointer.
Definition Types.hpp:75
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
std::uint8_t u8
Unsigned 8-bit integer.
Definition Types.hpp:41
@ Transient
Rewritten before every use. Recovery recreates the storage zero-filled.
float f32
32-bit floating point
Definition Types.hpp:59
TextureHandle output
The texture this pass writes: the storage texture of a compute pass (a render pass draws into its fra...
const std::vector< TextureHandle > * inputs
The pass's declared reads, already bound to units 0..n-1, in order.
TextureHandle input(u32 i) const
One full-screen pass.
std::function< void(const PassContext &)> execute
std::vector< ResourceId > reads
bool compute
A compute pass: no render pass is opened and no unit is bound — execute dispatches,...
AsyncComputePriority computePriority
std::vector< ResourceId > dependencies
Resources the pass uses but the graph must not bind.
bool keep
Wanted by a later frame rather than by this picture: not culled for nothing having read it yet.
u32 viewportX
The rect of its target this pass draws into. Zero width = all of it.
bool asyncCompute
The compute callback only accesses the graph resources it declares and private uniforms.
bool discardDepth
Its target's depth is only for testing while it draws, and nothing reads it afterwards (see RenderPas...
const char * name
Interned: valid as long as the timer.
Shape of a target the graph allocates.
bool gbuffer
A second colour attachment for the surface's material, drawn beside the first.
bool storage
Written by a compute shader (a pass with compute set), then sampled like any target.