Files
4c733a68c8 3D: back faces culled behind the camera too; M_19_03's view no longer covered (#18)
On M_19_03 (the seafront) the camera at (120.5, -4.5, 10) sits inside one
of the level's untextured boxes (prim 0x6792b0 at (120, 0, -30), 100 x 10 x
90). The IMD programs cull back faces in the object's space (L_2250 /
L_1250), so the PS2 draws nothing of it from inside. The port compared
screen areas and kept every triangle with a corner behind the camera, so
the box's inside faces covered everything below the horizon, the player
too.

The cull (and the outline pass's back-face pick) now takes the determinant
of the corners' (x, y, w): the screen area times the three w, whose sign
holds behind the camera. Triangles wholly in front of the camera are judged
as before.

Compared with PCSX2 from the same start: tools/pcsx2/warp_opening.pnach
makes New Game load 19:3:0, and SBC_WARP_OPENING does the same in the port.
Still different there: the platform top's tint and a paved surface past its
edge (docs/RECOMP_NOTES.md).

Diagnostics: SBC_3D_PICK=<kick>:<x>:<y> (meshes over a pixel, with w),
SBC_TRACE_OBJECTS now prints the camera and the matrices, SBC_TRACE_VERTS,
SBC_TRACE_TEXTURE_FAILS, FRAME in the untextured trace. README screenshot.

Co-authored-by: Thomas Martin <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
2026-12-01 01:14:26 +01:00

501 lines
23 KiB
C++

// Steambot Chronicles' 2D, native: the game's own sprite calls drawn by BozoEngine's renderer, their textures decoded
// from the game's own TIM2 data in EE memory. No GIF packet is read and no GS memory is kept.
//
// g2dSpriteSetPkt (0x222780) takes a 0x50-byte sprite record (docs/RECOMP_NOTES.md, "Drawing"):
// +0x00 x, +0x02 y (shorts), +0x04 Z, +0x08 flags (& 1 a colour per corner, & 2 rotated, & 4 point sampling),
// +0x0c / +0x0e the pivot (shorts), +0x10 / +0x14 the scale, +0x20 / +0x24 / +0x28 the rotations, +0x30 RGBA (four
// corners from +0x30 with flags & 1), +0x40 the texture descriptor {id, w, h, u0, v0, u1, v1} (shorts).
// The game builds a matrix from them (0x221870, VU0 macro code): the corner less the pivot, scaled, rotated, moved to the
// position, scaled by the screen's terms (gp-0x7fe4 / gp-0x7fe0) and offset (gp-0x7fec / gp-0x7fe8, GS units). The
// same steps run here; the quad is kept with the packet bucket current at the call (gp-0x7e80), and the frame's quads
// go to the renderer in bucket order at the kick (sbc_render.cpp), as the DMA chain would have drawn them.
//
// The texture: TexGetTEXimage's table (gp-0x7cc0, count gp-0x7ccc) gives the TEXimage, whose level-0 packet (+0) and
// CLUT packet (+0x10) are the game's own image transfers: BITBLTBUF (format), TRXREG (size) and a DMA REF tag to the
// TIM2 texels or colours. Those bytes are decoded once (keyed by their hash): indexed formats through the CLUT in the
// GS's CSM1 order.
#include "sbc_port.h"
#include "bozo/hash.h"
#include "bozo/port.h"
#include "bozo/render/renderer.h"
#include "bozo/render/texture.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <unordered_map>
#include <vector>
namespace sbc::draw2d
{
namespace
{
using bozo::render::Material;
using bozo::render::Renderer;
using bozo::render::Texture;
using bozo::render::Vertex;
uint8_t *g_ram = nullptr;
uint32_t rd32(uint32_t a)
{
uint32_t v;
std::memcpy(&v, g_ram + (a & 0x1FFFFFFu), 4);
return v;
}
uint64_t rd64(uint32_t a)
{
uint64_t v;
std::memcpy(&v, g_ram + (a & 0x1FFFFFFu), 8);
return v;
}
int16_t rd16s(uint32_t a)
{
int16_t v;
std::memcpy(&v, g_ram + (a & 0x1FFFFFFu), 2);
return v;
}
float rdf(uint32_t a)
{
float v;
std::memcpy(&v, g_ram + (a & 0x1FFFFFFu), 4);
return v;
}
const uint8_t *ptr(uint32_t a) { return g_ram + (a & 0x1FFFFFFu); }
bool trace()
{
static const bool on = std::getenv("SBC_TRACE_2D") != nullptr;
return on;
}
// ---- Textures ------------------------------------------------------------------------------------------------
// One of the game's image transfers (a TEXimage's packet): its format, size and the EE bytes it sends.
struct Transfer
{
uint32_t psm = 0, width = 0, height = 0, data = 0, bytes = 0;
bool valid = false;
};
Transfer transferOf(uint32_t packet)
{
Transfer t;
if (!packet)
return t;
for (uint32_t q = 1; q < 16; ++q)
{
const uint32_t at = packet + q * 16u;
const uint64_t lo = rd64(at);
const uint32_t w2 = rd32(at + 8), w3 = rd32(at + 12);
if (((lo >> 28) & 7u) == 3u && ((w3 >> 24) & 0x7Eu) == 0x50u) // a REF tag with DIRECT: the data
{
t.data = static_cast<uint32_t>(lo >> 32) & 0x0FFFFFF0u;
t.bytes = static_cast<uint32_t>(lo & 0xFFFFu) * 16u;
t.valid = t.width && t.height;
return t;
}
if (w3 == 0 && (w2 & 0xFFu) == 0x50u) // A+D BITBLTBUF
t.psm = static_cast<uint32_t>(lo >> 56) & 0x3Fu;
else if (w3 == 0 && (w2 & 0xFFu) == 0x52u) // A+D TRXREG
{
t.width = static_cast<uint32_t>(lo) & 0xFFFu;
t.height = static_cast<uint32_t>(lo >> 32) & 0xFFFu;
}
}
return t;
}
uint32_t bitsPerPixel(uint32_t psm)
{
switch (psm)
{
case 0x00: return 32; // CT32
case 0x01: return 24; // CT24
case 0x02: case 0x0A: return 16; // CT16, CT16S
case 0x13: return 8; // T8
case 0x14: return 4; // T4
default: return 0;
}
}
uint32_t colour16(uint16_t c)
{
const uint32_t r = (c & 31u) << 3, g = ((c >> 5) & 31u) << 3, b = ((c >> 10) & 31u) << 3;
return r | (g << 8) | (b << 16) | ((c & 0x8000u) ? 0x80000000u : 0u);
}
// The CLUT's colours in index order (CSM1: the 16-wide image the game uploads, read in the GS's order).
std::vector<uint32_t> paletteOf(const Transfer &c, uint32_t entries)
{
std::vector<uint32_t> out(entries, 0);
const uint32_t bpp = bitsPerPixel(c.psm);
if (!c.valid || (bpp != 32 && bpp != 16) || c.width == 0)
return out;
for (uint32_t i = 0; i < entries; ++i)
{
uint32_t x, y;
if (entries == 16)
{
x = i & 7u;
y = i >> 3;
}
else
bozo::render::csm1Position(i, x, y);
const uint32_t at = y * c.width + x;
if (at * (bpp / 8) + bpp / 8 > c.bytes)
continue;
if (bpp == 32)
{
uint32_t v;
std::memcpy(&v, ptr(c.data) + at * 4, 4);
out[i] = v;
}
else
{
uint16_t v;
std::memcpy(&v, ptr(c.data) + at * 2, 2);
out[i] = colour16(v);
}
}
return out;
}
struct CachedTexture
{
uint64_t hash = 0;
std::shared_ptr<const Texture> texture;
};
std::unordered_map<uint64_t, CachedTexture> g_textures; // by (texel address, CLUT address)
uint64_t g_textureVersion = 0;
// The TEXimage's texture as RGBA (alpha as the GS has it: 0x80 opaque), or null when it is not decodable.
std::shared_ptr<const Texture> textureOf(uint32_t image)
{
const Transfer t = transferOf(rd32(image));
// SBC_TRACE_TEXTURE_FAILS=1: each TEXimage not decoded, once, with what its packet holds.
static const bool fails = std::getenv("SBC_TRACE_TEXTURE_FAILS") != nullptr;
static std::unordered_map<uint32_t, bool> failed;
if (!t.valid)
{
if (fails && !failed[image])
{
failed[image] = true;
const uint32_t packet = rd32(image);
std::printf("[sbc:2d] texture %08x: no transfer found in packet %08x (format %02x %ux%u):", image, packet,
t.psm, t.width, t.height);
for (uint32_t q = 0; q < 8 && packet; ++q)
std::printf(" %08x%08x_%08x%08x", rd32(packet + q * 16u + 12), rd32(packet + q * 16u + 8),
rd32(packet + q * 16u + 4), rd32(packet + q * 16u));
std::printf("\n");
}
return nullptr;
}
const uint32_t bpp = bitsPerPixel(t.psm);
if (!bpp || static_cast<uint64_t>(t.width) * t.height * bpp / 8 > t.bytes)
{
if (trace() || (fails && !failed[image] && (failed[image] = true)))
std::printf("[sbc:2d] texture %08x: format %02x %ux%u in %u bytes not decoded\n", image, t.psm, t.width,
t.height, t.bytes);
return nullptr;
}
const Transfer c = bpp <= 8 ? transferOf(rd32(image + 0x10)) : Transfer{};
const uint32_t texelBytes = t.width * t.height * bpp / 8;
uint64_t hash = bozo::hash::xxh64(ptr(t.data), texelBytes, t.psm);
if (c.valid)
hash = bozo::hash::xxh64(ptr(c.data), c.bytes, hash);
const uint64_t key = (static_cast<uint64_t>(t.data) << 32) | c.data;
CachedTexture &slot = g_textures[key];
if (slot.texture && slot.hash == hash)
return slot.texture;
auto tex = std::make_shared<Texture>();
tex->width = t.width;
tex->height = t.height;
tex->rgba.resize(static_cast<size_t>(t.width) * t.height);
tex->key = key;
tex->version = ++g_textureVersion;
tex->asset = true;
const uint8_t *src = ptr(t.data);
const size_t count = tex->rgba.size();
if (bpp == 8 || bpp == 4)
{
const std::vector<uint32_t> palette = paletteOf(c, bpp == 8 ? 256u : 16u);
for (size_t i = 0; i < count; ++i)
{
const uint32_t index = bpp == 8 ? src[i] : (i & 1 ? src[i / 2] >> 4 : src[i / 2] & 15u);
tex->rgba[i] = palette[index];
}
}
else if (bpp == 32)
std::memcpy(tex->rgba.data(), src, count * 4);
else if (bpp == 24)
for (size_t i = 0; i < count; ++i)
tex->rgba[i] = src[i * 3] | (src[i * 3 + 1] << 8) | (src[i * 3 + 2] << 16) | 0x80000000u;
else
for (size_t i = 0; i < count; ++i)
{
uint16_t v;
std::memcpy(&v, src + i * 2, 2);
tex->rgba[i] = colour16(v);
}
if (trace())
std::printf("[sbc:2d] texture %08x: format %02x %ux%u texels %08x CLUT %08x (%02x) decoded\n", image, t.psm,
t.width, t.height, t.data, c.data, c.psm);
slot = {hash, tex};
return slot.texture;
}
// ---- The frame's quads ---------------------------------------------------------------------------------------
struct Quad
{
uint32_t bucket = 0, order = 0;
Material material;
Vertex v[6];
std::shared_ptr<const Texture> texture;
};
std::vector<Quad> g_quads;
uint32_t g_order = 0;
struct Screen
{
float offsetX = 0, offsetY = 0, scaleX = 1, scaleY = 1;
};
Screen screenOf(uint32_t gp)
{
Screen s;
s.offsetX = static_cast<float>(static_cast<int32_t>(rd32(gp - 0x7fecu)));
s.offsetY = static_cast<float>(static_cast<int32_t>(rd32(gp - 0x7fe8u)));
s.scaleX = rdf(gp - 0x7fe4u);
s.scaleY = rdf(gp - 0x7fe0u);
return s;
}
Screen g_screen;
bool g_screenKnown = false;
// g2dSpriteSetPkt(sprite): the quad kept for the kick instead of the packet.
void spriteSetPkt(uint8_t *rdram, R5900Context *ctx, Runtime *)
{
g_ram = rdram;
const uint32_t sprite = ctx->r[4].uw[0], gp = ctx->r[28].uw[0];
ctx->r[2].ud[0] = 0;
ctx->pc = ctx->r[31].uw[0];
const uint32_t desc = rd32(sprite + 0x40);
if (!desc)
return;
const int16_t id = rd16s(desc);
std::shared_ptr<const Texture> texture;
if (id >= 0 && id < static_cast<int32_t>(rd32(gp - 0x7cccu)))
if (const uint32_t image = rd32(rd32(gp - 0x7cc0u) + 4u * static_cast<uint32_t>(id)))
texture = textureOf(image);
const Screen s = screenOf(gp);
if (!g_screenKnown || std::memcmp(&s, &g_screen, sizeof s) != 0)
{
g_screen = s;
g_screenKnown = true;
if (trace())
std::printf("[sbc:2d] screen offset (%g, %g) scale (%g, %g)\n", s.offsetX, s.offsetY, s.scaleX, s.scaleY);
}
const uint32_t flags = rd32(sprite + 8);
const float w = rd16s(desc + 2), h = rd16s(desc + 4);
const float u0 = rd16s(desc + 8), v0 = rd16s(desc + 0xa), u1 = rd16s(desc + 0xc), v1 = rd16s(desc + 0xe);
const float px = rd16s(sprite + 0xc), py = rd16s(sprite + 0xe);
const float sx = rdf(sprite + 0x10), sy = rdf(sprite + 0x14);
const float x = rd16s(sprite), y = rd16s(sprite + 2);
const float rx = rdf(sprite + 0x20), ry = rdf(sprite + 0x24), rz = rdf(sprite + 0x28);
const bool rotated = (flags & 2u) != 0;
// The corners in the game's order: (0,0), (0,h), (w,0), (w,h), each with its UV and colour.
const float cx[4] = {0, 0, w, w}, cy[4] = {0, h, 0, h};
const float cu[4] = {u0, u0, u1, u1}, cv[4] = {v0, v1, v0, v1};
Vertex corner[4];
for (int i = 0; i < 4; ++i)
{
float vx = (cx[i] - px) * sx, vy = (cy[i] - py) * sy, vz = 0;
if (rotated)
{
// Rz, then Ry, then Rx (the game's 0x2222a0, 0x222150, 0x222010 in that order).
const float cz = std::cos(rz), sz = std::sin(rz);
float tx = cz * vx - sz * vy, ty = sz * vx + cz * vy;
vx = tx;
vy = ty;
const float cyy = std::cos(ry), syy = std::sin(ry);
tx = cyy * vx + syy * vz;
const float tz = -syy * vx + cyy * vz;
vx = tx;
vz = tz;
const float cxx = std::cos(rx), sxx = std::sin(rx);
ty = cxx * vy - sxx * vz;
vy = ty;
}
Vertex &v = corner[i];
v.x = (vx + x) * s.scaleX + s.offsetX;
v.y = (vy + y) * s.scaleY + s.offsetY;
v.z = 0;
const float tw = texture ? static_cast<float>(texture->width) : 1.0f;
const float th = texture ? static_cast<float>(texture->height) : 1.0f;
v.u = cu[i] / tw;
v.v = cv[i] / th;
v.rgba = rd32(sprite + 0x30 + ((flags & 1u) ? 4u * static_cast<uint32_t>(i) : 0u));
}
Quad q;
q.bucket = rd32(gp - 0x7e80u);
q.order = g_order++;
q.texture = texture;
Material &m = q.material;
m.textured = texture != nullptr;
m.blend = Material::Blend::Alpha;
m.depthTest = false;
m.depthWrite = false;
m.nearest = true;
m.clampU = m.clampV = true;
m.textureAlpha = true;
m.tex0 = texture ? texture->key : 0;
const int order[6] = {0, 1, 2, 2, 1, 3};
for (int i = 0; i < 6; ++i)
q.v[i] = corner[order[i]];
if (trace())
std::printf("[sbc:2d] sprite %08x bucket %u texture %d (%gx%g uv %g,%g-%g,%g) at (%g, %g)-(%g, %g) rgba %08x\n",
sprite, q.bucket, id, w, h, u0, v0, u1, v1, corner[0].x, corner[0].y, corner[3].x, corner[3].y,
corner[0].rgba);
g_quads.push_back(std::move(q));
}
// A text glyph (0x223170, from the font system's string drawer 0x223030 after it decoded the glyph into the
// glyph TEXimage at 0x5bc4b0 with _g2dFontDecode): a0 the string entry (+4 x, +8 y, +0xc w, +0x10 h as ints in
// the font's units, +0x14 Z, +0x20..+0x2c R, G, B, A), a1 the sprite template (UV at +0x30 and +0x40, 12.4
// texels). The game's screen terms: offset gp-0x7fdc / gp-0x7fd8, scale gp-0x7fd4 / gp-0x7fd0.
void glyph(uint8_t *rdram, R5900Context *ctx, Runtime *)
{
g_ram = rdram;
const uint32_t entry = ctx->r[4].uw[0], tmpl = ctx->r[5].uw[0], gp = ctx->r[28].uw[0];
ctx->pc = ctx->r[31].uw[0];
const std::shared_ptr<const Texture> texture = textureOf(0x5bc4b0u);
const float sx = rdf(gp - 0x7fd4u), sy = rdf(gp - 0x7fd0u);
const int32_t ox = static_cast<int32_t>(rd32(gp - 0x7fdcu)), oy = static_cast<int32_t>(rd32(gp - 0x7fd8u));
const int32_t x = static_cast<int32_t>(rd32(entry + 4)), y = static_cast<int32_t>(rd32(entry + 8));
const int32_t w = static_cast<int32_t>(rd32(entry + 0xc)), h = static_cast<int32_t>(rd32(entry + 0x10));
const float x0 = static_cast<float>(ox + static_cast<int32_t>(static_cast<float>(x) * sx));
const float y0 = static_cast<float>(oy + static_cast<int32_t>(static_cast<float>(y) * sy));
const float x1 = x0 + static_cast<float>(static_cast<int32_t>(static_cast<float>(w) * sx));
const float y1 = y0 + static_cast<float>(static_cast<int32_t>(static_cast<float>(h) * sy));
const uint32_t uv0 = rd32(tmpl + 0x30), uv1 = rd32(tmpl + 0x40);
const float tw = texture ? static_cast<float>(texture->width) : 1.0f, th = texture ? static_cast<float>(texture->height) : 1.0f;
const float u0 = static_cast<float>(uv0 & 0x3FFFu) / 16.0f / tw, v0 = static_cast<float>((uv0 >> 16) & 0x3FFFu) / 16.0f / th;
const float u1 = static_cast<float>(uv1 & 0x3FFFu) / 16.0f / tw, v1 = static_cast<float>((uv1 >> 16) & 0x3FFFu) / 16.0f / th;
const uint32_t rgba = (rd32(entry + 0x20) & 0xFFu) | ((rd32(entry + 0x24) & 0xFFu) << 8) |
((rd32(entry + 0x28) & 0xFFu) << 16) | ((rd32(entry + 0x2c) & 0xFFu) << 24);
Quad q;
q.bucket = rd32(gp - 0x7e80u);
q.order = g_order++;
q.texture = texture;
Material &m = q.material;
m.textured = texture != nullptr;
m.blend = Material::Blend::Alpha;
m.depthTest = false;
m.depthWrite = false;
m.nearest = false;
m.clampU = m.clampV = true;
m.textureAlpha = true;
m.tex0 = texture ? texture->key : 0;
const float cx[4] = {x0, x0, x1, x1}, cy[4] = {y0, y1, y0, y1};
const float cu[4] = {u0, u0, u1, u1}, cv[4] = {v0, v1, v0, v1};
Vertex corner[4];
for (int i = 0; i < 4; ++i)
{
corner[i].x = cx[i];
corner[i].y = cy[i];
corner[i].u = cu[i];
corner[i].v = cv[i];
corner[i].rgba = rgba;
}
const int order[6] = {0, 1, 2, 2, 1, 3};
for (int i = 0; i < 6; ++i)
q.v[i] = corner[order[i]];
g_quads.push_back(std::move(q));
}
bozo::render::Screen rendererScreen()
{
bozo::render::Screen s;
s.originX = g_screen.offsetX;
s.originY = g_screen.offsetY;
s.width = 640.0f * (g_screen.scaleX ? g_screen.scaleX : 1.0f);
s.height = 448.0f * (g_screen.scaleY ? g_screen.scaleY : 1.0f);
return s;
}
} // namespace
std::shared_ptr<const bozo::render::Texture> textureOfImage(uint8_t *rdram, uint32_t image)
{
g_ram = rdram;
return textureOf(image);
}
namespace
{
// The menus' copy of the screen (0x27fa80, from 0x27f640 and 0x2807e0 as a menu opens; M_03_02's entrance too):
// the 640 x 448 frame buffer read back in 64 x 224 tiles (0x27fc70: sceGsSetDefStoreImage, sceGsExecStoreImage)
// and halved (0x27fb60: 2 x 2 averages, alpha 0x80) into ten buffers of 32 x 224 pixels at *(0x5f5c90 + 4 n), the
// columns of a 320 x 224 picture the menu is drawn over. Natively: the picture the window presents next
// (Renderer::readPicture: the 3D, the 2D, before the settings window), averaged down to 320 x 224 and laid out the
// same way. Nothing presents (headless): the buffers stay as they are.
void screenGrab(uint8_t *rdram, R5900Context *ctx, Runtime *)
{
ctx->pc = ctx->r[31].uw[0];
g_ram = rdram;
const bozo::render::Screen s = rendererScreen();
std::vector<uint32_t> rgba;
if (!Renderer::instance().readPicture(0.0f, 0.0f, s.width, s.height, 320, 224, rgba) || rgba.size() != 320u * 224u)
{
if (static int told = 0; told++ < 4)
std::printf("[sbc:2d] screen grab (0x27fa80): no picture (nothing presents); the menu's background buffers "
"are left as they are\n");
return;
}
for (uint32_t n = 0; n < 10; ++n)
{
const uint32_t buffer = rd32(0x5f5c90u + 4u * n) & 0x1FFFFFFu;
if (buffer < 0x100000u || buffer + 32u * 224u * 4u > 0x2000000u)
continue;
for (uint32_t y = 0; y < 224; ++y)
std::memcpy(g_ram + buffer + y * 32u * 4u, &rgba[y * 320u + n * 32u], 32u * 4u);
}
if (static int told = 0; told++ < 4 || trace())
std::printf("[sbc:2d] screen grab (0x27fa80): the presented picture, 320 x 224, into the ten buffers at "
"0x5f5c90\n");
}
} // namespace
void install(bozo::port::Registry &r)
{
Renderer &renderer = Renderer::instance();
renderer.setEnabled(true);
renderer.setPresentsAlone(true);
renderer.setClearColour(0);
r.registerFunction(0x222780, spriteSetPkt); // g2dSpriteSetPkt
r.registerFunction(0x223170, glyph); // the font system's glyph sprite
r.registerFunction(0x27fa80, screenGrab); // the menus' half-size copy of the screen
}
void publish()
{
Renderer &renderer = Renderer::instance();
const bozo::render::Screen screen = rendererScreen();
renderer.beginFrame(screen); // the frame's quads are kept until here (a movie's frames come between)
draw3d::submit(renderer); // the frame's 3D first, the 2D over it
draw3d::screenEffects(renderer, screen);
std::stable_sort(g_quads.begin(), g_quads.end(),
[](const Quad &a, const Quad &b) { return a.bucket != b.bucket ? a.bucket < b.bucket : a.order < b.order; });
for (const Quad &q : g_quads)
renderer.drawOverlay(q.material, q.v, 6, q.texture);
renderer.publishOverlay(screen);
renderer.endFrame();
g_quads.clear();
g_order = 0;
}
} // namespace sbc::draw2d