Bring the 0.10.0 line into the VRAM pass, keeping the line's own releases where the two overlap.

The line freed the upscaler stand-in's scratch images, FSR 1's and SMAA's images and the DLSS sizes on its own while
this pass did: its versions stay. Of this pass's versions, what they do not cover stays: the stand-in's views of a game
image it read as it is now go with that image (vk_tonemap_forget from vk_exec_image_gone, under a lock), and the
upscaler release frees only what the line's idle release leaves, the reactive mask's snapshots, the FSR upscale
contexts and the DLSS sizes after a failure, at the same 600 presents and with vram_use = high keeping them as well.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 00:50:21 -05:00
36 changed files with 2196 additions and 280 deletions

View File

@@ -31,7 +31,6 @@
#include "vk_dlaa.h"
#include "vk_exec.h"
#include "vk_ffx.h"
#include "vk_fg.h"
#include "vk_rec.h"
#include "dlaa_mv_spv.h"
#include "dlaa_sharpen_spv.h"
@@ -574,6 +573,15 @@ static struct {
uint32_t hl_w, hl_h;
VkFormat hl_fmt;
int hl_fresh;
// before the bloom (NX2NAVI_DLSS_PREBLOOM, prebloom_run)
int pre_on; // the setting, read at each present
int pre; // this frame: the upscaler ran before the bloom chain; 1 back into the light target
// (no upscaling), 2 to the picture's size (PB), whose passes the retarget moves there
int pre_at; // where the last evaluation ran: 1 before the bloom, 0 at the game's upscaler
int comp; // this frame: the bloom composite was drawn into PB.comp (vk_dlaa_retarget)
VkImage comp_game; // the game's target that composite was meant for
uint32_t pic_w, pic_h; // the size of the game's upscaler's output, the last one seen (vk_dlaa_upscaler)
uint64_t pre_misses; // frames with the setting on that found no bloom chain to run before
} D = {.mv_ok = -1, .cfg_seen = ~0u};
// What this frame drew into a D32F depth at least DLAA_SCENE_MIN_W wide before DLSS ran (vk_dlaa_draw), for the line a
@@ -1485,6 +1493,7 @@ static struct Snap *mask_pass(VkCommandBuffer cb, VkImageView cv, uint32_t w, ui
// ---------------------------------------------------------------- teardown
// The HUD-less picture's images, a half-made pair too. The device must be idle, or the images not yet used.
static void pb_destroy(void); // the picture-size images of the upscaler before the bloom (prebloom_run)
static void hl_destroy(void) {
const VkCtx *c = vk_ctx();
if (D.hl_view) vkDestroyImageView(c->device, D.hl_view, NULL);
@@ -1560,6 +1569,7 @@ void vk_dlaa_release(void) {
for (int i = 0; i < 5; i++) destroy_size(&D.sizes[i]);
for (int i = 0; i < SNAPS; i++) destroy_snap(&R.s[i]);
hl_destroy();
pb_destroy();
if (D.mv_pipe) vkDestroyPipeline(c->device, D.mv_pipe, NULL);
if (D.mv_layout) vkDestroyPipelineLayout(c->device, D.mv_layout, NULL);
if (D.mv_dsl) vkDestroyDescriptorSetLayout(c->device, D.mv_dsl, NULL);
@@ -1615,6 +1625,10 @@ static int jitter_draw(const VkExec *ex) {
}
void vk_dlaa_viewport(VkExec *ex, VkViewport *v) {
float sx, sy;
// a pass moved to the picture's size (vk_dlaa_retarget); without a game viewport it covers the pass already
if (ex->has_vp && vk_dlaa_target_scale(ex, &sx, &sy))
v->x *= sx, v->y *= sy, v->width *= sx, v->height *= sy;
ex->vp_jittered = jitter_draw(ex);
if (!ex->vp_jittered) return;
ex->pass_jit = 1;
@@ -1793,7 +1807,7 @@ static void window_line(void) {
}
// dst: the target DLSS's picture goes to, NULL for the light target itself (DLAA in place).
static void hudless(VkCommandBuffer cb, struct Size *e, uint32_t w, uint32_t h);
static void hudless(VkCommandBuffer cb, VkImage src, uint32_t w, uint32_t h);
// Another light target than last frame's. The game draws its scene into one of a few pooled targets of the same size
// and moves to another whenever its pass list changes, in the middle of play with the camera and the scene carrying
@@ -2171,6 +2185,7 @@ static void evaluate(VkExec *ex, const VkExecTarget *dst) {
const VkExecTarget *t = &D.color, *z = &D.depth;
if (!dst) dst = t;
uint32_t w = t->width, h = t->height, ow = dst->width, oh = dst->height;
vk_exec_tail_begin(ex); // NX2NAVI_TAIL_EARLY: from here on the frame's tail, in a command buffer of its own
VkCommandBuffer cb = ex->cb;
vk_exec_barrier(ex); // what drew the scene is done (no pass is open here)
vk_gpu_gap_note(VK_GAP_UPSCALER, (uint64_t)ow * oh);
@@ -2181,6 +2196,18 @@ static void evaluate(VkExec *ex, const VkExecTarget *dst) {
}
struct Size *e = size_for(cb, w, h, ow, oh);
if (!e) return dl_fail("work image creation", 0);
// Before the bloom the input is the scene's light before the exposure and the tonemap, at the game's upscaler the
// tonemapped picture: a history built from one does not hold for the other (prebloom_run).
{
const int at = D.pre ? 1 : 0;
static int told;
if (D.evals && at != D.pre_at) {
D.reset = 1;
if (told < 8)
told++, rt_log("dlaa: the upscaler now runs %s: a new history", at ? "before the bloom" : "after the tonemap");
}
D.pre_at = at;
}
// Read once: vk_dlaa_frame runs on the present's thread and can clear fsr_frame while this replay is between the
// checks below, which would send the rest of this function to DLSS's path without NGX after all.
const int fsr = use_fsr();
@@ -2377,7 +2404,9 @@ evaluated:
vkCmdBlitImage(cb, shown, VK_IMAGE_LAYOUT_GENERAL, dst->image, VK_IMAGE_LAYOUT_GENERAL, 1, &b, VK_FILTER_NEAREST);
full_barrier(cb);
if (dst != t) vk_dlaa_swap_img = t->img, vk_dlaa_swap_view = e->out_view; // the scaling draw reads DLSS's picture
if (vk_dlaa_hudless_wanted) hudless(cb, e, ow, oh); // frame generation keeps the HUD out of its generated frames
// frame generation keeps the HUD out of its generated frames; before the bloom, DLSS's output is not yet the picture
// and the HUD-less one is taken at the game's upscaler (vk_dlaa_upscaler)
if (vk_dlaa_hudless_wanted && !D.pre) hudless(cb, e->out, ow, oh);
ex->recorded += 4;
vk_exec_draw_new_cb(); // NGX bound pipelines and descriptor sets of its own: the next draw binds everything again
D.jdepth = z->image, D.jw = z->width, D.jh = z->height;
@@ -2412,7 +2441,9 @@ evaluated:
// (vk_hdr_encode): DLSS's output is kept as RGBA16F for the same encode. The game's own HDR (native) encodes inside the
// game, which this cannot follow: no HUD-less picture there.
int vk_dlaa_hudless_wanted;
static void hudless(VkCommandBuffer cb, struct Size *e, uint32_t w, uint32_t h) {
// src: the picture without the HUD, linear, w x h, in GENERAL: DLSS's output where the upscaler ran at the game's
// upscaler, the game's upscaler's output where it ran before the bloom (vk_dlaa_upscaler).
static void hudless(VkCommandBuffer cb, VkImage src, uint32_t w, uint32_t h) {
const VkCtx *c = vk_ctx();
int hdr = vk_hdr_wanted() && !vk_hdr_native();
if (vk_hdr_native()) return;
@@ -2452,7 +2483,7 @@ static void hudless(VkCommandBuffer cb, struct Size *e, uint32_t w, uint32_t h)
}
VkImageBlit b = {.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, .srcOffsets = {{0, 0, 0}, {(int32_t)w, (int32_t)h, 1}},
.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, .dstOffsets = {{0, 0, 0}, {(int32_t)w, (int32_t)h, 1}}};
vkCmdBlitImage(cb, e->out, VK_IMAGE_LAYOUT_GENERAL, hdr ? D.hl : D.hl_srgb, VK_IMAGE_LAYOUT_GENERAL, 1, &b,
vkCmdBlitImage(cb, src, VK_IMAGE_LAYOUT_GENERAL, hdr ? D.hl : D.hl_srgb, VK_IMAGE_LAYOUT_GENERAL, 1, &b,
VK_FILTER_NEAREST);
full_barrier(cb);
if (hdr) {
@@ -2493,6 +2524,227 @@ static int output_target(VkFormat f) { return srgb8(f) || vk_hdr_native_picture(
double wide_dlss_share(void); // widescreen.c: the DLSS mode's share of the picture (1 DLAA), 0 off
// ---------------------------------------------------------------- before the bloom (NX2NAVI_DLSS_PREBLOOM)
// What the upscaler gets at the game's upscaler (vk_dlaa_upscaler) is the armed pair of the bloom composite: measured on
// frame dumps (NX2NAVI_DUMP; 2880x1620 scene in a 3840x2160 picture, and 1280x720 in 2560x1440 with DLSS), the light
// target holds the lit scene with its fog, transparents and lens flares as linear light up to about 160; the one
// full-screen draw that reads it and the bloom's smallest target writes the second light target with the scene depth
// attached, and that shader adds the bloom, applies the exposure, converts to the tonemap's colour space and ends in the
// fitted filmic curve and a 0..1 clamp (vk_hdr.c tonemap_splice finds the same curve). Its output, 0..1 and linear
// (the post pass after it stores its square root), is what DLSS anti-aliased and enlarged: a picture already exposed,
// tonemapped and bloomed, which the IsHDR and auto-exposure flags describe wrongly.
// With the setting on, the upscaler runs at the start of the bloom chain's first pass instead: two light-format targets
// smaller than the scene, no depth, right after the transparents (prebloom_start). Its input is then the scene's linear
// light before exposure and tonemap, at the render size, with the scene depth.
// - Without upscaling (DLAA) DLSS's picture goes back into the light target and the game's own passes (bloom, flares,
// composite and tonemap, post pass, its upscaler at 1:1, HUD) all run on it as they are.
// - With upscaling the game's passes after it are drawn at the render size, and the post pass and the game's upscaler
// are compute dispatches whose sizes the game sets, so they cannot be moved to the picture's size. What is moved
// (vk_dlaa_retarget) are the render passes from the light target on: DLSS's picture goes into PB.scene at the
// picture's size; a pass drawing into the light target (the lens flares, additive, no depth test) draws into
// PB.scene, and the bloom composite into PB.comp, both at the picture's size without the depth attachment, with
// their viewports and scissors scaled (vk_dlaa_viewport, vk_dlaa_target_scale). The composite samples the light
// target through its texture coordinates, so it reads PB.scene in its place (vk_dlaa_swap_img). The bloom chain
// reads the light target at its own small sizes; DLSS's picture is scaled back into it first, so the bloom starts
// from the resolved picture and does not shimmer with the jitter. The post pass and the game's upscaler then run on
// the game's composite target, which this frame never wrote, and at the game's upscaler PB.comp goes over their
// picture, as DLSS's did before (vk_dlaa_upscaler).
// A frame without that bloom chain (menus, a loading screen) runs the upscaler at the game's upscaler as before.
static struct {
VkImage scene, comp; // DLSS's picture with the flares, and the composite's picture: B10G11R11, picture size
VmaAllocation scene_mem, comp_mem;
VkImageView scene_view, comp_view;
uint32_t w, h;
uint64_t used; // D.presents when last used (idle_release frees them after IDLE_FREE_PRESENTS)
} PB;
static void pb_destroy(void) {
const VkCtx *c = vk_ctx();
if (PB.scene_view) vkDestroyImageView(c->device, PB.scene_view, NULL);
if (PB.comp_view) vkDestroyImageView(c->device, PB.comp_view, NULL);
if (PB.scene) vmaDestroyImage(c->vma, PB.scene, PB.scene_mem);
if (PB.comp) vmaDestroyImage(c->vma, PB.comp, PB.comp_mem);
memset(&PB, 0, sizeof PB);
}
// The two images at w x h, made again for another picture size (the old ones after the device is idle, as hudless).
static int pb_images(VkCommandBuffer cb, uint32_t w, uint32_t h) {
PB.used = D.presents;
if (PB.scene && PB.w == w && PB.h == h) return 1;
if (PB.scene || PB.comp) {
vk_queue_lock(), vkDeviceWaitIdle(vk_ctx()->device), vk_queue_unlock();
pb_destroy();
}
VkImageUsageFlags u = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT |
VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
const VkFormat f = VK_FORMAT_B10G11R11_UFLOAT_PACK32;
if (!image2d(w, h, f, u, &PB.scene, &PB.scene_mem, &PB.scene_view) ||
!image2d(w, h, f, u, &PB.comp, &PB.comp_mem, &PB.comp_view)) {
pb_destroy();
return 0;
}
VkImageMemoryBarrier2 ib[2];
VkImage im[2] = {PB.scene, PB.comp};
for (int i = 0; i < 2; i++)
ib[i] = (VkImageMemoryBarrier2){.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
.srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
.dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
.dstAccessMask = VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT,
.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED, .newLayout = VK_IMAGE_LAYOUT_GENERAL,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .image = im[i],
.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .imageMemoryBarrierCount = 2,
.pImageMemoryBarriers = ib};
vkCmdPipelineBarrier2(cb, &di);
PB.w = w, PB.h = h;
rt_log("dlaa: prebloom: the passes after the upscaler draw at %ux%u (two B10G11R11 images, %.0f MiB)", w, h,
2.0 * w * h * 4 / 1048576.0);
return 1;
}
// The bloom chain's first pass, on Ocarina's frame: two light-format targets, each smaller than the armed light target
// both ways, no depth. In the dumps (2880x1620 and 1280x720 scenes) it is the only such pass and it comes right after
// the last pass of transparents into the light target; its targets are 480x270 at both scene sizes. The passes in
// between that read the light target at a third or half of its size have one target, or a light target with an R8 one.
static int prebloom_start(const VkExec *ex) {
if (ex->pass_ncolor != 2 || ex->depth.image) return 0;
for (int i = 0; i < 2; i++) {
const VkExecTarget *t = &ex->color[i];
if (!t->image || t->format != VK_FORMAT_B10G11R11_UFLOAT_PACK32 || t->samples > 1 || t->is3d ||
t->width >= D.color.width || t->height >= D.color.height)
return 0;
}
return 1;
}
// Runs the upscaler at the bloom chain's first pass (prebloom_start), before that pass begins. 0: not here this frame
// (the picture's size not known yet, or not of the scene's shape): the game's upscaler runs it as before.
static int prebloom_run(VkExec *ex) {
const uint32_t w = D.color.width, h = D.color.height;
const double share = wide_dlss_share();
const int native = share <= 0 || share >= 1;
uint32_t ow = D.pic_w, oh = D.pic_h;
if (!native) {
int known = ow > w && oh > h && (double)ow * h <= 1.03 * (double)oh * w && (double)oh * w <= 1.03 * (double)ow * h;
if (!known || !pb_images(ex->cb, ow, oh)) {
if (D.pre_misses++ < 4)
rt_log("dlaa: prebloom: the picture's size is not known yet (last %ux%u for a %ux%u scene): this frame "
"runs the upscaler after the tonemap", ow, oh, w, h);
return 0;
}
}
vk_exec_hook_turn(); // as in vk_dlaa_pass: the replay workers' draws read the jitter and its end
D.ended = D.done = 1;
D.pre = native ? 1 : 2;
const uint64_t evals = D.evals;
if (native) {
evaluate(ex, NULL); // into the light target, which the bloom and everything after it read
} else {
D.up = 1;
VkExecTarget dst = {.image = PB.scene, .format = VK_FORMAT_B10G11R11_UFLOAT_PACK32,
.aspect = VK_IMAGE_ASPECT_COLOR_BIT, .layers = 1, .width = ow, .height = oh, .samples = 1};
evaluate(ex, &dst);
}
if (D.evals == evals) { // the evaluation failed (dl_fail) or did not run: nothing moves to the picture's size
D.pre = 0, D.up = 0;
return 1;
}
if (D.pre == 2) {
// the composite samples the light target: PB.scene, which the lens flares are then drawn into, in its place
vk_dlaa_swap_img = D.color.img, vk_dlaa_swap_view = PB.scene_view;
// and the bloom chain reads the light target itself, at its own sizes: the resolved picture, scaled back down
VkImageBlit b = {.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
.srcOffsets = {{0, 0, 0}, {(int32_t)ow, (int32_t)oh, 1}},
.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, D.color.level, D.color.base_layer, 1},
.dstOffsets = {{0, 0, 0}, {(int32_t)w, (int32_t)h, 1}}};
vkCmdBlitImage(ex->cb, PB.scene, VK_IMAGE_LAYOUT_GENERAL, D.color.image, VK_IMAGE_LAYOUT_GENERAL, 1, &b,
VK_FILTER_LINEAR);
full_barrier(ex->cb);
ex->recorded += 2;
}
static int told;
if (!told++)
rt_log("dlaa: prebloom: %s runs before the bloom, at the start of its first pass (two %ux%u targets, format %d), "
"on the light target %ux%u format %d with the depth %ux%u format %d; %s",
use_fsr() ? "FSR 3.1" : "DLSS", ex->color[0].width, ex->color[0].height, (int)ex->color[0].format, w, h,
(int)D.color.format, D.depth.width, D.depth.height, (int)D.depth.format,
native ? "its picture goes back into the light target (no upscaling)"
: "its picture is the picture's size, where the lens flares and the bloom composite are drawn");
return 1;
}
// vk_exec.c vk_exec_pass_begin, in every replay thread, once the pass's targets are resolved and the pass hooks ran:
// after the upscaler ran before the bloom to the picture's size (prebloom_run), a pass drawing into the light target
// draws into PB.scene and the bloom composite (one light-format target of the scene's size, with the scene depth) into
// PB.comp, each at the picture's size and without its depth attachment (neither tests depth in the dumps). Its draws'
// viewports and scissors follow (vk_dlaa_viewport, vk_dlaa_target_scale). A pass of the same targets begun again (the
// pass broken by a copy) keeps what it got: its targets are not resolved again.
void vk_dlaa_retarget(VkExec *ex) {
if (D.pre != 2 || !PB.scene || ex->pass_ncolor != 1) return;
VkExecTarget *c = &ex->color[0];
if (!c->image || c->image == PB.scene || c->image == PB.comp) return;
if (c->format != VK_FORMAT_B10G11R11_UFLOAT_PACK32 || c->samples > 1 || c->is3d || c->layers != 1) return;
VkImage to = VK_NULL_HANDLE;
int comp = 0;
if (c->image == D.color.image) to = PB.scene;
else if (c->width == D.color.width && c->height == D.color.height && ex->depth.image == D.depth.image &&
(!D.comp_game || D.comp_game == c->image))
to = PB.comp, comp = 1;
if (!to) return;
static int told[2];
if (!told[comp]++)
rt_log("dlaa: prebloom: %s (%ux%u format %d, %s) drawn at %ux%u", comp ? "the bloom composite" : "a pass into the light target",
c->width, c->height, (int)c->format, ex->depth.image ? "with the scene depth" : "no depth", PB.w, PB.h);
if (comp) D.comp_game = c->image, D.comp = 1;
c->image = to, c->img = NULL;
c->width = PB.w, c->height = PB.h;
c->level = c->base_layer = 0, c->scale = 1, c->blocks = 0, c->view_type = VK_IMAGE_VIEW_TYPE_2D;
memset(&ex->depth, 0, sizeof ex->depth);
ex->pass_depth = ex->pass_stencil = VK_FORMAT_UNDEFINED;
ex->pipe.depth_fmt = 0;
ex->pass_w = PB.w, ex->pass_h = PB.h, ex->pass_scale = 1, ex->pass_layers = 1, ex->pass_noatt = 0;
}
// The scale from the game's viewport and scissor to a pass vk_dlaa_retarget moved to the picture's size; 0 for any
// other pass.
int vk_dlaa_target_scale(const VkExec *ex, float *sx, float *sy) {
if (D.pre != 2 || !PB.scene || !D.color.width || !D.color.height) return 0;
VkImage t = ex->color[0].image;
if (!t || (t != PB.scene && t != PB.comp)) return 0;
*sx = (float)PB.w / (float)D.color.width, *sy = (float)PB.h / (float)D.color.height;
return 1;
}
// At the game's upscaler, with the upscaler run before the bloom: notes the picture's size, and puts the composite
// drawn at the picture's size over the game's upscaler's output (2), or leaves that output, which is already made from
// DLSS's picture (1). The HUD-less picture for frame generation is the result either way.
static void prebloom_upscaler(VkExec *ex, VkImage out, uint32_t w, uint32_t h) {
VkCommandBuffer cb = ex->cb;
if (D.pre == 2) {
if (!D.comp) {
static int told;
if (told < 8)
told++, rt_log("dlaa: prebloom: frame %llu drew no bloom composite after the upscaler: the game's "
"picture stays", (unsigned long long)__atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED));
return;
}
// the same size but for a picture size changed since the upscaler ran: scaled then, for that frame
VkImageBlit b = {.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
.srcOffsets = {{0, 0, 0}, {(int32_t)PB.w, (int32_t)PB.h, 1}},
.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
.dstOffsets = {{0, 0, 0}, {(int32_t)w, (int32_t)h, 1}}};
vkCmdBlitImage(cb, PB.comp, VK_IMAGE_LAYOUT_GENERAL, out, VK_IMAGE_LAYOUT_GENERAL, 1, &b,
PB.w == w && PB.h == h ? VK_FILTER_NEAREST : VK_FILTER_LINEAR);
full_barrier(cb);
ex->recorded += 2;
static int told;
if (!told++) rt_log("dlaa: prebloom: the composite drawn at %ux%u goes into the game's upscaler's %ux%u output",
PB.w, PB.h, w, h);
}
if (vk_dlaa_hudless_wanted) hudless(cb, out, w, h), ex->recorded += 2;
}
// Ocarina's own upscaler is the place for DLSS or FSR: its stand-in (vk_exec_draw.c untranslated_dispatch_fallback) has
// just scaled the scene into the image the rest of the frame shows, and the upscaler's picture goes over it there.
// Until this hook the upscaler ran at the pass after it (vk_dlaa_pass below), which was taken for the pass the HUD is
@@ -2510,7 +2762,13 @@ double wide_dlss_share(void); // widescreen.c: the DLSS mode's share of the pi
void vk_dlaa_upscaler(VkExec *ex, VkImage out, VkFormat fmt, uint32_t w, uint32_t h) {
static int on = -1;
if (on < 0) on = rt_knob("DLAA_AT_UPSCALER", "1");
if (!on || !dl_active() || !D.armed || D.done || D.gbuf || !out || fmt != VK_FORMAT_B10G11R11_UFLOAT_PACK32) return;
if (!dl_active() || !out || fmt != VK_FORMAT_B10G11R11_UFLOAT_PACK32) return;
if (D.armed && !D.gbuf && w >= D.color.width && h >= D.color.height) D.pic_w = w, D.pic_h = h; // prebloom_run
if (D.pre) {
prebloom_upscaler(ex, out, w, h);
return;
}
if (!on || !D.armed || D.done || D.gbuf) return;
uint32_t sw = D.color.width, sh = D.color.height;
if (w < sw || h < sh || out == D.color.image) return;
if ((double)w * sh > 1.03 * (double)h * sw || (double)h * sw > 1.03 * (double)w * sh) return; // not the scene's shape
@@ -2524,56 +2782,47 @@ void vk_dlaa_upscaler(VkExec *ex, VkImage out, VkFormat fmt, uint32_t w, uint32_
evaluate(ex, &dst);
}
// ---- what the upscaler holds after it is switched off
// Switched off in the menu, or failed (dl_fail), the upscaler kept everything it had made for the process: per scene
// size its output, motion vector, mask and sharpening images and a DLSS feature (at 2560x1440 with DLAA 81 MiB of
// images, at 2160 lines 182 MiB, and a DLSS feature is about 0.5 GB), the snapshots of the reactive mask, the HUD-less
// pair for frame generation (28 MiB, 63 MiB at 2160 lines) and the FSR 3.1 and FSR 4 contexts. RETIRE_PRESENTS presents
// after the upscaler goes off they are freed, here on the replay thread, which makes every evaluation and so cannot be
// inside one, after a wait for the device to be idle, as free_size does for a size it replaces: an evaluation may
// still run on the GPU, and the present thread only learns of the switch at its next frame. The HUD-less pair goes the
// same way while the upscaler runs and frame generation no longer asks for it (vk_dlaa_hudless_wanted, which the frame
// generator's release clears), once no frame generation swapchain is up that could still read it. Switched on again,
// the upscaler makes what it needs as at its first frame, with a new history, as any switch on does.
#define RETIRE_PRESENTS 8
static uint64_t retire_off_at, retire_hl_at; // replay: D.presents when the upscaler, or the HUD-less picture, went unused
static uint64_t retired_n; // replay: times the upscaler's memory was given back
// ---- what idle_release (below) leaves after the upscaler is switched off
// idle_release frees the DLSS sizes and the HUD-less picture some presents after the setting goes off. What it does not
// reach stayed for the process: the reactive mask's snapshots, the FSR 3.1 and FSR 4 upscale contexts, and the DLSS
// sizes after a failure (dl_fail turns the upscaler off without the setting changing, so D.on stays set). They go
// here, RETIRE_PRESENTS presents after the upscaler stopped running, on the replay thread, which makes every
// evaluation, after a wait for the device to be idle, as free_size does for a size it replaces. Switched on again, the
// upscaler makes them anew, as at its first frame in a run.
#define RETIRE_PRESENTS 600
static uint64_t retire_off_at; // replay: D.presents when the upscaler stopped running
static uint64_t retired_n; // replay: times this gave memory back
static int up_holds(void) {
for (int i = 0; i < DLSS_SIZES; i++)
if (D.sizes[i].out || D.sizes[i].feat) return 1;
for (int i = 0; i < SNAPS; i++)
if (R.s[i].before) return 1;
return D.hl != VK_NULL_HANDLE || vk_ffx_holds();
if (D.failed)
for (int i = 0; i < DLSS_SIZES; i++)
if (D.sizes[i].out || D.sizes[i].feat) return 1;
return vk_ffx_holds();
}
static void retire_idle(void) {
uint64_t now = __atomic_load_n(&D.presents, __ATOMIC_RELAXED);
int off = !dl_active(), hl_unused = D.hl && !vk_dlaa_hudless_wanted && !vk_fg_active();
if (!off) retire_off_at = 0;
if (!hl_unused) retire_hl_at = 0;
if (off && !retire_off_at) retire_off_at = now ? now : 1;
if (hl_unused && !retire_hl_at) retire_hl_at = now ? now : 1;
int all = off && now >= retire_off_at + RETIRE_PRESENTS && up_holds();
int hl = hl_unused && now >= retire_hl_at + RETIRE_PRESENTS;
if (!all && !hl) return;
vk_queue_lock(), vkDeviceWaitIdle(vk_ctx()->device), vk_queue_unlock();
if (hl || !vk_fg_active()) hl_destroy(), D.hl_fresh = 0;
if (all) {
int64_t was = vk_dlaa_vram();
for (int i = 0; i < DLSS_SIZES; i++) destroy_size(&D.sizes[i]);
for (int i = 0; i < SNAPS; i++) destroy_snap(&R.s[i]);
vk_ffx_release_idle();
D.last = NULL, D.host = NULL;
retired_n++;
rt_log("dlaa: the upscaler is off (%s) since %d presents: its images, DLSS features and FSR contexts are freed "
"(%llu time(s); DLSS reported %.0f MiB of its own); made again when it is switched on",
D.failed ? "it failed" : "the setting", RETIRE_PRESENTS, (unsigned long long)retired_n,
was > 0 ? was / 1048576.0 : 0.0);
} else {
rt_log("dlaa: frame generation no longer asks for the HUD-less picture: its images are freed");
if (!idle_free() || cfg_vram_keep()) return; // as idle_release: vram_use = high keeps them
if (dl_active()) {
retire_off_at = 0;
return;
}
retire_off_at = retire_hl_at = 0;
if (!retire_off_at) retire_off_at = now ? now : 1;
if (now < retire_off_at + RETIRE_PRESENTS || !up_holds()) return;
vk_queue_lock(), vkDeviceWaitIdle(vk_ctx()->device), vk_queue_unlock();
for (int i = 0; i < SNAPS; i++) destroy_snap(&R.s[i]);
vk_ffx_release_idle();
if (D.failed) { // as idle_release does for a size: frame generation is handed nothing of one from here on
for (int i = 0; i < DLSS_SIZES; i++) destroy_size(&D.sizes[i]);
D.last = NULL, D.host = NULL, D.fg_fresh = 0, D.fg_mv_view = VK_NULL_HANDLE, D.fg_mv = (VkFfxImage){0};
}
retired_n++;
rt_log("dlaa: the upscaler has not run for %d presents (%s): its mask snapshots and FSR contexts%s are freed (%llu "
"time(s)); made again when it runs", RETIRE_PRESENTS, D.failed ? "it failed" : "switched off",
D.failed ? " and DLSS sizes" : "", (unsigned long long)retired_n);
retire_off_at = 0;
}
void vk_dlaa_pass(VkExec *ex) {
@@ -2581,7 +2830,8 @@ void vk_dlaa_pass(VkExec *ex) {
retire_idle();
if (!dl_active()) return;
if (D.done) { // diagnostics: another scene drawn into a light target in the same present interval
if (ex->pass_ncolor == 1 && c->image && scene_color(c->format) && z->image && z->format == VK_FORMAT_D32_SFLOAT &&
// (not before the bloom: the flares and the composite after it are of that shape, and expected)
if (!D.pre && ex->pass_ncolor == 1 && c->image && scene_color(c->format) && z->image && z->format == VK_FORMAT_D32_SFLOAT &&
c->width == D.color.width && c->height == D.color.height && z->width == c->width)
F.second++, F.second_img = c->image;
return;
@@ -2628,6 +2878,8 @@ void vk_dlaa_pass(VkExec *ex) {
}
if (!D.armed) return;
if (!deferred) { // Ocarina's shape: the output pass ends the scene and runs DLSS
// NX2NAVI_DLSS_PREBLOOM: before the bloom chain's first pass instead, where there is one (prebloom_run)
if (D.pre_on && prebloom_start(ex) && prebloom_run(ex)) return;
int one = ex->pass_ncolor == 1 && c->image && !z->image && c->image != D.color.image && c->samples <= 1 &&
!c->is3d;
if (one) F.one_n++, F.one_w = c->width, F.one_h = c->height, F.one_fmt = c->format;
@@ -2749,6 +3001,39 @@ int vk_dlaa_resized_take(void) {
return 1;
}
// What DLSS holds while nothing uses it (NX2NAVI_DLSS_IDLE_FREE 1 or 2, the default 1). With the upscaler switched off
// in the menu, the sizes kept their images and features for the rest of the run, since only an evaluation frees a size
// (stale_sizes): at a 5120x2142 output that is the output, sharpening, mask, class and distance images, about 0.4 GiB,
// and about 0.5 GiB per feature. With frame generation off and DLSS on, the HUD-less picture (hudless) stayed the same
// way. After IDLE_FREE_PRESENTS presents without their use they go here, on the replaying thread that makes every
// other NGX call, without waiting for the GPU: the last evaluation that used them is that many presents back, and the
// CPU is never more than a few ahead. A feature made later is made with none alive, the case release_kept leaves
// clean. What it costs: switching DLSS on again more than IDLE_FREE_PRESENTS presents after switching it off makes
// the size's images and feature again, which its first evaluation waits for, as at the first frame DLSS ran in a run.
#define IDLE_FREE_PRESENTS 600
static uint64_t off_at; // D.presents when DLSS went off
static void idle_release(void) {
if (!idle_free() || cfg_vram_keep()) return; // vram_use = high keeps them
if (D.hl && D.presents - D.hl_at >= IDLE_FREE_PRESENTS && !D.hl_fresh) {
rt_log("dlaa: the HUD-less picture for frame generation went unused for %d presents: freed", IDLE_FREE_PRESENTS);
hl_destroy();
}
if (PB.scene && D.presents - PB.used >= IDLE_FREE_PRESENTS) { // prebloom off, or no upscaling, for that long
rt_log("dlaa: prebloom: the picture-size images went unused for %d presents: freed", IDLE_FREE_PRESENTS);
pb_destroy();
}
if (D.on || D.presents - off_at < IDLE_FREE_PRESENTS) return;
int n = 0;
for (int i = 0; i < DLSS_SIZES; i++)
if (D.sizes[i].out || D.sizes[i].feat) {
rt_log("dlaa: the DLSS size %ux%u -> %ux%u went unused for %d presents with the upscaler off: freed",
D.sizes[i].w, D.sizes[i].h, D.sizes[i].ow, D.sizes[i].oh, IDLE_FREE_PRESENTS);
destroy_size(&D.sizes[i]), n++;
}
if (n) // frame generation's motion vectors were a size's (evaluate); none is handed out this late (fg_current)
D.last = NULL, D.host = NULL, D.fg_fresh = 0, D.fg_mv_view = VK_NULL_HANDLE, D.fg_mv = (VkFfxImage){0};
}
void vk_dlaa_frame(void) {
static unsigned cfg_seen;
static int cfg_known;
@@ -2758,6 +3043,11 @@ void vk_dlaa_frame(void) {
else if (setting_presents) setting_presents--;
cfg_seen = v, cfg_known = 1;
vk_dlaa_swap_img = NULL, vk_dlaa_swap_view = VK_NULL_HANDLE;
D.pre = D.comp = 0, D.comp_game = VK_NULL_HANDLE; // the frame before was recorded up to its present
int pre_on = cfg_bool("NX2NAVI_DLSS_PREBLOOM"); // live: the evaluation point changes at the next frame
if (pre_on != D.pre_on) rt_log("dlaa: prebloom %s", pre_on ? "on: the upscaler runs before the bloom where the frame has one"
: "off: the upscaler runs at the game's upscaler");
D.pre_on = pre_on;
dlss_mode();
preset_check();
if (!N.ngx_up) { // FSR 3.1 follows the setting
@@ -2768,6 +3058,7 @@ void vk_dlaa_frame(void) {
if (on != D.on) {
rt_log("dlaa: %s", on ? "on" : "off");
D.on = on;
off_at = D.presents;
// either way the next DLAA frame starts a new history from an unjittered frame
D.reset = 1, D.cam_prev_ok = 0, D.jit_ok = D.jit_used = D.armed = D.done = D.gbuf = D.ended = D.up = 0;
D.hold = 0;
@@ -2775,6 +3066,7 @@ void vk_dlaa_frame(void) {
memset(&F, 0, sizeof F);
return;
}
idle_release();
if (!dl_active()) return;
if (D.share_off) D.share_off--;
if (!D.done && D.armed) D.share_off = 120; // vk_dlaa_pass: the output pass without the mode's share for a while