render/vulkan: undo alpha premult before sRGB encoding/decoding

sRGB encoding/decoding needs to happen with straight alpha, not
pre-multiplied alpha.
This commit is contained in:
Simon Ser 2023-10-17 12:21:08 +02:00
parent b560f36207
commit 5adf325333
2 changed files with 32 additions and 16 deletions

View file

@ -5,17 +5,25 @@ layout (input_attachment_index = 0, binding = 0) uniform subpassInput in_color;
layout(location = 0) in vec2 uv;
layout(location = 0) out vec4 out_color;
float linear_to_srgb(float x) {
float linear_channel_to_srgb(float x) {
return max(min(x * 12.92, 0.04045), 1.055 * pow(x, 1. / 2.4) - 0.055);
}
vec4 linear_color_to_srgb(vec4 color) {
if (color.a == 0) {
return vec4(0);
}
color.rgb /= color.a;
color.rgb = vec3(
linear_channel_to_srgb(color.r),
linear_channel_to_srgb(color.g),
linear_channel_to_srgb(color.b)
);
color.rgb *= color.a;
return color;
}
void main() {
vec4 val = subpassLoad(in_color).rgba;
out_color = vec4(
linear_to_srgb(val.r),
linear_to_srgb(val.g),
linear_to_srgb(val.b),
val.a
);
out_color = linear_color_to_srgb(val);
}

View file

@ -15,23 +15,31 @@ layout (constant_id = 0) const int TEXTURE_TRANSFORM = 0;
#define TEXTURE_TRANSFORM_IDENTITY 0
#define TEXTURE_TRANSFORM_SRGB 1
float srgb_to_linear(float x) {
float srgb_channel_to_linear(float x) {
return max(x / 12.92, pow((x + 0.055) / 1.055, 2.4));
}
vec4 srgb_color_to_linear(vec4 color) {
if (color.a == 0) {
return vec4(0);
}
color.rgb /= color.a;
color.rgb = vec3(
srgb_channel_to_linear(color.r),
srgb_channel_to_linear(color.g),
srgb_channel_to_linear(color.b)
);
color.rgb *= color.a;
return color;
}
void main() {
vec4 val = textureLod(tex, uv, 0);
if (TEXTURE_TRANSFORM == TEXTURE_TRANSFORM_SRGB) {
out_color = vec4(
srgb_to_linear(val.r),
srgb_to_linear(val.g),
srgb_to_linear(val.b),
val.a
);
out_color = srgb_color_to_linear(val);
} else { // TEXTURE_TRANSFORM_IDENTITY
out_color = val;
}
out_color *= data.alpha;
}