diff --git a/.gitignore b/.gitignore index b5647747..0754b129 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md index 6148c482..86d2ab95 100644 --- a/THIRD_PARTY_NOTICES.md +++ b/THIRD_PARTY_NOTICES.md @@ -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 diff --git a/docs/architecture.md b/docs/architecture.md index 6e9a9f40..f0e09b1f 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -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 diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 7a986be6..6026c2fe 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -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 diff --git a/runtime/cfg.c b/runtime/cfg.c index 68933004..cf26c264 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -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}, diff --git a/runtime/shaders/aa_common.glsl b/runtime/shaders/aa_common.glsl new file mode 100644 index 00000000..f176ff9b --- /dev/null +++ b/runtime/shaders/aa_common.glsl @@ -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))); } diff --git a/runtime/shaders/aa_post.comp b/runtime/shaders/aa_post.comp new file mode 100644 index 00000000..c5973da5 --- /dev/null +++ b/runtime/shaders/aa_post.comp @@ -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)); +} diff --git a/runtime/shaders/smaa.glsl b/runtime/shaders/smaa.glsl new file mode 100644 index 00000000..075cac9a --- /dev/null +++ b/runtime/shaders/smaa.glsl @@ -0,0 +1,1361 @@ +/** + * 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. + */ + + +/** + * _______ ___ ___ ___ ___ + * / || \/ | / \ / \ + * | (---- | \ / | / ^ \ / ^ \ + * \ \ | |\/| | / /_\ \ / /_\ \ + * ----) | | | | | / _____ \ / _____ \ + * |_______/ |__| |__| /__/ \__\ /__/ \__\ + * + * E N H A N C E D + * S U B P I X E L M O R P H O L O G I C A L A N T I A L I A S I N G + * + * http://www.iryoku.com/smaa/ + * + * Hi, welcome aboard! + * + * Here you'll find instructions to get the shader up and running as fast as + * possible. + * + * IMPORTANTE NOTICE: when updating, remember to update both this file and the + * precomputed textures! They may change from version to version. + * + * The shader has three passes, chained together as follows: + * + * |input|------------------· + * v | + * [ SMAA*EdgeDetection ] | + * v | + * |edgesTex| | + * v | + * [ SMAABlendingWeightCalculation ] | + * v | + * |blendTex| | + * v | + * [ SMAANeighborhoodBlending ] <------· + * v + * |output| + * + * Note that each [pass] has its own vertex and pixel shader. Remember to use + * oversized triangles instead of quads to avoid overshading along the + * diagonal. + * + * You've three edge detection methods to choose from: luma, color or depth. + * They represent different quality/performance and anti-aliasing/sharpness + * tradeoffs, so our recommendation is for you to choose the one that best + * suits your particular scenario: + * + * - Depth edge detection is usually the fastest but it may miss some edges. + * + * - Luma edge detection is usually more expensive than depth edge detection, + * but catches visible edges that depth edge detection can miss. + * + * - Color edge detection is usually the most expensive one but catches + * chroma-only edges. + * + * For quickstarters: just use luma edge detection. + * + * The general advice is to not rush the integration process and ensure each + * step is done correctly (don't try to integrate SMAA T2x with predicated edge + * detection from the start!). Ok then, let's go! + * + * 1. The first step is to create two RGBA temporal render targets for holding + * |edgesTex| and |blendTex|. + * + * In DX10 or DX11, you can use a RG render target for the edges texture. + * In the case of NVIDIA GPUs, using RG render targets seems to actually be + * slower. + * + * On the Xbox 360, you can use the same render target for resolving both + * |edgesTex| and |blendTex|, as they aren't needed simultaneously. + * + * 2. Both temporal render targets |edgesTex| and |blendTex| must be cleared + * each frame. Do not forget to clear the alpha channel! + * + * 3. The next step is loading the two supporting precalculated textures, + * 'areaTex' and 'searchTex'. You'll find them in the 'Textures' folder as + * C++ headers, and also as regular DDS files. They'll be needed for the + * 'SMAABlendingWeightCalculation' pass. + * + * If you use the C++ headers, be sure to load them in the format specified + * inside of them. + * + * You can also compress 'areaTex' and 'searchTex' using BC5 and BC4 + * respectively, if you have that option in your content processor pipeline. + * When compressing then, you get a non-perceptible quality decrease, and a + * marginal performance increase. + * + * 4. All samplers must be set to linear filtering and clamp. + * + * After you get the technique working, remember that 64-bit inputs have + * half-rate linear filtering on GCN. + * + * If SMAA is applied to 64-bit color buffers, switching to point filtering + * when accesing them will increase the performance. Search for + * 'SMAASamplePoint' to see which textures may benefit from point + * filtering, and where (which is basically the color input in the edge + * detection and resolve passes). + * + * 5. All texture reads and buffer writes must be non-sRGB, with the exception + * of the input read and the output write in + * 'SMAANeighborhoodBlending' (and only in this pass!). If sRGB reads in + * this last pass are not possible, the technique will work anyway, but + * will perform antialiasing in gamma space. + * + * IMPORTANT: for best results the input read for the color/luma edge + * detection should *NOT* be sRGB. + * + * 6. Before including SMAA.h you'll have to setup the render target metrics, + * the target and any optional configuration defines. Optionally you can + * use a preset. + * + * You have the following targets available: + * SMAA_HLSL_3 + * SMAA_HLSL_4 + * SMAA_HLSL_4_1 + * SMAA_GLSL_3 * + * SMAA_GLSL_4 * + * + * * (See SMAA_INCLUDE_VS and SMAA_INCLUDE_PS below). + * + * And four presets: + * SMAA_PRESET_LOW (%60 of the quality) + * SMAA_PRESET_MEDIUM (%80 of the quality) + * SMAA_PRESET_HIGH (%95 of the quality) + * SMAA_PRESET_ULTRA (%99 of the quality) + * + * For example: + * #define SMAA_RT_METRICS float4(1.0 / 1280.0, 1.0 / 720.0, 1280.0, 720.0) + * #define SMAA_HLSL_4 + * #define SMAA_PRESET_HIGH + * #include "SMAA.h" + * + * Note that SMAA_RT_METRICS doesn't need to be a macro, it can be a + * uniform variable. The code is designed to minimize the impact of not + * using a constant value, but it is still better to hardcode it. + * + * Depending on how you encoded 'areaTex' and 'searchTex', you may have to + * add (and customize) the following defines before including SMAA.h: + * #define SMAA_AREATEX_SELECT(sample) sample.rg + * #define SMAA_SEARCHTEX_SELECT(sample) sample.r + * + * If your engine is already using porting macros, you can define + * SMAA_CUSTOM_SL, and define the porting functions by yourself. + * + * 7. Then, you'll have to setup the passes as indicated in the scheme above. + * You can take a look into SMAA.fx, to see how we did it for our demo. + * Checkout the function wrappers, you may want to copy-paste them! + * + * 8. It's recommended to validate the produced |edgesTex| and |blendTex|. + * You can use a screenshot from your engine to compare the |edgesTex| + * and |blendTex| produced inside of the engine with the results obtained + * with the reference demo. + * + * 9. After you get the last pass to work, it's time to optimize. You'll have + * to initialize a stencil buffer in the first pass (discard is already in + * the code), then mask execution by using it the second pass. The last + * pass should be executed in all pixels. + * + * + * After this point you can choose to enable predicated thresholding, + * temporal supersampling and motion blur integration: + * + * a) If you want to use predicated thresholding, take a look into + * SMAA_PREDICATION; you'll need to pass an extra texture in the edge + * detection pass. + * + * b) If you want to enable temporal supersampling (SMAA T2x): + * + * 1. The first step is to render using subpixel jitters. I won't go into + * detail, but it's as simple as moving each vertex position in the + * vertex shader, you can check how we do it in our DX10 demo. + * + * 2. Then, you must setup the temporal resolve. You may want to take a look + * into SMAAResolve for resolving 2x modes. After you get it working, you'll + * probably see ghosting everywhere. But fear not, you can enable the + * CryENGINE temporal reprojection by setting the SMAA_REPROJECTION macro. + * Check out SMAA_DECODE_VELOCITY if your velocity buffer is encoded. + * + * 3. The next step is to apply SMAA to each subpixel jittered frame, just as + * done for 1x. + * + * 4. At this point you should already have something usable, but for best + * results the proper area textures must be set depending on current jitter. + * For this, the parameter 'subsampleIndices' of + * 'SMAABlendingWeightCalculationPS' must be set as follows, for our T2x + * mode: + * + * @SUBSAMPLE_INDICES + * + * | S# | Camera Jitter | subsampleIndices | + * +----+------------------+---------------------+ + * | 0 | ( 0.25, -0.25) | float4(1, 1, 1, 0) | + * | 1 | (-0.25, 0.25) | float4(2, 2, 2, 0) | + * + * These jitter positions assume a bottom-to-top y axis. S# stands for the + * sample number. + * + * More information about temporal supersampling here: + * http://iryoku.com/aacourse/downloads/13-Anti-Aliasing-Methods-in-CryENGINE-3.pdf + * + * c) If you want to enable spatial multisampling (SMAA S2x): + * + * 1. The scene must be rendered using MSAA 2x. The MSAA 2x buffer must be + * created with: + * - DX10: see below (*) + * - DX10.1: D3D10_STANDARD_MULTISAMPLE_PATTERN or + * - DX11: D3D11_STANDARD_MULTISAMPLE_PATTERN + * + * This allows to ensure that the subsample order matches the table in + * @SUBSAMPLE_INDICES. + * + * (*) In the case of DX10, we refer the reader to: + * - SMAA::detectMSAAOrder and + * - SMAA::msaaReorder + * + * These functions allow to match the standard multisample patterns by + * detecting the subsample order for a specific GPU, and reordering + * them appropriately. + * + * 2. A shader must be run to output each subsample into a separate buffer + * (DX10 is required). You can use SMAASeparate for this purpose, or just do + * it in an existing pass (for example, in the tone mapping pass, which has + * the advantage of feeding tone mapped subsamples to SMAA, which will yield + * better results). + * + * 3. The full SMAA 1x pipeline must be run for each separated buffer, storing + * the results in the final buffer. The second run should alpha blend with + * the existing final buffer using a blending factor of 0.5. + * 'subsampleIndices' must be adjusted as in the SMAA T2x case (see point + * b). + * + * d) If you want to enable temporal supersampling on top of SMAA S2x + * (which actually is SMAA 4x): + * + * 1. SMAA 4x consists on temporally jittering SMAA S2x, so the first step is + * to calculate SMAA S2x for current frame. In this case, 'subsampleIndices' + * must be set as follows: + * + * | F# | S# | Camera Jitter | Net Jitter | subsampleIndices | + * +----+----+--------------------+-------------------+----------------------+ + * | 0 | 0 | ( 0.125, 0.125) | ( 0.375, -0.125) | float4(5, 3, 1, 3) | + * | 0 | 1 | ( 0.125, 0.125) | (-0.125, 0.375) | float4(4, 6, 2, 3) | + * +----+----+--------------------+-------------------+----------------------+ + * | 1 | 2 | (-0.125, -0.125) | ( 0.125, -0.375) | float4(3, 5, 1, 4) | + * | 1 | 3 | (-0.125, -0.125) | (-0.375, 0.125) | float4(6, 4, 2, 4) | + * + * These jitter positions assume a bottom-to-top y axis. F# stands for the + * frame number. S# stands for the sample number. + * + * 2. After calculating SMAA S2x for current frame (with the new subsample + * indices), previous frame must be reprojected as in SMAA T2x mode (see + * point b). + * + * e) If motion blur is used, you may want to do the edge detection pass + * together with motion blur. This has two advantages: + * + * 1. Pixels under heavy motion can be omitted from the edge detection process. + * For these pixels we can just store "no edge", as motion blur will take + * care of them. + * 2. The center pixel tap is reused. + * + * Note that in this case depth testing should be used instead of stenciling, + * as we have to write all the pixels in the motion blur pass. + * + * That's it! + */ + +//----------------------------------------------------------------------------- +// SMAA Presets + +/** + * Note that if you use one of these presets, the following configuration + * macros will be ignored if set in the "Configurable Defines" section. + */ + +#if defined(SMAA_PRESET_LOW) +#define SMAA_THRESHOLD 0.15 +#define SMAA_MAX_SEARCH_STEPS 4 +#define SMAA_DISABLE_DIAG_DETECTION +#define SMAA_DISABLE_CORNER_DETECTION +#elif defined(SMAA_PRESET_MEDIUM) +#define SMAA_THRESHOLD 0.1 +#define SMAA_MAX_SEARCH_STEPS 8 +#define SMAA_DISABLE_DIAG_DETECTION +#define SMAA_DISABLE_CORNER_DETECTION +#elif defined(SMAA_PRESET_HIGH) +#define SMAA_THRESHOLD 0.1 +#define SMAA_MAX_SEARCH_STEPS 16 +#define SMAA_MAX_SEARCH_STEPS_DIAG 8 +#define SMAA_CORNER_ROUNDING 25 +#elif defined(SMAA_PRESET_ULTRA) +#define SMAA_THRESHOLD 0.05 +#define SMAA_MAX_SEARCH_STEPS 32 +#define SMAA_MAX_SEARCH_STEPS_DIAG 16 +#define SMAA_CORNER_ROUNDING 25 +#endif + +//----------------------------------------------------------------------------- +// Configurable Defines + +/** + * SMAA_THRESHOLD specifies the threshold or sensitivity to edges. + * Lowering this value you will be able to detect more edges at the expense of + * performance. + * + * Range: [0, 0.5] + * 0.1 is a reasonable value, and allows to catch most visible edges. + * 0.05 is a rather overkill value, that allows to catch 'em all. + * + * If temporal supersampling is used, 0.2 could be a reasonable value, as low + * contrast edges are properly filtered by just 2x. + */ +#ifndef SMAA_THRESHOLD +#define SMAA_THRESHOLD 0.1 +#endif + +/** + * SMAA_DEPTH_THRESHOLD specifies the threshold for depth edge detection. + * + * Range: depends on the depth range of the scene. + */ +#ifndef SMAA_DEPTH_THRESHOLD +#define SMAA_DEPTH_THRESHOLD (0.1 * SMAA_THRESHOLD) +#endif + +/** + * SMAA_MAX_SEARCH_STEPS specifies the maximum steps performed in the + * horizontal/vertical pattern searches, at each side of the pixel. + * + * In number of pixels, it's actually the double. So the maximum line length + * perfectly handled by, for example 16, is 64 (by perfectly, we meant that + * longer lines won't look as good, but still antialiased). + * + * Range: [0, 112] + */ +#ifndef SMAA_MAX_SEARCH_STEPS +#define SMAA_MAX_SEARCH_STEPS 16 +#endif + +/** + * SMAA_MAX_SEARCH_STEPS_DIAG specifies the maximum steps performed in the + * diagonal pattern searches, at each side of the pixel. In this case we jump + * one pixel at time, instead of two. + * + * Range: [0, 20] + * + * On high-end machines it is cheap (between a 0.8x and 0.9x slower for 16 + * steps), but it can have a significant impact on older machines. + * + * Define SMAA_DISABLE_DIAG_DETECTION to disable diagonal processing. + */ +#ifndef SMAA_MAX_SEARCH_STEPS_DIAG +#define SMAA_MAX_SEARCH_STEPS_DIAG 8 +#endif + +/** + * SMAA_CORNER_ROUNDING specifies how much sharp corners will be rounded. + * + * Range: [0, 100] + * + * Define SMAA_DISABLE_CORNER_DETECTION to disable corner processing. + */ +#ifndef SMAA_CORNER_ROUNDING +#define SMAA_CORNER_ROUNDING 25 +#endif + +/** + * If there is an neighbor edge that has SMAA_LOCAL_CONTRAST_FACTOR times + * bigger contrast than current edge, current edge will be discarded. + * + * This allows to eliminate spurious crossing edges, and is based on the fact + * that, if there is too much contrast in a direction, that will hide + * perceptually contrast in the other neighbors. + */ +#ifndef SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR +#define SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR 2.0 +#endif + +/** + * Predicated thresholding allows to better preserve texture details and to + * improve performance, by decreasing the number of detected edges using an + * additional buffer like the light accumulation buffer, object ids or even the + * depth buffer (the depth buffer usage may be limited to indoor or short range + * scenes). + * + * It locally decreases the luma or color threshold if an edge is found in an + * additional buffer (so the global threshold can be higher). + * + * This method was developed by Playstation EDGE MLAA team, and used in + * Killzone 3, by using the light accumulation buffer. More information here: + * http://iryoku.com/aacourse/downloads/06-MLAA-on-PS3.pptx + */ +#ifndef SMAA_PREDICATION +#define SMAA_PREDICATION 0 +#endif + +/** + * Threshold to be used in the additional predication buffer. + * + * Range: depends on the input, so you'll have to find the magic number that + * works for you. + */ +#ifndef SMAA_PREDICATION_THRESHOLD +#define SMAA_PREDICATION_THRESHOLD 0.01 +#endif + +/** + * How much to scale the global threshold used for luma or color edge + * detection when using predication. + * + * Range: [1, 5] + */ +#ifndef SMAA_PREDICATION_SCALE +#define SMAA_PREDICATION_SCALE 2.0 +#endif + +/** + * How much to locally decrease the threshold. + * + * Range: [0, 1] + */ +#ifndef SMAA_PREDICATION_STRENGTH +#define SMAA_PREDICATION_STRENGTH 0.4 +#endif + +/** + * Temporal reprojection allows to remove ghosting artifacts when using + * temporal supersampling. We use the CryEngine 3 method which also introduces + * velocity weighting. This feature is of extreme importance for totally + * removing ghosting. More information here: + * http://iryoku.com/aacourse/downloads/13-Anti-Aliasing-Methods-in-CryENGINE-3.pdf + * + * Note that you'll need to setup a velocity buffer for enabling reprojection. + * For static geometry, saving the previous depth buffer is a viable + * alternative. + */ +#ifndef SMAA_REPROJECTION +#define SMAA_REPROJECTION 0 +#endif + +/** + * SMAA_REPROJECTION_WEIGHT_SCALE controls the velocity weighting. It allows to + * remove ghosting trails behind the moving object, which are not removed by + * just using reprojection. Using low values will exhibit ghosting, while using + * high values will disable temporal supersampling under motion. + * + * Behind the scenes, velocity weighting removes temporal supersampling when + * the velocity of the subsamples differs (meaning they are different objects). + * + * Range: [0, 80] + */ +#ifndef SMAA_REPROJECTION_WEIGHT_SCALE +#define SMAA_REPROJECTION_WEIGHT_SCALE 30.0 +#endif + +/** + * On some compilers, discard cannot be used in vertex shaders. Thus, they need + * to be compiled separately. + */ +#ifndef SMAA_INCLUDE_VS +#define SMAA_INCLUDE_VS 1 +#endif +#ifndef SMAA_INCLUDE_PS +#define SMAA_INCLUDE_PS 1 +#endif + +//----------------------------------------------------------------------------- +// Texture Access Defines + +#ifndef SMAA_AREATEX_SELECT +#if defined(SMAA_HLSL_3) +#define SMAA_AREATEX_SELECT(sample) sample.ra +#else +#define SMAA_AREATEX_SELECT(sample) sample.rg +#endif +#endif + +#ifndef SMAA_SEARCHTEX_SELECT +#define SMAA_SEARCHTEX_SELECT(sample) sample.r +#endif + +#ifndef SMAA_DECODE_VELOCITY +#define SMAA_DECODE_VELOCITY(sample) sample.rg +#endif + +//----------------------------------------------------------------------------- +// Non-Configurable Defines + +#define SMAA_AREATEX_MAX_DISTANCE 16 +#define SMAA_AREATEX_MAX_DISTANCE_DIAG 20 +#define SMAA_AREATEX_PIXEL_SIZE (1.0 / float2(160.0, 560.0)) +#define SMAA_AREATEX_SUBTEX_SIZE (1.0 / 7.0) +#define SMAA_SEARCHTEX_SIZE float2(66.0, 33.0) +#define SMAA_SEARCHTEX_PACKED_SIZE float2(64.0, 16.0) +#define SMAA_CORNER_ROUNDING_NORM (float(SMAA_CORNER_ROUNDING) / 100.0) + +//----------------------------------------------------------------------------- +// Porting Functions + +#if defined(SMAA_HLSL_3) +#define SMAATexture2D(tex) sampler2D tex +#define SMAATexturePass2D(tex) tex +#define SMAASampleLevelZero(tex, coord) tex2Dlod(tex, float4(coord, 0.0, 0.0)) +#define SMAASampleLevelZeroPoint(tex, coord) tex2Dlod(tex, float4(coord, 0.0, 0.0)) +#define SMAASampleLevelZeroOffset(tex, coord, offset) tex2Dlod(tex, float4(coord + offset * SMAA_RT_METRICS.xy, 0.0, 0.0)) +#define SMAASample(tex, coord) tex2D(tex, coord) +#define SMAASamplePoint(tex, coord) tex2D(tex, coord) +#define SMAASampleOffset(tex, coord, offset) tex2D(tex, coord + offset * SMAA_RT_METRICS.xy) +#define SMAA_FLATTEN [flatten] +#define SMAA_BRANCH [branch] +#endif +#if defined(SMAA_HLSL_4) || defined(SMAA_HLSL_4_1) +SamplerState LinearSampler { Filter = MIN_MAG_LINEAR_MIP_POINT; AddressU = Clamp; AddressV = Clamp; }; +SamplerState PointSampler { Filter = MIN_MAG_MIP_POINT; AddressU = Clamp; AddressV = Clamp; }; +#define SMAATexture2D(tex) Texture2D tex +#define SMAATexturePass2D(tex) tex +#define SMAASampleLevelZero(tex, coord) tex.SampleLevel(LinearSampler, coord, 0) +#define SMAASampleLevelZeroPoint(tex, coord) tex.SampleLevel(PointSampler, coord, 0) +#define SMAASampleLevelZeroOffset(tex, coord, offset) tex.SampleLevel(LinearSampler, coord, 0, offset) +#define SMAASample(tex, coord) tex.Sample(LinearSampler, coord) +#define SMAASamplePoint(tex, coord) tex.Sample(PointSampler, coord) +#define SMAASampleOffset(tex, coord, offset) tex.Sample(LinearSampler, coord, offset) +#define SMAA_FLATTEN [flatten] +#define SMAA_BRANCH [branch] +#define SMAATexture2DMS2(tex) Texture2DMS tex +#define SMAALoad(tex, pos, sample) tex.Load(pos, sample) +#if defined(SMAA_HLSL_4_1) +#define SMAAGather(tex, coord) tex.Gather(LinearSampler, coord, 0) +#endif +#endif +#if defined(SMAA_GLSL_3) || defined(SMAA_GLSL_4) +#define SMAATexture2D(tex) sampler2D tex +#define SMAATexturePass2D(tex) tex +#define SMAASampleLevelZero(tex, coord) textureLod(tex, coord, 0.0) +#define SMAASampleLevelZeroPoint(tex, coord) textureLod(tex, coord, 0.0) +#define SMAASampleLevelZeroOffset(tex, coord, offset) textureLodOffset(tex, coord, 0.0, offset) +#define SMAASample(tex, coord) texture(tex, coord) +#define SMAASamplePoint(tex, coord) texture(tex, coord) +#define SMAASampleOffset(tex, coord, offset) texture(tex, coord, offset) +#define SMAA_FLATTEN +#define SMAA_BRANCH +#define lerp(a, b, t) mix(a, b, t) +#define saturate(a) clamp(a, 0.0, 1.0) +#if defined(SMAA_GLSL_4) +#define mad(a, b, c) fma(a, b, c) +#define SMAAGather(tex, coord) textureGather(tex, coord) +#else +#define mad(a, b, c) (a * b + c) +#endif +#define float2 vec2 +#define float3 vec3 +#define float4 vec4 +#define int2 ivec2 +#define int3 ivec3 +#define int4 ivec4 +#define bool2 bvec2 +#define bool3 bvec3 +#define bool4 bvec4 +#endif + +#if !defined(SMAA_HLSL_3) && !defined(SMAA_HLSL_4) && !defined(SMAA_HLSL_4_1) && !defined(SMAA_GLSL_3) && !defined(SMAA_GLSL_4) && !defined(SMAA_CUSTOM_SL) +#error you must define the shading language: SMAA_HLSL_*, SMAA_GLSL_* or SMAA_CUSTOM_SL +#endif + +//----------------------------------------------------------------------------- +// Misc functions + +/** + * Gathers current pixel, and the top-left neighbors. + */ +float3 SMAAGatherNeighbours(float2 texcoord, + float4 offset[3], + SMAATexture2D(tex)) { + #ifdef SMAAGather + return SMAAGather(tex, texcoord + SMAA_RT_METRICS.xy * float2(-0.5, -0.5)).grb; + #else + float P = SMAASamplePoint(tex, texcoord).r; + float Pleft = SMAASamplePoint(tex, offset[0].xy).r; + float Ptop = SMAASamplePoint(tex, offset[0].zw).r; + return float3(P, Pleft, Ptop); + #endif +} + +/** + * Adjusts the threshold by means of predication. + */ +float2 SMAACalculatePredicatedThreshold(float2 texcoord, + float4 offset[3], + SMAATexture2D(predicationTex)) { + float3 neighbours = SMAAGatherNeighbours(texcoord, offset, SMAATexturePass2D(predicationTex)); + float2 delta = abs(neighbours.xx - neighbours.yz); + float2 edges = step(SMAA_PREDICATION_THRESHOLD, delta); + return SMAA_PREDICATION_SCALE * SMAA_THRESHOLD * (1.0 - SMAA_PREDICATION_STRENGTH * edges); +} + +/** + * Conditional move: + */ +void SMAAMovc(bool2 cond, inout float2 variable, float2 value) { + SMAA_FLATTEN if (cond.x) variable.x = value.x; + SMAA_FLATTEN if (cond.y) variable.y = value.y; +} + +void SMAAMovc(bool4 cond, inout float4 variable, float4 value) { + SMAAMovc(cond.xy, variable.xy, value.xy); + SMAAMovc(cond.zw, variable.zw, value.zw); +} + + +#if SMAA_INCLUDE_VS +//----------------------------------------------------------------------------- +// Vertex Shaders + +/** + * Edge Detection Vertex Shader + */ +void SMAAEdgeDetectionVS(float2 texcoord, + out float4 offset[3]) { + offset[0] = mad(SMAA_RT_METRICS.xyxy, float4(-1.0, 0.0, 0.0, -1.0), texcoord.xyxy); + offset[1] = mad(SMAA_RT_METRICS.xyxy, float4( 1.0, 0.0, 0.0, 1.0), texcoord.xyxy); + offset[2] = mad(SMAA_RT_METRICS.xyxy, float4(-2.0, 0.0, 0.0, -2.0), texcoord.xyxy); +} + +/** + * Blend Weight Calculation Vertex Shader + */ +void SMAABlendingWeightCalculationVS(float2 texcoord, + out float2 pixcoord, + out float4 offset[3]) { + pixcoord = texcoord * SMAA_RT_METRICS.zw; + + // We will use these offsets for the searches later on (see @PSEUDO_GATHER4): + offset[0] = mad(SMAA_RT_METRICS.xyxy, float4(-0.25, -0.125, 1.25, -0.125), texcoord.xyxy); + offset[1] = mad(SMAA_RT_METRICS.xyxy, float4(-0.125, -0.25, -0.125, 1.25), texcoord.xyxy); + + // And these for the searches, they indicate the ends of the loops: + offset[2] = mad(SMAA_RT_METRICS.xxyy, + float4(-2.0, 2.0, -2.0, 2.0) * float(SMAA_MAX_SEARCH_STEPS), + float4(offset[0].xz, offset[1].yw)); +} + +/** + * Neighborhood Blending Vertex Shader + */ +void SMAANeighborhoodBlendingVS(float2 texcoord, + out float4 offset) { + offset = mad(SMAA_RT_METRICS.xyxy, float4( 1.0, 0.0, 0.0, 1.0), texcoord.xyxy); +} +#endif // SMAA_INCLUDE_VS + +#if SMAA_INCLUDE_PS +//----------------------------------------------------------------------------- +// Edge Detection Pixel Shaders (First Pass) + +/** + * Luma Edge Detection + * + * IMPORTANT NOTICE: luma edge detection requires gamma-corrected colors, and + * thus 'colorTex' should be a non-sRGB texture. + */ +float2 SMAALumaEdgeDetectionPS(float2 texcoord, + float4 offset[3], + SMAATexture2D(colorTex) + #if SMAA_PREDICATION + , SMAATexture2D(predicationTex) + #endif + ) { + // Calculate the threshold: + #if SMAA_PREDICATION + float2 threshold = SMAACalculatePredicatedThreshold(texcoord, offset, SMAATexturePass2D(predicationTex)); + #else + float2 threshold = float2(SMAA_THRESHOLD, SMAA_THRESHOLD); + #endif + + // Calculate lumas: + float3 weights = float3(0.2126, 0.7152, 0.0722); + float L = dot(SMAASamplePoint(colorTex, texcoord).rgb, weights); + + float Lleft = dot(SMAASamplePoint(colorTex, offset[0].xy).rgb, weights); + float Ltop = dot(SMAASamplePoint(colorTex, offset[0].zw).rgb, weights); + + // We do the usual threshold: + float4 delta; + delta.xy = abs(L - float2(Lleft, Ltop)); + float2 edges = step(threshold, delta.xy); + + // Then discard if there is no edge: + if (dot(edges, float2(1.0, 1.0)) == 0.0) + discard; + + // Calculate right and bottom deltas: + float Lright = dot(SMAASamplePoint(colorTex, offset[1].xy).rgb, weights); + float Lbottom = dot(SMAASamplePoint(colorTex, offset[1].zw).rgb, weights); + delta.zw = abs(L - float2(Lright, Lbottom)); + + // Calculate the maximum delta in the direct neighborhood: + float2 maxDelta = max(delta.xy, delta.zw); + + // Calculate left-left and top-top deltas: + float Lleftleft = dot(SMAASamplePoint(colorTex, offset[2].xy).rgb, weights); + float Ltoptop = dot(SMAASamplePoint(colorTex, offset[2].zw).rgb, weights); + delta.zw = abs(float2(Lleft, Ltop) - float2(Lleftleft, Ltoptop)); + + // Calculate the final maximum delta: + maxDelta = max(maxDelta.xy, delta.zw); + float finalDelta = max(maxDelta.x, maxDelta.y); + + // Local contrast adaptation: + edges.xy *= step(finalDelta, SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR * delta.xy); + + return edges; +} + +/** + * Color Edge Detection + * + * IMPORTANT NOTICE: color edge detection requires gamma-corrected colors, and + * thus 'colorTex' should be a non-sRGB texture. + */ +float2 SMAAColorEdgeDetectionPS(float2 texcoord, + float4 offset[3], + SMAATexture2D(colorTex) + #if SMAA_PREDICATION + , SMAATexture2D(predicationTex) + #endif + ) { + // Calculate the threshold: + #if SMAA_PREDICATION + float2 threshold = SMAACalculatePredicatedThreshold(texcoord, offset, predicationTex); + #else + float2 threshold = float2(SMAA_THRESHOLD, SMAA_THRESHOLD); + #endif + + // Calculate color deltas: + float4 delta; + float3 C = SMAASamplePoint(colorTex, texcoord).rgb; + + float3 Cleft = SMAASamplePoint(colorTex, offset[0].xy).rgb; + float3 t = abs(C - Cleft); + delta.x = max(max(t.r, t.g), t.b); + + float3 Ctop = SMAASamplePoint(colorTex, offset[0].zw).rgb; + t = abs(C - Ctop); + delta.y = max(max(t.r, t.g), t.b); + + // We do the usual threshold: + float2 edges = step(threshold, delta.xy); + + // Then discard if there is no edge: + if (dot(edges, float2(1.0, 1.0)) == 0.0) + discard; + + // Calculate right and bottom deltas: + float3 Cright = SMAASamplePoint(colorTex, offset[1].xy).rgb; + t = abs(C - Cright); + delta.z = max(max(t.r, t.g), t.b); + + float3 Cbottom = SMAASamplePoint(colorTex, offset[1].zw).rgb; + t = abs(C - Cbottom); + delta.w = max(max(t.r, t.g), t.b); + + // Calculate the maximum delta in the direct neighborhood: + float2 maxDelta = max(delta.xy, delta.zw); + + // Calculate left-left and top-top deltas: + float3 Cleftleft = SMAASamplePoint(colorTex, offset[2].xy).rgb; + t = abs(C - Cleftleft); + delta.z = max(max(t.r, t.g), t.b); + + float3 Ctoptop = SMAASamplePoint(colorTex, offset[2].zw).rgb; + t = abs(C - Ctoptop); + delta.w = max(max(t.r, t.g), t.b); + + // Calculate the final maximum delta: + maxDelta = max(maxDelta.xy, delta.zw); + float finalDelta = max(maxDelta.x, maxDelta.y); + + // Local contrast adaptation: + edges.xy *= step(finalDelta, SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR * delta.xy); + + return edges; +} + +/** + * Depth Edge Detection + */ +float2 SMAADepthEdgeDetectionPS(float2 texcoord, + float4 offset[3], + SMAATexture2D(depthTex)) { + float3 neighbours = SMAAGatherNeighbours(texcoord, offset, SMAATexturePass2D(depthTex)); + float2 delta = abs(neighbours.xx - float2(neighbours.y, neighbours.z)); + float2 edges = step(SMAA_DEPTH_THRESHOLD, delta); + + if (dot(edges, float2(1.0, 1.0)) == 0.0) + discard; + + return edges; +} + +//----------------------------------------------------------------------------- +// Diagonal Search Functions + +#if !defined(SMAA_DISABLE_DIAG_DETECTION) + +/** + * Allows to decode two binary values from a bilinear-filtered access. + */ +float2 SMAADecodeDiagBilinearAccess(float2 e) { + // Bilinear access for fetching 'e' have a 0.25 offset, and we are + // interested in the R and G edges: + // + // +---G---+-------+ + // | x o R x | + // +-------+-------+ + // + // Then, if one of these edge is enabled: + // Red: (0.75 * X + 0.25 * 1) => 0.25 or 1.0 + // Green: (0.75 * 1 + 0.25 * X) => 0.75 or 1.0 + // + // This function will unpack the values (mad + mul + round): + // wolframalpha.com: round(x * abs(5 * x - 5 * 0.75)) plot 0 to 1 + e.r = e.r * abs(5.0 * e.r - 5.0 * 0.75); + return round(e); +} + +float4 SMAADecodeDiagBilinearAccess(float4 e) { + e.rb = e.rb * abs(5.0 * e.rb - 5.0 * 0.75); + return round(e); +} + +/** + * These functions allows to perform diagonal pattern searches. + */ +float2 SMAASearchDiag1(SMAATexture2D(edgesTex), float2 texcoord, float2 dir, out float2 e) { + float4 coord = float4(texcoord, -1.0, 1.0); + float3 t = float3(SMAA_RT_METRICS.xy, 1.0); + while (coord.z < float(SMAA_MAX_SEARCH_STEPS_DIAG - 1) && + coord.w > 0.9) { + coord.xyz = mad(t, float3(dir, 1.0), coord.xyz); + e = SMAASampleLevelZero(edgesTex, coord.xy).rg; + coord.w = dot(e, float2(0.5, 0.5)); + } + return coord.zw; +} + +float2 SMAASearchDiag2(SMAATexture2D(edgesTex), float2 texcoord, float2 dir, out float2 e) { + float4 coord = float4(texcoord, -1.0, 1.0); + coord.x += 0.25 * SMAA_RT_METRICS.x; // See @SearchDiag2Optimization + float3 t = float3(SMAA_RT_METRICS.xy, 1.0); + while (coord.z < float(SMAA_MAX_SEARCH_STEPS_DIAG - 1) && + coord.w > 0.9) { + coord.xyz = mad(t, float3(dir, 1.0), coord.xyz); + + // @SearchDiag2Optimization + // Fetch both edges at once using bilinear filtering: + e = SMAASampleLevelZero(edgesTex, coord.xy).rg; + e = SMAADecodeDiagBilinearAccess(e); + + // Non-optimized version: + // e.g = SMAASampleLevelZero(edgesTex, coord.xy).g; + // e.r = SMAASampleLevelZeroOffset(edgesTex, coord.xy, int2(1, 0)).r; + + coord.w = dot(e, float2(0.5, 0.5)); + } + return coord.zw; +} + +/** + * Similar to SMAAArea, this calculates the area corresponding to a certain + * diagonal distance and crossing edges 'e'. + */ +float2 SMAAAreaDiag(SMAATexture2D(areaTex), float2 dist, float2 e, float offset) { + float2 texcoord = mad(float2(SMAA_AREATEX_MAX_DISTANCE_DIAG, SMAA_AREATEX_MAX_DISTANCE_DIAG), e, dist); + + // We do a scale and bias for mapping to texel space: + texcoord = mad(SMAA_AREATEX_PIXEL_SIZE, texcoord, 0.5 * SMAA_AREATEX_PIXEL_SIZE); + + // Diagonal areas are on the second half of the texture: + texcoord.x += 0.5; + + // Move to proper place, according to the subpixel offset: + texcoord.y += SMAA_AREATEX_SUBTEX_SIZE * offset; + + // Do it! + return SMAA_AREATEX_SELECT(SMAASampleLevelZero(areaTex, texcoord)); +} + +/** + * This searches for diagonal patterns and returns the corresponding weights. + */ +float2 SMAACalculateDiagWeights(SMAATexture2D(edgesTex), SMAATexture2D(areaTex), float2 texcoord, float2 e, float4 subsampleIndices) { + float2 weights = float2(0.0, 0.0); + + // Search for the line ends: + float4 d; + float2 end; + if (e.r > 0.0) { + d.xz = SMAASearchDiag1(SMAATexturePass2D(edgesTex), texcoord, float2(-1.0, 1.0), end); + d.x += float(end.y > 0.9); + } else + d.xz = float2(0.0, 0.0); + d.yw = SMAASearchDiag1(SMAATexturePass2D(edgesTex), texcoord, float2(1.0, -1.0), end); + + SMAA_BRANCH + if (d.x + d.y > 2.0) { // d.x + d.y + 1 > 3 + // Fetch the crossing edges: + float4 coords = mad(float4(-d.x + 0.25, d.x, d.y, -d.y - 0.25), SMAA_RT_METRICS.xyxy, texcoord.xyxy); + float4 c; + c.xy = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2(-1, 0)).rg; + c.zw = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1, 0)).rg; + c.yxwz = SMAADecodeDiagBilinearAccess(c.xyzw); + + // Non-optimized version: + // float4 coords = mad(float4(-d.x, d.x, d.y, -d.y), SMAA_RT_METRICS.xyxy, texcoord.xyxy); + // float4 c; + // c.x = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2(-1, 0)).g; + // c.y = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2( 0, 0)).r; + // c.z = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1, 0)).g; + // c.w = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1, -1)).r; + + // Merge crossing edges at each side into a single value: + float2 cc = mad(float2(2.0, 2.0), c.xz, c.yw); + + // Remove the crossing edge if we didn't found the end of the line: + SMAAMovc(bool2(step(0.9, d.zw)), cc, float2(0.0, 0.0)); + + // Fetch the areas for this line: + weights += SMAAAreaDiag(SMAATexturePass2D(areaTex), d.xy, cc, subsampleIndices.z); + } + + // Search for the line ends: + d.xz = SMAASearchDiag2(SMAATexturePass2D(edgesTex), texcoord, float2(-1.0, -1.0), end); + if (SMAASampleLevelZeroOffset(edgesTex, texcoord, int2(1, 0)).r > 0.0) { + d.yw = SMAASearchDiag2(SMAATexturePass2D(edgesTex), texcoord, float2(1.0, 1.0), end); + d.y += float(end.y > 0.9); + } else + d.yw = float2(0.0, 0.0); + + SMAA_BRANCH + if (d.x + d.y > 2.0) { // d.x + d.y + 1 > 3 + // Fetch the crossing edges: + float4 coords = mad(float4(-d.x, -d.x, d.y, d.y), SMAA_RT_METRICS.xyxy, texcoord.xyxy); + float4 c; + c.x = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2(-1, 0)).g; + c.y = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2( 0, -1)).r; + c.zw = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1, 0)).gr; + float2 cc = mad(float2(2.0, 2.0), c.xz, c.yw); + + // Remove the crossing edge if we didn't found the end of the line: + SMAAMovc(bool2(step(0.9, d.zw)), cc, float2(0.0, 0.0)); + + // Fetch the areas for this line: + weights += SMAAAreaDiag(SMAATexturePass2D(areaTex), d.xy, cc, subsampleIndices.w).gr; + } + + return weights; +} +#endif + +//----------------------------------------------------------------------------- +// Horizontal/Vertical Search Functions + +/** + * This allows to determine how much length should we add in the last step + * of the searches. It takes the bilinearly interpolated edge (see + * @PSEUDO_GATHER4), and adds 0, 1 or 2, depending on which edges and + * crossing edges are active. + */ +float SMAASearchLength(SMAATexture2D(searchTex), float2 e, float offset) { + // The texture is flipped vertically, with left and right cases taking half + // of the space horizontally: + float2 scale = SMAA_SEARCHTEX_SIZE * float2(0.5, -1.0); + float2 bias = SMAA_SEARCHTEX_SIZE * float2(offset, 1.0); + + // Scale and bias to access texel centers: + scale += float2(-1.0, 1.0); + bias += float2( 0.5, -0.5); + + // Convert from pixel coordinates to texcoords: + // (We use SMAA_SEARCHTEX_PACKED_SIZE because the texture is cropped) + scale *= 1.0 / SMAA_SEARCHTEX_PACKED_SIZE; + bias *= 1.0 / SMAA_SEARCHTEX_PACKED_SIZE; + + // Lookup the search texture: + return SMAA_SEARCHTEX_SELECT(SMAASampleLevelZero(searchTex, mad(scale, e, bias))); +} + +/** + * Horizontal/vertical search functions for the 2nd pass. + */ +float SMAASearchXLeft(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end) { + /** + * @PSEUDO_GATHER4 + * This texcoord has been offset by (-0.25, -0.125) in the vertex shader to + * sample between edge, thus fetching four edges in a row. + * Sampling with different offsets in each direction allows to disambiguate + * which edges are active from the four fetched ones. + */ + float2 e = float2(0.0, 1.0); + while (texcoord.x > end && + e.g > 0.8281 && // Is there some edge not activated? + e.r == 0.0) { // Or is there a crossing edge that breaks the line? + e = SMAASampleLevelZero(edgesTex, texcoord).rg; + texcoord = mad(-float2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord); + } + + float offset = mad(-(255.0 / 127.0), SMAASearchLength(SMAATexturePass2D(searchTex), e, 0.0), 3.25); + return mad(SMAA_RT_METRICS.x, offset, texcoord.x); + + // Non-optimized version: + // We correct the previous (-0.25, -0.125) offset we applied: + // texcoord.x += 0.25 * SMAA_RT_METRICS.x; + + // The searches are bias by 1, so adjust the coords accordingly: + // texcoord.x += SMAA_RT_METRICS.x; + + // Disambiguate the length added by the last step: + // texcoord.x += 2.0 * SMAA_RT_METRICS.x; // Undo last step + // texcoord.x -= SMAA_RT_METRICS.x * (255.0 / 127.0) * SMAASearchLength(SMAATexturePass2D(searchTex), e, 0.0); + // return mad(SMAA_RT_METRICS.x, offset, texcoord.x); +} + +float SMAASearchXRight(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end) { + float2 e = float2(0.0, 1.0); + while (texcoord.x < end && + e.g > 0.8281 && // Is there some edge not activated? + e.r == 0.0) { // Or is there a crossing edge that breaks the line? + e = SMAASampleLevelZero(edgesTex, texcoord).rg; + texcoord = mad(float2(2.0, 0.0), SMAA_RT_METRICS.xy, texcoord); + } + float offset = mad(-(255.0 / 127.0), SMAASearchLength(SMAATexturePass2D(searchTex), e, 0.5), 3.25); + return mad(-SMAA_RT_METRICS.x, offset, texcoord.x); +} + +float SMAASearchYUp(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end) { + float2 e = float2(1.0, 0.0); + while (texcoord.y > end && + e.r > 0.8281 && // Is there some edge not activated? + e.g == 0.0) { // Or is there a crossing edge that breaks the line? + e = SMAASampleLevelZero(edgesTex, texcoord).rg; + texcoord = mad(-float2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord); + } + float offset = mad(-(255.0 / 127.0), SMAASearchLength(SMAATexturePass2D(searchTex), e.gr, 0.0), 3.25); + return mad(SMAA_RT_METRICS.y, offset, texcoord.y); +} + +float SMAASearchYDown(SMAATexture2D(edgesTex), SMAATexture2D(searchTex), float2 texcoord, float end) { + float2 e = float2(1.0, 0.0); + while (texcoord.y < end && + e.r > 0.8281 && // Is there some edge not activated? + e.g == 0.0) { // Or is there a crossing edge that breaks the line? + e = SMAASampleLevelZero(edgesTex, texcoord).rg; + texcoord = mad(float2(0.0, 2.0), SMAA_RT_METRICS.xy, texcoord); + } + float offset = mad(-(255.0 / 127.0), SMAASearchLength(SMAATexturePass2D(searchTex), e.gr, 0.5), 3.25); + return mad(-SMAA_RT_METRICS.y, offset, texcoord.y); +} + +/** + * Ok, we have the distance and both crossing edges. So, what are the areas + * at each side of current edge? + */ +float2 SMAAArea(SMAATexture2D(areaTex), float2 dist, float e1, float e2, float offset) { + // Rounding prevents precision errors of bilinear filtering: + float2 texcoord = mad(float2(SMAA_AREATEX_MAX_DISTANCE, SMAA_AREATEX_MAX_DISTANCE), round(4.0 * float2(e1, e2)), dist); + + // We do a scale and bias for mapping to texel space: + texcoord = mad(SMAA_AREATEX_PIXEL_SIZE, texcoord, 0.5 * SMAA_AREATEX_PIXEL_SIZE); + + // Move to proper place, according to the subpixel offset: + texcoord.y = mad(SMAA_AREATEX_SUBTEX_SIZE, offset, texcoord.y); + + // Do it! + return SMAA_AREATEX_SELECT(SMAASampleLevelZero(areaTex, texcoord)); +} + +//----------------------------------------------------------------------------- +// Corner Detection Functions + +void SMAADetectHorizontalCornerPattern(SMAATexture2D(edgesTex), inout float2 weights, float4 texcoord, float2 d) { + #if !defined(SMAA_DISABLE_CORNER_DETECTION) + float2 leftRight = step(d.xy, d.yx); + float2 rounding = (1.0 - SMAA_CORNER_ROUNDING_NORM) * leftRight; + + rounding /= leftRight.x + leftRight.y; // Reduce blending for pixels in the center of a line. + + float2 factor = float2(1.0, 1.0); + factor.x -= rounding.x * SMAASampleLevelZeroOffset(edgesTex, texcoord.xy, int2(0, 1)).r; + factor.x -= rounding.y * SMAASampleLevelZeroOffset(edgesTex, texcoord.zw, int2(1, 1)).r; + factor.y -= rounding.x * SMAASampleLevelZeroOffset(edgesTex, texcoord.xy, int2(0, -2)).r; + factor.y -= rounding.y * SMAASampleLevelZeroOffset(edgesTex, texcoord.zw, int2(1, -2)).r; + + weights *= saturate(factor); + #endif +} + +void SMAADetectVerticalCornerPattern(SMAATexture2D(edgesTex), inout float2 weights, float4 texcoord, float2 d) { + #if !defined(SMAA_DISABLE_CORNER_DETECTION) + float2 leftRight = step(d.xy, d.yx); + float2 rounding = (1.0 - SMAA_CORNER_ROUNDING_NORM) * leftRight; + + rounding /= leftRight.x + leftRight.y; + + float2 factor = float2(1.0, 1.0); + factor.x -= rounding.x * SMAASampleLevelZeroOffset(edgesTex, texcoord.xy, int2( 1, 0)).g; + factor.x -= rounding.y * SMAASampleLevelZeroOffset(edgesTex, texcoord.zw, int2( 1, 1)).g; + factor.y -= rounding.x * SMAASampleLevelZeroOffset(edgesTex, texcoord.xy, int2(-2, 0)).g; + factor.y -= rounding.y * SMAASampleLevelZeroOffset(edgesTex, texcoord.zw, int2(-2, 1)).g; + + weights *= saturate(factor); + #endif +} + +//----------------------------------------------------------------------------- +// Blending Weight Calculation Pixel Shader (Second Pass) + +float4 SMAABlendingWeightCalculationPS(float2 texcoord, + float2 pixcoord, + float4 offset[3], + SMAATexture2D(edgesTex), + SMAATexture2D(areaTex), + SMAATexture2D(searchTex), + float4 subsampleIndices) { // Just pass zero for SMAA 1x, see @SUBSAMPLE_INDICES. + float4 weights = float4(0.0, 0.0, 0.0, 0.0); + + float2 e = SMAASample(edgesTex, texcoord).rg; + + SMAA_BRANCH + if (e.g > 0.0) { // Edge at north + #if !defined(SMAA_DISABLE_DIAG_DETECTION) + // Diagonals have both north and west edges, so searching for them in + // one of the boundaries is enough. + weights.rg = SMAACalculateDiagWeights(SMAATexturePass2D(edgesTex), SMAATexturePass2D(areaTex), texcoord, e, subsampleIndices); + + // We give priority to diagonals, so if we find a diagonal we skip + // horizontal/vertical processing. + SMAA_BRANCH + if (weights.r == -weights.g) { // weights.r + weights.g == 0.0 + #endif + + float2 d; + + // Find the distance to the left: + float3 coords; + coords.x = SMAASearchXLeft(SMAATexturePass2D(edgesTex), SMAATexturePass2D(searchTex), offset[0].xy, offset[2].x); + coords.y = offset[1].y; // offset[1].y = texcoord.y - 0.25 * SMAA_RT_METRICS.y (@CROSSING_OFFSET) + d.x = coords.x; + + // Now fetch the left crossing edges, two at a time using bilinear + // filtering. Sampling at -0.25 (see @CROSSING_OFFSET) enables to + // discern what value each edge has: + float e1 = SMAASampleLevelZero(edgesTex, coords.xy).r; + + // Find the distance to the right: + coords.z = SMAASearchXRight(SMAATexturePass2D(edgesTex), SMAATexturePass2D(searchTex), offset[0].zw, offset[2].y); + d.y = coords.z; + + // We want the distances to be in pixel units (doing this here allow to + // better interleave arithmetic and memory accesses): + d = abs(round(mad(SMAA_RT_METRICS.zz, d, -pixcoord.xx))); + + // SMAAArea below needs a sqrt, as the areas texture is compressed + // quadratically: + float2 sqrt_d = sqrt(d); + + // Fetch the right crossing edges: + float e2 = SMAASampleLevelZeroOffset(edgesTex, coords.zy, int2(1, 0)).r; + + // Ok, we know how this pattern looks like, now it is time for getting + // the actual area: + weights.rg = SMAAArea(SMAATexturePass2D(areaTex), sqrt_d, e1, e2, subsampleIndices.y); + + // Fix corners: + coords.y = texcoord.y; + SMAADetectHorizontalCornerPattern(SMAATexturePass2D(edgesTex), weights.rg, coords.xyzy, d); + + #if !defined(SMAA_DISABLE_DIAG_DETECTION) + } else + e.r = 0.0; // Skip vertical processing. + #endif + } + + SMAA_BRANCH + if (e.r > 0.0) { // Edge at west + float2 d; + + // Find the distance to the top: + float3 coords; + coords.y = SMAASearchYUp(SMAATexturePass2D(edgesTex), SMAATexturePass2D(searchTex), offset[1].xy, offset[2].z); + coords.x = offset[0].x; // offset[1].x = texcoord.x - 0.25 * SMAA_RT_METRICS.x; + d.x = coords.y; + + // Fetch the top crossing edges: + float e1 = SMAASampleLevelZero(edgesTex, coords.xy).g; + + // Find the distance to the bottom: + coords.z = SMAASearchYDown(SMAATexturePass2D(edgesTex), SMAATexturePass2D(searchTex), offset[1].zw, offset[2].w); + d.y = coords.z; + + // We want the distances to be in pixel units: + d = abs(round(mad(SMAA_RT_METRICS.ww, d, -pixcoord.yy))); + + // SMAAArea below needs a sqrt, as the areas texture is compressed + // quadratically: + float2 sqrt_d = sqrt(d); + + // Fetch the bottom crossing edges: + float e2 = SMAASampleLevelZeroOffset(edgesTex, coords.xz, int2(0, 1)).g; + + // Get the area for this direction: + weights.ba = SMAAArea(SMAATexturePass2D(areaTex), sqrt_d, e1, e2, subsampleIndices.x); + + // Fix corners: + coords.x = texcoord.x; + SMAADetectVerticalCornerPattern(SMAATexturePass2D(edgesTex), weights.ba, coords.xyxz, d); + } + + return weights; +} + +//----------------------------------------------------------------------------- +// Neighborhood Blending Pixel Shader (Third Pass) + +float4 SMAANeighborhoodBlendingPS(float2 texcoord, + float4 offset, + SMAATexture2D(colorTex), + SMAATexture2D(blendTex) + #if SMAA_REPROJECTION + , SMAATexture2D(velocityTex) + #endif + ) { + // Fetch the blending weights for current pixel: + float4 a; + a.x = SMAASample(blendTex, offset.xy).a; // Right + a.y = SMAASample(blendTex, offset.zw).g; // Top + a.wz = SMAASample(blendTex, texcoord).xz; // Bottom / Left + + // Is there any blending weight with a value greater than 0.0? + SMAA_BRANCH + if (dot(a, float4(1.0, 1.0, 1.0, 1.0)) < 1e-5) { + float4 color = SMAASampleLevelZero(colorTex, texcoord); + + #if SMAA_REPROJECTION + float2 velocity = SMAA_DECODE_VELOCITY(SMAASampleLevelZero(velocityTex, texcoord)); + + // Pack velocity into the alpha channel: + color.a = sqrt(5.0 * length(velocity)); + #endif + + return color; + } else { + bool h = max(a.x, a.z) > max(a.y, a.w); // max(horizontal) > max(vertical) + + // Calculate the blending offsets: + float4 blendingOffset = float4(0.0, a.y, 0.0, a.w); + float2 blendingWeight = a.yw; + SMAAMovc(bool4(h, h, h, h), blendingOffset, float4(a.x, 0.0, a.z, 0.0)); + SMAAMovc(bool2(h, h), blendingWeight, a.xz); + blendingWeight /= dot(blendingWeight, float2(1.0, 1.0)); + + // Calculate the texture coordinates: + float4 blendingCoord = mad(blendingOffset, float4(SMAA_RT_METRICS.xy, -SMAA_RT_METRICS.xy), texcoord.xyxy); + + // We exploit bilinear filtering to mix current pixel with the chosen + // neighbor: + float4 color = blendingWeight.x * SMAASampleLevelZero(colorTex, blendingCoord.xy); + color += blendingWeight.y * SMAASampleLevelZero(colorTex, blendingCoord.zw); + + #if SMAA_REPROJECTION + // Antialias velocity for proper reprojection in a later stage: + float2 velocity = blendingWeight.x * SMAA_DECODE_VELOCITY(SMAASampleLevelZero(velocityTex, blendingCoord.xy)); + velocity += blendingWeight.y * SMAA_DECODE_VELOCITY(SMAASampleLevelZero(velocityTex, blendingCoord.zw)); + + // Pack velocity into the alpha channel: + color.a = sqrt(5.0 * length(velocity)); + #endif + + return color; + } +} + +//----------------------------------------------------------------------------- +// Temporal Resolve Pixel Shader (Optional Pass) + +float4 SMAAResolvePS(float2 texcoord, + SMAATexture2D(currentColorTex), + SMAATexture2D(previousColorTex) + #if SMAA_REPROJECTION + , SMAATexture2D(velocityTex) + #endif + ) { + #if SMAA_REPROJECTION + // Velocity is assumed to be calculated for motion blur, so we need to + // inverse it for reprojection: + float2 velocity = -SMAA_DECODE_VELOCITY(SMAASamplePoint(velocityTex, texcoord).rg); + + // Fetch current pixel: + float4 current = SMAASamplePoint(currentColorTex, texcoord); + + // Reproject current coordinates and fetch previous pixel: + float4 previous = SMAASamplePoint(previousColorTex, texcoord + velocity); + + // Attenuate the previous pixel if the velocity is different: + float delta = abs(current.a * current.a - previous.a * previous.a) / 5.0; + float weight = 0.5 * saturate(1.0 - sqrt(delta) * SMAA_REPROJECTION_WEIGHT_SCALE); + + // Blend the pixels according to the calculated weight: + return lerp(current, previous, weight); + #else + // Just blend the pixels: + float4 current = SMAASamplePoint(currentColorTex, texcoord); + float4 previous = SMAASamplePoint(previousColorTex, texcoord); + return lerp(current, previous, 0.5); + #endif +} + +//----------------------------------------------------------------------------- +// Separate Multisamples Pixel Shader (Optional Pass) + +#ifdef SMAALoad +void SMAASeparatePS(float4 position, + float2 texcoord, + out float4 target0, + out float4 target1, + SMAATexture2DMS2(colorTexMS)) { + int2 pos = int2(position.xy); + target0 = SMAALoad(colorTexMS, pos, 0); + target1 = SMAALoad(colorTexMS, pos, 1); +} +#endif + +//----------------------------------------------------------------------------- +#endif // SMAA_INCLUDE_PS diff --git a/runtime/shaders/smaa_blend.comp b/runtime/shaders/smaa_blend.comp new file mode 100644 index 00000000..a1e346b3 --- /dev/null +++ b/runtime/shaders/smaa_blend.comp @@ -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)); +} diff --git a/runtime/shaders/smaa_edge.comp b/runtime/shaders/smaa_edge.comp new file mode 100644 index 00000000..32a9af0a --- /dev/null +++ b/runtime/shaders/smaa_edge.comp @@ -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)); +} diff --git a/runtime/shaders/smaa_encode.comp b/runtime/shaders/smaa_encode.comp new file mode 100644 index 00000000..a9476160 --- /dev/null +++ b/runtime/shaders/smaa_encode.comp @@ -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)); +} diff --git a/runtime/shaders/smaa_setup.glsl b/runtime/shaders/smaa_setup.glsl new file mode 100644 index 00000000..040b91f3 --- /dev/null +++ b/runtime/shaders/smaa_setup.glsl @@ -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); +} diff --git a/runtime/shaders/smaa_weight.comp b/runtime/shaders/smaa_weight.comp new file mode 100644 index 00000000..481372cb --- /dev/null +++ b/runtime/shaders/smaa_weight.comp @@ -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))); +} diff --git a/runtime/third_party/smaa/AreaTex.bin b/runtime/third_party/smaa/AreaTex.bin new file mode 100644 index 00000000..f4a7a1b4 Binary files /dev/null and b/runtime/third_party/smaa/AreaTex.bin differ diff --git a/runtime/third_party/smaa/LICENSE.txt b/runtime/third_party/smaa/LICENSE.txt new file mode 100644 index 00000000..e6c0a6db --- /dev/null +++ b/runtime/third_party/smaa/LICENSE.txt @@ -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. diff --git a/runtime/third_party/smaa/SearchTex.bin b/runtime/third_party/smaa/SearchTex.bin new file mode 100644 index 00000000..db5bf73f Binary files /dev/null and b/runtime/third_party/smaa/SearchTex.bin differ diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index f8ff5903..beb9ccc3 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -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 diff --git a/runtime/ui_text.c b/runtime/ui_text.c index 2fccdff7..61bb391d 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -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\\\\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。重启游戏后才会读取。对于修改游戏代码的金手指," diff --git a/runtime/ui_text.h b/runtime/ui_text.h index 66e67606..5215dc93 100644 --- a/runtime/ui_text.h +++ b/runtime/ui_text.h @@ -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, diff --git a/runtime/vk_aa.c b/runtime/vk_aa.c new file mode 100644 index 00000000..d1aa5bd7 --- /dev/null +++ b/runtime/vk_aa.c @@ -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 +#include + +#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; +} diff --git a/runtime/vk_exec.h b/runtime/vk_exec.h index 501d0cb6..5296b355 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -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); diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 8a50ce4f..d5591cac 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -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); diff --git a/tools/build_shaders.py b/tools/build_shaders.py index d62b4148..c3d82e18 100644 --- a/tools/build_shaders.py +++ b/tools/build_shaders.py @@ -9,11 +9,11 @@ missing and the stale ones before it compiles the runtime. Three kinds of source runtime/shaders/.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):