The display on the GPU

Everything the console shows is drawn with Vulkan (src/nds/gpu/vk/), and a run without a Vulkan 1.3 device shows nothing. The CPU's part is three copies a frame, in whole ranges: the video memory as the 2D engines address it, the video memory as the 3D engine addresses it, and the registers each line was drawn with.

The 2D engines. At each visible line's horizontal blank the board keeps the registers the line was drawn with (src/nds/board/lcd.c): the display control, the backgrounds' controls and scrolls, the affine parameters with the reference points as the line counts them on, the windows, the color effects and the master brightness. So a register the game changes between lines takes effect on the line it would on the console. When line 191 is done, one compute pass (shaders/compose.comp) makes every pixel of both screens: it reads its line's registers, fetches the four backgrounds' pixels and the objects' from the copy of video memory (tiles, maps, bitmaps, palettes and extended palettes decoded there), and composes them by priority, window and color effect, over the 3D engine's picture. The video memory, the palettes and the OAM are the ones the frame's end left: a change to them in the middle of a frame is seen for the whole frame.

Mosaic is the pixel's own arithmetic: a background takes its block's first pixel (an affine one with the reference point of the block's first line), and an object with the mosaic bit shows in each block what it has at the block's first pixel. The main memory display (display mode 3) is a frame the board keeps: a DMA channel with the display's timing sends a line of 256 pixels to DISP_MMEM_FIFO at each visible line, and the shader reads the frame from the copy.

The 3D engine. The geometry engine is on the CPU (src/nds/gpu/gpu3d.c, ported from melonDS): the matrix stacks, lighting, clipping and the viewport transform are the console's fixed point, and the game reads their results back (the clip matrix, the box, position and vector tests, the polygon and vertex counts). What it leaves is a frame's polygons, their vertices already on the screen. At line 215 they are drawn (render3d.c, after melonDS's OpenGL renderer): opaque pixels first, then the translucent polygons in their order, the stencil buffer holding each pixel's polygon ID so a translucent polygon does not blend with itself and a shadow falls only where its mask says, then edge marking and fog as passes over the picture, and anti-aliasing as the last. The fragment shader decodes each texel where it lies in the copy of texture memory (shaders/texture.glsl), in all seven formats, so there is no texture cache and no decoding on the CPU. The picture is the console's 256 by 192.

Four things are drawn that melonDS's OpenGL renderer leaves out or draws another way, each in a shader. Every triangle has its own three vertices, which say which of its sides are its polygon's, so the fragment shader knows how many pixels a pixel is inside its polygon (shaders/poly.frag):

With DISP3DCNT's bit 14 the picture starts from the clear bitmap, not from a plain plane: a pass (shaders/clear.frag) reads each pixel's color, depth and fog bit from texture slots 2 and 3.

Display capture. When DISPCAPCNT asks for a frame, the compute pass also writes the captured picture (the engine's frame or the 3D picture, blended with a VRAM bank's picture or the main memory display's as the register says) to a second image. It is read back in one range into the VRAM bank the register names, so the game, or the other engine, finds it there as a bitmap. A game that draws 3D on both screens does it this way.

The window. The frame, the upper screen above the lower one, is scaled into the window's swapchain image at its shape (present.c).


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