Files
Thomas MartinandClaude Opus 5.5 528c0f7324 POLYCALC, the VU0 program, written natively
poly_calc.c's VCALLMS 0 (the polygon effects: sword trails, polyline
strips) now runs runtime/oni_polycalc.cpp: a vertex lit, projected,
clipped and tested for facing with the VU0's own arithmetic in the
program's order, no pairs, pipelines or microcode. ONI_POLYCALC_COMPARE=1
runs the translated program beside it on every call and compares the
whole VU0 state: identical on 3.9 million calls in three fight scenes
and on every call of the 188 story states. ONI_POLYCALC=0 keeps the
translated program.

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

190 lines
9.4 KiB
C++

// Onimusha's VU0 program POLYCALC (poly_calc.c: pCalcPolygon's VCALLMS 0) written natively: one vertex lit, projected,
// clipped and tested for facing, as the microprogram does it, with the VU0's own arithmetic (bozo/ops_vu0.h: its
// rounding and flags) in the program's order, but as plain code: no pairs, no Q or MAC pipelines, no microcode.
//
// Its registers, as the game's loaders leave them (pCalcLoadGmMatrix: vf1-4 Gm.Ws, vf5-8 Gm.Lv, vf9-12 Gm.Lc, vf17-20
// Gm.Wc; pCalcLoadLightMatrix: vf5-12 a light's, vf30 a colour scale; pCalcLoadLocalWorlcMatrix: vf13-16 the local to
// world matrix) and pCalcPolygon (vf21 the position, vf22 the normal, vf25 the UV (integers, 4 fraction bits), vi10 the
// strip's parity, vi11 "do not kick"). What pCalcGetResult reads back: vi1, vf22 (the colour, integers), vf23 (XY in
// 12.4, Z an integer), vf25 (STQ), Q. Per vertex:
// - p = M (x, y, z, 1), n = M (x, y, z) (its 3 x 3); light = max(Lv n + Lv3, 0); colour = min(Lc light + Lc3, 1) x scale;
// - s = Ws p, q = 1 / s.w; P = s q; the clip flags of Wc p against its w (kept for the last three vertices);
// - STQ = (u / 16 / 255, v / 16 / 255, 0, 0) q;
// - the facing: the screen winding of (P-2, P-1, P) (vf28, vf29 keep the last two), its sign against the parity: 32 for
// a back face; vi1 = (kick or any of the last three vertices outside ? 1 : 0) + that.
// ONI_POLYCALC_COMPARE=1 runs the translated program beside it on every call and compares the whole VU0 state;
// ONI_POLYCALC=0 keeps the translated program.
#include "bozo/ops_vu0.h"
#include "bozo/port.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
namespace oni
{
namespace
{
using namespace bozo::vu0;
bozo::port::Function g_translated = nullptr;
uint32_t clipFlags(const Vec &v)
{
const uint32_t w = v.u[3] & 0x7FFFFFFFu;
uint32_t c = 0;
for (int k = 0; k < 3; ++k)
{
if (bozo::ps2f::lt(w, v.u[k]))
c |= 1u << (2 * k);
if (bozo::ps2f::lt(v.u[k], w ^ 0x80000000u))
c |= 2u << (2 * k);
}
return c;
}
void polycalc(R5900Context *ctx)
{
Vec *vf = ctx->vu0_vf;
Vec &acc = ctx->vu0_acc;
uint32_t *vi = ctx->vi;
itof(vf[25], vf[25], 12, 4); // the UV
// n = M n (the world's normal), p = M p.
mul(ctx, acc, vf[13], bc(vf[22], 0), 15);
madd(ctx, acc, acc, vf[14], bc(vf[22], 1), 15);
madd(ctx, vf[22], acc, vf[15], bc(vf[22], 2), 15);
mul(ctx, acc, vf[13], bc(vf[21], 0), 15);
madd(ctx, acc, acc, vf[14], bc(vf[21], 1), 15);
madd(ctx, acc, acc, vf[15], bc(vf[21], 2), 15);
madd(ctx, vf[21], acc, vf[16], bc(vf[0], 3), 15);
// The light's directions, then the screen.
mul(ctx, acc, vf[5], bc(vf[22], 0), 15);
madd(ctx, acc, acc, vf[6], bc(vf[22], 1), 15);
madd(ctx, acc, acc, vf[7], bc(vf[22], 2), 15);
madd(ctx, vf[22], acc, vf[8], bc(vf[0], 3), 15);
mul(ctx, acc, vf[1], bc(vf[21], 0), 15);
madd(ctx, acc, acc, vf[2], bc(vf[21], 1), 15);
madd(ctx, acc, acc, vf[3], bc(vf[21], 2), 15);
madd(ctx, vf[23], acc, vf[4], bc(vf[0], 3), 15);
max(vf[22], vf[22], bc(vf[0], 0), 15);
// The clip space, 1 / w.
mul(ctx, acc, vf[17], bc(vf[21], 0), 15);
madd(ctx, acc, acc, vf[18], bc(vf[21], 1), 15);
madd(ctx, acc, acc, vf[19], bc(vf[21], 2), 15);
const uint32_t q = div(ctx, vf[0].u[3], vf[23].u[3]);
madd(ctx, vf[24], acc, vf[20], bc(vf[0], 3), 15);
// The light's colours.
mul(ctx, acc, vf[9], bc(vf[22], 0), 15);
madd(ctx, acc, acc, vf[10], bc(vf[22], 1), 15);
vf[25].u[2] = vf[25].u[3] = static_cast<uint32_t>(static_cast<int32_t>(static_cast<int16_t>(vi[0])));
madd(ctx, acc, acc, vf[11], bc(vf[22], 2), 15);
madd(ctx, vf[22], acc, vf[12], bc(vf[0], 3), 15);
ctx->vu0_i_bits = 0x3b808080u; // 1 / 255
mul(ctx, vf[25], vf[25], broadcast(ctx->vu0_i_bits), 12);
mul(ctx, vf[23], vf[23], broadcast(q), 15);
ctx->vu0_clip_flags = ((ctx->vu0_clip_flags << 6) | clipFlags(vf[24])) & 0xFFFFFFu;
mul(ctx, vf[25], vf[25], broadcast(q), 15);
sub(ctx, acc, vf[0], vf[0], 15);
mini(vf[22], vf[22], bc(vf[0], 3), 15);
// The edges from P to the last two (vf28, vf29: kept for the next vertex).
msub(ctx, acc, acc, vf[23], bc(vf[0], 3), 14);
madd(ctx, vf[27], acc, vf[29], bc(vf[0], 3), 14);
move(vf[29], vf[28], 15);
madd(ctx, vf[26], acc, vf[28], bc(vf[0], 3), 14);
move(vf[28], vf[23], 15);
ftoi(vf[24], vf[23], 15, 4);
mul(ctx, vf[22], vf[22], vf[30], 14);
opmula(ctx, acc, vf[26], vf[27]);
{
Vec discard{};
opmsub(ctx, discard, acc, vf[27], vf[26]);
}
const uint32_t backSign = ctx->vu0_mac & 32u; // the winding's z below 0
ftoi(vf[22], vf[22], 15, 0);
ftoi(vf[23], vf[23], 12, 4);
ftoi(vf[23], vf[23], 2, 0);
// The facing against the parity, then the result.
const uint32_t back = vi[10] ? backSign : (32u + backSign) & 32u;
vi[12] = back;
const uint32_t out = (vi[11] | ((ctx->vu0_clip_flags & 0x03ffffu) ? 1u : 0u)) ? 1u : 0u;
vi[1] = (out + back) & 0xFFFFu;
ctx->vu0_q_bits = q;
}
// ONI_POLYCALC_COMPARE=1: the translated program and the native one on the same registers, every call.
struct Vu0State
{
Vec vf[32], acc;
uint32_t vi[16], q, i, status, mac, clip;
};
Vu0State save(const R5900Context *ctx)
{
Vu0State s;
std::memcpy(s.vf, ctx->vu0_vf, sizeof s.vf);
s.acc = ctx->vu0_acc;
std::memcpy(s.vi, ctx->vi, sizeof s.vi);
s.q = ctx->vu0_q_bits, s.i = ctx->vu0_i_bits, s.status = ctx->vu0_status, s.mac = ctx->vu0_mac, s.clip = ctx->vu0_clip_flags;
return s;
}
void restore(R5900Context *ctx, const Vu0State &s)
{
std::memcpy(ctx->vu0_vf, s.vf, sizeof s.vf);
ctx->vu0_acc = s.acc;
std::memcpy(ctx->vi, s.vi, sizeof s.vi);
ctx->vu0_q_bits = s.q, ctx->vu0_i_bits = s.i, ctx->vu0_status = s.status, ctx->vu0_mac = s.mac, ctx->vu0_clip_flags = s.clip;
}
uint64_t g_calls = 0, g_differ = 0;
void program(uint8_t *ram, R5900Context *ctx, Runtime *rt)
{
static const bool compare = std::getenv("ONI_POLYCALC_COMPARE") != nullptr;
if (!compare || !g_translated)
{
polycalc(ctx);
return;
}
const Vu0State in = save(ctx);
g_translated(ram, ctx, rt);
const Vu0State want = save(ctx);
restore(ctx, in);
polycalc(ctx);
const Vu0State got = save(ctx);
++g_calls;
bool same = std::memcmp(want.vf, got.vf, sizeof want.vf) == 0 && std::memcmp(&want.acc, &got.acc, sizeof want.acc) == 0 &&
std::memcmp(want.vi, got.vi, sizeof want.vi) == 0 && want.q == got.q && want.i == got.i &&
want.status == got.status && want.mac == got.mac && want.clip == got.clip;
if (!same && g_differ++ < 8)
{
std::printf("[oni:polycalc] call %llu differs:", (unsigned long long)g_calls);
for (int r = 1; r < 32; ++r)
if (std::memcmp(&want.vf[r], &got.vf[r], sizeof(Vec)) != 0)
std::printf(" vf%d %08x %08x %08x %08x / %08x %08x %08x %08x", r, want.vf[r].u[0], want.vf[r].u[1], want.vf[r].u[2],
want.vf[r].u[3], got.vf[r].u[0], got.vf[r].u[1], got.vf[r].u[2], got.vf[r].u[3]);
for (int r = 1; r < 16; ++r)
if (want.vi[r] != got.vi[r])
std::printf(" vi%d %x/%x", r, want.vi[r], got.vi[r]);
if (std::memcmp(&want.acc, &got.acc, sizeof want.acc) != 0)
std::printf(" acc");
if (want.q != got.q || want.i != got.i || want.status != got.status || want.mac != got.mac || want.clip != got.clip)
std::printf(" q %x/%x i %x/%x status %x/%x mac %x/%x clip %x/%x", want.q, got.q, want.i, got.i, want.status, got.status,
want.mac, got.mac, want.clip, got.clip);
std::printf("\n");
}
if (g_calls % 100000 == 0)
std::printf("[oni:polycalc] %llu calls compared, %llu differ\n", (unsigned long long)g_calls, (unsigned long long)g_differ);
}
} // namespace
// After installVu0 (the translated program, kept for the comparison): entry 0 drawn natively.
void installPolycalc(bozo::port::Registry &registry)
{
for (const BozoFunctionEntry &e : registry.vu0Programs())
if (e.address == 0)
g_translated = e.function;
const char *e = std::getenv("ONI_POLYCALC"); // ONI_POLYCALC=0: the translated program (the comparison's other side)
if (!(e && *e == '0'))
registry.vu0Program(0x0u, program);
}
} // namespace oni