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(renderer): configure the swapchain with a format the surface accepts

esengine0ca946a2a8d6
diff --git a/native/host_js/host_core.cpp b/native/host_js/host_core.cpp
index a5d117a9e..77fa8a27e 100644
--- a/native/host_js/host_core.cpp
+++ b/native/host_js/host_core.cpp
@@ -538,7 +538,7 @@ bool boot(Platform& platform) {
wgpuInstanceWaitAny(a.instance, 1, &df, UINT64_MAX);
if (!a.device) { LOGE("no device"); return false; }
- auto device = makeUnique<WebGPUDevice>(a.device, a.instance);
+ auto device = makeUnique<WebGPUDevice>(a.device, a.instance, a.adapter);
a.gfx = device.get();
if (!bindSurface()) return false;
static EstellaContext context;
diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.cpp b/src/esengine/renderer/webgpu/WebGPUDevice.cpp
index 2bcfdfc12..262c13d6e 100644
--- a/src/esengine/renderer/webgpu/WebGPUDevice.cpp
+++ b/src/esengine/renderer/webgpu/WebGPUDevice.cpp
@@ -53,7 +53,8 @@ u8 packSamplerKey(TextureFilter minFilter, TextureFilter magFilter,
}
} // namespace
-WebGPUDevice::WebGPUDevice(WGPUDevice device, WGPUInstance instance) : device_(device) {
+WebGPUDevice::WebGPUDevice(WGPUDevice device, WGPUInstance instance, WGPUAdapter adapter)
+ : device_(device), adapter_(adapter) {
if (device_) {
queue_ = wgpuDeviceGetQueue(device_);
if (instance) {
@@ -221,9 +222,22 @@ void WebGPUDevice::present() {
#endif // !__EMSCRIPTEN__
bool WebGPUDevice::configureSwapchain(u32 width, u32 height) {
- // The canvas swapchain prefers BGRA on most platforms; RGBA8 is universally
- // valid for emscripten surfaces and keeps readback simple during bring-up.
+ // RGBA8 is universally valid for emscripten surfaces and keeps readback simple.
surface_format_ = WGPUTextureFormat_RGBA8Unorm;
+#if !defined(__EMSCRIPTEN__)
+ // A native surface may accept nothing of the sort — a CAMetalLayer offers only
+ // 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.
+ if (adapter_) {
+ WGPUSurfaceCapabilities caps{};
+ if (wgpuSurfaceGetCapabilities(surface_, adapter_, &caps) == WGPUStatus_Success
+ && caps.formatCount > 0) {
+ surface_format_ = caps.formats[0];
+ wgpuSurfaceCapabilitiesFreeMembers(caps);
+ }
+ }
+#endif
WGPUSurfaceConfiguration cfg{};
cfg.device = device_;
cfg.format = surface_format_;
diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.hpp b/src/esengine/renderer/webgpu/WebGPUDevice.hpp
index f47150809..653b6a602 100644
--- a/src/esengine/renderer/webgpu/WebGPUDevice.hpp
+++ b/src/esengine/renderer/webgpu/WebGPUDevice.hpp
@@ -58,7 +58,10 @@ public:
* native shell that already built the instance to acquire the
* adapter/device passes it here so the surface is created on the SAME
* instance — native Dawn ties a surface to its creating instance. */
- explicit WebGPUDevice(WGPUDevice device = nullptr, WGPUInstance instance = nullptr);
+ /** @param adapter Native hosts pass theirs so the swapchain can ask the surface
+ * which formats it supports; omitting it assumes RGBA8. */
+ explicit WebGPUDevice(WGPUDevice device = nullptr, WGPUInstance instance = nullptr,
+ WGPUAdapter adapter = nullptr);
~WebGPUDevice() override;
void init() override;
@@ -288,6 +291,7 @@ private:
WGPUDevice device_ = nullptr;
WGPUQueue queue_ = nullptr;
WGPUInstance instance_ = nullptr;
+ WGPUAdapter adapter_ = nullptr; ///< Native only, host-owned: surface capability queries.
bool owns_instance_ = true; ///< False when a native host injected its instance (don't release it).
// Surface (the Default framebuffer target). The companion depth-stencil