Status: stage 1 + stage 2a + stages 2b1–2b4 + initial 3a/3b/FS (2)

The GPU's work runs on one Vulkan device (src/3ds/gpu/vk/), which needs Vulkan 1.3 and 8-bit storage buffers. The board's RAM regions reach it in one of three ways: imported where the driver takes host memory (VK_EXT_external_memory_host), moved into the device's own memory where it will not (below), or a copy in the device's host-visible memory where it takes neither (below). A GPU that shares the system's memory reads and writes the guest's memory in place on the first two; a discrete GPU works on a copy of it in its own memory; and the third keeps a copy the CPU fills and reads back by page. Memory fills, display transfers and texture copies are compute shaders (gpu/vk/shaders/), held byte for byte to the CPU reference gpu/transfer.c by tests/3ds_vk_transfer.c. The Y2R engine (y2r:u, services/y2r.c), which turns a video's YUV frames to RGB, converts on the GPU too (gpu/vk/y2r.c). Its streams are the application's virtual memory, so each page goes through a table to its physical page. tests/3ds_vk_y2r.c holds it to the CPU reference gpu/y2r.c, ported from Azahar's. PICA draws run on the GPU too (gpu/vk/render.c). The vertex program is decompiled (gpu/vk/vsgen.c; it and the interpreter gpu/shader.c decode the instructions with the decoder tools/decodetree makes from gpu/pica.decode) and the fragment pipeline generated (gpu/vk/fsgen.c), both ported from Azahar's shader generators and both written as SPIR-V words directly (src/common/utilities/spirv_emit.c, shared with NX), with no shader compiler at run time. The output merger is the pipeline's fixed state. A new pipeline is made as its first draw needs it, which pauses the game while the driver compiles it; with the Switch's async_pipelines=1 (--async-pipelines=1) workers make it instead (pipeline_threads of them) and the draws that need it are skipped until it is ready, so what they draw shows up a few frames late, and the run's end says how many were skipped. The driver keeps the pipelines it compiled between runs, as for a Switch game, in shader_cache/vk_pipelines.<driver>.bin of the game's folder, one file for each driver. The file is read before the first draw and written at the end of the run, and also while the game runs after 20 s in which no pipeline was made, so a run that stops early keeps what it made. A file another GPU or another build of the driver wrote, or one cut inside its header, is not given to the driver, and the log says why. The Switch's vk_pipeline_cache=0 neither reads nor writes it, and 3ds_vk_pipeline_cache and 3ds_vk_draw_pipeline_cache check the file. Where the device has VK_EXT_shader_object (issue #1454), no pipeline is made at all: a draw binds its vertex and fragment shaders as shader objects and sets the output merger, the vertex layout and the rest as dynamic state, changing only what differs from the draw before it in the same recording. A new shader is still compiled as its first draw needs it, or by the workers with async_pipelines=1, but a scene's few dozen shaders replace its hundreds of pipelines. The driver's binary of each shader is kept between runs in shader_cache/vk_shaders.<driver>.bin, written as the pipeline cache is, so a later run makes each shader from its binary with no compile; the file names the device's shader binary UUID and version, and one of another device or driver version is not read. The shared parts are in src/common/gpu/vk/shader_object.c. vk_ext_shader_object=off draws through pipelines on such a device, and the drivers' bans (src/common/gpu/vk/extension_bans.txt) and the other vk_ext_* settings hold for the 3DS's device as for a Switch game's. 3ds_vk_shader_object checks the binaries' file, and 3ds_vk_draw_pipelines the draws through pipelines. With shader objects and async_pipelines=1, a draw whose shaders are still being made is drawn with stand-ins, two shaders made once per run. The vertex one (gpu/vk/shaders/standin.vert) runs the vertex program from a buffer, with gpu/shader.c's flow control and vsgen.c's arithmetic, registers and outputs, its config in push constants and the integer attributes in scaled formats, which it reads as floats. The fragment one (gpu/vk/shaders/standin.frag) reads the fragment state from push constants, and follows fsgen.c's lighting, stages, rounding, sampling, alpha test, fog, scissor and depth. It takes every draw but those with gas, a procedural texture, shadow rendering, or a shadow or cube texture on unit 0, which are still skipped. Where the device keeps infinities and NaNs, both declare it as the generated shaders do. ctr_stand_in=0 skips the draws, and ctr_stand_in=2 gives the stand-ins every draw they draw, which 3ds_vk_draw_stand_in holds to the CPU reference. Vertex and index data are read where the GPU works (in place, or its copy), except in a draw with a geometry shader, whose vertex and geometry shaders run on the CPU (see below). tests/3ds_vk_draw.c holds the draws to the software renderer they replaced, which stays as their reference. The shown framebuffers are decoded and turned to the landscape screen by a compute shader straight from guest memory into the window's swapchain; the frame dumps, --shot and the tests' hashes are the readbacks.

What the GPU makes stays on it, and guest memory catches up only when something reads it. A render target's images are kept, and loaded from guest memory by a compute shader only when memory changed under them. The pages a draw makes newer stay the GPU's until the target is stored, on the GPU, for a GPU engine that reads that memory (a display transfer, a texture over it, the presenter), or for the CPU when it touches them. Textures are decoded once and kept while their memory is unchanged. Work goes to the GPU in submissions that nothing waits for, a few in flight at once (gpu/vk/device.c), and each GX queue run's interrupts are raised when its work is submitted. Not when it is done, as the console raises them: the DSP and the vblanks keep the host's time, so a GPU slower than the console's would leave a frame without its audio.

Write tracking keeps the guest from seeing any of this unfinished (snx_ctr_track_* in memory/ctr_memory.h). Each physical page has a level: the CPU's; watched, when the GPU keeps something made from it, a submission not done reads it or the GPU's copy of it holds the same bytes, so a write traps; or the GPU's, when a target there is newer, a submission not done writes it or the GPU's copy of it is newer, so every access traps. Every guest view of the page is protected to its level, and so are the memory API's accesses and a bus master's by physical address. A trap is resolved by the renderer, on the faulting thread:

A guest fault (memory/host.h) is taken by SIGSEGV or SIGBUS on Linux, FreeBSD and the other POSIX hosts, on an alternate stack large enough to wait for the GPU, and by a vectored exception handler on Windows. tests/3ds_memory_track.c checks the levels through the flat space and the API with a stand-in renderer.

A discrete GPU reads and writes each byte of guest memory it works on in place across the bus, every time: a draw's vertices each frame, a target stored and the display transfer that reads it back, the framebuffer the presenter decodes. There the GPU works on a copy of the RAM in its own memory instead (gpu/vk/device.c), kept in step with the RAM by page (gpu/vk/render.c), and the bus carries only what changes hands. A page the CPU wrote since is copied in before the GPU reads or writes it, by uploads that run before everything their submission holds (snx_vk_ring_uploads, so no rendering pass ends for them), and is watched from then on. A page the GPU wrote is the GPU's until the CPU's first access, which copies it back with the pages the GPU wrote around it (up to 256 on each side) and waits for that. A write copies its stale pages in first even where it covers them, as one need not write a whole page and a page goes back whole; when the device goes, what the GPU wrote goes back. The copy is taken as the Switch's setting vk_memory says, with its meaning (--vk-memory=N, which the launcher passes from its Debug settings): 0, the default, the copy unless the device shares the system's memory (snx_vk_memory_model), 1 the copy, 2 the RAM in place. The device says on the standard error which it took and the value, and when its memory cannot hold the copy, what refused it and that the run takes vk_memory=2. Its cost is the CPU's writes: a page the CPU rewrites after the GPU read it traps at each rewrite, where in place it traps only while a read is not done. The run's summary has a gpu: line: each screen's frames, what the GPU's work read and wrote, what was copied in and back, and the CPU's accesses that trapped. The pinned homebrew's title screen (its target) on an RX 6700 XT (RADV, resizable BAR), three 60 s runs each, by that line: what crosses the bus falls to 6.1 MiB a second, 245 KiB a frame, all of it copied in, from 76.6 in place (3018 KiB a frame: 36.8 MiB a second read, 39.8 written); amdgpu has the GPU busy 14.4 ms a second against 17.8; the CPU's time is the same, 323 and 325 ms a second, though its writes trap 61 times a frame against 0.2. tests/3ds_vk_copies.c checks which pages are copied in and back and when, and every GPU test runs again on each path (_copy, _in_place: --vk-memory=1 and 2), so lavapipe checks the copy too.

Compiled code addresses the guest flat: a 4 GiB host reservation where guest address va is base + va, each mapping a view of the board's RAM held in one host memory object (src/common/memory/host.c, the GBA window's too: a memfd on Linux and Android, an unlinked shm_open object on the other POSIX systems, a pagefile section mapped into placeholders on Windows 10 1803 and later). Every guest address of a physical page (a heap page and its MAP mirror, shared memory, the linear heap) is the same memory, and the HLE and the GPU read and write that object directly. Blocks read and write cpu->mem + va with no callback; a host fault in the reservation is the guest's data abort, reported with the block it happened in. A host whose pages are not 4 KiB (Apple silicon, some AArch64 and PowerPC64 kernels) keeps the checked bus, and its modules are compiled for it.

The cores take turns on the one host thread, each scheduling its own threads (a slice ends at an SVC or after SNX_CTR_POLL_BLOCKS blocks), so a busy thread on the system core leaves the application core's threads their time, as on the console. TLS pages come from the BASE region, outside the application's pool, as Azahar allocates them: a game that commits all of its pool still starts threads.

Debug switches are settings of g_ctr_debug_ini in src/nx/runtime/settings.txt, given as --key=value: --ctr-ipc-trace=1 logs each service request with the thread and its answer, --ctr-svc-trace=1 each SVC that fails (2: every SVC), --ctr-gpu-trace=1 each GX command and framebuffer swap, --ctr-jit-trace=1 each block compiled at run time. A run ended by --seconds (or a stop) reports each thread's core, priority, pc, what it waits on (whose event it is) and the return addresses on its stack, how long the process sat idle, and its most frequent SVCs.

SuperTux runs past main and its RomFS reads. It draws its title and loading screens through the Vulkan renderer, a port of Azahar's PICA pipeline (command processor, shader units, rasterizer, texturing, combiners, lighting), and plays its title music through a port of Azahar's HLE DSP. The software port of the shader units and vertex processing stays as the CPU reference of the GPU draws. A run of about 2 billion native blocks presented frames until it was ended. It is not playable yet:

The parity roadmap lists the remaining work, and the SuperTux integration target holds the pinned input.


Made from docs/platforms/3ds/README.md at 69237645142c. GPL-3.0-or-later.