From f29e34e7a0b675f417d47a5f434106b9e36f38a2 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 23:20:31 -0500 Subject: [PATCH] Make the observer-response scene mapper the default HDR tone mapper, anchored to the game's mid grey and tone slope. The HDR tonemap gains a mapper built on a finite cone response in LMS: scene grey 0.18 lands where the game's curve puts it and rises through it with the slope of the game's curve, and the hue bend of the game's clamp is given back to bright colours. It takes the scene before the game's exposure gain and the gain itself, which the splice now finds in the game's shader. The luminance mapper stays selectable and the game's own curve stays the third choice. The mapper fits the Rec. 709 gamut here, as the game's scene target holds no negative component. New settings: the tone mapper's mid grey, tone slope match, tone slope and hue clip emulation. The gamma correction on the present applies with the luminance mapper only. Not part of this: eye adaptation, the anti-aliasing and reconstruction shaders, bloom and local exposure sliders, the output colour space choice and a 16-bit float scene target. Checked by build and numerically against the source of the port; the picture on an HDR display has not been judged. THIRD_PARTY_NOTICES.md names the source. Co-Authored-By: Claude Fable 5.2 --- README.md | 2 +- THIRD_PARTY_NOTICES.md | 1 + docs/architecture.md | 3 +- nx2navi.ini.example | 28 ++-- runtime/cfg.c | 13 +- runtime/shaders/hdr_tonemap.frag | 239 +++++++++++++++++++++++++++++-- runtime/ui/menu.cpp | 31 ++-- runtime/ui_text.c | 55 ++++++- runtime/ui_text.h | 2 + runtime/vk.h | 4 +- runtime/vk_exec_draw.c | 6 +- runtime/vk_hdr.c | 185 +++++++++++++++++------- 12 files changed, 470 insertions(+), 99 deletions(-) diff --git a/README.md b/README.md index 47244d5a..ad2618b2 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ Report problems as issues, following [CONTRIBUTING.md](CONTRIBUTING.md). bars stay clean. The frame limit counts the frames the game draws, and frame generation adds its frames on top of it. The frame limit can also be handed to RivaTuner Statistics Server -- HDR on HDR displays: the game's own native HDR, or its SDR picture tone mapped to the display's peak, with exposure, highlights, shadows, contrast, saturation and gamma in the menu +- HDR on HDR displays: the game's own native HDR, or its scene tone mapped to the display's peak and matched to the game's mid grey and tone, with exposure, highlights, shadows, contrast and saturation in the menu - The game's known shaders prepared when the game is built, and the pipelines met in play built again in the background at each start, so play rarely stops to compile them; a shader or pipeline met for the first time can still hold a frame, and a draw that must wait for its pipeline has it built on worker threads, never on the game's diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md index 83a29525..06ac1cc1 100644 --- a/THIRD_PARTY_NOTICES.md +++ b/THIRD_PARTY_NOTICES.md @@ -38,6 +38,7 @@ Code copied or adapted from another project is listed here with its source, the | MacNativeGames NX2navi resolution patches: output, swapchain and scene size sites and instruction words for 1.2.1 and 1.4.3, the data-page layout, the HeapDef heap names and sizes | https://github.com/ly05010419/MacNativeGames @ `aaf6ba1f494fabaa209d049e528d6f923dfe822a` (`runtime/game_patches.c`, `game_patches_v143.c`, `game_version.h`, `scripts/make_output_res_overlay.py`), Copyright (c) 2026 MacNativeGames contributors | MIT | `recomp/patches-v121.txt`, `recomp/patches-v143.txt`, `runtime/resolution.c` | | AMD FidelityFX Super Resolution 1.0 (`ffx_a.h`, `ffx_fsr1.h`, unmodified; licence text in `runtime/shaders/ffx/LICENSE.txt`) | https://github.com/GPUOpen-Effects/FidelityFX-FSR @ `a21ffb8f6c13233ba336352bdff293894c706575`, Copyright (c) 2021 Advanced Micro Devices, Inc. | MIT | `runtime/shaders/ffx/`, `runtime/shaders/fsr_easu.comp`, `runtime/shaders/fsr_rcas.comp` (compiled into `runtime/fsr_easu_spv.h`, `runtime/fsr_rcas_spv.h`) | | RenoDX shader library: the RenoDRT tone mapper (Daniele curve, OkLab hue and chrominance, highlights, shadows, contrast, flare, saturation, highlight saturation, blowout), the luminance upgrade of an SDR grade, its colour matrices, and the swap chain pass's gamma correction, gamut compression, peak clamp and PQ encode, ported from HLSL to GLSL | https://github.com/clshortfuse/renodx @ `bc0ead39917634e95c534f7e8cfb59ab82880747` (`src/shaders/tonemap.hlsl`, `tonemap/reno_drt.hlsl`, `tonemap/daniele.hlsl`, `draw.hlsl`, `colorgrade.hlsl`, `colorcorrect.hlsl`, `color/oklab.hlsl`, `color/rgb.hlsl`, `color/pq.hlsl`, `color/srgb.hlsl`, `color/gamma.hlsl`; setting names, ranges and defaults of `src/templates/settings.hpp`), Copyright (c) 2025 Carlos Lopez Jr. | MIT | `runtime/shaders/hdr_tonemap.frag` and `runtime/shaders/hdr_encode.comp` (compiled into `runtime/hdr_tonemap_spv.h`, `runtime/hdr_encode_spv.h`); the HDR settings of `runtime/cfg.c`, `runtime/vk_hdr.c` and `runtime/ui/menu.cpp` | +| RenoDX scene mapper for this game and its PsychoV35 tone mapper: the finite cone response in LMS with its mean, deficit and midpoint chroma, the highlights, shadows and purity grading, the exact fit to the target gamut, the mid grey anchor and tone slope of the game's curve, and the hue clip emulation, ported from HLSL and Slang to GLSL (the paths its scene mapper takes; without eye adaptation) | https://github.com/clshortfuse/renodx @ `59bd6f606a5d105bc6de52627332703c840443be` (`src/shaders/tonemap/psychov/test35.hlsl`, `src/games/zelda-oot-oz/scene_tonemap.slangi`, `shared.h`; setting names, ranges and defaults of its `addon.cpp`), Copyright (C) 2026 Carlos Lopez | MIT | `runtime/shaders/hdr_tonemap.frag` (compiled into `runtime/hdr_tonemap_spv.h`); the HDR settings of `runtime/cfg.c`, `runtime/vk_hdr.c` and `runtime/ui/menu.cpp` | | 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) | diff --git a/docs/architecture.md b/docs/architecture.md index 683d3638..0dd51736 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -158,7 +158,8 @@ keeps per-core data without locks and relies on that, so the host models it (`ru - 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 - holds the HDR picture relative to the HUD's white; the game's FSR 1 pass loses its output clamp; the present reads + holds the HDR picture relative to the HUD's white (the function gets the values the curve takes, and the scene + before the game's exposure gain with that gain, which its default tone mapper anchors mid grey on); the game's FSR 1 pass loses its output clamp; the present reads the float picture in place of the window texture and `shaders/hdr_encode.comp` writes PQ for an HDR10 swapchain. `NX2NAVI_HDR=native` reports an HDR10 display to the game, which then renders and presents its own PQ picture. The menu switches between off and `1` during a run: the splice, the HDR extensions and the settings in the support diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 1abb7a06..a4e21170 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -467,20 +467,30 @@ # hdr_peak_nits = 1000 # hdr_game_nits = 203 # hdr_ui_nits = 203 -# With hdr = 1: gamma correction (off, 2.2 or 2.4) shows the picture as a display of that power would, as the game was -# made to be seen; off keeps the sRGB curve. The tone mapper: vanilla is the game's own curve at SDR brightness, -# renodrt carries the game's look up to the peak. -# hdr_gamma = 2.2 -# hdr_tonemap = renodrt -# With renodrt: exposure in percent (100 unchanged); highlights, shadows, contrast, saturation and highlight saturation -# from 0 to 100 (50 unchanged); blowout (bright parts towards white) and flare (lifted blacks) from 0 to 100 (0 none); -# hue correction in percent (how much bright colours keep their hue); grade, the percentage of the game's own colour -# grading the picture keeps. +# With hdr = 1, the tone mapper: vanilla is the game's own curve at SDR brightness; psychov35 sends the scene through a +# model of the eye's response, matched to the game's mid grey and tone, up to the peak; renodrt carries the game's SDR +# look up to the peak by brightness. +# hdr_tonemap = psychov35 +# With psychov35: where the scene's mid grey lands (vanilla: where the game's curve puts it; render: at the scene's +# value); whether the mid tones get the contrast of the game's curve; the tone curve's steepness through mid grey from +# 0 to 100 (50 unchanged); and in percent how much bright colours turn as where the game's SDR picture clips (fire +# towards yellow and white). +# hdr_midgray = vanilla +# hdr_match_slope = 1 +# hdr_tone_slope = 50 +# hdr_hue_clip = 50 +# With psychov35 and renodrt: exposure in percent (100 unchanged); highlights, shadows, contrast and saturation from 0 +# to 100 (50 unchanged). # hdr_exposure = 100 # hdr_highlights = 50 # hdr_shadows = 50 # hdr_contrast = 50 # hdr_saturation = 50 +# With renodrt: gamma correction (off, 2.2 or 2.4) shows the picture as a display of that power would, off keeps the +# sRGB curve; highlight saturation from 0 to 100 (50 unchanged); blowout (bright parts towards white) and flare (lifted +# blacks) from 0 to 100 (0 none); hue correction in percent (how much bright colours keep their hue); grade, the +# percentage of the game's own colour grading the picture keeps. +# hdr_gamma = 2.2 # hdr_highlight_sat = 50 # hdr_blowout = 0 # hdr_flare = 0 diff --git a/runtime/cfg.c b/runtime/cfg.c index eb5fb17d..167c8a64 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -46,8 +46,8 @@ static const char *const known[] = { "FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FLIGHT_EVERY", "FLUSH_SPAN", "FLUSH_UPLOAD", "FOG_CULL", "FOLIAGE_SHADOW_CULL", "FOLIAGE_SHADOW_VRS", "FOV", "FOV_PATCH", "FG_HUDLESS_SHOW", "FG_MULT", "FPS60", "FPS_LIMIT", "FRAMEGEN", "FRAMEGEN_MULTIPLIER", "FREEZE_IDLE", "FREE_CORES", "FULLSCREEN", "FXAA", "GAME_ASPECT", "GAME_RES", "GPA", "GPA_EMPTY", "GPA_TWIN", "GPA_TWIN_MAX_KB", "GPL", "GPL_DRAW_BUILD", "GPL_FEEDBACK", "PIPE_AHEAD", "PIPE_FAST_WORKERS", "PIPE_FRAME_WAIT_MS", "PIPE_LINK_DRAW_MS", "PIPE_SKIP_STEADY", "PROG_LEAD", "GPU", "GPU_INDEX", "GPU_PASSES", "GPU_NAME", "HDR", "HDR_BLOWOUT", "HDR_CONTRAST", "HDR_EXPOSURE", "HDR_FLARE", "HDR_GAMMA", "HDR_GAME_NITS", "HDR_GRADE", "HDR_HIGHLIGHT_SAT", "HDR_HIGHLIGHTS", "HDR_HUE", - "HDR_LIVE", - "HDR_PEAK_NITS", "HDR_SATURATION", "HDR_SHADOWS", "HDR_TONEMAP", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", + "HDR_HUE_CLIP", "HDR_LIVE", "HDR_MATCH_SLOPE", "HDR_MIDGRAY", + "HDR_PEAK_NITS", "HDR_SATURATION", "HDR_SHADOWS", "HDR_TONEMAP", "HDR_TONE_SLOPE", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", "BIG_BUILD_FREE_MB", "BIG_BUILD_MAX_MB", "LIB_PREBUILD_BIG", "LIB_PREBUILD_BIG_BG", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LIB_WARM", "PIPE_LINK_MS", "FRAME_HOLD_MS", "PACE_SMOOTH", "OQ_BATCH", "GATE_WAIT_MS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MEM_CENSUS", "MENU_LANG", "MENU_SOUND_VOLUME", "MENU_TEST", "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_CLICK", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MICROPHONE", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MONITOR", "MOTION", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_HOST", "MOVIE_PACE", "MVOBJ_ALL", "MVOBJ_BANKS", "MVOBJ_EQUAL", "MVOBJ_EXACT", "MVOBJ_EXEC", "MVOBJ_NOC4", "MVOBJ_ONLY", "MVOBJ_TIMES", "MVPREV_CHECK", "MVPREV_GROW", "MVPREV_SKIP", "MVPREV_TRUST", "MV_CHECK", @@ -89,7 +89,8 @@ static const struct { const char *key, *section; } sections[] = { {"HDR_UI_NITS", "display"}, {"HDR_GAMMA", "display"}, {"HDR_TONEMAP", "display"}, {"HDR_EXPOSURE", "display"}, {"HDR_HIGHLIGHTS", "display"}, {"HDR_SHADOWS", "display"}, {"HDR_CONTRAST", "display"}, {"HDR_SATURATION", "display"}, {"HDR_HIGHLIGHT_SAT", "display"}, {"HDR_BLOWOUT", "display"}, {"HDR_FLARE", "display"}, {"HDR_HUE", "display"}, - {"HDR_GRADE", "display"}, {"SHADOW_RES", "display"}, + {"HDR_GRADE", "display"}, {"HDR_MIDGRAY", "display"}, {"HDR_MATCH_SLOPE", "display"}, {"HDR_TONE_SLOPE", "display"}, + {"HDR_HUE_CLIP", "display"}, {"SHADOW_RES", "display"}, {"DLAA", "display"}, {"DLSS", "display"}, {"DLSS_PRESET", "display"}, {"DLSS_IDLE_FREE", "display"}, {"DLSS_CHAR_MASK", "display"}, {"FXAA", "display"}, {"FRAMEGEN", "display"}, {"FRAMEGEN_MULTIPLIER", "display"}, {"FG_MULT", "display"}, {"DEVICE_PROFILE", "display"}, {"ASSUME_FOCUS", "display"}, @@ -178,7 +179,11 @@ static const CfgMeta metas[] = { {"HDR", "0", "0|1|native", LIVE}, {"HDR_LIVE", "1", "0|1", RESTART}, {"HDR_PEAK_NITS", "1000", "400..10000", LIVE}, {"HDR_GAME_NITS", "203", "48..500", LIVE}, {"HDR_UI_NITS", "203", "48..500", LIVE}, {"HDR_GAMMA", "2.2", "off|2.2|2.4", LIVE}, - {"HDR_TONEMAP", "renodrt", "vanilla|renodrt", LIVE}, {"HDR_EXPOSURE", "100", "0..200", LIVE}, + {"HDR_TONEMAP", "psychov35", "vanilla|psychov35|renodrt", LIVE}, {"HDR_EXPOSURE", "100", "0..200", LIVE}, + // the observer-response mapper's: where scene grey lands, its slope there matched to the game's curve, that slope + // (50 unchanged) and how much of the game's hue bend at its clamp bright colours get + {"HDR_MIDGRAY", "vanilla", "render|vanilla", LIVE}, {"HDR_MATCH_SLOPE", "1", "0|1", LIVE}, + {"HDR_TONE_SLOPE", "50", "0..100", LIVE}, {"HDR_HUE_CLIP", "50", "0..100", LIVE}, {"HDR_HIGHLIGHTS", "50", "0..100", LIVE}, {"HDR_SHADOWS", "50", "0..100", LIVE}, {"HDR_CONTRAST", "50", "0..100", LIVE}, {"HDR_SATURATION", "50", "0..100", LIVE}, {"HDR_HIGHLIGHT_SAT", "50", "0..100", LIVE}, {"HDR_BLOWOUT", "0", "0..100", LIVE}, {"HDR_FLARE", "0", "0..100", LIVE}, {"HDR_HUE", "100", "0..100", LIVE}, diff --git a/runtime/shaders/hdr_tonemap.frag b/runtime/shaders/hdr_tonemap.frag index a3dcd919..ead24b0d 100644 --- a/runtime/shaders/hdr_tonemap.frag +++ b/runtime/shaders/hdr_tonemap.frag @@ -2,6 +2,8 @@ // build_shaders: keep functions // // Copyright (c) 2025 Carlos Lopez Jr. +// Copyright (C) 2026 Carlos Lopez +// SPDX-License-Identifier: MIT // // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated // documentation files (the "Software"), to deal in the Software without restriction, including without limitation the @@ -17,27 +19,40 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // // The HDR tonemap, as a function that runtime/vk_hdr.c (vk_hdr_shader_swap) splices into the game's own tonemap -// shader: nx_tonemap takes the three values the game feeds its fitted ACES curve (its scene light after its colour -// matrix, bloom and exposure) and returns the colour the shader then stores, linear Rec. 709 relative to the HUD's -// white, which the present encodes to PQ (shaders/hdr_encode.comp). main only exists so the compiler keeps the -// functions; vk_hdr.c drops it. build_shaders.py compiles this file without optimisation ("keep functions" above), -// as inlining would leave no function to splice. +// shader. nx_tonemap takes x, the three values the game feeds its fitted ACES curve (its scene light after its colour +// matrix, bloom, exposure and, in the gameplay shader, local exposure), and scene, the same light before the exposure +// gain and local exposure (rgb) with that gain (a). It returns the colour the shader then stores, linear Rec. 709 +// relative to the HUD's white, which the present encodes to PQ (shaders/hdr_encode.comp). main only exists so the +// compiler keeps the functions; vk_hdr.c drops it. build_shaders.py compiles this file without optimisation ("keep +// functions" above), as inlining would leave no function to splice. // -// The picture: the game's SDR result (its curve, clamped) is the colour grade; the tone mapper upgrades it to the -// HDR range by luminance against a neutral SDR curve of the same input, then maps that onto the display: a -// luminance curve towards the peak with exposure, highlights, shadows, contrast and flare, and in OkLab the hue of -// the input, saturation, highlight saturation and blowout. Settings (vk_hdr.c vk_hdr_support, support buffer bytes -// 1440..1503): +// Three tone mappers (p0.w): +// - 0, the game's curve: its SDR result at the picture's white. +// - 9, the observer-response mapper (scene_tonemap, psycho35): the scene light goes through a finite cone response +// in LMS, anchored so that scene grey 0.18 lands where the game's curve puts it (or at 0.18 times the gain) and, +// with the tone slope matched, rising through it as steeply as the game's curve does at its steepest; the response +// saturates at the display's peak. Chroma is the response's residual around its mean, scaled back just enough to +// fit the target gamut. It takes the scene without local exposure. Rec. 709 is the target here: the game's scene +// target is r11g11b10f, which holds no negative component, so a wider fit would be clipped channel by channel. +// - 3, the luminance mapper (drt): the game's SDR result is the colour grade, upgraded to the HDR range by luminance +// against a neutral SDR curve of the same input, then a luminance curve towards the peak with exposure, +// highlights, shadows, contrast and flare, and in OkLab the hue of the input, saturation, highlight saturation and +// blowout. +// Settings (vk_hdr.c vk_hdr_support, support buffer bytes 1440..1519): // p0: peak nits, game nits (white of the picture), HUD nits, tone mapper (-1: the game's own SDR picture, as when the -// swapchain is not HDR10; 0: the game's curve; 3: the HDR tone mapper) +// swapchain is not HDR10; else as above) // p1: exposure, highlights, shadows, contrast // p2: saturation, highlight saturation, blowout, flare (parsed: 1 = unchanged for the first two) -// p3: hue correction, grade strength, gamma correction (0 off, 1: 2.2, 2: 2.4; applied on the present), unused +// p3: hue correction, grade strength, gamma correction (0 off, 1: 2.2, 2: 2.4; applied on the present), tone slope +// p4: hue clip emulation (0..1), mid grey (0: 0.18 times the gain, 1: the game's curve at 0.18), match tone slope +// (0 or 1), unused layout(location = 0) in vec4 a0; layout(location = 1) in vec4 a1; layout(location = 2) in vec4 a2; layout(location = 3) in vec4 a3; layout(location = 4) in vec4 a4; +layout(location = 5) in vec4 a5; +layout(location = 6) in vec4 a6; layout(location = 0) out vec4 o; // Matrices are written row by row; mulm(m, v) is the row-vector product of that layout. @@ -209,11 +224,205 @@ vec3 aces_fitted(vec3 x) { // the game's curve and clamp return clamp(x * (2.51 * x + 0.03) / (x * (2.43 * x + 0.59) + 0.14), 0.0, 1.0); } -vec3 nx_tonemap(vec3 x, vec4 p0, vec4 p1, vec4 p2, vec4 p3) { +// ---- the observer-response mapper + +// Rec. 709 (D65) to the cone fundamentals LMS of the two-degree observer and back, and LMS to Rec. 2020; rows. The +// products of the XYZ matrices of each, worked out in double precision. +const mat3 PSY_BT709_TO_LMS = mat3(0.2896057766, 0.6972466442, 0.0763712786, + 0.0901837576, 0.7073490257, 0.1122031464, + 0.0155287401, 0.0536093073, 0.5097978305); +const mat3 PSY_LMS_TO_BT709 = mat3(4.9676196505, -4.9223795294, 0.3391991812, + -0.6196828788, 2.0517506515, -0.3587439844, + -0.0861520040, -0.0658193830, 1.9889544833); +const mat3 PSY_LMS_TO_BT2020 = mat3(2.9089213537, -2.4155642448, 0.1808342948, + -0.2275532730, 1.5457966613, -0.2838427127, + -0.0502712321, 0.0409489517, 1.7552839121); +const vec3 PSY_YF_WEIGHTS = vec3(0.68990272, 0.34832189, 0.0); // luminance of the observer from L and M +const vec3 PSY_D65_WHITE_LMS = vec3(1.0632236993, 0.9097359297, 0.5789358779); // PSY_BT709_TO_LMS of white + +// Exact mean on equal-component vectors. +float psy_mean(vec3 v) { return v.x + ((v.y - v.x) + (v.z - v.x)) / 3.0; } + +// A residual in white-normalised LMS as the displacement it is in the target's RGB (0: Rec. 709, else Rec. 2020). +vec3 psy_target_residual(vec3 normalized_lms, int target) { + vec3 physical_lms = normalized_lms * PSY_D65_WHITE_LMS; + return target == 0 ? mulm(PSY_LMS_TO_BT709, physical_lms) : mulm(PSY_LMS_TO_BT2020, physical_lms); +} + +// The finite response u of one cone in the log domain: log_u, log_tail = log(1 - u^h) and log_deficit = log(1 - u), +// kept as logarithms so that neither end underflows. +void psy_response_logs(float log_q, float k, float n, float h, out float log_u, out float log_tail, out float log_deficit) { + float latent = log(k) - log(1.0 - k) + n * log_q; + float correction = log(1.0 + exp(-abs(latent))); + log_u = (min(latent, 0.0) - correction) / h; + log_tail = -max(latent, 0.0) - correction; + log_deficit = latent > 20.0 ? log_tail - log(h) + : log(abs(log_u) < 0.01 + ? -log_u * (1.0 + log_u * (0.5 + log_u * (1.0 / 6.0 + log_u / 24.0))) + : 1.0 - exp(log_u)); +} + +struct PsyReference { + vec3 source_ratio; // the input against its adaptation anchor, largest component 1 + vec3 log_source_ratio; + vec3 anchor_power; + vec3 input_exponent; + float response_compression; + float peak; +}; + +struct PsySample { + float mean; // the response's mean over the cones, in peak units times the peak + float headroom; // what is left up to the peak, from the deficits + vec3 residual; // the chroma: the response around its mean, its direction half way to the source's +}; + +// The response of the three cones at exposure_stops above the reference's own drive. +PsySample psy_reference_at(PsyReference reference, float exposure_stops) { + vec3 response, deficit; + for (int i = 0; i < 3; ++i) { + if (reference.source_ratio[i] == 0.0) { + response[i] = 0.0; + deficit[i] = 1.0; + } else { + float log_u, log_tail, log_deficit; + psy_response_logs(reference.log_source_ratio[i] + exposure_stops * log(2.0), reference.anchor_power[i], + reference.input_exponent[i], reference.response_compression, log_u, log_tail, log_deficit); + response[i] = exp(log_u); + deficit[i] = exp(log_deficit); + } + } + float response_mean = psy_mean(response); + vec3 response_residual = response_mean > 0.5 ? psy_mean(deficit) - deficit : response - response_mean; + // normalise before squaring: the length can underflow where the vector itself does not + float residual_scale = max(abs(response_residual.x), max(abs(response_residual.y), abs(response_residual.z))); + vec3 scaled_residual = residual_scale > 0.0 ? response_residual / residual_scale : vec3(0.0); + float scaled_radius = length(scaled_residual); + float radius = residual_scale * scaled_radius; + vec3 direction = scaled_radius > 0.0 ? scaled_residual / scaled_radius : vec3(0.0); + vec3 source = reference.source_ratio - psy_mean(reference.source_ratio); + float source_radius = length(source); + if (source_radius > 0.0 && radius > 0.0) { + vec3 midpoint_direction = source / source_radius + direction; + float midpoint_length = length(midpoint_direction); + if (midpoint_length > 0.0) direction = midpoint_direction / midpoint_length; + } + PsySample s; + s.mean = reference.peak * response_mean; + s.headroom = reference.peak * psy_mean(deficit); + s.residual = reference.peak * radius * direction; + return s; +} + +// Extended linear Rec. 709 in, relative to reference white; the same out, inside the target gamut and below peak. +// anchor_in is the scene grey the eye is adapted to, anchor_out where it lands; cone is the response's exponent. +vec3 psycho35(vec3 bt709, float peak, float highlights, float shadows, float contrast, float purity_scale, float cone, + float anchor_in_gray, float anchor_out_gray, int target) { + vec3 input_lms = mulm(PSY_BT709_TO_LMS, bt709); + vec3 anchor_in = mulm(PSY_BT709_TO_LMS, vec3(anchor_in_gray)); + vec3 anchor_out = anchor_out_gray == anchor_in_gray ? anchor_in : mulm(PSY_BT709_TO_LMS, vec3(anchor_out_gray)); + if (highlights != 1.0 || shadows != 1.0) { + float input_yf = dot(PSY_YF_WEIGHTS, input_lms); + float anchor_yf = dot(PSY_YF_WEIGHTS, anchor_in); + float graded_y = input_yf; + if (highlights != 1.0) { + float ratio = graded_y / anchor_yf; + float blend = graded_y > anchor_yf ? clamp(log2(ratio) / log2(1.0 / anchor_yf), 0.0, 1.0) : 0.0; + blend = smoother(blend); + float shaped = pow(ratio, abs(highlights - 1.0)); + graded_y = highlights > 1.0 ? graded_y * mix(1.0, shaped, blend) : graded_y / mix(1.0, shaped, blend); + } + if (shadows != 1.0) { + float ratio = graded_y / anchor_yf; + float blend = graded_y > anchor_yf * exp2(-4.0) ? clamp(log2(ratio) / -4.0, 0.0, 1.0) : 1.0; + blend = smoother(blend); + float target_y = shadows > 1.0 + ? graded_y * (1.0 - anchor_yf * anchor_yf) + anchor_yf * anchor_yf * anchor_yf * pow(ratio, 2.0 - shadows) + : graded_y * (1.0 + anchor_yf * anchor_yf) - anchor_yf * anchor_yf * anchor_yf * pow(ratio, shadows); + graded_y = mix(graded_y, target_y, blend); + } + if (input_yf != 0.0) input_lms *= graded_y / input_yf; + } + // purity (saturation) around the neutral of the same luminance, relative to the adaptation + vec3 source_ratio = input_lms / anchor_in; + if (purity_scale != contrast) { + float neutral_ratio = dot(PSY_YF_WEIGHTS, input_lms) / dot(PSY_YF_WEIGHTS, anchor_in); + vec3 purity_direction = source_ratio - neutral_ratio; + float purity_amount = purity_scale / contrast; + // more purity must not turn a valid cone input negative: the common amount is bounded, not single cones + if (all(greaterThanEqual(source_ratio, vec3(0.0))) && neutral_ratio > 0.0 && purity_amount > 1.0) { + for (int c = 0; c < 3; ++c) + if (purity_direction[c] < 0.0) purity_amount = min(purity_amount, neutral_ratio / -purity_direction[c]); + source_ratio = max(neutral_ratio + purity_amount * purity_direction, 0.0); + } else { + source_ratio = neutral_ratio + purity_amount * purity_direction; + } + } + PsyReference reference; + float response_power = contrast * cone; + reference.response_compression = 1.0; + reference.peak = peak; + reference.anchor_power = pow(anchor_out / (PSY_D65_WHITE_LMS * reference.peak), vec3(reference.response_compression)); + reference.input_exponent = response_power * reference.response_compression / (1.0 - reference.anchor_power); + if (all(equal(source_ratio, vec3(0.0)))) return vec3(0.0); + // the exposure factored out: source_ratio = drive * reference.source_ratio + float drive = max(abs(source_ratio.x), max(abs(source_ratio.y), abs(source_ratio.z))); + reference.source_ratio = source_ratio / drive; + reference.log_source_ratio = log(vec3(reference.source_ratio.x != 0.0 ? reference.source_ratio.x : 1.0, + reference.source_ratio.y != 0.0 ? reference.source_ratio.y : 1.0, + reference.source_ratio.z != 0.0 ? reference.source_ratio.z : 1.0)); + PsySample actual = psy_reference_at(reference, log2(drive)); + if (all(equal(actual.residual, vec3(0.0)))) return vec3(actual.mean); // neutral: nothing to fit + // the exact fit: the chroma scaled until the nearest face of the target's cube (0 or the peak) is touched + vec3 target_displacement = psy_target_residual(actual.residual, target); + float scale = 1.0; + for (int channel = 0; channel < 3; ++channel) { + if (target_displacement[channel] > 0.0) scale = min(scale, actual.headroom / target_displacement[channel]); + else if (target_displacement[channel] < 0.0) scale = min(scale, actual.mean / -target_displacement[channel]); + } + return actual.mean + psy_target_residual(actual.residual * scale, 0); +} + +// The bend the game's clamp gives a bright colour (a clipped channel stops, the others catch up, the hue turns towards +// yellow, cyan or white), given to the unclamped colour: the curve's result, stretched back over the colour's own +// range of magnitudes, at the colour's luminance. +vec3 hue_clip(vec3 untonemapped, float amount) { + vec3 magnitude = abs(untonemapped); + vec3 reference = aces_fitted(magnitude); + float low = min(reference.x, min(reference.y, reference.z)); + float range = max(reference.x, max(reference.y, reference.z)) - low; + bool neutral = range == 0.0; + float mag_low = min(magnitude.x, min(magnitude.y, magnitude.z)); + float mag_high = max(magnitude.x, max(magnitude.y, magnitude.z)); + vec3 transferred = mix(vec3(mag_low), vec3(mag_high), (reference - low) / (neutral ? 1.0 : range)); + transferred = copy_sign(transferred, untonemapped); + if (neutral) transferred = untonemapped; + vec3 bent = mix(untonemapped, transferred, amount); + float original_y = y709(untonemapped), bent_y = y709(bent); + if (original_y > 0.0 && bent_y > 0.0) untonemapped = bent * (original_y / bent_y); + return untonemapped; +} + +// The scene (before the exposure gain) through the observer-response mapper, in picture whites. +vec3 scene_tonemap(vec3 untonemapped, float gain, vec4 p0, vec4 p1, vec4 p2, vec4 p3, vec4 p4) { + if (p1.x == 0.0) return vec3(0.0); // zero exposure is black; the mapper needs a positive anchor + float peak_nits = max(p0.x, p0.y); + float reference_exposure = p4.y == 1.0 ? aces_fitted(vec3(0.18)).x / 0.18 : 1.0; + if (p4.x > 0.0) untonemapped = hue_clip(untonemapped, p4.x); + // the log-log slope x V'(x) / V(x) of the game's curve where V''(x) = 0, at x = 0.120305925 + float cone = p4.z != 0.0 ? 1.3355037565264822 : 1.0; + float anchor_in = 0.18; + float anchor_out = anchor_in * gain * reference_exposure * p1.x; + return psycho35(untonemapped, peak_nits / p0.y, p1.y, p1.z, p1.w, p2.x, cone * p3.w, anchor_in, anchor_out, 0); +} + +vec3 nx_tonemap(vec3 x, vec4 scene, vec4 p0, vec4 p1, vec4 p2, vec4 p3, vec4 p4) { vec3 vanilla = aces_fitted(x); if (p0.w < 0.0) return vanilla; vec3 color = vanilla; - if (p0.w > 0.0) { + if (p0.w == 9.0) { + color = scene_tonemap(scene.rgb, scene.a, p0, p1, p2, p3, p4); + } else if (p0.w > 0.0) { vec3 graded = p3.y == 0.0 ? x : upgrade(x, neutral_sdr(x), vanilla, p3.y); float peak = p0.x / p0.y; // the display's peak in picture whites; corrected for the present's gamma if (p3.z != 0.0) peak = gamma_to_srgb(peak, p3.z == 1.0 ? 2.2 : 2.4); @@ -225,4 +434,4 @@ vec3 nx_tonemap(vec3 x, vec4 p0, vec4 p1, vec4 p2, vec4 p3) { return mix(color, vec3(0.0), isnan(color)); } -void main() { o = vec4(nx_tonemap(a0.xyz, a1, a2, a3, a4), 1.0); } +void main() { o = vec4(nx_tonemap(a0.xyz, a1, a2, a3, a4, a5, a6), 1.0); } diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index 9e7fd90b..afa03aff 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -915,8 +915,9 @@ static const char *hdr_note(void) { } // HDR (vk_hdr.c): off or the HDR tone mapper over the game's picture, switched at once, or the game's native HDR, -// which the game takes up at its start. The brightness rows follow while it is on; the tone mapper's own rows only -// with the first (native HDR is the game's picture as it is), its grading rows only with its own curve chosen. +// which the game takes up at its start. The brightness rows follow while it is on; the tone mapper row only with the +// first (native HDR is the game's picture as it is), then the rows of the tone mapper chosen: none for the game's +// curve, the grading rows of both others between the rows each has alone. static void row_hdr(void) { static const char *const values[] = {"0", "1", "native"}; const char *names[] = {menu_text(UI_MENU_FRAMEGEN_OFF), menu_text(UI_MENU_HDR_TONEMAPPED), menu_text(UI_MENU_HDR_NATIVE)}; @@ -926,18 +927,30 @@ static void row_hdr(void) { opt_slider("NX2NAVI_HDR_GAME_NITS", UI_MENU_HDR_GAME, 48, 500, UI_MENU_HDR_GAME_DESC, 1, "%.0f nits"); opt_slider("NX2NAVI_HDR_UI_NITS", UI_MENU_HDR_UI, 48, 500, UI_MENU_HDR_UI_DESC, 1, "%.0f nits"); if (!strcmp(cfg_value("NX2NAVI_HDR"), "native")) return; - static const char *const gammas[] = {"off", "2.2", "2.4"}; - const char *gamma_names[] = {menu_text(UI_MENU_FRAMEGEN_OFF), "2.2", "BT.1886 (2.4)"}; - opt_choice_list("NX2NAVI_HDR_GAMMA", UI_MENU_HDR_GAMMA, gammas, gamma_names, 3, UI_MENU_HDR_GAMMA_DESC); - static const char *const curves[] = {"vanilla", "renodrt"}; - static const char *const curve_names[] = {"Vanilla", "RenoDRT"}; - opt_choice_list("NX2NAVI_HDR_TONEMAP", UI_MENU_HDR_TONEMAP, curves, curve_names, 2, UI_MENU_HDR_TONEMAP_DESC); - if (!strcmp(cfg_value("NX2NAVI_HDR_TONEMAP"), "vanilla")) return; + static const char *const curves[] = {"vanilla", "psychov35", "renodrt"}; + static const char *const curve_names[] = {"Vanilla", "PsychoV35", "RenoDRT"}; + opt_choice_list("NX2NAVI_HDR_TONEMAP", UI_MENU_HDR_TONEMAP, curves, curve_names, 3, UI_MENU_HDR_TONEMAP_DESC); + const char *curve = cfg_value("NX2NAVI_HDR_TONEMAP"); + if (!strcmp(curve, "vanilla")) return; + const bool luminance = !strcmp(curve, "renodrt"); // else the observer-response mapper, as vk_hdr.c reads the setting + if (luminance) { + static const char *const gammas[] = {"off", "2.2", "2.4"}; + const char *gamma_names[] = {menu_text(UI_MENU_FRAMEGEN_OFF), "2.2", "BT.1886 (2.4)"}; + opt_choice_list("NX2NAVI_HDR_GAMMA", UI_MENU_HDR_GAMMA, gammas, gamma_names, 3, UI_MENU_HDR_GAMMA_DESC); + } else { + static const char *const greys[] = {"render", "vanilla"}; + static const char *const grey_names[] = {"Render", "Vanilla"}; + opt_choice_list("NX2NAVI_HDR_MIDGRAY", UI_MENU_HDR_MIDGRAY, greys, grey_names, 2, UI_MENU_HDR_MIDGRAY_DESC); + opt_toggle("NX2NAVI_HDR_MATCH_SLOPE", UI_MENU_HDR_MATCH_SLOPE, UI_MENU_HDR_MATCH_SLOPE_DESC); + opt_slider("NX2NAVI_HDR_TONE_SLOPE", UI_MENU_HDR_TONE_SLOPE, 0, 100, UI_MENU_HDR_TONE_SLOPE_DESC, 1, "%.0f"); + opt_slider("NX2NAVI_HDR_HUE_CLIP", UI_MENU_HDR_HUE_CLIP, 0, 100, UI_MENU_HDR_HUE_CLIP_DESC, 5, "%.0f %%"); + } opt_slider("NX2NAVI_HDR_EXPOSURE", UI_MENU_HDR_EXPOSURE, 0, 200, UI_MENU_HDR_EXPOSURE_DESC, 5, "%.0f %%"); opt_slider("NX2NAVI_HDR_HIGHLIGHTS", UI_MENU_HDR_HIGHLIGHTS, 0, 100, UI_MENU_HDR_HIGHLIGHTS_DESC, 1, "%.0f"); opt_slider("NX2NAVI_HDR_SHADOWS", UI_MENU_HDR_SHADOWS, 0, 100, UI_MENU_HDR_SHADOWS_DESC, 1, "%.0f"); opt_slider("NX2NAVI_HDR_CONTRAST", UI_MENU_HDR_CONTRAST, 0, 100, UI_MENU_HDR_CONTRAST_DESC, 1, "%.0f"); opt_slider("NX2NAVI_HDR_SATURATION", UI_MENU_HDR_SATURATION, 0, 100, UI_MENU_HDR_SATURATION_DESC, 1, "%.0f"); + if (!luminance) return; opt_slider("NX2NAVI_HDR_HIGHLIGHT_SAT", UI_MENU_HDR_HIGHLIGHT_SAT, 0, 100, UI_MENU_HDR_HIGHLIGHT_SAT_DESC, 1, "%.0f"); opt_slider("NX2NAVI_HDR_BLOWOUT", UI_MENU_HDR_BLOWOUT, 0, 100, UI_MENU_HDR_BLOWOUT_DESC, 1, "%.0f"); opt_slider("NX2NAVI_HDR_FLARE", UI_MENU_HDR_FLARE, 0, 100, UI_MENU_HDR_FLARE_DESC, 1, "%.0f"); diff --git a/runtime/ui_text.c b/runtime/ui_text.c index 61905516..8462312b 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -123,8 +123,9 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_HDR_GAMMA_DESC] = "Shows the picture as a 2.2 or BT.1886 (2.4) display would, as the game was made to be " "seen; Off keeps the sRGB curve, with lighter shadows.", [UI_MENU_HDR_TONEMAP] = "Tone mapper", - [UI_MENU_HDR_TONEMAP_DESC] = "Vanilla: the game's own curve at SDR brightness. RenoDRT: the game's look carried up " - "to your display's peak.", + [UI_MENU_HDR_TONEMAP_DESC] = "Vanilla: the game's own curve at SDR brightness. PsychoV35: the scene through a model " + "of the eye's response, matched to the game's mid grey and tone, up to your display's " + "peak. RenoDRT: the game's SDR look carried up to the peak by brightness.", [UI_MENU_HDR_EXPOSURE] = "Exposure", [UI_MENU_HDR_EXPOSURE_DESC] = "Brightness of the whole picture before the tone mapper; 100 % leaves it unchanged.", [UI_MENU_HDR_HIGHLIGHTS] = "Highlights", @@ -143,6 +144,16 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_HDR_FLARE_DESC] = "Lifts the darkest parts as a little stray light would; 0 keeps black black.", [UI_MENU_HDR_GRADE] = "Colour grading", [UI_MENU_HDR_GRADE_DESC] = "How much of the game's own colour grading the HDR picture keeps; 0 is the scene without it.", + [UI_MENU_HDR_MIDGRAY] = "Mid grey", + [UI_MENU_HDR_MIDGRAY_DESC] = "Where the scene's mid grey lands. Vanilla: where the game's own curve puts it. Render: " + "at the scene's value, a little darker.", + [UI_MENU_HDR_MATCH_SLOPE] = "Match tone slope", + [UI_MENU_HDR_MATCH_SLOPE_DESC] = "Gives the mid tones the contrast of the game's own curve.", + [UI_MENU_HDR_TONE_SLOPE] = "Tone slope", + [UI_MENU_HDR_TONE_SLOPE_DESC] = "Steepness of the tone curve through mid grey; 50 leaves it unchanged.", + [UI_MENU_HDR_HUE_CLIP] = "Hue clip emulation", + [UI_MENU_HDR_HUE_CLIP_DESC] = "How much bright colours turn as they do where the game's SDR picture clips: fire " + "towards yellow and white. 0 keeps their hue.", [UI_MENU_SHADOW_SCALE] = "Shadow resolution", [UI_MENU_SHADOW_SCALE_DESC] = "Sharper shadows. Higher costs more GPU power.", [UI_MENU_PCSS] = "Soft shadows", @@ -688,8 +699,9 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_HDR_GAMMA_DESC] = "Показывает картинку так, как её показал бы экран с гаммой 2.2 или BT.1886 (2.4), для " "которого игра делалась; «Выкл.» оставляет кривую sRGB с более светлыми тенями.", [UI_MENU_HDR_TONEMAP] = "Тональная кривая", - [UI_MENU_HDR_TONEMAP_DESC] = "Vanilla: собственная кривая игры с яркостью SDR. RenoDRT: вид игры, доведённый до " - "пиковой яркости экрана.", + [UI_MENU_HDR_TONEMAP_DESC] = "Vanilla: собственная кривая игры с яркостью SDR. PsychoV35: сцена через модель отклика " + "глаза, согласованная со средним серым и тоном игры, до пиковой яркости экрана. RenoDRT: " + "SDR-вид игры, доведённый до пика по яркости.", [UI_MENU_HDR_EXPOSURE] = "Экспозиция", [UI_MENU_HDR_EXPOSURE_DESC] = "Яркость всей картинки до тональной кривой; 100 % оставляет её без изменений.", [UI_MENU_HDR_HIGHLIGHTS] = "Света", @@ -708,6 +720,16 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_HDR_FLARE_DESC] = "Приподнимает самые тёмные участки, как немного рассеянного света; 0 оставляет чёрное чёрным.", [UI_MENU_HDR_GRADE] = "Цветокоррекция игры", [UI_MENU_HDR_GRADE_DESC] = "Сколько собственной цветокоррекции игры сохраняет HDR-картинка; 0 — сцена без неё.", + [UI_MENU_HDR_MIDGRAY] = "Средний серый", + [UI_MENU_HDR_MIDGRAY_DESC] = "Куда попадает средний серый сцены. Vanilla: туда же, куда его ставит кривая игры. " + "Render: на значение сцены, чуть темнее.", + [UI_MENU_HDR_MATCH_SLOPE] = "Наклон тона как в игре", + [UI_MENU_HDR_MATCH_SLOPE_DESC] = "Даёт средним тонам контраст собственной кривой игры.", + [UI_MENU_HDR_TONE_SLOPE] = "Наклон тона", + [UI_MENU_HDR_TONE_SLOPE_DESC] = "Крутизна тональной кривой у среднего серого; 50 оставляет её без изменений.", + [UI_MENU_HDR_HUE_CLIP] = "Имитация сдвига оттенка", + [UI_MENU_HDR_HUE_CLIP_DESC] = "Насколько яркие цвета сдвигаются так, как при обрезке в SDR-картинке игры: огонь к " + "жёлтому и белому. 0 сохраняет их оттенок.", [UI_MENU_SHADOW_SCALE] = "Разрешение теней", [UI_MENU_SHADOW_SCALE_DESC] = "Более чёткие тени. Выше — больше нагрузка на видеокарту.", [UI_MENU_PCSS] = "Мягкие тени", @@ -1549,8 +1571,9 @@ static const char *const text_pt_br[UI_TEXT_COUNT] = { [UI_MENU_HDR_GAMMA_DESC] = "Mostra a imagem como uma tela de gama 2.2 ou BT.1886 (2.4) mostraria, como o jogo foi " "feito para ser visto; Desligado mantém a curva sRGB, com sombras mais claras.", [UI_MENU_HDR_TONEMAP] = "Curva de tons", - [UI_MENU_HDR_TONEMAP_DESC] = "Vanilla: a curva do próprio jogo, com brilho SDR. RenoDRT: o visual do jogo levado até " - "o pico da sua tela.", + [UI_MENU_HDR_TONEMAP_DESC] = "Vanilla: a curva do próprio jogo, com brilho SDR. PsychoV35: a cena por um modelo da " + "resposta do olho, ajustada ao cinza médio e ao tom do jogo, até o pico da sua tela. " + "RenoDRT: o visual SDR do jogo levado até o pico pelo brilho.", [UI_MENU_HDR_EXPOSURE] = "Exposição", [UI_MENU_HDR_EXPOSURE_DESC] = "Brilho de toda a imagem antes da curva de tons; 100 % a deixa como está.", [UI_MENU_HDR_HIGHLIGHTS] = "Realces", @@ -1569,6 +1592,16 @@ static const char *const text_pt_br[UI_TEXT_COUNT] = { [UI_MENU_HDR_FLARE_DESC] = "Ergue as partes mais escuras como um pouco de luz difusa faria; 0 mantém o preto preto.", [UI_MENU_HDR_GRADE] = "Gradação de cor", [UI_MENU_HDR_GRADE_DESC] = "Quanto da gradação de cor do próprio jogo a imagem HDR mantém; 0 é a cena sem ela.", + [UI_MENU_HDR_MIDGRAY] = "Cinza médio", + [UI_MENU_HDR_MIDGRAY_DESC] = "Onde fica o cinza médio da cena. Vanilla: onde a curva do próprio jogo o coloca. " + "Render: no valor da cena, um pouco mais escuro.", + [UI_MENU_HDR_MATCH_SLOPE] = "Igualar inclinação do tom", + [UI_MENU_HDR_MATCH_SLOPE_DESC] = "Dá aos tons médios o contraste da curva do próprio jogo.", + [UI_MENU_HDR_TONE_SLOPE] = "Inclinação do tom", + [UI_MENU_HDR_TONE_SLOPE_DESC] = "Inclinação da curva de tons no cinza médio; 50 a deixa como está.", + [UI_MENU_HDR_HUE_CLIP] = "Emulação do corte de matiz", + [UI_MENU_HDR_HUE_CLIP_DESC] = "Quanto as cores brilhantes viram como onde a imagem SDR do jogo estoura: o fogo " + "para o amarelo e o branco. 0 mantém o matiz delas.", [UI_MENU_SHADOW_SCALE] = "Resolução das sombras", [UI_MENU_SHADOW_SCALE_DESC] = "Sombras mais nítidas. Mais alto exige mais da placa de vídeo.", [UI_MENU_SHADOW_RANGE] = "Alcance das cascatas de sombra", @@ -2206,7 +2239,7 @@ static const char *const text_zh_hans[UI_TEXT_COUNT] = { [UI_MENU_HDR_GAMMA] = "伽马校正", [UI_MENU_HDR_GAMMA_DESC] = "按游戏设计时的观看方式,以 2.2 或 BT.1886(2.4)显示器的效果呈现画面;关闭则保留 sRGB 曲线,暗部更亮。", [UI_MENU_HDR_TONEMAP] = "色调映射", - [UI_MENU_HDR_TONEMAP_DESC] = "Vanilla:游戏自身的曲线,SDR 亮度。RenoDRT:把游戏的观感延伸到显示器的峰值亮度。", + [UI_MENU_HDR_TONEMAP_DESC] = "Vanilla:游戏自身的曲线,SDR 亮度。PsychoV35:场景经过人眼响应模型,与游戏的中灰和影调对齐,直到显示器的峰值亮度。RenoDRT:按亮度把游戏的 SDR 观感延伸到峰值。", [UI_MENU_HDR_EXPOSURE] = "曝光", [UI_MENU_HDR_EXPOSURE_DESC] = "色调映射前整个画面的亮度;100 % 保持不变。", [UI_MENU_HDR_HIGHLIGHTS] = "高光", @@ -2225,6 +2258,14 @@ static const char *const text_zh_hans[UI_TEXT_COUNT] = { [UI_MENU_HDR_FLARE_DESC] = "像少量杂散光一样提亮最暗的部分;0 保持纯黑。", [UI_MENU_HDR_GRADE] = "调色", [UI_MENU_HDR_GRADE_DESC] = "HDR 画面保留游戏自身调色的程度;0 为不含调色的场景。", + [UI_MENU_HDR_MIDGRAY] = "中灰", + [UI_MENU_HDR_MIDGRAY_DESC] = "场景中灰落在哪里。Vanilla:游戏自身曲线给出的位置。Render:场景的原值,稍暗。", + [UI_MENU_HDR_MATCH_SLOPE] = "匹配影调斜率", + [UI_MENU_HDR_MATCH_SLOPE_DESC] = "让中间调具有游戏自身曲线的对比度。", + [UI_MENU_HDR_TONE_SLOPE] = "影调斜率", + [UI_MENU_HDR_TONE_SLOPE_DESC] = "影调曲线在中灰处的陡峭程度;50 保持不变。", + [UI_MENU_HDR_HUE_CLIP] = "色调裁切模拟", + [UI_MENU_HDR_HUE_CLIP_DESC] = "明亮色彩像游戏 SDR 画面裁切处那样偏转的程度:火焰偏向黄色和白色。0 保持其色调。", [UI_MENU_SHADOW_SCALE] = "阴影分辨率", [UI_MENU_SHADOW_SCALE_DESC] = "让阴影更清晰。越高越吃显卡。", [UI_MENU_SHADOW_RANGE] = "阴影级联范围", diff --git a/runtime/ui_text.h b/runtime/ui_text.h index 7d3262b0..668e9b3f 100644 --- a/runtime/ui_text.h +++ b/runtime/ui_text.h @@ -60,6 +60,8 @@ typedef enum { UI_MENU_HDR_SATURATION, UI_MENU_HDR_SATURATION_DESC, UI_MENU_HDR_HIGHLIGHT_SAT, UI_MENU_HDR_HIGHLIGHT_SAT_DESC, UI_MENU_HDR_BLOWOUT, UI_MENU_HDR_BLOWOUT_DESC, UI_MENU_HDR_FLARE, UI_MENU_HDR_FLARE_DESC, UI_MENU_HDR_GRADE, UI_MENU_HDR_GRADE_DESC, + UI_MENU_HDR_MIDGRAY, UI_MENU_HDR_MIDGRAY_DESC, UI_MENU_HDR_MATCH_SLOPE, UI_MENU_HDR_MATCH_SLOPE_DESC, + UI_MENU_HDR_TONE_SLOPE, UI_MENU_HDR_TONE_SLOPE_DESC, UI_MENU_HDR_HUE_CLIP, UI_MENU_HDR_HUE_CLIP_DESC, UI_MENU_SHADOW_SCALE, UI_MENU_SHADOW_SCALE_DESC, UI_MENU_SHADOW_RANGE, UI_MENU_SHADOW_RANGE_DESC, UI_MENU_CONTACT_SHADOWS, UI_MENU_CONTACT_SHADOWS_DESC, diff --git a/runtime/vk.h b/runtime/vk.h index d31c27c8..7153b63f 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -203,9 +203,9 @@ VkImageView vk_hdr_ui_target(VkCommandBuffer cb, uint32_t w, uint32_t h); void vk_hdr_ui_compose(VkCommandBuffer cb, VkImageView dst_view, VkFormat fmt, uint32_t w, uint32_t h); void vk_hdr_to_srgb8(const uint8_t *in, uint8_t *out, size_t px, int a2r); void vk_hdr_release(void); // before the device goes away -// The support buffer's HDR settings, four vec4 at 1440 (vk_exec_draw.c support_fill; read by the game's tonemap with +// The support buffer's HDR settings, five vec4 at 1440 (vk_exec_draw.c support_fill; read by the game's tonemap with // shaders/hdr_tonemap.frag spliced in, vk_hdr.c tonemap_splice). -void vk_hdr_support(float out[16]); +void vk_hdr_support(float out[20]); // vk_contact.c: screen-space contact shadows on the game's composed scene colour (NX2NAVI_CONTACT_SHADOWS) int vk_contact_strength(void); // percent, 0 = off void vk_contact_release(void); // before the device goes away diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 98a5da9b..6e0764de 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -36,8 +36,8 @@ #define MAX_DESC 1024 #define CHUNK_BYTES (4u << 20) #define ZERO_BYTES 65536u -#define SUPPORT_BYTES 1504u // the translator's SupportBuffer (std140): alpha_test, is_bgra[8], viewport_inverse, viewport_size, ...; - // 1408 pcss and 1424 shadow_spread (translator patches 0010, 0011), 1440..1503 HDR (support_fill) +#define SUPPORT_BYTES 1520u // the translator's SupportBuffer (std140): alpha_test, is_bgra[8], viewport_inverse, viewport_size, ...; + // 1408 pcss and 1424 shadow_spread (translator patches 0010, 0011), 1440..1519 HDR (support_fill) #define NVN_STAGE_VS 0 #define NVN_STAGE_FS 1 #define NVN_STAGE_CS 5 @@ -2819,7 +2819,7 @@ static void support_fill(uint8_t *b, float w, float h, float point_size) { memcpy(b + 1424, shadow_spread_params(), 16); // shadow_spread (translator patch 0011): sun shadow filter width for (int i = 0; i < 73; i++) memcpy(b + 192 + 16 * i, &one, 4); // render scale 1: the host renders at guest size if (vk_hdr_armed()) { // read by the game's tonemap only, once vk_hdr.c tonemap_splice has grown its block past the translator's - float hdr[16]; + float hdr[20]; vk_hdr_support(hdr); memcpy(b + 1440, hdr, sizeof hdr); } diff --git a/runtime/vk_hdr.c b/runtime/vk_hdr.c index 1b0d7962..0ad2eecc 100644 --- a/runtime/vk_hdr.c +++ b/runtime/vk_hdr.c @@ -2,7 +2,9 @@ // display's range. Settings: NX2NAVI_HDR, off and the tone mapper following the menu during a run (vk_hdr_wanted), the // game's own HDR (native) from the start only; the nits NX2NAVI_HDR_PEAK_NITS (the display's peak), // NX2NAVI_HDR_GAME_NITS (the picture's white, "paper white") and NX2NAVI_HDR_UI_NITS (the HUD's and this menu's white); -// NX2NAVI_HDR_GAMMA, NX2NAVI_HDR_TONEMAP and the grading sliders NX2NAVI_HDR_EXPOSURE .. NX2NAVI_HDR_GRADE, all read +// NX2NAVI_HDR_TONEMAP with what each tone mapper takes (NX2NAVI_HDR_MIDGRAY, NX2NAVI_HDR_MATCH_SLOPE, +// NX2NAVI_HDR_TONE_SLOPE and NX2NAVI_HDR_HUE_CLIP, or NX2NAVI_HDR_GAMMA and NX2NAVI_HDR_HIGHLIGHT_SAT .. +// NX2NAVI_HDR_GRADE) and the grading sliders NX2NAVI_HDR_EXPOSURE .. NX2NAVI_HDR_SATURATION of both, all read // when the settings change (vk_hdr_support). With NX2NAVI_HDR=0 the swapchain is SDR, the spliced tonemap gives the // game's own SDR picture (vk_hdr_support, p0.w = -1) and the present is what it was. // @@ -12,8 +14,9 @@ // 1. vk_hdr_shader_swap, on each fragment shader as it loads (in memory only; the cache keeps the translator's): 1.1.1's // tonemap ends in the fitted ACES curve and a clamp to 0..1 per channel. tonemap_splice links the function // nx_tonemap of shaders/hdr_tonemap.frag into that shader and stores its result in place of the three clamped -// channels: the game's SDR result upgraded to the HDR range and mapped onto the display (that file says how), with -// the settings in four vec4 the support buffer gains at 1440. Recognised by the curve's constants, not by a hash, so +// channels: the scene mapped onto the display by the tone mapper chosen (that file says how each does it), from the +// values the curve takes and the scene before the game's exposure gain, with the settings in five vec4 the support +// buffer gains at 1440. Recognised by the curve's constants, not by a hash, so // another translator still finds it. The game's FSR 1 upscale after it would clamp the picture to 1 again; // fsr_unclamp drops that clamp in its SPIR-V. // 2. vk_hdr_draw / vk_hdr_present_source: a draw into a presented window texture that samples one float texture of @@ -103,7 +106,7 @@ void vk_hdr_off(void) { g_native = 0, g_armed = 0; } typedef struct { float peak, game, ui; // nits float gamma; // 0 none, 1: 2.2, 2: 2.4 (the present's correction, hdr_encode.comp) - float tm[16]; // the support buffer's four vec4 (shaders/hdr_tonemap.frag p0..p3) + float tm[20]; // the support buffer's five vec4 (shaders/hdr_tonemap.frag p0..p4) } Nits; static struct { unsigned seen; int cur; Nits snap[2]; PlatMutex lock; } N = {.seen = ~0u, .lock = PLAT_MUTEX_INIT}; @@ -118,23 +121,32 @@ static Nits nits(void) { n->peak = clampf((float)cfg_double("NX2NAVI_HDR_PEAK_NITS"), 80.0f, 10000.0f); n->game = clampf((float)cfg_double("NX2NAVI_HDR_GAME_NITS"), 48.0f, 500.0f); n->ui = clampf((float)cfg_double("NX2NAVI_HDR_UI_NITS"), 48.0f, 500.0f); + // the tone mapper as shaders/hdr_tonemap.frag numbers it: the game's curve, the luminance mapper, the + // observer-response mapper + const char *tm = cfg_value("NX2NAVI_HDR_TONEMAP"); + float mapper = !strcmp(tm, "vanilla") ? 0.0f : !strcmp(tm, "renodrt") ? 3.0f : 9.0f; + // the gamma correction belongs to the luminance mapper: the other two give the picture as it is encoded const char *g = cfg_value("NX2NAVI_HDR_GAMMA"); - n->gamma = !strcmp(g, "2.2") ? 1.0f : !strcmp(g, "2.4") ? 2.0f : 0.0f; - // the sliders' 0..100 with 50 unchanged become factors around 1; blowout and hue are fractions - float pct[8]; - static const char *const keys[8] = {"NX2NAVI_HDR_HIGHLIGHTS", "NX2NAVI_HDR_SHADOWS", "NX2NAVI_HDR_CONTRAST", - "NX2NAVI_HDR_SATURATION", "NX2NAVI_HDR_HIGHLIGHT_SAT", "NX2NAVI_HDR_BLOWOUT", - "NX2NAVI_HDR_FLARE", "NX2NAVI_HDR_HUE"}; - for (int i = 0; i < 8; i++) pct[i] = clampf((float)cfg_double(keys[i]), 0.0f, 100.0f); + n->gamma = mapper != 3.0f ? 0.0f : !strcmp(g, "2.2") ? 1.0f : !strcmp(g, "2.4") ? 2.0f : 0.0f; + // the sliders' 0..100 with 50 unchanged become factors around 1; blowout, hue and hue clip are fractions + float pct[10]; + static const char *const keys[10] = {"NX2NAVI_HDR_HIGHLIGHTS", "NX2NAVI_HDR_SHADOWS", "NX2NAVI_HDR_CONTRAST", + "NX2NAVI_HDR_SATURATION", "NX2NAVI_HDR_HIGHLIGHT_SAT", "NX2NAVI_HDR_BLOWOUT", + "NX2NAVI_HDR_FLARE", "NX2NAVI_HDR_HUE", "NX2NAVI_HDR_TONE_SLOPE", + "NX2NAVI_HDR_HUE_CLIP"}; + for (int i = 0; i < 10; i++) pct[i] = clampf((float)cfg_double(keys[i]), 0.0f, 100.0f); float *t = n->tm; t[0] = n->peak, t[1] = n->game, t[2] = n->ui; - t[3] = !strcmp(cfg_value("NX2NAVI_HDR_TONEMAP"), "vanilla") ? 0.0f : 3.0f; + t[3] = mapper; t[4] = clampf((float)cfg_double("NX2NAVI_HDR_EXPOSURE"), 0.0f, 200.0f) / 100.0f; t[5] = pct[0] * 0.02f, t[6] = pct[1] * 0.02f, t[7] = pct[2] * 0.02f; t[8] = pct[3] * 0.02f, t[9] = pct[4] * 0.02f, t[10] = pct[5] * 0.01f, t[11] = pct[6] * 0.02f; t[12] = pct[7] * 0.01f; t[13] = clampf((float)cfg_double("NX2NAVI_HDR_GRADE"), 0.0f, 100.0f) / 100.0f; - t[14] = n->gamma, t[15] = 0; + t[14] = n->gamma, t[15] = pct[8] * 0.02f; + t[16] = pct[9] * 0.01f; + t[17] = !strcmp(cfg_value("NX2NAVI_HDR_MIDGRAY"), "render") ? 0.0f : 1.0f; + t[18] = cfg_bool("NX2NAVI_HDR_MATCH_SLOPE") ? 1.0f : 0.0f, t[19] = 0; __atomic_store_n(&N.cur, !N.cur, __ATOMIC_RELEASE); __atomic_store_n(&N.seen, v, __ATOMIC_RELEASE); } @@ -143,7 +155,7 @@ static Nits nits(void) { return N.snap[__atomic_load_n(&N.cur, __ATOMIC_ACQUIRE)]; } -void vk_hdr_support(float out[16]) { +void vk_hdr_support(float out[20]) { Nits n = nits(); memcpy(out, n.tm, sizeof n.tm); if (!vk_hdr_active()) out[3] = -1.0f; // no HDR10 swapchain (yet): the game's own SDR picture @@ -159,7 +171,7 @@ enum { SPV_OP_NAME = 5, SPV_OP_EXT_INST_IMPORT = 11, SPV_OP_EXT_INST = 12, SPV_O SPV_OP_FUNCTION_PARAMETER = 55, SPV_OP_FUNCTION_END = 56, SPV_OP_FUNCTION_CALL = 57, SPV_OP_VARIABLE = 59, SPV_OP_LOAD = 61, SPV_OP_STORE = 62, SPV_OP_ACCESS_CHAIN = 65, SPV_OP_DECORATE = 71, SPV_OP_MEMBER_DECORATE = 72, SPV_OP_VECTOR_SHUFFLE = 79, SPV_OP_COMPOSITE_CONSTRUCT = 80, SPV_OP_COMPOSITE_EXTRACT = 81, - SPV_OP_COMPOSITE_INSERT = 82, SPV_OP_FMUL = 133, SPV_OP_FDIV = 136, SPV_OP_LOOP_MERGE = 246, + SPV_OP_COMPOSITE_INSERT = 82, SPV_OP_FADD = 129, SPV_OP_FMUL = 133, SPV_OP_FDIV = 136, SPV_OP_LOOP_MERGE = 246, SPV_OP_SELECTION_MERGE = 247, SPV_OP_LABEL = 248, SPV_OP_BRANCH_CONDITIONAL = 250, SPV_OP_SWITCH = 251, SPV_OP_LINE = 8, SPV_OP_NO_LINE = 317, SPV_DEC_OFFSET = 35, SPV_SC_UNIFORM = 2, SPV_SC_OUTPUT = 3, SPV_SC_FUNCTION = 7, SPV_CAP_SHADER = 1, GLSL_FMA = 50, GLSL_FMAX = 40, GLSL_FCLAMP = 43, GLSL_NMAX = 80, @@ -286,13 +298,58 @@ static int put_mapped(SpvOut *o, const uint32_t *ins, const uint32_t *map, uint3 static int is_type_op(uint32_t op) { return op >= 19 && op <= 39; } static int is_const_op(uint32_t op) { return op >= SPV_OP_CONSTANT_TRUE && op <= SPV_OP_CONSTANT_NULL && op != 45; } +// What tonemap_splice knows of the game's module while it looks for the exposure: where each result is defined +// (def), the class of each variable (cls: 1 output, 2 uniform, 3 function), and for a function variable the value +// its stores gave it and how many there were (sval, scnt). +typedef struct { const uint32_t *w, *def, *sval; const uint8_t *cls, *scnt; uint32_t bound; } SpvView; + +// The value behind id when id loads a function variable that is stored once (the translator keeps every value in +// one); id itself otherwise. +static uint32_t spv_stored(const SpvView *v, uint32_t id) { + if (id >= v->bound || !v->def[id]) return id; + const uint32_t *l = v->w + v->def[id]; + if ((l[0] & 0xffff) != SPV_OP_LOAD || l[3] >= v->bound || v->cls[l[3]] != 3 || v->scnt[l[3]] != 1) return id; + return v->sval[l[3]]; +} + +static int spv_uniform_load(const SpvView *v, uint32_t id) { // id loads a member of a uniform block + if (id >= v->bound || !v->def[id]) return 0; + const uint32_t *l = v->w + v->def[id]; + if ((l[0] & 0xffff) != SPV_OP_LOAD || l[3] >= v->bound || !v->def[l[3]]) return 0; + const uint32_t *c = v->w + v->def[l[3]]; + return (c[0] & 0xffff) == SPV_OP_ACCESS_CHAIN && c[3] < v->bound && v->cls[c[3]] == 2; +} + +static int spv_mul_by_uniform(const SpvView *v, uint32_t id, uint32_t *other, uint32_t *uniform) { + if (id >= v->bound || !v->def[id]) return 0; + const uint32_t *m = v->w + v->def[id]; + if ((m[0] & 0xffff) != SPV_OP_FMUL) return 0; + if (spv_uniform_load(v, m[4])) { *other = m[3], *uniform = m[4]; return 1; } + if (spv_uniform_load(v, m[3])) { *other = m[4], *uniform = m[3]; return 1; } + return 0; +} + +// The game's exposure in x, a value it feeds its curve: x = scene * gain, the gain a uniform (the tonemap's constants, +// element 3 .z), or in the gameplay shader x = (scene * gain) * local exposure, the local exposure computed from a +// grid. 1 with the scene before the gain and the gain's load. +static int spv_exposure(const SpvView *v, uint32_t x, uint32_t *scene, uint32_t *gain) { + uint32_t val = spv_stored(v, x); + if (spv_mul_by_uniform(v, val, scene, gain)) return 1; + if (val >= v->bound || !v->def[val] || (v->w[v->def[val]] & 0xffff) != SPV_OP_FMUL) return 0; + const uint32_t *m = v->w + v->def[val]; + return spv_mul_by_uniform(v, spv_stored(v, m[3]), scene, gain) || spv_mul_by_uniform(v, spv_stored(v, m[4]), scene, gain); +} + // The fitted ACES curve the game ends its tonemap with: FClamp/NClamp(FMul(FMul(x, Fma(x, 2.51, 0.03)), rcp), 0, 1) // per channel, each result stored to a function variable that the output store of its channel loads. tonemap_splice -// keeps everything the game computes and only changes what the three colour channels store: nx_tonemap of the three x -// (shaders/hdr_tonemap.frag), called just before the first of those stores, with the settings from four vec4 the -// support buffer gains at 1440 (vk_exec_draw.c support_fill writes them; the translator's SupportBuffer ends there). -// The library's types are matched to the game's where the same (SPIR-V allows one declaration of each), its constants -// and functions appended with new ids. Gives up (0) on any shape it does not expect, and the shader stays as it was. +// keeps everything the game computes and only changes what the three colour channels store: nx_tonemap +// (shaders/hdr_tonemap.frag), called just before the first of those stores, of the three x, of the scene they come +// from before the exposure gain with that gain (spv_exposure; x and a gain of 1 when the shape is another), and of the +// settings from the HDR_SETS vec4 the support buffer gains at 1440 (vk_exec_draw.c support_fill writes them; the +// translator's SupportBuffer ends there). The library's types are matched to the game's where the same (SPIR-V allows +// one declaration of each), its constants and functions appended with new ids. Gives up (0) on any shape it does not +// expect, and the shader stays as it was. +enum { HDR_SETS = 5, HDR_PARAMS = HDR_SETS + 2 }; // nx_tonemap(vec3 x, vec4 scene, vec4 settings[HDR_SETS]) static int tonemap_splice(uint32_t **spirv, uint64_t *words) { uint32_t *w = *spirv; uint64_t n = *words; @@ -307,8 +364,10 @@ static int tonemap_splice(uint32_t **spirv, uint64_t *words) { int nfn = 0; uint32_t *def = calloc(bound, sizeof *def); // result id -> word index of its instruction int32_t *cval = calloc(bound, sizeof *cval); // integer constant id -> value + 1 - uint8_t *outv = calloc(bound, 1); // 1: an Output variable - if (!def || !cval || !outv) goto fail; + uint8_t *cls = calloc(bound, 1); // a variable's class: 1 output, 2 uniform, 3 function + uint32_t *sval = calloc(bound, sizeof *sval); // variable -> the value last stored to it + uint8_t *scnt = calloc(bound, 1); // variable -> its stores, counted to 2 + if (!def || !cval || !cls || !sval || !scnt) goto fail; for (uint64_t i = 5; i < n;) { uint32_t op = w[i] & 0xffff, len = w[i] >> 16; if (!len || i + len > n) goto fail; @@ -329,9 +388,10 @@ static int tonemap_splice(uint32_t **spirv, uint64_t *words) { else if (op == SPV_OP_LABEL && !first_label) first_label = i; if (op == SPV_OP_VARIABLE && len >= 4 && a[1] < bound) { if (a[1] == sb && sb) sb_ptr = a[0]; - if (a[2] == SPV_SC_OUTPUT) outv[a[1]] = 1; + cls[a[1]] = a[2] == SPV_SC_OUTPUT ? 1 : a[2] == SPV_SC_UNIFORM ? 2 : a[2] == SPV_SC_FUNCTION ? 3 : 0; } - if (len >= 3 && a[1] < bound && (op == SPV_OP_FMUL || op == SPV_OP_FDIV || op == SPV_OP_EXT_INST || + if (op == SPV_OP_STORE && len >= 3 && a[0] < bound) sval[a[0]] = a[1], scnt[a[0]] += scnt[a[0]] < 2; + if (len >= 3 && a[1] < bound && (op == SPV_OP_FMUL || op == SPV_OP_FDIV || op == SPV_OP_FADD || op == SPV_OP_EXT_INST || op == SPV_OP_LOAD || op == SPV_OP_ACCESS_CHAIN || op == SPV_OP_VARIABLE)) def[a[1]] = (uint32_t)i; i += len; @@ -380,7 +440,7 @@ static int tonemap_splice(uint32_t **spirv, uint64_t *words) { const uint32_t *a = w + i + 1; if ((w[i] & 0xffff) != SPV_OP_STORE || (w[i] >> 16) < 3 || a[0] >= bound || !def[a[0]]) continue; const uint32_t *ch = w + def[a[0]]; - if ((ch[0] & 0xffff) != SPV_OP_ACCESS_CHAIN || (ch[0] >> 16) != 5 || ch[3] >= bound || !outv[ch[3]] || + if ((ch[0] & 0xffff) != SPV_OP_ACCESS_CHAIN || (ch[0] >> 16) != 5 || ch[3] >= bound || cls[ch[3]] != 1 || ch[4] >= bound || cval[ch[4]] < 1 || cval[ch[4]] > 3) continue; int c = cval[ch[4]] - 1; @@ -406,6 +466,29 @@ static int tonemap_splice(uint32_t **spirv, uint64_t *words) { if (site[chan_site[c]] >= at || def[site_x[chan_site[c]]] >= at) goto fail; for (uint64_t i = lo; i < hi; i += w[i] >> 16) if ((w[i] & 0xffff) == SPV_OP_LABEL) goto fail; + // the scene before the exposure gain, and the gain: usable at the call when defined in the function's first block + // (it comes before every other) or in the call's own + const SpvView view = {w, def, sval, cls, scnt, bound}; + uint32_t scene[3], gain = 0; + uint64_t at_block = first_label, second_label = n; + for (uint64_t i = first_label + (w[first_label] >> 16); i < at; i += w[i] >> 16) + if ((w[i] & 0xffff) == SPV_OP_LABEL) { + at_block = i; + if (second_label == n) second_label = i; + } + int exposure_found = 1; + for (int c = 0; c < 3 && exposure_found; c++) { + uint32_t g = 0; + exposure_found = spv_exposure(&view, site_x[chan_site[c]], &scene[c], &g); + uint64_t ds = exposure_found && scene[c] < bound ? def[scene[c]] : 0, dg = exposure_found && g < bound ? def[g] : 0; + if (!ds || !dg || !(ds < second_label || ds > at_block) || !(dg < second_label || dg > at_block) || ds >= at || dg >= at) + exposure_found = 0; + if (c == 0) gain = g; + } + if (!exposure_found) { + for (int c = 0; c < 3; c++) scene[c] = site_x[chan_site[c]]; + gain = c1; + } // the library: its ids mapped into the game's id space const uint32_t *L = hdr_tonemap_spv; @@ -458,13 +541,13 @@ static int tonemap_splice(uint32_t **spirv, uint64_t *words) { for (uint64_t i = 5; i < ln; i += L[i] >> 16) // nx_tonemap's function type if ((L[i] & 0xffff) == SPV_OP_FUNCTION && L[i + 2] == fn) fn_type = L[i + 4]; if (!entry || !fn || !fn_type || !map[fn_type]) goto lib_fail; - // nx_tonemap(vec3, vec4 x 4) with its parameters by pointer, as the compiler writes them + // nx_tonemap(vec3, vec4 x (1 + HDR_SETS)) with its parameters by pointer, as the compiler writes them const uint32_t *ftype = NULL; for (uint64_t i = 5; i < ln && !ftype; i += L[i] >> 16) - if ((L[i] & 0xffff) == SPV_OP_TYPE_FUNCTION && L[i + 1] == fn_type && (L[i] >> 16) == 8) ftype = L + i; + if ((L[i] & 0xffff) == SPV_OP_TYPE_FUNCTION && L[i + 1] == fn_type && (L[i] >> 16) == 3 + HDR_PARAMS) ftype = L + i; if (!ftype) goto lib_fail; - uint32_t param_ptr[5], v3 = map[ftype[2]], v4 = 0, ptr_types_ok = 1; - for (int p = 0; p < 5; p++) { + uint32_t param_ptr[HDR_PARAMS], v3 = map[ftype[2]], v4 = 0, ptr_types_ok = 1; + for (int p = 0; p < HDR_PARAMS; p++) { param_ptr[p] = map[ftype[3 + p]]; uint32_t pointee = 0; for (uint64_t i = 5; i < ln; i += L[i] >> 16) @@ -503,9 +586,10 @@ static int tonemap_splice(uint32_t **spirv, uint64_t *words) { // the result: the game's module with the support block grown, the library's globals before the first function, // the call in main, the three stores changed, and the library's functions at the end - uint32_t idx[4], ptr_v4 = ptr_v4u ? ptr_v4u : id++, var[5], pv[4], pl[4], xv = id++, res = id++, e[3]; - for (int m = 0; m < 4; m++) idx[m] = id++, pv[m] = id++, pl[m] = id++; - for (int p = 0; p < 5; p++) var[p] = id++; + uint32_t idx[HDR_SETS], ptr_v4 = ptr_v4u ? ptr_v4u : id++, var[HDR_PARAMS], pv[HDR_SETS], pl[HDR_SETS], xv = id++, + sv = id++, res = id++, e[3]; + for (int m = 0; m < HDR_SETS; m++) idx[m] = id++, pv[m] = id++, pl[m] = id++; + for (int p = 0; p < HDR_PARAMS; p++) var[p] = id++; for (int c = 0; c < 3; c++) e[c] = id++; put(&o, w, 5); for (uint64_t i = 5; i < n;) { @@ -513,33 +597,37 @@ static int tonemap_splice(uint32_t **spirv, uint64_t *words) { if (i == first_fn) { put(&o, g.w, (uint32_t)g.n); if (!ptr_v4u) PUT(&o, SPV_OP_TYPE_POINTER, ptr_v4, SPV_SC_UNIFORM, st_v4); - for (int m = 0; m < 4; m++) PUT(&o, SPV_OP_CONSTANT, it, idx[m], 11 + m); + for (int m = 0; m < HDR_SETS; m++) PUT(&o, SPV_OP_CONSTANT, it, idx[m], 11 + m); } - if (i == st_at) { // the support block gains four vec4 at 1440 + if (i == st_at) { // the support block gains HDR_SETS vec4 at 1440 put(&o, w + i, len); - o.w[o.n - len] = (len + 4) << 16 | op; - for (int m = 0; m < 4; m++) put(&o, &st_v4, 1); + o.w[o.n - len] = (len + HDR_SETS) << 16 | op; + for (int m = 0; m < HDR_SETS; m++) put(&o, &st_v4, 1); } else if (i == dec_at) { put(&o, w + i, len); - for (int m = 0; m < 4; m++) PUT(&o, SPV_OP_MEMBER_DECORATE, st, 11 + m, SPV_DEC_OFFSET, 1440 + 16 * m); + for (int m = 0; m < HDR_SETS; m++) PUT(&o, SPV_OP_MEMBER_DECORATE, st, 11 + m, SPV_DEC_OFFSET, 1440 + 16 * m); } else if (i == first_label) { put(&o, w + i, len); - for (int p = 0; p < 5; p++) PUT(&o, SPV_OP_VARIABLE, param_ptr[p], var[p], SPV_SC_FUNCTION); + for (int p = 0; p < HDR_PARAMS; p++) PUT(&o, SPV_OP_VARIABLE, param_ptr[p], var[p], SPV_SC_FUNCTION); } else if (i == store_at[0] || i == store_at[1] || i == store_at[2]) { int c = i == store_at[0] ? 0 : i == store_at[1] ? 1 : 2; put(&o, w + i, len); o.w[o.n - len + 2] = e[c]; } else { if (i == at) { - for (int m = 0; m < 4; m++) { + for (int m = 0; m < HDR_SETS; m++) { PUT(&o, SPV_OP_ACCESS_CHAIN, ptr_v4, pv[m], sb, idx[m]); PUT(&o, SPV_OP_LOAD, st_v4, pl[m], pv[m]); } PUT(&o, SPV_OP_COMPOSITE_CONSTRUCT, v3, xv, site_x[chan_site[0]], site_x[chan_site[1]], site_x[chan_site[2]]); + PUT(&o, SPV_OP_COMPOSITE_CONSTRUCT, st_v4, sv, scene[0], scene[1], scene[2], gain); PUT(&o, SPV_OP_STORE, var[0], xv); - for (int m = 0; m < 4; m++) PUT(&o, SPV_OP_STORE, var[m + 1], pl[m]); - PUT(&o, SPV_OP_FUNCTION_CALL, v3, res, map[fn], var[0], var[1], var[2], var[3], var[4]); + PUT(&o, SPV_OP_STORE, var[1], sv); + for (int m = 0; m < HDR_SETS; m++) PUT(&o, SPV_OP_STORE, var[m + 2], pl[m]); + uint32_t call[4 + HDR_PARAMS] = {(4u + HDR_PARAMS) << 16 | SPV_OP_FUNCTION_CALL, v3, res, map[fn]}; + memcpy(call + 4, var, sizeof var); + put(&o, call, 4 + HDR_PARAMS); for (int c = 0; c < 3; c++) PUT(&o, SPV_OP_COMPOSITE_EXTRACT, ft, e[c], res, c); } put(&o, w + i, len); @@ -549,15 +637,15 @@ static int tonemap_splice(uint32_t **spirv, uint64_t *words) { put(&o, f.w, (uint32_t)f.n); if (o.bad) goto lib_fail; o.w[3] = id; - free(map), free(g.w), free(f.w), free(def), free(cval), free(outv); + free(map), free(g.w), free(f.w), free(def), free(cval), free(cls), free(sval), free(scnt); free(*spirv); *spirv = o.w; *words = o.n; - return 1; + return exposure_found ? 1 : 2; lib_fail: free(map), free(g.w), free(f.w), free(o.w); fail: - free(def), free(cval), free(outv); + free(def), free(cval), free(cls), free(sval), free(scnt); return 0; } #undef PUT @@ -565,10 +653,11 @@ fail: void vk_hdr_shader_swap(uint32_t **spirv, uint64_t *words) { // armed, not wanted: a shader loaded while HDR is off must be ready for the menu switching it on if (!vk_hdr_armed() || vk_hdr_native() || !*spirv || *words < 5) return; - if (tonemap_splice(spirv, words)) { - static volatile int32_t spliced; - rt_log("hdr: the game's tonemap maps its picture onto the display's range (%d shaders so far)", - plat_inc32(&spliced)); + int spliced = tonemap_splice(spirv, words); + if (spliced) { + static volatile int32_t count; + rt_log("hdr: the game's tonemap maps its picture onto the display's range (%d shaders so far)%s", + plat_inc32(&count), spliced == 2 ? "; its exposure was not found, so the tone mapper takes it as 1" : ""); } int unclamped = fsr_unclamp(*spirv, words); if (unclamped > 0) {