mirror of
https://github.com/TheBozos/Onimusha_Recomp.git
synced 2026-12-01 12:54:10 -10:00
Every saved state (440) swept for the VU1 programs it runs: only texmap (stage 4, k400c) besides the ones already native; it now matches its program exactly. bc_cl (std with a back-face test: the screen winding of the last three positions, VU1 1020 / 1021, against the vertex's status byte) is native, checked by forcing an object's Vu_type with the bot's poke. wld_bcl and spe, used by no saved state, keep their programs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
407 lines
19 KiB
C++
407 lines
19 KiB
C++
// Onimusha's models drawn natively (docs/NATIVE_3D.md): the VU1 programs' maths in C++, from the game's own data.
|
|
//
|
|
// "std" (Vu1mc std_cl / std_nc: skinned parts, a vertex weighted over up to three matrices). Its constants: the joints'
|
|
// matrices (the model work's pWnmat, VU1 0-171, a joint every four addresses), the clip and screen matrices (Gm.Wc,
|
|
// Gm.Ws: VU1 176-183), the light colours and directions (the model work's pLc, pLv: 184-191), the object's colour scale
|
|
// and XYZF2's upper half (Rgbf: 1022). A vertex: position, normal, UV (integers, 4 fraction bits), the matrices (their
|
|
// VU1 addresses; none past the first when the second is 0), the weights (0-248) and the strip flag. Per vertex:
|
|
// - M = w0 M1 + w1 M2 + w2 M3 (weights / 248), or M1; p = M (x, y, z, 1), n = M (x, y, z, 0);
|
|
// - light = max(D n + D3, 0) per light; colour = min(C' light + C'3, 255) with C' = C x (scale, 128); A 128;
|
|
// - s = S p, q = 1 / s.w, XYZ = s q in 12.4; STQ = (u / 16 / 255, v / 16 / 255, 1) q;
|
|
// - the clip flags of K p against its w; a vertex's XYZF2 gets ADC (no triangle ends there) when any of the last
|
|
// three vertices is outside or the vertex starts a strip.
|
|
// The others differ from it: "100" one matrix a vertex (the first); "1mat" one matrix for the part, folded into K, S and
|
|
// D once (D without its offset); "wld" no matrix (world positions); "_nc" no clip flags (ADC from the strip flag
|
|
// alone); "z" p.y clamped at 0; "texmap" STQ from p + 20 n projected by S with p's 1/w: ((x q - 1728) / 1024 q,
|
|
// (y q - 1936) / 1024 q, q). Every sum as the VU makes it (MULA, MADDA, MADDA, MADD), so the results match its own.
|
|
#include "oni_native3d.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstring>
|
|
|
|
namespace oni::native3d
|
|
{
|
|
namespace
|
|
{
|
|
V4 add(V4 a, V4 b) { return {a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w}; }
|
|
V4 mul(V4 a, float s) { return {a.x * s, a.y * s, a.z * s, a.w * s}; }
|
|
V4 mul(V4 a, V4 b) { return {a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w}; }
|
|
V4 apply(const M4 &m, float x, float y, float z, float w)
|
|
{
|
|
return add(add(add(mul(m.c[0], x), mul(m.c[1], y)), mul(m.c[2], z)), mul(m.c[3], w));
|
|
}
|
|
float bits(uint32_t b)
|
|
{
|
|
float f;
|
|
std::memcpy(&f, &b, 4);
|
|
return f;
|
|
}
|
|
uint32_t fbits(float f)
|
|
{
|
|
uint32_t b;
|
|
std::memcpy(&b, &f, 4);
|
|
return b;
|
|
}
|
|
// FTOI (the VU's: toward zero, saturated).
|
|
int32_t ftoi(float f, int fraction)
|
|
{
|
|
const double d = static_cast<double>(f) * (1 << fraction);
|
|
if (!(d == d))
|
|
return 0;
|
|
return static_cast<int32_t>(std::clamp(d, -2147483648.0, 2147483647.0));
|
|
}
|
|
uint32_t clipFlags(V4 v)
|
|
{
|
|
const float w = std::fabs(v.w);
|
|
uint32_t c = 0;
|
|
const float e[3] = {v.x, v.y, v.z};
|
|
for (int k = 0; k < 3; ++k)
|
|
{
|
|
if (e[k] > w)
|
|
c |= 1u << (2 * k);
|
|
if (e[k] < -w)
|
|
c |= 2u << (2 * k);
|
|
}
|
|
return c;
|
|
}
|
|
|
|
// The vertices of a batch: `in` gives each one's inputs and a matrix by its VU1 address; GS words to `out`.
|
|
template <class In>
|
|
// recent: the screen positions of the last two vertices (the back-face programs keep them in VU1 1020 and 1021).
|
|
void vertices(const Program &program, const Constants &k, const In &in, uint32_t n, uint32_t &clip, V4 *recent,
|
|
uint32_t *out)
|
|
{
|
|
const Skin skin = program.skin;
|
|
M4 clipM = k.clip, screen = k.screen, lightDir = k.lightDir;
|
|
if (skin == Skin::Object && n) // one matrix (the first vertex's) folded into the others once; no light offset
|
|
{
|
|
const M4 m = in.matrix(in.mats(0)[1]);
|
|
for (int j = 0; j < 4; ++j)
|
|
{
|
|
clipM.c[j] = apply(k.clip, m.c[j].x, m.c[j].y, m.c[j].z, m.c[j].w);
|
|
screen.c[j] = apply(k.screen, m.c[j].x, m.c[j].y, m.c[j].z, m.c[j].w);
|
|
lightDir.c[j] = j < 3 ? apply(k.lightDir, m.c[j].x, m.c[j].y, m.c[j].z, m.c[j].w) : V4{0, 0, 0, 0};
|
|
}
|
|
}
|
|
const V4 scale{k.scale.x, k.scale.y, k.scale.z, 128.0f};
|
|
M4 lightColour = k.lightColour;
|
|
for (V4 &c : lightColour.c)
|
|
c = mul(c, scale);
|
|
const float weightScale = bits(0x3b842108u), uvScale = bits(0x3b808080u);
|
|
for (uint32_t v = 0; v < n; ++v)
|
|
{
|
|
const V4 pos = in.position(v), nrm = in.normal(v);
|
|
int32_t uv[2];
|
|
in.uv(v, uv);
|
|
const auto mats = in.mats(v);
|
|
uint32_t w[4];
|
|
in.weights(v, w);
|
|
V4 p = pos, nn = nrm, e{};
|
|
if (program.envMap)
|
|
e = add(pos, mul(V4{nrm.x, nrm.y, nrm.z, 0.0f}, 20.0f));
|
|
if (skin == Skin::Weighted || skin == Skin::PerVertex)
|
|
{
|
|
M4 m = in.matrix(mats[1]);
|
|
if (skin == Skin::Weighted && mats[2])
|
|
{
|
|
const M4 a = in.matrix(mats[2]), c = in.matrix(mats[3]);
|
|
const float w0 = static_cast<float>(w[0]) * weightScale, w1 = static_cast<float>(w[1]) * weightScale,
|
|
w2 = static_cast<float>(w[2]) * weightScale;
|
|
for (int j = 0; j < 4; ++j)
|
|
m.c[j] = add(add(mul(m.c[j], w0), mul(a.c[j], w1)), mul(c.c[j], w2));
|
|
}
|
|
p = apply(m, pos.x, pos.y, pos.z, 1.0f);
|
|
nn = apply(m, nrm.x, nrm.y, nrm.z, 0.0f);
|
|
if (program.envMap)
|
|
e = apply(m, e.x, e.y, e.z, 1.0f);
|
|
}
|
|
if (program.clampY)
|
|
p.y = std::max(p.y, 0.0f);
|
|
V4 light = apply(lightDir, nn.x, nn.y, nn.z, 1.0f);
|
|
light = {std::max(light.x, 0.0f), std::max(light.y, 0.0f), std::max(light.z, 0.0f), std::max(light.w, 0.0f)};
|
|
const V4 colour = apply(lightColour, light.x, light.y, light.z, 1.0f);
|
|
const V4 s = apply(screen, p.x, p.y, p.z, 1.0f);
|
|
const float q = 1.0f / s.w;
|
|
clip = ((clip << 6) | clipFlags(apply(clipM, p.x, p.y, p.z, 1.0f))) & 0xffffffu;
|
|
bool adc = (program.clipping && (clip & 0x3ffffu)) || (w[3] & 0xffff);
|
|
if (program.backCull)
|
|
{
|
|
// The winding of (P-2, P-1, P) on the screen against the strip's parity (the vertex's status): a back
|
|
// face ends no triangle.
|
|
const V4 here = mul(s, q);
|
|
const V4 d1 = add(recent[0], mul(here, -1.0f)), d2 = add(recent[1], mul(here, -1.0f));
|
|
const bool negative = d1.x * d2.y - d1.y * d2.x < 0.0f;
|
|
adc = adc || (mats[0] ? negative : !negative);
|
|
recent[1] = recent[0];
|
|
recent[0] = here;
|
|
}
|
|
|
|
uint32_t *o = out + 12 * v;
|
|
V4 stq = mul(V4{static_cast<float>(uv[0]) / 16.0f * uvScale, static_cast<float>(uv[1]) / 16.0f * uvScale, 1.0f, 0.0f}, q);
|
|
if (program.envMap)
|
|
{
|
|
const V4 es = apply(screen, e.x, e.y, e.z, 1.0f);
|
|
stq = mul(V4{(es.x * q - 1728.0f) * (1.0f / 1024.0f), (es.y * q - 1936.0f) * (1.0f / 1024.0f), 1.0f, 0.0f}, q);
|
|
}
|
|
o[0] = fbits(stq.x), o[1] = fbits(stq.y), o[2] = fbits(stq.z), o[3] = fbits(stq.w);
|
|
o[4] = static_cast<uint32_t>(ftoi(std::min(colour.x, 255.0f), 0));
|
|
o[5] = static_cast<uint32_t>(ftoi(std::min(colour.y, 255.0f), 0));
|
|
o[6] = static_cast<uint32_t>(ftoi(std::min(colour.z, 255.0f), 0));
|
|
o[7] = 128;
|
|
o[8] = static_cast<uint32_t>(ftoi(s.x * q, 4));
|
|
o[9] = static_cast<uint32_t>(ftoi(s.y * q, 4));
|
|
o[10] = static_cast<uint32_t>(ftoi(s.z * q, 4));
|
|
o[11] = adc ? ((k.fog + 0x8000u) & 0xffffu) : k.fog;
|
|
}
|
|
}
|
|
|
|
// ---- VU1 memory as VIF1 leaves it (the comparison) ----
|
|
V4 vec(const uint8_t *mem, uint32_t q)
|
|
{
|
|
V4 v;
|
|
std::memcpy(&v, mem + ((q & 0x3ffu) << 4), 16);
|
|
return v;
|
|
}
|
|
int32_t word(const uint8_t *mem, uint32_t q, int k)
|
|
{
|
|
int32_t v;
|
|
std::memcpy(&v, mem + ((q & 0x3ffu) << 4) + 4 * k, 4);
|
|
return v;
|
|
}
|
|
M4 matrixAt(const uint8_t *mem, uint32_t q) { return {{vec(mem, q), vec(mem, q + 1), vec(mem, q + 2), vec(mem, q + 3)}}; }
|
|
struct VuMemory
|
|
{
|
|
const uint8_t *mem;
|
|
uint32_t first; // the first vertex's qword
|
|
V4 position(uint32_t v) const { return vec(mem, first + 5 * v); }
|
|
V4 normal(uint32_t v) const { return vec(mem, first + 5 * v + 1); }
|
|
void uv(uint32_t v, int32_t *o) const { o[0] = word(mem, first + 5 * v + 2, 0), o[1] = word(mem, first + 5 * v + 2, 1); }
|
|
std::array<uint32_t, 4> mats(uint32_t v) const
|
|
{
|
|
std::array<uint32_t, 4> m;
|
|
for (int k = 0; k < 4; ++k)
|
|
m[k] = static_cast<uint32_t>(word(mem, first + 5 * v + 3, k)) & 0xffff;
|
|
return m;
|
|
}
|
|
void weights(uint32_t v, uint32_t *o) const
|
|
{
|
|
for (int k = 0; k < 4; ++k)
|
|
o[k] = static_cast<uint32_t>(word(mem, first + 5 * v + 4, k));
|
|
}
|
|
M4 matrix(uint32_t address) const { return matrixAt(mem, address); }
|
|
};
|
|
|
|
// ---- The game's arrays (the native draw) ----
|
|
struct Arrays
|
|
{
|
|
const Batch &b;
|
|
const Constants &k;
|
|
V4 position(uint32_t v) const
|
|
{
|
|
V4 p;
|
|
std::memcpy(&p, b.positions + 16 * v, 16);
|
|
return p;
|
|
}
|
|
V4 normal(uint32_t v) const
|
|
{
|
|
V4 n{0, 0, 0, 0};
|
|
std::memcpy(&n, b.normals + 12 * v, 12);
|
|
return n;
|
|
}
|
|
void uv(uint32_t v, int32_t *o) const
|
|
{
|
|
int16_t t[2];
|
|
std::memcpy(t, b.uvs + 4 * v, 4);
|
|
o[0] = t[0], o[1] = t[1];
|
|
}
|
|
std::array<uint32_t, 4> mats(uint32_t v) const
|
|
{
|
|
const uint8_t *m = b.mats + 4 * v;
|
|
return {m[0], m[1], m[2], m[3]};
|
|
}
|
|
void weights(uint32_t v, uint32_t *o) const
|
|
{
|
|
uint16_t c;
|
|
std::memcpy(&c, b.weights + 2 * v, 2);
|
|
o[0] = (c & 0x1fu) << 3, o[1] = ((c >> 5) & 0x1fu) << 3, o[2] = ((c >> 10) & 0x1fu) << 3, o[3] = ((c >> 15) & 1u) << 7;
|
|
}
|
|
M4 matrix(uint32_t address) const
|
|
{
|
|
const uint32_t j = std::min<uint32_t>(address / 4, k.jointCount ? k.jointCount - 1 : 0);
|
|
M4 m;
|
|
if (k.joints && k.jointCount)
|
|
std::memcpy(&m, k.joints + 16 * j, 64);
|
|
else
|
|
m = M4{};
|
|
return m;
|
|
}
|
|
};
|
|
} // namespace
|
|
|
|
const Program *program(const char *name)
|
|
{
|
|
static const struct
|
|
{
|
|
const char *name;
|
|
Program program;
|
|
} kPrograms[] = {
|
|
{"std_cl", {Skin::Weighted, true}}, {"std_nc", {Skin::Weighted, false}},
|
|
{"100_cl", {Skin::PerVertex, true}}, {"100_nc", {Skin::PerVertex, false}},
|
|
{"100_clz", {Skin::PerVertex, true, true}}, {"100_ncz", {Skin::PerVertex, false, true}},
|
|
{"1mat_cl", {Skin::Object, true}}, {"1mat_nc", {Skin::Object, false}},
|
|
{"wld_cl", {Skin::World, true}}, {"wld_nc", {Skin::World, false}},
|
|
{"texmap", {Skin::Weighted, true, false, true}},
|
|
{"bc_cl", {Skin::Weighted, true, false, false, true}},
|
|
// (wld_bcl reads its history and the strip's parity at other pipeline offsets, its first vertex differently; spe
|
|
// lights specularly: neither in any saved state, both left to their programs.)
|
|
};
|
|
for (const auto &p : kPrograms)
|
|
if (std::strcmp(p.name, name) == 0)
|
|
return &p.program;
|
|
return nullptr;
|
|
}
|
|
|
|
void modelBatch(const Program &program, const uint8_t *mem, uint32_t top, uint32_t clip, std::vector<uint32_t> &out,
|
|
uint32_t &at)
|
|
{
|
|
Constants k;
|
|
k.clip = matrixAt(mem, 176), k.screen = matrixAt(mem, 180), k.lightColour = matrixAt(mem, 184), k.lightDir = matrixAt(mem, 188);
|
|
k.scale = vec(mem, 1022);
|
|
k.fog = static_cast<uint32_t>(word(mem, 1022, 3)) & 0xffffu;
|
|
const uint32_t n = static_cast<uint32_t>(word(mem, top, 0)) & 0x7fffu;
|
|
at = (top + 2 + 5 * n) & 0x3ffu;
|
|
out.assign(4 * (1 + 3 * n), 0);
|
|
std::memcpy(out.data(), mem + ((top & 0x3ffu) << 4), 16);
|
|
V4 recent[2] = {vec(mem, 1020), vec(mem, 1021)};
|
|
vertices(program, k, VuMemory{mem, top + 2}, n, clip, recent, out.data() + 4);
|
|
}
|
|
|
|
void drawBatch(const Program &program, const Constants &k, const Batch &b, uint32_t &clip, V4 *recent, uint32_t *out)
|
|
{
|
|
vertices(program, k, Arrays{b, k}, b.count, clip, recent, out);
|
|
}
|
|
|
|
// The shadows' volumes: the program "shadow" (a part a batch). Its constants: 0-3 the screen matrix S, 4-7 the clip
|
|
// matrix K, 8-11 clip space to the screen (C), 12 / 13 the ALPHA of a front / back face (A+D), 15 RGBAQ (A+D), 16 the
|
|
// clipped triangles' GIF tag (a fan). A part: its tag (a strip) and its positions, four a quad. Per vertex k:
|
|
// P = S p / w (12.4), its clip flags against K p; per quad, the ALPHA from its first triangle's screen winding (its
|
|
// cross product's z below 0: the front's). The strip's vertices 0 and 1 of each quad carry ADC; vertices 2 and 3 end
|
|
// a triangle each, drawn as they are when none of its vertices is outside K's volume, not at all (ADC) when all three
|
|
// are outside the same plane, else clipped (Sutherland-Hodgman, the planes -z, +z, -x, +x, -y, +y, in clip space; an
|
|
// edge keeps its start when that is inside and adds the crossing) and kicked as a fan of its own (C p' / w'),
|
|
// the strip's vertex then with ADC. The fans are kicked as they come, the strip after the part's last vertex.
|
|
void shadowPart(const uint8_t *constants, const uint8_t *part, std::vector<GsOp> &ops, uint64_t &alpha)
|
|
{
|
|
auto qword = [&](const uint8_t *base, uint32_t q, int k) { uint32_t v; std::memcpy(&v, base + 16 * q + 4 * k, 4); return v; };
|
|
auto matrix = [&](uint32_t q) { M4 m; std::memcpy(&m, constants + 16 * q, 64); return m; };
|
|
auto adWrite = [&](uint32_t q) {
|
|
GsOp op{GsOp::Kind::Register};
|
|
op.value = static_cast<uint64_t>(qword(constants, q, 0)) | static_cast<uint64_t>(qword(constants, q, 1)) << 32;
|
|
op.reg = qword(constants, q, 2) & 0xff;
|
|
return op;
|
|
};
|
|
auto tagPrim = [&](const uint8_t *base, uint32_t q) { return static_cast<uint64_t>(qword(base, q, 1) >> 15) & 0x7ff; };
|
|
const M4 screen = matrix(0), clipM = matrix(4), toScreen = matrix(8);
|
|
const GsOp rgbaq = adWrite(15), front = adWrite(12), back = adWrite(13);
|
|
GsOp alphaOp = front;
|
|
(void)alpha;
|
|
|
|
const uint32_t n = qword(part, 0, 0) & 0x7fff;
|
|
struct Point
|
|
{
|
|
V4 clip, screen;
|
|
uint32_t flags;
|
|
};
|
|
std::vector<Point> pts(n);
|
|
for (uint32_t k = 0; k < n; ++k)
|
|
{
|
|
V4 p;
|
|
std::memcpy(&p, part + 16 * (1 + k), 16);
|
|
const V4 s = apply(screen, p.x, p.y, p.z, 1.0f);
|
|
const float q = 1.0f / s.w;
|
|
pts[k].screen = mul(s, q);
|
|
pts[k].clip = apply(clipM, p.x, p.y, p.z, 1.0f);
|
|
pts[k].flags = clipFlags(pts[k].clip);
|
|
}
|
|
auto vertex = [&](const V4 &screenPoint, uint32_t w, bool adc) {
|
|
GsOp op{GsOp::Kind::Vertex};
|
|
op.x = static_cast<uint32_t>(ftoi(screenPoint.x, 4)) & 0xffff;
|
|
op.y = static_cast<uint32_t>(ftoi(screenPoint.y, 4)) & 0xffff;
|
|
op.z = (static_cast<uint32_t>(ftoi(screenPoint.z, 4)) >> 4) & 0xffffff;
|
|
op.fog = (w >> 4) & 0xff;
|
|
op.adc = adc;
|
|
return op;
|
|
};
|
|
// A triangle clipped to K's volume, kicked as a fan.
|
|
auto clipTriangle = [&](const V4 &a, const V4 &b, const V4 &c) {
|
|
std::vector<V4> poly = {a, b, c}, next;
|
|
static const struct { int axis; float sign; } kPlanes[] = {{2, -1}, {2, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 1}};
|
|
auto coord = [](const V4 &v, int axis) { return axis == 0 ? v.x : axis == 1 ? v.y : v.z; };
|
|
for (const auto &plane : kPlanes)
|
|
{
|
|
const uint32_t bit = 1u << (2 * plane.axis + (plane.sign < 0 ? 1 : 0));
|
|
next.clear();
|
|
for (size_t i = 0; i < poly.size(); ++i)
|
|
{
|
|
const V4 &p = poly[i], &c2 = poly[(i + 1) % poly.size()];
|
|
const bool pIn = !(clipFlags(p) & bit), cIn = !(clipFlags(c2) & bit);
|
|
if (pIn)
|
|
next.push_back(p);
|
|
if (pIn != cIn)
|
|
{
|
|
const float num = coord(p, plane.axis) - plane.sign * p.w;
|
|
const float den = (coord(c2, plane.axis) - plane.sign * c2.w) - num;
|
|
const float t = std::fabs(num / den);
|
|
next.push_back(add(p, mul(add(c2, mul(p, -1.0f)), t)));
|
|
}
|
|
}
|
|
poly.swap(next);
|
|
if (poly.empty())
|
|
break;
|
|
}
|
|
GsOp prim{GsOp::Kind::Prim};
|
|
prim.value = tagPrim(constants, 16);
|
|
ops.push_back(prim);
|
|
for (const V4 &v : poly)
|
|
{
|
|
const V4 s = apply(toScreen, v.x, v.y, v.z, v.w);
|
|
const float q = 1.0f / s.w;
|
|
ops.push_back(rgbaq);
|
|
ops.push_back(alphaOp);
|
|
ops.push_back(vertex(mul(s, q), static_cast<uint32_t>(ftoi(s.w * q, 4)), false));
|
|
}
|
|
};
|
|
|
|
std::vector<GsOp> strip;
|
|
GsOp prim{GsOp::Kind::Prim};
|
|
prim.value = tagPrim(part, 0);
|
|
strip.push_back(prim);
|
|
for (uint32_t k = 0; k < n; ++k)
|
|
{
|
|
const uint32_t m = k % 4;
|
|
bool adc = m < 2;
|
|
if (m == 2)
|
|
{
|
|
const V4 a = add(pts[k - 1].screen, mul(pts[k - 2].screen, -1.0f));
|
|
const V4 b = add(pts[k].screen, mul(pts[k - 1].screen, -1.0f));
|
|
alphaOp = a.x * b.y - a.y * b.x < 0.0f ? front : back;
|
|
}
|
|
if (m >= 2)
|
|
{
|
|
const uint32_t f0 = pts[k - 2].flags, f1 = pts[k - 1].flags, f2 = pts[k].flags;
|
|
if (f0 | f1 | f2)
|
|
{
|
|
adc = true;
|
|
if (!(f0 & f1 & f2))
|
|
clipTriangle(pts[k - 2].clip, pts[k - 1].clip, pts[k].clip);
|
|
}
|
|
}
|
|
strip.push_back(rgbaq);
|
|
strip.push_back(alphaOp);
|
|
strip.push_back(vertex(pts[k].screen, adc ? 0x8000u : 0u, adc));
|
|
}
|
|
ops.insert(ops.end(), strip.begin(), strip.end());
|
|
}
|
|
} // namespace oni::native3d
|