Files
Triforce-Source/gpu/vk/vk_framegen.cpp
ruckus7382sfgsgdf 6c7117635c Triforce v0.6.0 "Sage Of Power"
Faster frames (the replay's memory work, held submits, helper threads, collection and capture cuts), the game letting old areas go (QueryMemoryInfo), the 0.5.2 stutter fixed, AMD RX 5000/6000 and integrated-GPU fixes, the GPU device chosen by name, the in-game menus and launcher reworked, controller, Linux and benchmark fixes.
2026-11-20 17:06:21 -06:00

1366 lines
80 KiB
C++

// Frame interpolation (RX_VK_FRAMEGEN=1; off by default): frames made between the game's, from its last two, shown in
// between by a display thread, so a game at 60 fps is shown at 120 (RX_VK_FRAMEGEN_MULT=3: 180). The WebGPU
// renderer's (gpu/render/rx_framegen.cpp, whose comments explain it), on Vulkan:
// 1. each frame is copied (it may be drawn over next frame) and reduced to quarter-size luma;
// 2. block matching: a vector per 16x16 pixels within +-32 pixels (RX_VK_FRAMEGEN_RADIUS quarter texels, default
// 8), then a 3x3 median of them;
// 3. each in-between frame at time t blends the last frame moved back along t and the new one forward along 1 - t.
// (Triforce) Unless RX_VK_FRAMEGEN_FINE=0, the vectors of 2. are only the start (fine_vectors): one per 8x8 pixels, to a
// fraction of a pixel (fg_half, fg_fine_flow, fg_fine_pick), and 3. picks one motion per pixel (fg_fine_synth).
// Recorded into the present's own recording, after the game's work; the display thread shows the made frames and
// then the game's, evenly over the game's recent frame time, about one frame behind the game. Loading hitches
// (frames over 100 ms apart) are not interpolated.
#include <algorithm>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstring>
#include <deque>
#include <mutex>
#include <thread>
#include <cmath>
#include "vk_framegen.h"
#include "framegen_rows.h" // (Triforce: the Frame generation row's values, AMD FSR 4 among them)
#include "vk_dlssg.h"
#include "vk_fsr3_inputs.h" // (AMD FSR 3.1 frame interpolation's depth and motion)
#include "vk_fsr3fi.h"
#include "vk_fsr4.h" // (Triforce: AMD FSR 4's frame generation, fsr4fg)
#include "vk_menu.h"
#include "vk_pace.h" // (Triforce: whether RivaTuner holds the frames this thread presents)
#include "vk_present_rect.h"
#include "vk_temporal.h" // ((Triforce, S1) temporal_sharpens_3d: the upscaler sharpened the 3D before the HUD)
#include "vk_texture.h"
#include "sharpen_rules.h" // ((Triforce, S1, S3-S6) the window pass's Sharpness and filter, the PQ picture's shaders)
#include <vector>
#include "shaders/fg_copy_comp_spv.h"
#include "shaders/fg_fine_flow_comp_spv.h"
#include "shaders/fg_fine_pick_comp_spv.h"
#include "shaders/fg_fine_synth_comp_spv.h"
#include "shaders/fg_flow_comp_spv.h"
#include "shaders/fg_half_comp_spv.h"
#include "shaders/fg_luma_comp_spv.h"
#include "shaders/fg_median_comp_spv.h"
#include "shaders/fg_scale_comp_spv.h"
#include "shaders/fg_synth_comp_spv.h"
#include "shaders/fsr1_easu_comp_spv.h"
#include "shaders/fsr1_rcas_comp_spv.h"
#include "shaders/hdr_encode_comp_spv.h"
#include "vk_work.h" // (after the others: counts the commands that do GPU work, for full_barrier)
#ifndef CREATE_WAITABLE_TIMER_HIGH_RESOLUTION // (older Windows SDKs lack it: Windows 10 1803 and later take it)
#define CREATE_WAITABLE_TIMER_HIGH_RESOLUTION 0x00000002
#endif
namespace mkwvk {
int display_refresh_mhz(); // (vk_main.cpp: the window's display, mHz; outside the unnamed namespace, where it is defined)
extern std::atomic<uint32_t> present_kind; // (vk_replay.cpp: the frame log's)
extern std::atomic<bool> present_native_pq; // (vk_replay.cpp: the game's picture is HDR10 codes, HDR Native)
namespace {
constexpr int kMaxMult = 8; // (up to 8x: players asked; the made frames are allocated for the multiplier chosen only)
struct Img {
VkImage image = VK_NULL_HANDLE;
VmaAllocation alloc = nullptr;
VkImageView view = VK_NULL_HANDLE;
};
struct OutSet {
Img frame[kMaxMult];
int made = 0; // (frame[0..made) are made frames; frame[made] is the game's)
bool busy = false;
// (Auto: frame[0..shown) each at its own time, due[k] (steady clock), game[k] the game's own frame; shown < 0: the
// fixed multipliers' frames, made + 1 of them, shown evenly)
int shown = -1;
int64_t due[kMaxMult] = {};
bool game[kMaxMult] = {};
};
struct Job {
OutSet* set;
int64_t at;
uint64_t value; // (the recording that makes its frames)
// ((Triforce) set null: the held picture of the hold of the last picture, shown once: framegen_queue_held)
VkImage held = VK_NULL_HANDLE;
VkImageView held_view = VK_NULL_HANDLE;
uint32_t held_w = 0, held_h = 0;
int crop[4] = {0, 0, 0, 0};
bool has_crop = false;
};
struct Pipe {
VkDescriptorSetLayout set = VK_NULL_HANDLE;
VkPipelineLayout layout = VK_NULL_HANDLE;
VkPipeline pipe = VK_NULL_HANDLE;
};
struct State {
bool init = false;
std::atomic<bool> failed{false}; // also read by Reflex's game-thread eligibility check
uint32_t w = 0, h = 0;
Pipe copy, luma, flow, median, synth;
Pipe half, fine_flow, fine_pick, fine_synth; // ((Triforce) the fine vectors: fine_vectors(); null in the old way)
VkSampler nearest = VK_NULL_HANDLE, linear = VK_NULL_HANDLE;
Img frames[2], lumas[2], flows[2];
Img halfs[2], fines[2]; // ((Triforce) the fine vectors': each frame's half-size luma; the vectors, made and picked)
OutSet sets[3]; // (one shown, one waiting its place on the line, one being made: the pacer never holds the game back)
int cur = 0, next_set = 0;
bool have_prev = false;
int64_t last_at = 0;
std::mutex mu;
std::condition_variable cv;
std::deque<Job> jobs;
bool thread_started = false;
OutSet* recorded = nullptr; // (the set the last framegen_record filled, for framegen_queue)
int64_t recorded_at = 0;
int mult = 0; // (the multiplier the sets' frames were made for)
int64_t game_ns = 0; // (Auto: the game's frame time, smoothed: the delay its frames are shown at)
int64_t slot_next = 0; // (Auto: the display's next refresh to be given a frame, on the game's timeline)
// ((Triforce, FG-5) the frames' format: RGBA8, or for HDR Native's 10-bit picture (PQ codes) A2B10G10R10 (RGBA16F where the
// card stores no A2B10G10R10), so the game's frames and the made ones keep their 10 bits to the window: before, every frame
// shown was cut to 8 bits there. copy, synth and fine_synth are pipes_format's (their rgba8 storage images declared as the
// other format, sharpen_rules.h storage_format_patch: no new shader), the other format's kept in alt_*)
VkFormat format = VK_FORMAT_R8G8B8A8_UNORM, pipes_format = VK_FORMAT_R8G8B8A8_UNORM, alt_format = VK_FORMAT_UNDEFINED;
Pipe alt_copy, alt_synth, alt_fine_synth;
bool hdr_failed = false; // (the 10-bit pipes could not be made: HDR Native's frames stay 8 bits, said once)
// ((Triforce, FG-2) this present's window texture size and its crop, for the picture without the HUD: fsr4_frame)
uint32_t src_w = 0, src_h = 0;
PresentSourceRect source{};
} fg;
// Used only by present_image, whose lock serializes direct and interpolated presents.
struct ScaleState {
bool init = false, failed = false;
Pipe pipe;
Pipe easu, rcas; // (Triforce: Upscaling = AMD FSR 1, shaders/fsr1_*.hlsl)
// ((Triforce, S5) the same three for HDR Native's PQ picture, their storage images rgba16f (sharpen_rules.h
// storage_format_patch): made at its first such frame; pq_init -1 when the filter's could not be)
Pipe pipe16, easu16, rcas16;
int pq_init = 0;
VkFormat format = VK_FORMAT_R8G8B8A8_UNORM; // (the four images': RGBA8, or RGBA16F for a PQ picture)
VkSampler linear = VK_NULL_HANDLE, nearest = VK_NULL_HANDLE;
Img cropped, scaled, small; // (small: the picture at 1/8, for ultrawide's blurred menu sides)
Img easu_img; // (FSR 1: EASU's output, RCAS's input, the window's size)
uint32_t cropped_w = 0, cropped_h = 0, scaled_w = 0, scaled_h = 0, small_w = 0, small_h = 0;
} scale;
struct HdrState { // (hookshot) the HDR10 pass (framegen_hdr)
bool init = false, failed = false;
Pipe pipe;
Img out;
uint32_t w = 0, h = 0;
} hdr;
int64_t now_ns() {
return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
}
// (Triforce) The fine vectors (shaders/fg_fine_*.comp), the way the frames are made unless RX_VK_FRAMEGEN_FINE=0. The old
// way (fg_flow, fg_median, fg_synth) moved each pixel by a blend of the vectors of the four 16x16 blocks around it, each a
// whole number of quarter-size texels, 4 pixels: a straight line moving 6 pixels was moved 4 by some blocks and 8 by others
// and came out bent and broken, where the two frames disagreed a pixel took the nearer frame unmoved beside its moved
// neighbours, and a pixel alike in both frames was taken as still, so a thin line moving over a plain surface was not
// drawn at all between the game's frames ("frame gen makes straight line geometry look much worse", the owner, on 4K and
// ultrawide, where a block is 64 screen pixels). tests/test_framegen_lines.py measures both ways on moving lines. Read once
bool fine_vectors() {
static const bool on = !getenv("RX_VK_FRAMEGEN_FINE") || atoi(getenv("RX_VK_FRAMEGEN_FINE")) != 0;
return on;
}
// (how much the coarse search's vector must earn its length, fg_flow.comp's BIAS: 1 in the old way; the fine vectors'
// blocks try the coarse vectors of the blocks around on their own pixels, and less of a pull to none found the motion
// of a plain surface more often. tests/test_framegen_lines.py runs with this value)
constexpr float kFineBias = 0.3f;
// (hookshot) Frame generation's Auto (the menu's 1): a frame for each refresh of the display, no more. Each game frame
// is given the display's refreshes that fall between it and the game's frame before (on the game's own timeline, a
// refresh period apart), and each of them a frame made at its place between the two: 45 fps on a 60 Hz display makes one
// frame for every three of the game's (3 of 4 shown are made, at 3/4, 1/2, 1/4), on 120 Hz two or three a frame; at
// 60 fps on 60 Hz none (a refresh near a game frame shows it), and faster than the display a game frame may be given no refresh and is not shown. Shown one
// game frame time later, each at its refresh's time. The sets' frames are made once for the most a game frame may be
// given (the refresh over 30 fps, 2 to 4). An unknown refresh counts as 60 Hz.
int auto_cap() {
const int hz = (display_refresh_mhz() + 500) / 1000;
return std::clamp(hz > 0 ? (hz + 29) / 30 : 2, 2, 4);
}
int multiplier() { // (the Insert menu; 1 Auto (its most); the game's frames alone at 0)
if (opts.framegen == framegen_rows::FSR4) return 2; // (Triforce: AMD FSR 4, the row's last choice: one made frame)
return opts.framegen == 1 ? auto_cap() : std::clamp(opts.framegen, 1, kMaxMult);
}
/// Auto: the display's refreshes in the game frame's interval (last, now], next the first not given a frame yet (moved
/// past them). Each one's place between the two game frames in t (1: this one) and its time in at; most of them at the
/// most, those past it skipped. A timeline outside the interval (the first frame, a hitch) starts again at now.
int auto_slots(int64_t last, int64_t now, int64_t period, int64_t& next, float* t, int64_t* at, int most) {
if (now <= last || period <= 0) return 0;
if (next <= last || next > now + period) next = now;
int n = 0;
for (; next <= now; next += period)
if (n < most) t[n] = (float)(next - last) / (float)(now - last), at[n++] = next;
// A refresh within a quarter period of the game's frame shows that frame itself: at the game's rate the frames come
// a little early or late (jitter), and without this nearly every refresh got a made frame at t near 1 and some game
// frames none (their neighbour two). The timeline is pulled an eighth of the way to the frame, so that a game at the
// display's rate (or a whole fraction of it) stays on it while the two clocks drift apart, and other rates hardly
// change how many frames the display is given
const int64_t snap = period / 4; // (not "near": windows.h defines it empty)
if (next - now < snap) {
n = std::min(n + 1, most);
t[n - 1] = 1.0f, at[n - 1] = now, next += period + (now - next) / 8;
} else if (n > 0 && now - (next - period) < snap) {
t[n - 1] = 1.0f, at[n - 1] = now, next += (now - (next - period)) / 8;
}
return n;
}
void drop_pipe(Pipe& p) {
if (p.pipe) vkDestroyPipeline(dev.device, p.pipe, nullptr);
if (p.layout) vkDestroyPipelineLayout(dev.device, p.layout, nullptr);
if (p.set) vkDestroyDescriptorSetLayout(dev.device, p.set, nullptr);
p = {};
}
/// A compute pipeline over bindings of these types (push descriptors), push constants of push bytes
bool make_pipe(Pipe& p, const uint32_t* code, size_t bytes, std::initializer_list<VkDescriptorType> types, uint32_t push,
const VkSpecializationInfo* spec = nullptr) {
Pipe made;
const auto undo = [&] { drop_pipe(made); return false; };
std::vector<VkDescriptorSetLayoutBinding> b;
for (VkDescriptorType t : types) b.push_back({(uint32_t)b.size(), t, 1, VK_SHADER_STAGE_COMPUTE_BIT, nullptr});
VkDescriptorSetLayoutCreateInfo li{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
li.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR;
li.bindingCount = (uint32_t)b.size();
li.pBindings = b.data();
if (vkCreateDescriptorSetLayout(dev.device, &li, nullptr, &made.set) != VK_SUCCESS) return false;
const VkPushConstantRange pr{VK_SHADER_STAGE_COMPUTE_BIT, 0, std::max(push, 4u)};
VkPipelineLayoutCreateInfo pi{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO};
pi.setLayoutCount = 1;
pi.pSetLayouts = &made.set;
pi.pushConstantRangeCount = push ? 1 : 0;
pi.pPushConstantRanges = &pr;
if (vkCreatePipelineLayout(dev.device, &pi, nullptr, &made.layout) != VK_SUCCESS) {
made.layout = VK_NULL_HANDLE;
return undo();
}
VkShaderModuleCreateInfo mi{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO};
mi.codeSize = bytes;
mi.pCode = code;
VkShaderModule m = VK_NULL_HANDLE;
if (vkCreateShaderModule(dev.device, &mi, nullptr, &m) != VK_SUCCESS) return undo();
VkComputePipelineCreateInfo ci{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO};
ci.stage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
ci.stage.stage = VK_SHADER_STAGE_COMPUTE_BIT;
ci.stage.module = m;
ci.stage.pName = "main";
ci.stage.pSpecializationInfo = spec;
ci.layout = made.layout;
const VkResult r = vkCreateComputePipelines(dev.device, VK_NULL_HANDLE, 1, &ci, nullptr, &made.pipe);
vkDestroyShaderModule(dev.device, m, nullptr);
if (r != VK_SUCCESS) return undo();
p = made;
return true;
}
// The optional interpolation is disabled after setup fails: retain none of its unused setup objects.
void drop_setup() {
for (Pipe* p : {&fg.copy, &fg.luma, &fg.flow, &fg.median, &fg.synth, &fg.half, &fg.fine_flow, &fg.fine_pick, &fg.fine_synth,
&fg.alt_copy, &fg.alt_synth, &fg.alt_fine_synth})
drop_pipe(*p);
fg.pipes_format = VK_FORMAT_R8G8B8A8_UNORM, fg.alt_format = VK_FORMAT_UNDEFINED;
if (fg.nearest) vkDestroySampler(dev.device, fg.nearest, nullptr);
if (fg.linear) vkDestroySampler(dev.device, fg.linear, nullptr);
fg.nearest = fg.linear = VK_NULL_HANDLE;
}
bool init() {
if (fg.init) return !fg.failed;
fg.init = true;
fg.failed = true;
const auto undo = [] { drop_setup(); return false; };
if (!dev.push_descriptor || !vkCmdPushDescriptorSetKHR) return false;
const VkDescriptorType S = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, I = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
const int radius = getenv("RX_VK_FRAMEGEN_RADIUS") ? std::clamp(atoi(getenv("RX_VK_FRAMEGEN_RADIUS")), 1, 16) : 8;
const bool fine = fine_vectors();
const struct { int radius; float bias; } sc{radius, fine ? kFineBias : 1.0f}; // (fg_flow.comp's RADIUS and BIAS)
const VkSpecializationMapEntry me[2] = {{0, 0, 4}, {1, 4, 4}};
const VkSpecializationInfo spec{2, me, sizeof sc, &sc};
if (!make_pipe(fg.copy, FG_COPY_COMP_SPV, sizeof FG_COPY_COMP_SPV, {S, I}, 16) ||
!make_pipe(fg.luma, FG_LUMA_COMP_SPV, sizeof FG_LUMA_COMP_SPV, {S, I}, 0) ||
!make_pipe(fg.flow, FG_FLOW_COMP_SPV, sizeof FG_FLOW_COMP_SPV, {S, S, I}, 0, &spec) ||
!make_pipe(fg.median, FG_MEDIAN_COMP_SPV, sizeof FG_MEDIAN_COMP_SPV, {S, I}, 0))
return undo();
// ((Triforce) the fine vectors' passes in place of fg_synth's, or fg_synth's alone in the old way)
if (fine ? !make_pipe(fg.half, FG_HALF_COMP_SPV, sizeof FG_HALF_COMP_SPV, {S, I}, 0) ||
!make_pipe(fg.fine_flow, FG_FINE_FLOW_COMP_SPV, sizeof FG_FINE_FLOW_COMP_SPV, {S, S, S, S, I, S, S}, 0) ||
!make_pipe(fg.fine_pick, FG_FINE_PICK_COMP_SPV, sizeof FG_FINE_PICK_COMP_SPV, {S, S, S, I}, 0) ||
!make_pipe(fg.fine_synth, FG_FINE_SYNTH_COMP_SPV, sizeof FG_FINE_SYNTH_COMP_SPV, {S, S, S, I}, 4)
: !make_pipe(fg.synth, FG_SYNTH_COMP_SPV, sizeof FG_SYNTH_COMP_SPV, {S, S, S, I}, 4))
return undo();
VkSamplerCreateInfo si{VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO};
si.addressModeU = si.addressModeV = si.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
si.maxLod = 0.25f;
if (vkCreateSampler(dev.device, &si, nullptr, &fg.nearest) != VK_SUCCESS) {
fg.nearest = VK_NULL_HANDLE;
return undo();
}
si.magFilter = si.minFilter = VK_FILTER_LINEAR;
if (vkCreateSampler(dev.device, &si, nullptr, &fg.linear) != VK_SUCCESS) {
fg.linear = VK_NULL_HANDLE;
return undo();
}
fg.failed = false;
if (opts.framegen == 1)
logf("vk: frame interpolation ready: Auto, a frame for each of the display's refreshes (%d Hz; up to %d a game frame)",
(display_refresh_mhz() + 500) / 1000, multiplier());
else
logf("vk: frame interpolation ready: %dx the game's frames (made from its last two, shown evenly)", multiplier());
logf("vk: frame interpolation: %s", fine ? "fine vectors, one per 8x8 pixels (RX_VK_FRAMEGEN_FINE=0: the old 16x16 blocks)"
: "the old vectors, one per 16x16 pixels (RX_VK_FRAMEGEN_FINE=0)");
return true;
}
bool init_scale() {
if (scale.init) return !scale.failed;
scale.init = scale.failed = true;
if (!dev.push_descriptor || !vkCmdPushDescriptorSetKHR) return false;
const VkDescriptorType S = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, I = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
if (make_pipe(scale.pipe, FG_SCALE_COMP_SPV, sizeof FG_SCALE_COMP_SPV, {S, I, S}, 20)) {
VkSamplerCreateInfo si{VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO};
si.addressModeU = si.addressModeV = si.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
si.maxLod = 0.25f;
si.magFilter = si.minFilter = VK_FILTER_LINEAR;
if (vkCreateSampler(dev.device, &si, nullptr, &scale.linear) == VK_SUCCESS) {
scale.failed = false;
// (Triforce: AMD FSR 1's two passes and RCAS's point sampler. Any of them missing: Upscaling 3 runs the filter above)
si.magFilter = si.minFilter = VK_FILTER_NEAREST;
if (!make_pipe(scale.easu, FSR1_EASU_COMP_SPV, sizeof FSR1_EASU_COMP_SPV, {S, I}, 16) ||
!make_pipe(scale.rcas, FSR1_RCAS_COMP_SPV, sizeof FSR1_RCAS_COMP_SPV, {S, I}, 16) ||
vkCreateSampler(dev.device, &si, nullptr, &scale.nearest) != VK_SUCCESS) {
drop_pipe(scale.easu), drop_pipe(scale.rcas);
scale.nearest = VK_NULL_HANDLE;
logf("vk: AMD FSR 1 could not be set up: Upscaling 3 runs as Catmull-Rom");
}
return true;
}
scale.linear = VK_NULL_HANDLE;
}
drop_pipe(scale.pipe);
return false;
}
/// ((Triforce, S5) The window pass's three shaders for HDR Native's PQ picture (the game's A2B10G10R10 codes): their rgba8
/// storage images declared rgba16f (sharpen_rules.h storage_format_patch), so the picture goes through RGBA16F images and
/// keeps its 10 bits. Before, it went through RGBA8 ones into the HDR10 window. False: the filter's could not be made (then the
/// PQ picture is copied to the window as it is); FSR 1's two missing: Catmull-Rom in its place, as with RGBA8
bool init_scale_pq() {
if (scale.pq_init) return scale.pq_init > 0;
scale.pq_init = -1;
if (!init_scale()) return false;
const VkDescriptorType S = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, I = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
const auto patched = [](const uint32_t* code, size_t bytes) {
std::vector<uint32_t> w(code, code + bytes / 4);
return sharpen_rules::storage_format_patch(w, sharpen_rules::kSpvRgba8, sharpen_rules::kSpvRgba16f) ? w
: std::vector<uint32_t>{};
};
const std::vector<uint32_t> f = patched(FG_SCALE_COMP_SPV, sizeof FG_SCALE_COMP_SPV);
if (f.empty() || !make_pipe(scale.pipe16, f.data(), f.size() * 4, {S, I, S}, 20)) {
logf("vk: HDR Native: the window's filter for its 10-bit picture could not be made: the picture goes to the window as it is");
return false;
}
scale.pq_init = 1;
const std::vector<uint32_t> e = patched(FSR1_EASU_COMP_SPV, sizeof FSR1_EASU_COMP_SPV);
const std::vector<uint32_t> r = patched(FSR1_RCAS_COMP_SPV, sizeof FSR1_RCAS_COMP_SPV);
if (!scale.easu.pipe || e.empty() || r.empty() || !make_pipe(scale.easu16, e.data(), e.size() * 4, {S, I}, 16) ||
!make_pipe(scale.rcas16, r.data(), r.size() * 4, {S, I}, 16))
drop_pipe(scale.easu16), drop_pipe(scale.rcas16);
logf("vk: HDR Native: the window's filter takes the 10-bit picture through RGBA16F images%s",
scale.easu16.pipe ? "" : " (AMD FSR 1 as Catmull-Rom)");
return true;
}
bool make_img(Img& i, uint32_t w, uint32_t h, VkFormat f, VkImageUsageFlags usage) {
Img made;
VkImageCreateInfo ci{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
ci.imageType = VK_IMAGE_TYPE_2D;
ci.format = f;
ci.extent = {w, h, 1};
ci.mipLevels = ci.arrayLayers = 1;
ci.samples = VK_SAMPLE_COUNT_1_BIT;
ci.usage = usage;
if (!create_image(ci, made.image, made.alloc)) return false;
VkImageViewCreateInfo vi{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO};
vi.image = made.image;
vi.viewType = VK_IMAGE_VIEW_TYPE_2D;
vi.format = f;
vi.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
if (vkCreateImageView(dev.device, &vi, nullptr, &made.view) != VK_SUCCESS) {
destroy_image(made.image, made.alloc);
return false;
}
i = made;
return true;
}
void drop_img(Img& i) {
if (i.view) vkDestroyImageView(dev.device, i.view, nullptr);
if (i.image) destroy_image(i.image, i.alloc);
i = {};
}
void to_general(VkCommandBuffer cmd, VkImage image) {
VkImageMemoryBarrier2 b{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2};
b.srcStageMask = b.dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT;
b.dstAccessMask = VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT;
b.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
b.newLayout = VK_IMAGE_LAYOUT_GENERAL;
b.srcQueueFamilyIndex = b.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
b.image = image;
b.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
VkDependencyInfo d{VK_STRUCTURE_TYPE_DEPENDENCY_INFO};
d.imageMemoryBarrierCount = 1;
d.pImageMemoryBarriers = &b;
vkCmdPipelineBarrier2(cmd, &d);
}
// Only after the display jobs and device have drained (ensure_size), or before any frame used these images.
void drop_size() {
for (int i = 0; i < 2; i++) {
drop_img(fg.frames[i]), drop_img(fg.lumas[i]), drop_img(fg.flows[i]);
drop_img(fg.halfs[i]), drop_img(fg.fines[i]);
}
for (OutSet& set : fg.sets)
for (Img& t : set.frame) drop_img(t);
fg.w = fg.h = 0;
fg.mult = 0;
fg.have_prev = false;
fg.recorded = nullptr;
}
/// ((Triforce, FG-5) The format HDR Native's frames are kept in: A2B10G10R10, the game's own, where the card stores it in a
/// compute shader (shaderStorageImageExtendedFormats), else RGBA16F, which every card stores and which holds 10-bit codes too
VkFormat hdr_frames_format() {
static const VkFormat f = [] {
VkFormatProperties p{};
vkGetPhysicalDeviceFormatProperties(dev.physical, VK_FORMAT_A2B10G10R10_UNORM_PACK32, &p);
const VkFormatFeatureFlags need = VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT | VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT | VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
VK_FORMAT_FEATURE_TRANSFER_DST_BIT;
return dev.features.shaderStorageImageExtendedFormats && (p.optimalTilingFeatures & need) == need
? VK_FORMAT_A2B10G10R10_UNORM_PACK32 : VK_FORMAT_R16G16B16A16_SFLOAT;
}();
return f;
}
/// The three passes that store the frames (fg_copy, fg_synth, fg_fine_synth) for frames of format f, into alt_*: their rgba8
/// storage images declared as f's (and, for A2B10G10R10's Rgb10A2, the capability SPIR-V asks for it)
bool make_format_pipes(VkFormat f) {
const VkDescriptorType S = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, I = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
const bool rgb10 = f == VK_FORMAT_A2B10G10R10_UNORM_PACK32;
const auto patched = [rgb10](const uint32_t* code, size_t bytes) {
std::vector<uint32_t> w(code, code + bytes / 4);
if (sharpen_rules::storage_format_patch(w, sharpen_rules::kSpvRgba8, rgb10 ? sharpen_rules::kSpvRgb10A2 : sharpen_rules::kSpvRgba16f) != 1 ||
(rgb10 && !sharpen_rules::spv_require_capability(w, sharpen_rules::kSpvCapStorageImageExtendedFormats)))
w.clear();
return w;
};
const std::vector<uint32_t> c = patched(FG_COPY_COMP_SPV, sizeof FG_COPY_COMP_SPV);
const std::vector<uint32_t> m = fine_vectors() ? patched(FG_FINE_SYNTH_COMP_SPV, sizeof FG_FINE_SYNTH_COMP_SPV)
: patched(FG_SYNTH_COMP_SPV, sizeof FG_SYNTH_COMP_SPV);
if (c.empty() || m.empty() || !make_pipe(fg.alt_copy, c.data(), c.size() * 4, {S, I}, 16) ||
!make_pipe(fine_vectors() ? fg.alt_fine_synth : fg.alt_synth, m.data(), m.size() * 4, {S, S, S, I}, 4)) {
drop_pipe(fg.alt_copy), drop_pipe(fg.alt_synth), drop_pipe(fg.alt_fine_synth);
return false;
}
return true;
}
/// The frames' format for this present: RGBA8, or HDR Native's (hdr_frames_format) once its passes are made
VkFormat frames_format() {
if (!present_native_pq.load(std::memory_order_relaxed) || fg.hdr_failed) return VK_FORMAT_R8G8B8A8_UNORM;
const VkFormat f = hdr_frames_format();
if (fg.pipes_format == f || fg.alt_format == f) return f;
if (fg.alt_format == VK_FORMAT_UNDEFINED && make_format_pipes(f)) {
fg.alt_format = f;
return f;
}
fg.hdr_failed = true;
logf("vk: frame interpolation: its passes for HDR Native's 10-bit picture could not be made: its frames stay 8 bits");
return VK_FORMAT_R8G8B8A8_UNORM;
}
bool ensure_size(VkCommandBuffer cmd, uint32_t w, uint32_t h) {
const VkFormat format = frames_format(); // ((Triforce, FG-5))
if (fg.w == w && fg.h == h && fg.mult == multiplier() && fg.format == format) return true;
{ // (the display thread is done with the old sets first)
std::unique_lock lk(fg.mu);
fg.cv.wait(lk, [] { return fg.jobs.empty() && !fg.sets[0].busy && !fg.sets[1].busy && !fg.sets[2].busy; });
}
{
std::lock_guard qk(dev.present_mu); // (every queue idle: the present queue's too, device_wait_idle_locked)
device_wait_idle_locked();
}
drop_size();
if (format != fg.pipes_format) { // ((Triforce, FG-5) the passes that store the frames, the format's: both kept, swapped)
std::swap(fg.copy, fg.alt_copy), std::swap(fg.synth, fg.alt_synth), std::swap(fg.fine_synth, fg.alt_fine_synth);
std::swap(fg.pipes_format, fg.alt_format);
}
if (format != fg.format)
logf("vk: frame interpolation: its frames are %s%s", format == VK_FORMAT_R8G8B8A8_UNORM ? "RGBA8" :
format == VK_FORMAT_A2B10G10R10_UNORM_PACK32 ? "A2B10G10R10" : "RGBA16F",
format == VK_FORMAT_R8G8B8A8_UNORM ? "" : ": HDR Native's 10-bit picture kept to the window");
fg.format = format;
const uint32_t qw = (w + 3) / 4, qh = (h + 3) / 4, cw = (qw + 3) / 4, ch = (qh + 3) / 4;
const VkImageUsageFlags rw = VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
bool ok = true;
for (int i = 0; i < 2; i++) {
ok = ok && make_img(fg.frames[i], w, h, format, rw | VK_IMAGE_USAGE_TRANSFER_SRC_BIT);
ok = ok && make_img(fg.lumas[i], qw, qh, VK_FORMAT_R16_SFLOAT, rw);
ok = ok && make_img(fg.flows[i], cw, ch, VK_FORMAT_R16G16B16A16_SFLOAT, rw);
if (fine_vectors()) { // ((Triforce) the fine vectors': half-size luma, a vector per 8x8 pixels)
ok = ok && make_img(fg.halfs[i], (w + 1) / 2, (h + 1) / 2, VK_FORMAT_R16_SFLOAT, rw);
ok = ok && make_img(fg.fines[i], (w + 7) / 8, (h + 7) / 8, VK_FORMAT_R16G16B16A16_SFLOAT, rw);
}
}
const int mult = multiplier();
for (OutSet& set : fg.sets)
for (int k = 0; k < mult; k++)
ok = ok && make_img(set.frame[k], w, h, format, rw | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT);
if (!ok) {
fg.failed = true;
drop_size();
// Older submissions were drained above; ensure_size precedes every pass recorded by framegen_record.
drop_setup();
logf("vk: frame interpolation: its images (%ux%u) were not made: off", w, h);
return false;
}
fg.w = w, fg.h = h, fg.mult = mult;
for (int i = 0; i < 2; i++) {
to_general(cmd, fg.frames[i].image), to_general(cmd, fg.lumas[i].image), to_general(cmd, fg.flows[i].image);
if (fg.halfs[i].image) to_general(cmd, fg.halfs[i].image), to_general(cmd, fg.fines[i].image);
}
for (OutSet& set : fg.sets)
for (Img& t : set.frame)
if (t.image) to_general(cmd, t.image); // (only the multiplier's frames are made: a barrier on no image crashed the driver)
fg.have_prev = false;
return true;
}
/// One pass: pipe over w x h in 8x8 groups, the bindings in order (a sampler when one is given: a sampled image)
void pass(VkCommandBuffer cmd, const Pipe& p, std::initializer_list<std::pair<VkImageView, VkSampler>> bindings,
uint32_t w, uint32_t h, const void* push = nullptr, uint32_t push_bytes = 0) {
VkDescriptorImageInfo ii[8];
VkWriteDescriptorSet ws[8];
uint32_t n = 0;
for (const auto& [view, sampler] : bindings) {
ii[n] = {sampler, view, VK_IMAGE_LAYOUT_GENERAL};
ws[n] = {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
ws[n].dstBinding = n;
ws[n].descriptorCount = 1;
ws[n].descriptorType = sampler ? VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER : VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
ws[n].pImageInfo = &ii[n];
n++;
}
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, p.pipe);
vkCmdPushDescriptorSetKHR(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, p.layout, 0, n, ws);
if (push_bytes) vkCmdPushConstants(cmd, p.layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, push_bytes, push);
vkCmdDispatch(cmd, (w + 7) / 8, (h + 7) / 8, 1);
full_barrier(cmd);
}
/// The vectors from the frame before (prev) to this one (cur), w x h: one per 16x16 pixels (fg_flow, fg_median: flows[1]),
/// and (Triforce) with the fine vectors one per 8x8 from those (fg_fine_flow, then fg_fine_pick twice: fines[0])
void make_vectors(VkCommandBuffer cmd, int prev, int cur, uint32_t w, uint32_t h) {
const uint32_t qw = (w + 3) / 4, qh = (h + 3) / 4, cw = (qw + 3) / 4, ch = (qh + 3) / 4;
pass(cmd, fg.flow, {{fg.lumas[prev].view, fg.nearest}, {fg.lumas[cur].view, fg.nearest}, {fg.flows[0].view, VK_NULL_HANDLE}},
cw, ch);
pass(cmd, fg.median, {{fg.flows[0].view, fg.nearest}, {fg.flows[1].view, VK_NULL_HANDLE}}, cw, ch);
if (!fg.fine_flow.pipe) return;
const uint32_t fw = (w + 7) / 8, fh = (h + 7) / 8;
pass(cmd, fg.fine_flow, {{fg.halfs[prev].view, fg.nearest}, {fg.halfs[cur].view, fg.nearest}, {fg.flows[1].view, fg.nearest},
{fg.flows[0].view, fg.nearest}, {fg.fines[0].view, VK_NULL_HANDLE},
{fg.frames[prev].view, fg.linear}, {fg.frames[cur].view, fg.linear}},
fw, fh);
for (int i = 0; i < 2; i++)
pass(cmd, fg.fine_pick, {{fg.frames[prev].view, fg.linear}, {fg.frames[cur].view, fg.linear},
{fg.fines[i].view, fg.nearest}, {fg.fines[i ^ 1].view, VK_NULL_HANDLE}},
fw, fh);
}
/// The frame at t between the frame before and this one into out, w x h, from make_vectors' vectors
void make_frame(VkCommandBuffer cmd, int prev, int cur, const Img& out, float t, uint32_t w, uint32_t h) {
if (fg.fine_synth.pipe)
pass(cmd, fg.fine_synth, {{fg.frames[prev].view, fg.linear}, {fg.frames[cur].view, fg.linear},
{fg.fines[0].view, fg.nearest}, {out.view, VK_NULL_HANDLE}},
w, h, &t, 4);
else
pass(cmd, fg.synth, {{fg.frames[prev].view, fg.linear}, {fg.frames[cur].view, fg.linear},
{fg.flows[1].view, fg.nearest}, {out.view, VK_NULL_HANDLE}},
w, h, &t, 4);
}
// (hookshot) The display thread's wait for a frame's time, on a high-resolution timer. sleep_for rests on the system timer,
// whose 1 ms (timeBeginPeriod) Windows 11 does not keep for a process whose window it takes as unseen: with the presents
// no longer blocking (a present queue of their own) the thread slept before most frames and woke up to 20 ms after their time.
void wait_until_near(int64_t ns) {
static HANDLE timer = CreateWaitableTimerExW(nullptr, nullptr, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS);
LARGE_INTEGER d;
d.QuadPart = -std::max<int64_t>(1, ns / 100); // (relative, 100 ns units)
if (timer && SetWaitableTimerEx(timer, &d, 0, nullptr, nullptr, nullptr, 0)) {
WaitForSingleObject(timer, INFINITE);
return;
}
std::this_thread::sleep_for(std::chrono::nanoseconds(ns));
}
void display_main() {
SetThreadDescription(GetCurrentThread(), L"mkw_vk display");
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
int64_t interval = 16666667, last_at = 0;
struct { int on_time, late, skipped; int64_t since; } stat{};
int64_t next_show = 0, shown_at = 0;
int hist[6] = {};
for (;;) {
Job j;
{
std::unique_lock lk(fg.mu);
fg.cv.wait(lk, [] { return !fg.jobs.empty(); });
j = fg.jobs.front();
fg.jobs.pop_front();
// (Triforce) framegen_record waits for this while RivaTuner holds the presents: the next frame is recorded while
// this one's are shown, not after them
if (pace::display_yields()) fg.cv.notify_all();
}
if (!j.set) {
// (Triforce) the held picture again (the hold of the last picture), after the frames before it: no made frame, not a
// game frame for the counter, and the frames' spacing measured afresh after it
present_kind.store(0);
present_image(j.held, j.held_w, j.held_h, j.has_crop ? j.crop : nullptr, j.held_view);
last_at = 0;
continue;
}
if (last_at) {
const int64_t d = j.at - last_at;
if (d > 0 && d < 100000000) interval = (interval * 7 + std::clamp<int64_t>(d, 4000000, 50000000)) / 8;
}
last_at = j.at;
if (!stat.since) stat.since = j.at;
if (j.at - stat.since > 5000000000LL) {
logf("vk: frame interpolation: %d frames shown on time, %d late (over 2 ms); shown apart: <7 ms %d, 7-9 %d, 9-12 %d, "
"12-17 %d, 17-25 %d, over 25 %d", stat.on_time, stat.late, hist[0], hist[1], hist[2], hist[3], hist[4], hist[5]);
memset(hist, 0, sizeof hist);
stat = {0, 0, 0, j.at};
}
// (hookshot) The pacer. The game hands over frames 12 to 28 ms apart around a mean of 19, and shown as they came that
// was the stutter, whatever the frame rate. Every frame now has its place on an even line, the recent frame time
// apart, a frame early waiting for it: never more than one game frame behind what the game handed over, and a line
// that has fallen behind what came starts again from now. RX_VK_PACE=0: as they come.
// (Triforce) Not while RivaTuner Statistics Server holds these presents (vk_pace.cpp, pace_rule.h): its limiter spaces
// them, and this pacer, which spaces them by the game's frame time as it measures it, would be measuring RivaTuner's
// own spacing and could only fall behind it. Each frame then goes to its present at once, where RivaTuner holds it
const bool pace = opts.pace != 0 && !pace::display_yields();
const bool timed = j.set->shown >= 0; // (Auto: each frame at its own refresh's time)
const int shown = timed ? j.set->shown : j.set->made + 1;
const int64_t step = interval / std::max(shown, 1);
if (timed) menu_note_frame(true, 0); // (the overlay's game rate: Auto counts each game frame, given a refresh or not)
for (int k = 0; k < shown; k++) {
int64_t due = timed ? j.set->due[k] : pace ? next_show : 0;
const int64_t now = now_ns();
if (due < now) due = now;
if (!timed && due > j.at + interval + step) due = j.at + interval + step;
if (timed && due > now + 100000000) due = now; // (a game frame time gone wrong: not held back)
if (due - now > 1000000) wait_until_near(due - now - 1000000);
while (now_ns() < due) SwitchToThread();
const int64_t t = now_ns();
present_kind.store(timed ? (j.set->game[k] ? 0 : 1) : k < j.set->made ? 1 : 0);
present_image(j.set->frame[k].image, fg.w, fg.h, nullptr, j.set->frame[k].view);
menu_note_frame(!timed && k == j.set->made);
if (shown_at) {
const int64_t d = t - shown_at;
hist[d < 7000000 ? 0 : d < 9000000 ? 1 : d < 12000000 ? 2 : d < 17000000 ? 3 : d < 25000000 ? 4 : 5]++;
}
shown_at = t;
next_show = t + step;
(t - due > 2000000 ? stat.late : stat.on_time)++;
}
std::lock_guard lk(fg.mu);
j.set->busy = false;
fg.cv.notify_all();
}
}
} // namespace
// The Insert menu's Frame generation is off at 0. While it was ignored, every present still copied the frame and
// reduced it to luma (framegen_record), then a second thread showed it. Open areas paid for that on a full GPU and
// gained no made frames. Auto (1) and 2x and up are the interpolation; RX_VK_FRAMEGEN=0 keeps it off entirely.
bool framegen_wanted(int menu_value, int allow) {
return allow && menu_value >= 1; // (1: Auto)
}
int framegen_multiplier() { return !framegen_on() ? 0 : opts.framegen == 1 ? -1 : multiplier(); }
bool framegen_on() {
static const int allow = !getenv("RX_VK_FRAMEGEN") || atoi(getenv("RX_VK_FRAMEGEN")) != 0;
// (Triforce: not beside NVIDIA DLSS frame generation, whose frames go in at the present itself: vk_dlssg.cpp)
return framegen_wanted(opts.framegen, allow) && !dev.offscreen && !fg.failed && !dlssg::active();
}
// Ultrawide's menus (the Insert menu's Ultrawide menus): which of nx2/oot_wide.py's keep-list screens keep the whole picture, told
// to the game's side each present, and, while a menu shows, the sides of the middle 16:9 to show out of focus (1)
// or black (2), as fractions of the width. A menu's layout is set up once, when it is first opened: what tells one is
// that the frame draws no world (vk_draw.cpp world_drawn_at), as a pause, the map, a loading screen or a movie
// does not (3 presents' grace for a frame the world skips). The middle is centred: its right edge is 1 - x0. One 8-byte atomic: the
// frame generation's display thread reads it (framegen_scale) while the replay thread writes it (wide_sides_tick)
struct WideSides { float x0 = 0.0f; int side = 0; };
std::atomic<bool> wide_possible{false}; // (a margin and the setting on: vk_replay.cpp takes the scale pass's path for the sides)
WideSides wide_sides() {
WideSides w;
if (!host || !host->wide_menus) return w;
// (KEEP_VTABLES 1: the pause and system menus' background. 0, the item wheel's background, and 2 and 3, the event and
// the loading fade, keep covering the whole picture.
// (Triforce) This was 0xa: bit 3 was taken for a menu's, and it is the loading fade (host/oot_wide_hud.h's table). So
// with the row on Centred a loading fade covered the middle 16:9 alone: the picture showed at its left and right while
// a place loaded, sharp while the world was still drawn under the fade and out of focus after. A fade is one colour and
// has to cover the whole picture, as NX2navi's widescreen keeps both fades whole at every setting)
constexpr uint32_t kMenus = 0x2;
const float margin = host->wide_menus(opts.wide_menus ? 0xfu & ~kMenus : 0xfu);
extern std::atomic<int64_t> world_drawn_at;
extern int64_t presents_now;
extern std::atomic<int64_t> wheel_drawn_at;
// (no world: a menu. But not the item wheel, whose background is the last frame of play over the whole picture, as on the
// console: its sides are left as the game draws them)
const bool no_world = presents_now - world_drawn_at.load(std::memory_order_relaxed) > 3 &&
presents_now - wheel_drawn_at.load(std::memory_order_relaxed) > 3;
wide_possible.store(opts.wide_menus && margin > 0.0f, std::memory_order_relaxed);
if (opts.wide_menus && margin > 0.0f && no_world) w.x0 = margin, w.side = opts.wide_menus;
static int told = 0;
if (w.side != told) { // (each change logged: a report of blurred sides in play says when they came and went)
logf("vk: ultrawide menu sides %s at present %lld (the world last drawn at present %lld)", w.side ? "on" : "off",
(long long)presents_now, (long long)world_drawn_at.load(std::memory_order_relaxed));
told = w.side;
}
return w;
}
std::atomic<WideSides> wide_now; // (wide_sides_tick's, each present)
bool wide_sides_possible() { return wide_possible.load(std::memory_order_relaxed); }
VkImage framegen_scale(VkCommandBuffer cmd, VkImageView src, VkImage image, uint32_t sw, uint32_t sh, const int crop[4], uint32_t dw, uint32_t dh) {
const WideSides wide = wide_now.load(std::memory_order_relaxed); // (wide_sides_tick, each present)
// ((Triforce, S1) the Sharpness row: nothing here while the upscaler sharpens the 3D before the HUD (FSR 3.1, FSR 4, DLSS
// and DLAA: vk_temporal.h temporal_sharpens_3d), never twice and not the HUD; the finished picture with the game's own)
const int sharp = sharpen_rules::window_sharpness(opts.sharpness, temporal_sharpens_3d());
{ // (each change of where it sharpens, said: a report of a soft or an oversharpened picture then says which)
static int told = -1, told_value = -1;
const int where = !opts.sharpness ? 0 : sharp ? 1 : 2;
if (where != told || (where && opts.sharpness != told_value)) {
told = where, told_value = opts.sharpness;
static const char* const said[3] = {"off", "the finished picture at the window, the HUD with it",
"the 3D before the HUD, by the upscaler: nothing at the window"};
logf("vk: Sharpness %d%%: %s", opts.sharpness, said[where]);
}
}
if (!opts.upscale && !sharp && !wide.side) return VK_NULL_HANDLE;
if (!src || !image || !sw || !sh || !dw || !dh) return VK_NULL_HANDLE;
const PresentSourceRect source = present_source_rect(sw, sh, crop);
if (!source.w || !source.h) return VK_NULL_HANDLE;
if (!init_scale()) return VK_NULL_HANDLE;
// ((Triforce, S5) HDR Native: the game's PQ picture (10 bits) through RGBA16F images and the shaders' rgba16f builds, not
// RGBA8 ones, which took 2 bits off it before the HDR10 window. Those not made: the picture to the window as it is)
const bool pq = present_native_pq.load(std::memory_order_relaxed);
if (pq && !init_scale_pq()) return VK_NULL_HANDLE;
const VkFormat format = pq ? VK_FORMAT_R16G16B16A16_SFLOAT : VK_FORMAT_R8G8B8A8_UNORM;
if (scale.format != format) { // (made again in the other format below, each at its size)
{
std::lock_guard qk(dev.present_mu);
device_wait_idle_locked();
}
drop_img(scale.cropped), drop_img(scale.scaled), drop_img(scale.easu_img), drop_img(scale.small);
scale.cropped_w = scale.cropped_h = scale.scaled_w = scale.scaled_h = scale.small_w = scale.small_h = 0;
scale.format = format;
}
const Pipe& filter = pq ? scale.pipe16 : scale.pipe;
const Pipe& easu = pq ? scale.easu16 : scale.easu;
const Pipe& rcas = pq ? scale.rcas16 : scale.rcas;
// (the game draws into part of its window textures, 2048 x 1152 of 2560 x 1440 here: the filter was given the whole texture, and the
// picture came out in the top left corner of the window with black around it. The crop first, then the filter)
if (source.x || source.y || source.w != sw || source.h != sh) {
const uint32_t cw = source.w, ch = source.h;
if (scale.cropped_w != cw || scale.cropped_h != ch) {
{
std::lock_guard qk(dev.present_mu);
device_wait_idle_locked();
}
drop_img(scale.cropped);
scale.cropped_w = scale.cropped_h = 0;
if (!make_img(scale.cropped, cw, ch, scale.format, VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT)) {
drop_img(scale.cropped);
return VK_NULL_HANDLE;
}
scale.cropped_w = cw, scale.cropped_h = ch;
to_general(cmd, scale.cropped.image);
}
VkImageBlit b{};
b.srcSubresource = b.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
b.srcOffsets[0] = {(int)source.x, (int)source.y, 0};
b.srcOffsets[1] = {(int)(source.x + cw), (int)(source.y + ch), 1};
b.dstOffsets[1] = {(int)cw, (int)ch, 1};
vkCmdBlitImage(cmd, image, VK_IMAGE_LAYOUT_GENERAL, scale.cropped.image, VK_IMAGE_LAYOUT_GENERAL, 1, &b, VK_FILTER_NEAREST);
full_barrier(cmd);
src = scale.cropped.view;
}
if (scale.scaled_w != dw || scale.scaled_h != dh) {
{
std::lock_guard qk(dev.present_mu); // (the queue is idle only while nothing else submits to it)
device_wait_idle_locked();
}
drop_img(scale.scaled);
scale.scaled_w = scale.scaled_h = 0;
// (Triforce, 11-13: SAMPLED too. On an HDR10 display the HDR pass (framegen_hdr) samples this image whenever the
// picture was scaled or sharpened first; made without that usage, sampling it is not allowed, and the window was
// black with Sharpness or Upscaling on ("Sharpness slider deletes all visuals", RTX 3060 Ti on an HDR10 surface;
// shown in the lab with RX_VK_TEST_HDR_PASS=1: the window's mean brightness 0 with Sharpness 70, 131 with 0))
if (!make_img(scale.scaled, dw, dh, scale.format,
VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT)) {
drop_img(scale.scaled);
return VK_NULL_HANDLE;
}
scale.scaled_w = dw, scale.scaled_h = dh;
to_general(cmd, scale.scaled.image);
// (Triforce: AMD FSR 1's middle image, EASU's output and RCAS's input, at the window's size. Without it Upscaling 3 falls
// to the filter below)
drop_img(scale.easu_img);
if (easu.pipe && rcas.pipe && scale.nearest &&
make_img(scale.easu_img, dw, dh, scale.format, VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_SAMPLED_BIT))
to_general(cmd, scale.easu_img.image);
else
drop_img(scale.easu_img);
}
// (the size the picture is at: the cropped one when the game drew into part of its texture)
const uint32_t iw = src == scale.cropped.view ? scale.cropped_w : sw, ih = src == scale.cropped.view ? scale.cropped_h : sh;
// (Triforce: AMD FSR 1, EvilOOT's own window filter: EASU, the edge-adaptive upscale, then RCAS at the Sharpness row.
// ((Triforce, S4) only where it enlarges, as AMD's EASU is made for: not at 1:1, where the menu says the filter is not
// needed, nor to a smaller window; there Catmull-Rom or the plain copy with the other filters' Sharpness. (S6) Nor
// beside a menu with ultrawide's sides, which the filter below draws. (S3) At Sharpness 0 (or the 3D sharpened before
// the HUD) EASU alone, straight into the window's image: RCAS at its lightest still sharpened, 2 stops below its most)
const bool fsr1 = opts.upscale == 3 && scale.easu_img.view && !wide.side && sharpen_rules::fsr1_enlarges(iw, ih, dw, dh);
if (fsr1) {
const uint32_t ep[4] = {iw, ih, dw, dh};
// ((Triforce, options_version 7) the window's filter is chosen by the sizes, not by the player (upscaler_rows.h
// window_filter), so it sharpens only when the Sharpness row asks: at 0, or with the 3D sharpened before the HUD
// (sharp 0), EASU alone straight into the image that is shown, both storage images of one format)
pass(cmd, easu, {{src, scale.linear}, {sharp ? scale.easu_img.view : scale.scaled.view, VK_NULL_HANDLE}}, dw, dh, ep, sizeof ep);
if (sharp) {
const struct { uint32_t w, h; float sharp, pad; } rp{dw, dh, sharp / 100.0f, 0.0f};
pass(cmd, rcas, {{scale.easu_img.view, scale.nearest}, {scale.scaled.view, VK_NULL_HANDLE}}, dw, dh, &rp, sizeof rp);
}
return scale.scaled.image;
}
struct { int mode; float sharp; float x0, x1; int side; } pc{sharpen_rules::window_mode(opts.upscale, fsr1, iw, ih, dw, dh),
sharp / 100.0f, wide.x0, 1.0f - wide.x0, wide.side};
// (nothing left to do: the plain copy at 1:1, no Sharpness here, no sides. The picture goes to the window as it is)
if (!pc.mode && !sharp && !wide.side && iw == dw && ih == dh) return VK_NULL_HANDLE;
if (wide.side == 1) { // (the blurred sides: first the picture at 1/8, each pixel the mean of its 8 x 8, which they blur)
// (from #1448's checked source rectangle, as the crop above: the picture src now holds)
const uint32_t smw = std::max(1u, source.w / 8), smh = std::max(1u, source.h / 8);
if (scale.small_w != smw || scale.small_h != smh) {
{
std::lock_guard qk(dev.present_mu);
device_wait_idle_locked();
}
drop_img(scale.small);
scale.small_w = scale.small_h = 0;
if (!make_img(scale.small, smw, smh, scale.format, VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_SAMPLED_BIT)) {
drop_img(scale.small);
return VK_NULL_HANDLE;
}
scale.small_w = smw, scale.small_h = smh;
to_general(cmd, scale.small.image);
}
pc.side = 3; // (fg_scale.comp: make the small copy)
pass(cmd, filter, {{src, scale.linear}, {scale.small.view, VK_NULL_HANDLE}, {src, scale.linear}}, smw, smh, &pc, sizeof pc);
full_barrier(cmd);
pc.side = 1;
}
pass(cmd, filter, {{src, scale.linear}, {scale.scaled.view, VK_NULL_HANDLE}, {wide.side == 1 ? scale.small.view : src, scale.linear}},
dw, dh, &pc, sizeof pc);
static const bool report = getenv("RX_VK_UPSCALE_LOG") != nullptr;
static bool reported = false;
if (report && !reported) {
reported = true;
logf("vk: upscale dispatched: mode %d, sharpness %d%% (of RCAS's strongest, neighbours one source pixel apart), source %ux%u, "
"crop request %d,%d %dx%d, destination %ux%u%s",
pc.mode, sharp, sw, sh, crop ? crop[0] : 0, crop ? crop[1] : 0,
crop ? crop[2] : (int)sw, crop ? crop[3] : (int)sh, dw, dh, pq ? ", HDR Native's picture in RGBA16F" : "");
}
return scale.scaled.image;
}
VkImageView framegen_scaled_view() { return scale.scaled.view; }
/// (hookshot) The Insert menu's HDR: src's crop to a dw x dh HDR10 image (PQ, Rec. 2020, 10 bits), to be copied into an HDR10
/// swapchain's image as the SDR picture is (shaders/hdr_encode.comp); null when it cannot be made (then the SDR picture)
VkImage framegen_hdr(VkCommandBuffer cmd, VkImageView src, uint32_t sw, uint32_t sh, const int crop[4], uint32_t dw, uint32_t dh) {
if (!src || !sw || !sh || !dw || !dh) return VK_NULL_HANDLE;
const PresentSourceRect source = present_source_rect(sw, sh, crop);
if (!source.w || !source.h) return VK_NULL_HANDLE;
if (!hdr.init) {
hdr.init = hdr.failed = true;
const VkDescriptorType S = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, I = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
if (dev.push_descriptor && vkCmdPushDescriptorSetKHR && init_scale() &&
make_pipe(hdr.pipe, HDR_ENCODE_COMP_SPV, sizeof HDR_ENCODE_COMP_SPV, {S, I}, 32))
hdr.failed = false;
else
logf("vk: HDR: the HDR pass could not be made: SDR");
}
if (hdr.failed) return VK_NULL_HANDLE;
if (hdr.w != dw || hdr.h != dh) {
{
std::lock_guard qk(dev.present_mu);
device_wait_idle_locked();
}
drop_img(hdr.out);
hdr.w = hdr.h = 0;
if (!make_img(hdr.out, dw, dh, VK_FORMAT_A2B10G10R10_UNORM_PACK32, VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT)) {
drop_img(hdr.out);
static bool told;
if (!told) told = true, logf("vk: HDR: no 10-bit storage image on this GPU: SDR");
return VK_NULL_HANDLE;
}
hdr.w = dw, hdr.h = dh;
to_general(cmd, hdr.out.image);
}
const float x0 = source.x / (float)sw, y0 = source.y / (float)sh;
const float x1 = (source.x + source.w) / (float)sw, y1 = (source.y + source.h) / (float)sh;
const struct { float uv[4]; float white, peak, knee, pad; } pc{{x0, y0, x1, y1}, (float)std::clamp(opts.hdr_white, 80, 500),
(float)std::clamp(opts.hdr_peak, 400, 10000), 0.6f, 0.0f};
pass(cmd, hdr.pipe, {{src, scale.linear}, {hdr.out.view, VK_NULL_HANDLE}}, dw, dh, &pc, sizeof pc);
return hdr.out.image;
}
VkImage framegen_made_image(uint32_t* w, uint32_t* h) {
if (!fg.recorded || !fg.recorded->made) return VK_NULL_HANDLE;
*w = fg.w, *h = fg.h;
return fg.recorded->frame[0].image;
}
// ((Triforce, FG-2) The picture without the HUD, for AMD's frame generation (vk_fsr4.h fsr4fg, HUDLessColor). OoT's gameplay
// upscale writes the tone mapped scene, linear, into a B10G11R11 picture at the output's size; the HUD is drawn into that
// picture after it, and the game's last pass encodes it to the 8-bit sRGB picture presented (NX2navi's measurement of the
// same frame, its runtime/vk_dlaa.c: "a last draw copies that target into the 8-bit sRGB output target, encoding only").
// So at the upscale's dispatch, once the output is written (by an upscaler of ours, before or after the bloom, or by the
// game's own network), it is blitted into an R8G8B8A8_SRGB image (the blit encodes linear light to sRGB, as NX2navi's
// hudless_from does), tagged with the frame (texture_frame_no). At the present, the part of it that is the present's crop
// (framegen_rows.h hudless_source: scaled when the sizes differ) is blitted into one of the picture's size and its bytes
// copied into an RGBA8 UNORM image, which is what AMD's library is given, beside the picture with the HUD in the same 8-bit
// encoding. Only this frame's: a present whose frame has none (the upscale did not run, another format) makes no frame of
// AMD's. Not for HDR Native's picture, which the game encodes to PQ itself (vk_fsr4.cpp: no frame then), nor for an RGBA16F
// run's picture (RenoDX's: its own encoder). RX_VK_FG_HUDLESS=0: none taken (AMD's library then makes no frame: a test)
namespace {
struct Hudless {
std::mutex mu;
struct Taken {
Img img; // (R8G8B8A8_SRGB, w x h: the upscale's output)
uint32_t w = 0, h = 0;
uint64_t frame = 0, seq = 0; // (seq: the latest of a frame's, if it had two upscales)
bool valid = false;
} taken[2]; // (two: one taken while the frame before's is still to be used at its present)
int next = 0;
uint64_t seq = 0;
bool failed = false; // (its images could not be made: none for the run, said once)
uint32_t want_w = 0, want_h = 0; // (another size seen at a dispatch: the images made again at the present, hudless_for)
Img srgb, out; // (the present's crop: sRGB, then its bytes as UNORM, the picture's size)
uint32_t w = 0, h = 0;
int formats = 0; // (0 not asked, 1 the blits can be made, -1 not)
uint64_t taken_n = 0, given_n = 0, missing_n = 0;
bool told_first = false, told_format = false, told_formats = false, told_missing = false;
} hl;
bool hudless_off() {
static const bool off = getenv("RX_VK_FG_HUDLESS") && atoi(getenv("RX_VK_FG_HUDLESS")) == 0;
return off;
}
bool hudless_formats() { // (hl.mu held)
if (hl.formats) return hl.formats > 0;
VkFormatProperties a{}, b{}, c{};
vkGetPhysicalDeviceFormatProperties(dev.physical, VK_FORMAT_B10G11R11_UFLOAT_PACK32, &a);
vkGetPhysicalDeviceFormatProperties(dev.physical, VK_FORMAT_R8G8B8A8_SRGB, &b);
vkGetPhysicalDeviceFormatProperties(dev.physical, VK_FORMAT_R8G8B8A8_UNORM, &c);
const bool ok = (a.optimalTilingFeatures & VK_FORMAT_FEATURE_BLIT_SRC_BIT) &&
(b.optimalTilingFeatures & (VK_FORMAT_FEATURE_BLIT_SRC_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT |
VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT)) ==
(VK_FORMAT_FEATURE_BLIT_SRC_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT | VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT) &&
(c.optimalTilingFeatures & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT);
hl.formats = ok ? 1 : -1;
if (!ok) logf("vk: AMD FSR frame generation: the card cannot blit the upscale's picture into sRGB: no picture without the HUD, "
"so AMD's library makes no frame");
return ok;
}
} // namespace
void framegen_hudless_note(VkCommandBuffer cmd, Texture* out, uint32_t level) {
if (!cmd || !out || !out->image || !fsr4fg::frames_wanted() || hudless_off()) return; // (the row elsewhere: nothing)
if (present_native_pq.load(std::memory_order_relaxed)) return; // (HDR Native: the renderer's own makes the frame)
std::lock_guard lk(hl.mu);
if (hl.failed) return;
if (out->format != VK_FORMAT_B10G11R11_UFLOAT_PACK32 || out->samples != VK_SAMPLE_COUNT_1_BIT || out->layers != 1 ||
level >= out->levels) {
if (!hl.told_format)
hl.told_format = true, logf("vk: AMD FSR frame generation: the upscale's picture is vk format %d, not the game's "
"B10G11R11: no picture without the HUD from it", (int)out->format);
return;
}
if (!hudless_formats()) return;
const uint32_t w = std::max(1u, out->width >> level), h = std::max(1u, out->height >> level);
Hudless::Taken& t = hl.taken[hl.next];
if (!t.img.image || t.w != w || t.h != h) { // (made at the present: none this frame)
hl.want_w = w, hl.want_h = h;
return;
}
hl.next ^= 1;
full_barrier(cmd); // (the upscale's output, written just before)
VkImageBlit b{};
b.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, level, 0, 1};
b.srcOffsets[1] = {(int32_t)w, (int32_t)h, 1};
b.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
b.dstOffsets[1] = {(int32_t)w, (int32_t)h, 1};
vkCmdBlitImage(cmd, out->image, VK_IMAGE_LAYOUT_GENERAL, t.img.image, VK_IMAGE_LAYOUT_GENERAL, 1, &b, VK_FILTER_NEAREST);
full_barrier(cmd);
t.frame = texture_frame_no(), t.seq = ++hl.seq, t.valid = true;
hl.taken_n++;
if (!hl.told_first)
hl.told_first = true, logf("vk: AMD FSR frame generation: the picture without the HUD is taken at the gameplay upscale's "
"output, %ux%u (frame %llu)", w, h, (unsigned long long)t.frame);
}
namespace {
/// At the present: this frame's picture without the HUD (frame: the inputs' texture_frame_no), cut as the presented picture
/// is (fg.source of fg.src_w x fg.src_h), the picture's size w x h, RGBA8 UNORM (GENERAL); null when there is none. Also makes
/// the images a dispatch found missing (hl.want_*), the GPU idle first, as ensure_size does
VkImage hudless_for(VkCommandBuffer cmd, uint64_t frame, uint32_t w, uint32_t h) {
if (hudless_off()) return VK_NULL_HANDLE;
std::lock_guard lk(hl.mu);
if (hl.failed) return VK_NULL_HANDLE;
// (the images a dispatch asked for: not while one of them holds this frame's, whose blit may be in a recording not yet
// submitted (RX_VK_SHOT_MADE records frame generation into the present's own): the next present makes them)
bool recorded_now = false;
for (const Hudless::Taken& k : hl.taken) recorded_now |= k.valid && k.frame == frame;
const bool retake = hl.want_w && (hl.taken[0].w != hl.want_w || hl.taken[0].h != hl.want_h) && !recorded_now;
const bool remake = retake || hl.w != w || hl.h != h;
if (remake) {
{
std::lock_guard qk(dev.present_mu);
device_wait_idle_locked();
}
const VkImageUsageFlags xfer = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
bool ok = true;
if (retake)
for (Hudless::Taken& t : hl.taken) {
drop_img(t.img), t = {};
ok = ok && make_img(t.img, hl.want_w, hl.want_h, VK_FORMAT_R8G8B8A8_SRGB, xfer);
if (ok) to_general(cmd, t.img.image), t.w = hl.want_w, t.h = hl.want_h;
}
if (hl.w != w || hl.h != h) {
drop_img(hl.srgb), drop_img(hl.out), hl.w = hl.h = 0;
ok = ok && make_img(hl.srgb, w, h, VK_FORMAT_R8G8B8A8_SRGB, xfer) &&
make_img(hl.out, w, h, VK_FORMAT_R8G8B8A8_UNORM, xfer | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_SAMPLED_BIT);
if (ok) to_general(cmd, hl.srgb.image), to_general(cmd, hl.out.image), hl.w = w, hl.h = h;
}
if (!ok) {
for (Hudless::Taken& t : hl.taken) drop_img(t.img), t = {};
drop_img(hl.srgb), drop_img(hl.out), hl.w = hl.h = 0;
logf("vk: AMD FSR frame generation: the images of the picture without the HUD (%ux%u, %ux%u) were not made: AMD's "
"library makes no frame in this run", hl.want_w, hl.want_h, w, h);
hl.want_w = hl.want_h = 0, hl.failed = true;
}
return VK_NULL_HANDLE; // (this frame's was not taken: the images were not there)
}
Hudless::Taken* t = nullptr;
for (Hudless::Taken& k : hl.taken)
if (k.valid && k.frame == frame && (!t || k.seq > t->seq)) t = &k;
for (Hudless::Taken& k : hl.taken)
if (k.valid && k.frame <= frame) k.valid = false; // (each frame's once; an older one is never used)
const framegen_rows::SourceRect r = t ? framegen_rows::hudless_source(t->w, t->h, fg.src_w, fg.src_h, fg.source.x,
fg.source.y, fg.source.w, fg.source.h)
: framegen_rows::SourceRect{};
if (!t || r.x1 <= r.x0 || !hl.out.image) {
hl.missing_n++;
if (!hl.told_missing && hl.taken_n) // (the first frame after some were taken: what to look for when none is made)
hl.told_missing = true, logf("vk: AMD FSR frame generation: a present with the scene's depth and motion but no picture "
"without the HUD of its frame (frame %llu): the game's frame alone", (unsigned long long)frame);
return VK_NULL_HANDLE;
}
VkImageBlit b{};
b.srcSubresource = b.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
b.srcOffsets[0] = {r.x0, r.y0, 0}, b.srcOffsets[1] = {r.x1, r.y1, 1};
b.dstOffsets[1] = {(int32_t)w, (int32_t)h, 1};
const bool same = (uint32_t)(r.x1 - r.x0) == w && (uint32_t)(r.y1 - r.y0) == h;
full_barrier(cmd);
vkCmdBlitImage(cmd, t->img.image, VK_IMAGE_LAYOUT_GENERAL, hl.srgb.image, VK_IMAGE_LAYOUT_GENERAL, 1, &b,
same ? VK_FILTER_NEAREST : VK_FILTER_LINEAR);
full_barrier(cmd);
const VkImageCopy c{{VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, {0, 0, 0}, {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, {0, 0, 0}, {w, h, 1}};
vkCmdCopyImage(cmd, hl.srgb.image, VK_IMAGE_LAYOUT_GENERAL, hl.out.image, VK_IMAGE_LAYOUT_GENERAL, 1, &c); // (the bytes)
full_barrier(cmd);
if (hl.given_n++ == 0)
logf("vk: AMD FSR frame generation: the first picture without the HUD given, %ux%u from %d,%d %dx%d of the upscale's %ux%u%s",
w, h, r.x0, r.y0, r.x1 - r.x0, r.y1 - r.y0, t->w, t->h, same ? "" : " (scaled)");
return hl.out.image;
}
} // namespace
/// (hookshot) The made frame of a 2x set by AMD FSR 3.1's frame interpolation (vk_fsr3fi.h), from this frame's colour and
/// the scene's depth and motion (vk_fsr3_inputs.h): 1 when it made it into set.frame[0], 0 when it did not (no inputs this
/// present, a reset, anything not made), which leaves the set to the interpolation below. sequence: framegen_record's count
/// of its presents (one it did not hand over, a gap among them, makes the next one a reset: vk_fsr3fi.h Inputs::sequence)
int fsr3fi_frame(VkCommandBuffer cmd, OutSet& set, int cur, uint32_t uw, uint32_t uh, uint64_t sequence) {
// (every 600 presents asked: how many FSR made, and how many it left to the interpolation below and why)
static uint64_t asked = 0, made = 0, no_inputs = 0;
if (++asked % 600 == 0)
logf("vk: fsr3fi: %llu of the last 600 made frames by FSR 3.1, %llu without its inputs (the renderer's own made them)",
(unsigned long long)made, (unsigned long long)no_inputs),
made = no_inputs = 0;
if (fg.format != VK_FORMAT_R8G8B8A8_UNORM) return 0; // ((Triforce, FG-5) HDR Native's 10-bit frames: its passes take RGBA8)
fsr3_inputs::Frame f;
if (!fsr3_inputs::take(f)) return no_inputs++, 0;
fsr3fi::Inputs in;
in.color = fg.frames[cur].view;
in.color_image = fg.frames[cur].image;
in.color_format = VK_FORMAT_R8G8B8A8_UNORM; // (ensure_size, for fg.frames)
in.width = uw, in.height = uh;
in.depth = f.depth, in.motion = f.motion;
in.render_width = f.width, in.render_height = f.height;
in.jitter[0] = f.jitter[0], in.jitter[1] = f.jitter[1];
in.camera = f.camera;
in.frame_time_ms = f.frame_ms;
in.reset = f.reset;
in.sequence = sequence;
if (!fsr3fi::generate(cmd, in) || !fsr3fi::made_image()) return 0;
const VkImageCopy c{{VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, {0, 0, 0}, {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, {0, 0, 0},
{uw, uh, 1}};
vkCmdCopyImage(cmd, fsr3fi::made_image(), VK_IMAGE_LAYOUT_GENERAL, set.frame[0].image, VK_IMAGE_LAYOUT_GENERAL, 1, &c);
made++;
static bool told = false;
if (!told) {
told = true;
logf("vk: fsr3fi: AMD FSR 3.1 makes the frame between each two of the game's (%ux%u, its depth and motion %ux%u)", uw,
uh, f.width, f.height);
}
return 1;
}
/// (Triforce) The made frame of the row's AMD FSR 4, by AMD's frame generation library (vk_fsr4.h fsr4fg) from this frame's
/// picture and the scene's depth, motion and camera (vk_fsr3_inputs.h, as FSR 3.1's above). MADE: it is in set.frame[0].
/// SKIP: the library was given the frame as a new history (the first frame, a gap, a camera cut) and no frame is to be made
/// between two pictures that do not follow each other. OWN: it did not take the present (no scene in it, not available,
/// failed), which leaves the set to the interpolation below, as a 2x. blend: the frame before is there to make a frame from
/// ((Triforce, GAME) as well: the scene's depth and motion were there, this frame's picture without the HUD not, so nothing
/// is made and the game's frame is shown alone. HDR Native and an RGBA16F scene hold for every present: OWN, framegen_rows.h
/// held_made. Every 600 presents asked, a line of what became of them)
framegen_rows::Made fsr4_frame(VkCommandBuffer cmd, OutSet& set, int cur, uint32_t uw, uint32_t uh, uint64_t sequence, bool blend) {
if (!fsr4fg::frames_wanted()) {
// ((Triforce) the row at AMD FSR and AMD's library not asked for the frame: why, each time it changes
// ((Triforce) HDR Native or an RGBA16F scene, which hold it from every present, past FG_WHY_COUNT)
static int told = -1;
const framegen_rows::Held held = fsr4fg::held();
const int why = dlssg::active() ? framegen_rows::FG_WHY_COUNT : (int)fsr4fg::why();
const int key = why == framegen_rows::FG_OK ? framegen_rows::FG_WHY_COUNT + 1 + (int)held : why;
if (key != told)
told = key, logf("vk: AMD FSR frame generation is not used (%s): the renderer's own makes the frame, as at 2x",
why == framegen_rows::FG_WHY_COUNT ? "NVIDIA DLSS frame generation runs"
: why != framegen_rows::FG_OK ? framegen_rows::why_name((framegen_rows::Why)why)
: held == framegen_rows::HELD_HDR_NATIVE
? "paused with HDR Native: the game encodes its HDR picture itself, and no picture without the HUD can follow it"
: held == framegen_rows::HELD_HDR_SCENE
? "paused in this run: its scene is RenoDX's RGBA16F, which no picture without the HUD is taken from"
: "the row changed");
return framegen_rows::OWN;
}
static uint64_t asked = 0, n[4] = {}, bare = 0, own_motion = 0; // (n: by Made; bare: OWN without depth and motion)
const auto counted = [&](framegen_rows::Made m) {
n[m]++;
if (++asked % 600 == 0) {
logf("vk: AMD FSR frame generation %s: of the last 600 presents %llu made by AMD's library, %llu new histories, %llu "
"the game's frame alone (no picture without the HUD), %llu to the renderer's own (%llu of them "
"without the scene's depth and motion); %llu with what moves by itself in the motion",
fsr4fg::version(), (unsigned long long)n[framegen_rows::MADE], (unsigned long long)n[framegen_rows::SKIP],
(unsigned long long)n[framegen_rows::GAME], (unsigned long long)n[framegen_rows::OWN], (unsigned long long)bare,
(unsigned long long)own_motion);
n[0] = n[1] = n[2] = n[3] = bare = own_motion = 0;
}
return m;
};
fsr3_inputs::Frame f;
if (!fsr3_inputs::take(f) || !f.depth_image || !f.motion_image) {
bare++;
static bool told = false; // ((Triforce) the reason the lab's fgprobe1 run made no frame of AMD's: never said)
if (!told)
told = true, logf("vk: AMD FSR frame generation: a present without the scene's depth and motion (a menu or a movie, or "
"no upscale made them this frame): the renderer's own interpolation makes its frame");
return counted(framegen_rows::OWN);
}
own_motion += f.chain_motion;
fsr4fg::Inputs in;
in.color = fg.frames[cur].image; // (R8G8B8A8_UNORM: ensure_size; HDR Native's 10-bit format goes to the renderer's own)
in.width = uw, in.height = uh;
in.pq = present_native_pq.load(std::memory_order_relaxed) || fg.format != VK_FORMAT_R8G8B8A8_UNORM;
// ((Triforce, FG-2) this frame's picture without the HUD, cut as the picture is: AMD's library makes no frame without it)
in.hudless = in.pq ? VK_NULL_HANDLE : hudless_for(cmd, f.frame, uw, uh);
in.peak_nits = (float)std::clamp(opts.hdr_peak, 400, 10000);
in.depth = f.depth_image, in.motion = f.motion_image;
in.render_width = f.width, in.render_height = f.height;
in.jitter[0] = f.jitter[0], in.jitter[1] = f.jitter[1];
in.near_plane = f.camera.near_plane, in.far_plane = f.camera.far_plane, in.fov_y = f.camera.fov_y;
std::memcpy(in.view, f.view, sizeof in.view);
in.frame_ms = f.frame_ms;
in.reset = f.reset || !blend;
in.sequence = sequence;
const framegen_rows::Made made = fsr4fg::generate(cmd, in);
const VkImage image = made == framegen_rows::MADE ? fsr4fg::made_image() : VK_NULL_HANDLE;
if (made != framegen_rows::MADE || !image)
return counted(framegen_rows::game_frame_alone(made) ? made : framegen_rows::OWN);
const VkImageCopy c{{VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, {0, 0, 0}, {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, {0, 0, 0},
{uw, uh, 1}};
vkCmdCopyImage(cmd, image, VK_IMAGE_LAYOUT_GENERAL, set.frame[0].image, VK_IMAGE_LAYOUT_GENERAL, 1, &c);
return counted(framegen_rows::MADE);
}
bool framegen_record(VkCommandBuffer cmd, VkImageView src, uint32_t sw, uint32_t sh, const int crop[4]) {
const PresentSourceRect source = present_source_rect(sw, sh, crop);
const int x0 = (int)source.x, y0 = (int)source.y, w = (int)source.w, h = (int)source.h;
if (!src || w <= 0 || h <= 0 || !init() || !ensure_size(cmd, (uint32_t)w, (uint32_t)h)) return false;
if (!fg.thread_started) {
fg.thread_started = true;
std::thread(display_main).detach();
}
const int64_t now = now_ns(), before = fg.last_at;
const bool gap = fg.last_at && now - fg.last_at > 100000000; // (a loading hitch: nothing to blend)
if (fg.last_at && !gap)
fg.game_ns = fg.game_ns ? (fg.game_ns * 7 + (now - fg.last_at)) / 8 : now - fg.last_at;
fg.last_at = now;
OutSet* set;
{
std::unique_lock lk(fg.mu);
// (Triforce) While RivaTuner holds the presents, this wait is what holds the game: the display thread takes the
// game's frames at the cap, and a frame is recorded only once the one before has been taken up, so the game runs
// one frame ahead of what is shown, as with the game's own wait, not three (a set shown, one waiting, one made)
fg.cv.wait(lk, [] { return !fg.sets[fg.next_set].busy && (!pace::display_yields() || fg.jobs.empty()); });
set = &fg.sets[fg.next_set];
set->busy = true;
fg.next_set = (fg.next_set + 1) % 3;
}
const int cur = fg.cur, prev = cur ^ 1, m = multiplier();
const bool blend = fg.have_prev && !gap;
static uint64_t sequence = 0;
sequence++;
fg.src_w = sw, fg.src_h = sh, fg.source = source; // ((Triforce, FG-2) the picture without the HUD is cut as this one)
const uint32_t uw = (uint32_t)w, uh = (uint32_t)h;
const uint32_t qw = (uw + 3) / 4, qh = (uh + 3) / 4;
full_barrier(cmd);
const int32_t pc[4] = {x0, y0, w, h};
pass(cmd, fg.copy, {{src, fg.nearest}, {fg.frames[cur].view, VK_NULL_HANDLE}}, uw, uh, pc, sizeof pc);
pass(cmd, fg.luma, {{fg.frames[cur].view, fg.nearest}, {fg.lumas[cur].view, VK_NULL_HANDLE}}, qw, qh);
if (fg.half.pipe) // ((Triforce) the fine vectors' half-size luma)
pass(cmd, fg.half, {{fg.frames[cur].view, fg.nearest}, {fg.halfs[cur].view, VK_NULL_HANDLE}}, (uw + 1) / 2, (uh + 1) / 2);
set->made = 0;
set->shown = -1;
if (opts.framegen == 1) { // (Auto: a frame for each of the display's refreshes, at its own time)
const int mhz = display_refresh_mhz();
const int64_t period = (int64_t)(1e12 / (mhz > 0 ? mhz : 60000));
float t[kMaxMult];
int64_t at[kMaxMult];
int n;
if (blend) {
n = auto_slots(before, now, period, fg.slot_next, t, at, std::min(fg.mult, kMaxMult));
} else { // (nothing to blend from: the game's frame, and the timeline starts at it)
n = 1, t[0] = 1.0f, at[0] = now, fg.slot_next = now + period;
}
bool flow = false;
for (int k = 0; k < n; k++) {
set->game[k] = t[k] > 0.999f;
set->due[k] = at[k] + fg.game_ns;
if (set->game[k]) {
const VkImageCopy c{{VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, {0, 0, 0}, {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
{0, 0, 0}, {uw, uh, 1}};
vkCmdCopyImage(cmd, fg.frames[cur].image, VK_IMAGE_LAYOUT_GENERAL, set->frame[k].image,
VK_IMAGE_LAYOUT_GENERAL, 1, &c);
continue;
}
if (!flow) flow = true, make_vectors(cmd, prev, cur, uw, uh);
make_frame(cmd, prev, cur, set->frame[k], t[k], uw, uh);
}
set->shown = n;
set->made = n > 0 && !set->game[n - 1] ? n : std::max(0, n - 1); // (for framegen_made_image: any made)
full_barrier(cmd);
fg.have_prev = true;
fg.cur = prev;
fg.recorded = set;
fg.recorded_at = now;
return true;
}
// (hookshot) At 2x, AMD FSR 3.1's frame interpolation where it runs; it makes the midpoint only, so 3x and more (and Auto,
// above) stay with the interpolation below, as does any present it does not take
// (Triforce) The row's AMD FSR 4: AMD's library makes that one frame where it can. It is given every frame, one with
// nothing before it too (its history starts there); at a new history it makes none and none is blended in its place
bool none = false;
if (opts.framegen == framegen_rows::FSR4) {
const framegen_rows::Made by_fsr4 = fsr4_frame(cmd, *set, cur, uw, uh, sequence, blend);
set->made = blend && by_fsr4 == framegen_rows::MADE ? 1 : 0;
// ((Triforce, FG-2) a new history, or no picture without the HUD this frame: the game's frame alone, never the
// renderer's own frame in the place of AMD's. (HDR Native, an RGBA16F scene: OWN, the renderer's own below))
none = framegen_rows::game_frame_alone(by_fsr4);
}
if (set->made == 0 && !none && blend && m == 2 && fsr3fi::frames_wanted()) set->made = fsr3fi_frame(cmd, *set, cur, uw, uh, sequence);
if (set->made == 0 && !none && blend && m > 1) {
make_vectors(cmd, prev, cur, uw, uh);
for (int k = 1; k < m; k++) make_frame(cmd, prev, cur, set->frame[k - 1], (float)k / (float)m, uw, uh);
set->made = m - 1;
}
const VkImageCopy c{{VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, {0, 0, 0}, {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, {0, 0, 0},
{uw, uh, 1}};
vkCmdCopyImage(cmd, fg.frames[cur].image, VK_IMAGE_LAYOUT_GENERAL, set->frame[set->made].image,
VK_IMAGE_LAYOUT_GENERAL, 1, &c);
full_barrier(cmd);
fg.have_prev = true;
fg.cur = prev;
fg.recorded = set;
fg.recorded_at = now;
return true;
}
void framegen_queue(uint64_t value) {
if (!fg.recorded) return;
std::lock_guard lk(fg.mu);
fg.jobs.push_back({fg.recorded, fg.recorded_at, value});
fg.recorded = nullptr;
fg.cv.notify_all();
}
void framegen_queue_held(VkImage image, VkImageView view, uint32_t w, uint32_t h, const int crop[4]) {
if (!image || !fg.thread_started) return; // (no display thread yet: nothing of the game's was shown through it)
std::lock_guard lk(fg.mu);
int waiting = 0;
for (const Job& j : fg.jobs) waiting += !j.set;
if (waiting >= 2) return; // (the display thread behind: the picture it will show is the same one)
Job j{nullptr, now_ns(), 0};
j.held = image, j.held_view = view, j.held_w = w, j.held_h = h;
if (crop) std::memcpy(j.crop, crop, sizeof j.crop), j.has_crop = true;
fg.jobs.push_back(j);
fg.cv.notify_all();
}
void framegen_new_history() {
// (blend comes out false at the next framegen_record, as after a gap: no frame between this one and the one before the
// hold, the Auto timeline started at it, AMD FSR 4 given it as a new history; FSR 3.1, not called then, finds a gap in the
// presents' sequence after it. The game's frame time is measured afresh)
fg.have_prev = false;
fg.last_at = 0;
}
/// Each present (vk_replay.cpp): ultrawide's menu setting to the game's side, and the sides to treat now
void wide_sides_tick() { wide_now = wide_sides(); }
/// (Triforce) Each present (vk_replay.cpp), outside framegen_record's recording: what frame generation holds and no longer
/// uses is let go (AMD FSR 4's context once the row has left it, or after its failure: vk_fsr4.h)
void framegen_present_tick() { fsr4fg::present_tick(); }
/// (Triforce) The process is about to exit (HOS_OPT_EXITING, host/hos_relaunch.c): AMD's frame generation library is not
/// called from here on (vk_fsr4.h fsr4fg::exiting; framegen_rows.h teardown says why that is all there is to do)
void framegen_exiting() { fsr4fg::exiting(); }
} // namespace mkwvk