mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-19 16:20:01 -04:00
In-game mods (GameBanana browser, search and installs, with the Mask Salesman), code-patch and cheat mods, the devs' test18 to test45 work (full performance overlay, frame pacing on, steady frame rate, save export and import, bug capture, new options rows, Setup's Mods page and Add to Steam), occlusion queries and new defaults, Auto resolution that scales, replay helper threads and robust buffers off by default, the options in sections.
193 lines
11 KiB
C++
193 lines
11 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_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(); }
|
|
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;
|
|
int fsr3fi_frame(VkCommandBuffer, OutSet&, int, uint32_t w, uint32_t h, uint64_t sequence) {
|
|
CHECK(w == allocated_w && h == allocated_h && sequence > 0);
|
|
++fsr_calls; return fsr_result;
|
|
}
|
|
// (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; }
|
|
bool ensure_size(VkCommandBuffer, uint32_t w, uint32_t h) {
|
|
++sizes; allocated_w = w; allocated_h = h; 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);
|
|
fsr4_result = framegen_rows::OWN;
|
|
CHECK(framegen_record(nullptr, 1, sw, sh, crop) && fsr4_calls == 3 && fg.recorded->made == 1);
|
|
CHECK(dispatches == 18 && copies == 6);
|
|
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 == 4 && !fsr4_blend && fg.recorded->made == 0);
|
|
CHECK(dispatches == 20 && copies == 7);
|
|
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 == 29 && copies == 8);
|
|
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");
|
|
}
|