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.

feat(webgpu): GPU timing via timestamp queries — profiler parity with GL

yhh1c3e94718d72
diff --git a/desktop/src/engine/EngineHost.ts b/desktop/src/engine/EngineHost.ts
index 391b2117a..8d7160b7e 100644
--- a/desktop/src/engine/EngineHost.ts
+++ b/desktop/src/engine/EngineHost.ts
@@ -464,13 +464,23 @@ class EngineHostImpl {
// reads it synchronously (Module.preinitializedWebGPUDevice).
const gpu = (navigator as unknown as {
gpu?: {
- requestAdapter(): Promise<{ requestDevice(): Promise<unknown> } | null>;
+ requestAdapter(): Promise<{
+ features?: { has(name: string): boolean };
+ requestDevice(descriptor?: { requiredFeatures?: string[] }): Promise<unknown>;
+ } | null>;
};
}).gpu;
if (!gpu) throw new Error('WebGPU is not available in this renderer.');
const adapter = await gpu.requestAdapter();
if (!adapter) throw new Error('No WebGPU adapter available.');
- moduleArg.preinitializedWebGPUDevice = await adapter.requestDevice();
+ // Opt into timestamp-query when the adapter supports it, so the engine's GPU
+ // timer can populate gpuMs/gpuScopes (matches the GL timer-query path). Absent
+ // it, the WebGPU backend reports no GPU timing — same as before.
+ const hasTimestamp = adapter.features?.has('timestamp-query') ?? false;
+ const requiredFeatures = hasTimestamp ? ['timestamp-query'] : [];
+ console.info(`[engine] webgpu timestamp-query: ${hasTimestamp ? 'enabled (GPU timing on)' : 'unavailable (no GPU timing)'}`);
+ moduleArg.preinitializedWebGPUDevice = await adapter.requestDevice(
+ requiredFeatures.length ? { requiredFeatures } : undefined);
// The swapchain glue resolves the canvas by document.querySelector, so
// it must be connected (the headless host never attaches it to a view).
// Pin it at the page origin at its backing size: a hidden window never
diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.cpp b/src/esengine/renderer/webgpu/WebGPUDevice.cpp
index 17d7bbcb8..cfa3f6426 100644
--- a/src/esengine/renderer/webgpu/WebGPUDevice.cpp
+++ b/src/esengine/renderer/webgpu/WebGPUDevice.cpp
@@ -85,6 +85,14 @@ void WebGPUDevice::shutdown() {
bind_group_cache_.clear();
bind_group_ = nullptr;
texture_group_ = nullptr;
+
+ // GPU timing resources.
+ if (timestamp_qset_) { wgpuQuerySetRelease(timestamp_qset_); timestamp_qset_ = nullptr; }
+ if (timestamp_resolve_) { wgpuBufferRelease(timestamp_resolve_); timestamp_resolve_ = nullptr; }
+ for (auto& s : gpu_time_ring_) { if (s.buf) wgpuBufferRelease(s.buf); s.buf = nullptr; s.pending = false; }
+ gpu_time_results_.clear();
+ timestamp_supported_ = false;
+ timestamp_init_done_ = false;
if (clear_bind_group_) { wgpuBindGroupRelease(clear_bind_group_); clear_bind_group_ = nullptr; }
for (auto& [key, pipeline] : clear_pipelines_) {
if (pipeline) wgpuRenderPipelineRelease(pipeline);
@@ -1172,6 +1180,22 @@ void WebGPUDevice::beginRenderPass(const RenderPassDesc& desc) {
encoder_ = wgpuDeviceCreateCommandEncoder(device_, nullptr);
+ // GPU timing: time the surface (present) pass — one timestamped pass per frame
+ // in the common case. Reserve a free ring slot; skip timing if none is free.
+ pass_timed_ = false;
+ gpu_time_slot_ = kGpuTimeRing;
+ if (timestamp_supported_ && desc.target == FramebufferHandle::Default) {
+ for (u32 i = 0; i < kGpuTimeRing; ++i) {
+ const u32 s = (gpu_time_next_ + i) % kGpuTimeRing;
+ if (!gpu_time_ring_[s].pending) {
+ gpu_time_slot_ = s;
+ gpu_time_next_ = (s + 1) % kGpuTimeRing;
+ break;
+ }
+ }
+ pass_timed_ = (gpu_time_slot_ < kGpuTimeRing);
+ }
+
const bool scoped = desc.clearW != 0;
WGPURenderPassColorAttachment color{};
color.view = targetView;
@@ -1197,6 +1221,13 @@ void WebGPUDevice::beginRenderPass(const RenderPassDesc& desc) {
}
rp.depthStencilAttachment = &ds;
}
+ WGPUPassTimestampWrites tw{};
+ if (pass_timed_) {
+ tw.querySet = timestamp_qset_;
+ tw.beginningOfPassWriteIndex = 0;
+ tw.endOfPassWriteIndex = 1;
+ rp.timestampWrites = &tw; // outlives the call below (same scope)
+ }
pass_ = wgpuCommandEncoderBeginRenderPass(encoder_, &rp);
bind_group_dirty_ = true;
stencil_ref_ = 0; // a fresh pass encoder resets its stencil reference
@@ -1216,12 +1247,35 @@ void WebGPUDevice::endRenderPass() {
wgpuRenderPassEncoderRelease(pass_);
pass_ = nullptr;
+ // GPU timing: resolve this pass's begin/end timestamps into the reserved ring
+ // slot (recorded on the same encoder, so it runs after the pass on the GPU).
+ const bool timed = pass_timed_ && gpu_time_slot_ < kGpuTimeRing && encoder_;
+ if (timed) {
+ wgpuCommandEncoderResolveQuerySet(encoder_, timestamp_qset_, 0, 2, timestamp_resolve_, 0);
+ wgpuCommandEncoderCopyBufferToBuffer(encoder_, timestamp_resolve_, 0,
+ gpu_time_ring_[gpu_time_slot_].buf, 0, 16);
+ }
+
WGPUCommandBuffer commands = wgpuCommandEncoderFinish(encoder_, nullptr);
wgpuCommandEncoderRelease(encoder_);
encoder_ = nullptr;
wgpuQueueSubmit(queue_, 1, &commands);
wgpuCommandBufferRelease(commands);
+ if (timed) {
+ // Map async — drained by getTimerQueryNs a few frames later (the GpuTimer
+ // ring tolerates the latency).
+ gpu_time_ring_[gpu_time_slot_].pending = true;
+ WGPUBufferMapCallbackInfo cb = WGPU_BUFFER_MAP_CALLBACK_INFO_INIT;
+ cb.mode = WGPUCallbackMode_AllowSpontaneous;
+ cb.callback = &WebGPUDevice::onGpuTimeMapped;
+ cb.userdata1 = this;
+ cb.userdata2 = reinterpret_cast<void*>(static_cast<uintptr_t>(gpu_time_slot_));
+ wgpuBufferMapAsync(gpu_time_ring_[gpu_time_slot_].buf, WGPUMapMode_Read, 0, 16, cb);
+ }
+ pass_timed_ = false;
+ gpu_time_slot_ = kGpuTimeRing;
+
if (frame_view_) { wgpuTextureViewRelease(frame_view_); frame_view_ = nullptr; }
if (frame_texture_) { wgpuTextureRelease(frame_texture_); frame_texture_ = nullptr; }
}
@@ -1348,11 +1402,61 @@ void WebGPUDevice::discardReadback(ReadbackHandle handle) {
// Timing / queries / debug
// =============================================================================
-u32 WebGPUDevice::createTimerQuery() { return 0; } // "no GPU timing" — same contract as a bare GL backend
-void WebGPUDevice::beginTimerQuery(u32) {}
+void WebGPUDevice::ensureTimestamps() {
+ if (timestamp_init_done_) return;
+ timestamp_init_done_ = true;
+ if (!device_ || !wgpuDeviceHasFeature(device_, WGPUFeatureName_TimestampQuery)) return;
+
+ WGPUQuerySetDescriptor qd{};
+ qd.type = WGPUQueryType_Timestamp;
+ qd.count = 2; // begin + end of the timed pass
+ timestamp_qset_ = wgpuDeviceCreateQuerySet(device_, &qd);
+ if (!timestamp_qset_) return;
+
+ WGPUBufferDescriptor rd{};
+ rd.usage = WGPUBufferUsage_QueryResolve | WGPUBufferUsage_CopySrc;
+ rd.size = 16; // 2 × u64 nanosecond timestamps
+ timestamp_resolve_ = wgpuDeviceCreateBuffer(device_, &rd);
+ for (u32 i = 0; i < kGpuTimeRing; ++i) {
+ WGPUBufferDescriptor bd{};
+ bd.usage = WGPUBufferUsage_MapRead | WGPUBufferUsage_CopyDst;
+ bd.size = 16;
+ gpu_time_ring_[i].buf = wgpuDeviceCreateBuffer(device_, &bd);
+ }
+ timestamp_supported_ = timestamp_resolve_ && gpu_time_ring_[0].buf;
+}
+
+u32 WebGPUDevice::createTimerQuery() {
+ ensureTimestamps();
+ return timestamp_supported_ ? 1 : 0; // non-zero → the engine's GpuTimer enables; 0 → no GPU timing (as before)
+}
+
+void WebGPUDevice::beginTimerQuery(u32) {} // timing rides the pass: attached at beginRenderPass, resolved at endRenderPass
void WebGPUDevice::endTimerQuery() {}
bool WebGPUDevice::timerDisjoint() { return false; }
-bool WebGPUDevice::getTimerQueryNs(u32, u64*) { return false; }
+
+bool WebGPUDevice::getTimerQueryNs(u32, u64* outNs) {
+ if (gpu_time_results_.empty()) return false;
+ if (outNs) *outNs = gpu_time_results_.front();
+ gpu_time_results_.erase(gpu_time_results_.begin());
+ return true;
+}
+
+void WebGPUDevice::onGpuTimeMapped(WGPUMapAsyncStatus status, WGPUStringView /*message*/,
+ void* userdata1, void* userdata2) {
+ auto* self = static_cast<WebGPUDevice*>(userdata1);
+ const u32 slot = static_cast<u32>(reinterpret_cast<uintptr_t>(userdata2));
+ if (!self || slot >= kGpuTimeRing) return;
+ WGPUBuffer buf = self->gpu_time_ring_[slot].buf;
+ if (status == WGPUMapAsyncStatus_Success && buf) {
+ const auto* ts = static_cast<const u64*>(wgpuBufferGetConstMappedRange(buf, 0, 16));
+ // Cap the FIFO so a stalled consumer can't grow it without bound.
+ if (ts && ts[1] >= ts[0] && self->gpu_time_results_.size() < 16)
+ self->gpu_time_results_.push_back(ts[1] - ts[0]);
+ wgpuBufferUnmap(buf);
+ }
+ self->gpu_time_ring_[slot].pending = false;
+}
void WebGPUDevice::setWireframe(bool) {}
u32 WebGPUDevice::getError() { return 0; }
diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.hpp b/src/esengine/renderer/webgpu/WebGPUDevice.hpp
index 1c95d92d3..cd6e5a81d 100644
--- a/src/esengine/renderer/webgpu/WebGPUDevice.hpp
+++ b/src/esengine/renderer/webgpu/WebGPUDevice.hpp
@@ -326,6 +326,28 @@ private:
WGPUBindGroup cachedBindGroup(u32 group, u32 mask, const u64* ids, u32 idCount,
const WGPUBindGroupDescriptor& desc);
+ // GPU timing — the WebGPU analog of GLDevice's GL_TIME_ELAPSED timer, so the
+ // profiler's gpuMs/gpuScopes populate on both backends. Active only when the
+ // device has the timestamp-query feature; otherwise createTimerQuery returns 0
+ // and timing stays off (unchanged behavior). The surface (present) pass writes
+ // a begin/end timestamp; endRenderPass resolves them into a ring of readback
+ // buffers mapped asynchronously, and getTimerQueryNs drains the elapsed ns into
+ // the engine's GpuTimer (whose ring already tolerates a few frames of latency).
+ bool timestamp_supported_ = false;
+ bool timestamp_init_done_ = false;
+ WGPUQuerySet timestamp_qset_ = nullptr; ///< 2 slots: begin/end of the timed pass.
+ WGPUBuffer timestamp_resolve_ = nullptr; ///< QueryResolve target (16 bytes).
+ static constexpr u32 kGpuTimeRing = 4;
+ struct GpuTimeSlot { WGPUBuffer buf = nullptr; bool pending = false; };
+ GpuTimeSlot gpu_time_ring_[kGpuTimeRing] = {};
+ u32 gpu_time_next_ = 0; ///< Round-robin cursor into the ring.
+ u32 gpu_time_slot_ = kGpuTimeRing; ///< Slot reserved for the in-flight timed pass (== kGpuTimeRing: none).
+ bool pass_timed_ = false; ///< Did the current pass attach timestampWrites?
+ std::vector<u64> gpu_time_results_; ///< Resolved elapsed-ns, FIFO, drained by getTimerQueryNs.
+ void ensureTimestamps(); ///< Lazily create the query set + buffers (feature-gated).
+ static void onGpuTimeMapped(WGPUMapAsyncStatus status, WGPUStringView message,
+ void* userdata1, void* userdata2);
+
std::unordered_map<u8, WGPUSampler> samplers_; ///< Keyed by packed filter/wrap params.
// Explicit layouts, cached by binding mask (key = group << 32 | mask for