gsp::Gpu is modeled with real bounded resources: RegisterInterruptRelayQueue
returns the genuine 0x2a07 first-initialization result, one writable 0x1000
GX/interrupt shared block and up to four relay threads with real interrupt-queue
slots and guest-created events. AcquireRight/TryAcquireRight/ReleaseRight own a
single right, and WriteHWRegs, WriteHWRegsWithMask and SetBufferSwap fail with
0xD8202A06 without it, as in Azahar. WriteHWRegs and WithMask take the forms
libctru sends (data in static buffer 0, mask in 1) and check them as Azahar does:
an address below 0x420000, at most 0x80 bytes in whole words. No register
they could change is modeled (framebuffers come from SetBufferSwap and the shared
FrameBufferUpdate blocks, transfers from GX commands), so the values are not kept.
FlushDataCache/InvalidateDataCache are coherent no-ops behind a real process
handle check. TriggerCmdReqQueue executes RequestDma and MemoryFill as whole-span
checked transactions and CacheFlush as a coherent no-op. DisplayTransfer and
TextureCopy run on the transfer engine (src/3ds/gpu/transfer.c, ported from Azahar's
software blitter): tiled/linear conversion in both directions, the five framebuffer
formats, the X and XY box filters and FLIP/CROP skew, texture copies with input and output
gaps. GX commands address memory as the GSP module does (its virtual-to-physical windows
for the linear heaps, VRAM and New 3DS extra RAM), and the engine reads and writes
physical memory as a bus master, so a VRAM the CPU maps read-only still receives
transfers. RequestDma resolves the application's pages to physical ones the same way.
MemoryFill honors its control halfwords (trigger bit, 16/24/32-bit width) and fills from
start to end rounded down to 8 bytes, the end being an open bound that may be its
memory region's end. Each command raises its own interrupt (DMA, PSC0 or
PSC1, PPF) on the right holder's relay queue, a failing command stores its error and the
FAILED status as GSP does, and the queue goes on. SubmitCmdList writes the list's
address and size to the first command buffer channel and runs it on the PICA command
processor (src/3ds/gpu/pica.c, ported from Azahar's PicaCore): masked register
writes, batched and sequential extra data, shader program, swizzle and uniform uploads,
the lighting, fog and procedural-texture LUTs, default attributes and immediate mode,
command buffer jumps and the P3D interrupt request with autostop. A list outside memory
raises P3D without running. Draws go to the GPU (see above). The software renderer
ported from Azahar's (gpu/shader.c, gpu/draw.c, gpu/raster.c, gpu/lighting.c,
gpu/texture.c) is the reference the GPU's draws are compared with. Only its shader
units and vertex processing (gpu/shader.c, gpu/draw.c) are built into the runtime:
a draw with a geometry shader runs its vertex and geometry shaders there on the CPU, as
Azahar does, and the GPU draws the triangles they make. The reference samples the
nearest texel only, as Azahar's does, so texture filtering has no reference. A shadow
pass (fragment operation mode 3) writes each pixel's depth and strength into the colour
buffer as one word, depth << 8 | strength, through an R32UI view of the target's RGBA8
image. A shadow texture (Shadow2D or ShadowCube) reads those words through the same view
of the texture's image. The ExHeader's address-mapping capabilities
are applied at process creation (VRAM, DSP RAM, New 3DS extra RAM), as Azahar's
HandleSpecialMapping does; MMIO areas stay unmapped. SetBufferSwap and the shared
FrameBufferUpdate blocks latch real framebuffer descriptors in the five formats
the presenter converts (RGBA8, RGB8, RGB565, RGB5A1, RGBA4). A swap through
SetBufferSwap in another format stops rather than being shown with invented
colors. The LCD shows the left eye of the pair shown_fb (one bit) selects. A
swap whose shown pair has no address, a stride too short for a row or memory the
guest cannot read is dropped and the screen keeps its last frame.
ImportDisplayCaptureInfo reports the framebuffer each screen shows (its left address
for both eyes), and SaveVramSysArea and RestoreVramSysArea succeed only on a session
registered with the flag that allows them (0xD8202A06 otherwise), copying no VRAM
because the HLE applets draw nothing. Display timing is host-paced 60 Hz: latched
frames present at vblank waits and overdue swaps, and waits on the relay event
complete only at real vblank deliveries. MapMemoryBlock maps a block another process
owns (an HLE service's) into the application's range, or an application's own block
as a mirror within the permissions it was created with. A library applet's capture
block maps as an application's block does, at the address asked, onto its own pages.
Each mapping holds a reference to the block. HLE shared pages are reserved from
board FCRAM past the application pool.
Presentation is Vulkan only (volk, with no linked Vulkan library):
the device converts the guest framebuffers to RGBA8 and shows the landscape-
rotated screens live in an SDL3 Vulkan window through a real swapchain, in the
arrangement the 3DS display's Screen layout setting picks: the top screen above
the bottom one as on the console, one screen alone, the large screen at the left
with the other at its own size over 2.25 (Azahar's Android default), centred
vertically at the large one's right, or side by side, each screen keeping its
shape in a window large enough for it. Screen swap trades the screens' places,
and picks the bottom screen in the one-screen layout. Screen layout 4 gives each
screen a window of its own instead, which fills with
that screen. Top screen resolution and Bottom screen resolution scale the 3D
rendering of that screen, 1 to 16 times the console's 400x240 and 320x240, and
each screen keeps the console's proportions in the window whatever its scale. A
game that copies a target at a scale above 1 into its framebuffer with a
display transfer is shown from the
copy that transfer made, at the target's scale. A write by the guest to one of
its pixels is shown from memory. The Switch's vsync picks how the
window shows its images, as for a Switch game: 1 each at the display's refresh
(FIFO), 0 the newest at the next refresh (mailbox) or at once (immediate), and the
run says at start which it got. Its fullscreen=1 starts the window fullscreen (the
top screen's, with screen layout 4), but for a --seconds, --frames or --max-blocks run,
and F11 or Alt+Enter switches the window it is pressed in, as on the Switch;
Alt+Enter does not press Start. Esc twice within 3 seconds closes the game, a first
press warning on the terminal and in a line over the game in the window it was pressed in,
as a Switch game's notices show. The Switch's anisotropy (2 to 16) filters a
mipmapped texture the game filters linearly anisotropically, held to the device's most,
and the run says at start the value in effect. Bounded PPM snapshots land in
<output>/frames in every mode. -H (--headless) starts no SDL video, audio or
gamepad subsystem and makes no window, VkSurfaceKHR or swapchain, whatever display
the host has: frames still decode and are dumped, audio.wav still records, and
--script still presses. A gamepad is not read in this mode. Without a display the
window path is unavailable and frames are only recorded; without a Vulkan device the
presenter reports unavailability and no frame is fabricated. --frames N ends the run
after N decoded frames, and a frame limit above 0 needs a Vulkan device (--frames 0 ends before any frame is
decoded). --seconds N counts N
host seconds, which are the console's seconds at the default pace, and --shot PATH
writes the last frame of both screens as one PPM to exactly PATH, the top above the
bottom as the window shows them.
How a target's render scale is chosen and kept:
The separate windows and the two render resolutions are checked by 3ds_windows: each
screen takes its own setting's scale and no other's, both screens present, and the PPM
dumps stay the framebuffer's own size whatever the scale. With screen layout 4 on, that
test skips unless the device presents to both windows, and it skips where the host makes no
window (CI).
The one window's layouts are checked as a pure function by 3ds_layouts: the stacked and one-screen layouts at
480x400, the large and side layouts at several windows (some swapped), a narrow window and a wide one where both
screens still get a rectangle of their console's shape inside the window and over no part of the other, a screen
with no frame yet, screen layout 4 with no second window, and a value past the five shown as stacked.
A window minimized and restored gets its swapchain back, checked by 3ds_windows_minimized
(it skips where the host keeps the swapchain or does not minimize). On Windows a minimized
window's surface is 0x0 and no swapchain can be made at that size, so the first frame after
the restore makes one.
A frame takes a window image only if one is free at once, as the Switch's and the Vita's
presenters do: while the display holds them all (FIFO behind, a locked or remote session)
the frame is not shown, the game runs on, and the run's report counts those frames.
3ds_windows_no_image checks that no frame waits, that a held frame is still decoded and
that frames show again once an image is free. 3ds_windows_headless starts the presenter
with no window even where the host has a display, and checks that both screens still
decode, their dumps are written, and --shot's two-screen PPM is made.
Each shown frame's submission signals its swapchain image's own semaphore, which the
present waits for. A present's wait has no fence, so an image's semaphore is used again
only once that image is acquired again. The ring moves to its next slot at every
submission, so a slot's semaphore cannot stand for the image's. 3ds_windows_submissions
presents frames with other submissions between them, and
3ds_windows_submissions_validation runs it under the validation layer with SDL's
offscreen window, which needs no display.
The RAM is one shared mapping (a memfd on Linux), so that every guest view of a page is the
same memory, and some drivers will not import it (VK_EXT_external_memory_host): RADV
imports a host pointer as an amdgpu user pointer of private anonymous memory only, and its
vkAllocateMemory gives VK_ERROR_INVALID_EXTERNAL_HANDLE for the first region, VRAM. There
the RAM moves into the device's own memory, one allocation of a host-visible, coherent and
cached type exported as a dma-buf (VK_EXT_external_memory_dma_buf), each region's buffer
bound at the region's offset: before the guest runs, the RAM's bytes are copied into it, and
the object's view and every flat view of a RAM page are mapped from the dma-buf at the same
address and protection (snx_ctr_memory_adopt). The device says so on the standard error,
with the import's refusal, the size and the memory type. On an RX 6700 XT the CPU reads that
memory at 52.7 GB/s and the memfd at 51.0, and a page's first touch costs 0.31 us against
1.95. A mapping of a dma-buf never merges with its neighbours, though, so each page that
write tracking protects and opens again stays a mapping of its own: a host with few of them
(vm.max_map_count, 65530 where systemd 256's 1048576 is not set) can run out, and write
tracking then says, for each view it cannot protect, its address and the system's errno.
When the device can neither import the RAM nor take it into its own memory, the RAM is a copy
in the device's host-visible, coherent memory, which the CPU keeps in step with the board's
own by page: a page the guest wrote is copied in before the GPU first reads it, and a page the
GPU wrote is copied back before the CPU touches it (render.c's copy_in and copy_out, the
page tracking the copies path uses). No vkCmdCopyBuffer is possible there, because the
guest's RAM is not a VkBuffer: the copy is a memcpy. The device says so on the standard
error, with the size and the memory type. A Mac's MoltenVK is such a driver.
Made from docs/platforms/3ds/README.md at 69237645142c. GPL-3.0-or-later.