Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

perf(postprocess): an effect chain keeps its uniform buffers from frame to frame - a camera's chain is said again every frame, and each pass made a uniform buffer and deleted it each time, seventeen a frame for one lake scene; a cleared chain's buffers are taken over by the next chain's passes of the same size, and one nothing takes in eight clears is deleted

yhhf6defe876731
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7b6dddc80..67db0f8ab 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -35,6 +35,7 @@ published separately; it ships inside the editor.
### Fixed
+- **An effect chain keeps its uniform buffers from frame to frame.** A camera's post-process chain is said again every frame, and each of its passes made a uniform buffer for its parameters and deleted it when the chain was cleared: seventeen buffers created and seventeen deleted every frame for a lake scene with a volume's usual effects, counted in the GL calls of an exported page. The buffers of a cleared chain are now taken over by the passes of the next chain that need one the same size, and one that nothing has taken in eight clears is deleted. The same scene creates none in a steady frame.
- **Walking a frame's meshes costs less, and the profiler says what it costs.** Every mesh, in every view, asked its material's layout several questions by name (does it read the picture behind it, is its alpha coverage, does it cut out a shadow), each a string search; they are answered once when the layout is registered, and the last material and layout asked for are kept. A child of a model (most of an imported scene's meshes) was decomposed afresh by every view of every frame though its matrix had not changed; it is decomposed again only when it moves. And the mesh walk read the clock at every phase of every mesh while profiled, which was four in ten samples of a profiled frame and most of the time it reported; it is timed whole (the split by phase is the build option `ES_PROFILE_COLLECT_PHASES`). On the Bistro scene at 640x360, the best of seven interleaved runs went from 9.97 to 6.83 ms a frame, and a grid of 4096 meshes on one material from 4.08 to 3.09 ms. `render-submit-cost.mjs --wall` times frames with the profiler off.
- **Sprite and particle shaders compile in the background too.** With `quality.asyncShaderCompile`, mesh and material shaders were compiled without holding a frame, and the batch shader's variants (sprites, text, lit sprites) and the particle shader were not: each was linked in place when first needed, measured at 350 to 490 ms of one frame mid-play under ANGLE. They are deferred the same way now, their texture units set when the link lands. Without the setting nothing changes.
- **A shader warm-up waits for the scene still being made.** `warmShaders` (what `quality.prewarmShaders` runs behind the loading screen) stopped at the first three frames in which nothing was compiling. A ground still painting its pages or a field still growing its cells draws seconds later, with shaders nothing had asked for yet, so whether a scene opened complete depended on which arrived first: of the runs of one lake scene, about half opened at 13 s and went on compiling for eight more, the rest at 25 s and complete. Terrain and Foliage now hold the warm-up while they are preparing, and every run opens complete; `holdShaderWarmup(app, busy)` is how a game's own generated content does the same. The wait is still bounded by `prewarmTimeoutMs`.
diff --git a/src/esengine/renderer/frame/PostProcessPipeline.cpp b/src/esengine/renderer/frame/PostProcessPipeline.cpp
index 0c3329ad0..2a04d55c2 100644
--- a/src/esengine/renderer/frame/PostProcessPipeline.cpp
+++ b/src/esengine/renderer/frame/PostProcessPipeline.cpp
@@ -210,6 +210,7 @@ void PostProcessPipeline::shutdown() {
for (auto& pass : screenPasses_) releasePassResources(pass);
releasePassResources(sky_capture_.copy);
releasePassResources(cloud_shadow_copy_);
+ dropSpareParamUbos();
sky_capture_.target.reset();
sky_capture_.state = SkyCapture::State::Idle;
for (auto& [source, program] : compute_cache_) device_.deleteCompute(program);
@@ -774,9 +775,27 @@ void PostProcessPipeline::releasePassResources(PostProcessPass& pass) {
if (pass.paramUbo != BufferHandle::Invalid) {
device_.deleteBuffer(pass.paramUbo);
pass.paramUbo = BufferHandle::Invalid;
+ pass.paramUboBytes = 0;
}
}
+BufferHandle PostProcessPipeline::takeParamUbo(u32 bytes, const void* data) {
+ for (usize i = 0; i < spare_param_ubos_.size(); ++i) {
+ if (spare_param_ubos_[i].bytes != bytes) continue;
+ const BufferHandle handle = spare_param_ubos_[i].handle;
+ spare_param_ubos_[i] = spare_param_ubos_.back();
+ spare_param_ubos_.pop_back();
+ device_.updateBuffer(handle, 0, data, bytes);
+ return handle;
+ }
+ return device_.createBuffer({GfxBufferUsage::Uniform, bytes, /*dynamic=*/true}, GfxContent::retained(), data);
+}
+
+void PostProcessPipeline::dropSpareParamUbos() {
+ for (const SpareParamUbo& spare : spare_param_ubos_) device_.deleteBuffer(spare.handle);
+ spare_param_ubos_.clear();
+}
+
void PostProcessPipeline::setPassUniformFloat(const std::string& passName,
const std::string& uniform, f32 value) {
if (auto* pass = findPass(passName)) {
@@ -1065,9 +1084,8 @@ void PostProcessPipeline::renderPass(PostProcessPass& pass, const rg::PassContex
}
pass.paramDirty = false;
if (pass.paramUbo == BufferHandle::Invalid) {
- pass.paramUbo = device->createBuffer(
- {GfxBufferUsage::Uniform, layout->blockSize, /*dynamic=*/true},
- GfxContent::retained(), pass.paramBytes.data());
+ pass.paramUbo = takeParamUbo(layout->blockSize, pass.paramBytes.data());
+ pass.paramUboBytes = layout->blockSize;
} else {
device->updateBuffer(pass.paramUbo, 0, pass.paramBytes.data(),
static_cast<u32>(pass.paramBytes.size()));
@@ -1169,8 +1187,8 @@ void PostProcessPipeline::computePass(PostProcessPass& pass, const rg::PassConte
}
const u32 paramBytes = static_cast<u32>(values.size() * sizeof(f32));
if (pass.paramUbo == BufferHandle::Invalid) {
- pass.paramUbo = device_.createBuffer({GfxBufferUsage::Uniform, paramBytes, /*dynamic=*/true},
- GfxContent::retained(), values.data());
+ pass.paramUbo = takeParamUbo(paramBytes, values.data());
+ pass.paramUboBytes = paramBytes;
} else {
device_.updateBuffer(pass.paramUbo, 0, values.data(), paramBytes);
}
@@ -1202,7 +1220,21 @@ void PostProcessPipeline::computePass(PostProcessPass& pass, const rg::PassConte
}
void PostProcessPipeline::clearPasses() {
- for (auto& pass : passes_) releasePassResources(pass);
+ // What no chain took over in the last few clears is nobody's; what this chain holds is a later one's to take.
+ constexpr u32 kSpareClears = 8;
+ for (usize i = 0; i < spare_param_ubos_.size();) {
+ if (++spare_param_ubos_[i].clears <= kSpareClears) { ++i; continue; }
+ device_.deleteBuffer(spare_param_ubos_[i].handle);
+ spare_param_ubos_[i] = spare_param_ubos_.back();
+ spare_param_ubos_.pop_back();
+ }
+ for (auto& pass : passes_) {
+ if (pass.paramUbo == BufferHandle::Invalid) continue;
+ if (pass.paramUboBytes > 0) spare_param_ubos_.push_back({pass.paramUbo, pass.paramUboBytes, 0});
+ else device_.deleteBuffer(pass.paramUbo);
+ pass.paramUbo = BufferHandle::Invalid;
+ pass.paramUboBytes = 0;
+ }
passes_.clear();
// The display look is the chain's, like its passes: the next camera says its own.
display_lut_texture_ = 0;
diff --git a/src/esengine/renderer/frame/PostProcessPipeline.hpp b/src/esengine/renderer/frame/PostProcessPipeline.hpp
index a3b234bbd..659025cd7 100644
--- a/src/esengine/renderer/frame/PostProcessPipeline.hpp
+++ b/src/esengine/renderer/frame/PostProcessPipeline.hpp
@@ -108,6 +108,8 @@ struct PostProcessPass {
/// Rebuilt from float/vec4Uniforms over the layout defaults when dirty.
std::vector<u8> paramBytes;
BufferHandle paramUbo = BufferHandle::Invalid;
+ /** @brief The size paramUbo was made with: what lets the next chain's pass of the same size take it over. */
+ u32 paramUboBytes = 0;
bool paramDirty = true;
};
@@ -677,6 +679,18 @@ private:
resource::ShaderHandle compileBlit(const std::vector<std::string>& features);
/// Frees a pass's GPU-side param UBO (clearPasses/shutdown).
void releasePassResources(PostProcessPass& pass);
+ /**
+ * @brief A params block of `bytes` holding `data`: one a cleared chain left behind, if any is that size, or a new one.
+ *
+ * A camera's chain is said again every frame (clearPasses, then its passes), and each pass made and deleted a uniform buffer
+ * of its own each time: seventeen a frame for a lake scene's effects. The buffers of a cleared chain are kept for a few
+ * clears (a frame clears once per camera, and a camera with no effects sits between two frames of one with them), and one
+ * nothing took over in that time is deleted.
+ */
+ BufferHandle takeParamUbo(u32 bytes, const void* data);
+ void dropSpareParamUbos();
+ struct SpareParamUbo { BufferHandle handle; u32 bytes; u32 clears; };
+ std::vector<SpareParamUbo> spare_param_ubos_;
GfxDevice& device_;
RenderContext& context_;
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 57b1067ec..74b95b74a 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -4788,4 +4788,6 @@ export const NOTED = {
{"note": "Sprite and particle shaders compile in the background too."},
"perf(render): walking a frame's meshes asks nothing by name - a material's per-draw questions were string searches, a still child was decomposed again by every view, and the walk's own clock was most of what it reported":
{"note": "Walking a frame's meshes costs less, and the profiler says what it costs."},
+ "perf(postprocess): an effect chain keeps its uniform buffers from frame to frame - a camera's chain is said again every frame, and each pass made a uniform buffer and deleted it each time, seventeen a frame for one lake scene; a cleared chain's buffers are taken over by the next chain's passes of the same size, and one nothing takes in eight clears is deleted":
+ {"note": "An effect chain keeps its uniform buffers from frame to frame."},
};