How the Rust port draws a frame: which PlayStation rasterisation traits are always on, the ones that are opt-in, how per-texel semi-transparency becomes two passes, the enhancement passes that leave the faithful render bit-identical when off, and the screen-space overlay that stands in for retail’s ordering table. Retail’s own renderer is on renderer; how a pixel gets its colour is on shading.
legaia-engine-render: winit + wgpu, a software VRAM page sampled in the fragment shaderlegaia-render-kernels - wgpu-free GTE maths, screen primitives, VRAM capture, emitters - linked by the browser host tooRenderer::set_psx_mode gates the GTE’s register limits, vertex snap, 15-bit dither, the GPU’s 5-bit blend arithmetic and the field’s draw order onlyengine-ui, shared with the browser host| Trait | Gate | What it reproduces |
|---|---|---|
| Affine UV interpolation | unconditional | the texture warp on surfaces with steep depth gradients. It is the faithful behaviour, not an effect |
| Flat texture page + palette per primitive | unconditional | each triangle samples one page and one palette |
| Texture modulation | unconditional | texel * colour / 128 with the baked colour word - the game’s whole lighting on most paths |
| Semi-transparency | set_semi_blend, on by default | the four blend equations, per texel |
| 5-bit blend arithmetic | PSX mode | the GPU’s truncating 5-bit blend: a half-and-half layer drops the half step, a quarter layer adds F>>2, and the frame reads as a 15-bit framebuffer (renderer) |
| GTE register limits | PSX mode | a corner whose eye-space X / Y or depth passes the GTE’s 16-bit IR / SZ saturation lands where RTPS wrote it, not where a plain projection puts it (renderer) |
| Sub-pixel vertex snap | PSX mode | the per-vertex shimmer on slow geometry; corners truncate onto the 320×240 grid and each scanline takes its texels at the row itself |
| 15-bit ordered dither | PSX mode | the cross-hatched gradients of a 5-bit-per-channel framebuffer |
| Field draw order | PSX mode | a nearer primitive losing to a farther one, as the ordering table paints it (below) |
The play page’s PSX frame is single-sample. The page’s context has MSAA, which cannot change after creation and which WebGL cannot turn off on the canvas’s framebuffer. So a PSX frame draws into a single-sample framebuffer of its own (site/js/webgl-psx-target.js) and is copied to the canvas once; on the canvas a polygon edge would resolve to a blend of partly covered samples, which neither retail’s rasteriser nor the native renderer has. The enhanced mode keeps its MSAA.
There is no synthetic light. Retail runs no light source on the field path: shading is baked into each primitive’s colour word and the only colour operation is the depth cue. A missing colour stream defaults to white, and white reads as too bright, not unlit - the usual sign that a draw lost its colours.
Whether a texel blends is decided per texel: bit 15 of the palette entry picks blend or opaque inside one semi-transparent primitive. So the opaque pass draws everything and discards those texels, and a blend pass re-draws only the semi-transparent triangles, keeping only them.
A whole draw can be made semi-transparent too. Retail reads the top byte of an actor's colour word as primitive flags and ORs them into every packet, so one byte turns every primitive of the actor into a blended one. The port carries that byte per draw: the opaque pass treats every primitive of the draw as semi-transparent, and the blend pass re-draws the whole mesh in the forced mode. Underground Octam's corridor pillars use it (field-VM 4C D6).
| Mode | Equation |
|---|---|
0 | 0.5 * B + 0.5 * F |
1 | B + F (additive) |
2 | B - F (subtractive) |
3 | B + 0.25 * F |
Order mirrors the ordering table at per-primitive granularity. Every blended primitive is keyed by its centroid depth and the whole list blends far to near regardless of draw boundaries; equal keys draw later-submitted-first, as a bucket of the retail table does. Untextured primitives have no per-texel gate and blend every pixel.
In PSX mode the field pass draws each primitive at the depth of its ordering-table link rather than per pixel (Renderer::set_field_prim_order). Retail has no depth buffer, and three rules set the link:
0x800, on a decoration cell and on a placed object alike. A prologue’s 31 1F raises the actor’s high bit on a placed object or a seated prop, and the floor sampler raises 0x800000 on the player each step over a cell without bit 0x1000. A ground cell keeps the bucket its own emitter gives it.The three rules hold against a state’s table: on koin1_field_route_walk every quad of the floor sheet sits at the bucket of its farthest corner, and the terrain’s wall quads at their mean, ten buckets behind the sheet’s rule. The mean alone, with no record flags and no ground exception, hides floors and cabinets. With them concend_field_door_hop reads 1.000 where per-pixel depth reads .942: the sky’s quads link among the far ridge’s and cover them. A clean frame keeps per-pixel depth.
Not modelled: a walking NPC’s 0x800000, a posed prop’s record flag 0x800, and the order inside one bucket. Links FUN_80043B58, FUN_80045194; actor base FUN_8001ADA4, FUN_8001B964; floor sampler FUN_80019278.
--no-ssao turns it off) darkens creases and contact points from a depth and normal pre-pass of the opaque scene, blurred along depth and normal edges and multiplied in right after the opaque draws, so water, glass, halos, effects and the HUD never darken. Characters receive none - the estimator would read an arm against a torso as a crevice - but still occlude, so the ground under their feet keeps its contact shadow. In battle the stage counts as environment: the backdrop shell, the ground grid and the backdrop’s ribbon take the same AO a field floor does. It runs where frames are drawn through the scene camera with that actor split - the field, expansion maps, the world map and a battle - and nowhere else: a minigame venue (the Baka duel, the Muscle Dome, the fishing pond, the dance hall, the slot machine) owns its frame and camera, and both hosts ask one shared scope test before staging the pass.Each is identity when disabled: the shader helper returns its input when the uniform enable is zero, so the disabled path is pixel-identical to the faithful render.
A screen primitive is a textured quad or a flat quad carrying an ordering-table index. The pass reproduces the retail walk - farthest bucket first, later-submitted-first inside a bucket - and draws coalesced runs by blend class. Primitives are authored in the 320x240 display space every retail emitter clamps against, and the overlay stretches over the surface.
Its consumers are the things retail draws as 2D packets into the 3D table: battle streaks and trails, the field-to-battle transition styles, billboards. The corner projection is the GTE’s own, including its reciprocal and its saturating screen coordinates, so a corner behind the camera smears the way hardware does.
Backdrops draw first. A screen primitive whose four corners sit at the far plane is one retail links into the farthest bucket - the cast colour wash and the overworld sky band. Composited after the 3D pass, a blended primitive writes no depth, so a wash drawn there would erase every additive effect over the sky. Both hosts split those backdrops off the under list and draw them in the scene pass, after the clear and before the first 3D draw.
The battle sorts by each primitive's link. A battle has one ordering table, and the bodies, the after-image ghosts, the effect parts, the draw-kind-4 nodes and the backdrop shell all go through the same per-primitive leaves, which link each primitive at its corners' mean depth. Nothing on the GPU tests depth, so a monster and the additive ring around it sort by link, not by pixel: on Dohati's Chaos Breath the ring's packets sit in buckets nearer than every packet of the body, and the ring paints over the belly even where the belly stands in front of it. In PSX mode both hosts draw every battle mesh primitive at its corners' mean depth, as they do on the field; an effect part whose colour word carries the far-corner bit sorts at its farthest corner, as a field prop does (Inferno's tunnel wall stays behind its flames that way); the ground grid keeps its far bucket, and the screen primitives (trails, streaks, cast-arm packets) still composite over the 3D frame. Clean rendering keeps per-pixel depth: a mean pops as the camera turns, which the retail frame shows and the clean frame need not.
The renderer only ever pushed the software page to the GPU. The capture path is the missing direction: it quantises a readback to 15-bit colour and writes it into a rect of the CPU-side page, so the result is visible to every reader of that page. The field-to-battle transition needs it - the tile shatter and the curtain sample the field frame that was on screen when the encounter fired. The frame read back is the one the window presents, screen layers and enhancement passes included, less the transition’s own primitives.
Two retail nuances are stated rather than hidden: the transition’s depth-cue bank has no channel in the overlay, and retail subtracts from the display buffer each frame instead of clearing it, so its strips leave fading trails where the port’s leave black.
A retail battle frame's farthest bucket holds an opaque black rect, and a cast arm that arms its subtractive wash has that quad take the rect's place, so the frame's additive layers sum over the frames before it. That is why Ozma's fans read white on retail while a frame rasterised from the same packets on black is dark teal with thin red rays.
The native target is the one target that outlives its frame, so it reproduces this: while a cast's arms keep the frame, each frame loads an earlier one instead of clearing, and a capture seeds from the same one. Retail's earlier frame is the same display page two game frames back, eight vsyncs at the frame step a heavy arm runs at, so the renderer keeps a ring of eight frames; building on the last frame instead fills the buffer eight times too fast. The ring fills from the moment a cast module is resident, not from the first frame that keeps: an arm arms the wash a pass after the arm before it hands over, so its first kept frames read pages from before the arm. Started on copies of the last frame, Thunder Storm's second pass of arm 4 built on its first pass's white where retail's other page still held the dark frame before the arm. The play page does the same under PSX rasterisation (LegaiaPsxTarget in webgl-psx-target.js holds the same ring), and bloom stands the frames down on both hosts, since it reads the frame it adds to. The window's own target and the browser's canvas still clear, and there the additive arms draw dimmer than retail.
LEGAIA_PROFILE=1 prints a rolling one-second timing breakdown from the native window: frame average and percentiles, draw counts, and the tick / pose / draw-list / encode / submit / present split. It is free when off - every entry point short-circuits on one cached flag. scripts/ci/frame-bench.py and frame-bench-web.mjs run it, and the browser's own counters, over a fixed scene set (frame benchmark).
Two things the benchmark drove: a rebuilt mesh writes over last frame's buffers, since a buffer dropped within two frames of its birth goes back to a pool that the next mesh of its exact size draws from; and the shadow stage draws only what each light reaches, since a draw wholly outside a lamp's frustum writes no depth into that lamp's layer.
The blend tails and dither parity, the occlusion tunables, the transition emitter style by style, the battle HUD and menu chrome pipelines and the reference ports of retail’s draw-decision kernels are in docs/subsystems/renderer-port.md.