mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-25 18:48:51 -04:00
Evil HD (SMAA Ultra, 16x filtering, full textures, ambient occlusion with bounce light, sunlight shafts, foliage drawn 4x as far, the remastered texture pack's loader and the launcher's download of it), FSR 3.1 frame generation for every GPU, Intel XeSS, FSR 4 model choice, ReShade presets, Auto (this PC), HUD placement, the options menu in 13 languages, a second input for every control, hold to toggle, DualSense speaker and haptics, NFC from the player's own dumps, the above-60 fps game fixes, the Debug tab's flight and Inventory fixes, the launcher's full settings, and over 250 fixes.
145 lines
6.5 KiB
C++
145 lines
6.5 KiB
C++
// The production dispatch and away/back functions are included unchanged. Only the core grant and renderer are synthetic.
|
|
#define _Static_assert static_assert
|
|
#include "gpu/vk/vk_api.h"
|
|
#include <chrono>
|
|
#include <condition_variable>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <mutex>
|
|
#include <thread>
|
|
|
|
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL:%d %s\n", __LINE__, #x); std::exit(1); } } while (0)
|
|
using namespace std::chrono_literals;
|
|
|
|
struct HosThread { int vc_held = 0, vc_escaped = 0, releases = 0, takes = 0; bool returning_caller = false; };
|
|
static thread_local HosThread* cur;
|
|
static thread_local bool callback_active;
|
|
static std::mutex lease_mu, renderer_mu;
|
|
static std::condition_variable lease_cv, progress_cv;
|
|
static HosThread* owner;
|
|
static bool enabled = true, producer_queued, producer_done;
|
|
static bool require_no_lease = true, need_producer;
|
|
static int vulkan_calls, fallback_calls;
|
|
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<std::mutex> lock(lease_mu);
|
|
CHECK(owner == t && t->vc_held == 1);
|
|
owner = nullptr;
|
|
t->vc_held = 0;
|
|
t->releases++;
|
|
lease_cv.notify_all();
|
|
}
|
|
static void vc_acquire(HosThread* t) {
|
|
// Returning to guest execution must happen after the renderer's lock has unwound.
|
|
if (t->returning_caller) {
|
|
CHECK(!callback_active);
|
|
CHECK(renderer_mu.try_lock());
|
|
renderer_mu.unlock();
|
|
}
|
|
std::unique_lock<std::mutex> lock(lease_mu);
|
|
CHECK(lease_cv.wait_for(lock, 5s, [] { return owner == nullptr; }));
|
|
owner = t;
|
|
t->vc_held = 1;
|
|
t->takes++;
|
|
}
|
|
static void renderer(uint64_t gpu, uint64_t size) {
|
|
std::lock_guard<std::mutex> render_lock(renderer_mu);
|
|
callback_active = true;
|
|
CHECK(gpu == want_gpu && size == want_size);
|
|
if (require_no_lease) CHECK(!cur || !cur->vc_held);
|
|
if (need_producer) {
|
|
std::unique_lock<std::mutex> lock(lease_mu);
|
|
// This is a liveness bound, not a performance assertion. The producer needs the caller's one allowed core.
|
|
CHECK(progress_cv.wait_for(lock, 2s, [] { return producer_done; }));
|
|
}
|
|
callback_active = false;
|
|
}
|
|
static void vulkan_invalidate(uint64_t gpu, uint64_t size) { vulkan_calls++; renderer(gpu, size); }
|
|
static void wgr_nvn_invalidate(uint64_t gpu, uint64_t size) { fallback_calls++; renderer(gpu, size); }
|
|
// ((Triforce, THREAD-25) the back end's answer to "has this invalidate nothing to wait for?" (mkwvk_invalidate_quick, found by name
|
|
// in the product): asked with the caller's core still held, before anything is given up
|
|
static int quick_calls, quick_answer;
|
|
static int quick(uint64_t gpu, uint64_t size) {
|
|
quick_calls++;
|
|
CHECK(gpu == want_gpu && size == want_size);
|
|
CHECK(!callback_active);
|
|
if (cur && enabled) CHECK(cur->vc_held == 1 && cur->releases == 0);
|
|
return quick_answer;
|
|
}
|
|
static int (*volatile invalidate_quick_fn)(uint64_t, uint64_t);
|
|
|
|
#include "actual_invalidate.inc"
|
|
|
|
int main(int argc, char** argv) {
|
|
CHECK(argc == 2);
|
|
const char* name = argv[1];
|
|
const bool fallback = !std::strcmp(name, "fallback") || !std::strcmp(name, "fallback-immediate") || !std::strcmp(name, "quick-fallback");
|
|
const bool already_away = !std::strcmp(name, "already-away");
|
|
const bool non_guest = !std::strcmp(name, "non-guest");
|
|
enabled = std::strcmp(name, "disabled") != 0;
|
|
need_producer = !std::strcmp(name, "vulkan") || !std::strcmp(name, "fallback") || !std::strcmp(name, "producer");
|
|
// A separate case demonstrates producer starvation on the original wrapper, beyond the entry invariant.
|
|
require_no_lease = std::strcmp(name, "producer") != 0;
|
|
if (!std::strcmp(name, "zero")) want_gpu = want_size = 0;
|
|
// ((Triforce, THREAD-25) quick: the back end says there is nothing to wait for: no call, no release, no take. quick-declined: it
|
|
// says there may be: everything as before. quick-fallback: no Vulkan back end is running: it is not asked)
|
|
const bool quick_yes = !std::strcmp(name, "quick"), quick_no = !std::strcmp(name, "quick-declined");
|
|
const bool quick_fallback = !std::strcmp(name, "quick-fallback");
|
|
if (quick_yes || quick_no || quick_fallback) invalidate_quick_fn = quick, quick_answer = !quick_no;
|
|
MkwVkRenderer backend{};
|
|
backend.nvn_invalidate = vulkan_invalidate;
|
|
vk = fallback ? nullptr : &backend;
|
|
HosThread caller, producer;
|
|
caller.returning_caller = true;
|
|
cur = non_guest ? nullptr : &caller;
|
|
if (cur && enabled) owner = &caller, caller.vc_held = 1;
|
|
int outer_away = 0;
|
|
if (already_away) outer_away = hos_vcore_away();
|
|
const int before_release = caller.releases, before_take = caller.takes;
|
|
|
|
std::thread worker;
|
|
if (need_producer) {
|
|
worker = std::thread([&] {
|
|
cur = &producer;
|
|
{
|
|
std::lock_guard<std::mutex> lock(lease_mu);
|
|
producer_queued = true;
|
|
progress_cv.notify_all();
|
|
}
|
|
vc_acquire(&producer);
|
|
{
|
|
std::lock_guard<std::mutex> lock(lease_mu);
|
|
producer_done = true;
|
|
progress_cv.notify_all();
|
|
}
|
|
vc_release(&producer);
|
|
});
|
|
std::unique_lock<std::mutex> lock(lease_mu);
|
|
CHECK(progress_cv.wait_for(lock, 2s, [] { return producer_queued; }));
|
|
}
|
|
rx_nvn_invalidate(want_gpu, want_size);
|
|
if (worker.joinable()) worker.join();
|
|
CHECK(vulkan_calls == (fallback || quick_yes ? 0 : 1));
|
|
CHECK(fallback_calls == (fallback ? 1 : 0));
|
|
CHECK(quick_calls == (quick_yes || quick_no ? 1 : 0));
|
|
const bool owned_on_entry = cur && enabled && !already_away;
|
|
CHECK(caller.releases - before_release == (owned_on_entry && !quick_yes ? 1 : 0));
|
|
CHECK(caller.takes - before_take == (owned_on_entry && !quick_yes ? 1 : 0));
|
|
CHECK(caller.vc_held == (owned_on_entry ? 1 : 0));
|
|
CHECK(owner == (owned_on_entry ? &caller : nullptr));
|
|
if (need_producer) CHECK(producer_done && producer.takes == 1 && producer.releases == 1 && !producer.vc_held);
|
|
if (outer_away) {
|
|
hos_vcore_back();
|
|
CHECK(caller.vc_held == 1 && caller.releases == 1 && caller.takes == 1);
|
|
}
|
|
std::printf("PASS: %s actual invalidate wrapper\n", name);
|
|
}
|