From 491d39503e75c093069e4b99323e813303dc824b Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 5 Oct 2026 13:17:47 +0000 Subject: [PATCH 01/11] Wake every pipeline waiter when shader work is queued or promoted Compiler workers and renderer waiters share g_pipelineCv. notify_one could wake a renderer thread instead of a compiler, leaving a queued pipeline until some later wake-up; with skip_unready_pipelines its draws stay missing that much longer. Promoting a background build to priority now wakes the workers too. From upstream patchzyy/Wiicompiled 6f14bde (#244, KartPad 956d811e). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01Wg7mB8ogCWmp9GH19Uc82B --- aurora-main/lib/gfx/pipeline_cache.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/aurora-main/lib/gfx/pipeline_cache.cpp b/aurora-main/lib/gfx/pipeline_cache.cpp index b854e9d8c..d24757dc7 100644 --- a/aurora-main/lib/gfx/pipeline_cache.cpp +++ b/aurora-main/lib/gfx/pipeline_cache.cpp @@ -426,6 +426,7 @@ static PendingPipeline* touch_pending_pipeline(PipelineRef hash, bool prioritize g_priorityPipelines.emplace_back(std::move(*backgroundIt)); g_backgroundPipelines.erase(backgroundIt); + g_pipelineCv.notify_all(); return &g_priorityPipelines.back(); } @@ -566,7 +567,8 @@ static PipelineRef find_pipeline_impl(ShaderType type, const PipelineConfig& con } if (notifyWorker) { - g_pipelineCv.notify_one(); + // Compiler workers and renderer waiters share this condition variable. + g_pipelineCv.notify_all(); } if (notifyWaiters) { g_pipelineCv.notify_all(); From a0f97d307d8bc8ff8b553b0afe2b486249f799de Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 5 Oct 2026 13:17:47 +0000 Subject: [PATCH 02/11] Read packed three-byte vertex attributes without touching the next word load_u24 and raw_fetch_u8_3 read a whole 32-bit word pair even when the three bytes sit in the first word, so an attribute at the end of its binding read past it. Release builds disable Dawn's robustness, so that read isn't clamped. From upstream patchzyy/Wiicompiled 6f14bde (#244, KartPad 0f6b274). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01Wg7mB8ogCWmp9GH19Uc82B --- aurora-main/lib/gx/shader.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/aurora-main/lib/gx/shader.cpp b/aurora-main/lib/gx/shader.cpp index 4b07fb277..4467a2334 100644 --- a/aurora-main/lib/gx/shader.cpp +++ b/aurora-main/lib/gx/shader.cpp @@ -1739,8 +1739,22 @@ fn load_u16(p: ptr>, byte_off: u32, le: bool) -> u32 {{ return bswap16(raw, le); }} +fn load_u24_raw(p: ptr>, byte_off: u32) -> u32 {{ + let word_idx = byte_off >> 2u; + let sub = byte_off & 3u; + let word = p[word_idx]; + // Three bytes at offsets zero or one fit entirely in this word. Do not + // access the next word: this attribute may end at the binding boundary. + if (sub <= 1u) {{ + return (word >> (sub * 8u)) & 0x00FFFFFFu; + }} + let next = p[word_idx + 1u]; + let shift = sub * 8u; + return ((word >> shift) | (next << (32u - shift))) & 0x00FFFFFFu; +}} + fn load_u24(p: ptr>, byte_off: u32, le: bool) -> u32 {{ - let raw = load_u32_raw(p, byte_off) & 0x00FFFFFFu; + let raw = load_u24_raw(p, byte_off); if (le) {{ return raw; }} @@ -1780,7 +1794,7 @@ fn raw_fetch_u8_2(p: ptr>, byte_off: u32) -> vec2u {{ }} fn raw_fetch_u8_3(p: ptr>, byte_off: u32) -> vec3u {{ - let raw = load_u32_raw(p, byte_off); + let raw = load_u24_raw(p, byte_off); return vec3u( extractBits(raw, 0u, 8u), extractBits(raw, 8u, 8u), From 5bf1904ca81ad871fef7b03a3b97d939c0ad4d73 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 5 Oct 2026 13:17:48 +0000 Subject: [PATCH 03/11] Lock the video render mode against concurrent readers VIConfigure writes the mode on the game side while the GX thread, the frame worker and the window code read its size. The resize request is made after the lock is released. From upstream patchzyy/Wiicompiled 6f14bde (#244). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01Wg7mB8ogCWmp9GH19Uc82B --- aurora-main/lib/dolphin/vi/vi.cpp | 39 +++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/aurora-main/lib/dolphin/vi/vi.cpp b/aurora-main/lib/dolphin/vi/vi.cpp index d036b8650..37f49d637 100644 --- a/aurora-main/lib/dolphin/vi/vi.cpp +++ b/aurora-main/lib/dolphin/vi/vi.cpp @@ -7,11 +7,13 @@ #include #include #include +#include namespace aurora::vi { std::optional g_renderMode; namespace { std::atomic g_presentAspectCorrection{1.f}; +std::mutex g_renderModeMutex; float calculate_present_aspect_correction(const GXRenderModeObj& rm) noexcept { if (rm.viWidth == 0 || rm.viHeight == 0) { @@ -29,9 +31,8 @@ float calculate_present_aspect_correction(const GXRenderModeObj& rm) noexcept { const float verticalFill = static_cast(rm.viHeight) / nominalActiveHeight; return horizontalFill / verticalFill; } -} // namespace -Vec2 render_mode_size() noexcept { +Vec2 render_mode_size_locked() noexcept { if (!g_renderMode) { return {640, 528}; } @@ -40,18 +41,31 @@ Vec2 render_mode_size() noexcept { return {std::max(g_renderMode->fbWidth, 640), std::max(g_renderMode->efbHeight, 528)}; } +} // namespace + +Vec2 render_mode_size() noexcept { + std::lock_guard lock(g_renderModeMutex); + return render_mode_size_locked(); +} + void configure(const GXRenderModeObj* rm) noexcept { - const auto oldSize = render_mode_size(); - if (rm == nullptr) { - g_renderMode.reset(); - } else { - g_renderMode = *rm; - g_presentAspectCorrection.store(calculate_present_aspect_correction(*rm), std::memory_order_release); - } - if (rm == nullptr) { - g_presentAspectCorrection.store(1.f, std::memory_order_release); + bool sizeChanged = false; + { + std::lock_guard lock(g_renderModeMutex); + const auto oldSize = render_mode_size_locked(); + if (rm == nullptr) { + g_renderMode.reset(); + } else { + g_renderMode = *rm; + g_presentAspectCorrection.store(calculate_present_aspect_correction(*rm), std::memory_order_release); + } + if (rm == nullptr) { + g_presentAspectCorrection.store(1.f, std::memory_order_release); + } + sizeChanged = render_mode_size_locked() != oldSize; } - if (render_mode_size() != oldSize) { + // Never hold the mode lock across a resize request or a renderer callback. + if (sizeChanged) { window::request_frame_buffer_resize(); } } @@ -61,6 +75,7 @@ Vec2 configured_fb_size() noexcept { } Vec2 visible_fb_size() noexcept { + std::lock_guard lock(g_renderModeMutex); if (!g_renderMode) { return {640, 528}; } From c12b6b7d996277f7fea4691c05b1f0d42897b34b Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 5 Oct 2026 13:17:48 +0000 Subject: [PATCH 04/11] Treat pipeline cache rows with inconsistent sizes as misses Dawn asks for a blob's size before allocating and copying it. A row whose stored length, declared size or zstd frame size disagree, or whose compression flag is unknown, made it allocate the declared size and copy or decompress past the stored blob. Such rows are now logged and skipped. From upstream patchzyy/Wiicompiled 6f14bde (#244, KartPad 70951022). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01Wg7mB8ogCWmp9GH19Uc82B --- aurora-main/lib/webgpu/gpu_cache.cpp | 30 ++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/aurora-main/lib/webgpu/gpu_cache.cpp b/aurora-main/lib/webgpu/gpu_cache.cpp index 35e9642e7..e494bc757 100644 --- a/aurora-main/lib/webgpu/gpu_cache.cpp +++ b/aurora-main/lib/webgpu/gpu_cache.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -313,8 +314,33 @@ static size_t load_from_database(const XXH128_hash_t& keyHash, void* value, size if (ret == SQLITE_ROW) { // Hit const auto foundPtr = sqlite3_column_blob(load_stmt, 0); - foundSize = sqlite3_column_int64(load_stmt, 1); - const bool compressed = sqlite3_column_int(load_stmt, 2) != 0; + const auto declaredSize = sqlite3_column_int64(load_stmt, 1); + const auto storedSize = sqlite3_column_bytes(load_stmt, 0); + const auto compression = sqlite3_column_int(load_stmt, 2); + const bool compressed = compression == 1; + // Dawn asks for the size before allocating its destination. Validate here, + // not only during the copy: corrupt metadata must become a cache miss. + bool valid = declaredSize > 0 && + static_cast(declaredSize) <= std::numeric_limits::max() && + foundPtr != nullptr && storedSize > 0 && (compression == 0 || compression == 1); + if (valid && compressed) { +#if defined(AURORA_CACHE_USE_ZSTD) + // Our writer uses ZSTD_compress, which records the original content size. + const auto frameSize = ZSTD_getFrameContentSize(foundPtr, static_cast(storedSize)); + valid = frameSize != ZSTD_CONTENTSIZE_ERROR && frameSize != ZSTD_CONTENTSIZE_UNKNOWN && + frameSize == static_cast(declaredSize); +#else + valid = false; +#endif + } else if (valid) { + valid = declaredSize == storedSize; + } + if (!valid) { + Log.error("Ignoring cache entry with inconsistent size or compression metadata"); + check(sqlite3_reset(load_stmt)); + return 0; + } + foundSize = static_cast(declaredSize); if (value == nullptr) { g_hits.fetch_add(1, std::memory_order_relaxed); } else { From b361ecd54c95fdbfb486daeb6ab7caa2816cf731 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 5 Oct 2026 13:29:17 +0000 Subject: [PATCH 05/11] Stop draws from merging or reading past their own vertices Incomplete primitives no longer emit indices: quads drop a one- or two-vertex tail and draw a three-vertex tail as a triangle, triangle lists ignore leftover vertices, and fans or strips under three vertices emit nothing. Draws with no complete primitive are skipped. Previously an incomplete quad indexed vertices that belonged to the next merged draw, or past the end of the buffer. Merging now stops before the 16-bit index offset would wrap, never folds triangles into a single-instance line or point draw, and breaks after GXInvalidateVtxCache or a vertex-format switch, so the next draw uploads fresh arrays and uses its own format's shader. From upstream patchzyy/Wiicompiled 6f14bde (#244, KartPad batch). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01Wg7mB8ogCWmp9GH19Uc82B --- aurora-main/lib/gx/command_processor.cpp | 52 ++++++++--- aurora-main/lib/gx/pipeline.hpp | 1 + aurora-main/tests/CMakeLists.txt | 1 + aurora-main/tests/gx_fifo_test.cpp | 3 +- .../tests/renderer_regression_test.cpp | 86 +++++++++++++++++++ 5 files changed, 130 insertions(+), 13 deletions(-) create mode 100644 aurora-main/tests/renderer_regression_test.cpp diff --git a/aurora-main/lib/gx/command_processor.cpp b/aurora-main/lib/gx/command_processor.cpp index 5b5213956..328cb5c8c 100644 --- a/aurora-main/lib/gx/command_processor.cpp +++ b/aurora-main/lib/gx/command_processor.cpp @@ -33,10 +33,11 @@ using IndexBuffer = std::vector; static u32 prepare_idx_template(IndexBuffer& buf, GXPrimitive prim, u16 vtxCount) { size_t writePos = 0; if (prim == GX_QUADS) { - // Retain the existing incomplete-quad behavior: every started group emits a complete six-index quad. - buf.resize(((static_cast(vtxCount) + 3u) / 4u) * 6u); + // GX renders a three-vertex remainder as a triangle. One/two are ignored. + const u32 completeVertices = static_cast(vtxCount) & ~3u; + buf.resize((completeVertices / 4u) * 6u + (vtxCount % 4u == 3u ? 3u : 0u)); - for (u16 v = 0; v < vtxCount; v += 4) { + for (u32 v = 0; v < completeVertices; v += 4) { const u16 idx0 = v; const u16 idx1 = static_cast(v + 1); const u16 idx2 = static_cast(v + 2); @@ -48,15 +49,21 @@ static u32 prepare_idx_template(IndexBuffer& buf, GXPrimitive prim, u16 vtxCount buf[writePos++] = idx3; buf[writePos++] = idx0; } + if (vtxCount % 4u == 3u) { + buf[writePos++] = static_cast(completeVertices); + buf[writePos++] = static_cast(completeVertices + 1u); + buf[writePos++] = static_cast(completeVertices + 2u); + } } else if (prim == GX_TRIANGLES) { - buf.resize(vtxCount); - for (u16 v = 0; v < vtxCount; ++v) { + const u32 completeVertices = (static_cast(vtxCount) / 3u) * 3u; + buf.resize(completeVertices); + for (u32 v = 0; v < completeVertices; ++v) { buf[writePos++] = v; } } else if (prim == GX_TRIANGLEFAN) { - const u32 indexCount = vtxCount <= 3 ? vtxCount : 3u + (static_cast(vtxCount) - 3u) * 3u; + const u32 indexCount = vtxCount < 3 ? 0u : (static_cast(vtxCount) - 2u) * 3u; buf.resize(indexCount); - for (u16 v = 0; v < vtxCount; ++v) { + for (u32 v = 0; indexCount != 0 && v < vtxCount; ++v) { if (v < 3) { buf[writePos++] = v; continue; @@ -66,9 +73,9 @@ static u32 prepare_idx_template(IndexBuffer& buf, GXPrimitive prim, u16 vtxCount buf[writePos++] = v; } } else if (prim == GX_TRIANGLESTRIP) { - const u32 indexCount = vtxCount <= 3 ? vtxCount : 3u + (static_cast(vtxCount) - 3u) * 3u; + const u32 indexCount = vtxCount < 3 ? 0u : (static_cast(vtxCount) - 2u) * 3u; buf.resize(indexCount); - for (u16 v = 0; v < vtxCount; ++v) { + for (u32 v = 0; indexCount != 0 && v < vtxCount; ++v) { if (v < 3) { buf[writePos++] = v; continue; @@ -91,6 +98,13 @@ static u32 prepare_idx_template(IndexBuffer& buf, GXPrimitive prim, u16 vtxCount return static_cast(writePos); } +// Empty/incomplete draws consume FIFO bytes but cannot produce a primitive. +static bool has_complete_primitive(GXPrimitive prim, u16 count) { + if (prim == GX_POINTS) return count >= 1; + if (prim == GX_LINES || prim == GX_LINESTRIP) return count >= 2; + return count >= 3; +} + // GX FIFO opcodes - use CP_ prefix to avoid clashing with GXCommandList.h macros static constexpr u8 CP_CMD_NOP = GX_NOP; static constexpr u8 CP_CMD_LOAD_CP_REG = GX_LOAD_CP_REG; @@ -552,6 +566,10 @@ void process(const u8* data, u32 size, bool bigEndian) { for (int i = GX_VA_POS; i <= GX_VA_TEX7; ++i) { g_gxState.arrays[i].cachedRange = {}; } + // A merged draw retains its previous array uploads. Force a new draw so + // handle_draw_unmerged observes the invalidation and uploads fresh data. + // Pipeline configuration itself did not change. + g_gxState.stateDirty = true; break; } @@ -1926,6 +1944,10 @@ static u32 calculate_last_vtx_size(GXVtxFmt fmt) { g_gxState.lastVtxFmt = fmt; g_gxState.lastVtxSize = vtxSize; + // The format is selected by the draw opcode, without a register write. + // Even equal-stride formats may decode bytes differently, so do not merge + // into a draw using the previous format's shader and uniform layout. + g_gxState.stateDirty = true; return vtxSize; } @@ -2366,6 +2388,8 @@ bool submit_raw_draw(GXPrimitive prim, GXVtxFmt fmt, const uint8_t* vertices, ui return false; } + if (!has_complete_primitive(prim, vtxCount)) return true; + // This entry point bypasses process(), so it owns the renderer lock itself. std::lock_guard gpuLock(aurora::renderer_gpu_mutex()); if (model_array_hidden()) return true; @@ -2414,7 +2438,7 @@ static bool handle_draw(u8 cmd, const u8* data, u32& pos, u32 size, bool bigEndi return false; } - if (model_array_hidden()) { + if (!has_complete_primitive(prim, vtxCount) || model_array_hidden()) { pos += totalVtxBytes; return true; } @@ -2436,9 +2460,12 @@ static bool handle_draw(u8 cmd, const u8* data, u32& pos, u32 size, bool bigEndi // Only if the previous draw call was a single instance draw (no lines/points handling), and only into a draw // that resolved the same animated array: the merged whole renders through that draw's binding. Anything the // decision cache cannot vouch for (a command it was not recorded against) stays unmerged. + // Expanded lines/points have different vertex interpretation even with one instance. + // Triangle-list output has no restart index; index 65535 is usable. + // Overflow would address earlier vertices instead of the appended geometry. if (lastDraw != nullptr && prim != GX_LINES && prim != GX_LINESTRIP && prim != GX_POINTS && - !lastDraw->uniformReplayLayout.vertexMotion.enabled && - lastDraw->instanceCount == 1 && + !lastDraw->uniformReplayLayout.vertexMotion.enabled && !lastDraw->expandedPrimitive && + lastDraw->instanceCount == 1 && uint64_t(lastDraw->vtxCount) + vtxCount <= 65536u && (nativeWheelArrays.empty() || (nativeWheelLastDrawCommand == lastDraw && nativeWheelLastDecision == nativeWheel))) LIKELY { @@ -2615,6 +2642,7 @@ static void handle_draw_unmerged(GXPrimitive prim, GXVtxFmt fmt, u16 vtxCount, g .vtxCount = vtxCount, .indexCount = numIndices, .instanceCount = instanceCount, + .expandedPrimitive = prim == GX_LINES || prim == GX_LINESTRIP || prim == GX_POINTS, .bindGroups = bindGroups, .dstAlpha = pipelineState.dstAlpha, .screenRect = screen_rect(prim, fmt, vertices, vtxCount, vtxStride), diff --git a/aurora-main/lib/gx/pipeline.hpp b/aurora-main/lib/gx/pipeline.hpp index d52fe92c2..4ae19e6e6 100644 --- a/aurora-main/lib/gx/pipeline.hpp +++ b/aurora-main/lib/gx/pipeline.hpp @@ -29,6 +29,7 @@ struct DrawData { uint32_t vtxCount; uint32_t indexCount; uint32_t instanceCount; + bool expandedPrimitive; GXBindGroups bindGroups; uint32_t dstAlpha; // Valid only for simple orthographic rectangles/lines (textured or not). diff --git a/aurora-main/tests/CMakeLists.txt b/aurora-main/tests/CMakeLists.txt index ab3467302..13d142810 100644 --- a/aurora-main/tests/CMakeLists.txt +++ b/aurora-main/tests/CMakeLists.txt @@ -64,6 +64,7 @@ if (AURORA_ENABLE_GX) add_executable(gx_fifo_tests gx_fifo_test.cpp gx_test_stubs.cpp + renderer_regression_test.cpp stereo_replay_test.cpp stereo_interpolation_test.cpp stereo_mirror_test.cpp diff --git a/aurora-main/tests/gx_fifo_test.cpp b/aurora-main/tests/gx_fifo_test.cpp index 8eb5a083c..64335516e 100644 --- a/aurora-main/tests/gx_fifo_test.cpp +++ b/aurora-main/tests/gx_fifo_test.cpp @@ -2861,6 +2861,7 @@ TEST_F(GXFifoTest, DrawTopologyTemplatesPreserveExactGxIndexOrder) { const auto decodeAndReadIndices = [&](GXPrimitive primitive, u16 count) { std::vector fifo; append_test_draw(fifo, primitive, count); + aurora::gfx::testing::reset_vertex_push_record(); decode_fifo(fifo); return aurora::gfx::testing::last_pushed_indices(); }; @@ -2871,7 +2872,7 @@ TEST_F(GXFifoTest, DrawTopologyTemplatesPreserveExactGxIndexOrder) { g_gxState.stateDirty = true; EXPECT_EQ(decodeAndReadIndices(GX_TRIANGLEFAN, 5), (std::vector{0, 1, 2, 0, 2, 3, 0, 3, 4})); g_gxState.stateDirty = true; - EXPECT_EQ(decodeAndReadIndices(GX_TRIANGLEFAN, 2), (std::vector{0, 1})); + EXPECT_EQ(decodeAndReadIndices(GX_TRIANGLEFAN, 2), (std::vector{})); g_gxState.stateDirty = true; EXPECT_EQ(decodeAndReadIndices(GX_TRIANGLESTRIP, 6), (std::vector{0, 1, 2, 2, 1, 3, 2, 3, 4, 4, 3, 5})); g_gxState.stateDirty = true; diff --git a/aurora-main/tests/renderer_regression_test.cpp b/aurora-main/tests/renderer_regression_test.cpp new file mode 100644 index 000000000..0a845b5fb --- /dev/null +++ b/aurora-main/tests/renderer_regression_test.cpp @@ -0,0 +1,86 @@ +#include "gx_test_common.hpp" +#include "gx/pipeline.hpp" + +using aurora::gx::g_gxState; + +namespace { +std::vector draw(GXPrimitive primitive, u16 count, GXVtxFmt format = GX_VTXFMT0) { + std::vector bytes{static_cast(primitive | format), static_cast(count >> 8), static_cast(count)}; + bytes.resize(3 + count); + return bytes; +} +} // namespace + +TEST_F(GXFifoTest, MaximumQuadCountTerminatesWithoutOutOfRangeIndices) { + g_gxState.lastVtxFmt = GX_VTXFMT0; + g_gxState.lastVtxSize = 1; + for (const u16 count : {65532, 65533, 65534, 65535}) { + g_gxState.stateDirty = true; + decode_fifo(draw(GX_QUADS, count)); + const auto& indices = aurora::gfx::testing::last_pushed_indices(); + ASSERT_EQ(indices.size(), (count / 4) * 6 + (count % 4 == 3 ? 3 : 0)); + for (const auto index : indices) + ASSERT_LT(index, count); + } +} + +TEST_F(GXFifoTest, IncompletePrimitivesNeverJoinAcrossDraws) { + g_gxState.lastVtxFmt = GX_VTXFMT0; + g_gxState.lastVtxSize = 1; + aurora::gfx::testing::use_draw_command_tracking(true); + decode_fifo(draw(GX_TRIANGLES, 4)); + EXPECT_EQ(aurora::gfx::testing::last_pushed_indices(), (std::vector{0, 1, 2})); + decode_fifo(draw(GX_TRIANGLES, 5)); + EXPECT_EQ(aurora::gfx::testing::last_pushed_indices(), (std::vector{4, 5, 6})); + const auto before = aurora::gfx::testing::last_pushed_indices(); + decode_fifo(draw(GX_TRIANGLEFAN, 2)); + EXPECT_EQ(aurora::gfx::testing::last_pushed_indices(), before); +} + +TEST_F(GXFifoTest, MergeStopsBeforeSixteenBitIndexOverflow) { + g_gxState.lastVtxFmt = GX_VTXFMT0; + g_gxState.lastVtxSize = 1; + aurora::gfx::testing::use_draw_command_tracking(true); + decode_fifo(draw(GX_TRIANGLES, 65535)); + decode_fifo(draw(GX_TRIANGLES, 3)); + EXPECT_EQ(aurora::gfx::g_mergedDrawCallCount, 0u); + EXPECT_EQ(aurora::gfx::testing::last_pushed_indices(), (std::vector{0, 1, 2})); +} + +TEST_F(GXFifoTest, VertexCacheInvalidationBreaksDrawMerging) { + g_gxState.lastVtxFmt = GX_VTXFMT0; + g_gxState.lastVtxSize = 1; + aurora::gfx::testing::use_draw_command_tracking(true); + decode_fifo(draw(GX_TRIANGLES, 3)); + decode_fifo({GX_CMD_INVL_VC}); + EXPECT_TRUE(g_gxState.stateDirty); + decode_fifo(draw(GX_TRIANGLES, 3)); + EXPECT_EQ(aurora::gfx::g_mergedDrawCallCount, 0u); +} + +TEST_F(GXFifoTest, EqualStrideVertexFormatChangeBreaksDrawMerging) { + aurora::gfx::testing::use_real_vertex_format_helpers(true); + g_gxState.vtxDesc[GX_VA_POS] = GX_DIRECT; + for (const auto format : {GX_VTXFMT0, GX_VTXFMT1}) { + g_gxState.vtxFmts[format].attrs[GX_VA_POS].cnt = GX_POS_XY; + g_gxState.vtxFmts[format].attrs[GX_VA_POS].type = GX_U8; + } + g_gxState.vtxFmts[GX_VTXFMT1].attrs[GX_VA_POS].frac = 1; + aurora::gfx::testing::use_draw_command_tracking(true); + for (const auto format : {GX_VTXFMT0, GX_VTXFMT1}) { + auto bytes = draw(GX_TRIANGLES, 3, format); + bytes.resize(9); + decode_fifo(bytes); + } + EXPECT_EQ(aurora::gfx::g_mergedDrawCallCount, 0u); +} + +TEST_F(GXFifoTest, SingleExpandedPrimitiveCannotMergeWithTriangles) { + g_gxState.lastVtxFmt = GX_VTXFMT0; + g_gxState.lastVtxSize = 1; + aurora::gfx::testing::use_draw_command_tracking(true); + decode_fifo(draw(GX_POINTS, 1)); + decode_fifo(draw(GX_TRIANGLES, 3)); + EXPECT_EQ(aurora::gfx::g_mergedDrawCallCount, 0u); + EXPECT_EQ(aurora::gfx::testing::last_pushed_indices(), (std::vector{0, 1, 2})); +} From 20ef08b3b847627156d25ab2f6bb26cceee32118 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 5 Oct 2026 13:29:26 +0000 Subject: [PATCH 06/11] Decode the first GEN_MODE write even when it is all zeros The BP register cache skips writes equal to the cached value, and the cache starts at zero. A cold GEN_MODE write of zero (one TEV stage, no culling, no channels or texgens) was therefore dropped, leaving the decoded state at zero TEV stages until some other GEN_MODE value arrived. Seed the cache's register ID byte so the first write always decodes. From upstream patchzyy/Wiicompiled 6f14bde (#244, KartPad batch). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01Wg7mB8ogCWmp9GH19Uc82B --- aurora-main/lib/gx/gx.hpp | 2 ++ aurora-main/tests/gx_fifo_test.cpp | 45 ++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/aurora-main/lib/gx/gx.hpp b/aurora-main/lib/gx/gx.hpp index 778c23e64..84b45f02c 100644 --- a/aurora-main/lib/gx/gx.hpp +++ b/aurora-main/lib/gx/gx.hpp @@ -439,6 +439,8 @@ struct GXState { u32 pipelineStateGeneration = next_gx_state_epoch(); std::array bpRegCache = [] { std::array regs{}; + // Force the first GEN_MODE decode without changing its masked reset value. + regs[0x00] = 0xFF000000; regs[0xFE] = 0x00FFFFFF; return regs; }(); diff --git a/aurora-main/tests/gx_fifo_test.cpp b/aurora-main/tests/gx_fifo_test.cpp index 64335516e..80a458dea 100644 --- a/aurora-main/tests/gx_fifo_test.cpp +++ b/aurora-main/tests/gx_fifo_test.cpp @@ -1390,6 +1390,51 @@ TEST_F(GXFifoTest, BlendMode_Logic) { EXPECT_EQ(g_gxState.blendOp, GX_LO_XOR); } +TEST_F(GXFifoTest, GenMode_FirstZeroWriteDecodesAndRepeatDeduplicates) { + reset_gx_state(); + const auto before = g_gxState.pipelineStateGeneration; + decode_fifo(bp_cmd(0, 0)); + EXPECT_EQ(g_gxState.numTevStages, 1u); + EXPECT_EQ(g_gxState.cullMode, GX_CULL_NONE); + EXPECT_EQ(g_gxState.numChans, 0u); + EXPECT_EQ(g_gxState.numTexGens, 0u); + EXPECT_EQ(g_gxState.numIndStages, 0u); + EXPECT_EQ(g_gxState.bpRegCache[0], 0u); + EXPECT_NE(g_gxState.pipelineStateGeneration, before); + const auto decoded = g_gxState.pipelineStateGeneration; + decode_fifo(bp_cmd(0, 0)); + EXPECT_EQ(g_gxState.pipelineStateGeneration, decoded); +} + +TEST_F(GXFifoTest, GenMode_FirstMaskedWritePreservesZeroResetBits) { + for (const u32 mask : {0u, 1u << 10}) { + reset_gx_state(); + const auto before = g_gxState.pipelineStateGeneration; + decode_fifo(bp_cmd(0xFE, mask)); + decode_fifo(bp_cmd(0, 0xFFFFFF)); + EXPECT_EQ(g_gxState.bpRegCache[0], mask); + EXPECT_EQ(g_gxState.bpRegCache[0xFE], 0xFFFFFFu); + EXPECT_EQ(g_gxState.numTevStages, mask ? 2u : 1u); + EXPECT_EQ(g_gxState.cullMode, GX_CULL_NONE); + EXPECT_NE(g_gxState.pipelineStateGeneration, before); + decode_fifo(bp_cmd(0, 0)); + EXPECT_EQ(g_gxState.numTevStages, 1u); + EXPECT_EQ(g_gxState.bpRegCache[0], 0u); + } +} + +TEST_F(GXFifoTest, GenMode_ColdSingleStageApiSetupDecodes) { + reset_gx_state(); + GXSetNumTevStages(1); + GXSetNumTexGens(0); + GXSetNumChans(0); + GXSetCullMode(GX_CULL_NONE); + const auto bytes = flush_and_capture(); + decode_fifo(bytes); + EXPECT_EQ(g_gxState.numTevStages, 1u); + EXPECT_EQ(g_gxState.cullMode, GX_CULL_NONE); +} + TEST_F(GXFifoTest, BpMask_AppliesOnlyToNextWrite) { std::vector bytes; auto mask = bp_cmd(0xFE, 1u << 19); From 4407013d6172c7429436557ea71bc487d98a602c Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 5 Oct 2026 13:29:26 +0000 Subject: [PATCH 07/11] Check TEV register packing by content instead of uniform size The test expected one extra TEV register to grow the uniform by exactly 16 bytes, but the allocation is alignment-rounded, so it failed on the fork's baseline. Build the uniform and check that the register and the K color after it are packed next to each other instead. From upstream patchzyy/Wiicompiled 6f14bde (#244, KartPad batch). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01Wg7mB8ogCWmp9GH19Uc82B --- aurora-main/tests/gx_fifo_test.cpp | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/aurora-main/tests/gx_fifo_test.cpp b/aurora-main/tests/gx_fifo_test.cpp index 80a458dea..f2a1c0236 100644 --- a/aurora-main/tests/gx_fifo_test.cpp +++ b/aurora-main/tests/gx_fifo_test.cpp @@ -1328,12 +1328,35 @@ TEST(TevRegisterLivenessContract, PacksOneUniformWhenBothHalvesNeedInitialValue) auto config = baseline; config.tevStages[0].colorPass.a = GX_CC_C0; config.tevStages[0].alphaPass.a = GX_CA_A0; + config.tevStages[0].colorPass.b = GX_CC_KONST; + config.tevStages[0].kcSel = GX_TEV_KCSEL_K0; - const auto baselineInfo = aurora::gx::build_shader_info(baseline); const auto info = aurora::gx::build_shader_info(config); EXPECT_TRUE(info.loadsTevRegRgb.test(GX_TEVREG0)); EXPECT_TRUE(info.loadsTevRegAlpha.test(GX_TEVREG0)); - EXPECT_EQ(info.uniformSize, baselineInfo.uniformSize + sizeof(aurora::Vec4)); + // The final allocation is alignment-rounded, so adding one register need + // not increase it. Verify actual packing with a distinct following K color. + const auto savedReg = g_gxState.colorRegs[GX_TEVREG0]; + const auto savedKColor = g_gxState.kcolors[GX_KCOLOR0]; + g_gxState.colorRegs[GX_TEVREG0] = {11.f, 22.f, 33.f, 44.f}; + g_gxState.kcolors[GX_KCOLOR0] = {55.f, 66.f, 77.f, 88.f}; + EXPECT_TRUE(info.sampledKColors.test(GX_KCOLOR0)); + aurora::gfx::testing::reset_uniform_allocations(); + aurora::gx::build_uniform(info, 0, {}, {}, false); + const auto expectedReg = g_gxState.colorRegs[GX_TEVREG0]; + const auto expectedKColor = g_gxState.kcolors[GX_KCOLOR0]; + g_gxState.colorRegs[GX_TEVREG0] = savedReg; + g_gxState.kcolors[GX_KCOLOR0] = savedKColor; + const auto& bytes = aurora::gfx::testing::uniform_allocation(0); + const auto* reg = reinterpret_cast(&expectedReg); + const auto found = std::search(bytes.begin(), bytes.end(), reg, reg + sizeof(aurora::Vec4)); + ASSERT_NE(found, bytes.end()); + const size_t offset = static_cast(found - bytes.begin()); + ASSERT_LE(offset + 2 * sizeof(aurora::Vec4), bytes.size()); + EXPECT_EQ(std::memcmp(bytes.data() + offset + sizeof(aurora::Vec4), &expectedKColor, + sizeof(aurora::Vec4)), + 0); + aurora::gfx::testing::reset_uniform_allocations(); } // BP registers (direct FIFO writes, no dirty state flush needed) From a23fd087eecc048054d892e0989f14d1c81b341f Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 5 Oct 2026 13:31:08 +0000 Subject: [PATCH 08/11] Fix native EFB readback blits on BGRA surfaces and their Y clamp The shared blit shader clamps the sampled Y to flags.z..w, but the native readback uniform set flags.w to 0, so every row sampled row 0. The blit pipeline was also built only for the surface format, while the readback draws into an RGBA8 texture; on a BGRA surface (the usual Linux/Vulkan choice) the pipeline and attachment formats disagree, which release builds no longer catch because Dawn validation is skipped. Build blit pipelines for RGBA8, BGRA8 and the surface format, and pick one by destination format. The Quest 1 simple blit path is unchanged. From upstream patchzyy/Wiicompiled 6f14bde (#244, KartPad batch). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01Wg7mB8ogCWmp9GH19Uc82B --- aurora-main/lib/gfx/efb_ram_copy.cpp | 3 ++- aurora-main/lib/gfx/tex_copy_conv.cpp | 26 ++++++++++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/aurora-main/lib/gfx/efb_ram_copy.cpp b/aurora-main/lib/gfx/efb_ram_copy.cpp index c4136f44d..99de180a2 100644 --- a/aurora-main/lib/gfx/efb_ram_copy.cpp +++ b/aurora-main/lib/gfx/efb_ram_copy.cpp @@ -109,8 +109,9 @@ void ensure_native_texture(PendingCopy& pending, TextureHandle* cache = nullptr) *cache = pending.nativeTexture; } } + // The shared blit shader clamps Y to flags.z/w; preserve the full source. const std::array nativeBlitUniform{ - 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 64.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 64.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, }; pending.nativeBlitUniform = push_uniform(nativeBlitUniform); } diff --git a/aurora-main/lib/gfx/tex_copy_conv.cpp b/aurora-main/lib/gfx/tex_copy_conv.cpp index 7c7d7cd27..a8719316f 100644 --- a/aurora-main/lib/gfx/tex_copy_conv.cpp +++ b/aurora-main/lib/gfx/tex_copy_conv.cpp @@ -422,7 +422,7 @@ static wgpu::BindGroupLayout g_depthBindGroupLayout; static wgpu::Sampler g_nearestSampler; static wgpu::Sampler g_linearSampler; static absl::flat_hash_map g_pipelines; -static wgpu::RenderPipeline g_blitPipeline; +static absl::flat_hash_map g_blitPipelines; static wgpu::RenderPipeline g_quest1BlitPipeline; static bool quest1_simple_blit() noexcept { @@ -549,9 +549,15 @@ void initialize() { }; g_depthBindGroupLayout = g_device.CreateBindGroupLayout(&depthBindGroupLayoutDescriptor); - g_blitPipeline = create_pipeline( - {GX_TF_RGBA8, FragPassthrough, webgpu::g_graphicsConfig.surfaceConfiguration.format, "TexCopyConv Blit"}, - ShaderPreamble, g_bindGroupLayout); + // Native RAM readback uses RGBA even when the EFB/surface uses BGRA. + // Build both variants here; frame workers only read the completed map. + for (const auto format : {wgpu::TextureFormat::RGBA8Unorm, wgpu::TextureFormat::BGRA8Unorm, + webgpu::g_graphicsConfig.surfaceConfiguration.format}) { + if (format != wgpu::TextureFormat::Undefined && !g_blitPipelines.contains(format)) { + g_blitPipelines[format] = create_pipeline({GX_TF_RGBA8, FragPassthrough, format, "TexCopyConv Blit"}, + ShaderPreamble, g_bindGroupLayout); + } + } g_quest1BlitPipeline = create_pipeline( {GX_TF_RGBA8, {}, webgpu::g_graphicsConfig.surfaceConfiguration.format, "Quest 1 Simple TexCopy Blit"}, SimpleBlitShader, g_bindGroupLayout); @@ -585,7 +591,7 @@ void initialize() { void shutdown() { g_pipelines.clear(); - g_blitPipeline = {}; + g_blitPipelines.clear(); g_quest1BlitPipeline = {}; g_bindGroupLayout = {}; g_depthBindGroupLayout = {}; @@ -678,7 +684,15 @@ void run(const wgpu::CommandEncoder& cmd, const ConvRequest& req) { } void blit(const wgpu::CommandEncoder& cmd, const ConvRequest& req) { - execute(cmd, req, quest1_simple_blit() ? g_quest1BlitPipeline : g_blitPipeline); + if (quest1_simple_blit()) { + execute(cmd, req, g_quest1BlitPipeline); + return; + } + const auto it = g_blitPipelines.find(req.dst->format); + if (it == g_blitPipelines.end()) { + Log.fatal("Unsupported blit destination format {}", static_cast(req.dst->format)); + } + execute(cmd, req, it->second); } } // namespace aurora::gfx::tex_copy_conv From c91c1b289c3fab8095f4db3e89e19be9b3b37dc8 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 5 Oct 2026 13:31:09 +0000 Subject: [PATCH 09/11] Allow copying out of the EFB copy source snapshot When a copy with clear snapshots its source, the plain-copy path reads the snapshot with CopyTextureToTexture, but the snapshot texture was created without CopySrc usage. From upstream patchzyy/Wiicompiled 6f14bde (#244, KartPad batch). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01Wg7mB8ogCWmp9GH19Uc82B --- aurora-main/lib/gfx/common.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aurora-main/lib/gfx/common.cpp b/aurora-main/lib/gfx/common.cpp index 9e003618a..e5e3961d6 100644 --- a/aurora-main/lib/gfx/common.cpp +++ b/aurora-main/lib/gfx/common.cpp @@ -405,7 +405,7 @@ static size_t g_recordingSnapshotSlot = 0; static TextureHandle new_resolve_source_snapshot(wgpu::Extent3D size, wgpu::TextureFormat format) noexcept { const wgpu::TextureDescriptor textureDescriptor{ .label = "GX Copy Source Snapshot", - .usage = wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::CopyDst, + .usage = wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::CopySrc | wgpu::TextureUsage::CopyDst, .dimension = wgpu::TextureDimension::e2D, .size = size, .format = format, From c8220c0c112cd0df85e19e8485732f4ff93a4f50 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 5 Oct 2026 13:33:43 +0000 Subject: [PATCH 10/11] Report graphics startup failures instead of aborting When no backend could create a window and a WebGPU device, aurora hit an ASSERT and the process aborted with only SDL's last error in the log. Keep the first adapter, device or window error, return it from aurora_initialize with a new initializationStatus, and have the runtime throw it so the existing fatal popup and fatal log record the reason. ImGui shutdown now skips backends that never initialized, so the shutdown that follows a failed start does not crash. From upstream patchzyy/Wiicompiled 6f14bde (#244, KartPad batch). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01Wg7mB8ogCWmp9GH19Uc82B --- aurora-main/include/aurora/aurora.h | 8 ++++++++ aurora-main/lib/aurora.cpp | 20 +++++++++++++++++++- aurora-main/lib/imgui.cpp | 24 ++++++++++++++++++------ aurora-main/lib/webgpu/gpu.cpp | 9 +++++++++ runtime/src/main.cpp | 4 ++++ 5 files changed, 58 insertions(+), 7 deletions(-) diff --git a/aurora-main/include/aurora/aurora.h b/aurora-main/include/aurora/aurora.h index ff9120fff..237bfbff0 100644 --- a/aurora-main/include/aurora/aurora.h +++ b/aurora-main/include/aurora/aurora.h @@ -295,12 +295,20 @@ typedef struct { bool xrHeadsetOnly; } AuroraConfig; +typedef enum { + AURORA_INITIALIZATION_SUCCESS = 0, + AURORA_INITIALIZATION_GRAPHICS_UNAVAILABLE = 1, +} AuroraInitializationStatus; + typedef struct { AuroraBackend backend; const char* userPath; const char* cachePath; SDL_Window* window; AuroraWindowSize windowSize; + AuroraInitializationStatus initializationStatus; + // On failure, owned by SDL on the calling thread. Copy before another SDL call. + const char* initializationError; } AuroraInfo; AuroraInfo aurora_initialize(int argc, char* argv[], const AuroraConfig* config); diff --git a/aurora-main/lib/aurora.cpp b/aurora-main/lib/aurora.cpp index 5a12c9f2e..52c5a34f1 100644 --- a/aurora-main/lib/aurora.cpp +++ b/aurora-main/lib/aurora.cpp @@ -1229,15 +1229,23 @@ AuroraInfo initialize(int argc, char* argv[], const AuroraConfig& config) noexce const AuroraBackend requestedBackend = config.desiredBackend; AuroraBackend selectedBackend = requestedBackend; bool windowCreated = false; + std::string firstGraphicsError; + const auto rememberGraphicsError = [&] { + if (firstGraphicsError.empty() && SDL_GetError()[0] != '\0') { + firstGraphicsError = SDL_GetError(); + } + }; if (selectedBackend != BACKEND_AUTO) { Log.info("Requested graphics backend: {}", backend_name(selectedBackend)); if (window::create_window(selectedBackend)) { if (webgpu::initialize(selectedBackend)) { windowCreated = true; } else { + rememberGraphicsError(); window::destroy_window(); } } else { + rememberGraphicsError(); Log.error("Failed to create a window for backend {}: {}", backend_name(selectedBackend), SDL_GetError()); } if (!windowCreated) { @@ -1254,18 +1262,28 @@ AuroraInfo initialize(int argc, char* argv[], const AuroraConfig& config) noexce for (const auto backendType : PreferredBackendOrder) { selectedBackend = backendType; if (!window::create_window(selectedBackend)) { + rememberGraphicsError(); continue; } if (webgpu::initialize(selectedBackend)) { windowCreated = true; break; } else { + rememberGraphicsError(); window::destroy_window(); } } } - ASSERT(windowCreated, "Error creating window: {}", SDL_GetError()); + if (!windowCreated) { + if (firstGraphicsError.empty()) firstGraphicsError = "No supported graphics backend is available"; + SDL_SetError("%s", firstGraphicsError.c_str()); + Log.error("Graphics initialization failed: {}", firstGraphicsError); + return { + .initializationStatus = AURORA_INITIALIZATION_GRAPHICS_UNAVAILABLE, + .initializationError = SDL_GetError(), + }; + } if (requestedBackend != BACKEND_AUTO && selectedBackend != requestedBackend) { Log.error( "Graphics backend fallback in effect: video.graphics_api requested {}, " diff --git a/aurora-main/lib/imgui.cpp b/aurora-main/lib/imgui.cpp index f3854d42b..0cdd07b8d 100644 --- a/aurora-main/lib/imgui.cpp +++ b/aurora-main/lib/imgui.cpp @@ -85,18 +85,30 @@ void initialize() noexcept { void shutdown() noexcept { ZoneScoped; - if (g_useSdlRenderer) { - ImGui_ImplSDLRenderer3_Shutdown(); - } else { - ImGui_ImplWGPU_Shutdown(); + // Startup can fail before either backend initializes. A context alone does + // not mean its renderer/platform backend owns resources to release. + if (ImGui::GetCurrentContext() != nullptr) { + ImGuiIO& io = ImGui::GetIO(); + if (io.BackendRendererUserData != nullptr) { + if (g_useSdlRenderer) { + ImGui_ImplSDLRenderer3_Shutdown(); + } else { + ImGui_ImplWGPU_Shutdown(); + } + } + if (io.BackendPlatformUserData != nullptr) { + ImGui_ImplSDL3_Shutdown(); + } + ImGui::DestroyContext(); } - ImGui_ImplSDL3_Shutdown(); - ImGui::DestroyContext(); for (const auto& texture : g_sdlTextures) { SDL_DestroyTexture(texture); } g_sdlTextures.clear(); g_wgpuTextures.clear(); + g_useSdlRenderer = false; + g_scale = 0.f; + g_frameDataBuilt = true; } void process_event(const SDL_Event& event) noexcept { diff --git a/aurora-main/lib/webgpu/gpu.cpp b/aurora-main/lib/webgpu/gpu.cpp index 0876bb675..6b015aa78 100644 --- a/aurora-main/lib/webgpu/gpu.cpp +++ b/aurora-main/lib/webgpu/gpu.cpp @@ -627,12 +627,16 @@ bool initialize(AuroraBackend auroraBackend) { g_adapter = std::move(adapter); } else { Log.warn("Adapter request failed: {}", message); + const std::string_view reason{message}; + SDL_SetError("Graphics adapter unavailable: %.*s", + static_cast(std::min(reason.size(), 512)), reason.data()); } }); const auto status = g_instance.WaitAny(future, 5000000000); if (status != wgpu::WaitStatus::Success) { Log.error("Failed to create {} adapter: {}", magic_enum::enum_name(backend), magic_enum::enum_name(status)); + SDL_SetError("Graphics adapter request did not complete within its startup deadline"); return false; } if (!g_adapter) { @@ -845,11 +849,16 @@ bool initialize(AuroraBackend auroraBackend) { g_device = std::move(device); } else { Log.warn("Device request failed: {}", message); + const std::string_view reason{message}; + SDL_SetError("Graphics device unavailable: %.*s", + static_cast(std::min(reason.size(), 512)), + reason.data()); } }); const auto status = g_instance.WaitAny(future, 5000000000); if (status != wgpu::WaitStatus::Success) { Log.error("Failed to create device: {}", magic_enum::enum_name(status)); + SDL_SetError("Graphics device request did not complete within its startup deadline"); return false; } if (!g_device) { diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index abc2e9b4c..539cd4a60 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp @@ -1551,6 +1551,10 @@ int RuntimeMain(int argc, char** argv) { WiiRemoteInput::ConfigureSdlHints(RuntimeConfigFile::WiiRemotesEnabled(true)); const AuroraInfo auroraInfo = aurora_initialize(0, nullptr, &auroraConfig); + if (auroraInfo.initializationStatus != AURORA_INITIALIZATION_SUCCESS) { + throw std::runtime_error(auroraInfo.initializationError != nullptr + ? auroraInfo.initializationError : "No supported graphics backend is available"); + } if (requestedBackend != BACKEND_AUTO && auroraInfo.backend != requestedBackend) { RT_LOG(RT_TAG_RUNTIME) << "graphics_api=\"" << backend << "\" is not available on this system; aurora fell back to \"" From 897899a5f94ffa61eab6c28bb2377e9578b8dd66 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 5 Oct 2026 13:35:51 +0000 Subject: [PATCH 11/11] Sleep instead of spinning while waiting for a staging buffer When the GPU is two frames behind, begin_frame waits for the next staging buffer's MapAsync. It used to spin on ProcessEvents, holding a whole core busy for as long as the GPU took. It now calls ProcessEvents, checks for device loss, then sleeps on a condition variable until the map callback lands or 1 ms passes. The map state also carries a generation, so a callback from a request that shutdown or a rotation already retired can no longer mark the current buffer mapped or unmapped. From upstream patchzyy/Wiicompiled 6f14bde (#244, KartPad batch). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01Wg7mB8ogCWmp9GH19Uc82B --- aurora-main/lib/gfx/common.cpp | 35 ++++++----- aurora-main/lib/gfx/staging_map.hpp | 60 +++++++++++++++++++ .../tests/renderer_regression_test.cpp | 33 ++++++++++ 3 files changed, 110 insertions(+), 18 deletions(-) create mode 100644 aurora-main/lib/gfx/staging_map.hpp diff --git a/aurora-main/lib/gfx/common.cpp b/aurora-main/lib/gfx/common.cpp index e5e3961d6..bd897b9e5 100644 --- a/aurora-main/lib/gfx/common.cpp +++ b/aurora-main/lib/gfx/common.cpp @@ -1,4 +1,5 @@ #include "common.hpp" +#include "staging_map.hpp" #include "../gx/shader_info.hpp" #include "clear.hpp" @@ -139,12 +140,7 @@ wgpu::Buffer g_storageBuffer; constexpr size_t FrameSlotCount = 3; static std::array g_stagingBuffers; static size_t currentStagingBuffer = 0; -enum class BufferMapState { - Unmapped, - Mapping, - Mapped, -}; -static std::atomic s_mappingState{BufferMapState::Unmapped}; +static StagingMapState s_mappingState; static wgpu::Limits g_cachedLimits; // Advanced once per logical frame in the seal prologue, under the renderer GPU mutex and with the // producer blocked, so every later reader sees a value that no longer moves. @@ -1048,7 +1044,7 @@ void initialize() { label.c_str()); } currentStagingBuffer = 0; - s_mappingState.store(BufferMapState::Unmapped, std::memory_order_release); + s_mappingState.reset(); map_staging_buffer(); { @@ -1160,6 +1156,8 @@ void shutdown() { g_uniformBuffer = {}; g_indexBuffer = {}; g_storageBuffer = {}; + // Invalidate outstanding callbacks before releasing their buffers. + s_mappingState.reset(); g_stagingBuffers.fill({}); for (auto& pool : g_resolveSourceSnapshotPools) { pool.entry.reset(); @@ -1178,27 +1176,25 @@ void shutdown() { g_inOffscreen = false; g_frameIndex = UINT32_MAX; currentStagingBuffer = 0; - s_mappingState.store(BufferMapState::Unmapped, std::memory_order_release); } void map_staging_buffer() { - auto expected = BufferMapState::Unmapped; - if (!s_mappingState.compare_exchange_strong(expected, BufferMapState::Mapping, std::memory_order_acq_rel, - std::memory_order_acquire)) { + const auto generation = s_mappingState.request(); + if (generation == 0) { return; } g_stagingBuffers[currentStagingBuffer].MapAsync( wgpu::MapMode::Write, 0, StagingBufferSize, wgpu::CallbackMode::AllowSpontaneous, - [](wgpu::MapAsyncStatus status, wgpu::StringView message) { + [generation](wgpu::MapAsyncStatus status, wgpu::StringView message) { + const auto result = status == wgpu::MapAsyncStatus::Success ? BufferMapState::Mapped : BufferMapState::Unmapped; + if (!s_mappingState.complete(generation, result)) return; if (status == wgpu::MapAsyncStatus::CallbackCancelled || status == wgpu::MapAsyncStatus::Aborted) { Log.warn("Buffer mapping {}: {}", magic_enum::enum_name(status), message); - s_mappingState.store(BufferMapState::Unmapped, std::memory_order_release); return; } ASSERT(status == wgpu::MapAsyncStatus::Success, "Buffer mapping failed: {} {}", magic_enum::enum_name(status), message); - s_mappingState.store(BufferMapState::Mapped, std::memory_order_release); }); } @@ -1208,7 +1204,7 @@ static bool begin_frame_impl(bool clearEfb) { ZoneScopedN("Wait for buffer map"); map_staging_buffer(); while (true) { - const auto mappingState = s_mappingState.load(std::memory_order_acquire); + const auto mappingState = s_mappingState.state(); if (mappingState == BufferMapState::Mapped) { break; } @@ -1224,6 +1220,9 @@ static bool begin_frame_impl(bool clearEfb) { return false; } g_instance.ProcessEvents(); + webgpu::fail_if_device_lost(); + // Sleep until the map callback lands (or 1 ms passes) instead of spinning a core on ProcessEvents. + s_mappingState.wait_for_progress(); } } g_recordingSnapshotSlot = currentStagingBuffer; @@ -1296,12 +1295,12 @@ void abort_frame() noexcept { g_textureUploads.clear(); g_textureUpload.release(); } - if (s_mappingState.load(std::memory_order_acquire) == BufferMapState::Mapped) { + if (s_mappingState.state() == BufferMapState::Mapped) { // Pending interpolation tasks hold raw pointers into the mapped staging // range; they must be dropped before the buffer is unmapped and rotated. gx::drop_pending_frame_interpolation_uniforms(); g_stagingBuffers[currentStagingBuffer].Unmap(); - s_mappingState.store(BufferMapState::Unmapped, std::memory_order_release); + s_mappingState.reset(); currentStagingBuffer = (currentStagingBuffer + 1) % g_stagingBuffers.size(); map_staging_buffer(); } @@ -1741,7 +1740,7 @@ static bool end_batch_impl(const wgpu::CommandEncoder& cmd, bool advanceFrame, g_uniformUploadDestination = nullptr; } g_stagingBuffers[currentStagingBuffer].Unmap(); - s_mappingState.store(BufferMapState::Unmapped, std::memory_order_release); + s_mappingState.reset(); g_stats.drawCallCount = g_drawCallCount; g_stats.mergedDrawCallCount = g_mergedDrawCallCount; g_stats.lastVertSize = writeBuffer(g_verts, g_vertexBuffer, VertexBufferSize, "Vertex"); diff --git a/aurora-main/lib/gfx/staging_map.hpp b/aurora-main/lib/gfx/staging_map.hpp new file mode 100644 index 000000000..59796c330 --- /dev/null +++ b/aurora-main/lib/gfx/staging_map.hpp @@ -0,0 +1,60 @@ +#pragma once + +#include +#include +#include +#include + +namespace aurora::gfx { + +enum class BufferMapState { Unmapped, Mapping, Mapped }; + +// The renderer owns request/reset; Dawn may complete a request on another thread. +// An old callback must never publish readiness for a different staging slot. +class StagingMapState { + mutable std::mutex mutex_; + std::condition_variable changed_; + uint64_t generation_ = 0; + BufferMapState state_ = BufferMapState::Unmapped; + +public: + uint64_t request() { + std::lock_guard lock(mutex_); + if (state_ != BufferMapState::Unmapped) return 0; + state_ = BufferMapState::Mapping; + return ++generation_; + } + + bool complete(uint64_t generation, BufferMapState state) { + { + std::lock_guard lock(mutex_); + if (generation != generation_ || state_ != BufferMapState::Mapping) return false; + state_ = state; + } + changed_.notify_all(); + return true; + } + + void reset() { + { + std::lock_guard lock(mutex_); + ++generation_; + state_ = BufferMapState::Unmapped; + } + changed_.notify_all(); + } + + BufferMapState state() const { + std::lock_guard lock(mutex_); + return state_; + } + + void wait_for_progress() { + std::unique_lock lock(mutex_); + // ProcessEvents is still serviced between waits for implementations that + // need it. A spontaneous completion wakes immediately, without polling. + changed_.wait_for(lock, std::chrono::milliseconds(1), [&] { return state_ != BufferMapState::Mapping; }); + } +}; + +} // namespace aurora::gfx diff --git a/aurora-main/tests/renderer_regression_test.cpp b/aurora-main/tests/renderer_regression_test.cpp index 0a845b5fb..841a6b587 100644 --- a/aurora-main/tests/renderer_regression_test.cpp +++ b/aurora-main/tests/renderer_regression_test.cpp @@ -1,6 +1,9 @@ #include "gx_test_common.hpp" +#include "gfx/staging_map.hpp" #include "gx/pipeline.hpp" +#include + using aurora::gx::g_gxState; namespace { @@ -84,3 +87,33 @@ TEST_F(GXFifoTest, SingleExpandedPrimitiveCannotMergeWithTriangles) { EXPECT_EQ(aurora::gfx::g_mergedDrawCallCount, 0u); EXPECT_EQ(aurora::gfx::testing::last_pushed_indices(), (std::vector{0, 1, 2})); } + +TEST(StagingMapping, RetiredCallbacksCannotPublishAnotherBuffersReadiness) { + using namespace aurora::gfx; + StagingMapState state; + const auto old = state.request(); + EXPECT_EQ(state.request(), 0u); + state.reset(); + const auto current = state.request(); + EXPECT_FALSE(state.complete(old, BufferMapState::Mapped)); + EXPECT_FALSE(state.complete(old, BufferMapState::Unmapped)); + EXPECT_EQ(state.state(), BufferMapState::Mapping); + EXPECT_TRUE(state.complete(current, BufferMapState::Mapped)); + EXPECT_FALSE(state.complete(current, BufferMapState::Unmapped)); + EXPECT_EQ(state.state(), BufferMapState::Mapped); +} + +TEST(StagingMapping, AsyncCompletionWakesWaiters) { + using namespace aurora::gfx; + StagingMapState state; + const auto generation = state.request(); + std::thread callback([&] { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + state.complete(generation, BufferMapState::Mapped); + }); + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(2); + while (state.state() == BufferMapState::Mapping && std::chrono::steady_clock::now() < deadline) + state.wait_for_progress(); + callback.join(); + EXPECT_EQ(state.state(), BufferMapState::Mapped); +}