GPU submit/present

  1. The game resolves its NVN functions through nvnBootstrapLoader and nvnDeviceGetProcAddress. The loader (gpu/nvn/nvn_loader.c) puts the runtime's handler in each one's dispatch slot (gpu/nvn/nvn.hook), so the game's NVN driver never runs past its own name lookup. A game without nvnBootstrapLoader draws nothing.
  2. Command buffer calls write records into the game's command memory (gpu/nvn/nvn_cmd.c). A queue submission promises its fences and queues the replay on the GPU thread (gpu_call), and returns at once.
  3. The GPU thread replays the records in order (gpu/nvn/nvn_replay.c): NVN's state makes the renderer's draw state (gpu/draw_state.h), and the draws, clears, dispatches and copies go to g_renderer (gpu/renderer.h). Shaders: Maxwell SASS -> SPIR-V (gpu/shader/).
  4. Vk stream_incr lands compute writes in guest memory before incrementing the syncpoint.
  5. A window present queues its texture after the queue's work. vi's own present path (a program that uses vi with its own buffers) waits for its fences on the calling thread, then queues the present (gpu_call). present_buffer uses the matching RT and the crop, or converts guest memory.

A program that writes its own command buffers (an OpenGL game, homebrew) submits them with /dev/nvhost-gpu's SUBMIT_GPFIFO (gpu_submit_gpfifo in hle/services/nvdrv.c). The ioctl promises the fence and queues the entries (gpu_submit), and the GPU thread runs them through the Maxwell front end: gpu.c pulls the GPFIFO and hands each method to its engine in gpu/maxwell/. The 3D engine's registers make the same draw state as NVN's (maxwell/draw_state.c), and its draws go to g_renderer. The macro engine (maxwell/mme.c) interprets every macro, with no macro HLE or JIT.

The shader programs a game ships are translated at startup, on the renderer's shader file thread rather than the GPU thread, into <id>.<driver>.vkshipped (prepare_shaders: before the game's first thread runs, behind main.c's loading screen, or in the background as it runs). gpu/shipped_pass.c walks the RomFS and the executables' segments, and gpu/shader/shipped.c finds the programs by NVN's code wrapper (or a bare NVN shader file's layout) in their bytes, and in the Yaz0 SARCs whose names list a shader container. In a BNSH, a program's code record names its control section, which gives where its constants (the bank 1 that its draws bind) start in its code area. The translation reads the jump tables of BRX from them, and a draw takes such a shader only while its bank 1 holds the same words. gpu/vk/pipeline.c translates each into the file, which a draw's lookup reads from when it has no shader of its own. The files the walk found nothing in are kept in <id>.vkscan by their name, place and size, under the hash of the walk's sources (cmake/ship_walk.cmake) rather than the build, so a new build or driver reads only the files that hold programs again.

A game's own Vulkan (guest_vulkan)

A game that links NVIDIA's Vulkan driver (one module exports the loader's vk* entry points) runs that driver as recompiled code by default. It writes Maxwell command buffers into the GPFIFO above. With the experimental guest_vulkan setting, gpu/gvk/gvk.c puts a function of its own into the dispatch slot of each entry point that the driver's module (the one that exports vkGetInstanceProcAddr) exports. gpu/gvk/gvk.hook routes the game's direct calls to them, and a module recompiled before that list stops the game. gpu/gvk/gvk_api.c executes the calls on the renderer's device and graphics queue after compatibility fixes. The driver's code then runs only for calls that no slot takes. A host without memory import (gvk_check_device), a 32-bit game and a big-endian host give the slots back, and the game's own driver runs. Save states cannot hold GVK objects.

Calls and memory:

What the console's driver takes and the host's does not:

Present: the swapchain's images are host images, and a present blits one into the renderer's window (present_image). It applies the android::Surface crop rectangle from vkCreateViSurfaceNN, scaled to host pixels, so that dynamic resolution fills the display. If the next present has reached the display queue, acquire waits for it within the application's timeout. Otherwise the renderer's copy already released the guest image from the display.

Resolution scaling: the resolution setting is a whole factor for GVK, none below 1.5×, otherwise the nearest integer. It is at most the factor at which the largest swapchain the game can make (1920x1080, maxImageExtent) fits the host's framebuffers (maxFramebufferWidth and maxFramebufferHeight, within maxImageDimension2D): 8 on an RX 9070, whose images reach 32768 but its framebuffers 16384. A target gets a shadow only where it fits them too. GVK scales an image by its use, not by its size or usage flags (one game gives every texture COLOR_ATTACHMENT).

Renderer coherence + lifetime

Invalidation: host write tracking + mem_unmap -> gpu_mem_changed. Vk treats zeroed unmap/heap-return ranges as written at the next cache check. Write tracking (plat_ww_scan) is userfaultfd's asynchronous write protection with PAGEMAP_SCAN on Linux 6.7 and later, and the reservation's write watch on Windows. Every other POSIX host (macOS, FreeBSD, Android, older Linux; write_tracking=1 on any Linux) protects the pages a scan reported and takes the first write to each in the fault handler, which opens the page again. A system call can't fault into the handler, so the runtime opens what the kernel will write before the call (plat_ww_open, next to own_acquire). With write_tracking_hot_pages at 1, or 2 (Auto, the default) a page the scans find written twice in a row stays writable and reads as written at each scan, for 32 scans, without its fault. A cache takes a page reported so as one more check of its bytes, but a render target's write-back keeps memory's bytes on the pages written since its baseline: each baseline protects the target's hot pages again first, at every address of them (vk_ww_cool, after the writes so far were taken in).

Renderer.save: optional pipeline-cache persistence every 300 frontend ticks (~5s) + exit, concurrent with execution. Exit skips renderer teardown and guest-thread joins.

Videodec/VIC channels

Each NVDEC/VIC fd owns a host1x_open channel, registers and syncpoints; NVDEC also owns a decoder. NVDEC writes guest surfaces; VIC composites output.


Made from docs/dev/architecture.md at 69237645142c. GPL-3.0-or-later.