17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-28 23:57:28 +01:00

Add a white balance to the tonemapping

This commit is contained in:
2026-10-27 01:40:19 +01:00
parent 82d3842495
commit ac0b4e5f15
13 changed files with 122 additions and 6 deletions

View File

@@ -28,13 +28,16 @@ layout(std430, binding = EXPOSURE_SSBO_BINDING) readonly buffer exposure_ssbo
// param[0].x: share of the bloom in the image (0: no bloom, the texture is not read), param[0].y: 1 if the automatic
// exposure is applied, param[0].z: fixed exposure multiplier, param[0].w: scale of the bloom texture (it holds the sum of
// the levels of the chain), param[1].x: 1 when this is the last pass before the window, which takes the dither
// the levels of the chain), param[1].x: 1 when this is the last pass before the window, which takes the dither,
// param[1].yzw: gains of the white balance (0: none)
void main()
{
vec2 texcoord = f_coords;
vec3 hdr_color = texture(tex1, texcoord).xyz;
if (param[0].x > 0.0)
hdr_color = mix(hdr_color, texture(bloom_tex, texcoord).rgb * param[0].w, param[0].x);
if (param[1].y > 0.0)
hdr_color = white_balance(hdr_color, param[1].yzw);
float exposurescale = param[0].z;
if (param[0].y > 0.5 && exposure > 0.0)
exposurescale *= exposure;

View File

@@ -139,6 +139,22 @@ vec3 ApplyAgX(vec3 linearColorRec709)
return linearColorRec709;
}
// white balance of the camera (von kries in the bradford cone space). Gains: ratios of the cone responses of the white of
// the display to those of the light taken as white (gfx::exposure_settings::white_balance())
vec3 white_balance(vec3 rgb, vec3 gains)
{
// linear rec.709 -> xyz -> bradford, and back
const mat3 rgb_to_lms = mat3(
0.42272529, 0.05569978, 0.02138264,
0.49134532, 0.96153405, 0.08764187,
0.02735794, 0.02318381, 0.98050813);
const mat3 lms_to_rgb = mat3(
2.53804452, -0.14600406, -0.04229851,
-1.29327700, 1.11664831, -0.07160722,
-0.04023688, -0.02232903, 1.02275269);
return lms_to_rgb * (gains * (rgb_to_lms * rgb));
}
vec4 tonemap(vec4 x)
{
// Use AgX by default. Reinhard and ACES above are kept for reference.

View File

@@ -21,13 +21,15 @@ layout(set = POST_SET, binding = POST_TEXTURE_FIRST + 1) uniform sampler2D bloom
// param[0].x: share of the bloom in the image (0: no bloom, the texture is not read), param[0].y: 1 if the automatic
// exposure is applied (exposure_ssbo of common.glsl, worked out by exposure_average.comp), param[0].z: fixed exposure multiplier, param[0].w: scale of
// the bloom texture (it holds the sum of the levels of the chain), param[1].x: 1 when this is the last pass before the
// window, which takes the dither
// window, which takes the dither, param[1].yzw: gains of the white balance (0: none)
void main()
{
vec2 texcoord = f_coords;
vec3 hdr_color = texture(tex1, texcoord).xyz;
if (param[0].x > 0.0)
hdr_color = mix(hdr_color, texture(bloom_tex, texcoord).rgb * param[0].w, param[0].x);
if (param[1].y > 0.0)
hdr_color = white_balance(hdr_color, param[1].yzw);
float exposurescale = param[0].z;
if (param[0].y > 0.5 && exposure > 0.0)
exposurescale *= exposure;

View File

@@ -139,6 +139,22 @@ vec3 ApplyAgX(vec3 linearColorRec709)
return linearColorRec709;
}
// white balance of the camera (von kries in the bradford cone space). Gains: ratios of the cone responses of the white of
// the display to those of the light taken as white (gfx::exposure_settings::white_balance())
vec3 white_balance(vec3 rgb, vec3 gains)
{
// linear rec.709 -> xyz -> bradford, and back
const mat3 rgb_to_lms = mat3(
0.42272529, 0.05569978, 0.02138264,
0.49134532, 0.96153405, 0.08764187,
0.02735794, 0.02318381, 0.98050813);
const mat3 lms_to_rgb = mat3(
2.53804452, -0.14600406, -0.04229851,
-1.29327700, 1.11664831, -0.07160722,
-0.04023688, -0.02232903, 1.02275269);
return lms_to_rgb * (gains * (rgb_to_lms * rgb));
}
vec4 tonemap(vec4 x)
{
// Use AgX by default. Reinhard and ACES above are kept for reference.