Files
Thomas MartinandClaude Opus 5.5 0d5dd2d33f The game's own GIF packets, read natively at their send
Every GS packet the game builds itself is now read from the structures it
was built from, as a list of register writes, instead of parsing GIF tags:
Spr_draw's sprites, Screen_blur_draw, Gifreg_set_dma's tables, scroll_disp's
rain and mist layers, oniGsMoveImage, the effects' sprite arrays (add_scr_efc,
esp_sp00), StencilMapPolygon's pond and Md_draw_semi's semi-transparent parts.
ONI_NATIVE_GIF=0 keeps the packets, =compare checks both kick by kick:
identical on all 188 story states (126,821 sends, 19.5 million kicks).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-29 16:51:10 +01:00

287 lines
15 KiB
C++

// The GS work the game sends itself (sceDmaSend on the GIF's channel), read natively from the game's structures at the
// send instead of from the packet it built (docs/NATIVE_3D.md, "The game's own GIF packets"). Each reader gives the
// register writes the packet carries, in its order; runtime/oni_vu1.cpp applies them to the same GS state.
#include "oni_gsnative.h"
#include <cstring>
namespace oni::gsnative
{
namespace
{
using oni::native3d::GsOp;
uint32_t u32(const uint8_t *ram, uint32_t a)
{
uint32_t v;
std::memcpy(&v, ram + (a & 0x1ffffffu), 4);
return v;
}
uint64_t u64(const uint8_t *ram, uint32_t a)
{
uint64_t v;
std::memcpy(&v, ram + (a & 0x1ffffffu), 8);
return v;
}
void reg(std::vector<GsOp> &ops, uint32_t r, uint64_t value)
{
GsOp op{GsOp::Kind::Register};
op.reg = r;
op.value = value;
ops.push_back(op);
}
} // namespace
// Spr_draw (0x1808e0): the primitives Spr_entry queued (Spr_queue 0x3d87e0, Spr_queue_ctr 0x214564; Esp_trans's
// sprites), each the ordering table's sprite (+4 its attribute, +8 RGBA, +0xc UV, +0x10 XYZ, +0x18 UV, +0x1c XY,
// +0x20 Z, +0x24 a pointer to its TEX0), drawn in context 2 after the packet's header: TEXA 0x8000, ZBUF_2 (no Z
// written), TEST_2 0x7000d, PRIM 0x835e (a textured, blended sprite), TEX1_2 0x60; each with its ALPHA_2 (0x44 for
// attribute 0x2000, 0x42 for 0x4000, else 0x48), then TEX0_2, RGBAQ, UV, XYZF2, UV, XYZF2.
bool sprites(const uint8_t *ram, std::vector<GsOp> &ops)
{
const uint32_t count = u32(ram, 0x214564);
if (count > 0x400)
return false;
reg(ops, 0x3b, 0x8000);
reg(ops, 0x4f, 0x13100008cull);
reg(ops, 0x48, 0x7000d);
reg(ops, 0x00, 0x835e);
reg(ops, 0x15, 0x60);
for (uint32_t i = 0; i < count; ++i)
{
const uint32_t p = u32(ram, 0x3d87e0 + 4 * i);
const uint32_t attr = u32(ram, p + 4);
reg(ops, 0x43, (attr & 0x2000) ? 0x44 : (attr & 0x4000) ? 0x42 : 0x48);
reg(ops, 0x07, u64(ram, u32(ram, p + 0x24)));
reg(ops, 0x01, u32(ram, p + 8));
reg(ops, 0x03, u32(ram, p + 0xc));
reg(ops, 0x04, u64(ram, p + 0x10));
reg(ops, 0x03, u32(ram, p + 0x18));
reg(ops, 0x04, static_cast<uint64_t>(u32(ram, p + 0x1c)) | static_cast<uint64_t>(u32(ram, p + 0x20)) << 32);
}
return true;
}
// Gifreg_set_dma(pD, length) (0x12e6d0; main's and Game_reset's GS set-up): the caller's table of length register
// writes (a qword each: the value, then the register), sent as A+D.
bool registers(const uint8_t *ram, uint32_t table, uint32_t length, std::vector<GsOp> &ops)
{
if (length > 0x1000)
return false;
for (uint32_t k = 0; k < length; ++k)
reg(ops, ram[(table + 16 * k + 8) & 0x1ffffffu], u64(ram, table + 16 * k));
return true;
}
// scroll_disp (0x145f10): the cut's scrolling layers (the rain or mist over a backdrop, scroll.c), from its entry in
// scr_set_w (0x1f5030 + scr_type x 40: +2 the Z, +0x10 the lens's shift, +0x12 its bottom, +0x14 the Y offset,
// +0x18 the two colours) and the layers' positions (pos_x 0x3d6c10, which scroll_disp_sub01 has just moved), drawn
// through Scroll_info's TEX0 (0x2144a8). Two packets: layers 0 and 1 taken away from the frame (ALPHA_2 0x42, FIX
// 0x18, the second colour), then layers 4 to 7 added (0x48, FIX 8, the first): each six strips of 128 pixels whose
// top bends with the lens ((x - bottom + 4 x layer)^2 >> shift), 256 rows high, its UVs the layer's place in the
// 512 x 256 texture. The sums are the builder's: 32 bits, 16-bit truncations where it makes them.
bool scroll(const uint8_t *ram, std::vector<GsOp> &ops)
{
int16_t type;
std::memcpy(&type, ram + 0x2144c0, 2);
if (type < 0 || type >= 143)
return false; // entry 143, the table's end, has the other builder (scroll_disp_sub00)
const uint32_t e = 0x1f5030u + 40u * static_cast<uint32_t>(type);
if (u32(ram, e + 0x24) != 0x1463a0u)
return false;
auto s16 = [&](uint32_t a) { int16_t v; std::memcpy(&v, ram + (a & 0x1ffffffu), 2); return static_cast<int32_t>(v); };
auto sx16 = [](int32_t v) { return static_cast<int32_t>(static_cast<int16_t>(v)); };
const uint64_t z = static_cast<uint64_t>(static_cast<uint16_t>(s16(e + 2))) << 32;
const uint32_t shift = static_cast<uint32_t>(s16(e + 0x10)) & 31u;
const int32_t bottom = s16(e + 0x12), ofsY = s16(e + 0x14);
const uint64_t tex0 = u64(ram, 0x2144a8);
auto xyz = [&](int32_t x, int32_t y) {
const uint32_t X = (static_cast<uint32_t>(x + 0x6c0) & 0xffffu) << 4;
const uint32_t Y = ((static_cast<uint32_t>(y) << 16) + 0x0f200000u) & 0xffff0000u;
return z | static_cast<uint32_t>(X | (Y << 3));
};
auto uv = [](int32_t u, int32_t v) {
return static_cast<uint64_t>(static_cast<int64_t>(static_cast<int32_t>(static_cast<uint32_t>(u | v << 16) << 4)));
};
auto layers = [&](uint64_t alpha, uint64_t rgba, int first, int last) {
reg(ops, 0x43, alpha);
reg(ops, 0x3b, 0x8000);
reg(ops, 0x4f, 0x13100008cull);
for (int i = first; i <= last; ++i)
{
const int32_t k = 4 * i, p = s16(0x3d6c10 + 2 * i);
int32_t x = sx16(-(p & 0x7f)), t = sx16(x + p);
for (int32_t n = 0; n < 0x300; n += 0x80)
{
const int32_t x0 = x, x1 = sx16(x + 0x80);
const int32_t d0 = x0 - bottom + k, d1 = x1 - bottom + k;
auto sq = [](int32_t d) { return static_cast<int32_t>(static_cast<uint32_t>(d) * static_cast<uint32_t>(d)); };
const int32_t y0 = sx16(ofsY - (sq(d0) >> shift)), y1 = sx16(ofsY - (sq(d1) >> shift));
const int32_t a = sx16(t) & 0x1ff, v = (sx16(t) & 0x200) >> 2;
const int32_t u0 = (a >> 1) + 1, u1 = (a + 0x80) >> 1;
reg(ops, 0x00, 0x35c);
reg(ops, 0x07, tex0);
reg(ops, 0x01, rgba);
reg(ops, 0x03, uv(u0, v));
reg(ops, 0x05, xyz(x0, y0));
reg(ops, 0x01, rgba);
reg(ops, 0x03, uv(u1, v));
reg(ops, 0x05, xyz(x1, y1));
reg(ops, 0x01, rgba);
reg(ops, 0x03, uv(u0, v + 0x7f));
reg(ops, 0x05, xyz(x0, y0 + 0x100));
reg(ops, 0x01, rgba);
reg(ops, 0x03, uv(u1, v + 0x7f));
reg(ops, 0x05, xyz(x1, y1 + 0x100));
x = x1;
t = sx16(t + 0x80);
}
}
};
layers(0x1800000042ull, u32(ram, e + 0x1c), 0, 1);
layers(0x800000048ull, u32(ram, e + 0x18), 4, 7);
return true;
}
// Esp_sp_draw's sends (0x180d10): each effect's prims array (Get_esp_sp_prims) as add_scr_efc (the cut's falling
// dust and drops, esp_sp02.c) and esp_sp00 (sword hits' sparks and blood) fill it: six register writes at +0x10 (a
// qword each, the value then the register: TEXA, ZBUF_2, PRIM 0x35e (sprites), ALPHA_2, TEX0_2, RGBAQ), then from
// +0x90 each sprite's two corners (UV, XYZF2, UV, XYZF2: 64 bits each, as REGLIST carries them). Esp_sp_draw has
// turned the array's size word into the send's tag: count = ((qwc + 1) x 16 - 0x90) / 32. False when the array is
// not laid out so (the packet then drawn as before).
bool espSprites(const uint8_t *ram, uint32_t packet, std::vector<GsOp> &ops)
{
const uint32_t qwc = u32(ram, packet) & 0xffff;
if (qwc < 8 || (qwc - 8) % 2 != 0 || u64(ram, packet + 0x10) != 0x1000000000008006ull ||
(u64(ram, packet + 0x80) >> 58) != ((4ull << 2) | 1ull) || u64(ram, packet + 0x88) != 0x4343)
return false;
const uint32_t count = (qwc - 8) / 2;
if ((u64(ram, packet + 0x80) & 0x7fff) != count)
return false;
for (uint32_t k = 2; k <= 7; ++k)
reg(ops, ram[(packet + 16 * k + 8) & 0x1ffffffu], u64(ram, packet + 16 * k));
for (uint32_t i = 0; i < count; ++i)
{
const uint32_t q = packet + 0x90 + 32 * i;
reg(ops, 0x03, u64(ram, q));
reg(ops, 0x04, u64(ram, q + 8));
reg(ops, 0x03, u64(ram, q + 16));
reg(ops, 0x04, u64(ram, q + 24));
}
return true;
}
// Md_draw_semi / Md_draw_semi_l (0x18da30, 0x18df70: the semi-transparent models' parts, Samanosuke's hair...):
// the list of register writes they build at G+0x44ac, in segments linked as the DMA reads them (a CNT tag, or END
// for the last; then an A+D tag for the segment's qwc - 1 writes; then the writes, a qword each: the value, then the
// register): the header (TEST_2, ALPHA_2, TEXA, ZBUF_2, RGBAQ, PRIM 0x25c), each group's TEX0_2, then each
// point's RGBAQ (Q from pCalcPolygon), ST and XYZ2 (XYZ3 where pCalcGetResult culls it). False when a segment is
// not so (the packet then drawn as before).
bool registerChain(const uint8_t *ram, uint32_t tag, std::vector<GsOp> &ops)
{
for (int guard = 0; guard < 4096; ++guard)
{
if (tag & 0x8000000fu)
return false;
const uint32_t t = u32(ram, tag), qwc = t & 0xffff, id = (t >> 28) & 7;
if ((id != 1 && id != 7) || qwc == 0)
return false;
const uint64_t gif = u64(ram, tag + 16);
if ((gif >> 60) != 1 || (gif & 0x7fff) != qwc - 1 || (u64(ram, tag + 24) & 0xf) != 0xe ||
((gif >> 15) & 1) != (id == 7 ? 1u : 0u))
return false;
for (uint32_t k = 2; k <= qwc; ++k)
reg(ops, ram[(tag + 16 * k + 8) & 0x1ffffffu], u64(ram, tag + 16 * k));
if (id == 7)
return true;
tag += 16 * (qwc + 1);
}
return false;
}
// oniGsMoveImage (0x1c1140; Sprite_sp's moving pictures): a copy within the GS's memory from its 13 arguments
// (source and destination base, width and format; source X, Y; destination X, Y; width, height; direction), as
// BITBLTBUF, TRXPOS, TRXREG and TRXDIR 2 (local to local). Each argument sign-extended, as the game shifts them.
void moveImage(const int16_t a[13], std::vector<GsOp> &ops)
{
auto s = [](int16_t v, int shift) { return static_cast<uint64_t>(static_cast<int64_t>(v)) << shift; };
reg(ops, 0x50, s(a[0], 0) | s(a[1], 16) | s(a[2], 24) | s(a[3], 32) | s(a[4], 48) | s(a[5], 56));
reg(ops, 0x51, s(a[6], 0) | s(a[7], 16) | s(a[8], 32) | s(a[9], 48) | s(a[12], 59));
reg(ops, 0x52, s(a[10], 0) | s(a[11], 32));
reg(ops, 0x53, 2);
}
// StencilMapPolygon(pReg, pPoly, pUv) (0x1b6440, stcl_utl.c; the background movie drawn through a cut's polygons:
// stage 3 room 7's pond) sends TRANS_DATA (0x208100), its own arrays: a table of nine register writes (a qword
// each, the value then the register: TEST_1, TEX0_1, TEX1_1, CLAMP_1, PABE, FOGCOL, FOG, TEXA, ALPHA_1, which pReg
// may have changed), the strip's PRIM (0x74 with STQ from StencilRemapTex, 0x174 with the caller's UVs) at +0xa0,
// then from +0xb0 a vertex of three qwords per point (the ST's S, T, Q or the UV's U, V; the colour's four bytes;
// the projected X, Y, Z, the first two points' with ADC), qwc = 11 + 3 x points. Read as the arrays they are: the
// writes in their order, a colour that keeps Q where the UVs carry none (register 0x101).
bool stencil(const uint8_t *ram, uint32_t address, uint32_t qwc, std::vector<GsOp> &ops)
{
if (address != 0x208100u || qwc < 11 || (qwc - 11) % 3 != 0 || qwc > 11 + 3 * 64)
return false;
for (uint32_t k = 1; k <= 9; ++k)
reg(ops, ram[0x208100u + 16 * k + 8], u64(ram, 0x208100u + 16 * k));
const uint64_t prim = (u64(ram, 0x2081a0) >> 47) & 0x7ff;
reg(ops, 0x00, prim);
const bool uvs = prim & 0x100;
for (uint32_t i = 0, n = (qwc - 11) / 3; i < n; ++i)
{
const uint32_t v = 0x2081b0u + 0x30u * i;
auto w = [&](uint32_t q, uint32_t k) { return u32(ram, v + 16 * q + 4 * k); };
const uint32_t rgba = (w(1, 0) & 0xff) | (w(1, 1) & 0xff) << 8 | (w(1, 2) & 0xff) << 16 | (w(1, 3) & 0xff) << 24;
if (uvs)
{
reg(ops, 0x03, (w(0, 0) & 0x3fff) | (w(0, 1) & 0x3fff) << 16);
reg(ops, 0x101, rgba);
}
else
{
reg(ops, 0x02, w(0, 0) | static_cast<uint64_t>(w(0, 1)) << 32);
reg(ops, 0x01, rgba | static_cast<uint64_t>(w(0, 2)) << 32);
}
const bool adc = (w(2, 3) >> 15) & 1;
reg(ops, adc ? 0x0d : 0x05, (w(2, 0) & 0xffff) | (w(2, 1) & 0xffff) << 16 | static_cast<uint64_t>(w(2, 2)) << 32);
}
return true;
}
// Screen_blur_draw (0x18ab60): the frame shown again over itself, through its blur state (G+0x48ac, nine 16-bit
// fields: the centre x, y; the left, right, top and bottom half-sizes; the far U, V; the alpha) and the display buffer
// drawn into (G+0x407c: the other one's block, 35 x 64 apart, is the texture): PRIM 0x3f, TEST_2 0x50000 (Z always),
// ALPHA_2 0x4000000044 (FIX 0x40), TEXA 0, TEX1_2 0x61, TEX0_2, CLAMP_2 0x37c009fc00a, a strip (PRIM 0x354) of four
// corners (RGBAQ 0x808080 with the alpha, UV, XYZF2 at Z 0xffffff, F 0x80), CLAMP_2 0. Its 16-bit sums are the
// game's: sign-extended as it makes them.
bool blur(const uint8_t *ram, std::vector<GsOp> &ops)
{
const uint32_t s = 0x27de30 + 0x48ac;
auto f = [&](uint32_t off) { uint16_t v; std::memcpy(&v, ram + s + off, 2); return static_cast<uint32_t>(v); };
auto corner = [&](uint32_t x, uint32_t y) {
const int64_t xs = static_cast<int32_t>(x), ys = static_cast<int32_t>(y);
return static_cast<uint64_t>(xs) | static_cast<uint64_t>(ys << 16) | 0x80ffffffull << 32;
};
const uint32_t frame = u32(ram, 0x27de30 + 0x407c);
reg(ops, 0x00, 0x3f);
reg(ops, 0x48, 0x50000);
reg(ops, 0x43, 0x4000000044ull);
reg(ops, 0x3b, 0);
reg(ops, 0x15, 0x61);
reg(ops, 0x07, 0x668028000ull | static_cast<uint64_t>(((frame * 35u) << 12) >> 6));
reg(ops, 0x09, 0x37c009fc00aull);
reg(ops, 0x00, 0x354);
reg(ops, 0x01, 0x8000808080ull | static_cast<uint64_t>(f(0x10)) << 24);
reg(ops, 0x03, 0);
reg(ops, 0x04, corner(f(0) - f(4), f(2) - f(8)));
reg(ops, 0x03, f(0xc));
reg(ops, 0x04, corner(f(0) + f(6), f(2) - f(8)));
reg(ops, 0x03, static_cast<uint64_t>(f(0xe)) << 16);
reg(ops, 0x04, corner(f(0) - f(4), f(2) + f(0xa)));
reg(ops, 0x03, f(0xc) | static_cast<uint64_t>(f(0xe)) << 16);
reg(ops, 0x04, corner(f(0) + f(6), f(2) + f(0xa)));
reg(ops, 0x09, 0);
return true;
}
} // namespace oni::gsnative