// 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 #include #include #include #include #include #include #include #include // ((Triforce) std::exchange: framegen_idle) #include #include "vk_framegen.h" #include "framegen_rows.h" // (Triforce: the Frame generation row's values, AMD FSR 4 among them) #include "framegen_idle.h" // ((Triforce, 11-21, PRESENT-18) its images let go once it has been off a while) #include "framegen_refresh.h" // ((Triforce, 11-21, the upscaler audit's UPS-05) a fixed multiplier and the display's refresh) #include "vk_antilag.h" // ((Triforce) 11-24, AMD Anti-Lag 2: which game frame a set shows) #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 "present_mode.h" // ((Triforce) 11-24: FIFO, for a set on the game's timeline) #include "vk_temporal.h" // ((Triforce, S1) temporal_sharpens_3d: the upscaler sharpened the 3D before the HUD) #include "wide_sides_rule.h" // ((Triforce) when ultrawide's menu sides show) #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 "vk_n64.h" // ((Triforce, N64 Classic, step 4) its TV in the window filter's place: framegen_scale) #include #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_hud_comp_spv.h" // ((Triforce) AMD FSR 3.1's interpolation source with the HUD marked: hud_source) #include "shaders/fg_hud_composite_comp_spv.h" // ((Triforce) the HUD over FSR 3.1's made frames: fsr3fi_frames) #include "shaders/fg_hud_layer_comp_spv.h" // ((Triforce) how see-through the HUD is: hud_layer) #include "shaders/fg_luma_comp_spv.h" #include "shaders/fg_median_comp_spv.h" #include "shaders/fg_scale_comp_spv.h" #include "shaders/n64_tv_comp_spv.h" // ((Triforce, N64 Classic) shaders/n64_tv.comp) #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 "shaders/colour_vision_comp_spv.h" // ((Triforce) shaders/colour_vision.comp: the Colour vision correction row) #include "colour_vision.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) int swapchain_present_mode(); // ((Triforce) 11-24, vk_main.cpp: present_mode.h's numbers, -1 none: the timeline's FIFO) extern std::atomic present_kind; // (vk_replay.cpp: the frame log's) extern std::atomic 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] = {}; uint64_t antilag_id = 0; // ((Triforce) 11-24, AMD Anti-Lag 2: the game frame this set shows, vk_antilag.h) // ((Triforce) 11-24, RX_VK_FG_GAME_TIMELINE=1) a fixed multiplier's set on the game's own timeline: due[0..made] its frames' // times (fixed_dues), shown at them by display_main; frame_ns the game's frame time and extra_ns the GPU's delay it was // made with (pace_delay) bool by_time = false; int64_t frame_ns = 0, extra_ns = 0; }; 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 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 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; // ((Triforce) 11-24, RX_VK_FG_GAME_TIMELINE=1: the GPU's delay a fixed multiplier's sets are shown at past their lead, // pace_delay. The display thread writes it, framegen_record reads it) std::atomic late_ns{0}; 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; // ((Triforce, N64 Classic, step 4) its TV's pipeline (shaders/n64_tv.comp: init_tv), and beside ultrawide's menu sides the // window-size image the filter draws them into first) Pipe tv; bool tv_tried = false; Img tv_img; uint32_t tv_w = 0, tv_h = 0; // ((Triforce) 11-24, the Colour vision correction row: its pipeline (shaders/colour_vision.comp, init_cv), made at the first // present that asks for it, and its image at the window's size; cv_shown: framegen_scale's last image is cv_img) Pipe cv; bool cv_tried = false, cv_shown = false; Img cv_img; uint32_t cv_w = 0, cv_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::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; } // ---- ((Triforce) 11-24) A fixed multiplier's sets on the game's own timeline (RX_VK_FG_GAME_TIMELINE=1; off: as before, each // set's frames a step apart from the last frame shown, RX_VK_FG_TIMELINE's). The game hands its frames over 15 to 22 ms apart at // ~55 fps: shown evenly apart, every set's frames took the same ~6 ms whatever the game frame's own time, a stutter with even // frames. On the timeline each frame moves as far as its time. Under FIFO (V-Sync on) the screen shows one present a refresh at // most: the frames go a refresh apart at the least (fifo_slot), and a set whose frames have no room left out made frames, spread // over it (fifo_catch_up, fifo_pick), so they do not queue and hold the game back ---- /// Of a set's made + 1 frames, frame k at its place between the game's frame before (last) and this one (now), (k + 1) / /// (made + 1) of the way, delay later (pace_lead and pace_delay's) void fixed_dues(int64_t last, int64_t now, int made, int64_t delay, int64_t* due) { for (int k = 0; k <= made; k++) due[k] = last + (now - last) * (k + 1) / (made + 1) + delay; } /// The set's delay before the GPU's: its made frames come from the game frame that came at now, so none of them can be shown /// before it. made / (made + 1) of the game's frame time: the first frame due at now after a game frame of the usual time int64_t pace_lead(int64_t frame, int made) { return made > 0 ? frame * made / (made + 1) : 0; } /// The GPU's delay past the lead, from what the last set needed (need: its own delay and how late its first frame could be /// shown, a millisecond to spare): up by half of what is missing, down by a sixteenth of what is left over, at most a game /// frame. A hitch (a need past four game frames) leaves it as it was int64_t pace_delay(int64_t extra, int64_t need, int64_t frame) { if (frame <= 0 || need > 4 * frame) return extra; extra += need > extra ? (need - extra) / 2 : (need - extra) / 16; return std::clamp(extra, 0, frame); } /// Under FIFO: at its due time, but no sooner than a refresh after the frame before was given (slot; 0 none yet). A slot past /// by more than half a refresh (the thread held up) gives now int64_t fifo_slot(int64_t due, int64_t slot, int64_t refresh, int64_t now) { if (slot <= 0 || refresh <= 0) return due; const int64_t next = slot + refresh; return std::max(due, next < now - refresh / 2 ? now : next); } /// The set's frame shown k-th when `shown` of its `frames` are: at the even steps across it, the last always the game's int fifo_pick(int frames, int shown, int k) { return ((k + 1) * frames * 2 + shown) / (2 * shown) - 1; } /// How many made frames a set leaves out under FIFO so that its game frame (due[made]) is shown no more than a refresh after /// its own time, the frames a refresh apart from slot. At most made - 1; 0 without a slot or a refresh int fifo_catch_up(int made, const int64_t* due, int64_t slot, int64_t refresh, int64_t now) { if (made < 2 || slot <= 0 || refresh <= 0) return 0; int64_t next = slot + refresh; if (next < now - refresh / 2) next = now; for (int shown = made + 1; shown > 2; shown--) { int64_t at = std::max(due[fifo_pick(made + 1, shown, 0)], next); for (int k = 1; k < shown; k++) at = std::max(due[fifo_pick(made + 1, shown, k)], at + refresh); if (at - due[made] <= refresh) return made + 1 - shown; } return made - 1; } bool game_timeline() { static const bool on = getenv("RX_VK_FG_GAME_TIMELINE") && atoi(getenv("RX_VK_FG_GAME_TIMELINE")) == 1; return on; } 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 types, uint32_t push, const VkSpecializationInfo* spec = nullptr) { Pipe made; const auto undo = [&] { drop_pipe(made); return false; }; std::vector 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 = ≺ 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 w(code, code + bytes / 4); return sharpen_rules::storage_format_patch(w, sharpen_rules::kSpvRgba8, sharpen_rules::kSpvRgba16f) ? w : std::vector{}; }; const std::vector 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 e = patched(FSR1_EASU_COMP_SPV, sizeof FSR1_EASU_COMP_SPV); const std::vector 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; } /// ((Triforce, 11-22, N64 Classic, step 4) its TV's pipeline (shaders/n64_tv.comp), made at its first present; rgba8 only (the /// TV does not run on HDR Native's PQ picture). False: not made (said once), and the window's filter draws as before bool init_tv() { if (scale.tv_tried) return scale.tv.pipe != VK_NULL_HANDLE; scale.tv_tried = true; const VkDescriptorType S = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, I = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE; if (!init_scale() || !make_pipe(scale.tv, N64_TV_COMP_SPV, sizeof N64_TV_COMP_SPV, {S, I, S, S}, 48)) { drop_pipe(scale.tv); logf("vk: N64 Classic: its TV pass could not be made: the window's filter draws as before"); return false; } logf("vk: N64 Classic: its TV pass is ready"); return true; } /// ((Triforce) 11-24) The Colour vision correction's pipeline (shaders/colour_vision.comp), made at its first present. False: /// not made (said once), and the picture is shown uncorrected bool init_cv() { if (scale.cv_tried) return scale.cv.pipe != VK_NULL_HANDLE; scale.cv_tried = true; const VkDescriptorType S = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, I = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE; if (!init_scale() || !make_pipe(scale.cv, COLOUR_VISION_COMP_SPV, sizeof COLOUR_VISION_COMP_SPV, {S, I}, 20)) { drop_pipe(scale.cv); logf("vk: Colour vision correction: its pass could not be made: the picture is shown uncorrected"); return false; } 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 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 c = patched(FG_COPY_COMP_SPV, sizeof FG_COPY_COMP_SPV); const std::vector 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> 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(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; int64_t line_slot = 0; // ((Triforce) 11-24: the time the last frame of a set on the game's timeline was given, fifo_slot) 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); antilag_present_frame = 0; // ((Triforce) 11-24: no game frame of its own, no Anti-Lag 2 present stage) 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(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); // (a fixed multiplier's set with made frames: each frame at its own time, a step apart on the same line, whatever the // Frame pacing row says. With the row Off the set went out as it came, all within a millisecond: a swapchain that does // not wait for a refresh (VSync Off: immediate, mailbox) shows each present at once, so the game's frame covered the // made ones, and the picture moved at the game's own rate with the made frames paid for. Not beside RivaTuner, which // spaces the presents itself. RX_VK_FG_TIMELINE=0: as they come with the row Off, as before) static const bool fg_spaced = !(getenv("RX_VK_FG_TIMELINE") && atoi(getenv("RX_VK_FG_TIMELINE")) == 0); const bool spaced = pace || (fg_spaced && !timed && j.set->made > 0 && !pace::display_yields()); if (timed) menu_note_frame(true, 0); // (the overlay's game rate: Auto counts each game frame, given a refresh or not) // ((Triforce) 11-24, RX_VK_FG_GAME_TIMELINE=1) a fixed multiplier's set on the game's timeline: each frame at its own // time (due), under FIFO a refresh apart at the least and the made frames the refresh has no room for left out; the // GPU's delay learned from when the set's work was done (pace_delay). Not beside RivaTuner, which spaces them itself const bool on_line = !timed && j.set->by_time && !pace::display_yields(); const bool fifo = on_line && swapchain_present_mode() == present_mode::kFifo; const int mhz = display_refresh_mhz(); const int64_t refresh = fifo ? (int64_t)(1e12 / (mhz > 0 ? mhz : 60000)) : 0; const int frames = j.set->made + 1; const int left_out = fifo ? fifo_catch_up(j.set->made, j.set->due, line_slot, refresh, now_ns()) : 0; const int show_n = on_line ? frames - left_out : shown; for (int i = 0; i < show_n; i++) { const int k = on_line && left_out ? fifo_pick(frames, show_n, i) : i; int64_t due = timed || on_line ? j.set->due[k] : spaced ? next_show : 0; if (fifo) due = fifo_slot(due, line_slot, refresh, now_ns()); const int64_t now = now_ns(); if (due < now) due = now; if (!timed && !on_line && due > j.at + interval + step) due = j.at + interval + step; if ((timed || on_line) && 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(); if (on_line && i == 0) { // (the set's work done on the GPU: how late it could be shown, for the next sets' delay) while (vk_timeline_completed() < j.value && now_ns() - due < 50000000) wait_until_near(250000); const int64_t need = j.set->extra_ns + (now_ns() - j.set->due[0]) + 1000000; fg.late_ns.store(pace_delay(fg.late_ns.load(std::memory_order_relaxed), need, j.set->frame_ns), std::memory_order_relaxed); } const int64_t t = now_ns(); line_slot = fifo ? t : 0; present_kind.store(timed ? (j.set->game[k] ? 0 : 1) : k < j.set->made ? 1 : 0); // ((Triforce) 11-24, AMD Anti-Lag 2: the set's game frame gives its frame's present stage, a made frame none) antilag_present_frame = (timed ? j.set->game[k] : k == j.set->made) ? j.set->antilag_id : 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 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; extern std::atomic world_drawn_at; extern int64_t presents_now; extern std::atomic 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. (Triforce) The program that draws it also draws the centred // backdrop of a system menu over the title, which does get the sides: the host says which it is, wide_sides_rule.h) const bool centred_menu = host->wide_backdrop && host->wide_backdrop() != 0; // ((Triforce) bit 4, the system overlay's screen (host/oot_wide_hud.h): whole while the item wheel's background is drawn, // which from the wheel's first opening on the overlay draws too (squeezed, it went into the middle 16:9); squeezed at // every other time, so the system menus' header and guides stay where this game's options line up with them) const bool wheel_now = presents_now - wheel_drawn_at.load(std::memory_order_relaxed) <= wide_sides_rule::kGrace && !centred_menu; const uint32_t overlay_bit = wheel_now ? 0x10u : 0u; const float margin = host->wide_menus(overlay_bit | (opts.wide_menus ? 0xfu & ~kMenus : 0xfu)); // ((Triforce) 3, Extended: the menus at their own size with their side panes at the screen's edges, host/hos_wide_ui.c: no // sides of their own) const int centred = opts.wide_menus == 1 || opts.wide_menus == 2 ? opts.wide_menus : 0; const int side = wide_sides_rule::show(centred, margin, presents_now, world_drawn_at.load(std::memory_order_relaxed), wheel_drawn_at.load(std::memory_order_relaxed), centred_menu); wide_possible.store(centred && margin > 0.0f, std::memory_order_relaxed); if (side) w.x0 = margin, w.side = side; 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, the last picture's blur at %lld%s)", w.side ? "on" : "off", (long long)presents_now, (long long)world_drawn_at.load(std::memory_order_relaxed), (long long)wheel_drawn_at.load(std::memory_order_relaxed), centred_menu ? ", a menu's centred backdrop" : ""); told = w.side; } return w; } std::atomic wide_now; // (wide_sides_tick's, each present) bool wide_sides_possible() { return wide_possible.load(std::memory_order_relaxed); } VkImage scale_picture(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), drop_img(scale.tv_img); scale.cropped_w = scale.cropped_h = scale.scaled_w = scale.scaled_h = scale.small_w = scale.small_h = 0; scale.tv_w = scale.tv_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, 11-22, N64 Classic, step 4) its TV in the filter's place (upscaler_rows.h kTvFilter; n64_tv_now: not on HDR // Native's PQ picture, nor under DLSS frame generation): from the picture to its place in the window at the window's own // pixels, the glow from the picture at 1/8 (the copy ultrawide's menu sides blur). Beside those sides the filter draws // them first, into an image of the window's size, and the TV goes over it. Its pipeline not made: the filter, as before if (!pq && n64_tv_now(pq) && init_tv()) { const n64_rules::Look look = n64_look(); const n64_rules::AnimFrame f = n64_anim(); // ((step 8) the TV switching off and on: 1, 1 and no flash at rest) 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); } struct { int mode; float sharp; float x0, x1; int side; } sp{0, 0.0f, wide.x0, 1.0f - wide.x0, 3}; pass(cmd, filter, {{src, scale.linear}, {scale.small.view, VK_NULL_HANDLE}, {src, scale.linear}}, smw, smh, &sp, sizeof sp); VkImageView picture = src; if (wide.side) { if (scale.tv_w != dw || scale.tv_h != dh) { { std::lock_guard qk(dev.present_mu); device_wait_idle_locked(); } drop_img(scale.tv_img); scale.tv_w = scale.tv_h = 0; if (!make_img(scale.tv_img, dw, dh, scale.format, VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_SAMPLED_BIT)) { drop_img(scale.tv_img); return VK_NULL_HANDLE; } scale.tv_w = dw, scale.tv_h = dh; to_general(cmd, scale.tv_img.image); } sp.side = wide.side; pass(cmd, filter, {{src, scale.linear}, {scale.tv_img.view, VK_NULL_HANDLE}, {wide.side == 1 ? scale.small.view : src, scale.linear}}, dw, dh, &sp, sizeof sp); picture = scale.tv_img.view; } const int mask = n64_rules::mask_shown(look.mask, dh); // ((11-22, the owner's test) the TV's screen for the mouse (vk_n64.h n64_screen_point), and the options' own screens // under it with the game's (vk_menu.cpp menu_tv_layer: their layer of this place, drawn now; null when none shows) n64_screen_drawn(look.curvature, f.scale_x, f.scale_y); const VkImageView menu = menu_tv_layer(cmd); struct { float lines, scan, mask, bleed, glow, curve, saturation; int mask_type; float anim_x, anim_y, flash; int menu; } tp{ (float)n64_rules::kTvLines, n64_rules::scanline_strength(look.scanlines, dh), look.mask_strength, n64_rules::tv_bleed_uv(look.bleed, source.w, source.h), look.glow, look.curvature, look.saturation, mask, f.scale_x, f.scale_y, f.flash, menu ? 1 : 0}; // ((11-22, the owner's second look) the glow's 1/8 copy made again with the options over the picture (the TV's menu 2): // from the picture alone, the glow added a blur of the game's own panes under them through their plates. After the // ultrawide sides took the copy before it, which stays the picture's) if (menu) { tp.menu = 2; pass(cmd, scale.tv, {{picture, scale.linear}, {scale.small.view, VK_NULL_HANDLE}, {picture, scale.linear}, {menu, scale.linear}}, smw, smh, &tp, sizeof tp); tp.menu = 1; } pass(cmd, scale.tv, {{picture, scale.linear}, {scale.scaled.view, VK_NULL_HANDLE}, {scale.small.view, scale.linear}, {menu ? menu : scale.small.view, scale.linear}}, dw, dh, &tp, sizeof tp); // (each new place, preset, mask or scanline strength said once the switch is over: a report of moire or a dim picture // then says which. (11-22) Before, the place and the mask alone, so the first line, written as the TV switched on with // Off's look still shown, said "scanlines 0%" for good) static uint64_t told = ~0ull; const int shown = n64_shown(); const uint64_t now = (uint64_t)dw << 44 | (uint64_t)dh << 28 | (uint64_t)(shown & 3) << 24 | (uint64_t)(mask & 7) << 16 | (uint64_t)std::lround(std::clamp(tp.scan, 0.0f, 1.0f) * 1000.0f); if (f.done && now != told) { told = now; static const char* const masks[] = {"", "no mask", "the aperture grille", "the slot mask", "the shadow mask"}; logf("vk: N64 Classic: the TV (%s) at %ux%u from a %ux%u picture: scanlines %.0f%%, %s%s", n64_rules::name(shown), dw, dh, source.w, source.h, (double)(tp.scan * 100.0f), masks[mask >= 1 && mask <= 4 ? mask : 1], wide.side ? ", over ultrawide's menu sides" : ""); } return scale.scaled.image; } // (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; } // ((Triforce) 11-24) The window's picture (scale_picture above), then the Colour vision correction row's pass over it // (shaders/colour_vision.comp): from the scaled picture, or from src's crop at the window's size where nothing scaled it. Off, // the default, adds nothing. Not over HDR Native's PQ picture (said once): the SDR picture only, which the HDR10 expansion // (framegen_hdr) then takes from framegen_scaled_view as it takes the scaled one 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 VkImage scaled = scale_picture(cmd, src, image, sw, sh, crop, dw, dh); scale.cv_shown = false; const int mode = opts.colour_vision; if (!colour_vision::enabled(mode) || !src || !image || !sw || !sh || !dw || !dh) return scaled; if (present_native_pq.load(std::memory_order_relaxed)) { static bool told; if (!told) told = true, logf("vk: Colour vision correction: not over HDR Native's picture: shown uncorrected"); return scaled; } if (scaled && scale.format != VK_FORMAT_R8G8B8A8_UNORM) return scaled; const PresentSourceRect source = present_source_rect(sw, sh, crop); if (!source.w || !source.h || !init_cv()) return scaled; if (scale.cv_w != dw || scale.cv_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.cv_img); scale.cv_w = scale.cv_h = 0; if (!make_img(scale.cv_img, dw, dh, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT)) { drop_img(scale.cv_img); return scaled; } scale.cv_w = dw, scale.cv_h = dh; to_general(cmd, scale.cv_img.image); } struct { float uv[4]; int mode; } pc{{0.0f, 0.0f, 1.0f, 1.0f}, mode}; if (!scaled) { pc.uv[0] = source.x / (float)sw, pc.uv[1] = source.y / (float)sh; pc.uv[2] = (source.x + source.w) / (float)sw, pc.uv[3] = (source.y + source.h) / (float)sh; } pass(cmd, scale.cv, {{scaled ? scale.scaled.view : src, scale.linear}, {scale.cv_img.view, VK_NULL_HANDLE}}, dw, dh, &pc, sizeof pc); static int told = 0; if (told != mode) { told = mode; static const char* const names[] = {"", "protan", "deutan", "tritan"}; logf("vk: Colour vision correction: %s, at %ux%u", names[mode], dw, dh); } scale.cv_shown = true; return scale.cv_img.image; } VkImageView framegen_scaled_view() { return scale.cv_shown ? scale.cv_img.view : 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_hud_before(VkCommandBuffer cmd, Texture* picture, uint32_t level); void framegen_hudless_note(VkCommandBuffer cmd, Texture* out, uint32_t level) { // ((Triforce) 11-24) the Frame generation by row at AMD FSR 3.1: the same picture, copied for its HUD mark instead (hud_source) if (fsr3fi::frames_wanted()) return framegen_hud_before(cmd, out, 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 // ((Triforce) 11-24) The HUD for AMD FSR 3.1's frame interpolation (the Frame generation by row). The game draws its HUD (hearts, // minimap, button prompts) into the same picture as the scene, after its gameplay upscale, and has no picture without it of its // own. Guessing the HUD as the pixels the same in both frames while the scene moves (the patched passes' kKeepStatic) fails for // its see-through parts (the minimap, the prompts' backing), its soft edges and anything animated, which were moved with the // scene: a HUD that smears and shakes with every turn of the camera. So the picture is copied just after the upscale wrote it // (framegen_hud_before, from vk_draw.cpp's dispatch of the upscale, whoever drew it: framegen_hudless_note); at the present what // differs from that copy in the picture as presented is the HUD, marked in the interpolation source's alpha (shaders/fg_hud.comp, // vk_fsr3fi.h Inputs::hud_mask). A frame without the copy (no gameplay upscale: a menu, a movie) keeps the guess. // The mark alone still lets the HUD smear: the frame before's HUD stays in the interpolation's previous source and is dragged // with the scene, and the inpainting spreads its colours into the holes a turn opens beside it. So the copy itself, cropped as // the frame (hud.hudless), is what FSR 3.1 interpolates: its own picture without the HUD, the HUD in neither frame, and this // frame's HUD laid over the made frames after (vk_fsr3fi.h Inputs::present; RX_VK_FSR_FG_HUDLESS=0: the mark alone). The copy // only where the HUD is: elsewhere the presented frame's own pixels, sRGB-encoded under the HUD (the copy is linear light, from // before the game's last pass: given whole, FSR 3.1 found the two different everywhere and every made frame was the game's own). // Laid over whole, the presented pixel still brings this frame's scene with it wherever the HUD lets the scene through (the // hearts' glow, the item box's shadow, the faint icons, the minimap): a halo of the real frame's background around every HUD // element of every made frame. So how see-through each HUD pixel is (T) is learned from frame to frame, the HUD staying where it // is while the scene behind it moves (shaders/fg_hud_layer.comp: hud.layer, the HUD's own colour and T); FSR 3.1 is given the // picture without the HUD as its present too, so it lays nothing over its made frames, and the HUD goes over each made frame's // own scene (shaders/fg_hud_composite.comp); where T is not known, the presented pixel (RX_VK_FSR_FG_HUD_LAYER=0: everywhere). // Not AMD's library's picture without the HUD (the Frame generation row's AMD FSR, Hudless above): that one is taken at the // same dispatches, for that row only namespace { struct HudState { std::mutex mu; // (framegen_hud_before is called from the replay's workers) bool init = false, failed = false; Pipe mask; // (the picture after the upscale, and as presented: its format and size; two of each, this frame's at k and the frame // before's at k ^ 1, for the HUD layer's fit: prev says the frame before's are there and its pair) Img before[2], after[2]; int k = 0; bool prev = false; Pipe fit_pipe, over_pipe; bool layer_failed = false; Img fit; // (the HUD layer's fit, kept from frame to frame: the crop's size, RGBA16F) Img layer; // (the HUD's colour and how see-through it is, the crop's size, RGBA16F: fg_hud_layer.comp) bool layered = false; // (this present's layer made: fsr3fi_frames lays the HUD over the made frames with it) uint64_t layers = 0; // (presents with it: the 600-present line) Img source; // (the interpolation source: the frame's crop, RGBA8, the HUD's alpha 0) Img hudless; // (the same crop of the picture before the HUD, RGBA8: FSR 3.1's own picture without the HUD) VkFormat format = VK_FORMAT_UNDEFINED; uint32_t pw = 0, ph = 0, sw = 0, sh = 0; VkImage picture = VK_NULL_HANDLE; // (the picture copied: framegen_hud_before's, its image) const Texture* tex = nullptr; // (and its texture: not read unless texture_destroyed_since says it is still there) uint32_t tex_id = 0; uint64_t tex_noted = 0; bool fresh = false; // (copied since the last present) bool ready = false; // (this present's source made: fsr3fi_frames gives it) struct Dead { uint64_t frame, value; Img img; }; std::vector dead; // (images replaced, freed once the GPU is past them) bool told = false; uint64_t marked = 0; // (presents given to FSR 3.1 with the HUD marked: its 600-present line) } hud; void hud_retire(Img& i) { if (i.image) hud.dead.push_back({texture_frame_no(), vk_retirement_value(), i}); i = {}; } void hud_free_dead() { const uint64_t now = texture_frame_no(), done = vk_timeline_completed(); for (size_t i = 0; i < hud.dead.size();) { if (now - hud.dead[i].frame > 4 && hud.dead[i].value && done >= hud.dead[i].value) { drop_img(hud.dead[i].img); hud.dead[i] = hud.dead.back(); hud.dead.pop_back(); } else { i++; } } } bool hud_image(VkCommandBuffer cmd, Img& i, uint32_t w, uint32_t h, VkFormat f, VkImageUsageFlags usage) { if (i.image) return true; if (!make_img(i, w, h, f, usage)) return false; to_general(cmd, i.image); return true; } /// FSR 3.1's frame interpolation off for a while (framegen_idle): the HUD's images let go too, once the GPU is past them void hud_release() { std::lock_guard lk(hud.mu); for (int i = 0; i < 2; i++) hud_retire(hud.before[i]), hud_retire(hud.after[i]); hud_retire(hud.fit), hud_retire(hud.layer), hud_retire(hud.source), hud_retire(hud.hudless); hud.format = VK_FORMAT_UNDEFINED, hud.pw = hud.ph = hud.sw = hud.sh = 0; hud.picture = VK_NULL_HANDLE, hud.tex = nullptr, hud.fresh = hud.prev = hud.ready = hud.layered = false; hud_free_dead(); } /// Whether the HUD goes over FSR 3.1's made frames by its layer (RX_VK_FSR_FG_HUD_LAYER, read once): with the picture without /// the HUD only (RX_VK_FSR_FG_HUDLESS) bool hud_layer_wanted() { static const bool on = [] { const char* e = getenv("RX_VK_FSR_FG_HUD_LAYER"); return !(e && *e && atoi(e) == 0); }(); return on; } bool hudless_wanted() { static const bool on = [] { const char* e = getenv("RX_VK_FSR_FG_HUDLESS"); return !(e && *e && atoi(e) == 0); }(); return on; } /// The HUD layer of this present (hud.layer, shaders/fg_hud_layer.comp), from this frame's pictures and the frame before's: /// how see-through the HUD is where it is known. False: none (the HUD laid over the made frames whole, as presented) bool hud_layer(VkCommandBuffer cmd, int x0, int y0, uint32_t w, uint32_t h) { if (hud.layer_failed || !hud_layer_wanted()) return false; const VkDescriptorType S = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, I = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE; if (!hud.fit_pipe.pipe && (!make_pipe(hud.fit_pipe, FG_HUD_LAYER_COMP_SPV, sizeof FG_HUD_LAYER_COMP_SPV, {S, S, S, S, I, I}, 20) || !make_pipe(hud.over_pipe, FG_HUD_COMPOSITE_COMP_SPV, sizeof FG_HUD_COMPOSITE_COMP_SPV, {I, S, S}, 12))) { drop_pipe(hud.fit_pipe), drop_pipe(hud.over_pipe); hud.layer_failed = true; logf("vk: fsr3fi: the HUD layer's pipelines were not made: the HUD laid over the made frames as presented"); return false; } const VkImageUsageFlags rw = VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; if (!hud_image(cmd, hud.fit, w, h, VK_FORMAT_R16G16B16A16_SFLOAT, rw) || !hud_image(cmd, hud.layer, w, h, VK_FORMAT_R16G16B16A16_SFLOAT, rw)) { hud.layer_failed = true; logf("vk: fsr3fi: the HUD layer's images (%ux%u) were not made: the HUD laid over the made frames as presented", w, h); return false; } const int k = hud.k, j = hud.prev ? k ^ 1 : k; // (no frame before: this frame's bound in its place, not read) const int32_t pc[5] = {x0, y0, (int32_t)w, (int32_t)h, hud.prev ? 1 : 0}; pass(cmd, hud.fit_pipe, {{hud.before[k].view, fg.nearest}, {hud.after[k].view, fg.nearest}, {hud.before[j].view, fg.nearest}, {hud.after[j].view, fg.nearest}, {hud.fit.view, VK_NULL_HANDLE}, {hud.layer.view, VK_NULL_HANDLE}}, w, h, pc, sizeof pc); if (hud.layers++ == 0) logf("vk: fsr3fi: the HUD laid over the made frames by how see-through it is, learned from frame to frame (%ux%u)", w, h); return true; } /// This present's interpolation source with the HUD marked (hud.source), from the game's frame cropped into fg.frames[cur] /// (at x0, y0 of the presented picture, sw x sh): when the picture was copied after its upscale this frame and is the /// presented picture's size. False: the frame as it is, the HUD guessed. And its HUD layer (hud_layer: hud.layered) bool hud_source(VkCommandBuffer cmd, int cur, uint32_t sw, uint32_t sh, int x0, int y0, uint32_t w, uint32_t h) { std::lock_guard lk(hud.mu); hud_free_dead(); hud.ready = hud.layered = false; const bool fresh = hud.fresh; hud.fresh = false; const int k = hud.k; // (the picture destroyed since its copy, or another image now: nothing to copy from) if (!fresh || hud.failed || !hud.picture || !hud.tex || hud.pw != sw || hud.ph != sh || !fsr3fi::frames_wanted() || texture_destroyed_since(hud.tex_id, hud.tex_noted) || hud.tex->image != hud.picture) return hud.prev = false; if (!hud.init) { hud.init = true; const VkDescriptorType S = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, I = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE; if (!make_pipe(hud.mask, FG_HUD_COMP_SPV, sizeof FG_HUD_COMP_SPV, {S, S, S, I, I}, 20)) { hud.failed = true; logf("vk: fsr3fi: the HUD mask's pipeline was not made: the HUD guessed"); return hud.prev = false; } } if (hud.sw != w || hud.sh != h) { // (the layer's fit is the crop's: started again) hud_retire(hud.source), hud_retire(hud.hudless), hud_retire(hud.fit), hud_retire(hud.layer); hud.sw = w, hud.sh = h, hud.prev = false; } const VkImageUsageFlags rw = VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; if (!hud_image(cmd, hud.after[k], sw, sh, hud.format, VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT) || !hud_image(cmd, hud.source, w, h, VK_FORMAT_R8G8B8A8_UNORM, rw | VK_IMAGE_USAGE_TRANSFER_SRC_BIT) || !hud_image(cmd, hud.hudless, w, h, VK_FORMAT_R8G8B8A8_UNORM, rw | VK_IMAGE_USAGE_TRANSFER_SRC_BIT)) { hud.failed = true; logf("vk: fsr3fi: the HUD mask's images (%ux%u) were not made: the HUD guessed", w, h); return hud.prev = false; } full_barrier(cmd); // (the picture's last writes and reads, and the last readers of this source) 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}, {sw, sh, 1}}; vkCmdCopyImage(cmd, hud.picture, VK_IMAGE_LAYOUT_GENERAL, hud.after[k].image, VK_IMAGE_LAYOUT_GENERAL, 1, &c); full_barrier(cmd); // (pq 0: the picture is linear, sRGB-encoded under the HUD by fg_hud.comp. HDR Native's PQ picture never comes here: // vk_fsr3fi.cpp frames_wanted) const int32_t pc[5] = {x0, y0, (int32_t)w, (int32_t)h, 0}; pass(cmd, hud.mask, {{fg.frames[cur].view, fg.nearest}, {hud.before[k].view, fg.nearest}, {hud.after[k].view, fg.nearest}, {hud.source.view, VK_NULL_HANDLE}, {hud.hudless.view, VK_NULL_HANDLE}}, w, h, pc, sizeof pc); if (!hud.told) { hud.told = true; logf("vk: fsr3fi: the HUD marked for it: what the game drew over its picture after the upscale (%ux%u, format %d)", sw, sh, (int)hud.format); } hud.layered = hudless_wanted() && hud_layer(cmd, x0, y0, w, h); hud.prev = hud.layered; // (a fit uses the pair before only if its layer ran) hud.k ^= 1; hud.marked++; return hud.ready = true; } /// (Triforce) This present gave FSR 3.1 a frame (fsr3fi_frames, right before fsr3fi::generate): its own images were in use, /// whatever came of it and whichever made frames were shown. framegen_idle takes it, and clears it for the next present: both /// on the replay thread, framegen_record before framegen_idle in every present bool fsr_given = false; /// (Triforce) Who made the last set's frames (framegen_maker: perf_framegen_text, the FPS counter) std::atomic last_maker{0}; } // namespace void framegen_hud_before(VkCommandBuffer cmd, Texture* picture, uint32_t level) { if (!cmd || !picture || level || !picture->image || picture->samples != VK_SAMPLE_COUNT_1_BIT || picture->levels != 1 || picture->layers != 1 || picture->type != VK_IMAGE_TYPE_2D || (picture->format != VK_FORMAT_B10G11R11_UFLOAT_PACK32 && picture->format != VK_FORMAT_R16G16B16A16_SFLOAT) || !framegen_on() || !fsr3fi::frames_wanted()) return; std::lock_guard lk(hud.mu); if (hud.failed) return; if (hud.format != picture->format || hud.pw != picture->width || hud.ph != picture->height) { for (int i = 0; i < 2; i++) hud_retire(hud.before[i]), hud_retire(hud.after[i]); hud.format = picture->format, hud.pw = picture->width, hud.ph = picture->height, hud.prev = false; } Img& before = hud.before[hud.k]; // (this frame's: the frame before's, at k ^ 1, is the HUD layer's) if (!hud_image(cmd, before, hud.pw, hud.ph, hud.format, VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT)) { hud.failed = true; logf("vk: fsr3fi: the HUD mask's copy (%ux%u) was not made: the HUD guessed", hud.pw, hud.ph); return; } full_barrier(cmd); // (the upscale's writes; and the last presents' mask and layer passes, which read this copy) 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}, {hud.pw, hud.ph, 1}}; vkCmdCopyImage(cmd, picture->image, VK_IMAGE_LAYOUT_GENERAL, before.image, VK_IMAGE_LAYOUT_GENERAL, 1, &c); full_barrier(cmd); // (before the game draws its HUD into the picture) hud.picture = picture->image, hud.tex = picture, hud.tex_id = picture->id, hud.tex_noted = texture_destroyed_count(); hud.fresh = true; } int framegen_maker() { return last_maker.load(std::memory_order_relaxed); } /// ((Triforce) 11-24, the Frame generation by row) A set's made frames 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): made frame i at t[i] between the frame before /// (0) and this one (1), into set.frame[slot[i]]. Returns count when it made them all, 0 when it did not (no inputs this /// present, a reset, anything not made), which leaves the set to the renderer's own interpolation. Called every present while /// FSR is asked (count 0 too: a set with nothing to make, or nothing to blend from), so its history is the frame before whatever /// the set asks for next. 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). Was fsr3fi_frame: the 2x midpoint alone, the HUD moved with the scene int fsr3fi_frames(VkCommandBuffer cmd, OutSet& set, int cur, uint32_t uw, uint32_t uh, uint64_t sequence, const float* t, const int* slot, int count) { // (every 600 presents asked: how many sets FSR made, and how many it left to the renderer's own and why) static uint64_t asked = 0, made = 0, no_inputs = 0, own_motion = 0, marked_before = 0, layers_before = 0; if (++asked % 600 == 0) { logf("vk: fsr3fi: of the last 600 presents %llu made by FSR 3.1, %llu without its inputs (the renderer's own made " "them); %llu with the upscaler chain's motion; %llu with the HUD marked, %llu with its HUD layer", (unsigned long long)made, (unsigned long long)no_inputs, (unsigned long long)own_motion, (unsigned long long)(hud.marked - marked_before), (unsigned long long)(hud.layers - layers_before)); made = no_inputs = own_motion = 0, marked_before = hud.marked, layers_before = hud.layers; } 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; own_motion += f.chain_motion; fsr3fi::Inputs in; in.color = fg.frames[cur].view; in.color_image = fg.frames[cur].image; const bool hudless = hudless_wanted(); // (the HUD layer: FSR 3.1 given the picture without the HUD as its present too, so it lays nothing over the made frames, // and the HUD goes over them below by how see-through it is) const bool layered = hud.ready && hudless && hud.layered; if (hud.ready && hudless) { // (the picture before the HUD, interpolated; this frame, HUD and all, laid over it) in.color = hud.hudless.view, in.color_image = hud.hudless.image; in.present = layered ? hud.hudless.view : fg.frames[cur].view; } else if (hud.ready) { // (the same frame, the HUD marked in its alpha: hud_source) in.color = hud.source.view, in.color_image = hud.source.image; in.hud_mask = true; } 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; // (its images in use this present from here, whatever generate makes of it: a reset, its history alone, none made) fsr_given = true; if (count <= 0) return (void)fsr3fi::generate(cmd, in, t, 0), 0; // (its history alone) if ((int)fsr3fi::generate(cmd, in, t, (uint32_t)count) != count) 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}}; for (int i = 0; i < count; i++) { const VkImage image = fsr3fi::made_image((uint32_t)i); if (!image) return 0; vkCmdCopyImage(cmd, image, VK_IMAGE_LAYOUT_GENERAL, set.frame[slot[i]].image, VK_IMAGE_LAYOUT_GENERAL, 1, &c); } if (layered) { // (this frame's HUD over each made frame's own scene: shaders/fg_hud_composite.comp) full_barrier(cmd); const int32_t pc[3] = {(int32_t)uw, (int32_t)uh, 0}; for (int i = 0; i < count; i++) pass(cmd, hud.over_pipe, {{set.frame[slot[i]].view, VK_NULL_HANDLE}, {fg.frames[cur].view, fg.nearest}, {hud.layer.view, fg.nearest}}, uw, uh, pc, sizeof pc); } made++; static int told = 0; if (told < count) { // (once, and again for a set of more made frames than before) told = count; logf("vk: fsr3fi: AMD FSR 3.1 makes %d frame(s) between each two of the game's (%ux%u, its depth and motion %ux%u)", count, uw, uh, f.width, f.height); } return count; } /// (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; } set->antilag_id = antilag_frame(); // ((Triforce) 11-24, AMD Anti-Lag 2: the game frame this set shows) // ((Triforce, 11-20) the row's multiplier as it is now, and no more than the frames the sets were made with (ensure_size, // above): the menu runs on the display thread while this one waited for a set, and a step up of the row in that wait // had the game's frame copied into an image never made, which crashed in the driver (framegen_rows.h set_multiplier)) const int cur = fg.cur, prev = cur ^ 1; int m = framegen_rows::set_multiplier(multiplier(), fg.mult); // ((Triforce, 11-21, the upscaler audit's UPS-05) RX_VK_FRAMEGEN_REFRESH=1: a fixed multiplier (2x to 8x) makes no more // frames than the display's refreshes that fall in one frame of the game (framegen_refresh.h): the owner's 5x on 165 Hz // showed 331 frames a second, half of them never on the screen. Off unless set; never more than the sets' frames (m) static const bool refresh_cap = framegen_refresh::on(getenv("RX_VK_FRAMEGEN_REFRESH")); if (refresh_cap && opts.framegen >= 2 && opts.framegen <= kMaxMult) { const int row = m; m = framegen_refresh::frames(m, fg.game_ns, framegen_refresh::period_ns(display_refresh_mhz())); static int told = 0; if (m != told) told = m, logf("vk: frame interpolation: %d frame(s) for each of the game's, of the row's %d (the game's frames %.2f ms " "apart, the display at %.1f Hz: RX_VK_FRAMEGEN_REFRESH)", m, row, fg.game_ns / 1e6, display_refresh_mhz() / 1000.0); } 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); // ((Triforce) 11-24, the Frame generation by row at AMD FSR 3.1: its source with the HUD marked, and the picture without the // HUD, read by fsr3fi_frames below. Whether FSR 3.1 is asked is read once here, for the whole set) const bool fsr_asked = fsr3fi::frames_wanted() != 0 && fg.format == VK_FORMAT_R8G8B8A8_UNORM; if (fsr_asked) hud_source(cmd, cur, sw, sh, x0, y0, uw, uh); set->made = 0; set->shown = -1; set->by_time = false; // ((Triforce) 11-24: a reused set; only a fixed multiplier's below sets it) 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; } // ((Triforce) 11-24) the Frame generation by row at AMD FSR 3.1 (vk_fsr3fi.h): each made frame at its refresh's place, // from the scene's depth and motion. Given every present, with none to make too, so the next set has its history; a // set it does not make goes to the renderer's own below float made_t[kMaxMult]; int made_slot[kMaxMult], made_n = 0; for (int k = 0; k < n; k++) if (t[k] <= 0.999f) made_t[made_n] = t[k], made_slot[made_n++] = k; const int fsr_n = blend ? made_n : 0; const bool fsr = fsr_asked && fsr3fi_frames(cmd, *set, cur, uw, uh, sequence, made_t, made_slot, fsr_n) == fsr_n && fsr_n > 0; 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 (fsr) continue; // (made above, into this slot) 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) last_maker.store(fsr ? kMakerFsr3 : flow ? kMakerOwn : kMakerNone, std::memory_order_relaxed); 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); } // ((Triforce) 11-24) the Frame generation by row at AMD FSR 3.1 (vk_fsr3fi.h): the set's made frames at k/m, 1/2 at 2x, 1/3 // and 2/3 at 3x. Given every present it is asked, with none to make too (nothing to blend from), so the next set has its // history; any it does not make stays with the interpolation below. (It was the 2x midpoint alone, RX_VK_FSR_FG=1) int maker = opts.framegen == framegen_rows::FSR4 && set->made ? kMakerAmd : kMakerNone; if (set->made == 0 && !none && m > 1 && fsr_asked) { float made_t[kMaxMult]; int made_slot[kMaxMult]; for (int k = 1; k < m; k++) made_t[k - 1] = (float)k / (float)m, made_slot[k - 1] = k - 1; const int fsr_n = blend ? m - 1 : 0; if (fsr3fi_frames(cmd, *set, cur, uw, uh, sequence, made_t, made_slot, fsr_n) == fsr_n && fsr_n > 0) set->made = fsr_n, maker = kMakerFsr3; } 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; maker = kMakerOwn; } last_maker.store(maker, std::memory_order_relaxed); // ((Triforce, 11-20) the game's frame after the made ones, in a frame the set has: the row gone to AMD FSR 4 in the wait // above, over sets made for the game's frames alone, has one made frame and no second image) set->made = framegen_rows::game_slot(set->made, fg.mult); // ((Triforce) 11-24, RX_VK_FG_GAME_TIMELINE=1) the set's times on the game's own timeline (fixed_dues): each frame at its // place between the two game frames, its lead and the GPU's delay later. Without a frame before (the first, a hitch) or a // made frame, none if (game_timeline() && blend && set->made > 0 && fg.game_ns > 0) { const int64_t extra = fg.late_ns.load(std::memory_order_relaxed); fixed_dues(before, now, set->made, pace_lead(fg.game_ns, set->made) + extra, set->due); for (int k = 0; k <= set->made; k++) set->game[k] = k == set->made; set->frame_ns = fg.game_ns, set->extra_ns = extra, set->by_time = true; } 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(); } /// ((Triforce, 11-21, the performance audit's PRESENT-18, after Source A's) Each present, after its queue (vk_replay.cpp, /// after framegen_queue): frame generation's images let go once it has been off for framegen_idle::kPresents presents, the /// display thread done with its sets and every queue idle first, as ensure_size does before it drops them. They are made /// again at the next frame that needs them. framegen_idle.h has why and the rule; RX_VK_FG_KEEP=1: kept void framegen_idle(bool recorded) { static framegen_idle::Counts counts; static const bool keep = getenv("RX_VK_FG_KEEP") && atoi(getenv("RX_VK_FG_KEEP")) != 0; const bool on = framegen_on(); bool have_hudless; { std::lock_guard lk(hl.mu); have_hudless = hl.w || hl.taken[0].w || hl.taken[1].w; } // ((Triforce) 11-24) and AMD FSR 3.1's frame interpolation's own images by this present's own use of them (fsr_given), not // by recorded: a set the renderer's own made leaves them unused. Let go with its HUD's images, once the GPU is past them const bool fsr_used = std::exchange(fsr_given, false); framegen_idle::Present p{on, recorded, fsr4fg::frames_wanted(), fg.w != 0, have_hudless}; p.fsr_wanted = on && fsr3fi::frames_wanted(), p.fsr_used = fsr_used; const framegen_idle::Release go = framegen_idle::step(counts, p, keep); if (go.fsr) fsr3fi::release(), hud_release(); if (!go.shared && !go.hudless) return; { // (as ensure_size: the display thread done with the sets, then every queue idle) 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); device_wait_idle_locked(); } if (go.shared) { // (the frames and the sets' made frames; the lumas and flows are small) const uint64_t mb = ((uint64_t)fg.w * fg.h * 4 * (2 + 3 * (uint64_t)std::max(fg.mult, 1))) >> 20; logf("vk: frame generation off for %u presents: its images (%ux%u, %dx) let go, about %llu MB (RX_VK_FG_KEEP=1: kept)", framegen_idle::kPresents, fg.w, fg.h, fg.mult, (unsigned long long)mb); drop_size(); } if (go.hudless) { std::lock_guard lk(hl.mu); const uint64_t mb = ((uint64_t)hl.taken[0].w * hl.taken[0].h * 4 * 2 + (uint64_t)hl.w * hl.h * 4 * 2) >> 20; for (Hudless::Taken& t : hl.taken) drop_img(t.img), t = {}; drop_img(hl.srgb), drop_img(hl.out), hl.w = hl.h = 0; hl.want_w = hl.want_h = 0; logf("vk: AMD FSR frame generation off for %u presents: the images of the picture without the HUD let go, about %llu MB", framegen_idle::kPresents, (unsigned long long)mb); } } 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