# The 3D engine: geometry and rendering Source: GBATEK "DS 3D Video" (Overview, I/O Map, Display Control, Geometry Commands, Matrix Load/Multiply/Types/Stack, Polygon Attributes and Definitions, Light Parameters, Shadow Polygons, Texture Attributes/Formats/Coordinates/Blending, Toon/Edge/Fog/Alpha- Blending/Anti-Aliasing, Status, Tests, Rear-Plane, Final 2D Output); gbadev-org ndsdoc (CC0, 3D command pages, a younger and smaller text). DOC unless marked. The 3D engine has no counterpart in gba-python or anywhere else in the family: it is new work. ## Two engines with a double buffer between them - **Geometry engine** (on the ARM9's I/O): a 256-entry command FIFO (4000400h, packed command words) and command ports (4000440h-40005FFh) feed a 4-entry PIPE; commands do matrix work (projection, position, position-and-vector and texture matrices with their stacks: 1, 31, 31-with-vectors and 1 deep), set vertex attributes (colour, normal with up to four directional lights, texture coordinates and the texture matrix modes), submit vertices (six formats), clip polygons against the view volume and write the results to **vertex RAM** (max 6,144 vertices) and **polygon RAM** (max 2,048 polygons). BOX_TEST, POS_TEST and VEC_TEST answer the CPU through GXSTAT and result registers. Every command has a cycle cost in 33.51 MHz units (GBATEK's table: MTX_MULT_4x4 35, VTX_16 9, NORMAL 9-12 by lights, BOX_TEST 103, SWAP_BUFFERS "until VBlank, plus 392"), and the FIFO's fill level, GXSTAT's busy bits and the GXFIFO IRQ (IF bit 21, level-triggered) are visible to the CPU, so the geometry engine's **timing is in the observable contract**. - **SWAP_BUFFERS** exchanges the two sets of vertex and polygon RAM at the next V-blank: the rendering engine draws what the geometry engine built in the previous frame. Texture VRAM and the rendering registers (4000060h, 4000330h-40003BFh) are not swapped. - **Rendering engine**: draws the polygon list scan line by scan line into a **48-line cache** (no frame buffer), starting at line 214 (in V-blank) and keeping ahead of the display; RDLINES_COUNT reports the minimum buffered lines of the previous frame, and DISP3DCNT bit 12 flags an underflow. The output becomes engine A's BG0 (and can go to the capture unit). Per pixel: Z or W depth buffering, texture formats (4/16/256-colour, A3I5, A5I3, 4x4 compressed, direct), texture blending (modulation, decal, toon/ highlight), shadow polygons with a stencil, translucency with polygon IDs, alpha test, edge marking (8 edge colours by polygon ID), fog (32-entry density table), anti-aliasing of edges, a clear colour or a rear-plane bitmap from texture slots 2-3. ## What makes it hard The geometry engine's arithmetic is fixed point and documented in detail (matrix formats, the 1/W division, lighting with a specular table); it can be specified exactly and judged by reading results back (CLIPMTX_RESULT, VECMTX_RESULT, POS_RESULT, VEC_RESULT, BOX_TEST). The rasteriser is the open problem of DS emulation: the exact edge-walking and interpolation (perspective-correct for textures and colours, with the hardware's own precision), the polygon-ordering and translucency rules, the anti-aliasing coverage, the fill rules for edges and 1-dot polygons, and the per-line time budget that decides which lines underflow. GBATEK documents the parameters but not the rasteriser's exact arithmetic. The known hardware-recorded data for it is **polyrastertest** (Jakly; MIT; expected pictures recorded on a New 3DS XL), which is **set aside** with the rest of its author's work pending Aubrey's word (provenance.md); StrikerX3's line test (no licence, sources only) is credited by it. Without them, the rasteriser's exactness has no judge here: the plan is to specify from GBATEK, mark the arithmetic HYPOTHESIS, and add judges (hardware captures or respect-clean test ROMs with recorded outputs) when found. ## Modelling level (proposal) - Geometry engine: **command level with each command's cycle cost**, FIFO and PIPE occupancy modelled so GXSTAT and the IRQ are right at the CPU's cycle; the arithmetic bit-exact. - Rendering engine: **functional per line, with a per-line budget**: draw each line's pixels in the hardware's order with its arithmetic, run ahead of the display by up to 48 lines; the timing model only decides RDLINES_COUNT and underflow. A pure producer (domains.md): nothing it does is visible to the CPU except through capture, the underflow flag and RDLINES_COUNT. ## Unknowns (U) and contradictions (C) - U1 The rasteriser's exact interpolation and edge rules (no judge: above). - U2 The per-line rendering budget (cycles per polygon, per pixel) that decides RDLINES and underflow. - U3 NORMAL's 9-12 cycles for 0-4 lights (GBATEK's own "Huh"). - U4 The PIPE's depth and how FIFO-full stalls the CPU's write (bus wait or dropped?). - U5 Which geometry edge cases are bit-exact in the documents (division precision, the "1-dot" depth boundary). - U6 Whether 3D differs between DS models (polyrastertest's note: none known). - C1 GBATEK's command table and ndsdoc's pages: compared in the build, any difference a C-item (ndsdoc is CC0 and newer; GBATEK cites more tests).