Files
Triforce-Source/tests/present_source_crop_test.cpp
ruckus7382sfgsgdf 4fa4f9188b Triforce v0.8.0
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.
2026-11-25 10:21:32 -06:00

218 lines
13 KiB
C++

// Production consumers inserted by test_present_source_crop.py; no GPU or game data.
#include <algorithm>
#include <atomic>
#include <climits>
#include <condition_variable>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <initializer_list>
#include <mutex>
#include <new>
#include <thread>
#include <utility>
#define VK_NO_PROTOTYPES
#define VK_USE_64_BIT_PTR_DEFINES 0
#include <vulkan/vulkan.h>
#include "vk_present_rect.h"
#include "framegen_refresh.h" // ((Triforce, 11-21, the upscaler audit's UPS-05) a fixed multiplier and the display's refresh
#include "framegen_rows.h" // (Triforce: the Frame generation row's AMD FSR 4, which framegen_record asks first)
using namespace mkwvk;
using VmaAllocation = void*;
constexpr int kMaxMult = 8;
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #x); std::exit(1); } } while (0)
/* ACTUAL_STATE */
struct { int framegen = 2, hdr_white = 200, hdr_peak = 1000; } opts;
struct { bool push_descriptor = true; std::mutex present_mu; } dev;
constexpr bool vkCmdPushDescriptorSetKHR = true;
constexpr uint32_t HDR_ENCODE_COMP_SPV[] = {0};
bool init_scale() { std::abort(); }
bool make_pipe(Pipe&, const uint32_t*, size_t, std::initializer_list<VkDescriptorType>, int) { std::abort(); }
void logf(const char*, ...) { std::abort(); } // ((Triforce, UPS-05) framegen_record's line takes arguments; never reached here)
void device_wait_idle_locked() { std::abort(); }
void drop_img(Img&) { std::abort(); }
bool make_img(Img&, uint32_t, uint32_t, VkFormat, VkImageUsageFlags) { std::abort(); }
void to_general(VkCommandBuffer, VkImage) { std::abort(); }
float hdr_uv[4]{};
int blits = 0, clears = 0, initializations = 0, sizes = 0, dispatches = 0, copies = 0;
uint32_t allocated_w = 0, allocated_h = 0;
namespace fsr3fi { bool wanted = false; bool frames_wanted() { return wanted; } }
// (Triforce) whether RivaTuner Statistics Server holds the display thread's presents (vk_pace.h): not here
namespace pace { bool display_yields() { return false; } }
int fsr_calls = 0, fsr_result = 1;
// ((Triforce) 11-24) AMD FSR 3.1's frame interpolation at any multiplier (the Frame generation by row): a set's frames at k/m,
// each into its own slot; made (fsr_result) or none. Its HUD source is recorded first (hud_source: nothing here)
int fsr3fi_frames(VkCommandBuffer, OutSet&, int, uint32_t w, uint32_t h, uint64_t sequence, const float* t, const int* slot,
int count) {
CHECK(w == allocated_w && h == allocated_h && sequence > 0);
for (int i = 0; i < count; i++) CHECK(t[i] > 0.0f && t[i] < 1.0f && slot[i] == i);
++fsr_calls; return fsr_result ? count : 0;
}
bool hud_source(VkCommandBuffer, int, uint32_t, uint32_t, int, int, uint32_t w, uint32_t h) {
CHECK(w == allocated_w && h == allocated_h);
return false;
}
enum { kMakerNone = 0, kMakerOwn = 1, kMakerFsr3 = 2, kMakerAmd = 3 };
std::atomic<int> last_maker{0};
// ((Triforce) 11-24) AMD Anti-Lag 2's frame of a set, and the game's timeline (RX_VK_FG_GAME_TIMELINE, off here)
uint64_t antilag_frame() { return 0; }
bool game_timeline() { return false; }
void fixed_dues(int64_t, int64_t, int, int64_t, int64_t*) { std::abort(); }
int64_t pace_lead(int64_t, int) { std::abort(); }
// (Triforce) AMD FSR 4's frame generation (vk_framegen.cpp fsr4_frame, vk_fsr4.h): a stand-in that answers as told
int fsr4_calls = 0;
bool fsr4_blend = false;
framegen_rows::Made fsr4_result = framegen_rows::OWN;
framegen_rows::Made fsr4_frame(VkCommandBuffer, OutSet&, int, uint32_t w, uint32_t h, uint64_t sequence, bool blend) {
CHECK(w == allocated_w && h == allocated_h && sequence > 0);
++fsr4_calls; fsr4_blend = blend; return fsr4_result;
}
VkImageBlit last_blit{};
int32_t copy_pc[4]{};
void vkCmdBlitImage(VkCommandBuffer, VkImage, VkImageLayout, VkImage, VkImageLayout,
uint32_t count, const VkImageBlit* b, VkFilter filter) {
CHECK(count == 1 && filter == VK_FILTER_LINEAR); ++blits; last_blit = *b;
}
void vkCmdClearColorImage(VkCommandBuffer, VkImage, VkImageLayout, const VkClearColorValue* c,
uint32_t count, const VkImageSubresourceRange* range) {
CHECK(count == 1 && range->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT);
CHECK(c->float32[0] == 0 && c->float32[1] == 0 && c->float32[2] == 0 && c->float32[3] == 1); ++clears;
}
bool init() { ++initializations; return true; }
int multiplier();
bool ensure_size(VkCommandBuffer, uint32_t w, uint32_t h) {
// (as the real one: the sets' frames are made for the multiplier, which framegen_record holds its own to)
++sizes; allocated_w = w; allocated_h = h; fg.mult = multiplier(); return true;
}
int64_t now_ns() { return 1000000; }
int multiplier() { return 2; }
int display_refresh_mhz() { return 60000; }
int auto_slots(int64_t, int64_t, int64_t, int64_t&, float*, int64_t*, int) { return 0; }
void display_main() { std::abort(); } // Never started: isolated recorder sets thread_started.
void full_barrier(VkCommandBuffer) {}
void pass(VkCommandBuffer, const Pipe& p, std::initializer_list<std::pair<VkImageView, VkSampler>>,
uint32_t, uint32_t, const void* pc = nullptr, uint32_t bytes = 0) {
++dispatches;
if (&p == &fg.copy) { CHECK(bytes == sizeof copy_pc); std::memcpy(copy_pc, pc, bytes); }
if (&p == &hdr.pipe) { CHECK(bytes == 32); std::memcpy(hdr_uv, pc, sizeof hdr_uv); }
}
void vkCmdCopyImage(VkCommandBuffer, VkImage, VkImageLayout, VkImage, VkImageLayout,
uint32_t count, const VkImageCopy* c) {
CHECK(count == 1 && c->extent.width == allocated_w && c->extent.height == allocated_h); ++copies;
}
/* ACTUAL_FUNCTIONS */
void reset() {
fg.~State(); new (&fg) State;
fg.thread_started = true;
blits = clears = initializations = sizes = dispatches = copies = 0;
allocated_w = allocated_h = 0;
fsr3fi::wanted = false; fsr_calls = 0; fsr_result = 1;
opts.framegen = 2; fsr4_calls = 0; fsr4_blend = false; fsr4_result = framegen_rows::OWN;
std::memset(copy_pc, 0, sizeof copy_pc);
}
void check(uint32_t sw, uint32_t sh, const int* crop, uint32_t x, uint32_t y, uint32_t w, uint32_t h) {
reset();
blit_crop_to_rect(nullptr, 1, sw, sh, crop, 2, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, {3, 5, 1920, 1080});
const bool valid = w && h;
CHECK(blits == int(valid) && clears == int(!valid));
if (valid) {
CHECK(last_blit.srcOffsets[0].x == int(x) && last_blit.srcOffsets[0].y == int(y));
CHECK(last_blit.srcOffsets[1].x == int(x + w) && last_blit.srcOffsets[1].y == int(y + h));
CHECK(last_blit.dstOffsets[0].x == 3 && last_blit.dstOffsets[1].x == 1923);
}
blits = clears = 0;
blit_crop(nullptr, 1, sw, sh, crop, 2, VK_IMAGE_LAYOUT_GENERAL, 320, 180);
CHECK(blits == int(valid) && clears == int(!valid));
if (valid) CHECK(last_blit.dstOffsets[0].x == 0 && last_blit.dstOffsets[1].x == 320);
const bool recorded = framegen_record(nullptr, 1, sw, sh, crop);
CHECK(recorded == valid && initializations == int(valid) && sizes == int(valid));
if (valid) {
CHECK(allocated_w == w && allocated_h == h && dispatches == 2 && copies == 1);
CHECK(copy_pc[0] == int(x) && copy_pc[1] == int(y) && copy_pc[2] == int(w) && copy_pc[3] == int(h));
// Newly merged FSR3 receives the same validated extent; fallback still uses the shared crop.
fsr3fi::wanted = true;
CHECK(framegen_record(nullptr, 1, sw, sh, crop) && fsr_calls == 1 && fg.recorded->made == 1);
CHECK(dispatches == 4 && copies == 2);
fsr_result = 0;
CHECK(framegen_record(nullptr, 1, sw, sh, crop) && fsr_calls == 2 && fg.recorded->made == 1);
CHECK(dispatches == 9 && copies == 3);
CHECK(copy_pc[0] == int(x) && copy_pc[1] == int(y) && copy_pc[2] == int(w) && copy_pc[3] == int(h));
// (Triforce) The row's AMD FSR 4 is asked only at that value, and first. Its made frame stands (no blend passes);
// a new history gets no made frame and nothing blended in its place; a present it does not take falls to the
// shared interpolation, as a 2x
CHECK(fsr4_calls == 0);
const auto shown = [] { for (OutSet& set : fg.sets) set.busy = false; }; // (as the display thread leaves the sets)
opts.framegen = 9; fsr3fi::wanted = false;
shown();
fsr4_result = framegen_rows::MADE;
CHECK(framegen_record(nullptr, 1, sw, sh, crop) && fsr4_calls == 1 && fsr4_blend && fg.recorded->made == 1);
CHECK(dispatches == 11 && copies == 4 && fsr_calls == 2);
fsr4_result = framegen_rows::SKIP;
CHECK(framegen_record(nullptr, 1, sw, sh, crop) && fsr4_calls == 2 && fg.recorded->made == 0);
CHECK(dispatches == 13 && copies == 5);
// ((Triforce, FG-2) no picture without the HUD, or HDR Native: the game's frame alone, never the shared interpolation's
// frame in place of AMD's; the present's source handed on for the picture without the HUD's cut)
shown();
fsr4_result = framegen_rows::GAME;
CHECK(framegen_record(nullptr, 1, sw, sh, crop) && fsr4_calls == 3 && fg.recorded->made == 0);
CHECK(dispatches == 15 && copies == 6);
CHECK(fg.src_w == sw && fg.src_h == sh && fg.source.x == x && fg.source.y == y && fg.source.w == w && fg.source.h == h);
fsr4_result = framegen_rows::OWN;
CHECK(framegen_record(nullptr, 1, sw, sh, crop) && fsr4_calls == 4 && fg.recorded->made == 1);
CHECK(dispatches == 20 && copies == 7);
shown();
fg.have_prev = false; // (nothing before this frame: still handed over, as a frame with nothing to blend from)
CHECK(framegen_record(nullptr, 1, sw, sh, crop) && fsr4_calls == 5 && !fsr4_blend && fg.recorded->made == 0);
CHECK(dispatches == 22 && copies == 8);
opts.framegen = 2;
// ((Triforce) the fine vectors (vk_framegen.cpp fine_vectors), their pipelines made: a 2x frame is the copy, the
// quarter- and half-size luma, the coarse vectors and their median, the fine vectors, two picks and one made frame)
shown();
fg.have_prev = true;
fg.half.pipe = fg.fine_flow.pipe = fg.fine_pick.pipe = fg.fine_synth.pipe = 1;
CHECK(framegen_record(nullptr, 1, sw, sh, crop) && fg.recorded->made == 1);
CHECK(dispatches == 31 && copies == 9);
fg.half.pipe = fg.fine_flow.pipe = fg.fine_pick.pipe = fg.fine_synth.pipe = VK_NULL_HANDLE;
} else {
CHECK(dispatches == 0 && copies == 0 && !fg.recorded);
fsr3fi::wanted = true; fg.have_prev = true;
CHECK(!framegen_record(nullptr, 1, sw, sh, crop) && fsr_calls == 0);
CHECK(dispatches == 0 && copies == 0 && !fg.recorded);
}
// Execute the actual HDR consumer, keeping resources ready to isolate source validation.
hdr.init = true; hdr.failed = false; hdr.w = 1920; hdr.h = 1080; hdr.out.image = 77;
const int before = dispatches;
CHECK(framegen_hdr(nullptr, 1, sw, sh, crop, 1920, 1080) == (valid ? 77 : VK_NULL_HANDLE));
CHECK(dispatches == before + int(valid));
if (valid) {
CHECK(hdr_uv[0] == x / float(sw) && hdr_uv[1] == y / float(sh));
CHECK(hdr_uv[2] == (x + w) / float(sw) && hdr_uv[3] == (y + h) / float(sh));
}
}
int main() {
check(3440, 1440, nullptr, 0, 0, 3440, 1440);
const int ordinary[] = {160, 90, 2240, 1260}; check(2560, 1440, ordinary, 160, 90, 2240, 1260);
const int clipped[] = {2500, 1400, 100, 100}; check(2560, 1440, clipped, 2500, 1400, 60, 40);
const int overflow[] = {1, 1, INT_MAX, INT_MAX}; check(640, 480, overflow, 1, 1, 639, 479);
const int bad[][4] = {{-1, 0, 40, 40}, {0, -1, 40, 40}, {INT_MIN, 0, INT_MAX, 40},
{640, 0, 1, 1}, {0, 480, 1, 1}, {INT_MAX, INT_MAX, 1, 1}};
for (const auto& c : bad) check(640, 480, c, 0, 0, 0, 0);
const int disabled[][4] = {{INT_MIN, INT_MIN, 0, 10}, {-1, -1, 10, -1}, {20, 30, -1, -1}};
for (const auto& c : disabled) check(640, 480, c, 0, 0, 640, 480);
check(0, 480, nullptr, 0, 0, 0, 0);
check(640, 0, nullptr, 0, 0, 0, 0);
check(uint32_t(INT_MAX) + 1, 480, nullptr, 0, 0, 0, 0);
check(640, uint32_t(INT_MAX) + 1, nullptr, 0, 0, 0, 0);
uint32_t seed = 42;
for (int i = 0; i < 4096; ++i) {
seed = seed * 1664525u + 1013904223u; const int x = int(seed % 640);
seed = seed * 1664525u + 1013904223u; const int y = int(seed % 480);
const int c[] = {x, y, INT_MAX, INT_MAX};
check(640, 480, c, uint32_t(x), uint32_t(y), uint32_t(640-x), uint32_t(480-y));
}
reset(); blit_crop_to_rect(nullptr, 1, 640, 480, nullptr, 2, VK_IMAGE_LAYOUT_GENERAL, {0, 0, 0, 100});
CHECK(blits == 0 && clears == 0);
std::puts("PASS: presentation/capture/interpolation source crops agree; invalid input clears or skips, no signed overflow");
}