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.
177 lines
9.8 KiB
C++
177 lines
9.8 KiB
C++
#define _CRT_SECURE_NO_WARNINGS
|
|
#include <vulkan/vulkan.h>
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <deque>
|
|
#include <exception>
|
|
#include <map>
|
|
#include <mutex>
|
|
#include <set>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <unordered_map>
|
|
#include <utility>
|
|
#include <vector>
|
|
#include "barrier_rules.h"
|
|
#include "desc_pool_grow.h" // ((Triforce, 11-22, DESC-33) a slot's pool at half, whole once a recording ran out)
|
|
namespace desc_pool_grow = mkwvk::desc_pool_grow;
|
|
static void require(bool ok,const char* why) { if(!ok) { std::fprintf(stderr,"FAIL: %s\n",why); std::exit(1); } }
|
|
struct Allocation { std::vector<uint8_t> cpu,visible; };
|
|
using VmaAllocation=Allocation*;
|
|
struct { VkDevice device{}; VkQueue queue{}; VkPhysicalDeviceProperties props{}; std::mutex present_mu; } dev;
|
|
std::map<VkBuffer,Allocation> buffers;
|
|
uintptr_t serial=100;
|
|
template<class T> T handle() { return reinterpret_cast<T>(++serial); }
|
|
bool coherent=false,fail_buffer=false;
|
|
unsigned flushed=0,queued=0;
|
|
std::mutex par_mutex;
|
|
struct ParLock { std::lock_guard<std::mutex> lock{par_mutex}; };
|
|
void logf(const char*,...) {}
|
|
bool create_buffer(uint64_t size,VkBufferUsageFlags,bool,VkBuffer& buffer,VmaAllocation& allocation,void** mapped,bool=false) {
|
|
if(fail_buffer) return false;
|
|
buffer=handle<VkBuffer>(); auto& a=buffers[buffer]; a.cpu.assign((size_t)size,0x9a); a.visible.assign((size_t)size,0xc3);
|
|
allocation=&a; *mapped=a.cpu.data(); return true;
|
|
}
|
|
void destroy_buffer(VkBuffer buffer,VmaAllocation allocation) { require(&buffers.at(buffer)==allocation,"allocation owner"); buffers.erase(buffer); }
|
|
void flush_mapped(VmaAllocation allocation,uint64_t size) {
|
|
if(!size)return;
|
|
require(allocation && size<=allocation->cpu.size(),"valid stream flush range");
|
|
std::copy_n(allocation->cpu.begin(),(size_t)size,allocation->visible.begin()); flushed++;
|
|
}
|
|
/* RES */
|
|
std::deque<RecordingRes> res;
|
|
thread_local RecordingRes* cur=nullptr;
|
|
const uint64_t kStream=1024;
|
|
constexpr VkBufferUsageFlags kStreamUse=VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT|VK_BUFFER_USAGE_STORAGE_BUFFER_BIT|VK_BUFFER_USAGE_TRANSFER_SRC_BIT|VK_BUFFER_USAGE_TRANSFER_DST_BIT|VK_BUFFER_USAGE_INDEX_BUFFER_BIT|VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
|
|
std::atomic<uint64_t> stream_extra{0},stream_grown{0},stream_peak{0};
|
|
bool stream_grow_off() { return false; }
|
|
struct BufferRef { VkBuffer buffer{}; uint64_t offset=0,size=0,address=0; };
|
|
void draw_flush_recording(int slot);
|
|
/* STREAM */
|
|
VkCommandBuffer rec_cmd{};
|
|
uint64_t rec_value=0,targets_serial=0;
|
|
VkBuffer zero_buffer{}; VmaAllocation zero_alloc{};
|
|
VkDescriptorPool make_pool(uint32_t = 0) { return handle<VkDescriptorPool>(); }
|
|
bool pool_grow_on() { return true; } // ((Triforce, 11-22, DESC-33) vk_draw.cpp: RX_VK_DESC_POOL_GROW and its counts)
|
|
desc_pool_grow::Took pool_most;
|
|
uint64_t pools_added = 0, pools_whole = 0;
|
|
VKAPI_ATTR VkResult VKAPI_CALL vkResetDescriptorPool(VkDevice,VkDescriptorPool,VkDescriptorPoolResetFlags) { return VK_SUCCESS; }
|
|
VKAPI_ATTR void VKAPI_CALL vkDestroyDescriptorPool(VkDevice,VkDescriptorPool,const VkAllocationCallbacks*) {}
|
|
struct Pass {}; Pass& pass() { static Pass p;return p; }
|
|
bool work_since_barrier=false,last_dyn_valid=false,last_desc_valid=false,last_vb_valid=false;
|
|
VkPipeline last_pipeline{};
|
|
struct { bool on=false; } vertex_set,last_ib;
|
|
void descriptors_disturbed() {}
|
|
std::unordered_map<int,int>& handle_hits() { static std::unordered_map<int,int> h;return h; }
|
|
struct BankCache { uint64_t at=0; };
|
|
std::array<std::array<BankCache,2>,2>& bank_cache() { static std::array<std::array<BankCache,2>,2> b;return b; }
|
|
void make_dummies() {}
|
|
thread_local uint64_t desc_rec=0; // (vk_draw.cpp: the recording the kept descriptors are of)
|
|
/* FLUSH */
|
|
/* BEGIN */
|
|
struct Recording { int slot;VkCommandBuffer cmd,upload,init; VkFence fence;uint64_t value=1,batch=0;bool submitted=false; };
|
|
struct Expected { BufferRef ref;std::vector<uint8_t> bytes; };
|
|
std::map<VkCommandBuffer,std::vector<Expected>> expected;
|
|
std::vector<int> submitted_slots;
|
|
std::map<VkCommandBuffer,int> slots;
|
|
bool texture_uploads_recorded() { return true; }
|
|
void gprof_submitted(int,bool) {}
|
|
void gpu_time_end(Recording&) {}
|
|
VKAPI_ATTR VkResult VKAPI_CALL vkEndCommandBuffer(VkCommandBuffer) { return VK_SUCCESS; }
|
|
VkSemaphore timeline{},rec_timeline{};
|
|
uint64_t rec_seq=0;
|
|
std::mutex rec_map_mu;
|
|
std::multimap<uint64_t,uint64_t> value_rec;
|
|
std::set<uint64_t> rec_open_values;
|
|
struct { std::vector<VkCommandBufferSubmitInfo> cbs;uint64_t signal=0,rec=0;bool any_signal=false;size_t recs=0; } pend;
|
|
uint64_t batch_next=0;std::chrono::steady_clock::time_point pend_since;
|
|
bool batch=false;bool batch_on() { return batch; }
|
|
namespace barrier_rules = mkwvk::barrier_rules; // (the held submits' rule, gpu/vk/barrier_rules.h: RX_VK_BATCH=1 here)
|
|
barrier_rules::Batch batch_way() { return batch ? barrier_rules::Batch::Hold : barrier_rules::Batch::Off; }
|
|
int submit_depth=0;
|
|
int flush_reason=0;constexpr int FR_OTHER=0,FR_CAP=1,FR_IDLE=2;
|
|
bool batch_idle_on() { return false; } // ((Triforce, 11-22, SYNC-09) RX_VK_BATCH_IDLE: off, as unset; not this test's)
|
|
bool batch_gpu_idle() { return false; }
|
|
bool event_writes_held(VkCommandBuffer) { return false; } // (no event's write held: RX_VK_EVENT_BARRIER's, not this test's)
|
|
void split_part_submitted(VkCommandBuffer,bool) {} // (RX_VK_SPLIT_BARRIER's count)
|
|
void end_pass(VkCommandBuffer) { require(false,"a pass ended at a submit"); }
|
|
void full_barrier(VkCommandBuffer,const char*) { require(false,"a barrier at a submit"); }
|
|
void device_lost() { require(false,"unexpected device loss"); }
|
|
#define PROF(x) ((void)0)
|
|
VKAPI_ATTR VkResult VKAPI_CALL vkQueueSubmit2(VkQueue,uint32_t count,const VkSubmitInfo2* submits,VkFence) {
|
|
for(unsigned i=0;i<count;i++) for(unsigned j=0;j<submits[i].commandBufferInfoCount;j++) {
|
|
auto cmd=submits[i].pCommandBufferInfos[j].commandBuffer;
|
|
if(slots.count(cmd))submitted_slots.push_back(slots.at(cmd));
|
|
for(auto& e:expected[cmd]) {
|
|
auto& a=buffers.at(e.ref.buffer);auto& seen=coherent?a.cpu:a.visible;
|
|
require(e.ref.offset+e.bytes.size()<=seen.size(),"consumer bounds");
|
|
require(std::equal(e.bytes.begin(),e.bytes.end(),seen.begin()+e.ref.offset),"GPU draw input must see the CPU-authored bytes");
|
|
}
|
|
}
|
|
queued++;return VK_SUCCESS;
|
|
}
|
|
void queue_flush_locked() {
|
|
if(pend.cbs.empty())return;
|
|
VkSubmitInfo2 si{VK_STRUCTURE_TYPE_SUBMIT_INFO_2};si.commandBufferInfoCount=(uint32_t)pend.cbs.size();si.pCommandBufferInfos=pend.cbs.data();
|
|
vkQueueSubmit2(dev.queue,1,&si,VK_NULL_HANDLE);pend.cbs.clear();
|
|
}
|
|
/* SUBMIT */
|
|
struct Job { Recording* r;int inits=0;bool uploads=true; };
|
|
void submit_segments(std::vector<Job>& jobs) {
|
|
bool init_lost=false;
|
|
for(auto& job:jobs) { Job* j=&job;
|
|
/* SEGMENT */
|
|
}
|
|
}
|
|
static Recording recording(int slot) {
|
|
Recording r{slot,handle<VkCommandBuffer>(),handle<VkCommandBuffer>(),handle<VkCommandBuffer>(),handle<VkFence>()};
|
|
slots[r.cmd]=slot; return r;
|
|
}
|
|
static void write(Recording& r,size_t bytes,unsigned seed) {
|
|
std::vector<uint8_t> data(bytes);for(size_t i=0;i<bytes;i++)data[i]=uint8_t(i*37+seed*61);
|
|
BufferRef ref=stream_copy(data.data(),data.size());require(ref.buffer!=VK_NULL_HANDLE && ref.size==bytes,"stream copy accepted");
|
|
expected[r.cmd].push_back({ref,std::move(data)});
|
|
}
|
|
int main(int argc,char**argv) {
|
|
require(argc==3,"fixture arguments");std::string mode=argv[1];coherent=std::string(argv[2])=="coherent";
|
|
dev.props.limits.minUniformBufferOffsetAlignment=256;dev.props.limits.minStorageBufferOffsetAlignment=64;
|
|
// (first-stream-failure: the slot's first stream cannot be made, as when host-visible memory runs out at a recording's start)
|
|
fail_buffer=mode=="first-stream-failure";
|
|
Recording a=recording(0);draw_begin_recording(0,a.cmd,1);
|
|
fail_buffer=false;
|
|
if(mode=="first-stream-failure") {
|
|
require(res[0].streams.empty() && !res[0].stream && res[0].si==0,"the slot has no stream");
|
|
submit_recording(a);require(!flushed && a.submitted,"a recording without a stream submits with nothing to flush");
|
|
std::puts("PASS: a recording whose first stream could not be made");return 0;
|
|
}
|
|
if(mode=="empty") { submit_recording(a);require(!flushed,"empty recording must not flush old bytes");return 0; }
|
|
write(a,701,1);
|
|
if(mode=="grow" || mode=="reuse") {
|
|
write(a,3107,2);write(a,1100011,3);require(res[0].si==2,"exercise multiple grown streams");
|
|
}
|
|
if(mode=="allocation-failure") {
|
|
fail_buffer=true;std::vector<uint8_t> large(3000,9);auto missing=stream_copy(large.data(),large.size());fail_buffer=false;
|
|
require(!missing.buffer && res[0].si==0,"failed growth must leave the current stream usable");
|
|
}
|
|
if(mode=="segments" || mode=="failed-segment") {
|
|
Recording b=recording(1),c=recording(2);
|
|
std::thread worker([&]{draw_begin_recording(1,b.cmd,1);write(b,997,4);write(b,3009,5);});worker.join();
|
|
draw_begin_recording(2,c.cmd,1);write(c,487,6);require(cur==&res[2],"main thread current slot differs from submitted worker slots");
|
|
std::vector<Job> jobs{{&a,1,true},{&b,mode=="failed-segment"?-1:0,true},{&c,0,false}};submit_segments(jobs);
|
|
require(submitted_slots==(mode=="failed-segment"?std::vector<int>{0}:std::vector<int>{0,1,2}),"segment failure/ordering contract");return 0;
|
|
}
|
|
batch=mode=="batch";submit_recording(a);
|
|
if(batch) {require(queued==0,"batch retained before queue use");queue_flush_locked();}
|
|
if(mode=="reuse") {
|
|
expected[a.cmd].clear();draw_begin_recording(0,a.cmd,2);require(res[0].si==0 && res[0].used==0 && res[0].streams.size()==3,"real begin resets used range and retains grown streams");
|
|
write(a,69,7);submit_recording(a);
|
|
}
|
|
require(a.submitted,"healthy recording submitted");std::puts("PASS: production draw stream input visibility and submission paths");
|
|
}
|