Files
NX2navi/tools/xlate/patches/0017-goto-elimination-outer-loop-exits.patch
chrg6 77d0c0b84e Port Ocarina of Time (Switch 2) to the recomp host
The NVN2 front end and the SM86 to Maxwell shader lowering (tools/xlate/AmpereLowering.cs, mesh and task stages,
bindless texture pools, BRX jump tables, the NVN2 compute c0 layout) with four translator patches (0017-0020) bring the
opening of the game to 58-60 fps on the Vulkan renderer.

This round fixes the haze: SM86 IPA is lowered as a plain Maxwell IPA.PASS (texture coordinates were corrected for
perspective twice); LDSM, TXD, both F2IP byte forms, HMMA, MOVM and HFMA2.RELU are lowered and spill mode has eight
uniform register groups; conditional rendering keeps the newest counter report per address and treats a report as
pending until its value is in guest memory, so occlusion tests and the depth clears around the depth copy behave; and
the upscaler stand-in decodes the post pass's sRGB picture before writing the linear target. Tensor-core programs keep
the stand-in unless NX2NAVI_NATIVE_TENSOR=1, because their lowering does not match the hardware yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 05:04:39 -05:00

119 lines
4.6 KiB
Diff

--- a/src/Ryujinx.Graphics.Shader/StructuredIr/GotoElimination.cs
+++ b/src/Ryujinx.Graphics.Shader/StructuredIr/GotoElimination.cs
@@ -1,4 +1,5 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
+using Ryujinx.Graphics.Shader.Translation;
using System;
using System.Collections.Generic;
using static Ryujinx.Graphics.Shader.StructuredIr.AstHelper;
@@ -9,8 +10,12 @@
{
// This is a modified version of the algorithm presented on the paper
// "Taming Control Flow: A Structured Approach to Eliminating Goto Statements".
- public static void Eliminate(GotoStatement[] gotos)
+ // NX2NAVI: makes the flags RouteOuterExits needs (StructuredProgramContext.NewTemp).
+ [ThreadStatic] private static Func<AggregateType, AstOperand> _newTemp;
+
+ public static void Eliminate(GotoStatement[] gotos, Func<AggregateType, AstOperand> newTemp)
{
+ _newTemp = newTemp;
for (int index = gotos.Length - 1; index >= 0; index--)
{
GotoStatement stmt = gotos[index];
@@ -381,9 +386,84 @@
first = next;
}
+ if (type == AstBlockType.DoWhile)
+ {
+ RouteOuterExits(newBlock);
+ }
+
return newBlock;
}
+ // NX2NAVI: a loop made here around statements that already hold a break or continue (made by MoveOutward or
+ // EncloseDoWhile for an enclosing loop) would take them as its own: the break then left only the new loop and
+ // the enclosing one never ended (a GPU hang in fragment shaders whose light loop holds a BRX switch, lowered to
+ // compares and branches by AmpereLowering). Each such exit now sets a flag and breaks the new loop; after it the
+ // flag repeats the exit for the enclosing loop.
+ private static void RouteOuterExits(AstBlock loop)
+ {
+ List<AstOperation> exits = [];
+
+ CollectExits(loop, exits);
+
+ if (exits.Count == 0 || _newTemp == null)
+ {
+ return;
+ }
+
+ AstBlock parent = loop.Parent;
+ AstOperand breakFlag = null, continueFlag = null;
+
+ foreach (AstOperation exit in exits)
+ {
+ bool isBreak = exit.Inst == Instruction.LoopBreak;
+ AstOperand flag = isBreak ? (breakFlag ??= _newTemp(AggregateType.Bool)) : (continueFlag ??= _newTemp(AggregateType.Bool));
+ AstBlock block = ParentBlock(exit);
+
+ block.AddBefore(exit, Assign(flag, Const(IrConsts.True)));
+
+ if (!isBreak)
+ {
+ block.AddBefore(exit, new AstOperation(Instruction.LoopBreak));
+ block.Remove(exit);
+ }
+ }
+
+ foreach (AstOperand flag in new[] { continueFlag, breakFlag })
+ {
+ if (flag == null)
+ {
+ continue;
+ }
+
+ parent.AddBefore(loop, Assign(flag, Const(IrConsts.False)));
+
+ AstBlock check = new(AstBlockType.If, flag);
+
+ check.AddFirst(new AstOperation(flag == breakFlag ? Instruction.LoopBreak : Instruction.LoopContinue));
+
+ parent.AddAfter(loop, check);
+ }
+ }
+
+ // The breaks and continues in block that belong to a loop around it (those inside a nested loop are its own).
+ private static void CollectExits(AstBlock block, List<AstOperation> exits)
+ {
+ foreach (IAstNode node in block)
+ {
+ if (node is AstBlock child)
+ {
+ if (child.Type != AstBlockType.DoWhile)
+ {
+ CollectExits(child, exits);
+ }
+ }
+ else if (node is AstOperation op && (op.Inst == Instruction.LoopBreak || op.Inst == Instruction.LoopContinue))
+ {
+ exits.Add(op);
+ }
+ }
+ }
+
private static bool BlockMatches(IAstNode node, AstBlockType type, IAstNode cond)
{
if (node is not AstBlock block)
--- a/src/Ryujinx.Graphics.Shader/StructuredIr/StructuredProgram.cs
+++ b/src/Ryujinx.Graphics.Shader/StructuredIr/StructuredProgram.cs
@@ -68,7 +68,7 @@
}
}
- GotoElimination.Eliminate(context.GetGotos());
+ GotoElimination.Eliminate(context.GetGotos(), context.NewTemp);
AstOptimizer.Optimize(context);