14#ifdef ES_ENABLE_POSTPROCESS
20#include <glm/gtc/constants.hpp>
21#include <glm/gtc/matrix_transform.hpp>
28constexpr glm::vec3 kFaceDir[6] = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};
29constexpr glm::vec3 kFaceUp[6] = {{0, 1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}, {0, 1, 0}, {0, 1, 0}};
34 ProbeCapture& c = probe_capture_;
35 c.position = position;
36 c.face = std::clamp(face, 8u, 128u);
38 c.state = ProbeCapture::State::Capturing;
43 ProbeCapture& c = probe_capture_;
45 case ProbeCapture::State::Idle:
return 2;
46 case ProbeCapture::State::Capturing:
return 0;
47 case ProbeCapture::State::Drawn:
48 c.readback = c.atlas ? device_.requestReadback(c.atlas->handle(), c.face * 6, c.face) :
ReadbackHandle::Invalid;
50 c.state = ProbeCapture::State::Reading;
52 case ProbeCapture::State::Reading: {
55 c.state = ProbeCapture::State::Idle;
57 c.pixels.resize(
static_cast<usize>(c.face) * 6 * c.face * 4);
58 return device_.takeReadback(c.readback, c.pixels.data(), c.pixels.size()) ? 1 : 2;
64void RenderFrame::declareProbeCapture(
ecs::Registry& registry) {
65#ifdef ES_ENABLE_POSTPROCESS
66 ProbeCapture& c = probe_capture_;
68 if (c.state != ProbeCapture::State::Capturing || c.frame == planar_frame_ || !post_process_)
return;
69 c.frame = planar_frame_;
73 const u32 f = std::min(c.next, 5u);
74 auto& target = c.faces[f];
75 if (!target || target->getWidth() != n || target->getSpecification().colorFormat != format) {
81 if (!c.atlas || c.atlas->getWidth() != n * 6 || c.atlas->getHeight() != n) {
83 spec.width = n * 6; spec.height = n; spec.linearFilter =
false;
87 if (!target || !c.atlas) {
88 ES_LOG_ERROR(
"Probe capture: could not make its {}x{} targets", n, n);
89 c.state = ProbeCapture::State::Idle;
93 const f32 metre = world_units_per_meter_;
94 const glm::vec3 at =
rebased(c.position);
95 c.viewProjection = glm::perspective(glm::half_pi<f32>(), 1.0f, 0.05f * metre, 4000.0f * metre)
96 * glm::lookAt(at, at + kFaceDir[f], kFaceUp[f]);
99 face.name =
"probe-capture-face";
100 face.write = graph_.importTarget(target->handle(), n, n,
false, target->getColorAttachment());
101 face.clear = face.clearDepth =
true;
102 for (
u32 i = 0; i < 4; ++i) face.clearColor[i] = 0.0f;
104 if (shadow_resource_ !=
rg::kNoResource) face.dependencies.push_back(shadow_resource_);
105 collectProbeFace(registry);
106 face.execute = [
this](
const rg::PassContext&) { executeProbeFace(); };
108 graph_.addPass(std::move(face));
111 pack.name =
"probe-capture-pack";
112 pack.reads = {faceResource};
113 pack.write = graph_.importTarget(c.atlas->handle(), n * 6, n,
false, c.atlas->getColorAttachment());
115 pack.viewportX = f * n;
121 pack.execute = [
this, f](
const rg::PassContext&) {
122 post_process_->encodeRgbm();
123 ProbeCapture& done = probe_capture_;
124 if (done.state != ProbeCapture::State::Capturing || done.next != f)
return;
126 if (f == 5) done.state = ProbeCapture::State::Drawn;
128 graph_.addPass(std::move(pack));
135 auto& c = probe_capture_;
136 probe_pool_.beginFrame();
138 probe_list_.setCullingMask(0xFFFFFFFFu);
139 probe_list_.setDepthMask(draw_list_.depthMask());
140 probe_list_.setYSortMask(draw_list_.ySortMask());
141 auto ctx = makeContext();
142 ctx.view_projection = c.viewProjection;
144 frustum.extractFromMatrix(
ctx.view_projection);
145 RenderCollectContext collect{registry, frustum, clip_state_, probe_pool_, probe_list_,
ctx,
147 collect.screen_ui = screen_domain_;
148 collect.ui_tree_layers = ui_tree_layers_;
151 for (
auto& plugin : plugins_) plugin->collect(collect);
154void RenderFrame::executeProbeFace() {
155 ProbeCapture& c = probe_capture_;
161 probe_list_.finalize(probe_pool_);
162 probe_pool_.upload();
163 context_.useFrameConstants(c.frameUbo,
toClipDepthRange(c.viewProjection, device_.clipDepthRange()));
165 context_.lights().uploadAndBind();
168 device_.invalidatePipelineCache();
172 if (grid.
isValid()) probe_overrides_[entity] = grid;
173 else probe_overrides_.erase(entity);
Per-frame CPU scope timing (ES_PROFILE_SCOPE) read by the editor profiler.
#define ES_PROFILE_COUNTER(name, value)
Logging utilities for ESEngine.
#define ES_LOG_ERROR(...)
Logs an error message.
Post-processing effects pipeline.
Frame resources and the shared scene rendering contract.
static Unique< Framebuffer > create(GfxDevice &device, const FramebufferSpec &spec)
Creates a framebuffer from specification.
static constexpr GfxContent transient()
virtual bool supportsFloatTargets()=0
Whether RGBA16F color attachments are renderable.
static u32 frameConstantsSize()
Bytes a buffer passed to useFrameConstants must hold.
void requestProbeCapture(const glm::vec3 &position, u32 face)
Capture the scene around position (world) into six face-pixel faces, one a frame, and read them back:...
void setProbeVolumeOverride(u32 entity, resource::ProbeVolumeHandle grid)
The grid a volume reads instead of its own probes (a capture's), or an invalid handle to stop.
i32 pollProbeCapture()
0 = not yet, 1 = ready (probeCapturePixels), 2 = none asked for or it failed. Between frames only.
Central container for all entities and components.
bool isValid() const
Checks if the handle references a valid resource.
Handle< esengine::ProbeVolume > ProbeVolumeHandle
Handle to a grid of baked irradiance.
constexpr ResourceId kNoResource
std::int32_t i32
Signed 32-bit integer.
std::size_t usize
Size type for indexing and sizes.
glm::mat4 toClipDepthRange(const glm::mat4 &p, ClipDepthRange range)
The projection p as a device keeping range wants it.
GfxReadbackStatus
State of an in-flight readback. GL completes at request time (first poll reports Ready); WebGPU resol...
std::uint32_t u32
Unsigned 32-bit integer.
glm::vec3 rebased(const glm::vec3 &world)
A world position as the frame draws it: from the origin.
CameraView computeCameraView(const glm::mat4 &viewProjection)
static EstellaContext & ctx()
float f32
32-bit floating point
Framebuffer creation parameters.
GfxPixelFormat colorFormat
Color attachment format. RGBA16F needs GfxDevice::supportsFloatTargets.
bool depthStencil
Whether to create depth/stencil attachment.
bool linearFilter
Texture filter mode: false=GL_NEAREST, true=GL_LINEAR.
u32 height
Framebuffer height in pixels.
u32 width
Framebuffer width in pixels.