Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
nvn: preserve the required presentation texture contract
  • Loading branch information
seedds committed Nov 3, 2026
commit 8c2c04205b8f74d439a2642e88d57ebc53d4e37b
7 changes: 4 additions & 3 deletions dm/gfx/nvn/nvn_mtl.m
Original file line number Diff line number Diff line change
Expand Up @@ -12888,11 +12888,12 @@ static uint64_t snap_take(const tex_t *t) {
static uint64_t present_enqueue(const tex_t *t, double period, const int crop[4], uint64_t ev_val) {
atomic_store(&dm_present_ns, now_ns());
if (g_classic || atomic_load(&g_present_discard)) return 0; // Ticket number 0 = no pending present; the swapchain texture acquire does not wait (g_classic: no Metal 4 queue to present on)
if (t) { xv_check(t->serial, 0, 0, "present"); if (g_xv_npresent < 128) g_xv_present[g_xv_npresent++] = t->serial; }
// t is required: NVN has already read its dimensions; external producers pass a local tex_t. The queue owns a value copy, never an optional texture pointer.
xv_check(t->serial, 0, 0, "present"); if (g_xv_npresent < 128) g_xv_present[g_xv_npresent++] = t->serial;
// A texture whose content comes from its storage is uploaded when it is first resolved (and again when its pages change), in an upload batch only the submit thread may open.
// Bastion fills both window textures with WriteTexels at boot (main+0x9374) and presents one before drawing into it: resolved on the present thread, the upload aborted the host.
// Resolve it here, where the game queues the present, and commit the batch, so the display command buffer comes after the upload on the queue and the present thread finds it uploaded
if (t && !ev_val && on_sub_thread() && tex_from_storage(t)) { tex_mtl(t); up_flush(); }
if (!ev_val && on_sub_thread() && tex_from_storage(t)) { tex_mtl(t); up_flush(); }
static pthread_once_t once = PTHREAD_ONCE_INIT; pthread_once(&once, present_start);
link_sync();
{ // Test switch DM_TEST_LINK_STALL=start seconds,duration seconds: counting from the first present, pause the display link at that point (imitating the display link not calling back while in the foreground) and release it when the duration is up. For verifying the fallback; not set in the product
Expand All @@ -12903,7 +12904,7 @@ static uint64_t present_enqueue(const tex_t *t, double period, const int crop[4]
}
if (ws_on()) { static uint64_t f0; uint64_t f = atomic_load(&g_cur_frame); if (!f0) f0 = f; else if (f >= f0 + 300) { ws_report(f, f - f0); f0 = f; } }
tex_t snap; uint64_t ss = 0; int slot = 0;
if (t && !ev_val && mtl_snap_on()) {
if (!ev_val && mtl_snap_on()) {
slot = (int)(atomic_load(&g_snap_next) % (uint64_t)g_snap_n);
if ((ss = snap_take(t))) { memset(&snap, 0, sizeof snap); snap.magic = MAGIC('T', 'E', 'X', 0); snap.serial = ss; t = &snap; }
}
Expand Down
2 changes: 1 addition & 1 deletion dm/gfx/nvn/nvn_obj.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ void mtl_exec(const uint8_t *handle);
void mtl_sub_fill(uint64_t dst, uint64_t n, uint32_t value); // in-order buffer fill within the submission (OP_CLEAR_BUF)
void mtl_exec_rest(const uint64_t *handles, int n); // Tells which handles in this submission still follow the current handle (used by the pass lookahead across handles)
// Present: draw the swapchain texture to the front-end CAMetalLayer (only counts when there is no window)
uint64_t mtl_present(const tex_t *t, double period_s, const int crop[4]); // Enqueue a present and return the ticket immediately; period_s: minimum time this image is displayed (= present period, 0 = unlimited)
uint64_t mtl_present(const tex_t *t, double period_s, const int crop[4]); // t is required and copied into the queue; crop may be NULL (whole texture). Returns the ticket immediately; period_s: minimum display time (0 = unlimited)
void mtl_present_wait(uint64_t ticket); // Wait until the present with this ticket has been processed (blit committed to the GPU queue)
void mtl_present_flush(void); // Wait until all enqueued presents have been processed
void mtl_present_test_stall(uint64_t ns); // Unit test: extra pause in ns before each present on the present thread
Expand Down
1 change: 1 addition & 0 deletions docs/reference/rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ The replay on Metal (`dm/gfx/nvn/nvn_mtl.m`) and the Vulkan and OpenGL layers. E
- **Fix:** `present_now` commits the residency set after resolving the presented texture (`res_commit`, free when nothing changed), for every game.
- **Found on:** [Super Smash Bros. Ultimate](../../dm/games/01006A800016E000/README.md) (13.0.5, `DM_WINDOW=1`).
- **Check:** Breath of the Wild windowed with the change: its title, 60 fps, no GPU error.
- **Enqueue contract:** the texture descriptor is required, while the crop may be null (the whole image). NVN reads the descriptor's dimensions before calling `mtl_present`; Vulkan and GL use `mtl_present_ext`, which constructs a descriptor on its stack. The queue copies the descriptor and crop before returning a ticket. Redundant nullable-texture checks in `present_enqueue` contradicted that contract and led the analyzer to follow a null path into the mandatory value copy; they are removed rather than inventing a no-texture present. Valid presentation behavior is unchanged; the existing presentation fixtures exercise that path.

### A present snapshot's copy needs the same residency commit

Expand Down