# Video This is the hardest part, and the one with the least public information. The picture took six attempts on hardware before it showed. ## The path 1. `VdInitializeEngines(0, NULL, NULL, NULL, NULL)` gives the GPU's command processor to the title, with the kernel's own microcode. This is what Direct3D does. 2. A command ring of the title's own (`VdInitializeRingBuffer`, read-pointer write-back, `VdSetSystemCommandBufferGpuIdentifierAddress` for the swap fence). This is ported from xenon-forge's `xbox360-video`. 3. `VdRetrainEDRAM` at init and after swaps, as Direct3D does. 4. **The display LUT gets filled** with an identity ramp (see below). 5. Two front buffers in physical memory (write-combined), tiled 32 bpp. 6. Each frame: draw into the back buffer, then put into the ring a scanout write plus the 64 dwords `VdSwap` writes. The fence `VdSwap` places tells when the GPU got there. 7. **After the flip has latched**, commit the surface through 0x60EC (see below). ## Why the screen stays black Everything above, minus the last two points, makes the registers point at the title's buffer (`D1GRPH_PRIMARY_SURFACE_ADDRESS` = 0x6110, the detiler base 0x2840), latching at 60 fps. **The TV still shows black.** These were tried on hardware and **do nothing** for it: - the kernel's scaler program (`VdInitializeScalerCommandBuffer`), which **hangs** the title with the arguments Direct3D passes; - `VdSetDisplayMode(0x40000000)`; - `VdTurnDisplayOn(0)`; - an alpha of 0xFF in the pixels; - the colour matrix (it already held libxenon's RGB values). What works, as [xsdk](https://github.com/jessenic/xsdk) found and we confirmed: 1. **The LUT.** While a title runs, the display's LUT is all zero, which maps every colour to black. Fill it with libxenon's autofill: ```c DC_LUTA_CONTROL (0x64C0) = 0; DC_LUT_RW_INDEX (0x6488) = 0; DC_LUT_RW_MODE (0x6484) = 0; DC_LUT_WRITE_EN_MASK (0x649C) = 7; DC_LUT_AUTOFILL (0x64A0) = 1; wait until 0x64A0 & 2 ``` 2. **The commit, as XAM does it.** Write the surface into 0x2840 (address) and 0x2844 (pitch) and into D1GRPH (0x6110 address, 0x6120 pitch, 0x6134 x end, 0x6138 y end). Then set **0x60EC (`D1CRTC_DOUBLE_BUFFER_CONTROL`) = 1**; the display takes the surface and clears it again. Write 0 afterwards. GPU registers are at `0x7FC80000 + index * 4`, with index = byte offset / 4. Whether a display controller is there at all shows in the autofill: on a console it finishes, in Xenia it never does. xexkit uses the register path only when it finishes. Writing 0x6110 from the CPU in Xenia turns its picture white. ## Sharp at 1080p: native surface, scaler off `VdGetCurrentDisplayInformation` reports a **640x480** surface to a starting title, whatever the output is. Kept like that, the display scaler stretches 640x480 to the output (1920x1080): blurry, and 4:3 pulled to 16:9. xexkit instead asks `VdQueryVideoMode` for the output (width, height, interlaced at words 0-2). On a progressive output it makes the front buffers the output's size and writes the scaler registers for 1:1 (scaler off), as libxenon does: | Register | Value for w x h | |---|---| | 0x6590 scaler enable | 0 when the scale is 1:1 | | 0x6594 tap control | 0x101 | | 0x6584 viewport size | w << 16 \| h | | 0x65E8 | w / 4 - 1 | | 0x65B0 / 0x65C0 filter control | kernel's value \| 0x100 | | 0x65B4 / 0x65C4 scale (8.24) | source / output | | 0x65B8 / 0x65C8 phase | (3 << 15) + (scale >> 9) | Write them between 0x65CC = 0x10000 (update lock) and 0x65CC = 0. Save the kernel's values at init and put them back at exit, and after the Guide (see [The Guide](The-Guide.md)). Interlaced outputs (1080i) keep the kernel's surface. ## Pixels and tiling A front buffer is tiled in 32x32-pixel tiles of 4 KB, the same layout as libxenon's console: ```c index = (((y >> 5) * 32 * pitch + ((x >> 5) << 10) + (x & 3) + ((y & 1) << 2) + (((x & 31) >> 2) << 3) + (((y & 31) >> 1) << 6)) ^ ((y & 8) << 2)); ``` A pixel word is `B << 24 | G << 16 | R << 8 | A`. The display reads 0xAARRGGBB little-endian. Use `xex_video_rgb(r, g, b)`. The fetch constant handed to `VdSwap` holds the buffer's **virtual** address. `VdSwap` makes it physical. Addresses in the 0xE0000000+ window map to physical memory with a 0x1000 offset; `MmGetPhysicalAddress` handles it. ## Drawing fast into write-combined memory Front buffers are write-combined: stores are fast only in address order. At 1080p a per-pixel `pixels[tiled_index(x, y)] = c` loop took **11.4 ms** for a 1080x1080 image, too slow next to a game's own work. Two things fix it: 1. **Scale each source row across first**, into cached memory. 2. **Fill whole tiles in address order.** Within a tile, each pair of rows is 64 consecutive words: 8 groups of 4 pixels of the even row, then 4 of the odd row. The group order is XORed with 4 when `y & 8`: ```c for (pair = 0; pair < 16; pair++) { // rows 2*pair, 2*pair+1 out = tile + pair * 64; flip = (pair & 4) ? 4 : 0; for (g = 0; g < 8; g++) { xg = (g ^ flip) * 4; out[g*8+0..3] = even_row[xg+0..3]; out[g*8+4..7] = odd_row[xg+0..3]; } } ``` Only the edge tiles need the per-pixel path. FAKE-08's `platform/xex/source/XexHost.cpp` has the complete code. ## Pacing `xex_video_begin()` waits until the previous flip is on screen: the swap fence has landed, the scanout register holds the address, and its update bits (0x6144) have cleared. So drawing then presenting once per loop runs at the display's 60 Hz with no timer. A 30 fps program needs a timer on top; see [Time](Time.md). ## Diagnostics `xex_video_trace` (a printf-like hook) logs init and the first flips. `xex_video_dump_registers()` logs the CRTC, graphics, scaler, colour matrix and LUT registers; `xex_video_dump_lut()` logs some LUT entries. These made the black-screen hunt possible.