mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-19 04:19:57 -04:00
Replace the enhanced HDR with a port of RenoDX's tone mapper.
The game's ACES tonemap shader gets a GLSL function linked into its SPIR-V where it clamps each channel: the game's SDR result upgraded to the HDR range by luminance and mapped to the display with the RenoDRT curve, exposure, highlights, shadows, contrast, saturation, highlight saturation, blowout, flare and hue correction. The settings reach it as four vec4 the support buffer gains at 1440. The present encode is a port of the swap chain pass: gamma correction, gamut compression, the peak clamp and PQ. Without an HDR10 swapchain the shader gives the game's own SDR picture; native HDR is unchanged. The menu offers Off, RenoDX and Native, with gamma correction, the tone mapper and its grading rows. The shader build compiles sources marked "keep functions" without optimisation so their functions survive for the splice. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,29 @@
|
||||
#version 450
|
||||
//
|
||||
// Copyright (c) 2025 Carlos Lopez Jr.
|
||||
//
|
||||
// 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
|
||||
// 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.
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// HDR10 encode on the present (runtime/vk_hdr.c vk_hdr_encode): the game's picture, crop rectangle at offset, linear
|
||||
// Rec. 709 with the HUD's white at 1.0 (an sRGB view decodes the 8-bit window texture), into an RGBA16F image of PQ
|
||||
// code values in Rec. 2020 that FSR 1 or the blit then scale into the HDR10 swapchain. p.x: nits of 1.0, p.y: the
|
||||
// display's peak in nits, p.z: 1 when the source holds sRGB-encoded values in a UNORM format.
|
||||
// PQ: SMPTE ST 2084; the Rec. 709 to Rec. 2020 matrix: ITU-R BT.2087.
|
||||
// code values in Rec. 2020 that FSR 1 or the blit then scale into the HDR10 swapchain. In order: the gamma correction
|
||||
// (the game's sRGB-encoded look shown as a 2.2 or 2.4 display would: decode of the sRGB code with that power), the
|
||||
// conversion to Rec. 2020, a gamut compression that desaturates towards the grey of the same luminance until no
|
||||
// channel is negative (in a gamma-encoded space whose 50% is the scene's mid grey), the HUD's white in nits, the
|
||||
// largest channel scaled down to the peak, and PQ (SMPTE ST 2084).
|
||||
// p.x: nits of 1.0, p.y: the display's peak in nits, p.z: 1 when the source holds sRGB-encoded values in a UNORM
|
||||
// format, p.w: gamma correction (0 none, 1: 2.2, 2: 2.4).
|
||||
layout(local_size_x = 8, local_size_y = 8) in;
|
||||
layout(binding = 0) uniform sampler2D src;
|
||||
layout(binding = 1, rgba16f) uniform writeonly image2D dst;
|
||||
@@ -13,10 +33,24 @@ layout(push_constant) uniform P {
|
||||
vec4 p;
|
||||
} pc;
|
||||
|
||||
vec3 srgb_decode(vec3 c) {
|
||||
return mix(c / 12.92, pow((c + 0.055) / 1.055, vec3(2.4)), greaterThan(c, vec3(0.04045)));
|
||||
vec3 copy_sign(vec3 mag, vec3 s) {
|
||||
vec3 a = abs(mag);
|
||||
return mix(a, -a, lessThan(s, vec3(0.0)));
|
||||
}
|
||||
|
||||
vec3 sign_pow(vec3 x, float e) { return copy_sign(pow(abs(x), vec3(e)), x); }
|
||||
|
||||
vec3 srgb_decode(vec3 c) {
|
||||
return mix(pow((c + 0.055) / 1.055, vec3(2.4)), c / 12.92, lessThanEqual(c, vec3(0.04045)));
|
||||
}
|
||||
|
||||
vec3 srgb_encode(vec3 c) {
|
||||
return mix(1.055 * pow(c, vec3(1.0 / 2.4)) - 0.055, c * 12.92, lessThanEqual(c, vec3(0.0031308)));
|
||||
}
|
||||
|
||||
// The colour an sRGB code shows on a display of power g, sign kept.
|
||||
vec3 gamma_correct(vec3 c, float g) { return copy_sign(pow(srgb_encode(abs(c)), vec3(g)), c); }
|
||||
|
||||
vec3 pq(vec3 nits) {
|
||||
const float m1 = 0.1593017578125, m2 = 78.84375, c1 = 0.8359375, c2 = 18.8515625, c3 = 18.6875;
|
||||
vec3 y = pow(clamp(nits / 10000.0, 0.0, 1.0), vec3(m1));
|
||||
@@ -26,9 +60,25 @@ vec3 pq(vec3 nits) {
|
||||
void main() {
|
||||
ivec2 at = ivec2(gl_GlobalInvocationID.xy);
|
||||
if (any(greaterThanEqual(at, pc.size))) return;
|
||||
vec3 c = max(texelFetch(src, at + pc.offset, 0).rgb, vec3(0.0));
|
||||
vec3 c = texelFetch(src, at + pc.offset, 0).rgb;
|
||||
if (pc.p.z > 0.5) c = srgb_decode(clamp(c, 0.0, 1.0));
|
||||
const mat3 to2020 = mat3(0.6274, 0.0691, 0.0164, 0.3293, 0.9195, 0.0880, 0.0433, 0.0114, 0.8956);
|
||||
vec3 nits = min(max(to2020 * c, vec3(0.0)) * pc.p.x, vec3(pc.p.y));
|
||||
imageStore(dst, at, vec4(pq(nits), 1.0));
|
||||
c = mix(c, vec3(0.0), isnan(c));
|
||||
if (pc.p.w == 1.0) c = gamma_correct(c, 2.2);
|
||||
else if (pc.p.w == 2.0) c = gamma_correct(c, 2.4);
|
||||
// Rec. 709 to Rec. 2020 (rows)
|
||||
c = c * mat3(0.6274038960, 0.3292830384, 0.0433130657,
|
||||
0.0690972894, 0.9195403951, 0.0113623156,
|
||||
0.0163914388, 0.0880133078, 0.8955952532);
|
||||
float gray = dot(c, vec3(0.2627002120, 0.6779980715, 0.0593017165));
|
||||
const float MID_GRAY_GAMMA = log(1.0 / pow(10.0, 0.75)) / log(0.5); // about 2.49: 18% grey encodes to 50%
|
||||
vec3 encoded = sign_pow(c, 1.0 / MID_GRAY_GAMMA);
|
||||
float encoded_gray = pow(max(gray, 0.0), 1.0 / MID_GRAY_GAMMA);
|
||||
float lowest = min(0.0, min(encoded.r, min(encoded.g, encoded.b)));
|
||||
float distance = encoded_gray - lowest;
|
||||
float ratio = distance == 0.0 ? 0.0 : -lowest / distance;
|
||||
c = sign_pow(mix(vec3(encoded_gray), encoded, 1.0 - ratio), MID_GRAY_GAMMA);
|
||||
c *= pc.p.x;
|
||||
float max_channel = max(max(max(c.r, c.g), c.b), pc.p.y);
|
||||
c *= pc.p.y / max_channel;
|
||||
imageStore(dst, at, vec4(pq(max(c, vec3(0.0))), 1.0));
|
||||
}
|
||||
|
||||
@@ -1,73 +1,228 @@
|
||||
#version 450
|
||||
// The game's tonemap pass for HDR output (runtime/vk_hdr.c vk_hdr_shader_swap puts it in place of the translated
|
||||
// shader when NX2NAVI_HDR=1). Same inputs as the game's: fp_t_tcb_8 (bloom, clamped to 10000 as the game does) plus
|
||||
// fp_t_tcb_A (the scene), weights in c1[0] and the saturation grade in c3[0]. The game maps luminance and each channel
|
||||
// with 1 - e^-x, blends the two by the luminance curve squared, clamps to 0..1 and grades saturation. Here the curve is
|
||||
// the game's up to PIVOT and a straight line with its slope above, so mid-greys look as in SDR and highlights keep
|
||||
// going; the grade works on the colour divided by its largest channel above 1; a roll-off on the largest channel stops
|
||||
// at the display's peak. The output is relative to the HUD's white (1.0), which the present maps to NX2NAVI_HDR_UI_NITS.
|
||||
// Hue correction (hdr.z, NX2NAVI_HDR_HUE): the game's SDR clamp turns bright saturated colours towards white (a torch flame
|
||||
// clips red, then green, and reads yellow-white, the colour its emitted light was tuned to); the extended curve keeps
|
||||
// their ratios, so they stay saturated. From the picture's white upwards the colour moves towards the game's own SDR
|
||||
// result at the HDR luminance, by up to hdr.z.
|
||||
// The approach: an "extended vanilla" curve.
|
||||
// Bindings are those of the game's translated shader: vk_hdr.c rewrites them from the original's names.
|
||||
layout(location = 0) in vec4 uv;
|
||||
layout(location = 0) out vec4 color;
|
||||
// build_shaders: keep functions
|
||||
//
|
||||
// Copyright (c) 2025 Carlos Lopez Jr.
|
||||
//
|
||||
// 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
|
||||
// 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.
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// 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):
|
||||
// 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)
|
||||
// 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
|
||||
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 = 0) out vec4 o;
|
||||
|
||||
layout(set = 0, binding = 0, std140) uniform SupportBuffer {
|
||||
vec4 pad[90];
|
||||
vec4 hdr; // byte 1440 (vk_exec_draw.c support_fill): game white / HUD white, peak / game white, hue correction
|
||||
} support_buffer;
|
||||
layout(set = 0, binding = 1) uniform sampler2D fp_t_tcb_8;
|
||||
layout(set = 0, binding = 2) uniform sampler2D fp_t_tcb_A;
|
||||
layout(set = 0, binding = 3, std140) uniform C1 { vec4 v[4096]; } fp_c1;
|
||||
layout(set = 0, binding = 4, std140) uniform C3 { vec4 v[4096]; } fp_c3;
|
||||
// Matrices are written row by row; mulm(m, v) is the row-vector product of that layout.
|
||||
vec3 mulm(mat3 m, vec3 v) { return v * m; }
|
||||
|
||||
const float PIVOT = 0.18;
|
||||
const mat3 BT709_TO_AP1 = mat3(0.6130974024, 0.3395231462, 0.0473794514,
|
||||
0.0701937225, 0.9163538791, 0.0134523985,
|
||||
0.0206155929, 0.1095697729, 0.8698146342);
|
||||
const mat3 AP1_TO_BT709 = mat3(1.7050509927, -0.6217921207, -0.0832588720,
|
||||
-0.1302564175, 1.1408047366, -0.0105483191,
|
||||
-0.0240033568, -0.1289689761, 1.1529723329);
|
||||
const mat3 BT709_TO_OKLABLMS = mat3(0.4122214708, 0.5363325363, 0.0514459929,
|
||||
0.2119034982, 0.6806995451, 0.1073969566,
|
||||
0.0883024619, 0.2817188376, 0.6299787005);
|
||||
const mat3 OKLABLMS_TO_OKLAB = mat3(0.2104542553, 0.7936177850, -0.0040720468,
|
||||
1.9779984951, -2.4285922050, 0.4505937099,
|
||||
0.0259040371, 0.7827717662, -0.8086757660);
|
||||
const mat3 OKLAB_TO_OKLABLMS = mat3(1.0, 0.3963377774, 0.2158037573,
|
||||
1.0, -0.1055613458, -0.0638541728,
|
||||
1.0, -0.0894841775, -1.2914855480);
|
||||
const mat3 OKLABLMS_TO_BT709 = mat3(4.0767416621, -3.3077115913, 0.2309699292,
|
||||
-1.2684380046, 2.6097574011, -0.3413193965,
|
||||
-0.0041960863, -0.7034186147, 1.7076147010);
|
||||
|
||||
float curve(float x) { // the game's 1 - e^-x below PIVOT, its tangent there above
|
||||
float y0 = 1.0 - exp(-PIVOT);
|
||||
return x <= PIVOT ? 1.0 - exp(-x) : y0 + exp(-PIVOT) * (x - PIVOT);
|
||||
float y709(vec3 c) { return dot(c, vec3(0.2126390059, 0.7151686788, 0.0721923154)); }
|
||||
|
||||
float divide_safe(float a, float b, float fallback) { return b == 0.0 ? fallback : a / b; }
|
||||
|
||||
vec3 copy_sign(vec3 mag, vec3 s) {
|
||||
vec3 a = abs(mag);
|
||||
return mix(a, -a, lessThan(s, vec3(0.0)));
|
||||
}
|
||||
|
||||
vec3 curve3(vec3 x) { return vec3(curve(x.r), curve(x.g), curve(x.b)); }
|
||||
vec3 cbrt(vec3 x) { return copy_sign(pow(abs(x), vec3(1.0 / 3.0)), x); }
|
||||
|
||||
float maxc(vec3 c) { return max(c.r, max(c.g, c.b)); }
|
||||
vec3 oklab_from_bt709(vec3 c) { return mulm(OKLABLMS_TO_OKLAB, cbrt(mulm(BT709_TO_OKLABLMS, c))); }
|
||||
|
||||
void main() {
|
||||
vec3 x = min(texture(fp_t_tcb_8, uv.xy).rgb, vec3(10000.0)) + texture(fp_t_tcb_A, uv.xy).rgb;
|
||||
vec4 w = fp_c1.v[0], g = fp_c3.v[0];
|
||||
float game = support_buffer.hdr.x > 0.0 ? support_buffer.hdr.x : 1.0;
|
||||
float peak = support_buffer.hdr.y > 1.0 ? support_buffer.hdr.y : 1.0;
|
||||
vec3 bt709_from_oklab(vec3 lab) {
|
||||
vec3 lms = mulm(OKLAB_TO_OKLABLMS, lab);
|
||||
return mulm(OKLABLMS_TO_BT709, lms * lms * lms);
|
||||
}
|
||||
|
||||
float lum = max(x.r * 0.2989 + x.g * w.x + x.b * w.y, 0.0);
|
||||
float sdr = 1.0 - exp(-lum);
|
||||
vec3 by_lum = lum > 0.0 ? x * (curve(lum) / lum) : x;
|
||||
vec3 c = max(mix(by_lum, curve3(x), sdr * sdr), vec3(0.0));
|
||||
vec3 clamp_ap1(vec3 c) { return mulm(AP1_TO_BT709, max(mulm(BT709_TO_AP1, c), vec3(0.0))); }
|
||||
|
||||
// the game's saturation grade, on the colour scaled into 0..1 by its largest channel
|
||||
float s = max(maxc(c), 1.0);
|
||||
vec3 n = c / s;
|
||||
float m = maxc(n);
|
||||
float d = n.r * 0.6666667 + (n.g + n.b) * w.z - 1.0;
|
||||
float grade = g.y * (1.0 - d * d) + g.x;
|
||||
c = s * ((n - m) * grade + m);
|
||||
// The hue (OkLab a, b direction) of correct with the chrominance of incorrect.
|
||||
vec3 hue_oklab(vec3 incorrect, vec3 correct) {
|
||||
vec3 il = oklab_from_bt709(incorrect), cl = oklab_from_bt709(correct);
|
||||
float pre = length(il.yz);
|
||||
vec2 ab = cl.yz;
|
||||
ab *= divide_safe(pre, length(ab), 1.0);
|
||||
il.yz = ab;
|
||||
return clamp_ap1(bt709_from_oklab(il));
|
||||
}
|
||||
|
||||
// the game's own SDR result: the same blend clamped to 0..1, graded
|
||||
vec3 sdr_c = clamp(mix(lum > 0.0 ? x * (sdr / lum) : x, vec3(1.0) - exp(-x), sdr * sdr), 0.0, 1.0);
|
||||
float sm = maxc(sdr_c), sd = sdr_c.r * 0.6666667 + (sdr_c.g + sdr_c.b) * w.z - 1.0;
|
||||
sdr_c = (sdr_c - sm) * (g.y * (1.0 - sd * sd) + g.x) + sm;
|
||||
float hue = clamp(support_buffer.hdr.z, 0.0, 1.0);
|
||||
const vec3 Y = vec3(0.2126, 0.7152, 0.0722); // Rec. 709 luminance
|
||||
float yc = dot(c, Y), ys = dot(sdr_c, Y);
|
||||
if (hue > 0.0 && ys > 1e-5) c = mix(c, max(sdr_c, vec3(0.0)) * (yc / ys), hue * smoothstep(0.5, 1.5, maxc(c)));
|
||||
float srgb_decode(float c) { return c <= 0.04045 ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4); }
|
||||
|
||||
// roll-off of the largest channel from half the peak to the peak
|
||||
float top = maxc(c), knee = 0.5 * peak;
|
||||
if (top > knee) {
|
||||
float r = peak - knee;
|
||||
c *= (knee + r * (1.0 - exp(-(top - knee) / r))) / top;
|
||||
// pow_to_srgb: a value of a gamma-g picture as the sRGB value of the same code (the peak's correction).
|
||||
float gamma_to_srgb(float c, float g) { return srgb_decode(pow(c, 1.0 / g)); }
|
||||
|
||||
float smoother(float t) { return t * t * t * (t * (t * 6.0 - 15.0) + 10.0); }
|
||||
|
||||
float highlights(float x, float h, float mid) {
|
||||
if (h == 1.0) return x;
|
||||
float t = 0.0;
|
||||
if (x > mid) t = clamp(log2(x / mid) / log2(1.0 / mid), 0.0, 1.0);
|
||||
t = smoother(t);
|
||||
if (h > 1.0) return mix(x, mid * pow(x / mid, h), t);
|
||||
float b = mid * pow(x / mid, 2.0 - h);
|
||||
return divide_safe(x * x, mix(x, b, t), x);
|
||||
}
|
||||
|
||||
float shadows(float x, float s, float mid) {
|
||||
if (s == 1.0) return x;
|
||||
float ratio = max(divide_safe(x, mid, 0.0), 0.0);
|
||||
float base_term = x * mid;
|
||||
float base_scale = divide_safe(base_term, ratio, 0.0);
|
||||
float shadow_floor = mid / 16.0;
|
||||
float t = 1.0;
|
||||
if (x > shadow_floor) t = clamp(log2(x / mid) / log2(shadow_floor / mid), 0.0, 1.0);
|
||||
t = smoother(t);
|
||||
if (s > 1.0) {
|
||||
float raised = x * (1.0 + divide_safe(base_term, pow(ratio, s), 0.0));
|
||||
return x + (raised - x * (1.0 + base_scale)) * t;
|
||||
}
|
||||
color = vec4(max(c, vec3(0.0)) * game, 0.0);
|
||||
float lowered = x * (1.0 - divide_safe(base_term, pow(ratio, 2.0 - s), 0.0));
|
||||
return x + (lowered - x * (1.0 - base_scale)) * t;
|
||||
}
|
||||
|
||||
// The luminance curve: n peak nits, g contrast, c_d mid grey in nits, t_1 flare; 100 nits reference, scene grey 0.18.
|
||||
float curve(float x, float n, float g, float c_d, float t_1) {
|
||||
const float n_r = 100.0, c = 0.18, w_g = 0.0, r_hit_min = 128.0, r_hit_max = 256.0;
|
||||
float m_0 = n / n_r;
|
||||
float m_1 = 0.5 * (m_0 + sqrt(m_0 * (m_0 + 4.0 * t_1)));
|
||||
float r_hit = r_hit_min + (r_hit_max - r_hit_min) * (log(m_0) / log(10000.0 / 100.0));
|
||||
float u = pow((r_hit / m_1) / ((r_hit / m_1) + 1.0), g);
|
||||
float m = m_1 / u;
|
||||
float w_i = log(n / 100.0) / log(2.0);
|
||||
float c_t = (c_d / n_r) * (1.0 + w_i * w_g);
|
||||
float g_ip = 0.5 * (c_t + sqrt(c_t * (c_t + 4.0 * t_1)));
|
||||
float g_ipp2 = -m_1 * pow(g_ip / m, 1.0 / g) / (pow(g_ip / m, 1.0 / g) - 1.0);
|
||||
float w_2 = c / g_ipp2;
|
||||
float s_2 = w_2 * m_1;
|
||||
float u_2 = pow((r_hit / m_1) / ((r_hit / m_1) + w_2), g);
|
||||
float m_2 = m_1 / u_2;
|
||||
float ts = pow(max(0.0, x) / (x + s_2), g) * m_2;
|
||||
return max(0.0, (ts * ts) / (ts + t_1));
|
||||
}
|
||||
|
||||
struct Drt {
|
||||
float nits_peak, exposure, highlights, shadows, contrast, saturation, dechroma, flare, hue, blowout;
|
||||
float mid_gray_nits;
|
||||
bool clamp_zero; // clamp to Rec. 709 (no negative channel) before the peak
|
||||
};
|
||||
|
||||
// The tone mapper by luminance, then hue, saturation and blowout in OkLab; clamped to the peak in Rec. 709.
|
||||
vec3 drt(vec3 bt709, Drt d) {
|
||||
const float mid = 0.18;
|
||||
float peak = d.nits_peak / 100.0;
|
||||
float y_original = y709(bt709);
|
||||
float y = y_original * d.exposure;
|
||||
y = highlights(y, d.highlights, mid);
|
||||
y = shadows(y, d.shadows, mid);
|
||||
float y_new = clamp(curve(y, d.nits_peak, d.contrast, d.mid_gray_nits, d.flare), 0.0, peak);
|
||||
vec3 color = bt709 * (y_original > 0.0 ? y_new / y_original : 0.0);
|
||||
if (d.dechroma != 0.0 || d.saturation != 1.0 || d.hue != 0.0 || d.blowout != 0.0) {
|
||||
vec3 lab = oklab_from_bt709(color);
|
||||
if (d.hue != 0.0) {
|
||||
vec3 old_lab = oklab_from_bt709(bt709);
|
||||
float pre = length(lab.yz);
|
||||
lab.yz = mix(lab.yz, old_lab.yz, d.hue);
|
||||
lab.yz *= divide_safe(pre, length(lab.yz), 1.0);
|
||||
}
|
||||
if (d.dechroma != 0.0) lab.yz *= mix(1.0, 0.0, clamp(pow(y_original / 100.0, 1.0 - d.dechroma), 0.0, 1.0));
|
||||
if (d.blowout != 0.0) {
|
||||
float percent_max = clamp(y_original * 100.0 / 10000.0, 0.0, 1.0);
|
||||
float change = pow(1.0 - percent_max, 100.0 * abs(d.blowout));
|
||||
if (d.blowout < 0.0) change = 2.0 - change;
|
||||
lab.yz *= change;
|
||||
}
|
||||
lab.yz *= d.saturation;
|
||||
color = bt709_from_oklab(lab);
|
||||
}
|
||||
if (d.clamp_zero) color = max(color, vec3(0.0));
|
||||
return min(color, vec3(peak));
|
||||
}
|
||||
|
||||
// The same curve at SDR range with nothing changed: what the SDR grade is measured against.
|
||||
vec3 neutral_sdr(vec3 c) {
|
||||
Drt d = Drt(100.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 18.0, true);
|
||||
return drt(c, d);
|
||||
}
|
||||
|
||||
// The graded SDR colour carried to the HDR range: its luminance gains what the untonemapped colour has above the
|
||||
// neutral SDR curve, in the graded colour's hue.
|
||||
vec3 upgrade(vec3 untonemapped, vec3 neutral, vec3 graded, float strength) {
|
||||
float yu = y709(untonemapped), yn = y709(neutral), yg = y709(graded);
|
||||
float ratio;
|
||||
if (yu < yn) {
|
||||
ratio = yu / yn;
|
||||
} else {
|
||||
float y_new = yg + max(0.0, yu - yn);
|
||||
ratio = yg > 0.0 ? y_new / yg : 0.0;
|
||||
}
|
||||
vec3 scaled = hue_oklab(graded * ratio, graded);
|
||||
return mix(untonemapped, scaled, strength);
|
||||
}
|
||||
|
||||
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) {
|
||||
vec3 vanilla = aces_fitted(x);
|
||||
if (p0.w < 0.0) return vanilla;
|
||||
vec3 color = vanilla;
|
||||
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);
|
||||
Drt d = Drt(peak * 100.0, p1.x, p1.y, p1.z, p1.w, p2.x, p2.z, 0.10 * pow(p2.w, 10.0), p3.x, -(p2.y - 1.0),
|
||||
18.0, false);
|
||||
color = drt(graded, d);
|
||||
}
|
||||
color *= p0.y / p0.z; // the picture's white against the HUD's
|
||||
return mix(color, vec3(0.0), isnan(color));
|
||||
}
|
||||
|
||||
void main() { o = vec4(nx_tonemap(a0.xyz, a1, a2, a3, a4), 1.0); }
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#version 450
|
||||
// The in-game menu on an HDR10 swapchain (runtime/vk_hdr.c vk_hdr_ui_compose): ImGui drew it into an RGBA8 image,
|
||||
// premultiplied sRGB colour over a clear 0; this turns each pixel back into its colour, puts its white at p.x nits and
|
||||
// premultiplied sRGB colour over a clear 0; this turns each pixel back into its colour (with the picture's gamma
|
||||
// correction p.y: 0 none, 1: 2.2, 2: 2.4, as shaders/hdr_encode.comp applies it), puts its white at p.x nits and
|
||||
// writes the PQ code with its coverage, which the pipeline blends over the picture (in PQ code values, as the menu
|
||||
// blends in sRGB code values on an SDR swapchain). PQ: SMPTE ST 2084; Rec. 709 to Rec. 2020: ITU-R BT.2087.
|
||||
layout(location = 0) in vec2 uv;
|
||||
@@ -21,7 +22,8 @@ vec3 pq(vec3 nits) {
|
||||
void main() {
|
||||
vec4 u = texture(ui, uv);
|
||||
if (u.a <= 0.0) discard;
|
||||
vec3 c = srgb_decode(clamp(u.rgb / u.a, 0.0, 1.0));
|
||||
vec3 code = clamp(u.rgb / u.a, 0.0, 1.0);
|
||||
vec3 c = pc.p.y == 1.0 ? pow(code, vec3(2.2)) : pc.p.y == 2.0 ? pow(code, vec3(2.4)) : srgb_decode(code);
|
||||
const mat3 to2020 = mat3(0.6274, 0.0691, 0.0164, 0.3293, 0.9195, 0.0880, 0.0433, 0.0114, 0.8956);
|
||||
color = vec4(pq(max(to2020 * c, vec3(0.0)) * pc.p.x), u.a);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user