From bf3aa0af9355f8a9d634a391c972f8b8fe45505b Mon Sep 17 00:00:00 2001 From: Simon Schneegans Date: Thu, 9 Jun 2022 11:34:18 +0200 Subject: [PATCH] :wrench: Use methods shader I/O --- kwin/main.js.in | 4 +-- resources/shaders/common.glsl | 68 +++++++++++++++++++++++++++-------- resources/shaders/tv.frag | 12 +++---- 3 files changed, 60 insertions(+), 24 deletions(-) diff --git a/kwin/main.js.in b/kwin/main.js.in index 5e755be..0e9ebe2 100755 --- a/kwin/main.js.in +++ b/kwin/main.js.in @@ -109,7 +109,7 @@ class BMW%NICK%Effect { { type: Effect.ShaderUniform, fragmentShader: this.shader, - uniform: "uForOpening", + uniform: "forOpening", from: 1.0, to: 1.0 } @@ -143,7 +143,7 @@ class BMW%NICK%Effect { { type: Effect.ShaderUniform, fragmentShader: this.shader, - uniform: "uForOpening", + uniform: "forOpening", from: 0.0, to: 0.0 } diff --git a/resources/shaders/common.glsl b/resources/shaders/common.glsl index 947420d..04c1085 100644 --- a/resources/shaders/common.glsl +++ b/resources/shaders/common.glsl @@ -17,17 +17,21 @@ // --------------------------------------------------------------------- standard uniforms -// Each shader can access these standard uniforms: +// Each shader can access these standard input values: -// bool uForOpening: True if a window-open animation is ongoing, false otherwise. -// sampler2D uTexture: Contains the texture of the window. -// float uProgress: A value which transitions from 0 to 1 during the animation. -// float uTime: A steadily increasing value in seconds. -// vec2 uSize: The size of uTexture in pixels. -// float uPadding: The empty area around the actual window (e.g. where the shadow -// is drawn). For now, this will only be set on GNOME. +// vec2 iTexCoord: Texture coordinates for retrieving the window input color. +// bool uForOpening: True if a window-open animation is ongoing, false otherwise. +// float uProgress: A value which transitions from 0 to 1 during the animation. +// float uTime: A steadily increasing value in seconds. +// vec2 uSize: The size of uTexture in pixels. +// float uPadding: The empty area around the actual window (e.g. where the shadow +// is drawn). For now, this will only be set on GNOME. -// Furthermore, there is the global input "vec2 iTexCoord" and the output "vec4 oColor". +// Furthermore, there are two global methods for reading the window input color and +// setting the shader output color. Both methods assume straight alpha: + +// vec4 getInputColor(vec2 coords) +// void setOutputColor(vec4 outColor) #if defined(KWIN) // -------------------------------------------------------------------- @@ -43,11 +47,23 @@ bool uForOpening = forOpening == 1.0; in vec2 texcoord0; out vec4 fragColor; -#define uTexture sampler #define uProgress animationProgress #define uPadding 0.0 #define iTexCoord texcoord0 -#define oColor fragColor + +vec4 getInputColor(vec2 coords) { + vec4 color = texture2D(sampler, coords); + + if (color.a > 0.0) { + color.rgb /= color.a; + } + + return color; +} + +void setOutputColor(vec4 outColor) { + fragColor = vec4(outColor.rgb * outColor.a, outColor.a); +} #elif defined(KWIN_LEGACY) // ----------------------------------------------------------- @@ -62,11 +78,23 @@ bool uForOpening = forOpening == 1.0; varying vec2 texcoord0; -#define uTexture sampler #define uProgress animationProgress #define uPadding 0.0 #define iTexCoord texcoord0 -#define oColor gl_FragColor + +vec4 getInputColor(vec2 coords) { + vec4 color = texture2D(sampler, coords); + + if (color.a > 0.0) { + color.rgb /= color.a; + } + + return color; +} + +void setOutputColor(vec4 outColor) { + gl_FragColor = vec4(outColor.rgb * outColor.a, outColor.a); +} #else // GNOME -------------------------------------------------------------------------- @@ -80,7 +108,19 @@ uniform float uPadding; // On GNOME, we set iTexCoord and oColor to be aliases for the cogl variables. #define iTexCoord cogl_tex_coord_in[0] -#define oColor cogl_color_out + +// Shell.GLSLEffect uses straight alpha. So we have to convert from premultiplied. +vec4 getInputColor(vec2 coords) { + vec4 color = texture2D(uTexture, coords); + + if (color.a > 0.0) { + color.rgb /= color.a; + } + + return color; +} + +void setOutputColor(vec4 outColor) { cogl_color_out = outColor; } #endif // ------------------------------------------------------------------------------- diff --git a/resources/shaders/tv.frag b/resources/shaders/tv.frag index 79b35fb..ea53b4c 100644 --- a/resources/shaders/tv.frag +++ b/resources/shaders/tv.frag @@ -54,14 +54,8 @@ void main() { // Assemble the final alpha value. float mask = tbMask * lrMask * ffMask; - oColor = texture2D(uTexture, coords); - - // Shell.GLSLEffect uses straight alpha. So we have to convert from premultiplied. - if (oColor.a > 0.0) { - oColor.rgb /= oColor.a; - } - - oColor.rgb = mix(oColor.rgb, uColor.rgb * oColor.a, smoothstep(0.0, 1.0, prog)); + vec4 oColor = getInputColor(coords); + oColor.rgb = mix(oColor.rgb, uColor.rgb * oColor.a, smoothstep(0.0, 1.0, prog)); oColor.a *= mask; // These are pretty useful for understanding how this works. @@ -69,4 +63,6 @@ void main() { // oColor = vec4(vec3(lrMask), 1); // oColor = vec4(vec3(ffMask), 1); // oColor = vec4(vec3(mask), 1); + + setOutputColor(oColor); } \ No newline at end of file