Non deterministic texture format in example_glfw_wgpu #9576

Closed
opened 2026-10-09 16:59:12 +00:00 by ypujante · 0 comments
ypujante commented 2026-10-09 16:59:12 +00:00 (Migrated from github.com)

Version/Branch of Dear ImGui:

Version 1.92.9b, Branch: docking

Back-ends:

imgui_impl_glfw.cpp + imgui_impl_wgpu.cpp

Compiler, OS:

macOS Clang 21

Full config/build information:

// (Copy from the next line. Keep the ``` markers for formatting.)

Dear ImGui 1.92.9b (19291)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=201703
define: __APPLE__
define: __GNUC__=4
define: __clang_version__=21.0.0 (clang-2100.3.34.2)
define: IMGUI_HAS_VIEWPORT
define: IMGUI_HAS_DOCK
IM_ASSERT: runs expression: OK. expand size: OK
--------------------------------
io.BackendPlatformName: imgui_impl_glfw (3501)
io.BackendRendererName: imgui_impl_wgpu (WGPU, Native)
io.ConfigFlags: 0x00000080
 DockingEnable
io.ConfigDpiScaleFonts
io.ConfigViewportsNoDecoration
io.ConfigViewportsNoDefaultParent
io.ConfigDockingWithShift
io.ConfigMacOSXBehaviors
io.ConfigNavCaptureKeyboard
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigWindowsMoveFromTitleBarOnly
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x0000181E
 HasMouseCursors
 HasSetMousePos
 PlatformHasViewports
 HasMouseHoveredViewport
 RendererHasVtxOffset
 RendererHasTextures
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,128
io.Fonts->FontLoaderName: stb_truetype
io.DisplaySize: 1280.00,720.00
io.DisplayFramebufferScale: 2.00,2.00
--------------------------------
style.WindowPadding: 8.00,8.00
style.WindowBorderSize: 1.00
style.FramePadding: 4.00,3.00
style.FrameRounding: 0.00
style.FrameBorderSize: 0.00
style.ItemSpacing: 8.00,4.00
style.ItemInnerSpacing: 4.00,4.00

Details:

I scratched my head for a long time trying to figure out why the iconic blue buttons from ImGui did not look exactly the same in my application.

I traced it down to this code in example_glfw_wgpu.cpp

// line 469
    WGPUSurfaceCapabilities surface_capabilities = {};
    wgpuSurfaceGetCapabilities(wgpu_surface, adapter, &surface_capabilities);

    preferred_fmt = surface_capabilities.formats[0];

The issue is that picking surface_capabilities.formats[0] seems to be completely non deterministic.

I printed the preferred_fmt value for Emscripten and I got WGPUTextureFormat_BGRA8Unorm (which looks right)
On native macOS I got WGPUTextureFormat_BGRA8UnormSrgb (which looks wrong)

Forcing WGPUTextureFormat_BGRA8Unorm on macOS fixes the issue...

I think a little helper, something like that would help address this issue:

static WGPUTextureFormat SelectSurfaceFormat(const WGPUSurfaceCapabilities& capabilities)
{
  const WGPUTextureFormat preferredFormats[] = {
    WGPUTextureFormat_BGRA8Unorm,
    WGPUTextureFormat_RGBA8Unorm,
    WGPUTextureFormat_BGRA8UnormSrgb,
    WGPUTextureFormat_RGBA8UnormSrgb,
  };

  for(WGPUTextureFormat preferredFormat : preferredFormats)
  {
    for(size_t i = 0; i < capabilities.formatCount; i++)
    {
      if(capabilities.formats[i] == preferredFormat)
        return preferredFormat;
    }
  }

  return capabilities.formatCount > 0 ? capabilities.formats[0] : WGPUTextureFormat_Undefined;
}

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

### Version/Branch of Dear ImGui: Version 1.92.9b, Branch: docking ### Back-ends: imgui_impl_glfw.cpp + imgui_impl_wgpu.cpp ### Compiler, OS: macOS Clang 21 ### Full config/build information: // (Copy from the next line. Keep the ``` markers for formatting.) ```cpp Dear ImGui 1.92.9b (19291) -------------------------------- sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20 define: __cplusplus=201703 define: __APPLE__ define: __GNUC__=4 define: __clang_version__=21.0.0 (clang-2100.3.34.2) define: IMGUI_HAS_VIEWPORT define: IMGUI_HAS_DOCK IM_ASSERT: runs expression: OK. expand size: OK -------------------------------- io.BackendPlatformName: imgui_impl_glfw (3501) io.BackendRendererName: imgui_impl_wgpu (WGPU, Native) io.ConfigFlags: 0x00000080 DockingEnable io.ConfigDpiScaleFonts io.ConfigViewportsNoDecoration io.ConfigViewportsNoDefaultParent io.ConfigDockingWithShift io.ConfigMacOSXBehaviors io.ConfigNavCaptureKeyboard io.ConfigInputTextCursorBlink io.ConfigWindowsResizeFromEdges io.ConfigWindowsMoveFromTitleBarOnly io.ConfigMemoryCompactTimer = 60.0 io.BackendFlags: 0x0000181E HasMouseCursors HasSetMousePos PlatformHasViewports HasMouseHoveredViewport RendererHasVtxOffset RendererHasTextures -------------------------------- io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,128 io.Fonts->FontLoaderName: stb_truetype io.DisplaySize: 1280.00,720.00 io.DisplayFramebufferScale: 2.00,2.00 -------------------------------- style.WindowPadding: 8.00,8.00 style.WindowBorderSize: 1.00 style.FramePadding: 4.00,3.00 style.FrameRounding: 0.00 style.FrameBorderSize: 0.00 style.ItemSpacing: 8.00,4.00 style.ItemInnerSpacing: 4.00,4.00 ``` ### Details: I scratched my head for a long time trying to figure out why the iconic blue buttons from ImGui did not look exactly the same in my application. I traced it down to this code in example_glfw_wgpu.cpp ``` // line 469 WGPUSurfaceCapabilities surface_capabilities = {}; wgpuSurfaceGetCapabilities(wgpu_surface, adapter, &surface_capabilities); preferred_fmt = surface_capabilities.formats[0]; ``` The issue is that picking `surface_capabilities.formats[0]` seems to be completely non deterministic. I printed the preferred_fmt value for Emscripten and I got `WGPUTextureFormat_BGRA8Unorm` (which looks right) On native macOS I got `WGPUTextureFormat_BGRA8UnormSrgb` (which looks wrong) Forcing `WGPUTextureFormat_BGRA8Unorm` on macOS fixes the issue... I think a little helper, something like that would help address this issue: ```cpp static WGPUTextureFormat SelectSurfaceFormat(const WGPUSurfaceCapabilities& capabilities) { const WGPUTextureFormat preferredFormats[] = { WGPUTextureFormat_BGRA8Unorm, WGPUTextureFormat_RGBA8Unorm, WGPUTextureFormat_BGRA8UnormSrgb, WGPUTextureFormat_RGBA8UnormSrgb, }; for(WGPUTextureFormat preferredFormat : preferredFormats) { for(size_t i = 0; i < capabilities.formatCount; i++) { if(capabilities.formats[i] == preferredFormat) return preferredFormat; } } return capabilities.formatCount > 0 ? capabilities.formats[0] : WGPUTextureFormat_Undefined; } ``` ### Screenshots/Video: _No response_ ### Minimal, Complete and Verifiable Example code:
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ocornut/imgui#9576
No description provided.