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.

fix(render): one GPU timer per frame, because the API has one place to put it

esengine55f2d1adfccf
diff --git a/sdk/src/wasm.ts b/sdk/src/wasm.ts
index f3756717e..c9c6a6cc8 100644
--- a/sdk/src/wasm.ts
+++ b/sdk/src/wasm.ts
@@ -476,7 +476,7 @@ export interface ESEngineModule {
engine_getCpuScopes?(): string;
/** Last frame's named counters as a JSON object {"render.culled": n, …}. */
engine_getCounters?(): string;
- /** Last frame's per-pass GPU times as a JSON object {"submit": ms, …}. */
+ /** Last frame's per-pass GPU times as a JSON object {"graph": ms, …}. */
engine_getGpuScopes?(): string;
/** Resident texture VRAM (RGBA8 estimate, bytes) for the profiler's memory pillar. */
renderer_getTextureBytes?(): number;
diff --git a/src/esengine/renderer/frame/RenderFrame.cpp b/src/esengine/renderer/frame/RenderFrame.cpp
index 8cafd5a4d..dbabcf5f9 100644
--- a/src/esengine/renderer/frame/RenderFrame.cpp
+++ b/src/esengine/renderer/frame/RenderFrame.cpp
@@ -239,15 +239,11 @@ void RenderFrame::drawScene() {
// which atlas tile each light reads, so the block has to go up after it.
context_.lights().uploadAndBind();
- gpu_timer_.poll(device_);
{
ES_PROFILE_SCOPE("render.submit");
- gpu_timer_.begin(device_);
draw_list_.execute(device_, pool_, context_.materials(), context_.getWhiteTextureId(),
&frame_capture_, context_.skinUbo());
- gpu_timer_.end(device_);
}
- FrameProfiler::get().gpuScope("submit", gpu_timer_.lastMs());
// Handed over to whatever the graph runs next. Blend/depth/colour-mask come
// from each fullscreen pass's own pipeline, but scissor is dynamic state and
@@ -513,11 +509,11 @@ void RenderFrame::end() {
{
ES_PROFILE_SCOPE("render.graph");
- gpu_timer_pp_.poll(device_);
- gpu_timer_pp_.begin(device_);
+ gpu_timer_.poll(device_);
+ gpu_timer_.begin(device_);
graph_.execute();
- gpu_timer_pp_.end(device_);
- FrameProfiler::get().gpuScope("graph", gpu_timer_pp_.lastMs());
+ gpu_timer_.end(device_);
+ FrameProfiler::get().gpuScope("graph", gpu_timer_.lastMs());
}
scene_resource_ = rg::kNoResource;
@@ -532,11 +528,8 @@ void RenderFrame::end() {
}
}
- // The graph's own time is the whole of it — the scene pass runs inside it, so
- // adding the submit timer to this would count the scene twice. The submit
- // timer stays as the scene's own share, published as its gpuScope.
- const f32 graphMs = gpu_timer_pp_.lastMs();
- stats_.gpu_time_ms = graphMs < 0.0f ? gpu_timer_.lastMs() : graphMs;
+ // The graph's own time is the whole frame's — the scene pass runs inside it.
+ stats_.gpu_time_ms = gpu_timer_.lastMs();
// The frame's pass closes HERE, not at the next begin: a WebGPU backend
// submits on endRenderPass, and a surface texture acquired this task must
diff --git a/src/esengine/renderer/frame/RenderFrame.hpp b/src/esengine/renderer/frame/RenderFrame.hpp
index c32e6f7c2..e0b2b2a60 100644
--- a/src/esengine/renderer/frame/RenderFrame.hpp
+++ b/src/esengine/renderer/frame/RenderFrame.hpp
@@ -282,8 +282,14 @@ private:
i32 pollReadback(ReadbackHandle& handle, std::vector<u8>& pixels, u32 w, u32 h);
Stats stats_;
+ /**
+ * ONE per frame, spanning the graph. GL's TIME_ELAPSED has a single global
+ * target and WebGPU's timestamps land in one queue, so a second timer opened
+ * inside this one's span does not measure a nested region — it ends this
+ * query on GL and steals this reading on WebGPU. The scene is a pass of the
+ * graph, so its own share is a per-pass question, not a second timer.
+ */
GpuTimer gpu_timer_;
- GpuTimer gpu_timer_pp_;
FrameCapture frame_capture_;
std::vector<u8> snapshot_pixels_;
RenderTargetManager::Handle replay_rt_ = 0;