Map Ocarina's global memory accesses through pointer tables and summed bases to storage buffers.

The translator dropped 36113 global accesses per Kokiri boot (loads read 0, stores and atomics deleted); 837 remain.
AmpereLowering computes a predicated LEA's carry sum unconditionally, so a base like c[0][0x110] no longer reaches the
address as a phi; the edge AA's tile list is written and its blend weights follow the edges. Patch 0022 extends
GlobalToStorage: a constant-buffer base inside a sum of terms, and buffers reached through a pointer the program loads
from another storage buffer at a constant offset (the pointer tables Ocarina's vertex, pixel, mesh and compute shaders
read, also through register spills and phis). Such a buffer is listed with its outer buffer and offset, and the runtime
binds it at the address read from guest memory when the stage is bound (vk_exec_draw.c ptr_addr). The fog injection's
64x36x96 volumes now hold in-scattering.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-06 14:02:22 -05:00
parent eed3e6b786
commit 9b4cca39ca
8 changed files with 470 additions and 23 deletions

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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;