diff --git a/runtime/gfx.h b/runtime/gfx.h index 2fb8f82e..dc0c714a 100644 --- a/runtime/gfx.h +++ b/runtime/gfx.h @@ -99,6 +99,8 @@ typedef struct { uint16_t count; // descriptors (array length) uint8_t separate; // TEX: texture and sampler handles are separate uint8_t pool; // TEX: 1 the texture pool, 2 the sampler pool, indexed by bindless handles (set 2) + uint16_t ptr; // SSBO: 0, or 1 + the byte offset of this buffer's address inside the storage buffer that + // cbuf_slot and handle name (a pointer table; the translator's patch 0022, res.json "ptr") } GfxShaderRes; typedef struct { uint8_t type, comps, packed; } GfxShaderIo; // GFX_TYPE_*, components, 1 with a Component decoration int gfx_shader_stage(const GfxShader *s); // GFX_STAGE_VS, GFX_STAGE_FS or GFX_STAGE_CS diff --git a/runtime/gfx_shader.c b/runtime/gfx_shader.c index c1e98d18..1bdc4f2f 100644 --- a/runtime/gfx_shader.c +++ b/runtime/gfx_shader.c @@ -716,6 +716,7 @@ static void add_res(GfxShader *s, int kind, const char *o, const char *e) { r->cbuf_slot = (int8_t)json_int(o, e, "cb_slot", -1); r->handle = (uint16_t)json_int(o, e, "cb_offset", 0); r->flags = (uint8_t)json_int(o, e, "flags", 0); + r->ptr = (uint16_t)(json_int(o, e, "ptr", -1) + 1); } return; } diff --git a/runtime/vk_dump.c b/runtime/vk_dump.c index 316fbc8c..dba648fe 100644 --- a/runtime/vk_dump.c +++ b/runtime/vk_dump.c @@ -576,6 +576,7 @@ static void describe_bindings(Str *b, const VkExec *ex, const VkPipeInfo *info) if (r && bd->kind == GFX_RES_SSBO) { // where the address lives (c0: the stage's NVN slot table) and the stage's slots int st = nvn_stage(bd->stage); sp(b, " %s ssbo at c%d+%#x%s, bound:", stn[st], r->cbuf_slot, r->handle * 4, r->flags & 1 ? " written" : ""); + if (r->ptr) sp(b, " (address at +%#x of that buffer)", r->ptr - 1u); if (st == 5 && r->cbuf_slot == 0 && r->handle * 4 >= 0x10 && r->handle * 4 + 12 <= 0x10 + (int)sizeof ex->cs_push) { uint64_t pa; uint32_t ps; // an NVN2 compute push-constant pointer {address, size} memcpy(&pa, ex->cs_push + r->handle * 4 - 0x10, 8), memcpy(&ps, ex->cs_push + r->handle * 4 - 0x10 + 8, 4); diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 470b4f74..c4b59455 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -169,7 +169,7 @@ static struct { // handles. sup and c0: the support inputs and the base instance count too. p: storage buffers whose address the shader // loads from a user uniform block; the address read from the block counts too. ok 0: never skipped (too many // descriptors or such buffers). -#define RS_PTRS 4 +#define RS_PTRS 32 typedef struct { uint32_t off, len; } RsRange; typedef struct { int st; const GfxShaderRes *res; } RsPtr; typedef struct { uint32_t n, bytes; uint8_t sup, c0, ok, np; RsPtr p[RS_PTRS]; RsRange r[]; } RsList; @@ -1229,24 +1229,50 @@ static int push_ptr(const VkExec *ex, int st, const GfxShaderRes *r, p_bind_buf return 0; } -// vk_dump.c: the guest address a compute storage resource resolves to through c0 (push_ptr), 0 when none. +// The buffer a storage resource names by where its address lives (GfxShaderRes.ptr aside): an NVN slot of the stage, a +// pointer in a user uniform block (size 0: up to the end of the backing), or an NVN2 compute push pointer. *tmp holds +// the last two. NULL when it names none. +static const p_bind_buf *ssbo_direct(const VkExec *ex, int st, const GfxShaderRes *r, int *ubo, p_bind_buf *tmp) { + int slot = ssbo_slot(r, st, ubo); + *tmp = (p_bind_buf){0}; + if (slot >= 0 && slot < 16) return *ubo ? &ex->ubo[st][slot] : &ex->ssbo[st][slot]; + if (ubo_ptr_res(r)) return tmp->addr = ubo_ptr(ex, st, r), tmp; + return push_ptr(ex, st, r, tmp) ? tmp : NULL; +} + +// The address of a storage resource read through a pointer table (GfxShaderRes.ptr): the u64 at offset ptr - 1 of the +// buffer it names, as that buffer holds it at this point of the replay (like ubo_ptr); 0 when the buffer is unbound, +// too short or not backed. Ocarina binds such a table as NVN storage slot 1 of most stages (five pointers at 0-0x20) +// and its vertex, pixel and mesh shaders read their instance and light data through them (PORT-HANDOFF.md 7.17). +static uint64_t ptr_addr(const VkExec *ex, int st, const GfxShaderRes *r) { + p_bind_buf tmp; + int ubo; + const p_bind_buf *b = ssbo_direct(ex, st, r, &ubo, &tmp); + uint64_t off = r->ptr - 1u, a = 0; + VkMemSpan sp; + if (!b || !b->addr || (b->size && b->size < off + 8) || upd_read(ex, b->addr + off, &a, 8)) return a; + if (vk_mem_resolve(b->addr + off, 8, &sp)) memcpy(&a, (const void *)(uintptr_t)(b->addr + off), 8); + return a; +} + +// vk_dump.c: the guest address a compute storage resource resolves to, 0 when none. uint64_t vk_exec_cs_storage_addr(const VkExec *ex, const GfxShaderRes *r) { - p_bind_buf up = {0}; - int ubo, slot = ssbo_slot(r, NVN_STAGE_CS, &ubo); - if (slot >= 0 && slot < 16) return ubo ? ex->ubo[NVN_STAGE_CS][slot].addr : ex->ssbo[NVN_STAGE_CS][slot].addr; - return push_ptr(ex, NVN_STAGE_CS, r, &up) ? up.addr : 0; + p_bind_buf tmp; + int ubo; + if (r->ptr) return ptr_addr(ex, NVN_STAGE_CS, r); + const p_bind_buf *b = ssbo_direct(ex, NVN_STAGE_CS, r, &ubo, &tmp); + return b ? b->addr : 0; } static VkDescriptorBufferInfo ssbo_info(VkExec *ex, int st, const GfxShaderRes *r, int written) { if (!__atomic_load_n(&LIM.ubo_max, __ATOMIC_ACQUIRE)) limits_init(); uint64_t max = LIM.sbo_max, align = LIM.sbo_align; VkDescriptorBufferInfo bi; - int ubo, slot = ssbo_slot(r, st, &ubo); + int ubo = 0; p_bind_buf up = {0}; - const p_bind_buf *b = NULL; - if (slot >= 0 && slot < 16) b = ubo ? &ex->ubo[st][slot] : &ex->ssbo[st][slot]; - else if (ubo_ptr_res(r)) up.addr = ubo_ptr(ex, st, r), b = &up; // size 0: up to the end of the backing - else if (push_ptr(ex, st, r, &up)) b = &up; + const p_bind_buf *b; + if (r->ptr) up.addr = ptr_addr(ex, st, r), b = &up; // size 0: up to the end of the backing + else b = ssbo_direct(ex, st, r, &ubo, &up); if (b) { // vk_caps.c: shaders translated for sbuf_align address the buffer from its address rounded down to it. uint64_t lead = vk_caps.sbuf_align ? b->addr & (vk_caps.sbuf_align - 1) : 0; @@ -2287,12 +2313,11 @@ static const RsList *rs_list(const VkExec *ex, const VkPipeInfo *info) { else if (slot >= 3 && slot - 3 < 18) rs_add(tmp, &n, ex, &ex->ubo[st][slot - 3], sizeof(p_bind_buf)); } // slot 1: the program (bind_sig); others: the zero buffer } else if (b->kind == GFX_RES_SSBO) { // every descriptor of the binding is the same range - int ubo, slot = ssbo_slot(r, st, &ubo); - if (slot >= 0 && slot < 16) { - rs_add(tmp, &n, ex, ubo ? &ex->ubo[st][slot] : &ex->ssbo[st][slot], sizeof(p_bind_buf)); - } else if (ubo_ptr_res(r)) { + int ubo, slot = ssbo_slot(r, st, &ubo), direct = slot >= 0 && slot < 16; + if (direct) rs_add(tmp, &n, ex, ubo ? &ex->ubo[st][slot] : &ex->ssbo[st][slot], sizeof(p_bind_buf)); + else if (ubo_ptr_res(r)) rs_add(tmp, &n, ex, &ex->ubo[st][r->cbuf_slot - 3], sizeof(p_bind_buf)); + if (r->ptr || (!direct && ubo_ptr_res(r))) { // an address read from memory: bind_sig reads it again if (np == RS_PTRS) { ok = 0; break; } - rs_add(tmp, &n, ex, &ex->ubo[st][r->cbuf_slot - 3], sizeof(p_bind_buf)); ptr[np++] = (RsPtr){st, r}; } } else if ((b->kind == GFX_RES_TEX || b->kind == GFX_RES_IMG) && b->type != VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER && @@ -2345,7 +2370,8 @@ static int bind_sig(const VkExec *ex, const VkPipeInfo *info) { PUT(ps); } if (l->c0) PUT(c0_binst); - for (int i = 0; i < l->np; i++) PUT(ubo_ptr(ex, l->p[i].st, l->p[i].res)); + for (int i = 0; i < l->np; i++) + PUT(l->p[i].res->ptr ? ptr_addr(ex, l->p[i].st, l->p[i].res) : ubo_ptr(ex, l->p[i].st, l->p[i].res)); #undef PUT diff |= n != RS.hn; RS.hn = n; diff --git a/tools/build_xlate.py b/tools/build_xlate.py index b1bbec6b..3cfa6b86 100644 --- a/tools/build_xlate.py +++ b/tools/build_xlate.py @@ -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 and 0015-0020 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-0022 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) != 20: - sys.exit(f"expected 20 patches, found {len(patches)}") + if len(patches) != 21: + sys.exit(f"expected 21 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"], []): diff --git a/tools/xlate/AmpereLowering.cs b/tools/xlate/AmpereLowering.cs index ac5e6fbd..b055c72c 100644 --- a/tools/xlate/AmpereLowering.cs +++ b/tools/xlate/AmpereLowering.cs @@ -838,8 +838,11 @@ static class AmpereLowering ulong iscadd(int dst, int bb) => 0x5c18000000000000UL | ((ulong)shift << 39) | ((ulong)bb << 20) | ((ulong)a << 8) | (uint)dst | (negB ? 1UL << 48 : 0); if (carry == 7) { EmitG(iscadd(d, b)); return; } if (negB) throw Fail("LEA carry out with negated b"); - Mov(SX, b); - EmitG(iscadd(SC, SX)); + // The sum goes to scratch unconditionally; only the destination and the carry are guarded. A guarded + // copy of b made a constant-buffer base (c[0][0x110] in Ocarina's edge-AA tile list, prog 98) a phi + // of it and the scratch register's old value, and GlobalToStorage could not find the buffer. + Mov(SX, b, false); + Emit(Unguarded(iscadd(SC, SX))); if (d != RZ) Mov(d, SC); IsetpLtU((int)carry, SC, SX); // carry = sum < b return; diff --git a/tools/xlate/Program.cs b/tools/xlate/Program.cs index f451c27d..92221f37 100644 --- a/tools/xlate/Program.cs +++ b/tools/xlate/Program.cs @@ -225,7 +225,18 @@ static class SwshXlate 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 b in p.Info.SBuffers) + { + // A buffer whose address is a pointer stored in another storage buffer (patch 0022, GlobalToStorage + // PointerSlotFlag): cb_slot 0x20 | the slot of the outer buffer's address, cb_offset its word offset in + // bits 0-9 and the pointer's byte offset in the outer buffer / 8 in bits 10-15. Written as the outer + // buffer's cb_slot / cb_offset plus "ptr", the byte offset. + bool pointer = (b.SbCbSlot & 0x20) != 0; + var o = new JsonObject { ["binding"] = b.Binding, ["slot"] = b.Slot, ["cb_slot"] = pointer ? b.SbCbSlot & 0x1f : b.SbCbSlot, + ["cb_offset"] = pointer ? b.SbCbOffset & 0x3ff : b.SbCbOffset, ["flags"] = (int)b.Flags }; + if (pointer) o["ptr"] = (b.SbCbOffset >> 10) * 8; + res["sbufs"].AsArray().Add((JsonNode)o); + } 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, ["pool"] = t.ArrayLength == Accessor.TexturePoolLength ? 1 : t.ArrayLength == Accessor.SamplerPoolLength ? 2 : 0 }); diff --git a/tools/xlate/patches/0022-global-memory-through-pointers.patch b/tools/xlate/patches/0022-global-memory-through-pointers.patch new file mode 100644 index 00000000..cae725d2 --- /dev/null +++ b/tools/xlate/patches/0022-global-memory-through-pointers.patch @@ -0,0 +1,403 @@ +--- a/src/Ryujinx.Graphics.Shader/Translation/Optimizations/GlobalToStorage.cs ++++ b/src/Ryujinx.Graphics.Shader/Translation/Optimizations/GlobalToStorage.cs +@@ -160,6 +160,34 @@ + } + } + ++ // NX2NAVI: 32-bit words the program loaded from a storage buffer at a constant byte offset (the halves of a ++ // pointer the buffer holds: Ocarina's NVN2 shaders read pointer tables through c0), and the local or shared ++ // memory words such a value was spilled to. FindBaseInAddTree takes them as buffer bases. ++ public readonly Dictionary PointerWords = []; ++ private readonly Dictionary _pointerMemory = []; ++ ++ // A store of a pointer word, of one reloaded from memory (the spills of tools/xlate/AmpereLowering.cs store ++ // every reloaded register back), or of a sum with exactly one such term (a pointer advanced by an index, as ++ // IMAD.WIDE leaves it) maps the memory word to that pointer; any other store unmaps it. ++ public void NotePointerStore(LsMemoryType type, Operand baseOffset, int constOffset, Operand value) ++ { ++ LsKey key = new(baseOffset, constOffset, type); ++ ++ if (TryGetPointerSum(this, value, out PointerWord word)) ++ { ++ _pointerMemory[key] = word; ++ } ++ else ++ { ++ _pointerMemory.Remove(key); ++ } ++ } ++ ++ public bool TryGetPointerMemory(LsMemoryType type, Operand baseOffset, int constOffset, out PointerWord word) ++ { ++ return _pointerMemory.TryGetValue(new LsKey(baseOffset, constOffset, type), out word); ++ } ++ + public bool TryGetMemoryTargetCb(LsMemoryType type, Operand baseOffset, int constOffset, out SearchResult result) + { + LsKey key = new(baseOffset, constOffset, type); +@@ -182,6 +210,10 @@ + } + } + ++ // NX2NAVI: a word loaded from the storage buffer whose address is at c[Slot][Offset] (word offset), ByteOffset ++ // bytes into that buffer. ++ private readonly record struct PointerWord(int Slot, int Offset, int ByteOffset); ++ + private readonly struct SearchResult + { + public static SearchResult NotFound => new(-1, 0); +@@ -190,6 +222,8 @@ + public int SbCbOffset { get; } + public Operand Offset { get; } + public int ConstOffset { get; } ++ // NX2NAVI: the base address when it is not c[SbCbSlot][SbCbOffset] (a pointer loaded from memory). ++ public Operand Base { get; init; } + + public SearchResult(int sbCbSlot, int sbCbOffset) + { +@@ -273,6 +307,8 @@ + uint targetCb = PackCbSlotAndOffset(result.SbCbSlot, result.SbCbOffset); + gtsContext.AddMemoryTargetCb(type, baseOffset, constOffset, targetCb, result); + } ++ ++ gtsContext.NotePointerStore(type, baseOffset, constOffset, value); + } + } + } +@@ -308,6 +344,11 @@ + Operand globalAddress = operation.GetSource(0); + SearchResult result = FindUniqueBaseAddressCb(gtsContext, block, globalAddress, needsOffset: true); + ++ if (!result.Found && globalAddress.AsgOp is not PhiNode) ++ { ++ result = FindBaseInAddTree(gtsContext, resourceManager, gpuAccessor, node, operation); ++ } ++ + if (result.Found) + { + // We found the storage buffer that is being accessed. +@@ -317,11 +358,21 @@ + + Operand offset = result.Offset; + ++ // NX2NAVI: a 32-bit load at a constant offset of a buffer whose address is in a constant buffer may be ++ // half of a pointer stored there; FindBaseInAddTree takes such words as bases of later accesses. ++ if (operation.Inst == Instruction.Load && operation.StorageKind == StorageKind.GlobalMemory && ++ operation.Dest != null && result.Base == null && result.SbCbSlot < PointerSlotFlag && ++ offset != null && offset.Type == OperandType.Constant) ++ { ++ gtsContext.PointerWords[operation.Dest] = ++ new PointerWord(result.SbCbSlot, result.SbCbOffset, offset.Value + result.ConstOffset); ++ } ++ + bool storageUnaligned = gpuAccessor.QueryHasUnalignedStorageBuffer(); + + if (storageUnaligned) + { +- Operand baseAddress = Cbuf(result.SbCbSlot, result.SbCbOffset); ++ Operand baseAddress = result.Base ?? Cbuf(result.SbCbSlot, result.SbCbOffset); + + Operand baseAddressMasked = Local(); + Operand hostOffset = Local(); +@@ -655,7 +706,7 @@ + + if (targetCbs.Count == 0) + { +- gpuAccessor.Log($"Failed to find storage buffer for global memory operation \"{operation.Inst}\"."); ++ gpuAccessor.Log($"Failed to find storage buffer for global memory operation \"{operation.Inst}\" at {DescribeAddress(operation.GetSource(0), 5)}."); + } + + if (gtsContext.TryGetFunctionId(operation, isMultiTarget: true, targetCbs, out functionId)) +@@ -696,7 +747,10 @@ + + Operand inRangeHigh = context.ICompareEqual(addrHighBorrowed, baseAddrHigh); + +- Operand inRange = context.BitwiseAnd(inRangeLow, inRangeHigh); ++ // NX2NAVI: with one candidate the word after the address is not needed to tell buffers apart, and it is ++ // not always a size: an NVN2 compute program's pushed constants hold bare pointers, which the host binds ++ // up to the end of their memory (runtime/vk_exec_draw.c push_ptr). Robust buffer access bounds the rest. ++ Operand inRange = targetCbs.Count == 1 ? inRangeHigh : context.BitwiseAnd(inRangeLow, inRangeHigh); + + Operand lblSkip = Label(); + context.BranchIfFalse(lblSkip, inRange); +@@ -774,6 +828,33 @@ + return true; + } + ++ // NX2NAVI: the expression an unresolved address comes from, for the note (diagnosis only). ++ private static string DescribeAddress(Operand op, int depth) ++ { ++ switch (op.Type) ++ { ++ case OperandType.Constant: return $"0x{op.Value:x}"; ++ case OperandType.ConstantBuffer: return $"c{op.GetCbufSlot()}[0x{op.GetCbufOffset() * 4:x}]"; ++ case OperandType.LocalVariable: break; ++ default: return op.Type.ToString(); ++ } ++ if (depth == 0) return "_"; ++ if (op.AsgOp is PhiNode phi) ++ { ++ HashSet parts = []; ++ for (int i = 0; i < phi.SourcesCount && parts.Count < 4; i++) parts.Add(DescribeAddress(phi.GetSource(i), depth - 1)); ++ return $"phi({string.Join(",", parts)})"; ++ } ++ if (op.AsgOp is Operation o) ++ { ++ string name = o.Inst == Instruction.Load ? $"ld.{o.StorageKind}" : o.Inst.ToString(); ++ List args = []; ++ for (int i = 0; i < o.SourcesCount; i++) args.Add(DescribeAddress(o.GetSource(i), depth - 1)); ++ return $"{name}({string.Join(",", args)})"; ++ } ++ return "undef"; ++ } ++ + private static uint PackCbSlotAndOffset(int cbSlot, int cbOffset) + { + return (uint)((ushort)cbSlot | ((ushort)cbOffset << 16)); +@@ -1065,6 +1146,246 @@ + return GetBaseAddressCbWithOffset(src1, src2, constOffset); + } + ++ // NX2NAVI: storage buffers whose address is a pointer the program loaded from another storage buffer get a slot ++ // of their own: SbCbSlot is PointerSlotFlag | the slot of the outer buffer's address, SbCbOffset its word offset ++ // (bits 0-9) | the pointer's byte offset in the outer buffer / 8 (bits 10-15). The host reads the pointer from ++ // the outer buffer when it binds the stage (tools/xlate/Program.cs writes the slot as "ptr" in res.json). ++ public const int PointerSlotFlag = 0x20; ++ ++ private static Operand SkipCopies(Operand op) ++ { ++ while (op.AsgOp is Operation copy && copy.Inst == Instruction.Copy) ++ { ++ op = copy.GetSource(0); ++ } ++ ++ return op; ++ } ++ ++ // The terms of an integer sum (through Add and Copy), at most 16; false when there are more. ++ private static bool AddTerms(Operand op, List terms) ++ { ++ op = SkipCopies(op); ++ ++ if (op.AsgOp is Operation add && add.Inst == Instruction.Add) ++ { ++ return AddTerms(add.GetSource(0), terms) && AddTerms(add.GetSource(1), terms); ++ } ++ ++ terms.Add(op); ++ ++ return terms.Count <= 16; ++ } ++ ++ // A term of an address that can be its base: a 64-bit-aligned constant buffer word outside c1 (patch 0020 says ++ // why), or the low half of a pointer loaded from a storage buffer (directly or back from where it was spilled). ++ private static bool TryGetBaseTerm(GtsContext gtsContext, Operand term, out PointerWord word) ++ { ++ return TryGetBaseTerm(gtsContext, term, out word, null); ++ } ++ ++ // A phi is a base term when all its sources that are defined are the same one (phis it reaches again are ++ // part of a loop and say nothing): a pointer loaded in a branch or before a loop. ++ private static bool TryGetBaseTerm(GtsContext gtsContext, Operand term, out PointerWord word, HashSet visited) ++ { ++ if (term.AsgOp is PhiNode phi) ++ { ++ visited ??= []; ++ visited.Add(phi); ++ bool any = false; ++ word = default; ++ ++ for (int i = 0; i < phi.SourcesCount; i++) ++ { ++ Operand source = SkipCopies(phi.GetSource(i)); ++ ++ if (source.Type == OperandType.Undefined || (source.AsgOp is PhiNode seen && visited.Contains(seen))) ++ { ++ continue; ++ } ++ ++ if (visited.Count > 16 || !TryGetBaseTerm(gtsContext, source, out PointerWord sourceWord, visited) || ++ (any && sourceWord != word)) ++ { ++ return false; ++ } ++ ++ word = sourceWord; ++ any = true; ++ } ++ ++ return any; ++ } ++ ++ if (term.Type == OperandType.ConstantBuffer) ++ { ++ word = new PointerWord(term.GetCbufSlot(), term.GetCbufOffset(), -1); ++ ++ return (word.Offset & 1) == 0 && word.Slot != 1; ++ } ++ ++ if (gtsContext.PointerWords.TryGetValue(term, out word)) ++ { ++ return true; ++ } ++ ++ return term.AsgOp is Operation load && ++ load.Inst == Instruction.Load && ++ TryGetMemoryOffsets(load, out LsMemoryType type, out Operand baseOffset, out int constOffset) && ++ gtsContext.TryGetPointerMemory(type, baseOffset, constOffset, out word); ++ } ++ ++ private static bool TryGetPointerSum(GtsContext gtsContext, Operand value, out PointerWord word) ++ { ++ List terms = []; ++ bool found = false; ++ word = default; ++ ++ if (!AddTerms(value, terms)) ++ { ++ return false; ++ } ++ ++ foreach (Operand term in terms) ++ { ++ if (TryGetBaseTerm(gtsContext, term, out PointerWord candidate) && candidate.ByteOffset >= 0) ++ { ++ if (found) ++ { ++ return false; ++ } ++ ++ found = true; ++ word = candidate; ++ } ++ } ++ ++ return found; ++ } ++ ++ private static bool IsHighTermOf(GtsContext gtsContext, Operand term, PointerWord low) ++ { ++ if (low.ByteOffset < 0) ++ { ++ return term.Type == OperandType.ConstantBuffer && ++ term.GetCbufSlot() == low.Slot && ++ term.GetCbufOffset() == low.Offset + 1; ++ } ++ ++ return TryGetBaseTerm(gtsContext, term, out PointerWord high) && ++ high.Slot == low.Slot && ++ high.Offset == low.Offset && ++ high.ByteOffset == low.ByteOffset + 4; ++ } ++ ++ // NX2NAVI: the base of an address that FindUniqueBaseAddressCb does not see: its low word is a sum with exactly one ++ // term that can be a base (TryGetBaseTerm), anywhere in the sum (Ocarina's compute shaders add c0[0x110] to a ++ // sum of indices; its pointer tables give vertex, pixel and mesh shaders addresses like ptr + index * 16), and the ++ // high word is a sum with that base's high word as a term (an index or offset that happens to sit in c0 is not ++ // taken for a base). Inserts the offset from the base before the access. ++ private static SearchResult FindBaseInAddTree( ++ GtsContext gtsContext, ++ ResourceManager resourceManager, ++ IGpuAccessor gpuAccessor, ++ LinkedListNode node, ++ Operation operation) ++ { ++ List terms = []; ++ ++ if (operation.SourcesCount < 2 || !AddTerms(operation.GetSource(0), terms)) ++ { ++ return SearchResult.NotFound; ++ } ++ ++ List highTerms = []; ++ ++ if (!AddTerms(operation.GetSource(1), highTerms)) ++ { ++ return SearchResult.NotFound; ++ } ++ ++ Operand baseTerm = null; ++ PointerWord word = default; ++ ++ foreach (Operand term in terms) ++ { ++ if (TryGetBaseTerm(gtsContext, term, out PointerWord candidate) && ++ highTerms.Exists(high => IsHighTermOf(gtsContext, high, candidate))) ++ { ++ if (baseTerm != null) ++ { ++ return SearchResult.NotFound; ++ } ++ ++ baseTerm = term; ++ word = candidate; ++ } ++ } ++ ++ if (baseTerm == null) ++ { ++ return SearchResult.NotFound; ++ } ++ ++ int sbCbSlot = word.Slot; ++ int sbCbOffset = word.Offset; ++ ++ if (word.ByteOffset >= 0) ++ { ++ if (word.Slot >= PointerSlotFlag || word.Offset >= 1 << 10 || (word.ByteOffset & 7) != 0 || word.ByteOffset >= 64 * 8) ++ { ++ gpuAccessor.Log($"Pointer at byte {word.ByteOffset} of the buffer at c{word.Slot}[0x{word.Offset * 4:x}] has no storage buffer slot."); ++ ++ return SearchResult.NotFound; ++ } ++ ++ if (!resourceManager.TryGetStorageBufferBinding(word.Slot, word.Offset, false, out int outerBinding)) ++ { ++ return SearchResult.NotFound; ++ } ++ ++ sbCbSlot = PointerSlotFlag | word.Slot; ++ sbCbOffset = word.Offset | (word.ByteOffset / 8) << 10; ++ ++ // The base is the pointer as the outer buffer holds it, loaded again here: the term may be a pointer ++ // advanced by an index, or a value whose load does not dominate this access. ++ baseTerm = EmitBefore(node, Instruction.Load, StorageKind.StorageBuffer, ++ Const(outerBinding), Const(0), OuterWordOffset(gpuAccessor, node, word)); ++ } ++ ++ Operand offset = EmitBefore(node, Instruction.Subtract, StorageKind.None, operation.GetSource(0), baseTerm); ++ ++ return new SearchResult(sbCbSlot, sbCbOffset, offset) ++ { ++ Base = word.ByteOffset >= 0 ? baseTerm : null, ++ }; ++ } ++ ++ private static Operand EmitBefore(LinkedListNode node, Instruction inst, StorageKind storageKind, params Operand[] sources) ++ { ++ Operand dest = Local(); ++ node.List.AddBefore(node, new Operation(inst, storageKind, dest, sources)); ++ ++ return dest; ++ } ++ ++ // The word index of a pointer's low half in the outer buffer, as GenerateInlineStorageOp addresses that buffer. ++ private static Operand OuterWordOffset(IGpuAccessor gpuAccessor, LinkedListNode node, PointerWord word) ++ { ++ Operand byteOffset = Const(word.ByteOffset); ++ ++ if (gpuAccessor.QueryHasUnalignedStorageBuffer()) ++ { ++ Operand outer = Cbuf(word.Slot, word.Offset); ++ Operand masked = EmitBefore(node, Instruction.BitwiseAnd, StorageKind.None, outer, ++ Const(-gpuAccessor.QueryHostStorageBufferOffsetAlignment())); ++ Operand address = EmitBefore(node, Instruction.Add, StorageKind.None, outer, byteOffset); ++ byteOffset = EmitBefore(node, Instruction.Subtract, StorageKind.None, address, masked); ++ } ++ ++ return EmitBefore(node, Instruction.ShiftRightU32, StorageKind.None, byteOffset, Const(2)); ++ } ++ + private static SearchResult FindBaseAddressCbFromMemory(GtsContext gtsContext, Operation operation, int constOffset, bool needsOffset) + { + if (operation != null)