diff --git a/.github/ISSUE_TEMPLATE/1-crash.yml b/.github/ISSUE_TEMPLATE/1-crash.yml index f6235128..8ee7797b 100644 --- a/.github/ISSUE_TEMPLATE/1-crash.yml +++ b/.github/ISSUE_TEMPLATE/1-crash.yml @@ -12,7 +12,7 @@ body: id: version attributes: label: Version - description: The title line of the settings menu (F1), for example "Ocarina Recomp 0.0.1-ocarina - game 1.1.1". + description: The title line of the settings menu (F1), for example "NX2navi 0.0.1-ocarina - game 1.1.1". validations: required: true - type: input diff --git a/README.md b/README.md index ec72632f..c595a679 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ no part of them may be copied, published, or used to train, fine-tune or prompt written permission. The base import stays under its [MIT License](LICENSE), and third-party code keeps its own license ([THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md)). No license grants anything for Nintendo's game. -Ocarina Recomp is an independent, non-commercial fan project, not affiliated with or endorsed by Nintendo. *The Legend +NX2navi is an independent, non-commercial fan project, not affiliated with or endorsed by Nintendo. *The Legend of Zelda*, *Ocarina of Time* and *Nintendo Switch* are trademarks of Nintendo. ## Building from source diff --git a/docs/launcher.md b/docs/launcher.md index 0e8f73b9..c06d6625 100644 --- a/docs/launcher.md +++ b/docs/launcher.md @@ -73,7 +73,9 @@ A path the Windows ANSI code page cannot hold breaks the MSYS2 tools, so the too PyPI's sha256, into `pydeps/`. 2. **Inspect Your Game** to **Compile**: `tools/make_game.py --nup --out --msys --events`, whose `@@` progress lines drive the rows (check, exefs, romfs, lift, compile). -3. The game's Cetra Display is decoded for the window. +3. **Prepare the Shaders**: make_game.py's last step translates every shader of the game into the game's shader + cache and the driver's own cache (`tools/prepare_shaders.py`), so play does not stop to compile them. +4. The game's Cetra Display is decoded for the window. ## Updating @@ -104,7 +106,8 @@ The stub swaps `python.next` in while no Python runs, then starts the new versio objects.json`, from clang's dependency files). 6. **Swap In the New Build**: the link writes `ocarina.exe.new`, which replaces `ocarina.exe` only once it is complete. A failed rebuild leaves the old build playable; Rebuild the Game tries again. -7. Every version but the current and the previous one is deleted. +7. **Prepare the Shaders** again for the new build; shaders the translator already prepared come from the cache. +8. Every version but the current and the previous one is deleted. When the new version fails to start (exit code 86 from `start.pyw`, or any error within six seconds), the stub sets `current.txt` back to `previous.txt`'s version and starts it with `--rolled-back`. The game is never rebuilt while it @@ -127,12 +130,14 @@ Built Game" is On. The dump is never touched. ## Releases 1. Bump `VERSION`; build the shader translator (`tools/build_xlate.py`). -2. With Python 3.14 and uv: +2. With Python 3.14, numpy (the icon is drawn with it, `launcher/ui/art.py`) and uv: ```powershell - .venv-py314\Scripts\python tools\make_launcher.py --xlate build\xlate --feed owner/name --notes notes.md + uv run --no-project --python 3.14 --with numpy tools\make_launcher.py --xlate build\xlate --ffx amd_fidelityfx_vk.dll --feed owner/name --notes notes.md ``` + `--ffx` puts AMD's signed `amd_fidelityfx_vk.dll` (FidelityFX SDK 1.1.4) in the release for FSR 3.1. + On Linux it cross-compiles the two programs with clang, lld and llvm-windres against the same MSYS2 packages and unpacks Tcl/Tk with msiextract (msitools). It writes into `build/launcher-release`: `NX2navi-Setup-.exe` (the setup program with the launcher folder appended as a zip), `NX2navi-app-.zip`, diff --git a/runtime/discord_rpc.c b/runtime/discord_rpc.c index 6e546ac1..4e9ad6b7 100644 --- a/runtime/discord_rpc.c +++ b/runtime/discord_rpc.c @@ -170,7 +170,7 @@ static int send_activity(PlatIpc *p, const char *image) { snprintf(json, sizeof json, "{\"cmd\":\"SET_ACTIVITY\",\"nonce\":\"%u\",\"args\":{\"pid\":%lu,\"activity\":{\"details\":\"Game " "%d.%d.%d \\u00b7 Recomp %.*s\",\"timestamps\":{\"start\":%llu},\"assets\":{\"large_image\":\"%s\"," - "\"large_text\":\"Ocarina Recomp %s\"}}}}", + "\"large_text\":\"NX2navi %s\"}}}}", next_nonce(), (unsigned long)plat_process_id(), v / 100, v / 10 % 10, v % 10, port_len, rt_port_version, (unsigned long long)start, image, rt_port_version); if (!write_frame(p, OP_FRAME, json)) return 0; diff --git a/runtime/dlssg/nx2_sl.cpp b/runtime/dlssg/nx2_sl.cpp index ebeb78b2..67e54df4 100644 --- a/runtime/dlssg/nx2_sl.cpp +++ b/runtime/dlssg/nx2_sl.cpp @@ -107,8 +107,10 @@ extern "C" NX2SL_API int nx2sl_init(const wchar_t *dir, Nx2SlLog log) { p.numPathsToPlugins = 1; p.pathToLogsAndData = dir; p.logMessageCallback = sl_log; - // the game tracks its own command buffer state; no plugin downloads at run time - p.flags = sl::PreferenceFlags::eDisableCLStateTracking | sl::PreferenceFlags::eUseManualHooking; + // the game tracks its own command buffer state; no plugin downloads at run time. The depth and motion vectors are + // tagged per frame (slSetTagForFrame, nx2sl_frame), which Streamline refuses without the frame-based flag. + p.flags = sl::PreferenceFlags::eDisableCLStateTracking | sl::PreferenceFlags::eUseManualHooking | + sl::PreferenceFlags::eUseFrameBasedResourceTagging; p.featuresToLoad = features; p.numFeaturesToLoad = 3; p.applicationId = 231313132; // the id NVIDIA's samples use for builds without an issued one (as vk_dlaa.c's NGX) diff --git a/runtime/fs_host.c b/runtime/fs_host.c index 70feea09..92dd13e4 100644 --- a/runtime/fs_host.c +++ b/runtime/fs_host.c @@ -547,6 +547,7 @@ static uint32_t save_commit_locked(void) { static PlatMutex pend_lock = PLAT_MUTEX_INIT; static PlatCond pend_cv = PLAT_COND_INIT; static volatile int32_t commit_pending; +static int exiting; // under pend_lock: fs_save_flush ran, the process is ending static void save_wait_commit(void) { if (!plat_cas32(&commit_pending, 0, 0)) return; @@ -555,7 +556,14 @@ static void save_wait_commit(void) { plat_mutex_unlock(&pend_lock); } -void fs_save_flush(void) { save_wait_commit(); } +// The window closed: a Commit already started finishes first, and none starts afterwards, as the process exit would +// cut it off halfway (the startup recovery then keeps the previous one; here it is kept outright). +void fs_save_flush(void) { + plat_mutex_lock(&pend_lock); + exiting = 1; + while (commit_pending) plat_cond_wait(&pend_cv, &pend_lock); + plat_mutex_unlock(&pend_lock); +} static uint32_t commit_run(void) { plat_mutex_lock(&tx_lock); @@ -582,8 +590,15 @@ static uint32_t save_commit(void) { if (!quiet++) rt_log("fs commit save: nothing changed (logged once)"); return 0; } + plat_mutex_lock(&pend_lock); + if (exiting) { + plat_mutex_unlock(&pend_lock); + rt_log("fs commit save: the game is closing; the last finished commit stays"); + return 0; + } plat_inc32(&save_writers); // fs_save_busy until the thread is done plat_xchg32(&commit_pending, 1); + plat_mutex_unlock(&pend_lock); if (plat_thread_spawn(commit_main, NULL, "save commit", 0)) { return 0; } diff --git a/runtime/gfx_present.c b/runtime/gfx_present.c index 77262433..8afde4dd 100644 --- a/runtime/gfx_present.c +++ b/runtime/gfx_present.c @@ -71,9 +71,10 @@ void gfx_window_focus_changed(int in) { void gfx_window_closed(void) { int restart = plat_cas32(&g_restart, 0, 0) != 0; rt_log("gfx: window closed; %s", restart ? "restarting" : "exiting"); - fs_save_flush(); + cfg_save(); // a menu change saves 400 ms later on the render thread (option_rows.cpp); closing sooner kept the old file vk_pipeline_save(); prof_write(); // plat_exit runs no atexit handlers + fs_save_flush(); // last: the game's threads still run until plat_exit, and a Commit after this one is refused if (restart) { plat_setenv("NX2NAVI_REEXEC", NULL); // the new process counts its own address-space retries (memory.c) if (!plat_restart()) rt_log("gfx: the restart could not start the game again"); @@ -308,23 +309,28 @@ static void free_capture(void) { g_cap_valid = 0; } -// Returns 1 with a usable swapchain, 0 when the window has no area (minimised). Called with g_pl held. +// Returns 1 with a usable swapchain, 0 when the window has no area (minimised) or the create failed, which leaves +// g_dirty set so the next present tries again. Called with g_pl held. g_dirty is cleared before the window's size is +// read: a resize that arrives while the swapchain is being made (AMD's frame generation swapchain takes tens of ms) +// sets it again, where clearing it at the end lost that resize and kept a swapchain of the old size, whose acquires +// then timed out frame after frame behind AMD's presenter (the picture stopped). static int create_swapchain(void) { flip_locked(); // the acquired image goes out before its swapchain is replaced + plat_xchg32(&g_dirty, 0); VkCtx *c = vk_ctx(); VkSurfaceCapabilitiesKHR caps; - if (vkGetPhysicalDeviceSurfaceCapabilitiesKHR(c->phys, g_surface, &caps) != VK_SUCCESS) return 0; + if (vkGetPhysicalDeviceSurfaceCapabilitiesKHR(c->phys, g_surface, &caps) != VK_SUCCESS) return plat_xchg32(&g_dirty, 1), 0; int cw = 0, ch = 0; wsys_client_size(&cw, &ch); VkExtent2D ext = caps.currentExtent.width != UINT32_MAX ? caps.currentExtent : (VkExtent2D){(uint32_t)cw, (uint32_t)ch}; - if (!ext.width || !ext.height) return 0; + if (!ext.width || !ext.height) return plat_xchg32(&g_dirty, 1), 0; uint32_t nf = 0; vkGetPhysicalDeviceSurfaceFormatsKHR(c->phys, g_surface, &nf, NULL); VkSurfaceFormatKHR fmts[64]; if (nf > 64) nf = 64; vkGetPhysicalDeviceSurfaceFormatsKHR(c->phys, g_surface, &nf, fmts); - if (!nf) return 0; + if (!nf) return plat_xchg32(&g_dirty, 1), 0; // UNORM: the blit writes the bytes the game wrote (no sRGB encode). NX2NAVI_HDR: HDR10 when the surface offers it. VkSurfaceFormatKHR sf = fmts[0]; int hdr = vk_hdr_surface_format(fmts, nf, &sf), found = hdr; @@ -356,6 +362,16 @@ static int create_swapchain(void) { ci.oldSwapchain = VK_NULL_HANDLE; old = g_sc = VK_NULL_HANDLE; r = vk_fg_create_swapchain(&ci, &g_sc); + // once AMD's swapchain context exists every swapchain call goes through it: a plain swapchain in its place would + // get AMD's acquire and present. Try again at the next present instead. + if (r != VK_SUCCESS && vk_fg_active()) { + static volatile int32_t n_fail; + if (plat_inc32(&n_fail) <= 5) rt_log("gfx: frame generation swapchain %ux%u failed (%d); trying again", ext.width, ext.height, (int)r); + g_sc = VK_NULL_HANDLE; + vk_queue_unlock(); + plat_xchg32(&g_dirty, 1); + return 0; + } } if (r != VK_SUCCESS && vk_dlssg_active()) r = vk_dlssg_create_swapchain(c->device, &ci, &g_sc); // Streamline's hook else if (r != VK_SUCCESS) r = vkCreateSwapchainKHR(c->device, &ci, NULL, &g_sc); @@ -363,6 +379,7 @@ static int create_swapchain(void) { rt_log("gfx: vkCreateSwapchainKHR failed (%d)", (int)r); g_sc = old; vk_queue_unlock(); + plat_xchg32(&g_dirty, 1); return 0; } for (uint32_t i = 0; i < g_sc_n; i++) { @@ -375,7 +392,7 @@ static int create_swapchain(void) { g_sc_fmt = sf.format; g_sc_hdr = hdr; vk_hdr_set_active(hdr); - if (hdr && !vk_fg_active()) vk_hdr_metadata(g_sc, 1); + if (hdr) vk_hdr_metadata(g_sc, 1); g_sc_ext = ext; g_sc_n = MAX_IMAGES; if (vk_fg_active()) { // AMD's swapchain: the count first (a larger capacity is not cut down to it) @@ -384,7 +401,15 @@ static int create_swapchain(void) { g_sc_n = n < MAX_IMAGES ? n : MAX_IMAGES; memset(g_sc_img, 0, sizeof g_sc_img); vk_fg_get_images(g_sc, &g_sc_n, g_sc_img); - } else if (vk_dlssg_active()) vk_dlssg_get_images(c->device, g_sc, &g_sc_n, g_sc_img); + } else if (vk_dlssg_active()) { // Streamline's swapchain too: the count first, then only that many images + uint32_t n = 0; + vk_dlssg_get_images(c->device, g_sc, &n, NULL); + g_sc_n = n < MAX_IMAGES ? n : MAX_IMAGES; + memset(g_sc_img, 0, sizeof g_sc_img); + if (vk_dlssg_get_images(c->device, g_sc, &g_sc_n, g_sc_img) < 0) g_sc_n = 0; + for (uint32_t i = 0; i < g_sc_n; i++) + if (!g_sc_img[i]) g_sc_n = i; // never a view of a null image + } else vkGetSwapchainImagesKHR(c->device, g_sc, &g_sc_n, g_sc_img); VkSemaphoreCreateInfo semi = {.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO}; for (uint32_t i = 0; i < g_sc_n; i++) { @@ -396,7 +421,6 @@ static int create_swapchain(void) { } free_capture(); vk_queue_unlock(); - plat_xchg32(&g_dirty, 0); return 1; } @@ -691,7 +715,7 @@ static int movie_overlay(VkCommandBuffer cb, int slot, VkImage dst) { plat_mutex_unlock(&g_mv_lock); if (g_mvi_w != w || g_mvi_h != h) { if (g_mvi) { - vkDeviceWaitIdle(c->device); + vk_queue_lock(), vkDeviceWaitIdle(c->device), vk_queue_unlock(); vmaDestroyImage(c->vma, g_mvi, g_mvia); g_mvi = VK_NULL_HANDLE; } @@ -791,10 +815,12 @@ static VkImage hudless_record(VkCommandBuffer cb, unsigned slot, int w, int h, c VkImage pic; VkImageView pv; uint32_t pw, ph; - if (!vk_fg_active() || !vk_dlaa_fg_hudless(&pic, &pv, &pw, &ph) || (int)pw != w || (int)ph != h) return VK_NULL_HANDLE; + VkFormat pf; + if (!vk_fg_active() || !vk_dlaa_fg_hudless(&pic, &pv, &pw, &ph, &pf) || (int)pw != w || (int)ph != h) + return VK_NULL_HANDLE; VkCtx *c = vk_ctx(); if (g_hl && (g_hl_w != g_sc_ext.width || g_hl_h != g_sc_ext.height || g_hl_fmt != g_sc_fmt)) { - vkDeviceWaitIdle(c->device); + vk_queue_lock(), vkDeviceWaitIdle(c->device), vk_queue_unlock(); vkDestroyImageView(c->device, g_hl_view, NULL); vmaDestroyImage(c->vma, g_hl, g_hl_mem); g_hl = VK_NULL_HANDLE; @@ -826,7 +852,9 @@ static VkImage hudless_record(VkCommandBuffer cb, unsigned slot, int w, int h, c VK_PIPELINE_STAGE_2_TRANSFER_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_2_TRANSFER_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT); } - put_picture(cb, slot, pic, VK_FORMAT_R8G8B8A8_UNORM, w, h, rect, g_hl, to, general); + // the steps' per-slot views of their source: a slot of its own (the presented picture's views stay valid for this + // command buffer); with HDR the PQ encode as for the presented float picture + put_picture(cb, slot + FRAMES, pic, pf, w, h, rect, g_hl, to, general); sync_barrier(cb, g_hl, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, VK_ACCESS_2_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT); @@ -865,10 +893,22 @@ static void present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w : vkAcquireNextImageKHR(c->device, g_sc, 1000000000ull, g_acquire[slot], VK_NULL_HANDLE, &idx); } vk_device_fault_report(r); - if (r != VK_SUCCESS && r != VK_SUBOPTIMAL_KHR) goto out; // lost, not ready: skip the frame + // lost, not ready: skip the frame. Acquires that keep timing out (1 s each) mean a swapchain nothing will free an + // image of again: three in a row rebuild it, where the picture used to stop for good. + static uint32_t n_timeouts; + if (r == VK_TIMEOUT || r == VK_NOT_READY) { + if (++n_timeouts >= 3) { + rt_log("gfx: no swapchain image for %u acquires (%d): the swapchain is made again", n_timeouts, (int)r); + n_timeouts = 0; + plat_xchg32(&g_dirty, 1); + } + } else { + n_timeouts = 0; + } + if (r != VK_SUCCESS && r != VK_SUBOPTIMAL_KHR) goto out; if (r == VK_SUBOPTIMAL_KHR) plat_xchg32(&g_dirty, 1); vkResetFences(c->device, 1, &g_fence[slot]); - if (g_sc_hdr && !vk_fg_active()) vk_hdr_metadata(g_sc, 0); // a changed peak or white goes to the display + if (g_sc_hdr) vk_hdr_metadata(g_sc, 0); // a changed peak or white goes to the display if (g_th_pending[slot]) { // that fence has signalled: the thumbnail its submission wrote is complete g_th_tag[g_th_pending[slot] % (uint64_t)g_th_n] = g_th_pending[slot]; g_th_pending[slot] = 0; @@ -943,7 +983,7 @@ static void present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w // Frame generation without the HUD in its generated frames: the picture as it would be without the HUD (vk_dlaa.c, // DLSS's or FSR 3.1's output before the HUD was drawn over it) goes through the same steps into an image of the // swapchain's size, which frame generation compares with the presented one to find the HUD and leave it unwarped - VkImage hudless = src && !g_sc_hdr ? hudless_record(cb, slot, w, h, rect, to, general) : VK_NULL_HANDLE; + VkImage hudless = src ? hudless_record(cb, slot, w, h, rect, to, general) : VK_NULL_HANDLE; static int show_hl = -1; // NX2NAVI_FG_HUDLESS_SHOW=1 (test): the HUD-less picture on screen instead if (show_hl < 0) show_hl = cfg_get("NX2NAVI_FG_HUDLESS_SHOW") && !strcmp(cfg_get("NX2NAVI_FG_HUDLESS_SHOW"), "1"); if (hudless && show_hl) { @@ -1071,7 +1111,7 @@ void gfx_window_release_vk(void) { plat_rmutex_lock(&g_pl); if (c && g_started) { vk_queue_lock(); - vkDeviceWaitIdle(c->device); + vk_queue_lock(), vkDeviceWaitIdle(c->device), vk_queue_unlock(); vk_queue_unlock(); g_flip_on = 0; // its blit is done (device idle); the swapchain goes away unpresented menu_vk_release(); diff --git a/runtime/gfx_window_sdl.c b/runtime/gfx_window_sdl.c index 98091ac5..bdb63e0b 100644 --- a/runtime/gfx_window_sdl.c +++ b/runtime/gfx_window_sdl.c @@ -255,7 +255,7 @@ static void open_window(void *arg) { (void)arg; int w, h; gfx_cfg_window_size(&w, &h); - g_win = SDL_CreateWindow("Ocarina Recomp", w, h, SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY); + g_win = SDL_CreateWindow("NX2navi", w, h, SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY); if (!g_win) { rt_log("gfx: SDL_CreateWindow failed (%s)", SDL_GetError()); return; diff --git a/runtime/gfx_window_win.c b/runtime/gfx_window_win.c index b2e71362..3ec11635 100644 --- a/runtime/gfx_window_win.c +++ b/runtime/gfx_window_win.c @@ -22,6 +22,7 @@ #include "nvn_api.h" #include "ui/menu.h" #include "vk.h" +#include "vk_dlssg.h" #include "platform/platform.h" #include @@ -378,7 +379,7 @@ static void window_main(void *arg) { // button flashes, and NX2NAVI_FULLSCREEN waits for the first activation. // NX2NAVI_BACKGROUND_WINDOW=1 (test runs): no flashing, never fullscreen at start, and F11 keeps the window's place. int background = g_background = cfg_get("NX2NAVI_BACKGROUND_WINDOW") && atoi(cfg_get("NX2NAVI_BACKGROUND_WINDOW")); - g_hwnd = CreateWindowExW(0, wc.lpszClassName, L"Ocarina Recomp", WND_STYLE, CW_USEDEFAULT, CW_USEDEFAULT, ow, oh, NULL, + g_hwnd = CreateWindowExW(0, wc.lpszClassName, L"NX2navi", WND_STYLE, CW_USEDEFAULT, CW_USEDEFAULT, ow, oh, NULL, NULL, inst, NULL); if (!g_hwnd) { rt_log("gfx: CreateWindowEx failed (%lu)", GetLastError()); @@ -427,7 +428,9 @@ VkSurfaceKHR wsys_surface(VkInstance inst) { VkWin32SurfaceCreateInfoKHR si = {.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR, .hinstance = GetModuleHandleW(NULL), .hwnd = g_hwnd}; VkSurfaceKHR surface = VK_NULL_HANDLE; - VkResult r = vkCreateWin32SurfaceKHR(inst, &si, NULL, &surface); + // through Streamline when DLSS Frame Generation is loaded: it maps swapchains to windows from this call + PFN_vkCreateWin32SurfaceKHR create = (PFN_vkCreateWin32SurfaceKHR)vk_dlssg_instance_proc(inst, "vkCreateWin32SurfaceKHR"); + VkResult r = (create ? create : vkCreateWin32SurfaceKHR)(inst, &si, NULL, &surface); if (r != VK_SUCCESS) rt_log("gfx: vkCreateWin32SurfaceKHR failed (%d)", (int)r); return r == VK_SUCCESS ? surface : VK_NULL_HANDLE; } diff --git a/runtime/guest_setjmp.c b/runtime/guest_setjmp.c index 06c42a1b..742fdebb 100644 --- a/runtime/guest_setjmp.c +++ b/runtime/guest_setjmp.c @@ -2,8 +2,9 @@ // // The guest's longjmp restores x19-x30, sp and d8-d15 and returns to the setjmp call's return address, which in lifted // code is the middle of a host function: rt_dispatch has no function there and the run ends ("not a function start"). -// Ocarina's libjpeg reaches it whenever a JPEG fails to decode (its error_exit longjmps to the decoder's trap at -// main+0x1661670). +// Ocarina's font rasterizer reaches it whenever a large glyph's cells overflow its pool: main+0x166215c longjmps back +// to main+0x1661670, which reports the overflow, and the caller renders the glyph again in smaller bands. Text at a +// 2160-high picture does it in normal play. // // The overrides keep both halves on the host: // setjmp(env): the guest registers longjmp restores, kept for env, and the host state of the lifted function that diff --git a/runtime/host.h b/runtime/host.h index f8f79bab..8c3bb39f 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -144,6 +144,8 @@ uint32_t mem_map(uint64_t dst, uint64_t src, uint64_t n); uint32_t mem_unmap(uint64_t dst, uint64_t src, uint64_t n); // 0x05 uint32_t mem_query(uint64_t a, RtMemoryInfo *out); // 0x06 int mem_readable(uint64_t a, uint64_t n); // every page readable +int mem_read_hold(uint64_t a, uint64_t n); // mem_readable that keeps the range mapped until mem_read_release +void mem_read_release(void); uint32_t mem_map_physical(uint64_t a, uint64_t n); // 0x2C uint32_t mem_unmap_physical(uint64_t a, uint64_t n); // 0x2D int mem_info(uint32_t id, uint64_t *out); // GetInfo memory ids 2-7, 12-17, 21, 22, 28; 0 if not a memory id @@ -268,6 +270,7 @@ void res_heapdef_served(void); // romfs.c serves the void wide_init(void); void wide_present(void); // each present: DLSS's render scale (res_fixed) // after rt_patches_init: widescreen.c, NX2NAVI_GAME_ASPECT int wide_available(void); // the generated C has the widescreen sets +uint64_t wide_heap_extra_kib(void); // the texture heap's growth for the widescreen picture void res_init(void); // after rt_patches_init: the patch page and the sets float res_dlss_lod_bias(void); // NX2NAVI_DLSS upscaling: texture LOD bias (<= 0) int res_dlss_mode(const char *mode); // NX2NAVI_DLSS changed while running: new scene size diff --git a/runtime/kernel.c b/runtime/kernel.c index 5074eef3..8c4d5a63 100644 --- a/runtime/kernel.c +++ b/runtime/kernel.c @@ -923,6 +923,10 @@ static void core_put(RtThread *t) { t->cur_core = -1; t->preempt = 0; g_core_owner[c] = NULL; + // Waiters that were to interrupt t (core_kick) forget it: t owns no core now, and once it exits and its handle + // closes (thr_free), a waiter's next ap_try would read and interrupt freed memory. + for (RtThread *w = g_core_wait; w; w = w->cnext) + if (w->target == t) w->target = NULL; core_grant(c); } diff --git a/runtime/memory.c b/runtime/memory.c index 8e33530b..ebd523d2 100644 --- a/runtime/memory.c +++ b/runtime/memory.c @@ -508,23 +508,52 @@ static uint64_t run_start(const uint8_t *p, uint64_t i, uint8_t v) { // first return i; } -// Whether every byte of [a, a + n) can be read: no free, inaccessible or no-access page (mem_query's regions). For -// host reads of guest memory the guest itself may never make, such as a texture upload of a pool slot no shader -// samples (vk_exec_draw.c, texture pools: stale slots point at unmapped memory). -int mem_readable(uint64_t a, uint64_t n) { +static void query_locked(uint64_t a, RtMemoryInfo *out); + +static int readable_locked(uint64_t a, uint64_t n) { for (uint64_t end = a + n; a < end;) { RtMemoryInfo r; - mem_query(a, &r); + query_locked(a, &r); if (r.state == ST_FREE || r.state == ST_INACCESSIBLE || !(r.perm & PERM_R) || r.base + r.size <= a) return 0; a = r.base + r.size; } return 1; } +// Whether every byte of [a, a + n) can be read: no free, inaccessible or no-access page (mem_query's regions). For +// host reads of guest memory the guest itself may never make, such as a texture upload of a pool slot no shader +// samples (vk_exec_draw.c, texture pools: stale slots point at unmapped memory). +int mem_readable(uint64_t a, uint64_t n) { + plat_mutex_lock_shared(&lock); + int ok = readable_locked(a, n); + plat_mutex_unlock_shared(&lock); + return ok; +} + +// mem_readable for a read that takes a while on another thread than the guest's: with 1 the range stays mapped until +// mem_read_release, as unmapping it (UnmapPhysicalMemory, a smaller heap, a moved stack) waits for the lock. A pool +// slot's storage the game has just released could otherwise be decommitted halfway through the copy (a texture +// upload faulted that way in tex_bl_copy). In between, the caller takes no lock of this file (mem_query, mem_readable +// and the mapping calls would wait for themselves). 0: not readable, and nothing held. +int mem_read_hold(uint64_t a, uint64_t n) { + plat_mutex_lock_shared(&lock); + if (readable_locked(a, n)) return 1; + plat_mutex_unlock_shared(&lock); + return 0; +} + +void mem_read_release(void) { plat_mutex_unlock_shared(&lock); } + uint32_t mem_query(uint64_t a, RtMemoryInfo *out) { + plat_mutex_lock_shared(&lock); + query_locked(a, out); + plat_mutex_unlock_shared(&lock); + return R_OK; +} + +static void query_locked(uint64_t a, RtMemoryInfo *out) { RtMemoryInfo r = {.base = a & ~(PAGE - 1), .size = PAGE}; uint64_t low = L.aslr.base; - plat_mutex_lock_shared(&lock); int mi; if (a >= RT_ADDRESS_LIMIT) { r.base = RT_ADDRESS_LIMIT; @@ -590,9 +619,7 @@ uint32_t mem_query(uint64_t a, RtMemoryInfo *out) { r.base = lo; r.size = hi - lo; } - plat_mutex_unlock_shared(&lock); if (out) *out = r; - return R_OK; } // ---- GetInfo memory ids diff --git a/runtime/ov_movie2.c b/runtime/ov_movie2.c index ea57ec46..e3a6c6e3 100644 --- a/runtime/ov_movie2.c +++ b/runtime/ov_movie2.c @@ -346,6 +346,12 @@ static void decoder_thread(void *arg) { update_events(v); plat_mutex_unlock(&v->lock); plat_mutex_lock(&v->codec); + // a flush (ov_flush) between taking the sample and the codec: the sample belongs to the stream before it, and fed + // into the flushed decoder its picture would come out after the seek + plat_mutex_lock(&v->lock); + int stale = v->generation != gen; + plat_mutex_unlock(&v->lock); + if (stale) s.size = 0, s.eos = 0; if (v->dav1d && s.data && s.size) { Dav1dData d = {0}; uint8_t *p = dav1d_data_create(&d, s.size); diff --git a/runtime/platform/linux.c b/runtime/platform/linux.c index 14633350..4f64b309 100644 --- a/runtime/platform/linux.c +++ b/runtime/platform/linux.c @@ -1675,7 +1675,13 @@ void plat_child_stop(PlatChild *c) { // A child that died makes the write fail with EPIPE; SIGPIPE is ignored for the process (main.c), as Windows has none. int plat_child_write(PlatChild *c, const void *data, size_t n) { return write_all(c->in, data, n); } +// See win32.c plat_child_read: a child that never answers fails the read after PLAT_CHILD_READ_MS. long plat_child_read(PlatChild *c, void *buf, size_t cap) { + struct pollfd pf = {.fd = c->out, .events = POLLIN}; + int r; + do r = poll(&pf, 1, PLAT_CHILD_READ_MS); + while (r < 0 && errno == EINTR); + if (r <= 0) return -1; // timed out or failed ssize_t k; do k = read(c->out, buf, cap); while (k < 0 && errno == EINTR); diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index 0d3cea87..6cd0b13b 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -425,7 +425,8 @@ typedef struct PlatChild PlatChild; PlatChild *plat_child_start(const char *exe, const char *arg, uint32_t *err); void plat_child_stop(PlatChild *c); // kills the child and frees c int plat_child_write(PlatChild *c, const void *data, size_t n); // 1 when all n bytes were written -long plat_child_read(PlatChild *c, void *buf, size_t cap); // bytes read (at least 1); 0 or negative: pipe broke +#define PLAT_CHILD_READ_MS 120000 // plat_child_read gives up on a child that sends nothing for this long +long plat_child_read(PlatChild *c, void *buf, size_t cap); // bytes read (at least 1); 0 or negative: pipe broke or timed out // ---- local IPC: a stream connection to another program of this machine, one listen location per name (Discord's // Rich Presence, discord_rpc.c). On Windows the named pipe \\.\pipe\; on Linux the Unix domain socket diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index dca59d8d..4e768c6d 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -693,6 +693,19 @@ static LONG CALLBACK crash_veh(EXCEPTION_POINTERS *ep) { DWORD w; if (n > 0) WriteFile(GetStdHandle(STD_ERROR_HANDLE), b, (DWORD)n, &w, NULL); } + // the caller's registers (the object a call through a table read its pointer from is often one of them) + const CONTEXT *x = ep->ContextRecord; + char b[600]; + int n = snprintf(b, sizeof b, + "[host] crash_veh: rax %#llx rbx %#llx rcx %#llx rdx %#llx rsi %#llx rdi %#llx rbp %#llx r8 %#llx " + "r9 %#llx r12 %#llx r13 %#llx r14 %#llx r15 %#llx\n", + (unsigned long long)x->Rax, (unsigned long long)x->Rbx, (unsigned long long)x->Rcx, + (unsigned long long)x->Rdx, (unsigned long long)x->Rsi, (unsigned long long)x->Rdi, + (unsigned long long)x->Rbp, (unsigned long long)x->R8, (unsigned long long)x->R9, + (unsigned long long)x->R12, (unsigned long long)x->R13, (unsigned long long)x->R14, + (unsigned long long)x->R15); + DWORD w; + if (n > 0) WriteFile(GetStdHandle(STD_ERROR_HANDLE), b, (DWORD)n, &w, NULL); } const char *what = code == EXCEPTION_ACCESS_VIOLATION ? "access violation" : code == EXCEPTION_ILLEGAL_INSTRUCTION ? "illegal instruction" @@ -1719,7 +1732,18 @@ int plat_child_write(PlatChild *c, const void *data, size_t n) { return WriteFile(c->in, data, (DWORD)n, &put, NULL) && put == n; } +// A child that stays alive but never answers (a translator stuck on one program) used to block the reader for good, +// and with it the draw or dispatch waiting for that shader: after PLAT_CHILD_READ_MS without a byte the read fails, +// and the caller stops the child (gfx_shader.c svc_request). The pipe is anonymous, so readiness is polled. long plat_child_read(PlatChild *c, void *buf, size_t cap) { + ULONGLONG t0 = GetTickCount64(); + for (unsigned spins = 0;; spins++) { + DWORD avail = 0; + if (!PeekNamedPipe(c->out, NULL, 0, NULL, &avail, NULL)) return -1; // broken: the child exited + if (avail) break; + if (GetTickCount64() - t0 > PLAT_CHILD_READ_MS) return -1; + Sleep(spins < 50 ? 0 : 1); + } DWORD got = 0; if (!ReadFile(c->out, buf, (DWORD)cap, &got, NULL)) return -1; return (long)got; diff --git a/runtime/resolution.c b/runtime/resolution.c index 3dc1ee36..34748b87 100644 --- a/runtime/resolution.c +++ b/runtime/resolution.c @@ -174,7 +174,7 @@ static uint64_t shadow_kib(void) { } // The generated C carries the output and scene patch sets for the builds that have them. Without them (Ocarina) the game draws at its -// own sizes whatever the settings say, so nothing grows for them: no larger pool or HeapDef, no grown sizes reported. +// own sizes whatever these settings say; its heap grows for its widescreen picture instead (widescreen.c). int res_adjustable(void) { return rt_patch_set("output") >= 0 && rt_patch_set("scene") >= 0; } static int bigger(void) { @@ -183,6 +183,7 @@ static int bigger(void) { } uint64_t res_heap_extra_kib(void) { + if (!res_adjustable()) return wide_heap_extra_kib(); // Ocarina: its widescreen picture (widescreen.c) if (!bigger()) return 0; double grow = fmax(0.0, (double)R.ow * R.oh / (BASE_W * BASE_H) - 1.0) + fmax(0.0, (double)R.hw * R.hh / (SCENE_W * SCENE_H) - 1.0); diff --git a/runtime/romfs.c b/runtime/romfs.c index 99ef89ed..174c59ac 100644 --- a/runtime/romfs.c +++ b/runtime/romfs.c @@ -731,18 +731,28 @@ static void restbl_overlay(void) { // The HeapDef table a bigger game resolution needs (resolution.c): its texture heap and that heap's parent grow. The // base is an overlay that replaces the table (a mod's heap sizes stay), else the RomFS original; the grown table is -// written to /resolution/ and served in the base's place. -#define HEAPDEF "System/HeapSize/Product.Nin_NX_NVN.HeapDef.byml.zs" +// written to /resolution/ and served in the base's place. Switch titles name it for NX, Switch 2 ones +// (Ocarina) for Ounce. +static const char *const heapdef_names[] = {"System/HeapSize/Product.Nin_NX_NVN.HeapDef.byml.zs", + "System/HeapSize/Product.Nin_Ounce_NVN2.HeapDef.byml.zs"}; static void heapdef_overlay(void) { if (!res_heap_extra_kib()) return; static const char keeps[] = "the game keeps its own resolution"; + const char *rel = heapdef_names[0]; + for (size_t k = 0; k < sizeof heapdef_names / sizeof *heapdef_names; k++) { + uint64_t i = lower(heapdef_names[k]); + if (i < file_count && !strcmp(entry_name(entry(i)), heapdef_names[k])) { + rel = heapdef_names[k]; + break; + } + } Overlay *base; size_t n; - void *in = read_served(HEAPDEF, &base, &n, keeps), *out = NULL; + void *in = read_served(rel, &base, &n, keeps), *out = NULL; size_t out_n = 0; int ok = in && res_heapdef_build(in, n, &out, &out_n); free(in); - if (ok && serve_generated(HEAPDEF, base, out, out_n, "resolution", "grown heaps", keeps)) res_heapdef_served(); + if (ok && serve_generated(rel, base, out, out_n, "resolution", "grown heaps", keeps)) res_heapdef_served(); } // The player camera's pack with its stick speeds multiplied by NX2NAVI_STICK_SENS (camera_speed.c). The base is an diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index 61f4bb96..5bf1395e 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -767,8 +767,8 @@ static void row_hdr(void) { opt_slider("NX2NAVI_HDR_PEAK_NITS", UI_MENU_HDR_PEAK, 400, 4000, UI_MENU_HDR_PEAK_DESC, 50, "%.0f nits"); opt_slider("NX2NAVI_HDR_GAME_NITS", UI_MENU_HDR_GAME, 80, 500, UI_MENU_HDR_GAME_DESC, 5, "%.0f nits"); opt_slider("NX2NAVI_HDR_UI_NITS", UI_MENU_HDR_UI, 80, 500, UI_MENU_HDR_UI_DESC, 5, "%.0f nits"); - // the hue correction belongs to the runtime's own HDR tonemap; the native one has the game's - if (strcmp(cfg_value("NX2NAVI_HDR"), "native")) opt_slider("NX2NAVI_HDR_HUE", UI_MENU_HDR_HUE, 0, 100, UI_MENU_HDR_HUE_DESC, 5, "%.0f %%"); + // no hue slider: the hue correction belonged to the runtime's replacement tonemap, and Enhanced now extends the + // game's own tonemap in place (vk_hdr.c aces_extend), so NX2NAVI_HDR_HUE changes nothing in this game } // Frame generation (vk_fg.cpp), after a restart; it needs a temporal upscaler's motion vectors @@ -779,7 +779,8 @@ static void row_framegen(void) { const char *names[] = {menu_text(UI_MENU_FRAMEGEN_OFF), "AMD FSR 3.1", "NVIDIA DLSS"}; const char *v = cfg_value("NX2NAVI_FRAMEGEN"); int cur = !strcmp(v, "fsr3") ? 1 : !strcmp(v, "dlss") ? 2 : 0; - // NVIDIA DLSS (vk_dlssg.c) is listed only while it is set: Streamline still fails in its plugin start-up here + // NVIDIA DLSS (vk_dlssg.c) is listed only while it is set: Streamline now starts and takes the swapchain, but presents + // no generated frames yet (one presented per frame, status OK) int n = cur == 2 ? 3 : 2; int nv = opt_choice_index("NX2NAVI_FRAMEGEN", UI_MENU_FRAMEGEN, names, n, cur, 0, UI_MENU_FRAMEGEN_DESC, menu_text(UI_MENU_FRAMEGEN_NOTE)); @@ -1049,7 +1050,7 @@ static void layout_menu(uint32_t w, uint32_t h, float scale) { // the port version and the game update, right-aligned on the title line: what a bug report needs first char game[16], version[96]; snprintf(game, sizeof game, "%d.%d.%d", rt_game_version / 100, rt_game_version / 10 % 10, rt_game_version % 10); - snprintf(version, sizeof version, "Ocarina Recomp %s - game %s", rt_port_version, game); + snprintf(version, sizeof version, "NX2navi %s - game %s", rt_port_version, game); ImGui::SameLine(ImGui::GetWindowWidth() - ImGui::GetStyle().WindowPadding.x - ImGui::CalcTextSize(version).x); ImGui::TextDisabled("%s", version); diff --git a/runtime/ui_text.c b/runtime/ui_text.c index b57a74c9..2654b6d8 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -46,7 +46,7 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_ON] = "On", [UI_MENU_OFF] = "Off", [UI_MENU_AUTO] = "Automatic", [UI_MENU_DEFAULT_MARK] = "default", [UI_MENU_AFTER_RESTART] = "after restart", [UI_MENU_SET_BY_ENV] = "set by environment variable", [UI_MENU_SAVE_FAILED] = "could not save settings", - [UI_MENU_VERSION] = "Ocarina Recomp %s, game %s", + [UI_MENU_VERSION] = "NX2navi %s, game %s", [UI_MENU_FULLSCREEN] = "Fullscreen", [UI_MENU_FULLSCREEN_DESC] = "Fills the screen with the game; F11 toggles it.", [UI_MENU_WINDOW_SIZE] = "Window size", [UI_MENU_WINDOW_SIZE_DESC] = "Size of the game window when not fullscreen.", @@ -558,7 +558,7 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_DEFAULT_MARK] = "по умолчанию", [UI_MENU_AFTER_RESTART] = "после перезапуска", [UI_MENU_SET_BY_ENV] = "задано переменной окружения", [UI_MENU_SAVE_FAILED] = "не удалось сохранить настройки", - [UI_MENU_VERSION] = "Ocarina Recomp %s, игра %s", + [UI_MENU_VERSION] = "NX2navi %s, игра %s", [UI_MENU_FULLSCREEN] = "Полный экран", [UI_MENU_FULLSCREEN_DESC] = "Игра на весь экран; F11 переключает режим.", [UI_MENU_WINDOW_SIZE] = "Размер окна", [UI_MENU_WINDOW_SIZE_DESC] = "Размер окна игры вне полноэкранного режима.", @@ -1073,7 +1073,7 @@ static const char *const text_pt_br[UI_TEXT_COUNT] = { [UI_MENU_DEFAULT_MARK] = "padrão", [UI_MENU_AFTER_RESTART] = "após reiniciar", [UI_MENU_SET_BY_ENV] = "definido por variável de ambiente", [UI_MENU_SAVE_FAILED] = "não foi possível salvar as configurações", - [UI_MENU_VERSION] = "Ocarina Recomp %s, jogo %s", + [UI_MENU_VERSION] = "NX2navi %s, jogo %s", [UI_MENU_FULLSCREEN] = "Tela cheia", [UI_MENU_FULLSCREEN_DESC] = "O jogo ocupa a tela inteira; F11 alterna.", [UI_MENU_WINDOW_SIZE] = "Tamanho da janela", @@ -1587,7 +1587,7 @@ static const char *const text_zh_hans[UI_TEXT_COUNT] = { [UI_MENU_AFTER_RESTART] = "重启后生效", [UI_MENU_SET_BY_ENV] = "由环境变量设定", [UI_MENU_SAVE_FAILED] = "无法保存设置", - [UI_MENU_VERSION] = "Ocarina Recomp %s,游戏 %s", + [UI_MENU_VERSION] = "NX2navi %s,游戏 %s", [UI_MENU_FULLSCREEN] = "全屏", [UI_MENU_FULLSCREEN_DESC] = "游戏铺满屏幕;按 F11 切换。", [UI_MENU_WINDOW_SIZE] = "窗口大小", diff --git a/runtime/vk_astc.c b/runtime/vk_astc.c index 9c901633..fa23a4d7 100644 --- a/runtime/vk_astc.c +++ b/runtime/vk_astc.c @@ -1,7 +1,7 @@ // ASTC textures on the GPU: the NX2NAVI_ASTC knob, a compute decoder that turns raw blocks into the RGBA8 image the CPU // decoder (tex_astc.c) would have written, and the native ASTC formats where the device samples them itself. // -// NX2NAVI_ASTC_BC = 1 (gpu mode only, default 0): a second pass (runtime/shaders/bc3_encode.comp) encodes the decoded +// NX2NAVI_ASTC_BC = 1 (gpu mode only; unset: by the video memory and RAM, create_bc_pipeline): a second pass (runtime/shaders/bc3_encode.comp) encodes the decoded // texels to BC3 and the image is BC3, 8 bits per texel instead of 32. For GPUs with little video memory, at the cost of // BC3's quality: textures look slightly blockier, and data textures (normal maps) lose some precision. // @@ -304,17 +304,25 @@ static int create_pipeline(VkCtx *c) { // Unset: on when the largest device-local heap is under 20 GiB. There RGBA8 ASTC, four times the size of BC3, fills // video memory (a GTX 1650 4 GiB went from 10-23 to 3-12 ms of GPU time a frame, issue #87). Ocarina in Kokiri Forest // holds 15.9 GiB of VRAM with RGBA8 and 8.2 GiB with BC3, at the same GPU time and no difference to see in the frame, -// so 8-16 GiB cards take BC3 too; BC3 loses some precision (normal maps), so 24 and 32 GiB cards keep RGBA8. +// so 8-16 GiB cards take BC3 too; BC3 loses some precision (normal maps), so 24 and 32 GiB cards keep RGBA8 when the +// PC has the memory for it. Windows charges a process's video memory to its commit charge, so RGBA8 textures also +// count against RAM plus page file: on a 32 GiB card with 31 GiB of RAM a long session in the world held 17 GiB of +// texture memory, 29.8 GiB of VRAM and 45.7 GiB of commit, and a second process beside it ran the system's commit out. +// So RGBA8 needs 48 GiB of RAM as well. +#define BC_KEEP_RGBA8_VRAM (20ull << 30) +#define BC_KEEP_RGBA8_RAM_MB (48.0f * 1024 - 1024) // a 48 GiB PC reports a little under 48 GiB static int create_bc_pipeline(VkCtx *c) { const char *e = cfg_get("NX2NAVI_ASTC_BC"); VkDeviceSize vram = 0; for (uint32_t h = 0; h < c->mem_props.memoryHeapCount && h < VK_MAX_MEMORY_HEAPS; h++) if (c->mem_props.memoryHeaps[h].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT && c->mem_props.memoryHeaps[h].size > vram) vram = c->mem_props.memoryHeaps[h].size; - int on = e && *e ? *e != '0' : vram < (20ull << 30); + float ram_used, ram_mb, proc; + plat_mem_info(&ram_used, &ram_mb, &proc); + int on = e && *e ? *e != '0' : vram < BC_KEEP_RGBA8_VRAM || (ram_mb > 0 && ram_mb < BC_KEEP_RGBA8_RAM_MB); if (!(e && *e)) - rt_log("vk: NX2NAVI_ASTC_BC unset: BC3 %s (largest device-local heap %llu MiB)", on ? "on" : "off", - (unsigned long long)(vram >> 20)); + rt_log("vk: NX2NAVI_ASTC_BC unset: BC3 %s (largest device-local heap %llu MiB, RAM %.0f MiB)", on ? "on" : "off", + (unsigned long long)(vram >> 20), ram_mb); if (!on) return 0; VkFormatProperties p; vkGetPhysicalDeviceFormatProperties(c->phys, VK_FORMAT_BC3_SRGB_BLOCK, &p); diff --git a/runtime/vk_device.c b/runtime/vk_device.c index 3cb9564d..5814f55f 100644 --- a/runtime/vk_device.c +++ b/runtime/vk_device.c @@ -145,10 +145,10 @@ static int name_has(const char *s, const char *sub) { // NX2NAVI_GPU_NAME (case-insensitive part of the name) overrides. static int pick_device(void) { uint32_t n = 0; - vkEnumeratePhysicalDevices(g_vk.instance, &n, NULL); + vk_dlssg_enumerate(g_vk.instance, &n, NULL); if (!n) { rt_log("vk: no Vulkan device"); return 0; } VkPhysicalDevice *pd = calloc(n, sizeof *pd); - vkEnumeratePhysicalDevices(g_vk.instance, &n, pd); + vk_dlssg_enumerate(g_vk.instance, &n, pd); const char *e = cfg_get("NX2NAVI_GPU_INDEX"); // no fixed default: the best device when unset int want = e && *e ? atoi(e) : -1; const char *name = cfg_get("NX2NAVI_GPU_NAME"); diff --git a/runtime/vk_dlaa.c b/runtime/vk_dlaa.c index 09207e1c..c63ab805 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -20,6 +20,7 @@ #include #include "cfg.h" +#include "gfx.h" #include "host.h" #include "platform/platform.h" #include "vk.h" @@ -361,18 +362,20 @@ static struct { float fg_jx, fg_jy; int fg_fresh, fg_reset; // fresh: not handed out yet (vk_dlaa_fg_inputs) // the picture without the HUD for frame generation (vk_dlaa_fg_hudless): DLSS's output encoded to sRGB as the game's - // last pass encodes its picture, then copied bit for bit into an RGBA8 UNORM image, the format the present reads + // last pass encodes its picture, then copied bit for bit into an RGBA8 UNORM image, the format the present reads; with + // HDR on, DLSS's output as RGBA16F, as the game's float picture the present encodes (hl_srgb unused) VkImage hl_srgb, hl; VmaAllocation hl_srgb_mem, hl_mem; VkImageView hl_view; uint32_t hl_w, hl_h; + VkFormat hl_fmt; int hl_fresh; } D = {.mv_ok = -1, .cfg_seen = ~0u}; -int vk_dlaa_fg_hudless(VkImage *img, VkImageView *view, uint32_t *w, uint32_t *h) { +int vk_dlaa_fg_hudless(VkImage *img, VkImageView *view, uint32_t *w, uint32_t *h, VkFormat *fmt) { if (!D.hl_fresh) return 0; D.hl_fresh = 0; - *img = D.hl, *view = D.hl_view, *w = D.hl_w, *h = D.hl_h; + *img = D.hl, *view = D.hl_view, *w = D.hl_w, *h = D.hl_h, *fmt = D.hl_fmt; return 1; } @@ -421,7 +424,7 @@ static int image2d(uint32_t w, uint32_t h, VkFormat f, VkImageUsageFlags usage, static void free_size(struct Size *e) { const VkCtx *c = vk_ctx(); - vkDeviceWaitIdle(c->device); // a third scene size: the evicted images may still be in flight + vk_queue_lock(), vkDeviceWaitIdle(c->device), vk_queue_unlock(); // a third scene size: the evicted images may still be in flight if (e->out_view) vkDestroyImageView(c->device, e->out_view, NULL); if (e->mv_view) vkDestroyImageView(c->device, e->mv_view, NULL); if (e->out) vmaDestroyImage(c->vma, e->out, e->out_mem); @@ -887,7 +890,7 @@ fail: static void free_snap(struct Snap *s) { const VkCtx *c = vk_ctx(); - vkDeviceWaitIdle(c->device); // a fourth size: the evicted images may still be in flight + vk_queue_lock(), vkDeviceWaitIdle(c->device), vk_queue_unlock(); // a fourth size: the evicted images may still be in flight if (s->before_view) vkDestroyImageView(c->device, s->before_view, NULL); if (s->mask_view) vkDestroyImageView(c->device, s->mask_view, NULL); if (s->before) vmaDestroyImage(c->vma, s->before, s->before_mem); @@ -1256,30 +1259,38 @@ evaluated: } // The HUD-less picture (vk_dlaa_fg_hudless): DLSS's output, which the HUD is then drawn over, through the encode the -// game's last pass gives its picture (linear to sRGB), as RGBA8 UNORM bytes like the picture the present reads. +// game's last pass gives its picture (linear to sRGB), as RGBA8 UNORM bytes like the picture the present reads. With +// HDR (NX2NAVI_HDR = 1) the game's picture stays float and linear up to the present, which encodes it to PQ +// (vk_hdr_encode): DLSS's output is kept as RGBA16F for the same encode. The game's own HDR (native) encodes inside the +// game, which this cannot follow: no HUD-less picture there. int vk_dlaa_hudless_wanted; static void hudless(VkCommandBuffer cb, struct Size *e, uint32_t w, uint32_t h) { const VkCtx *c = vk_ctx(); - if (D.hl && (D.hl_w != w || D.hl_h != h)) { - vkDeviceWaitIdle(c->device); + int hdr = vk_hdr_wanted() && !vk_hdr_native(); + if (vk_hdr_native()) return; + VkFormat fmt = hdr ? VK_FORMAT_R16G16B16A16_SFLOAT : VK_FORMAT_R8G8B8A8_UNORM; + if (D.hl && (D.hl_w != w || D.hl_h != h || D.hl_fmt != fmt)) { + vk_queue_lock(), vkDeviceWaitIdle(c->device), vk_queue_unlock(); vkDestroyImageView(c->device, D.hl_view, NULL); - vmaDestroyImage(c->vma, D.hl, D.hl_mem), vmaDestroyImage(c->vma, D.hl_srgb, D.hl_srgb_mem); + vmaDestroyImage(c->vma, D.hl, D.hl_mem); + if (D.hl_srgb) vmaDestroyImage(c->vma, D.hl_srgb, D.hl_srgb_mem); D.hl = D.hl_srgb = VK_NULL_HANDLE; } if (!D.hl) { VkImageView v2 = VK_NULL_HANDLE; VkImageUsageFlags u = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; - if (!image2d(w, h, VK_FORMAT_R8G8B8A8_SRGB, u, &D.hl_srgb, &D.hl_srgb_mem, &v2) || - !image2d(w, h, VK_FORMAT_R8G8B8A8_UNORM, u | VK_IMAGE_USAGE_STORAGE_BIT, &D.hl, &D.hl_mem, &D.hl_view)) { + if ((!hdr && !image2d(w, h, VK_FORMAT_R8G8B8A8_SRGB, u, &D.hl_srgb, &D.hl_srgb_mem, &v2)) || + !image2d(w, h, fmt, u | VK_IMAGE_USAGE_STORAGE_BIT, &D.hl, &D.hl_mem, &D.hl_view)) { rt_log("dlaa: the HUD-less picture's images could not be made: frame generation sees the HUD"); vk_dlaa_hudless_wanted = 0; return; } if (v2) vkDestroyImageView(c->device, v2, NULL); - D.hl_w = w, D.hl_h = h; + D.hl_w = w, D.hl_h = h, D.hl_fmt = fmt; VkImageMemoryBarrier2 ib[2]; - VkImage im[2] = {D.hl_srgb, D.hl}; - for (int i = 0; i < 2; i++) + VkImage im[2] = {D.hl, D.hl_srgb}; + int nim = D.hl_srgb ? 2 : 1; + for (int i = 0; i < nim; i++) ib[i] = (VkImageMemoryBarrier2){.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2, .srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, .dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, @@ -1288,14 +1299,19 @@ static void hudless(VkCommandBuffer cb, struct Size *e, uint32_t w, uint32_t h) .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .image = im[i], .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}}; - VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .imageMemoryBarrierCount = 2, + VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .imageMemoryBarrierCount = (uint32_t)nim, .pImageMemoryBarriers = ib}; vkCmdPipelineBarrier2(cb, &di); } VkImageBlit b = {.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, .srcOffsets = {{0, 0, 0}, {(int32_t)w, (int32_t)h, 1}}, .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, .dstOffsets = {{0, 0, 0}, {(int32_t)w, (int32_t)h, 1}}}; - vkCmdBlitImage(cb, e->out, VK_IMAGE_LAYOUT_GENERAL, D.hl_srgb, VK_IMAGE_LAYOUT_GENERAL, 1, &b, VK_FILTER_NEAREST); + vkCmdBlitImage(cb, e->out, VK_IMAGE_LAYOUT_GENERAL, hdr ? D.hl : D.hl_srgb, VK_IMAGE_LAYOUT_GENERAL, 1, &b, + VK_FILTER_NEAREST); full_barrier(cb); + if (hdr) { + D.hl_fresh = 1; + return; + } VkImageCopy cp = {.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, .extent = {w, h, 1}}; vkCmdCopyImage(cb, D.hl_srgb, VK_IMAGE_LAYOUT_GENERAL, D.hl, VK_IMAGE_LAYOUT_GENERAL, 1, &cp); diff --git a/runtime/vk_dlaa.h b/runtime/vk_dlaa.h index b4061798..879beda6 100644 --- a/runtime/vk_dlaa.h +++ b/runtime/vk_dlaa.h @@ -21,10 +21,11 @@ typedef struct { float fov_y, aspect; } VkDlaaCamera; int vk_dlaa_fg_views(VkImageView *depth, VkImageView *mv); -// The picture without the HUD at the size of the game's picture, RGBA8 UNORM bytes like it (sRGB-encoded), made at -// each evaluation while vk_dlaa_hudless_wanted (frame generation sets it); 1 once per evaluation. +// The picture without the HUD at the size of the game's picture, in *fmt: RGBA8 UNORM bytes like it (sRGB-encoded), or +// RGBA16F linear with HDR on; made at each evaluation while vk_dlaa_hudless_wanted (frame generation sets it); 1 once per +// evaluation. extern int vk_dlaa_hudless_wanted; -int vk_dlaa_fg_hudless(VkImage *img, VkImageView *view, uint32_t *w, uint32_t *h); +int vk_dlaa_fg_hudless(VkImage *img, VkImageView *view, uint32_t *w, uint32_t *h, VkFormat *fmt); int vk_dlaa_fg_camera(VkDlaaCamera *c); // ... and its temporal upscaler is AMD FSR 3.1 (NX2NAVI_TAA = fsr3) instead of DLSS // vk_device.c: NGX's instance and device extensions join the lists (room for max names); vk_dlaa_init after the // device exists brings NGX up and checks that DLSS is available. diff --git a/runtime/vk_dlssg.c b/runtime/vk_dlssg.c index dfab1b37..37a09c74 100644 --- a/runtime/vk_dlssg.c +++ b/runtime/vk_dlssg.c @@ -89,6 +89,22 @@ VkResult vk_dlssg_create_instance(const VkInstanceCreateInfo *ci, VkInstance *ou return f ? f(ci, NULL, out) : vkCreateInstance(ci, NULL, out); } +// An instance function through the interposer while Streamline is loaded, else NULL (the caller takes the loader's). +// The window surface goes through it: Streamline learns the window a swapchain shows from vkCreateWin32SurfaceKHR, and +// without that its swapchain hook found "no window corresponding to this application" and generated no frames. +PFN_vkVoidFunction vk_dlssg_instance_proc(VkInstance inst, const char *name) { + return S.loaded > 0 ? S.gipa()(inst, name) : NULL; +} + +// The interposer's vkCreateDevice finds the device's instance in a map its own vkEnumeratePhysicalDevices fills; a +// physical device enumerated through the loader alone left it a null instance, so the plugins got an empty instance +// table and sl.common called a null GetPhysicalDeviceMemoryProperties while it set up. Enumerate through it as well. +VkResult vk_dlssg_enumerate(VkInstance inst, uint32_t *n, VkPhysicalDevice *pd) { + PFN_vkEnumeratePhysicalDevices f = + S.loaded > 0 ? (PFN_vkEnumeratePhysicalDevices)S.gipa()(inst, "vkEnumeratePhysicalDevices") : NULL; + return f ? f(inst, n, pd) : vkEnumeratePhysicalDevices(inst, n, pd); +} + VkResult vk_dlssg_create_device(VkInstance inst, VkPhysicalDevice pd, const VkDeviceCreateInfo *ci, VkDevice *out) { PFN_vkCreateDevice f = S.loaded > 0 ? (PFN_vkCreateDevice)S.gipa()(inst, "vkCreateDevice") : NULL; return f ? f(pd, ci, NULL, out) : vkCreateDevice(pd, ci, NULL, out); diff --git a/runtime/vk_dlssg.h b/runtime/vk_dlssg.h index b4474c8b..e23ecb19 100644 --- a/runtime/vk_dlssg.h +++ b/runtime/vk_dlssg.h @@ -7,6 +7,8 @@ int vk_dlssg_wanted(void); // NX2NAVI_FRAMEGEN = dlss with DLSS or FSR 3.1 ups int vk_dlssg_early(void); // before the instance exists: loads the DLL and Streamline; 1 when its proxies are there // vk_device.c: the instance and device through Streamline's proxies when vk_dlssg_early succeeded, else the loader's VkResult vk_dlssg_create_instance(const VkInstanceCreateInfo *ci, VkInstance *out); +VkResult vk_dlssg_enumerate(VkInstance inst, uint32_t *n, VkPhysicalDevice *pd); // through the interposer too +PFN_vkVoidFunction vk_dlssg_instance_proc(VkInstance inst, const char *name); // the interposer's, or NULL VkResult vk_dlssg_create_device(VkInstance inst, VkPhysicalDevice pd, const VkDeviceCreateInfo *ci, VkDevice *out); void vk_dlssg_device_ready(VkDevice dev); // after the device: checks DLSS-G and takes the swapchain hooks int vk_dlssg_active(void); diff --git a/runtime/vk_fg.cpp b/runtime/vk_fg.cpp index 50b6081e..68b3138c 100644 --- a/runtime/vk_fg.cpp +++ b/runtime/vk_fg.cpp @@ -145,6 +145,17 @@ extern "C" uint64_t vk_fg_presents(VkSwapchainKHR sc) { return vk_fg_active() && F.fn.pOutGetLastPresentCountFFXAPI && sc ? F.fn.pOutGetLastPresentCountFFXAPI(sc) : 0; } +// The HDR10 metadata goes to AMD's swapchain through its own call (vk_hdr.c vk_hdr_metadata). +extern "C" PFN_vkSetHdrMetadataEXT vk_fg_hdr_metadata_fn(void) { + return vk_fg_active() ? F.fn.pOutSetHdrMetadataEXT : nullptr; +} + +// An HDR10 swapchain (10-bit PQ or 16-bit float): frame generation is told its inputs are HDR. +static bool hdr_format(VkFormat f) { + return f == VK_FORMAT_A2B10G10R10_UNORM_PACK32 || f == VK_FORMAT_A2R10G10B10_UNORM_PACK32 || + f == VK_FORMAT_R16G16B16A16_SFLOAT; +} + extern "C" void vk_fg_frame(VkCommandBuffer cb, VkSwapchainKHR sc, VkFormat fmt, VkExtent2D ext, VkImage hudless, VkImageView hudless_view) { (void)hudless_view; @@ -152,7 +163,7 @@ extern "C" void vk_fg_frame(VkCommandBuffer cb, VkSwapchainKHR sc, VkFormat fmt, vk_dlaa_hudless_wanted = 1; // the upscaling pass keeps a copy of the picture before the HUD (vk_dlaa.c hudless) const VkCtx *c = vk_ctx(); if (F.fg && (F.fg_ext.width != ext.width || F.fg_ext.height != ext.height || F.fg_fmt != fmt)) { - vkDeviceWaitIdle(c->device); // a new window size: the context is made for its display size + vk_queue_lock(), vkDeviceWaitIdle(c->device), vk_queue_unlock(); // a new window size: the context is made for its display size vk_ffx_destroy(&F.fg); F.fg = nullptr; } @@ -170,14 +181,14 @@ extern "C" void vk_fg_frame(VkCommandBuffer cb, VkSwapchainKHR sc, VkFormat fmt, ffxCreateContextDescFrameGeneration d = {}; d.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_FRAMEGENERATION; d.header.pNext = &hl.header; - d.flags = FFX_FRAMEGENERATION_ENABLE_DEPTH_INFINITE; + d.flags = FFX_FRAMEGENERATION_ENABLE_DEPTH_INFINITE | (hdr_format(fmt) ? FFX_FRAMEGENERATION_ENABLE_HIGH_DYNAMIC_RANGE : 0); d.displaySize = {ext.width, ext.height}; d.maxRenderSize = {ext.width, ext.height}; d.backBufferFormat = ffxApiGetSurfaceFormatVK(fmt); ffxReturnCode_t r = vk_ffx_create(&F.fg, &d.header); if (r != FFX_API_RETURN_OK) return F.fg = nullptr, fail("the frame generation context", r); F.fg_ext = ext, F.fg_fmt = fmt; - rt_log("fg: frame generation context for %ux%u", ext.width, ext.height); + rt_log("fg: frame generation context for %ux%u%s", ext.width, ext.height, hdr_format(fmt) ? " (HDR)" : ""); } VkFfxImage depth, mv; float jx = 0, jy = 0; diff --git a/runtime/vk_fg.h b/runtime/vk_fg.h index 015b3f06..b24d6903 100644 --- a/runtime/vk_fg.h +++ b/runtime/vk_fg.h @@ -20,4 +20,5 @@ VkResult vk_fg_present(const VkPresentInfoKHR *pi); // on the game's queue, it // from which generation tells the HUD apart and keeps it out of the generated frames. void vk_fg_frame(VkCommandBuffer cb, VkSwapchainKHR sc, VkFormat fmt, VkExtent2D ext, VkImage hudless, VkImageView hudless_view); uint64_t vk_fg_presents(VkSwapchainKHR sc); // frames the swapchain presented, generated ones included (0: off) +PFN_vkSetHdrMetadataEXT vk_fg_hdr_metadata_fn(void); // AMD's replacement for vkSetHdrMetadataEXT while active, else NULL void vk_fg_release(void); diff --git a/runtime/vk_hdr.c b/runtime/vk_hdr.c index 2728a878..491e5045 100644 --- a/runtime/vk_hdr.c +++ b/runtime/vk_hdr.c @@ -6,10 +6,12 @@ // The game's picture (docs/architecture.md, "HDR"). The game tonemaps its scene into an r11g11b10f target, upscales it // and draws the HUD into a second one (float, nothing in these passes clamps), and one last draw copies that into the // 8-bit sRGB window texture it presents. So: -// 1. vk_hdr_shader_swap: the tonemap's fragment shader becomes shaders/hdr_tonemap.frag, which keeps values above 1 -// (relative to the HUD's white) and rolls off at the peak. It is recognised by its constants and resources, not by -// a hash, so another game version or translator still finds it; its SPIR-V is replaced in memory only. The game's -// FSR 1 upscale after it would clamp the picture to 1 again; fsr_unclamp drops that clamp in its SPIR-V. +// 1. vk_hdr_shader_swap, on each fragment shader as it loads (in memory only; the cache keeps the translator's): 1.1.1's +// tonemap ends in the fitted ACES curve and a clamp to 0..1 per channel, and aces_extend changes those clamps so +// values above 1 (relative to the HUD's white) survive, rolled off at the peak. Recognised by the curve's +// constants, not by a hash, so another translator still finds it. A tonemap of the earlier shape (1 - e^-x and a +// saturation grade) becomes shaders/hdr_tonemap.frag instead. The game's FSR 1 upscale after it would clamp the +// picture to 1 again; fsr_unclamp drops that clamp in its SPIR-V. // 2. vk_hdr_draw / vk_hdr_present_source: a draw into a presented window texture that samples one float texture of // its size is that copy; the present then reads the float texture instead of the window texture. Any other last // draw into it leaves the window texture, decoded from sRGB: menus and movies stay SDR at the HUD's white. @@ -18,10 +20,11 @@ // 3. vk_hdr_encode: on the present, the crop rectangle goes to PQ code values in an RGBA16F image (shaders/hdr_encode.comp) // that FSR 1 or the blit scale into the swapchain. vk_hdr_ui_*: the in-game menu draws into an RGBA8 image and // shaders/hdr_ui.frag blends it onto the swapchain at the HUD's white. -// The tonemap's approach: an "extended vanilla" curve (shaders/hdr_tonemap.frag). +// The tonemap's approach: the game's curve up to near its white, then its tangent ("extended vanilla"). #include "vk.h" #include +#include #include #include "cfg.h" @@ -33,6 +36,7 @@ #include "host.h" #include "nvn_api.h" #include "vk_exec.h" +#include "vk_fg.h" #include "platform/platform.h" #define SLOTS 8 @@ -161,8 +165,181 @@ static int fsr_unclamp(uint32_t *w, uint64_t *words) { return changed ? changed : -1; } +// Ocarina 1.1.1's tonemap (one draw from the scene light target, R11G11B10F, into a target of the same size, before +// anti-aliasing, DLSS and the HUD): the game's exposure, then per channel the fitted ACES filmic curve +// x (2.51 x + 0.03) / (x (2.43 x + 0.59) + 0.14) and clamp(.., 0, 1), the value it stores. aces_extend changes each such +// clamp in place, so the game's exposure and look stay its own: +// y = aces(x) up to the x where the curve reaches ACES_YP of the SDR white, its tangent above, +// a roll-off of y from half the peak to the peak (support +1440 .y: peak / picture white, at least 1), +// times the picture's white against the HUD's (+1440 .x). +// The values are relative to the HUD's white, as the present's encode expects. The support buffer gets a member at +// 1440 (vk_exec_draw.c support_fill writes it), which the translator's SupportBuffer ends before. Recognised by the +// curve's 2.51 / 0.03 pair feeding a clamp to 0..1: any shader that ends with that curve keeps its highlights. +// Returns the clamps changed (0: not that shader). +enum { SPV_OP_TYPE_INT = 21, SPV_OP_TYPE_BOOL = 20, SPV_OP_TYPE_VECTOR = 23, SPV_OP_TYPE_STRUCT = 30, + SPV_OP_TYPE_POINTER = 32, SPV_OP_FUNCTION = 54, SPV_OP_LOAD = 61, SPV_OP_COMPOSITE_EXTRACT = 81, + SPV_OP_FNEGATE = 127, SPV_OP_FSUB = 131, SPV_OP_FMUL = 133, SPV_OP_FDIV = 136, SPV_OP_SELECT = 169, + SPV_OP_FORD_GREATER = 186, SPV_OP_MEMBER_DECORATE = 72, SPV_DEC_OFFSET = 35, SPV_SC_UNIFORM = 2, GLSL_EXP = 27, + GLSL_FMA = 50 }; +#define ACES_YP 0.8f + +static double aces(double x) { return x * (2.51 * x + 0.03) / (x * (2.43 * x + 0.59) + 0.14); } + +static int aces_extend(uint32_t **spirv, uint64_t *words) { + uint32_t *w = *spirv; + uint64_t n = *words; + uint32_t bound = w[3], glsl = 0, ft = 0, bt = 0, it = 0, v4 = 0, c0 = 0, c1 = 0, c251 = 0, c003 = 0, sb = 0, sb_ptr = 0, + st = 0, st_len = 0, ptr_v4 = 0; + const float f251 = 2.51f, f003 = 0.03f, fone = 1.0f; + uint32_t b251, b003, bone; + memcpy(&b251, &f251, 4), memcpy(&b003, &f003, 4), memcpy(&bone, &fone, 4); + uint32_t last_offset = 0; + uint32_t *def = calloc(bound ? bound : 1, sizeof *def); // result id -> word index of its instruction + if (!def) return 0; + for (uint64_t i = 5; i < n;) { + uint32_t op = w[i] & 0xffff, len = w[i] >> 16; + if (!len || i + len > n) { free(def); return 0; } + const uint32_t *a = w + i + 1; + if (op == SPV_OP_EXT_INST_IMPORT && len >= 3 && name_is(a, len, "GLSL.std.450")) glsl = a[0]; + else if (op == SPV_OP_TYPE_FLOAT && len == 3 && a[1] == 32) ft = a[0]; + else if (op == SPV_OP_TYPE_BOOL && len == 2) bt = a[0]; + else if (op == SPV_OP_TYPE_INT && len == 4 && a[1] == 32 && a[2] == 1) it = a[0]; + else if (op == SPV_OP_TYPE_VECTOR && len == 4 && a[1] == ft && ft && a[2] == 4) v4 = a[0]; + else if (op == SPV_OP_CONSTANT && len == 4 && a[0] == ft && ft) { + if (a[2] == 0) c0 = a[1]; + else if (a[2] == bone) c1 = a[1]; + else if (a[2] == b251) c251 = a[1]; + else if (a[2] == b003) c003 = a[1]; + } else if (op == SPV_OP_NAME && len >= 3 && name_is(a, len, "support_buffer")) sb = a[0]; + else if (op == SPV_OP_TYPE_POINTER && len == 4 && a[1] == SPV_SC_UNIFORM && a[2] == v4 && v4) ptr_v4 = a[0]; + else if (op == SPV_OP_MEMBER_DECORATE && len == 5 && a[1] == 10 && a[2] == SPV_DEC_OFFSET && a[3] == 1424) + last_offset = a[0]; // the struct whose member 10 is at 1424 + if (op == SPV_OP_VARIABLE && len >= 4 && a[1] == sb && sb) sb_ptr = a[0]; + // results: [0] type, [1] id for the instructions that have both + if ((op == SPV_OP_FMUL || op == SPV_OP_FDIV || op == SPV_OP_EXT_INST || op == SPV_OP_LOAD) && len >= 3 && a[1] < bound) + def[a[1]] = (uint32_t)i; + i += len; + } + for (uint64_t i = 5; i < n && sb_ptr; i += w[i] >> 16) // the support buffer's block: pointer -> struct + if ((w[i] & 0xffff) == SPV_OP_TYPE_POINTER && (w[i] >> 16) == 4 && w[i + 1] == sb_ptr) st = w[i + 3]; + for (uint64_t i = 5; i < n && st; i += w[i] >> 16) + if ((w[i] & 0xffff) == SPV_OP_TYPE_STRUCT && w[i + 1] == st) st_len = w[i] >> 16; + // the clamps: FClamp(FMul(FMul(x, Fma(x, 2.51, 0.03)), rcp), 0, 1) + enum { MAX_SITES = 8 }; + uint64_t site[MAX_SITES]; + uint32_t site_x[MAX_SITES]; + int nsite = 0; + if (glsl && ft && bt && it && v4 && c0 && c1 && c251 && c003 && st_len == 13 && last_offset == st) + for (uint64_t i = 5; i < n && nsite < MAX_SITES; i += w[i] >> 16) { + const uint32_t *a = w + i + 1; + if ((w[i] & 0xffff) != SPV_OP_EXT_INST || (w[i] >> 16) != 8 || a[2] != glsl || a[3] != GLSL_FCLAMP || + a[5] != c0 || a[6] != c1 || a[4] >= bound || !def[a[4]]) + continue; + const uint32_t *m = w + def[a[4]]; // FMul(num, rcp) + if ((m[0] & 0xffff) != SPV_OP_FMUL || m[3] >= bound || !def[m[3]]) continue; + const uint32_t *num = w + def[m[3]]; // FMul(x, Fma(x, 2.51, 0.03)) + if ((num[0] & 0xffff) != SPV_OP_FMUL || num[4] >= bound || !def[num[4]]) continue; + const uint32_t *f = w + def[num[4]]; + if ((f[0] & 0xffff) != SPV_OP_EXT_INST || (f[0] >> 16) != 8 || f[4] != GLSL_FMA || f[6] != c251 || f[7] != c003) + continue; + site[nsite] = i, site_x[nsite++] = num[3]; + } + free(def); + if (!nsite) return 0; + // the curve's constants: where it reaches ACES_YP, its slope there, the tangent's offset + double lo = 0, hi = 64; + for (int k = 0; k < 60; k++) *(aces((lo + hi) / 2) < ACES_YP ? &lo : &hi) = (lo + hi) / 2; + double xp = (lo + hi) / 2, slope = (aces(xp + 1e-4) - aces(xp - 1e-4)) / 2e-4; + float fxp = (float)xp, fslope = (float)slope, foff = (float)(ACES_YP - slope * xp), fhalf = 0.5f; + uint32_t id = bound; + uint32_t new_ptr = ptr_v4 ? 0 : id++, ptr = ptr_v4 ? ptr_v4 : new_ptr; + uint32_t i11 = id++, kxp = id++, kslope = id++, koff = id++, khalf = id++; + // the struct's extra word, its member decoration, six global instructions, 22 instructions of at most 8 words a site + uint64_t cap = n + 1 + 5 + 6 * 5 + (uint64_t)nsite * 22 * 8; + uint32_t *o = malloc(cap * sizeof *o); + if (!o) return 0; + uint64_t k = 0; + memcpy(o, w, 5 * sizeof *w), k = 5; + // one instruction: its opcode, then its operands +#define EMIT(...) do { const uint32_t ins_[] = {__VA_ARGS__}; const uint32_t n_ = sizeof ins_ / sizeof *ins_; \ + o[k] = n_ << 16 | ins_[0]; memcpy(o + k + 1, ins_ + 1, (n_ - 1) * sizeof *ins_); k += n_; } while (0) + int globals_done = 0, s = 0; + for (uint64_t i = 5; i < n;) { + uint32_t op = w[i] & 0xffff, len = w[i] >> 16; + const uint32_t *a = w + i + 1; + if (op == SPV_OP_FUNCTION && !globals_done) { // new types and constants close the global section + if (new_ptr) EMIT(SPV_OP_TYPE_POINTER, new_ptr, SPV_SC_UNIFORM, v4); + uint32_t b[4]; + memcpy(&b[0], &fxp, 4), memcpy(&b[1], &fslope, 4), memcpy(&b[2], &foff, 4), memcpy(&b[3], &fhalf, 4); + EMIT(SPV_OP_CONSTANT, it, i11, 11); + EMIT(SPV_OP_CONSTANT, ft, kxp, b[0]); + EMIT(SPV_OP_CONSTANT, ft, kslope, b[1]); + EMIT(SPV_OP_CONSTANT, ft, koff, b[2]); + EMIT(SPV_OP_CONSTANT, ft, khalf, b[3]); + globals_done = 1; + } + if (op == SPV_OP_TYPE_STRUCT && a[0] == st) { // the support block gains a vec4 at 1440 + memcpy(o + k, w + i, len * sizeof *w); + o[k] = (len + 1) << 16 | op; + o[k + len] = v4; + k += len + 1; + } else if (op == SPV_OP_MEMBER_DECORATE && len == 5 && a[0] == st && a[1] == 10 && a[2] == SPV_DEC_OFFSET && + a[3] == 1424) { + memcpy(o + k, w + i, len * sizeof *w), k += len; + EMIT(SPV_OP_MEMBER_DECORATE, st, 11, SPV_DEC_OFFSET, 1440); + } else if (s < nsite && i == site[s]) { + uint32_t x = site_x[s], r = a[4], res = a[1]; + uint32_t pv = id++, hv = id++, g = id++, p = id++, lin = id++, gt = id++, r0 = id++, y = id++, pm = id++, + kn = id++, rng = id++, d = id++, q = id++, nq = id++, e = id++, om = id++, rl = id++, gk = id++, + y2 = id++, gpos = id++, gs = id++; + EMIT(SPV_OP_ACCESS_CHAIN, ptr, pv, sb, i11); + EMIT(SPV_OP_LOAD, v4, hv, pv); + EMIT(SPV_OP_COMPOSITE_EXTRACT, ft, g, hv, 0); + EMIT(SPV_OP_COMPOSITE_EXTRACT, ft, p, hv, 1); + EMIT(SPV_OP_EXT_INST, ft, lin, glsl, GLSL_FMA, x, kslope, koff); // the tangent + EMIT(SPV_OP_FORD_GREATER, bt, gt, x, kxp); + EMIT(SPV_OP_EXT_INST, ft, r0, glsl, GLSL_FMAX, r, c0); + EMIT(SPV_OP_SELECT, ft, y, gt, lin, r0); + EMIT(SPV_OP_EXT_INST, ft, pm, glsl, GLSL_FMAX, p, c1); // the peak, in picture whites + EMIT(SPV_OP_FMUL, ft, kn, pm, khalf); // the knee: half of it + EMIT(SPV_OP_FSUB, ft, rng, pm, kn); + EMIT(SPV_OP_FSUB, ft, d, y, kn); + EMIT(SPV_OP_FDIV, ft, q, d, rng); + EMIT(SPV_OP_FNEGATE, ft, nq, q); + EMIT(SPV_OP_EXT_INST, ft, e, glsl, GLSL_EXP, nq); + EMIT(SPV_OP_FSUB, ft, om, c1, e); + EMIT(SPV_OP_EXT_INST, ft, rl, glsl, GLSL_FMA, rng, om, kn); + EMIT(SPV_OP_FORD_GREATER, bt, gk, y, kn); + EMIT(SPV_OP_SELECT, ft, y2, gk, rl, y); + EMIT(SPV_OP_FORD_GREATER, bt, gpos, g, c0); // the picture's white, 1 when unset + EMIT(SPV_OP_SELECT, ft, gs, gpos, g, c1); + EMIT(SPV_OP_FMUL, ft, res, y2, gs); + s++; + } else { + memcpy(o + k, w + i, len * sizeof *w), k += len; + } + i += len; + } +#undef EMIT + if (!globals_done || s != nsite || k > cap) { + free(o); + return 0; + } + o[3] = id; + free(*spirv); + *spirv = o; + *words = k; + return nsite; +} + void vk_hdr_shader_swap(uint32_t **spirv, uint64_t *words) { if (!vk_hdr_wanted() || vk_hdr_native() || !*spirv || *words < 5) return; + int extended = aces_extend(spirv, words); + if (extended > 0) { + static volatile int32_t exts; + rt_log("hdr: the game's tonemap keeps highlights above the picture's white (%d channels; %d shaders so far)", + extended, plat_inc32(&exts)); + } int unclamped = fsr_unclamp(*spirv, words); if (unclamped > 0) { static volatile int32_t fsrs; @@ -352,7 +529,9 @@ void vk_hdr_metadata(VkSwapchainKHR sc, int new_sc) { VkCtx *c = vk_ctx(); if (!c->has_hdr_metadata || (!new_sc && cfg_version() == sent)) return; sent = cfg_version(); - PFN_vkSetHdrMetadataEXT set = (PFN_vkSetHdrMetadataEXT)vkGetDeviceProcAddr(c->device, "vkSetHdrMetadataEXT"); + // AMD's frame generation swapchain takes it through its own call + PFN_vkSetHdrMetadataEXT set = vk_fg_hdr_metadata_fn(); + if (!set) set = (PFN_vkSetHdrMetadataEXT)vkGetDeviceProcAddr(c->device, "vkSetHdrMetadataEXT"); if (!set) return; Nits n = nits(); VkHdrMetadataEXT m = {.sType = VK_STRUCTURE_TYPE_HDR_METADATA_EXT, diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index 453ea21a..7d1d46ac 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -3062,6 +3062,9 @@ static void lb_compute(const GfxShader *cs) { return; } double t0 = now_ms(); + // at normal priority: while it is in flight a dispatch of the same shader waits for it (vk_pipeline_compute), and an + // idle-priority compile under load held that frame for seconds + if (LB.idle) plat_thread_priority(PLAT_PRIO_NORMAL); VkPipeline pipe = compute_create(cs, l); double ms = now_ms() - t0; lb_lock(); diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index cff7b7b8..cd45addf 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -762,13 +762,18 @@ static void upload(Tex *e) { const nvn_texdesc *d = &t->d; int bw, bh, bpb = nvn_fmt_bpb(d->format, &bw, &bh); if (!bpb || !t->mem) return; - if (!mem_readable(e->pub.addr, e->t.L.size)) { // a stale slot (the bindless pools look every slot up): no upload; - static volatile int32_t n_unmapped; // a later write to mapped storage uploads it (make_fresh_snap) + // A stale slot (the bindless pools look every slot up): no upload; a later write to mapped storage uploads it + // (make_fresh_snap). + if (!mem_readable(e->pub.addr, e->t.L.size)) { + static volatile int32_t n_unmapped; log_limited(&n_unmapped, "texture %#llx (%llu bytes): storage not mapped; not uploaded", (unsigned long long)e->pub.addr, (unsigned long long)e->t.L.size); e->uploaded = 1; return; } + static __thread char ctx[160]; // what this thread copies, for a crash report + snprintf(ctx, sizeof ctx, "texture upload %#llx (%llu bytes, fmt %#x %ux%u)", (unsigned long long)e->pub.addr, + (unsigned long long)e->t.L.size, d->format, d->width, d->height); uint64_t p0 = perf_begin(), u0 = kern_now_ns(); int linear = nvn_tex_is_linear(d), is3d = d->target == NVN_TEXTURE_TARGET_3D, one_d = e->pub.type == VK_IMAGE_TYPE_1D; uint32_t levels = linear ? 1 : e->pub.levels, layers = linear ? 1 : e->pub.layers; @@ -803,6 +808,18 @@ static void upload(Tex *e) { VkCommandBuffer cb = vk_upload_begin(); uint8_t *stg = vk_staging(size, &sb, NULL); if (acheck) rmap = vk_staging(decoded, &rbuf, &ralloc); + // The part's copies out of the storage, with it held mapped (mem_read_hold): the game releases a texture's + // memory on its own thread, and a decommit in the middle of a copy faulted (tex_bl_copy). Only the copies run + // held, so a waiting unmap and the guest's memory calls behind it never wait for the staging block or a decode. + // Storage released since the check above uploads as zeros, as the game no longer has that texture. + uint8_t **tmps = calloc(k1 - k0, sizeof *tmps); + int held = mem_read_hold(e->pub.addr, e->t.L.size); + if (held) plat_crash_context(ctx); + else { + static volatile int32_t n_gone; + log_limited(&n_gone, "texture %#llx (%llu bytes): storage released during the upload; uploaded as zeros", + (unsigned long long)e->pub.addr, (unsigned long long)e->t.L.size); + } for (k = k0; k < k1; k++) { uint32_t lv = rg[k].imageSubresource.mipLevel, ly = rg[k].imageSubresource.baseArrayLayer; uint32_t w = rg[k].imageExtent.width, h = rg[k].imageExtent.height, sl = rg[k].imageExtent.depth; @@ -810,11 +827,24 @@ static void upload(Tex *e) { size_t raw = (size_t)rb * rows * sl; rg[k].bufferOffset -= base; uint8_t *dst = stg + rg[k].bufferOffset, *tmp = (astc && !agpu) || rgb ? malloc(raw) : NULL; - if (nvn_deswizzle(d, lv, ly, t->mem, tmp ? tmp : dst, rb)) { + tmps[k - k0] = tmp; + if (!held) { + memset(tmp ? tmp : dst, 0, raw); + } else if (nvn_deswizzle(d, lv, ly, t->mem, tmp ? tmp : dst, rb)) { log_limited(&n_bad, "texture %#llx fmt %#x target %d: level %u layer %u has no CPU layout; uploaded as zeros", (unsigned long long)e->pub.addr, d->format, d->target, lv, ly); memset(tmp ? tmp : dst, 0, raw); } + } + if (held) { + plat_crash_context(NULL); + mem_read_release(); // the part's reads of the storage are done + } + for (k = k0; k < k1; k++) { // the decodes, from the copies + uint32_t lv = rg[k].imageSubresource.mipLevel, ly = rg[k].imageSubresource.baseArrayLayer; + uint32_t w = rg[k].imageExtent.width, h = rg[k].imageExtent.height, sl = rg[k].imageExtent.depth; + uint32_t rb = (w + bw - 1) / bw * (uint32_t)bpb, rows = (h + bh - 1) / bh; + uint8_t *dst = stg + rg[k].bufferOffset, *tmp = tmps[k - k0]; if (agpu) { ar[k] = (VkAstcRegion){.src = rg[k].bufferOffset, .w = w, .h = h, .slices = sl, .level = lv, .layer = ly}; } else if (astc) { @@ -832,6 +862,7 @@ static void upload(Tex *e) { } free(tmp); } + free(tmps); if (acheck) { raw_copy = malloc(size); memcpy(raw_copy, stg, size); diff --git a/runtime/widescreen.c b/runtime/widescreen.c index 29078e66..e10b0dc1 100644 --- a/runtime/widescreen.c +++ b/runtime/widescreen.c @@ -30,6 +30,7 @@ #define GAME_W 2560 // the picture, main+0x1202ee0 and main+0x31f39f4 #define GAME_H 1440 #define SCENE_H 1080 +#define GAME_DTA_KIB 312320.0 // the texture heap made for that picture (wide_heap_extra_kib) // The window's size, or the monitor's in fullscreen; 0 when unknown. static int screen_size(int *w, int *h) { @@ -107,6 +108,35 @@ void wide_present(void) { } } +// The picture the settings ask for, *ow x *oh; 0 when it is the game's own 2560x1440 (every set stays off). Read at the +// start, before the window exists: the monitor's size is known then. +static int picture_size(unsigned *ow, unsigned *oh, int quiet) { + double r = wanted_ratio(), game = (double)GAME_W / GAME_H; + *oh = picture_height(); + if (r <= 0 || r < game * 1.01) { + if (r > 0 && !quiet) rt_log("widescreen: a %.4f picture is not wider than 16:9; the game's 16:9", r); + r = game; + } + if (r == game && *oh == GAME_H) return 0; + if (r > 32.0 / 9.0) r = 32.0 / 9.0; + *ow = (unsigned)lround(*oh * r / 8) * 8; // whole multiples of 8 pixels + return 1; +} + +// The texture heap: the engine's render targets live in "DynamicTextureAllocatorMem" of the HeapDef table +// (System/HeapSize/Product.Nin_Ounce_NVN2.HeapDef.byml.zs; 312320 KiB, carved from "Graphics"), made for the 2560x1440 +// picture. Larger pictures' targets did not fit: the render-target manager's search for a free block (main+0x3ed00) +// fell to its out-of-memory path and read through a null block when the pause menu's blur needed one at 5120x2160. +// The heap and its parent grow by the shipped size times the picture's pixels over 2560x1440 (its targets are the +// picture's size or a share of it), in whole MiB; resolution.c rewrites the table (res_heapdef_build) and memory.c adds +// the amount to the application pool, so no other heap shrinks. 0 without the widescreen sets or for the game's size. +uint64_t wide_heap_extra_kib(void) { + unsigned ow, oh; + if (!wide_available() || !picture_size(&ow, &oh, 1)) return 0; + double grow = (double)ow * oh / ((double)GAME_W * GAME_H) - 1; + return grow > 0 ? (uint64_t)ceil(GAME_DTA_KIB * grow / 1024) * 1024 : 0; +} + void wide_init(void) { int so = rt_patch_set("wide_out"), ss = rt_patch_set("wide_scene"), sa = rt_patch_set("wide_aspect"), sh = rt_patch_set("wide_hud"), sm = rt_patch_set("wide_movie"); @@ -114,22 +144,16 @@ void wide_init(void) { // frame (3/4 of the picture at most), and writing other sizes into wide_scene's constant or the 1920x1080 render // sizes left the light target where the controller put it. DLSS's modes fix the share through res_fixed instead // (wide_present). - double r = wanted_ratio(), game = (double)GAME_W / GAME_H, f = 0.75; - unsigned oh = picture_height(); - if (r <= 0 || r < game * 1.01) { - if (r > 0) rt_log("widescreen: a %.4f picture is not wider than 16:9; the game's 16:9", r); - r = game; - } - if (r == game && oh == GAME_H && f == 0.75) return; // the game's own sizes: every set stays off + double game = (double)GAME_W / GAME_H, f = 0.75; + unsigned ow, oh; + if (!picture_size(&ow, &oh, 0)) return; // the game's own sizes: every set stays off if (so < 0 || ss < 0 || sa < 0 || sh < 0 || sm < 0) { rt_log("widescreen: the generated C has no widescreen patch sets (gen.py --patches recomp/patches-v111.txt); " "the game's own 2560x1440 picture"); return; } - if (r > 32.0 / 9.0) r = 32.0 / 9.0; - // Widths in whole multiples of 8 pixels, the scene's height even: the picture oh high and as wide as its shape, the - // scene 3/4 of both. - unsigned ow = (unsigned)lround(oh * r / 8) * 8, sw = (unsigned)lround(ow * f / 8) * 8; + // The scene 3/4 of both sides, its width in whole multiples of 8 pixels and its height even. + unsigned sw = (unsigned)lround(ow * f / 8) * 8; unsigned sch = (unsigned)lround(oh * f / 2) * 2; uint64_t out = (uint64_t)oh << 32 | ow, scene = (uint64_t)sch << 32 | sw; float match = 16.0f / 9.0f, ratio = (float)ow / oh, tolerance = 0.01f, squeeze = match / ratio; diff --git a/tools/make_kit.py b/tools/make_kit.py index 032f7472..d0494d83 100644 --- a/tools/make_kit.py +++ b/tools/make_kit.py @@ -13,7 +13,7 @@ import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] -NAME = 'OcarinaRecomp-BuildKit' +NAME = 'NX2navi-BuildKit' TOOLS = ('make_game.py', 'nup.py', 'romfs_index.py', 'check_content.py', 'hostmem.py', 'prepare_shaders.py', 'shader_census.py', 'shader_list.py', 'zstd_compat.py') TOP = ('LICENSE', 'LICENSE-ADDITIONS', 'THIRD_PARTY_NOTICES.md', 'VERSION', 'nx2navi.ini.example') diff --git a/tools/make_launcher.py b/tools/make_launcher.py index 317c9fe5..a217578b 100644 --- a/tools/make_launcher.py +++ b/tools/make_launcher.py @@ -202,7 +202,9 @@ def build_native(work, cc, windres): (work / 'nx2navi.ico').write_bytes(art.triforce_ico()) shutil.copy2(ROOT / 'launcher' / 'native' / 'app.manifest', work / 'app.manifest') res = work / 'res.o' - run([*windres, '-I', work, ROOT / 'launcher' / 'native' / 'res.rc', '-O', 'coff', '-o', res]) + # windres preprocesses with the gcc it finds on PATH: the toolchain's own bin first (no MSYS2 shell sets it) + env = dict(os.environ, PATH=str(Path(windres[0]).parent) + os.pathsep + os.environ.get('PATH', '')) + run([*windres, '-I', work, ROOT / 'launcher' / 'native' / 'res.rc', '-O', 'coff', '-o', res], env=env) flags = ['-municode', '-mwindows', '-O2', '-Wall', '-static', '-s'] stub = work / 'NX2navi.exe' setup = work / 'setup-stub.exe' diff --git a/tools/xlate/AmpereLowering.cs b/tools/xlate/AmpereLowering.cs index c49c59e6..8a5cbed4 100644 --- a/tools/xlate/AmpereLowering.cs +++ b/tools/xlate/AmpereLowering.cs @@ -18,7 +18,9 @@ // SpillFrom up live in local memory after the program's own, and an instruction that names one works on a block of // four temporaries (R188-R203, one block per aligned group of four guest registers, so pairs and quads stay // contiguous) loaded before it and stored after it; uniform registers then take blocks of four from R204-R235 (eight -// groups: an Ocarina mesh program used more than the four R220-R235 held). +// groups: an Ocarina mesh program used more than the four R220-R235 held). A program with more than eight groups of +// uniform registers is lowered once more in the wide spill layout: guest registers from R156 spilled, temporaries +// R156-R171, uniform registers R172-R235 (sixteen groups, UR0-UR62). Programs that fit keep the first layout. using System; using System.Collections.Generic; @@ -43,7 +45,21 @@ static class AmpereLowering public RegisterPressureException(string message) : base(message) { } } - const int SpillFrom = 188, TempBase = 188, UrPool = 204, UrPoolEnd = 236, SpillBytes = (256 - SpillFrom) * 4; + // The uniform registers do not fit the spill layout's pool: Lower retries in the wide spill layout. + sealed class UrPoolException : LoweringException + { + public UrPoolException(string message) : base(message) { } + } + + // Spill layouts: guest registers from SpillFrom up in local memory, the temporaries from there, the uniform pool + // after them up to UrPoolEnd. + readonly record struct SpillLayout(int SpillFrom, int UrPool) + { + public int TempBase => SpillFrom; + public int SpillBytes => (256 - SpillFrom) * 4; + } + const int UrPoolEnd = 236; + static readonly SpillLayout Spill = new(188, 204), SpillWide = new(156, 172); // Task (NVN2 stage 6) and mesh (stage 7) programs. Both are lowered as compute programs: the stage buffer they // write with ISBEWR and read with ISBERD becomes shared memory, laid out by this record, and MeshSpirv.cs turns @@ -120,12 +136,15 @@ static class AmpereLowering readonly MeshInfo _mesh; // task or mesh program (lowered as compute), else null public byte[] C1; // the program's c1 constants (NVN2: after the code; BRX jump tables), or null readonly int _spillAt = -1; // spill mode: byte offset of guest register SpillFrom in local memory; -1 otherwise + readonly SpillLayout _sl; // spill mode: the layout + int SpillFrom => _sl.SpillFrom; readonly Dictionary _temps = new(); // spill mode: guest register group (r & ~3) -> its temporaries, this instruction readonly Dictionary _urBlocks = new(); // spill mode: uniform register group (n & ~3) -> its registers - public Lowerer(Insn[] code, byte[] header, bool compute, int[] table, MeshInfo mesh, int spillAt = -1) + public Lowerer(Insn[] code, byte[] header, bool compute, int[] table, MeshInfo mesh, int spillAt = -1, SpillLayout layout = default) { _code = code; _header = header; _compute = compute; _table = table; _mesh = mesh; _firstOutput = new int[code.Length + 1]; _spillAt = spillAt; + _sl = layout.SpillFrom != 0 ? layout : Spill; int highest = HighestGpr(code); if (spillAt < 0 && highest >= UrLimit) throw new RegisterPressureException($"guest registers up to R{highest}"); UrBase = spillAt >= 0 ? SpillFrom : (highest + 2) & ~1; @@ -139,7 +158,7 @@ static class AmpereLowering if (!_temps.TryGetValue(g, out int t)) { if (_temps.Count == 4) throw Fail("more than four spilled register groups in one instruction"); - t = TempBase + 4 * _temps.Count; + t = _sl.TempBase + 4 * _temps.Count; _temps[g] = t; for (int k = 0; k < 4; k++) // LDL.32 t+k, [RZ + slot] Emit(Unguarded(0xef40000000000000UL | (uint)(t + k) | ((ulong)RZ << 8) | ((ulong)(uint)(_spillAt + 4 * (g - SpillFrom + k)) << 20) | (4UL << 48))); @@ -223,8 +242,8 @@ static class AmpereLowering int q = (int)reg & ~3; if (!_urBlocks.TryGetValue(q, out int b)) { - b = UrPool + 4 * _urBlocks.Count; - if (b + 4 > UrPoolEnd) throw Fail("more uniform register groups than spill mode has room for"); + b = _sl.UrPool + 4 * _urBlocks.Count; + if (b + 4 > UrPoolEnd) throw new UrPoolException(Fail("more uniform register groups than spill mode has room for").Message); _urBlocks[q] = b; } return b + ((int)reg & 3); @@ -471,7 +490,7 @@ static class AmpereLowering case 0x23c: Hmma(); return; case 0x23a: Movm(); return; case 0x306: case 0xb06: case 0xd06: I2f(op); return; - case 0x387: case 0x983: LocalAccess(op == 0x387); return; + case 0x387: case 0x987: case 0x983: LocalAccess(op != 0x983); return; case 0x526: IpaIndexed(); return; case 0xabb: UldcIndexed(); return; } @@ -2057,7 +2076,7 @@ static class AmpereLowering // the low half and zeroes the high half, ISCADD merges the column pair). // The work runs unguarded because every lane must supply its fragments to the shuffles; only the result writes // take the instruction's guard. Scratch R239-R252 (this file's scratch range, see the header); the guest operands - // are never there (guest and uniform registers end below R237, spill temporaries are R188-R235). + // are never there (guest and uniform registers end below R237, spill temporaries and uniform registers are R156-R235). void Hmma() { bool k16 = _i.Bit(75), f32 = _i.Bit(76); @@ -2175,13 +2194,24 @@ static class AmpereLowering } // STL / LDL (local memory) at [Ra + offset 40-63], size 73-75 (U8 S8 U16 S16 32 64 128); 64 and 128 bits go as - // 32-bit words. STL data at 32-39. A uniform address (bit 91) is not lowered. + // 32-bit words. STL data at 32-39. With bit 91 the address adds a uniform register URb (STL 0x987, LDL 0x983 in + // that form): a store's at 64-69, a load's at 32-37 (its B field; Ocarina's mesh programs write that register just + // before the access). Ra + URb goes to a scratch register first, or URb's register is the address when Ra is RZ. void LocalAccess(bool store) { uint size = _i.Bits(73, 3), allowed = store ? 0xe00u : 0x200e00u; - if (size > 6 || (_i.W2 & ~allowed) != 0x00100000 || (!store && _i.Bits(32, 8) != 0) || (store && _i.Bits(16, 8) != 0)) + bool uniform = _i.Bit(91); + uint w2 = uniform ? _i.W2 & ~(store ? 0x0800003fu : 0x08000000u) : _i.W2; + uint bField = _i.Bits(32, 8) & (uniform && !store ? 0xc0u : 0xffu); + if (size > 6 || (w2 & ~allowed) != 0x00100000 || (!store && bField != 0) || (store && _i.Bits(16, 8) != 0)) throw Fail("local memory mode"); int addr = Gpr(_i.Bits(24, 8)), value = store ? Gpr(_i.Bits(32, 8)) : Gpr((uint)Dst); + if (uniform) + { + int ub = Ur(store ? _i.Bits(64, 6) : _i.Bits(32, 6)); + if (ub != RZ && addr == RZ) addr = ub; + else if (ub != RZ) { Iadd3(SX, addr, ub, RZ, false); addr = SX; } + } uint off = _i.Bits(40, 24), words = size == 6 ? 4u : size == 5 ? 2u : 1u; if (!store && words > 1 && addr != RZ && addr >= value && addr < value + words) { Mov(SX, addr, false); addr = SX; } for (uint k = 0; k < words; k++) @@ -2432,8 +2462,16 @@ static class AmpereLowering if (header != 0) spillAt = ((int)(BitConverter.ToUInt32(code, 4) & 0xffffff) + (int)(BitConverter.ToUInt32(code, 8) & 0xffffff) + (int)(BitConverter.ToUInt32(code, 12) & 0xffffff) / 32 + 15) & ~15; - lowered = new Lowerer(insns, sph, !graphics, table, mesh, spillAt) { C1 = C1Bytes }.Run(out branches, out first); - localBytes = spillAt + SpillBytes; + try + { + lowered = new Lowerer(insns, sph, !graphics, table, mesh, spillAt, Spill) { C1 = C1Bytes }.Run(out branches, out first); + localBytes = spillAt + Spill.SpillBytes; + } + catch (UrPoolException) + { + lowered = new Lowerer(insns, sph, !graphics, table, mesh, spillAt, SpillWide) { C1 = C1Bytes }.Run(out branches, out first); + localBytes = spillAt + SpillWide.SpillBytes; + } } int start = graphics ? 0x50 : 0;