From b380f0ee73f813482b0a36bc0d966656c8273077 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Wed, 16 Sep 2026 19:26:46 -0700 Subject: [PATCH] Fixed explicit swap chains in a headless init: MTL screenshots, MTL/VK/WGPU window list compaction, MTL/WGPU user depth pipeline format. (#3984) --- src/renderer_mtl.cpp | 24 ++++++++++++------------ src/renderer_vk.cpp | 12 +++++------- src/renderer_webgpu.cpp | 21 +++++++++------------ src/renderer_webgpu.h | 1 + 4 files changed, 27 insertions(+), 31 deletions(-) diff --git a/src/renderer_mtl.cpp b/src/renderer_mtl.cpp index ee2d06650..19b0d00b6 100644 --- a/src/renderer_mtl.cpp +++ b/src/renderer_mtl.cpp @@ -1787,17 +1787,14 @@ static_assert(BX_COUNTOF(s_accessNames) == Access::Count, "Invalid s_accessNames { --m_numWindows; - if (m_numWindows > 1) + if (m_numWindows != denseIdx) { FrameBufferHandle handle = m_windows[m_numWindows]; - m_windows[m_numWindows] = {kInvalidHandle}; - - if (m_numWindows != denseIdx) - { - m_windows[denseIdx] = handle; - m_frameBuffers[handle.idx].m_denseIdx = denseIdx; - } + m_windows[denseIdx] = handle; + m_frameBuffers[handle.idx].m_denseIdx = denseIdx; } + + m_windows[m_numWindows] = {kInvalidHandle}; } } @@ -5139,6 +5136,8 @@ static_assert(BX_COUNTOF(s_accessNames) == Access::Count, "Invalid s_accessNames murmur.add(m_metalLayer->pixelFormat() ); murmur.add(formatColor); murmur.add(formatDepthStencil); + murmur.add(NULL != m_backBufferDepth ? m_backBufferDepth->pixelFormat() : MTL::PixelFormatInvalid); + murmur.add(NULL != m_backBufferStencil ? m_backBufferStencil->pixelFormat() : MTL::PixelFormatInvalid); murmur.add(sampleCount); return murmur.end(); @@ -6015,17 +6014,18 @@ static_assert(BX_COUNTOF(s_accessNames) == Access::Count, "Invalid s_accessNames continue; } - const uint16_t fbhIdx = 0 == ii ? kInvalidHandle : m_windows[ii].idx; + const bool isMainWindow = !isValid(m_windows[ii]); + const uint16_t fbhIdx = isMainWindow ? kInvalidHandle : m_windows[ii].idx; - bool needScreenshot = 0 == ii && NULL != m_capture; + bool needScreenshot = isMainWindow && NULL != m_capture; for (uint8_t jj = 0, numShots = _render->m_numScreenShots; jj < numShots && !needScreenshot; ++jj) { needScreenshot = _render->m_screenShot[jj].handle.idx == fbhIdx; } - const uint32_t width = 0 == ii ? m_mainSwapChain.width : frameBuffer.m_width; - const uint32_t height = 0 == ii ? m_mainSwapChain.height : frameBuffer.m_height; + const uint32_t width = isMainWindow ? m_mainSwapChain.width : frameBuffer.m_width; + const uint32_t height = isMainWindow ? m_mainSwapChain.height : frameBuffer.m_height; if (needScreenshot) { diff --git a/src/renderer_vk.cpp b/src/renderer_vk.cpp index a5f2d9950..f882cdb18 100644 --- a/src/renderer_vk.cpp +++ b/src/renderer_vk.cpp @@ -2911,16 +2911,14 @@ VK_IMPORT_DEVICE if (UINT16_MAX != denseIdx) { --m_numWindows; - if (m_numWindows > 1) + if (m_numWindows != denseIdx) { FrameBufferHandle handle = m_windows[m_numWindows]; - m_windows[m_numWindows] = {kInvalidHandle}; - if (m_numWindows != denseIdx) - { - m_windows[denseIdx] = handle; - m_frameBuffers[handle.idx].m_denseIdx = denseIdx; - } + m_windows[denseIdx] = handle; + m_frameBuffers[handle.idx].m_denseIdx = denseIdx; } + + m_windows[m_numWindows] = {kInvalidHandle}; } } diff --git a/src/renderer_webgpu.cpp b/src/renderer_webgpu.cpp index 9643b3ca7..900defdba 100644 --- a/src/renderer_webgpu.cpp +++ b/src/renderer_webgpu.cpp @@ -2176,16 +2176,14 @@ WGPU_IMPORT if (UINT16_MAX != denseIdx) { --m_numWindows; - if (m_numWindows > 1) + if (m_numWindows != denseIdx) { FrameBufferHandle handle = m_windows[m_numWindows]; - m_windows[m_numWindows] = {kInvalidHandle}; - if (m_numWindows != denseIdx) - { - m_windows[denseIdx] = handle; - m_frameBuffers[handle.idx].m_denseIdx = denseIdx; - } + m_windows[denseIdx] = handle; + m_frameBuffers[handle.idx].m_denseIdx = denseIdx; } + + m_windows[m_numWindows] = {kInvalidHandle}; } } @@ -3442,10 +3440,7 @@ WGPU_IMPORT : NULL ; - const TextureFormat::Enum formatDepthStencil = fb.isSwapChain() - ? fb.m_swapChain.m_desc.formatDepthStencil - : TextureFormat::Enum(fb.m_formatDepthStencil) - ; + const TextureFormat::Enum formatDepthStencil = TextureFormat::Enum(fb.m_formatDepthStencil); const bool hasFragmentShader = NULL != program.m_fsh; const bool bgra8Storage = hasBgra8Storage(entries, entryCount); @@ -3822,7 +3817,7 @@ WGPU_IMPORT { _murmur.add(_fb.m_swapChain.m_viewFormat); _murmur.add(_fb.m_swapChain.m_desc.formatColor); - _murmur.add(_fb.m_swapChain.m_desc.formatDepthStencil); + _murmur.add(_fb.m_swapChain.m_formatDepthStencil); return; } @@ -5339,6 +5334,8 @@ WGPU_IMPORT wgpuRelease(m_depthStencilView); wgpuRelease(m_msaaTextureView); + m_formatDepthStencil = uint8_t(TextureFormat::Count); + if (isValid(m_desc.depth) ) { const TextureWGPU& texture = s_renderWGPU->m_textures[m_desc.depth.idx]; diff --git a/src/renderer_webgpu.h b/src/renderer_webgpu.h index bd88a8c04..bf370afa1 100644 --- a/src/renderer_webgpu.h +++ b/src/renderer_webgpu.h @@ -679,6 +679,7 @@ namespace wgpu { , m_msaaTextureView(NULL) , m_depthStencilView(NULL) , m_viewFormat(WGPUTextureFormat_Undefined) + , m_formatDepthStencil(uint8_t(TextureFormat::Count) ) , m_readable(false) { }