mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-17 20:09:57 -04:00
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:
@@ -354,7 +354,9 @@ def module_glue(layout, linked, hashes, paths, version):
|
||||
for name in ('exefs', 'romfs', 'profile'):
|
||||
out.append(f'const char rt_default_{name}[] = {c_string(paths[name].as_posix())};')
|
||||
number = int(DISPLAY_VERSIONS[version].replace('.', '')) if version in DISPLAY_VERSIONS else 0 # 1.4.3 -> 143
|
||||
out += [f'const int rt_game_version = {number};', f'const char rt_port_version[] = {c_string(port_version())};', '']
|
||||
out += ['const unsigned long long rt_app_pool_bytes = 0xCD500000ull;', # Switch 1 retail application pool
|
||||
'const int rt_nvn_api_version[2] = {55, 15};', # NVN1: NX2NAVI main requires major 55, minor >= 15
|
||||
f'const int rt_game_version = {number};', f'const char rt_port_version[] = {c_string(port_version())};', '']
|
||||
return '\n'.join(out)
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""Build the shader translator: build/xlate/nx2navi_xlate.exe (Ryujinx 1.3.2 + patches, NativeAOT, SPIR-V for Vulkan).
|
||||
|
||||
Copies tools/vendor/ryujinx to build/xlate/ryujinx, applies tools/xlate/patches/*.patch there (tools/vendor stays
|
||||
byte-identical; 0001-0004 are MNG's, 0005-0013, 0015 and 0016 are ours), then runs `dotnet publish`. Needs the .NET 10 SDK, the MSVC build
|
||||
byte-identical; 0001-0004 are MNG's, 0005-0013 and 0015-0020 are ours), then runs `dotnet publish`. Needs the .NET 10 SDK, the MSVC build
|
||||
tools and vswhere.exe (docs/building.md). Run with .venv. On Linux it builds build/xlate/nx2navi_xlate for linux-x64, which
|
||||
NativeAOT links with the system's clang (no MSVC); NativeAOT builds for the OS it runs on only.
|
||||
|
||||
@@ -99,8 +99,8 @@ def prepare_sources() -> Path:
|
||||
for junk in list(src.glob("src/*/obj")) + list(src.glob("src/*/bin")):
|
||||
shutil.rmtree(junk)
|
||||
patches = sorted((XLATE / "patches").glob("*.patch"))
|
||||
if len(patches) != 15:
|
||||
sys.exit(f"expected 15 patches, found {len(patches)}")
|
||||
if len(patches) != 19:
|
||||
sys.exit(f"expected 19 patches, found {len(patches)}")
|
||||
for patch in patches:
|
||||
# git apply works outside a repository when given a directory; --check first so a failure names the patch.
|
||||
for extra in (["--check"], []):
|
||||
@@ -117,7 +117,7 @@ def prepare_sources() -> Path:
|
||||
def identity() -> str:
|
||||
"""sha256 over the translator's inputs, in a fixed order: path and content hash of each file."""
|
||||
h = hashlib.sha256()
|
||||
files = [Path(__file__).resolve(), XLATE / "Program.cs", XLATE / "nx2navi_xlate.csproj",
|
||||
files = [Path(__file__).resolve(), XLATE / "Program.cs", XLATE / "AmpereLowering.cs", XLATE / "MeshSpirv.cs", XLATE / "nx2navi_xlate.csproj",
|
||||
*sorted((XLATE / "patches").glob("*.patch"))]
|
||||
for f in files:
|
||||
h.update(f"{f.name}\0{hashlib.sha256(f.read_bytes()).hexdigest()}\n".encode())
|
||||
@@ -161,7 +161,7 @@ def main() -> None:
|
||||
work = BUILD / "xlate"
|
||||
shutil.rmtree(work, ignore_errors=True)
|
||||
work.mkdir(parents=True)
|
||||
for name in ("Program.cs", "nx2navi_xlate.csproj"):
|
||||
for name in ("Program.cs", "AmpereLowering.cs", "MeshSpirv.cs", "nx2navi_xlate.csproj"):
|
||||
shutil.copy2(XLATE / name, work / name)
|
||||
out = BUILD / "publish"
|
||||
shutil.rmtree(out, ignore_errors=True)
|
||||
|
||||
@@ -24,6 +24,17 @@ SUBSDK0 = [ # section 0.1: asked by the movie library through nvnDeviceGetProcA
|
||||
"nvnTextureGetNvRmSurfaceNVX",
|
||||
]
|
||||
MAIN_COUNT = 534
|
||||
# NVN2 (Switch 2) procs this host implements; the rest of the NVN2 names get traps (runtime/nvn_procs.c nvn_trap).
|
||||
NVN2 = [
|
||||
"nvnCommandBufferDrawMeshTasks",
|
||||
"nvnCommandBufferDrawMeshTasksIndirect",
|
||||
"nvnCommandBufferPushComputeConstants",
|
||||
"nvnCommandBufferSetGeometryProcessingMode",
|
||||
"nvnProgramBuilderSetDefaults",
|
||||
"nvnProgramBuilderSetDevice",
|
||||
"nvnProgramBuilderSetShaders",
|
||||
"nvnProgramInitializeFromBuilder",
|
||||
]
|
||||
|
||||
|
||||
def main() -> int:
|
||||
@@ -39,7 +50,7 @@ def main() -> int:
|
||||
return 1
|
||||
# nvnDeviceGetProcAddress is in both lists; keep one row per name.
|
||||
extra_sub = [n for n in SUBSDK0 if n not in main_names]
|
||||
rows = sorted(main_names) + sorted(extra_sub)
|
||||
rows = sorted(main_names) + sorted(extra_sub) + sorted(n for n in NVN2 if n not in main_names)
|
||||
text = ("// Generated by tools/gen_nvn_procs.py: the NVN proc names of NX2navi 1.2.1.\n"
|
||||
+ "".join(f"NVN_PROC({n})\n" for n in rows))
|
||||
OUT.write_text(text, encoding="utf-8", newline="\n")
|
||||
|
||||
2387
tools/xlate/AmpereLowering.cs
Normal file
2387
tools/xlate/AmpereLowering.cs
Normal file
File diff suppressed because it is too large
Load Diff
367
tools/xlate/MeshSpirv.cs
Normal file
367
tools/xlate/MeshSpirv.cs
Normal file
@@ -0,0 +1,367 @@
|
||||
// Turns the compute SPIR-V the translator made from a lowered NVN2 task or mesh program (AmpereLowering.MeshInfo) into a
|
||||
// TaskEXT or MeshEXT module (SPV_EXT_mesh_shader, VK_EXT_mesh_shader).
|
||||
//
|
||||
// The translator's main function is kept as it is and called from a new entry function:
|
||||
// mesh: [after a task stage: the task payload copied into the shared memory payload area, barrier] main; barrier;
|
||||
// SetMeshOutputsEXT(vertex limit, primitive count from shared memory); every thread copies vertices
|
||||
// i, i + threads, ... and primitives likewise from the shared memory stage buffer into the outputs: position
|
||||
// and point size built-ins, generic vector g at location g, the primitive's vertex indices.
|
||||
// task: main; barrier; the payload area copied out to the TaskPayloadWorkgroupEXT variable; EmitMeshTasksEXT with
|
||||
// the mesh workgroup count the program wrote.
|
||||
// An EXIT in main returns to the entry function, so threads that leave early still reach the barrier. SPV_EXT_mesh_shader
|
||||
// needs SPIR-V 1.4, where BufferBlock is gone: the translator's storage buffers (Uniform + BufferBlock, SPIR-V 1.3) move
|
||||
// to the StorageBuffer storage class with Block, and the entry point lists every global variable.
|
||||
//
|
||||
// Opcode and enum numbers are the SPIR-V 1.6 specification's and SPV_EXT_mesh_shader's.
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
static class MeshSpirv
|
||||
{
|
||||
// Generic vectors the mesh program writes (output locations).
|
||||
public static IEnumerable<int> GenericLocations(AmpereLowering.MeshInfo m)
|
||||
{
|
||||
var seen = new SortedSet<int>();
|
||||
foreach (int a in m.Lines) if (a >= 0x80) seen.Add((a - 0x80) / 16);
|
||||
return seen;
|
||||
}
|
||||
|
||||
const int OpExtension = 10, OpExtInstImport = 11, OpExtInst = 12, OpMemoryModel = 14, OpEntryPoint = 15,
|
||||
OpExecutionMode = 16, OpCapability = 17, OpTypeVoid = 19, OpTypeBool = 20, OpTypeInt = 21, OpTypeFloat = 22,
|
||||
OpTypeVector = 23, OpTypeArray = 28, OpTypePointer = 32, OpTypeFunction = 33, OpConstant = 43,
|
||||
OpFunction = 54, OpFunctionEnd = 56, OpFunctionCall = 57, OpVariable = 59, OpLoad = 61, OpStore = 62,
|
||||
OpAccessChain = 65, OpInBoundsAccessChain = 66, OpPtrAccessChain = 67, OpInBoundsPtrAccessChain = 70,
|
||||
OpDecorate = 71, OpCompositeConstruct = 80, OpIAdd = 128, OpIMul = 132, OpULessThan = 176,
|
||||
OpShiftRightLogical = 194, OpBitwiseAnd = 199, OpBitcast = 124, OpControlBarrier = 224, OpSelectionMerge = 247,
|
||||
OpLabel = 248, OpBranch = 249, OpBranchConditional = 250, OpReturn = 253,
|
||||
OpEmitMeshTasksEXT = 5294, OpSetMeshOutputsEXT = 5295;
|
||||
const int ScUniform = 2, ScOutput = 3, ScWorkgroup = 4, ScInput = 1, ScFunction = 7, ScStorageBuffer = 12, ScTaskPayload = 5402;
|
||||
const int DecBlock = 2, DecBufferBlock = 3, DecBuiltIn = 11, DecLocation = 30;
|
||||
const int BiPosition = 0, BiPointSize = 1, BiLocalInvocationIndex = 29,
|
||||
BiPrimitivePointIndices = 5294, BiPrimitiveLineIndices = 5295, BiPrimitiveTriangleIndices = 5296;
|
||||
const int CapMeshShading = 5283, ModelTask = 5364, ModelMesh = 5365, ModelGLCompute = 5;
|
||||
const int ModeOutputVertices = 26, ModeOutputPoints = 27, ModeOutputLines = 5269, ModeOutputPrimitives = 5270,
|
||||
ModeOutputTriangles = 5298;
|
||||
const int GlslUMin = 38;
|
||||
|
||||
sealed class Insn
|
||||
{
|
||||
public int Op;
|
||||
public List<uint> W; // operands, without the opcode word
|
||||
public Insn(int op, IEnumerable<uint> w) { Op = op; W = w.ToList(); }
|
||||
}
|
||||
|
||||
static List<uint> Str(string s)
|
||||
{
|
||||
var bytes = System.Text.Encoding.UTF8.GetBytes(s + "\0");
|
||||
var w = new List<uint>();
|
||||
for (int i = 0; i < bytes.Length; i += 4)
|
||||
{
|
||||
uint v = 0;
|
||||
for (int k = 0; k < 4 && i + k < bytes.Length; k++) v |= (uint)bytes[i + k] << (8 * k);
|
||||
w.Add(v);
|
||||
}
|
||||
return w;
|
||||
}
|
||||
|
||||
// The first function that reaches itself through OpFunctionCall, or -1. Vulkan SPIR-V forbids recursion
|
||||
// (VUID-StandaloneSpirv-None-04634); NVIDIA's compiler inlines such a cycle until its thread's stack overflows.
|
||||
public static long CallCycle(byte[] spirv)
|
||||
{
|
||||
var w = new uint[spirv.Length / 4];
|
||||
Buffer.BlockCopy(spirv, 0, w, 0, w.Length * 4);
|
||||
var calls = new Dictionary<uint, List<uint>>();
|
||||
uint fn = 0;
|
||||
for (int p = 5; p < w.Length;)
|
||||
{
|
||||
int n = (int)(w[p] >> 16), op = (int)(w[p] & 0xffff);
|
||||
if (n == 0) break;
|
||||
if (op == OpFunction) fn = w[p + 2];
|
||||
else if (op == OpFunctionCall) { if (!calls.TryGetValue(fn, out var l)) calls[fn] = l = new(); l.Add(w[p + 3]); }
|
||||
p += n;
|
||||
}
|
||||
var state = new Dictionary<uint, int>(); // 1 on the path, 2 done
|
||||
long found = -1;
|
||||
bool Visit(uint f)
|
||||
{
|
||||
state[f] = 1;
|
||||
if (calls.TryGetValue(f, out var l))
|
||||
foreach (uint g in l)
|
||||
{
|
||||
if (state.TryGetValue(g, out int st) ? st == 1 : Visit(g)) { if (found < 0) found = g; return true; }
|
||||
}
|
||||
state[f] = 2;
|
||||
return false;
|
||||
}
|
||||
foreach (uint f in calls.Keys.ToList()) if (!state.ContainsKey(f) && Visit(f)) break;
|
||||
return found;
|
||||
}
|
||||
|
||||
public static byte[] Convert(byte[] spirv, AmpereLowering.MeshInfo m)
|
||||
{
|
||||
if (spirv.Length < 20 || BitConverter.ToUInt32(spirv, 0) != 0x07230203) throw new Exception("mesh: not a SPIR-V module");
|
||||
var words = new uint[spirv.Length / 4];
|
||||
Buffer.BlockCopy(spirv, 0, words, 0, words.Length * 4);
|
||||
uint bound = words[3];
|
||||
var ins = new List<Insn>();
|
||||
for (int p = 5; p < words.Length;)
|
||||
{
|
||||
int n = (int)(words[p] >> 16), op = (int)(words[p] & 0xffff);
|
||||
if (n == 0 || p + n > words.Length) throw new Exception("mesh: malformed SPIR-V");
|
||||
ins.Add(new Insn(op, new ArraySegment<uint>(words, p + 1, n - 1)));
|
||||
p += n;
|
||||
}
|
||||
uint Id() => bound++;
|
||||
|
||||
// ---- storage buffers: Uniform + BufferBlock -> StorageBuffer + Block
|
||||
var bufferBlocks = new HashSet<uint>();
|
||||
foreach (var i in ins)
|
||||
if (i.Op == OpDecorate && i.W[1] == DecBufferBlock) { bufferBlocks.Add(i.W[0]); i.W[1] = DecBlock; }
|
||||
var pointee = new Dictionary<uint, (uint sc, uint type)>(); // pointer type -> storage class, pointee
|
||||
var ssboPointerTypes = new HashSet<uint>();
|
||||
foreach (var i in ins)
|
||||
if (i.Op == OpTypePointer)
|
||||
{
|
||||
if (i.W[1] == ScUniform && bufferBlocks.Contains(i.W[2])) { i.W[1] = ScStorageBuffer; ssboPointerTypes.Add(i.W[0]); }
|
||||
pointee[i.W[0]] = (i.W[1], i.W[2]);
|
||||
}
|
||||
var ssboPointers = new HashSet<uint>();
|
||||
var newTypes = new List<Insn>(); // inserted before the first function
|
||||
var sbPointerOf = new Dictionary<uint, uint>(); // pointee -> StorageBuffer pointer type
|
||||
uint StoragePointer(uint type)
|
||||
{
|
||||
if (sbPointerOf.TryGetValue(type, out uint t)) return t;
|
||||
foreach (var (pt, (sc, pe)) in pointee) if (sc == ScStorageBuffer && pe == type) { sbPointerOf[type] = pt; return pt; }
|
||||
t = Id();
|
||||
newTypes.Add(new Insn(OpTypePointer, new[] { t, (uint)ScStorageBuffer, type }));
|
||||
pointee[t] = (ScStorageBuffer, type);
|
||||
sbPointerOf[type] = t;
|
||||
return t;
|
||||
}
|
||||
foreach (var i in ins)
|
||||
{
|
||||
if (i.Op == OpVariable && ssboPointerTypes.Contains(i.W[0])) { i.W[2] = ScStorageBuffer; ssboPointers.Add(i.W[1]); }
|
||||
else if (i.Op is OpAccessChain or OpInBoundsAccessChain or OpPtrAccessChain or OpInBoundsPtrAccessChain && ssboPointers.Contains(i.W[2]))
|
||||
{
|
||||
var (sc, pe) = pointee[i.W[0]];
|
||||
if (sc != ScStorageBuffer) i.W[0] = StoragePointer(pe);
|
||||
ssboPointers.Add(i.W[1]);
|
||||
}
|
||||
}
|
||||
|
||||
// ---- existing types and ids
|
||||
uint Find(Func<Insn, bool> f) { var i = ins.FirstOrDefault(f); return i == null ? 0 : i.W[0]; }
|
||||
int firstFunction = ins.FindIndex(i => i.Op == OpFunction);
|
||||
var globals = new List<Insn>(); // new types, constants, variables, inserted before the first function
|
||||
uint Ty(int op, params uint[] operands)
|
||||
{
|
||||
uint t = Find(i => i.Op == op && i.W.Skip(1).SequenceEqual(operands));
|
||||
if (t == 0) t = newTypes.Concat(globals).FirstOrDefault(i => i.Op == op && i.W.Skip(1).SequenceEqual(operands))?.W[0] ?? 0;
|
||||
if (t != 0) return t;
|
||||
t = Id();
|
||||
globals.Add(new Insn(op, new[] { t }.Concat(operands)));
|
||||
return t;
|
||||
}
|
||||
uint tVoid = Ty(OpTypeVoid), tBool = Ty(OpTypeBool), tUint = Ty(OpTypeInt, 32, 0), tFloat = Ty(OpTypeFloat, 32);
|
||||
uint tFnVoid = Ty(OpTypeFunction, tVoid);
|
||||
var constants = new Dictionary<uint, uint>();
|
||||
uint C(uint v)
|
||||
{
|
||||
if (constants.TryGetValue(v, out uint id)) return id;
|
||||
id = Id();
|
||||
globals.Add(new Insn(OpConstant, new[] { tUint, id, v }));
|
||||
constants[v] = id;
|
||||
return id;
|
||||
}
|
||||
uint Ptr(int sc, uint type) { uint t = Id(); globals.Add(new Insn(OpTypePointer, new[] { t, (uint)sc, type })); return t; }
|
||||
uint Arr(uint elem, uint n) { uint t = Id(); globals.Add(new Insn(OpTypeArray, new[] { t, elem, C(n) })); return t; }
|
||||
var decorations = new List<Insn>();
|
||||
var interfaceVars = new List<uint>();
|
||||
uint Var(uint ptrType, int sc, int builtin = -1, int location = -1)
|
||||
{
|
||||
uint v = Id();
|
||||
globals.Add(new Insn(OpVariable, new[] { ptrType, v, (uint)sc }));
|
||||
if (builtin >= 0) decorations.Add(new Insn(OpDecorate, new[] { v, (uint)DecBuiltIn, (uint)builtin }));
|
||||
if (location >= 0) decorations.Add(new Insn(OpDecorate, new[] { v, (uint)DecLocation, (uint)location }));
|
||||
return v;
|
||||
}
|
||||
|
||||
var shared = ins.Where(i => i.Op == OpVariable && i.W[2] == ScWorkgroup).ToList();
|
||||
if (shared.Count != 1) throw new Exception($"mesh: {shared.Count} workgroup variables (one shared memory array expected)");
|
||||
uint sharedVar = shared[0].W[1];
|
||||
uint pSharedUint = Ptr(ScWorkgroup, tUint);
|
||||
var entry = ins.Single(i => i.Op == OpEntryPoint);
|
||||
if (entry.W[0] != ModelGLCompute) throw new Exception("mesh: the translator's entry point is not a compute one");
|
||||
uint mainFn = entry.W[1];
|
||||
uint glsl = Find(i => i.Op == OpExtInstImport && Str("GLSL.std.450").SequenceEqual(i.W.Skip(1)));
|
||||
var preamble = new List<Insn>(); // capability, extension, import: inserted after the last capability
|
||||
if (glsl == 0) { glsl = Id(); preamble.Add(new Insn(OpExtInstImport, new[] { glsl }.Concat(Str("GLSL.std.450")))); }
|
||||
|
||||
uint pInputUint = Ptr(ScInput, tUint);
|
||||
uint lii = Var(pInputUint, ScInput, BiLocalInvocationIndex);
|
||||
int payloadWords = m.PayloadBytes / 4;
|
||||
uint payloadVar = 0, pPayloadUint = 0;
|
||||
if (m.Stage == 6 || m.HasTask)
|
||||
{
|
||||
if (payloadWords == 0) throw new Exception("mesh: a task payload of size 0");
|
||||
payloadVar = Var(Ptr(ScTaskPayload, Arr(tUint, (uint)payloadWords)), ScTaskPayload);
|
||||
pPayloadUint = Ptr(ScTaskPayload, tUint);
|
||||
}
|
||||
|
||||
// ---- the entry function body
|
||||
var body = new List<Insn>();
|
||||
uint Emit(int op, uint type, params uint[] operands) { uint r = Id(); body.Add(new Insn(op, new[] { type, r }.Concat(operands))); return r; }
|
||||
void Do(int op, params uint[] operands) => body.Add(new Insn(op, operands));
|
||||
uint Label() { uint l = Id(); Do(OpLabel, l); return l; }
|
||||
uint Add(uint a, uint b) => Emit(OpIAdd, tUint, a, b);
|
||||
uint SharedWord(uint index) => Emit(OpLoad, tUint, Emit(OpAccessChain, pSharedUint, sharedVar, index));
|
||||
void Barrier() => Do(OpControlBarrier, C(2), C(2), C(0x108)); // Workgroup, Workgroup, AcquireRelease | WorkgroupMemory
|
||||
uint UMin(uint a, uint b) => Emit(OpExtInst, tUint, glsl, GlslUMin, a, b);
|
||||
// if (a < b) { then() }
|
||||
void If(uint a, uint b, Action then)
|
||||
{
|
||||
uint c = Emit(OpULessThan, tBool, a, b), t = Id(), merge = Id();
|
||||
Do(OpSelectionMerge, merge, 0);
|
||||
Do(OpBranchConditional, c, t, merge);
|
||||
Do(OpLabel, t);
|
||||
then();
|
||||
Do(OpBranch, merge);
|
||||
Do(OpLabel, merge);
|
||||
}
|
||||
// Words i, i + threads, ... below words of shared memory from word sharedAt <-> the payload variable.
|
||||
void CopyPayload(uint tid, bool toShared)
|
||||
{
|
||||
int threads = Math.Max(1, m.Threads);
|
||||
for (int k = 0; k * threads < payloadWords; k++)
|
||||
{
|
||||
uint idx = k == 0 ? tid : Add(tid, C((uint)(k * threads)));
|
||||
void copy()
|
||||
{
|
||||
uint pp = Emit(OpAccessChain, pPayloadUint, payloadVar, idx);
|
||||
uint sp = Emit(OpAccessChain, pSharedUint, sharedVar, Add(idx, C((uint)(m.Pay / 4))));
|
||||
if (toShared) Do(OpStore, sp, Emit(OpLoad, tUint, pp));
|
||||
else Do(OpStore, pp, Emit(OpLoad, tUint, sp));
|
||||
}
|
||||
if ((k + 1) * threads > payloadWords) If(idx, C((uint)payloadWords), copy);
|
||||
else copy();
|
||||
}
|
||||
}
|
||||
|
||||
uint entryFn = Id();
|
||||
body.Add(new Insn(OpFunction, new[] { tVoid, entryFn, 0u, tFnVoid }));
|
||||
Label();
|
||||
uint tid = Emit(OpLoad, tUint, lii);
|
||||
if (m.Stage == 7 && m.HasTask) { CopyPayload(tid, true); Barrier(); }
|
||||
Emit(OpFunctionCall, tVoid, mainFn);
|
||||
Barrier();
|
||||
var modes = new List<Insn>();
|
||||
if (m.Stage == 6)
|
||||
{
|
||||
uint count = SharedWord(C((uint)(m.Pay + 4) / 4));
|
||||
CopyPayload(tid, false);
|
||||
Do(OpEmitMeshTasksEXT, count, C(1), C(1), payloadVar);
|
||||
}
|
||||
else
|
||||
{
|
||||
int maxV = m.MaxVertices, maxP = m.MaxPrimitives, ipp = m.IndicesPerPrimitive, threads = Math.Max(1, m.Threads);
|
||||
if (maxV <= 0 || maxP <= 0) throw new Exception("mesh: no vertex or primitive limit");
|
||||
// outputs
|
||||
uint tV4 = Ty(OpTypeVector, tFloat, 4);
|
||||
uint pOutFloat = Ptr(ScOutput, tFloat);
|
||||
uint posVar = 0, sizeVar = 0;
|
||||
var generic = new Dictionary<int, uint>();
|
||||
foreach (int a in m.Lines)
|
||||
{
|
||||
if (a >= 0x70 && a < 0x80 && posVar == 0) posVar = Var(Ptr(ScOutput, Arr(tV4, (uint)maxV)), ScOutput, BiPosition);
|
||||
else if (a == 0x6c && sizeVar == 0) sizeVar = Var(Ptr(ScOutput, Arr(tFloat, (uint)maxV)), ScOutput, BiPointSize);
|
||||
else if (a >= 0x80 && !generic.ContainsKey((a - 0x80) / 16))
|
||||
generic[(a - 0x80) / 16] = Var(Ptr(ScOutput, Arr(tV4, (uint)maxV)), ScOutput, -1, (a - 0x80) / 16);
|
||||
}
|
||||
uint tIdx = ipp == 1 ? tUint : Ty(OpTypeVector, tUint, (uint)ipp);
|
||||
int idxBuiltin = ipp == 1 ? BiPrimitivePointIndices : ipp == 2 ? BiPrimitiveLineIndices : BiPrimitiveTriangleIndices;
|
||||
uint idxVar = Var(Ptr(ScOutput, Arr(tIdx, (uint)maxP)), ScOutput, idxBuiltin);
|
||||
uint pOutIdx = Ptr(ScOutput, tIdx);
|
||||
|
||||
uint count = UMin(SharedWord(C((uint)m.Map / 4)), C((uint)maxP));
|
||||
Do(OpSetMeshOutputsEXT, C((uint)maxV), count);
|
||||
int lines = m.Lines.Count;
|
||||
for (int k = 0; k * threads < maxV; k++)
|
||||
{
|
||||
uint v = k == 0 ? tid : Add(tid, C((uint)(k * threads)));
|
||||
If(v, C((uint)maxV), () =>
|
||||
{
|
||||
// word ((v >> 5) * lines + line) * 32 + (v & 31) of the vertex area
|
||||
uint group = Emit(OpShiftRightLogical, tUint, v, C(5)), lane = Emit(OpBitwiseAnd, tUint, v, C(31));
|
||||
uint at = Add(Add(Emit(OpIMul, tUint, group, C((uint)(lines * 32))), lane), C((uint)m.Attr / 4));
|
||||
for (int l = 0; l < lines; l++)
|
||||
{
|
||||
int a = m.Lines[l];
|
||||
uint var = 0, comp = uint.MaxValue;
|
||||
if (a >= 0x70 && a < 0x80) (var, comp) = (posVar, (uint)(a - 0x70) / 4);
|
||||
else if (a == 0x6c) var = sizeVar;
|
||||
else if (a >= 0x80) (var, comp) = (generic[(a - 0x80) / 16], (uint)((a - 0x80) % 16) / 4);
|
||||
if (var == 0) continue; // layer, viewport index: per primitive in Vulkan, not written
|
||||
uint value = Emit(OpBitcast, tFloat, SharedWord(Add(at, C((uint)(l * 32)))));
|
||||
uint ptr = comp == uint.MaxValue ? Emit(OpAccessChain, pOutFloat, var, v) : Emit(OpAccessChain, pOutFloat, var, v, C(comp));
|
||||
Do(OpStore, ptr, value);
|
||||
}
|
||||
});
|
||||
}
|
||||
for (int k = 0; k * threads < maxP; k++)
|
||||
{
|
||||
uint p = k == 0 ? tid : Add(tid, C((uint)(k * threads)));
|
||||
If(p, count, () =>
|
||||
{
|
||||
// byte 4 + ipp * p + j of the primitive area
|
||||
uint first = Add(Emit(OpIMul, tUint, p, C((uint)ipp)), C((uint)m.Map + 4));
|
||||
var idx = new uint[ipp];
|
||||
for (int j = 0; j < ipp; j++)
|
||||
{
|
||||
uint b = j == 0 ? first : Add(first, C((uint)j));
|
||||
uint word = SharedWord(Emit(OpShiftRightLogical, tUint, b, C(2)));
|
||||
uint shift = Emit(OpIMul, tUint, Emit(OpBitwiseAnd, tUint, b, C(3)), C(8));
|
||||
uint value = Emit(OpBitwiseAnd, tUint, Emit(OpShiftRightLogical, tUint, word, shift), C(0xff));
|
||||
idx[j] = UMin(value, C((uint)maxV - 1));
|
||||
}
|
||||
uint composite = ipp == 1 ? idx[0] : Emit(OpCompositeConstruct, tIdx, idx);
|
||||
Do(OpStore, Emit(OpAccessChain, pOutIdx, idxVar, p), composite);
|
||||
});
|
||||
}
|
||||
Do(OpReturn);
|
||||
modes.Add(new Insn(OpExecutionMode, new[] { entryFn, (uint)ModeOutputVertices, (uint)maxV }));
|
||||
modes.Add(new Insn(OpExecutionMode, new[] { entryFn, (uint)ModeOutputPrimitives, (uint)maxP }));
|
||||
modes.Add(new Insn(OpExecutionMode, new[] { entryFn, (uint)(ipp == 1 ? ModeOutputPoints : ipp == 2 ? ModeOutputLines : ModeOutputTriangles) }));
|
||||
}
|
||||
body.Add(new Insn(OpFunctionEnd, Array.Empty<uint>()));
|
||||
|
||||
// ---- reassemble
|
||||
foreach (var i in ins.Where(i => i.Op == OpExecutionMode && i.W[0] == mainFn)) i.W[0] = entryFn;
|
||||
preamble.Insert(0, new Insn(OpExtension, Str("SPV_EXT_mesh_shader")));
|
||||
preamble.Insert(0, new Insn(OpCapability, new[] { (uint)CapMeshShading }));
|
||||
int lastCap = ins.FindLastIndex(i => i.Op == OpCapability);
|
||||
// the entry point: the new model and function, every global variable as its interface (SPIR-V 1.4)
|
||||
var name = Str("main");
|
||||
var globalVars = ins.Take(firstFunction).Where(i => i.Op == OpVariable && i.W[2] != ScFunction).Select(i => i.W[1])
|
||||
.Concat(globals.Where(i => i.Op == OpVariable).Select(i => i.W[1])).ToList();
|
||||
entry.W = new List<uint> { (uint)(m.Stage == 6 ? ModelTask : ModelMesh), entryFn }.Concat(name).Concat(globalVars).ToList();
|
||||
int lastMode = ins.FindLastIndex(i => i.Op == OpExecutionMode || i.Op == OpEntryPoint);
|
||||
int firstType = ins.FindIndex(i => i.Op >= OpTypeVoid && i.Op <= 39 || i.Op == OpConstant || i.Op == OpVariable);
|
||||
var result = new List<Insn>();
|
||||
for (int k = 0; k < ins.Count; k++)
|
||||
{
|
||||
if (k == firstType) result.AddRange(decorations);
|
||||
if (k == firstFunction) { result.AddRange(newTypes); result.AddRange(globals); }
|
||||
result.Add(ins[k]);
|
||||
if (k == lastCap) result.AddRange(preamble);
|
||||
if (k == lastMode) result.AddRange(modes);
|
||||
}
|
||||
result.AddRange(body);
|
||||
|
||||
var output = new List<uint> { 0x07230203, 0x00010400, words[2], bound, 0 };
|
||||
foreach (var i in result) { output.Add((uint)((i.W.Count + 1) << 16 | i.Op)); output.AddRange(i.W); }
|
||||
var bytes = new byte[output.Count * 4];
|
||||
Buffer.BlockCopy(output.ToArray(), 0, bytes, 0, bytes.Length);
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,9 @@
|
||||
// "ok <key>" or "fail <key> <message>"; a malformed line gets "fail ? <message>" and the loop continues.
|
||||
// nx2navi_xlate <jobs.json> <outdir> batch mode: a JSON array of jobs, prints a summary, exit 1 if any failed.
|
||||
// job: { "key", "stage": "vs|ps|cs", "code": path (graphics = 0x50-byte SPH + instructions, compute = instructions), "c1": hex,
|
||||
// "sm86": bool + "size": bytes (an NVN2 program, lowered by AmpereLowering.cs), "nvn2_table": [start, textures,
|
||||
// images] (NVN2 compute binding table), "lowered_out": path, "mesh": {"stage": 6|7, "threads", "maxv", "maxp",
|
||||
// "topo", "payload", "task"} (stage "ts" / "ms": an NVN2 task or mesh program, AmpereLowering.MeshInfo),
|
||||
// "tex": {slot: type}, "img": {slot: {type, fmt}}, "attr": {loc: float|sint|uint}, "fout": {idx: float|sint|uint},
|
||||
// "local": [x, y, z], "shared": bytes, "debug": bool, "reduced": bool, "softfloat64": bool, "zero_locals": bool,
|
||||
// "sbuf_align": int, "subgroup": int }
|
||||
@@ -37,13 +40,21 @@ static class SwshXlate
|
||||
// 全部资源放进第 0 组,各占不同绑定号(spirv-cross 出成一个参数缓冲);常量段号 index 的缓冲由翻译器给 binding = index+1 的惯例不依赖
|
||||
// All resources go into set 0, each with a distinct binding number (spirv-cross emits them as one argument buffer); the translator's convention of giving the buffer of constant segment number index binding = index+1 is not relied on
|
||||
public SetBindingPair CreateConstantBufferBinding(int index) => new(0, _binding++);
|
||||
public SetBindingPair CreateImageBinding(int count, bool isBuffer) { int b = _binding; _binding += count; return new(0, b); }
|
||||
// A pool array (bindless) takes one binding number: the runtime moves it to its own set (vk_pipeline.c
|
||||
// spirv_pool_rebind), and 32768 skipped numbers pushed the bindings after it past the runtime's 16-bit field.
|
||||
static bool PoolArray(int count) => count == TexturePoolLength || count == SamplerPoolLength;
|
||||
public SetBindingPair CreateImageBinding(int count, bool isBuffer) { int b = _binding; _binding += PoolArray(count) ? 1 : count; return new(0, b); }
|
||||
public SetBindingPair CreateStorageBufferBinding(int index) => new(0, _binding++);
|
||||
public SetBindingPair CreateTextureBinding(int count, bool isBuffer) { int b = _binding; _binding += count; return new(0, b); }
|
||||
public SetBindingPair CreateTextureBinding(int count, bool isBuffer) { int b = _binding; _binding += PoolArray(count) ? 1 : count; return new(0, b); }
|
||||
public ReadOnlySpan<ulong> GetCode(ulong address, int minimumSize) => MemoryMarshal.Cast<byte, ulong>(new ReadOnlySpan<byte>(_code)[(int)address..]);
|
||||
public int QuerySamplerArrayLengthFromPool() => 32;
|
||||
// Bindless handles loaded from memory index the whole texture and sampler pools (Ryujinx BindlessElimination
|
||||
// GenerateBindlessAccess): arrays of TexturePoolLength textures (handle bits 0-19) and SamplerPoolLength samplers
|
||||
// (bits 20-31; NVN sampler ids are below 4096). res.json marks them "pool": 1 (textures) / 2 (samplers); the
|
||||
// runtime binds them from its global pool set (vk_exec_draw.c, texture and sampler pools).
|
||||
public const int TexturePoolLength = 32768, SamplerPoolLength = 4096;
|
||||
public int QuerySamplerArrayLengthFromPool() => SamplerPoolLength;
|
||||
public int QueryTextureArrayLengthFromBuffer(int slot) => 32;
|
||||
public int QueryTextureArrayLengthFromPool() => 32;
|
||||
public int QueryTextureArrayLengthFromPool() => TexturePoolLength;
|
||||
|
||||
string Get(string table, int slot) => _job[table] is JsonObject t && t[slot.ToString()] is JsonNode n ? (n is JsonObject o ? (string)o["type"] : (string)n) : null;
|
||||
int TexSlot(int handle, int cbufSlot)
|
||||
@@ -68,9 +79,19 @@ static class SwshXlate
|
||||
_ => SamplerType.Texture2D,
|
||||
};
|
||||
}
|
||||
// Image handles: NVN1 c[2] 0x120 + 8 * slot (NVN2 compute handles are renumbered there by AmpereLowering.Handle);
|
||||
// TexSlot knows only the separate texture range, so image formats were never answered (an R32UI view of an
|
||||
// R11G11B10F target was declared float, and the raw words the shader packs were converted on store).
|
||||
int ImgSlot(int handle, int cbufSlot)
|
||||
{
|
||||
(int off0, _, _) = TextureHandle.UnpackOffsets(handle);
|
||||
int bytes = off0 * 4;
|
||||
return (cbufSlot < 0 || (cbufSlot & 0xffff) == 2) && bytes >= 0x120 && bytes < TexBase && (bytes - 0x120) % 8 == 0 ? (bytes - 0x120) / 8 : -1;
|
||||
}
|
||||
public TextureFormat QueryTextureFormat(int handle, int cbufSlot = -1)
|
||||
{
|
||||
int slot = TexSlot(handle, cbufSlot);
|
||||
int slot = ImgSlot(handle, cbufSlot);
|
||||
if (slot < 0) slot = TexSlot(handle, cbufSlot);
|
||||
if (slot >= 0) ImgQueried.Add(slot);
|
||||
string f = _job["img"] is JsonObject t && slot >= 0 && t[slot.ToString()] is JsonObject o ? (string)o["fmt"] : null;
|
||||
if (f == null) { Notes.Add($"图像句柄 {handle:X} 槽 {slot} 无 PC 格式,按 R8G8B8A8Unorm"); return TextureFormat.R8G8B8A8Unorm; }
|
||||
@@ -80,6 +101,8 @@ static class SwshXlate
|
||||
public int QueryComputeLocalSizeX() => Local(0);
|
||||
public int QueryComputeLocalSizeY() => Local(1);
|
||||
public int QueryComputeLocalSizeZ() => Local(2);
|
||||
// "local_mem": local memory bytes of a compute program (the lowering's spill area adds to it); else Ryujinx's 0x1000.
|
||||
public int QueryComputeLocalMemorySize() => _job["local_mem"] is JsonNode n ? (int)n : 0x1000;
|
||||
public int QueryComputeSharedMemorySize() => _job["shared"] is JsonNode n ? (int)n : 0; // 配对 PC 的 groupshared 声明之和 | sum of the paired PC shader's groupshared declarations
|
||||
static AttributeType Attr(string s) => s switch { "sint" => AttributeType.Sint, "uint" => AttributeType.Uint, _ => AttributeType.Float };
|
||||
public GpuGraphicsState QueryGraphicsState()
|
||||
@@ -134,13 +157,58 @@ static class SwshXlate
|
||||
try
|
||||
{
|
||||
byte[] code = File.ReadAllBytes((string)job["code"]);
|
||||
AmpereLowering.MeshInfo mesh = null;
|
||||
// "sm86": true: the code is an NVN2 (Switch 2) program as stored, "size" bytes long (control section +0x38);
|
||||
// it is lowered to Maxwell first (AmpereLowering.cs). "lowered_out": a path that receives the Maxwell code.
|
||||
if (job["sm86"] is JsonValue sm && sm.TryGetValue<bool>(out bool isSm86) && isSm86)
|
||||
{
|
||||
int size = job["size"] is JsonNode sz ? (int)sz : code.Length;
|
||||
AmpereLowering.C1Bytes = job["c1"] is JsonNode c1h ? Convert.FromHexString((string)c1h) : null;
|
||||
int[] table = job["nvn2_table"] is JsonArray t && t.Count == 3 ? new[] { (int)t[0], (int)t[1], (int)t[2] } : null;
|
||||
if (job["mesh"] is JsonObject mj)
|
||||
{
|
||||
// A task or mesh program: lowered and translated as a compute program whose shared memory holds
|
||||
// the stage buffer, then rewritten into a TaskEXT / MeshEXT module (MeshSpirv.cs).
|
||||
mesh = new AmpereLowering.MeshInfo
|
||||
{
|
||||
Stage = (int)mj["stage"], Threads = (int)mj["threads"], MaxVertices = (int)mj["maxv"],
|
||||
MaxPrimitives = (int)mj["maxp"], Topology = (int)mj["topo"], PayloadBytes = (int)mj["payload"],
|
||||
HasTask = mj["task"] is JsonValue tv && tv.TryGetValue<bool>(out bool tb) && tb,
|
||||
};
|
||||
code = AmpereLowering.Lower(code, size, false, out int meshLocal, null, mesh);
|
||||
if (meshLocal != 0) job["local_mem"] = meshLocal;
|
||||
job["local"] = new JsonArray(mesh.Threads, 1, 1);
|
||||
job["shared"] = mesh.SharedBytes;
|
||||
}
|
||||
else
|
||||
{
|
||||
code = AmpereLowering.Lower(code, size, (string)job["stage"] != "cs", out int local, table);
|
||||
if (local != 0) job["local_mem"] = local;
|
||||
}
|
||||
if (job["lowered_out"] is JsonNode lo) File.WriteAllBytes((string)lo, code);
|
||||
}
|
||||
var acc = new Accessor(code, job);
|
||||
// 作业 debug = false:关 DebugMode(DebugMode 会跳过 AST 优化器,产物满是临时变量;不改语义)
|
||||
// Job debug = false: turn off DebugMode (DebugMode skips the AST optimizer and the output is full of temporaries; semantics unchanged)
|
||||
var flags = (job["debug"] is JsonNode dbg && !(bool)dbg ? 0 : TranslationFlags.DebugMode) | ((string)job["stage"] == "cs" ? TranslationFlags.Compute : 0);
|
||||
var flags = (job["debug"] is JsonNode dbg && !(bool)dbg ? 0 : TranslationFlags.DebugMode) | ((string)job["stage"] is "cs" or "ts" or "ms" ? TranslationFlags.Compute : 0);
|
||||
var ctx = Translator.CreateContext(0, acc, new TranslationOptions(TargetLanguage.Spirv, TargetApi.Vulkan, flags));
|
||||
ShaderProgram p = ctx.Translate();
|
||||
File.WriteAllBytes(Path.Combine(outDir, key + ".spv"), p.BinaryCode);
|
||||
// "glsl_out": a path that receives the same program as GLSL, for reading (diagnosis only).
|
||||
if (job["glsl_out"] is JsonNode go)
|
||||
{
|
||||
var gctx = Translator.CreateContext(0, new Accessor(code, job), new TranslationOptions(TargetLanguage.Glsl, TargetApi.OpenGL, flags));
|
||||
File.WriteAllText((string)go, gctx.Translate().Code);
|
||||
}
|
||||
byte[] spirv = p.BinaryCode;
|
||||
// A recursive call graph (subroutines the translator could not tell apart) is refused: Vulkan forbids it
|
||||
// and NVIDIA's compiler overflows its stack on it (MeshSpirv.CallCycle).
|
||||
if (MeshSpirv.CallCycle(spirv) is long cyc && cyc >= 0) throw new Exception($"recursive call graph (function %{cyc})");
|
||||
if (mesh != null)
|
||||
{
|
||||
if (job["compute_out"] is JsonNode co) File.WriteAllBytes((string)co, spirv);
|
||||
spirv = MeshSpirv.Convert(spirv, mesh);
|
||||
}
|
||||
File.WriteAllBytes(Path.Combine(outDir, key + ".spv"), spirv);
|
||||
var res = new JsonObject
|
||||
{
|
||||
["key"] = key, ["stage"] = (string)job["stage"], ["notes"] = new JsonArray(acc.Notes.ConvertAll(n => (JsonNode)n).ToArray()),
|
||||
@@ -149,10 +217,14 @@ static class SwshXlate
|
||||
["tex_queried"] = new JsonArray(acc.TexQueried.Select(x => (JsonNode)x).ToArray()), ["img_queried"] = new JsonArray(acc.ImgQueried.Select(x => (JsonNode)x).ToArray()),
|
||||
};
|
||||
if (acc.QueryMotionPrevious()) (res["mvprev"], res["mvprev_banks"]) = (acc.MotionPreviousLocation, acc.MotionPreviousBanks);
|
||||
if (mesh != null)
|
||||
res["mesh"] = new JsonObject { ["lines"] = mesh.Lines.Count, ["shared"] = mesh.SharedBytes,
|
||||
["outputs"] = new JsonArray(MeshSpirv.GenericLocations(mesh).Select(x => (JsonNode)x).ToArray()) };
|
||||
foreach (var b in p.Info.CBuffers) res["cbufs"].AsArray().Add((JsonNode)new JsonObject { ["binding"] = b.Binding, ["slot"] = b.Slot });
|
||||
foreach (var b in p.Info.SBuffers) res["sbufs"].AsArray().Add((JsonNode)new JsonObject { ["binding"] = b.Binding, ["slot"] = b.Slot, ["cb_slot"] = b.SbCbSlot, ["cb_offset"] = b.SbCbOffset, ["flags"] = (int)b.Flags });
|
||||
foreach (var (list, name) in new[] { (p.Info.Textures, "textures"), (p.Info.Images, "images") })
|
||||
foreach (var t in list) res[name].AsArray().Add((JsonNode)new JsonObject { ["binding"] = t.Binding, ["type"] = (int)t.Type, ["format"] = t.Format.ToString(), ["cbuf_slot"] = t.CbufSlot, ["handle"] = t.HandleIndex, ["array"] = t.ArrayLength, ["separate"] = t.Separate, ["flags"] = (int)t.Flags });
|
||||
foreach (var t in list) res[name].AsArray().Add((JsonNode)new JsonObject { ["binding"] = t.Binding, ["type"] = (int)t.Type, ["format"] = t.Format.ToString(), ["cbuf_slot"] = t.CbufSlot, ["handle"] = t.HandleIndex, ["array"] = t.ArrayLength, ["separate"] = t.Separate, ["flags"] = (int)t.Flags,
|
||||
["pool"] = t.ArrayLength == Accessor.TexturePoolLength ? 1 : t.ArrayLength == Accessor.SamplerPoolLength ? 2 : 0 });
|
||||
File.WriteAllText(Path.Combine(outDir, key + ".res.json"), res.ToJsonString(new JsonSerializerOptions { WriteIndented = true }));
|
||||
return null;
|
||||
}
|
||||
|
||||
118
tools/xlate/patches/0017-goto-elimination-outer-loop-exits.patch
Normal file
118
tools/xlate/patches/0017-goto-elimination-outer-loop-exits.patch
Normal 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);
|
||||
|
||||
22
tools/xlate/patches/0018-integer-image-formats.patch
Normal file
22
tools/xlate/patches/0018-integer-image-formats.patch
Normal 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);
|
||||
27
tools/xlate/patches/0019-fragment-depth-register.patch
Normal file
27
tools/xlate/patches/0019-fragment-depth-register.patch
Normal 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)
|
||||
18
tools/xlate/patches/0020-storage-base-not-from-c1.patch
Normal file
18
tools/xlate/patches/0020-storage-base-not-from-c1.patch
Normal 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))
|
||||
Reference in New Issue
Block a user