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>
This commit is contained in:
chrg6
2026-11-06 03:03:08 -05:00
parent 0478c13e45
commit 77d0c0b84e
65 changed files with 8136 additions and 554 deletions

View File

@@ -0,0 +1,118 @@
--- 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);

View File

@@ -0,0 +1,22 @@
--- a/src/Ryujinx.Graphics.Shader/StructuredIr/ShaderProperties.cs
+++ b/src/Ryujinx.Graphics.Shader/StructuredIr/ShaderProperties.cs
@@ -1,3 +1,4 @@
+using Ryujinx.Graphics.Shader.Translation;
using System.Collections.Generic;
namespace Ryujinx.Graphics.Shader.StructuredIr
@@ -70,7 +71,13 @@
// specify a format, we can just declare it without a format and the GPU will handle it.
if (gpuAccessor.QueryHostSupportsImageLoadFormatted())
{
- return TextureFormat.Unknown;
+ // NX2NAVI: an integer image still needs its format: without one it is declared float, and a store of raw
+ // words (Ocarina packs R11G11B10 itself and stores it through an R32UI view) converts them.
+ TextureFormat integer = gpuAccessor.QueryTextureFormat(handle, cbufSlot);
+
+ AggregateType component = integer.GetComponentType();
+
+ return component == AggregateType.U32 || component == AggregateType.S32 ? integer : TextureFormat.Unknown;
}
TextureFormat format = gpuAccessor.QueryTextureFormat(handle, cbufSlot);

View File

@@ -0,0 +1,27 @@
--- a/src/Ryujinx.Graphics.Shader/Translation/TranslatorContext.cs
+++ b/src/Ryujinx.Graphics.Shader/Translation/TranslatorContext.cs
@@ -159,8 +159,22 @@
internal int GetDepthRegister()
{
- // The depth register is always two registers after the last color output.
- return BitOperations.PopCount((uint)Definitions.OmapTargets) + 1;
+ // NX2NAVI: every render target with any component enabled takes four registers (the colour outputs above), then
+ // the sample mask when written, then one register is skipped. The component count was used, which matches
+ // only when every enabled target writes all four: Ocarina's depth downsample (targets 0 and 2, .x each) read
+ // its depth from R3, never written, and wrote 0 (all its fog then sat at the near plane). As the hardware's
+ // ExitFragment counts.
+ int targets = 0;
+
+ for (int rtIndex = 0; rtIndex < 8; rtIndex++)
+ {
+ if ((Definitions.OmapTargets & (0xf << (rtIndex * 4))) != 0)
+ {
+ targets++;
+ }
+ }
+
+ return targets * 4 + (Definitions.OmapSampleMask ? 1 : 0) + 1;
}
public void SetLayerOutputAttribute(int attr)

View File

@@ -0,0 +1,18 @@
--- a/src/Ryujinx.Graphics.Shader/Translation/Optimizations/GlobalToStorage.cs
+++ b/src/Ryujinx.Graphics.Shader/Translation/Optimizations/GlobalToStorage.cs
@@ -1046,6 +1046,15 @@
// because constant buffer are always encoded as the second operand,
// so the second operand will always be the one from the last instruction.
+ // NX2NAVI: c1 holds the compiler's constants (BRX jump tables, offsets), never a buffer address; an address
+ // that adds one of them to a pointer from another bank (Ocarina's NVN2 compute: a push-constant pointer in
+ // c0 plus the constant 12 from c1) has the pointer as its base. Taking c1 sent those stores to nothing.
+ if (src1.Type == OperandType.ConstantBuffer && src2.Type == OperandType.ConstantBuffer &&
+ src2.GetCbufSlot() == 1 && src1.GetCbufSlot() != 1)
+ {
+ return GetBaseAddressCbWithOffset(src1, src2, constOffset);
+ }
+
if (src1.Type != OperandType.ConstantBuffer ||
(src1.Type == OperandType.ConstantBuffer && src2.Type == OperandType.ConstantBuffer) ||
(src2.Type == OperandType.ConstantBuffer && src2.GetCbufSlot() == DriverReservedCb))