diff --git a/runtime/mesh_fb_hold.h b/runtime/mesh_fb_hold.h new file mode 100644 index 00000000..cf4a5b7a --- /dev/null +++ b/runtime/mesh_fb_hold.h @@ -0,0 +1,31 @@ +// The mesh fallback's depth hold (vk_exec_draw.c "a list's depth-only draw is held"): which lists' colour draws were +// skipped lately. Header only, so that tests/mesh_fb_hold_check.c checks the table without a device. +// +// A list is its indirect record's address in the game's memory (below 2^39). One word per slot: the address above +// MFB_HOLD_BITS bits of the present count. The colour draw stamps its list with the present it was skipped in and +// clears the stamp when it is drawn; the list is held while the stamp is at most MFB_HOLD_PRESENTS presents old. +// Read and written by the replay's threads without a lock: a lost update costs a list one frame of the hold, and two +// lists in one slot (MFB_HOLD_SLOTS of them, by a hash of the address) hold only the one stamped last. +#pragma once +#include + +#define MFB_HOLD_SLOTS 1024 +#define MFB_HOLD_PRESENTS 2 +#define MFB_HOLD_BITS 24 +#define MFB_HOLD_MASK ((1ull << MFB_HOLD_BITS) - 1) + +static inline uint32_t mfb_hold_index(uint64_t list) { return (uint32_t)((list >> 2) * 0x9e3779b1u) >> 22; } + +// The list's colour draw was skipped for its modules or pipeline (waits 1) at this present, or was drawn (0). +static inline void mfb_hold_mark(volatile uint64_t *tab, uint64_t list, int waits, uint64_t present) { + volatile uint64_t *slot = &tab[mfb_hold_index(list)]; + if (waits) __atomic_store_n(slot, list << MFB_HOLD_BITS | (present & MFB_HOLD_MASK), __ATOMIC_RELAXED); + else if (__atomic_load_n(slot, __ATOMIC_RELAXED) >> MFB_HOLD_BITS == list) __atomic_store_n(slot, 0, __ATOMIC_RELAXED); +} + +// Whether the list's depth-only draw is held at this present. +static inline int mfb_hold_test(const volatile uint64_t *tab, uint64_t list, uint64_t present) { + uint64_t w = __atomic_load_n(&tab[mfb_hold_index(list)], __ATOMIC_RELAXED); + if (!w || w >> MFB_HOLD_BITS != list) return 0; + return ((present - w) & MFB_HOLD_MASK) <= MFB_HOLD_PRESENTS; +} diff --git a/runtime/vk_device.c b/runtime/vk_device.c index 79bfdc05..25f8503a 100644 --- a/runtime/vk_device.c +++ b/runtime/vk_device.c @@ -18,6 +18,7 @@ #include "vk_gpa.h" #include "vk_fg.h" #include "vk_dlssg.h" +#include "vk_dlssg_device.h" #include "vk_lowlat.h" #include "vk_mesh_path.h" @@ -272,7 +273,7 @@ retry: // (vk_dlssg_create_device), and the settings ask for what only an NVIDIA card runs. Not usable, so the best // usable one is taken below, as for a card that is gone; where no NVIDIA card is usable either, the pass // without a choice takes what there is and the device is made without frame generation. - if ((want >= 0 || (name && *name)) && vk_dlssg_interposed() && p.vendorID != 0x10de) { + if (dlssg_card_skipped(vk_dlssg_interposed(), want >= 0 || (name && *name), p.vendorID)) { rt_log("vk: gpu %u skipped: DLSS Frame Generation is on (framegen = dlss), which only an NVIDIA card runs", i); continue; } diff --git a/runtime/vk_dlssg.c b/runtime/vk_dlssg.c index 51812434..e055fc01 100644 --- a/runtime/vk_dlssg.c +++ b/runtime/vk_dlssg.c @@ -42,6 +42,7 @@ #include "vk.h" #include "vk_dlaa.h" #include "vk_dlssg.h" +#include "vk_dlssg_device.h" #include "dlssg/nx2_sl.h" #include "ui/menu.h" @@ -213,20 +214,31 @@ static void give_up(const char *why) { // set to DLSS and the Graphics card setting on the Intel one, that ended every start with "no usable Vulkan device" // (exit code 70) until nx2navi.ini was edited by hand: four reports on 0.10.1. A device of another vendor is now made // by the loader, without frame generation, and so is one the interposer refuses for any other reason. +// The order of the attempts is vk_dlssg_device.h's dlssg_device_create (tests/dlssg_device_check.c). +typedef struct { PFN_vkCreateDevice f; VkPhysicalDevice pd; const VkDeviceCreateInfo *ci; VkDevice *out; } DevCreate; +static int create_interposed(void *u) { + const DevCreate *d = u; + return (int)d->f(d->pd, d->ci, NULL, d->out); +} +static int create_loader(void *u) { + const DevCreate *d = u; + return (int)vkCreateDevice(d->pd, d->ci, NULL, d->out); +} + VkResult vk_dlssg_create_device(VkInstance inst, VkPhysicalDevice pd, const VkDeviceCreateInfo *ci, VkDevice *out) { - if (S.loaded > 0) { - VkPhysicalDeviceProperties p; - vkGetPhysicalDeviceProperties(pd, &p); - if (p.vendorID != 0x10de) give_up("the graphics card in use is not an NVIDIA card (the Graphics card setting, gpu_index)"); + VkPhysicalDeviceProperties p; + vkGetPhysicalDeviceProperties(pd, &p); + DevCreate d = {S.loaded > 0 ? (PFN_vkCreateDevice)S.gipa()(inst, "vkCreateDevice") : NULL, pd, ci, out}; + int how, refused; + VkResult r = (VkResult)dlssg_device_create(d.f != NULL, p.vendorID, create_interposed, create_loader, &d, &how, &refused); + if (how == DLSSG_DEV_NOT_NVIDIA) { + give_up("the graphics card in use is not an NVIDIA card (the Graphics card setting, gpu_index)"); + } else if (how == DLSSG_DEV_REFUSED) { + char why[96]; + snprintf(why, sizeof why, "Streamline could not make the device (%d)", refused); + give_up(why); } - PFN_vkCreateDevice f = S.loaded > 0 ? (PFN_vkCreateDevice)S.gipa()(inst, "vkCreateDevice") : NULL; - if (!f) return vkCreateDevice(pd, ci, NULL, out); - VkResult r = f(pd, ci, NULL, out); - if (r == VK_SUCCESS) return r; - char why[96]; - snprintf(why, sizeof why, "Streamline could not make the device (%d)", (int)r); - give_up(why); - return vkCreateDevice(pd, ci, NULL, out); + return r; } void vk_dlssg_device_ready(VkPhysicalDevice physical, VkDevice dev) { diff --git a/runtime/vk_dlssg_device.h b/runtime/vk_dlssg_device.h new file mode 100644 index 00000000..fc40d4a5 --- /dev/null +++ b/runtime/vk_dlssg_device.h @@ -0,0 +1,39 @@ +// DLSS Frame Generation and the card the device is made on (vk_device.c pick_device, vk_dlssg.c +// vk_dlssg_create_device). Header only, so that tests/dlssg_device_check.c checks both decisions without a device. +// +// With frame generation on, the instance and the device are made through Streamline's interposer, whose vkCreateDevice +// asks the card for NVIDIA's extensions (VK_NV_low_latency2 among them) and fails without them +// (VK_ERROR_EXTENSION_NOT_PRESENT, -7). On a laptop with an NVIDIA card and Intel graphics, framegen = dlss and +// gpu_index on the Intel one, that was the only attempt, and every start ended with "no usable Vulkan device" (exit +// code 70) until nx2navi.ini was edited by hand. +#pragma once +#include + +#define DLSSG_VENDOR_NVIDIA 0x10deu + +// pick_device: a card the player chose (gpu_index, gpu_name) counts as not usable while the interposer is in use and +// the card is of another vendor, so the best usable card is taken instead, as for a chosen card that is gone. Without +// a choice nothing is skipped: the pass takes what there is, and dlssg_device_create makes the device. +static inline int dlssg_card_skipped(int interposed, int chosen, uint32_t vendor) { + return interposed && chosen && vendor != DLSSG_VENDOR_NVIDIA; +} + +// A creation attempt: 0 (VK_SUCCESS) or the VkResult it failed with. +typedef int (*DlssgCreate)(void *user); + +// How the device was made, or tried: by the loader with the interposer not in use; through the interposer; by the +// loader because the card is not an NVIDIA one (the interposer is not asked); by the loader after the interposer +// refused (*refused holds its result). In the last two frame generation is off for the run. +enum { DLSSG_DEV_LOADER, DLSSG_DEV_INTERPOSER, DLSSG_DEV_NOT_NVIDIA, DLSSG_DEV_REFUSED }; + +// Returns the result of the attempt that counts: the interposer's when it made the device, else the loader's. +static inline int dlssg_device_create(int interposed, uint32_t vendor, DlssgCreate via_interposer, DlssgCreate via_loader, + void *user, int *how, int *refused) { + *refused = 0; + if (!interposed) return *how = DLSSG_DEV_LOADER, via_loader(user); + if (vendor != DLSSG_VENDOR_NVIDIA) return *how = DLSSG_DEV_NOT_NVIDIA, via_loader(user); + int r = via_interposer(user); + if (!r) return *how = DLSSG_DEV_INTERPOSER, 0; + *refused = r; + return *how = DLSSG_DEV_REFUSED, via_loader(user); +} diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index dad7ac6d..1df6c203 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -31,6 +31,7 @@ #include "vk_exec.h" #include "vk_gpa.h" #include "mesh_fb.h" +#include "mesh_fb_hold.h" #include "vk_mesh_fb.h" #include "vk_scratch_lru.h" #include "vk_index_rewrite.h" @@ -4482,10 +4483,8 @@ static const VkPipeInfo *PD_info; // the pipeline of the last draw prepare_dra // skipped in and clears the stamp when it is drawn; the depth-only draw, which comes first in the frame, is held when // the stamp is at most MFB_HOLD_PRESENTS presents old, so a list whose colour draw no longer comes is held no longer // than that. A draw whose stages write memory is never held (mfb_draw): the writes are wanted whatever is drawn. -// One word per slot (the address above 24 bits of the present count), read and written by the replay's threads without -// a lock: a lost update costs a list one frame of the hold. NX2NAVI_MESH_FALLBACK_HOLD=0 turns it off. -#define MFB_HOLD_SLOTS 1024 -#define MFB_HOLD_PRESENTS 2 +// The table is mesh_fb_hold.h's (tests/mesh_fb_hold_check.c), read and written by the replay's threads without a lock: +// a lost update costs a list one frame of the hold. NX2NAVI_MESH_FALLBACK_HOLD=0 turns it off. static volatile uint64_t mfb_hold[MFB_HOLD_SLOTS]; static volatile uint64_t mfb_held_draws; @@ -4495,23 +4494,13 @@ static int mfb_hold_on(void) { return on; } -static volatile uint64_t *mfb_hold_slot(uint64_t list) { - return &mfb_hold[(uint32_t)((list >> 2) * 0x9e3779b1u) >> 22]; -} - // The list's colour draw was skipped for its modules or pipeline (waits 1), or was drawn (0). static void mfb_hold_set(uint64_t list, int waits) { - volatile uint64_t *slot = mfb_hold_slot(list); - if (waits) - __atomic_store_n(slot, list << 24 | (__atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED) & 0xffffff), - __ATOMIC_RELAXED); - else if (__atomic_load_n(slot, __ATOMIC_RELAXED) >> 24 == list) __atomic_store_n(slot, 0, __ATOMIC_RELAXED); + mfb_hold_mark(mfb_hold, list, waits, __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED)); } static int mfb_held(uint64_t list) { - uint64_t w = __atomic_load_n(mfb_hold_slot(list), __ATOMIC_RELAXED); - if (!w || w >> 24 != list) return 0; - return ((__atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED) - w) & 0xffffff) <= MFB_HOLD_PRESENTS; + return mfb_hold_test(mfb_hold, list, __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED)); } // A held depth-only draw is left out, counted as a draw whose own pipeline is building: the frame lacks it for the diff --git a/runtime/vk_tex_detail.h b/runtime/vk_tex_detail.h new file mode 100644 index 00000000..aff4e1ca --- /dev/null +++ b/runtime/vk_tex_detail.h @@ -0,0 +1,20 @@ +// The level texture_detail = auto picks (vk_texture.c "texture detail", vk_texture_detail_auto). Header only, so that +// tests/tex_detail_check.c checks the choice without a device. +#pragma once +#include + +// Full from DETAIL_FULL_MB of device-local budget on, low under DETAIL_LOW_MB, reduced between; under DETAIL_RGBA8_MB +// one step lower where the ASTC textures stay uncompressed. vk_texture.c says where each number comes from. +#define DETAIL_FULL_MB 10240 +#define DETAIL_LOW_MB 6144 +#define DETAIL_RGBA8_MB 20480 + +// mb: the device-local budget in MiB, 0 when the device reports none. shared: the video memory is the system's own +// (an integrated GPU), whose budget is a share of the PC's memory: never full. rgba8: the ASTC textures stay RGBA8 +// here (astc = cpu, a card without the decoder, astc_bc = 0). Returns 0 full, 1 reduced, 2 low. +static inline int tex_detail_auto(int64_t mb, int shared, int rgba8) { + int most = !mb || mb >= DETAIL_FULL_MB ? 0 : mb >= DETAIL_LOW_MB ? 1 : 2; + if (mb && !most && shared) most = 1; + if (mb && mb < DETAIL_RGBA8_MB && most < 2 && rgba8) most++; + return most; +} diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index 4be073c0..a26d4ee7 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -29,6 +29,7 @@ #include "vk_dlaa.h" #include "vk_exec.h" #include "vk_tex_compact.h" +#include "vk_tex_detail.h" #include "vk_tex_release.h" #include "prof.h" #include "platform/platform.h" @@ -1118,15 +1119,13 @@ static int astc_opaque(const tex_t *t, uint64_t addr, const TexVkFormat *f) { // An integrated GPU's budget is a share of the system's memory, not memory of its own: one reported 10,361 MiB on a // handheld with 16 GiB of memory in all, auto chose full, and the game ended when a save was loaded until its player // set reduced. auto never chooses full there. +// The choice itself is vk_tex_detail.h's tex_detail_auto, with the three budgets (tests/tex_detail_check.c). #define DETAIL_MIN 256 #define DETAIL_LOW_MIN 1024 -#define DETAIL_FULL_MB 10240 -#define DETAIL_LOW_MB 6144 // The levels above assume the ASTC textures end as BC3 (or stay ASTC), a byte a texel. Where they stay RGBA8 (astc = // cpu, a card without the decoder, astc_bc = 0) they take four times that, which is what one level left out gives // back: under this budget auto then goes one step lower. A card with 8 GiB that decoded on the CPU held two texture // arrays of 852 MiB each, was 560 MiB over its budget and lost its device. -#define DETAIL_RGBA8_MB 20480 #define EXACT_MAX 65536 static nvn_hmap exact = {.klen = sizeof(TexKey)}; // keys made whole whatever the setting; value 1 static uint64_t n_exact; // textures made whole again so far @@ -1150,10 +1149,7 @@ int vk_texture_detail_auto(void) { mb = vk_mem_vram(&used, &budget, &exact_budget) ? (int64_t)(budget >> 20) : 0; __atomic_store_n(&detail_budget_mb, mb, __ATOMIC_RELAXED); } - int most = !mb || mb >= DETAIL_FULL_MB ? 0 : mb >= DETAIL_LOW_MB ? 1 : 2; - if (mb && !most && detail_shared()) most = 1; - if (mb && mb < DETAIL_RGBA8_MB && most < 2 && vk_astc_mode() != VK_ASTC_NATIVE && !vk_astc_bc()) most++; - return most; + return tex_detail_auto(mb, detail_shared(), vk_astc_mode() != VK_ASTC_NATIVE && !vk_astc_bc()); } static const char *const detail_names[3] = {"full", "reduced", "low"}; diff --git a/tests/dlssg_device_check.c b/tests/dlssg_device_check.c new file mode 100644 index 00000000..1fe392ba --- /dev/null +++ b/tests/dlssg_device_check.c @@ -0,0 +1,84 @@ +// runtime/vk_dlssg_device.h: which card counts as usable while DLSS Frame Generation's interposer is in use, and how +// the device is made when the interposer cannot make it (vk_device.c pick_device, vk_dlssg.c vk_dlssg_create_device). +// The interposer and the loader are stand-ins that answer as the real ones did in a player's log. Built and run by +// tests/dlssg_device_check.py; prints each failed check and exits 1 on any. +#include + +#include "vk_dlssg_device.h" + +#define VENDOR_INTEL 0x8086u +#define VENDOR_AMD 0x1002u +#define EXT_NOT_PRESENT (-7) // VK_ERROR_EXTENSION_NOT_PRESENT, what the interposer answered on Intel graphics + +static int fails; +#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0) + +// What a run did: the calls each stand-in got, and what each answers. +typedef struct { int interposer_calls, loader_calls, interposer_result, loader_result; } Run; +static int via_interposer(void *u) { + Run *r = u; + r->interposer_calls++; + return r->interposer_result; +} +static int via_loader(void *u) { + Run *r = u; + r->loader_calls++; + return r->loader_result; +} + +// 0.10.1's creation, for comparison: the interposer alone whenever it is in use. +static int create_0101(int interposed, Run *r) { return interposed ? via_interposer(r) : via_loader(r); } + +static const char *const hows[] = {"the loader", "the interposer", "the loader (not an NVIDIA card)", + "the loader (the interposer refused)"}; + +static void expect(const char *what, int interposed, unsigned vendor, int interposer_result, int loader_result, + int want_result, int want_how, int want_interposer_calls, int want_loader_calls) { + Run r = {0, 0, interposer_result, loader_result}; + int how = -1, refused = 0; + int got = dlssg_device_create(interposed, vendor, via_interposer, via_loader, &r, &how, &refused); + CHECK(got == want_result, "%s: result %d, not %d", what, got, want_result); + CHECK(how == want_how, "%s: made by %s, not by %s", what, how >= 0 && how < 4 ? hows[how] : "?", hows[want_how]); + CHECK(r.interposer_calls == want_interposer_calls, "%s: the interposer was asked %d times, not %d", what, + r.interposer_calls, want_interposer_calls); + CHECK(r.loader_calls == want_loader_calls, "%s: the loader was asked %d times, not %d", what, r.loader_calls, + want_loader_calls); + CHECK((how == DLSSG_DEV_REFUSED) == (refused != 0), "%s: the interposer's result is %d", what, refused); + printf(" %s: %s, result %d\n", what, how >= 0 && how < 4 ? hows[how] : "?", got); +} + +int main(void) { + // ---- the laptop of the report: an RTX 3050 (gpu 0) and Intel Iris Xe (gpu 1), framegen = dlss, gpu_index = 1 + { + Run old = {0, 0, EXT_NOT_PRESENT, 0}; + int r = create_0101(1, &old); + CHECK(r == EXT_NOT_PRESENT && !old.loader_calls, "0.10.1 on the Intel card: %d", r); + printf(" 0.10.1, the Intel card through the interposer: result %d and nothing else tried (exit code 70)\n", r); + } + CHECK(dlssg_card_skipped(1, 1, VENDOR_INTEL), "the chosen Intel card is not skipped with the interposer in use"); + CHECK(!dlssg_card_skipped(1, 1, DLSSG_VENDOR_NVIDIA), "the chosen NVIDIA card is skipped"); + CHECK(!dlssg_card_skipped(1, 0, DLSSG_VENDOR_NVIDIA), "the NVIDIA card is skipped in the pass without a choice"); + printf(" pick: the chosen Intel card is skipped, so the pass without a choice takes the NVIDIA card\n"); + expect("the NVIDIA card, interposer in use", 1, DLSSG_VENDOR_NVIDIA, 0, 0, 0, DLSSG_DEV_INTERPOSER, 1, 0); + + // ---- no NVIDIA card at all (framegen = dlss left in nx2navi.ini): the device is made without frame generation + CHECK(!dlssg_card_skipped(1, 0, VENDOR_AMD), "a card is skipped in the pass without a choice"); + CHECK(!dlssg_card_skipped(1, 0, VENDOR_INTEL), "a card is skipped in the pass without a choice"); + expect("an AMD card, interposer in use", 1, VENDOR_AMD, EXT_NOT_PRESENT, 0, 0, DLSSG_DEV_NOT_NVIDIA, 0, 1); + expect("Intel graphics, interposer in use", 1, VENDOR_INTEL, EXT_NOT_PRESENT, 0, 0, DLSSG_DEV_NOT_NVIDIA, 0, 1); + + // ---- an NVIDIA card the interposer refuses (an old driver): once through it, then the loader + expect("an NVIDIA card the interposer refuses", 1, DLSSG_VENDOR_NVIDIA, EXT_NOT_PRESENT, 0, 0, DLSSG_DEV_REFUSED, 1, 1); + // ... and when the loader fails too, its result is the one reported + expect("an NVIDIA card neither can make a device on", 1, DLSSG_VENDOR_NVIDIA, EXT_NOT_PRESENT, -3, -3, + DLSSG_DEV_REFUSED, 1, 1); + + // ---- frame generation off: the loader, whatever the card, and a chosen card of any vendor stands + expect("an NVIDIA card, frame generation off", 0, DLSSG_VENDOR_NVIDIA, 0, 0, 0, DLSSG_DEV_LOADER, 0, 1); + expect("Intel graphics, frame generation off", 0, VENDOR_INTEL, EXT_NOT_PRESENT, 0, 0, DLSSG_DEV_LOADER, 0, 1); + CHECK(!dlssg_card_skipped(0, 1, VENDOR_INTEL), "a chosen Intel card is skipped with frame generation off"); + CHECK(!dlssg_card_skipped(0, 1, VENDOR_AMD), "a chosen AMD card is skipped with frame generation off"); + + printf("dlssg_device_check: %s\n", fails ? "FAILED" : "passed"); + return fails ? 1 : 0; +} diff --git a/tests/dlssg_device_check.py b/tests/dlssg_device_check.py new file mode 100644 index 00000000..82cd0ec2 --- /dev/null +++ b/tests/dlssg_device_check.py @@ -0,0 +1,41 @@ +"""Build and run tests/dlssg_device_check.c: runtime/vk_dlssg_device.h, which card counts as usable while DLSS Frame +Generation's interposer is in use and how the device is made when the interposer cannot make it (vk_device.c +pick_device, vk_dlssg.c vk_dlssg_create_device), with stand-ins for the interposer and the loader. + +No GPU, no game files, any Python. + +Usage: dlssg_device_check.py +""" +import os +import shutil +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +MSYS_BIN = Path(r'C:\msys64\ucrt64\bin') + + +def compiler(): + """$CC, else MSYS2 UCRT64's clang, else clang on PATH (as tests/diff/common.py picks it, without its Unicorn).""" + if os.environ.get('CC'): + return os.environ['CC'] + if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists(): + return str(MSYS_BIN / 'clang.exe') + return shutil.which('clang') or 'clang' + + +def main(): + work = ROOT / 'build' / 'test' / 'dlssg_device' + work.mkdir(parents=True, exist_ok=True) + exe = work / ('dlssg_device_check.exe' if os.name == 'nt' else 'dlssg_device_check') + env = dict(os.environ) + if os.name == 'nt' and MSYS_BIN.exists(): + env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '') + subprocess.check_call([compiler(), '-std=gnu11', '-O1', '-Wall', '-Werror', '-I', str(ROOT / 'runtime'), + str(ROOT / 'tests' / 'dlssg_device_check.c'), '-o', str(exe)], env=env) + return subprocess.run([str(exe)], env=env).returncode + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tests/mesh_fb_hold_check.c b/tests/mesh_fb_hold_check.c new file mode 100644 index 00000000..512daf68 --- /dev/null +++ b/tests/mesh_fb_hold_check.c @@ -0,0 +1,67 @@ +// runtime/mesh_fb_hold.h: the table behind the mesh fallback's depth hold (vk_exec_draw.c): a list whose colour draw +// was skipped for its pipeline holds its depth-only draw for a few presents, and no longer. Built and run by +// tests/mesh_fb_hold_check.py; prints each failed check and exits 1 on any. +#include +#include + +#include "mesh_fb_hold.h" + +static int fails; +#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0) + +static volatile uint64_t tab[MFB_HOLD_SLOTS]; + +int main(void) { + const uint64_t grass = 0x59220f600ull, leaves = 0x26d876000ull; // record addresses as the game's memory has them + // ---- a frame's order is depth first, colour after: the stamp of one frame holds the depth draw of the next + uint64_t present = 1000; + CHECK(!mfb_hold_test(tab, grass, present), "a list nothing stamped is held"); + mfb_hold_mark(tab, grass, 1, present); // the colour draw waits for its pipeline + CHECK(mfb_hold_test(tab, grass, present), "not held in the present of the stamp"); + CHECK(mfb_hold_test(tab, grass, present + 1), "not held one present later"); + CHECK(mfb_hold_test(tab, grass, present + MFB_HOLD_PRESENTS), "not held at the last present of the hold"); + CHECK(!mfb_hold_test(tab, grass, present + MFB_HOLD_PRESENTS + 1), "still held after the hold"); + CHECK(!mfb_hold_test(tab, leaves, present), "another list is held"); + // ---- while the colour draw keeps waiting, each frame stamps again and the hold goes on + for (int i = 1; i <= 600; i++) { + CHECK(mfb_hold_test(tab, grass, present + (uint64_t)i), "frame %d of a wait: not held", i); + mfb_hold_mark(tab, grass, 1, present + (uint64_t)i); + } + present += 600; + // ---- the colour draw is drawn: the stamp goes, and the depth draw comes back in the next frame + mfb_hold_mark(tab, grass, 0, present + 1); + CHECK(!mfb_hold_test(tab, grass, present + 1), "held after the colour draw was drawn"); + CHECK(!mfb_hold_test(tab, grass, present + 2), "held in the frame after it"); + // ---- a colour draw that no longer comes (the list left the view): the hold lapses by itself + mfb_hold_mark(tab, leaves, 1, present); + CHECK(mfb_hold_test(tab, leaves, present + 2) && !mfb_hold_test(tab, leaves, present + 3), "the hold does not lapse"); + // ---- a draw of one list does not clear another list's stamp, in whatever slot each lies + mfb_hold_mark(tab, grass, 1, present); + mfb_hold_mark(tab, leaves, 0, present); + CHECK(mfb_hold_index(grass) == mfb_hold_index(leaves) || mfb_hold_test(tab, grass, present), + "another list's draw cleared the stamp"); + // ---- the present count's 24 bits wrap (77 hours at 60 fps): the hold counts across the wrap + memset((void *)tab, 0, sizeof tab); + mfb_hold_mark(tab, grass, 1, MFB_HOLD_MASK); + CHECK(mfb_hold_test(tab, grass, MFB_HOLD_MASK + 1) && mfb_hold_test(tab, grass, MFB_HOLD_MASK + 2) && + !mfb_hold_test(tab, grass, MFB_HOLD_MASK + 3), "the hold is wrong across the wrap of the present count"); + // ---- the highest address the game's memory has keeps its bits above the count + const uint64_t top = (1ull << 39) - 4; + memset((void *)tab, 0, sizeof tab); + mfb_hold_mark(tab, top, 1, 5); + CHECK(mfb_hold_test(tab, top, 6) && !mfb_hold_test(tab, top - 4, 6), "the top address is not told from its neighbour"); + // ---- every index is inside the table, and a field of records spreads over it + int used[MFB_HOLD_SLOTS] = {0}, most = 0, slots = 0; + for (uint64_t a = 0x59220f600ull; a < 0x59220f600ull + 256 * 12; a += 12) { // 256 records of 12 bytes + uint32_t i = mfb_hold_index(a); + CHECK(i < MFB_HOLD_SLOTS, "index %u out of the table", i); + if (i < MFB_HOLD_SLOTS && !used[i]++) slots++; + } + for (int i = 0; i < MFB_HOLD_SLOTS; i++) + if (used[i] > most) most = used[i]; + CHECK(slots >= 192 && most <= 4, "256 records fall into %d slots, up to %d in one", slots, most); + printf(" 256 neighbouring records fall into %d slots, at most %d in one\n", slots, most); + + printf("mesh_fb_hold_check: %s\n", fails ? "FAILED" : "passed"); + return fails ? 1 : 0; +} diff --git a/tests/mesh_fb_hold_check.py b/tests/mesh_fb_hold_check.py new file mode 100644 index 00000000..4960018d --- /dev/null +++ b/tests/mesh_fb_hold_check.py @@ -0,0 +1,40 @@ +"""Build and run tests/mesh_fb_hold_check.c: runtime/mesh_fb_hold.h, the table behind the mesh fallback's depth hold +(vk_exec_draw.c): a list whose colour draw waits for its pipeline holds its depth-only draw, and for how long. + +No GPU, no game files, any Python. + +Usage: mesh_fb_hold_check.py +""" +import os +import shutil +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +MSYS_BIN = Path(r'C:\msys64\ucrt64\bin') + + +def compiler(): + """$CC, else MSYS2 UCRT64's clang, else clang on PATH (as tests/diff/common.py picks it, without its Unicorn).""" + if os.environ.get('CC'): + return os.environ['CC'] + if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists(): + return str(MSYS_BIN / 'clang.exe') + return shutil.which('clang') or 'clang' + + +def main(): + work = ROOT / 'build' / 'test' / 'mesh_fb_hold' + work.mkdir(parents=True, exist_ok=True) + exe = work / ('mesh_fb_hold_check.exe' if os.name == 'nt' else 'mesh_fb_hold_check') + env = dict(os.environ) + if os.name == 'nt' and MSYS_BIN.exists(): + env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '') + subprocess.check_call([compiler(), '-std=gnu11', '-O1', '-Wall', '-Werror', '-I', str(ROOT / 'runtime'), + str(ROOT / 'tests' / 'mesh_fb_hold_check.c'), '-o', str(exe)], env=env) + return subprocess.run([str(exe)], env=env).returncode + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tests/tex_detail_check.c b/tests/tex_detail_check.c new file mode 100644 index 00000000..350dea08 --- /dev/null +++ b/tests/tex_detail_check.c @@ -0,0 +1,53 @@ +// runtime/vk_tex_detail.h: the level texture_detail = auto picks (vk_texture.c vk_texture_detail_auto), for the cards and +// budgets players reported. Built and run by tests/tex_detail_check.py; prints each failed check and exits 1 on any. +#include + +#include "vk_tex_detail.h" + +static int fails; +static const char *const names[] = {"full", "reduced", "low"}; + +static void expect(long long mb, int shared, int rgba8, int want, const char *what) { + int got = tex_detail_auto(mb, shared, rgba8); + if (got != want) { + fails++; + printf("FAIL: %s (budget %lld MiB, integrated %d, uncompressed ASTC %d): %s, not %s\n", what, mb, shared, rgba8, + names[got], names[want]); + } else { + printf(" ok: %-62s budget %5lld MiB -> %s\n", what, mb, names[got]); + } +} + +int main(void) { + // ---- the budgets in players' logs of 0.10.1 + expect(10361, 1, 0, 1, "ROG Ally X, integrated (0.10.1: full, ended at save load)"); + expect(7365, 0, 0, 1, "RX 7600, 8 GiB"); + expect(11313, 0, 0, 0, "RX 7700 XT, 12 GiB"); + expect(15569, 0, 0, 0, "RX 6800 XT, 16 GiB"); + expect(31419, 0, 0, 0, "RTX 5090, 32 GiB"); + // ---- between the old 8,192 MiB and the new 10,240: the cards of 10 GiB + expect(9300, 0, 0, 1, "a 10 GiB card (0.10.1: full)"); + expect(8192, 0, 0, 1, "8,192 MiB, where full started in 0.10.1"); + expect(10239, 0, 0, 1, "one MiB under the new start of full"); + expect(10240, 0, 0, 0, "the new start of full"); + // ---- low under 6,144 MiB, as before + expect(6144, 0, 0, 1, "6,144 MiB"); + expect(6143, 0, 0, 2, "one MiB under it"); + expect(4730, 0, 0, 2, "an RTX 3060 Laptop, 6 GiB"); + // ---- an integrated GPU never gets full, and follows the budget below that + expect(16000, 1, 0, 1, "integrated with a 16 GiB budget"); + expect(8034, 1, 0, 1, "Intel Iris Xe (8,034 MiB)"); + expect(5000, 1, 0, 2, "integrated with a 5 GiB budget"); + // ---- no budget reported: full, as before, also on an integrated GPU + expect(0, 0, 0, 0, "no budget reported"); + expect(0, 1, 0, 0, "no budget reported, integrated"); + // ---- uncompressed ASTC textures: one step lower under 20,480 MiB + expect(15569, 0, 1, 1, "a 16 GiB card with uncompressed ASTC"); + expect(7365, 0, 1, 2, "an 8 GiB card with uncompressed ASTC"); + expect(4730, 0, 1, 2, "a 6 GiB card with uncompressed ASTC (already low)"); + expect(24000, 0, 1, 0, "a 24 GiB card with uncompressed ASTC"); + expect(10361, 1, 1, 2, "integrated with uncompressed ASTC"); + + printf("tex_detail_check: %s\n", fails ? "FAILED" : "passed"); + return fails ? 1 : 0; +} diff --git a/tests/tex_detail_check.py b/tests/tex_detail_check.py new file mode 100644 index 00000000..88129143 --- /dev/null +++ b/tests/tex_detail_check.py @@ -0,0 +1,40 @@ +"""Build and run tests/tex_detail_check.c: runtime/vk_tex_detail.h, the level texture_detail = auto picks for a video +memory budget, on a card with memory of its own and on an integrated GPU (vk_texture.c vk_texture_detail_auto). + +No GPU, no game files, any Python. + +Usage: tex_detail_check.py +""" +import os +import shutil +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +MSYS_BIN = Path(r'C:\msys64\ucrt64\bin') + + +def compiler(): + """$CC, else MSYS2 UCRT64's clang, else clang on PATH (as tests/diff/common.py picks it, without its Unicorn).""" + if os.environ.get('CC'): + return os.environ['CC'] + if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists(): + return str(MSYS_BIN / 'clang.exe') + return shutil.which('clang') or 'clang' + + +def main(): + work = ROOT / 'build' / 'test' / 'tex_detail' + work.mkdir(parents=True, exist_ok=True) + exe = work / ('tex_detail_check.exe' if os.name == 'nt' else 'tex_detail_check') + env = dict(os.environ) + if os.name == 'nt' and MSYS_BIN.exists(): + env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '') + subprocess.check_call([compiler(), '-std=gnu11', '-O1', '-Wall', '-Werror', '-I', str(ROOT / 'runtime'), + str(ROOT / 'tests' / 'tex_detail_check.c'), '-o', str(exe)], env=env) + return subprocess.run([str(exe)], env=env).returncode + + +if __name__ == '__main__': + sys.exit(main())