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): the swapchain's colour encoding is the engine's decision

esengine12d1703f675f
diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.cpp b/src/esengine/renderer/webgpu/WebGPUDevice.cpp
index aad5adfb4..15095b6ea 100644
--- a/src/esengine/renderer/webgpu/WebGPUDevice.cpp
+++ b/src/esengine/renderer/webgpu/WebGPUDevice.cpp
@@ -329,6 +329,11 @@ bool WebGPUDevice::configureSurface(const NativeSurface& window, u32 width, u32
return configureSwapchain(width, height);
}
+/** Whether a surface format makes the hardware encode on write. */
+static bool isSrgbFormat(WGPUTextureFormat f) {
+ return f == WGPUTextureFormat_BGRA8UnormSrgb || f == WGPUTextureFormat_RGBA8UnormSrgb;
+}
+
bool WebGPUDevice::surfaceBytesAreBGRA() const {
return surface_format_ == WGPUTextureFormat_BGRA8Unorm
|| surface_format_ == WGPUTextureFormat_BGRA8UnormSrgb;
@@ -352,11 +357,21 @@ bool WebGPUDevice::configureSwapchain(u32 width, u32 height) {
// BGRA8Unorm, while Vulkan happens to take RGBA8 — and configuring a format the
// surface does not advertise leaves every getCurrentTexture in error rather
// than failing here. So ask, and take the surface's preferred (first) format.
+ //
+ // Of the formats it advertises, take a NON-sRGB one: the engine hands the
+ // surface values that are already display-encoded (the shaders in gamma
+ // mode, the final blit in linear), so an *Srgb surface encodes them twice.
if (adapter_) {
WGPUSurfaceCapabilities caps{};
if (wgpuSurfaceGetCapabilities(surface_, adapter_, &caps) == WGPUStatus_Success
&& caps.formatCount > 0) {
surface_format_ = caps.formats[0];
+ for (size_t i = 0; i < caps.formatCount; ++i) {
+ if (!isSrgbFormat(caps.formats[i])) {
+ surface_format_ = caps.formats[i];
+ break;
+ }
+ }
wgpuSurfaceCapabilitiesFreeMembers(caps);
}
}
diff --git a/tools/goldenProjects.mjs b/tools/goldenProjects.mjs
index e9d3193b1..b049a66b6 100644
--- a/tools/goldenProjects.mjs
+++ b/tools/goldenProjects.mjs
@@ -105,11 +105,8 @@ export const GOLDEN = [
// Two textured platforms and the sky behind them. Measured over three runs
// and stable to the byte; the falling player is NOT, which is why it is not
// here. A frame that lost its textures still spreads, and still fails these.
- //
- // macOS only, and that is a FINDING: Linux reads an exact sRGB encode of
- // these on all three points, because WebGPUDevice takes `caps.formats[0]`
- // and so the surface's sRGB-ness is the driver's answer, not the engine's.
- desktopPixelsHosts: ['macos'],
+ // Every desktop OS must read the same values: the swapchain's sRGB-ness is
+ // the engine's decision, not the driver's, and these are what says so.
desktopPixels: [
{ x: 0.30, y: 0.35, rgb: [121, 76, 32], tol: 12 },
{ x: 0.70, y: 0.20, rgb: [121, 76, 32], tol: 12 },