mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-17 22:19:57 -04:00
Add one anti-aliasing setting: the game's SMAA, off, FXAA, SMAA at its Ultra preset, or TAA.
Ocarina anti-aliases every frame with its own SMAA in three compute passes after the bloom composite. The new aa setting (nx2navi.ini aa, the Screen tab's Anti-aliasing row) keeps that by default. off, fxaa and smaa leave out the game's edge and weight passes and replace its composition with a pass of our own that writes the same square-rooted RGB9E5 words into the scene target: a plain copy, an edge smoothing written here, or the SMAA reference implementation with its area and search textures (MIT). taa is DLSS or FSR 3.1 at the scene's size, which the row sets through the upscaler; while either runs, the game's SMAA stays whatever aa says. The game's passes are known by a hash of their guest code. The shader build now also rebuilds a header when a file its source includes changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -44,6 +44,8 @@ __pycache__/
|
||||
*.spv
|
||||
# The menu font's bytes as a header for compilers without #embed (tools/build_shaders.py EMBEDDED)
|
||||
/runtime/ui/roboto_ttf.h
|
||||
/runtime/smaa_area_tex.h
|
||||
/runtime/smaa_search_tex.h
|
||||
|
||||
# Build outputs
|
||||
*.exe
|
||||
|
||||
@@ -43,6 +43,7 @@ Code copied or adapted from another project is listed here with its source, the
|
||||
| AMD Display Library (ADL) declarations: `AdapterInfo`, `ADLSingleSensorData`, `ADLPMLogDataOutput`, the `ADL_PMLOG_*` sensor numbers and the `ADL2_*` function signatures | https://github.com/GPUOpen-LibrariesAndSDKs/display-library @ `84c881cb2136f04287eb3af12de00a9e2867aa9e` (`include/adl_structures.h`, `include/adl_defines.h`, `include/adl_sdk.h`), Copyright (c) 2016 - 2022 Advanced Micro Devices, Inc. | MIT | `runtime/perfmon.c` |
|
||||
| Zstandard 1.5.7 (MSYS2 `mingw-w64-ucrt-x86_64-zstd`), static link | https://github.com/facebook/zstd, Copyright (c) Meta Platforms, Inc. and affiliates | BSD-3-Clause | `runtime/resolution.c` (the HeapDef table), `runtime/ov_zstd.c` (the game's zstd frames decoded on the host) |
|
||||
| musl `fma.c`, `fmaf.c` and `scalbn.c`, adapted into one file for the x86-64-v2 build | https://github.com/kraj/musl @ `5a56ad13e1e0790c06122978aad2a14d6b9efc1a` (mirror of https://git.musl-libc.org/cgit/musl), Copyright (c) 2005-2020 Rich Felker and contributors | MIT | `runtime/fma.c` (the correctly rounded fma/fmaf the FP lowering needs on CPUs without FMA3) |
|
||||
| SMAA reference implementation and its precomputed area and search textures | https://github.com/iryoku/smaa @ `71c806a838bdd7d517df19192a20f0c61b3ca29d` (`SMAA.hlsl`; `Textures/AreaTex.h` and `Textures/SearchTex.h` as raw bytes; licence text in `runtime/third_party/smaa/LICENSE.txt`), Copyright (C) 2013 Jorge Jimenez, Jose I. Echevarria, Belen Masia, Fernando Navarro, Diego Gutierrez | MIT | `runtime/shaders/smaa.glsl` (unmodified), `runtime/third_party/smaa/AreaTex.bin`, `runtime/third_party/smaa/SearchTex.bin`; used by `runtime/shaders/smaa_*.comp` and `runtime/vk_aa.c` |
|
||||
|
||||
## Compiler runtime in a source build
|
||||
|
||||
|
||||
@@ -187,6 +187,11 @@ keeps per-core data without locks and relies on that, so the host models it (`ru
|
||||
cache", `cache_save`).
|
||||
- The game's tensor-core upscaler is replaced by a decode-and-scale pass (`vk_tonemap.c`); `vk_fsr.c` runs AMD FSR 1
|
||||
(EASU, RCAS) on the present when the window is larger than the picture.
|
||||
- Anti-aliasing (`vk_aa.c`, `NX2NAVI_AA`): the game anti-aliases with its own SMAA, three compute passes after the bloom
|
||||
composite (edges, blending weights, composition into the scene target as square-rooted RGB9E5 words). `off`, `fxaa`
|
||||
and `smaa` leave out the first two and replace the composition with `shaders/aa_post.comp` or SMAA at its Ultra preset
|
||||
(`shaders/smaa_*.comp`); the game's passes are known by a hash of their guest code (`vk_exec_draw.c` `aa_dispatch`).
|
||||
`taa` is DLSS or FSR 3.1 at the scene's size; while either runs, the game's SMAA stays.
|
||||
- HDR (`vk_hdr.c`): the game tonemaps its scene into a float target, upscales it, draws the HUD into a second float
|
||||
target and copies that into the 8-bit sRGB window texture it presents. With `NX2NAVI_HDR=1` the tonemap shader gets
|
||||
the function of `shaders/hdr_tonemap.frag` spliced in where its SDR curve clamps each channel, so the float picture
|
||||
|
||||
@@ -490,8 +490,13 @@
|
||||
# frame before, DLSS is told to favour the current frame by this percentage (0, 35, 65 or 100; 0 off). Higher removes
|
||||
# more of the trails and leaves those edges a little less smooth. Changes while the game runs take effect at once.
|
||||
# dlss_char_mask = 65
|
||||
# The game's own FXAA after the post effects; with DLAA on it is not needed. Changes while the game runs take effect at
|
||||
# once.
|
||||
# Anti-aliasing of the 3D picture: game (the game's own SMAA, as on the console), off, fxaa, smaa (SMAA at its Ultra
|
||||
# preset in the game's place) or taa (DLSS or FSR 3.1 at full size: dlaa = 1 with dlss = dlaa, which the menu's
|
||||
# Anti-aliasing row sets with it). While DLSS or FSR 3.1 runs, the game's SMAA stays whatever this says. Changes while
|
||||
# the game runs take effect at once.
|
||||
# aa = game
|
||||
# The FXAA switch of the game this runtime was first written for; Ocarina has no such pass, so it changes nothing here
|
||||
# (aa above is the setting).
|
||||
# fxaa = 1
|
||||
# Frame generation: off, fsr3 (AMD FSR 3.1 on any GPU where amd_fidelityfx_vk.dll loads, so not on Linux: one generated
|
||||
# frame between every two the game draws, the HUD kept out of it) or dlss (NVIDIA DLSS Frame Generation on GeForce RTX
|
||||
|
||||
@@ -35,7 +35,7 @@ static int save_ok = 1; // the last cfg_save_to wrote (o
|
||||
// logged as unknown. A new variable goes here too, or its ini key logs a harmless "unknown key" line.
|
||||
static const char *const known[] = {
|
||||
"CORES", "TM_DISPATCH", "NVN2_CS_TABLE", "SKIP_SHADERS", "MOVIE_DEBAND", "MOVIE_FPS", "MOVIE_SKIP", "MOVIE_OVERLAY", "MOVIES", "NPLN", "NVN2_PROGRAMS", "NVN_TRAP", "OCARINA_REALTIME", "ONESHOT_BUDGET", "ONESHOT_WAIT_MS", "OCARINA_STEP", "PHYSICS_STEPS", "PICKUP_COUNT", "STEP_STEADY", "VI_HZ", "VI_VRR", "TIME_DUMP", "SHADER_DUMP", "SHOT_AFTER", "SHOT_DIR", "SHOT_EVERY", // Switch 2 port: movie skip (romfs.c), NVN2 proc traps (nvn_procs.c)
|
||||
"AB", "AMIIBO_DIR", "ANISO", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CHANNELS", "AUDIO_DEVICE", "AUDIO_GAME_PRIO", "AUDIO_OFF", "AUDIO_PREFILL_MS", "AUDIO_STATS",
|
||||
"AA", "AA_LOG", "AB", "AMIIBO_DIR", "ANISO", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CHANNELS", "AUDIO_DEVICE", "AUDIO_GAME_PRIO", "AUDIO_OFF", "AUDIO_PREFILL_MS", "AUDIO_STATS",
|
||||
"BARRIER_GAME", "MESH_AHEAD", "MESH_FALLBACK", "MESH_FALLBACK_MB", "PROBE_MESHFB_DIR",
|
||||
"BACKGROUND_WINDOW", "BIND_A", "BIND_B", "BIND_DOWN", "BIND_L", "BIND_LCLICK", "BIND_LEFT", "BIND_LSTICK_DOWN", "BIND_LSTICK_LEFT",
|
||||
"BIND_LSTICK_RIGHT", "BIND_LSTICK_UP", "BIND_MINUS",
|
||||
@@ -99,7 +99,7 @@ static const struct { const char *key, *section; } sections[] = {
|
||||
{"COLOR_BRIGHTNESS", "display"}, {"COLOR_CONTRAST", "display"}, {"COLOR_SATURATION", "display"},
|
||||
{"COLOR_GAMMA", "display"}, {"HDR", "display"}, {"HDR_LIVE", "display"}, {"HDR_PEAK_NITS", "display"}, {"HDR_GAME_NITS", "display"},
|
||||
{"HDR_UI_NITS", "display"}, {"SHADOW_RES", "display"},
|
||||
{"DLAA", "display"}, {"FSR4", "display"}, {"FSR4_FG", "display"}, {"DLSS", "display"}, {"DLSS_PRESET", "display"}, {"DLSS_IDLE_FREE", "display"}, {"DLSS_CHAR_MASK", "display"}, {"FXAA", "display"},
|
||||
{"DLAA", "display"}, {"FSR4", "display"}, {"FSR4_FG", "display"}, {"DLSS", "display"}, {"DLSS_PRESET", "display"}, {"DLSS_IDLE_FREE", "display"}, {"DLSS_CHAR_MASK", "display"}, {"FXAA", "display"}, {"AA", "display"},
|
||||
{"FRAMEGEN", "display"}, {"FRAMEGEN_MULTIPLIER", "display"}, {"FG_MULT", "display"},
|
||||
{"DEVICE_PROFILE", "display"}, {"ASSUME_FOCUS", "display"},
|
||||
{"DISCORD_RPC", "discord"}, {"DISCORD_APP_ID", "discord"}, {"DISCORD_IMAGE", "discord"},
|
||||
@@ -177,6 +177,7 @@ static const CfgMeta metas[] = {
|
||||
// the same where DLSS's frame generation runs through NGX itself (gfx_present.c ng_multiplier): 2x to 6x or dynamic
|
||||
{"FG_MULT", "2", "2|3|4|5|6|dynamic", LIVE},
|
||||
{"DLSS_CHAR_MASK", "65", "0|35|65|100", LIVE}, {"FXAA", "1", "0|1", LIVE},
|
||||
{"AA", "game", "game|off|fxaa|smaa|taa", LIVE}, // vk_aa.c: what runs in place of the game's SMAA
|
||||
{"GAME_ASPECT", "16:9", "16:9|window|21:9|32:9", RESTART}, // widescreen.c
|
||||
{"GAME_RES", "1440", "1080|1440|1800|2160|2880|window", RESTART}, // widescreen.c: the picture's height
|
||||
{"ASPECT", "fit", "fit|stretch", LIVE}, {"FOV", "100", "70..140", LIVE},
|
||||
|
||||
19
runtime/shaders/aa_common.glsl
Normal file
19
runtime/shaders/aa_common.glsl
Normal file
@@ -0,0 +1,19 @@
|
||||
// Shared by the anti-aliasing passes (runtime/vk_aa.c) that stand in for the game's SMAA. The game's composition pass
|
||||
// stores the post-processed picture into its R11G11B10F scene target as raw words: each channel is the square root of
|
||||
// its linear value, the three packed together as RGB9E5 (red in bits 0-8, green 9-17, blue 18-26, the shared exponent
|
||||
// in 27-31, Vulkan's E5B9G9R9 layout). The passes here write the same words through an R32_UINT view of that target.
|
||||
|
||||
// RGB9E5 of a non-negative colour: the shared exponent of the largest channel, each channel rounded to 9 bits of it.
|
||||
uint aa_pack_rgb9e5(vec3 c) {
|
||||
const float MAXV = 65408.0; // (2^9 - 1) / 2^9 * 2^15: the largest value the format holds
|
||||
c = clamp(c, vec3(0.0), vec3(MAXV));
|
||||
float m = max(max(c.r, c.g), c.b);
|
||||
float e = max(-16.0, floor(log2(max(m, 1e-30)))) + 16.0; // biased by 15, plus one
|
||||
float scale = exp2(e - 24.0); // 2^(e - 15 - 9)
|
||||
if (floor(m / scale + 0.5) >= 512.0) e += 1.0, scale *= 2.0;
|
||||
uvec3 q = uvec3(floor(c / scale + 0.5));
|
||||
return q.r | (q.g << 9) | (q.b << 18) | (uint(e) << 27);
|
||||
}
|
||||
|
||||
// The stored form of a linear colour: its square root, which is also the space the edges are looked for in.
|
||||
vec3 aa_encode(vec3 linear) { return sqrt(max(linear, vec3(0.0))); }
|
||||
100
runtime/shaders/aa_post.comp
Normal file
100
runtime/shaders/aa_post.comp
Normal file
@@ -0,0 +1,100 @@
|
||||
#version 450
|
||||
// The game's SMAA composition replaced (runtime/vk_aa.c): mode 0 stores the composite as it is (anti-aliasing off),
|
||||
// mode 1 runs a fast approximate edge smoothing (FXAA) over it first. Input: the game's composite, linear, already
|
||||
// tonemapped. Output: the scene target's raw RGB9E5 words of the square root of each channel (aa_common.glsl).
|
||||
//
|
||||
// The smoothing works on the square-rooted luma: where the local contrast is high enough, it finds whether the edge
|
||||
// runs across or along the pixel, walks along the edge both ways until the luma pair it follows changes, and moves the
|
||||
// sample point toward the neighbour across the edge by how far the pixel sits from the nearer end of the edge. A
|
||||
// blur of the 3x3 neighbourhood's contrast handles edges thinner than a pixel. The sample at the moved point is
|
||||
// bilinear, so the move blends the two sides.
|
||||
#extension GL_GOOGLE_include_directive : require
|
||||
#include "aa_common.glsl"
|
||||
|
||||
layout(local_size_x = 8, local_size_y = 8) in;
|
||||
layout(binding = 0) uniform sampler2D src;
|
||||
layout(binding = 1, r32ui) uniform writeonly uimage2D dst;
|
||||
layout(push_constant) uniform Push {
|
||||
vec2 inv; // 1 / size
|
||||
ivec2 size; // the scene target's size
|
||||
int mode; // 0 copy, 1 FXAA
|
||||
} pc;
|
||||
|
||||
vec3 at(vec2 uv) { return aa_encode(textureLod(src, uv, 0.0).rgb); }
|
||||
float luma(vec3 c) { return dot(c, vec3(0.299, 0.587, 0.114)); }
|
||||
float luma_at(vec2 uv) { return luma(at(uv)); }
|
||||
|
||||
const float EDGE_MIN = 0.0312; // no smoothing below this contrast (dark areas)
|
||||
const float EDGE_REL = 0.125; // nor below this share of the brightest neighbour
|
||||
const float SUBPIXEL = 0.75; // how much a thin feature is blurred
|
||||
const int STEPS = 12;
|
||||
const float STEP_LEN[STEPS] = float[](1.0, 1.0, 1.0, 1.0, 1.0, 1.5, 2.0, 2.0, 2.0, 2.0, 4.0, 8.0);
|
||||
|
||||
vec3 fxaa(vec2 uv) {
|
||||
vec3 cM = at(uv);
|
||||
float lM = luma(cM);
|
||||
vec2 d = pc.inv;
|
||||
float lN = luma_at(uv + vec2(0.0, -d.y)), lS = luma_at(uv + vec2(0.0, d.y));
|
||||
float lW = luma_at(uv + vec2(-d.x, 0.0)), lE = luma_at(uv + vec2(d.x, 0.0));
|
||||
float lo = min(lM, min(min(lN, lS), min(lW, lE)));
|
||||
float hi = max(lM, max(max(lN, lS), max(lW, lE)));
|
||||
float range = hi - lo;
|
||||
if (range < max(EDGE_MIN, hi * EDGE_REL)) return cM;
|
||||
float lNW = luma_at(uv + vec2(-d.x, -d.y)), lNE = luma_at(uv + vec2(d.x, -d.y));
|
||||
float lSW = luma_at(uv + vec2(-d.x, d.y)), lSE = luma_at(uv + vec2(d.x, d.y));
|
||||
|
||||
// thin features: how far the centre stands out from the neighbourhood's weighted mean
|
||||
float mean = (2.0 * (lN + lS + lW + lE) + lNW + lNE + lSW + lSE) / 12.0;
|
||||
float sub = clamp(abs(mean - lM) / range, 0.0, 1.0);
|
||||
sub = smoothstep(0.0, 1.0, sub);
|
||||
sub = sub * sub * SUBPIXEL;
|
||||
|
||||
// the edge's direction: horizontal when the luma changes more from row to row than from column to column
|
||||
float across_rows = abs(0.25 * lNW - 0.5 * lN + 0.25 * lNE) + abs(0.5 * lW - lM + 0.5 * lE) +
|
||||
abs(0.25 * lSW - 0.5 * lS + 0.25 * lSE);
|
||||
float across_cols = abs(0.25 * lNW - 0.5 * lW + 0.25 * lSW) + abs(0.5 * lN - lM + 0.5 * lS) +
|
||||
abs(0.25 * lNE - 0.5 * lE + 0.25 * lSE);
|
||||
bool horizontal = across_cols >= across_rows;
|
||||
|
||||
// which side of the pixel the edge is on: the neighbour across it with the larger step
|
||||
float l1 = horizontal ? lN : lW, l2 = horizontal ? lS : lE;
|
||||
float g1 = abs(l1 - lM), g2 = abs(l2 - lM);
|
||||
bool first = g1 >= g2;
|
||||
float grad = 0.25 * max(g1, g2);
|
||||
float step_len = horizontal ? d.y : d.x;
|
||||
float pair; // the mean luma of the pixel and its neighbour across the edge, which the walk follows
|
||||
if (first) step_len = -step_len, pair = 0.5 * (l1 + lM);
|
||||
else pair = 0.5 * (l2 + lM);
|
||||
vec2 edge = uv;
|
||||
if (horizontal) edge.y += 0.5 * step_len;
|
||||
else edge.x += 0.5 * step_len;
|
||||
|
||||
// walk along the edge both ways until the pair's luma changes by more than a quarter of the step across it
|
||||
vec2 dir = horizontal ? vec2(d.x, 0.0) : vec2(0.0, d.y);
|
||||
vec2 uv1 = edge - dir * STEP_LEN[0], uv2 = edge + dir * STEP_LEN[0];
|
||||
float e1 = luma_at(uv1) - pair, e2 = luma_at(uv2) - pair;
|
||||
bool done1 = abs(e1) >= grad, done2 = abs(e2) >= grad;
|
||||
for (int i = 1; i < STEPS && !(done1 && done2); i++) {
|
||||
if (!done1) uv1 -= dir * STEP_LEN[i], e1 = luma_at(uv1) - pair, done1 = abs(e1) >= grad;
|
||||
if (!done2) uv2 += dir * STEP_LEN[i], e2 = luma_at(uv2) - pair, done2 = abs(e2) >= grad;
|
||||
}
|
||||
float dist1 = horizontal ? uv.x - uv1.x : uv.y - uv1.y;
|
||||
float dist2 = horizontal ? uv2.x - uv.x : uv2.y - uv.y;
|
||||
bool nearer1 = dist1 < dist2;
|
||||
float offset = 0.5 - min(dist1, dist2) / (dist1 + dist2);
|
||||
// only when the luma at the nearer end moves the way the centre's does: else the pixel is outside the edge's span
|
||||
if (((nearer1 ? e1 : e2) < 0.0) == (lM < pair)) offset = 0.0;
|
||||
offset = max(offset, sub);
|
||||
vec2 moved = uv;
|
||||
if (horizontal) moved.y += offset * step_len;
|
||||
else moved.x += offset * step_len;
|
||||
return at(moved);
|
||||
}
|
||||
|
||||
void main() {
|
||||
ivec2 p = ivec2(gl_GlobalInvocationID.xy);
|
||||
if (p.x >= pc.size.x || p.y >= pc.size.y) return;
|
||||
vec2 uv = (vec2(p) + 0.5) * pc.inv;
|
||||
vec3 c = pc.mode == 1 ? fxaa(uv) : aa_encode(texelFetch(src, p, 0).rgb);
|
||||
imageStore(dst, p, uvec4(aa_pack_rgb9e5(c), 0u, 0u, 0u));
|
||||
}
|
||||
1361
runtime/shaders/smaa.glsl
Normal file
1361
runtime/shaders/smaa.glsl
Normal file
File diff suppressed because it is too large
Load Diff
19
runtime/shaders/smaa_blend.comp
Normal file
19
runtime/shaders/smaa_blend.comp
Normal file
@@ -0,0 +1,19 @@
|
||||
#version 450
|
||||
// SMAA pass 3 (runtime/vk_aa.c): neighbourhood blending of the game's composite by the weights, in linear light (as SMAA
|
||||
// asks of a picture read through an sRGB view), stored into the game's scene target as its raw RGB9E5 words of the
|
||||
// square root (aa_common.glsl).
|
||||
#include "smaa_setup.glsl"
|
||||
#include "aa_common.glsl"
|
||||
layout(binding = 0) uniform sampler2D color; // the game's composite, linear filtering
|
||||
layout(binding = 1) uniform sampler2D weights; // linear filtering
|
||||
layout(binding = 2, r32ui) uniform writeonly uimage2D dst;
|
||||
|
||||
void main() {
|
||||
ivec2 p;
|
||||
vec2 uv;
|
||||
if (!smaa_pixel(p, uv)) return;
|
||||
vec4 offset;
|
||||
SMAANeighborhoodBlendingVS(uv, offset);
|
||||
vec3 c = SMAANeighborhoodBlendingPS(uv, offset, color, weights).rgb;
|
||||
imageStore(dst, p, uvec4(aa_pack_rgb9e5(aa_encode(c)), 0u, 0u, 0u));
|
||||
}
|
||||
15
runtime/shaders/smaa_edge.comp
Normal file
15
runtime/shaders/smaa_edge.comp
Normal file
@@ -0,0 +1,15 @@
|
||||
#version 450
|
||||
// SMAA pass 1 (runtime/vk_aa.c): luma edge detection over pass 0's luma into the edges image (red: an edge on the
|
||||
// pixel's left, green: on its top).
|
||||
#include "smaa_setup.glsl"
|
||||
layout(binding = 0) uniform sampler2D luma; // smaa_encode.comp's, linear filtering
|
||||
layout(binding = 1, rgba8) uniform writeonly image2D edges;
|
||||
|
||||
void main() {
|
||||
ivec2 p;
|
||||
vec2 uv;
|
||||
if (!smaa_pixel(p, uv)) return;
|
||||
vec4 offset[3];
|
||||
SMAAEdgeDetectionVS(uv, offset);
|
||||
imageStore(edges, p, vec4(SMAALumaEdgeDetectionPS(uv, offset, luma), 0.0, 0.0));
|
||||
}
|
||||
18
runtime/shaders/smaa_encode.comp
Normal file
18
runtime/shaders/smaa_encode.comp
Normal file
@@ -0,0 +1,18 @@
|
||||
#version 450
|
||||
// SMAA pass 0 (runtime/vk_aa.c): the luma SMAA looks for edges in, from the game's linear composite, in the square-root
|
||||
// space the scene target stores (aa_common.glsl). One channel: SMAA's luma edge detection takes the dot product of the
|
||||
// colour it reads with its luma weights, and a one-channel image reads as (r, 0, 0), so r holds the luma over the red
|
||||
// weight.
|
||||
#extension GL_GOOGLE_include_directive : require
|
||||
#include "aa_common.glsl"
|
||||
layout(local_size_x = 8, local_size_y = 8) in;
|
||||
layout(binding = 0) uniform sampler2D src;
|
||||
layout(binding = 1, r16f) uniform writeonly image2D dst;
|
||||
layout(push_constant) uniform Push { vec4 metrics; } pc;
|
||||
|
||||
void main() {
|
||||
ivec2 p = ivec2(gl_GlobalInvocationID.xy);
|
||||
if (p.x >= int(pc.metrics.z) || p.y >= int(pc.metrics.w)) return;
|
||||
const vec3 W = vec3(0.2126, 0.7152, 0.0722); // smaa.glsl SMAALumaEdgeDetectionPS's weights
|
||||
imageStore(dst, p, vec4(dot(aa_encode(texelFetch(src, p, 0).rgb), W) / W.r, 0.0, 0.0, 1.0));
|
||||
}
|
||||
21
runtime/shaders/smaa_setup.glsl
Normal file
21
runtime/shaders/smaa_setup.glsl
Normal file
@@ -0,0 +1,21 @@
|
||||
// Settings and resources shared by the SMAA passes (runtime/vk_aa.c; smaa_*.comp). smaa.glsl holds the SMAA reference
|
||||
// implementation, unmodified; its pixel- and vertex-shader functions run here inside compute shaders, one invocation
|
||||
// per pixel, with the texture coordinate of the pixel's centre. Its "discard" of a pixel without edges becomes nothing:
|
||||
// the edge pass then stores the zero edges it found, which is what the next pass reads for such a pixel anyway.
|
||||
#extension GL_GOOGLE_include_directive : require
|
||||
layout(local_size_x = 8, local_size_y = 8) in;
|
||||
layout(push_constant) uniform Push {
|
||||
vec4 metrics; // 1 / width, 1 / height, width, height
|
||||
} pc;
|
||||
#define SMAA_RT_METRICS pc.metrics
|
||||
#define SMAA_GLSL_4 1
|
||||
#define SMAA_PRESET_ULTRA 1
|
||||
#define discard
|
||||
#include "smaa.glsl"
|
||||
|
||||
// the pixel this invocation works on and its centre's texture coordinate; false past the image's edge
|
||||
bool smaa_pixel(out ivec2 p, out vec2 uv) {
|
||||
p = ivec2(gl_GlobalInvocationID.xy);
|
||||
uv = (vec2(p) + 0.5) * pc.metrics.xy;
|
||||
return p.x < int(pc.metrics.z) && p.y < int(pc.metrics.w);
|
||||
}
|
||||
17
runtime/shaders/smaa_weight.comp
Normal file
17
runtime/shaders/smaa_weight.comp
Normal file
@@ -0,0 +1,17 @@
|
||||
#version 450
|
||||
// SMAA pass 2 (runtime/vk_aa.c): blending weights from the edges, with the area and search lookup textures.
|
||||
#include "smaa_setup.glsl"
|
||||
layout(binding = 0) uniform sampler2D edges; // linear filtering
|
||||
layout(binding = 1) uniform sampler2D area; // linear filtering
|
||||
layout(binding = 2) uniform sampler2D search; // nearest
|
||||
layout(binding = 3, rgba8) uniform writeonly image2D weights;
|
||||
|
||||
void main() {
|
||||
ivec2 p;
|
||||
vec2 uv;
|
||||
if (!smaa_pixel(p, uv)) return;
|
||||
vec2 pix;
|
||||
vec4 offset[3];
|
||||
SMAABlendingWeightCalculationVS(uv, pix, offset);
|
||||
imageStore(weights, p, SMAABlendingWeightCalculationPS(uv, pix, offset, edges, area, search, vec4(0.0)));
|
||||
}
|
||||
BIN
runtime/third_party/smaa/AreaTex.bin
vendored
Normal file
BIN
runtime/third_party/smaa/AreaTex.bin
vendored
Normal file
Binary file not shown.
24
runtime/third_party/smaa/LICENSE.txt
vendored
Normal file
24
runtime/third_party/smaa/LICENSE.txt
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
Copyright (C) 2013 Jorge Jimenez (jorge@iryoku.com)
|
||||
Copyright (C) 2013 Jose I. Echevarria (joseignacioechevarria@gmail.com)
|
||||
Copyright (C) 2013 Belen Masia (bmasia@unizar.es)
|
||||
Copyright (C) 2013 Fernando Navarro (fernandn@microsoft.com)
|
||||
Copyright (C) 2013 Diego Gutierrez (diegog@unizar.es)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software. As clarification, there is no
|
||||
requirement that the copyright notice and permission be included in binary
|
||||
distributions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
BIN
runtime/third_party/smaa/SearchTex.bin
vendored
Normal file
BIN
runtime/third_party/smaa/SearchTex.bin
vendored
Normal file
Binary file not shown.
@@ -990,6 +990,45 @@ static void row_upscaling(void) {
|
||||
// FSR 4 that failed in a row stays listed (it runs on this device) while FSR 3.1 serves: the row shows FSR 3.1, and
|
||||
// choosing FSR 4 again tries it anew
|
||||
if (e.kind == FSR4) vk_ffx4_retry();
|
||||
// the Anti-aliasing row follows: DLSS and FSR 3.1 are its TAA, and leaving them leaves TAA for the game's SMAA
|
||||
if (e.kind >= DLSS) opt_apply("NX2NAVI_AA", "taa");
|
||||
else if (!strcmp(cfg_value("NX2NAVI_AA"), "taa")) opt_apply("NX2NAVI_AA", "game");
|
||||
}
|
||||
|
||||
// Anti-aliasing (vk_aa.c NX2NAVI_AA): the game's own SMAA (the default), off, FXAA or SMAA in its place, or TAA, which is
|
||||
// DLSS or FSR 3.1 at the scene's size, the Upscaling row's temporal modes. TAA is listed where one of them runs: NVIDIA
|
||||
// DLAA where DLSS is offered, else FSR 3.1 Native AA where it loads. While a temporal mode is set (dlaa), the row shows
|
||||
// TAA, noted as the Upscaling row's when that mode also scales. Choosing TAA sets the upscaler to the native mode;
|
||||
// choosing another value turns the temporal upscaler off, with frame generation, which needs it, as the Upscaling row
|
||||
// does for bilinear and FSR 1, and the picture is then fitted to the window as that row had it.
|
||||
static void row_aa(void) {
|
||||
enum { GAME, OFF, FXAA, SMAA, TAA };
|
||||
static const char *const values[] = {"game", "off", "fxaa", "smaa", "taa"};
|
||||
const bool dlss = dlss_offered(), fsr = vk_ffx_available(), temporal = cfg_bool("NX2NAVI_DLAA");
|
||||
const bool fsr_set = !strcmp(cfg_value("NX2NAVI_TAA"), "fsr3");
|
||||
const char *names[5] = {menu_text(UI_MENU_AA_GAME), menu_text(UI_MENU_FRAMEGEN_OFF), menu_text(UI_MENU_AA_FXAA),
|
||||
menu_text(UI_MENU_AA_SMAA), nullptr};
|
||||
int n = 4;
|
||||
if (dlss || fsr || temporal)
|
||||
names[n++] = menu_text((temporal ? !fsr_set : dlss) ? UI_MENU_AA_TAA_DLSS : UI_MENU_AA_TAA_FSR);
|
||||
int cur = GAME;
|
||||
if (temporal) cur = TAA;
|
||||
else
|
||||
for (int i = 0; i < 4; i++)
|
||||
if (!strcmp(cfg_value("NX2NAVI_AA"), values[i])) cur = i;
|
||||
const char *note = temporal && strcmp(cfg_value("NX2NAVI_DLSS"), "dlaa") ? menu_text(UI_MENU_AA_UPSCALER) : nullptr;
|
||||
int nv = opt_choice_index("NX2NAVI_AA", UI_MENU_AA, names, n, cur, GAME, UI_MENU_AA_DESC, note);
|
||||
if (nv < 0 || nv >= n || nv == cur) return;
|
||||
if (nv == TAA) {
|
||||
opt_apply("NX2NAVI_DLAA", "1");
|
||||
opt_apply("NX2NAVI_DLSS", "dlaa");
|
||||
opt_apply("NX2NAVI_TAA", dlss ? "dlss" : "fsr3");
|
||||
if (!dlss) opt_apply("NX2NAVI_FSR4", "0");
|
||||
} else if (temporal) {
|
||||
opt_apply("NX2NAVI_DLAA", "0");
|
||||
opt_apply("NX2NAVI_FRAMEGEN", "off");
|
||||
}
|
||||
opt_apply("NX2NAVI_AA", values[nv]);
|
||||
}
|
||||
|
||||
// Under the HDR value: "after restart" when what is set waits for one (the game's own HDR, to or from it:
|
||||
@@ -1223,6 +1262,7 @@ static void tab_screen(float scale) {
|
||||
if (wide_available()) opt_choice("NX2NAVI_GAME_ASPECT", UI_MENU_GAME_ASPECT, wide, 4, UI_MENU_GAME_ASPECT_DESC);
|
||||
if (wide_available()) row_resolution();
|
||||
row_upscaling();
|
||||
row_aa();
|
||||
row_framegen();
|
||||
row_hdr();
|
||||
// The sharpening runs under FSR 1 (vk_fsr.c vk_fsr_record, RCAS) and FSR 3.1 (vk_ffx.cpp, its own RCAS); with
|
||||
|
||||
@@ -151,6 +151,17 @@ static const char *const text_en[UI_TEXT_COUNT] = {
|
||||
[UI_MENU_DLSS_PERFORMANCE] = "Performance", [UI_MENU_DLSS_ULTRA] = "Ultra Performance",
|
||||
[UI_MENU_FXAA] = "Game anti-aliasing (FXAA)",
|
||||
[UI_MENU_FXAA_DESC] = "The game's own FXAA edge smoothing. It blurs fine detail a little; with DLAA on it is not needed.",
|
||||
[UI_MENU_AA] = "Anti-aliasing",
|
||||
[UI_MENU_AA_DESC] = "How the edges of the 3D picture are smoothed. Game SMAA is the game's own, as on the console. Off "
|
||||
"leaves the edges as drawn. FXAA is quick and a little soft. SMAA finds more of the edges than the "
|
||||
"game's and keeps detail sharp. TAA is DLSS or FSR 3.1 at full size: the steadiest edges in motion; "
|
||||
"it sets the upscaler. Takes effect at once.",
|
||||
[UI_MENU_AA_GAME] = "Game SMAA",
|
||||
[UI_MENU_AA_FXAA] = "FXAA",
|
||||
[UI_MENU_AA_SMAA] = "SMAA (Ultra)",
|
||||
[UI_MENU_AA_TAA_DLSS] = "TAA (NVIDIA DLAA)",
|
||||
[UI_MENU_AA_TAA_FSR] = "TAA (AMD FSR 3.1)",
|
||||
[UI_MENU_AA_UPSCALER] = "set by Upscaling",
|
||||
|
||||
[UI_MENU_FPS_LIMIT] = "Frame rate limit",
|
||||
[UI_MENU_FPS_LIMIT_DESC] = "The highest frame rate the game draws at. Frame generation adds its frames on top.",
|
||||
@@ -830,6 +841,17 @@ static const char *const text_ru[UI_TEXT_COUNT] = {
|
||||
[UI_MENU_DLSS_PERFORMANCE] = "Производительность", [UI_MENU_DLSS_ULTRA] = "Макс. производительность",
|
||||
[UI_MENU_FXAA] = "Сглаживание игры (FXAA)",
|
||||
[UI_MENU_FXAA_DESC] = "Собственное сглаживание FXAA. Слегка размывает мелкие детали; с DLAA оно не нужно.",
|
||||
[UI_MENU_AA] = "Сглаживание",
|
||||
[UI_MENU_AA_DESC] = "Как сглаживаются края 3D-картинки. SMAA игры — собственное, как на консоли. «Выкл.» оставляет края "
|
||||
"как есть. FXAA быстрое и слегка мягкое. SMAA находит больше краёв, чем игровое, и сохраняет "
|
||||
"чёткость. TAA — это DLSS или FSR 3.1 в полном размере: самые устойчивые края в движении; задаёт "
|
||||
"масштабирование. Действует сразу.",
|
||||
[UI_MENU_AA_GAME] = "SMAA игры",
|
||||
[UI_MENU_AA_FXAA] = "FXAA",
|
||||
[UI_MENU_AA_SMAA] = "SMAA (Ultra)",
|
||||
[UI_MENU_AA_TAA_DLSS] = "TAA (NVIDIA DLAA)",
|
||||
[UI_MENU_AA_TAA_FSR] = "TAA (AMD FSR 3.1)",
|
||||
[UI_MENU_AA_UPSCALER] = "задано масштабированием",
|
||||
|
||||
[UI_MENU_FPS_LIMIT] = "Ограничение частоты кадров",
|
||||
[UI_MENU_FPS_LIMIT_DESC] = "Максимальная частота кадров, которые рисует игра. Генерация кадров добавляет свои сверх неё.",
|
||||
@@ -1811,6 +1833,17 @@ static const char *const text_pt_br[UI_TEXT_COUNT] = {
|
||||
[UI_MENU_FXAA] = "Antisserrilhado do jogo (FXAA)",
|
||||
[UI_MENU_FXAA_DESC] = "A suavização de bordas FXAA do próprio jogo. Borra um pouco os detalhes finos; com o DLAA "
|
||||
"ligado não é necessária.",
|
||||
[UI_MENU_AA] = "Antisserrilhado",
|
||||
[UI_MENU_AA_DESC] = "Como as bordas da imagem 3D são suavizadas. SMAA do jogo é o próprio, como no console. Desligado "
|
||||
"deixa as bordas como desenhadas. FXAA é rápido e um pouco suave. SMAA encontra mais bordas que o do "
|
||||
"jogo e mantém os detalhes nítidos. TAA é o DLSS ou o FSR 3.1 em tamanho cheio: as bordas mais "
|
||||
"estáveis em movimento; define o escalonamento. Vale na hora.",
|
||||
[UI_MENU_AA_GAME] = "SMAA do jogo",
|
||||
[UI_MENU_AA_FXAA] = "FXAA",
|
||||
[UI_MENU_AA_SMAA] = "SMAA (Ultra)",
|
||||
[UI_MENU_AA_TAA_DLSS] = "TAA (NVIDIA DLAA)",
|
||||
[UI_MENU_AA_TAA_FSR] = "TAA (AMD FSR 3.1)",
|
||||
[UI_MENU_AA_UPSCALER] = "definido pelo escalonamento",
|
||||
[UI_MENU_TAB_CHEATS] = "Cheats",
|
||||
[UI_MENU_CHEATS_NONE] = "Nenhum cheat para esta versão do jogo. Os cheats de um mod são um arquivo de cheats do "
|
||||
"Atmosphere na pasta cheats dele, com o nome da build do jogo: mods\\<mod>\\cheats\\%s. "
|
||||
@@ -2579,6 +2612,15 @@ static const char *const text_zh_hans[UI_TEXT_COUNT] = {
|
||||
[UI_MENU_DLSS_CHARS_FULL] = "强",
|
||||
[UI_MENU_FXAA] = "游戏抗锯齿(FXAA)",
|
||||
[UI_MENU_FXAA_DESC] = "游戏自身的 FXAA 边缘平滑。会让细节略微模糊;开启 DLAA 时不需要。",
|
||||
[UI_MENU_AA] = "抗锯齿",
|
||||
[UI_MENU_AA_DESC] = "3D 画面边缘的平滑方式。游戏 SMAA 是游戏自带的,与主机相同。关闭则保留原始边缘。FXAA 快速,略显柔和。SMAA 比游戏自带的找到更多边缘,并保持细节清晰。TAA 即全尺寸的 DLSS 或 "
|
||||
"FSR 3.1:运动中边缘最稳定;它会设置放大方式。立即生效。",
|
||||
[UI_MENU_AA_GAME] = "游戏 SMAA",
|
||||
[UI_MENU_AA_FXAA] = "FXAA",
|
||||
[UI_MENU_AA_SMAA] = "SMAA(极高)",
|
||||
[UI_MENU_AA_TAA_DLSS] = "TAA(NVIDIA DLAA)",
|
||||
[UI_MENU_AA_TAA_FSR] = "TAA(AMD FSR 3.1)",
|
||||
[UI_MENU_AA_UPSCALER] = "由放大设置决定",
|
||||
[UI_MENU_TAB_CHEATS] = "金手指",
|
||||
[UI_MENU_CHEATS_NONE] = "此游戏版本没有金手指。模组的金手指是它的 cheats 文件夹中以游戏构建命名的 Atmosphere "
|
||||
"金手指文件:mods\\<模组>\\cheats\\%s。重启游戏后才会读取。对于修改游戏代码的金手指,"
|
||||
|
||||
@@ -61,6 +61,8 @@ typedef enum {
|
||||
UI_MENU_DLSS_BALANCED, UI_MENU_DLSS_PERFORMANCE, UI_MENU_DLSS_ULTRA, UI_MENU_FSR3_NATIVE, UI_MENU_DLSS_PRESET, UI_MENU_DLSS_PRESET_DESC,
|
||||
UI_MENU_DLSS_PRESET_DEFAULT, UI_MENU_DLSS_PRESET_K, UI_MENU_DLSS_PRESET_J, UI_MENU_DLSS_CHARS, UI_MENU_DLSS_CHARS_DESC,
|
||||
UI_MENU_DLSS_CHARS_OFF, UI_MENU_DLSS_CHARS_LIGHT, UI_MENU_DLSS_CHARS_MEDIUM, UI_MENU_DLSS_CHARS_FULL, UI_MENU_FXAA, UI_MENU_FXAA_DESC,
|
||||
UI_MENU_AA, UI_MENU_AA_DESC, UI_MENU_AA_GAME, UI_MENU_AA_FXAA, UI_MENU_AA_SMAA, UI_MENU_AA_TAA_DLSS, UI_MENU_AA_TAA_FSR,
|
||||
UI_MENU_AA_UPSCALER,
|
||||
|
||||
// Game
|
||||
UI_MENU_FPS_LIMIT, UI_MENU_FPS_LIMIT_DESC, UI_MENU_FPS_30, UI_MENU_FPS_40, UI_MENU_FPS_45, UI_MENU_FPS_60, UI_MENU_FPS_90,
|
||||
|
||||
329
runtime/vk_aa.c
Normal file
329
runtime/vk_aa.c
Normal file
@@ -0,0 +1,329 @@
|
||||
// The anti-aliasing setting (NX2NAVI_AA, nx2navi.ini aa): what runs in place of the game's own SMAA.
|
||||
//
|
||||
// Ocarina anti-aliases every frame with SMAA in three compute passes right after the bloom composite: edge detection
|
||||
// into 4x4-pixel tiles, blending weights from the tiles (an indirect dispatch, with the game's area lookup texture),
|
||||
// and the composition, which reads the composite and the weights and writes the result into the scene target that the
|
||||
// game's upscaler, the HUD and the output passes then take. The composition stores each channel's square root packed
|
||||
// as RGB9E5 words through an integer view of that R11G11B10F target (vk_exec_draw.c raw_note).
|
||||
//
|
||||
// aa = game (the default) leaves those passes alone. off, fxaa and smaa leave out the edge and weight passes and
|
||||
// replace the composition (vk_exec_draw.c aa_dispatch) with one of the passes here, which read the same composite and
|
||||
// write the same words: off stores it as it is, fxaa smooths it first (shaders/aa_post.comp), smaa runs SMAA at its
|
||||
// Ultra preset (shaders/smaa_*.comp, the reference implementation in shaders/smaa.glsl with the area and search
|
||||
// textures of third_party/smaa). taa is DLSS or FSR 3.1 at the scene's own size: the menu sets the upscaler for it
|
||||
// (ui/menu.cpp row_aa). While DLSS or FSR 3.1 anti-aliases the scene (dlaa = 1 and it runs), the game's SMAA stays as it
|
||||
// is whatever aa says: they take the picture after it, and their inputs are tuned with it.
|
||||
//
|
||||
// The game's passes are known by a hash of their guest code (vk_aa_role), which does not change with the translator or
|
||||
// its settings. Everything here runs on the replay thread.
|
||||
#include "vk.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "cfg.h"
|
||||
#include "host.h"
|
||||
#include "nvn_api.h" // gfx_counters
|
||||
#include "vk_dlaa.h"
|
||||
#include "vk_exec.h"
|
||||
#include "aa_post_spv.h"
|
||||
#include "smaa_blend_spv.h"
|
||||
#include "smaa_edge_spv.h"
|
||||
#include "smaa_encode_spv.h"
|
||||
#include "smaa_weight_spv.h"
|
||||
|
||||
static const unsigned char area_bytes[] = {
|
||||
#include "smaa_area_tex.h"
|
||||
};
|
||||
static const unsigned char search_bytes[] = {
|
||||
#include "smaa_search_tex.h"
|
||||
};
|
||||
#define AREA_W 160
|
||||
#define AREA_H 560
|
||||
#define SEARCH_W 64
|
||||
#define SEARCH_H 16
|
||||
|
||||
// FNV-1a over the guest code of the game's three SMAA compute programs (vk_exec_draw.c cs_guest_hash), game 1.1.1,
|
||||
// found with NX2NAVI_AA_LOG=1: the edges (a 4x4-pixel tile per invocation), the weights (an indirect dispatch over the
|
||||
// tiles with edges) and the composition (16x16 pixels per group)
|
||||
static const struct { uint64_t hash; int role; } GAME_SMAA[] = {
|
||||
{0xfcb21f2afa88378cull, VK_AA_ROLE_EDGES},
|
||||
{0xe44377cbfafc1101ull, VK_AA_ROLE_WEIGHTS},
|
||||
{0x470d661afb1cb73aull, VK_AA_ROLE_COMPOSE},
|
||||
};
|
||||
|
||||
int vk_aa_role(uint64_t guest_hash) {
|
||||
if (!guest_hash) return VK_AA_ROLE_NONE;
|
||||
for (size_t i = 0; i < sizeof GAME_SMAA / sizeof GAME_SMAA[0]; i++)
|
||||
if (GAME_SMAA[i].hash == guest_hash) return GAME_SMAA[i].role;
|
||||
return VK_AA_ROLE_NONE;
|
||||
}
|
||||
|
||||
static const char *const NAMES[] = {"game", "off", "fxaa", "smaa"};
|
||||
|
||||
// DLSS or FSR 3.1 anti-aliases this frame's scene: dlaa = 1 and one of them runs (vk_dlaa.c)
|
||||
static int temporal(void) { return cfg_bool("NX2NAVI_DLAA") && (vk_dlaa_available() || vk_dlaa_fsr3()); }
|
||||
|
||||
static struct {
|
||||
uint64_t frame_read;
|
||||
int mode; // this frame's (vk_aa_mode)
|
||||
int said; // the last refusal logged: 1 + the mode refused, 0 none
|
||||
int composed; // the last composition seen was replaced (vk_aa_composed): the passes before it may go
|
||||
uint64_t frames_on, composes; // frames with a replacement asked for, compositions replaced
|
||||
int none_told;
|
||||
} A = {.frame_read = ~0ull, .composed = 1};
|
||||
|
||||
int vk_aa_mode(void) {
|
||||
uint64_t f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);
|
||||
if (f == A.frame_read) return A.mode;
|
||||
A.frame_read = f;
|
||||
const char *v = cfg_value("NX2NAVI_AA");
|
||||
int want = VK_AA_GAME;
|
||||
for (int i = 1; i < 4; i++)
|
||||
if (!strcmp(v, NAMES[i])) want = i;
|
||||
int tmp = want != VK_AA_GAME && temporal();
|
||||
int said = tmp ? 1 + want : !strcmp(v, "taa") && !temporal() ? 1 + 4 : 0;
|
||||
if (said != A.said) {
|
||||
A.said = said;
|
||||
if (said == 5) rt_log("aa: taa needs DLSS or FSR 3.1 at work (dlaa = 1): the game's SMAA stays");
|
||||
else if (said) rt_log("aa: %s waits while DLSS or FSR 3.1 anti-aliases the scene: the game's SMAA stays", NAMES[want]);
|
||||
}
|
||||
A.mode = tmp ? VK_AA_GAME : want;
|
||||
if (A.mode != VK_AA_GAME && !A.none_told && ++A.frames_on == 3600 && !A.composes) {
|
||||
A.none_told = 1; // a game version or a frame without the passes this file knows: nothing is replaced
|
||||
rt_log("aa: aa = %s found none of the game's SMAA passes in %llu frames: the picture stays the game's",
|
||||
NAMES[A.mode], (unsigned long long)A.frames_on);
|
||||
}
|
||||
return A.mode;
|
||||
}
|
||||
|
||||
int vk_aa_skip_ok(void) { return A.composed; }
|
||||
|
||||
// A composition that could not be replaced runs as the game's, and the edge and weight passes run again from the next
|
||||
// frame on, until a composition is replaced again: the game's composition must never read weights left out.
|
||||
void vk_aa_composed(int ok) {
|
||||
if (ok && !A.composes++) rt_log("aa: the game's SMAA replaced by %s", NAMES[A.mode]);
|
||||
A.composed = ok;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- pipelines
|
||||
enum { P_POST, P_LUMA, P_EDGE, P_WEIGHT, P_BLEND, P_N };
|
||||
#define S VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
|
||||
#define I VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
|
||||
static const struct { const uint32_t *code; size_t size; int n; VkDescriptorType t[4]; const char *name; } PIPES[P_N] = {
|
||||
{aa_post_spv, sizeof aa_post_spv, 2, {S, I}, "aa_post"},
|
||||
{smaa_encode_spv, sizeof smaa_encode_spv, 2, {S, I}, "smaa_encode"},
|
||||
{smaa_edge_spv, sizeof smaa_edge_spv, 2, {S, I}, "smaa_edge"},
|
||||
{smaa_weight_spv, sizeof smaa_weight_spv, 4, {S, S, S, I}, "smaa_weight"},
|
||||
{smaa_blend_spv, sizeof smaa_blend_spv, 3, {S, S, I}, "smaa_blend"},
|
||||
};
|
||||
#undef S
|
||||
#undef I
|
||||
|
||||
typedef struct { VkImage img; VmaAllocation mem; VkImageView view; } Img;
|
||||
|
||||
static struct {
|
||||
int tried, ok;
|
||||
PFN_vkCmdPushDescriptorSetKHR push;
|
||||
VkDescriptorSetLayout dsl[P_N];
|
||||
VkPipelineLayout layout[P_N];
|
||||
VkPipeline pipe[P_N];
|
||||
VkSampler linear, nearest;
|
||||
int tables; // the area and search textures: 0 not made, 1 ready, -1 failed
|
||||
Img area, search;
|
||||
Img luma, edges, weights; // at the scene's size (w, h)
|
||||
uint32_t w, h;
|
||||
} P;
|
||||
|
||||
static int pipes_init(void) {
|
||||
if (P.tried) return P.ok;
|
||||
P.tried = 1;
|
||||
VkCtx *c = vk_ctx();
|
||||
P.push = (PFN_vkCmdPushDescriptorSetKHR)vk_dpa(c->device, "vkCmdPushDescriptorSetKHR");
|
||||
int ok = P.push != NULL;
|
||||
VkSamplerCreateInfo si = {.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, .magFilter = VK_FILTER_LINEAR,
|
||||
.minFilter = VK_FILTER_LINEAR, .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
|
||||
.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
|
||||
.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
|
||||
.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, .maxLod = 0};
|
||||
ok = ok && vkCreateSampler(c->device, &si, NULL, &P.linear) == VK_SUCCESS;
|
||||
si.magFilter = si.minFilter = VK_FILTER_NEAREST;
|
||||
ok = ok && vkCreateSampler(c->device, &si, NULL, &P.nearest) == VK_SUCCESS;
|
||||
for (int p = 0; ok && p < P_N; p++) {
|
||||
VkDescriptorSetLayoutBinding b[4];
|
||||
for (int i = 0; i < PIPES[p].n; i++)
|
||||
b[i] = (VkDescriptorSetLayoutBinding){.binding = (uint32_t)i, .descriptorType = PIPES[p].t[i],
|
||||
.descriptorCount = 1, .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT};
|
||||
VkDescriptorSetLayoutCreateInfo dci = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
|
||||
.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR,
|
||||
.bindingCount = (uint32_t)PIPES[p].n, .pBindings = b};
|
||||
VkPushConstantRange pr = {VK_SHADER_STAGE_COMPUTE_BIT, 0, 32};
|
||||
VkPipelineLayoutCreateInfo li = {.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, .setLayoutCount = 1,
|
||||
.pSetLayouts = &P.dsl[p], .pushConstantRangeCount = 1, .pPushConstantRanges = &pr};
|
||||
VkShaderModuleCreateInfo mi = {.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, .codeSize = PIPES[p].size,
|
||||
.pCode = PIPES[p].code};
|
||||
VkShaderModule m = VK_NULL_HANDLE;
|
||||
ok = vkCreateDescriptorSetLayout(c->device, &dci, NULL, &P.dsl[p]) == VK_SUCCESS &&
|
||||
vkCreatePipelineLayout(c->device, &li, NULL, &P.layout[p]) == VK_SUCCESS &&
|
||||
vkCreateShaderModule(c->device, &mi, NULL, &m) == VK_SUCCESS;
|
||||
if (ok) {
|
||||
VkComputePipelineCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
|
||||
.stage = {.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
|
||||
.stage = VK_SHADER_STAGE_COMPUTE_BIT, .module = m, .pName = "main"},
|
||||
.layout = P.layout[p]};
|
||||
ok = vkCreateComputePipelines(c->device, VK_NULL_HANDLE, 1, &ci, NULL, &P.pipe[p]) == VK_SUCCESS;
|
||||
}
|
||||
if (m) vkDestroyShaderModule(c->device, m, NULL);
|
||||
if (!ok) rt_log("aa: the %s pipeline could not be created", PIPES[p].name);
|
||||
}
|
||||
rt_log("aa: anti-aliasing passes %s", ok ? "ready" : "unavailable: the game's SMAA stays");
|
||||
return P.ok = ok;
|
||||
}
|
||||
|
||||
static void img_free(Img *m) {
|
||||
if (m->view) vk_defer_view(m->view);
|
||||
if (m->img) vk_defer_image(m->img, m->mem);
|
||||
memset(m, 0, sizeof *m);
|
||||
}
|
||||
|
||||
// An image of the passes' own, in the GENERAL layout from cb on (the transition is recorded into cb)
|
||||
static int img_make(VkCommandBuffer cb, Img *m, VkFormat f, uint32_t w, uint32_t h, VkImageUsageFlags usage) {
|
||||
VkCtx *c = vk_ctx();
|
||||
VkImageCreateInfo ii = {.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, .imageType = VK_IMAGE_TYPE_2D, .format = f,
|
||||
.extent = {w, h, 1}, .mipLevels = 1, .arrayLayers = 1, .samples = VK_SAMPLE_COUNT_1_BIT,
|
||||
.tiling = VK_IMAGE_TILING_OPTIMAL, .usage = usage | VK_IMAGE_USAGE_SAMPLED_BIT,
|
||||
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED};
|
||||
VmaAllocationCreateInfo ai = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE};
|
||||
if (vmaCreateImage(c->vma, &ii, &ai, &m->img, &m->mem, NULL) != VK_SUCCESS) return m->img = VK_NULL_HANDLE, 0;
|
||||
VkImageViewCreateInfo vi = {.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .image = m->img,
|
||||
.viewType = VK_IMAGE_VIEW_TYPE_2D, .format = f,
|
||||
.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
|
||||
if (vkCreateImageView(c->device, &vi, NULL, &m->view) != VK_SUCCESS) {
|
||||
vmaDestroyImage(c->vma, m->img, m->mem);
|
||||
memset(m, 0, sizeof *m);
|
||||
return 0;
|
||||
}
|
||||
VkImageMemoryBarrier2 b = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
|
||||
.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 = (usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) ? VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
|
||||
: VK_IMAGE_LAYOUT_GENERAL,
|
||||
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.image = m->img, .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
|
||||
VkDependencyInfo d = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .imageMemoryBarrierCount = 1, .pImageMemoryBarriers = &b};
|
||||
vkCmdPipelineBarrier2(cb, &d);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// The area and search textures, uploaded once through the upload batch and waited for (the first SMAA frame only)
|
||||
static int tables_make(void) {
|
||||
if (P.tables) return P.tables > 0;
|
||||
P.tables = -1;
|
||||
VkCommandBuffer cb = vk_upload_begin();
|
||||
int ok = img_make(cb, &P.area, VK_FORMAT_R8G8_UNORM, AREA_W, AREA_H, VK_IMAGE_USAGE_TRANSFER_DST_BIT) &&
|
||||
img_make(cb, &P.search, VK_FORMAT_R8_UNORM, SEARCH_W, SEARCH_H, VK_IMAGE_USAGE_TRANSFER_DST_BIT);
|
||||
VkBuffer buf = VK_NULL_HANDLE;
|
||||
VkDeviceSize off = 0;
|
||||
uint8_t *st = ok ? vk_staging_at(sizeof area_bytes + sizeof search_bytes, &buf, &off) : NULL;
|
||||
if (st) {
|
||||
memcpy(st, area_bytes, sizeof area_bytes);
|
||||
memcpy(st + sizeof area_bytes, search_bytes, sizeof search_bytes);
|
||||
VkBufferImageCopy r = {.bufferOffset = off, .imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
|
||||
.imageExtent = {AREA_W, AREA_H, 1}};
|
||||
vkCmdCopyBufferToImage(cb, buf, P.area.img, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &r);
|
||||
r.bufferOffset = off + sizeof area_bytes, r.imageExtent = (VkExtent3D){SEARCH_W, SEARCH_H, 1};
|
||||
vkCmdCopyBufferToImage(cb, buf, P.search.img, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &r);
|
||||
VkImageMemoryBarrier2 b[2];
|
||||
for (int i = 0; i < 2; i++)
|
||||
b[i] = (VkImageMemoryBarrier2){.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
|
||||
.srcStageMask = VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT,
|
||||
.srcAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT,
|
||||
.dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
|
||||
.dstAccessMask = VK_ACCESS_2_MEMORY_READ_BIT,
|
||||
.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
||||
.newLayout = VK_IMAGE_LAYOUT_GENERAL,
|
||||
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.image = i ? P.search.img : P.area.img,
|
||||
.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
|
||||
VkDependencyInfo d = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .imageMemoryBarrierCount = 2, .pImageMemoryBarriers = b};
|
||||
vkCmdPipelineBarrier2(cb, &d);
|
||||
}
|
||||
vk_upload_end();
|
||||
if (!st) {
|
||||
img_free(&P.area), img_free(&P.search);
|
||||
rt_log("aa: SMAA's lookup textures could not be made: smaa runs as off");
|
||||
return 0;
|
||||
}
|
||||
vk_upload_flush(1);
|
||||
P.tables = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// SMAA's work images at the scene's size, made again when it changes
|
||||
static int work_images(VkCommandBuffer cb, uint32_t w, uint32_t h) {
|
||||
if (P.luma.img && P.w == w && P.h == h) return 1;
|
||||
img_free(&P.luma), img_free(&P.edges), img_free(&P.weights);
|
||||
P.w = P.h = 0;
|
||||
if (!img_make(cb, &P.luma, VK_FORMAT_R16_SFLOAT, w, h, VK_IMAGE_USAGE_STORAGE_BIT) ||
|
||||
!img_make(cb, &P.edges, VK_FORMAT_R8G8B8A8_UNORM, w, h, VK_IMAGE_USAGE_STORAGE_BIT) ||
|
||||
!img_make(cb, &P.weights, VK_FORMAT_R8G8B8A8_UNORM, w, h, VK_IMAGE_USAGE_STORAGE_BIT)) {
|
||||
img_free(&P.luma), img_free(&P.edges), img_free(&P.weights);
|
||||
return 0;
|
||||
}
|
||||
P.w = w, P.h = h;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// One pass: its pipeline, its images (samplers: linear but for the search texture) and the push constants
|
||||
static void pass(VkCommandBuffer cb, int p, const VkImageView *views, const VkSampler *samp, const void *pc, size_t pcn,
|
||||
uint32_t w, uint32_t h) {
|
||||
VkDescriptorImageInfo ii[4];
|
||||
VkWriteDescriptorSet wr[4];
|
||||
for (int i = 0; i < PIPES[p].n; i++) {
|
||||
int sampled = PIPES[p].t[i] == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
||||
ii[i] = (VkDescriptorImageInfo){sampled ? samp[i] : VK_NULL_HANDLE, views[i], VK_IMAGE_LAYOUT_GENERAL};
|
||||
wr[i] = (VkWriteDescriptorSet){.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = (uint32_t)i,
|
||||
.descriptorCount = 1, .descriptorType = PIPES[p].t[i], .pImageInfo = &ii[i]};
|
||||
}
|
||||
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE, P.pipe[p]);
|
||||
rec_push(P.push, cb, VK_PIPELINE_BIND_POINT_COMPUTE, P.layout[p], 0, (uint32_t)PIPES[p].n, wr); // through the recorder
|
||||
vkCmdPushConstants(cb, P.layout[p], VK_SHADER_STAGE_COMPUTE_BIT, 0, (uint32_t)pcn, pc);
|
||||
vkCmdDispatch(cb, (w + 7) / 8, (h + 7) / 8, 1);
|
||||
VkMemoryBarrier2 mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, .srcStageMask = VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT,
|
||||
.srcAccessMask = VK_ACCESS_2_SHADER_WRITE_BIT, .dstStageMask = VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT,
|
||||
.dstAccessMask = VK_ACCESS_2_SHADER_READ_BIT};
|
||||
VkDependencyInfo d = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .memoryBarrierCount = 1, .pMemoryBarriers = &mb};
|
||||
vkCmdPipelineBarrier2(cb, &d);
|
||||
}
|
||||
|
||||
int vk_aa_record(VkCommandBuffer cb, int mode, VkImageView src, VkImageView dst, uint32_t w, uint32_t h) {
|
||||
if (mode == VK_AA_GAME || !w || !h || !pipes_init()) return 0;
|
||||
if (mode == VK_AA_SMAA && (!tables_make() || !work_images(cb, w, h))) {
|
||||
static int told;
|
||||
if (!told++) rt_log("aa: SMAA's images could not be made (%ux%u): smaa runs as off", w, h);
|
||||
mode = VK_AA_OFF;
|
||||
}
|
||||
VkMemoryBarrier2 mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, .srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
|
||||
.srcAccessMask = VK_ACCESS_2_MEMORY_WRITE_BIT, .dstStageMask = VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT,
|
||||
.dstAccessMask = VK_ACCESS_2_SHADER_READ_BIT | VK_ACCESS_2_SHADER_WRITE_BIT};
|
||||
VkDependencyInfo dep = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .memoryBarrierCount = 1, .pMemoryBarriers = &mb};
|
||||
vkCmdPipelineBarrier2(cb, &dep);
|
||||
if (mode != VK_AA_SMAA) {
|
||||
struct { float inv[2]; int32_t size[2]; int32_t mode; } pc = {{1.0f / (float)w, 1.0f / (float)h},
|
||||
{(int32_t)w, (int32_t)h}, mode == VK_AA_FXAA};
|
||||
const VkImageView v[2] = {src, dst};
|
||||
const VkSampler s[2] = {P.linear, VK_NULL_HANDLE};
|
||||
pass(cb, P_POST, v, s, &pc, sizeof pc, w, h);
|
||||
return 1;
|
||||
}
|
||||
const float metrics[4] = {1.0f / (float)w, 1.0f / (float)h, (float)w, (float)h};
|
||||
const VkSampler lin[4] = {P.linear, P.linear, P.linear, P.linear};
|
||||
pass(cb, P_LUMA, (const VkImageView[]){src, P.luma.view}, lin, metrics, sizeof metrics, w, h);
|
||||
pass(cb, P_EDGE, (const VkImageView[]){P.luma.view, P.edges.view}, lin, metrics, sizeof metrics, w, h);
|
||||
pass(cb, P_WEIGHT, (const VkImageView[]){P.edges.view, P.area.view, P.search.view, P.weights.view},
|
||||
(const VkSampler[]){P.linear, P.linear, P.nearest, VK_NULL_HANDLE}, metrics, sizeof metrics, w, h);
|
||||
pass(cb, P_BLEND, (const VkImageView[]){src, P.weights.view, dst}, lin, metrics, sizeof metrics, w, h);
|
||||
return 1;
|
||||
}
|
||||
@@ -280,6 +280,19 @@ int vk_exec_held_ready(void); // ... and one of them can go on now (vk_b
|
||||
// sample DLSS's output vk_dlaa_swap_view in its place, so the game's scaling draw scales DLSS's picture.
|
||||
extern const VkTexImage *vk_dlaa_swap_img;
|
||||
extern VkImageView vk_dlaa_swap_view;
|
||||
// vk_aa.c, the anti-aliasing setting in place of the game's SMAA (vk_exec_draw.c aa_dispatch). vk_aa_mode: this frame's
|
||||
// replacement, VK_AA_GAME when the game's SMAA runs as it is. vk_aa_role: which of the game's SMAA passes a compute
|
||||
// program is, by the hash of its guest code. vk_aa_skip_ok: the edge and weight passes may be left out (the last
|
||||
// composition was replaced). vk_aa_record: the replacement of the composition into cb, from src (the composite, sampled)
|
||||
// into dst (the scene target's R32_UINT storage view) of w x h; 0 when it could not. vk_aa_composed: what became of the
|
||||
// composition this frame.
|
||||
enum { VK_AA_GAME, VK_AA_OFF, VK_AA_FXAA, VK_AA_SMAA };
|
||||
enum { VK_AA_ROLE_NONE, VK_AA_ROLE_EDGES, VK_AA_ROLE_WEIGHTS, VK_AA_ROLE_COMPOSE };
|
||||
int vk_aa_mode(void);
|
||||
int vk_aa_role(uint64_t guest_hash);
|
||||
int vk_aa_skip_ok(void);
|
||||
int vk_aa_record(VkCommandBuffer cb, int mode, VkImageView src, VkImageView dst, uint32_t w, uint32_t h);
|
||||
void vk_aa_composed(int ok);
|
||||
// vk_contact.c: a pass about to begin (its targets resolved, no pass open), and a dispatch or copy about to be recorded
|
||||
void vk_contact_pass_begin(VkExec *ex);
|
||||
void vk_contact_flush(VkExec *ex);
|
||||
|
||||
@@ -5695,6 +5695,110 @@ static int fallback_9e5_scene(VkFormat src, VkFormat dst) {
|
||||
return src == VK_FORMAT_E5B9G9R9_UFLOAT_PACK32 && dst == VK_FORMAT_B10G11R11_UFLOAT_PACK32;
|
||||
}
|
||||
|
||||
// FNV-1a of a program's compute code as the game hands it over, kept per program: what vk_aa.c knows the game's SMAA
|
||||
// passes by, the same with every translator and setting. 0 without compute code.
|
||||
static uint64_t cs_guest_hash(const prog_t *p) {
|
||||
static struct { uint64_t serial, hash; } memo[1024];
|
||||
typeof(memo[0]) *m = p->serial ? &memo[p->serial * 0x9E3779B97F4A7C15ull >> 54] : NULL;
|
||||
if (m && m->serial == p->serial) return m->hash;
|
||||
const uint8_t *ctl, *code;
|
||||
uint32_t len;
|
||||
uint64_t h = 0;
|
||||
if (find_stage(p, NVN_STAGE_CS, &ctl, &code, &len) && code && len) {
|
||||
h = 0xcbf29ce484222325ull;
|
||||
for (uint32_t i = 0; i < len; i++) h = (h ^ code[i]) * 0x100000001b3ull;
|
||||
}
|
||||
if (m) m->serial = p->serial, m->hash = h;
|
||||
return h;
|
||||
}
|
||||
|
||||
// The anti-aliasing setting (vk_aa.c): while it asks for something other than the game's SMAA, the game's edge and
|
||||
// weight passes are left out and its composition is replaced by vk_aa_record, from the same composite (the sampled
|
||||
// R11G11B10F image of the output's size) into the same scene target, through the integer storage view the game's own
|
||||
// pass writes it with. 1: the dispatch is handled (left out or replaced). NX2NAVI_AA_LOG=1 logs each compute program's
|
||||
// guest code hash once, with its dispatch size and output, to find the passes in another game version.
|
||||
static int aa_dispatch(VkExec *ex, int x, int y, int z) {
|
||||
int mode = vk_aa_mode();
|
||||
if (mode == VK_AA_GAME || !ex->prog) return 0;
|
||||
uint64_t gh = cs_guest_hash(ex->prog);
|
||||
static int log_on = -1;
|
||||
if (log_on < 0) log_on = rt_knob("AA_LOG", "0");
|
||||
if (log_on) {
|
||||
static uint64_t seen[512];
|
||||
static int nseen;
|
||||
int known = 0;
|
||||
for (int i = 0; i < nseen; i++) known |= seen[i] == gh;
|
||||
if (!known && nseen < 512) {
|
||||
seen[nseen++] = gh;
|
||||
const VkTexImage *o = NULL;
|
||||
vk_texture_lock();
|
||||
for (int i = 0; i < 32 && !o; i++) {
|
||||
uint64_t h = ex->img[NVN_STAGE_CS][i];
|
||||
view_t v; int hv = 0;
|
||||
o = h ? vk_texture_by_id_locked(NVN_HANDLE_TEX(h), &v, &hv) : NULL;
|
||||
}
|
||||
rt_log("aa: compute prog %llu guest %016llx dispatch %dx%dx%d output %ux%u fmt %d",
|
||||
(unsigned long long)ex->prog->serial, (unsigned long long)gh, x, y, z, o ? o->width : 0,
|
||||
o ? o->height : 0, o ? (int)o->format : 0);
|
||||
vk_texture_unlock();
|
||||
}
|
||||
}
|
||||
int role = vk_aa_role(gh);
|
||||
if (role == VK_AA_ROLE_NONE) return 0;
|
||||
if (role != VK_AA_ROLE_COMPOSE) return vk_aa_skip_ok();
|
||||
const VkTexImage *dst = NULL, *src = NULL;
|
||||
VkImageView dv = VK_NULL_HANDLE, sv = VK_NULL_HANDLE;
|
||||
vk_texture_lock();
|
||||
// the scene target, written as raw words: the R11G11B10F image, or at times the RGB9E5 one the same memory also
|
||||
// resolves as (raw_9e5_twin), which takes the same words
|
||||
for (int i = 0; i < 32 && !dst; i++) {
|
||||
uint64_t h = ex->img[NVN_STAGE_CS][i];
|
||||
view_t v; int hv = 0;
|
||||
const VkTexImage *t = h ? vk_texture_by_id_locked(NVN_HANDLE_TEX(h), &v, &hv) : NULL;
|
||||
if (!t || (t->format != VK_FORMAT_B10G11R11_UFLOAT_PACK32 && t->format != VK_FORMAT_E5B9G9R9_UFLOAT_PACK32) ||
|
||||
t->type != VK_IMAGE_TYPE_2D || t->samples != 1)
|
||||
continue;
|
||||
dst = t;
|
||||
if (storage_view_format(t, &v, hv) == VK_FORMAT_R32_UINT) dv = get_view(ex, t, &v, hv, 3, 1); // 3: Texture2D
|
||||
}
|
||||
for (int i = 0; dst && i < 128 && !src; i++) { // the composite: R11G11B10F of the same size, another image
|
||||
uint64_t h = ex->tex[NVN_STAGE_CS][i];
|
||||
view_t v; int hv = 0;
|
||||
const VkTexImage *t = h ? vk_texture_by_id_locked(NVN_HANDLE_TEX(h), &v, &hv) : NULL;
|
||||
if (!t || t->image == dst->image || t->format != VK_FORMAT_B10G11R11_UFLOAT_PACK32 || t->width != dst->width ||
|
||||
t->height != dst->height || t->type != VK_IMAGE_TYPE_2D || t->samples != 1)
|
||||
continue;
|
||||
src = t;
|
||||
view_t nv;
|
||||
memset(&nv, 0, sizeof nv);
|
||||
sv = get_view(ex, t, &nv, 0, 3, 0);
|
||||
}
|
||||
uint32_t w = dst ? dst->width : 0, hh = dst ? dst->height : 0;
|
||||
if (dv && sv) vk_texture_storage_bound_locked(dst); // the GPU writes it below, as the game's pass would have
|
||||
// Left to the game, once logged. Expected while an area loads: the target is then the RGB9E5 image at the same
|
||||
// memory, made without storage use, which the game's own pass cannot write either.
|
||||
static int told;
|
||||
if ((!dv || !sv) && !told++)
|
||||
rt_log("aa: a composition of the game's SMAA left to the game: target %s (%ux%u fmt %d, storage %d), composite %s",
|
||||
dst ? (dv ? "ok" : "without an integer storage view") : "not found", w, hh, dst ? (int)dst->format : 0,
|
||||
dst ? !!(dst->usage & VK_IMAGE_USAGE_STORAGE_BIT) : 0, src ? (sv ? "ok" : "without a view") : "not found");
|
||||
vk_texture_unlock();
|
||||
// recorded without the texture lock: SMAA's first frame waits for its lookup textures' upload. The views stay valid
|
||||
// meanwhile: images and views go only through the deferred release.
|
||||
int ok = 0;
|
||||
if (dv && sv) {
|
||||
vk_exec_barrier_dispatch(ex); // ends the pass; the composite's writes are visible
|
||||
ok = vk_aa_record(ex->cb, mode, sv, dv, w, hh);
|
||||
if (ok) {
|
||||
B.pipe[1] = VK_NULL_HANDLE; // the replay must bind its compute pipeline again
|
||||
ex->recorded++;
|
||||
vk_exec_barrier(ex);
|
||||
}
|
||||
}
|
||||
vk_aa_composed(ok);
|
||||
return ok;
|
||||
}
|
||||
|
||||
static void untranslated_dispatch_fallback(VkExec *ex) {
|
||||
static int on = -1;
|
||||
if (on < 0) on = rt_knob("CS_FALLBACK", "1");
|
||||
@@ -5877,6 +5981,7 @@ static void op_dispatch(VkExec *ex, const p_dispatch *d) {
|
||||
vk_contact_flush(ex);
|
||||
cb_res(ex);
|
||||
dummies_init();
|
||||
if (aa_dispatch(ex, d->x, d->y, d->z)) return; // the game's SMAA left out or replaced (vk_aa.c)
|
||||
// A tensor-core program runs the stand-in while NX2NAVI_NATIVE_TENSOR is off, so ask that before translating it.
|
||||
// Translating it anyway recorded four AI-upscaler compute shaders of 0.87-4.4M SPIR-V words that this game never
|
||||
// dispatches natively, and building them cost gigabytes of memory at boot (NX2NAVI_LIB_PREBUILD_BIG, bg_main).
|
||||
@@ -5932,6 +6037,7 @@ static void op_dispatch_ind(VkExec *ex, const p_dispatch_ind *d) {
|
||||
return warn_once(W_INDIRECT, "indirect dispatch arguments at %#llx are not backed or not aligned: skipped", (unsigned long long)d->ind);
|
||||
cb_res(ex);
|
||||
dummies_init();
|
||||
if (aa_dispatch(ex, 0, 0, 0)) return; // as in op_dispatch
|
||||
if (!native_tensor(ex)) { untranslated_dispatch_fallback(ex); return; } // as in op_dispatch: never translate it
|
||||
int present;
|
||||
const GfxShader *cs = shader_for(ex, NVN_STAGE_CS, &present, NULL, 0);
|
||||
|
||||
@@ -9,11 +9,11 @@ missing and the stale ones before it compiles the runtime. Three kinds of source
|
||||
runtime/shaders/<name>.spvasm SPIR-V assembly, assembled with spirv-as and checked with spirv-val
|
||||
tools/make_dlaa_mask.py, make_mvobj_fs.py shaders assembled in Python (ASSEMBLED); they need no other tool
|
||||
|
||||
One more kind of header is no shader: a file the runtime embeds (EMBEDDED: the menu's font) as a list of its bytes,
|
||||
for a compiler without #embed (runtime/ui/menu.cpp includes it there). It needs no tool.
|
||||
One more kind of header is no shader: a file the runtime embeds (EMBEDDED: the menu's font, the SMAA lookup textures) as
|
||||
a list of its bytes, for a compiler without #embed (runtime/ui/menu.cpp, runtime/vk_aa.c include them). It needs no tool.
|
||||
|
||||
A compiled or assembled header records the SHA-256 of its source and is stale when that no longer matches; a Python
|
||||
one is stale when its text is not what its script gives. glslc, spirv-as and spirv-val come from MSYS2 UCRT64 on
|
||||
A compiled or assembled header records the SHA-256 of its source (with the files a GLSL source includes from its folder)
|
||||
and is stale when that no longer matches; a Python one is stale when its text is not what its script gives. glslc, spirv-as and spirv-val come from MSYS2 UCRT64 on
|
||||
Windows (packages mingw-w64-ucrt-x86_64-shaderc and mingw-w64-ucrt-x86_64-spirv-tools) and from PATH elsewhere
|
||||
(shaderc, spirv-tools). A folder that carries its headers ready-made (the build kit, tools/make_kit.py) needs none of
|
||||
them: nothing there is stale.
|
||||
@@ -42,7 +42,8 @@ ASM_TARGET = 'vulkan1.3'
|
||||
# header name -> the script beside this one that assembles it; the script's HEADER is the header's text
|
||||
ASSEMBLED = {'dlaa_mask': 'make_dlaa_mask.py', 'mvobj_fs': 'make_mvobj_fs.py'}
|
||||
# a file below runtime/ the runtime embeds -> the header of its bytes, below runtime/ too (git ignores it)
|
||||
EMBEDDED = {'ui/fonts/Roboto-Medium.ttf': 'ui/roboto_ttf.h'}
|
||||
EMBEDDED = {'ui/fonts/Roboto-Medium.ttf': 'ui/roboto_ttf.h',
|
||||
'third_party/smaa/AreaTex.bin': 'smaa_area_tex.h', 'third_party/smaa/SearchTex.bin': 'smaa_search_tex.h'}
|
||||
|
||||
|
||||
def embedded(src):
|
||||
@@ -50,9 +51,26 @@ def embedded(src):
|
||||
return src.suffix not in STAGES and src.suffix not in (ASM, '.py')
|
||||
|
||||
|
||||
def included(src, seen=None):
|
||||
"""The files a GLSL source includes from its own folder (#include "name"), and theirs, in order of first use."""
|
||||
seen = [] if seen is None else seen
|
||||
for line in src.read_bytes().decode('latin-1').splitlines(): # the SMAA source is not UTF-8
|
||||
words = line.strip().split('"')
|
||||
if line.strip().startswith('#include') and len(words) >= 2:
|
||||
inc = src.parent / words[1]
|
||||
if inc.is_file() and inc not in seen:
|
||||
seen.append(inc)
|
||||
included(inc, seen)
|
||||
return seen
|
||||
|
||||
|
||||
def source_hash(src):
|
||||
data = src.read_bytes()
|
||||
return hashlib.sha256(data if embedded(src) else data.replace(b'\r\n', b'\n')).hexdigest() # text: the same on a CRLF checkout
|
||||
if embedded(src):
|
||||
return hashlib.sha256(data).hexdigest()
|
||||
if src.suffix in STAGES: # a header goes stale with the files its source includes too
|
||||
data += b''.join(inc.read_bytes() for inc in included(src))
|
||||
return hashlib.sha256(data.replace(b'\r\n', b'\n')).hexdigest() # text: the same on a CRLF checkout
|
||||
|
||||
|
||||
def bytes_header(src, runtime):
|
||||
|
||||
Reference in New Issue
Block a user