Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
PostProcessBindings.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3#ifdef ES_ENABLE_POSTPROCESS
4
6#include "ActiveContext.hpp"
7#include "BoundarySpan.hpp"
17
18#include <glm/glm.hpp>
19#include <cstring>
20
21namespace esengine {
22
23static EstellaContext& ctx() { return activeCtx(); }
24
25#define g_device (ctx().tryGet<GfxDevice>())
26#define g_initialized (ctx().state().initialized)
27#define g_renderContext (ctx().tryGet<RenderContext>())
28#define g_resourceManager (ctx().tryGet<resource::ResourceManager>())
29#define g_postProcessPipeline (ctx().tryGet<PostProcessPipeline>())
30
31bool postprocess_init(u32 width, u32 height) {
32 if (!g_initialized || !g_renderContext || !g_resourceManager) return false;
33
34 if (!g_postProcessPipeline) {
37 }
38
39 g_postProcessPipeline->init(width, height);
40 g_device->endRenderPass();
41
42 return g_postProcessPipeline->isInitialized();
43}
44
46 if (g_postProcessPipeline) {
47 // Tear down the passes/FBOs/blit shader, but KEEP the service registered.
48 // The pipeline is owned by RenderFrame (registered as a bare-pointer service
49 // and the very object RenderFrame::begin renders through). Unregistering it
50 // here orphaned it on a warm re-play: `postprocess_init` then saw no service,
51 // built a *separate* owned duplicate, and RenderFrame kept using its own —
52 // now permanently un-initialized — pipeline, so `usePostProcess` went false
53 // and a linear-space scene rendered without the mandatory linear→sRGB encode
54 // (i.e. black). Leaving it registered lets the lazy re-init (_applyForCamera →
55 // init → resize) restore the SAME pipeline RenderFrame draws with.
56 g_postProcessPipeline->shutdown();
57 }
58}
59
60void postprocess_resize(u32 width, u32 height) {
61 if (g_postProcessPipeline) {
62 g_postProcessPipeline->resize(width, height);
63 }
64}
65
66u32 postprocess_addPass(const std::string& name, u32 shaderHandle) {
67 if (!g_postProcessPipeline) return 0;
68 return g_postProcessPipeline->addPass(name, resource::ShaderHandle(shaderHandle));
69}
70
71u32 postprocess_addComputePass(const std::string& name, const std::string& wgsl, const std::string& params,
72 u32 workgroupX, u32 workgroupY) {
73 if (!g_postProcessPipeline) return 0;
74 std::vector<std::string> names;
75 usize start = 0;
76 while (start <= params.size() && !params.empty()) {
77 const usize comma = params.find(',', start);
78 const usize end = comma == std::string::npos ? params.size() : comma;
79 if (end > start) names.push_back(params.substr(start, end - start));
80 if (comma == std::string::npos) break;
81 start = comma + 1;
82 }
83 return g_postProcessPipeline->addComputePass(name, wgsl, names, workgroupX, workgroupY);
84}
85
86u32 postprocess_addNativePass(const std::string& name, const std::string& kind) {
87 if (!g_postProcessPipeline) return 0;
88 return g_postProcessPipeline->addNativePass(name, kind);
89}
90
91bool postprocess_setGIScene(uintptr_t headerPtr, u32 headerCount, uintptr_t sdfPtr, u32 sdfCount, uintptr_t instancesPtr, u32 instanceCount,
92 uintptr_t pixelsPtr, u32 pixelCount) {
93 const f32* header = boundarySpan<f32>(headerPtr, headerCount, "postprocess_setGIScene.header");
94 const f32* sdf = boundarySpan<f32>(sdfPtr, sdfCount, "postprocess_setGIScene.sdf");
95 const f32* instances = boundarySpan<f32>(instancesPtr, instanceCount, "postprocess_setGIScene.instances");
96 const u32* pixels = boundarySpan<u32>(pixelsPtr, pixelCount, "postprocess_setGIScene.pixels");
97 if (!header || !sdf || !instances || !pixels || headerCount < 22) return false;
99 scene.header.assign(header, header + headerCount);
100 scene.sdf.assign(sdf, sdf + sdfCount);
101 scene.instances.assign(instances, instances + instanceCount);
102 scene.pixels.assign(pixels, pixels + pixelCount);
103 scene.full = true;
104 ++scene.version;
105 return true;
106}
107
108bool postprocess_updateGIField(uintptr_t headerPtr, u32 headerCount, uintptr_t sdfPtr, u32 sdfOffset, u32 sdfCount) {
110 if (scene.sdf.empty() || scene.header.size() < 22 || headerCount != scene.header.size() || static_cast<u64>(sdfOffset) + sdfCount > scene.sdf.size()) return false;
111 const f32* header = boundarySpan<f32>(headerPtr, headerCount, "postprocess_updateGIField.header");
112 const f32* sdf = boundarySpan<f32>(sdfPtr, sdfCount, "postprocess_updateGIField.sdf");
113 if (!header || !sdf) return false;
114 std::copy(header, header + headerCount, scene.header.begin());
115 std::copy(sdf, sdf + sdfCount, scene.sdf.begin() + sdfOffset);
116 if (!scene.full) {
117 scene.dirtyBegin = scene.dirtyEnd == scene.dirtyBegin ? sdfOffset : std::min(scene.dirtyBegin, sdfOffset);
118 scene.dirtyEnd = std::max(scene.dirtyEnd, sdfOffset + sdfCount);
119 }
120 ++scene.version;
121 return true;
122}
123
125 return g_postProcessPipeline ? g_postProcessPipeline->supportsCompute() : (g_device && g_device->supportsCompute());
126}
127
128void postprocess_setMsaaSamples(u32 samples) {
129 if (g_postProcessPipeline) g_postProcessPipeline->setRequestedSamples(samples);
130}
131
133 return g_postProcessPipeline ? g_postProcessPipeline->sceneSamples() : 0u;
134}
135
137 return g_device ? g_device->maxSamples() : 0u;
138}
139
140i32 postprocess_hdrFormat(uintptr_t outPtr) {
141 auto* out = boundarySpanMut<f32>(outPtr, 4, "postprocess_hdrFormat.out");
142 if (!out) return 0;
143 for (u32 i = 0; i < 4; ++i) out[i] = 0.0f;
144 if (!g_postProcessPipeline) return 0;
145 const HdrFormatDecision& d = g_postProcessPipeline->hdrFormat();
146 out[0] = static_cast<f32>(static_cast<u8>(d.requested));
147 out[1] = static_cast<f32>(static_cast<u8>(d.effective));
148 out[2] = static_cast<f32>(static_cast<u8>(d.refusal));
149 out[3] = d.linear ? 1.0f : 0.0f;
150 return 1;
151}
152
153std::string postprocess_hdrFormatNames() {
154 if (!g_postProcessPipeline) return {};
155 const HdrFormatDecision& d = g_postProcessPipeline->hdrFormat();
156 return std::string(pixelFormatName(d.requested)) + '|' + pixelFormatName(d.effective)
157 + '|' + hdrRefusalName(d.refusal);
158}
159
160void postprocess_setPassScale(const std::string& passName, f32 scale) {
161 if (g_postProcessPipeline) {
162 g_postProcessPipeline->setPassScale(passName, scale);
163 }
164}
165
166void postprocess_requestSkyCapture(const std::string& passName, u32 width, u32 height) {
167 if (g_postProcessPipeline) g_postProcessPipeline->requestSkyCapture(passName, width, height);
168}
169
171 return g_postProcessPipeline ? g_postProcessPipeline->pollSkyCapture() : 2;
172}
173
174u32 postprocess_giStats(uintptr_t dst, u32 capacity) {
175 if (capacity < kDynamicGIStatFloats) return 0;
176 f32* out = boundarySpanMut<f32>(dst, kDynamicGIStatFloats, "postprocess_giStats.out");
177 if (!out) return 0;
178 const DynamicGIStats& s = dynamicGIStats();
179 const f32 v[kDynamicGIStatFloats] = {s.width, s.height, s.probeSpacing, s.probes, s.probeRays, s.cardPixels, s.cardPixelsRelit, s.cardRays,
180 s.applyPixels, s.applyLoadsPerPixel, s.forcedAttachmentBytes, s.residentBytes, s.levels, s.lights, s.instances, s.frame};
181 std::memcpy(out, v, sizeof(v));
183}
184
185bool postprocess_takeSkyCapture(uintptr_t dst, u32 bytes) {
186 if (!g_postProcessPipeline || dst == 0) return false;
187 const std::vector<u8>& px = g_postProcessPipeline->skyCapturePixels();
188 if (px.empty() || px.size() > bytes) return false;
189 std::memcpy(reinterpret_cast<void*>(dst), px.data(), px.size());
190 return true;
191}
192
193void postprocess_setPassContinuesEffect(const std::string& passName) {
194 if (g_postProcessPipeline) {
195 g_postProcessPipeline->setPassContinuesEffect(passName);
196 }
197}
198
199void postprocess_setUniformFloat(const std::string& passName,
200 const std::string& uniform, f32 value) {
201 if (g_postProcessPipeline) {
202 g_postProcessPipeline->setPassUniformFloat(passName, uniform, value);
203 }
204}
205
206void postprocess_setPassTexture(const std::string& passName,
207 const std::string& uniform, u32 textureHandle) {
208 if (!g_postProcessPipeline) return;
209 u32 glId = 0;
210 if (textureHandle != 0) {
211 if (auto* rm = ctx().tryGet<resource::ResourceManager>()) {
212 if (auto* tex = rm->getTexture(resource::TextureHandle(textureHandle))) {
213 glId = tex->getId();
214 }
215 }
216 }
217 g_postProcessPipeline->setPassTexture(passName, uniform, glId);
218}
219
220void postprocess_setUniformVec4(const std::string& passName,
221 const std::string& uniform,
222 f32 x, f32 y, f32 z, f32 w) {
223 if (g_postProcessPipeline) {
224 g_postProcessPipeline->setPassUniformVec4(passName, uniform, glm::vec4(x, y, z, w));
225 }
226}
227
228void postprocess_begin() {
229 if (g_postProcessPipeline) {
230 g_postProcessPipeline->begin();
231 }
232}
233
234void postprocess_end() {
235 // The chain itself ran inside Renderer.end(), declared to the frame's graph
236 // beside the scene pass that feeds it. This closes the capture.
237 if (g_postProcessPipeline) {
238 g_postProcessPipeline->chainDone();
239 }
240}
241
243 if (!g_postProcessPipeline) return false;
244 return g_postProcessPipeline->isInitialized();
245}
246
247void postprocess_setBypass(bool bypass) {
248 if (g_postProcessPipeline) {
249 g_postProcessPipeline->setBypass(bypass);
250 }
251}
252
253void postprocess_setOutputTransform(u32 transform) {
254 if (!g_postProcessPipeline) return;
255 // An unknown value is the identity, not a guess: a project built against a
256 // newer curve list must not get a different one silently.
257 g_postProcessPipeline->setOutputTransform(transform == 1 ? OutputTransform::ACES
258 : transform == 2 ? OutputTransform::Neutral
260}
261
262void postprocess_setDisplayLut(u32 textureHandle, f32 amount) {
263 if (!g_postProcessPipeline) return;
264 u32 glId = 0;
265 if (textureHandle != 0) {
266 if (auto* rm = ctx().tryGet<resource::ResourceManager>()) {
267 if (auto* tex = rm->getTexture(resource::TextureHandle(textureHandle))) glId = tex->getId();
268 }
269 }
270 g_postProcessPipeline->setDisplayLut(glId, amount);
271}
272
274 if (g_postProcessPipeline) g_postProcessPipeline->setViewExposure(ev);
275}
276
278 if (g_postProcessPipeline) {
279 g_postProcessPipeline->clearPasses();
280 }
281}
282
284 if (g_postProcessPipeline) {
285 g_postProcessPipeline->setOutputViewport(x, y, w, h);
286 }
287}
288
289void postprocess_setPresentRequired(bool required) {
290 if (g_postProcessPipeline) {
291 g_postProcessPipeline->setPresentRequired(required);
292 }
293}
294
296 if (!g_postProcessPipeline) return;
297 g_postProcessPipeline->beginScreenCapture();
298}
299
301 if (!g_postProcessPipeline) return;
302 g_postProcessPipeline->endScreenCapture();
303}
304
306 if (!g_postProcessPipeline) return;
307 g_postProcessPipeline->executeScreenPasses();
308}
309
310u32 postprocess_addScreenPass(const std::string& name, u32 shaderHandle) {
311 if (!g_postProcessPipeline) return 0;
312 return g_postProcessPipeline->addScreenPass(name, resource::ShaderHandle(shaderHandle));
313}
314
316 if (g_postProcessPipeline) {
317 g_postProcessPipeline->clearScreenPasses();
318 }
319}
320
321void postprocess_setScreenUniformFloat(const std::string& passName,
322 const std::string& uniform, f32 value) {
323 if (g_postProcessPipeline) {
324 g_postProcessPipeline->setScreenPassUniformFloat(passName, uniform, value);
325 }
326}
327
328void postprocess_setScreenUniformVec4(const std::string& passName,
329 const std::string& uniform,
330 f32 x, f32 y, f32 z, f32 w) {
331 if (g_postProcessPipeline) {
332 g_postProcessPipeline->setScreenPassUniformVec4(passName, uniform, glm::vec4(x, y, z, w));
333 }
334}
335
336} // namespace esengine
337
338#endif // ES_ENABLE_POSTPROCESS
Always-on validation for JS-supplied pointer+length pairs at the WASM boundary.
Custom geometry for user-defined mesh rendering.
Diffuse global illumination from the scene's distance field and surface cache, run as compute passes ...
#define g_renderContext
#define g_resourceManager
#define g_initialized
#define g_device
Abstract graphics device interface.
Immediate mode 2D drawing API.
Post-processing effects pipeline.
Rendering context containing all renderer state.
Frame resources and the shared scene rendering contract.
Central resource management system.
System for computing hierarchical world transforms.
ServiceRegistry & services()
Post-processing effects pipeline.
void registerOwned(Unique< T > service)
Registers an owned service.
Handle< esengine::Shader > ShaderHandle
Handle to a shader resource.
Definition Handle.hpp:214
Handle< esengine::Texture > TextureHandle
Handle to a texture resource.
Definition Handle.hpp:217
const T * boundarySpan(uintptr_t ptr, u64 count, const char *what)
Validates a JS-supplied pointer to count elements of T and returns it typed.
constexpr Unique< T > makeUnique(Args &&... args)
Creates a Unique pointer with the given arguments.
Definition Types.hpp:99
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
bool postprocess_init(u32 width, u32 height)
const char * pixelFormatName(GfxPixelFormat format)
A pixel format as a word.
Definition HdrFormat.hpp:47
void postprocess_setUniformVec4(const std::string &passName, const std::string &uniform, f32 x, f32 y, f32 z, f32 w)
void postprocess_clearScreenPasses()
u32 postprocess_maxMsaaSamples()
void postprocess_setBypass(bool bypass)
void postprocess_setMsaaSamples(u32 samples)
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
void postprocess_shutdown()
void postprocess_setPassTexture(const std::string &passName, const std::string &uniform, u32 textureHandle)
void postprocess_end()
void postprocess_setUniformFloat(const std::string &passName, const std::string &uniform, f32 value)
std::size_t usize
Size type for indexing and sizes.
Definition Types.hpp:64
@ ACES
Narkowicz ACES filmic curve, for a scene that carries HDR radiance.
@ None
Encode only. Values above 1 clip, which is what LDR content means.
@ Neutral
Khronos PBR Neutral: hue and saturation survive, highlights roll off.
DynamicGIScene & dynamicGIScene()
Definition DynamicGI.cpp:15
void postprocess_begin()
void postprocess_setPresentRequired(bool required)
T * boundarySpanMut(uintptr_t ptr, u64 count, const char *what)
Mutable variant of boundarySpan, for entry points that WRITE into a JS buffer.
void postprocess_setPassScale(const std::string &passName, f32 scale)
u32 postprocess_giStats(uintptr_t dst, u32 capacity)
const DynamicGIStats & dynamicGIStats()
Definition DynamicGI.cpp:24
i32 postprocess_hdrFormat(uintptr_t outPtr)
The format decision the last frame COMMITTED to, into four floats at outPtr: requested,...
bool postprocess_takeSkyCapture(uintptr_t dst, u32 bytes)
void postprocess_resize(u32 width, u32 height)
void postprocess_setOutputTransform(u32 transform)
bool postprocess_updateGIField(uintptr_t headerPtr, u32 headerCount, uintptr_t sdfPtr, u32 sdfOffset, u32 sdfCount)
Updates the scene the dynamic GI traces after the camera moved: the header, and sdfCount floats of th...
void postprocess_setScreenUniformVec4(const std::string &passName, const std::string &uniform, f32 x, f32 y, f32 z, f32 w)
u32 postprocess_effectiveMsaaSamples()
i32 postprocess_pollSkyCapture()
std::string postprocess_hdrFormatNames()
The last committed decision as words: "requested|effective|reason". Empty before a frame has committe...
void postprocess_requestSkyCapture(const std::string &passName, u32 width, u32 height)
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
std::uint8_t u8
Unsigned 8-bit integer.
Definition Types.hpp:41
void postprocess_executeScreenPasses()
void postprocess_setOutputViewport(u32 x, u32 y, u32 w, u32 h)
const char * hdrRefusalName(HdrRefusal why)
The refusal as a word, for a log line and for a reader.
Definition HdrFormat.hpp:31
void postprocess_setDisplayLut(u32 textureHandle, f32 amount)
bool postprocess_setGIScene(uintptr_t headerPtr, u32 headerCount, uintptr_t sdfPtr, u32 sdfCount, uintptr_t instancesPtr, u32 instanceCount, uintptr_t pixelsPtr, u32 pixelCount)
Hands the dynamic GI the scene it traces: the header, the distance field, the instances and the card ...
void postprocess_beginScreenCapture()
void postprocess_setScreenUniformFloat(const std::string &passName, const std::string &uniform, f32 value)
void postprocess_clearPasses()
u32 postprocess_addPass(const std::string &name, u32 shaderHandle)
constexpr u32 kDynamicGIStatFloats
Definition DynamicGI.hpp:94
static EstellaContext & ctx()
EstellaContext & activeCtx()
The engine context every binding routes through — single source of truth.
u32 postprocess_addNativePass(const std::string &name, const std::string &kind)
Adds a pass the engine implements itself ("voxelGI"); its parameters are set by name like any pass's.
void postprocess_endScreenCapture()
void postprocess_setPassContinuesEffect(const std::string &passName)
bool postprocess_isInitialized()
void postprocess_setViewExposure(f32 ev)
u32 postprocess_addScreenPass(const std::string &name, u32 shaderHandle)
float f32
32-bit floating point
Definition Types.hpp:59
bool postprocess_supportsCompute()
Whether compute passes run on this device: WebGPU yes, WebGL2 no.
u32 postprocess_addComputePass(const std::string &name, const std::string &wgsl, const std::string &params, u32 workgroupX, u32 workgroupY)
Adds a pass that is a WGSL compute shader over the picture (see PostProcessPipeline::addComputePass).
The format decision one frame committed to.
Definition HdrFormat.hpp:67