emuforge

The RDP and the VI's filters

The RDP (Reality Display Processor) draws. It takes commands, loads textures into its own 4 KiB of memory (TMEM), and writes pixels and their depth into RDRAM. The VI (Video Interface) reads a finished image from RDRAM, filters it, and shows it. Both use the ninth bits of RDRAM.

Both are in crates/n64-rdp, which is a port of paraLLEl-RDP, by Themaister (MIT), with thanks, which this page calls the base. Its credits name the commit, what was ported and how its author says that it was made. The rest of n64emu is written from scratch. The noise, the Vulkan code and the way that memory is shared are n64emu's own.

What works now: drawing and loads, saving them in a state, and the VI's picture, on llvmpipe and on an Arc A750. As extras: a picture at two or four times the size, and pictures that a front end reads straight from the GPU's memory.

How a list is drawn

  1. The console gives whole commands (Rdp::commands), when the emulated RDP takes them. length says how long each one is.
  2. Commands that set something change a register. The registers are the last command of each kind, so a state does not depend on a GPU.
  3. Loads and primitives are put together as a batch. All loads of a batch come before all that it draws, and all primitives in it draw to one image. So a new batch starts when the image changes, when a load could read what the batch drew, and when there is no more room. Only the commands decide this, and never time.
  4. The GPU runs a batch as compute shaders. It works out the spans of each line, and cuts the picture into squares of 8 by 8 pixels to find which primitives each has. Loads write TMEM. Then each primitive is shaded in its squares, and at last each pixel goes through its primitives in their order, with depth, blending and coverage.
  5. Before the console can read its results, it calls finish. That waits for the GPU and copies what the batches wrote to RDRAM. The picture needs no finish (below).

RDRAM is there twice

The console's RDRAM is ordinary memory, and the GPU has its own copy. The base can also share the console's memory with the GPU (host memory import). n64emu does not, because the console could then use memory that the GPU is changing, and Rust's rules do not allow that.

  • To the GPU: before a batch, the pages (1 KiB) that it needs and that the console changed. write marks them as changed.
  • From the GPU: in finish, the bytes that batches could write: the lines of the color image (and of depth, when it is written) from the first line that a primitive has to the last, rounded out to whole 8 bytes. The shader that writes stores only pixels inside of the primitives' spans, so nothing else can change. The rest of such a page stays the console's.
  • pending has every page that commands since the last finish could read or write, and pending_writes the bytes that they could write. The console calls finish before it reads in the second or writes in it. If it does not, its bytes are old or wrong, in the same way each time.
  • A page can have both: the RDP's bytes and, next to them, bytes that the console changed. Then only the console's bytes go to the GPU, and only the RDP's come back. Games keep their own data on the page where an image ends: before this, in 900 frames, Banjo-Kazooie's processor waited 422 times for such a page and Mario Kart 64's RSP 107 times, and none of those reads and writes touched a byte that the RDP could write.
  • A write where commands only read does not wait for the GPU. unsent has the pages of the commands that the GPU was not given yet. Before the console writes there, send gives them to the GPU with a copy of the bytes as they are then, and nothing more is needed: the GPU reads the copy. Games often write a buffer that the RDP took a texture from a moment earlier. In GoldenEye 007, 900 frames had 4,237 such writes, and each of them waited about 0.6 ms for the GPU before (2026-12-02, on an Arc A750). A test compares this with finishing first, for every kind of list.

The ninth bits

Each byte of RDRAM has a ninth bit. The RDP keeps a part of a pixel's coverage or of its depth there, and the VI uses the coverage. n64brew's page about RDRAM describes the ninth bits. The processor can read them in one mode of the MI (EBus), by its page about the MI (both CC BY-SA 4.0).

  • ninth gives them as one bit for each byte. The GPU has them as a number from 0 to 3 for each two bytes, and nothing is lost between the two forms.
  • Which of the two bits is the first byte's is a guess. The base does not need to know.
  • What a write by the processor does to them is not known. That page says, as a thing to be tested further, that they seem to become the sign of each byte. write leaves them alone, and the console decides.
  • At power-on they are all 1, and TMEM is 0. That is the base's choice.

Noise

The RDP and the VI can use noise. How the hardware makes it is not known, so n64emu has its own function, and its bytes are not a console's. It depends on the pixel, on the number of the primitive since reset (in the VI, on the number of the field), and on what it is for. It is the same each time.

The picture

picture makes the VI's picture from its registers. It depends only on its inputs, RDRAM, the ninth bits and the state, and not on the picture before it. It goes through four passes: it reads the image with its coverage, filters edges or takes dither out, then the divot filter takes out single pixels that stand out, and at last it is scaled, with gamma if that is on.

  • A picture has only the dots that the VI shows, each with alpha 255: the columns and rows between H_VIDEO and V_VIDEO, without the first 8 and the last 7 pixels of each line, which the VI does not show unless the line is cut off there. Nothing else is made or copied. Picture::whole is the television's whole picture in the same dots, 640 columns and 240 rows for NTSC or 288 for PAL (times the scale), which has the shape 4:3, and Picture::at is where the picture is in it. Super Mario 64's is 625 by 237 at 8, 1. A front end shows it at that shape. When the VI shows no dot, there is no picture, as when it is off.
  • Interlaced, a picture has only the rows of its field, and Picture::field says which ones. The console puts the fields together.
  • If the VI is off or has nothing to show, the picture has no pixels.
  • A picture does not finish. The GPU makes it from its own copy of RDRAM, after it was given all commands (send) and the pages that the console changed. That copy is what finish would have copied back, so the picture is the same, and nothing comes back to RDRAM.
  • One frame later, without waiting: start_picture starts a picture and does not wait, and take_picture gives it later. The GPU does all work in order, so it is the picture of RDRAM as it was at the start, whatever the commands and the console did after that. The console's previous_picture takes the one that it started at the last call and starts the next, so a front end shows each picture one frame later and the GPU has a whole frame for it. picture in between changes nothing there.
  • Measured on 2026-12-02 (Arc A750, 900 frames each of Super Mario 64, GoldenEye 007, Banjo-Kazooie and Mario Kart 64, all USA, a picture after each frame, n64emu-dev --pictures --rdp-waits): before, a picture waited twice, for the copy back (425 to 639 times, 0.6 to 1.0 ms each) and for its result (638 to 868 times, about 0.5 ms each). One frame later, 6 to 94 of the pictures still found the GPU busy, for 0.001 to 0.015 ms a frame. With the bytes above instead of pages, no finish is left in Banjo-Kazooie, GoldenEye 007 and Mario Kart 64, and one in Super Mario 64 (a DMA into a line of an image). Each picture and every frame's hashes are the same as before, both ways. The host's user cycles did not change: the waits were sleeps.
  • Registers that a game changes from line to line are missing. A whole picture has the registers that it was given. A game that moves or scales single lines this way would show them all alike.
  • The GPU's time, measured on 2026-12-02 (Arc A750, 900 frames at 60 a second, late pictures, n64emu-dev --pace 60 --gpu-times): Super Mario 64 0.83 ms a frame (1.8 submissions and 16 dispatches a frame), GoldenEye 007 1.98 ms (2.5 submissions, 33 dispatches). The kernel's own count for the process (drm-engine-render in /proc/PID/fdinfo, which GPU monitors read) agrees within about 3 %, so its render engine is busy 6 % and 12 % of the time. The VI's six passes take about 0.3 ms of it.

The same bytes each time

  • Nothing depends on how long the GPU takes or when a shader was compiled. Each shader is compiled before it is used, and nothing else is drawn in its place. The first use of a new combination of modes can make the game wait for that.
  • Shaders are compiled ahead, on threads of their own, so that the game seldom waits: those that every game uses as soon as an RDP is made, and the new ones of one batch side by side. A front end can keep the list of what a game used (Rdp::shader_list) and give it back the next time (Rdp::compile_ahead); n64emu-dev --shader-list PATH does that. A batch that needs a shader that is not ready waits for it, so this changes when the work is done and never what it gives. Measured on 2026-12-02 with an empty shader cache, Ocarina of Time (USA), 1,200 frames: without a list the game waited 20 times longer than 0.5 ms, 829 ms in all and 122 ms at most, on an Arc A750 (337 ms on llvmpipe); with a list, never. With a full shader cache, a shader takes about 0.2 ms on the Arc and 5 to 20 ms on llvmpipe.
  • Every variable in the shaders has a value before it is used. Tests look for that in the sources and in what was compiled, and say what they do not see.
  • Lines in a load are worked out with whole numbers, and not with floating point.

When the color image and the depth image overlap at different addresses, pixels use each other's bytes. Then each primitive is drawn by itself. It reads what was there before it, and all of its color is written before its depth. That order is a guess, and such drawing is slow. Rdp::overlaps counts these primitives. In the base, the result depends on the order in which the GPU runs. The tests with broken commands found that this was so here too, before it was changed.

When they start at the same address and a color pixel has 8 or 32 bits, the depth of a pixel is in the color of other pixels, which the same batch draws. In the base, the result depends on the order in which the GPU runs here too (a test showed it in n64emu before it was changed). Now each pixel reads memory as it was before its batch, which is what the base gives where it does not depend on that order. With 16-bit pixels, a pixel's color and depth are the same bytes, and the base has a rule for that.

Where the base has no defined result, n64emu has one. Each of these is a guess until it is measured on a console:

WhenWhat happens
a palette is on and the texture is YUVthe texels are 0
a level of detail is used that the mode does not work out, such as in one-cycle modeit is 0
a load reads past the end of RDRAMit goes on at the start
a load would read before its first texelnothing is written there
the VI's filter reads past the right edge of its imageit reads 0
copy mode with a 32-bit image (a setting that the base leaves unset)the setting is the one that the pass for depth and blending has
color and depth at the same address, with 8 or 32-bit colordepth is read from memory as it was before the batch
the mode that looks at the next line, on the last line of a primitive with all 1,024 linesthat line is worked out as for any other primitive

In the base, these could give different bytes on two drivers. In three of the test lists they did, and after the first row of this table was defined, two drivers agreed there. For one of the three, it was not checked that this is the only cause.

Limits

From the base's README:

  • Chroma keying in the color combiner is missing.
  • Some very unusual loads into TMEM are missing. A load that is not supported does nothing.
  • In the first cycle, the combined color and alpha are 0.
  • In one-cycle mode, the fraction for the level of detail is not worked out. Nor is the level of detail in copy mode.
  • In two-cycle mode, the blender reads the color in memory in its first cycle too. The alpha test uses the current pixel, and not the next one. Texel 1 in the second cycle is texel 0 of the current pixel.

Upscaling, beside the exact picture

An extra, as the commandments want it: the app's menu has it as Render scale (1x, 2x or 4x), and n64emu-dev --scale too. Rdp::set_render_scale (2 or 4) has every batch drawn a second time into an upscaled copy of RDRAM on the GPU, which only Rdp::picture_scaled reads. RDRAM, the ninth bits, TMEM, states and Rdp::picture are the same at every scale: a test runs every kind of list at 1, 2 and 4 and compares all of them, and the hashes of Super Mario 64 (900 frames, memory, sound, state and picture) were those of the exact build at 2 and 4.

  • The shaders are the base's, which kept its upscaling: lines, squares and pixels are worked out at the larger size, and each pixel of RDRAM has a slice for each of its samples. Textures are read from TMEM as always, so they stay as sharp as the game made them.
  • The upscaled copy takes the exact RDRAM where that changed some other way (n64emu's own shader, upscale_sync.comp): before each batch and each picture, every slice takes the exact bytes where they are not what it last took, and after a batch is drawn exactly, it notes the exact result, so that only the upscaled drawing is kept there. So what the processor wrote, and a batch whose color and depth overlap (which is not drawn upscaled), show at the exact size.
  • The picture goes through the VI's passes at the larger size. The fetch bug is not emulated there, as in the base.
  • Cost, measured on 2026-12-02 on the Arc A750, Super Mario 64 (USA), 900 frames: 12.9 s at 1, 12.8 s at 2 and 13.5 s at 4 without pictures; with an upscaled picture after each frame, 15.1 s, 15.0 s and 16.8 s. A picture at 4 is 2,560 by 960 dots (9.8 MB), which picture_scaled copies from the GPU to the processor each frame. Shared pictures (below) are not copied.

Pictures shared with the front end, without a copy

Rdp::share(true) and then Rdp::shared_picture once a frame, instead of start_picture and take_picture. The picture is never copied through the host's memory: the VI's last pass writes it into a buffer whose memory the RDP exports as a file descriptor, and the front end's OpenGL imports that memory once (GL_EXT_memory_object_fd) and reads it as a texture buffer, RGBA8, rows from the top down. Exported binary semaphores (GL_EXT_semaphore_fd) say when a picture is done and when the front end no longer reads it. This is n64emu's own code. The app shares pictures when its OpenGL is on the RDP's GPU, and otherwise takes each picture through the host's memory.

  • Weaving is done there too, by the same rules as n64emu's Weaver: a field is woven with the last one when that is the other field, of the same shape, of the frame just before (follows), and otherwise each of its rows is there twice. The last pass writes each dot at its row of the whole picture, and the other field's row beside it, from a small buffer of the GPU's own that keeps the last field (the same pass writes the new field there). So there is no pass and no copy more, and for a picture that is not interlaced nothing at all.
  • Shared::picture is the picture that the call before started, so a front end shows each picture one frame later, as with start_picture. Picture is woven: height has both fields' rows, field says which one is new, and whole and at stay in a field's rows, as Weaver leaves them.
  • Three buffers, made for the largest picture at the scale in use (640 by 576 dots times the scale: 1.5 MB at 1, 5.9 MB at 2 and 23.6 MB at 4 each). New ones come with the first picture in them (Shared::buffers), only when the scale of the pictures changes or sharing is turned on again.
  • What the front end does, exactly: when buffers is not empty, glFinish, delete the old ones without signaling anything, and import the new ones (a dedicated memory object of allocation bytes, a buffer of size bytes at offset 0, and both semaphores, all as opaque file descriptors). For each picture in buffer B: glWaitSemaphoreEXT(ready(B)) naming B, once, before B is read, also if it is never shown; and once, when it shows anything else (the next shared picture, a blank or a CPU picture), glSignalSemaphoreEXT(released(B)) naming B, and glFlush, at the latest before the call after the next one: a picture that was handed out by call t is given back before call t + 2. With no picture it may keep B on the screen.
  • What the RDP does: it writes a picture only into a buffer that is neither the one that it handed out last nor the one that it started last. Before it writes one that was handed out, its submission waits for released(B) and takes B from VK_QUEUE_FAMILY_EXTERNAL; after writing, it gives B to VK_QUEUE_FAMILY_EXTERNAL and signals ready(B) in the same submission. Front ends that cannot share keep start_picture (or start_picture_scaled, which is the upscaled picture one frame later) and take_picture.
  • What a device needs for it: VK_KHR_external_memory_fd and VK_KHR_external_semaphore_fd, a buffer's memory and a binary semaphore that can be exported as opaque file descriptors, and a front end's OpenGL of the same device and driver UUIDs (Gpu::uuids). llvmpipe (Mesa 25) cannot: its binary semaphores are exported only as sync files, which OpenGL does not take. So share refuses it, and the tests of sharing run on the Arc A750.
  • Tested (tests/shared.rs), on 2026-12-02 on the Arc A750, by a front end of the tests' own: a second Vulkan instance and device that imports the descriptors, waits for ready, takes the buffer, copies the picture to the host and gives the buffer back, as OpenGL would. One image in five pictures (not interlaced, two fields that follow each other, one that does not, at scale 1 and from the upscaled copy at 2) gave the bytes of picture or picture_scaled, woven by Weaver's rules. That is all that was checked: not every list, not scale 4, not a front end that holds a buffer, and the GPU's time was not measured. For a picture that is not interlaced, the last pass writes the same dots as before, into the shared buffer instead of its own, and nothing is copied to the host.

How it is tested

The lists are made by the tests, and none is from a game. There are 21 kinds. A program outside this repository runs the base on the same ones and gives checksums of RDRAM, the ninth bits and TMEM. Agreement with the base shows only that the port is right: what the base has wrong has to be found on a console.

  • 54 lists without noise (three of each kind) give the same bytes as the base without any change, with the port on an Arc A750 or on llvmpipe and the base on llvmpipe. With both on the Arc, and the base waiting for its shaders, 52 of them are the same. The other two use a palette with a YUV texture, which the base does not define. With that defined as here, and with this crate's noise, the base gives the same as the port in all 63 lists on both drivers.
  • All 63 lists, those with noise too, were run twice on the Arc, with an empty shader cache and then with a full one. All results were the same.
  • Pictures: 42 pictures of 9 lists, with random registers of the VI, are the same as the base's, on llvmpipe and on the Arc.
  • Random and broken commands (broken) come in ten kinds: mixed commands of every kind, many primitives and states up to each limit of a batch, primitives on all 1,024 lines and columns, loads of every kind, color and depth that overlap or start at one address, and pictures with random registers of the VI. Most seeds keep color and depth apart. Each seed runs in a process of its own, with a time limit, in three ways that have to give the same: a plain one; one that copies all of RDRAM, fills the buffers of each batch with ones first, has no robust buffer access and calculates with 32-bit numbers only; and one that fills them with another pattern and goes on from a saved state in a new RDP. On llvmpipe, a second process does the plain run on one thread, so in another order. No page outside of pending_writes may change. On 2026-12-01, 6,000 seeds passed on llvmpipe with none failing and none at the time limit of 300 s: seeds 0 to 4,999 as the generator was at 6f398c5, and 5,000 to 5,999 as it is now, where most seeds share their shader states (which changed what seeds 0 to 4,999 make). In 90 % of them, color and depth never overlapped at different addresses. The same 5,000 earlier seeds then passed on the Arc A750, one process at a time, without a Vulkan error.
  • These found two faults before they were fixed, and each has a test of its own now: the line after the last one of a primitive on all 1,024 lines was not worked out, and color and depth at one address with 8-bit pixels gave results that depended on the order of work. With both faults put back, the random commands found them in 5 of 200 seeds of the kinds that make such cases. No failure is no proof.
  • Two drivers cannot show that others agree.

Tests take a CPU device such as llvmpipe, and never the first GPU that they find. A real GPU needs its exact name in N64_RDP_TEST_DEVICE for that run. Random lists run on it only when its exact name is in N64_RDP_RANDOM_DEVICE too, and only seeds that passed on a CPU device first. MESA_SHADER_CACHE_DIR has to name a folder for the run, or no device is used:

MESA_SHADER_CACHE_DIR=target/shader-cache cargo test -p n64-rdp

Building the crate needs no shader compiler, as the compiled shaders are in the repository. crates/n64-rdp/shaders/build.sh makes them again with glslc and checks them with spirv-val.

What a device needs

Vulkan 1.1, VK_KHR_8bit_storage, 8-bit and 16-bit values in storage buffers, 24 storage buffers for a shader, and a queue for compute. Calculating with such values is optional. The RDP opens a device of its own (Device::open), or takes a front end's (Gpu::from_front_end), which turned these on and holds a lock while it uses the queue. Shared pictures need more (above), and without it the picture is copied. The base needs host memory import for its fastest way, and n64emu does not.

From commit 9ba9172cfa64. Edit this page