Shaders

The shader code is Ampere SASS, which the runtime calls SM86 (gpu/shader/sm86.h). Its format is the one Volta (SM 7.0) brought and Ada (SM 8.9) still has: 128-bit instructions, with the opcode in bits 0 to 11 of the low word. Each program ends with EXIT (0x794d, control word 0x000fea0003800000), then a BRA to itself (0xfffffff000007947, 0x000fc0000383ffff). The Maxwell decoder can't read it. The SM86 frontend has Mesa's NAK (MIT, src/nouveau/compiler/nak/sm70_encode.rs) as the reference for the encoding.

The program starts at the shader data's GPU address, with no wrapper (the older formats have one of 0x30 bytes). A compute program starts with its first instruction. A graphics program starts with a 128-byte program header of version 4: word 0 is 0x020c0481 for a vertex program and 0x000c5482 for a fragment program.

An Ounce control section has 0xC9 at 0x04, where the older formats have 1. Formats 7 and 11 share one layout: 0x30 the header's bytes (0xd8), 0x38 the code's bytes, 0x3c the constants' bytes, 0x40 the constants' offset (the code's bytes rounded up to 256; the constants are bank 1), 0x4c the registers, the stage in the halfword at 0x56, and a compute program's workgroup size at 0x58, 0x5c and 0x60, and its shared memory bytes at 0x64 (0 in most programs, up to 0xc000). Programs run to 250 KB. A compute program has no NVN header with its size before it, so the renderer reads as many bytes as its constants' offset gives, up to 512 KiB (cs_read_code). With the Switch's 64 KiB window, one game's compute program of 256,160 bytes had no end in it, and each of its dispatches (one workgroup of 32, about one a frame) was skipped as not translated.

Bytes 0xa8 to 0xbb are the driver's record of a compute program's c[0]. Byte 0xa8 is the kind. A kind 0 program reads the driver's table as on the Switch: the grid at 0x200, uniform buffers from 0x210 and storage buffers from 0x310, 16 bytes each, and its handles in bank 2. A kind 1 program reads a packed c[0], which the driver's macro 0xeac writes: its bytes rounded up to 16, the grid, the root tables (push constants), the uniform and storage buffers, then the low words of its texture, separate texture, separate sampler and image handles, as many of each as bytes 0xa9 to 0xb6 count. c0_packed in gpu/nvn/nvn_draw.c builds it. The byte counts add up to the size in the halfword at 0xba for every program checked. The driver's nvnProgramSetShaders (sdk offset 0x5bf8b0) reads them, and it checks the version and the format at 0x5bfde8. One control section also holds SPIR-V 1.6 at +0x128.

A compute program reaches its storage buffers through 64-bit addresses (LDG, STG), with no check against a size. Of about 400 storage buffers of one game's programs, 5 read the size word after their address, so the size the game binds doesn't bound the accesses: binding exactly that size hung the GPU, with accesses past it. The renderer binds all of the memory mapped from the address, up to 16 MiB (vk_gbuf_desc).

So such a program's storage is not made for each dispatch. Its stores log the bytes they write: the translator has every store and atomic of a compute program set a bit for each byte it writes in the dispatch's write log (mark_written, binding SNX_VKB_WPAGES, the layout SH_WLOG_* in shader.h): a part for each buffer it writes, a flag for each 4 KiB window of the buffer, then the window's bits. A store past the windows sets none. The Maxwell translator's compute programs log the same way (write_log.c, which both translators call), so a Switch game's results land from the log too. They don't take the mirror below: in one Switch game, props that a compute program culls vanished for single frames with it where the host traps guest memory (mirror_plan has the counts).

Where the device has sparse residency of buffers and write tracking is on, the program reads and writes a mirror of guest memory in the GPU's memory (vk/mirror.c): one sparse buffer as big as the guest's address space, at the same offsets, with a mask beside it, a bit for each byte, memory bound in granules of 2 MiB where bindings are. Windows over the same guest memory are the same bytes, and the programs run one after another on the graphics queue, as on the console. After each dispatch a pass ORs the bits its log gives into the mask at each buffer's place, the bits past the buffer's length dropped (mirror_fold.comp, vk_mirror_fold). Only the GPU reads such a log, so it is in the GPU's memory (vk_wlog_get_gpu) and cleared there after the pass (vk_wlog_clear). In host memory each store's log write, a store and an atomic OR, crossed the bus on a discrete GPU: on an RTX 5080 one upscaler program took 47 ms a dispatch for 0.14 ms, and each pass, which read the log, 14 ms for 0.3 ms. A merge of the main branch had lost the dispatch's side of the mirror for a while: each writing dispatch made its 16 MiB buffers again, its input copied on the CPU and composed on the GPU over the results before it, hundreds of them a frame at addresses a few KiB apart, and one game's races ran at about one frame a second. A host shadow keeps the guest bytes each page was given. When a buffer is bound, its pages the CPU wrote since (write tracking) are compared with the shadow, and only the bytes the CPU changed are copied in, so the bytes the programs wrote there and haven't landed stay. At each submit a pass lists the pages written in it, those with mask bits set, with their masks, and clears the masks (mirror_harvest.comp). Once the submit is done, where the host traps guest memory, each listed page is queued, still pending (own_entries), and stays GPU-owned in the mirror from the next point where nothing is being recorded (vk_mirror_publish): before the releases its submits gate let the game see them, at the end of a GPU job, and before a CPU read of them. There the CPU's changes to it since it was brought up to date are copied into the mirror (a byte both wrote keeps the CPU's, which came after the submit), and once that copy is submitted the page is published over guest memory with the mirror as its owner (docs/layers/gpu/ownership.md). Nothing of this is recorded where the results land: that runs inside a frame's recycling, a submission and a draw's setup, and recording there went into a command buffer being reset or submitted, which crashed in the driver. The pass for the pages past the harvest's room runs there, in a command buffer of its own, and takes its descriptor sets from a pool of its own: the frame's sets (vk_desc_alloc) started the frame's command buffer inside the landing after a present's submission. It stayed open, and when its slot came round again the draws recorded into it past the recording thread, with sets only that thread can resolve. The CPU's first touch of a page lands it whole, which is exact as none of its bytes can change in between, and the mirror lets go of its shadow then (vk_mirror_landed). Pages the CPU never reads never come back. Without traps the pass copies the pages' bytes too, and they land in guest memory and in the shadow once the submit is done, the bytes logged and no others, as do the pages the CPU touched in the last presents with traps. A logged byte that guest memory no longer holds as the shadow has it keeps the CPU's: the shadow holds every CPU write from before the submit, so the CPU wrote it later. Landed over it, the write was lost to guest memory and the mirror both (one game's characters showed squares of wrong lighting). Landing every written page at each submit took most of the GPU thread in one game's menus. A read of guest memory under pending writes lands them first, waiting for the submission that wrote them, or finishing the GPU's work where they are still being recorded, and a draw reading them takes the mirror instead (buffer.c's buf_get), as does a draw reading a range part of which the mirror owns (vk_mirror_holds): read elsewhere, the CPU's bytes between the owned pages were staged again for each draw. Before, every writing dispatch copied the 16 MiB of each range twice (its original bytes, and its input), compared all of it after the dispatch and copied it back from the GPU, and the per-dispatch buffers took gigabytes of rings.

Without the mirror (no sparse residency of buffers, memory shared with the CPU, no write tracking) each writer's result is made for the dispatch as before and lands from the dispatch's write log (vk/writelog.c), which the host reads once the submit is done. A writer that logs nothing (a draw, a buffer image) is compared.

Without the mirror, a byte the CPU writes while a program's logged writes are pending, which the program writes too, takes the program's value (vk_write_runs): nothing keeps the bytes from before the dispatch to tell the two apart, and on the console both reach memory in an order the program left open. In the mirror the shadow tells them apart: a byte that differs from it was written after the submit, and keeps the CPU's value (above). Once the submit is done and the page is the mirror's, the CPU's write traps and lands the page first, so the CPU's value comes last there too. A byte the CPU writes and the program doesn't is kept. The comparison took the CPU's value instead; over 80 seconds of one game, 14,000 results landed with no byte written by both.

The translator declares the global buffers Aliased and Coherent: one game binds a range and a part of it to the same program, writes through one and waits for the value through the other.

A program may also reach a buffer through a pointer in memory. One game's material programs load the addresses of five tables from a table whose own address is in cbuf 0 (LDG.E.64 R, [UR + 8]), then index them (IMAD.WIDE), and its compute programs pick one of two such pointers with a predicate. The tables are gigabytes away from the table that points to them, outside its 16 MiB. The translator names such a buffer by its pointer's cell (SH_GBUF_INDIRECT in shader.h, the cell's offset in the buffer of a constant buffer word): the renderer reads the pointer from guest memory when it binds the draw or dispatch, a pending write there landed first (gbuf_pointer in vk/render.c), and binds the memory mapped from it as a bare address's, and the program finds the buffer's base by loading the cell itself. An address that may be either of two pointers is compared with each buffer when it runs (global_dynamic). A cell that the GPU writes later in the stream than the bind would be read too early: the program would then index the window bound from the older pointer. Each such buffer is another window of up to 16 MiB bound for the draw, with the in-place bookkeeping of its pages. A general path (physical storage buffers, VK_KHR_buffer_device_address) would need every guest page a pointer can reach imported and tracked, which the import limits and the ownership model above don't allow.

RED and ATOMG of F32 add (.FTZ.RN) are one OpAtomicFAddEXT where the device has shaderBufferFloat32AtomicAdd, else a loop of compare and exchange on the word's bits.


Made from docs/platforms/nx/ounce.md at 69237645142c. GPL-3.0-or-later.