diff --git a/gpu/vk/spv_exact.h b/gpu/vk/spv_exact.h new file mode 100644 index 0000000..769f91a --- /dev/null +++ b/gpu/vk/spv_exact.h @@ -0,0 +1,115 @@ +// Which texture bindings of a translated shader are read by texel position or asked for their size (vk_shader.cpp +// make_module, for the texture detail of tex_detail.h): a texture bound there must be whole, as a fetch at a level the +// image lacks, or coordinates worked out from a size that is half the game's, read the wrong texels. A sampled read +// (normalised coordinates and derivatives) picks the same level of the game's texture wherever that level is kept. +// tests/test_spv_exact.py +// +// Counted: OpImageFetch, OpImageQuerySizeLod, OpImageQuerySize and OpImageQueryLevels on a sampled, single-sample 2D +// texture or 2D array, the only kinds the texture detail leaves levels out of. Not counted: a size query on an array +// whose result is used for its third component alone, the layer count, which no level changes. +#pragma once +#include +#include +#include +#include +#include + +namespace spv_exact { + +using Bindings = std::array; // (a bit a binding, 0 to 255; set 0 is the only set the translation uses) + +inline bool any(const Bindings& b) { return (b[0] | b[1] | b[2] | b[3]) != 0; } +inline bool has(const Bindings& b, uint32_t binding) { return binding < 256 && (b[binding >> 6] >> (binding & 63) & 1); } + +inline Bindings scan(const uint32_t* w, size_t n) { + Bindings out{}; + if (n < 5 || w[0] != 0x07230203u) return out; + enum : uint16_t { + OpTypeImage = 25, OpTypeSampledImage = 27, OpTypeArray = 28, OpTypeRuntimeArray = 29, OpTypePointer = 32, + OpVariable = 59, OpLoad = 61, OpAccessChain = 65, OpInBoundsAccessChain = 66, OpDecorate = 71, + OpCompositeExtract = 81, OpCopyObject = 83, OpImageFetch = 95, OpImage = 100, OpImageQuerySizeLod = 103, + OpImageQuerySize = 104, OpImageQueryLevels = 106, + }; + struct Image { bool d2, arrayed, ms, sampled; }; + std::unordered_map binding, inner, pointee, var_type, from; + std::unordered_map images; + struct Use { uint32_t image, result; bool size; }; + std::vector uses; + for (size_t i = 5; i < n;) { + const uint32_t op = w[i] & 0xffffu, wc = w[i] >> 16; + if (!wc || i + wc > n) break; + const uint32_t* a = w + i + 1; + const uint32_t na = wc - 1; + switch (op) { + case OpDecorate: + if (na >= 3 && a[1] == 33) binding[a[0]] = a[2]; // (Binding) + break; + case OpTypeImage: // (result, sampled type, dim, depth, arrayed, ms, sampled, format) + if (na >= 7) images[a[0]] = Image{a[2] == 1, a[4] != 0, a[5] != 0, a[6] == 1}; + break; + case OpTypeSampledImage: case OpTypeArray: case OpTypeRuntimeArray: + if (na >= 2) inner[a[0]] = a[1]; + break; + case OpTypePointer: + if (na >= 3) pointee[a[0]] = a[2]; + break; + case OpVariable: + if (na >= 2) var_type[a[1]] = a[0]; + break; + case OpLoad: case OpAccessChain: case OpInBoundsAccessChain: case OpImage: case OpCopyObject: + if (na >= 3) from[a[1]] = a[2]; + break; + case OpImageFetch: case OpImageQueryLevels: + if (na >= 3) uses.push_back({a[2], a[1], false}); + break; + case OpImageQuerySizeLod: case OpImageQuerySize: + if (na >= 3) uses.push_back({a[2], a[1], true}); + break; + default: break; + } + i += wc; + } + if (uses.empty()) return out; + // (a size query's result used for something other than its third component: every word of every other instruction + // that equals the result's id counts, a literal that happens to as well: the texture then stays whole, the safe side) + std::unordered_map other_use; + for (const Use& u : uses) + if (u.size) other_use[u.result] = false; + if (!other_use.empty()) + for (size_t i = 5; i < n;) { + const uint32_t op = w[i] & 0xffffu, wc = w[i] >> 16; + if (!wc || i + wc > n) break; + const bool layer_only = op == OpCompositeExtract && wc == 5 && w[i + 4] == 2; + for (uint32_t k = (op == OpImageQuerySizeLod || op == OpImageQuerySize) ? 3 : 1; k < wc; k++) { + const auto it = other_use.find(w[i + k]); + if (it != other_use.end() && !(layer_only && k == 3)) it->second = true; + } + i += wc; + } + for (const Use& u : uses) { + uint32_t v = u.image; + for (int hops = 0; hops < 16; hops++) { + const auto f = from.find(v); + if (f == from.end()) break; + v = f->second; + } + const auto vt = var_type.find(v); + const auto b = binding.find(v); + if (vt == var_type.end() || b == binding.end() || b->second >= 256) continue; + const auto p = pointee.find(vt->second); + if (p == pointee.end()) continue; + uint32_t t = p->second; + for (int hops = 0; hops < 8; hops++) { + const auto in = inner.find(t); + if (in == inner.end()) break; + t = in->second; + } + const auto im = images.find(t); + if (im == images.end() || !im->second.d2 || im->second.ms || !im->second.sampled) continue; + if (u.size && im->second.arrayed && !other_use[u.result]) continue; // (the layer count alone) + out[b->second >> 6] |= 1ull << (b->second & 63); + } + return out; +} + +} // namespace spv_exact diff --git a/gpu/vk/tex_detail.h b/gpu/vk/tex_detail.h new file mode 100644 index 0000000..9044454 --- /dev/null +++ b/gpu/vk/tex_detail.h @@ -0,0 +1,57 @@ +// Texture detail by the card's video memory (vk_texture.cpp create): which cards make the game's large material +// textures without their largest mip levels, and which textures that takes. After NX2navi's texture_detail +// (its runtime/vk_tex_detail.h and vk_texture.c "texture detail"; the figures below are its measurements). +// tests/test_tex_detail.py +// +// The material textures of a loaded area are what fills a small card, and three quarters of a mipmapped texture is its +// largest level. Past what the driver allows, Windows moves video memory into system memory and the frame rate falls +// to a fraction. A walk through Castle Town with the allowance taken as a 6 GB card's 4,730 MB held 6,907 MB whole, +// 5,472 MB without the largest level and 4,987 MB without two; a tour sized for an 8 GB card ran 29.6 fps (1% low +// 11.4) whole and 42.8 (36.4) without the largest level, nothing spilled. +#pragma once +#include +#include + +namespace tex_detail { + +enum Level { kFull = 0, kReduced = 1, kLow = 2 }; + +// What the driver allows the process at the start, in MB: full from here on (the cards of 12 GB: 11,250-11,313 MB +// in players' logs; a 10 GB card's ~9,300 MB is under it, as what "full" takes is more than its textures) +constexpr uint64_t kFullMb = 10240; +// low under this (every 6 GB card); reduced between (8 GB cards: ~7,100-7,600 MB) +constexpr uint64_t kLowMb = 6144; +// A texture loses a level only from this size each way, two only from kLowMin: interface art, glyph atlases, lookup +// tables and ramps are smaller or have no mip levels +constexpr uint32_t kMin = 256; +constexpr uint32_t kLowMin = 1024; + +/// The level "auto" picks. allowed: bytes, 0 when the driver reports none (then full, as before). integrated: the +/// video memory is the system's own, whose allowance is a share of the PC's memory, not memory of its own: never full +/// (a handheld with 16 GB in all reported 10,361 MB and ended at a save's load with everything whole). +inline int by_vram(uint64_t allowed, bool integrated) { + const uint64_t mb = allowed >> 20; + if (!mb) return kFull; + int level = mb >= kFullMb ? kFull : mb >= kLowMb ? kReduced : kLow; + if (level == kFull && integrated) level = kReduced; + return level; +} + +/// RX_VK_TEX_DETAIL's value: 0 full, 1 reduced, 2 low; -1 for auto, unset or anything else. +inline int parse(const char* v) { + if (!v) return -1; + return !strcmp(v, "full") ? kFull : !strcmp(v, "reduced") ? kReduced : !strcmp(v, "low") ? kLow : -1; +} + +/// The top levels a texture is made without at that detail level. w, h, levels: the texture whole. The caller has +/// checked what the sizes cannot say: a decoded ASTC texture, 2D or a 2D array, one sample, sampled only. +inline uint32_t skip(int level, uint32_t w, uint32_t h, uint32_t levels) { + if (level <= kFull || levels < 2 || w < kMin || h < kMin) return 0; + return level >= kLow && levels >= 4 && w >= kLowMin && h >= kLowMin ? 2 : 1; +} + +inline const char* name(int level) { + return level == kFull ? "full" : level == kReduced ? "reduced" : "low"; +} + +} // namespace tex_detail diff --git a/gpu/vk/texture_gone.h b/gpu/vk/texture_gone.h new file mode 100644 index 0000000..a650904 --- /dev/null +++ b/gpu/vk/texture_gone.h @@ -0,0 +1,49 @@ +// (hookshot) Which textures were destroyed, by their creation number (Texture::id), apart from Vulkan so that +// tests/test_texture_gone.py can run it. +// +// A texture pointer kept from one draw to a later one (vk_temporal.cpp: the scene's colour and depth from the scene's draws +// to the engine's pass, the upscale's output from one frame's dispatch to the next frame's tone mapping) has to be known +// to be the same texture still. texture_destroy_generation says only that some texture went since, anywhere: with the video +// memory full the cache lets textures go in most frames, and each of those frames then lost the engine's picture (the tone +// mapping was not drawn at the output's size, and the frame was the scene's size scaled up: a soft frame, some every second +// on an 8 GB card at 2560x1440). This says whether that one texture went. +#pragma once + +#include +#include + +namespace texture_gone { + +/// The creation numbers of the textures destroyed last, and how many went in all. A texture noted while it was there (its +/// number, and count() then) is there still while its number is not among those that went since. When more went since than +/// are kept, or the number is none (0), it cannot be told: it counts as gone, which costs a frame what the old rule cost it +class Record { +public: + static constexpr uint64_t kKept = 1024; + + void went(uint32_t id) { + std::lock_guard lk(mu_); + ids_[n_ % kKept] = id; + n_++; + } + + uint64_t count() const { + std::lock_guard lk(mu_); + return n_; + } + + bool gone_since(uint32_t id, uint64_t since) const { + std::lock_guard lk(mu_); + if (!id || since > n_ || n_ - since > kKept) return true; + for (uint64_t i = since; i < n_; i++) + if (ids_[i % kKept] == id) return true; + return false; + } + +private: + mutable std::mutex mu_; + uint32_t ids_[kKept] = {}; + uint64_t n_ = 0; +}; + +} // namespace texture_gone diff --git a/gpu/vk/vk_api.h b/gpu/vk/vk_api.h index be36f3b..a41c21f 100644 --- a/gpu/vk/vk_api.h +++ b/gpu/vk/vk_api.h @@ -26,10 +26,15 @@ enum { HOS_OPT_DLSS_JITTER_W = 24, HOS_OPT_DLSS_JITTER_H, HOS_OPT_DLSS_JITTER_PH /* (hookshot) the Insert menu's HDR set to Native (host/nn_vi.c QueryDisplayHdrStatus and QueryDisplayHdrPaperWhite): whether the game is told its display is HDR10 (the swapchain is one), and the peak and paper white it is told, in nits */ /* (HOS_OPT_DISCORD, 43: the Insert menu's Discord status row, host/discord_rpc.c: 1 on) */ -enum { HOS_OPT_HDR_NATIVE = 40, HOS_OPT_HDR_PEAK, HOS_OPT_HDR_WHITE, HOS_OPT_DISCORD }; +/* (HOS_OPT_SPRINT, 45, and HOS_OPT_SPRINT_HOLD, 46: the Sprint button row (0 off, 1 the left stick's click, 2 B) and + the Sprint mode row (0 toggle, 1 hold), host/oot_sprint.c) */ +/* (HOS_OPT_VOLUME, 44: the Insert menu's Master volume row, host/nn_audio.c: percent of the game's own, 0 to 100) */ +enum { HOS_OPT_HDR_NATIVE = 40, HOS_OPT_HDR_PEAK, HOS_OPT_HDR_WHITE, HOS_OPT_DISCORD, HOS_OPT_VOLUME, HOS_OPT_SPRINT, HOS_OPT_SPRINT_HOLD }; /* (Triforce) option_set only, each frame the game's own Options screen runs (host/oot_menu.c): the menu then draws its tabs over that screen's header, the game's own list the first of them (gpu/vk/vk_menu.cpp game_options_shown) */ -enum { HOS_OPT_GAME_OPTIONS = 48 }; +/* (Triforce) HOS_OPT_EXITING, option_set only: the process is about to exit (host/hos_relaunch.c): what must be shut + down in order before the DLLs are unloaded is (NVIDIA's Streamline, gpu/vk/vk_dlssg.cpp) */ +enum { HOS_OPT_GAME_OPTIONS = 48, HOS_OPT_EXITING }; /* (hookshot) the controls' names (host/nn_hid.c, host/pad_sdl.c, the pad_ and key_ lines of triforce_options.ini): the Switch's buttons in nn::hid's bit order, and a gamepad's inputs by position, SDL's order (the bottom face diff --git a/gpu/vk/vk_dlssg.cpp b/gpu/vk/vk_dlssg.cpp new file mode 100644 index 0000000..613209c --- /dev/null +++ b/gpu/vk/vk_dlssg.cpp @@ -0,0 +1,492 @@ +// (Triforce) NVIDIA DLSS Frame Generation through NVIDIA's Streamline, after NX2navi's runtime/vk_dlssg.c and +// runtime/dlssg/nx2_sl.cpp (its additions are in the public domain, over an MIT base). +// +// Asked for by the Insert menu's DLSS frame generation row (2x, 3x, 4x; dlss_frame_generation= in the options file) or +// RX_VK_DLSSG=<2|3|4>, read once at the start: Streamline has to be up before the Vulkan instance is made, and the +// instance, the device and every function of both are then fetched through its interposer (the SDK's manual hooking: +// its vkCreateInstance and vkCreateDevice add the extensions, features and queues DLSS-G needs, and its swapchain +// functions are where the generated frames go in). Nothing of Streamline is linked: sl.interposer.dll is loaded from +// the game's folder (or its dlssg folder) with its plugins sl.common, sl.dlss_g, sl.pcl, sl.reflex and nvngx_dlssg.dll; +// without them, or on another vendor's card, the game starts as without the row. +// +// Each frame of the game: where it starts (after the present of the one before) Reflex's sleep and the frame's first +// latency markers, the submission's end before the present's submit, and at the present the camera, the depth and the +// motion vectors of AMD FSR 3.1's inputs (vk_fsr3_inputs.cpp makes them at the gameplay upscale whatever the upscaler: +// the depth reversed, the motion in render pixels toward the frame before), then the present through the interposer, +// which shows one to three generated frames between two of the game's. DLSS-G needs Reflex, which is Streamline's +// here: the renderer's own Reflex (vk_reflex.cpp) and its own frame generation (vk_framegen.cpp) stand aside. +// Generation pauses while the menu is open and while a frame has no scene (a pause, a load, a movie), after the new +// state has held for 12 presents. +// +// Built only with HOOKSHOT_STREAMLINE_SDK= (nx2/build.py; NVIDIA's headers stay outside the +// repository); without it every function answers "off". RX_VK_DLSSG_LOG=1: Streamline's own information lines too. +#include "vk_dlssg.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "dlss_math.h" +#include "vk_fsr3_inputs.h" +#include "vk_menu.h" +#include "vk_paths.h" + +#ifdef HOOKSHOT_DLSSG +#include +#include +#include +#include +#include +#include +#endif + +namespace mkwvk::dlssg { + +namespace { + +/// dlss_frame_generation= in triforce_options.ini (the menu's own load comes after the instance is made) +int ini_value() { + char exe[MAX_PATH]; + const DWORD n = game_module_path(exe, MAX_PATH); + std::string dir(exe, n); + const std::string path = dir.substr(0, dir.find_last_of("\\/") + 1) + "triforce_options.ini"; + int v = 0; + if (FILE* f = fopen(path.c_str(), "r")) { + char line[160]; + while (fgets(line, sizeof line, f)) + if (!strncmp(line, "dlss_frame_generation=", 22)) v = atoi(line + 22); + fclose(f); + } + return v; +} + +const char* state_line = "Not asked for."; + +} // namespace + +int asked() { + static const int v = [] { + const char* e = getenv("RX_VK_DLSSG"); + const int row = e && *e ? atoi(e) : ini_value(); // (the row: 0 off, 1 2x, 2 3x, 3 4x; the switch: the multiplier itself) + const int mult = e && *e ? row : row > 0 ? row + 1 : 0; + return mult >= 2 ? (mult > 4 ? 4 : mult) : 0; + }(); + return v; +} + +#ifndef HOOKSHOT_DLSSG + +PFN_vkGetInstanceProcAddr early() { + if (asked()) logf("vk: dlssg: this build has no NVIDIA DLSS frame generation (built without NVIDIA's Streamline SDK)"); + if (asked()) state_line = "This build was made without NVIDIA's Streamline SDK."; + return nullptr; +} +void device_extensions(std::vector&, const std::vector&) {} +void gave_up(const char*) {} +void device_ready(VkPhysicalDevice, VkDevice) {} +bool active() { return false; } +void swapchain_created() {} +void frame_start() {} +void submit_end() {} +void frame(VkCommandBuffer, uint32_t, uint32_t) {} +void present_marker(bool) {} +int multiplier() { return 1; } +const char* status() { return state_line; } +void shutdown() {} + +#else + +namespace { + +HMODULE mod; +PFun_slInit* p_init; +PFun_slShutdown* p_shutdown; +PFun_slIsFeatureLoaded* p_loaded; +PFun_slIsFeatureSupported* p_supported; +PFun_slSetTagForFrame* p_tag; +PFun_slSetConstants* p_constants; +PFun_slGetNewFrameToken* p_token; +PFun_slGetFeatureFunction* p_feature; +PFun_slDLSSGSetOptions* p_dlssg_options; +PFun_slDLSSGGetState* p_dlssg_state; +PFun_slPCLSetMarker* p_marker; +PFun_slReflexSetOptions* p_reflex; +PFun_slReflexSleep* p_sleep; +PFN_vkGetInstanceProcAddr p_gipa; + +int loaded; // 1: Streamline is up and the instance goes through it; -1: given up for this run +std::atomic running{false}; // the device was made through it and DLSS-G answers: active() +std::atomic frame_no{0}; // the game's frame being made: numbered at frame_start +std::atomic frames_max{0}; // numFramesToGenerateMax as last reported, 0 before +std::atomic shown_mult{1}; +int frames_set = -1; // the frames to generate as last set (0 off), -1 before the first present +int mode_now, mode_pending; // generation as the driver has it, and the presents since the other state was wanted +uint64_t presented, shown; +constexpr int kSettle = 12; + +void sl_log(sl::LogType type, const char* msg) { + static const bool all = getenv("RX_VK_DLSSG_LOG") != nullptr; + // (its information lines stay out, but for the one that says why no frame is generated: a window that is not the one + // in front, a present mode it does not take, ...: said when it changes) + if (type == sl::LogType::eInfo && !all) { + const char* why = msg ? strstr(msg, "DLSS-G disabled") : nullptr; + if (!why) return; + static std::string last; + static std::mutex mu; + std::lock_guard lk(mu); + std::string line(why); + while (!line.empty() && (line.back() == '\n' || line.back() == '\r')) line.pop_back(); + if (line == last) return; + last = line; + logf("vk: dlssg: Streamline: %s", line.c_str()); + return; + } + static std::atomic n{0}; + if (n++ < (all ? 8000 : 400)) { + std::string line(msg ? msg : ""); + while (!line.empty() && (line.back() == '\n' || line.back() == '\r')) line.pop_back(); + logf("vk: dlssg: streamline %s: %s", type == sl::LogType::eError ? "error" : type == sl::LogType::eWarn ? "warning" : "info", + line.c_str()); + } +} + +template T* proc(const char* name) { return reinterpret_cast(GetProcAddress(mod, name)); } + +/// The token of frame n. Tokens come from a small ring Streamline uses again, so each call takes its own right before use +sl::FrameToken* token(uint32_t n) { + sl::FrameToken* t = nullptr; + return n && p_token(t, &n) == sl::Result::eOk ? t : nullptr; +} + +/// A matrix of ours (row major, clip = M * position, column vectors) as Streamline takes it: for row vectors, so transposed +void matrix(sl::float4x4& m, const float* v) { + for (int r = 0; r < 4; r++) m[r] = sl::float4(v[r], v[4 + r], v[8 + r], v[12 + r]); +} + +/// The inverse of m (row major), false when it has none +bool inverse(const float* m, float out[16]) { + static const float identity[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; + return dlss_math::reprojection(m, identity, out); // (identity * inverse(m)) +} + +void reflex_apply() { + if (!p_reflex) return; + sl::ReflexOptions o{}; + // (the Insert menu's NVIDIA Reflex row says with or without boost; off there is still on here: DLSS-G needs it) + o.mode = opts.reflex == 2 ? sl::ReflexMode::eLowLatencyWithBoost : sl::ReflexMode::eLowLatency; + o.frameLimitUs = 0; // (the frame limit stays the game's own pacer's) + const sl::Result r = p_reflex(o); + static int n; + if (n++ < 4 || r != sl::Result::eOk) + logf("vk: dlssg: Reflex low latency %s (%d)", opts.reflex == 2 ? "on with boost" : "on", (int)r); +} + +/// The interposer and its plugins: beside the game, or in its dlssg folder +std::wstring files_dir() { + char exe[MAX_PATH]; + const DWORD n = game_module_path(exe, MAX_PATH); + std::string dir(exe, n); + dir = dir.substr(0, dir.find_last_of("\\/")); + for (const char* sub : {"", "\\dlssg"}) { + const std::string d = dir + sub; + if (GetFileAttributesA((d + "\\sl.interposer.dll").c_str()) != INVALID_FILE_ATTRIBUTES) return std::wstring(d.begin(), d.end()); + } + return {}; +} + +} // namespace + +PFN_vkGetInstanceProcAddr early() { + if (!asked() || loaded) return loaded > 0 ? p_gipa : nullptr; + loaded = -1; + static std::wstring dir; // (Streamline keeps the pointers) + dir = files_dir(); + if (dir.empty()) { + logf("vk: dlssg: sl.interposer.dll is not beside the game (nor in its dlssg folder): DLSS frame generation off"); + state_line = "NVIDIA's Streamline files are not beside the game."; + return nullptr; + } + for (const wchar_t* need : {L"sl.common.dll", L"sl.dlss_g.dll", L"sl.pcl.dll", L"sl.reflex.dll", L"nvngx_dlssg.dll"}) + if (GetFileAttributesW((dir + L"\\" + need).c_str()) == INVALID_FILE_ATTRIBUTES) { + logf("vk: dlssg: %ls is missing beside sl.interposer.dll: DLSS frame generation off", need); + state_line = "Some of NVIDIA's Streamline files are missing beside the game."; + return nullptr; + } + mod = LoadLibraryW((dir + L"\\sl.interposer.dll").c_str()); + if (!mod) { + logf("vk: dlssg: sl.interposer.dll did not load (error %lu): DLSS frame generation off", GetLastError()); + state_line = "NVIDIA's Streamline did not load."; + return nullptr; + } + p_init = proc("slInit"); + p_shutdown = proc("slShutdown"); + p_loaded = proc("slIsFeatureLoaded"); + p_supported = proc("slIsFeatureSupported"); + p_tag = proc("slSetTagForFrame"); + p_constants = proc("slSetConstants"); + p_token = proc("slGetNewFrameToken"); + p_feature = proc("slGetFeatureFunction"); + p_gipa = reinterpret_cast(GetProcAddress(mod, "vkGetInstanceProcAddr")); + if (!p_init || !p_tag || !p_constants || !p_token || !p_feature || !p_gipa) { + logf("vk: dlssg: sl.interposer.dll lacks its entry points: DLSS frame generation off"); + state_line = "NVIDIA's Streamline files are of another version."; + return nullptr; + } + static const sl::Feature features[] = {sl::kFeatureDLSS_G, sl::kFeatureReflex, sl::kFeaturePCL}; + static const wchar_t* paths[1]; + paths[0] = dir.c_str(); + sl::Preferences p{}; + p.showConsole = false; + p.logLevel = getenv("RX_VK_DLSSG_LOG") ? sl::LogLevel::eVerbose : sl::LogLevel::eDefault; + p.pathsToPlugins = paths; + p.numPathsToPlugins = 1; + p.pathToLogsAndData = nullptr; // (no log file of its own: its warnings and errors come to ours) + p.logMessageCallback = sl_log; + // (the renderer tracks its own command buffer state; the depth and motion are tagged per 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_dlss.cpp's NGX) + p.engine = sl::EngineType::eCustom; + p.renderAPI = sl::RenderAPI::eVulkan; + const sl::Result r = p_init(p, sl::kSDKVersion); + if (r != sl::Result::eOk) { + logf("vk: dlssg: slInit failed (%d): DLSS frame generation off", (int)r); + state_line = "NVIDIA's Streamline did not start."; + return nullptr; + } + logf("vk: dlssg: Streamline is up (%dx asked for): the Vulkan instance and device are made through it", asked()); + state_line = "Starting."; + loaded = 1; + return p_gipa; +} + +void device_extensions(std::vector& want, const std::vector& all) { + if (loaded <= 0) return; + static const char* const promoted[] = {VK_KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME, VK_KHR_SYNCHRONIZATION_2_EXTENSION_NAME, + VK_KHR_DYNAMIC_RENDERING_EXTENSION_NAME, VK_KHR_CREATE_RENDERPASS_2_EXTENSION_NAME, + VK_KHR_BIND_MEMORY_2_EXTENSION_NAME, VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME, + VK_KHR_COPY_COMMANDS_2_EXTENSION_NAME, VK_KHR_MAINTENANCE_1_EXTENSION_NAME, + VK_KHR_MAINTENANCE_2_EXTENSION_NAME, VK_KHR_MAINTENANCE_3_EXTENSION_NAME}; + for (const char* name : promoted) { + bool listed = false, asked_already = false; + for (const auto& e : all) listed |= !strcmp(e.extensionName, name); + for (const char* w : want) asked_already |= !strcmp(w, name); + if (listed && !asked_already) want.push_back(name); + } +} + +void gave_up(const char* why) { + if (loaded <= 0) return; + loaded = -1, running = false; + logf("vk: dlssg: DLSS frame generation is off in this run: %s", why); + state_line = "Not on this graphics card or driver."; +} + +void device_ready(VkPhysicalDevice physical, VkDevice) { + if (loaded <= 0) return; + bool feature = false; + bool ok = p_loaded && p_loaded(sl::kFeatureDLSS_G, feature) == sl::Result::eOk && feature; + void* f = nullptr; + if (ok && p_feature(sl::kFeatureDLSS_G, "slDLSSGSetOptions", f) == sl::Result::eOk) p_dlssg_options = (PFun_slDLSSGSetOptions*)f; + if (ok && p_feature(sl::kFeatureDLSS_G, "slDLSSGGetState", f) == sl::Result::eOk) p_dlssg_state = (PFun_slDLSSGGetState*)f; + if (p_feature(sl::kFeaturePCL, "slPCLSetMarker", f) == sl::Result::eOk) p_marker = (PFun_slPCLSetMarker*)f; + if (p_feature(sl::kFeatureReflex, "slReflexSetOptions", f) == sl::Result::eOk) p_reflex = (PFun_slReflexSetOptions*)f; + if (p_feature(sl::kFeatureReflex, "slReflexSleep", f) == sl::Result::eOk) p_sleep = (PFun_slReflexSleep*)f; + if (!p_dlssg_options || !p_marker) ok = false; + if (!ok) { + sl::Result why = sl::Result::eErrorNotInitialized; + if (p_supported) { + sl::AdapterInfo adapter{}; + adapter.vkPhysicalDevice = physical; + why = p_supported(sl::kFeatureDLSS_G, adapter); + } + logf("vk: dlssg: frame generation does not run here (the DLSS-G plugin loaded: %d, its options: %d, the latency marker: %d; " + "Streamline's support check: %s (%d)). It needs an RTX 40 or 50 card and a current driver", (int)feature, + p_dlssg_options != nullptr, p_marker != nullptr, sl::getResultAsStr(why), (int)why); + state_line = "Not on this graphics card or driver: it needs an RTX 40 or 50 card."; + return; + } + sl::DLSSGState s{}; + if (p_dlssg_state && p_dlssg_state(sl::ViewportHandle(0), s, nullptr) == sl::Result::eOk) frames_max = s.numFramesToGenerateMax; + if (frames_max.load()) logf("vk: dlssg: DLSS frame generation available, up to %ux here", frames_max.load() + 1); + else logf("vk: dlssg: DLSS frame generation available (how many frames this card generates is not reported yet)"); + state_line = "Runs in the world; pauses in menus and loads."; + running = true; + reflex_apply(); // (no swapchain and no present yet: the mode alone) +} + +bool active() { return running.load(std::memory_order_relaxed); } + +void swapchain_created() { + if (active()) reflex_apply(); +} + +void frame_start() { + if (!active()) return; + const uint32_t n = frame_no.fetch_add(1) + 1; + sl::FrameToken* t = token(n); + // (no hold before the first present: the driver has no present to time the frame by) + if (t && p_sleep && presented) p_sleep(*t); + if ((t = token(n))) { + // (the game's simulation and its recording of the frame cannot be told apart from here: both markers at the start) + p_marker(sl::PCLMarker::eSimulationStart, *t); + p_marker(sl::PCLMarker::eSimulationEnd, *t); + p_marker(sl::PCLMarker::eRenderSubmitStart, *t); + } +} + +void submit_end() { + sl::FrameToken* t = active() ? token(frame_no.load()) : nullptr; + if (t) p_marker(sl::PCLMarker::eRenderSubmitEnd, *t); +} + +void frame(VkCommandBuffer cmd, uint32_t out_width, uint32_t out_height) { + if (!active()) return; + const uint32_t n = frame_no.load(); + sl::FrameToken* t = token(n); + if (!t) return; + const sl::ViewportHandle vp(0); + fsr3_inputs::Frame in; + const bool have = fsr3_inputs::take(in) && in.depth_image && in.motion_image; + int want = have && !menu_is_open(); + if (want == mode_now) mode_pending = 0; + else if (++mode_pending >= kSettle) mode_now = want, mode_pending = 0; + else want = mode_now; // (the state it has until the new one has stayed) + const uint32_t most = frames_max.load(); + int generate = want ? asked() - 1 : 0; + if (generate > (int)(most ? most : 1)) generate = (int)(most ? most : 1); // (one while Streamline has not said) + shown_mult = generate + 1; + + static sl::Constants last; // (the constants of the last frame that had its inputs) + static bool have_last; + if (generate && !have && have_last) { + // (generation is on for this present and the frame has no scene: a menu or a fade inside the settle time. Streamline + // asks for the common constants every frame and for null tags where depth or motion are not valid: the last + // frame's constants again, as a new start) + sl::Constants c = last; + c.reset = sl::Boolean::eTrue; + p_constants(c, *t, vp); + sl::ResourceTag none[2] = {sl::ResourceTag(nullptr, sl::kBufferTypeDepth, sl::ResourceLifecycle::eValidUntilPresent), + sl::ResourceTag(nullptr, sl::kBufferTypeMotionVectors, sl::ResourceLifecycle::eValidUntilPresent)}; + if ((t = token(n))) p_tag(*t, vp, none, 2, (sl::CommandBuffer*)cmd); + } + if (generate && have) { + float clip_to_view[16], prev_to_clip[16]; + if (!inverse(in.projection, clip_to_view) || !inverse(in.clip_to_prev, prev_to_clip)) { + static int told; + if (!told++) logf("vk: dlssg: a frame whose projection has no inverse: no generated frame after it"); + return; + } + sl::Constants c{}; + matrix(c.cameraViewToClip, in.projection); + matrix(c.clipToCameraView, clip_to_view); + matrix(c.clipToPrevClip, in.clip_to_prev); + matrix(c.prevClipToClip, prev_to_clip); + c.jitterOffset = sl::float2(in.jitter[0], in.jitter[1]); + // (the motion is in render pixels: this makes it fractions of the picture) + c.mvecScale = sl::float2(1.0f / (float)in.width, 1.0f / (float)in.height); + c.cameraPinholeOffset = sl::float2(0, 0); + // (the view matrix's rows are the camera's right, up and back axes, its last column the translation: the position is + // -R^T t) + const float* v = in.view; + c.cameraRight = sl::float3(v[0], v[1], v[2]); + c.cameraUp = sl::float3(v[4], v[5], v[6]); + c.cameraFwd = sl::float3(-v[8], -v[9], -v[10]); + c.cameraPos = sl::float3(-(v[0] * v[3] + v[4] * v[7] + v[8] * v[11]), -(v[1] * v[3] + v[5] * v[7] + v[9] * v[11]), + -(v[2] * v[3] + v[6] * v[7] + v[10] * v[11])); + c.cameraNear = in.camera.near_plane; + c.cameraFar = std::isfinite(in.camera.far_plane) && in.camera.far_plane < 1e8f ? in.camera.far_plane : 1e8f; + c.cameraFOV = in.camera.fov_y; + c.cameraAspectRatio = (float)in.width / (float)in.height; + c.depthInverted = sl::Boolean::eTrue; // (FSR's copy of the depth: 1 at the near plane) + c.cameraMotionIncluded = sl::Boolean::eTrue; + c.motionVectors3D = sl::Boolean::eFalse; + c.reset = in.reset ? sl::Boolean::eTrue : sl::Boolean::eFalse; + p_constants(c, *t, vp); + last = c, have_last = true; + sl::Resource rd(sl::ResourceType::eTex2d, (void*)in.depth_image, nullptr, (void*)in.depth, (uint32_t)VK_IMAGE_LAYOUT_GENERAL); + rd.width = in.width, rd.height = in.height, rd.nativeFormat = (uint32_t)VK_FORMAT_R32_SFLOAT; + rd.mipLevels = 1, rd.arrayLayers = 1, rd.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT; + sl::Resource rm(sl::ResourceType::eTex2d, (void*)in.motion_image, nullptr, (void*)in.motion, (uint32_t)VK_IMAGE_LAYOUT_GENERAL); + rm.width = in.width, rm.height = in.height, rm.nativeFormat = (uint32_t)VK_FORMAT_R16G16_SFLOAT; + rm.mipLevels = 1, rm.arrayLayers = 1, rm.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT; + sl::Extent ext{0, 0, in.width, in.height}; + sl::ResourceTag tags[2] = {sl::ResourceTag(&rd, sl::kBufferTypeDepth, sl::ResourceLifecycle::eValidUntilPresent, &ext), + sl::ResourceTag(&rm, sl::kBufferTypeMotionVectors, sl::ResourceLifecycle::eValidUntilPresent, &ext)}; + if ((t = token(n))) p_tag(*t, vp, tags, 2, (sl::CommandBuffer*)cmd); + static bool told; + if (!told) { + told = true; + logf("vk: dlssg: the first frame's inputs: depth and motion %ux%u for a %ux%u picture, near %.4f far %.6g fov %.2f deg, " + "jitter %+.3f %+.3f px", in.width, in.height, out_width, out_height, c.cameraNear, c.cameraFar, + c.cameraFOV * 57.2957795f, in.jitter[0], in.jitter[1]); + } + } + if (generate != frames_set) { + sl::DLSSGOptions o{}; + o.mode = generate ? sl::DLSSGMode::eOn : sl::DLSSGMode::eOff; + o.numFramesToGenerate = generate ? (uint32_t)generate : 1; + // (off for every menu and load, and on again after: with this flag off only suspends. Without it the generator's + // resources go and come back inside the present with the queue held, a frame of 100 to 150 ms each time) + o.flags = sl::DLSSGFlags::eRetainResourcesWhenOff; + const sl::Result r = p_dlssg_options(vp, o); + if (generate) logf("vk: dlssg: frame generation on, %dx (%d made for each of the game's) (%d), picture %ux%u", generate + 1, + generate, (int)r, out_width, out_height); + else logf("vk: dlssg: frame generation paused (%d)", (int)r); + frames_set = generate; + } +} + +void present_marker(bool after) { + sl::FrameToken* t = active() ? token(frame_no.load()) : nullptr; + if (!t) return; + p_marker(after ? sl::PCLMarker::ePresentEnd : sl::PCLMarker::ePresentStart, *t); + if (!after) return; + presented++; + sl::DLSSGState s{}; + if (!p_dlssg_state || p_dlssg_state(sl::ViewportHandle(0), s, nullptr) != sl::Result::eOk) return; + shown += s.numFramesActuallyPresented; + { // (the moment the frames a present shows change, the first 30 times: 2 with one made frame, 1 while Streamline + // holds back, as it does for a window that is not the one in front) + static uint32_t last_shown; + static int told; + if (s.numFramesActuallyPresented != last_shown && told < 30) { + told++, last_shown = s.numFramesActuallyPresented; + logf("vk: dlssg: %u frame(s) shown for each present from present %llu on (status %u)", last_shown, + (unsigned long long)presented, (unsigned)s.status); + } + } + if (s.numFramesToGenerateMax && s.numFramesToGenerateMax != frames_max.load()) { // (a 0 now and then is no news) + logf("vk: dlssg: up to %ux on this card and driver", s.numFramesToGenerateMax + 1); + frames_max = s.numFramesToGenerateMax; + } + if (presented == 600 || presented % 3600 == 0) + logf("vk: dlssg: %llu presents, %llu frames shown, %u shown for the last one, status %u", (unsigned long long)presented, + (unsigned long long)shown, s.numFramesActuallyPresented, (unsigned)s.status); +} + +int multiplier() { return active() ? shown_mult.load(std::memory_order_relaxed) : 1; } + +const char* status() { return state_line; } + +void shutdown() { + if (loaded <= 0) return; + // (no present may be inside Streamline while it goes, and none may start after: the presents' lock is taken and kept, + // as the process exits right after this) + dev.present_mu.lock(); + running = false, loaded = -1; + if (p_shutdown) p_shutdown(); + logf("vk: dlssg: Streamline shut down"); +} + +#endif + +} // namespace mkwvk::dlssg diff --git a/gpu/vk/vk_dlssg.h b/gpu/vk/vk_dlssg.h new file mode 100644 index 0000000..0cb3dc9 --- /dev/null +++ b/gpu/vk/vk_dlssg.h @@ -0,0 +1,57 @@ +// (Triforce) NVIDIA DLSS Frame Generation through NVIDIA's Streamline (vk_dlssg.cpp), after NX2navi's runtime/vk_dlssg.c +// and runtime/dlssg/nx2_sl.cpp. In a build without HOOKSHOT_STREAMLINE_SDK every function here answers "off". +#pragma once + +#include +#include + +#include "vk_internal.h" + +namespace mkwvk::dlssg { + +/// The frames shown for each of the game's as asked at the start, 2 to 4; 0: off. The Insert menu's DLSS frame +/// generation row as the options file has it (dlss_frame_generation=), RX_VK_DLSSG=<0|2|3|4> over it. Read once: the +/// Vulkan instance and device are made through Streamline, so a change takes a restart. +int asked(); + +/// Before the Vulkan instance is made: Streamline loaded and started from the game's folder. Its vkGetInstanceProcAddr, +/// through which the instance, the device and every function of both are then fetched; nullptr when it is not asked +/// for, not built in, its files are missing or it did not start (the loader's own is used, as without it). +PFN_vkGetInstanceProcAddr early(); + +/// The extensions Vulkan 1.3 made core, asked for by name as well where the device lists them: Streamline's plugins +/// look their functions up under the extensions' names, which resolve only with the extension enabled. +void device_extensions(std::vector& want, const std::vector& all); + +/// The device was not made through Streamline (another vendor's card, or Streamline refused it): off for this run. +void gave_up(const char* why); + +/// The device is made (through Streamline): whether frame generation runs on this card and driver, said in the log. +void device_ready(VkPhysicalDevice physical, VkDevice device); + +/// Frame generation runs in this session (the swapchain's presents go through Streamline). The game's frames then +/// reach the display from the replay's thread alone, and Reflex is Streamline's. +bool active(); + +/// A swapchain was made: Reflex's mode is the swapchain's and is set again. +void swapchain_created(); + +/// Where a frame of the game starts (after the present of the one before): Reflex's sleep, then the frame's first +/// latency markers. Frames are numbered from here. +void frame_start(); +/// The frame's submissions went out. +void submit_end(); +/// Before the present's command buffer ends: this frame's camera, depth and motion for Streamline, and generation on +/// or off (off in the menu, and while a frame has no scene). cmd: the present's command buffer. +void frame(VkCommandBuffer cmd, uint32_t out_width, uint32_t out_height); +/// Around vkQueuePresentKHR. +void present_marker(bool after); + +/// The frames the display gets for each of the game's right now: 1 while generation is off or paused. +int multiplier(); +/// One line for the menu about what runs, or why it does not. +const char* status(); + +void shutdown(); + +} // namespace mkwvk::dlssg diff --git a/gpu/vk/vk_draw.cpp b/gpu/vk/vk_draw.cpp index f95421d..c82152f 100644 --- a/gpu/vk/vk_draw.cpp +++ b/gpu/vk/vk_draw.cpp @@ -728,12 +728,34 @@ void pcache_init() { std::string dir(exe, n); dir = dir.substr(0, dir.find_last_of("\\/") + 1); pcache_path = dir + "mkw_vk_pipelines.bin"; + // (Triforce, 11-14) The file is kept for one set of translated shaders: the ones named beside it (.for, the programs' + // stamp). The driver's data only ever grows: every update that changes the translator (and a Setup build after a lab one: + // another C++ library, other SPIR-V) added its ~650 MB of pipelines to the pipelines of shaders that no longer exist, and + // the whole file is read into memory at every start and held by the driver. A player's file after six updates: 4.1 GB, on a + // 32 GB PC whose log says "RAM short" all session (1.2-1.5 GB available, private 31.9-33.8 GB); the lab's: 3.9 GB, about + // that much more RAM in use at the title than with a fresh file (647 MB). A file for other shaders, or with no name beside + // it, is not read: the cache starts empty, the driver's own cache serves the start's pipelines, and the first save writes + // the new file over the old one. RX_VK_PCACHE_KEEP_OLD=1: read whatever is there, as before + char pcache_for[20]; + snprintf(pcache_for, sizeof pcache_for, "%016llx", (unsigned long long)program_cache_stamp()); + bool pcache_same = getenv("RX_VK_PCACHE_KEEP_OLD") != nullptr; + if (!pcache_same) { + if (FILE* f = fopen((pcache_path + ".for").c_str(), "rb")) { + char had[20]{}; + pcache_same = fread(had, 1, 16, f) == 16 && !memcmp(had, pcache_for, 16); + fclose(f); + } + } std::vector data; if (FILE* f = fopen(pcache_path.c_str(), "rb")) { _fseeki64(f, 0, SEEK_END); const int64_t sz = _ftelli64(f); _fseeki64(f, 0, SEEK_SET); - if (sz > 32) { + if (!pcache_same) { + if (sz > 32) + logf("vk: pipeline cache %s (%lld MB) is for other shaders than these, or older than this rule: not read; a new " + "one is made (RX_VK_PCACHE_KEEP_OLD=1: read)", pcache_path.c_str(), (long long)(sz >> 20)); + } else if (sz > 32) { data.resize((size_t)sz); if (fread(data.data(), 1, data.size(), f) != data.size()) data.clear(); } @@ -3122,6 +3144,10 @@ void build_descriptors(const NvnState& s, const Program& p) { : VK_NULL_HANDLE; upload_defer_ok = false; if (pre_retarget_on && view && tex == pre_retarget.from) view = pre_retarget.to_view; // (the engine's picture) + // (Triforce, texture detail: a texture with levels left out, at a binding the stage reads by texel + // position or asks the size of, is made whole: vk_texture.cpp texture_want_whole) + if (tex && tex->detail_skip && !tex->whole_asked && shader_reads_exact(st.module, w.dstBinding)) + texture_want_whole(tex); if (dump_on()) dump_input('t', (uint32_t)(dw().imgs.size() - 0), handle, view ? tex : nullptr); if (view && tex && trace_tex_wanted(tex)) trace_tex_hit = tex; if (log_img_wanted(p.key)) { @@ -8149,6 +8175,32 @@ void predict_plan_ensure() { std::chrono::duration(std::chrono::steady_clock::now() - t0).count()); } +// (Triforce, 11-14) The builder stays out of the game in play. It ran on its 1 to 3 idle-priority threads the whole session, +// and idle priority did not keep it out of the frame: each build is tens of ms inside the driver's compiler, which the replay's +// own calls into the driver wait behind, and the PC's memory fills with what the driver keeps of them. A player's session of +// v0.3.6 (RTX 4070 SUPER, 13700K, a ~2,850-draw scene): 55-64 fps before the builder began, 41-47 with it running (8 s of +// compiling per 2.6 s of play), the GPU's time a frame 12-15 -> 16-17 ms, the game's private memory 31.9 -> 33.8 GB of 32. +// On a driver that takes seconds a pipeline (AMD) the builder would never have ended. Now it builds while the frame shown is +// a light one (the title, the menus, the pause and map screens, a loading screen: under RX_VK_PREDICT_BG_PLAY_DRAWS draws, +// 600) or none is shown, and rests while the game is in play. RX_VK_PREDICT_BG_PLAY=1: one thread goes on in play, resting +// three times as long as each program took (a quarter of a thread); =2: all of them, as v0.3.6 +std::atomic frame_draws_last{0}; +std::atomic frame_shown_ms{0}; +namespace { +int64_t bg_now_ms() { + return std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count(); +} +bool bg_game_in_play() { + static const int play_draws = getenv("RX_VK_PREDICT_BG_PLAY_DRAWS") ? atoi(getenv("RX_VK_PREDICT_BG_PLAY_DRAWS")) : 600; + return frame_draws_last.load(std::memory_order_relaxed) >= play_draws && + bg_now_ms() - frame_shown_ms.load(std::memory_order_relaxed) < 500; +} +} // namespace +void frame_shown_note(int draws) { + frame_draws_last.store(draws, std::memory_order_relaxed); + frame_shown_ms.store(bg_now_ms(), std::memory_order_relaxed); +} + /// (Triforce) The predicted pipelines predict_run did not get to, built behind the game: see above void predict_background() { static std::atomic started{false}; @@ -8183,15 +8235,28 @@ void predict_background() { const unsigned cpus = std::max(1u, std::thread::hardware_concurrency()); static const unsigned nthreads = getenv("RX_VK_PREDICT_BG_THREADS") ? (unsigned)std::clamp(atoi(getenv("RX_VK_PREDICT_BG_THREADS")), 1, 32) : std::clamp(cpus / 6, 1u, 3u); + static const int in_play = getenv("RX_VK_PREDICT_BG_PLAY") ? std::clamp(atoi(getenv("RX_VK_PREDICT_BG_PLAY")), 0, 2) : 0; logf("vk: predicted pipelines: %zu programs' are still to build for this driver (%zu were built by earlier starts): built " - "behind the game on %u idle thread(s) (RX_VK_PREDICT_BG=0: not)", keys.size(), had.size(), nthreads); + "behind the game on %u idle thread(s) while the frame shown is a light one (the title, the menus, a loading screen)%s " + "(RX_VK_PREDICT_BG=0: not)", keys.size(), had.size(), nthreads, + in_play == 2 ? " and in play too (RX_VK_PREDICT_BG_PLAY=2)" : in_play == 1 ? ", and on a quarter of one in play" : ""); std::atomic next{0}, made{0}, programs_done{0}; - const auto worker = [&] { + std::atomic rested_ms{0}; + const auto worker = [&](unsigned id) { ub_thread_off(); // (a background thread: see ub_thread_off) SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_IDLE); driver_cache_only = true; + int64_t rest_until = 0; // (in play, RX_VK_PREDICT_BG_PLAY=1: this thread's rest after a program) for (;;) { if (prepare_stopped()) return; + // (the game in play: see above. Looked at before each program; one already inside the driver finishes) + if (in_play != 2) { + const int64_t r0 = bg_now_ms(); + while (!prepare_stopped() && bg_game_in_play() && !(in_play == 1 && id == 0 && bg_now_ms() >= rest_until)) Sleep(50); + if (prepare_stopped()) return; + if (id == 0) rested_ms += bg_now_ms() - r0; + } + const int64_t b0 = bg_now_ms(); const size_t i = next++; if (i >= keys.size()) return; const auto& list = by_program[keys[i]]; @@ -8203,17 +8268,20 @@ void predict_background() { vkDestroyPipeline(dev.device, pl, nullptr), made++; }); predicted_done(pstamp, keys[i]); + if (in_play == 1 && bg_game_in_play()) rest_until = bg_now_ms() + 3 * (bg_now_ms() - b0); if (++programs_done % 2000 == 0) - logf("vk: predicted pipelines: %zu of %zu programs built behind the game so far", programs_done.load(), keys.size()); + logf("vk: predicted pipelines: %zu of %zu programs built behind the game so far (rested %.0f s for the game in play)", + programs_done.load(), keys.size(), rested_ms.load() / 1000.0); } }; std::vector threads; - for (unsigned i = 0; i < nthreads; i++) threads.emplace_back(worker); + for (unsigned i = 0; i < nthreads; i++) threads.emplace_back(worker, i); for (std::thread& t : threads) t.join(); const bool all = programs_done.load() >= keys.size() && !prepare_stopped(); if (all) predicted_done(pstamp, kPredictedAll); - logf("vk: predicted pipelines: %zu built behind the game for %zu programs in %.0f s%s", made.load(), programs_done.load(), - std::chrono::duration(std::chrono::steady_clock::now() - t0).count(), + logf("vk: predicted pipelines: %zu built behind the game for %zu programs in %.0f s, %.0f s of it rested for the game in " + "play%s", made.load(), programs_done.load(), + std::chrono::duration(std::chrono::steady_clock::now() - t0).count(), rested_ms.load() / 1000.0, all ? ": every predicted program is built for this driver now" : " (stopped: the rest at the next start)"); }).detach(); } @@ -8786,12 +8854,18 @@ void pipeline_cache_save(bool force) { while (busy.load()) Sleep(1); // (the one under way, then this one) busy = true; } - auto save = [] { + auto save = [force] { struct Done { ~Done() { busy = false; } } done; try { size_t n = 0; // (the size last read or written: nothing new, not fetched nor written again; see pcache_saved_size) if (vkGetPipelineCacheData(dev.device, pcache, &n, nullptr) == VK_SUCCESS && n && n != pcache_saved_size) { + // (Triforce, 11-14) In play the file is written again only once the cache has grown by RX_VK_PCACHE_PLAY_MB (64): + // every 32 new pipelines had the whole cache fetched into memory and the whole file written (650 MB when fresh, + // 4 GB at its worst), a commit spike and seconds of disk load in the middle of play, for a few hundred KB new. + // What play builds is in the driver's own cache at once, and the save at the game's close takes all of it + static const size_t play_grow = (size_t)(getenv("RX_VK_PCACHE_PLAY_MB") ? std::max(0ll, atoll(getenv("RX_VK_PCACHE_PLAY_MB"))) : 64) << 20; + if (!force && pcache_saved_size > (1u << 20) && n < pcache_saved_size + play_grow) return; std::vector data(n); if (vkGetPipelineCacheData(dev.device, pcache, &n, data.data()) == VK_SUCCESS) { const std::string tmp = pcache_path + ".tmp"; @@ -8801,6 +8875,13 @@ void pipeline_cache_save(bool force) { // (only a file in place counts as saved: a failed write is tried again at the next save) if (ok && MoveFileExA(tmp.c_str(), pcache_path.c_str(), MOVEFILE_REPLACE_EXISTING)) { pcache_saved_size = n; + // (the shaders it is for, beside it: pcache_init reads the file only for these. Not when the old + // file was read whatever it was for, RX_VK_PCACHE_KEEP_OLD: that would name the old data as these') + if (FILE* s = getenv("RX_VK_PCACHE_KEEP_OLD") ? nullptr : fopen((pcache_path + ".for").c_str(), "wb")) { + char want[20]; + snprintf(want, sizeof want, "%016llx", (unsigned long long)program_cache_stamp()); + fwrite(want, 1, 16, s), fclose(s); + } logf("vk: pipeline cache saved (%zu KB)", n >> 10); } } diff --git a/gpu/vk/vk_framegen.cpp b/gpu/vk/vk_framegen.cpp index e0be811..a7a0c4d 100644 --- a/gpu/vk/vk_framegen.cpp +++ b/gpu/vk/vk_framegen.cpp @@ -20,6 +20,7 @@ #include #include "vk_framegen.h" +#include "vk_dlssg.h" #include "vk_fsr3_inputs.h" // (AMD FSR 3.1 frame interpolation's depth and motion) #include "vk_fsr3fi.h" #include "vk_menu.h" @@ -486,7 +487,8 @@ bool framegen_wanted(int menu_value, int allow) { bool framegen_on() { static const int allow = !getenv("RX_VK_FRAMEGEN") || atoi(getenv("RX_VK_FRAMEGEN")) != 0; - return framegen_wanted(opts.framegen, allow) && !dev.offscreen && !fg.failed; + // (Triforce: not beside NVIDIA DLSS frame generation, whose frames go in at the present itself: vk_dlssg.cpp) + return framegen_wanted(opts.framegen, allow) && !dev.offscreen && !fg.failed && !dlssg::active(); } // Ultrawide's menus (the Insert menu's Ultrawide menus): which of nx2/oot_wide.py's keep-list screens keep the whole picture, told diff --git a/gpu/vk/vk_fsr3_inputs.cpp b/gpu/vk/vk_fsr3_inputs.cpp index eec2291..ffc4fdd 100644 --- a/gpu/vk/vk_fsr3_inputs.cpp +++ b/gpu/vk/vk_fsr3_inputs.cpp @@ -7,6 +7,7 @@ // projection (nx2/oot_jitter.py: P[0][2] and P[1][2]) is read back from it, given to FSR in render pixels and taken off the // motion, with dlss_math.h's functions: the same jitter, read the same way, as DLSS's. #include "vk_fsr3_inputs.h" +#include "vk_dlssg.h" #include #include @@ -46,10 +47,15 @@ struct Image { struct Camera { float current[16]{}; // (its view-projection, jittered) float projection[16]{}; // (its projection, jittered: the near plane and the field of view come from it) + float view[12]{}; // (its view matrix's three rows, at the buffer's start: NVIDIA DLSS frame generation's camera) float jitter[2]{}; // (the projection's offset of the picture's centre, in clip space: nx2/oot_jitter.py) uint32_t width = 0, height = 0; Texture* depth = nullptr; - uint64_t depth_gen = 0; // (texture_destroy_generation when taken: a depth texture destroyed since is not used) + // (the depth texture's creation number and texture_destroyed_count when taken: destroyed since, it is not used. It was + // texture_destroy_generation, which any texture's end changes: with the video memory full the cache lets textures go + // in most frames, and each of those frames was left to the game's own upscale) + uint32_t depth_id = 0; + uint64_t noted = 0; uint64_t frame = 0; // (texture_frame_no when taken) bool valid = false; }; @@ -207,7 +213,8 @@ VkImageView depth_view(Texture* t) { } // namespace -bool wanted() { return fsr3_wanted() || fsr3fi::frames_wanted() != 0; } +// (Triforce: or NVIDIA DLSS frame generation, which takes the same depth and motion: vk_dlssg.cpp) +bool wanted() { return fsr3_wanted() || fsr3fi::frames_wanted() != 0 || dlssg::active(); } void note_scene_draw(const Texture* color0, uint32_t width, uint32_t height, Texture* depth, uint64_t camera, uint64_t camera_size) { @@ -227,9 +234,10 @@ void note_scene_draw(const Texture* color0, uint32_t width, uint32_t height, Tex Camera& k = s.cam; std::memcpy(k.current, c + 0x30 / 4, sizeof k.current); std::memcpy(k.projection, c + 0x70 / 4, sizeof k.projection); + std::memcpy(k.view, c, sizeof k.view); // (the offset sead's projection gave the frustum, in its third column: 2 * offset, which moves the picture by -that) k.jitter[0] = -c[0x78 / 4], k.jitter[1] = -c[0x88 / 4]; - k.width = width, k.height = height, k.depth = depth, k.depth_gen = texture_destroy_generation(), k.frame = now; + k.width = width, k.height = height, k.depth = depth, k.depth_id = depth->id, k.noted = texture_destroyed_count(), k.frame = now; k.valid = true; } @@ -249,7 +257,7 @@ bool prepare(VkCommandBuffer cmd, Texture* in, VkImageView in_view, Frame& out, if (!in || !in_view) return fail("no input view"); const uint64_t now = texture_frame_no(); if (!cam.valid || now - cam.frame > 1) return fail("no scene camera this frame"); - if (cam.depth_gen != texture_destroy_generation()) return fail("the scene depth may be gone"); + if (texture_destroyed_since(cam.depth_id, cam.noted)) return fail("the scene depth is gone"); const uint32_t rw = in->width, rh = in->height; if (rw != cam.width || rh != cam.height) return fail("the input is not the scene's size"); if (cam.depth->width != rw || cam.depth->height != rh || !(cam.depth->usage & VK_IMAGE_USAGE_SAMPLED_BIT) || @@ -316,6 +324,16 @@ bool prepare(VkCommandBuffer cmd, Texture* in, VkImageView in_view, Frame& out, dlss_math::jitter_to_pixels(cam.jitter, rw, rh, f.jitter); // (in render pixels, x right and y down: FSR's, as DLSS's) f.camera = camera; f.reset = reset; + { // (Triforce, NVIDIA DLSS frame generation's: the images, and the matrices with no jitter in them on either side) + f.depth_image = s.depth.image, f.motion_image = s.motion.image; + std::memcpy(f.projection, projection, sizeof f.projection); + std::memcpy(f.view, cam.view, sizeof f.view); + float current[16]; + std::memcpy(current, cam.current, sizeof current); + dlss_math::unjitter(current, cam.jitter); + if (!motion || !dlss_math::reprojection(current, previous, f.clip_to_prev)) + for (int i = 0; i < 16; i++) f.clip_to_prev[i] = i % 5 == 0 ? 1.0f : 0.0f; + } f.frame_ms = s.last_time.time_since_epoch().count() && !reset ? (float)std::min(250.0, std::chrono::duration(t - s.last_time).count()) : 16.667f; diff --git a/gpu/vk/vk_fsr3_inputs.h b/gpu/vk/vk_fsr3_inputs.h index f0d9be7..78025d3 100644 --- a/gpu/vk/vk_fsr3_inputs.h +++ b/gpu/vk/vk_fsr3_inputs.h @@ -36,6 +36,12 @@ struct Frame { bool reset = false; ///< no history: the first frame, a new size, or a gap of more than two frames float frame_ms = 16.667f; ///< since the frame before's uint64_t frame = 0; ///< texture_frame_no() when prepared + // (Triforce, for NVIDIA DLSS frame generation, vk_dlssg.cpp: the depth's and the motion's images themselves, and the + // camera's matrices as the game has them, row major, clip = M * position, without the jitter) + VkImage depth_image = VK_NULL_HANDLE, motion_image = VK_NULL_HANDLE; + float projection[16]{}; ///< view space to clip space + float clip_to_prev[16]{}; ///< this frame's clip space to the frame before's; the identity on a reset + float view[12]{}; ///< the view matrix's three rows: the camera's axes and its translation }; /// FSR 3.1 is asked for at all: its upscaler (the Upscaler row) or its frame interpolation (the Frame generation row). The diff --git a/gpu/vk/vk_fsr3fi.cpp b/gpu/vk/vk_fsr3fi.cpp index 8c20a4a..cd9d8a4 100644 --- a/gpu/vk/vk_fsr3fi.cpp +++ b/gpu/vk/vk_fsr3fi.cpp @@ -18,6 +18,7 @@ #include +#include "vk_dlssg.h" #include "vk_fsr3fi.h" #include "vk_menu.h" // (the Frame generation row) #include "vk_gprof.h" // (RX_VK_PASS_TIMING: a span per pass, so the report names them) @@ -675,7 +676,7 @@ bool capable() { uint32_t frames_wanted() { // (the row first: a device is not asked anything, and nothing is logged, while frame generation is not at 2x) - return asked() && capable() ? 1u : 0u; + return asked() && !dlssg::active() && capable() ? 1u : 0u; // (Triforce: not beside NVIDIA DLSS frame generation) } bool prepare(uint32_t width, uint32_t height, uint32_t render_width, uint32_t render_height, diff --git a/gpu/vk/vk_internal.h b/gpu/vk/vk_internal.h index 39e5a82..2799d2d 100644 --- a/gpu/vk/vk_internal.h +++ b/gpu/vk/vk_internal.h @@ -180,7 +180,7 @@ void memory_stats(int64_t* mirrors, uint64_t* mirror_mb, int64_t* sync_bytes, in void memory_collect_written(); /// RX_VK_CAPTURE (on with the mirrors; =0 off): at a submit, on the submitting thread, the pages the CPU wrote since the /// last one, and a copy of them as they are then; the replay stamps them and fills the mirrors from that copy -void memory_capture_submit(); +bool memory_capture_submit(); // (true: a capture was queued for the replay to take) /// The replay of the next submit: its capture (false: none, the pages are collected now as before) bool memory_take_capture(uint64_t value, uint64_t completed); bool memory_capture_now(uint64_t value, uint64_t completed); // (hookshot: made and taken at once) @@ -319,6 +319,7 @@ bool prepare_is_update(); void prepare_background(); void state_list_run(); // (an update: the state list's pipelines noted before precompile_run) void predict_background(); // (Triforce: the predicted pipelines predict_run did not get to, built behind the game) +void frame_shown_note(int draws); // (Triforce, 11-14: a frame was shown with this many draws; the builder rests in play) void predict_plan_ensure(); // (Triforce: its plan, made before the precompile when an older build prepared this install) void predict_run(void (*progress)(size_t done, size_t total)); // (after a prepare_run that prepared: the notes' predictions) /// (hookshot) The window was closed while the shaders were prepared: prepare_run and precompile_run take nothing more and return, diff --git a/gpu/vk/vk_main.cpp b/gpu/vk/vk_main.cpp index 03a4d39..9debf89 100644 --- a/gpu/vk/vk_main.cpp +++ b/gpu/vk/vk_main.cpp @@ -1,6 +1,7 @@ // The Vulkan back end's start: vulkan-1.dll, the instance, the device (Vulkan 1.3 and the extensions the NVN replay // wants where the GPU has them: mesh shaders, host memory import, graphics pipeline libraries), the window's // swapchain, and the rx_* table (vk_api.h). The NVN replay itself is vk_replay.cpp. +#include "vk_dlssg.h" #include "vk_paths.h" #include "vk_loader.h" @@ -37,6 +38,9 @@ VKX_DEVICE3(VKX_DEFINE) #undef VKX_DEFINE static PFN_vkGetInstanceProcAddr get_instance_proc; +// (Triforce, NVIDIA DLSS frame generation, vk_dlssg.cpp: while Streamline is up, get_instance_proc is its interposer's, +// so the instance, the device and every function of both are fetched through it; this is the loader's own) +static PFN_vkGetInstanceProcAddr loader_instance_proc; bool load_global() { #ifdef _WIN32 // (hookshot: the loader from System32 only, not from the current folder or PATH) @@ -47,6 +51,8 @@ bool load_global() { if (!m) return false; get_instance_proc = (PFN_vkGetInstanceProcAddr)(void*)GetProcAddress(m, "vkGetInstanceProcAddr"); if (!get_instance_proc) return false; + loader_instance_proc = get_instance_proc; + if (const PFN_vkGetInstanceProcAddr interposer = mkwvk::dlssg::early()) get_instance_proc = interposer; #define VKX_LOAD(f) f = (PFN_##f)get_instance_proc(nullptr, #f); VKX_GLOBAL(VKX_LOAD) #undef VKX_LOAD @@ -58,6 +64,12 @@ void load_instance(VkInstance instance) { VKX_INSTANCE(VKX_LOAD) #undef VKX_LOAD } +/// (Triforce) Streamline is given up for this run before the device is made: the instance's functions from the loader again +void use_loader(VkInstance instance) { + if (!loader_instance_proc || get_instance_proc == loader_instance_proc) return; + get_instance_proc = loader_instance_proc; + load_instance(instance); +} void load_device(VkDevice device) { #define VKX_LOAD(f) f = (PFN_##f)vkGetDeviceProcAddr(device, #f); VKX_DEVICE(VKX_LOAD) @@ -260,7 +272,14 @@ bool create_device() { dlss_device_extensions(want, exts); // (hookshot: NVIDIA DLSS's, vk_dlss.cpp) bool fsr_descriptor_buffer = false; // (hookshot, linux: only with a Vulkan FSR 4 library beside the game, vk_fsr4.cpp) if (fsr4_wants_descriptor_buffer()) optional(VK_EXT_DESCRIPTOR_BUFFER_EXTENSION_NAME, fsr_descriptor_buffer); - reflex_device_extensions(want, exts); // (hookshot: NVIDIA Reflex's, vk_reflex.cpp) + // (Triforce, NVIDIA DLSS frame generation: Streamline's device asks for Reflex's extensions itself, and the renderer's + // own Reflex stays off in such a run; another vendor's card is made by the loader, without it) + if (dlssg::early() && dev.props.vendorID != 0x10de) { + dlssg::gave_up("the graphics card in use is not an NVIDIA card"); + vkx::use_loader(dev.instance); + } + if (dlssg::early()) dlssg::device_extensions(want, exts); + else reflex_device_extensions(want, exts); // (hookshot: NVIDIA Reflex's, vk_reflex.cpp) antilag_device_extensions(want, exts); // (hookshot: AMD Anti-Lag 2's, vk_antilag.cpp) // the queue: one family for graphics, compute and transfer @@ -470,8 +489,16 @@ bool create_device() { ci.pQueueCreateInfos = qs; ci.enabledExtensionCount = (uint32_t)want.size(); ci.ppEnabledExtensionNames = want.data(); - if (vkCreateDevice(dev.physical, &ci, nullptr, &dev.device) != VK_SUCCESS) { - logf("vk: vkCreateDevice failed"); + VkResult made = vkCreateDevice(dev.physical, &ci, nullptr, &dev.device); + if (made != VK_SUCCESS && dlssg::early()) { // (Streamline's refused it: the loader's, without frame generation) + char why[96]; + snprintf(why, sizeof why, "Streamline could not make the device (VkResult %d)", (int)made); + dlssg::gave_up(why); + vkx::use_loader(dev.instance); + made = vkCreateDevice(dev.physical, &ci, nullptr, &dev.device); + } + if (made != VK_SUCCESS) { + logf("vk: vkCreateDevice failed (VkResult %d)", (int)made); return false; } @@ -481,6 +508,10 @@ bool create_device() { dev.present_queue = VK_NULL_HANDLE; dev.present_family = dev.queue_family; if (compute_family != UINT32_MAX) vkGetDeviceQueue(dev.device, compute_family, 0, &dev.present_queue), dev.present_family = compute_family; + dlssg::device_ready(dev.physical, dev.device); + // (with DLSS frame generation the presents stay on the replay's queue: the generated frames are Streamline's work + // inside the present, on that queue) + if (dlssg::active()) dev.present_queue = VK_NULL_HANDLE, dev.present_family = dev.queue_family; if (dev.buffer_marker) buffer_markers_init(); coopmat_init(); reflex_device_created(); @@ -700,6 +731,7 @@ bool create_swapchain(uint32_t width, uint32_t height) { mode == VK_PRESENT_MODE_MAILBOX_KHR ? "mailbox" : mode == VK_PRESENT_MODE_IMMEDIATE_KHR ? "immediate" : "fifo"); swap_views_make(ci.imageUsage, mutable_view ? view_formats[1] : fmt.format); reflex_swapchain_created(sc); // (NVIDIA Reflex: its sleep mode on the new swapchain) + dlssg::swapchain_created(); // (Triforce, DLSS frame generation: Streamline's Reflex mode is the swapchain's too) return true; } diff --git a/gpu/vk/vk_memory.cpp b/gpu/vk/vk_memory.cpp index 0c90837..e7353e9 100644 --- a/gpu/vk/vk_memory.cpp +++ b/gpu/vk/vk_memory.cpp @@ -2004,11 +2004,12 @@ void copy_pairs(const CopyPair* pairs, int64_t n) { std::unique_ptr capture_make(); void shadow_free(Pool& p); void shadow_forget(Pool& p, uint64_t at, uint64_t len); // (the write-back's diff, below: a range its shadow no longer knows) -void memory_capture_submit() { +bool memory_capture_submit() { std::unique_ptr c = capture_make(); - if (!c) return; + if (!c) return false; std::lock_guard lk(cap_mu); captures.push_back(std::move(c)); + return true; // (one is queued: the replay takes it at this submit's place, memory_take_capture) } /// (the pages the CPU wrote since the last capture, copied now: a submit's capture, or memory_capture_now's) std::unique_ptr capture_make() { diff --git a/gpu/vk/vk_menu.cpp b/gpu/vk/vk_menu.cpp index 173c4e1..a16ad23 100644 --- a/gpu/vk/vk_menu.cpp +++ b/gpu/vk/vk_menu.cpp @@ -33,6 +33,7 @@ #include #include +#include "vk_dlssg.h" #include "vk_paths.h" #include "vk_menu.h" #include "vk_menu_hit.h" @@ -112,6 +113,14 @@ const char* tx_framegen(int v) { return t[v]; } const char* tx_onoff(int v) { return v ? "On" : "Off"; } +const char* tx_dlssg(int v) { return v == 1 ? "2x" : v == 2 ? "3x" : v == 3 ? "4x" : "Off"; } +/// (what runs in this session, or why not: vk_dlssg.cpp. Variant 0 stands for the longest line, for the menu's height) +const char* hn_dlssg(int v, int variant) { + if (variant > 0) return nullptr; + if (variant == 0) return "Not on this graphics card or driver: it needs an RTX 40 or 50 card."; + if (!v) return "Off."; + return mkwvk::dlssg::asked() ? mkwvk::dlssg::status() : "Starts with the next start of the game."; +} const char* tx_menu_shortcut(int v) { static const char* const names[] = {"Insert or F1", "F1 only"}; // (F1 is never turned off: the menu is always one key away) return names[v ? 1 : 0]; @@ -124,7 +133,8 @@ const char* tx_nits(int v) { } const char* tx_fps_cap(int v) { static const char* const t[] = {"60 (the console's)", "90", "120", "144", "165", "240"}; - return t[v < 0 ? 0 : v > 5 ? 5 : v]; + if (v < 0) return v == -1 ? "45" : v == -2 ? "40" : "30"; // (Triforce: the limits under 60, host/nn_vi.c fps_max) + return t[v > 5 ? 5 : v]; } // (hookshot) Motion controls: the choice, and whether a connected controller has a gyroscope (host/pad_sdl.c). Either can be // chosen whatever is connected: a pad plugged in later is read when it comes @@ -393,6 +403,7 @@ const char* tx_text_speed(int v) { return t[v]; } const char* tx_ultrawide(int v) { return v ? "Monitor's shape" : "16:9"; } +const char* tx_wide_hud(int v) { return v ? "Screen edges" : "Middle"; } const char* tx_prompts(int v) { static const char* t[HOS_PROMPT_N + 1] = {"Auto", "Switch", "Xbox", "PlayStation 4", "PlayStation 5", "Steam Deck"}; return t[std::min(std::max(v, 0), (int)HOS_PROMPT_N)]; @@ -467,6 +478,9 @@ const char* hn_mic(int v, int variant) { return missing ? "Not plugged in: Default stands in until it is back." : "This microphone, by its name. A change applies at once."; } const char* tx_rumble(int v) { return v ? tx_percent(v) : "Off"; } +const char* tx_volume(int v) { return v ? tx_percent(v) : "Muted"; } +const char* tx_sprint(int v) { return v == 1 ? "Left stick click" : v == 2 ? "B" : "Off"; } +const char* tx_sprint_mode(int v) { return v ? "Hold" : "Toggle"; } // (hookshot) Menu size: how large the overlays are drawn (vk_ui.h unit); Auto is Large in fullscreen at 1080 lines and up while // a controller is the menu's input, else Medium bool pad_last = false; // (a controller was the menu's last input: its buttons in the prompts) @@ -634,6 +648,11 @@ Row rows[] = { hn_output_res, true}, {TAB_DISPLAY, "Ultrawide", &opts.ultrawide, 0, 1, 1, tx_ultrawide, "The camera's shape on a screen wider or narrower than 16:9 (4:3, 16:10). Applies after a restart.", hn_ultrawide, true}, + // (Triforce: host/oot_wide_hud.c) + {TAB_DISPLAY, "Ultrawide HUD", &opts.wide_hud, 0, 1, 1, tx_wide_hud, + "With Ultrawide: where the hearts, the item slot and the map sit on a screen wider than 16:9. Middle: in the " + "middle 16:9, as on a normal screen. Screen edges: out in the screen's corners. Applies after a restart.", nullptr, + true, true}, {TAB_DISPLAY, "Ultrawide menus", &opts.wide_menus, 0, 2, 1, tx_wide_menus, "With Ultrawide: the pause, item and map menus, and what shows beside them. Loading screens and pre-rendered movies, " "which draw no world either, get the same sides. Changes at once.", hn_wide_menus}, @@ -677,8 +696,9 @@ Row rows[] = { "RenoDX's colour grading: the scene's saturation. 50 is neutral.", nullptr, false, true}, {TAB_DISPLAY, "RenoDX bloom", &opts.rdx_bloom, 0, 100, 5, tx_rdx_grade, "How strong the glow around bright things is. 50 is the game's.", nullptr, false, true}, - {TAB_DISPLAY, "Game frame rate cap", &opts.fps_cap, 0, 5, 1, tx_fps_cap, + {TAB_DISPLAY, "Game frame rate cap", &opts.fps_cap, -3, 5, 1, tx_fps_cap, "Limits frames rendered by the game. Generated frames are additional. Takes a restart. " + "30, 40 and 45 are for a PC or handheld that holds one of them steadily where 60 swings; the game keeps its speed. " "Past 60 is experimental: the game keeps real time, but some cutscenes and bosses can misbehave (Gohma, the intro's " "narration). 60 is the safe choice.", nullptr, true, true}, @@ -712,6 +732,11 @@ Row rows[] = { {TAB_GRAPHICS, "Frame generation", &opts.framegen, 0, 8, 1, tx_framegen, "Makes frames between the game's own, for smoother motion, at a little input delay. Fast motion or camera cuts can " "show artifacts.", hn_framegen, false, true}, + // (Triforce, after NX2navi's: vk_dlssg.cpp. Read at the start, where the device is made through NVIDIA's Streamline) + {TAB_GRAPHICS, "DLSS frame generation", &opts.dlssg, 0, 3, 1, tx_dlssg, + "NVIDIA RTX 40 and 50 cards: frames made between the game's own by NVIDIA DLSS, from the scene's depth and motion, " + "with NVIDIA Reflex. The Frame generation row above is not used with it. Applies after a restart.", hn_dlssg, true, + true}, {TAB_GRAPHICS, "Frame pacing", &opts.pace, 0, 1, 1, tx_onoff, "When each frame is shown.", hn_pace}, {TAB_GRAPHICS, "NVIDIA Reflex", &opts.reflex, 0, 2, 1, tx_reflex, "NVIDIA GPUs: each frame starts as late as it can, for less input delay. Not used with frame generation.", hn_reflex}, @@ -727,14 +752,17 @@ Row rows[] = { hn_speed}, {TAB_SYSTEM, "Text speed", &opts.text_speed, 0, 3, 1, tx_text_speed, "How fast the letters of dialogue and other messages appear.", hn_text_speed}, + // (Triforce, after NX2navi's volume row: over the game's own mix, host/nn_audio.c; the game's own Options have none) + {TAB_AUDIO, "Master volume", &opts.volume, 0, 100, 5, tx_volume, + "How loud the game is, in percent of its own volume. The game's sounds and music keep their balance.", nullptr, false, + true}, {TAB_AUDIO, "Audio output", &opts.audio_output, 0, 2, 1, tx_audio_output, "The speakers the game mixes its sound for. Applies after a restart.", hn_audio_output, true, true}, {TAB_AUDIO, "Microphone", &opts.mic, 0, 2 + kMaxMics, 1, tx_mic, "What the ocarina hears when the game's microphone option is on (playing an instrument, or humming).", hn_mic, false, true}, {TAB_SYSTEM, "Discord status", &opts.discord, 0, 1, 1, tx_onoff, - "Shows the game and the time played in your Discord status, while Discord runs on this PC. Triforce has no Discord " - "application of its own: it needs discord_app_id= in triforce_options.ini.", nullptr, false, + "Shows \"Playing Triforce\" and the time played in your Discord status, while Discord runs on this PC.", nullptr, false, true}, // Controls (11 rows; Motion opens the 6 TAB_MOTION rows, Controls its page with the 2 TAB_PAGE rows) @@ -747,10 +775,22 @@ Row rows[] = { "count. The - and = keys change it in game too.", nullptr, false, true}, {TAB_CONTROLS, "Invert mouse vertical", &opts.mouse_invert, 0, 1, 1, tx_onoff, "Swaps up and down for the mouse.", hn_mouse_invert, false, true}, + // (Triforce: host/prompts.c build_camera) + {TAB_CONTROLS, "Camera speed", &opts.camera_speed, 25, 300, 5, tx_sens, + "How fast the right stick turns the camera: times the game's own speed, at any Camera sensitivity of the game's " + "Options. The mouse keeps its own sensitivity. Applies after a restart.", nullptr, true, true}, {TAB_CONTROLS, "Invert camera X", &opts.invert_cam_x, 0, 1, 1, tx_onoff, "Inverts horizontal look (left/right) on the right analog stick.", nullptr, false, true}, {TAB_CONTROLS, "Invert camera Y", &opts.invert_cam_y, 0, 1, 1, tx_onoff, "Inverts vertical look (up/down) on the right analog stick.", nullptr, false, true}, + // (Triforce, after NX2navi's sprint button: host/oot_sprint.h) + {TAB_CONTROLS, "Sprint button", &opts.sprint_button, 0, 2, 1, tx_sprint, + "Link sprints from a button press, without rolling first. Left stick click: the game has no other use for it. B: sprints " + "only while Link moves, and puts the weapon away as usual otherwise (with the game's Swap Jump Controls B is the jump: " + "use the stick's click).", nullptr, false, true}, + {TAB_CONTROLS, "Sprint mode", &opts.sprint_mode, 0, 1, 1, tx_sprint_mode, + "Toggle: a press starts the sprint and the next press, or stopping, ends it. Hold: Link sprints while the button is " + "held.", nullptr, false, true}, {TAB_CONTROLS, "Stick dead zone", &opts.stick_dead_zone, 0, 5, 1, tx_stick_dead_zone, "How far a stick moves before the game sees it, on each axis. Lower for a precise stick, higher for one that drifts. " "The game's own dead zone still follows.", nullptr, false, true}, diff --git a/gpu/vk/vk_menu.h b/gpu/vk/vk_menu.h index 6948ffb..bdf1f61 100644 --- a/gpu/vk/vk_menu.h +++ b/gpu/vk/vk_menu.h @@ -24,7 +24,7 @@ struct Options { int rdx_exposure = 100; // percent, 0 to 200 int rdx_highlights = 50, rdx_shadows = 50, rdx_contrast = 50, rdx_saturation = 50; // 0 to 100, 50 neutral int rdx_bloom = 50; // 0 to 100, 50 the game's - int fps_cap = 0; // (hookshot) Frame rate cap: 0 60 (the console's), 90, 120, 144, 165, 240 (host/nn_vi.c reads frame_rate_cap= at the start) + int fps_cap = 0; // (hookshot) Frame rate cap: 0 60 (the console's), 90, 120, 144, 165, 240; -1 45, -2 40, -3 30 (host/nn_vi.c reads frame_rate_cap= at the start) int resolution = 0; // the 3D's (Render resolution): 0 Auto (the game's dynamic resolution), else a share of the output, // 1..6 = 100%, 75%, 67%, 50%, 58%, 33% (upscaler_rows.h; nx2/oot_patches.py) int output_res = 0; // the game's whole frame, docked: 0 its 2048x1152, 1 2560x1440, 2 3840x2160, 3 1920x1080, 4 5120x1440, 5 5120x2880; read once, at the start (host/nn_applet.c) @@ -54,6 +54,7 @@ struct Options { int mouse_invert = 0; // the mouse's up and down swapped int text_speed = 0; // how fast dialogue prints: 0 the game's, 1 2x, 2 4x, 3 instant (host/nn_vi.c, nx2/oot_patches.py; HOS_TEXT_SPEED) int invert_cam_x = 0; // right-stick camera horizontal inversion: 0 normal, 1 inverted + int camera_speed = 100; // (Triforce) the right stick's camera speed, percent of the game's own (host/prompts.c build_camera) int invert_cam_y = 0; // right-stick camera vertical inversion: 0 normal, 1 inverted int rumble = 100; // the controllers' rumble in percent of the game's: 0 Off, 25 to 150 (host/pad_sdl.c) int stick_dead_zone = 0; // a stick axis below it is 0: 0 XInput's 7849 (~24%), 1..5 5% to 30% (host/nn_hid.c) @@ -72,6 +73,11 @@ struct Options { int vram_mb = 0; // dynamic texture cache budget limit in MB int mirror_mb = 0; // staging mirror limit in MB int audio_output = 0; // (hookshot) the speakers the game mixes for: 0 Auto (the default output's), 1 Stereo, 2 5.1; read once, at the start (host/nn_audio.c; HOS_AUDIO_CHANNELS) + int volume = 100; // (Triforce) Master volume, percent of the game's own (host/nn_audio.c render_frame; master_volume= in the ini) + int dlssg = 0; // (Triforce) NVIDIA DLSS frame generation: 0 off, 1 2x, 2 3x, 3 4x; read at the start (vk_dlssg.cpp, dlss_frame_generation=) + int wide_hud = 0; // (Triforce) with Ultrawide: 0 the HUD in the middle 16:9, 1 its corner pieces at the screen's edges (host/oot_wide_hud.c) + int sprint_button = 0; // (Triforce) the sprint button: 0 off, 1 the left stick's click, 2 B (host/oot_sprint.h) + int sprint_mode = 0; // 0 toggle (a press starts it, the next ends it), 1 hold int discord = 1; // (hookshot) the Discord status (host/discord_rpc.c): 1 on, shown while a Discord client runs on this PC int debug_tools = 0; // (hookshot) the Debug tab's tools (#785: flight, free camera, teleport, inventory): 0 off; HOS_OOT_CHEATS=1 also turns them on int mic = 1; // (hookshot) the ocarina's microphone: 0 Off, 1 the system's default, 2.. a device as host->mic_name lists it (host/nn_audio_in.c) diff --git a/gpu/vk/vk_reflex.cpp b/gpu/vk/vk_reflex.cpp index 5c4e4b0..fae2bf1 100644 --- a/gpu/vk/vk_reflex.cpp +++ b/gpu/vk/vk_reflex.cpp @@ -24,6 +24,7 @@ #include #include +#include "vk_dlssg.h" #include "vk_framegen.h" #include "vk_menu.h" #include "vk_work.h" // (last: RX_VK_RECORDER=defer drains before the latency-sleep wait; =thread does not need to, the driver signals it) @@ -59,6 +60,8 @@ Reflex& rx() { int wanted_mode() { static const char* e = getenv("RX_VK_REFLEX"); + // (Triforce: with NVIDIA DLSS frame generation Reflex is Streamline's, vk_dlssg.cpp: this one stays off) + if (dlssg::early()) return 0; return std::clamp(e && *e ? atoi(e) : opts.reflex, 0, 2); } diff --git a/gpu/vk/vk_replay.cpp b/gpu/vk/vk_replay.cpp index d27dd23..0d0df8f 100644 --- a/gpu/vk/vk_replay.cpp +++ b/gpu/vk/vk_replay.cpp @@ -29,6 +29,7 @@ #include #include +#include "vk_dlssg.h" #include "vk_temporal.h" #include "vk_fsr3_inputs.h" // (the camera jitter while AMD FSR 3.1 upscales) #include "status_line.h" @@ -758,6 +759,15 @@ void replay(VkCommandBuffer& cmd, NvnState& s, const uint8_t* p, const uint8_t* memory_bind_event_write(true); // (Triforce: not a write-back the captures of its page wait for) gpu_write(cmd, e.gpu, &v, 4); memory_bind_event_write(false); + { // (Triforce, 11-14, the lab) RX_VK_EVENT_EARLY=1: the word reaches the game's memory as the replay passes the + // signal, not when the GPU has run it and its write-back has landed. OoT's Presentation thread polls its + // frame's event (nvnEventGetValue) before it presents and lets the next frame's command buffers be built: + // that poll ends at the GPU's end of the frame. With this it ends at the replay's end of it, the GPU's tail + // (and the write-back, and the poll's own sleep) no longer in the frame's cycle. The game then writes its + // next frame's memory while the GPU still reads this frame's + static const bool early = getenv("RX_VK_EVENT_EARLY") && atoi(getenv("RX_VK_EVENT_EARLY")) != 0; + if (early) guest_cpu_write(e.gpu, &v, 4); + } break; } case NOP_WAIT_EVENT: { // (a wait for an event another queue's work signals: submit_job orders them) @@ -2218,7 +2228,11 @@ bool wait_recorded(uint64_t f, uint64_t timeout_ns, int* ok) { } uint64_t fence(); /// (hookshot) A fence of one queue (nvnQueueFenceSync): the value of that queue's last submission +bool async_unforced(); +uint64_t fence_q_async(uint64_t queue); +bool async_wait_step(uint64_t f); uint64_t fence_q(uint64_t queue) { + if (async_unforced() && queue && !per_queue_fences_off()) return fence_q_async(queue); if (async_on() || !sync_waits_on() || per_queue_fences_off() || !queue) return fence(); std::lock_guard lk(sync_order_mu); auto it = q_last.find(queue); @@ -2227,6 +2241,20 @@ uint64_t fence_q(uint64_t queue) { int wait(uint64_t f, uint64_t timeout_ns) { if (completed() >= f) return 1; queue_flush_for(FR_WAIT); + // (Triforce, 11-14) the replay thread, nothing forced: the fence's own work, on the recordings' timeline once the replay + // thread has recorded it whole; until then the wait looks again each time a job of the thread's ends. As below for the + // submitting thread's replay: not the other queues' stopped work, which the timeline's value waits for + if (async_unforced() && !per_queue_fences_off()) { + const auto t0 = std::chrono::steady_clock::now(); + for (;;) { + if (completed() >= f) return 1; + const uint64_t waited = (uint64_t)std::chrono::duration_cast(std::chrono::steady_clock::now() - t0).count(); + int ok = 0; + if (wait_recorded(f, timeout_ns > waited ? timeout_ns - waited : 0, &ok)) return ok; + if (waited >= timeout_ns) return 0; + if (!async_wait_step(f)) break; // (not a value the replay thread still has to record: the timeline's, below) + } + } // (hookshot) a fence of one queue whose work up to it is recorded whole (none of it stopped): that work, not the other // queues' stopped work the timeline above waits for (which a wait for it forced past its waits after RX_VK_WAIT_HOLD_US) if (!async_on() && sync_waits_on() && !per_queue_fences_off()) { @@ -2296,8 +2324,33 @@ void wait_writebacks_reading(const void*, uint64_t) {} // (nothing to write ba constexpr uint64_t kTexPageChunk = 1u << 20; std::unique_ptr[]> tex_pages[(1ull << 39 >> 12) / kTexPageChunk]; std::mutex tex_pages_mu; +// (Triforce, 11-14) and by their exact bytes, for the replay thread's readbacks: the page counts above answer "a texture's +// storage is in these pages", and OoT's exposure meter buffers (0x1040 bytes each, read back every frame) share their last +// page with a texture's storage. With the replay a frame behind that answer sent every one of those readbacks to wait for +// the replay thread to have nothing left: the main thread or a worker held for up to a whole replay, 5% of their time at the +// Market, and the frame's hand-over with them. The exact ranges say no texture is there +std::mutex tex_exact_mu; +std::vector> tex_exact; // (live textures' storage [start, end), the guest window; tex_exact_mu) +void tex_exact_add(uint64_t lo, uint64_t hi, int d) { + std::lock_guard lk(tex_exact_mu); + if (d > 0) return tex_exact.emplace_back(lo, hi), void(); + for (size_t i = tex_exact.size(); i-- > 0;) + if (tex_exact[i].first == lo && tex_exact[i].second == hi) { + tex_exact[i] = tex_exact.back(); + tex_exact.pop_back(); + return; + } +} +bool tex_exact_any(uint64_t gpu, uint64_t size) { + if (gpu >= (1ull << 39)) return true; // (not tracked: as before) + std::lock_guard lk(tex_exact_mu); + for (const auto& r : tex_exact) + if (r.first < gpu + size && r.second > gpu) return true; + return false; +} void tex_pages_add(const nvn_tex* t, int d) { if (!t || !t->gpu || t->gpu >= (1ull << 39) || !t->L.size) return; // (the guest window: virtual pools are not read) + tex_exact_add(t->gpu, t->gpu + t->L.size, d); const uint64_t p0 = t->gpu >> 12, p1 = std::min((t->gpu + t->L.size - 1) >> 12, uint64_t((1ull << 39 >> 12) - 1)); for (uint64_t p = p0; p <= p1; p++) { auto& c = tex_pages[p / kTexPageChunk]; @@ -2373,15 +2426,23 @@ void invalidate(uint64_t gpu, uint64_t size) { // waits for (OoT's presentation work before the scene's: the fog of the frame before, for a frame, at 1 in 20 of the // exposure readbacks). The write-back is waited for on the recordings' timeline instead, which a stopped queue // does not hold back - const bool unforced = !async_on() && sync_waits_on() && !per_queue_fences_off(); + // (Triforce, 11-14: and on the replay thread with nothing forced, async_unforced: the same rule. The catch-up there + // held the game's thread for the whole replay behind it, 11.5 ms at 520 of 3000 readbacks at the Market, and made + // the stopped queues go past their waits) + const bool unforced = async_unforced() || (!async_on() && sync_waits_on() && !per_queue_fences_off()); if (behind0 && !no_catchup && !unforced) wait_queue_empty(), w = true; // (work handed over may write it back too) const auto w1 = std::chrono::steady_clock::now(); const uint64_t v = memory_writeback_value(host->gpu_guest(gpu), size, nullptr); if (v > completed()) { int ok = 0; if (!(unforced && wait_recorded(v, UINT64_MAX, &ok))) { - if (unforced && replay_behind()) wait_queue_empty(); // (not recorded whole: as before) - wait_value(v); + if (unforced && !async_on() && replay_behind()) wait_queue_empty(); // (not recorded whole: as before) + if (async_unforced()) { // (its recording is being made: when the replay thread has it, on its timeline) + while (completed() < v && !wait_recorded(v, UINT64_MAX, &ok) && async_wait_step(v)) {} + if (completed() < v && !ok) wait_value(v); + } else { + wait_value(v); + } } w = true; } @@ -2426,6 +2487,10 @@ void invalidate(uint64_t gpu, uint64_t size) { if (!texture_gpu_written_in(gpu, size)) { if (!replay_behind()) return; if (!always && !tex_pages_any(gpu, size)) return; // (no texture's storage there: buffers only) + // (Triforce: RX_VK_TEX_EXACT=1, the lab: none in these bytes, no wait: see tex_exact. NOT the default: the wait is the + // one that orders the exposure meter. Without it the Market flickered at once, 4,416 cell blinks in 6,732 frames against + // 0, for 70.3 fps against 67.6-68.0: the game reads each frame's sums whole, after that frame's replay and GPU work) + if (static const bool exact = getenv("RX_VK_TEX_EXACT") != nullptr; exact && !always && async_unforced() && !tex_exact_any(gpu, size)) return; unforced ? sync_settle() : wait_queue_empty(); if (!texture_gpu_written_in(gpu, size)) return; } @@ -3043,7 +3108,9 @@ void do_present(const nvn_tex* t, const int crop[4]) { temporal_idle(); // (DLSS, FSR: their video memory let go once not used for a while) status_tick(); // (the status line, and the video-memory warning) reflex_present_begins(); - struct ReflexSleep { ~ReflexSleep() { reflex_sleep(); antilag_input(); } } reflex_sleep_after; + // (Triforce, NVIDIA DLSS frame generation, vk_dlssg.cpp: Streamline's Reflex sleeps there instead, and the next + // frame's latency markers start) + struct ReflexSleep { ~ReflexSleep() { reflex_sleep(); antilag_input(); dlssg::frame_start(); } } reflex_sleep_after; { // (hookshot, RX_VK_PASS_TRACE_AT=: the passes of the frames around these frame numbers, for finding what a frame is made of) static std::atomic seen{0}; static const int64_t at = getenv("RX_VK_PASS_TRACE_AT") ? atoll(getenv("RX_VK_PASS_TRACE_AT")) : 0; @@ -3153,6 +3220,13 @@ void do_present(const nvn_tex* t, const int crop[4]) { full_barrier(r.cmd); presents++; presents_now = presents; + { // (Triforce, 11-14) this frame's draws, for the predicted pipelines' builder: it rests while the game is in play + static int64_t d_last; + int64_t d_now = 0, x; + draw_stats(&d_now, &x, &x, &x); + frame_shown_note((int)std::clamp(d_now - d_last, 0, 1 << 30)); + d_last = d_now; + } par_tune_present(); // (Triforce: RX_VK_PARALLEL_AUTO) wide_sides_tick(); if (presents == 1) hang_watch_start(); @@ -3799,6 +3873,7 @@ void present_image(VkImage src, uint32_t sw, uint32_t sh, const int crop[4], VkI } menu_record(f.cmd, index, dev.swap_extent.width, dev.swap_extent.height); // (hookshot) Dear ImGui's overlays, over all of it image_barrier(f.cmd, dst, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR); + dlssg::frame(f.cmd, dev.swap_extent.width, dev.swap_extent.height); // (Triforce: this frame's camera, depth and motion for Streamline) if (vkEndCommandBuffer(f.cmd) == VK_ERROR_DEVICE_LOST) device_lost(); // after the image's acquisition. (Not on the timeline: one queue runs submits in order, and a wait for a value // another thread had counted but not yet submitted blocked the queue for good) @@ -3833,6 +3908,7 @@ void present_image(VkImage src, uint32_t sw, uint32_t sh, const int crop[4], VkI std::memcpy(r0, st_reason, sizeof r0); } reflex_submit_ends(); + dlssg::submit_end(); res = vkQueueSubmit2(dev.queue, 1, &si, f.done); if (res == VK_ERROR_DEVICE_LOST) device_lost(); VkPresentInfoKHR pi{VK_STRUCTURE_TYPE_PRESENT_INFO_KHR}; @@ -3843,6 +3919,7 @@ void present_image(VkImage src, uint32_t sw, uint32_t sh, const int crop[4], VkI pi.pSwapchains = &dev.swapchain; pi.pImageIndices = &index; antilag_present(); // (AMD Anti-Lag 2: the frame's present stage) + dlssg::present_marker(false); // (Triforce, DLSS frame generation: the present goes through Streamline's interposer below) // (hookshot) vkQueuePresentKHR blocked a quarter of the display thread's time with frame interpolation at 4x (a 14700K and an // RTX 5070 Ti), and with the queue's lock held there the game's main thread waited ~9% of its time and the presentation thread // ~15% to submit. With a queue of their own (dev.present_queue, a compute family's: vk_main.cpp create_device) the presents go @@ -3856,6 +3933,7 @@ void present_image(VkImage src, uint32_t sw, uint32_t sh, const int crop[4], VkI res = vkQueuePresentKHR(dev.queue, &pi); } if (res == VK_ERROR_DEVICE_LOST) device_lost(); + dlssg::present_marker(true); reflex_presented(); if (res < 0 && res != VK_ERROR_OUT_OF_DATE_KHR) { // (a present that failed: said, a few times) static int logged; @@ -3971,6 +4049,7 @@ struct Job { nvn_samp_par samp{}; uint64_t ev_gpu = 0; // (a CPU signal: the event and its value) uint32_t ev_value = 0; + bool ev_capture = false; // (Triforce: a capture was taken at the signal, on the game's thread: event_signal_queued) uint64_t value = 0; // (a submit's or present's timeline value: its fence, handed out as it came) uint64_t queue = 0; // (a submit's queue) int crop[4] = {0, 0, 0, 0}; @@ -3979,12 +4058,41 @@ struct Job { std::chrono::steady_clock::time_point queued; }; std::mutex q_mu; -std::condition_variable q_cv, q_space, q_idle; +std::condition_variable q_cv, q_space, q_idle, q_done; std::deque q_jobs; int q_presents = 0; bool q_busy = false; +std::unordered_map q_last_async; // (queue -> the value of its last submit handed over; q_mu) +// (Triforce, 11-14) The replay thread with nothing forced (the default with RX_VK_ASYNC=1; RX_VK_ASYNC_FORCED=1: as it was). +// The thread's mode was left behind when the submitting thread's replay got its ordering rules (sync_settle, the queues' own +// fences, readbacks waited for on the recordings' timeline): every catch-up of the game's threads (a readback, a virtual +// pool remapped, a finish) still made the stopped queues go past their event waits, ahead of the work they wait for, and +// held the caller until the replay thread had nothing left. At the Market with the game held one present ahead: 36.7 fps +// against 60 on the submitting thread, 522 of 3000 readbacks waiting 11.5 ms each, 60,000 waits gone past in a run where +// the submitting thread's replay has none, 52 cell blinks in 3,616 frames against 0. Now, as there: a catch-up waits for +// what was handed over to be replayed as far as its waits allow and forces nothing; a fence is its own queue's last submit, +// waited for on the recordings' timeline; a readback waits for the recording that writes its range back, not for the queue +bool async_unforced() { + static const bool v = async_on() && !(getenv("RX_VK_ASYNC_FORCED") && atoi(getenv("RX_VK_ASYNC_FORCED")) != 0); + return v; +} +uint64_t fence_q_async(uint64_t queue) { + std::lock_guard lk(q_mu); + auto it = q_last_async.find(queue); + return it != q_last_async.end() ? it->second : q_values.load(); +} +/// A wait for a value the replay thread has not recorded whole yet: until its next job ends (or half a millisecond). False +/// when the thread has nothing that could record it (idle, no queue stopped) +bool async_wait_step(uint64_t) { + std::unique_lock lk(q_mu); + if (q_jobs.empty() && !q_busy && !blocked_n.load()) return false; + q_done.wait_for(lk, std::chrono::microseconds(500)); + return true; +} int presents_ahead() { // (RX_VK_PRESENTS_AHEAD=: how many presents the game may hand over ahead of the replay) - static const int n = std::clamp(getenv("RX_VK_PRESENTS_AHEAD") ? atoi(getenv("RX_VK_PRESENTS_AHEAD")) : 2, 1, 8); + // (Triforce: one with nothing forced: OoT writes its double-buffered data again a frame after it handed it over, and a + // replay two presents behind read the next frame's: the white and black frames the thread was turned off for) + static const int n = std::clamp(getenv("RX_VK_PRESENTS_AHEAD") ? atoi(getenv("RX_VK_PRESENTS_AHEAD")) : async_unforced() ? 1 : 2, 1, 8); return n; } @@ -4027,7 +4135,8 @@ void replay_thread_main() { update_blocked(); std::lock_guard lk(q_mu); q_busy = false; - if (q_jobs.empty() && !blocked_n.load()) q_idle.notify_all(); + if (q_jobs.empty()) q_idle.notify_all(); // (the waiters look at blocked_n themselves: wait_queue_empty) + q_done.notify_all(); continue; } // (RX_VK_TIMELINE: one frame's jobs every 240 presents: when the game handed each over, how long it waited @@ -4037,18 +4146,66 @@ void replay_thread_main() { const auto t_start = std::chrono::steady_clock::now(); int64_t d0 = 0, x; if (timeline) draw_stats(&d0, &x, &x, &x); + // (Triforce, 11-14, diagnostics) RX_VK_FT=: from that present on, RX_VK_FT_N presents (8): each job of this + // thread at its start and its end, by the host's clock (QueryPerformanceCounter ms, the last 100 s): the renderer's + // half of the host's frame trace (HOS_FT, nvn_native.c) + static const int64_t ft_from = getenv("RX_VK_FT") ? atoll(getenv("RX_VK_FT")) : -1; + static const int64_t ft_n = getenv("RX_VK_FT_N") ? atoll(getenv("RX_VK_FT_N")) : 8; + const bool ft = ft_from >= 0 && presents >= ft_from && presents < ft_from + ft_n; + const auto ft_ms = [] { + return (double)(std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count() % + 100000000000ll) / 1e6; + }; + static const char* const kinds[] = {"submit", "present", "texture released", "program released", "register texture", + "register sampler", "event signal"}; + int64_t ft_d0 = 0; + if (ft) { + draw_stats(&ft_d0, &x, &x, &x); + if (j.kind == JOB_SUBMIT || j.kind == JOB_PRESENT || j.kind == JOB_EVENT_SIGNAL) + logf("ft %.3f job> %s value %llu queue %#llx (handed over %.2f ms ago; GPU done up to %llu)", ft_ms(), + kinds[j.kind], (unsigned long long)j.value, (unsigned long long)j.queue, + std::chrono::duration(t_start - j.queued).count(), (unsigned long long)completed()); + } switch (j.kind) { case JOB_SUBMIT: submit_job(j.handles, j.value, j.queue); break; case JOB_PRESENT: // (the frame's work whole before it is shown) - drain(0); + // (Triforce, 11-14) With the game a frame ahead of the replay, what is stopped at a present is mostly the NEXT + // frame's work: the worker queues hand theirs over early, each waiting for the main queue's signal of that + // frame, which comes after this present. Made to go here it ran ahead of its signaller: one present in ten at the + // Market (1,499 of 15,000, 14,375 waits gone past) against 29 in a run of the submitting thread's replay. So a + // present can let on what may go and force nothing (RX_VK_PRESENT_DRAIN=0); work that stays stopped then goes on + // at its signal, or after hold_ms as anywhere. NOT the default: the first run with it stood at a loading screen + // for good (the Market save, 5 draws a frame for six minutes, 11-14 03:20): a load waits for work only a present + // makes go. The picture with the presents forcing showed no flicker (0 cell blinks in 6,472 frames) + if (static const bool keep = getenv("RX_VK_PRESENT_DRAIN") && atoi(getenv("RX_VK_PRESENT_DRAIN")) == 0; async_unforced() && keep) { + burst_begin(0); + schedule(); + burst_end(); + } else { + drain(0); + } present_value = j.value; do_present(j.has_tex ? &j.tex : nullptr, j.has_crop ? j.crop : nullptr); break; case JOB_TEXTURE_RELEASED: // (after the stopped work that may use it) + // (Triforce: with nothing forced the release is kept until the stopped work has gone on, as on the submitting + // thread's replay: update_blocked makes it then) + if (async_unforced() && hold_releases() && blocked_n.load()) { + std::lock_guard hl(held_mu); + held_tex_releases.push_back(j.tex); + st_held_releases++; + break; + } drain(1); do_texture_released(&j.tex); break; case JOB_PROGRAM_RELEASED: + if (async_unforced() && hold_releases() && blocked_n.load()) { + std::lock_guard hl(held_mu); + held_prog_releases.push_back(j.prog); + st_held_releases++; + break; + } drain(1); do_program_released(&j.prog); break; @@ -4056,12 +4213,26 @@ void replay_thread_main() { case JOB_REGISTER_SAMPLER: register_sampler_copy(j.id, j.samp); break; case JOB_EVENT_SIGNAL: // (the CPU set an event: the stopped ones may go on) ev_values[j.ev_gpu] = j.ev_value; + if (j.ev_capture) { // (the CPU's writes up to its signal, for what goes on from here: see event_signal_queued) + std::lock_guard lk(replay_mu); + uint64_t v = rec_value_now; + for (const auto& [queue, q] : qruns) + if (q.waiting) v = std::max(v, q.value); + memory_take_capture(v, completed()); + st_resume_captures++; + } burst_begin(0); schedule(); burst_end(); break; } update_blocked(); + if (ft && (j.kind == JOB_SUBMIT || j.kind == JOB_PRESENT || j.kind == JOB_EVENT_SIGNAL)) { + int64_t ft_d1 = 0; + draw_stats(&ft_d1, &x, &x, &x); + logf("ft %.3f job< %s value %llu: %lld draws, %d queues stopped, GPU done up to %llu", ft_ms(), kinds[j.kind], + (unsigned long long)j.value, (long long)(ft_d1 - ft_d0), blocked_n.load(), (unsigned long long)completed()); + } if (j.kind == JOB_PRESENT && presents % 3600 == 0) log_order_stats(); if (timeline && (j.kind == JOB_SUBMIT || j.kind == JOB_PRESENT)) { struct T { char k; double q, s, e; int64_t d; size_t h; }; @@ -4096,7 +4267,8 @@ void replay_thread_main() { std::lock_guard lk(q_mu); if (j.kind == JOB_PRESENT) q_presents--, q_space.notify_all(); q_busy = false; - if (q_jobs.empty() && !blocked_n.load()) q_idle.notify_all(); + if (q_jobs.empty()) q_idle.notify_all(); + q_done.notify_all(); } } @@ -4114,6 +4286,7 @@ void enqueue(Job&& j) { q_presents++; } if (j.kind == JOB_SUBMIT || j.kind == JOB_PRESENT) j.value = ++q_values; + if (j.kind == JOB_SUBMIT && j.queue) q_last_async[j.queue] = j.value; q_jobs.push_back(std::move(j)); q_cv.notify_one(); } @@ -4139,6 +4312,10 @@ void wait_queue_empty() { return; } std::unique_lock lk(q_mu); + if (async_unforced()) { // (what was handed over, replayed as far as its waits allow: nothing made to go past one) + q_idle.wait(lk, [] { return q_jobs.empty() && !q_busy; }); + return; + } drain_waiters++; // (stopped submissions go on now: replay_thread_main) q_cv.notify_all(); q_idle.wait(lk, [] { return q_jobs.empty() && !q_busy && blocked_n.load() == 0; }); @@ -4286,6 +4463,20 @@ void event_signal_queued(uint64_t gpu, uint32_t value) { } Job j{JOB_EVENT_SIGNAL}; j.ev_gpu = gpu, j.ev_value = value; + // (Triforce, 11-14) The game writes what a queue's work reads after it submitted that work, up to this signal (OoT's + // presentation queue waits for its frame's data this way): the console's GPU reads memory after its wait. The submitting + // thread's replay takes a capture when a stopped queue goes on from a CPU event (resume_queue, RX_VK_RESUME_CAPTURE); the + // replay thread took none, and what went on from the wait read the frame before's fog and exposure: a frame's flash, 27 + // cell blinks in 6,600 frames at the Market against none. Here the capture is taken at the signal itself, on the game's + // thread, where the data is as the game means it, and the replay thread takes it in at the signal's place among its + // jobs. Only while the thread has work behind it: with none, the next submit's own capture has it all + static const bool capture = !getenv("RX_VK_RESUME_CAPTURE") || atoi(getenv("RX_VK_RESUME_CAPTURE")) != 0; + if (capture && async_unforced() && replay_behind()) { + std::lock_guard lk(submit_order_mu); // (captures and jobs in one order) + j.ev_capture = memory_capture_submit(); + enqueue(std::move(j)); + return; + } enqueue(std::move(j)); } void register_sampler_queued(int id, const nvn_samp_par* p) { @@ -4411,11 +4602,17 @@ void fill_replay_table(MkwVkRenderer& t) { if (id == HOS_OPT_HDR_PEAK) return std::clamp(opts.hdr_peak, 400, 10000); if (id == HOS_OPT_HDR_WHITE) return std::clamp(opts.hdr_white, 80, 500); if (id == HOS_OPT_DISCORD) return opts.discord != 0; + if (id == HOS_OPT_VOLUME) return std::clamp(opts.volume, 0, 100); + if (id == HOS_OPT_SPRINT) return opts.sprint_button; + if (id == HOS_OPT_SPRINT_HOLD) return opts.sprint_mode; return id == 0 ? opts.realtime : id == 1 ? menu_is_open() : id == 2 ? resolution_auto() && !temporal_full_scale() : id == 3 ? (temporal_full_scale() ? 10000 : resolution_scale()) : id == 4 ? opts.pad_background : id == 5 ? opts.text_speed : id == 6 ? opts.mouse_native : id == 7 ? opts.mouse_sens : id == 8 ? opts.mouse_invert : id == 9 ? opts.rumble : id == 16 ? opts.invert_cam_x : id == 17 ? opts.invert_cam_y : id == HOS_OPT_GYRO ? opts.gyro : id == HOS_OPT_GYRO_SENS_X ? opts.gyro_sens_x : id == HOS_OPT_GYRO_SENS_Y ? opts.gyro_sens_y : id == HOS_OPT_GYRO_INV_X ? opts.gyro_inv_x : id == HOS_OPT_GYRO_INV_Y ? opts.gyro_inv_y : id == HOS_OPT_GYRO_DEADZONE ? opts.gyro_deadzone : id == HOS_OPT_DLSS_JITTER_W || id == HOS_OPT_DLSS_JITTER_H || id == HOS_OPT_DLSS_JITTER_PHASES ? jitter_option(id) : 0; }; - t.option_set = menu_option_set; + t.option_set = [](int id, int value) { + if (id == HOS_OPT_EXITING) return dlssg::shutdown(); // (the process is about to exit: host/hos_relaunch.c) + menu_option_set(id, value); + }; t.nvn_register_sampler = register_sampler_queued; t.nvn_set_texture_pool_size = set_texture_pool_size; t.nvn_texture_released = texture_released_of; diff --git a/gpu/vk/vk_shader.cpp b/gpu/vk/vk_shader.cpp index 43c6376..3001d65 100644 --- a/gpu/vk/vk_shader.cpp +++ b/gpu/vk/vk_shader.cpp @@ -95,6 +95,8 @@ void ptr_diag_log(uint64_t key, const char* kind) { } } // namespace #include "vk_shader.h" +#include "spv_exact.h" +#include "vk_texture.h" namespace mkwvk { namespace { @@ -653,6 +655,19 @@ uint32_t spirv_crc32(const std::vector& spirv) { } /// (local, RenoDX port) *renodx: set when RenoDX has a replacement for this module, which is made in its place (vk_renodx.h) +// (Triforce, texture detail: the texture bindings each module reads by texel position or asks the size of, spv_exact.h; +// kept only where the card's detail level leaves levels out, and only for the modules that have such a binding) +std::mutex exact_mu; +std::atomic exact_gen{1}; // (bumped when an entry changes: shader_reads_exact's per-thread answer) +std::unordered_map exact_of; +void exact_note(VkShaderModule m, const std::vector& code) { + if (!m || texture_detail_level() <= 0) return; + const spv_exact::Bindings b = spv_exact::scan(code.data(), code.size()); + std::lock_guard lk(exact_mu); + if (spv_exact::any(b)) exact_of[m] = b; + else if (!exact_of.erase(m)) return; // (a handle used again; nothing kept and nothing there: no thread's answer changes) + exact_gen.fetch_add(1, std::memory_order_release); +} VkShaderModule make_module(const std::vector& spirv, uint32_t* renodx = nullptr, uint32_t* crc_out = nullptr) { const uint32_t crc = spirv_crc32(spirv); if (crc_out) *crc_out = crc; @@ -682,14 +697,31 @@ VkShaderModule make_module(const std::vector& spirv, uint32_t* renodx salted[2] ^= salt; ci.pCode = salted.data(); vkCreateShaderModule(dev.device, &ci, nullptr, &m); + exact_note(m, code); return m; } vkCreateShaderModule(dev.device, &ci, nullptr, &m); + exact_note(m, code); return m; } } // namespace +bool shader_reads_exact(VkShaderModule module, uint32_t binding) { + // (the last module asked about, a thread: draws that follow each other mostly share their program) + static thread_local VkShaderModule last = VK_NULL_HANDLE; + static thread_local spv_exact::Bindings last_bits{}; + static thread_local uint64_t last_gen = 0; + const uint64_t gen = exact_gen.load(std::memory_order_acquire); // (a new module may have taken an old one's handle) + if (module != last || last_gen != gen) { + std::lock_guard lk(exact_mu); + const auto it = exact_of.find(module); + last_bits = it == exact_of.end() ? spv_exact::Bindings{} : it->second; + last = module, last_gen = gen; + } + return spv_exact::has(last_bits, binding); +} + /// A word of a stage's constant bank: c0 / c2 the driver's banks, c1 its own constants, c3.. its uniform buffers uint32_t stage_cbuf_word(const NvnState& s, const Stage& st, uint32_t index, uint32_t offset) { if (index == 1) { diff --git a/gpu/vk/vk_shader.h b/gpu/vk/vk_shader.h index 8afdf90..04d2faa 100644 --- a/gpu/vk/vk_shader.h +++ b/gpu/vk/vk_shader.h @@ -31,6 +31,10 @@ struct Stage { /// recompiler) run over the stage's constant banks and storage buffers as the draw has them uint32_t stage_handle(const NvnState& s, const Stage& st, uint32_t program, uint32_t element = 0); +/// (Triforce, texture detail) Whether the module reads the texture at that binding by texel position or asks its size +/// (spv_exact.h); false on a card whose textures are whole anyway. Any thread of the replay. +bool shader_reads_exact(VkShaderModule module, uint32_t binding); + /// A translated program variant: its stages, in descriptor order. The pipeline layout and pipelines made for it live /// in vk_pipeline.cpp (keyed by key). struct Program { diff --git a/gpu/vk/vk_temporal.cpp b/gpu/vk/vk_temporal.cpp index 6ec3a99..6fdc7e3 100644 --- a/gpu/vk/vk_temporal.cpp +++ b/gpu/vk/vk_temporal.cpp @@ -137,7 +137,10 @@ struct Camera { VkFormat format = VK_FORMAT_UNDEFINED; // (the scene's colour target's: for the log) const Texture* color = nullptr; // (the scene's colour target itself: temporal_pre_tonemap draws into it) Texture* depth = nullptr; - uint64_t depth_gen = 0; // (texture_destroy_generation when taken: a depth texture destroyed since is not used) + // (the two textures' creation numbers and texture_destroyed_count when taken: one destroyed since is not used. It was + // texture_destroy_generation, which any texture's end changes: there_still) + uint32_t color_id = 0, depth_id = 0; + uint64_t noted = 0; uint64_t frame = 0; // (texture_frame_no when taken) bool valid = false; }; @@ -196,7 +199,8 @@ struct State { const Texture* pre_final = nullptr; bool pre_direct = false; Texture* pre_out = nullptr; - uint64_t pre_out_gen = 0; + uint32_t pre_out_id = 0; // (its creation number and texture_destroyed_count when noted: there_still) + uint64_t pre_out_noted = 0; const Texture* pre_scene = nullptr; uint64_t pre_late = 0, pre_drawn = 0, pre_missed = 0; int pre_miss_run = 0; // (frames in a row without the point) @@ -466,6 +470,13 @@ bool make_pipelines(bool hdr) { return true; } +/// A texture kept from an earlier draw is the same one still: it was not destroyed since it was noted (its creation number +/// and texture_destroyed_count then). texture_destroy_generation was asked before, which any texture's end changes: on a +/// card whose video memory is full the cache lets textures go in most frames, and each of those frames lost the engine +/// or its picture at the output's size +bool there_still(uint32_t id, uint64_t noted) { return !texture_destroyed_since(id, noted); } +bool there_still(const Camera& cam) { return there_still(cam.color_id, cam.noted) && there_still(cam.depth_id, cam.noted); } + /// A view of a depth texture's depth alone, for sampling (made once per image); of a colour texture, its colour (the /// scene's target, sampled and written by temporal_pre_tonemap) VkImageView depth_view(Texture* t) { @@ -1134,7 +1145,8 @@ void temporal_note_scene_draw(const Texture* color0, uint32_t width, uint32_t he const bool plain = std::isfinite(n) && n > 0.0f && p11 > 0.0f && (!std::isfinite(f) || f > n); k.near_z = plain ? n : 0.0f, k.far_z = plain ? (std::isfinite(f) ? f : 1e9f) : 0.0f; k.fov_y = plain ? 2.0f * std::atan(1.0f / p11) : 0.0f; - k.width = width, k.height = height, k.format = color0->format, k.color = color0, k.depth = depth, k.depth_gen = texture_destroy_generation(), k.frame = now; + k.width = width, k.height = height, k.format = color0->format, k.color = color0, k.depth = depth, k.frame = now; + k.color_id = color0->id, k.depth_id = depth->id, k.noted = texture_destroyed_count(); k.valid = true; } @@ -1426,7 +1438,7 @@ bool pre_dispatch(VkCommandBuffer cmd, int mode, Texture* in, VkImageView in_vie const bool direct = s.pre_direct && out && out == s.pre_out; Texture* final = const_cast(s.pre_final); s.pre_pending = false, s.pre_candidate = {}, s.pre_final = nullptr, s.pre_direct = false; - if (out && !out_level) s.pre_out = out, s.pre_out_gen = texture_destroy_generation(); + if (out && !out_level) s.pre_out = out, s.pre_out_id = out->id, s.pre_out_noted = texture_destroyed_count(); if (out && temporal_scene_format(out->format)) note_output(out, out_level); return direct || pre_copy(cmd, final, in, in_view, out, out_view, out_level); } @@ -1530,7 +1542,7 @@ bool temporal_upscale(VkCommandBuffer cmd, uint64_t key, bool known, Texture* in s.asked = texture_frame_no(), s.holding = true; // (not after a failure: what it holds is let go, temporal_idle) if (!in || !in_view || !out || !out_view) return fail("no input or output view"); if (!cam.valid || texture_frame_no() - cam.frame > 1) return fail("no scene camera this frame"); - if (cam.depth_gen != texture_destroy_generation()) return fail("the scene depth may be gone"); + if (!there_still(cam)) return fail("the scene's colour or depth is gone"); const uint32_t rw = in->width, rh = in->height; const uint32_t ow = std::max(1u, out->width >> out_level), oh = std::max(1u, out->height >> out_level); if (rw != cam.width || rh != cam.height) return fail("the input is not the scene's size"); @@ -1787,7 +1799,7 @@ Texture* temporal_pre_tonemap(VkCommandBuffer cmd) { const bool f3 = !ur::is_dlss(mode) && !ur::is_fsr4(mode); Engine* const engine = f3 ? nullptr : &engine_of(mode); // (anything unexpected: nothing here, and the upscale's dispatch draws as before, on the tone mapped picture) - if (!cam.valid || !scene || (engine && (!engine->e || engine->failed)) || cam.depth_gen != texture_destroy_generation()) + if (!cam.valid || !scene || (engine && (!engine->e || engine->failed)) || !there_still(cam)) return nullptr; const uint32_t rw = cam.width, rh = cam.height; const VkImageUsageFlags need = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT; @@ -1849,7 +1861,9 @@ bool temporal_pre_composite(const Texture* color0, uint32_t width, uint32_t heig // (the engine's picture is larger than the scene: the draw once more, into the upscale's output at its size) Texture* out = s.pre_out; const auto& pic = s.pre_picture; - if (!pic.view || (pic.width == width && pic.height == height) || !out || s.pre_out_gen != texture_destroy_generation()) + // (the output as the last frame's dispatch had it, unless that texture itself went since: with the video memory full + // textures go in most frames, and by the generation each of those frames was left to pre_copy, the scene's size scaled up) + if (!pic.view || (pic.width == width && pic.height == height) || !out || !there_still(s.pre_out_id, s.pre_out_noted)) return false; if (out->width != pic.width || out->height != pic.height || !temporal_scene_format(out->format) || !(out->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) || out->samples != VK_SAMPLE_COUNT_1_BIT || out->layers != 1) diff --git a/gpu/vk/vk_texture.cpp b/gpu/vk/vk_texture.cpp index ff32d54..6eadb6f 100644 --- a/gpu/vk/vk_texture.cpp +++ b/gpu/vk/vk_texture.cpp @@ -48,7 +48,9 @@ #include "host_shaders/bc_encoder_comp_spv.h" #include "bc_levels.h" #include "astc_bc_policy.h" +#include "tex_detail.h" #include "vram_compact.h" +#include "texture_gone.h" #include "keep_rendered.h" #include "target_overlap.h" #include "host_shaders/block_linear_unswizzle_2d_comp_spv.h" @@ -174,6 +176,10 @@ struct KeyHash { } }; std::unordered_map, KeyHash> textures; +// (Triforce, texture detail: the keys made whole whatever the card's detail level, for the session, and the textures asked +// whole since the last sweep: texture_want_whole) +std::unordered_set whole_keys; +std::vector whole_pending; std::unordered_multimap by_gpu; uint64_t frame_no = 1; uint64_t total_bytes = 0; @@ -1774,9 +1780,11 @@ bool texture_dirty(Texture& tex, uint32_t gen, Ranges& out, bool same_ok = false } uint64_t destroy_gen = 1; // (bumped when textures go: what was resolved to them goes stale) +texture_gone::Record gone_record; // (which ones went: texture_destroyed_since, for a pointer kept across draws) void destroy_later(Texture& tex) { destroy_gen++; + gone_record.went(tex.id); Dead d{tex.last_value, tex.image, tex.allocation, {}, {}}; if (tex.pool_key && tex.allocation && tex.sparse_chunks.empty() && pool_cap()) { // (a render target's: to the pool, its views too) d.pool_key = tex.pool_key, d.view_map = std::move(tex.views), d.bytes = tex.bytes + tex.pad; @@ -1851,6 +1859,7 @@ Texture* create(const Key& key, const nvn_tex* t, VkFormat vf, bool astc, int bw const uint32_t max_dim = std::max({x.width, x.height, x.depth}); const uint32_t max_levels = 1 + (uint32_t)std::floor(std::log2((double)max_dim)); x.levels = nvn_tex_is_linear(&d) ? 1 : std::clamp(d.levels, 1, max_levels); + bool by_detail = false; // (its top levels are left out by the card's texture detail alone: tex_detail.h) { // huge textures leave out their top levels: wider than RX_VK_TEX_MAX_DIM (default 2048) once they would take // 64 MB, and down to RX_VK_TEX_MAX_MB (default 512) whatever their size (MKW's terrain material arrays are // 8192 x 8192 x 68 ASTC: 24 GB as RGBA8) @@ -1888,6 +1897,30 @@ Texture* create(const Key& key, const nvn_tex* t, VkFormat vf, bool astc, int bw logf("vk: texture %#llx %ux%ux%u x%u (NVN %#x, %llu MB): its %u top level(s) left out (%llu MB)", (unsigned long long)t->gpu, x.width, x.height, x.depth, x.layers, d.format, (unsigned long long)(bytes_at(0) >> 20), x.skip, (unsigned long long)(bytes_at(x.skip) >> 20)); + } + // (texture detail, tex_detail.h: on a card the driver allows under 10 GB a material texture is made without its + // largest level, under 6 GB without two. Only a decoded ASTC texture the game samples: 2D or a 2D array, one + // sample, mipmapped, 256 texels or more each way, not one first met as a render target or a copy's destination. + // RX_VK_TEX_DETAIL=full|reduced|low says so on any card; read at the first texture) + // (nor one a shader reads by texel position or asks the size of: whole_keys, texture_want_whole) + const auto detail_level = [] { return texture_detail_level(); }; + if (!as_target && astc && !x.sparse && x.type == VK_IMAGE_TYPE_2D && d.samples <= 1 && + (d.target == NVN_TEXTURE_TARGET_2D || d.target == NVN_TEXTURE_TARGET_2D_ARRAY) && !whole_keys.count(key)) { + const uint32_t by = tex_detail::skip(detail_level(), x.width, x.height, x.levels); + if (by > x.skip) { + x.detail_skip = true; + static std::atomic n{0}, whole{0}, kept{0}; + by_detail = !x.skip; + const uint64_t c = ++n; + whole += bytes_at(0) >> 2, kept += bytes_at(by) >> 2; // (texels) + x.skip = by; + if (c >= 64 && (c & (c - 1)) == 0) + logf("vk: texture detail: %llu textures made without their largest level(s) so far, %llu M texels " + "kept of %llu M", (unsigned long long)c, (unsigned long long)(kept.load() >> 20), + (unsigned long long)(whole.load() >> 20)); + } + } + if (x.skip) { x.width = std::max(1u, x.width >> x.skip), x.height = std::max(1u, x.height >> x.skip); x.depth = std::max(1u, x.depth >> x.skip); x.levels -= x.skip; @@ -1939,7 +1972,8 @@ Texture* create(const Key& key, const nvn_tex* t, VkFormat vf, bool astc, int bw return false; }(); static const bool rgba8_instead = getenv("RX_VK_UNEVEN_RGBA8") != nullptr; - const bool bc_candidate = astc && !(x.skip && x.layers == 1) && x.type == VK_IMAGE_TYPE_2D && + // (one whose levels the texture detail left out is BC3 all the same: a copy into a level it lacks is left out) + const bool bc_candidate = astc && !(x.skip && x.layers == 1 && !by_detail) && x.type == VK_IMAGE_TYPE_2D && x.samples == VK_SAMPLE_COUNT_1_BIT && x.width <= kBcDim && x.height <= kBcDim && px * x.layers * 4 >= min_bytes; if (bc_candidate && uneven && amd_fix) { @@ -2296,19 +2330,34 @@ unsigned view_layer(const nvn_view* v) { return v && v->set_layers ? v->min_layer : 0; } -Texture* texture_get(const nvn_tex* t, bool for_sampling) { - PROF_HOT(PROF_TEXGET); - if (!t || !t->gpu || t->d.target == NVN_TEXTURE_TARGET_BUFFER) return nullptr; - const VkFormatInfo fi = vk_format(t->d.format); - if (fi.format == VK_FORMAT_UNDEFINED) { - static int seen[256]; - if (t->d.format >= 0 && t->d.format < 256 && seen[t->d.format]++ < 2) - logf("vk: NVN format %#x has no Vulkan format: its textures are left out", t->d.format); - return nullptr; - } - int bw = 1, bh = 1; - const int bpb = nvn_block_size(t->d.format, &bw, &bh); - if (bpb <= 0) return nullptr; +int texture_detail_level() { + static const int level = [] { + const int asked = tex_detail::parse(getenv("RX_VK_TEX_DETAIL")); + uint64_t used = 0, allowed = 0; + const bool known = vram_usage(&used, &allowed); + const bool integrated = dev.props.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU; + const int l = asked >= 0 ? asked : tex_detail::by_vram(known ? allowed : 0, integrated); + if (asked >= 0) + logf("vk: texture detail: %s (RX_VK_TEX_DETAIL)", tex_detail::name(l)); + else if (l) + logf("vk: texture detail: %s, for the %llu MB of VRAM allowed here%s (full from %llu MB, low under %llu; " + "RX_VK_TEX_DETAIL=full|reduced|low says otherwise)", tex_detail::name(l), + (unsigned long long)(allowed >> 20), integrated ? " on an integrated GPU" : "", + (unsigned long long)tex_detail::kFullMb, (unsigned long long)tex_detail::kLowMb); + return l; + }(); + return level; +} +void texture_want_whole(Texture* tex) { + if (!tex || !tex->detail_skip || tex->whole_asked) return; + ParLock lk; + if (tex->whole_asked) return; + tex->whole_asked = true; + whole_pending.push_back(tex); +} + +/// The image the game's texture has: its storage and what makes two textures over one storage different images +static Key key_of(const nvn_tex* t, const VkFormatInfo& fi, int bpb) { Key k{}; k.gpu = t->gpu; k.target = (uint32_t)t->d.target; @@ -2324,6 +2373,23 @@ Texture* texture_get(const nvn_tex* t, bool for_sampling) { k.layout = nvn_tex_is_linear(&t->d) ? 0x80000000u | (t->d.stride ? t->d.stride : host->tex_linear_stride(&t->d)) : t->L.tileH | t->L.tileD << 4; k.cls = format_class(fi.format, t->d.format, bpb, fi.astc); + return k; +} + +Texture* texture_get(const nvn_tex* t, bool for_sampling) { + PROF_HOT(PROF_TEXGET); + if (!t || !t->gpu || t->d.target == NVN_TEXTURE_TARGET_BUFFER) return nullptr; + const VkFormatInfo fi = vk_format(t->d.format); + if (fi.format == VK_FORMAT_UNDEFINED) { + static int seen[256]; + if (t->d.format >= 0 && t->d.format < 256 && seen[t->d.format]++ < 2) + logf("vk: NVN format %#x has no Vulkan format: its textures are left out", t->d.format); + return nullptr; + } + int bw = 1, bh = 1; + const int bpb = nvn_block_size(t->d.format, &bw, &bh); + if (bpb <= 0) return nullptr; + const Key k = key_of(t, fi, bpb); Texture* tex; auto it = textures.find(k); if (it == textures.end()) { @@ -2349,6 +2415,14 @@ uint64_t texture_destroy_generation() { return destroy_gen; } +uint64_t texture_destroyed_count() { + return gone_record.count(); +} + +bool texture_destroyed_since(uint32_t id, uint64_t since) { + return gone_record.gone_since(id, since); +} + uint32_t texture_epoch() { return epoch; } @@ -4771,6 +4845,27 @@ void texture_sweep(uint64_t completed, bool sweep, uint64_t budget, uint64_t evi } run(); } + // (Triforce, texture detail: the ones a shader reads by texel position go now, whatever their age, and their keys are + // made whole from here on: the next draw that binds one makes it again from the game's memory, every level. A pointer + // is only compared here: its texture may have gone since it was asked) + if (!whole_pending.empty()) { + static uint64_t n_whole, logged_at; + for (Texture* want : whole_pending) + for (auto& kv : textures) + if (kv.second.get() == want && want->detail_skip && want->whole_asked) { + whole_keys.insert(kv.first); + if (std::none_of(evict.begin(), evict.end(), [&](const Evict& e) { return e.tex == want; })) + evict.push_back({want, false, false}); + n_whole++; + break; + } + whole_pending.clear(); + if (n_whole >= logged_at * 2 + 1) { + logged_at = n_whole; + logf("vk: texture detail: %llu textures kept whole so far: a shader reads them by texel position or asks " + "their size", (unsigned long long)n_whole); + } + } for (const Evict& e : evict) { // (and each one's bookkeeping and its end, as before) Texture* tex = e.tex; if (e.shifted) n_dead++, b_dead += tex->bytes, shifted_now++; @@ -4862,8 +4957,38 @@ void texture_released(const nvn_tex* t) { if (!t) return; auto [a, b] = by_gpu.equal_range(t->gpu); for (auto it = a; it != b; ++it) it->second->let_go = std::max(frame_no, 1); // (texture_sweep: first to go while VRAM is short) + // (Triforce, a lab switch, off: RX_VK_RELEASE_OWN=1 finalizes only the image the released texture has, NX2navi's rule + // (there a release that finalized every image at the storage left terrain height maps drawn at height zero). Not + // the default: this renderer keeps an image for each size and format of a storage, and in the lab's first minute of + // play 32,767 images were spared this way, most of them at the start of one pool, render targets of other sizes + // that only such a release ever marks as let go. What that does to memory and to the write-backs before eviction + // has not been measured (the one run with it on was slow for another reason). The count of the others spared is + // logged as it doubles) + static const bool own_only = getenv("RX_VK_RELEASE_OWN") && atoi(getenv("RX_VK_RELEASE_OWN")) != 0; + const Texture* own = nullptr; + bool own_known = false; + if (own_only) { + const VkFormatInfo fi = vk_format(t->d.format); + int bw = 1, bh = 1; + const int bpb = fi.format != VK_FORMAT_UNDEFINED ? nvn_block_size(t->d.format, &bw, &bh) : 0; + if (bpb > 0) { + const auto it = textures.find(key_of(t, fi, bpb)); + own_known = true, own = it == textures.end() ? nullptr : it->second.get(); + } + } for (auto it = a; it != b; ++it) if (it->second->gpu_written && !it->second->released) { + if (own_known && it->second != own) { + static uint64_t spared, told_at; + if (++spared >= told_at * 2 + 1) { + told_at = spared; + logf("vk: a texture let go at %#llx (%ux%u NVN %#x): another texture's image there, #%u %ux%u NVN %#x, " + "which the GPU drew, stays as it is (%llu such so far)", (unsigned long long)t->gpu, t->d.width, + t->d.height, t->d.format, it->second->id, it->second->width, it->second->height, + it->second->nvn_format, (unsigned long long)spared); + } + continue; + } it->second->released = true, it->second->release_gen = memory_generation(); watch_tex(it->second, "finalized"); static int logged; // (a big one: the course maps' kind) diff --git a/gpu/vk/vk_texture.h b/gpu/vk/vk_texture.h index 6b2bdf3..8099eb3 100644 --- a/gpu/vk/vk_texture.h +++ b/gpu/vk/vk_texture.h @@ -29,6 +29,8 @@ struct Texture { VkImageCreateFlags create_flags = 0; // (hookshot: as its image was made, for the compaction's copy of it) uint32_t width = 1, height = 1, depth = 1, levels = 1, layers = 1; // the image's (after skip) uint32_t skip = 0; // top levels of the texture left out (huge textures: RX_VK_TEX_MAX_DIM, _MAX_MB) + bool detail_skip = false; // (Triforce) levels of it are left out by the card's texture detail (tex_detail.h) + bool whole_asked = false; // (and a shader reads it by texel position: it goes, and is made whole: texture_want_whole) // (a huge array kept whole as a sparse resident image instead: each layer gets device memory when it is first // written, so only what the game streams in costs VRAM. MKW's terrain arrays, 8192 x 8192 x 68 BC6H: 5.8 GB // each, written by the GPU's copies into level 0, which a texture with its top level left out lost) @@ -150,6 +152,17 @@ int texture_segment_end(int index); uint32_t texture_epoch(); /// Changes whenever a texture is destroyed (pointers to textures kept elsewhere go stale) uint64_t texture_destroy_generation(); +/// (Triforce) The texture detail in effect: 0 full, 1 reduced, 2 low (tex_detail.h; chosen at the first call, which needs +/// the device and its allocator) +int texture_detail_level(); +/// (Triforce) A shader reads the texture by texel position or asks for its size (spv_exact.h) and the texture detail left +/// levels of it out: it is let go at the next sweep and made whole from then on. Any thread of the replay. +void texture_want_whole(Texture* tex); +/// One texture's own end, where the generation above is too wide (any texture's end changes it, and textures go in most +/// frames while the video memory is full): how many textures were destroyed so far, and whether the one with this creation +/// number (Texture::id) was, since a count taken while it was there. True too when it cannot be told (texture_gone.h) +uint64_t texture_destroyed_count(); +bool texture_destroyed_since(uint32_t id, uint64_t since); /// The texture an NVN texture's storage is (made and uploaded at first use, uploaded again when the CPU wrote its /// storage since). null for formats and targets that have no image (buffers, unknown formats). Texture* texture_get(const nvn_tex* t, bool for_sampling); diff --git a/host/discord_rpc.c b/host/discord_rpc.c index 12c5b12..ff39afc 100644 --- a/host/discord_rpc.c +++ b/host/discord_rpc.c @@ -35,8 +35,9 @@ #include "../gpu/vk/vk_api.h" -#define HOOKSHOT_DISCORD_APP_ID_DEFAULT "" /* (Triforce: no Discord application of its own, so no status unless the player names one: upstream Hookshot's id would announce "Playing Hookshot") */ -#define HOOKSHOT_DISCORD_IMAGE_DEFAULT "hookshot" /* (its large-image art asset key) */ +#define HOOKSHOT_DISCORD_APP_ID_DEFAULT "1571462295710597261" /* (Triforce's own Discord application, made 11-15: "Playing Triforce" and its logo. An application id is public: it is in every status the client sends) */ +/* (its large-image art asset key: the name the owner's logo has on Discord's side, under Rich Presence > Art Assets) */ +#define HOOKSHOT_DISCORD_IMAGE_DEFAULT "triangle_emblem_discord_zoomed_out_transparent" void hos_log(const char *fmt, ...); int rx_option(int id, int otherwise); @@ -224,9 +225,9 @@ static int send_activity(Ipc p, long long started) { snprintf(version, sizeof version, "%s", hos_version()); version[strcspn(version, " +")] = 0; /* ("v2.0.12 (source ...)": the release alone) */ snprintf(json, sizeof json, - "{\"cmd\":\"SET_ACTIVITY\",\"nonce\":\"%u\",\"args\":{\"pid\":%lu,\"activity\":{\"details\":\"Ocarina of Time\"," - "\"state\":\"Hookshot %s\",\"timestamps\":{\"start\":%lld},\"assets\":{\"large_image\":\"%s\",\"large_text\":" - "\"Hookshot %s\"}}}}", + "{\"cmd\":\"SET_ACTIVITY\",\"nonce\":\"%u\",\"args\":{\"pid\":%lu,\"activity\":{\"details\":\"Triforce %s\"," + "\"timestamps\":{\"start\":%lld},\"assets\":{\"large_image\":\"%s\",\"large_text\":" + "\"Triforce %s\"}}}}", ++nonce, process_id(), version, started, HOOKSHOT_DISCORD_IMAGE_DEFAULT, version); if (!write_frame(p, OP_FRAME, json)) return 0; uint32_t op = 0; diff --git a/host/hos_main.c b/host/hos_main.c index 76ca130..ac94194 100644 --- a/host/hos_main.c +++ b/host/hos_main.c @@ -83,14 +83,17 @@ extern const uint64_t hos_entry_init_libc0, hos_entry_init_libc1, hos_entry_init /* nn_fs.c */ extern const char *hos_romfs_dir, *hos_user_dir; -/* HOS_LARGE_PAGES=1 (experiment): the guest window in 2 MB pages, for fewer TLB misses (the game touches hundreds of MB - of scattered heap; 4 KB pages cover only ~12 MB of it in the CPU's TLB). The whole window is then physical memory at - once (4 GB, locked), and Windows needs the account to hold "Lock pages in memory" (SeLockMemoryPrivilege, Local - Security Policy > User Rights Assignment; log off and on after): without it this logs why and uses normal pages. */ -int hos_large_pages; /* (hos_mem.c: freed pages are not discarded then) */ +/* HOS_LARGE_PAGES=1 or = (experiment): the game's heap in 2 MB pages, for fewer TLB misses (the game's threads spend + their time on loads from a heap of 6.9 GB; 4 KB pages cover 8 MB of it in the CPU's second-level TLB, 2 MB pages 4 GB). + (triforce: the heap alone, from HOS_HEAP_BASE up: the window is 512 GB here and large pages are physical memory at once. + OoT asks for 6,894 MB; =1 takes 6,912.) That much is then physical memory from the start, locked, and Windows needs the + account to hold "Lock pages in memory" (SeLockMemoryPrivilege; log off and on after it is granted): without it, or + when Windows cannot find the memory in one piece, this logs why and uses normal pages. */ +int hos_large_pages; /* (hos_mem.c: freed pages are not discarded there) */ +uint64_t hos_large_lo, hos_large_hi; /* (the guest range in large pages: mkw_glue.c hos_commit, hos_mem.c) */ #if defined(_WIN32) && !defined(A64_MEM_ZERO) #pragma comment(lib, "advapi32.lib") /* (the token privilege calls) */ -static void *reserve_large(SIZE_T size) { +static void *reserve_large(void *at, SIZE_T size) { HANDLE token; if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token)) return NULL; TOKEN_PRIVILEGES tp = {0}; @@ -107,7 +110,7 @@ static void *reserve_large(SIZE_T size) { } const SIZE_T large = GetLargePageMinimum(); if (!large || size % large) return NULL; - void *p = VirtualAlloc((void *)A64_HOST_BASE, size, MEM_RESERVE | MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE); + void *p = VirtualAlloc(at, size, MEM_RESERVE | MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE); if (!p) hos_log("mem: HOS_LARGE_PAGES: %llu MB of large pages not available (error %lu): normal pages", (unsigned long long)(size >> 20), GetLastError()); return p; @@ -127,10 +130,26 @@ static int reserve_guest(void) { pages changed (GetWriteWatch) so that it uploads only those (soft write watch builds mark pages themselves). */ #if defined(A64_WRITE_TRACK) const DWORD watch = 0; - if (getenv("HOS_LARGE_PAGES") && reserve_large(GUEST_SPACE) == (void *)A64_HOST_BASE) { - hos_large_pages = 1; - hos_log("mem: the guest window is in large pages (%llu MB)", (unsigned long long)(GUEST_SPACE >> 20)); - return 1; + if (getenv("HOS_LARGE_PAGES")) { + /* three reservations: below the heap, the heap's large pages, above them (a commit never spans two: hos_commit) */ + uint64_t mb = strtoull(getenv("HOS_LARGE_PAGES"), NULL, 10); + if (mb < 64) mb = 6912; + const uint64_t n = ((mb << 20) + 0x1FFFFFULL) & ~0x1FFFFFULL; + uint8_t *const base = (uint8_t *)A64_HOST_BASE; + const ULONGLONG t0 = GetTickCount64(); + if (HOS_HEAP_BASE + n < GUEST_SPACE && VirtualAlloc(base, HOS_HEAP_BASE, MEM_RESERVE, PAGE_NOACCESS) == base) { + if (reserve_large(base + HOS_HEAP_BASE, (SIZE_T)n) == base + HOS_HEAP_BASE && + VirtualAlloc(base + HOS_HEAP_BASE + n, GUEST_SPACE - HOS_HEAP_BASE - n, MEM_RESERVE, PAGE_NOACCESS)) { + void hos_commit_init(void); + hos_commit_init(); + hos_large_lo = HOS_HEAP_BASE, hos_large_hi = HOS_HEAP_BASE + n, hos_large_pages = 1; + hos_log("mem: the game's heap is in large pages (%llu MB at %#llx, made in %llu ms)", (unsigned long long)(n >> 20), + (unsigned long long)HOS_HEAP_BASE, (unsigned long long)(GetTickCount64() - t0)); + return 1; + } + VirtualFree(base + HOS_HEAP_BASE, 0, MEM_RELEASE); + VirtualFree(base, 0, MEM_RELEASE); + } } #else const DWORD watch = MEM_WRITE_WATCH; diff --git a/host/hos_mem.c b/host/hos_mem.c index b30c7b2..7cf957e 100644 --- a/host/hos_mem.c +++ b/host/hos_mem.c @@ -130,8 +130,8 @@ static void discard_pages(uint64_t addr, uint64_t size) { if (keep < 0) keep = getenv("HOS_KEEP_FREED_PAGES") != NULL; if (hi <= lo || keep) return; #if defined(_WIN32) && !defined(__EMSCRIPTEN__) - extern int hos_large_pages; /* (hos_main.c: large pages cannot be discarded) */ - if (hos_large_pages) return; + extern uint64_t hos_large_lo, hos_large_hi; /* (hos_main.c: large pages cannot be discarded) */ + if (hos_large_hi && lo < hos_large_hi && hi > hos_large_lo) return; typedef DWORD(WINAPI * DiscardFn)(PVOID, SIZE_T); static DiscardFn discard; static int looked; diff --git a/host/hos_relaunch.c b/host/hos_relaunch.c index 824ee57..beb74bb 100644 --- a/host/hos_relaunch.c +++ b/host/hos_relaunch.c @@ -9,6 +9,7 @@ Windows: a new process of this executable (GetModuleFileNameW) with this one's command line (GetCommandLineW), in the same folder and environment; then this one exits. */ #include "hos.h" +#include "../gpu/vk/vk_api.h" /* (HOS_OPT_EXITING) */ #include #include #include @@ -90,6 +91,10 @@ void hos_window_closed_exit(void) { const int again = relaunch_asked != 0; hos_log(again ? "display: window closed for a restart (the Insert menu's Restart now)" : "display: window closed, exiting"); hos_log_flush(); + { void hos_fs_save_finish(void); hos_fs_save_finish(); } /* (host/nn_fs.c: a save still being committed is finished first) */ + /* (Triforce) the renderer is told: NVIDIA's Streamline is shut down in order, where leaving it to the DLLs' unloading + crashed inside nvngx_dlssg.dll at every exit with DLSS frame generation on */ + { void rx_option_set(int id, int value); rx_option_set(HOS_OPT_EXITING, 1); } if (again) relaunch(); ExitProcess(0); } diff --git a/host/hos_thread.c b/host/hos_thread.c index 0fe3ba7..db83214 100644 --- a/host/hos_thread.c +++ b/host/hos_thread.c @@ -1188,6 +1188,16 @@ int hos_vcores_on(void) { static int vc_cores(void) { return hos_cores() > 8 ? 8 : hos_cores(); } static unsigned vc_allowed(HosThread *t) { const unsigned all = (1u << vc_cores()) - 1; + /* (Triforce, 11-14, the lab) HOS_VCORE_ANY=1: a thread takes any free core, not only the ones its affinity mask names. + What the game needs of the cores is that no two threads run under one core number at a time (its per-core data has no + locks); the masks are the console's placement. OoT pins nearly every thread to one core (six BaseProc executors, five + module workers, the main and Presentation threads on six cores), and here a thread waits for ITS core while others + stand free: at the Market ~61 waits a frame of ~0.1 ms each, 7 ms of waiting a frame summed over the threads, on the + path the frame now waits for (the game's own CPU work, 13-14 ms a frame with the replay on its own thread). The risk: + a thread that reads its core number, blocks, and goes on under another */ + static int any = -1; + if (any < 0) any = getenv("HOS_VCORE_ANY") && atoi(getenv("HOS_VCORE_ANY")) != 0; + if (any) return all; unsigned m = (unsigned)(t->core_mask & all); if (!m) m = 1u << hos_thread_core(t); return m; diff --git a/host/mf_av1.c b/host/mf_av1.c index b6af1d3..48139be 100644 --- a/host/mf_av1.c +++ b/host/mf_av1.c @@ -88,9 +88,13 @@ static void av1_missing(void) { CreateThread(NULL, 0, av1_missing_notice, NULL, 0, NULL); } -void *mf_av1_open(int w, int h) { +/* tell: say so (the log, the notice) when Windows has no decoder; 0 when the caller has another one to use */ +void *mf_av1_open(int w, int h, int tell) { if (!com_ready()) return NULL; - if (getenv("HOS_AV1_MISSING")) { av1_missing(); return NULL; } + if (getenv("HOS_AV1_MISSING")) { + if (tell) av1_missing(); + return NULL; + } MFT_REGISTER_TYPE_INFO in = {MFMediaType_Video, MFVideoFormat_AV1}; IMFActivate **acts = NULL; UINT32 n = 0; @@ -119,7 +123,10 @@ void *mf_av1_open(int w, int h) { CoTaskMemFree(acts); acts = NULL, n = 0; } - if (!mft) { av1_missing(); return NULL; } + if (!mft) { + if (tell) av1_missing(); + return NULL; + } IMFMediaType *t = NULL; MFCreateMediaType(&t); IMFMediaType_SetGUID(t, &MF_MT_MAJOR_TYPE, &MFMediaType_Video); diff --git a/host/mkw_glue.c b/host/mkw_glue.c index 51657bd..040276b 100644 --- a/host/mkw_glue.c +++ b/host/mkw_glue.c @@ -21,7 +21,16 @@ void hos_commit(uint64_t addr, uint64_t size) { hos_log("mem: commit of %#llx bytes at %#llx: outside the guest window", (unsigned long long)size, (unsigned long long)addr); return; } - const uint64_t lo = addr & ~0xFFFFULL, hi = (addr + size + 0xFFFF) & ~0xFFFFULL; + uint64_t lo = addr & ~0xFFFFULL, hi = (addr + size + 0xFFFF) & ~0xFFFFULL; + { /* (HOS_LARGE_PAGES: the heap's large pages are a reservation of their own, committed whole; what lies below or + above them is committed by itself) */ + extern uint64_t hos_large_lo, hos_large_hi; + if (hos_large_hi && lo < hos_large_hi && hi > hos_large_lo) { + if (lo < hos_large_lo) hos_commit(lo, hos_large_lo - lo); + if (hi <= hos_large_hi) return; + lo = hos_large_hi; + } + } if (!VirtualAlloc(GP(lo), (SIZE_T)(hi - lo), MEM_COMMIT, PAGE_READWRITE)) { const DWORD err = GetLastError(); hos_log("mem: commit of %#llx bytes at %#llx failed (error %lu)", (unsigned long long)(hi - lo), diff --git a/host/nn_applet.c b/host/nn_applet.c index 4d98ecf..3a98473 100644 --- a/host/nn_applet.c +++ b/host/nn_applet.c @@ -137,7 +137,12 @@ uint32_t hos_output_size(int height) { Castle Town's guard-house conversation/camera test crashed with 305 MB at 2560 x 1440 and passed with 512 MB. Keep at least 512 MB by default; larger scaled pools and an explicit HOS_RT_POOL_MB= still win. The 5120 x 2880 output's pool is kept to 900 MB: scaled, it is 1220 MB, and 1220 and 1000 MB both failed the game's own reservation at boot; at 900 - MB it boots and plays with the 3D at the full 5120 x 2880. Every other output keeps its whole scaled pool. */ + MB it boots and plays with the 3D at the full 5120 x 2880. Every other output keeps its whole scaled pool. + That 900 MB is the most for the whole pool there, the RGBA16F scene's 128 MB counted in: the 5120 x 2880 output with RenoDX + HDR asked 900 + 128 = 1028 MB, and the game stopped at boot in f_83b7b340, where the "Graphics" heap is made larger by as + much as the pool (nx2/oot_patches.py): a null pointer (a player's log, v0.3.7). With the 900 MB, 11-15 on an RTX 4070 SUPER: that + output with RenoDX HDR boots and played in the world, 17,160 presents at about 50 fps, RenoDX's four shaders in place, no + error from the pool (one short session, not every area; its scene target and each copy are 118 MB there). */ uint64_t hos_rt_pool_size(uint64_t game) { const char *e = getenv("HOS_RT_POOL_MB"); extern volatile uint64_t hos_wide_out; @@ -150,9 +155,16 @@ uint64_t hos_rt_pool_size(uint64_t game) { extern int hos_scene_rgba16f(void); /* (local, RenoDX port) the scene's colour target at 8 bytes a pixel in place of 4: room for it and its copies, at 3840 x 2160 33 MB each */ const uint64_t rgba16f = hos_scene_rgba16f() ? 128ULL << 20 : 0; - const uint64_t v = (e ? strtoull(e, NULL, 0) << 20 : scaled < minimum ? minimum : is_five_k && scaled > five_k ? five_k : scaled) + (e ? 0 : rgba16f); + const uint64_t asked = (e ? strtoull(e, NULL, 0) << 20 : scaled < minimum ? minimum : is_five_k && scaled > five_k ? five_k : scaled) + (e ? 0 : rgba16f); + /* (the 5120 x 2880 output: 900 MB for the whole pool, see above; an explicit HOS_RT_POOL_MB still wins) */ + const uint64_t v = !e && is_five_k && asked > five_k ? five_k : asked; static int logged; - if (!logged++) hos_log("applet: render target pool %llu MB (the game's %llu MB)", (unsigned long long)(v >> 20), (unsigned long long)(game >> 20)); + if (!logged++) { + hos_log("applet: render target pool %llu MB (the game's %llu MB)", (unsigned long long)(v >> 20), (unsigned long long)(game >> 20)); + if (v != asked) + hos_log("applet: render target pool: %llu MB asked (the 5120x2880 output with an RGBA16F scene), kept to %llu MB: more fails the game's reservation at boot", + (unsigned long long)(asked >> 20), (unsigned long long)(v >> 20)); + } return v; } HLE(_ZN2nn2oe16GetOperationModeEv) { RET(handheld() ? 0 : 1); } /* OperationMode_Handheld / _Console (docked) */ diff --git a/host/nn_audio.c b/host/nn_audio.c index e16a049..2b61b6b 100644 --- a/host/nn_audio.c +++ b/host/nn_audio.c @@ -32,6 +32,7 @@ #else #include "hos_sdl.h" #endif +#include "../gpu/vk/vk_api.h" /* (HOS_OPT_VOLUME: the menu's Master volume) */ #include "hos_paths.h" /* (the game folder: triforce_options.ini, au_menu_output) */ void hos_system_event_init(uint64_t key, int autoclear); @@ -454,6 +455,19 @@ static int mix_depth(int i, int guard) { /* how many sub mixes lie between thi return 1 + mix_depth(mixes[i].dest, guard + 1); } +/* (Triforce) The menu's Master volume as a gain, 0 to 1: the row while the renderer answers, the options file's + master_volume= before it does (the title's first sounds), 1 without either. HOS_AUDIO_VOLUME=<0-100> over both */ +int rx_option(int id, int otherwise); +int hos_ini_option(const char *key, int otherwise); +static float master_volume(void) { + static int from_file = -1, from_env = -2; + if (from_env == -2) from_env = getenv("HOS_AUDIO_VOLUME") ? atoi(getenv("HOS_AUDIO_VOLUME")) : -1; + if (from_file < 0) from_file = hos_ini_option("master_volume", 100); + int v = from_env >= 0 ? from_env : rx_option(HOS_OPT_VOLUME, from_file); + v = v < 0 ? 0 : v > 100 ? 100 : v; + return (float)v / 100.0f; +} + static void render_frame(int16_t *pcm, int channels) { /* 240 frames of the device's 2 or 6 channels */ for (int m = 0; m < MAX_MIXES; m++) if (mixes[m].used) { @@ -575,6 +589,20 @@ static void render_frame(int16_t *pcm, int channels) { /* 240 frames of the de lim_env = want < lim_env ? want : lim_env + (want - lim_env) * 0.0004f; for (int ch = 0; ch < nch; ch++) out[ch][i] *= lim_in * lim_env * lim_out; } + { /* (Triforce) the Insert menu's Master volume, over everything the game mixed and after the limiter, so the + limiter works on the game's own levels. A change is ramped across the frame (5 ms): no click */ + static float master_now = -1.0f; + const float to = master_volume(); + if (master_now < 0.0f) master_now = to; + if (master_now != 1.0f || to != 1.0f) { + const float step = (to - master_now) / FRAME; + for (int i = 0; i < FRAME; i++) { + const float g = master_now + step * (float)(i + 1); + for (int ch = 0; ch < nch; ch++) out[ch][i] *= g; + } + } + master_now = to; + } for (int i = 0; i < FRAME; i++) for (int ch = 0; ch < nch; ch++) { float a = out[ch][i] * 32767.0f; diff --git a/host/nn_fs.c b/host/nn_fs.c index 573b008..9090d46 100644 --- a/host/nn_fs.c +++ b/host/nn_fs.c @@ -1,6 +1,7 @@ /* nn::fs: mounts map to host folders. rom: the game's RomFS (extracted by tools/nx.py romfs), read-only - save: /save/ (MountSaveData / MountSaveDataReadOnly) + save: /save/ (MountSaveData / MountSaveDataReadOnly); MountSaveData's root is a working copy of it, + /save-tx//work, which Commit makes the save as a transaction (save_tx.h) cache: /cache/ (MountCacheStorage) temp: /temp (MountTemporaryStorage) hos_romfs_dir and hos_user_dir are set by hos_main.c. Reads are positional (ReadFile with an offset), so any number @@ -257,10 +258,82 @@ HLE(_ZN2nn2fs18IsSaveDataExistingENS_13ApplicationIdERKNS_7account3UidE) { (unsigned long long)G64(ARG(1))); RET(GetFileAttributesA(root) != INVALID_FILE_ATTRIBUTES); } +/* (triforce) The save as a transaction (save_tx.h): MountSaveData mounts a working copy of the save, and Commit makes that + copy the save, whole or not at all, with the saves it replaces kept as backups. Commit's first part (the copy of the + working copy as it is then) runs on the game's thread; the second (written out to the disk, put in place, the backup) on a + thread of its own, which the next Commit, a mount of the save and the game's end wait for. */ +#include "save_tx.h" +typedef struct { char name[32]; char uid[40]; SaveTxPaths p; } TxMount; +static TxMount tx_mounts[4]; +static int ntx; +static SRWLOCK tx_lock = SRWLOCK_INIT; /* (tx_mounts, tx_thread and every step of a transaction) */ +static HANDLE tx_thread; /* (the thread finishing the last Commit, or NULL) */ +static SaveTxPaths tx_finish_paths; + +static DWORD WINAPI tx_finish_main(LPVOID arg) { + (void)arg; + char backup[HOS_FS_PATH]; + const ULONGLONG t0 = GetTickCount64(); + const int r = save_tx_finish(&tx_finish_paths, backup, sizeof backup); + if (r != 1) hos_log("fs: the save could not be committed as a transaction: its files were %s", r == 0 ? "copied over the save's instead" : "NOT saved (the disk?)"); + else if (backup[0]) hos_log("fs: save committed (%llu ms on its thread); the save before it is kept in %s", (unsigned long long)(GetTickCount64() - t0), backup); + else if (hos_trace || getenv("HOS_FS_TRACE")) hos_log("fs: save committed (%llu ms on its thread)", (unsigned long long)(GetTickCount64() - t0)); + return 0; +} +/* (with tx_lock held) the thread of the last Commit has ended */ +static void tx_wait_locked(void) { + if (!tx_thread) return; + WaitForSingleObject(tx_thread, INFINITE); + CloseHandle(tx_thread); + tx_thread = NULL; +} +/* The game ends (host/hos_relaunch.c, and at exit): a Commit still on its thread is finished first */ +void hos_fs_save_finish(void) { + AcquireSRWLockExclusive(&tx_lock); + tx_wait_locked(); + ReleaseSRWLockExclusive(&tx_lock); +} +/* MountSaveData's root for uid: the working copy (1, in root), or 0: the save folder itself, as before */ +static int tx_mount(const char *name, const char *uid, char *root, size_t cap) { + if (!save_tx_enabled()) return 0; + static int at_exit; + int ok = 0; + char note[256] = ""; + AcquireSRWLockExclusive(&tx_lock); + if (!at_exit) at_exit = 1, atexit(hos_fs_save_finish); + tx_wait_locked(); + int i; + for (i = 0; i < ntx && strcmp(tx_mounts[i].uid, uid); i++) {} + if (i < ntx) { /* (mounted already under another name: the same working copy, no second recovery under the game's files) */ + if (ntx < 4) { + tx_mounts[ntx] = tx_mounts[i]; + snprintf(tx_mounts[ntx].name, sizeof tx_mounts[ntx].name, "%s", name); + snprintf(root, cap, "%s", tx_mounts[ntx++].p.work); + ok = 1; + } + } else if (ntx < 4) { + TxMount *m = &tx_mounts[ntx]; + save_tx_paths(&m->p, hos_user_dir, uid); + const int away = hos_vcore_away(); + ok = save_tx_mount(&m->p, note, sizeof note); + if (away) hos_vcore_back(); + if (ok) { + snprintf(m->name, sizeof m->name, "%s", name); + snprintf(m->uid, sizeof m->uid, "%s", uid); + snprintf(root, cap, "%s", m->p.work); + ntx++; + } + } + ReleaseSRWLockExclusive(&tx_lock); + if (note[0]) hos_log("fs: save: %s", note); + if (!ok) hos_log("fs: save: no working copy could be made: the save folder itself is mounted, and saves are written in place"); + return ok; +} /* MountSaveData(char const*, account::Uid const&) */ HLE(_ZN2nn2fs13MountSaveDataEPKcRKNS_7account3UidE) { - char sub[64]; + char sub[64], root[HOS_FS_PATH]; snprintf(sub, sizeof sub, "save\\%016llx%016llx", (unsigned long long)G64(ARG(1) + 8), (unsigned long long)G64(ARG(1))); + if (tx_mount(gstr(ARG(0)), sub + 5, root, sizeof root)) { RET(mount(gstr(ARG(0)), root, 1) ? 0 : R_NOT_MOUNTED); return; } RET(user_mount(gstr(ARG(0)), sub) ? 0 : R_NOT_MOUNTED); } /* MountSaveDataReadOnly(char const*, ApplicationId, account::Uid const&) */ @@ -307,8 +380,36 @@ HLE(_ZN2nn2fs7UnmountEPKc) { for (int i = 0; i < nmounts; i++) if (!strcmp(mounts[i].name, gstr(ARG(0)))) { mounts[i] = mounts[--nmounts]; break; } ReleaseSRWLockExclusive(&mount_lock); + AcquireSRWLockExclusive(&tx_lock); /* (the save's working copy stays on the disk until its next mount makes it again) */ + for (int i = 0; i < ntx; i++) + if (!strcmp(tx_mounts[i].name, gstr(ARG(0)))) { tx_mounts[i] = tx_mounts[--ntx]; break; } + ReleaseSRWLockExclusive(&tx_lock); +} +/* nn::fs::Commit(char const*): the save mount's working copy becomes the save (save_tx.h); any other mount, and a save mounted + without a working copy, as before: nothing to do, its files are where they belong */ +HLE(_ZN2nn2fs6CommitEPKc) { + const char *name = gstr(ARG(0)); + AcquireSRWLockExclusive(&tx_lock); + int i; + for (i = 0; i < ntx && strcmp(tx_mounts[i].name, name); i++) {} + if (i < ntx) { + const int away = hos_vcore_away(); + const ULONGLONG t0 = GetTickCount64(); + tx_wait_locked(); /* (the Commit before this one: its staging folder is this one's) */ + const int staged = save_tx_stage(&tx_mounts[i].p); + if (staged == 1) { + tx_finish_paths = tx_mounts[i].p; + tx_thread = CreateThread(NULL, 0, tx_finish_main, NULL, 0, NULL); + if (!tx_thread) tx_finish_main(NULL); /* (no thread: finished here) */ + } else { + hos_log("fs: the save could not be staged for its commit: its files were %s", staged == 0 ? "copied over the save's instead" : "NOT saved (the disk?)"); + } + if (hos_trace || getenv("HOS_FS_TRACE")) hos_log("fs: Commit(%s): staged in %llu ms", name, (unsigned long long)(GetTickCount64() - t0)); + if (away) hos_vcore_back(); + } + ReleaseSRWLockExclusive(&tx_lock); + RET(0); } -HLE(_ZN2nn2fs6CommitEPKc) { RET(0); } HLE(_ZN2nn2fs26SetPriorityOnCurrentThreadENS0_8PriorityE) {} /* ---- files ---------------------------------------------------------------------------------------------------- */ diff --git a/host/nn_hid.c b/host/nn_hid.c index 621bdc1..7e12080 100644 --- a/host/nn_hid.c +++ b/host/nn_hid.c @@ -245,6 +245,11 @@ static int mouse_stick(int32_t *rx, int32_t *ry, int pointing) { read after a pause starts afresh) */ const int64_t ticks = next_tick ? (now - next_tick) / MOUSE_TICK_NS + 1 : 1; double ox = look_x, oy = look_y; + if (!pointing) { /* (Triforce: the Camera speed row is the stick's; the game multiplies this deflection by it too) */ + double hos_camera_speed(void); /* (host/prompts.c) */ + const double cam = hos_camera_speed(); + ox /= cam, oy /= cam; + } const double shown_length = sqrt(ox * ox + oy * oy); if (shown_length > 1) ox /= shown_length, oy /= shown_length; shown[0] = (int32_t)lround(ox * 32767); @@ -944,6 +949,10 @@ static void npad_states(Ctx *c) { } if (connected) { read_input(&buttons, stick); + { /* (Triforce) the sprint button's press is its own while Link plays (host/oot_sprint.c) */ + void hos_oot_sprint_pad(uint64_t *buttons); + hos_oot_sprint_pad(&buttons); + } uint64_t axes = 0; uint64_t flight = 0; int height = 0; diff --git a/host/nn_os_sync.c b/host/nn_os_sync.c index f1c9bf8..d6d6351 100644 --- a/host/nn_os_sync.c +++ b/host/nn_os_sync.c @@ -719,6 +719,19 @@ static int logged_wait(Ctx *c, Obj *e, int64_t timeout_ns, int try_only, const c (unsigned long long)e->key, (unsigned long long)c->x[30]); return r; } + { /* (Triforce, 11-14) HOS_FT: a wait that blocked 0.2 ms or more, with its caller, at its end (nvn_native.c hos_ft_on) */ + int hos_ft_on(void); + double hos_ft_ms(void); + if (!try_only && hos_ft_on()) { + const int64_t f0 = hos_now_ns(); + const int r = event_wait(e, timeout_ns, try_only); + const int64_t took = hos_now_ns() - f0; + if (took >= 200000) + hos_log("ft %.3f waited %.2f ms on %s event %#llx (lr %#llx)", hos_ft_ms(), took / 1e6, what, (unsigned long long)e->key, + (unsigned long long)c->x[30]); + return r; + } + } if (!on || try_only) return event_wait(e, timeout_ns, try_only); const int64_t t0 = hos_now_ns(); const int r = event_wait(e, timeout_ns, try_only); @@ -808,6 +821,11 @@ static void who_note_signal(Ctx *c, uint64_t key) { } HLE(_ZN2nn2os16SignalLightEventEPNS0_14LightEventTypeE) { who_note_signal(c, ARG(0)); + { /* (Triforce, 11-14) HOS_FT: every light event signalled */ + int hos_ft_on(void); + double hos_ft_ms(void); + if (hos_ft_on()) hos_log("ft %.3f signal %#llx (lr %#llx)", hos_ft_ms(), (unsigned long long)ARG(0), (unsigned long long)c->x[30]); + } event_signal(obj(ARG(0), K_EVENT)); } HLE(_ZN2nn2os15ClearLightEventEPNS0_14LightEventTypeE) { event_clear(obj(ARG(0), K_EVENT)); } @@ -933,6 +951,11 @@ static void queue_trace(Obj *q, uint64_t v, const char *what) { /* mode: 0 send (back), 1 jam (front). try: do not block. Returns 1 if done. */ static int queue_put(Obj *q, uint64_t v, int jam, int try_only) { queue_trace(q, v, jam ? "jam" : "send"); + { /* (Triforce, 11-14) HOS_FT: every message sent, by its queue */ + int hos_ft_on(void); + double hos_ft_ms(void); + if (hos_ft_on()) hos_log("ft %.3f send queue %#llx value %#llx", hos_ft_ms(), (unsigned long long)q->key, (unsigned long long)v); + } AcquireSRWLockExclusive(&q->lk); while (q->qlen == q->qcap) { if (try_only) { ReleaseSRWLockExclusive(&q->lk); return 0; } @@ -997,6 +1020,11 @@ HLE(_ZN2nn2os24ReceiveLightMessageQueueEPmPNS0_21LightMessageQueueTypeE) { uint64_t v; queue_get(obj(ARG(1), K_QUEUE), &v, 1, -1); G64(ARG(0)) = v; + { /* (Triforce, 11-14) HOS_FT: a message taken (whose queue it is) */ + int hos_ft_on(void); + double hos_ft_ms(void); + if (hos_ft_on()) hos_log("ft %.3f got queue %#llx value %#llx", hos_ft_ms(), (unsigned long long)ARG(1), (unsigned long long)v); + } } HLE(_ZN2nn2os27TryReceiveLightMessageQueueEPmPNS0_21LightMessageQueueTypeE) { uint64_t v; diff --git a/host/nn_vi.c b/host/nn_vi.c index 8ebfe70..df54aaf 100644 --- a/host/nn_vi.c +++ b/host/nn_vi.c @@ -115,12 +115,16 @@ static int fps_max(void) { static int v = -1; if (v < 0) { /* (the Insert menu's Frame rate cap, frame_rate_cap=: 0 the console's 60, then 90, 120, 144, 165, 240; HOS_FPS_MAX over it) */ - static const int caps[] = {60, 90, 120, 144, 165, 240}; + /* (Triforce, after NX2navi's limits under 60: -1 45, -2 40, -3 30, for a PC or a handheld's display that holds one + of them steadily where 60 swings. The pacing follows the cap and the game's time step is real time (realtime_step), + as when a PC cannot hold 60: the game keeps its speed) */ + static const int caps[] = {60, 90, 120, 144, 165, 240}, low[] = {45, 40, 30}; const int i = hos_ini_option("frame_rate_cap", 0); const char *e = getenv("HOS_FPS_MAX"); - const int want = e ? atoi(e) : caps[i >= 0 && i < 6 ? i : 0]; - v = want > 60 ? (want > 240 ? 240 : want) : 60; + const int want = e ? atoi(e) : i >= 0 && i < 6 ? caps[i] : i < 0 && i >= -3 ? low[-i - 1] : 60; + v = want > 60 ? (want > 240 ? 240 : want) : want >= 20 && want < 60 ? want : 60; if (v > 60) hos_log("vi: frame rate cap %d: pacing, the game's limiter and its time step follow it", v); + if (v < 60) hos_log("vi: frame rate cap %d: pacing follows it, the game keeps real time", v); } return v; } @@ -137,7 +141,8 @@ int hos_vi_native_interval(Ctx *c, int requested) { if (game_interval < 0) game_interval = getenv("HOS_GAME_INTERVAL") && atoi(getenv("HOS_GAME_INTERVAL")) != 0; int rx_option(int id, int otherwise); const int clamped = requested < 1 ? 1 : requested > 4 ? 4 : requested; - present_interval = game_interval || !rx_option(0, 1) ? clamped : 1; + /* (a cap under 60 paces by itself: with the game's 2 vsyncs on top a cap of 30 would show 15) */ + present_interval = (game_interval || !rx_option(0, 1)) && fps_max() >= 60 ? clamped : 1; return requested; } /* (hookshot) The window's textures as the console's display queue has them: each present is shown at its display @@ -414,7 +419,8 @@ static void realtime_step(int64_t now) { } /* (the Insert menu's Game speed "per frame": a frame is a 60th of a second of game time. Not past 60 fps: the game would run faster than real time, so a cap above 60 keeps the real-time step whatever that row says) */ - if (!rx_option(0, 1) && fps_max() <= 60) { + /* (nor under 60: a cap of 30 would be the game at half speed for good) */ + if (!rx_option(0, 1) && fps_max() == 60) { int *slot = (int *)GP(mod + 0x2c8); if (*slot >= 15 && *slot <= 60) *slot = 60; return; @@ -456,7 +462,7 @@ static void realtime_step(int64_t now) { const int top = timed ? 60 : fps_max(); const double ratio = sum / count * 60.0; int *slot = (int *)GP(mod + 0x2c8); - if (*slot < 15 || *slot > fps_max()) { + if (*slot < 15 || *slot > (fps_max() > 60 ? fps_max() : 60)) { /* (under a cap below 60 it starts at the game's 60) */ static int logged; if (!logged++) hos_log("vi: the graphics module's frame rate reads %d: no real-time step", *slot); return; diff --git a/host/nn_video.c b/host/nn_video.c index 9592487..9efc511 100644 --- a/host/nn_video.c +++ b/host/nn_video.c @@ -67,9 +67,13 @@ static DWORD WINAPI av1_notice(LPVOID unused) { return 0; } #endif -/* (hookshot) host/mf_av1.c: AV1 on Media Foundation, as the static FFmpeg has no AV1 decoder */ +/* (hookshot) host/mf_av1.c: AV1 on Media Foundation (Windows' decoder, in hardware where the GPU has one). + (Triforce) It needs Microsoft's AV1 Video Extension, which not every PC has: without it the movies are decoded here, by + dav1d, when the FFmpeg the build links has it (the Tools' FFmpeg with AV1; nx2/build.py takes whichever is there), as + NX2navi decodes them always. No notice and no black movies then. HOS_AV1=dav1d: dav1d first, for tests; + HOS_AV1_MISSING=1: as if the extension were missing. */ typedef void (*mf_av1_picture_fn)(void *user, const uint8_t *y, const uint8_t *uv, int w, int h, int stride, int64_t pts); -void *mf_av1_open(int w, int h); +void *mf_av1_open(int w, int h, int tell); void mf_av1_decode(void *m, const uint8_t *data, size_t n, int64_t pts, mf_av1_picture_fn fn, void *user); void mf_av1_flush(void *m); void mf_av1_close(void *m); @@ -283,15 +287,23 @@ static void dec_init_sized(Ctx *c, enum AVCodecID id, const char *what, int w, i if (id == AV_CODEC_ID_AV1) codec = avcodec_find_decoder_by_name("libdav1d"); if (!codec && id == AV_CODEC_ID_AV1) codec = avcodec_find_decoder_by_name("libaom-av1"); #endif - if (!codec) codec = avcodec_find_decoder(id); Dec *d = (Dec *)calloc(1, sizeof *d); InitializeSRWLock(&d->lock); +#ifdef _WIN32 /* (Triforce) AV1: Windows' decoder, else dav1d; never FFmpeg's own, which needs a hardware context */ + if (id == AV_CODEC_ID_AV1) { + const char *first = getenv("HOS_AV1"); + codec = avcodec_find_decoder_by_name("libdav1d"); + if (!(codec && first && !strcmp(first, "dav1d"))) d->mf = mf_av1_open(w, h, 0); + if (d->mf) codec = NULL; + } else +#endif + if (!codec) codec = avcodec_find_decoder(id); if (codec) { d->ctx = avcodec_alloc_context3(codec); d->ctx->thread_count = id == AV_CODEC_ID_AV1 ? 4 : 2; /* (1080p AV1 in software) */ if (avcodec_open2(d->ctx, codec, NULL) < 0) avcodec_free_context(&d->ctx); } - if (!d->ctx && id == AV_CODEC_ID_AV1) d->mf = mf_av1_open(w, h); + if (!d->ctx && !d->mf && id == AV_CODEC_ID_AV1) d->mf = mf_av1_open(w, h, 1); /* (and its notice when there is none) */ #ifdef _WIN32 if (!d->ctx && !d->mf && id == AV_CODEC_ID_AV1) { /* (issue 380: no AV1 decoder in Windows: the movies were black, with sound) */ static volatile LONG told; @@ -307,8 +319,8 @@ static void dec_init_sized(Ctx *c, enum AVCodecID id, const char *what, int w, i set_host(ARG(0), DEC_MAGIC, d); static int logged; if (logged++ < 4) - hos_log("video: %s decoder %s (%dx%d)", what, d->ctx ? "ready" : d->mf ? "ready on Media Foundation" : - "not available in this FFmpeg", w, h); + hos_log("video: %s decoder %s%s (%dx%d)", what, d->ctx ? "ready: " : d->mf ? "ready on Media Foundation" : + "not available in this FFmpeg", d->ctx ? d->ctx->codec->name : "", w, h); RET(0); } static void picture_free(Picture *p) { diff --git a/host/nvn_native.c b/host/nvn_native.c index e2dd4a1..7d5709f 100644 --- a/host/nvn_native.c +++ b/host/nvn_native.c @@ -366,10 +366,59 @@ NV(nvnMemoryPoolGetBufferAddress) { if (no_pool(c, "buffer address")) { RET(0); return; } RET(((const nvn_pool *)P(0))->gpu); } -NV(nvnMemoryPoolFlushMappedRange) {} /* CPU writes are seen through write watch */ +/* (Triforce, 11-14, diagnostics) HOS_FT=: a frame trace. From that present of the game's on, for HOS_FT_N presents (8), + every line "ft ..." in the log carries the clock (ms, QueryPerformanceCounter's, the last 100 s of it): the game's + submits, presents, acquires, event polls and blocking sync waits here; its threads' light events waited for 0.2 ms or more, + every light event signalled and every message sent (nn_os_sync.c); readbacks that waited (rx_backend.c). The renderer writes + its own lines by the same clock (RX_VK_FT, vk_replay.cpp). What a frame's cycle waits for, link by link */ +int hos_ft_on(void) { + static long long from = -2, n = 8; + extern volatile LONG64 hos_present_count; /* display.c */ + if (from == -2) { + const char *e = getenv("HOS_FT"), *k = getenv("HOS_FT_N"); + if (k) n = atoll(k); + from = e ? atoll(e) : -1; + } + return from >= 0 && hos_present_count >= from && hos_present_count < from + n; +} +double hos_ft_ms(void) { return (double)(hos_now_ns() % 100000000000LL) / 1e6; } + +/* (Triforce, 11-14, diagnostics) HOS_NVN_LOG_FLUSH=: the game's own marks of who owns a mapped range: FlushMappedRange (the + CPU wrote it: the GPU may read it from here on) and InvalidateMappedRange (the CPU is about to read what the GPU wrote). n calls + of both kinds logged in the order they come, from HOS_NVN_LOG_FLUSH_AFTER_S seconds (150) after the first one, and a count of + each every 5 s. What a replay behind the game needs to know is here: which ranges the game hands over, and when in its frame */ +static void flush_note(Ctx *c, int kind, uint64_t gpu, uint64_t size) { + static int want = -1, after_s = 150; + static volatile LONG logged; + static volatile LONG64 n[2], bytes[2], first, last; + if (want < 0) { + const char *a = getenv("HOS_NVN_LOG_FLUSH_AFTER_S"); + if (a) after_s = atoi(a); + want = getenv("HOS_NVN_LOG_FLUSH") ? atoi(getenv("HOS_NVN_LOG_FLUSH")) : 0; + } + if (!want) return; + const int64_t now = hos_now_ns(); + if (!first) InterlockedCompareExchange64(&first, now, 0); + InterlockedIncrement64(&n[kind]); + InterlockedAdd64(&bytes[kind], (LONG64)size); + if (now - first >= after_s * 1000000000LL && logged < want && InterlockedIncrement(&logged) <= want) + hos_log("nvn: %s %#llx +%#llx (lr %#llx)", kind ? "invalidate" : "flush", (unsigned long long)gpu, (unsigned long long)size, + (unsigned long long)c->x[30]); + const int64_t l = last; + if (now - l >= 5000000000LL && InterlockedCompareExchange64(&last, now, l) == l && l) + hos_log("nvn: mapped ranges in 5 s: %lld flushed (%lld KB), %lld invalidated (%lld KB)", + (long long)InterlockedExchange64(&n[0], 0), (long long)(InterlockedExchange64(&bytes[0], 0) >> 10), + (long long)InterlockedExchange64(&n[1], 0), (long long)(InterlockedExchange64(&bytes[1], 0) >> 10)); + else if (!l) InterlockedCompareExchange64(&last, now, 0); +} +NV(nvnMemoryPoolFlushMappedRange) { /* CPU writes are seen through write watch */ + const nvn_pool *o = (const nvn_pool *)P(0); + if (o) flush_note(c, 0, o->gpu + U64(1), U64(2)); +} NV(nvnMemoryPoolInvalidateMappedRange) { /* the CPU is about to read what the GPU wrote */ if (no_pool(c, "invalidate mapped range")) return; const nvn_pool *o = (const nvn_pool *)P(0); + flush_note(c, 1, o->gpu + U64(1), U64(2)); if ((int64_t)U64(1) >= 0) rx_nvn_invalidate(o->gpu + U64(1), U64(2)); } NV(nvnMemoryPoolSetDebugLabel) {} @@ -497,9 +546,13 @@ NV(nvnBufferGetAddress) { NV(nvnBufferGetMemoryPool) { RETP(((const nvn_buffer *)P(0))->pool); } NV(nvnBufferGetMemoryOffset) { RET((uint64_t)((const nvn_buffer *)P(0))->off); } NV(nvnBufferGetSize) { RET(((const nvn_buffer *)P(0))->size); } -NV(nvnBufferFlushMappedRange) {} +NV(nvnBufferFlushMappedRange) { + const nvn_buffer *o = (const nvn_buffer *)P(0); + if (o && o->pool) flush_note(c, 0, POOL(o->pool)->gpu + (uint64_t)o->off + U64(1), U64(2)); +} NV(nvnBufferInvalidateMappedRange) { const nvn_buffer *o = (const nvn_buffer *)P(0); + if (o->pool) flush_note(c, 1, POOL(o->pool)->gpu + (uint64_t)o->off + U64(1), U64(2)); if (o->pool) rx_nvn_invalidate(POOL(o->pool)->gpu + (uint64_t)o->off + U64(1), U64(2)); } NV(nvnBufferSetDebugLabel) {} @@ -1106,7 +1159,11 @@ NV(nvnSyncWait) { if (block < 0) block = getenv("HOS_NVN_SYNC_BLOCK") != NULL; const int away = hos_vcore_away(); const int64_t t0 = hos_now_ns(); + const int ft = hos_ft_on(); + if (ft) hos_log("ft %.3f syncwait> fence %llu timeout %llu (lr %#llx)", hos_ft_ms(), (unsigned long long)s->fence, + (unsigned long long)U64(1), (unsigned long long)c->x[30]); const int ok = rx_nvn_wait(s->fence, block ? ~0ull >> 1 : U64(1)); + if (ft) hos_log("ft %.3f syncwait< %d", hos_ft_ms(), ok); wait_note(ok ? 0 : 1, t0); if (away) hos_vcore_back(); RET(ok ? NVN_SYNC_WAIT_RESULT_CONDITION_SATISFIED : NVN_SYNC_WAIT_RESULT_TIMEOUT_EXPIRED); @@ -1138,8 +1195,10 @@ NV(nvnEventGetValue) { if (nowait < 0) nowait = getenv("HOS_PRESENT_NOWAIT") && atoi(getenv("HOS_PRESENT_NOWAIT")) != 0; if (nowait && (c->x[30] == 0x805904a0 || c->x[30] == 0x80590530)) { RET(1); return; } } - uint32_t v = 0; nvn_gpu_read(((const nvn_event *)P(0))->gpu, &v, 4); RET(v); - if (sync_log()) hos_log("nvn: EventGetValue %p = %u (lr %#llx)", P(0), v, (unsigned long long)c->x[30]); + const uint64_t ev_gpu = ((const nvn_event *)P(0))->gpu; /* (before RET: it writes x0) */ + uint32_t v = 0; nvn_gpu_read(ev_gpu, &v, 4); RET(v); + if (sync_log()) hos_log("nvn: EventGetValue %#llx = %u (lr %#llx)", (unsigned long long)ev_gpu, v, (unsigned long long)c->x[30]); + if (hos_ft_on()) hos_log("ft %.3f eventget %#llx = %u (lr %#llx)", hos_ft_ms(), (unsigned long long)ev_gpu, v, (unsigned long long)c->x[30]); } NV(nvnEventSignal) { const nvn_event *e = (const nvn_event *)P(0); @@ -2375,7 +2434,10 @@ NV(nvnQueueSubmitCommands) { core is given up for it, as Present and AcquireTexture do, so the game's other threads run on it meanwhile. Nothing in the replay runs guest code or needs the core) */ const int away = hos_vcore_away(); + const int ft = hos_ft_on(); + if (ft) hos_log("ft %.3f submit> queue %p, %d handles (lr %#llx)", hos_ft_ms(), (void *)q, n, (unsigned long long)c->x[30]); rx_nvn_submit_q(h, n, (uint64_t)(uintptr_t)q); + if (ft) hos_log("ft %.3f submit< queue %p", hos_ft_ms(), (void *)q); if (away) hos_vcore_back(); return; } @@ -2475,7 +2537,10 @@ NV(nvnWindowAcquireTexture) { *(int *)P(2) = o->active ? o->next++ % o->active : 0; { const int away = hos_vcore_away(); + const int ft = hos_ft_on(); + if (ft) hos_log("ft %.3f acquire>", hos_ft_ms()); hos_vi_native_acquire(o->active ? o->active : o->ntex); + if (ft) hos_log("ft %.3f acquire<", hos_ft_ms()); if (away) hos_vcore_back(); } /* the acquire sync: this texture is no longer read by the display path (presents copy it in GPU order) */ @@ -2489,7 +2554,10 @@ NV(nvnQueuePresentTexture) { held_flush(); /* (a present flushes the queue) */ { const int away = hos_vcore_away(); + const int ft = hos_ft_on(); + if (ft) hos_log("ft %.3f present> (lr %#llx)", hos_ft_ms(), (unsigned long long)c->x[30]); rx_nvn_present(win_tex(o, i), o->crop); + if (ft) hos_log("ft %.3f present<", hos_ft_ms()); if (away) hos_vcore_back(); } o->presents++; diff --git a/host/oot_high_fps.c b/host/oot_high_fps.c index 39e82b7..30c211e 100644 --- a/host/oot_high_fps.c +++ b/host/oot_high_fps.c @@ -1,6 +1,6 @@ -/* (hookshot) The sound manager's job at 60 Hz and the physics solver's steps rounded up, under a Frame rate cap above 60: the - lifted f_80435300, f_838ceb80 and f_838cfc10 call these (nx2/oot_patches.py); host/oot_high_fps.h has why and the logic. - HOS_FPS_LOG (set): what they do, every 600 calls. */ +/* (hookshot) The sound manager's job at 60 Hz, the physics solver's steps rounded up, the floors' carry, the height-field + surfaces' fixed steps and the ocarina recorder's clock, under a Frame rate cap above 60: the lifted code calls these + (nx2/oot_patches.py); host/oot_high_fps.h has why and the logic. HOS_FPS_LOG (set): what they do, every 600 calls. */ #include "hos.h" #include "oot_high_fps.h" #define WIN32_LEAN_AND_MEAN @@ -64,3 +64,50 @@ uint32_t hos_oot_solver_steps(uint32_t game, uint32_t scaled_bits, uint32_t per_ } return (uint32_t)steps; } + +/* The follow-surface updates' compare (f_8015d490, f_80159dc0, f_838baa40): what the physics' inverse step is compared with */ +uint32_t hos_oot_floor_cut(void) { + const uint32_t cut = oot_floor_cut(__atomic_load_n(&hos_frame_seconds_bits, __ATOMIC_RELAXED)); + if (fps_log()) { + static volatile LONG64 calls; + const LONG64 n = InterlockedIncrement64(&calls); + if (n % 600 == 1) + hos_log("oot: a floor's carry is cut to a tenth above an inverse step of %.1f (the game's 100)", oot_bits_float(cut)); + } + return cut; +} + +static FrameWhole sim_whole; /* (a table of the surfaces' own: sharing hos_frame_whole's, they and the waters' ripples would + take each other's slots in turn, each starting again without its remainder) */ +static long long sim_calls, sim_given; /* (HOS_FPS_LOG's counts, under sim_lock) */ +static SRWLOCK sim_lock = SRWLOCK_INIT; /* (the physics step's jobs) */ + +/* The fixed steps of f_83895f20, f_838a2660 and f_80652cb0: object, the manager or floor (its x19); quotient_bits, the update's + seconds over 1/60 s. The float the game's cut then takes */ +uint32_t hos_oot_sim_steps(uint64_t object, uint32_t quotient_bits) { + const int t = timed(); + long long given = -1; + AcquireSRWLockExclusive(&sim_lock); + const uint32_t bits = oot_sim_steps(&sim_whole, object, quotient_bits, t); + if (fps_log()) { + const float steps = oot_bits_float(bits); + if (steps > 0.0f && steps < 1000.0f) sim_given += (long long)steps; + if (!(++sim_calls % 600)) given = sim_given, sim_given = 0; + } + ReleaseSRWLockExclusive(&sim_lock); + if (given >= 0) + hos_log("oot: the water and floor surfaces got %lld fixed steps in the last 600 updates (quotient %.3f%s)", given, + oot_bits_float(quotient_bits), t ? ", fractions carried" : ""); + return bits; +} + +static float record_rem; +static SRWLOCK record_lock = SRWLOCK_INIT; /* (the ocarina manager's update alone runs the four places: a lock all the same) */ + +/* The ocarina recorder's clock (f_832b2690, f_832b32b0, f_832b6840): the frames it adds for the frame step */ +uint32_t hos_oot_record_frames(uint32_t step_bits) { + AcquireSRWLockExclusive(&record_lock); + const uint32_t frames = oot_record_frames(&record_rem, step_bits, timed()); + ReleaseSRWLockExclusive(&record_lock); + return frames; +} diff --git a/host/oot_high_fps.h b/host/oot_high_fps.h index 404a478..679fa0d 100644 --- a/host/oot_high_fps.h +++ b/host/oot_high_fps.h @@ -19,11 +19,33 @@ fps); 4 steps a 60 fps frame would be 0 above 240. A moving platform (the stone one under the spikes in the Deku Tree) then slides out from under Link (issue 1798). While the step is under 1 the count is rounded up instead, so a solver step never covers more time than at 60 fps, give or take 10% (the frame time's jitter, so a steady rate keeps a steady count), - and it is at least 1 and never under the game's. A step of 1 or more (a frame slower than 60 fps) keeps the game's count. */ + and it is at least 1 and never under the game's. A step of 1 or more (a frame slower than 60 fps) keeps the game's count. + + Three more places, as NX2navi found and fixed them for this build (its recomp/patches-v111.txt, the sets ofollow, owater and + orecord: public domain / MIT), here as hooks in the lifted C: + + Floors that carry a character: the character controller's three follow-surface updates (f_8015d490, f_80159dc0, + f_838baa40) add the velocity of what the character stands on to its move, but keep only a tenth of it when the physics' + inverse step (one over the frame's seconds times the time scale) is over 100.0. At the console's 60 and 30 fps that is a + slow motion under 0.6 of the game's speed; with the frames' own time as the step it is every update above 100 fps, and + Link slides off a moving platform that carries him at 60. The compare takes 100 / (the frame's seconds x 60) while a frame + is shorter than a 60 fps one, so the cut comes where it came at 60 fps; 100 otherwise. + + The fixed steps of the physics' height-field surfaces: the two water managers (f_83895f20, f_838a2660) and the interactive + floors (f_80652cb0: the Deku Tree's ground webs) simulate in steps of 1/60 s. Each update divides its seconds by 1/60 and cuts + the quotient to whole steps, dropping the fraction: every update shorter than 1/60 s ran none, so above 60 fps those surfaces + stood still whatever pushed into them. The quotient becomes the whole steps in it plus a remainder kept for the object + (oot_sim_steps), as a float the game's own cut and clamp then take: 60 steps a second at any rate. + + The ocarina recorder's clock: the ocarina manager adds (unsigned)frame step to the length of the current note or rest + (in 60 fps frames) at four places (f_832b2690, f_832b32b0, twice in f_832b6840). Cut to 0 above 60 fps, the clock stood + still: a recorded song lost its rests and the clock's limits never came. One remainder for the four (oot_record_frames): + the clock counts 60 a second at any rate. */ #pragma once #include #include #include +#include "frame_whole.h" typedef struct { float part; /* (the steps added up since the job last ran, under 1) */ @@ -84,3 +106,34 @@ static inline int32_t oot_solver_steps(int32_t game, uint32_t scaled_bits, uint3 if (steps < 1) steps = 1; return steps > game ? steps : game; } + +/* The number the follow-surface updates compare the physics' inverse step with (a float's bits): seconds_bits, the frame's + seconds as the step hook has them (host/nn_vi.c hos_frame_seconds_bits; 0: the game's own step) */ +static inline uint32_t oot_floor_cut(uint32_t seconds_bits) { + const float frames = oot_bits_float(seconds_bits) * 60.0f; /* (the frame in 60 fps frames) */ + if (!seconds_bits || !(frames > 0.0f) || !(frames < 1.0f)) return 0x42c80000u; /* (100.0, the game's) */ + return oot_float_bits(100.0f / frames); +} + +/* A height-field surface's fixed steps for this update: quotient_bits, its seconds over 1/60 s (a float's bits). With the frames' + own time as the step the whole steps in it plus the object's remainder, as a float; else, and for a quotient that is no + count (0, below 0, NaN, huge), the quotient as it is, for the game's own cut. The caller holds the table's lock. */ +static inline uint32_t oot_sim_steps(FrameWhole *w, uint64_t object, uint32_t quotient_bits, int timed) { + const float q = oot_bits_float(quotient_bits); + if (!timed || !(q > 0.0f) || q >= 2147483648.0f) return quotient_bits; + return oot_float_bits((float)frame_whole(w, object, quotient_bits, 1)); +} + +/* The frames the ocarina recorder's clock takes for a frame step (a float's bits): as the game cuts it (fcvtzu: toward zero, + below 0 and NaN 0), with the fraction carried in *rem while the frames' own time is the step */ +static inline uint32_t oot_record_frames(float *rem, uint32_t step_bits, int timed) { + const float step = oot_bits_float(step_bits); + if (!timed || !(step > 0.0f) || step >= 2147483648.0f) { + *rem = 0.0f; + return !(step > 0.0f) ? 0 : step >= 4294967296.0f ? 0xffffffffu : (uint32_t)step; + } + const float sum = *rem + step; + const uint32_t whole = (uint32_t)sum; + *rem = sum - (float)whole; + return whole; +} diff --git a/host/oot_sprint.c b/host/oot_sprint.c new file mode 100644 index 0000000..9e255e0 --- /dev/null +++ b/host/oot_sprint.c @@ -0,0 +1,122 @@ +/* (Triforce) The sprint button: host/oot_sprint.h has the rules and why. Here: the settings (the Insert menu's Sprint + button and Sprint mode rows, HOS_SPRINT=ls|b and HOS_SPRINT_HOLD=1 over them), Link's state read from the game's + memory, and the two calls the lifted code makes (nx2/oot_patches.py patch_sprint). After NX2navi's runtime/sprint.c. + + Game 1.1.1's addresses (main at 0x80020000): Link's movement update f_804caa80 loads his input flags at 0x804ce798 + and tests the dash input twice from them (to start the sprint, to keep it); the update of his camera and stance + f_80c225d0 loads their low byte at 0x80c27f3c. x19 is Link at both. + Link's state: the id at [[[[Link +0x18] +0x270] +0x188] +0x1c0] +0x28 (+0x4c when that reads 0x11a, a state that + wraps another), the field the game compares with 1 (Walk) before it starts a sprint itself. + HOS_SPRINT_LOG (set): the first calls and each change, in the log. */ +#include "hos.h" +#include "oot_sprint.h" +#include "../gpu/vk/vk_api.h" +#include +#include + +int rx_option(int id, int otherwise); +int hos_ini_option(const char *key, int otherwise); +extern volatile uint32_t hos_frame_seconds_bits; /* (host/nn_vi.c: the frame's seconds, 0 at a cap of 60) */ + +#define LINK_CLASS 0x84b6dfc0ULL /* what Link's first word reads */ +#define ACTOR_MANAGER_AT 0x84c9e0d0ULL +#define ACTOR_MANAGER_CLASS 0x84bffe68ULL +#define CONTROLLED_CLASS 0x84b69698ULL + +static OotSprint sprint = OOT_SPRINT_INIT; + +static int log_on(void) { + static int on = -1; + int v = __atomic_load_n(&on, __ATOMIC_RELAXED); + if (v < 0) v = getenv("HOS_SPRINT_LOG") != NULL, __atomic_store_n(&on, v, __ATOMIC_RELAXED); + return v; +} + +/* The button and the mode: the menu's rows once the renderer answers, the options file's before, the test switches + over both */ +static int setting_button(void) { + static int env = -2, file = -1; + if (env == -2) { + const char *e = getenv("HOS_SPRINT"); + env = !e ? -1 : !strcmp(e, "ls") || !strcmp(e, "l3") ? OOT_SPRINT_LS : !strcmp(e, "b") ? OOT_SPRINT_B : OOT_SPRINT_OFF; + } + if (env >= 0) return env; + if (file < 0) file = hos_ini_option("sprint_button", 0); + const int v = rx_option(HOS_OPT_SPRINT, file); + return v == OOT_SPRINT_LS || v == OOT_SPRINT_B ? v : OOT_SPRINT_OFF; +} +static int setting_hold(void) { + static int env = -2, file = -1; + if (env == -2) env = getenv("HOS_SPRINT_HOLD") ? atoi(getenv("HOS_SPRINT_HOLD")) != 0 : -1; + if (env >= 0) return env; + if (file < 0) file = hos_ini_option("sprint_mode", 0); + return rx_option(HOS_OPT_SPRINT_HOLD, file) != 0; +} + +/* Reads through Link's pointers: one that is not a pointer into the game's heap or its program image (stale, or a + number) reads 0 instead of faulting. Both ranges are committed whole (host/hos_mem.c), so the check is two compares. + (Not VirtualQuery: with a dozen calls of it in each update of Link the lab ran 2.8 fps, 350 ms a frame, where the + same run without them made 48 to 66, 11-14. The game's memory is gigabytes of pages in few regions.) */ +static int readable(uint64_t a, uint64_t n) { + if (a >= HOS_HEAP_BASE && a - HOS_HEAP_BASE < hos_heap_size) return n <= hos_heap_size - (a - HOS_HEAP_BASE); + if (a >= hos_image_base && a < hos_image_end) return n <= hos_image_end - a; + return 0; +} +static uint64_t ld64(uint64_t a) { return readable(a, 8) ? *(const uint64_t *)GP(a) : 0; } +static int32_t ld32(uint64_t a) { return readable(a, 4) ? *(const int32_t *)GP(a) : 0; } + +/* 1 when the object is the Link the player controls. His class has one more instance the player does not control: the + game's actor manager names the controlled one at +0x470, whose +0x18 must be Link's own +0x18. */ +static int is_link(uint64_t link) { + if (!link || ld64(link) != LINK_CLASS) return 0; + const uint64_t slot = ld64(ACTOR_MANAGER_AT), mgr = slot ? ld64(slot) : 0; + if (!mgr || ld64(mgr) != ACTOR_MANAGER_CLASS) return 1; + const uint64_t ctl = ld64(mgr + 0x470); + if (!ctl || ld64(ctl) != CONTROLLED_CLASS) return 1; + return ld64(ctl + 0x18) == ld64(link + 0x18); +} + +static int32_t state_id(uint64_t link) { + uint64_t p = ld64(link + 0x18); + if (p) p = ld64(p + 0x270); + if (p) p = ld64(p + 0x188); + if (p) p = ld64(p + 0x1c0); + if (!p) return -1; + int32_t id = ld32(p + 0x28); + if (id == 0x11a) id = ld32(p + 0x4c); + return id; +} + +/* The controller as the game is about to read it (host/nn_hid.c npad_states) */ +void hos_oot_sprint_pad(uint64_t *buttons) { + oot_sprint_pad(&sprint, setting_button(), buttons); +} + +/* f_804caa80 at 0x804ce798, after the load of Link's input flags: the flags, with the dash input while the sprint is on */ +uint64_t hos_oot_sprint_link(Ctx *c, uint64_t link, uint64_t flags) { + (void)c; + const int button = setting_button(); + if (button == OOT_SPRINT_OFF && !OOT_SPRINT_GET(sprint.active)) return flags; /* (off: one load and out) */ + static int told_class; + if (log_on() && !told_class) { + told_class = 1; + hos_log("sprint: the movement update's object %#llx reads %#llx (Link's class is %#llx), state %d", + (unsigned long long)link, (unsigned long long)ld64(link), (unsigned long long)LINK_CLASS, (int)state_id(link)); + } + if (!is_link(link)) return flags; + const uint32_t bits = __atomic_load_n(&hos_frame_seconds_bits, __ATOMIC_RELAXED); + float seconds = 0.0f; + memcpy(&seconds, &bits, 4); + const double frames = bits && seconds > 0.0f && seconds < 1.0f ? (double)seconds * 60.0 : 1.0; + const int32_t id = state_id(link); + const int was = OOT_SPRINT_GET(sprint.active); + const int on = oot_sprint_step(&sprint, oot_sprint_kind(id), button, setting_hold(), frames); + if (log_on() && on != was) hos_log("sprint: %s (Link's state %d, the game's own dash input %d)", on ? "on" : "off", (int)id, (int)(flags >> 4 & 1)); + return on ? flags | OOT_SPRINT_DASH : flags; +} + +/* f_80c225d0 at 0x80c27f3c, after the load of the same flags' low byte for the stance and the camera */ +uint64_t hos_oot_sprint_stance(Ctx *c, uint64_t link, uint64_t flags) { + (void)c; + return OOT_SPRINT_GET(sprint.active) && is_link(link) ? flags | OOT_SPRINT_DASH : flags; +} diff --git a/host/oot_sprint.h b/host/oot_sprint.h new file mode 100644 index 0000000..35cd8bb --- /dev/null +++ b/host/oot_sprint.h @@ -0,0 +1,138 @@ +/* (Triforce) The sprint button's rules, after NX2navi's runtime/sprint.c (public domain additions over an MIT base): + Link sprints while his input flags hold the dash input (bit 4 of the word at *(Link +0x50)); the game itself sets it + only out of a roll. With a sprint button chosen, the lifted code asks hos_oot_sprint_link / hos_oot_sprint_stance + right after it loads those flags (nx2/oot_patches.py patch_sprint), and they put the bit in while the button's sprint + is on: the game's own sprint then runs, with its speed, animation, camera and every rule of its own that ends it. + + The button: the left stick's click (which the game does not use) or B, the bottom face button of the Nintendo layout. + Toggle: a press starts the sprint, the next press or Link's state ends it. Hold: sprint while it is held. During + gameplay the button is kept from the game, so it does not also do its own action; B only for a press that begins + while Link moves, and then until it is let go, so B puts the weapon away as usual otherwise and the game never sees + half a press. A press made in a menu, a cutscene or a load (no update of Link within OOT_SPRINT_POLLS polls of the + controller) starts nothing. With the game's Swap Jump Controls B is the jump: the stick's click is the one to use. + + Here: the logic alone, for tests/test_sprint.py. host/oot_sprint.c reads Link and the settings. */ +#ifndef OOT_SPRINT_H +#define OOT_SPRINT_H +#include + +enum { OOT_SPRINT_OFF = 0, OOT_SPRINT_LS = 1, OOT_SPRINT_B = 2 }; /* the Sprint button row's values */ +enum { OOT_SPRINT_MOVE, OOT_SPRINT_KEEP, OOT_SPRINT_WAIT, OOT_SPRINT_STOP }; /* what Link's state does to a sprint */ +#define OOT_SPRINT_DASH 0x10u /* the dash input in Link's flags */ +#define OOT_SPRINT_BIT_B (1u << 1) /* HidNpadButton: B, the left stick's click, ZL (the game's targeting) */ +#define OOT_SPRINT_BIT_LS (1u << 4) +#define OOT_SPRINT_BIT_ZL (1u << 8) +#define OOT_SPRINT_POLLS 8 /* the button is kept from the game while Link's update ran within this many polls */ +/* Standing still this long ends a toggled sprint that has moved, or this long after a press made while standing, which + has not moved yet. In 60 Hz frames, not updates: above 60 fps Link is updated once a rendered frame, and four updates + are 17 ms at 240 fps: any short stop would end the sprint */ +#define OOT_SPRINT_WAIT_STOP 4.0 +#define OOT_SPRINT_WAIT_START 90.0 + +/* Link's state id (host/oot_sprint.c reads it) -> its kind. A state not listed stops the sprint. */ +static inline int oot_sprint_kind(int32_t id) { + switch (id) { + case 1: /* Walk (running too) */ + case 13: /* Jump */ + case 32: /* Land */ + case 33: /* Fall */ + case 68: /* Clamber */ + return OOT_SPRINT_MOVE; + case 253: /* StepOn */ + case 35: /* Slide */ + case 36: /* SlideLand */ + return OOT_SPRINT_KEEP; + case 40: /* Wait */ + return OOT_SPRINT_WAIT; + default: + return OOT_SPRINT_STOP; + } +} + +static inline uint64_t oot_sprint_bit(int button) { + return button == OOT_SPRINT_LS ? OOT_SPRINT_BIT_LS : button == OOT_SPRINT_B ? OOT_SPRINT_BIT_B : 0; +} + +/* The controller's polls come from the game's input reads, Link's updates from one of its workers: the fields both + touch are read and written whole (__atomic), each side's own are plain */ +typedef struct { + uint64_t pressed; /* the button's presses since Link's last update (pad -> step) */ + int held; /* the button held at the last poll */ + int zl_held; /* ZL held at the last poll */ + int polls; /* polls since Link's last update */ + int moving; /* Link's state was a MOVE one at his last update */ + int active; /* the sprint is on: the dash input goes in */ + int latched; /* the press now held began in gameplay (B: while Link moved): kept from the game */ + uint64_t last; /* pad only: the buttons at the last poll */ + int moved; /* step only: the sprint has moved since it started */ + double wait_frames; /* step only: 60 Hz frames stood still */ + int last_button; /* step only: the setting at the last update */ +} OotSprint; +#define OOT_SPRINT_INIT {0, 0, 0, OOT_SPRINT_POLLS, 0, 0, 0, 0, 0, 0.0, -1} + +#define OOT_SPRINT_GET(f) __atomic_load_n(&(f), __ATOMIC_SEQ_CST) +#define OOT_SPRINT_SET(f, v) __atomic_store_n(&(f), (v), __ATOMIC_SEQ_CST) + +static inline void oot_sprint_reset(OotSprint *s) { + OOT_SPRINT_SET(s->active, 0), OOT_SPRINT_SET(s->pressed, (uint64_t)0), OOT_SPRINT_SET(s->latched, 0); +} + +/* One poll of the controller, the buttons as the game is about to get them: the sprint button's bit is taken out of + them while its press is the sprint's. */ +static inline void oot_sprint_pad(OotSprint *s, int button, uint64_t *buttons) { + if (button == OOT_SPRINT_OFF) { /* nothing may stay on for when a button is chosen again */ + oot_sprint_reset(s); + s->last = *buttons; + return; + } + const uint64_t bit = oot_sprint_bit(button), now = *buttons; + const int n = OOT_SPRINT_GET(s->polls), gameplay = n < OOT_SPRINT_POLLS; + if (n <= OOT_SPRINT_POLLS) OOT_SPRINT_SET(s->polls, n + 1); + /* A press is decided at its start, for its whole length: kept from the game, and the sprint's, when it begins in + gameplay (B: while Link moves); else the game's alone, and no sprint's */ + if (now & ~s->last & bit) { + const int mine = gameplay && (button != OOT_SPRINT_B || OOT_SPRINT_GET(s->moving)); + OOT_SPRINT_SET(s->latched, mine); + if (mine) __atomic_fetch_or(&s->pressed, bit, __ATOMIC_SEQ_CST); + } else if (!(now & bit)) { + OOT_SPRINT_SET(s->latched, 0); + } + s->last = now; + OOT_SPRINT_SET(s->held, (now & bit) != 0); + OOT_SPRINT_SET(s->zl_held, (now & OOT_SPRINT_BIT_ZL) != 0); + if (OOT_SPRINT_GET(s->latched) && gameplay) *buttons &= ~bit; +} + +/* One update of Link: his state's kind, the settings, and the 60 Hz frames this update covers (1 at 60 fps, 0.5 at + 120). 1 when the sprint is on for it. */ +static inline int oot_sprint_step(OotSprint *s, int kind, int button, int hold, double frames) { + OOT_SPRINT_SET(s->polls, 0); + if (button != s->last_button) s->last_button = button, OOT_SPRINT_SET(s->active, 0), OOT_SPRINT_SET(s->pressed, (uint64_t)0); + const int press = (__atomic_exchange_n(&s->pressed, (uint64_t)0, __ATOMIC_SEQ_CST) & oot_sprint_bit(button)) != 0; + int on = OOT_SPRINT_GET(s->active); + if (OOT_SPRINT_GET(s->zl_held)) kind = OOT_SPRINT_STOP; /* targeting: the game's own stance */ + OOT_SPRINT_SET(s->moving, kind == OOT_SPRINT_MOVE); + if (button == OOT_SPRINT_OFF) { + on = 0; + } else if (hold) { + on = OOT_SPRINT_GET(s->held) && OOT_SPRINT_GET(s->latched) && kind != OOT_SPRINT_STOP; + } else if (press) { + if (on) on = 0; + else if (kind != OOT_SPRINT_STOP && (button != OOT_SPRINT_B || kind == OOT_SPRINT_MOVE)) + on = 1, s->moved = kind == OOT_SPRINT_MOVE, s->wait_frames = 0; + } else if (on) { + if (kind == OOT_SPRINT_STOP) { + on = 0; + } else if (kind == OOT_SPRINT_WAIT) { + s->wait_frames += frames; + if (s->wait_frames > (s->moved ? OOT_SPRINT_WAIT_STOP : OOT_SPRINT_WAIT_START)) on = 0; + } else { + s->wait_frames = 0; + s->moved |= kind == OOT_SPRINT_MOVE; + } + } + OOT_SPRINT_SET(s->active, on); + return on; +} + +#endif diff --git a/host/oot_wide_hud.c b/host/oot_wide_hud.c new file mode 100644 index 0000000..d344991 --- /dev/null +++ b/host/oot_wide_hud.c @@ -0,0 +1,107 @@ +/* (Triforce) The 2D interface on a picture wider than 16:9: host/oot_wide_hud.h has the rules and why. Here: the call the + lifted code makes when a layout's full-screen projection is set (nx2/oot_wide.py HUD, the 2D canvas setup f_80794330 + at 0x80794368), and the Ultrawide HUD row's setting. + + The Insert menu's "Ultrawide HUD" row (ultrawide_hud= in the options file): 0 the middle, as before; 1 the screen's + edges. HOS_WIDE_HUD=<0|1> over it. Read once, at the start: the hearts', the item slot's and the small map's panes + are moved in the layouts as the game loads them (host/prompts.c build_layouts asks hos_wide_hud_out). + Lab switches: HOS_WIDE_LOG=1 names each screen class the first time its projection is set (its address and where it + was put); HOS_WIDE_SHIFT==,... moves whole layouts to a side, to find out what a class draws. */ +#include "hos.h" +#include "oot_wide_hud.h" +#include +#include + +int hos_ini_option(const char *key, int otherwise); +extern volatile float hos_wide_squeeze; +extern volatile uint32_t hos_wide_keep_mask; + +#define SEEN_MAX 96 +static uint64_t seen[SEEN_MAX]; +static int nseen; +static struct { uint64_t cls; int side; } forced[32]; +static int nforced = -1; + +static int edges_on(void) { + static volatile int v = -1; + if (v < 0) { + const char *e = getenv("HOS_WIDE_HUD"); + v = e ? atoi(e) != 0 : hos_ini_option("ultrawide_hud", 0) != 0; + } + return v; +} + +/* host/prompts.c build_layouts: how far out the side panes of the layouts go (oot_hud_out), 0 when they stay */ +float hos_wide_hud_out(void) { + return edges_on() ? oot_hud_out(hos_wide_squeeze) : 0.0f; +} + +static void read_forced(void) { + nforced = 0; + const char *e = getenv("HOS_WIDE_SHIFT"); + while (e && *e && nforced < 32) { + char *end; + const uint64_t cls = strtoull(e, &end, 16); + if (end == e || *end != '=') break; + const char s = end[1]; + forced[nforced].cls = cls, forced[nforced].side = s == 'l' ? OOT_HUD_LEFT : s == 'r' ? OOT_HUD_RIGHT : OOT_HUD_MIDDLE; + nforced++; + e = strchr(end, ','); + if (e) e++; + } +} + +static int side_of(uint64_t cls) { + if (nforced < 0) read_forced(); + for (int i = 0; i < nforced; i++) + if (forced[i].cls == cls) return forced[i].side; + return OOT_HUD_MIDDLE; +} + +/* A printable text at a, inside the program's image, for the log: "" when there is none */ +static void text_at(uint64_t a, char *out, size_t cap) { + out[0] = 0; + if (a < hos_image_base || a + cap >= hos_image_end) return; + const char *s = (const char *)GP(a); + size_t n = 0; + while (n + 1 < cap && s[n] >= 0x20 && s[n] < 0x7f) n++; + if (n < 3 || s[n]) return; + memcpy(out, s, n), out[n] = 0; +} + +static void say_once(uint64_t cls, int keep, int side, const float *row, float move) { + static int on = -1; + if (on < 0) on = getenv("HOS_WIDE_LOG") != NULL; + if (!on) return; + for (int i = 0; i < nseen; i++) + if (seen[i] == cls) return; + if (nseen >= SEEN_MAX) return; + seen[nseen++] = cls; + /* (what the class may name itself by: a text its table points at, at a few places the game's classes keep one) */ + char names[4][48]; + const int at[4] = {0x378, -8, 0x10, 0x18}; + for (int i = 0; i < 4; i++) { + names[i][0] = 0; + const uint64_t slot = cls + (uint64_t)(int64_t)at[i]; + if (slot < hos_image_base || slot + 8 >= hos_image_end) continue; + uint64_t p = G64(slot); + text_at(p, names[i], sizeof names[i]); + if (!names[i][0] && p >= hos_image_base && p + 16 < hos_image_end) text_at(G64(p + 8), names[i], sizeof names[i]); + } + hos_log("wide hud: screen class %#llx: %s (m00 %.6f, m30 %.4f, m03 %.4f) names [%s] [%s] [%s] [%s]", (unsigned long long)cls, + keep ? "kept whole" : side < 0 ? "left edge" : side > 0 ? "right edge" : "middle", (double)row[0], (double)row[3], + (double)move, names[0], names[1], names[2], names[3]); +} + +/* nx2/oot_wide.py HUD: screen is the canvas' screen object; row the first column of the layout's full-screen projection + and move the first number of its fourth column (oot_wide_hud.h oot_hud_row) */ +void hos_wide_hud_row(Ctx *c, uint64_t screen, float *row, float *move) { + (void)c; + const float q = hos_wide_squeeze; + if (!(q > 0.0f)) return; + const uint64_t cls = hos_guest_range(screen, 8) ? G64(screen) : 0; /* (the first word of the screen object: its class) */ + const int keep = (oot_hud_keep_bit(cls) & hos_wide_keep_mask) != 0; + const int side = keep ? OOT_HUD_MIDDLE : side_of(cls); + say_once(cls, keep, side, row, *move); + oot_hud_row(row, move, q, keep, side); +} diff --git a/host/oot_wide_hud.h b/host/oot_wide_hud.h new file mode 100644 index 0000000..d926dba --- /dev/null +++ b/host/oot_wide_hud.h @@ -0,0 +1,64 @@ +/* (Triforce) Where the game's 2D interface goes on a picture wider than 16:9: the rules alone, for + tests/test_wide_hud.py. host/oot_wide_hud.c reads the game's memory and the settings; nx2/oot_wide.py has the hook. + + The game lays its interface out on a 16:9 grid and draws each layout (a "screen": the hearts, the action buttons, the + map, a menu, a fade) through one full-screen projection. It sets a layout's projection once, when the layout is made, + not each frame; the hook hands it over then. Two things are done with it on a wider picture: + - kept: as it is, the layout stretched over the whole picture (the item wheel's and the menus' backgrounds, which + redraw a capture of the whole picture, and the fades, which are one colour); + - middle: times the squeeze (16:9 over the picture's ratio), so the layout keeps its shape in the middle 16:9 of the + picture. Everything else, as NX2navi's patch does it. + (oot_hud_row can also move a whole layout out to one side, which the lab's HOS_WIDE_SHIFT uses to find out what a + screen draws. No layout of the game's is placed that way: see below.) + + The Insert menu's "Ultrawide HUD" row on Screen edges puts the hearts and the item slot in the picture's top corners + and the small map at its right edge. Those are moved inside their layouts, by oot_hud_out, because a projection + cannot do it: PlayerStatus_00 holds the hearts (on a pane at the grid's top left corner) and the item slot (on one at + its top right) together, and MapMenu_00 is the small map and the pause menu's whole-screen map in one layout, whose + projection is not set again as the menu opens (tried: the menu's map sat off to the right). nx2/prompt_assets.py + HUD_SIDE_PANES lists the panes, host/prompts.c build_layouts writes them as the game loads its layouts, so the row is + read once, at the start. Layouts that follow something in the 3D world (the targeting pointer, an enemy's bar, a + speech balloon) and the action buttons stay in the middle. */ +#ifndef OOT_WIDE_HUD_H +#define OOT_WIDE_HUD_H +#include + +enum { OOT_HUD_MIDDLE = 0, OOT_HUD_LEFT = -1, OOT_HUD_RIGHT = 1 }; + +/* The screens that keep the whole picture, by their class (the first word of the screen object), a bit each: bit k of + the keep mask keeps screen k whole (the Insert menu's Ultrawide menus row clears the menus' bits 1 and 3). */ +#define OOT_HUD_KEEP_N 4 +static const uint64_t oot_hud_keep_classes[OOT_HUD_KEEP_N] = { + 0x84c0d098ULL, /* the item wheel's background */ + 0x84c04fb0ULL, /* the pause and system menus' background */ + 0x84c061c8ULL, /* the event fade */ + 0x84c07b30ULL, /* the loading fade */ +}; + +static inline uint32_t oot_hud_keep_bit(uint64_t screen_class) { + for (int k = 0; k < OOT_HUD_KEEP_N; k++) + if (oot_hud_keep_classes[k] == screen_class) return 1u << k; + return 0; +} + +#define OOT_HUD_HALF 1920.0f /* (half the 16:9 grid's width, in the layouts' units) */ + +/* How far out, in the layouts' units, a pane at a side of the 16:9 grid goes to sit at that side of a picture whose + squeeze is q: the same distance oot_hud_row moves a whole layout (1 - q of the clip space's half, q per unit). */ +static inline float oot_hud_out(float q) { + return q > 0.0f && q < 1.0f ? OOT_HUD_HALF * (1.0f / q - 1.0f) : 0.0f; +} + +/* A layout's projection for a picture whose squeeze is q (0 < q <= 1). The game keeps the matrix by columns: `first` is + its first column (m00 m10 m20 m30: what x is multiplied by) and `move` the first number of its fourth column (m03, + what is added to clip x). keep: the layout keeps the whole picture. side: where it goes when it is not kept. + (The fourth number of `first` is m30, not the move: a move put there bends the picture, and the layouts it was first + tried on left the screen altogether.) */ +static inline void oot_hud_row(float first[4], float *move, float q, int keep, int side) { + if (keep || !(q > 0.0f) || q >= 1.0f) return; + for (int k = 0; k < 4; k++) first[k] *= q; + /* (clip space is 2 wide: the 16:9 middle leaves 1 - q free on each side) */ + *move = *move * q + (float)side * (1.0f - q); +} + +#endif diff --git a/host/posix/linux_stubs.c b/host/posix/linux_stubs.c index 392b33f..9acd6e1 100644 --- a/host/posix/linux_stubs.c +++ b/host/posix/linux_stubs.c @@ -8,7 +8,7 @@ #include "../../gpu/vk/vk_api.h" typedef void (*mf_av1_picture_fn)(void *user, const uint8_t *y, const uint8_t *uv, int w, int h, int stride, int64_t pts); -void *mf_av1_open(int w, int h) { (void)w, (void)h; return NULL; } +void *mf_av1_open(int w, int h, int tell) { (void)w, (void)h, (void)tell; return NULL; } void mf_av1_decode(void *m, const uint8_t *data, size_t n, int64_t pts, mf_av1_picture_fn fn, void *user) { (void)m, (void)data, (void)n, (void)pts, (void)fn, (void)user; } diff --git a/host/prompts.c b/host/prompts.c index 523d57b..d865ce6 100644 --- a/host/prompts.c +++ b/host/prompts.c @@ -31,6 +31,7 @@ int hos_mods_has(const char *prefix, const char *suffix); /* mods.c */ #define TEXTURES_PATH "ui/layout2archive/common.product.100.nin_ounce_nvn2.bntx.zs" #define LAYOUTS_PATH "ui/layout2archive/common.product.100.nin_ounce_nvn2.blarc.zs" #define SIZES_PATH "system/resource/resourcesizetable." /* (then the version, ".rsizetable.zs") */ +#define CAMERA_PATH "pack/actor/playercamera.pack.zs" /* (Triforce: the Camera speed row, build_camera) */ #define MAGIC 0x18029a7fu #define PAD_BASE 0xF700 static const char *const faces[HOS_PROMPT_N] = {"switch", "xbox", "ps4", "ps5", "steamdeck"}; @@ -391,7 +392,10 @@ static void ring_places(const int *bind, int place[4]) { - the ocarina hint's icons moved apart for the wide ones bound (prompts//hint.bin, nx2/prompt_assets.py HINTS: "HPHN", u32 count of pad inputs, each one's icon's extra width (f32), u32 panes; each: u32 offset, u8 slot, 3 bytes, f32 x): each icon the one before's extra further on, the last one where it is; - none when both are as the game has them (the game's own file) */ + - the hearts, the item slot and the small map out at a wide picture's sides (prompts/wide.bin, nx2/prompt_assets.py HUD_SIDE_PANES: "HPWD", u32 + count; each: u32 offset, i8 side, 3 bytes, f32 x), with the Insert menu's Ultrawide HUD row on Screen edges + (host/oot_wide_hud.h); + none when all are as the game has them (the game's own file) */ static int build_layouts(char *out, size_t cap) { char dir[MAX_PATH], path[MAX_PATH]; int face; @@ -453,6 +457,27 @@ static int build_layouts(char *out, size_t cap) { h = fnv(fnv(h, shift, sizeof shift), hint, (size_t)hn); } } + float hos_wide_hud_out(void); + const float wide_out = hos_wide_hud_out(); + long wn = 0; + snprintf(path, sizeof path, "%sprompts/wide.bin", dir); + uint8_t *wide = wide_out > 0.0f ? read_all(path, "HPWD", &wn) : NULL; + float *wxs = NULL; + if (wide) { + const uint32_t count = rd32le(wide + 4); + if (count && count <= ((size_t)wn - 8) / 12) { + wxs = (float *)malloc(count * sizeof *wxs); + for (uint32_t k = 0; k < count; k++) { + const uint8_t *e = wide + 8 + 12 * k; + memcpy(&wxs[k], e + 8, 4); + wxs[k] += (float)(int8_t)e[4] * wide_out; + add_run(&runs, &nruns, &cap_runs, rd32le(e), 4, (const uint8_t *)&wxs[k]); + } + h = fnv(fnv(h, &wide_out, sizeof wide_out), wide, (size_t)wn); + } + } else if (wide_out > 0.0f) { + hos_log("prompts: no prompts/wide.bin: the Ultrawide HUD stays in the middle (Setup's Update makes it)"); + } int ok = 0; if (nruns) { char keypath[MAX_PATH]; @@ -460,9 +485,12 @@ static int build_layouts(char *out, size_t cap) { snprintf(path, sizeof path, "%sprompts/Common.blarc", dir); snprintf(keypath, sizeof keypath, "%sprompts/Common.blarc.key", dir); ok = write_patched(path, out, keypath, h, runs, nruns); - hos_log("prompts: the Talk ring's A B X Y at places %d %d %d %d, the ocarina hint %s%s", place[0], place[1], place[2], - place[3], xs ? "spread" : "as is", ok ? "" : ": could not be written"); + hos_log("prompts: the Talk ring's A B X Y at places %d %d %d %d, the ocarina hint %s, the hearts, item slot and map %s%s", + place[0], place[1], place[2], place[3], xs ? "spread" : "as is", wxs ? "at the picture's sides" : "as is", + ok ? "" : ": could not be written"); } + free(wxs); + free(wide); free(xs); free(hint); free(runs); @@ -499,11 +527,67 @@ static int build(char *out, size_t cap) { return ok; } +/* (Triforce) The Insert menu's Camera speed row (camera_speed= in the options file, percent; HOS_CAMERA_SPEED= + over it): how fast the right stick turns the camera, times the game's own speed at any of the five Camera sensitivity + settings of its Options. The rates are five floats in the game's camera pack (nx2/prompt_assets.py CAMERA has where and + why): prompts/PlayerCamera.pack.zs is that pack (prompts/PlayerCamera.pack) with each rate of prompts/camera.bin ("HPCM", + u32 count; each: u32 offset, f32 the game's rate) times the factor. Read once: the game loads the pack at its start, so + the row applies after a restart. At 100 the game's own file is served. NX2navi's camera speed setting, without its + search of the game's memory for a change during play. */ +int hos_ini_option(const char *key, int otherwise); +static volatile double camera_applied = 1.0; /* the factor in the pack the game was given */ +static double camera_wanted(void) { + static int v = -1; + if (v < 0) { + const char *e = getenv("HOS_CAMERA_SPEED"); + v = e && atoi(e) > 0 ? atoi(e) : hos_ini_option("camera_speed", 100); + v = v < 25 ? 25 : v > 300 ? 300 : v; + } + return v / 100.0; +} +/* host/nn_hid.c: the mouse's panning reaches the game as the right stick; divided by this it keeps its own sensitivity */ +double hos_camera_speed(void) { + return camera_applied; +} +static int build_camera(char *out, size_t cap) { + char dir[MAX_PATH], path[MAX_PATH], keypath[MAX_PATH]; + int face; + const double k = camera_wanted(); + if (k == 1.0 || !setup(dir, sizeof dir, &face)) return 0; + long tn = 0; + snprintf(path, sizeof path, "%sprompts/camera.bin", dir); + uint8_t *t = read_all(path, "HPCM", &tn); + const uint32_t count = t ? rd32le(t + 4) : 0; + if (!count || count > 8 || (size_t)tn < 8 + 8 * (size_t)count) { + hos_log("prompts: no prompts/camera.bin: the camera keeps the game's speed (Setup's Update makes it)"); + free(t); + return 0; + } + float rates[8]; + size_t nruns = 0, cap_runs = 8; + Run *runs = (Run *)malloc(cap_runs * sizeof *runs); + for (uint32_t i = 0; i < count; i++) { + memcpy(&rates[i], t + 8 + 8 * i + 4, 4); + rates[i] = (float)(rates[i] * k); + add_run(&runs, &nruns, &cap_runs, rd32le(t + 8 + 8 * i), 4, (const uint8_t *)&rates[i]); + } + const uint64_t h = fnv(fnv(0xcbf29ce484222325ull, &k, sizeof k), t, (size_t)tn); + snprintf(out, cap, "%sprompts/PlayerCamera.pack.zs", dir); + snprintf(path, sizeof path, "%sprompts/PlayerCamera.pack", dir); + snprintf(keypath, sizeof keypath, "%sprompts/PlayerCamera.key", dir); + const int ok = write_patched(path, out, keypath, h, runs, nruns); + if (ok) camera_applied = k; + hos_log("prompts: camera speed x%.2f, the first rate %.2f%s", k, (double)rates[0], ok ? "" : ": could not be written"); + free(runs); + free(t); + return ok; +} + /* mods.c: the file for a RomFS path the prompts replace (key: lower case, '/'), 1 with one. Asked only when no mod has the file: a mod's own wins */ int hos_prompts_romfs(const char *key, char *out, size_t cap) { - static volatile LONG state[3]; /* the font, the textures, the layouts: 0 not built, 1 being built, 2 built, 3 none */ - static char path[3][MAX_PATH]; + static volatile LONG state[4]; /* the font, the textures, the layouts, the camera: 0 not built, 1 being built, 2 built, 3 none */ + static char path[4][MAX_PATH]; const size_t kn = strlen(key); if (!strncmp(key, SIZES_PATH, sizeof SIZES_PATH - 1) && kn > 14 && !strcmp(key + kn - 14, ".rsizetable.zs")) { char dir[MAX_PATH]; /* (the game's size table with room for the faces' fonts, larger than its: nx2/prompt_assets.py) */ @@ -512,9 +596,10 @@ int hos_prompts_romfs(const char *key, char *out, size_t cap) { snprintf(out, cap, "%sprompts/rsizetable.zs", dir); return GetFileAttributesA(out) != INVALID_FILE_ATTRIBUTES; } - const int which = !strcmp(key, FONT_PATH) ? 0 : !strcmp(key, TEXTURES_PATH) ? 1 : !strcmp(key, LAYOUTS_PATH) ? 2 : -1; + const int which = !strcmp(key, FONT_PATH) ? 0 : !strcmp(key, TEXTURES_PATH) ? 1 : !strcmp(key, LAYOUTS_PATH) ? 2 : + !strcmp(key, CAMERA_PATH) ? 3 : -1; if (which < 0) return 0; - static int (*const builders[3])(char *, size_t) = {build, build_textures, build_layouts}; + static int (*const builders[4])(char *, size_t) = {build, build_textures, build_layouts, build_camera}; if (InterlockedCompareExchange(&state[which], 1, 0) == 0) InterlockedExchange(&state[which], builders[which](path[which], MAX_PATH) ? 2 : 3); while (state[which] == 1) Sleep(1); diff --git a/host/rx_backend.c b/host/rx_backend.c index d1a2679..154e3f8 100644 --- a/host/rx_backend.c +++ b/host/rx_backend.c @@ -69,6 +69,21 @@ int hos_gpu_guest_ranges(uint64_t (*out)[2], int max); /* The soft write watch's marks (rt/a64.h A64_MARK, hos_mark_written) over the GPU-visible ranges, read and cleared: the Vulkan back end's own collection (gpu/render's rx_collect_written works only with the WebGPU renderer started). One consumer: with Vulkan the WebGPU renderer never runs. */ +#ifdef A64_MARK_ONE +/* A64_MARK_ONE (build.py RX_MARK_ONE=1): lifted code marks the page of a store's first byte alone, so a mark says "this + page and the next may have been written" (a store is at most 4 KB long: rt/a64.h, the lift's mark groups). */ +static int page_collected(uint64_t page) { /* is the page in a range some collection reads (and so clears its mark)? */ + static uint64_t (*all)[2]; + static SRWLOCK mu = SRWLOCK_INIT; + int in = 0; + AcquireSRWLockExclusive(&mu); + if (!all) all = malloc(sizeof *all * 4096); + const int n = hos_gpu_guest_ranges(all, 4096); + for (int i = 0; i < n && !in; i++) in = page >= all[i][0] >> 12 && page < (all[i][1] + 4095) >> 12; + ReleaseSRWLockExclusive(&mu); + return in; +} +#endif static int64_t collect_in(const uint64_t (*ranges)[2], int nranges, uint32_t **pages) { size_t n = 0, cap = 4096; uint32_t *out = malloc(cap * sizeof *out); @@ -76,6 +91,46 @@ static int64_t collect_in(const uint64_t (*ranges)[2], int nranges, uint32_t **p const uint64_t lo = ranges[r][0], hi = ranges[r][1] < hos_guest_size() ? ranges[r][1] : hos_guest_size(); uint64_t p1 = (hi + 4095) >> 12; if (p1 > A64_DIRTY_PAGES) p1 = A64_DIRTY_PAGES; +#ifdef A64_MARK_ONE + /* carry: the page before was marked, so this one may be written too. Into the range's first page from the page + before it: that mark is another collection's to clear, or nobody's (then it is cleared here: it would say so at + every collection). Out of the range's last page: marked on for whoever collects the page after. */ + uint64_t p = lo >> 12; + int carry = 0; + if (p > 0 && p <= p1 && __atomic_load_n(&a64_dirty[p - 1], __ATOMIC_RELAXED)) { + carry = 1; + if (!page_collected(p - 1)) __atomic_exchange_n(&a64_dirty[p - 1], 0, __ATOMIC_ACQUIRE); + } + while (p < p1) { + if (!carry) { + if (!(p & 31) && p + 32 <= p1) { /* (32 pages at a time while none is marked) */ + uint64_t w[4]; + memcpy(w, &a64_dirty[p], 32); + if (!(w[0] | w[1] | w[2] | w[3])) { + p += 32; + continue; + } + } + if (!(p & 7) && p + 8 <= p1) { /* (eight pages a load while none is marked) */ + uint64_t w; + memcpy(&w, &a64_dirty[p], 8); + if (!w) { + p += 8; + continue; + } + } + } + const int marked = __atomic_load_n(&a64_dirty[p], __ATOMIC_RELAXED) && __atomic_exchange_n(&a64_dirty[p], 0, __ATOMIC_ACQUIRE); + if (marked || carry) { + if (n == cap) out = realloc(out, (cap *= 2) * sizeof *out); + out[n++] = (uint32_t)p; + } + carry = marked; + p++; + } + if (carry && p1 < A64_DIRTY_PAGES) __atomic_store_n(&a64_dirty[p1], 1, __ATOMIC_RELEASE); + continue; +#endif for (uint64_t p = lo >> 12; p < p1;) { if (!(p & 31) && p + 32 <= p1) { /* (32 pages at a time while none is marked) */ uint64_t w[4]; @@ -430,7 +485,14 @@ void rx_nvn_write_texels(const nvn_tex *t, unsigned level, unsigned layer0, cons void rx_nvn_invalidate(uint64_t gpu, uint64_t size) { /* The callback can wait for replay or GPU write-back: let another guest thread use this core meanwhile. */ const int away = hos_vcore_away(); + int hos_ft_on(void); + double hos_ft_ms(void); + const int ft = hos_ft_on(); /* (Triforce, 11-14: a readback that waited 0.2 ms or more, nvn_native.c HOS_FT) */ + const int64_t t0 = ft ? hos_now_ns() : 0; vk ? vk->nvn_invalidate(gpu, size) : wgr_nvn_invalidate(gpu, size); + if (ft && hos_now_ns() - t0 >= 200000) + hos_log("ft %.3f readback %#llx +%#llx waited %.2f ms", hos_ft_ms(), (unsigned long long)gpu, (unsigned long long)size, + (hos_now_ns() - t0) / 1e6); if (away) hos_vcore_back(); } int rx_guest_imported(uint64_t lo, uint64_t hi) { diff --git a/host/save_tx.h b/host/save_tx.h new file mode 100644 index 0000000..80bf744 --- /dev/null +++ b/host/save_tx.h @@ -0,0 +1,401 @@ +/* (triforce) The save as a transaction, with backups. Included by nn_fs.c after its file helpers (its CreateFileA and the rest + are the long-path ones there); tests/test_save_tx.py compiles it alone. + + On the console the game writes its save files and then calls nn::fs::Commit: until then the old save is what a power cut + leaves. Here the "save:" mount was the save folder itself and Commit did nothing, so a crash, a kill or a power cut in the + middle of a save left a half-written one, and nothing kept an older save. NX2navi commits its save as a transaction and + keeps backups (its runtime/fs_host.c, "save transactions": public domain); this is that design for Triforce's folders: + + /save// the committed save: where it always was (Setup's Convert saves, the lab's save copies) + /save-tx//work/ the working copy, the save mount's root; made from the committed save at each first mount + /save-tx//new/ Commit's staging copy of the working copy + /save-tx//ready-/ a finished commit not swapped in yet: the newest save + /save-tx//old/ the committed save a Commit replaced, on its way to the backups + /save-backup/// the saves Commits replaced: the newest SAVE_TX_BACKUPS of them + + Commit: (1) copy work to new, every file written out to the disk (FlushFileBuffers); (2) rename new to ready-, the commit + point, and delete the ready- it supersedes; (3) rename save/ to old; (4) rename ready- to save/; (5) old + becomes a backup when the run has made none yet or the last one is SAVE_TX_BACKUP_GAP_S old, else it is deleted. Windows + refuses (3) while another program holds a file of the save open (a virus scanner, a sync tool): ready- then stays the + newest save, and the next Commit and the next start try (3) to (5) again. A step that fails before the commit point falls + back to what was done before: the working copy's files copied over the save's. + + The first mount of a run (save_tx_mount) puts the disk right after a run that died: new (an unfinished copy) is deleted; the + highest ready- is swapped in; a save folder that is gone with old still there gets old back; then the working copy is + made again from the committed save, so changes no Commit took are gone, as on the console. + + HOS_SAVE_TX=0: the save folder itself is the mount again and Commit does nothing, as before. */ +#pragma once +#include +#include +#include +#include + +#define SAVE_TX_BACKUPS 5 +#define SAVE_TX_BACKUP_GAP_S 300 +#define SAVE_TX_MOVE_TRIES 10 /* (20 ms apart: 0.2 s at most on the game's thread) */ + +typedef struct { + char save[HOS_FS_PATH], tx[HOS_FS_PATH], work[HOS_FS_PATH], staging[HOS_FS_PATH], old[HOS_FS_PATH], backups[HOS_FS_PATH]; +} SaveTxPaths; + +/* (tests: a Commit or a recovery stops before its save_tx_crash_at-th step, from 1, and leaves the disk as a process killed + there would; save_tx_test_now stands for the clock) */ +static int save_tx_crash_at, save_tx_step, save_tx_dead; +static time_t save_tx_test_now; +static time_t save_tx_last_backup; /* (this run's last backup: 0 before the first) */ + +static int tx_crashed(void) { + if (save_tx_crash_at && ++save_tx_step == save_tx_crash_at) save_tx_dead = 1; + return save_tx_dead; +} +static time_t tx_now(void) { return save_tx_test_now ? save_tx_test_now : time(NULL); } +static int tx_exists(const char *p) { return GetFileAttributesA(p) != INVALID_FILE_ATTRIBUTES; } +static int tx_is_dir(const char *p) { + const DWORD a = GetFileAttributesA(p); + return a != INVALID_FILE_ATTRIBUTES && (a & FILE_ATTRIBUTE_DIRECTORY) && !(a & FILE_ATTRIBUTE_REPARSE_POINT); +} + +static void tx_make_dirs(const char *path) { /* path and every parent of it */ + char p[HOS_FS_PATH]; + snprintf(p, sizeof p, "%s", path); + for (char *q = p + 1; *q; q++) + if ((*q == '\\' || *q == '/') && q[-1] != ':') { + const char keep = *q; + *q = 0; + CreateDirectoryA(p, NULL); + *q = keep; + } + CreateDirectoryA(p, NULL); +} + +static void tx_delete_tree(const char *dir) { + char pat[HOS_FS_PATH]; + WIN32_FIND_DATAA fd; + snprintf(pat, sizeof pat, "%s\\*", dir); + HANDLE f = FindFirstFileA(pat, &fd); + if (f != INVALID_HANDLE_VALUE) { + do { + if (!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, "..")) continue; + char sub[HOS_FS_PATH]; + snprintf(sub, sizeof sub, "%s\\%s", dir, fd.cFileName); + /* (a link goes itself: never what it points to) */ + if (fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { if (!DeleteFileA(sub)) RemoveDirectoryA(sub); } + else if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) tx_delete_tree(sub); + else DeleteFileA(sub); + } while (FindNextFileA(f, &fd)); + FindClose(f); + } + RemoveDirectoryA(dir); +} + +static int tx_empty(const char *dir) { /* 1: a folder with nothing in it */ + char pat[HOS_FS_PATH]; + WIN32_FIND_DATAA fd; + int any = 0; + snprintf(pat, sizeof pat, "%s\\*", dir); + HANDLE f = FindFirstFileA(pat, &fd); + if (f == INVALID_HANDLE_VALUE) return 1; + do { + if (strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..")) any = 1; + } while (!any && FindNextFileA(f, &fd)); + FindClose(f); + return !any; +} + +/* One file; the source may be open for writing (a save file the game has not closed). 1: copied */ +static int tx_copy_file(const char *src, const char *dst, int flush) { + HANDLE in = CreateFileA(src, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, NULL); + if (in == INVALID_HANDLE_VALUE) return 0; + HANDLE out = CreateFileA(dst, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + if (out == INVALID_HANDLE_VALUE) { + CloseHandle(in); + return 0; + } + static const DWORD chunk = 1u << 16; + char *buf = (char *)malloc(chunk); + int ok = buf != NULL; + while (ok) { + DWORD got = 0, put = 0; + if (!ReadFile(in, buf, chunk, &got, NULL)) ok = 0; + else if (!got) break; + else if (!WriteFile(out, buf, got, &put, NULL) || put != got) ok = 0; + } + free(buf); + if (ok && flush && !FlushFileBuffers(out)) ok = 0; + CloseHandle(out); + CloseHandle(in); + return ok; +} + +/* A folder with everything in it, links left out; dst is made. 1: every file copied */ +static int tx_copy_tree(const char *src, const char *dst, int flush) { + tx_make_dirs(dst); + if (!tx_is_dir(dst)) return 0; + char pat[HOS_FS_PATH]; + WIN32_FIND_DATAA fd; + snprintf(pat, sizeof pat, "%s\\*", src); + HANDLE f = FindFirstFileA(pat, &fd); + if (f == INVALID_HANDLE_VALUE) return tx_is_dir(src); /* (an empty folder has "." at least; a missing one is no copy) */ + int ok = 1; + do { + if (!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, "..")) continue; + if (fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) continue; + char a[HOS_FS_PATH], b[HOS_FS_PATH]; + if (snprintf(a, sizeof a, "%s\\%s", src, fd.cFileName) >= (int)sizeof a || + snprintf(b, sizeof b, "%s\\%s", dst, fd.cFileName) >= (int)sizeof b) { ok = 0; continue; } + if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ok &= tx_copy_tree(a, b, flush); + else ok &= tx_copy_file(a, b, flush); + } while (FindNextFileA(f, &fd)); + FindClose(f); + return ok; +} + +/* Rename a folder; tried again while another program holds a file in it. 1: moved */ +static int tx_move(const char *from, const char *to, int tries) { + for (int i = 0; i < tries; i++) { + if (MoveFileExA(from, to, 0)) return 1; + if (i + 1 < tries) Sleep(20); + } + return 0; +} + +/* The highest ready- under tx (0: none), its path in out */ +static unsigned tx_ready_highest(const SaveTxPaths *p, char *out, size_t cap) { + char pat[HOS_FS_PATH]; + WIN32_FIND_DATAA fd; + unsigned best = 0; + snprintf(pat, sizeof pat, "%s\\ready-*", p->tx); + HANDLE f = FindFirstFileA(pat, &fd); + if (f == INVALID_HANDLE_VALUE) return 0; + do { + const char *digits = fd.cFileName + 6; + if (strncmp(fd.cFileName, "ready-", 6) || !*digits || strlen(digits) > 9 || strspn(digits, "0123456789") != strlen(digits)) continue; + if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) || (fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) continue; + const unsigned n = (unsigned)strtoul(digits, NULL, 10); + if (n > best) best = n; + } while (FindNextFileA(f, &fd)); + FindClose(f); + if (best && out) snprintf(out, cap, "%s\\ready-%u", p->tx, best); + return best; +} + +static void tx_ready_delete_below(const SaveTxPaths *p, unsigned n) { + char pat[HOS_FS_PATH]; + WIN32_FIND_DATAA fd; + snprintf(pat, sizeof pat, "%s\\ready-*", p->tx); + HANDLE f = FindFirstFileA(pat, &fd); + if (f == INVALID_HANDLE_VALUE) return; + do { + const char *digits = fd.cFileName + 6; + if (strncmp(fd.cFileName, "ready-", 6) || !*digits || strlen(digits) > 9 || strspn(digits, "0123456789") != strlen(digits)) continue; + if ((unsigned)strtoul(digits, NULL, 10) >= n) continue; + char sub[HOS_FS_PATH]; + snprintf(sub, sizeof sub, "%s\\%s", p->tx, fd.cFileName); + tx_delete_tree(sub); + } while (FindNextFileA(f, &fd)); + FindClose(f); +} + +static int tx_backup_name(const char *name) { /* YYYYMMDD-HHMMSS, or that with - after it: the names made here */ + if (strlen(name) < 15 || strspn(name, "0123456789") != 8 || name[8] != '-' || strspn(name + 9, "0123456789") != 6) return 0; + return !name[15] || (name[15] == '-' && name[16] && strspn(name + 16, "0123456789") == strlen(name + 16)); +} + +/* Deletes the oldest backups past SAVE_TX_BACKUPS; only folders with the names made here */ +static void tx_backups_prune(const SaveTxPaths *p) { + for (;;) { + char pat[HOS_FS_PATH], oldest[64] = ""; + WIN32_FIND_DATAA fd; + int count = 0; + snprintf(pat, sizeof pat, "%s\\*", p->backups); + HANDLE f = FindFirstFileA(pat, &fd); + if (f == INVALID_HANDLE_VALUE) return; + do { + if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) || (fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) continue; + if (!tx_backup_name(fd.cFileName) || strlen(fd.cFileName) >= sizeof oldest) continue; + count++; + /* (by the 15 characters of the time, then shorter first: "x" was made before "x-2") */ + const int c = strncmp(fd.cFileName, oldest, 15); + if (!oldest[0] || c < 0 || (!c && (strlen(fd.cFileName) < strlen(oldest) || + (strlen(fd.cFileName) == strlen(oldest) && strcmp(fd.cFileName, oldest) < 0)))) + snprintf(oldest, sizeof oldest, "%s", fd.cFileName); + } while (FindNextFileA(f, &fd)); + FindClose(f); + if (count <= SAVE_TX_BACKUPS) return; + char sub[HOS_FS_PATH]; + snprintf(sub, sizeof sub, "%s\\%s", p->backups, oldest); + tx_delete_tree(sub); + if (tx_exists(sub)) return; /* (it cannot be deleted: no loop over it) */ + } +} + +/* old, the save a Commit replaced: a backup when wanted (always: a recovery's), else deleted. The name of the backup in made */ +static void tx_old_away(const SaveTxPaths *p, int always, char *made, size_t cap) { + if (made && cap) made[0] = 0; + if (!tx_exists(p->old)) return; + const time_t now = tx_now(); + if (tx_empty(p->old)) { /* (the folder a first mount made, before any save: nothing to keep) */ + tx_delete_tree(p->old); + return; + } + if (always || !save_tx_last_backup || now - save_tx_last_backup >= SAVE_TX_BACKUP_GAP_S) { + struct tm t; + const time_t n = now; +#ifdef _WIN32 + gmtime_s(&t, &n); +#else + gmtime_r(&n, &t); +#endif + char name[64], to[HOS_FS_PATH]; + strftime(name, sizeof name, "%Y%m%d-%H%M%S", &t); + tx_make_dirs(p->backups); + snprintf(to, sizeof to, "%s\\%s", p->backups, name); + for (int k = 2; tx_exists(to) && k < 100; k++) snprintf(to, sizeof to, "%s\\%s-%d", p->backups, name, k); + if (tx_move(p->old, to, 3)) { + save_tx_last_backup = now; + if (made) snprintf(made, cap, "%s", to); + tx_backups_prune(p); + return; + } + } + tx_delete_tree(p->old); +} + +/* Steps (3) and (4): the finished commit at ready becomes the committed save. 1: swapped in */ +static int tx_swap(const SaveTxPaths *p, const char *ready) { + if (tx_exists(p->old)) tx_delete_tree(p->old); /* (left by a swap that died before its backup step: already replaced) */ + if (tx_exists(p->save)) { + if (tx_crashed()) return 0; + if (!tx_move(p->save, p->old, SAVE_TX_MOVE_TRIES)) return 0; /* (held open elsewhere: ready stays the newest save) */ + } + if (tx_crashed()) return 0; + if (!tx_move(ready, p->save, SAVE_TX_MOVE_TRIES)) { + if (!tx_exists(p->save)) tx_move(p->old, p->save, SAVE_TX_MOVE_TRIES); /* (the save back where it was) */ + return 0; + } + return 1; +} + +static void save_tx_paths(SaveTxPaths *p, const char *user, const char *uid) { + snprintf(p->save, sizeof p->save, "%s\\save\\%s", user, uid); + snprintf(p->tx, sizeof p->tx, "%s\\save-tx\\%s", user, uid); + snprintf(p->work, sizeof p->work, "%s\\save-tx\\%s\\work", user, uid); + snprintf(p->staging, sizeof p->staging, "%s\\save-tx\\%s\\new", user, uid); + snprintf(p->old, sizeof p->old, "%s\\save-tx\\%s\\old", user, uid); + snprintf(p->backups, sizeof p->backups, "%s\\save-backup\\%s", user, uid); +} + +static int save_tx_enabled(void) { + const char *e = getenv("HOS_SAVE_TX"); + return !(e && e[0] == '0'); +} + +/* The first mount of the save in a run: the disk put right after a run that died, then the working copy made from the + committed save. 1: mount p->work; 0: the working copy could not be made, mount p->save as before. note (may be NULL) gets + a line for the log when the recovery found something to do */ +static int save_tx_mount(const SaveTxPaths *p, char *note, size_t cap) { + if (note && cap) note[0] = 0; + tx_make_dirs(p->tx); + if (tx_exists(p->staging)) { /* (an unfinished copy: the commit never happened) */ + tx_delete_tree(p->staging); + if (note) snprintf(note, cap, "an unfinished save was dropped (the run before ended in the middle of saving)"); + } + char ready[HOS_FS_PATH]; + const unsigned n = tx_ready_highest(p, ready, sizeof ready); + if (n) { /* (a finished commit: the newest save) */ + const int in = tx_swap(p, ready); + if (save_tx_dead) return 0; + if (in) tx_ready_delete_below(p, n + 1); + if (note) snprintf(note, cap, in ? "a finished save that was not in place yet was put in place" : + "a finished save could not be put in place (another program holds the save folder): it is used from where it is"); + } else if (!tx_exists(p->save) && tx_exists(p->old)) { /* (died between steps (3) and (4) of a swap whose ready is gone) */ + tx_move(p->old, p->save, SAVE_TX_MOVE_TRIES); + if (note) snprintf(note, cap, "the save was put back in place"); + } + if (tx_crashed()) return 0; + tx_old_away(p, 1, NULL, 0); + tx_delete_tree(p->work); + if (tx_exists(p->work)) return 0; /* (a file of it is held open: no second run of the game beside this one's files) */ + /* (the newest save: a finished commit still waiting, else the committed one) */ + const unsigned left = tx_ready_highest(p, ready, sizeof ready); + const char *from = left ? ready : p->save; + if (tx_is_dir(from)) { + if (!tx_copy_tree(from, p->work, 0)) { + tx_delete_tree(p->work); + return 0; + } + } else tx_make_dirs(p->work); + tx_make_dirs(p->save); /* (the save "exists" from its first mount on, as before: IsSaveDataExisting) */ + return tx_is_dir(p->work); +} + +/* Every file of a folder written out to the disk (FlushFileBuffers). 1: all of them */ +static int tx_flush_tree(const char *dir) { + char pat[HOS_FS_PATH]; + WIN32_FIND_DATAA fd; + snprintf(pat, sizeof pat, "%s\\*", dir); + HANDLE f = FindFirstFileA(pat, &fd); + if (f == INVALID_HANDLE_VALUE) return tx_is_dir(dir); /* (an empty folder, where the listing has no "." entry) */ + int ok = 1; + do { + if (!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, "..") || (fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) continue; + char sub[HOS_FS_PATH]; + snprintf(sub, sizeof sub, "%s\\%s", dir, fd.cFileName); + if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { ok &= tx_flush_tree(sub); continue; } + HANDLE h = CreateFileA(sub, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if (h == INVALID_HANDLE_VALUE) { ok = 0; continue; } + if (!FlushFileBuffers(h)) ok = 0; + CloseHandle(h); + } while (FindNextFileA(f, &fd)); + FindClose(f); + return ok; +} + +/* nn::fs::Commit of the save mount, in two parts so that the game's thread pays only the first (5 ms for a real save, against + 30 with the second: nn_fs.c runs that one on a thread of its own). + + save_tx_stage, on the game's thread: the working copy as it is at this moment, copied to the staging folder. What the game + writes after its Commit returned is not in it. 1: staged; 0: the staging copy could not be made and the working copy's files + were copied over the save's, as before this file (nothing left to finish); -1: that failed too */ +static int save_tx_stage(const SaveTxPaths *p) { + if (tx_exists(p->staging)) tx_delete_tree(p->staging); + if (tx_crashed()) return -1; + if (!tx_exists(p->staging) && tx_copy_tree(p->work, p->staging, 0)) return 1; + tx_delete_tree(p->staging); /* (the disk is full, or something sits where the folder goes) */ + return tx_copy_tree(p->work, p->save, 0) ? 0 : -1; +} + +/* save_tx_finish, after a save_tx_stage that returned 1: the staged copy written out, made the newest save and put in place. + 1: committed (swapped in, or waiting as ready-); 0: a step before the commit point failed and the staged files were + copied over the save's; -1: that failed too. backup (may be NULL) gets the backup's folder when one was made */ +static int save_tx_finish(const SaveTxPaths *p, char *backup, size_t cap) { + if (backup && cap) backup[0] = 0; + const int staged = tx_flush_tree(p->staging); + char ready[HOS_FS_PATH]; + const unsigned n = tx_ready_highest(p, NULL, 0) + 1; + snprintf(ready, sizeof ready, "%s\\ready-%u", p->tx, n); + if (tx_crashed()) return -1; + if (!staged || !tx_move(p->staging, ready, 3)) { /* (a file could not be written out, or the folder cannot be renamed) */ + const int copied = tx_copy_tree(p->staging, p->save, 0); + tx_delete_tree(p->staging); + return copied ? 0 : -1; + } + /* (committed: from here a death leaves ready-, which the next start puts in place) */ + if (tx_crashed()) return 1; + tx_ready_delete_below(p, n); + if (tx_swap(p, ready)) { + if (tx_crashed()) return 1; + tx_old_away(p, 0, backup, cap); + } + return 1; +} + +/* Both parts one after the other (the tests, and a build without threads) */ +__attribute__((unused)) static int save_tx_commit(const SaveTxPaths *p, char *backup, size_t cap) { + if (backup && cap) backup[0] = 0; + const int staged = save_tx_stage(p); + if (staged != 1 || save_tx_dead) return save_tx_dead ? -1 : staged; + return save_tx_finish(p, backup, cap); +} diff --git a/nx2/build.py b/nx2/build.py index 9d741d8..fdceadb 100644 --- a/nx2/build.py +++ b/nx2/build.py @@ -537,6 +537,11 @@ def ffmpeg_version_key(): # instead of Windows' write watch (GetWriteWatch over the 6 GB MKW's GPU sees took ~20 ms a scan, in one thread or # four: the race's replay waited ~12 ms a frame for it). RX_SOFT_WRITE_WATCH=0: Windows' write watch (a full rebuild) WRITE_TRACK = [] if os.environ.get("RX_SOFT_WRITE_WATCH") == "0" else ["-DA64_WRITE_TRACK"] +# (triforce) RX_MARK_ONE=1: a lifted store marks the page of its first byte alone (one store and three instructions less +# at every guest store; the marks were ~5-8% of the game threads' CPU at OoT's Market), and the collection takes a mark +# as "this page and the next" (host/rx_backend.c collect_in): a store of up to 4 KB reaches no further. A full rebuild. +if WRITE_TRACK and os.environ.get("RX_MARK_ONE") == "1": + WRITE_TRACK = WRITE_TRACK + ["-DA64_MARK_ONE"] # (hookshot) the host CPU the lifted game and the runtime are built for: x86-64-v3 (AVX2, FMA, BMI1/2, LZCNT, # MOVBE; Haswell and Zen 1 on). With FMA the FPX1 fast paths cover double-precision FMA (RECOMP_FPX_FMA64; it took @@ -802,6 +807,15 @@ def main(): # (hookshot) code_hot.c, the lift's hot functions together (A64_HOT_FUNCS + A64_HOT_CHUNK=1): at RX_HOT_OPT # (default O2), so the hot callees inline into their hot callers hot_fl = [("-" + os.environ.get("RX_HOT_OPT", "O2")) if f == "-" + a.opt else f for f in fl] + # (triforce) RX_HOT_CAPTURE=: the hot pieces' own -capture-tracking-max-uses-to-explore (see flags()): a hot function + # that names the context more often than the limit is compiled as if the context escaped (every guest store may change + # it), and the largest functions are among the hottest + if os.environ.get("RX_HOT_CAPTURE"): + hot_fl = [("-capture-tracking-max-uses-to-explore=" + os.environ["RX_HOT_CAPTURE"]) + if f.startswith("-capture-tracking-max-uses-to-explore=") else f for f in hot_fl] + # (triforce) RX_HOT_FLAGS=: more flags for the hot pieces alone (a variant of them without building the rest again) + if os.environ.get("RX_HOT_FLAGS"): + hot_fl = hot_fl + os.environ["RX_HOT_FLAGS"].split() # (hookshot) RX_PGO=gen: code_hot.c counts its branches (host/pgo_rt.c writes them; -O1 builds much sooner, and the counters are # placed before the optimiser, so a profile from it fits the -O2 build). RX_PGO=: built with that profile if PGO == "gen": @@ -925,6 +939,18 @@ def main(): ngx_deps = tree_headers(os.path.join(NGX_SDK, "include")) if NGX_SDK else b"" if NGX_SDK: print("the Vulkan back end: with NVIDIA DLSS (NVIDIA's NGX SDK in %s)" % NGX_SDK, flush=True) + # (NVIDIA DLSS frame generation, a local build's option: HOOKSHOT_STREAMLINE_SDK=. + # Only vk_dlssg.cpp sees its headers; nothing of it is linked (sl.interposer.dll is loaded beside the game at run + # time). Without it that file is the stubs that say it is not there. Windows only) + sl_sdk = os.environ.get("HOOKSHOT_STREAMLINE_SDK", "") + if sl_sdk and (LINUX or not os.path.isfile(os.path.join(sl_sdk, "include", "sl_dlss_g.h"))): + print("build: HOOKSHOT_STREAMLINE_SDK=%s has no include/sl_dlss_g.h (or this is not a Windows build): built " + "without NVIDIA DLSS frame generation" % sl_sdk, flush=True) + sl_sdk = "" + sl_flags = ["-DHOOKSHOT_DLSSG", "-I" + os.path.join(sl_sdk, "include")] if sl_sdk else [] + sl_deps = tree_headers(os.path.join(sl_sdk, "include")) if sl_sdk else b"" + if sl_sdk: + print("the Vulkan back end: with NVIDIA DLSS frame generation (NVIDIA's Streamline SDK in %s)" % sl_sdk, flush=True) # (AMD FSR 4, a build's option too: only vk_fsr4.cpp sees AMD's headers; without them it is the stubs that say FSR is # not there) fsr_flags = ["-DHOOKSHOT_FSR", "-I" + os.path.join(kit.HERE, "..", "third_party", "fidelityfx")] if FSR else [] @@ -974,12 +1000,14 @@ def main(): vflags + ([shader_stamp] if os.path.basename(src) == "vk_shader.cpp" else []) + ([stamp_def] if os.path.basename(src) == "vk_menu.cpp" else []) + (ngx_flags if os.path.basename(src) == "vk_dlss.cpp" else []) + + (sl_flags if os.path.basename(src) == "vk_dlssg.cpp" else []) + (imgui_flags if os.path.basename(src) in imgui_users else []) + (fsr_flags if os.path.basename(src) == "vk_fsr4.cpp" else []) + (renodx_flags if os.path.basename(src) == "vk_renodx.cpp" else []), vk_lib_deps if lib else vdeps + (logo_text if os.path.basename(src) == "vk_imgui.cpp" else b"") + (renodx_deps if os.path.basename(src) == "vk_renodx.cpp" else b"") + (ngx_deps if os.path.basename(src) == "vk_dlss.cpp" else b"") + + (sl_deps if os.path.basename(src) == "vk_dlssg.cpp" else b"") + (imgui_deps if os.path.basename(src) in imgui_users else b""))) if NGX_SDK and not LINUX and toolchain.mingw(): # (llvm-mingw: NVIDIA's static library was built with Microsoft's compiler for its static runtime (/MT). What it asks diff --git a/nx2/make_game.py b/nx2/make_game.py index e4109cc..ad9664e 100644 --- a/nx2/make_game.py +++ b/nx2/make_game.py @@ -1255,6 +1255,103 @@ def fetch_ngx_sdk(a): fail("NVIDIA's DLSS SDK could not be downloaded (%s). Check the connection and build again, or untick NVIDIA DLSS." % e) +def fsr_script(): + """scripts/fsr_sdk.py: AMD's two FSR 4 libraries; None where the builder carries no scripts/""" + import importlib.util + path = os.path.join(ROOT, "scripts", "fsr_sdk.py") + if not os.path.isfile(path): + return None + spec = importlib.util.spec_from_file_location("fsr_sdk", path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +def fsr_runtime_dir(work): + """Where AMD's FSR 4 libraries are kept between builds: HOOKSHOT_FSR_SDK, else the fsr-sdk folder of the work folder""" + return os.environ.get("HOOKSHOT_FSR_SDK", "") or os.path.join(work, "fsr-sdk") + + +def fetch_fsr_runtime(work): + """(--fsr, Windows) AMD's two signed FSR 4 libraries into fsr_runtime_dir, only what is missing. A download that fails + is said and the build goes on: the game then does not offer AMD FSR 4 until a build gets them""" + fsr = fsr_script() + if LINUX or fsr is None: + return + print(fsr.NOTICE, flush=True) + try: + fsr.fetch(fsr_runtime_dir(work), say=lambda line: print(line, flush=True)) + except (OSError, ValueError) as e: + print("AMD's FSR 4 libraries could not be downloaded (%s): the game will not offer AMD FSR 4. Build again with a " + "connection to get them." % e, flush=True) + + +def place_fsr_runtime(out, work, wanted): + """(AMD FSR 4) a game built with it gets AMD's two libraries and their license text beside its exe, where + gpu/vk/vk_fsr4.cpp looks; a folder built with them before and without FSR 4 now loses them. Returns what was placed""" + fsr = fsr_script() + if LINUX or fsr is None: + return [] + if not wanted or not fsr.complete(fsr_runtime_dir(work)): + if not wanted: + fsr.remove_runtime(out) + return [] + placed = fsr.install_runtime(fsr_runtime_dir(work), out) + print("AMD FSR 4: %s (AMD's, MIT license)" % ", ".join(os.path.basename(p) for p in placed), flush=True) + return placed + + +def streamline_script(): + """scripts/streamline_sdk.py: NVIDIA's Streamline files for DLSS frame generation; None where the builder carries no + scripts/ (frame generation is then left out, DLSS itself is not)""" + import importlib.util + path = os.path.join(ROOT, "scripts", "streamline_sdk.py") + if not os.path.isfile(path): + return None + spec = importlib.util.spec_from_file_location("streamline_sdk", path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +def fetch_streamline_sdk(a): + """With --fetch-ngx-sdk on Windows (the NVIDIA DLSS tick, NVIDIA's licenses accepted): NVIDIA's Streamline files for DLSS + frame generation too, into HOOKSHOT_STREAMLINE_SDK (default: the streamline folder in HOOKSHOT_NGX_SDK), which + nx2/build.py then builds gpu/vk/vk_dlssg.cpp with. A download that fails leaves frame generation out of this build and + says so: the build goes on, with DLSS as before""" + if LINUX or not a.accept_nvidia_license: + return + sl = streamline_script() + if sl is None: + return + sdk = os.environ.get("HOOKSHOT_STREAMLINE_SDK", "") or os.path.join(os.environ.get("HOOKSHOT_NGX_SDK", ""), "streamline") + print(sl.NOTICE, flush=True) + try: + sl.fetch(sdk, say=lambda line: print(line, flush=True)) + except (OSError, ValueError) as e: + print("NVIDIA's Streamline could not be downloaded (%s): this build has no DLSS frame generation. Build again " + "with a connection to get it." % e, flush=True) + os.environ.pop("HOOKSHOT_STREAMLINE_SDK", None) + return + os.environ["HOOKSHOT_STREAMLINE_SDK"] = sdk + + +def place_dlssg_runtime(out): + """(NVIDIA DLSS frame generation) a game built with NVIDIA's Streamline (HOOKSHOT_STREAMLINE_SDK, as nx2/build.py takes it) + gets Streamline's DLLs, nvngx_dlssg.dll and their license texts in its dlssg folder, where gpu/vk/vk_dlssg.cpp looks. A + folder built with them before and without them now loses them. Returns the folder, or None""" + sl = streamline_script() + if sl is None: + return None + sdk = os.environ.get("HOOKSHOT_STREAMLINE_SDK", "") + if LINUX or not sl.complete(sdk): + sl.remove_runtime(out) + return None + dst = sl.install_runtime(sdk, out) + print("NVIDIA DLSS frame generation: %s (NVIDIA's, under NVIDIA's licenses: never share this game folder)" % dst, flush=True) + return dst + + def ngx_built(): """This build has NVIDIA DLSS: HOOKSHOT_NGX_SDK holds this platform's library (what nx2/build.py takes)""" sdk = os.environ.get("HOOKSHOT_NGX_SDK", "") @@ -1350,7 +1447,8 @@ def parse_args(): p.add_argument("--fetch-ngx-sdk", action="store_true", help="download NVIDIA's DLSS SDK into HOOKSHOT_NGX_SDK before the build") p.add_argument("--accept-nvidia-license", action="store_true", help="NVIDIA's DLSS SDK license, accepted (with --fetch-ngx-sdk)") # (AMD FSR 4, off unless asked: nothing is downloaded. gpu/vk/vk_fsr4.cpp) - p.add_argument("--fsr", action="store_true", help="build with AMD FSR 4 (AMD's DLLs are not included: see the readme)") + p.add_argument("--fsr", action="store_true", help="build with AMD FSR 4; on Windows AMD's two libraries are downloaded " + "from AMD's repository and put beside the game (scripts/fsr_sdk.py)") p.add_argument("--no-fsr", action="store_true", help="with --update: build without AMD FSR 4 even if the folder had it") # (RenoDX's HDR, in every build since 11-13: its shaders and the Slang compiler are downloaded and compiled here. # scripts/renodx_shaders.py, gpu/vk/vk_renodx.cpp) @@ -1423,9 +1521,11 @@ def main(a=None): drop_stale_objects(work) if getattr(a, "fetch_ngx_sdk", False): fetch_ngx_sdk(a) + fetch_streamline_sdk(a) # (DLSS frame generation's files, with the same tick) # (AMD FSR 4: the build's switch for nx2/build.py, and the folder's mark for the next --update) if fsr_wanted(a, out): os.environ["HOOKSHOT_FSR"] = "1" + fetch_fsr_runtime(work) # (AMD's two libraries, for the game folder: place_fsr_runtime at the end) open(os.path.join(out, FSR_MARK), "w").close() if os.environ.get("HOOKSHOT_VK_PREBUILT"): # (the AppImage's back end, built without FSR 4) print("--fsr: this build uses the prebuilt Vulkan back end (HOOKSHOT_VK_PREBUILT), which has no AMD FSR 4: the " @@ -1709,6 +1809,8 @@ def main(a=None): pass drop_old_program(out) place_ngx_runtime(out) + place_dlssg_runtime(out) + place_fsr_runtime(out, work, os.environ.get("HOOKSHOT_FSR") == "1") # every shader of the game, for its first start to translate (gpu/vk/vk_shader.cpp seed_open). Without it the game still runs, compiling them # as it meets them: not worth stopping a finished build for if run(shader_census_cmd(romfs, os.path.join(out, "triforce_shaders.bin")), check=False, output=sys.stderr): diff --git a/nx2/oot_patches.py b/nx2/oot_patches.py index 115c49f..1d2d4a9 100644 --- a/nx2/oot_patches.py +++ b/nx2/oot_patches.py @@ -372,6 +372,42 @@ SOLVER_NEW = SOLVER_OLD.replace( SOLVER_DECL = "extern uint32_t hos_oot_solver_steps(uint32_t game, uint32_t scaled_bits, uint32_t per_frame_bits);\n" +# Three more places that go wrong with a frame step under one frame (host/oot_high_fps.h has why and the logic; at a cap of 60 the +# host hands back what the game computes). Found and fixed for this build by NX2navi (its recomp/patches-v111.txt, the sets +# ofollow, owater and orecord: main+0x13d704, +0x13a064, +0x389ac20; +0x3876014, +0x38827f8, +0x632cfc; +0x32926c8, +0x3293410, +# +0x3296b08, +0x3296da4; main is at 0x80020000 here), there as code words, here as hooks in the lifted C: +# - the character controller's three follow-surface updates compare the physics' inverse step with 100.0 and keep a tenth of +# the floor's velocity above it: every update above 100 fps, so a moving platform slid out from under Link. The 100.0 comes +# from hos_oot_floor_cut (100 / the frame's 60 fps frames while that is under one). +# - the two water managers and the interactive floors (the Deku Tree's ground webs) run seconds / (1/60 s), cut to whole, +# fixed steps: none in an update shorter than 1/60 s, so the surfaces stood still above 60 fps. Before the cut the quotient +# goes through hos_oot_sim_steps (x19 the object, each function's x0): the whole steps with the object's remainder kept. +# - the ocarina recorder's clock adds (unsigned)frame step at four places: 0 above 60 fps, so a recording lost its rests. +# hos_oot_record_frames carries the fraction. +FLOOR_HEADS = ("void f_8015d490(Ctx *__restrict A64_NOESC c) {\n", "void f_80159dc0(Ctx *__restrict A64_NOESC c) {\n", + "void f_838baa40(Ctx *__restrict A64_NOESC c) {\n") +# (mov w8, #0x42c80000; fmov s1, w8: the 100.0 of the compare that follows) +FLOOR_OLD = (" c->x[8] = 0x42c80000ULL;\n c->x[8] &= 0xFFFFFFFFULL;\n" + " { a64_vset64(c->vreg[1],0); memcpy(&c->vreg[1][0],&c->x[8],4); }\n") +FLOOR_NEW = FLOOR_OLD.replace("c->x[8] = 0x42c80000ULL;", + "c->x[8] = hos_oot_floor_cut(); /* (nx2/oot_patches.py: a floor's carry above 100 fps) */", 1) +FLOOR_DECL = "extern uint32_t hos_oot_floor_cut(void);\n" +# (a function and the register its cut goes to: fdiv s0, s8, s0 then fcvtzs w, s0, the function's one cut of s0 into that register) +SIM_SITES = (("void f_83895f20(Ctx *__restrict A64_NOESC c) {\n", 8), ("void f_838a2660(Ctx *__restrict A64_NOESC c) {\n", 8), + ("void f_80652cb0(Ctx *__restrict A64_NOESC c) {\n", 22)) +SIM_OLD = " c->x[%d]=recomp_fp_to_int(c->vreg[0][0]&UINT64_C(0xffffffff),32,32,1,3,c->fpcr,&c->fpsr);\n" +SIM_HOOK = (" a64_vset64(c->vreg[0],hos_oot_sim_steps(c->x[19], (uint32_t)c->vreg[0][0]));" + " /* (nx2/oot_patches.py: fixed steps above 60 fps) */\n") +SIM_DECL = "extern uint32_t hos_oot_sim_steps(uint64_t object, uint32_t quotient_bits);\n" +# (a function and how many times it adds the step to the clock: fcvtzu w8, s0) +RECORD_SITES = (("void f_832b2690(Ctx *__restrict A64_NOESC c) {\n", 1), ("void f_832b32b0(Ctx *__restrict A64_NOESC c) {\n", 1), + ("void f_832b6840(Ctx *__restrict A64_NOESC c) {\n", 2)) +RECORD_OLD = " c->x[8]=recomp_fp_to_int(c->vreg[0][0]&UINT64_C(0xffffffff),32,32,0,3,c->fpcr,&c->fpsr);\n" +RECORD_NEW = (" c->x[8]=hos_oot_record_frames((uint32_t)c->vreg[0][0]);" + " /* (nx2/oot_patches.py: the ocarina recorder's clock above 60 fps) */\n") +RECORD_DECL = "extern uint32_t hos_oot_record_frames(uint32_t step_bits);\n" + + def _function(s, head_text): """(start, end) of the function whose head is head_text, or None""" head = s.find(head_text) @@ -420,6 +456,108 @@ def patch_solver_steps(s): return s, done +def patch_floor_carry(s): + """The follow-surface updates' compare: (s, how many of the three are in s with the hook, made now or before)""" + done = 0 + for head_text in FLOOR_HEADS: + at = _function(s, head_text) + if at is None: + continue + head, end = at + body = s[head:end] + if body.count(FLOOR_NEW) == 1: + done += 1 + elif body.count(FLOOR_OLD) == 1: + s = s[:head] + body.replace(FLOOR_OLD, FLOOR_NEW, 1) + s[end:] + done += 1 + if done and FLOOR_DECL not in s: + s = s.replace('#include "a64.h"\n', '#include "a64.h"\n' + FLOOR_DECL, 1) + return s, done + + +def patch_sim_steps(s): + """The height-field surfaces' fixed steps: (s, how many of the three places are in s with the hook, made now or before)""" + done = 0 + for head_text, reg in SIM_SITES: + at = _function(s, head_text) + if at is None: + continue + head, end = at + body = s[head:end] + old = SIM_OLD % reg + if body.count(SIM_HOOK + old) == 1: + done += 1 + elif body.count(old) == 1 and SIM_HOOK not in body: + s = s[:head] + body.replace(old, SIM_HOOK + old, 1) + s[end:] + done += 1 + if done and SIM_DECL not in s: + s = s.replace('#include "a64.h"\n', '#include "a64.h"\n' + SIM_DECL, 1) + return s, done + + +def patch_record_clock(s): + """The ocarina recorder's clock: (s, how many of its four adds are in s with the hook, made now or before). A function + whose adds are not as many as RECORD_SITES says (another game version) is left as it is.""" + done = 0 + for head_text, count in RECORD_SITES: + at = _function(s, head_text) + if at is None: + continue + head, end = at + body = s[head:end] + if body.count(RECORD_NEW) == count and RECORD_OLD not in body: + done += count + elif body.count(RECORD_OLD) == count and RECORD_NEW not in body: + s = s[:head] + body.replace(RECORD_OLD, RECORD_NEW) + s[end:] + done += count + if done and RECORD_DECL not in s: + s = s.replace('#include "a64.h"\n', '#include "a64.h"\n' + RECORD_DECL, 1) + return s, done + + +# The sprint button (host/oot_sprint.h has the rules, host/oot_sprint.c the game's addresses; after NX2navi's set "sprint"). +# Link sprints while his input flags hold the dash input (bit 4), which the game sets only out of a roll. Two functions load +# those flags and test the bit: his movement update f_804caa80 (to start the sprint and to keep it), and the update of his +# stance and camera f_80c225d0 (the low byte). After each load the host gets the flags and gives them back, with the bit +# while the button's sprint is on. With the Sprint button row off the call returns what it got. +SPRINT_LINK_HEAD = "void f_804caa80(Ctx *__restrict A64_NOESC c) {\n" +SPRINT_LINK_OLD = (" c->x[10]=recomp_load64(c,c->x[19] + 80);\n" + " c->x[9]=recomp_load32(c,c->x[19] + 44);\n" + " c->x[10]=recomp_load64(c,c->x[10] + 0);\n") +SPRINT_LINK_ADD = " c->x[10]=hos_oot_sprint_link(c,c->x[19],c->x[10]); /* (nx2/oot_patches.py: the sprint button) */\n" +SPRINT_STANCE_HEAD = "void f_80c225d0(Ctx *__restrict A64_NOESC c) {\n" +SPRINT_STANCE_OLD = (" c->x[8]=recomp_load64(c,c->x[19] + 80);\n" + " c->x[8]=recomp_load8(c,c->x[8] + 0);\n" + "if (((c->x[8] >> 4) & 1)) { goto L_80c27f54; }\n") +SPRINT_STANCE_NEW = (" c->x[8]=recomp_load64(c,c->x[19] + 80);\n" + " c->x[8]=recomp_load8(c,c->x[8] + 0);\n" + " c->x[8]=hos_oot_sprint_stance(c,c->x[19],c->x[8]); /* (nx2/oot_patches.py: the sprint button) */\n" + "if (((c->x[8] >> 4) & 1)) { goto L_80c27f54; }\n") +SPRINT_DECL = ("extern uint64_t hos_oot_sprint_link(Ctx *c, uint64_t link, uint64_t flags);\n" + "extern uint64_t hos_oot_sprint_stance(Ctx *c, uint64_t link, uint64_t flags);\n") + + +def patch_sprint(s): + """The sprint button's two calls: (s, how many of the two are in s, made now or before). A function whose code differs + (another game version) is left as it is.""" + done = 0 + for head_text, old, new in ((SPRINT_LINK_HEAD, SPRINT_LINK_OLD, SPRINT_LINK_OLD + SPRINT_LINK_ADD), + (SPRINT_STANCE_HEAD, SPRINT_STANCE_OLD, SPRINT_STANCE_NEW)): + at = _function(s, head_text) + if at is None: + continue + head, end = at + body = s[head:end] + if body.count(new) == 1: + done += 1 + elif body.count(old) == 1: + s = s[:head] + body.replace(old, new) + s[end:] + done += 1 + if done and SPRINT_DECL not in s: + s = s.replace('#include "a64.h"\n', '#include "a64.h"\n' + SPRINT_DECL, 1) + return s, done + + def patch_text_speed(s): """The scale at the head of the letters' block of f_80b1e140; None when the file has no such function, s unchanged when the function is there but its code differs (another game version).""" @@ -476,6 +614,8 @@ def apply(gen, *, protect_hearts=False, required=False): step_sites, step_files = 0, [] # (the divides with the hook, and the files that have it) whole_sites = 0 # (the counters of WHOLE_SITES with the whole frames: patch_whole_frames) sound_jobs = solver_sites = 0 # (patch_sound_job, patch_solver_steps: the places found with their hooks) + floor_sites = sim_sites = record_sites = 0 # (patch_floor_carry, patch_sim_steps, patch_record_clock: the same) + sprint_sites = 0 # (patch_sprint: its two calls) for path in sorted(glob.glob(os.path.join(glob.escape(gen), "code_*.c"))): with open(path, encoding="utf-8", newline="") as f: s = f.read() @@ -561,12 +701,41 @@ def apply(gen, *, protect_hearts=False, required=False): if solver != out: print("oot_patches: the solver steps rounded up above 60 fps in", os.path.basename(path)) out = solver + floor, found = patch_floor_carry(out) + floor_sites += found + if floor != out: + print("oot_patches: a floor's carry above 100 fps in", os.path.basename(path)) + out = floor + sim, found = patch_sim_steps(out) + sim_sites += found + if sim != out: + print("oot_patches: the water and floor surfaces' fixed steps above 60 fps in", os.path.basename(path)) + out = sim + record, found = patch_record_clock(out) + record_sites += found + if record != out: + print("oot_patches: the ocarina recorder's clock above 60 fps in", os.path.basename(path)) + out = record + sprinted, found = patch_sprint(out) + sprint_sites += found + if sprinted != out: + print("oot_patches: the sprint button in", os.path.basename(path)) + out = sprinted if out != s: whole_write.write_whole(path, out, newline="") if sound_jobs != 1: # (without it, above 60 fps sounds and then the music can stop after a few loading zones) print("oot_patches: WARNING: the sound job was found %d times, not once" % sound_jobs) if solver_sites != len(SOLVER_HEADS): # (without them, above 60 fps moving platforms can drop Link) print("oot_patches: WARNING: the solver step count was found at %d of %d places" % (solver_sites, len(SOLVER_HEADS))) + if floor_sites != len(FLOOR_HEADS): # (without them, above 100 fps a moving platform carries Link at a tenth of its speed) + print("oot_patches: WARNING: a floor's carry was found at %d of %d places" % (floor_sites, len(FLOOR_HEADS))) + if sim_sites != len(SIM_SITES): # (without them, above 60 fps the water and web surfaces stand still) + print("oot_patches: WARNING: the surfaces' fixed steps were found at %d of %d places" % (sim_sites, len(SIM_SITES))) + if record_sites != sum(n for _, n in RECORD_SITES): # (without them, above 60 fps a recorded song loses its rests) + print("oot_patches: WARNING: the ocarina recorder's clock was found at %d of %d places" + % (record_sites, sum(n for _, n in RECORD_SITES))) + if sprint_sites != 2: # (without them the Sprint button row does nothing) + print("oot_patches: WARNING: the sprint button's calls were found at %d of 2 places" % sprint_sites) for what, n in done.items(): if n != 1: print("oot_patches: WARNING: the %s was found %d times" % (what, n)) diff --git a/nx2/oot_wide.py b/nx2/oot_wide.py index 8faad91..90ac70f 100644 --- a/nx2/oot_wide.py +++ b/nx2/oot_wide.py @@ -40,13 +40,15 @@ def _size(name): % (name, name, name)) -HUD = ("{ extern volatile float hos_wide_squeeze; const float _q = hos_wide_squeeze; if (_q > 0.0f) { float _m00, _m11; " +# (the row goes to the host with the canvas' screen: host/oot_wide_hud.c keeps it for the screens of KEEP_VTABLES, squeezes it +# for the rest, and moves the layouts of its table out to an edge with the Insert menu's Ultrawide HUD row on Screen edges) +HUD = ("{ extern volatile float hos_wide_squeeze; if (hos_wide_squeeze > 0.0f) { float _m00, _m11; " "memcpy(&_m00, c->vreg[1], 4); memcpy(&_m11, (const uint8_t *)c->vreg[4] + 4, 4); " - "if (_m00 == %s && _m11 == %s) { const uint64_t _vt = recomp_load64(c, c->x[0] - 0x1b8); " - "extern volatile uint32_t hos_wide_keep_mask; const uint32_t _kb = %s; " - "if (!(_kb & hos_wide_keep_mask)) { float _row[4]; memcpy(_row, c->vreg[1], 16); for (int _k = 0; _k < 4; _k++) _row[_k] *= _q; " - "memcpy(c->vreg[1], _row, 16); } } } } /* (nx2/oot_wide.py: HUD in the middle 16:9) */" - % (UI_M00, UI_M11, " : ".join("_vt == 0x%xULL ? %du" % (v, 1 << k) for k, v in enumerate(KEEP_VTABLES)) + " : 0u")) + "if (_m00 == %s && _m11 == %s) { extern void hos_wide_hud_row(Ctx *c, uint64_t screen, float *first, float *move); " + "float _row[4], _tx; memcpy(_row, c->vreg[1], 16); memcpy(&_tx, c->vreg[6], 4); " + "hos_wide_hud_row(c, c->x[0] - 0x1b8, _row, &_tx); memcpy(c->vreg[1], _row, 16); memcpy(c->vreg[6], &_tx, 4); } } }" + " /* (nx2/oot_wide.py: HUD in the middle 16:9, or at the screen's edges) */" + % (UI_M00, UI_M11)) MOVIE = ("{ extern volatile float hos_wide_margin; const float _m = hos_wide_margin; if (_m > 0.0f) { " "const uint64_t _vp = c->x[29] - 40; uint32_t _ua = (uint32_t)recomp_load32(c, _vp + 8), _ub = (uint32_t)recomp_load32(c, _vp + 16); " diff --git a/nx2/prompt_assets.py b/nx2/prompt_assets.py index 18fdb5f..9858ab4 100644 --- a/nx2/prompt_assets.py +++ b/nx2/prompt_assets.py @@ -59,8 +59,9 @@ SWITCH_ICON = {"south": 0xE041, "east": 0xE040, "west": 0xE043, "north": 0xE042, STATIC = {0xE049: "dpad", 0xE064: "dpad_vertical_outline", 0xE065: "dpad_horizontal_outline"} -def zs_dictionary(romfs): - """zs.zsdic, out of Pack/ZsDic.pack.zs (a SARC, itself zstd without a dictionary)""" +def zs_dictionary(romfs, which=b"zs.zsdic"): + """zs.zsdic (or another of its dictionaries: pack.zsdic for the packs), out of Pack/ZsDic.pack.zs (a SARC, itself zstd + without a dictionary)""" import zstandard with open(os.path.join(romfs, "Pack", "ZsDic.pack.zs"), "rb") as f: b = zstandard.ZstdDecompressor().decompressobj().decompress(f.read()) @@ -71,9 +72,9 @@ def zs_dictionary(romfs): for i in range(n): attr, start, end = struct.unpack_from(" len(lay): + continue + v = struct.unpack_from(f"<{n}f", lay, at) + if any(v[1::2]) or not any(k in xs for k in v[::2]) or any(k and k not in xs for k in v[::2]): + continue + out += [at + 8 * k for k in range(n // 2) if v[2 * k] in xs] + if o >= 8 and struct.unpack_from("> 8): + e, value = struct.unpack_from("= kh >> 8: + return None + at = keys + struct.unpack_from("> 8 + if e >> 24 != 0xc0 or ah & 0xff != 0xc0 or not 1 <= count <= 8 or set(d[value + 4:value + 4 + count]) != {0xd2}: + return None + vals = value + 4 + ((count + 3) & ~3) + return [(vals + 4 * j, struct.unpack_from("> 12) & (A64_DIRTY_PAGES - 1)) +#ifdef A64_MARK_ONE /* (triforce, build.py RX_MARK_ONE=1: the first byte's page alone. The collection takes a mark as "this page + and the next", host/rx_backend.c collect_in: a store of up to 4 KB reaches no further) */ +#define A64_MARK(a, n) (__atomic_store_n(&a64_dirty[A64_DIRTY_AT(a)], 1, __ATOMIC_RELEASE)) +#else #define A64_MARK(a, n) (__atomic_store_n(&a64_dirty[A64_DIRTY_AT(a)], 1, __ATOMIC_RELEASE), \ - __atomic_store_n(&a64_dirty[A64_DIRTY_AT((a) + (n) - 1)], 1, __ATOMIC_RELEASE))""" + __atomic_store_n(&a64_dirty[A64_DIRTY_AT((a) + (n) - 1)], 1, __ATOMIC_RELEASE)) +#endif""" SUBST = { "a64.h": [ diff --git a/readme.md b/readme.md index aea7088..3f0e45d 100644 --- a/readme.md +++ b/readme.md @@ -173,7 +173,7 @@ Close the game and the emulator before converting. | What happens | What to try | |---|---| | Setup says the build isn't supported | Check you chose the right game, with its update installed | -| Setup says GitHub could not be reached | Check your connection; you can still play what you have | +| Setup says the Triforce site could not be reached | Check your connection; you can still play what you have | | The game opens and closes straight away | Update your graphics driver | | A short hitch the first time something appears | Expected for now: shaders are being prepared, and the same spot is smooth next time | | The wrong language | Edit the code in `language.txt` beside the game; the file lists them all | diff --git a/scripts/fsr_sdk.py b/scripts/fsr_sdk.py new file mode 100644 index 0000000..c3fab5f --- /dev/null +++ b/scripts/fsr_sdk.py @@ -0,0 +1,125 @@ +"""AMD's FSR 4 libraries for a build with AMD FSR 4 (HOOKSHOT_FSR=1, gpu/vk/vk_fsr4.cpp). + +The build itself needs nothing from here: AMD's headers are in third_party/fidelityfx. What the game needs at run time, +beside its exe, are AMD's two signed libraries of FidelityFX SDK 2.4.0, which this fetches from AMD's own repository at +that tag, each checked against its SHA-256: + - amd_fidelityfx_loader_vk.dll (39 KB) + - amd_fidelityfx_upscaler_vk.dll (36 MB) +They are AMD's, under the SDK's MIT license (third_party/fidelityfx/LICENSE.md, copied beside them). Without them the +game runs as before and the Upscaler row does not offer AMD FSR 4; with them FSR 4 is offered where AMD's library says +the card runs it (Radeon RX 6000, 7000 and 9000 cards with a current driver). + +Usage: python -I scripts/fsr_sdk.py [--game ] [] + : where the two files are kept between builds; default work/fsr-sdk + --game: copied beside the game's exe too (nx2/make_game.py --fsr does the same for the folder it builds) +Windows only: on Linux the game looks for a Vulkan FSR 4 library of its own (see gpu/vk/vk_fsr4.cpp). +""" +import argparse +import hashlib +import os +import shutil +import sys +import urllib.request + +VERSION = "2.4.0" +BASE = "https://raw.githubusercontent.com/GPUOpen-LibrariesAndSDKs/FidelityFX-SDK/v%s/Kits/FidelityFX/signedbin/" % VERSION +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + +# (file, its size, its SHA-256) +RUNTIME = [ + ("amd_fidelityfx_loader_vk.dll", 39184, "38a56a01fb3565c0757d9f5c1e4e69de4b65e5f2e5769b323b200dc1f142bab9"), + ("amd_fidelityfx_upscaler_vk.dll", 36135696, "4a7e895af6e0b615d40a84d59eb8c2b1054ad91cdd0c739567940270d51bdab7"), +] +LICENSE_SOURCE = os.path.join(ROOT, "third_party", "fidelityfx", "LICENSE.md") +LICENSE_NAME = "amd-fidelityfx-LICENSE.md" + +NOTICE = ("AMD FSR 4 uses AMD's signed libraries of FidelityFX SDK %s (MIT license), downloaded from AMD's repository: " + "github.com/GPUOpen-LibrariesAndSDKs/FidelityFX-SDK" % VERSION) + + +def sha256_file(path): + h = hashlib.sha256() + with open(path, "rb") as f: + for block in iter(lambda: f.read(1 << 20), b""): + h.update(block) + return h.hexdigest() + + +def missing(folder): + """The files that are not in folder, or are other files than the pinned ones""" + return [name for name, _, digest in RUNTIME + if not os.path.isfile(os.path.join(folder, name)) or sha256_file(os.path.join(folder, name)) != digest] + + +def complete(folder): + return bool(folder) and os.path.isdir(folder) and not missing(folder) + + +def download(url): + request = urllib.request.Request(url, headers={"User-Agent": "fsr-sdk-fetch"}) + with urllib.request.urlopen(request, timeout=300) as r: + return r.read() + + +def fetch(folder, say=print, get=download): + """Every pinned file into folder that is not there yet. Raises OSError or ValueError; a file that is not the pinned + one is never left in folder""" + need = missing(folder) if os.path.isdir(folder) else [name for name, _, _ in RUNTIME] + if not need: + say("AMD's FSR 4 libraries are in %s already" % folder) + return + os.makedirs(folder, exist_ok=True) + table = {name: (size, digest) for name, size, digest in RUNTIME} + for name in need: + size, digest = table[name] + say("AMD FSR 4: %s (%.1f MB) from AMD's repository" % (name, size / 1048576.0)) + data = get(BASE + name) + if len(data) != size or hashlib.sha256(data).hexdigest() != digest: + raise ValueError("%s is not the file of FidelityFX SDK %s (%d bytes, another SHA-256)" % (name, VERSION, len(data))) + tmp = os.path.join(folder, name + ".part") + with open(tmp, "wb") as f: + f.write(data) + os.replace(tmp, os.path.join(folder, name)) + + +def install_runtime(folder, game): + """AMD's two libraries and their license text from folder beside the game's exe. Returns what was placed""" + placed = [] + for name, _, _ in RUNTIME: + shutil.copyfile(os.path.join(folder, name), os.path.join(game, name)) + placed.append(os.path.join(game, name)) + if os.path.isfile(LICENSE_SOURCE): + shutil.copyfile(LICENSE_SOURCE, os.path.join(game, LICENSE_NAME)) + return placed + + +def remove_runtime(game): + """A game folder built without FSR 4 after one built with it loses AMD's files. True when any was there""" + gone = False + for name in [name for name, _, _ in RUNTIME] + [LICENSE_NAME]: + path = os.path.join(game, name) + if os.path.isfile(path): + os.remove(path) + gone = True + return gone + + +def main(): + p = argparse.ArgumentParser(description=__doc__.split("\n")[0]) + p.add_argument("folder", nargs="?", default=os.path.join(ROOT, "work", "fsr-sdk")) + p.add_argument("--game") + a = p.parse_args() + print(NOTICE) + try: + fetch(a.folder) + except (OSError, ValueError) as e: + print("AMD's FSR 4 libraries could not be downloaded: %s" % e) + return 1 + if a.game: + for path in install_runtime(a.folder, a.game): + print("AMD FSR 4: %s" % path) + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/scripts/oot_saves.py b/scripts/oot_saves.py index 35ca18d..6a1ec16 100644 --- a/scripts/oot_saves.py +++ b/scripts/oot_saves.py @@ -178,9 +178,34 @@ def pick(found, name, flag, wanted=None): return next(iter(found.items())) +def own_tx(save): + """Hookshot commits its save as a transaction too (host/save_tx.h): /save-tx/ beside /save/ holds + the game's working copy (work) and a finished commit that could not be put in place yet (ready-, newer than the save + folder). That folder for a save folder, or None when there is none.""" + save = os.path.normpath(save) + parent = os.path.dirname(save) + tx = os.path.join(os.path.dirname(parent), "save-tx", os.path.basename(save)) + return tx if os.path.basename(parent).lower() == "save" and os.path.isdir(tx) else None + + +def own_newest(save): + """The save Hookshot loads at its next start: the highest ready- of its transaction folder that holds a save, else the + save folder.""" + tx = own_tx(save) + ready = [] + for name in (os.listdir(tx) if tx else []): + digits = name[6:] + path = os.path.join(tx, name) + if name.startswith("ready-") and digits.isdigit() and len(digits) <= 9 and is_save_folder(path): + ready.append((int(digits), path)) + return max(ready)[1] if ready else save + + def pending_commit(save): """NX2navi writes a save to profile/save-tx first and moves it into profile/save/ after: while something is waiting there, that is the save the game loads next, not the account folder.""" + if own_tx(save): # (Hookshot's own transaction folder, which own_newest reads) + return None parent = os.path.dirname(os.path.normpath(save)) tx = os.path.join(os.path.dirname(parent), "save-tx") if os.path.basename(parent).lower() == "save" and os.path.isdir(tx): @@ -352,7 +377,7 @@ def run(args): _, em = locate(other, args.folder, flag) hs_id, hs_path = pick(hs, "Hookshot", "--hookshot ") em_id, em_path = pick(em, other.display, "--profile ", args.profile) - src, dst = (em_path, hs_path) if args.command == "import" else (hs_path, em_path) + src, dst = (em_path, hs_path) if args.command == "import" else (own_newest(hs_path), em_path) if os.path.realpath(src) == os.path.realpath(dst): raise Fail("%s is both the source and the target" % src) if not args.force: @@ -372,6 +397,10 @@ def run(args): n += 1 backup = "%s-%d" % (base, n) copy_saves(src, dst, backup, args.keep_settings, args.dry_run) + # (into Hookshot: its working copy and any finished commit still waiting belong to the save that was replaced, and the + # game would put such a commit over the copy at its next start) + if args.command == "import" and not args.dry_run and own_tx(dst): + shutil.rmtree(own_tx(dst), ignore_errors=True) def main(argv=None): diff --git a/scripts/renodx_shaders.py b/scripts/renodx_shaders.py index 843729b..c8cac82 100644 --- a/scripts/renodx_shaders.py +++ b/scripts/renodx_shaders.py @@ -49,6 +49,23 @@ SHADERS = [ (0xD08E7AD8, "scene_mapping_hdr_0x09EB4538.frag.slang", 2, "the scene's tone mapping, native HDR, with local exposure"), (0xCCB24C45, "fov_capture_0x0258153C.frag.slang", 1, "the fog, which records the camera's field of view"), ] +# (Triforce, 11-14) The same four game shaders under the other checksums they have. The four above are what a translator +# built with Microsoft's C++ library makes (the lab's builds); a translator built with libc++ (llvm-mingw: EVERY Setup build, +# and the build the patch's author matched on) gives the same programs other bytes, and v0.3.6 shipped with the lab's four +# alone: RenoDX replaced nothing in any player's game, the wide scene format on and the game's own tone mapping drawn (a +# player's log of 11-14: "none of its four shaders has replaced"; a copy of his Setup-built folder run in the lab with +# RX_VK_RENODX_TRACE=1: the frame's last draw is module C749AC23, the patch's encoder). Each entry here names one of the four +# above and is the same replacement under another name. Found by what the modules are, not their bytes (the lab's +# perf\night\spvsig.py: opcode counts, constants, decorations; the two builds' modules differ only in how ids are numbered, +# 41 of the encoder's 1,327 words): of the 24,350 modules a Setup build of v0.3.6 made, exactly one has each of the four's +# signature. C749AC23 and D2EDEE38 are also the patch's; its other two (0D00542E, 9E0C0C3A: v0.3.3's translator) are made by +# neither build any more. A change to the emitter changes all eight: match again the same way +ALSO = { + 0x4AE77AB6: (0xC749AC23,), + 0x66644A1B: (0xD2EDEE38,), + 0xD08E7AD8: (0xCC713D29,), + 0xCCB24C45: (0x93C92C19,), +} FLAGS = ["-stage", "fragment", "-O3", "-g0", "-entry", "main", "-Wno-30056", "-Wno-15205", "-Wno-30081", "-Wno-41035", "-DNDEBUG", "-target", "spirv"] @@ -215,7 +232,7 @@ def fetch(folder, platform, opener=urllib.request.urlopen, say=print): def stamp(): """What a compiled header is made from: the commit, the compiler, the shaders and their flags. In the header's first line, so one made from something else is made again""" - h = hashlib.sha256(repr((COMMIT, SLANG_VERSION, SHADERS, FLAGS, FILES)).encode()) + h = hashlib.sha256(repr((COMMIT, SLANG_VERSION, SHADERS, FLAGS, FILES, sorted(ALSO.items()))).encode()) return "// renodx_shaders.py %s" % h.hexdigest()[:16] @@ -253,6 +270,9 @@ def compile_header(renodx, compiler, out_path, say=print, first_line=None): lines.append("static const struct { uint32_t crc; const uint32_t* words; uint32_t count; int kind; } RENODX_SHADERS[] = {") for crc, name, kind, what in SHADERS: lines.append(" {0x%08Xu, RENODX_%08X, sizeof RENODX_%08X / 4, %d}," % (crc, crc, crc, kind)) + for crc, name, kind, what in SHADERS: # (the same replacement under the game shader's other checksums: ALSO) + for other in ALSO.get(crc, ()): + lines.append(" {0x%08Xu, RENODX_%08X, sizeof RENODX_%08X / 4, %d}," % (other, crc, crc, kind)) lines.append("};") os.makedirs(os.path.dirname(out_path) or ".", exist_ok=True) with open(out_path + ".part", "w", newline="\n", encoding="utf-8") as f: diff --git a/scripts/streamline_sdk.py b/scripts/streamline_sdk.py new file mode 100644 index 0000000..609cfbd --- /dev/null +++ b/scripts/streamline_sdk.py @@ -0,0 +1,245 @@ +"""NVIDIA's Streamline files for a build with NVIDIA DLSS frame generation (HOOKSHOT_STREAMLINE_SDK, gpu/vk/vk_dlssg.cpp). + +NVIDIA's files are not part of Triforce and are never committed or bundled. The sl.*.dll files and the headers are under +Streamline's own MIT license (license.txt); nvngx_dlssg.dll is under the NVIDIA RTX SDKs License (nvngx_dlss.license.txt), +which licenses it for systems with NVIDIA GPUs. Both texts are fetched beside them: read them before using the files. + +This takes them out of NVIDIA's Streamline SDK release at one pinned version, each file checked against its SHA-256, +into , at the paths they have in the SDK: + - include/*.h: what the build reads (only gpu/vk/vk_dlssg.cpp sees them; nothing of Streamline is linked); + - bin/x64/: the six DLLs the game loads at run time (sl.interposer, sl.common, sl.dlss_g, sl.pcl, sl.reflex, nvngx_dlssg); + - license.txt and bin/x64/nvngx_dlss.license.txt. +The release is one archive of 276 MB. Only the parts of it that hold these files are downloaded (HTTP range requests, +about 12 MB); a server that does not serve ranges gets the whole archive read once instead. A folder that already is +NVIDIA's whole SDK, unpacked, serves as it is. + +Usage: python -I scripts/streamline_sdk.py [--accept-nvidia-license] [--game ] [] + : default work/streamline-sdk + --game: the DLLs and the two license texts copied into /dlssg too, where the game looks for them + (nx2/make_game.py does the same for the folder it builds) + --accept-nvidia-license: the licenses accepted without the question (scripts, Setup's NVIDIA DLSS tick) +Then build with HOOKSHOT_STREAMLINE_SDK=. Windows only. +""" +import argparse +import hashlib +import io +import os +import shutil +import sys +import urllib.request +import zipfile + +VERSION = "2.14.16" +URL = "https://github.com/NVIDIA-RTX/Streamline/releases/download/v%s/streamline-sdk-v%s.zip" % (VERSION, VERSION) +SIZE = 276016170 +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +LICENSE_URL = "https://github.com/NVIDIA-RTX/Streamline/blob/v%s/license.txt" % VERSION + +# (path in NVIDIA's SDK, its SHA-256) +HEADERS = [ + ("include/sl.h", "e1e81a7428d15b30db37587e9469bd68a56d630d820a4accec0aee3b17e157dd"), + ("include/sl_appidentity.h", "1337385ac9867d66fa6beb34c750e79aaf25a74a156a47e83f24937299dade87"), + ("include/sl_consts.h", "17de74afda2cb96204ff380464bed926f660d92015fe0304dff4e329509c88b9"), + ("include/sl_core_api.h", "328dc3a2c1dee579c200ca97e2d5b1ec38c893be4ba2745ef5ad506af7172e81"), + ("include/sl_core_types.h", "f420cb052ce14489fc9fa0d933c3ed81a010deb01fa13b1aa8d45da1c5ba3234"), + ("include/sl_deepdvc.h", "c6086ffe6b1e00588368fdb91e4ab9dd9617624e0e4344cd503a2ca73e46d1a4"), + ("include/sl_device_wrappers.h", "af7741305b1a468c3a83efaa2005ecd53d2ffd7aadb8ec6868c9dcb4a8a8e3a1"), + ("include/sl_directsr.h", "65d4e68b2a0f0425ea4d381379172197b81352a50a5e399a8e8f51953e765c71"), + ("include/sl_dlss.h", "d2c8c61fa71794c8ba424cebce5b3079ebfab8e47ed98b4b00cf8b257ecfd6b5"), + ("include/sl_dlss_d.h", "6e51cf9815301c597d0657970a668147e34a20db5a76d0af254ec11248f5c4fe"), + ("include/sl_dlss_g.h", "1fc18cbe004e280df1f787276d08a1b28b8a8c4c65856fbaa659f56dff6a915d"), + ("include/sl_helpers.h", "48629edbf25dfdb6444c91235fc278f0f9a5878243fae83204c5906ce9eacd8a"), + ("include/sl_helpers_vk.h", "82604567d239d00ac0ac2270cec744f6f11266e1d9d5187a95d4954f76d8c41c"), + ("include/sl_hooks.h", "cfeff70a52e1cc012cf4c955a15cc9d0b290f7e084dcb3c0168a9af06ff8f122"), + ("include/sl_matrix_helpers.h", "a65758d85abba1e12845d266d7d5e36aaefa952383cdf093f3143cd6d8653341"), + ("include/sl_nis.h", "f45be3cf149200e1eae081cd144c95726a7a290a89822a5d52b9f538e52484aa"), + ("include/sl_nvperf.h", "025f9f1c8482e75a90f8b8efccdc9cfc53a9fc8b05ba3acc6c3783708b66bd69"), + ("include/sl_pcl.h", "f43c5135fc8d5349ccd345e424f8cf1f61953a9f17e9897204b0741a3ab7b0fb"), + ("include/sl_reflex.h", "3b623a1189e04a686384d224a58c4ad9974c4e6e3204077676f6ec529475164c"), + ("include/sl_result.h", "2a0f6c12863bdc00b38910a5ec85d1f083c5671ee817a920afe626ac2a9100f7"), + ("include/sl_security.h", "a36b1c20394402bee7ce738089a5a1305ff6e9dd9dd517566154ce834f7c32b4"), + ("include/sl_struct.h", "28deda67ea1a74371dd4f33ff4842b7db03b2db007001da6db06f6982297928d"), + ("include/sl_template.h", "af2d4a61361f2603872dcb6edc86cc9e18912762a5c03e6a782ff39a8c3b871b"), + ("include/sl_version.h", "2a43f565b45a07d4a4259dc61038ec93a7fb8ce7cb3509ea1fbeeb217f9a1ce8"), +] +# What the game loads at run time: without all of it the game leaves DLSS frame generation off (vk_dlssg.cpp early) +RUNTIME = [ + ("bin/x64/sl.interposer.dll", "b1d7395aebd7b9722841d64dcf7433680020b6a8e421f851c16d5ba0c2182edf"), + ("bin/x64/sl.common.dll", "38eea3b4209afc455636ea372b5933d65fcc13becec6c84d101dcfc4d8648f75"), + ("bin/x64/sl.dlss_g.dll", "3ed9538df724993bea8a050195a6a78b9c8c8a566d5b05ade477aab3c390e90f"), + ("bin/x64/sl.pcl.dll", "5698c38e722318681f215c9fe68e45b9bedd51378acc699d8eee26e961e57770"), + ("bin/x64/sl.reflex.dll", "443577e869352436ff7ec8f4d9c90418d3e95368436ce219261e0f4f07a38122"), + ("bin/x64/nvngx_dlssg.dll", "8d59d49b36410dd6b62fef2503a5871d347415bd85d943fa0848563a95dd50c7"), +] +LICENSES = [ + ("license.txt", "7b6f23e7d6f3ad6292f9308d2b42cdc3d82ae4e9b2abb55f230279d83bedd43d"), + ("bin/x64/nvngx_dlss.license.txt", "3027f23ca5a46dd9cb8183fbd522983a86f64d7daac5982912bf9f214671f294"), +] +# The license texts as they are named beside the DLLs in a game folder +GAME_LICENSE_NAMES = {"license.txt": "streamline-LICENSE.txt", "bin/x64/nvngx_dlss.license.txt": "nvidia-rtx-sdks-LICENSE.txt"} +GAME_DIR = "dlssg" + +NOTICE = ("NVIDIA DLSS frame generation uses NVIDIA's Streamline (its sl.*.dll files, under Streamline's MIT license) and " + "nvngx_dlssg.dll (under the NVIDIA RTX SDKs License, for systems with NVIDIA GPUs). They are NVIDIA's, not part of " + "Triforce: " + LICENSE_URL) + + +def files(): + return HEADERS + RUNTIME + LICENSES + + +def sha256_file(path): + h = hashlib.sha256() + with open(path, "rb") as f: + for block in iter(lambda: f.read(1 << 20), b""): + h.update(block) + return h.hexdigest() + + +def missing(sdk): + """The pinned files that are not in sdk, or are other files than the pinned ones""" + return [name for name, digest in files() + if not os.path.isfile(os.path.join(sdk, *name.split("/"))) or sha256_file(os.path.join(sdk, *name.split("/"))) != digest] + + +def complete(sdk): + """sdk holds every pinned file: what nx2/build.py and the game need""" + return bool(sdk) and os.path.isdir(sdk) and not missing(sdk) + + +class RangeReader(io.RawIOBase): + """The archive at url as a seekable file that downloads only what is read (HTTP range requests)""" + + def __init__(self, url, size, opener=None): + self.url, self.size, self.pos = url, size, 0 + self.opener = opener or urllib.request.urlopen + self.fetched = 0 + + def readable(self): + return True + + def seekable(self): + return True + + def tell(self): + return self.pos + + def seek(self, offset, whence=io.SEEK_SET): + self.pos = offset if whence == io.SEEK_SET else self.pos + offset if whence == io.SEEK_CUR else self.size + offset + return self.pos + + def readinto(self, buffer): + want = min(len(buffer), self.size - self.pos) + if want <= 0: + return 0 + request = urllib.request.Request(self.url, headers={"Range": "bytes=%d-%d" % (self.pos, self.pos + want - 1), + "User-Agent": "streamline-sdk-fetch"}) + with self.opener(request, timeout=120) as r: + if getattr(r, "status", 206) != 206: # (the whole archive would come: the caller reads it once instead) + raise ValueError("the server does not serve parts of the archive") + data = r.read() + if len(data) != want: + raise ValueError("a part of the archive came short (%d of %d bytes)" % (len(data), want)) + buffer[:want] = data + self.pos += want + self.fetched += want + return want + + +def open_archive(url=URL, size=SIZE, opener=None, say=print): + """(zip file, how it is read): by ranges, or downloaded whole into memory when the server serves none""" + reader = RangeReader(url, size, opener) + try: + return zipfile.ZipFile(io.BufferedReader(reader, buffer_size=1 << 20)), reader + except (ValueError, zipfile.BadZipFile, OSError) as e: + say("NVIDIA's archive is read whole (%s): %d MB" % (e, size >> 20)) + request = urllib.request.Request(url, headers={"User-Agent": "streamline-sdk-fetch"}) + with (opener or urllib.request.urlopen)(request, timeout=600) as r: + data = r.read() + if len(data) != size: + raise ValueError("NVIDIA's archive is %d bytes, not the %d of Streamline %s" % (len(data), size, VERSION)) + return zipfile.ZipFile(io.BytesIO(data)), None + + +def fetch(sdk, say=print, archive=None): + """Every pinned file into sdk that is not there yet. archive: an open zipfile to take them from (tests); else NVIDIA's + release. Raises OSError or ValueError; a file that is not the pinned one is never left in sdk""" + need = missing(sdk) if os.path.isdir(sdk) else [name for name, _ in files()] + if not need: + say("NVIDIA's Streamline %s is in %s already" % (VERSION, sdk)) + return + reader = None + if archive is None: + say("NVIDIA's Streamline %s: %d files from %s" % (VERSION, len(need), URL)) + archive, reader = open_archive(say=say) + digests = dict(files()) + names = {info.filename.replace("\\", "/"): info for info in archive.infolist()} + for name in need: + info = names.get(name) or next((i for n, i in names.items() if n.endswith("/" + name)), None) + if info is None: + raise ValueError("NVIDIA's archive has no %s" % name) + data = archive.read(info) + if hashlib.sha256(data).hexdigest() != digests[name]: + raise ValueError("%s is not the file of Streamline %s (another SHA-256)" % (name, VERSION)) + dst = os.path.join(sdk, *name.split("/")) + os.makedirs(os.path.dirname(dst), exist_ok=True) + tmp = dst + ".part" + with open(tmp, "wb") as f: + f.write(data) + os.replace(tmp, dst) + if reader is not None: + say("NVIDIA's Streamline %s: %d files, %.1f MB downloaded of the archive's %d MB" % + (VERSION, len(need), reader.fetched / 1048576.0, SIZE >> 20)) + + +def install_runtime(sdk, game): + """The DLLs and the license texts from sdk into /dlssg. Returns the folder""" + dst = os.path.join(game, GAME_DIR) + os.makedirs(dst, exist_ok=True) + for name, _ in RUNTIME: + shutil.copyfile(os.path.join(sdk, *name.split("/")), os.path.join(dst, os.path.basename(name))) + for name, _ in LICENSES: + shutil.copyfile(os.path.join(sdk, *name.split("/")), os.path.join(dst, GAME_LICENSE_NAMES[name])) + return dst + + +def remove_runtime(game): + """A game folder built without frame generation after one built with it loses NVIDIA's files""" + dst = os.path.join(game, GAME_DIR) + if not os.path.isdir(dst): + return False + known = {os.path.basename(name) for name, _ in RUNTIME} | set(GAME_LICENSE_NAMES.values()) + for name in os.listdir(dst): + if name in known: + os.remove(os.path.join(dst, name)) + try: + os.rmdir(dst) + except OSError: + pass + return True + + +def main(): + p = argparse.ArgumentParser(description=__doc__.split("\n")[0]) + p.add_argument("sdk", nargs="?", default=os.path.join(ROOT, "work", "streamline-sdk")) + p.add_argument("--game") + p.add_argument("--accept-nvidia-license", action="store_true") + a = p.parse_args() + print(NOTICE) + if not a.accept_nvidia_license: + if input("Accept NVIDIA's licenses and download the files? [y/N] ").strip().lower() not in ("y", "yes"): + print("Nothing was downloaded.") + return 1 + try: + fetch(a.sdk) + except (OSError, ValueError) as e: + print("NVIDIA's Streamline could not be downloaded: %s" % e) + return 1 + print("Build with HOOKSHOT_STREAMLINE_SDK=%s" % a.sdk) + if a.game: + print("NVIDIA's files for the game: %s" % install_runtime(a.sdk, a.game)) + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/setup/Saves.cs b/setup/Saves.cs index 24d6fe2..5a4c266 100644 --- a/setup/Saves.cs +++ b/setup/Saves.cs @@ -30,6 +30,38 @@ static class Saves public static string TriforceFolder(string game) => Path.Combine(game, "user", "save", TriforceUid); + /// Triforce commits its save as a transaction (host/save_tx.h): the game's save mount is a working copy in + /// \save-tx\\work, and a finished commit that could not be put in place yet (ready-, the highest n) waits beside + /// it and is newer than the save folder. The folder of that transaction for a save folder \save\, or null when + /// the folder is not such a save folder + static string? TriforceTx(string save) + { + var parent = Path.GetDirectoryName(save.TrimEnd('\\')); + if (parent == null || !string.Equals(Path.GetFileName(parent), "save", StringComparison.OrdinalIgnoreCase)) return null; + var user = Path.GetDirectoryName(parent); + return user == null ? null : Path.Combine(user, "save-tx", Path.GetFileName(save.TrimEnd('\\'))); + } + + /// The save Triforce would load at its next start: a finished commit still waiting, else the save folder + public static string TriforceNewest(string game) + { + var folder = TriforceFolder(game); + try + { + var tx = TriforceTx(folder); + if (tx != null && Directory.Exists(tx)) + { + var ready = Directory.GetDirectories(tx, "ready-*") + .Select(d => (Dir: d, M: Regex.Match(Path.GetFileName(d), @"^ready-(\d{1,9})$"))) + .Where(r => r.M.Success && IsSaveSet(r.Dir)) + .OrderByDescending(r => int.Parse(r.M.Groups[1].Value)).FirstOrDefault(); + if (ready.Dir != null) return ready.Dir; + } + } + catch (Exception) { } + return folder; + } + /// A folder holding this game's saves public static bool IsSaveSet(string dir) => Directory.Exists(dir) && (File.Exists(Path.Combine(dir, "option.sav")) || File.Exists(Path.Combine(dir, "slot_00", "progress.sav")) @@ -266,6 +298,12 @@ static class Saves } Directory.CreateDirectory(dst); CopyTree(src, dst); + // (into Triforce: its working copy and any finished commit still waiting belong to the save that was replaced, and the game + // would put such a commit over the copy at its next start) + if (TriforceTx(dst) is { } tx && Directory.Exists(tx)) + { + try { Directory.Delete(tx, true); } catch (Exception) { } + } return backup; } diff --git a/setup/Shell.Home.cs b/setup/Shell.Home.cs index 0f117e1..a00a82f 100644 --- a/setup/Shell.Home.cs +++ b/setup/Shell.Home.cs @@ -83,7 +83,7 @@ sealed partial class Shell var none = offline.Message.Contains("no Triforce release"); NewsMark.Fill = Brush("TextFaint"); NewsLine.Text = none ? "No release is published yet" : "Couldn't check for updates"; - NewsDetail.Text = (none ? "" : "GitHub could not be reached. ") + (hasGame ? "You can play what you have." : "You can build with the tools you have."); + NewsDetail.Text = (none ? "" : "The Triforce site could not be reached. ") + (hasGame ? "You can play what you have." : "You can build with the tools you have."); NewsDetail.Foreground = Brush("TextDim"); } else if (update) diff --git a/setup/Shell.Saves.cs b/setup/Shell.Saves.cs index 951aab5..372f068 100644 --- a/setup/Shell.Saves.cs +++ b/setup/Shell.Saves.cs @@ -202,7 +202,7 @@ sealed partial class Shell SavesInfo.Text = "Copying..."; try { - var from = into ? pick.Folder : tri; + var from = into ? pick.Folder : Saves.TriforceNewest(game); var to = into ? tri : pick.Folder; var label = into ? "Triforce before " + pick.Name : pick.Name + " before Triforce"; var backup = await Task.Run(() => Saves.Copy(from, to, backups, label)); diff --git a/setup/Shell.Test.cs b/setup/Shell.Test.cs index 1b06cd0..84a740c 100644 --- a/setup/Shell.Test.cs +++ b/setup/Shell.Test.cs @@ -67,7 +67,7 @@ sealed partial class Shell Show(FetchPage); stage.All(false); FetchUnknown(true); - FetchState("Gathering the stones", "Asking GitHub for the newest Triforce.", "Checking for updates"); + FetchState("Gathering the stones", "Asking for the newest Triforce.", "Checking for updates"); FetchCancel.Content = "Cancel"; await Shot(dir, "02-gather-checking"); @@ -78,7 +78,7 @@ sealed partial class Shell stage.All(false); FetchUnknown(false); - FetchTitle.Text = "GitHub could not be reached"; + FetchTitle.Text = "The Triforce site could not be reached"; FetchNote.Text = "Check your internet connection, then try again."; FetchNote.Foreground = Brush("Bad"); FetchStep.Text = ""; diff --git a/setup/Shell.cs b/setup/Shell.cs index 712d74e..a61d4c9 100644 --- a/setup/Shell.cs +++ b/setup/Shell.cs @@ -241,8 +241,11 @@ sealed partial class Shell { if (DlssTick.IsChecked != true) { Install.Set("Dlss", "0"); return; } DlssTick.IsChecked = false; - Ask("NVIDIA DLSS is NVIDIA's, under NVIDIA's own license (github.com/NVIDIA/DLSS, LICENSE.txt). Its files are downloaded to " + - "this PC; a game folder built with them must not be shared. Do you accept NVIDIA's license?", "I accept", T(S.Back), + // (11-15: the tick brings DLSS frame generation too: NVIDIA's Streamline (MIT) and nvngx_dlssg.dll, under the NVIDIA RTX + // SDKs License as DLSS itself: nx2/make_game.py fetch_streamline_sdk, scripts/streamline_sdk.py) + Ask("NVIDIA DLSS and DLSS frame generation are NVIDIA's, under NVIDIA's own licenses (github.com/NVIDIA/DLSS, LICENSE.txt; " + + "github.com/NVIDIA-RTX/Streamline, license.txt). Their files are downloaded to this PC; a game folder built with them " + + "must not be shared. Do you accept NVIDIA's licenses?", "I accept", T(S.Back), () => { DlssTick.IsChecked = true; Install.Set("Dlss", "1"); }); }; SetupBack.Click += (_, _) => ShowHome(homeOffline); @@ -447,7 +450,7 @@ sealed partial class Shell FetchUnknown(false); FetchCancel.IsEnabled = true; var offline = e is HttpRequestException || e is TaskCanceledException && !cancelled; - FetchTitle.Text = cancelled ? "Stopped" : offline ? "GitHub could not be reached" : "Something went wrong"; + FetchTitle.Text = cancelled ? "Stopped" : offline ? "The Triforce site could not be reached" : "Something went wrong"; FetchNote.Text = cancelled ? "Nothing was changed." : offline ? "Check your internet connection, then try again." : e.Message; FetchNote.Foreground = Brush(cancelled ? "Text" : "Bad"); FetchStep.Text = ""; @@ -793,6 +796,10 @@ sealed partial class Shell // (NVIDIA DLSS chosen: its SDK fetched first, only what is missing, and built in; a build and an update alike) var dlss = DlssChosen; if (dlss && !args.Contains("--fetch-ngx-sdk")) args.AddRange(new[] { "--fetch-ngx-sdk", "--accept-nvidia-license" }); + // (11-15: AMD FSR 4 in every build: its code is built in and AMD's two signed libraries (MIT license) are fetched + // from AMD's repository and put beside the game: nx2/make_game.py --fsr, scripts/fsr_sdk.py. The Upscaler row offers + // it only on a card AMD's library says runs it) + if (!args.Contains("--fsr") && !args.Contains("--no-fsr")) args.Add("--fsr"); Program.Log("build: make_game.py " + string.Join(" ", args) + " (tools " + tools.Root + ")"); runner = new Runner(tools, args, dlss ? NgxSdk : null); ShowClock(); diff --git a/tests/fpx_hot_test.c b/tests/fpx_hot_test.c new file mode 100644 index 0000000..97b7569 --- /dev/null +++ b/tests/fpx_hot_test.c @@ -0,0 +1,235 @@ +/* tests/test_fpx_hot.py's driver: the hot functions' float helpers (a64_fpx_hot_*: the fast path inline, the exact path + called without the context) and the four-lane helpers lifted code calls (a64_vfpx_add32 / sub32 / mul32, a64_vfpx_fma32) + against the exact bodies (recomp_fpe_*), lane by lane in order: the value, FPSR and the rest of the context. + Built against the real a64.h with the a64_suyu.h / a64_suyu.c / a64_fpx.h the generators write, at the build's FP flags, + at -march=x86-64-v3 (the binary32 FMA by the host's instruction) and -march=x86-64-v2 (its binary64 form; the four-lane + multiply-add lane by lane). + + fpx_hot_test n random operand sets for every helper over random FPCR / FPSR states + Exit 0: no mismatch. 1: a mismatch (the first few are printed). 3: the CPU lacks this build's -march. */ +#include "a64.h" +#include +#include +#include + +static uint64_t rs; +static uint64_t rnd(void) { rs ^= rs << 13; rs ^= rs >> 7; rs ^= rs << 17; return rs; } +static uint32_t r32(void) { return (uint32_t)(rnd() >> 16); } +static uint32_t rbelow(uint32_t n) { return (uint32_t)(((rnd() >> 32) * n) >> 32); } +static uint32_t mkf(uint32_t sign, int e, uint32_t frac) { return (sign << 31) | ((uint32_t)e << 23) | (frac & 0x7fffff); } +static uint64_t mkd(uint64_t sign, int e, uint64_t frac) { return (sign << 63) | ((uint64_t)e << 52) | (frac & UINT64_C(0xfffffffffffff)); } +static float F(uint32_t x) { float f; memcpy(&f, &x, 4); return f; } +static uint32_t B(float f) { uint32_t x; memcpy(&x, &f, 4); return x; } +static double D(uint64_t x) { double f; memcpy(&f, &x, 8); return f; } +static uint64_t BD(double f) { uint64_t x; memcpy(&x, &f, 8); return x; } + +/* ---- operand classes and states (as tests/fpx_sticky_test.c's) ---- */ +static uint32_t sparse_frac(void) { + switch (rbelow(4)) { + case 0: return 0; + case 1: return (uint32_t)1 << rbelow(23); + case 2: return r32() & 0x7ff000; + default: return r32() & 0x7fffff; + } +} +static uint32_t any32(void) { + const uint32_t s = r32() & 1; + switch (rbelow(16)) { + case 0: return s << 31; + case 1: return mkf(s, 0, r32() | 1); + case 2: return mkf(s, 0, rbelow(8) + 1); + case 3: return mkf(s, 0, 0x7fffff - rbelow(8)); + case 4: return mkf(s, 1 + rbelow(3), sparse_frac()); + case 5: return mkf(s, 254 - rbelow(3), sparse_frac()); + case 6: return mkf(s, 255, 0); + case 7: return mkf(s, 255, (r32() | 1) & 0x7fffff); + case 8: return mkf(s, 255, 0x400000 | (r32() & 0x3fffff)); + case 9: case 10: return mkf(s, 120 + rbelow(15), sparse_frac()); + default: return mkf(s, 1 + rbelow(254), r32()); + } +} +static uint64_t any64(void) { + const uint64_t s = r32() & 1; + const uint64_t fr = rnd() & UINT64_C(0xfffffffffffff), sp = rbelow(3) ? fr : (rbelow(2) ? 0 : UINT64_C(1) << rbelow(52)); + switch (rbelow(16)) { + case 0: return s << 63; + case 1: return mkd(s, 0, fr | 1); + case 2: return mkd(s, 0, rbelow(8) + 1); + case 3: return mkd(s, 0, UINT64_C(0xfffffffffffff) - rbelow(8)); + case 4: return mkd(s, 1 + (int)rbelow(3), sp); + case 5: return mkd(s, 2046 - (int)rbelow(3), sp); + case 6: return mkd(s, 2047, 0); + case 7: return mkd(s, 2047, fr | 1); + case 8: return mkd(s, 2047, UINT64_C(0x8000000000000) | fr); + case 9: case 10: return mkd(s, 1016 + (int)rbelow(15), sp); + default: return mkd(s, 1 + (int)rbelow(2046), fr); + } +} +static void edge_pair32(uint32_t *a, uint32_t *b) { /* a product near the bottom of the normal range or the overflow edge */ + const int target = (rbelow(8) == 0) ? 255 - (int)rbelow(3) : (int)rbelow(60) - 30; + int ea = 1 + (int)rbelow(253); + int eb = target - ea + 127; + if (eb < 1 || eb > 254) { eb = 1 + (int)rbelow(254); ea = target - eb + 127; } + if (ea < 1) ea = rbelow(4) ? 1 : 0; + if (ea > 254) ea = 254; + *a = mkf(r32() & 1, ea, rbelow(2) ? sparse_frac() : r32()); + *b = mkf(r32() & 1, eb, rbelow(2) ? sparse_frac() : r32()); +} +static uint32_t addend32(uint32_t a, uint32_t b) { + const double p = (double)F(a) * (double)F(b); + switch (rbelow(10)) { + case 0: return any32(); + case 1: return B((float)-p); + case 2: return B((float)-p) + (uint32_t)((int)rbelow(9) - 4); + case 3: return r32() & 0x80000000u; + case 4: return B((float)p) ^ (rbelow(2) ? 0x80000000u : 0); + case 5: return mkf(r32() & 1, 0, r32()); + case 6: return mkf(r32() & 1, 1 + rbelow(4), r32()); + default: return mkf(r32() & 1, 1 + rbelow(254), r32()); + } +} +static uint64_t addend64(uint64_t a, uint64_t b) { + const double p = D(a) * D(b); + switch (rbelow(8)) { + case 0: return any64(); + case 1: return BD(-p); + case 2: return BD(-p) + (uint64_t)((int)rbelow(9) - 4); + case 3: return (uint64_t)(r32() & 1) << 63; + default: return mkd(r32() & 1, 1 + (int)rbelow(2046), rnd()); + } +} +static void gen_state(Ctx *c) { + uint64_t cr = 0, sr = 0; + if (rbelow(2)) cr |= 1u << 24; /* FZ */ + if (rbelow(10) == 0) cr |= 1u << 25; /* DN (gate closed) */ + if (rbelow(10) == 0) cr |= (uint64_t)rbelow(4) << 22; /* RMode */ + if (rbelow(40) == 0) cr |= r32() & 0x07ff9f07u; /* anything else */ + if (rbelow(50) == 0) cr |= UINT64_C(1) << 32; /* the host's kill switch */ + switch (rbelow(6)) { + case 0: sr = 0x9f; break; /* every cumulative flag */ + case 1: sr = 0x10; break; /* IXC only */ + case 2: sr = 0; break; + default: sr = (r32() & 0x9f) | (rbelow(4) ? 0x10 : 0); break; + } + memset(c, 0, sizeof *c); + c->fpcr = cr; + c->fpsr = sr; + c->x[5] = 0x1234; /* (a helper must leave the rest alone) */ + c->vreg[7][1] = 0x5678; +} + +static unsigned long long checks, bad, per_bad[16], per_n[16]; +static const char *const NAMES[16] = {"add32", "sub32", "div32", "sqrt32", "mul32", "fma32", "recps32", "rsqrts32", "add64", "sub64", + "div64", "sqrt64", "mul64", "fma64", "vector add/sub/mul", "vector mul/fma by element"}; +static void report(int k, const Ctx *c0, const Ctx *got, const Ctx *want, const uint64_t *g, const uint64_t *w, int n, const uint64_t *in, int nin) { + int diff = memcmp(got, want, sizeof *got) != 0; + for (int i = 0; i < n; i++) diff |= g[i] != w[i]; + checks++, per_n[k]++; + if (!diff) return; + bad++, per_bad[k]++; + if (bad <= 12) { + printf("MISMATCH %s: fpcr %#llx fpsr %#llx; in", NAMES[k], (unsigned long long)c0->fpcr, (unsigned long long)c0->fpsr); + for (int i = 0; i < nin; i++) printf(" %#llx", (unsigned long long)in[i]); + printf("; got"); + for (int i = 0; i < n; i++) printf(" %#llx", (unsigned long long)g[i]); + printf(" fpsr %#llx; want", (unsigned long long)got->fpsr); + for (int i = 0; i < n; i++) printf(" %#llx", (unsigned long long)w[i]); + printf(" fpsr %#llx\n", (unsigned long long)want->fpsr); + } +} +#define SCALAR(k, hot_call, exact_call, ...) do { \ + Ctx c0, c1, c2; gen_state(&c0); c1 = c0; c2 = c0; \ + const uint64_t in_[] = {__VA_ARGS__}; \ + uint64_t g_ = hot_call, w_ = exact_call; \ + report(k, &c0, &c1, &c2, &g_, &w_, 1, in_, (int)(sizeof in_ / sizeof in_[0])); } while (0) + +static void vec(int k, const Ctx *c0, Ctx *got, Ctx *want, const uint32_t g[4], const uint32_t w[4], const uint32_t *in, int nin) { + uint64_t gg[4], ww[4], ii[12]; + for (int i = 0; i < 4; i++) gg[i] = g[i], ww[i] = w[i]; + for (int i = 0; i < nin; i++) ii[i] = in[i]; + report(k, c0, got, want, gg, ww, 4, ii, nin); +} + +int main(int argc, char **argv) { + unsigned a_, b_, c_, d_; +#if defined(__FMA__) + if (!__get_cpuid(1, &a_, &b_, &c_, &d_) || !(c_ & (1u << 12)) || !(c_ & (1u << 28))) return 3; + if (!__get_cpuid_count(7, 0, &a_, &b_, &c_, &d_) || !(b_ & (1u << 5))) return 3; +#else + if (!__get_cpuid(1, &a_, &b_, &c_, &d_) || !(c_ & (1u << 20)) || !(c_ & (1u << 23))) return 3; +#endif + const unsigned long long n = argc > 1 ? strtoull(argv[1], NULL, 10) : 1000000; + rs = argc > 2 ? strtoull(argv[2], NULL, 10) : 20261114; + if (!rs) rs = 1; + for (unsigned long long it = 0; it < n; it++) { + uint32_t a = any32(), b = any32(); + if (rbelow(3) == 0) edge_pair32(&a, &b); + const uint32_t z = addend32(a, b), zs = (a ^ b) & 0x80000000u; + SCALAR(0, a64_fpx_hot_add32_add32(&c1, a, b), recomp_fpe_add32(&c2, a, b), a, b); + SCALAR(1, a64_fpx_hot_sub32_sub32(&c1, a, b), recomp_fpe_sub32(&c2, a, b), a, b); + SCALAR(2, a64_fpx_hot_div32_div32(&c1, a, b), recomp_fpe_div32(&c2, a, b), a, b); + SCALAR(3, a64_fpx_hot_sqrt32_sqrt32(&c1, a), recomp_fpe_sqrt32(&c2, a), a); + SCALAR(4, a64_fpx_hot_mul32_fma32(&c1, a, b, zs), recomp_fpe_fma32(&c2, a, b, zs), a, b, zs); + SCALAR(5, a64_fpx_hot_fma32_fma32(&c1, a, b, z), recomp_fpe_fma32(&c2, a, b, z), a, b, z); + SCALAR(6, a64_fpx_hot_recps32_recps32(&c1, a, b), recomp_fpe_recps32(&c2, a, b), a, b); + SCALAR(7, a64_fpx_hot_rsqrts32_rsqrts32(&c1, a, b), recomp_fpe_rsqrts32(&c2, a, b), a, b); + { + const uint64_t p = any64(), q = any64(), r = addend64(p, q), rz = (p ^ q) & (UINT64_C(1) << 63); + SCALAR(8, a64_fpx_hot_add64_add64(&c1, p, q), recomp_fpe_add64(&c2, p, q), p, q); + SCALAR(9, a64_fpx_hot_sub64_sub64(&c1, p, q), recomp_fpe_sub64(&c2, p, q), p, q); + SCALAR(10, a64_fpx_hot_div64_div64(&c1, p, q), recomp_fpe_div64(&c2, p, q), p, q); + SCALAR(11, a64_fpx_hot_sqrt64_sqrt64(&c1, p), recomp_fpe_sqrt64(&c2, p), p); + SCALAR(12, a64_fpx_hot_mul64_fma64(&c1, p, q, rz), recomp_fpe_fma64(&c2, p, q, rz), p, q, rz); + SCALAR(13, a64_fpx_hot_fma64_fma64(&c1, p, q, r), recomp_fpe_fma64(&c2, p, q, r), p, q, r); + } + /* ---- four lanes: mostly ordinary numbers (so the one-op path is taken), now and then any operand in a lane ---- */ + uint32_t vn[4], vm[4], vz[4]; + for (int i = 0; i < 4; i++) { + vn[i] = rbelow(6) ? mkf(r32() & 1, 100 + rbelow(60), r32()) : any32(); + vm[i] = rbelow(6) ? mkf(r32() & 1, 100 + rbelow(60), r32()) : any32(); + vz[i] = rbelow(6) ? mkf(r32() & 1, 100 + rbelow(60), r32()) : addend32(vn[i], vm[i]); + } + const uint32_t el = rbelow(8) ? mkf(r32() & 1, 100 + rbelow(60), r32()) : any32(); + for (int op = 0; op < 3; op++) { /* straight add, sub (4 and 2 lanes), mul */ + for (int k = 4; k >= (op == 2 ? 4 : 2); k -= 2) { + Ctx c0, c1, c2; gen_state(&c0); c1 = c0; c2 = c0; + uint64_t dn[2], dm[2], dd[2] = {0x1111111111111111ULL, 0x2222222222222222ULL}; + uint32_t g[4], w[4] = {0}; + memcpy(dn, vn, 16); memcpy(dm, vm, 16); + const int alias = rbelow(4) == 0; /* (the destination is the first source's register) */ + uint64_t *dst = alias ? dn : dd; + if (op == 0) a64_vfpx_add32(&c1, dst, dn, dm, k); + else if (op == 1) a64_vfpx_sub32(&c1, dst, dn, dm, k); + else a64_vfpx_mul32(&c1, dst, dn, dm, 4); + memcpy(g, dst, 16); + for (int j = 0; j < k; j++) + w[j] = (uint32_t)(op == 0 ? recomp_fpe_add32(&c2, vn[j], vm[j]) : op == 1 ? recomp_fpe_sub32(&c2, vn[j], vm[j]) + : recomp_fpe_fma32(&c2, vn[j], vm[j], (vn[j] ^ vm[j]) & 0x80000000u)); + uint32_t in[8]; memcpy(in, vn, 16); memcpy(in + 4, vm, 16); + vec(14, &c0, &c1, &c2, g, w, in, 8); + } + } + for (int op = 0; op < 3; op++) { /* by element, as vectorize_elem writes it: mul, fmla, fmls */ + Ctx c0, c1, c2; gen_state(&c0); c1 = c0; c2 = c0; + uint64_t dn[2], dd[2], e[2]; + uint32_t g[4], w[4]; + memcpy(dn, vn, 16); memcpy(dd, vz, 16); + e[0] = e[1] = (uint64_t)el << 32 | el; + const int alias = op == 0 && rbelow(4) == 0; + uint64_t *dst = alias ? dn : dd; + if (op == 0) a64_vfpx_mul32(&c1, dst, dn, e, 4); + else a64_vfpx_fma32(&c1, dst, dn, e, op == 2); + memcpy(g, dst, 16); + for (int j = 0; j < 4; j++) { + const uint32_t x = op == 2 ? vn[j] ^ 0x80000000u : vn[j]; + w[j] = (uint32_t)recomp_fpe_fma32(&c2, x, el, op == 0 ? (vn[j] ^ el) & 0x80000000u : vz[j]); + } + uint32_t in[9]; memcpy(in, vn, 16); memcpy(in + 4, vz, 16); in[8] = el; + vec(15, &c0, &c1, &c2, g, w, in, 9); + } + } + for (int k = 0; k < 16; k++) + if (per_bad[k]) printf(" %s: %llu of %llu mismatch\n", NAMES[k], per_bad[k], per_n[k]); + printf("%llu checks, %llu mismatches\n", checks, bad); + return bad ? 1 : 0; +} diff --git a/tests/invalidate_vcore_test.cpp b/tests/invalidate_vcore_test.cpp index 0fac6b6..18deee3 100644 --- a/tests/invalidate_vcore_test.cpp +++ b/tests/invalidate_vcore_test.cpp @@ -25,6 +25,10 @@ static uint64_t want_gpu = 0x123456789abULL, want_size = 0x100002345ULL; static const MkwVkRenderer* vk; static int hos_vcores_on() { return enabled; } +static int64_t hos_now_ns() { return 0; } // (the wrapper times its call for the lab's HOS_NVN_LOG_FLUSH log) +static void hos_log(const char*, ...) {} +int hos_ft_on() { return 0; } // (the frame trace, HOS_FT: off here) +double hos_ft_ms() { return 0.0; } static void vc_release(HosThread* t) { std::lock_guard lock(lease_mu); CHECK(owner == t && t->vc_held == 1); diff --git a/tests/program_cache_cleanup_test.cpp b/tests/program_cache_cleanup_test.cpp index 21e4b7f..8477144 100644 --- a/tests/program_cache_cleanup_test.cpp +++ b/tests/program_cache_cleanup_test.cpp @@ -91,6 +91,8 @@ std::atomic st_cache_hits{0}; // is the cache's own, so the counts below are of what the loader made) const std::vector* renodx_replacement(uint32_t) { return nullptr; } #define PROF_EXACT() // (vk_prof.h's exact-timing scope, since #989: nothing to time here) +// (local, texture detail: vk_shader.cpp's note of which bindings a module reads by exact texel. Nothing to note here) +void exact_note(VkShaderModule, const std::vector&) {} #include "cache_functions.inc" void save(uint64_t key, bool empty_second = false, int bad_answer = 0) { diff --git a/tests/test_camera_speed.py b/tests/test_camera_speed.py new file mode 100644 index 0000000..63e4946 --- /dev/null +++ b/tests/test_camera_speed.py @@ -0,0 +1,128 @@ +"""The Camera speed row (host/prompts.c build_camera, nx2/prompt_assets.py CAMERA). + +The game's five camera sensitivities are five floats in a BYML file inside its camera pack. Setup's Update writes the +pack decompressed and a table of where the floats are; at the start the host serves the pack with each one times the +row's factor. Checked here, without the game: + +- The table: each float of the StickSensitivity array, at its place in the pack, with the game's value; other arrays + and other files of the pack are left out. +- A pack that is not the one known gives no table: no such file, no such array, an array of something else than + floats, a value that is no speed. +- The host's and the menu's ends are wired: the pack's path, the row, its key in the options file, the mouse. + +python -I -B tests/test_camera_speed.py +""" +import struct +import sys +import tempfile +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "nx2")) +import prompt_assets # noqa: E402 + +RATES = (1.2, 1.6, 2.0, 2.4, 3.0) + + +def byml(entries): + """a little-endian BYML whose root dictionary holds entries: [(key, value)], a value a float, or a list of floats (an + array), or a list of ints (an array of ints)""" + names = sorted(k for k, _ in entries) + keys = bytearray(struct.pack(" 300 ? 300 : v;", prompts) + menu = (ROOT / "gpu/vk/vk_menu.cpp").read_text(encoding="utf-8") + self.assertIn('{TAB_CONTROLS, "Camera speed", &opts.camera_speed, 25, 300, 5, tx_sens,', menu) + hid = (ROOT / "host/nn_hid.c").read_text(encoding="utf-8") + self.assertIn("ox /= cam, oy /= cam;", hid) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_fpx_hot.py b/tests/test_fpx_hot.py new file mode 100644 index 0000000..0e26178 --- /dev/null +++ b/tests/test_fpx_hot.py @@ -0,0 +1,78 @@ +"""The hot functions' float helpers and the four-lane helpers, game-free: bit-exact with the exact bodies. + +a64_lift.py gives hot lifted functions their own copy of every FPX1 helper (a64_fpx_hot_*): the fast path inline, and past it +the exact path called without the context (a64_fpx_nc_*: the control word in, the status word in and out), the result +carried as a float or double. NEON's four-lane FADD / FSUB / FMUL, and FMUL / FMLA / FMLS by element (vectorize_elem), go +through a64_vfpx_add32 / sub32 / mul32 and a64_vfpx_fma32: one SSE or FMA op when every lane passes the scalar fast path's +tests, otherwise lane by lane through the scalar helpers, again without the context. And where the build has FMA +(x86-64-v3) the binary32 fused multiply-add's fast path is the host's instruction (a64_rtgen.fma32_native) instead of +suyu's binary64 form. + +This builds what the real build builds (a64_fpx.h from a64_lift.fpx_header(), a64_suyu.h / a64_suyu.c from a64_rtgen.py, +through tests/test_fpx_sticky.py's generator) and compiles tests/fpx_hot_test.c against the real rt/a64.h at the build's +FP flags, at -march=x86-64-v3 and at -march=x86-64-v2 (the binary64 form; the four-lane multiply-add lane by lane). For +random operands (zeros, subnormals, infinities, NaNs, the extreme normals, products at the underflow and overflow edges, +cancellations) under random FPCR and FPSR states, each helper's value, FPSR and the rest of the context must equal the +exact body's: recomp_fpe_* for the scalars, and for four lanes the exact body lane by lane in order, FPSR carried from +lane to lane (what the lane loops did before). + + python -I -B tests/test_fpx_hot.py [--cc clang] [--cxx clang++] [--n 2000000] + +Skipped (exit 0) where tests/test_fpx_sticky.py is. +""" +import argparse +import os +import platform +import sys +import tempfile +import time + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +import test_fpx_sticky as S # noqa: E402 (its compilers, flags and generator) + +SHAPES = {(a + s, a + s, n, n) for s in ("32", "64") for a, n in (("add", 2), ("sub", 2), ("div", 2), ("sqrt", 1), ("fma", 3))} | \ + {("mul32", "fma32", 2, 3), ("mul64", "fma64", 2, 3), ("recps32", "recps32", 2, 2), ("rsqrts32", "rsqrts32", 2, 2)} + + +def main(): + p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + p.add_argument("--cc") + p.add_argument("--cxx") + p.add_argument("--n", type=int, default=2000000) + a = p.parse_args() + if platform.machine().lower() not in ("x86_64", "amd64"): + S.skip("not an x86-64 host: %s" % platform.machine()) + cc = S.compiler(a.cc, "CC", ("clang", "gcc", "cc"), "clang") + cxx = S.compiler(a.cxx, "CXX", ("clang++", "g++", "c++"), "clang++") + if not cc or not cxx: + S.skip("no C compiler" if not cc else "no C++ compiler") + t0 = time.time() + failed, ran = [], [] + with tempfile.TemporaryDirectory(prefix="hookshot-fpx-hot-") as tmp: + S.generate(cxx, tmp, S.target_flags(cxx, tmp)) + import a64_lift + with open(os.path.join(tmp, "a64_fpx.h"), "w", newline="\n") as f: # (every shape, not only the sticky test's two) + f.write(a64_lift.fpx_header(SHAPES)) + for march, _ in S.BUILDS: + exe = os.path.join(tmp, "fpx_hot_test_%s.exe" % march) + S.build([cc] + S.target_flags(cc, tmp) + ["-march=" + march] + S.FLAGS + + ["-D_CRT_SECURE_NO_WARNINGS", "-Wno-everything", "-I" + os.path.join(S.KIT, "rt"), "-I" + tmp, + os.path.join(S.ROOT, "tests", "fpx_hot_test.c"), os.path.join(tmp, "a64_suyu.c"), "-o", exe], + "the driver at " + march) + r = S.run([exe, str(a.n), "20261114"]) + print("%s: %s" % (march, r.stdout.rstrip().replace("\n", "\n "))) + if r.returncode == 3: + print("fpx hot: %s skipped (this CPU is not %s)" % (march, march)) + continue + ran.append(march) + if r.returncode: + failed.append(march) + if not ran: + S.skip("this CPU is not x86-64-v2") + if failed: + raise SystemExit("fpx hot: FAIL (%s) in %.1f s" % (", ".join(failed), time.time() - t0)) + print("fpx hot: pass in %.1f s" % (time.time() - t0)) + + +if __name__ == "__main__": + main() diff --git a/tests/test_fsr_sdk.py b/tests/test_fsr_sdk.py new file mode 100644 index 0000000..fe1527f --- /dev/null +++ b/tests/test_fsr_sdk.py @@ -0,0 +1,97 @@ +"""AMD's FSR 4 libraries for the players' build (scripts/fsr_sdk.py, nx2/make_game.py --fsr). + +Without the network: a stand-in for AMD's repository hands out this test's own files, and the script's table is pointed +at them. +- both files are fetched into the folder; a second fetch downloads nothing; a damaged one alone is fetched again; +- a file of another size or SHA-256 stops the fetch and is not left in the folder; +- the game folder gets the two libraries and AMD's license text beside the exe, and loses exactly those again; +- make_game.py fetches with --fsr, places them at the end, and Setup asks for --fsr. + +python -I -B tests/test_fsr_sdk.py +""" +import hashlib +import importlib.util +import tempfile +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +spec = importlib.util.spec_from_file_location("fsr_sdk", ROOT / "scripts" / "fsr_sdk.py") +fsr = importlib.util.module_from_spec(spec) +spec.loader.exec_module(fsr) + + +def content(name): + return (("AMD's %s\n" % name) * 300).encode() + + +class FsrSdkTest(unittest.TestCase): + def setUp(self): + self.saved = fsr.RUNTIME + fsr.RUNTIME = [(name, len(content(name)), hashlib.sha256(content(name)).hexdigest()) for name, _, _ in self.saved] + self.tmp = tempfile.TemporaryDirectory(prefix="fsr-sdk-") + self.dir = Path(self.tmp.name) + self.asked = [] + self.said = [] + + def tearDown(self): + fsr.RUNTIME = self.saved + self.tmp.cleanup() + + def get(self, url): + self.asked.append(url) + return content(url.rsplit("/", 1)[1]) + + def test_fetch(self): + folder = self.dir / "sdk" + fsr.fetch(str(folder), say=self.said.append, get=self.get) + self.assertTrue(fsr.complete(str(folder))) + self.assertEqual(len(self.asked), 2) + self.assertTrue(all(url.startswith(fsr.BASE) for url in self.asked)) + fsr.fetch(str(folder), say=self.said.append, get=self.get) + self.assertEqual(len(self.asked), 2) # (nothing asked for again) + self.assertIn("already", self.said[-1]) + (folder / "amd_fidelityfx_loader_vk.dll").write_bytes(b"damaged") + self.assertEqual(fsr.missing(str(folder)), ["amd_fidelityfx_loader_vk.dll"]) + fsr.fetch(str(folder), say=self.said.append, get=self.get) + self.assertEqual(len(self.asked), 3) + self.assertTrue(fsr.complete(str(folder))) + + def test_another_file_refused(self): + folder = self.dir / "sdk" + with self.assertRaises(ValueError): + fsr.fetch(str(folder), say=self.said.append, get=lambda url: content(url) + b"x") + self.assertEqual(sorted(p.name for p in folder.iterdir()), []) + self.assertFalse(fsr.complete(str(folder))) + + def test_game_folder(self): + folder, game = self.dir / "sdk", self.dir / "game" + fsr.fetch(str(folder), say=self.said.append, get=self.get) + game.mkdir() + (game / "Triforce.exe").write_bytes(b"the game") + placed = fsr.install_runtime(str(folder), str(game)) + self.assertEqual(sorted(Path(p).name for p in placed), ["amd_fidelityfx_loader_vk.dll", "amd_fidelityfx_upscaler_vk.dll"]) + self.assertTrue((game / fsr.LICENSE_NAME).is_file()) + self.assertIn("Advanced Micro Devices", (game / fsr.LICENSE_NAME).read_text(encoding="utf-8", errors="replace")) + self.assertTrue(fsr.remove_runtime(str(game))) + self.assertEqual([p.name for p in game.iterdir()], ["Triforce.exe"]) + self.assertFalse(fsr.remove_runtime(str(game))) + + def test_real_table(self): + self.assertEqual([name for name, _, _ in self.saved], ["amd_fidelityfx_loader_vk.dll", "amd_fidelityfx_upscaler_vk.dll"]) + for name, size, digest in self.saved: + self.assertGreater(size, 30000, name) + self.assertRegex(digest, r"^[0-9a-f]{64}$", name) + self.assertTrue(Path(fsr.LICENSE_SOURCE).is_file()) + + def test_wired(self): + src = (ROOT / "nx2" / "make_game.py").read_text(encoding="utf-8") + for needle in ("fetch_fsr_runtime(work)", 'place_fsr_runtime(out, work, os.environ.get("HOOKSHOT_FSR") == "1")', + "fsr.remove_runtime(out)"): + self.assertIn(needle, src) + shell = (ROOT / "setup" / "Shell.cs").read_text(encoding="utf-8") + self.assertIn('args.Add("--fsr")', shell) + + +if __name__ == "__main__": + raise SystemExit(unittest.main(verbosity=1)) diff --git a/tests/test_high_fps_fixes.py b/tests/test_high_fps_fixes.py index 77b78b9..49b2422 100644 --- a/tests/test_high_fps_fixes.py +++ b/tests/test_high_fps_fixes.py @@ -13,7 +13,19 @@ Solver: the game cuts frame step * n + 0.001 to its solver steps (n a 60 fps fra wanders; with n = 4, 0 above 240). With the frames' own time and a step under 1 it is rounded up (10% leeway for the frame time's jitter) and at least 1, never under the game's; a step of 1 or more, or no frame time (a cap of 60), keeps the game's count. -The patches put the gate at the job's head and the step through the host, hook both solver places, declare the hooks, and leave +Floors: the follow-surface updates keep a tenth of a floor's velocity above an inverse step of 100, which is every update above +100 fps; the number compared with becomes 100 / the frame's 60 fps frames under one frame (200 at 120 fps, 240 at 144), and stays +100 at 60 fps and below and without a frame time. + +Surfaces: the water managers and the interactive floors cut seconds / (1/60 s) to whole fixed steps, 0 above 60 fps; with the +fractions carried per object 144 updates of 60/144 give 60 steps, each object its own, and without a frame time, or for a +quotient that is no count, the quotient is handed back as it is. + +Recorder: the ocarina recorder's clock adds (unsigned)step, 0 above 60 fps; with the fraction carried 144 frames give 60, a +whole step gives the game's count, and without a frame time it is the game's cut. + +The patches put the gate at the job's head and the step through the host, hook both solver places, the three floor compares, the three surface cuts and the four +recorder adds, declare the hooks, and leave a file patched before, or whose code differs, as it is. python -I -B tests/test_high_fps_fixes.py @@ -113,6 +125,48 @@ int main(void) { CHECK(OURS(1.6f, 4.0f, 1) == 6 && OURS(1.708f, 8.0f, 1) == 13); /* (a slow frame: the game's count) */ CHECK(oot_solver_steps(3, bits(NAN), bits(4.0f), 1) == 3 && oot_solver_steps(0, bits(-1.0f), bits(4.0f), 1) == 0); CHECK(oot_solver_steps(1, bits(2.0f), bits(0.0f), 1) == 1); /* (no steps a frame: the game's) */ + /* (a floor's carry: the number the inverse step is compared with) */ + CHECK(oot_floor_cut(0) == bits(100.0f)); /* (a cap of 60) */ + CHECK(oot_floor_cut(bits(1.0f / 60.0f)) == bits(100.0f) && oot_floor_cut(bits(1.0f / 30.0f)) == bits(100.0f)); + CHECK(oot_bits_float(oot_floor_cut(bits(1.0f / 120.0f))) > 199.9f && oot_bits_float(oot_floor_cut(bits(1.0f / 120.0f))) < 200.1f); + CHECK(oot_bits_float(oot_floor_cut(bits(1.0f / 144.0f))) > 239.9f && oot_bits_float(oot_floor_cut(bits(1.0f / 144.0f))) < 240.1f); + for (int fps = 61; fps <= 240; fps++) { /* (the inverse step of a frame at fps is under the cut, as 60 is under 100) */ + const float secs = 1.0f / (float)fps; + CHECK(1.0f / secs < oot_bits_float(oot_floor_cut(bits(secs)))); + CHECK(1.0f / (secs * 0.59f) > oot_bits_float(oot_floor_cut(bits(secs)))); /* (and a slow motion under 0.6 still passes it) */ + } + CHECK(oot_floor_cut(bits(NAN)) == bits(100.0f) && oot_floor_cut(bits(-1.0f)) == bits(100.0f)); + { /* (the surfaces' fixed steps) */ + static FrameWhole w; + float n = 0, m = 0; + for (int i = 0; i < 144; i++) { + n += oot_bits_float(oot_sim_steps(&w, 0x1000, bits(60.0f / 144.0f), 1)); + m += oot_bits_float(oot_sim_steps(&w, 0x2000, bits(60.0f / 144.0f), 1)); + } + CHECK((n == 60.0f || n == 59.0f) && m == n); /* (each object its own remainder) */ + CHECK(oot_sim_steps(&w, 0x3000, bits(1.0f), 1) == bits(1.0f) && oot_sim_steps(&w, 0x3000, bits(2.0f), 1) == bits(2.0f)); + CHECK(oot_sim_steps(&w, 0x3000, bits(0.42f), 0) == bits(0.42f)); /* (a cap of 60: the quotient, for the game's cut) */ + CHECK(oot_sim_steps(&w, 0x3000, bits(0.0f), 1) == bits(0.0f) && oot_sim_steps(&w, 0x3000, bits(-2.0f), 1) == bits(-2.0f)); + CHECK(oot_sim_steps(&w, 0x3000, bits(NAN), 1) == bits(NAN) && oot_sim_steps(&w, 0x3000, bits(3e9f), 1) == bits(3e9f)); + float slow = 0; /* (a frame slower than 60 fps: 1.5 steps a frame are 1 and 2 in turn) */ + for (int i = 0; i < 40; i++) slow += oot_bits_float(oot_sim_steps(&w, 0x4000, bits(1.5f), 1)); + CHECK(slow == 60.0f); + } + { /* (the ocarina recorder's clock) */ + float rem = 0; + uint32_t n = 0; + for (int i = 0; i < 144; i++) n += oot_record_frames(&rem, bits(60.0f / 144.0f), 1); + CHECK(n == 60 || n == 59); + rem = 0, n = 0; + for (int i = 0; i < 1200; i++) n += oot_record_frames(&rem, bits(0.5f), 1); + CHECK(n == 600); + rem = 0; + CHECK(oot_record_frames(&rem, bits(1.0f), 1) == 1 && rem == 0.0f && oot_record_frames(&rem, bits(2.0f), 1) == 2 && rem == 0.0f); + CHECK(oot_record_frames(&rem, bits(0.75f), 1) == 0 && rem == 0.75f && oot_record_frames(&rem, bits(0.75f), 1) == 1 && rem == 0.5f); + CHECK(oot_record_frames(&rem, bits(0.42f), 0) == 0 && rem == 0.0f); /* (a cap of 60: the game's cut, the remainder cleared) */ + CHECK(oot_record_frames(&rem, bits(1.9f), 0) == 1 && oot_record_frames(&rem, bits(-1.0f), 1) == 0 && oot_record_frames(&rem, bits(NAN), 1) == 0); + CHECK(oot_record_frames(&rem, bits(5e9f), 0) == 0xffffffffu); /* (fcvtzu saturates) */ + } if (!fails) printf("PASS\n"); return fails != 0; } @@ -125,6 +179,18 @@ LIFTED_SOLVER = '#include "a64.h"\n' + "".join(head + " c->x[8]=1;\n" + oot_p for head in oot_patches.SOLVER_HEADS) +LIFTED_FLOOR = '#include "a64.h"\n' + "".join( + head + " { { uint64_t _v=recomp_load32(c,(c->x[8]+40ULL)); a64_vset64(c->vreg[0],_v); } }\n" + oot_patches.FLOOR_OLD + + "if ((c->n == c->v) && !c->z) { goto L_1; }\nL_1:;\n return;\n}\n" for head in oot_patches.FLOOR_HEADS) +# (a divide, the cut into the site's register, and one more cut of another register after it, as f_80652cb0 has many) +LIFTED_SIM = '#include "a64.h"\n' + "".join( + head + " _v=a64_fpx_div32_div32(c,_a,_b);\n a64_vset64(c->vreg[0],_v);}\n" + oot_patches.SIM_OLD % reg + + (oot_patches.SIM_OLD % (reg + 1)).replace("vreg[0]", "vreg[1]") + " return;\n}\n" for head, reg in oot_patches.SIM_SITES) +LIFTED_RECORD = '#include "a64.h"\n' + "".join( + head + (oot_patches.RECORD_OLD + " recomp_store32(c,c->x[20] + 1532,c->x[8]);\n") * count + + oot_patches.RECORD_OLD.replace(",32,32,0,3,", ",32,32,1,3,") + " return;\n}\n" for head, count in oot_patches.RECORD_SITES) + + class HighFpsTests(unittest.TestCase): def test_gate_and_steps(self): compiler = shutil.which("gcc") or shutil.which("clang") @@ -168,5 +234,48 @@ class HighFpsTests(unittest.TestCase): self.assertEqual(oot_patches.patch_solver_steps(other), (other, 0)) + def test_floor_patch(self): + out, n = oot_patches.patch_floor_carry(LIFTED_FLOOR) + self.assertEqual(n, 3) + self.assertEqual(out.count(oot_patches.FLOOR_NEW), 3) + self.assertNotIn("0x42c80000", out) + self.assertIn(oot_patches.FLOOR_DECL, out) + self.assertEqual(oot_patches.patch_floor_carry(out), (out, 3)) # (patched before: as it is) + other = LIFTED_FLOOR.replace("0x42c80000ULL", "0x42c90000ULL") # (another game version's code) + self.assertEqual(oot_patches.patch_floor_carry(other), (other, 0)) + # (the same words in a function that is not one of the three stay the game's) + stray = LIFTED_FLOOR + "void f_80000000(Ctx *__restrict A64_NOESC c) {\n" + oot_patches.FLOOR_OLD + "}\n" + out, n = oot_patches.patch_floor_carry(stray) + self.assertEqual((n, out.count(oot_patches.FLOOR_OLD)), (3, 1)) + + def test_sim_patch(self): + out, n = oot_patches.patch_sim_steps(LIFTED_SIM) + self.assertEqual(n, 3) + self.assertEqual(out.count(oot_patches.SIM_HOOK), 3) + for _, reg in oot_patches.SIM_SITES: # (the hook right before the game's own cut, which stays) + self.assertIn(oot_patches.SIM_HOOK + oot_patches.SIM_OLD % reg, out) + self.assertEqual(out.count("recomp_fp_to_int"), LIFTED_SIM.count("recomp_fp_to_int")) + self.assertIn(oot_patches.SIM_DECL, out) + self.assertEqual(oot_patches.patch_sim_steps(out), (out, 3)) + other = LIFTED_SIM.replace(",32,32,1,3,", ",32,31,1,3,") + self.assertEqual(oot_patches.patch_sim_steps(other), (other, 0)) + twice = LIFTED_SIM.replace(oot_patches.SIM_OLD % 22, (oot_patches.SIM_OLD % 22) * 2) # (two such cuts: not this build) + self.assertEqual(oot_patches.patch_sim_steps(twice)[1], 2) + + def test_record_patch(self): + out, n = oot_patches.patch_record_clock(LIFTED_RECORD) + self.assertEqual(n, 4) + self.assertEqual(out.count(oot_patches.RECORD_NEW), 4) + self.assertNotIn(oot_patches.RECORD_OLD, out) + self.assertEqual(out.count(",32,32,1,3,"), 3) # (the functions' signed cuts stay the game's) + self.assertIn(oot_patches.RECORD_DECL, out) + self.assertEqual(oot_patches.patch_record_clock(out), (out, 4)) + # (a function with another number of adds than this build's is left whole) + head, _ = oot_patches.RECORD_SITES[2] + other = LIFTED_RECORD.replace(head, head + oot_patches.RECORD_OLD, 1) + out, n = oot_patches.patch_record_clock(other) + self.assertEqual((n, out.count(oot_patches.RECORD_OLD)), (2, 3)) + + if __name__ == "__main__": raise SystemExit(unittest.main(verbosity=1)) diff --git a/tests/test_mouse_settings.py b/tests/test_mouse_settings.py index b2c8849..77243b8 100644 --- a/tests/test_mouse_settings.py +++ b/tests/test_mouse_settings.py @@ -28,7 +28,9 @@ def main(): raise SystemExit("the menu lost a mouse row: " + row) if "id == 7 ? opts.mouse_sens" not in replay or "id == 8 ? opts.mouse_invert" not in replay: raise SystemExit("options 7 and 8 do not reach the host") - if "t.option_set = menu_option_set" not in replay or "void (*option_set)(int id, int value);" not in api: + sets = "t.option_set = menu_option_set" in replay or ("t.option_set = [](int id, int value) {" in replay + and " menu_option_set(id, value);" in replay) + if not sets or "void (*option_set)(int id, int value);" not in api: raise SystemExit("the host cannot change the menu's sensitivity") if "vk->option_set(id, value)" not in backend: raise SystemExit("rx_option_set does not reach the renderer") diff --git a/tests/test_nvn_null_pool.py b/tests/test_nvn_null_pool.py index 634255f..0a26722 100644 --- a/tests/test_nvn_null_pool.py +++ b/tests/test_nvn_null_pool.py @@ -74,6 +74,7 @@ static uint64_t h2g(void *p) { return (uint64_t)(uintptr_t)p; } #define RETP(p) RET(h2g(p)) #define U64(i) (c->x[i]) static void rx_nvn_invalidate(uint64_t a, uint64_t b) { (void)a; (void)b; } +static void flush_note(Ctx *c, int kind, uint64_t gpu, uint64_t size) { (void)c; (void)kind; (void)gpu; (void)size; } /* (the lab's HOS_NVN_LOG_FLUSH line) */ ''' + guard + "\n" + flags + "\n" + size + "\n" + map_ + "\n" + address + "\n" + invalidate + r''' int main(void) { Ctx c; diff --git a/tests/test_output_resolution.py b/tests/test_output_resolution.py index eb39549..664dcac 100644 --- a/tests/test_output_resolution.py +++ b/tests/test_output_resolution.py @@ -31,6 +31,7 @@ constexpr int MAX_PATH = 260; DWORD hos_game_path_a(char *path, int size) { return snprintf(path, size, "./hookshot.exe"); } void hos_log(const char *, ...) {} volatile uint64_t hos_wide_out = 0; // (host/hos_main.c: the widescreen picture, 0 = none; nn_applet.c reads it) +int hos_scene_rgba16f(void) { return 0; } // (host/nn_vi.c: the scene's colour target, the game's own here; tests/test_rt_pool_minimum.py has the RGBA16F cases) struct { int handheld, output_res; } opts; namespace mkwvk { int up_now = 0; // (the Upscaler shown: 0 the game's own, 1 DLSS, 2 DLAA) @@ -98,7 +99,7 @@ int main(int argc, char **argv) { source.write_text(fixture, encoding="utf-8") subprocess.run([args.cxx, "-std=c++17", "-I", str(ROOT), str(source), "-o", str(exe)], check=True) for saved, handheld, override, mode, w, h, label in cases: - (work / "hookshot_options.ini").write_text(f"output_res={saved}\nhandheld_profile={handheld}\n", encoding="ascii") + (work / "triforce_options.ini").write_text(f"output_res={saved}\nhandheld_profile={handheld}\n", encoding="ascii") env = os.environ.copy() for key in ("HOS_OUTPUT_RES", "HOS_HANDHELD", "HOS_RT_POOL_MB"): env.pop(key, None) diff --git a/tests/test_rt_pool_minimum.py b/tests/test_rt_pool_minimum.py index eab53c5..cc77a59 100644 --- a/tests/test_rt_pool_minimum.py +++ b/tests/test_rt_pool_minimum.py @@ -84,6 +84,14 @@ int main(void) { if (hos_rt_pool_size(305 * MIB) != (512 + 128) * MIB) { fprintf(stderr, "RGBA16F scene: not 640 MiB\n"); failed = 1; } override = "256"; if (hos_rt_pool_size(305 * MIB) != 256 * MIB) { fprintf(stderr, "RGBA16F scene: the override not kept\n"); failed = 1; } + /* (the 5120 x 2880 output with an RGBA16F scene: 900 MiB for the whole pool, not 900 + 128, which failed the game's + reservation at boot; 3840 x 2160 keeps its 128 MiB more, and an explicit override still wins) */ + output_width = 5120; output_height = 2880; override = NULL; + if (hos_rt_pool_size(305 * MIB) != 900 * MIB) { fprintf(stderr, "RGBA16F scene at 5120x2880: not 900 MiB\n"); failed = 1; } + override = "1028"; + if (hos_rt_pool_size(305 * MIB) != 1028 * MIB) { fprintf(stderr, "RGBA16F scene at 5120x2880: the override not kept\n"); failed = 1; } + output_width = 3840; output_height = 2160; override = NULL; + if (hos_rt_pool_size(305 * MIB) != (687 + 128) * MIB) { fprintf(stderr, "RGBA16F scene at 3840x2160: not 815 MiB\n"); failed = 1; } return failed; } ''' @@ -98,7 +106,7 @@ int main(void) { subprocess.run([compiler, "-std=c11", "-O2", str(source), "-o", str(binary)], check=True) subprocess.run([str(binary)], check=True, timeout=5) print("PASS: 512 MiB floor, 4K/ultrawide growth and explicit overrides, the 5K output's 900 MiB (14 cases), " - "an RGBA16F scene's 128 MiB more") + "an RGBA16F scene's 128 MiB more, and the 5K output's 900 MiB with it") if __name__ == "__main__": diff --git a/tests/test_save_tx.py b/tests/test_save_tx.py new file mode 100644 index 0000000..554c541 --- /dev/null +++ b/tests/test_save_tx.py @@ -0,0 +1,242 @@ +"""The save as a transaction, with backups (host/save_tx.h), on real folders in a temporary place. + +A Commit copies the working copy to a staging folder, renames that to ready- (the commit point) and swaps it in for the +committed save, whose old state becomes a backup at most every five minutes, the newest five kept. Checked here: +- a first save, a second one and its backup, the five-minute gap, the five kept, a player's own folder among them left alone; +- a Commit killed before each of its steps, then the next start: the committed save is the whole old one or the whole new one, + never a mix, it is the new one once the commit point was passed, and the working copy equals it; +- changes no Commit took are gone at the next start, as on the console; +- a staging folder that cannot be made: the files are copied over the save's as before the transactions (result 0); +- (Windows) a save folder another program holds open: the commit waits as ready-, the next start plays from it, and the + next Commit with the folder free puts it in place. + +python -I -B tests/test_save_tx.py +""" +import shutil +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] + +FIXTURE = r""" +#define WIN32_LEAN_AND_MEAN +#include +#define HOS_FS_PATH 1024 +#include "save_tx.h" +#include +static int fails; +#define CHECK(c) do { if (!(c)) { printf("FAIL line %d: %s\n", __LINE__, #c); fails++; } } while (0) + +static void put(const char *dir, const char *name, const char *text) { + char path[HOS_FS_PATH]; + snprintf(path, sizeof path, "%s\\%s", dir, name); + FILE *f = fopen(path, "wb"); + if (!f) { printf("FAIL: cannot write %s\n", path); fails++; return; } + for (int i = 0; i < 3000; i++) fputs(text, f); /* (more than one copy chunk would hold: 3000 x a few bytes) */ + fclose(f); +} +static int has(const char *dir, const char *name, const char *text) { + char path[HOS_FS_PATH], got[16] = ""; + snprintf(path, sizeof path, "%s\\%s", dir, name); + FILE *f = fopen(path, "rb"); + if (!f) return 0; + fseek(f, 0, SEEK_END); + const long size = ftell(f); + fseek(f, size - (long)strlen(text), SEEK_SET); + const size_t n = fread(got, 1, strlen(text), f); + fclose(f); + return size == 3000L * (long)strlen(text) && n == strlen(text) && !memcmp(got, text, n); +} +/* (a save of three files, one in a slot folder, all written with one tag) */ +static void write_save(const char *dir, const char *tag) { + char slot[HOS_FS_PATH]; + tx_make_dirs(dir); + snprintf(slot, sizeof slot, "%s\\slot_00", dir); + tx_make_dirs(slot); + put(dir, "account.sav", tag); + put(dir, "option.sav", tag); + put(slot, "progress.sav", tag); +} +/* 1: the folder is exactly a save written with tag */ +static int is_save(const char *dir, const char *tag) { + char slot[HOS_FS_PATH]; + snprintf(slot, sizeof slot, "%s\\slot_00", dir); + return has(dir, "account.sav", tag) && has(dir, "option.sav", tag) && has(slot, "progress.sav", tag); +} +static int count_dirs(const char *dir) { + char pat[HOS_FS_PATH]; + WIN32_FIND_DATAA fd; + int n = 0; + snprintf(pat, sizeof pat, "%s\\*", dir); + HANDLE f = FindFirstFileA(pat, &fd); + if (f == INVALID_HANDLE_VALUE) return 0; + do { + if (strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) n++; + } while (FindNextFileA(f, &fd)); + FindClose(f); + return n; +} +static void new_run(void) { save_tx_crash_at = save_tx_step = save_tx_dead = 0; save_tx_last_backup = 0; } +static int clean_tx(const SaveTxPaths *p) { /* nothing of a commit left behind */ + return !tx_exists(p->staging) && !tx_exists(p->old) && !tx_ready_highest(p, NULL, 0); +} + +int main(int argc, char **argv) { + if (argc < 2) return 2; + char user[HOS_FS_PATH], note[256], backup[HOS_FS_PATH]; + SaveTxPaths p; + save_tx_test_now = 1800000000; + + /* a first save, a second one and its backup, the gap, the five kept */ + snprintf(user, sizeof user, "%s\\a", argv[1]); + save_tx_paths(&p, user, "0123456789abcdef0000000000000001"); + new_run(); + CHECK(save_tx_mount(&p, note, sizeof note) == 1 && !note[0]); + CHECK(tx_is_dir(p.work) && tx_is_dir(p.save)); + write_save(p.work, "v1"); + CHECK(save_tx_commit(&p, backup, sizeof backup) == 1 && !backup[0]); + CHECK(is_save(p.save, "v1") && clean_tx(&p) && count_dirs(p.backups) == 0); + write_save(p.work, "v2"); + CHECK(save_tx_commit(&p, backup, sizeof backup) == 1 && backup[0] && is_save(backup, "v1")); + CHECK(is_save(p.save, "v2") && clean_tx(&p) && count_dirs(p.backups) == 1); + save_tx_test_now += 10; /* (ten seconds on: no backup) */ + write_save(p.work, "v3"); + CHECK(save_tx_commit(&p, backup, sizeof backup) == 1 && !backup[0] && is_save(p.save, "v3") && count_dirs(p.backups) == 1); + save_tx_test_now += SAVE_TX_BACKUP_GAP_S; + write_save(p.work, "v4"); + CHECK(save_tx_commit(&p, backup, sizeof backup) == 1 && is_save(backup, "v3") && count_dirs(p.backups) == 2); + { /* (a folder of the player's own among the backups stays) */ + char own[HOS_FS_PATH]; + snprintf(own, sizeof own, "%s\\my old save", p.backups); + write_save(own, "me"); + for (int i = 0; i < 9; i++) { + save_tx_test_now += SAVE_TX_BACKUP_GAP_S; + write_save(p.work, i & 1 ? "wa" : "wb"); + CHECK(save_tx_commit(&p, backup, sizeof backup) == 1 && backup[0]); + } + CHECK(count_dirs(p.backups) == SAVE_TX_BACKUPS + 1 && is_save(own, "me")); + CHECK(is_save(backup, "wa") && is_save(p.save, "wb")); /* (the last backup is the save before the last) */ + } + { /* (two backups in one second get two names) */ + save_tx_last_backup = 0; + write_save(p.work, "x1"); + CHECK(save_tx_commit(&p, backup, sizeof backup) == 1 && backup[0]); + save_tx_last_backup = 0; + char second[HOS_FS_PATH]; + write_save(p.work, "x2"); + CHECK(save_tx_commit(&p, second, sizeof second) == 1 && second[0] && strcmp(second, backup)); + CHECK(is_save(second, "x1") && count_dirs(p.backups) == SAVE_TX_BACKUPS + 1); + } + + /* changes no Commit took are gone at the next start */ + write_save(p.work, "zz"); + new_run(); + CHECK(save_tx_mount(&p, note, sizeof note) == 1 && !note[0]); + CHECK(is_save(p.work, "x2") && is_save(p.save, "x2")); + + /* a Commit killed before each of its steps, then the next start */ + int reached_end = 0; + for (int at = 1; at <= 12 && !reached_end; at++) { + char sub[32]; + snprintf(sub, sizeof sub, "%s\\k%d", argv[1], at); + snprintf(user, sizeof user, "%s", sub); + save_tx_paths(&p, user, "0123456789abcdef0000000000000001"); + new_run(); + CHECK(save_tx_mount(&p, NULL, 0) == 1); + write_save(p.work, "o1"); + CHECK(save_tx_commit(&p, NULL, 0) == 1 && is_save(p.save, "o1")); + write_save(p.work, "n2"); + save_tx_crash_at = at, save_tx_step = 0, save_tx_dead = 0; + const int r = save_tx_commit(&p, NULL, 0); + const int died = save_tx_dead; + if (!died) reached_end = 1; /* (more steps asked for than a Commit has: it ran whole) */ + new_run(); + CHECK(save_tx_mount(&p, note, sizeof note) == 1); + const int old = is_save(p.save, "o1"), new_ = is_save(p.save, "n2"); + if (!(old ^ new_)) { printf("FAIL: killed before step %d, the save is neither the old nor the new one\n", at); fails++; } + if (r == 1 && !new_) { printf("FAIL: killed before step %d after the commit point, the save is not the new one\n", at); fails++; } + if (r != 1 && died && !old) { printf("FAIL: killed before step %d before the commit point, the save is not the old one\n", at); fails++; } + CHECK(is_save(p.work, new_ ? "n2" : "o1") && clean_tx(&p)); + /* (and the game saves on from there) */ + write_save(p.work, "n3"); + CHECK(save_tx_commit(&p, NULL, 0) == 1 && is_save(p.save, "n3") && clean_tx(&p)); + } + CHECK(reached_end); + + /* a staging folder that cannot be made: the files copied over the save's, as before the transactions */ + snprintf(user, sizeof user, "%s\\f", argv[1]); + save_tx_paths(&p, user, "0123456789abcdef0000000000000001"); + new_run(); + CHECK(save_tx_mount(&p, NULL, 0) == 1); + write_save(p.work, "f1"); + CHECK(save_tx_commit(&p, NULL, 0) == 1); + { FILE *f = fopen(p.staging, "wb"); if (f) fclose(f); } /* (a file where the staging folder goes) */ + write_save(p.work, "f2"); + CHECK(save_tx_commit(&p, NULL, 0) == 0 && is_save(p.save, "f2")); + DeleteFileA(p.staging); + write_save(p.work, "f3"); + CHECK(save_tx_commit(&p, NULL, 0) == 1 && is_save(p.save, "f3") && clean_tx(&p)); + +#ifdef _WIN32 + /* a save folder another program holds open */ + snprintf(user, sizeof user, "%s\\h", argv[1]); + save_tx_paths(&p, user, "0123456789abcdef0000000000000001"); + new_run(); + CHECK(save_tx_mount(&p, NULL, 0) == 1); + write_save(p.work, "h1"); + CHECK(save_tx_commit(&p, NULL, 0) == 1 && is_save(p.save, "h1")); + char held[HOS_FS_PATH]; + snprintf(held, sizeof held, "%s\\option.sav", p.save); + HANDLE h = CreateFileA(held, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + CHECK(h != INVALID_HANDLE_VALUE); + write_save(p.work, "h2"); + CHECK(save_tx_commit(&p, NULL, 0) == 1); + CHECK(is_save(p.save, "h1") && tx_ready_highest(&p, NULL, 0) == 1); /* (committed, waiting) */ + new_run(); + CHECK(save_tx_mount(&p, note, sizeof note) == 1 && note[0]); + CHECK(is_save(p.work, "h2") && is_save(p.save, "h1")); /* (the next start plays from the newest save) */ + write_save(p.work, "h3"); + CHECK(save_tx_commit(&p, NULL, 0) == 1 && tx_ready_highest(&p, NULL, 0) == 2 && is_save(p.save, "h1")); + CloseHandle(h); + write_save(p.work, "h4"); + CHECK(save_tx_commit(&p, NULL, 0) == 1 && is_save(p.save, "h4") && clean_tx(&p)); + /* (and held again, then freed before the next start: that start puts the waiting save in place) */ + h = CreateFileA(held, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + write_save(p.work, "h5"); + CHECK(save_tx_commit(&p, NULL, 0) == 1 && is_save(p.save, "h4")); + CloseHandle(h); + new_run(); + CHECK(save_tx_mount(&p, note, sizeof note) == 1 && note[0] && is_save(p.save, "h5") && is_save(p.work, "h5") && clean_tx(&p)); +#endif + if (!fails) printf("PASS\n"); + return fails != 0; +} +""" + + +class SaveTxTests(unittest.TestCase): + def test_transactions(self): + compiler = shutil.which("gcc") or shutil.which("clang") + if not compiler: + self.skipTest("no C compiler") + if sys.platform != "win32": + self.skipTest("the fixture calls the Windows file functions (the game's Linux build has them from host/posix)") + with tempfile.TemporaryDirectory(prefix="triforce-save-tx-") as tmp: + tmp = Path(tmp) + (tmp / "t.c").write_text(FIXTURE, encoding="utf-8") + binary = tmp / "t.exe" + build = subprocess.run([compiler, "-std=gnu11", "-Wall", "-O1", "-I", str(ROOT / "host"), str(tmp / "t.c"), + "-o", str(binary)], text=True, capture_output=True) + self.assertEqual(build.returncode, 0, build.stderr) + work = tmp / "saves" + work.mkdir() + run = subprocess.run([str(binary), str(work)], text=True, capture_output=True, timeout=120) + self.assertEqual(run.returncode, 0, run.stdout + run.stderr) + self.assertIn("PASS", run.stdout) + + +if __name__ == "__main__": + raise SystemExit(unittest.main(verbosity=1)) diff --git a/tests/test_sprint.py b/tests/test_sprint.py new file mode 100644 index 0000000..9b3ff36 --- /dev/null +++ b/tests/test_sprint.py @@ -0,0 +1,243 @@ +"""The sprint button (host/oot_sprint.h, nx2/oot_patches.py patch_sprint). + +Link sprints while his input flags hold the dash input, which the game sets only out of a roll. With a button chosen, +the host puts the bit in while the button's sprint is on. The rules checked here, without the game: + +- Off: nothing is taken from the controller and no sprint starts. +- Toggle with the left stick's click: a press in gameplay starts the sprint, the next ends it; the click never reaches + the game in gameplay; standing still ends a sprint that had moved after 4 frames (60 Hz frames: 8 updates at 120 fps do + not end it, 9 do), one that has not moved yet after 90; a state the list does not know ends it at once; ZL ends it. +- A press with no update of Link for 8 polls (a menu, a cutscene) is the game's and starts nothing. +- B: a sprint only for a press that begins while Link moves, kept from the game until it is let go; a press while + standing is the game's own B, whole. +- Hold: on while the button is held, off when it is let go. +- Changing the button ends a sprint. + +The patch: the two calls go in once each, a second run changes nothing, other code is left as it is. + +python -I -B tests/test_sprint.py +""" +import shutil +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "nx2")) +import oot_patches # noqa: E402 + +FIXTURE = r''' +#include +#include "oot_sprint.h" + +static int fails; +#define CHECK(cond, what) do { if (!(cond)) { fails++; printf("FAIL line %d: %s\n", __LINE__, what); } } while (0) + +static OotSprint s; +static void fresh(void) { OotSprint z = OOT_SPRINT_INIT; s = z; } +/* one frame: Link's update, then the controller's poll; the buttons the game gets */ +static int on; +static uint64_t frame(int kind, int button, int hold, double frames, uint64_t buttons) { + on = oot_sprint_step(&s, kind, button, hold, frames); + oot_sprint_pad(&s, button, &buttons); + return buttons; +} + +int main(void) { + const uint64_t LS = OOT_SPRINT_BIT_LS, B = OOT_SPRINT_BIT_B, ZL = OOT_SPRINT_BIT_ZL, A = 1; + uint64_t got; + + /* the states */ + CHECK(oot_sprint_kind(1) == OOT_SPRINT_MOVE && oot_sprint_kind(13) == OOT_SPRINT_MOVE && oot_sprint_kind(68) == OOT_SPRINT_MOVE, "Walk, Jump, Clamber move"); + CHECK(oot_sprint_kind(35) == OOT_SPRINT_KEEP && oot_sprint_kind(253) == OOT_SPRINT_KEEP, "Slide, StepOn keep"); + CHECK(oot_sprint_kind(40) == OOT_SPRINT_WAIT, "Wait waits"); + CHECK(oot_sprint_kind(0) == OOT_SPRINT_STOP && oot_sprint_kind(-1) == OOT_SPRINT_STOP && oot_sprint_kind(999) == OOT_SPRINT_STOP, "anything else stops"); + + /* off */ + fresh(); + got = frame(OOT_SPRINT_MOVE, OOT_SPRINT_OFF, 0, 1, LS | A); + got = frame(OOT_SPRINT_MOVE, OOT_SPRINT_OFF, 0, 1, LS | A); + CHECK(got == (LS | A) && !on, "off: the click is the game's, no sprint"); + + /* toggle, the stick's click */ + fresh(); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, 0); + got = frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, LS | A); /* the press: seen by the next update */ + CHECK(got == A, "the click is kept from the game in gameplay"); + got = frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, LS); + CHECK(on && got == 0, "the press started the sprint"); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, 0); + CHECK(on, "it stays on with the button let go"); + for (int i = 0; i < 4; i++) frame(OOT_SPRINT_WAIT, OOT_SPRINT_LS, 0, 1, 0); + CHECK(on, "four frames of standing do not end a sprint that moved"); + frame(OOT_SPRINT_WAIT, OOT_SPRINT_LS, 0, 1, 0); + CHECK(!on, "the fifth does"); + /* the next press starts again, the one after ends it */ + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, LS); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, 0); + CHECK(on, "on again"); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, LS); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, 0); + CHECK(!on, "the next press ends it"); + + /* at 120 fps the standing time is the same 4 frames of 60 Hz */ + fresh(); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 0.5, 0); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 0.5, LS); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 0.5, 0); + CHECK(on, "on at 120 fps"); + for (int i = 0; i < 8; i++) frame(OOT_SPRINT_WAIT, OOT_SPRINT_LS, 0, 0.5, 0); + CHECK(on, "eight updates of half a frame do not end it"); + frame(OOT_SPRINT_WAIT, OOT_SPRINT_LS, 0, 0.5, 0); + CHECK(!on, "the ninth does"); + + /* a press while standing: on, and 90 frames to start moving */ + fresh(); + frame(OOT_SPRINT_WAIT, OOT_SPRINT_LS, 0, 1, 0); + frame(OOT_SPRINT_WAIT, OOT_SPRINT_LS, 0, 1, LS); + frame(OOT_SPRINT_WAIT, OOT_SPRINT_LS, 0, 1, 0); + CHECK(on, "a press while standing arms the sprint"); + for (int i = 0; i < 89; i++) frame(OOT_SPRINT_WAIT, OOT_SPRINT_LS, 0, 1, 0); + CHECK(on, "still armed after 89 frames"); + frame(OOT_SPRINT_WAIT, OOT_SPRINT_LS, 0, 1, 0); + frame(OOT_SPRINT_WAIT, OOT_SPRINT_LS, 0, 1, 0); + CHECK(!on, "not after 91"); + + /* an unknown state, and ZL */ + fresh(); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, 0); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, LS); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, 0); + frame(OOT_SPRINT_KEEP, OOT_SPRINT_LS, 0, 1, 0); + CHECK(on, "a slide keeps it"); + frame(OOT_SPRINT_STOP, OOT_SPRINT_LS, 0, 1, 0); + CHECK(!on, "a state the list does not know ends it"); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, LS); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, ZL); + CHECK(on, "on (ZL is seen by the next update)"); + got = frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, ZL); + CHECK(!on && got == ZL, "targeting ends it, and ZL is the game's"); + + /* no update of Link: a menu. The press is the game's and starts nothing */ + fresh(); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, 0); + for (int i = 0; i < 9; i++) { uint64_t b = 0; oot_sprint_pad(&s, OOT_SPRINT_LS, &b); } + got = LS; + oot_sprint_pad(&s, OOT_SPRINT_LS, &got); + CHECK(got == LS, "in a menu the click reaches the game"); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, LS); + CHECK(!on, "and starts no sprint when play goes on with it still held"); + got = frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, LS); + CHECK(!on && got == LS, "the whole press stays the game's"); + + /* B */ + fresh(); + frame(OOT_SPRINT_WAIT, OOT_SPRINT_B, 0, 1, 0); + got = frame(OOT_SPRINT_WAIT, OOT_SPRINT_B, 0, 1, B); + CHECK(got == B, "B while standing is the game's B"); + got = frame(OOT_SPRINT_WAIT, OOT_SPRINT_B, 0, 1, B); + CHECK(!on && got == B, "and no sprint"); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_B, 0, 1, 0); + got = frame(OOT_SPRINT_MOVE, OOT_SPRINT_B, 0, 1, B); + CHECK(got == 0, "B while moving is kept from the game"); + got = frame(OOT_SPRINT_WAIT, OOT_SPRINT_B, 0, 1, B); + CHECK(got == 0, "until it is let go, whatever Link does meanwhile"); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_B, 0, 1, 0); + got = frame(OOT_SPRINT_WAIT, OOT_SPRINT_B, 0, 1, B); + CHECK(got == B, "the next press, standing, is the game's again"); + + /* hold */ + fresh(); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 1, 1, 0); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 1, 1, LS); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 1, 1, LS); + CHECK(on, "hold: on while held"); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 1, 1, 0); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 1, 1, 0); + CHECK(!on, "hold: off when let go"); + + /* another button chosen */ + fresh(); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, 0); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, LS); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, 0); + CHECK(on, "on with the click"); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_B, 0, 1, 0); + CHECK(!on, "choosing B ends it"); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, LS); + frame(OOT_SPRINT_MOVE, OOT_SPRINT_OFF, 0, 1, 0); + CHECK(!on && !s.active && !s.pressed, "off leaves nothing on"); + + printf(fails ? "sprint: FAILED\n" : "sprint: all checks passed\n"); + return fails != 0; +} +''' + +LIFTED = ( + '#include "a64.h"\n' + "void f_804caa80(Ctx *__restrict A64_NOESC c) {\n" + " c->x[10]=recomp_load64(c,c->x[10] + 0);\n" # (the same load elsewhere in the function: left) + "L_804ce790:;\n" + + oot_patches.SPRINT_LINK_OLD + + "if (((c->x[9] >> 20) & 1)) { goto L_804cfa90; }\n" + "if (((c->x[10] >> 4) & 1)) { goto L_804ced50; }\n" + "}\n" + "\nvoid f_80c225d0(Ctx *__restrict A64_NOESC c) {\n" + + oot_patches.SPRINT_STANCE_OLD + + "}\n" + "\nvoid f_80000000(Ctx *__restrict A64_NOESC c) {\n" # (another function with the same lines: left) + + oot_patches.SPRINT_LINK_OLD + oot_patches.SPRINT_STANCE_OLD + + "}\n" +) + + +class SprintTest(unittest.TestCase): + def test_rules(self): + compiler = shutil.which("gcc") or shutil.which("clang") + if not compiler: + self.skipTest("no C compiler") + with tempfile.TemporaryDirectory(prefix="sprint-") as d: + tmp = Path(d) + (tmp / "t.c").write_text(FIXTURE, encoding="utf-8") + binary = tmp / ("t.exe" if sys.platform == "win32" else "t") + build = subprocess.run([compiler, "-std=gnu11", "-Wall", "-Wextra", "-O1", "-I", str(ROOT / "host"), + str(tmp / "t.c"), "-o", str(binary)], text=True, capture_output=True) + self.assertEqual(build.returncode, 0, build.stderr) + run = subprocess.run([str(binary)], text=True, capture_output=True, timeout=30) + self.assertEqual(run.returncode, 0, run.stdout + run.stderr) + self.assertIn("all checks passed", run.stdout) + + def test_patch(self): + out, n = oot_patches.patch_sprint(LIFTED) + self.assertEqual(n, 2) + self.assertEqual(out.count("hos_oot_sprint_link(c,c->x[19],c->x[10]);"), 1) + self.assertEqual(out.count("hos_oot_sprint_stance(c,c->x[19],c->x[8]);"), 1) + self.assertEqual(out.count(oot_patches.SPRINT_DECL), 1) + link = out.index("hos_oot_sprint_link(c,") + self.assertLess(out.index("c->x[10]=recomp_load64(c,c->x[19] + 80);"), link) + self.assertLess(link, out.index("if (((c->x[9] >> 20) & 1))")) # (before both tests of the flags) + stance = out.index("hos_oot_sprint_stance(c,") + self.assertLess(stance, out.index("goto L_80c27f54;")) + self.assertIn(oot_patches.SPRINT_LINK_OLD + oot_patches.SPRINT_STANCE_OLD, out) # (the other function: as it was) + self.assertEqual(oot_patches.patch_sprint(out), (out, 2)) # (a second run) + + def test_other_code_left(self): + other = LIFTED.replace("c->x[10]=recomp_load64(c,c->x[19] + 80);", "c->x[10]=recomp_load64(c,c->x[19] + 88);", 1) + out, n = oot_patches.patch_sprint(other) + self.assertEqual(n, 1) # (the stance's alone) + self.assertNotIn("hos_oot_sprint_link(c,", out) + + def test_wired(self): + hid = (ROOT / "host/nn_hid.c").read_text(encoding="utf-8") + self.assertIn("hos_oot_sprint_pad(&buttons);", hid) + api = (ROOT / "gpu/vk/vk_api.h").read_text(encoding="utf-8") + self.assertIn("HOS_OPT_SPRINT, HOS_OPT_SPRINT_HOLD", api) + replay = (ROOT / "gpu/vk/vk_replay.cpp").read_text(encoding="utf-8") + self.assertIn("if (id == HOS_OPT_SPRINT) return opts.sprint_button;", replay) + self.assertIn("if (id == HOS_OPT_SPRINT_HOLD) return opts.sprint_mode;", replay) + + +if __name__ == "__main__": + raise SystemExit(unittest.main(verbosity=1)) diff --git a/tests/test_spv_exact.cpp b/tests/test_spv_exact.cpp new file mode 100644 index 0000000..66fab78 --- /dev/null +++ b/tests/test_spv_exact.cpp @@ -0,0 +1,143 @@ +// spv_exact::scan (gpu/vk/spv_exact.h): the texture bindings a shader reads by texel position or asks the size of. +// tests/test_spv_exact.py +#include "../gpu/vk/spv_exact.h" + +#include +#include +#include + +static int failures = 0; +static void expect(bool ok, const char* what) { + if (!ok) { + std::printf("FAIL: %s\n", what); + failures++; + } +} + +struct Module { + std::vector w{0x07230203u, 0x00010500u, 0, 100, 0}; + void op(uint16_t code, std::initializer_list args) { + w.push_back((uint32_t)(args.size() + 1) << 16 | code); + for (uint32_t a : args) w.push_back(a); + } + // a combined image sampler variable `var` at `binding`: dim 1 is 2D, 2 is 3D + void texture(uint32_t var, uint32_t binding, uint32_t dim, bool arrayed, bool ms = false, uint32_t sampled = 1, bool in_array = false) { + const uint32_t base = var * 10; + op(71, {var, 33, binding}); // OpDecorate Binding + op(71, {var, 34, 0}); // OpDecorate DescriptorSet + op(25, {base + 1, 1, dim, 0, arrayed ? 1u : 0u, ms ? 1u : 0u, sampled, 0}); // OpTypeImage + op(27, {base + 2, base + 1}); // OpTypeSampledImage + uint32_t t = base + 2; + if (in_array) op(28, {base + 3, t, 2}), t = base + 3; // OpTypeArray + op(32, {base + 4, 0, t}); // OpTypePointer + op(59, {base + 4, var, 0}); // OpVariable + } + // the image of the variable, loaded: returns its id + uint32_t image(uint32_t var, uint32_t id, bool through_chain = false) { + uint32_t from = var; + if (through_chain) op(65, {0, id + 2, var, 7}), from = id + 2; // OpAccessChain + op(61, {0, id, from}); // OpLoad + op(100, {0, id + 1, id}); // OpImage + return id + 1; + } + spv_exact::Bindings scan() const { return spv_exact::scan(w.data(), w.size()); } +}; + +int main() { + using spv_exact::has; + using spv_exact::any; + { // sampled only: nothing + Module m; + m.texture(10, 3, 1, false); + m.op(61, {0, 200, 10}); // OpLoad + m.op(87, {0, 201, 200, 202}); // OpImageSampleImplicitLod + expect(!any(m.scan()), "a sampled read marks nothing"); + } + { // a fetch + Module m; + m.texture(10, 3, 1, false); + m.texture(11, 4, 1, false); + m.op(95, {0, 300, m.image(10, 200), 301}); // OpImageFetch + const auto b = m.scan(); + expect(has(b, 3) && !has(b, 4), "a fetch marks its binding and no other"); + } + { // a fetch through an access chain into an array of textures + Module m; + m.texture(10, 9, 1, false, false, 1, true); + m.op(95, {0, 300, m.image(10, 200, true), 301}); + expect(has(m.scan(), 9), "a fetch through an array of textures marks the array's binding"); + } + { // the size of a 2D texture + Module m; + m.texture(10, 5, 1, false); + m.op(103, {0, 300, m.image(10, 200), 301}); // OpImageQuerySizeLod + m.op(81, {0, 302, 300, 0}); // .x + expect(has(m.scan(), 5), "a size query on a 2D texture marks it"); + } + { // the number of levels + Module m; + m.texture(10, 6, 1, true); + m.op(106, {0, 300, m.image(10, 200)}); // OpImageQueryLevels + expect(has(m.scan(), 6), "a level count query marks it"); + } + { // the layer count of an array alone + Module m; + m.texture(10, 7, 1, true); + m.op(103, {0, 300, m.image(10, 200), 301}); + m.op(81, {0, 302, 300, 2}); // .z + m.op(81, {0, 303, 300, 2}); + expect(!any(m.scan()), "an array's size used for its layer count alone marks nothing"); + } + { // an array's size used for its width too + Module m; + m.texture(10, 7, 1, true); + m.op(103, {0, 300, m.image(10, 200), 301}); + m.op(81, {0, 302, 300, 2}); + m.op(81, {0, 303, 300, 0}); + expect(has(m.scan(), 7), "an array's size used for its width marks it"); + } + { // an array's size handed on whole + Module m; + m.texture(10, 7, 1, true); + m.op(103, {0, 300, m.image(10, 200), 301}); + m.op(111, {0, 302, 300}); // OpConvertSToF of the whole vector + expect(has(m.scan(), 7), "an array's size used whole marks it"); + } + { // a 2D texture's third component does not exist: any use of its size counts + Module m; + m.texture(10, 8, 1, false); + m.op(103, {0, 300, m.image(10, 200), 301}); + expect(has(m.scan(), 8), "a 2D texture's size query marks it whatever is done with it"); + } + { // the kinds the texture detail never touches + Module m; + m.texture(10, 1, 2, false); // 3D + m.texture(11, 2, 1, false, true); // multisampled + m.texture(12, 3, 1, false, false, 2); // a storage image + m.op(95, {0, 300, m.image(10, 200), 301}); + m.op(95, {0, 310, m.image(11, 210), 311}); + m.op(104, {0, 320, m.image(12, 220)}); // OpImageQuerySize + expect(!any(m.scan()), "3D, multisampled and storage images mark nothing"); + } + { // two textures, one fetched and one sampled, bindings past 63 + Module m; + m.texture(10, 70, 1, false); + m.texture(11, 200, 1, true); + m.op(95, {0, 300, m.image(11, 200), 301}); + const auto b = m.scan(); + expect(has(b, 200) && !has(b, 70) && !has(b, 255) && !has(b, 300), "bindings up to 255"); + } + { // not SPIR-V, or cut short + const uint32_t junk[] = {1, 2, 3, 4, 5, 6}; + expect(!any(spv_exact::scan(junk, 6)), "another magic number"); + Module m; + m.texture(10, 3, 1, false); + m.op(95, {0, 300, m.image(10, 200), 301}); + m.w.push_back(9u << 16 | 95); // an instruction longer than what is left + expect(has(m.scan(), 3), "a module cut short keeps what was read"); + expect(!any(spv_exact::scan(m.w.data(), 3)), "shorter than a header"); + } + if (failures) return EXIT_FAILURE; + std::printf("spv exact: all checks passed\n"); + return EXIT_SUCCESS; +} diff --git a/tests/test_spv_exact.py b/tests/test_spv_exact.py new file mode 100644 index 0000000..c8d0bf9 --- /dev/null +++ b/tests/test_spv_exact.py @@ -0,0 +1,38 @@ +"""Compile and run the check for texture bindings read by texel position (gpu/vk/spv_exact.h; no game/GPU).""" +import argparse +import subprocess +import tempfile +from pathlib import Path + +RUN_ALL_ARGS = ["--cxx", "g++"] + +ROOT = Path(__file__).resolve().parents[1] + + +def check_source(): + """The shader modules are still scanned, and a draw still asks for a texture whole at such a binding""" + shader = (ROOT / "gpu/vk/vk_shader.cpp").read_text(encoding="utf-8") + draw = (ROOT / "gpu/vk/vk_draw.cpp").read_text(encoding="utf-8") + texture = (ROOT / "gpu/vk/vk_texture.cpp").read_text(encoding="utf-8") + for src, name, needles in ((shader, "vk_shader.cpp", ('#include "spv_exact.h"', "spv_exact::scan(")), + (draw, "vk_draw.cpp", ("texture_want_whole(tex)",)), + (texture, "vk_texture.cpp", ("void texture_want_whole(Texture* tex)", "whole_keys.count(key)"))): + for needle in needles: + if needle not in src: + raise SystemExit(name + " no longer has: " + needle) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--cxx", default="g++") + args = parser.parse_args() + check_source() + with tempfile.TemporaryDirectory(prefix="spv-exact-") as tmp: + exe = Path(tmp) / "spv-exact" + subprocess.run([args.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror", + str(ROOT / "tests/test_spv_exact.cpp"), "-o", str(exe)], check=True) + subprocess.run([str(exe)], check=True) + + +if __name__ == "__main__": + main() diff --git a/tests/test_streamline_sdk.py b/tests/test_streamline_sdk.py new file mode 100644 index 0000000..66055de --- /dev/null +++ b/tests/test_streamline_sdk.py @@ -0,0 +1,190 @@ +"""NVIDIA's Streamline files for DLSS frame generation (scripts/streamline_sdk.py, nx2/make_game.py place_dlssg_runtime). + +Without the network: an archive made here stands for NVIDIA's release, and the script's file table is pointed at it. +- every listed file is taken out of the archive into the SDK folder at its own path, also from an archive whose paths + have a folder in front; a second fetch downloads nothing; +- a file whose SHA-256 is another than the pinned one stops the fetch, and is not left in the folder; +- a file missing from the archive stops the fetch; +- the archive read by HTTP ranges gives the same files, and far less than the archive is read; a server that serves + no ranges gets the archive read whole; +- the game folder gets the DLLs and the two license texts in dlssg, and loses them again; +- make_game.py puts them there only for a complete SDK folder, and takes them away for a build without it. + +python -I -B tests/test_streamline_sdk.py +""" +import hashlib +import importlib.util +import io +import os +import sys +import tempfile +import unittest +import zipfile +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] + + +def load(name, path): + spec = importlib.util.spec_from_file_location(name, path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +sl = load("streamline_sdk", ROOT / "scripts" / "streamline_sdk.py") + + +def content(name): + return (("the file %s\n" % name) * 400).encode() + + +def fake_tables(): + """The script's three tables with the SHA-256 of this test's own contents""" + def with_digests(table): + return [(name, hashlib.sha256(content(name)).hexdigest()) for name, _ in table] + return with_digests(sl.HEADERS), with_digests(sl.RUNTIME), with_digests(sl.LICENSES) + + +def archive_bytes(prefix="", leave_out=(), change=()): + data = io.BytesIO() + with zipfile.ZipFile(data, "w", zipfile.ZIP_DEFLATED) as z: + z.writestr(prefix + "docs/big.bin", os.urandom(600000)) # (what a fetch has no reason to read) + for name, _ in sl.HEADERS + sl.RUNTIME + sl.LICENSES: + if name in leave_out: + continue + z.writestr(prefix + name, content(name) + (b"changed" if name in change else b"")) + z.writestr(prefix + "bin/x64/other.dll", os.urandom(300000)) + return data.getvalue() + + +class FakeResponse: + def __init__(self, data, status): + self.data, self.status = data, status + + def read(self): + return self.data + + def __enter__(self): + return self + + def __exit__(self, *a): + return False + + +class StreamlineSdkTest(unittest.TestCase): + def setUp(self): + self.saved = (sl.HEADERS, sl.RUNTIME, sl.LICENSES) + sl.HEADERS, sl.RUNTIME, sl.LICENSES = fake_tables() + self.tmp = tempfile.TemporaryDirectory(prefix="streamline-sdk-") + self.dir = Path(self.tmp.name) + self.said = [] + + def tearDown(self): + sl.HEADERS, sl.RUNTIME, sl.LICENSES = self.saved + self.tmp.cleanup() + + def say(self, line): + self.said.append(line) + + def test_fetch_from_archive(self): + sdk = self.dir / "sdk" + sl.fetch(str(sdk), say=self.say, archive=zipfile.ZipFile(io.BytesIO(archive_bytes()))) + self.assertEqual(sl.missing(str(sdk)), []) + self.assertTrue(sl.complete(str(sdk))) + self.assertTrue((sdk / "include" / "sl_dlss_g.h").is_file()) + self.assertTrue((sdk / "bin" / "x64" / "nvngx_dlssg.dll").is_file()) + self.assertFalse((sdk / "bin" / "x64" / "other.dll").exists()) + self.said.clear() + sl.fetch(str(sdk), say=self.say, archive=None) # (nothing is missing: no archive is opened at all) + self.assertIn("already", self.said[0]) + + def test_folder_in_front(self): + sdk = self.dir / "sdk" + sl.fetch(str(sdk), say=self.say, archive=zipfile.ZipFile(io.BytesIO(archive_bytes("streamline-sdk-v2/")))) + self.assertTrue(sl.complete(str(sdk))) + + def test_only_what_is_missing(self): + sdk = self.dir / "sdk" + sl.fetch(str(sdk), say=self.say, archive=zipfile.ZipFile(io.BytesIO(archive_bytes()))) + victim = sdk / "bin" / "x64" / "sl.pcl.dll" + victim.write_bytes(b"damaged") + self.assertEqual(sl.missing(str(sdk)), ["bin/x64/sl.pcl.dll"]) + self.assertFalse(sl.complete(str(sdk))) + sl.fetch(str(sdk), say=self.say, archive=zipfile.ZipFile(io.BytesIO(archive_bytes()))) + self.assertEqual(victim.read_bytes(), content("bin/x64/sl.pcl.dll")) + + def test_another_file_refused(self): + sdk = self.dir / "sdk" + with self.assertRaises(ValueError): + sl.fetch(str(sdk), say=self.say, archive=zipfile.ZipFile(io.BytesIO(archive_bytes(change=("bin/x64/sl.dlss_g.dll",))))) + self.assertFalse((sdk / "bin" / "x64" / "sl.dlss_g.dll").exists()) + self.assertFalse(sl.complete(str(sdk))) + + def test_missing_from_archive(self): + with self.assertRaises(ValueError): + sl.fetch(str(self.dir / "sdk"), say=self.say, archive=zipfile.ZipFile(io.BytesIO(archive_bytes(leave_out=("license.txt",))))) + + def test_ranges(self): + data = archive_bytes() + asked = [] + + def opener(request, timeout=0): + first, last = (int(x) for x in request.headers["Range"].split("=")[1].split("-")) + asked.append(last - first + 1) + return FakeResponse(data[first:last + 1], 206) + + archive, reader = sl.open_archive("https://example.invalid/sdk.zip", len(data), opener, self.say) + sdk = self.dir / "sdk" + sl.fetch(str(sdk), say=self.say, archive=archive) + self.assertTrue(sl.complete(str(sdk))) + self.assertIsNotNone(reader) + self.assertEqual(reader.fetched, sum(asked)) + + def test_no_ranges(self): + data = archive_bytes() + + def opener(request, timeout=0): + return FakeResponse(data, 200) + + archive, reader = sl.open_archive("https://example.invalid/sdk.zip", len(data), opener, self.say) + self.assertIsNone(reader) + self.assertTrue(any("read whole" in line for line in self.said)) + sdk = self.dir / "sdk" + sl.fetch(str(sdk), say=self.say, archive=archive) + self.assertTrue(sl.complete(str(sdk))) + + def test_game_folder(self): + sdk, game = self.dir / "sdk", self.dir / "game" + sl.fetch(str(sdk), say=self.say, archive=zipfile.ZipFile(io.BytesIO(archive_bytes()))) + game.mkdir() + dst = Path(sl.install_runtime(str(sdk), str(game))) + self.assertEqual(dst, game / "dlssg") + names = sorted(p.name for p in dst.iterdir()) + self.assertEqual(names, sorted(["nvngx_dlssg.dll", "sl.common.dll", "sl.dlss_g.dll", "sl.interposer.dll", "sl.pcl.dll", + "sl.reflex.dll", "streamline-LICENSE.txt", "nvidia-rtx-sdks-LICENSE.txt"])) + (dst / "the players own note.txt").write_text("kept") + self.assertTrue(sl.remove_runtime(str(game))) + self.assertEqual([p.name for p in dst.iterdir()], ["the players own note.txt"]) # (only NVIDIA's files go) + self.assertFalse(sl.remove_runtime(str(self.dir / "no such game"))) + + def test_real_table(self): + headers, runtime, licenses = self.saved + self.assertEqual(len(runtime), 6) + self.assertIn("include/sl_dlss_g.h", dict(headers)) + for name, digest in headers + runtime + licenses: + self.assertRegex(digest, r"^[0-9a-f]{64}$", name) + self.assertEqual(set(sl.GAME_LICENSE_NAMES), {name for name, _ in licenses}) + + def test_make_game(self): + src = (ROOT / "nx2" / "make_game.py").read_text(encoding="utf-8") + for needle in ("fetch_streamline_sdk(a)", "place_dlssg_runtime(out)", 'os.environ["HOOKSHOT_STREAMLINE_SDK"] = sdk', + "sl.remove_runtime(out)"): + self.assertIn(needle, src) + build = (ROOT / "nx2" / "build.py").read_text(encoding="utf-8") + self.assertIn('os.environ.get("HOOKSHOT_STREAMLINE_SDK", "")', build) + self.assertIn('"-DHOOKSHOT_DLSSG"', build) + + +if __name__ == "__main__": + raise SystemExit(unittest.main(verbosity=1)) diff --git a/tests/test_tex_detail.cpp b/tests/test_tex_detail.cpp new file mode 100644 index 0000000..c29a3a4 --- /dev/null +++ b/tests/test_tex_detail.cpp @@ -0,0 +1,62 @@ +// tex_detail (gpu/vk/tex_detail.h): the detail level a card gets and the levels a texture loses at it. +// tests/test_tex_detail.py +#include "../gpu/vk/tex_detail.h" + +#include +#include + +static int failures = 0; +static void expect(bool ok, const char* what) { + if (!ok) { + std::printf("FAIL: %s\n", what); + failures++; + } +} + +int main() { + const uint64_t MB = 1ull << 20; + using namespace tex_detail; + // ---- the allowances in players' logs + expect(by_vram(11311 * MB, false) == kFull, "a 12 GB card (11,311 MB allowed): full"); + expect(by_vram(11247 * MB, false) == kFull, "the same card with something else open"); + expect(by_vram(15569 * MB, false) == kFull, "a 16 GB card"); + expect(by_vram(31419 * MB, false) == kFull, "a 32 GB card"); + expect(by_vram(9300 * MB, false) == kReduced, "a 10 GB card"); + expect(by_vram(7365 * MB, false) == kReduced, "an 8 GB RX 7600"); + expect(by_vram(7133 * MB, false) == kReduced, "an 8 GB RX 7700S"); + expect(by_vram(4730 * MB, false) == kLow, "a 6 GB RTX 3060 Laptop"); + expect(by_vram(3379 * MB, false) == kLow, "a 4 GB laptop card"); + // ---- the two lines + expect(by_vram(10240 * MB, false) == kFull, "full starts at 10,240 MB"); + expect(by_vram(10239 * MB, false) == kReduced, "one MB under it"); + expect(by_vram(6144 * MB, false) == kReduced, "reduced starts at 6,144 MB"); + expect(by_vram(6143 * MB, false) == kLow, "one MB under it"); + // ---- no allowance reported: whole, as before + expect(by_vram(0, false) == kFull, "nothing reported"); + expect(by_vram(0, true) == kFull, "nothing reported, integrated"); + // ---- an integrated GPU never gets full, and follows the allowance below that + expect(by_vram(10361 * MB, true) == kReduced, "a handheld that reported 10,361 MB"); + expect(by_vram(16000 * MB, true) == kReduced, "integrated with 16 GB allowed"); + expect(by_vram(8034 * MB, true) == kReduced, "an Iris Xe (8,034 MB)"); + expect(by_vram(5000 * MB, true) == kLow, "integrated with 5 GB allowed"); + + // ---- the switch + expect(parse(nullptr) == -1 && parse("auto") == -1 && parse("") == -1 && parse("1") == -1, "auto, unset, anything else"); + expect(parse("full") == kFull && parse("reduced") == kReduced && parse("low") == kLow, "the three names"); + + // ---- what a texture loses + expect(skip(kFull, 2048, 2048, 12) == 0, "full: nothing"); + expect(skip(kReduced, 2048, 2048, 12) == 1, "reduced: the largest level"); + expect(skip(kLow, 2048, 2048, 12) == 2, "low: the two largest of a 2048"); + expect(skip(kLow, 1024, 1024, 11) == 2, "low: two of a 1024"); + expect(skip(kLow, 1023, 1024, 10) == 1, "low: one where a side is under 1024"); + expect(skip(kLow, 512, 512, 10) == 1, "low: one of a 512"); + expect(skip(kLow, 1024, 1024, 3) == 1, "low: one where two would leave a single level"); + expect(skip(kReduced, 256, 256, 9) == 1, "reduced: a 256 still loses one"); + expect(skip(kReduced, 255, 512, 9) == 0 && skip(kLow, 512, 128, 9) == 0, "a side under 256: whole"); + expect(skip(kReduced, 1024, 1024, 1) == 0 && skip(kLow, 2048, 2048, 1) == 0, "no mip levels: whole"); + expect(skip(kReduced, 1365, 1365, 11) == 1, "a terrain material array's 1365"); + if (failures) return EXIT_FAILURE; + std::printf("tex detail: all checks passed\n"); + return EXIT_SUCCESS; +} diff --git a/tests/test_tex_detail.py b/tests/test_tex_detail.py new file mode 100644 index 0000000..844ce63 --- /dev/null +++ b/tests/test_tex_detail.py @@ -0,0 +1,35 @@ +"""Compile and run the texture detail rule (gpu/vk/tex_detail.h; no game/GPU).""" +import argparse +import subprocess +import tempfile +from pathlib import Path + +RUN_ALL_ARGS = ["--cxx", "g++"] + +ROOT = Path(__file__).resolve().parents[1] + + +def check_source(): + """The texture setup still asks the rule, reads the switch and keeps the textures it must leave whole""" + src = (ROOT / "gpu/vk/vk_texture.cpp").read_text(encoding="utf-8") + for needle in ('#include "tex_detail.h"', "tex_detail::by_vram(", 'getenv("RX_VK_TEX_DETAIL")', + "tex_detail::skip(detail_level(), x.width, x.height, x.levels)", + "!as_target && astc && !x.sparse"): + if needle not in src: + raise SystemExit("vk_texture.cpp no longer has: " + needle) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--cxx", default="g++") + args = parser.parse_args() + check_source() + with tempfile.TemporaryDirectory(prefix="tex-detail-") as tmp: + exe = Path(tmp) / "tex-detail" + subprocess.run([args.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror", + str(ROOT / "tests/test_tex_detail.cpp"), "-o", str(exe)], check=True) + subprocess.run([str(exe)], check=True) + + +if __name__ == "__main__": + main() diff --git a/tests/test_texture_gone.cpp b/tests/test_texture_gone.cpp new file mode 100644 index 0000000..e2aa7ce --- /dev/null +++ b/tests/test_texture_gone.cpp @@ -0,0 +1,58 @@ +// gpu/vk/texture_gone.h: which textures were destroyed, by their creation number. Run by tests/test_texture_gone.py. +#include +#include + +#include "../gpu/vk/texture_gone.h" + +static void require(bool ok, const char* what) { + if (!ok) { + fprintf(stderr, "FAIL: %s\n", what); + exit(1); + } +} + +int main() { + using texture_gone::Record; + { + Record r; + require(r.count() == 0, "nothing went yet"); + // (the upscale's output, texture 7, noted while it is there) + const uint64_t noted = r.count(); + require(!r.gone_since(7, noted), "a texture noted is there while nothing went"); + // (the cache lets other textures go, as in most frames while the video memory is full: the generation changed for + // each, and each cost the frame its picture at the output's size) + for (uint32_t id = 100; id < 140; id++) r.went(id); + require(r.count() == 40, "the count is the textures that went"); + require(!r.gone_since(7, noted), "other textures' ends do not make a noted texture gone"); + require(r.gone_since(120, noted), "a texture that went since is gone"); + // (noted again later: what went before does not count, a pointer's address reused by a new texture's number) + const uint64_t later = r.count(); + require(!r.gone_since(120, later), "what went before the note is not asked about"); + r.went(7); + require(r.gone_since(7, noted) && r.gone_since(7, later), "the noted texture's own end is seen"); + require(!r.gone_since(8, later), "and it is no other texture's"); + } + { + // (no number: a texture that was never numbered cannot be told from one that went) + Record r; + require(r.gone_since(0, r.count()), "a texture without a number counts as gone"); + // (a count this record never gave) + require(r.gone_since(7, 5), "a count from ahead of the record counts as gone"); + } + { + // (more went since than are kept, an area's change: it cannot be told, and it counts as gone, the old rule's answer) + Record r; + const uint64_t noted = r.count(); + for (uint32_t i = 0; i < Record::kKept; i++) r.went(1000 + i); + require(!r.gone_since(7, noted), "as many as are kept: still told"); + require(r.gone_since(1000, noted), "the oldest kept is still seen"); + r.went(5000); + require(r.gone_since(7, noted), "one more than are kept: not told, gone"); + // (and a note taken since is told again) + const uint64_t later = r.count(); + r.went(5001); + require(!r.gone_since(7, later) && r.gone_since(5001, later), "a later note is told from what is kept"); + } + printf("PASS: a kept texture is gone only when it was destroyed itself, or when it cannot be told\n"); + return 0; +} diff --git a/tests/test_texture_gone.py b/tests/test_texture_gone.py new file mode 100644 index 0000000..bdbb4e0 --- /dev/null +++ b/tests/test_texture_gone.py @@ -0,0 +1,99 @@ +"""A texture the temporal upscaler keeps from one draw to a later one is given up only when that texture itself was destroyed. + +With the engine (NVIDIA DLSS, AMD FSR 4) before the game's bloom and scaling up, the tone mapping is drawn once more at the +output's size from the engine's picture, into the upscale's output as the last frame's dispatch had it. That output, and the +scene's colour and depth kept from the scene's draws, were checked with texture_destroy_generation, which changes whenever +any texture is destroyed. On a card whose video memory is full the cache lets textures go in most frames (a tester's 8 GB +card at 2560x1440: 154 times in some 2300 frames), and each of those frames was shown from the scene's size scaled up in +place of the engine's picture: a soft frame, reported as a blur that comes and goes. + +What must hold: gpu/vk/texture_gone.h tells one texture's end from another's (compiled and run here, no game or GPU), +vk_texture.cpp records every texture that goes there, and vk_temporal.cpp and vk_fsr3_inputs.cpp (AMD FSR 3.1's scene depth) +ask it, not the generation, for the textures they keep (the generation stays for the views' caches, which any texture's end +may make stale). + +python -I -B tests/test_texture_gone.py [--cxx g++] +""" +import argparse +import subprocess +import tempfile +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] + + +def block(text, marker): + start = text.index(marker) + end = text.index("{", start) + 1 + depth = 1 + while depth: + depth += (text[end] == "{") - (text[end] == "}") + end += 1 + return text[start:end] + + +def need(where, text, needles): + for needle in needles: + if needle not in text: + raise SystemExit(where + " no longer has: " + needle) + + +def check_source(): + tex = (ROOT / "gpu/vk/vk_texture.cpp").read_text(encoding="utf-8") + temporal = (ROOT / "gpu/vk/vk_temporal.cpp").read_text(encoding="utf-8") + header = (ROOT / "gpu/vk/vk_texture.h").read_text(encoding="utf-8") + # (every texture's end is recorded, with the generation: destroy_later is where a texture goes) + need("vk_texture.cpp: destroy_later()", block(tex, "void destroy_later(Texture& tex)"), ["destroy_gen++;", "gone_record.went(tex.id);"]) + need("vk_texture.cpp", tex, ['#include "texture_gone.h"', "texture_gone::Record gone_record;"]) + need("vk_texture.cpp: texture_destroyed_count()", block(tex, "uint64_t texture_destroyed_count()"), ["return gone_record.count();"]) + need("vk_texture.cpp: texture_destroyed_since()", block(tex, "bool texture_destroyed_since(uint32_t id, uint64_t since)"), + ["return gone_record.gone_since(id, since);"]) + need("vk_texture.h", header, ["uint64_t texture_destroyed_count();", "bool texture_destroyed_since(uint32_t id, uint64_t since);"]) + # (every texture has a number: one without would always count as gone, and the engine would never draw before the bloom) + need("vk_texture.cpp: create()", block(tex, "Texture* create(const Key& key"), ["p->id = ++ids;"]) + if tex.count("std::make_unique()") != 1: + raise SystemExit("vk_texture.cpp: textures are made in another place than create(), where they get their number") + + # (the upscaler: the textures it keeps are asked about themselves) + need("vk_temporal.cpp: there_still()", temporal, + ["bool there_still(uint32_t id, uint64_t noted) { return !texture_destroyed_since(id, noted); }", + "bool there_still(const Camera& cam) { return there_still(cam.color_id, cam.noted) && there_still(cam.depth_id, cam.noted); }"]) + need("vk_temporal.cpp: temporal_note_scene_draw()", block(temporal, "void temporal_note_scene_draw(const Texture* color0"), + ["k.color_id = color0->id, k.depth_id = depth->id, k.noted = texture_destroyed_count();"]) + need("vk_temporal.cpp: pre_dispatch()", block(temporal, "bool pre_dispatch(VkCommandBuffer cmd, int mode"), + ["s.pre_out = out, s.pre_out_id = out->id, s.pre_out_noted = texture_destroyed_count();"]) + need("vk_temporal.cpp: temporal_pre_composite()", block(temporal, "bool temporal_pre_composite(const Texture* color0"), + ["!there_still(s.pre_out_id, s.pre_out_noted)"]) + need("vk_temporal.cpp: temporal_pre_tonemap()", block(temporal, "Texture* temporal_pre_tonemap(VkCommandBuffer cmd)"), ["!there_still(cam)"]) + # (and no texture it keeps is given up for another texture's end: the generation is left to the views' cache alone) + uses = [line.strip() for line in temporal.splitlines() if "texture_destroy_generation()" in line and not line.strip().startswith("//")] + if uses != ["const uint64_t gen = texture_destroy_generation();"]: + raise SystemExit("vk_temporal.cpp asks texture_destroy_generation for more than its views' cache: " + "; ".join(uses)) + need("vk_temporal.cpp: depth_view()", block(temporal, "VkImageView depth_view(Texture* t)"), + ["const uint64_t gen = texture_destroy_generation();", "if (gen != s.depth_views_gen)"]) + + # (AMD FSR 3.1's own inputs keep the scene's depth the same way: a frame is not left to the game's upscale for another + # texture's end) + fsr3 = (ROOT / "gpu/vk/vk_fsr3_inputs.cpp").read_text(encoding="utf-8") + need("vk_fsr3_inputs.cpp", fsr3, + ["k.depth = depth, k.depth_id = depth->id, k.noted = texture_destroyed_count(), k.frame = now;", + 'if (texture_destroyed_since(cam.depth_id, cam.noted)) return fail("the scene depth is gone");']) + uses = [line.strip() for line in fsr3.splitlines() if "texture_destroy_generation()" in line and not line.strip().startswith("//")] + if uses != ["const uint64_t gen = texture_destroy_generation();"]: + raise SystemExit("vk_fsr3_inputs.cpp asks texture_destroy_generation for more than its views' cache: " + "; ".join(uses)) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--cxx", default="g++") + args = parser.parse_args() + check_source() + with tempfile.TemporaryDirectory(prefix="texture-gone-") as tmp: + exe = Path(tmp) / "texture-gone" + subprocess.run([args.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror", str(ROOT / "tests/test_texture_gone.cpp"), "-o", str(exe)], + check=True) + subprocess.run([str(exe)], check=True) + + +if __name__ == "__main__": + main() diff --git a/tests/test_wide_hud.py b/tests/test_wide_hud.py new file mode 100644 index 0000000..4568735 --- /dev/null +++ b/tests/test_wide_hud.py @@ -0,0 +1,213 @@ +"""The HUD on a picture wider than 16:9 (host/oot_wide_hud.h, host/oot_wide_hud.c, nx2/prompt_assets.py HUD_SIDE_PANES). + +With the Insert menu's Ultrawide HUD row on Screen edges the hearts and the item slot sit in the picture's top corners +and the small map at its right edge; on Middle everything keeps the middle 16:9, as before. The rules checked here, +without the game: + +- A layout in the middle: its projection times the squeeze, nothing moved. A kept one (a fade, a menu's background): + untouched. A 16:9 picture (squeeze 1) or no squeeze: untouched. +- A layout at an edge (the lab's switch): a point at the 16:9 grid's right edge lands on the picture's right edge, one + at its left edge on the picture's left edge, and the layout keeps its shape. +- A pane moved in a layout (the hearts', the item slot's, the small map's) goes as far as a whole layout moved by its + projection: a corner pane lands on the picture's corner through the middle projection. +- prompts/wide.bin: one entry per pane and copy of each layout, and one per animation key that puts a pane at the small + map's place (the keys at 0 and other curves are left); a layout that is not the one known gives none. + +python -I -B tests/test_wide_hud.py +""" +import shutil +import struct +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "nx2")) +import prompt_assets # noqa: E402 + +FIXTURE = r''' +#include +#include +#include "oot_wide_hud.h" + +static int fails; +#define CHECK(cond, what) do { if (!(cond)) { fails++; printf("FAIL line %d: %s\n", __LINE__, what); } } while (0) +static int near(float a, float b) { return fabsf(a - b) < 1e-4f; } + +/* clip x of the layout's point x through the projection (first column's m00, the move m03) */ +static float clip(const float first[4], float move, float x) { return first[0] * x + move; } + +int main(void) { + const float m00 = 2.0f / 3840.0f; + const float q = (16.0f / 9.0f) / (21.0f / 9.0f); /* a 21:9 picture */ + float first[4], move; + + /* the middle */ + first[0] = m00, first[1] = first[2] = first[3] = 0, move = 0; + oot_hud_row(first, &move, q, 0, OOT_HUD_MIDDLE); + CHECK(near(clip(first, move, 1920.0f), q) && near(clip(first, move, -1920.0f), -q) && move == 0.0f, "the middle 16:9"); + + /* kept, and a 16:9 picture */ + first[0] = m00, move = 0; + oot_hud_row(first, &move, q, 1, OOT_HUD_RIGHT); + CHECK(first[0] == m00 && move == 0.0f, "a kept layout is untouched"); + oot_hud_row(first, &move, 1.0f, 0, OOT_HUD_RIGHT); + CHECK(first[0] == m00 && move == 0.0f, "a 16:9 picture is untouched"); + oot_hud_row(first, &move, 0.0f, 0, OOT_HUD_LEFT); + CHECK(first[0] == m00 && move == 0.0f, "no squeeze: untouched"); + + /* the edges */ + first[0] = m00, move = 0; + oot_hud_row(first, &move, q, 0, OOT_HUD_RIGHT); + CHECK(near(clip(first, move, 1920.0f), 1.0f), "the grid's right edge on the picture's right edge"); + CHECK(near(clip(first, move, 1920.0f) - clip(first, move, -1920.0f), 2.0f * q), "its shape kept"); + first[0] = m00, move = 0; + oot_hud_row(first, &move, q, 0, OOT_HUD_LEFT); + CHECK(near(clip(first, move, -1920.0f), -1.0f), "the grid's left edge on the picture's left edge"); + + /* a pane moved in the layout: as far as the projection would move it */ + const float out = oot_hud_out(q); + first[0] = m00, move = 0; + oot_hud_row(first, &move, q, 0, OOT_HUD_MIDDLE); + CHECK(near(clip(first, move, 1920.0f + out), 1.0f), "the right corner pane on the picture's corner"); + CHECK(near(clip(first, move, -1920.0f - out), -1.0f), "the left corner pane on the picture's corner"); + CHECK(near(clip(first, move, 1626.0f + out) - 1.0f, clip(first, move, 1626.0f) - q), "a pane beside it keeps its distance to the edge"); + CHECK(oot_hud_out(1.0f) == 0.0f && oot_hud_out(0.0f) == 0.0f && oot_hud_out(1.5f) == 0.0f, "nothing on a 16:9 or narrower picture"); + CHECK(near(oot_hud_out((16.0f / 9.0f) / (32.0f / 9.0f)), 1920.0f), "32:9: a whole half grid further out"); + + /* the classes */ + CHECK(oot_hud_keep_bit(0x84c04fb0ULL) == 2u && oot_hud_keep_bit(0x84c08300ULL) == 0u, "the menus' background is kept, the map's screen is not"); + + printf(fails ? "wide hud: FAILED\n" : "wide hud: all checks passed\n"); + return fails != 0; +} +''' + + +def layout(name, panes, curves=()): + """a made-up layout as nx2/prompt_assets.py reads one: "BFFH", the names' and the records' tables' offsets at +0x118 and + +0x120, the count at +0x130, the name before "RootPane" among the texts; panes: [(name, x)]; curves: [(value past the + last key, [(value, slope)])], each an array as layout_keys reads one, 8-aligned, the first value 8 bytes before it""" + names = ["RootPane"] + [n for n, _ in panes] + xs = [0.0] + [x for _, x in panes] + n = len(names) + head = 0x140 + names_at, recs_at = head, head + 8 * n + rec0 = recs_at + 8 * n + texts_at = rec0 + 0x44 * n + texts = b"\0" + name.encode() + b"\0" + offs = [] + for t in names: + offs.append(texts_at + len(texts)) + texts += t.encode() + b"\0" + b = bytearray(texts_at) + texts + b[0:4] = b"BFFH" + struct.pack_into(" 0}, {1920.0, 1626.0, 1270.0, 1536.0, 1470.0, 1490.0, 1706.0}) + first = blarc.index(layout("PlayerStatus_00", GOOD)) + self.assertTrue(all(off >= first for off, _, _ in got)) # (the other layout's pane of the same name: left) + self.assertNotIn(1666.0, {x for _, _, x in got}) # (the whole-screen map's own pane: left) + self.assertEqual(len({off for off, _, _ in got}), len(got)) + with tempfile.TemporaryDirectory(prefix="widehud-") as d: + path = Path(d) / "wide.bin" + prompt_assets.write_wide(path, got) + raw = path.read_bytes() + self.assertEqual(raw[:4], b"HPWD") + self.assertEqual(struct.unpack_from("x[0] - 0x1b8, _row, &_tx);", wide) + menu = (ROOT / "gpu/vk/vk_menu.cpp").read_text(encoding="utf-8") + self.assertIn('"Ultrawide HUD", &opts.wide_hud, 0, 1, 1, tx_wide_hud,', menu) + + +if __name__ == "__main__": + unittest.main() diff --git a/third_party/a64-kit/lift/a64_lift.py b/third_party/a64-kit/lift/a64_lift.py index 3b44835..eab8189 100644 --- a/third_party/a64-kit/lift/a64_lift.py +++ b/third_party/a64-kit/lift/a64_lift.py @@ -490,6 +490,23 @@ def local_regs(): return int(v) if v.isdigit() else 0 +_local_sets = None + + +def local_here(name): + """(triforce) A64_LOCAL_REGS for this function? A64_LOCAL_HOT=1: only the hot functions (A64_HOT_FUNCS). + A64_NO_LOCAL=: the functions listed there (one name a line) keep their registers in the context: the ones other + tools edit by their exact text after the lift (the game's patches).""" + global _local_sets + if _local_sets is None: + hot = hot_functions() if os.environ.get("A64_LOCAL_HOT") else None + path = os.environ.get("A64_NO_LOCAL") + no = {w for line in open(path) for w in line.split()[:1]} if path else set() + _local_sets = (hot, no) + hot, no = _local_sets + return (hot is None or name in hot) and name not in no + + def localize_regs(text, local, dirty): """Expand the context marks (see SYNC_CALL): with `local`, the registers become locals first, and each mark writes back what may have changed since the last one (dirty: per instruction index, the masks before and after it) or @@ -727,11 +744,15 @@ static inline __m128i a64_v_ovf(const Ctx *c, __m128i a, __m128i b, __m128i t) { const __m128i inf = _mm_cmpeq_epi32(a64_v_abs(t), _mm_set1_epi32(0x7f800000)); return _mm_andnot_si128(_mm_or_si128(a64_v_nonfin(a), a64_v_nonfin(b)), inf); } +/* (triforce) Past the fast path the lanes' helper is called without the context and on copies of the registers (the + control word in, the status word in and out): handed the context or pointers into it, the call may change any of it, + and the compiler kept every guest register in memory around each vector op. */ #define A64_VFPX_SUM(name, op, scalar) \ -static __attribute__((noinline, cold)) void a64_vfpx_##name##_lanes(Ctx *c, uint64_t *d, const uint64_t *n, const uint64_t *m, int k) { \ +static __attribute__((noinline, cold)) void a64_vfpx_##name##_lanes(uint64_t fpcr, uint64_t *fpsr, uint64_t *d, const uint64_t *n, const uint64_t *m, int k) { \ + Ctx t; t.fpcr = fpcr; t.fpsr = *fpsr; \ uint32_t a[4], b[4], r[4] = {0}; memcpy(a, n, 16); memcpy(b, m, 16); \ - for (int j = 0; j < k; j++) r[j] = (uint32_t)scalar(c, a[j], b[j]); \ - memcpy(d, r, 16); } \ + for (int j = 0; j < k; j++) r[j] = (uint32_t)scalar(&t, a[j], b[j]); \ + memcpy(d, r, 16); *fpsr = t.fpsr; } \ static inline __attribute__((always_inline)) void a64_vfpx_##name(Ctx *c, uint64_t *d, const uint64_t *n, const uint64_t *m, int k) { \ if (RECOMP_LIKELY(RECOMP_FPX_OPEN(c))) { \ const __m128i a = _mm_loadu_si128((const __m128i *)n), b = _mm_loadu_si128((const __m128i *)m); \ @@ -751,7 +772,8 @@ static inline __attribute__((always_inline)) void a64_vfpx_##name(Ctx *c, uint64 !(_mm_movemask_epi8(_mm_andnot_si128(a64_v_ovf(c, a, b, t), bad)) & lanes)) { \ if (k == 4) _mm_storeu_si128((__m128i *)d, r); else a64_vst_lo64(d, r); \ return; } } \ - a64_vfpx_##name##_lanes(c, d, n, m, k); } + { uint64_t nn_[2], mm_[2], dd_[2], s_ = c->fpsr; memcpy(nn_, n, 16); memcpy(mm_, m, 16); \ + a64_vfpx_##name##_lanes(c->fpcr, &s_, dd_, nn_, mm_, k); c->fpsr = s_; memcpy(d, dd_, 16); } } #else #define A64_VFPX_SUM(name, op, scalar) \ static __attribute__((noinline)) void a64_vfpx_##name(Ctx *c, uint64_t *d, const uint64_t *n, const uint64_t *m, int k) { \ @@ -761,10 +783,12 @@ static __attribute__((noinline)) void a64_vfpx_##name(Ctx *c, uint64_t *d, const #endif """ VFPX_MUL_HEADER = r""" -static __attribute__((noinline, cold)) void a64_vfpx_mul32_lanes(Ctx *c, uint64_t *d, const uint64_t *n, const uint64_t *m) { +/* (triforce: without the context, as A64_VFPX_SUM's) */ +static __attribute__((noinline, cold)) void a64_vfpx_mul32_lanes(uint64_t fpcr, uint64_t *fpsr, uint64_t *d, const uint64_t *n, const uint64_t *m) { + Ctx t; t.fpcr = fpcr; t.fpsr = *fpsr; uint32_t a[4], b[4], r[4]; memcpy(a, n, 16); memcpy(b, m, 16); - for (int j = 0; j < 4; j++) r[j] = (uint32_t)a64_fpx_mul32_fma32(c, a[j], b[j], (a[j] ^ b[j]) & 0x80000000u); - memcpy(d, r, 16); } + for (int j = 0; j < 4; j++) r[j] = (uint32_t)a64_fpx_mul32_fma32(&t, a[j], b[j], (a[j] ^ b[j]) & 0x80000000u); + memcpy(d, r, 16); *fpsr = t.fpsr; } static inline __attribute__((always_inline)) void a64_vfpx_mul32(Ctx *c, uint64_t *d, const uint64_t *n, const uint64_t *m, int k) { (void)k; #if RECOMP_FPX_HOST == 2 @@ -792,7 +816,8 @@ static inline __attribute__((always_inline)) void a64_vfpx_mul32(Ctx *c, uint64_ } } #endif - a64_vfpx_mul32_lanes(c, d, n, m); + { uint64_t nn_[2], mm_[2], dd_[2], s_ = c->fpsr; memcpy(nn_, n, 16); memcpy(mm_, m, 16); + a64_vfpx_mul32_lanes(c->fpcr, &s_, dd_, nn_, mm_); c->fpsr = s_; memcpy(d, dd_, 16); } } """ @@ -936,10 +961,28 @@ def fpx_header(used): ", ".join("a%d" % i for i in range(nf)), rescue, exact, ", ".join("a%d" % i for i in range(ne)))) # (the hot functions' copy, see HOT_FPX: the fast path inline, everything else through the helper) + # (triforce) Past the fast path the helper is called without the context (a64_fpx_nc_*: the control word in, the + # status word in and out; the FP bodies read and write nothing else of it). A call handed the context may change + # any of it, so with one behind every float op the compiler stored each guest register as it was written and read + # the control and status words again at every op. And the result goes on as a float (or double), the bits made + # from it: the exact call's bits met the fast path's in an integer, and every float value went through a general + # register and the context's memory on its way to the next op. The tests and the results are the same. + # -DA64_FPX_HOT_CTX: the copy as it was. + args_f, args_e = ", ".join("a%d" % i for i in range(nf)), ", ".join("a%d" % i for i in range(ne)) + ftype, to_f, to_b = ("float", "recomp_fpx_f", "recomp_fpx_fb") if fast.endswith("32") else ("double", "recomp_fpx_d", "recomp_fpx_db") + out.append("#ifndef A64_FPX_HOT_CTX") + out.append("static __attribute__((noinline, cold)) uint64_t a64_fpx_nc_%s_%s(uint64_t fpcr, uint64_t *fpsr, %s) { " + "Ctx t; t.fpcr = fpcr; t.fpsr = *fpsr; const uint64_t v = a64_fpx_%s_%s(&t, %s); *fpsr = t.fpsr; return v; }" + % (fast, exact, params, fast, exact, args_e)) + out.append("static inline __attribute__((always_inline)) uint64_t a64_fpx_hot_%s_%s(Ctx *c, %s) { uint64_t v; %s r; " + "if (RECOMP_FPX_OPEN(c) && RECOMP_LIKELY(recomp_fpx_%s(%s, RECOMP_FPX_FZ(c), &v))) r = %s(v); " + "else { uint64_t s = c->fpsr; v = a64_fpx_nc_%s_%s(c->fpcr, &s, %s); c->fpsr = s; r = %s(v); } " + "return %s(r); }" % (fast, exact, params, ftype, fast, args_f, to_f, fast, exact, args_e, to_f, to_b)) + out.append("#else") out.append("static inline __attribute__((always_inline)) uint64_t a64_fpx_hot_%s_%s(Ctx *c, %s) { uint64_t v; " "if (RECOMP_FPX_OPEN(c) && RECOMP_LIKELY(recomp_fpx_%s(%s, RECOMP_FPX_FZ(c), &v))) return v; " - "return a64_fpx_%s_%s(c, %s); }" % (fast, exact, params, fast, ", ".join("a%d" % i for i in range(nf)), - fast, exact, ", ".join("a%d" % i for i in range(ne)))) + "return a64_fpx_%s_%s(c, %s); }" % (fast, exact, params, fast, args_f, fast, exact, args_e)) + out.append("#endif") out.append(VSET_HEADER) out.append(VFCMP_HEADER) if any(e[:2] in (("add32", "add32"), ("sub32", "sub32"), ("mul32", "fma32")) for e in used): @@ -950,13 +993,76 @@ def fpx_header(used): out.append("A64_VFPX_SUM(sub32, _mm_sub_ps, a64_fpx_sub32_sub32)") if ("mul32", "fma32", 2, 3) in used: out.append(VFPX_MUL_HEADER) + if ("fma32", "fma32", 3, 3) in used: + out.append(VFPX_FMA_HEADER) return "\n".join(out) + "\n" +# (triforce) FMUL / FMLA / FMLS .4S by element (a vector times one lane of another: matrix code) were a loop over the four +# lanes, each lane a scalar helper with its gate and tests: 5,700 of the 9,000 lane loops in OoT's hot functions, 850 more +# the multiply-adds. The multiply becomes a64_vfpx_mul32 on the lane spread to four (the straight .4S multiply's helper), the +# multiply-add a64_vfpx_fma32 (one FMA op where the host has it). A lane that fails the scalar fast path's tests sends the +# whole op through the scalar helpers, lane by lane, as before. (Matched on the text as widen_vstores leaves it.) +# A64_NO_VFPX_ELEM=1 at lift time: off. +VFPX_ELEM = re.compile( + r"\{ uint32_t _a\[4\],_r\[4\],_m; memcpy\(_a,c->vreg\[(\d+)\],16\); memcpy\(&_m,\(const uint8_t\*\)c->vreg\[(\d+)\]\+(\d+),4\); " + r"memcpy\(_r,c->vreg\[(\d+)\],16\);for\(int _i=0;_i<4;\+\+_i\)\{uint64_t _n=_a\[_i\],_z0=_r\[_i\];\{uint64_t _a=_n,_b=_m,_z=_z0,_v;" + r"(_z=\(_a\^_b\)&0x80000000ULL;_v=a64_fpx_mul32_fma32|_v=a64_fpx_fma32_fma32|_a\^=0x80000000ULL;_v=a64_fpx_fma32_fma32)" + r"\(c,_a,_b,_z\);_r\[_i\]=\(uint32_t\)_v;\}\}a64_vset64\(c->vreg\[(\d+)\],0\); a64_vst16\(c->vreg\[(\d+)\],_r\); \}") + + +def vectorize_elem(text): + if "_z0=_r[_i]" not in text or os.environ.get("A64_NO_VFPX") or os.environ.get("A64_NO_VFPX_ELEM"): + return text + + def sub(m): + a, e, off, acc, op, d1, d2 = m.groups() + if d1 != d2 or (not op.startswith("_z=") and acc != d1): + return m.group(0) + head = ("{ uint32_t _m; uint64_t _e[2]; memcpy(&_m,(const uint8_t*)c->vreg[%s]+%s,4); _e[0]=_e[1]=(uint64_t)_m<<32|_m; " + % (e, off)) + if op.startswith("_z="): + return head + "a64_vfpx_mul32(c,c->vreg[%s],c->vreg[%s],_e,4); }" % (d1, a) + return head + "a64_vfpx_fma32(c,c->vreg[%s],c->vreg[%s],_e,%d); }" % (d1, a, op.startswith("_a^=")) + return VFPX_ELEM.sub(sub, text) + + +VFPX_FMA_HEADER = r""" +/* (triforce) FMLA / FMLS on four single-precision lanes (vectorize_elem in a64_lift.py): d = fma(+-n, m, d). One FMA op + where the host has it, kept when every lane passes the scalar fast path's tests (recomp_fpx_fma32); otherwise each lane + goes through its scalar helper, called without the context as a64_vfpx_mul32's lanes are. */ +#if RECOMP_FPX_HOST == 2 && defined(__FMA__) +#include +#endif +static __attribute__((noinline, cold)) void a64_vfpx_fma32_lanes(uint64_t fpcr, uint64_t *fpsr, uint64_t *d, const uint64_t *n, const uint64_t *m, const uint64_t *z) { + Ctx t; t.fpcr = fpcr; t.fpsr = *fpsr; + uint32_t a[4], b[4], w[4], r[4]; memcpy(a, n, 16); memcpy(b, m, 16); memcpy(w, z, 16); + for (int j = 0; j < 4; j++) r[j] = (uint32_t)a64_fpx_fma32_fma32(&t, a[j], b[j], w[j]); + memcpy(d, r, 16); *fpsr = t.fpsr; } +static inline __attribute__((always_inline)) void a64_vfpx_fma32(Ctx *c, uint64_t *d, const uint64_t *n, const uint64_t *m, int neg) { + uint64_t nn_[2], mm_[2], zz_[2], dd_[2]; + memcpy(nn_, n, 16); memcpy(mm_, m, 16); memcpy(zz_, d, 16); + if (neg) { nn_[0] ^= 0x8000000080000000ULL; nn_[1] ^= 0x8000000080000000ULL; } +#if RECOMP_FPX_HOST == 2 && defined(__FMA__) + if (RECOMP_LIKELY(RECOMP_FPX_OPEN(c))) { + const __m128i a = _mm_loadu_si128((const __m128i *)nn_), b = _mm_loadu_si128((const __m128i *)mm_), z = _mm_loadu_si128((const __m128i *)zz_); + const __m128i t = _mm_castps_si128(_mm_fmadd_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b), _mm_castsi128_ps(z))); + /* kept: big, or a finite result of a zero factor (RECOMP_FPX_KEEP32); under FZ no subnormal operand */ + const __m128i keep = _mm_or_si128(a64_v_big(t), _mm_andnot_si128(a64_v_nonfin(t), _mm_or_si128(a64_v_zero(a), a64_v_zero(b)))); + __m128i bad = _mm_andnot_si128(keep, _mm_set1_epi32(-1)); + if (RECOMP_FPX_FZ(c)) bad = _mm_or_si128(bad, _mm_or_si128(_mm_or_si128(a64_v_subn(a), a64_v_subn(b)), a64_v_subn(z))); + if (RECOMP_LIKELY(!_mm_movemask_epi8(bad))) { _mm_storeu_si128((__m128i *)d, t); return; } + } +#endif + { uint64_t s_ = c->fpsr; a64_vfpx_fma32_lanes(c->fpcr, &s_, dd_, nn_, mm_, zz_); c->fpsr = s_; memcpy(d, dd_, 16); } +} +""" + + def op_text(text, fpx): """one instruction's emitted C as lifted code has it: FPX guards outlined (their shapes added to fpx, which fpx_header turns into a64_fpx.h), NEON FP vectorized, stores widened, flags packed""" - return stack_stores(pack_flags(widen_vstores(vectorize_fpx(outline_fpx(text, fpx))))) + return stack_stores(pack_flags(vectorize_elem(widen_vstores(vectorize_fpx(outline_fpx(text, fpx)))))) class Lifter: @@ -1625,7 +1731,7 @@ class Lifter: pcs = sorted(body) dead = {} if os.environ.get("A64_KEEP_FLAGS") else self.dead_flags(body, pcs, texts) frame = not os.environ.get("A64_MARK_FRAME") - local = local_regs() and e not in self.no_local + local = local_regs() and e not in self.no_local and local_here(self.fname(e)) dirty = self.dirty_regs(body, pcs, texts) if local else None for i, pc in enumerate(pcs): self.cur_i = i diff --git a/third_party/a64-kit/lift/a64_rtgen.py b/third_party/a64-kit/lift/a64_rtgen.py index 40c4600..750173e 100644 --- a/third_party/a64-kit/lift/a64_rtgen.py +++ b/third_party/a64-kit/lift/a64_rtgen.py @@ -33,9 +33,28 @@ def runtime_h(): h = re.sub(r"^%s\(.*\n" % re.escape(proto), "", h, flags=re.M) # (every context parameter A64_NOESC, rt/a64.h: these functions keep no pointer to it) h = re.sub(r"([(,]\s*)GuestContext\s*\*\s*(?=(?:\w+\s*)?[,)])", r"\1GuestContext *A64_NOESC ", h) + h = fma32_native(h) return "/* Generated by a64-kit/lift/a64_rtgen.py from suyu/arm64_to_c.h (GPL-2.0-or-later). Do not edit. */\n" + h +# (triforce) The binary32 fused multiply-add's fast path on x86-64: suyu computes it in binary64 (three widenings, a multiply, +# an add, a narrowing and the midpoint test), because x86-64's baseline has no FMA. The game is built for x86-64-v3 where +# the CPU has it (nx2/build.py), and there one FMA instruction is the correctly rounded result, as on Arm. The tests that +# decide whether the native result is kept are unchanged. -DA64_FPX_NO_FMA32: the binary64 form. +FMA32_OLD = ("#if RECOMP_FPX_HOST == 1\n" + " const uint64_t t = recomp_fpx_fb(__builtin_fmaf(recomp_fpx_f(a), recomp_fpx_f(b), recomp_fpx_f(z)));\n" + "#elif RECOMP_FPX_HOST == 2\n" + " const double s = (double)recomp_fpx_f(a) * (double)recomp_fpx_f(b) + (double)recomp_fpx_f(z);\n") +FMA32_NEW = FMA32_OLD.replace("#if RECOMP_FPX_HOST == 1\n", + "#if RECOMP_FPX_HOST == 1 || (RECOMP_FPX_HOST == 2 && defined(__FMA__) && !defined(A64_FPX_NO_FMA32))\n") + + +def fma32_native(h): + if h.count(FMA32_OLD) != 1: + raise SystemExit("a64_rtgen: suyu's recomp_fpx_fma32 changed: update fma32_native") + return h.replace(FMA32_OLD, FMA32_NEW) + + def _function(src, signature): """The text of one top-level C function, from its signature to its closing brace at column 0.""" a = src.find(signature)