Add HDR10 output

With hdr = 1 the game's tonemap shader is replaced by one that keeps highlights up to the display's peak, the present
reads the float picture the game copies into its 8-bit window texture, and a compute pass encodes it as PQ in Rec. 2020
for an HDR10 swapchain. Peak, paper white and HUD brightness are live settings in the menu and totk.ini. The approach
follows the RenoDX TotK mod by souperman9 and Musa; no code was copied.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ProSepandClaude Opus 5.5 committed 2026-10-31 21:17:12 +01:00
1 parent f469c5a774
commit 007786d487
28 files changed
+1222 -22

No files matched your search

+34
View File
@@ -0,0 +1,34 @@
#version 450
// 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.
layout(local_size_x = 8, local_size_y = 8) in;
layout(binding = 0) uniform sampler2D src;
layout(binding = 1, rgba16f) uniform writeonly image2D dst;
layout(push_constant) uniform P {
ivec2 offset;
ivec2 size;
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 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));
return pow((c1 + c2 * y) / (1.0 + c3 * y), vec3(m2));
}
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));
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));
}
+8
View File
@@ -0,0 +1,8 @@
#version 450
// One triangle over the whole target, for hdr_ui.frag (runtime/vk_hdr.c vk_hdr_ui_compose).
layout(location = 0) out vec2 uv;
void main() {
uv = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2);
gl_Position = vec4(uv * 2.0 - 1.0, 0.0, 1.0);
}
+61
View File
@@ -0,0 +1,61 @@
#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 TOTK_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 TOTK_HDR_UI_NITS.
// Approach after RenoDX's TotK mod by souperman9 and Musa (github.com/clshortfuse/renodx, src/games/totk, MIT): its
// "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;
layout(set = 0, binding = 0, std140) uniform SupportBuffer {
vec4 pad[88];
vec4 hdr; // byte 1408 (vk_exec_draw.c support_fill): game white / HUD white, peak / game white
} 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;
const float PIVOT = 0.18;
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);
}
vec3 curve3(vec3 x) { return vec3(curve(x.r), curve(x.g), curve(x.b)); }
float maxc(vec3 c) { return max(c.r, max(c.g, c.b)); }
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;
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));
// 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);
// 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;
}
color = vec4(max(c, vec3(0.0)) * game, 0.0);
}
+27
View File
@@ -0,0 +1,27 @@
#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
// 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;
layout(location = 0) out vec4 color;
layout(binding = 0) uniform sampler2D ui;
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 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));
return pow((c1 + c2 * y) / (1.0 + c3 * y), vec3(m2));
}
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));
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);
}