benilla-re systems structs docs

system models · source

models: the shared model

The shared model, CM2Shared: one per .m2 file, holding the file's bytes with every offset turned into a pointer, the view the device draws, its textures and its device buffers. Every instance of the file points at it.

The code runs from 0x71cd10 through 0x71e0a0, with the header fixups in the next object file, 0x71e0c0 through 0x71f840, and the buffer helpers and index sort in the last, 0x71f860 through 0x71faa0. A shared model is 0x164 bytes, created and cached by the cache (cache); its fixed-up header is md, at +0x130.

Roots: 0x71d5e0, 0x71d610

Shared model object

offsetholds
+0x000, +0x004reference count, cache
+0x008flags: 1 loaded, 2 the open flag the load was given
+0x00cread request while loading
+0x010instances waiting for the load (link +0x1c/+0x20 in the instance)
+0x014, +0x018, +0x01crelease list links, release time
+0x020file name, 0x104 bytes
+0x124, +0x128, +0x12cbucket links, name hash
+0x130, +0x134file data, file size
+0x138, +0x13cselected view, texture handles
+0x140 to +0x14cdevice index buffer handles, device vertex buffer handles
+0x150, +0x154, +0x158per-batch vertex and pixel shader ids, copy of the view's submeshes
+0x15c, +0x160instance copies the device buffers hold, most instances one batched draw may take

Addresses: 0x71d2c0, 0x71d330, 0x71d430, 0x71d480 Confidence: verified Proof: one Test: none

0x71d2c0 stores the cache at +0x4, clears flags 1 and 2 of +0x8, sets +0x0 to 1 and zeroes the pointers listed. The destructor unlinks +0x124/+0x128, closes md+0x5c texture handles from +0x13c, frees the buffers and +0x150, +0x154, +0x158 (count-prefixed), and frees the file data through 0x71f9d0.

Loading

Load(file, loose) records loose != 0 as flag 2, allocates a buffer of the file's size and queues a read request whose completion closes the file and finishes the load, whatever the flag (0x71d590). Finishing fixes up the header, initializes the view, sets the loaded flag and initializes every instance that was waiting. An instance added after the load initializes at once.

Addresses: 0x71d4e0, 0x71d5e0, 0x71d610, 0x71d640, 0x71d5a0, 0x71f9a0, 0x70ebd0 Confidence: verified Proof: one Test: none

0x71d4e0 stores the size at +0x134, the buffer from 0x71f9a0 at +0x130, and fills the request with the file, buffer, size, the shared model, the completion 0x71d5e0 at +0x10 and the read function 0x71d610 at +0x14. 0x71d640 returns at once when flag 1 of +0x8 is set, runs 0x71cdf0 and 0x71d6c0, sets flag 1, and unlinks each waiting instance before 0x70ebd0.

Header fixup

The file starts with an MD20 header of version 0x100 or 0x101 and at least 0x154 bytes. Every array is a count and an offset; the fixup checks that the array ends inside the file and replaces the offset with a pointer, or with 0 for an empty array, and does the same inside the views, colours, textures, transparencies, texture frames, texture transforms, attachments, events, lights, cameras, ribbons and particles. Any check that fails makes the load fail.

headerarrayelement
+0x08name1 byte
+0x14 to +0x2cglobal sequences, sequences, sequence lookup, playable table4, 0x44, 2, 4 bytes
+0x34, +0x3c, +0x44bones, key bone lookup, vertices0x6c, 2, 0x30 bytes
+0x4c, +0x54, +0x5cviews, colours, textures0x2c, 0x38, 0x10 bytes
+0x64, +0x6c, +0x74transparency, texture frames, texture transforms0x1c, 0x1c, 0x54 bytes
+0x7c to +0xacreplaceable lookup, materials, then five 16-bit lookups2, 4, 2 bytes
+0xec to +0xfccollision indices, vertices, normals2, 12, 12 bytes
+0x104 to +0x134attachments, attachment lookup, events, lights, cameras, camera lookup, ribbons0x30, 2, 0x2c, 0xd4, 0x7c, 2, 0xdc bytes
+0x13c; +0x144, +0x14cparticles; two 16-bit arrays in version 0x1010x1f8; 2 bytes

Addresses: 0x71cdf0, 0x71ce27, 0x71ce36, 0x71d284, 0x71cd10, 0x71e0c0, 0x71e110, 0x71e160, 0x71e1b0, 0x71e210, 0x71e270, 0x71e2f0, 0x71e440, 0x71e4f0, 0x71e5e0, 0x71e6d0, 0x71e880, 0x71e8d0, 0x71e9d0, 0x71eaa0, 0x71edb0, 0x71ef40, 0x71f210 Confidence: verified Proof: one Test: none

0x71ce27 compares the magic with 0x3032444d and 0x71ce36 the version with 0x100 and 0x101. Each helper takes the base, the size and the pair: offset greater than size, or offset + n·size greater than size, returns 0. The element sizes are the multipliers of the helpers: 0x71e0c0 8, 0x71e110 4, 0x71e160 2, 0x71e1b0 12, the others their imul constants. The version-0x101 arrays at 0x71d284 are fixed up after the result is recorded, even on failure.

View selection

The view the shared model draws is the one with the most bones that still fits the vertex shader palette: up to 256 bones, or (constants − 31) / 3 when vertex shaders are on and that is smaller. Without a fitting view the load fails. A material with blend 5 or above is marked unlit. With vertex or pixel shaders the view's batches get their shader ids, and every texture with a name is created with its wrap flags.

Addresses: 0x71d6c0, 0x71df30, 0x449d90 Confidence: verified Proof: one Test: none

0x71d6c0 multiplies constants − 0x1f by 0xaaaaaaab and shifts right by 1. A view's bone count is its +0x28; ties keep the later view. +0x160 is 0x10000 divided by the view's +0x8, lowered to the view's bone count divided by each submesh's +0xc, at least 1. Materials are header +0x88; the test sets bit 0 of the flags when the blend word is 5 or more. A texture record whose name length +0x8 exceeds 1 calls TextureCreate (0x449d90) with its flag bits 1 and 2 moved to bits 8 and 0x10 of the cache's texture settings and flag 2 of +0x8 as the open flag, so the textures may come from loose files exactly when the model file could (0x71da5e).

Vertex shader id

A batch's vertex shader id is 5·(3·(3·Z + t0) + t1) with Z = G + 2·L + 4·B + 20·P: G is 1 when material flag 2 is clear, L 1 when flag 1 is clear, B the submesh's word +0x10 and P 1 when the cache grants pixel shaders (bit 0x10). The digit t_k of stage k is 0 from the batch's texture count +0xe on; else 2 when the coordinate lookup entry (header +0xa0, unit +0x12 + k) is above 2; else 1 when the transform lookup entry (header +0xb0, unit +0x16 + k) is below the transform count +0x74; else 0. The scene adds the nearest-light count to a lit element's id and may rewrite B.

Addresses: 0x71de50, 0x71de93, 0x71dea5, 0x71deaa, 0x71debe, 0x71deeb, 0x71df18, 0x71d909, 0x711695, 0x707cf6, 0x707cfe, 0x70aff0, 0x70b040 Confidence: verified Proof: two Test: models::vertex_shader_id_difftest

0x71de93 shifts bit 4 of the cache's features [[this+4]+4] into P. The loop 0x71dea5 runs k below 2: jae to digit 0 at the count, cmp word,2; jbe at 0x71deaa to digit 2, 0x71debe compares the transform entry with +0x74 to digit 1. 0x71deeb is not ax on the material's flags word; the lea chain to 0x71df18 builds Z, then 3·x + t twice and 5·x. 0x70aff0 splits an id into id mod 5, mod 3 twice, & 1 twice, mod 5 and the quotient, so t1, t0, G, L, B and P are digits 5 to 0 and digit 6 is the light count. 0x71d909 and 0x711695 call it with the unit and its submesh; 0x707cf6 adds [collector + 0x180] (lights) to the stored id when the material's flag 1 is clear, before 0x70b040. A word B above 4 selects the programs of the other P, and B + 5·P above 9 an id past the 1800 slots. vertex_shader_id_difftest runs 0x71de50 whole.

Model vertex programs

Shaders\Vertex\Model2.bls holds 1800 programs per level, one per vertex shader id: bytecode for Direct3D vertex shader 2.0 (level 1), ARB text for OpenGL (level 2). A program reads the vertex constants c6 to c9 only as stage matrices: c6 and c7 when t0 is 1, c8 and c9 when t1 is 1; digits 0 and 2 read none. Digit 2 writes coordinate set k as 0.5 + 0.5·R.xy/|R| with R = P − 2(P·N)N: P is the position and N the normalized normal, in the space c2 to c5 project from; the 0.5 is a literal. Digit 0 passes coordinate set k through; digit 1 writes the dots of the two constants with (u, v, 0, 1).

Addresses: 0x706510, 0x71de50, 0x595130, 0x5951e6, 0x595211, 0x598320, 0x5990a7, 0x5990ae, 0x5990b6, 0x58e6b1, 0x58e6c7, 0x70b8d0, 0x70cd16, 0x70cd34, 0x58b330 Confidence: verified Proof: two Test: models::model2_program_constants

0x595211 calls slot 0 of the shaders in array order, so record i of the level's set is slot i of the 1800 that 0x706510 loads. Level 1 is set at 0x5990a7 and level 2 at 0x58e6b1 (gx, Shader files). Over the 1800 records of each level, by index digits: every program of a pair (t0, t1) reads the constants above and no other of c6 to c9, 200 programs a pair; relative addressing has only c31 to c33 as base, and def sets only c0. The rows c31 to c33 of the bone matrix take P and the 3 by 3 part N when B is 0; a skinned program blends c31[A0.x + i] by the weights first. The generated stage: Direct3D takes dp3 of position and normal, mad with -N, dp3 and rsq of R, mul of R.xy, then mad by a literal 0.5 from def c0; ARB the same with a PARAM literal, and writes (0, 1) to z and w. 0x70b8d0 also writes (0.5, 0, 0, 0.5) and (0, 0.5, 0, 0.5) to c(6 + 2k), c(7 + 2k), and 0x70cd16 to 0x70cd34 sends them. The census is made over the install's file.

Lit program colour

A program without the pixel digit (P = 0) writes the front colour c28 · (E, 1) + c29 with alpha c28.w when its material is lit (L = 1), and c28 + c29 when it is not. The point light count d is the id's last digit, 0 to 4, which the draw adds from the lighting record when material flag 1 is clear and vertex shaders are granted. With d = 0, E is the harmonic sum with no clamp; with d of 1 to 4 E is clamped to [0, 1] per channel before the multiply. The unlit colour ignores d.

Addresses: 0x707b6f, 0x707b7a, 0x707b87, 0x707c01, 0x707c8f, 0x707c98, 0x707ca7, 0x707ceb, 0x707cf6, 0x707cfe, 0x70b040, 0x71bff5, 0x70c625, 0x70c712, 0x70a239 Confidence: verified Proof: one Test: models::model2_lit_programs_d3d, models::model2_lit_programs_arb

0x707c98 tests bit 8 of the cache's features [[this+4]+4]. 0x707ca7 skips the rest when the local [ebp-0x3c] is not 0; that local is set when batch flag 4 is set (0x707b6f) and the scene's special callback [scene+0x11c], stored by 0x70a239, is non-null (0x707b7a), else 0 (0x707b87), and such a special batch becomes element kind 1 (0x707c01) and keeps id −1 (0x707c8f). 0x707ceb tests material flag 1; clear, 0x707cf6 loads [[ebp-0x14]+0x3b8]+0x180, the lighting record's count ([ebp-0x14] is the model instance), adds it to the id and stores it at +0x38 before 0x70b040. 0x71bff5 is jae on count 4. Direct3D clamps E with max 0 and then min 1; ARB with MIN 1 and then MAX 0. c28 and c29 are stored at 0x70c712 (render#material-colours): the instance colour [model+0x1a0] times the unit's colour entry (0x70c625), with the element alpha, and [model+0x1ac] with w 0. Saturation of the colour output after the multiply is the interface's rule, not a byte of the file. The tests read the 1800 programs of each level: the clamp pair and its order, no clamp at d = 0, E.w = 1, the copy of c28, and the unlit program the same for every d.

Pixel program outputs

A program with the pixel digit (P = 1) writes no front colour and reads no c10 to c16, c28 or c29. It writes the light term to texture output 0 (xyz): 1 when unlit, 0 when lit at d = 0, and for d of 1 to 4 the sum of the lights clamped to [0, 1], with no harmonic terms. Texture output 1 takes the normalized normal, and the stage coordinates move to outputs 2 and 3. Its lights, fog and normal are those of P = 0.

Addresses: 0x70c696, 0x70c6d5, 0x70baf0 Confidence: verified Proof: one Test: models::model2_lit_programs_d3d, models::model2_lit_programs_arb

The pixel stage takes the model colours as pixel constants c8 and c9 (0x70c696, 0x70c6d5) and the harmonic vectors as c1 to c7 (render#lighting-lanes). The tests assert, over the 450 P = 1 programs of each level, the output each case writes, its clamp pair for d of 1 to 4, the lights read, and that no colour output, c10 to c16, c28 or c29 is touched.

Light constants

A lit program with d lights reads c17 to c(16 + d), the light colours, c21 to c(20 + d), their view positions, and c25, c26, c27, the constant, linear and quadratic attenuation with component k for light k. With P = 0 it also reads c10 to c16, the harmonic vectors, and c28, c29. Each light adds colour · max(0, N·v/|v|) / (c25 + c26·|v| + c27·|v|²) to E with v the light position minus the vertex, in the space c2 to c5 project from, and N the bone-transformed normal, normalized. Direct3D reads c30 when fog is on.

Addresses: 0x70bcf3, 0x70baf0, 0x71c4e0 Confidence: verified Proof: one Test: models::model2_lit_programs_d3d, models::model2_lit_programs_arb

The harmonic vectors are lights#harmonic-constants; render#lighting-lanes has the light constants' stores. The set of c10 to c30 each program reads is exactly this over all 1800 programs per level, and none of c17 to c27 at d = 0. The ARB program reads them as c10[0..6], c17[0..d-1], c17[4..3+d] and c17[8..10], whose swizzles name the first d components.

Program fog

When the material allows fog (G = 1) the Direct3D program writes oFog = min(max(z·c30.x + c30.y, 0), 1) per vertex, z the view-space depth, and the ARB program writes z to the fog coordinate; with G = 0 both write 0. c30 is (−r, end·r) with r = 1/(end − start), or (0, 0) with fog off. Vertex programs run at the Direct3D level only on a device with vertex shader 2.0, and the default gxApi is direct3d.

Addresses: 0x70bdc7, 0x70bdd9, 0x70c00a, 0x71c128, 0x599098, 0x5990ae, 0x58e6b1 Confidence: verified Proof: one Test: models::model2_lit_programs_d3d, models::model2_lit_programs_arb

z is the dot of the third bone matrix row with the position (dp4 r.z, c33 at the Direct3D level, a DP4 with c31[2] at ARB; a skinned program blends the row first). 0x70bdc7 and 0x70bdd9 store −(+0x18c) and +0x188 · +0x18c, where +0x18c is r stored as a float (fdivr at 0x71c128), so c30.y is the float-rounded product; 0x70c00a zeroes both without fog (render#lighting-and-fog). No ARB program reads c30 (program.local[30] is in none of the 1800), so the factor comes from the device's fog state. The device's vertex-program level is 1 for Direct3D vertex shader 2.0 or later (0x599098), 0 for 1.1 (0x5990ae), which runs fixed-function, and 2 for OpenGL (0x58e6b1); the model cache grants vertex shaders for 1 or 2 (cache#feature-mask). The choice of API is gx/device#backend-selection-and-creation.

Device buffers

The buffers hold +0x15c copies of the selected view, one per instance of a batched draw; a load sets it to 1, and drawing a batched run of n raises it to n rounded up to 16, at most +0x160, and frees the buffers to be rebuilt. Per copy the vertex buffer holds the vertices the view's lookup names, in lookup order, 48 bytes each, their bone index bytes replaced by the view's palette bytes plus copy · bones. Per submesh and copy the index buffer holds the submesh's triangle entries plus copy · lookup count with shader ids, else the copy's vertex offset less the submesh's first vertex; the submesh copy in +0x158 gets its run's start.

Addresses: 0x71dab0, 0x71dc80, 0x71dfa0, 0x71dff0, 0x71d7c8, 0x71db97, 0x71dbc9, 0x71dc25, 0x71dd90, 0x71ddbd Confidence: verified Proof: one Test: none

0x71dfa0(n), called only at 0x70d3bf in the batched run draw, grows +0x15c when it is below both n and +0x160: (n + 15) & ~15, capped, then 0x71dff0 frees +0x144, +0x140, +0x14c and +0x148. 0x71dab0 sizes view+0x8 · +0x15c words; per submesh it reads the start +0x8 of the view's record and copies +0xa entries of view +0xc from there (0x71dbc9), each plus the base, which starts at 0 when +0x150 is set and at −copy+0x4 otherwise (0x71db97) and grows per copy by view +0x0 or the copy's +0x6; 0x71dc25 stores the run start in the copy's +0x8. 0x71dc80 sizes view+0x0 · +0x15c · 48 bytes, copies the vertex view+0x4[p] for each p in a submesh's range (0x71dd90) and stores view+0x14[p] + copy · +0xc · 0x01010101 at +0x10 (0x71ddbd). The buffers are named CM2Shared_idx and CM2Shared_vtx.