From 91d970e9c5f612f62118ac88188959e57e048d54 Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Wed, 3 Jul 2024 07:45:48 +0200 Subject: [PATCH] gpu: Add shaders for the new specialization constant This adds the following functions: output_color_from_alt() alt_color_from_output() Converts between the two colors output_color_alpha() alt_color_alpha() Multiplies a color with an alpha value --- gsk/gpu/shaders/color.glsl | 127 +++++++++++++++++++++++++++++ gsk/gpu/shaders/gskgpuconvert.glsl | 18 ---- 2 files changed, 127 insertions(+), 18 deletions(-) diff --git a/gsk/gpu/shaders/color.glsl b/gsk/gpu/shaders/color.glsl index 3f18784593..91538b2419 100644 --- a/gsk/gpu/shaders/color.glsl +++ b/gsk/gpu/shaders/color.glsl @@ -1,6 +1,17 @@ #ifndef _COLOR_ #define _COLOR_ +#define COLOR_SPACE_OUTPUT_PREMULTIPLIED (1u << 2) +#define COLOR_SPACE_ALT_PREMULTIPLIED (1u << 3) +#define COLOR_SPACE_OUTPUT_SHIFT 8u +#define COLOR_SPACE_ALT_SHIFT 16u +#define COLOR_SPACE_COLOR_STATE_MASK 0xFFu + +#define OUTPUT_COLOR_SPACE ((GSK_COLOR_STATES >> COLOR_SPACE_OUTPUT_SHIFT) & COLOR_SPACE_COLOR_STATE_MASK) +#define ALT_COLOR_SPACE ((GSK_COLOR_STATES >> COLOR_SPACE_ALT_SHIFT) & COLOR_SPACE_COLOR_STATE_MASK) +#define OUTPUT_PREMULTIPLIED ((GSK_COLOR_STATES & COLOR_SPACE_OUTPUT_PREMULTIPLIED) == COLOR_SPACE_OUTPUT_PREMULTIPLIED) +#define ALT_PREMULTIPLIED ((GSK_COLOR_STATES & COLOR_SPACE_ALT_PREMULTIPLIED) == COLOR_SPACE_ALT_PREMULTIPLIED) + vec4 color_premultiply (vec4 color) { @@ -13,6 +24,122 @@ color_unpremultiply (vec4 color) return color.a > 0.0 ? color / vec4 (color.aaa, 1.0) : color; } +float +srgb_eotf (float v) +{ + if (v >= 0.04045) + return pow (((v + 0.055) / (1.0 + 0.055)), 2.4); + else + return v / 12.92; +} + +float +srgb_oetf (float v) +{ + if (v > 0.0031308) + return 1.055 * pow (v, 1.0 / 2.4) - 0.055; + else + return 12.92 * v; +} + +vec4 +alt_color_alpha (vec4 color, + float alpha) +{ + if (ALT_PREMULTIPLIED) + return color * alpha; + else + return vec4 (color.rgb, color.a * alpha); +} + +vec4 +output_color_alpha (vec4 color, + float alpha) +{ + if (OUTPUT_PREMULTIPLIED) + return color * alpha; + else + return vec4 (color.rgb, color.a * alpha); +} + +vec4 +alt_color_from_output (vec4 color) +{ + if (OUTPUT_COLOR_SPACE == ALT_COLOR_SPACE) + { + if (OUTPUT_PREMULTIPLIED && !ALT_PREMULTIPLIED) + return color_unpremultiply (color); + else if (!OUTPUT_PREMULTIPLIED && ALT_PREMULTIPLIED) + return color_premultiply (color); + else + return color; + } + + if (OUTPUT_PREMULTIPLIED) + color = color_unpremultiply (color); + + if (OUTPUT_COLOR_SPACE == GDK_COLOR_STATE_ID_SRGB && + ALT_COLOR_SPACE == GDK_COLOR_STATE_ID_SRGB_LINEAR) + { + color = vec4 (srgb_eotf (color.r), + srgb_eotf (color.g), + srgb_eotf (color.b), + color.a); + } + else if (OUTPUT_COLOR_SPACE == GDK_COLOR_STATE_ID_SRGB_LINEAR && + ALT_COLOR_SPACE == GDK_COLOR_STATE_ID_SRGB) + { + color = vec4 (srgb_oetf (color.r), + srgb_oetf (color.g), + srgb_oetf (color.b), + color.a); + } + + if (ALT_PREMULTIPLIED) + color = color_premultiply (color); + + return color; +} + +vec4 +output_color_from_alt (vec4 color) +{ + if (OUTPUT_COLOR_SPACE == ALT_COLOR_SPACE) + { + if (ALT_PREMULTIPLIED && !OUTPUT_PREMULTIPLIED) + return color_unpremultiply (color); + else if (!ALT_PREMULTIPLIED && OUTPUT_PREMULTIPLIED) + return color_premultiply (color); + else + return color; + } + + if (ALT_PREMULTIPLIED) + color = color_unpremultiply (color); + + if (ALT_COLOR_SPACE == GDK_COLOR_STATE_ID_SRGB && + OUTPUT_COLOR_SPACE == GDK_COLOR_STATE_ID_SRGB_LINEAR) + { + color = vec4 (srgb_eotf (color.r), + srgb_eotf (color.g), + srgb_eotf (color.b), + color.a); + } + else if (ALT_COLOR_SPACE == GDK_COLOR_STATE_ID_SRGB_LINEAR && + OUTPUT_COLOR_SPACE == GDK_COLOR_STATE_ID_SRGB) + { + color = vec4 (srgb_oetf (color.r), + srgb_oetf (color.g), + srgb_oetf (color.b), + color.a); + } + + if (OUTPUT_PREMULTIPLIED) + color = color_premultiply (color); + + return color; +} + float luminance (vec3 color) { diff --git a/gsk/gpu/shaders/gskgpuconvert.glsl b/gsk/gpu/shaders/gskgpuconvert.glsl index f6e6c57d96..04dcbe8f61 100644 --- a/gsk/gpu/shaders/gskgpuconvert.glsl +++ b/gsk/gpu/shaders/gskgpuconvert.glsl @@ -13,24 +13,6 @@ #define SOURCE_COLOR_STATE ((GSK_VARIATION >> VARIATION_SOURCE_SHIFT) & VARIATION_COLOR_STATE_MASK) #define TARGET_COLOR_STATE ((GSK_VARIATION >> VARIATION_TARGET_SHIFT) & VARIATION_COLOR_STATE_MASK) -float -srgb_eotf (float v) -{ - if (v >= 0.04045) - return pow (((v + 0.055) / (1.0 + 0.055)), 2.4); - else - return v / 12.92; -} - -float -srgb_oetf (float v) -{ - if (v > 0.0031308) - return 1.055 * pow (v, 1.0 / 2.4) - 0.055; - else - return 12.92 * v; -} - vec4 srgb_to_linear_srgb (vec4 color) {