From 5ff44467d77c2167cdefbf81014b349c97fe10f1 Mon Sep 17 00:00:00 2001 From: Felix Dick Date: Wed, 28 Sep 2022 14:48:05 +0200 Subject: [PATCH 1/8] Avoid 38 files to compile every time a shader is modified. --- src/render/OpenGL.cpp | 1 + src/render/OpenGL.hpp | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/render/OpenGL.cpp b/src/render/OpenGL.cpp index 2ae8d747..73cad693 100644 --- a/src/render/OpenGL.cpp +++ b/src/render/OpenGL.cpp @@ -1,3 +1,4 @@ +#include "Shaders.hpp" #include "OpenGL.hpp" #include "../Compositor.hpp" #include "../helpers/MiscFunctions.hpp" diff --git a/src/render/OpenGL.hpp b/src/render/OpenGL.hpp index e3350c15..df47ebd3 100644 --- a/src/render/OpenGL.hpp +++ b/src/render/OpenGL.hpp @@ -8,7 +8,6 @@ #include -#include "Shaders.hpp" #include "Shader.hpp" #include "Texture.hpp" #include "Framebuffer.hpp" From 7edbaea23de36182fb57a634b95f671c353f8aa7 Mon Sep 17 00:00:00 2001 From: Felix Dick Date: Wed, 28 Sep 2022 14:50:27 +0200 Subject: [PATCH 2/8] Make the rounding texture shaders smaller and more efficient. --- src/render/shaders/Textures.hpp | 223 ++++++++------------------------ 1 file changed, 53 insertions(+), 170 deletions(-) diff --git a/src/render/shaders/Textures.hpp b/src/render/shaders/Textures.hpp index 3ec099ab..b7d672d9 100644 --- a/src/render/shaders/Textures.hpp +++ b/src/render/shaders/Textures.hpp @@ -3,138 +3,38 @@ #include inline static constexpr auto ROUNDED_SHADER_FUNC = [](const std::string colorVarName) -> std::string { - return R"#( - if (pixCoord[0] < topLeft[0]) { - // we're close left - if (pixCoord[1] < topLeft[1]) { - // top + return R"#( - if (ignoreCorners == 1) { + // branchless baby! + vec2 pixCoord = v_texcoord - vec2(0.5); + pixCoord *= (vec2(lessThan(pixCoord, vec2(0.0))) * vec2(-2.0) + vec2(1.0)) * fullSize; + pixCoord -= (bottomRight - topLeft) * vec2(0.5); + + if (all(greaterThan(pixCoord, vec2(0.0)))) { + + if (ignoreCorners == 1) + discard; + + float dist = length(pixCoord); + + if (dist > radius) + discard; + + if (primitiveMultisample == 1 && dist > radius - 1.0) { + float distances = 0.0; + if (length(pixCoord - vec2(0.25, 0.25)) < radius) { distances = distances + 1.0; } + if (length(pixCoord - vec2(0.75, 0.25)) < radius) { distances = distances + 1.0; } + if (length(pixCoord - vec2(0.25, 0.75)) < radius) { distances = distances + 1.0; } + if (length(pixCoord - vec2(0.75, 0.75)) < radius) { distances = distances + 1.0; } + + if (distances == 0.0) discard; - return; - } - float topLeftDistance = distance(topLeft, pixCoord); + distances = distances / 4.0; - if (topLeftDistance > radius - 1.0) { - if (primitiveMultisample == 0 && topLeftDistance > radius) { - discard; - return; - } else if (primitiveMultisample == 1) { - float distances = 0.0; - if (distance(topLeft, pixCoord + vec2(0.25, 0.25)) < radius) { distances = distances + 1.0; } - if (distance(topLeft, pixCoord + vec2(0.75, 0.25)) < radius) { distances = distances + 1.0; } - if (distance(topLeft, pixCoord + vec2(0.25, 0.75)) < radius) { distances = distances + 1.0; } - if (distance(topLeft, pixCoord + vec2(0.75, 0.75)) < radius) { distances = distances + 1.0; } - - if (distances == 0.0) { - discard; - return; - } - - distances = distances / 4.0; - - )#" + colorVarName + R"#( = )#" + colorVarName + R"#( * distances; - } - } - } else if (pixCoord[1] > bottomRight[1]) { - // bottom - - if (ignoreCorners == 1) { - discard; - return; - } - - float topLeftDistance = distance(vec2(topLeft[0], bottomRight[1]), pixCoord); - - if (topLeftDistance > radius - 1.0) { - if (primitiveMultisample == 0 && topLeftDistance > radius) { - discard; - return; - } else if (primitiveMultisample == 1) { - float distances = 0.0; - if (distance(vec2(topLeft[0], bottomRight[1]), pixCoord + vec2(0.25, 0.25)) < radius) { distances = distances + 1.0; } - if (distance(vec2(topLeft[0], bottomRight[1]), pixCoord + vec2(0.75, 0.25)) < radius) { distances = distances + 1.0; } - if (distance(vec2(topLeft[0], bottomRight[1]), pixCoord + vec2(0.25, 0.75)) < radius) { distances = distances + 1.0; } - if (distance(vec2(topLeft[0], bottomRight[1]), pixCoord + vec2(0.75, 0.75)) < radius) { distances = distances + 1.0; } - - if (distances == 0.0) { - discard; - return; - } - - distances = distances / 4.0; - - )#" + colorVarName + R"#( = )#" + colorVarName + R"#( * distances; - } - } + /* )#" + colorVarName + R"#( = )#" + colorVarName + R"#( * distances; */ } - } - else if (pixCoord[0] > bottomRight[0]) { - // we're close right - if (pixCoord[1] < topLeft[1]) { - // top - if (ignoreCorners == 1) { - discard; - return; - } - - float topLeftDistance = distance(vec2(bottomRight[0], topLeft[1]), pixCoord); - - if (topLeftDistance > radius - 1.0) { - if (primitiveMultisample == 0 && topLeftDistance > radius) { - discard; - return; - } else if (primitiveMultisample == 1) { - float distances = 0.0; - if (distance(vec2(bottomRight[0], topLeft[1]), pixCoord + vec2(0.25, 0.25)) < radius) { distances = distances + 1.0; } - if (distance(vec2(bottomRight[0], topLeft[1]), pixCoord + vec2(0.75, 0.25)) < radius) { distances = distances + 1.0; } - if (distance(vec2(bottomRight[0], topLeft[1]), pixCoord + vec2(0.25, 0.75)) < radius) { distances = distances + 1.0; } - if (distance(vec2(bottomRight[0], topLeft[1]), pixCoord + vec2(0.75, 0.75)) < radius) { distances = distances + 1.0; } - - if (distances == 0.0) { - discard; - return; - } - - distances = distances / 4.0; - - )#" + colorVarName + R"#( = )#" + colorVarName + R"#( * distances; - } - } - } else if (pixCoord[1] > bottomRight[1]) { - // bottom - - if (ignoreCorners == 1) { - discard; - return; - } - - float topLeftDistance = distance(bottomRight, pixCoord); - - if (topLeftDistance > radius - 1.0) { - if (primitiveMultisample == 0 && topLeftDistance > radius) { - discard; - return; - } else if (primitiveMultisample == 1) { - float distances = 0.0; - if (distance(bottomRight, pixCoord + vec2(0.25, 0.25)) < radius) { distances = distances + 1.0; } - if (distance(bottomRight, pixCoord + vec2(0.75, 0.25)) < radius) { distances = distances + 1.0; } - if (distance(bottomRight, pixCoord + vec2(0.25, 0.75)) < radius) { distances = distances + 1.0; } - if (distance(bottomRight, pixCoord + vec2(0.75, 0.75)) < radius) { distances = distances + 1.0; } - - if (distances == 0.0) { - discard; - return; - } - - distances = distances / 4.0; - - )#" + colorVarName + R"#( = )#" + colorVarName + R"#( * distances; - } - } - } } )#"; }; @@ -167,16 +67,11 @@ uniform int primitiveMultisample; uniform int ignoreCorners; void main() { - if (radius == 0.0) { - gl_FragColor = v_color; - return; - } - vec4 pixColor = v_color; - vec2 pixCoord = fullSize * v_texcoord; - - )#" + ROUNDED_SHADER_FUNC("pixColor") + R"#( + if (radius > 0.0) { + )#" + ROUNDED_SHADER_FUNC("pixColor") + R"#( + } gl_FragColor = pixColor; })#"; @@ -192,6 +87,7 @@ void main() { v_texcoord = texcoord; })#"; +// this is texture rendering!! inline const std::string TEXFRAGSRCRGBA = R"#( precision mediump float; varying vec2 v_texcoord; // is in 0-1 @@ -215,10 +111,9 @@ void main() { vec4 pixColor = texture2D(tex, v_texcoord); - if (discardOpaque == 1 && pixColor[3] * alpha == 1.0) { + if (discardOpaque == 1 && pixColor[3] * alpha == 1.0) discard; - return; - } + if (applyTint == 1) { pixColor[0] = pixColor[0] * tint[0]; @@ -226,10 +121,7 @@ void main() { pixColor[2] = pixColor[2] * tint[2]; } - vec2 pixCoord = fullSize * v_texcoord; - - )#" + ROUNDED_SHADER_FUNC("pixColor") + - R"#( + )#" + ROUNDED_SHADER_FUNC("pixColor") + R"#( gl_FragColor = pixColor * alpha; })#"; @@ -255,10 +147,8 @@ uniform int ignoreCorners; void main() { - if (discardOpaque == 1 && alpha == 1.0) { + if (discardOpaque == 1 && alpha == 1.0) discard; - return; - } vec4 pixColor = vec4(texture2D(tex, v_texcoord).rgb, 1.0); @@ -268,10 +158,7 @@ void main() { pixColor[2] = pixColor[2] * tint[2]; } - vec2 pixCoord = fullSize * v_texcoord; - - )#" + ROUNDED_SHADER_FUNC("pixColor") + - R"#( + )#" + ROUNDED_SHADER_FUNC("pixColor") + R"#( gl_FragColor = pixColor * alpha; })#"; @@ -288,12 +175,13 @@ uniform vec2 halfpixel; void main() { vec2 uv = v_texcoord * 2.0; - vec4 sum = texture2D(tex, uv) * 4.0; - sum += texture2D(tex, uv - halfpixel.xy * radius); - sum += texture2D(tex, uv + halfpixel.xy * radius); - sum += texture2D(tex, uv + vec2(halfpixel.x, -halfpixel.y) * radius); - sum += texture2D(tex, uv - vec2(halfpixel.x, -halfpixel.y) * radius); - gl_FragColor = sum / 8.0; + vec4 sum = texture2D(tex, uv) * 4.0; + sum += texture2D(tex, uv - halfpixel.xy * radius); + sum += texture2D(tex, uv + halfpixel.xy * radius); + sum += texture2D(tex, uv + vec2(halfpixel.x, -halfpixel.y) * radius); + sum += texture2D(tex, uv - vec2(halfpixel.x, -halfpixel.y) * radius); + + gl_FragColor = sum / 8.0; } )#"; @@ -309,17 +197,17 @@ uniform vec2 halfpixel; void main() { vec2 uv = v_texcoord / 2.0; - vec4 sum = texture2D(tex, uv + vec2(-halfpixel.x * 2.0, 0.0) * radius); + vec4 sum = texture2D(tex, uv + vec2(-halfpixel.x * 2.0, 0.0) * radius); - sum += texture2D(tex, uv + vec2(-halfpixel.x, halfpixel.y) * radius) * 2.0; - sum += texture2D(tex, uv + vec2(0.0, halfpixel.y * 2.0) * radius); - sum += texture2D(tex, uv + vec2(halfpixel.x, halfpixel.y) * radius) * 2.0; - sum += texture2D(tex, uv + vec2(halfpixel.x * 2.0, 0.0) * radius); - sum += texture2D(tex, uv + vec2(halfpixel.x, -halfpixel.y) * radius) * 2.0; - sum += texture2D(tex, uv + vec2(0.0, -halfpixel.y * 2.0) * radius); - sum += texture2D(tex, uv + vec2(-halfpixel.x, -halfpixel.y) * radius) * 2.0; + sum += texture2D(tex, uv + vec2(-halfpixel.x, halfpixel.y) * radius) * 2.0; + sum += texture2D(tex, uv + vec2(0.0, halfpixel.y * 2.0) * radius); + sum += texture2D(tex, uv + vec2(halfpixel.x, halfpixel.y) * radius) * 2.0; + sum += texture2D(tex, uv + vec2(halfpixel.x * 2.0, 0.0) * radius); + sum += texture2D(tex, uv + vec2(halfpixel.x, -halfpixel.y) * radius) * 2.0; + sum += texture2D(tex, uv + vec2(0.0, -halfpixel.y * 2.0) * radius); + sum += texture2D(tex, uv + vec2(-halfpixel.x, -halfpixel.y) * radius) * 2.0; - gl_FragColor = sum / 12.0; + gl_FragColor = sum / 12.0; } )#"; @@ -348,10 +236,8 @@ void main() { vec4 pixColor = texture2D(texture0, v_texcoord); - if (discardOpaque == 1 && pixColor[3] * alpha == 1.0) { + if (discardOpaque == 1 && pixColor[3] * alpha == 1.0) discard; - return; - } if (applyTint == 1) { pixColor[0] = pixColor[0] * tint[0]; @@ -359,10 +245,7 @@ void main() { pixColor[2] = pixColor[2] * tint[2]; } - vec2 pixCoord = fullSize * v_texcoord; - - )#" + ROUNDED_SHADER_FUNC("pixColor") + - R"#( + )#" + ROUNDED_SHADER_FUNC("pixColor") + R"#( gl_FragColor = pixColor * alpha; })#"; From 0549aa193f39e5f868eb9efcdb0532ebe9387e49 Mon Sep 17 00:00:00 2001 From: Felix Dick Date: Wed, 28 Sep 2022 23:33:18 +0200 Subject: [PATCH 3/8] fixing your shit. --- src/render/shaders/Textures.hpp | 158 ++++++++++++++++---------------- 1 file changed, 80 insertions(+), 78 deletions(-) diff --git a/src/render/shaders/Textures.hpp b/src/render/shaders/Textures.hpp index 16905e3b..50a68e02 100644 --- a/src/render/shaders/Textures.hpp +++ b/src/render/shaders/Textures.hpp @@ -5,38 +5,38 @@ inline static constexpr auto ROUNDED_SHADER_FUNC = [](const std::string colorVarName) -> std::string { return R"#( - // branchless baby! - vec2 pixCoord = v_texcoord - vec2(0.5); - pixCoord *= (vec2(lessThan(pixCoord, vec2(0.0))) * vec2(-2.0) + vec2(1.0)) * fullSize; - pixCoord -= (bottomRight - topLeft) * vec2(0.5); + // branchless baby! + vec2 pixCoord = v_texcoord - vec2(0.5); + pixCoord *= (vec2(lessThan(pixCoord, vec2(0.0))) * vec2(-2.0) + vec2(1.0)) * fullSize; + pixCoord -= (bottomRight - topLeft) * vec2(0.5); - if (all(greaterThan(pixCoord, vec2(0.0)))) { + if (all(greaterThan(pixCoord, vec2(0.0)))) { - if (ignoreCorners == 1) - discard; + if (ignoreCorners == 1) + discard; - float dist = length(pixCoord); + float dist = length(pixCoord); - if (dist > radius) - discard; + if (dist > radius) + discard; - if (primitiveMultisample == 1 && dist > radius - 1.0) { - float distances = 0.0; - if (length(pixCoord + vec2(0.25, 0.25)) < radius) { distances = distances + 1.0; } - if (length(pixCoord + vec2(0.75, 0.25)) < radius) { distances = distances + 1.0; } - if (length(pixCoord + vec2(0.25, 0.75)) < radius) { distances = distances + 1.0; } - if (length(pixCoord + vec2(0.75, 0.75)) < radius) { distances = distances + 1.0; } + if (primitiveMultisample == 1 && dist > radius - 1.0) { + float distances = 0.0; + if (length(pixCoord + vec2(0.25, 0.25)) < radius) { distances = distances + 1.0; } + if (length(pixCoord + vec2(0.75, 0.25)) < radius) { distances = distances + 1.0; } + if (length(pixCoord + vec2(0.25, 0.75)) < radius) { distances = distances + 1.0; } + if (length(pixCoord + vec2(0.75, 0.75)) < radius) { distances = distances + 1.0; } - if (distances == 0.0) - discard; + if (distances == 0.0) + discard; - distances = distances / 4.0; + distances = distances / 4.0; - )#" + colorVarName + R"#( = )#" + colorVarName + R"#( * distances; - } + )#" + colorVarName + R"#( = )#" + colorVarName + R"#( * distances; + } - } - )#"; + } +)#"; }; inline const std::string QUADVERTSRC = R"#( @@ -67,13 +67,14 @@ uniform int primitiveMultisample; uniform int ignoreCorners; void main() { - vec4 pixColor = v_color; - if (radius > 0.0) { - )#" + ROUNDED_SHADER_FUNC("pixColor") + R"#( - } + vec4 pixColor = v_color; - gl_FragColor = pixColor; + if (radius > 0.0) { + )#" + ROUNDED_SHADER_FUNC("pixColor") + R"#( + } + + gl_FragColor = pixColor; })#"; inline const std::string TEXVERTSRC = R"#( @@ -83,8 +84,8 @@ attribute vec2 texcoord; varying vec2 v_texcoord; void main() { - gl_Position = vec4(proj * vec3(pos, 1.0), 1.0); - v_texcoord = texcoord; + gl_Position = vec4(proj * vec3(pos, 1.0), 1.0); + v_texcoord = texcoord; })#"; // this is texture rendering!! @@ -109,21 +110,21 @@ uniform int ignoreCorners; void main() { - vec4 pixColor = texture2D(tex, v_texcoord); + vec4 pixColor = texture2D(tex, v_texcoord); - if (discardOpaque == 1 && pixColor[3] * alpha == 1.0) - discard; + if (discardOpaque == 1 && pixColor[3] * alpha == 1.0) + discard; - if (applyTint == 1) { - pixColor[0] = pixColor[0] * tint[0]; - pixColor[1] = pixColor[1] * tint[1]; - pixColor[2] = pixColor[2] * tint[2]; - } + if (applyTint == 1) { + pixColor[0] = pixColor[0] * tint[0]; + pixColor[1] = pixColor[1] * tint[1]; + pixColor[2] = pixColor[2] * tint[2]; + } - )#" + ROUNDED_SHADER_FUNC("pixColor") + R"#( + )#" + ROUNDED_SHADER_FUNC("pixColor") + R"#( - gl_FragColor = pixColor * alpha; + gl_FragColor = pixColor * alpha; })#"; inline const std::string TEXFRAGSRCRGBX = R"#( @@ -147,20 +148,20 @@ uniform int ignoreCorners; void main() { - if (discardOpaque == 1 && alpha == 1.0) - discard; + if (discardOpaque == 1 && alpha == 1.0) + discard; - vec4 pixColor = vec4(texture2D(tex, v_texcoord).rgb, 1.0); + vec4 pixColor = vec4(texture2D(tex, v_texcoord).rgb, 1.0); - if (applyTint == 1) { - pixColor[0] = pixColor[0] * tint[0]; - pixColor[1] = pixColor[1] * tint[1]; - pixColor[2] = pixColor[2] * tint[2]; - } + if (applyTint == 1) { + pixColor[0] = pixColor[0] * tint[0]; + pixColor[1] = pixColor[1] * tint[1]; + pixColor[2] = pixColor[2] * tint[2]; + } - )#" + ROUNDED_SHADER_FUNC("pixColor") + R"#( + )#" + ROUNDED_SHADER_FUNC("pixColor") + R"#( - gl_FragColor = pixColor * alpha; + gl_FragColor = pixColor * alpha; })#"; inline const std::string FRAGBLUR1 = R"#( @@ -173,15 +174,15 @@ uniform float radius; uniform vec2 halfpixel; void main() { - vec2 uv = v_texcoord * 2.0; + vec2 uv = v_texcoord * 2.0; - vec4 sum = texture2D(tex, uv) * 4.0; - sum += texture2D(tex, uv - halfpixel.xy * radius); - sum += texture2D(tex, uv + halfpixel.xy * radius); - sum += texture2D(tex, uv + vec2(halfpixel.x, -halfpixel.y) * radius); - sum += texture2D(tex, uv - vec2(halfpixel.x, -halfpixel.y) * radius); + vec4 sum = texture2D(tex, uv) * 4.0; + sum += texture2D(tex, uv - halfpixel.xy * radius); + sum += texture2D(tex, uv + halfpixel.xy * radius); + sum += texture2D(tex, uv + vec2(halfpixel.x, -halfpixel.y) * radius); + sum += texture2D(tex, uv - vec2(halfpixel.x, -halfpixel.y) * radius); - gl_FragColor = sum / 8.0; + gl_FragColor = sum / 8.0; } )#"; @@ -195,19 +196,19 @@ uniform float radius; uniform vec2 halfpixel; void main() { - vec2 uv = v_texcoord / 2.0; + vec2 uv = v_texcoord / 2.0; - vec4 sum = texture2D(tex, uv + vec2(-halfpixel.x * 2.0, 0.0) * radius); + vec4 sum = texture2D(tex, uv + vec2(-halfpixel.x * 2.0, 0.0) * radius); - sum += texture2D(tex, uv + vec2(-halfpixel.x, halfpixel.y) * radius) * 2.0; - sum += texture2D(tex, uv + vec2(0.0, halfpixel.y * 2.0) * radius); - sum += texture2D(tex, uv + vec2(halfpixel.x, halfpixel.y) * radius) * 2.0; - sum += texture2D(tex, uv + vec2(halfpixel.x * 2.0, 0.0) * radius); - sum += texture2D(tex, uv + vec2(halfpixel.x, -halfpixel.y) * radius) * 2.0; - sum += texture2D(tex, uv + vec2(0.0, -halfpixel.y * 2.0) * radius); - sum += texture2D(tex, uv + vec2(-halfpixel.x, -halfpixel.y) * radius) * 2.0; + sum += texture2D(tex, uv + vec2(-halfpixel.x, halfpixel.y) * radius) * 2.0; + sum += texture2D(tex, uv + vec2(0.0, halfpixel.y * 2.0) * radius); + sum += texture2D(tex, uv + vec2(halfpixel.x, halfpixel.y) * radius) * 2.0; + sum += texture2D(tex, uv + vec2(halfpixel.x * 2.0, 0.0) * radius); + sum += texture2D(tex, uv + vec2(halfpixel.x, -halfpixel.y) * radius) * 2.0; + sum += texture2D(tex, uv + vec2(0.0, -halfpixel.y * 2.0) * radius); + sum += texture2D(tex, uv + vec2(-halfpixel.x, -halfpixel.y) * radius) * 2.0; - gl_FragColor = sum / 12.0; + gl_FragColor = sum / 12.0; } )#"; @@ -234,18 +235,19 @@ uniform int ignoreCorners; void main() { - vec4 pixColor = texture2D(texture0, v_texcoord); + vec4 pixColor = texture2D(texture0, v_texcoord); - if (discardOpaque == 1 && pixColor[3] * alpha == 1.0) - discard; + if (discardOpaque == 1 && pixColor[3] * alpha == 1.0) + discard; - if (applyTint == 1) { - pixColor[0] = pixColor[0] * tint[0]; - pixColor[1] = pixColor[1] * tint[1]; - pixColor[2] = pixColor[2] * tint[2]; - } + if (applyTint == 1) { + pixColor[0] = pixColor[0] * tint[0]; + pixColor[1] = pixColor[1] * tint[1]; + pixColor[2] = pixColor[2] * tint[2]; + } - )#" + ROUNDED_SHADER_FUNC("pixColor") + R"#( + )#" + ROUNDED_SHADER_FUNC("pixColor") + R"#( - gl_FragColor = pixColor * alpha; -})#"; + gl_FragColor = pixColor * alpha; +} +)#"; From 65fb526d5c3ef0bfab83f6fdc2e69270c4522448 Mon Sep 17 00:00:00 2001 From: Felix Dick Date: Thu, 29 Sep 2022 06:56:17 +0200 Subject: [PATCH 4/8] Even less branching (taken more or less from the border shader). --- src/render/shaders/Textures.hpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/render/shaders/Textures.hpp b/src/render/shaders/Textures.hpp index 50a68e02..a541ec0a 100644 --- a/src/render/shaders/Textures.hpp +++ b/src/render/shaders/Textures.hpp @@ -22,15 +22,15 @@ inline static constexpr auto ROUNDED_SHADER_FUNC = [](const std::string colorVar if (primitiveMultisample == 1 && dist > radius - 1.0) { float distances = 0.0; - if (length(pixCoord + vec2(0.25, 0.25)) < radius) { distances = distances + 1.0; } - if (length(pixCoord + vec2(0.75, 0.25)) < radius) { distances = distances + 1.0; } - if (length(pixCoord + vec2(0.25, 0.75)) < radius) { distances = distances + 1.0; } - if (length(pixCoord + vec2(0.75, 0.75)) < radius) { distances = distances + 1.0; } + distances += float(length(pixCoord + vec2(0.25, 0.25)) < radius); + distances += float(length(pixCoord + vec2(0.75, 0.25)) < radius); + distances += float(length(pixCoord + vec2(0.25, 0.75)) < radius); + distances += float(length(pixCoord + vec2(0.75, 0.75)) < radius); if (distances == 0.0) discard; - distances = distances / 4.0; + distances /= 4.0; )#" + colorVarName + R"#( = )#" + colorVarName + R"#( * distances; } From b38e7b596f03dc6f7f6adec36df5bea2325642b5 Mon Sep 17 00:00:00 2001 From: Felix Dick Date: Thu, 29 Sep 2022 21:10:05 +0200 Subject: [PATCH 5/8] Don't pass bottomRight to textureShaders compute it within. --- src/render/OpenGL.cpp | 4 ---- src/render/shaders/Textures.hpp | 6 +----- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/render/OpenGL.cpp b/src/render/OpenGL.cpp index faf91d37..02100b38 100644 --- a/src/render/OpenGL.cpp +++ b/src/render/OpenGL.cpp @@ -152,7 +152,6 @@ void CHyprOpenGLImpl::initShaders() { m_RenderData.pCurrentMonData->m_shQUAD.posAttrib = glGetAttribLocation(prog, "pos"); m_RenderData.pCurrentMonData->m_shQUAD.texAttrib = glGetAttribLocation(prog, "texcoord"); m_RenderData.pCurrentMonData->m_shQUAD.topLeft = glGetUniformLocation(prog, "topLeft"); - m_RenderData.pCurrentMonData->m_shQUAD.bottomRight = glGetUniformLocation(prog, "bottomRight"); m_RenderData.pCurrentMonData->m_shQUAD.fullSize = glGetUniformLocation(prog, "fullSize"); m_RenderData.pCurrentMonData->m_shQUAD.radius = glGetUniformLocation(prog, "radius"); m_RenderData.pCurrentMonData->m_shQUAD.primitiveMultisample = glGetUniformLocation(prog, "primitiveMultisample"); @@ -166,7 +165,6 @@ void CHyprOpenGLImpl::initShaders() { m_RenderData.pCurrentMonData->m_shRGBA.posAttrib = glGetAttribLocation(prog, "pos"); m_RenderData.pCurrentMonData->m_shRGBA.discardOpaque = glGetUniformLocation(prog, "discardOpaque"); m_RenderData.pCurrentMonData->m_shRGBA.topLeft = glGetUniformLocation(prog, "topLeft"); - m_RenderData.pCurrentMonData->m_shRGBA.bottomRight = glGetUniformLocation(prog, "bottomRight"); m_RenderData.pCurrentMonData->m_shRGBA.fullSize = glGetUniformLocation(prog, "fullSize"); m_RenderData.pCurrentMonData->m_shRGBA.radius = glGetUniformLocation(prog, "radius"); m_RenderData.pCurrentMonData->m_shRGBA.primitiveMultisample = glGetUniformLocation(prog, "primitiveMultisample"); @@ -182,7 +180,6 @@ void CHyprOpenGLImpl::initShaders() { m_RenderData.pCurrentMonData->m_shRGBX.posAttrib = glGetAttribLocation(prog, "pos"); m_RenderData.pCurrentMonData->m_shRGBX.discardOpaque = glGetUniformLocation(prog, "discardOpaque"); m_RenderData.pCurrentMonData->m_shRGBX.topLeft = glGetUniformLocation(prog, "topLeft"); - m_RenderData.pCurrentMonData->m_shRGBX.bottomRight = glGetUniformLocation(prog, "bottomRight"); m_RenderData.pCurrentMonData->m_shRGBX.fullSize = glGetUniformLocation(prog, "fullSize"); m_RenderData.pCurrentMonData->m_shRGBX.radius = glGetUniformLocation(prog, "radius"); m_RenderData.pCurrentMonData->m_shRGBX.primitiveMultisample = glGetUniformLocation(prog, "primitiveMultisample"); @@ -198,7 +195,6 @@ void CHyprOpenGLImpl::initShaders() { m_RenderData.pCurrentMonData->m_shEXT.texAttrib = glGetAttribLocation(prog, "texcoord"); m_RenderData.pCurrentMonData->m_shEXT.discardOpaque = glGetUniformLocation(prog, "discardOpaque"); m_RenderData.pCurrentMonData->m_shEXT.topLeft = glGetUniformLocation(prog, "topLeft"); - m_RenderData.pCurrentMonData->m_shEXT.bottomRight = glGetUniformLocation(prog, "bottomRight"); m_RenderData.pCurrentMonData->m_shEXT.fullSize = glGetUniformLocation(prog, "fullSize"); m_RenderData.pCurrentMonData->m_shEXT.radius = glGetUniformLocation(prog, "radius"); m_RenderData.pCurrentMonData->m_shEXT.primitiveMultisample = glGetUniformLocation(prog, "primitiveMultisample"); diff --git a/src/render/shaders/Textures.hpp b/src/render/shaders/Textures.hpp index a541ec0a..42bd0856 100644 --- a/src/render/shaders/Textures.hpp +++ b/src/render/shaders/Textures.hpp @@ -6,6 +6,7 @@ inline static constexpr auto ROUNDED_SHADER_FUNC = [](const std::string colorVar return R"#( // branchless baby! + vec2 bottomRight = fullSize - radius; vec2 pixCoord = v_texcoord - vec2(0.5); pixCoord *= (vec2(lessThan(pixCoord, vec2(0.0))) * vec2(-2.0) + vec2(1.0)) * fullSize; pixCoord -= (bottomRight - topLeft) * vec2(0.5); @@ -59,7 +60,6 @@ varying vec4 v_color; varying vec2 v_texcoord; uniform vec2 topLeft; -uniform vec2 bottomRight; uniform vec2 fullSize; uniform float radius; @@ -88,7 +88,6 @@ void main() { v_texcoord = texcoord; })#"; -// this is texture rendering!! inline const std::string TEXFRAGSRCRGBA = R"#( precision mediump float; varying vec2 v_texcoord; // is in 0-1 @@ -96,7 +95,6 @@ uniform sampler2D tex; uniform float alpha; uniform vec2 topLeft; -uniform vec2 bottomRight; uniform vec2 fullSize; uniform float radius; @@ -134,7 +132,6 @@ uniform sampler2D tex; uniform float alpha; uniform vec2 topLeft; -uniform vec2 bottomRight; uniform vec2 fullSize; uniform float radius; @@ -221,7 +218,6 @@ uniform samplerExternalOES texture0; uniform float alpha; uniform vec2 topLeft; -uniform vec2 bottomRight; uniform vec2 fullSize; uniform float radius; From 6eb7d0038644fab09c8512ad34cdcbde1882ffc0 Mon Sep 17 00:00:00 2001 From: Felix Dick Date: Thu, 29 Sep 2022 23:19:56 +0200 Subject: [PATCH 6/8] Send absolute screen coordinates to texture shaders. --- src/render/OpenGL.cpp | 9 +++------ src/render/shaders/Textures.hpp | 8 ++++---- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/render/OpenGL.cpp b/src/render/OpenGL.cpp index 02100b38..29b479b6 100644 --- a/src/render/OpenGL.cpp +++ b/src/render/OpenGL.cpp @@ -334,15 +334,13 @@ void CHyprOpenGLImpl::renderRectWithDamage(wlr_box* box, const CColor& col, pixm glUniformMatrix3fv(m_RenderData.pCurrentMonData->m_shQUAD.proj, 1, GL_FALSE, glMatrix); glUniform4f(m_RenderData.pCurrentMonData->m_shQUAD.color, col.r / 255.f, col.g / 255.f, col.b / 255.f, col.a / 255.f); - const auto TOPLEFT = Vector2D(round, round); - const auto BOTTOMRIGHT = Vector2D(box->width - round, box->height - round); + const auto TOPLEFT = Vector2D(box->x, box->y); const auto FULLSIZE = Vector2D(box->width, box->height); static auto *const PMULTISAMPLEEDGES = &g_pConfigManager->getConfigValuePtr("decoration:multisample_edges")->intValue; // Rounded corners glUniform2f(m_RenderData.pCurrentMonData->m_shQUAD.topLeft, (float)TOPLEFT.x, (float)TOPLEFT.y); - glUniform2f(m_RenderData.pCurrentMonData->m_shQUAD.bottomRight, (float)BOTTOMRIGHT.x, (float)BOTTOMRIGHT.y); glUniform2f(m_RenderData.pCurrentMonData->m_shQUAD.fullSize, (float)FULLSIZE.x, (float)FULLSIZE.y); glUniform1f(m_RenderData.pCurrentMonData->m_shQUAD.radius, round); glUniform1i(m_RenderData.pCurrentMonData->m_shQUAD.primitiveMultisample, (int)(*PMULTISAMPLEEDGES == 1 && round != 0)); @@ -438,9 +436,8 @@ void CHyprOpenGLImpl::renderTextureInternalWithDamage(const CTexture& tex, wlr_b static auto *const PMULTISAMPLEEDGES = &g_pConfigManager->getConfigValuePtr("decoration:multisample_edges")->intValue; // Rounded corners - glUniform2f(shader->topLeft, (float)TOPLEFT.x, (float)TOPLEFT.y); - glUniform2f(shader->bottomRight, (float)BOTTOMRIGHT.x, (float)BOTTOMRIGHT.y); - glUniform2f(shader->fullSize, (float)FULLSIZE.x, (float)FULLSIZE.y); + glUniform2f(shader->topLeft, pBox->x, pBox->y); + glUniform2f(shader->fullSize, pBox->width, pBox->height); glUniform1f(shader->radius, round); glUniform1i(shader->primitiveMultisample, (int)(*PMULTISAMPLEEDGES == 1 && round != 0 && !noAA)); diff --git a/src/render/shaders/Textures.hpp b/src/render/shaders/Textures.hpp index 42bd0856..a7c53aea 100644 --- a/src/render/shaders/Textures.hpp +++ b/src/render/shaders/Textures.hpp @@ -6,10 +6,10 @@ inline static constexpr auto ROUNDED_SHADER_FUNC = [](const std::string colorVar return R"#( // branchless baby! - vec2 bottomRight = fullSize - radius; - vec2 pixCoord = v_texcoord - vec2(0.5); - pixCoord *= (vec2(lessThan(pixCoord, vec2(0.0))) * vec2(-2.0) + vec2(1.0)) * fullSize; - pixCoord -= (bottomRight - topLeft) * vec2(0.5); + highp vec2 pixCoord = vec2(gl_FragCoord); + pixCoord -= (topLeft + fullSize / vec2(2.0)); + pixCoord *= (vec2(lessThan(pixCoord, vec2(0.0))) * vec2(-2.0) + vec2(1.0)); + pixCoord -= fullSize * vec2(0.5) - vec2(radius); if (all(greaterThan(pixCoord, vec2(0.0)))) { From 6fe103cf06e99e4cd8c03e616b5a939067e7e165 Mon Sep 17 00:00:00 2001 From: Felix Dick Date: Sat, 1 Oct 2022 02:45:36 +0200 Subject: [PATCH 7/8] Cut the number of pixels that call length() in half. --- src/render/shaders/Textures.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/render/shaders/Textures.hpp b/src/render/shaders/Textures.hpp index 1e8281d0..de5f0358 100644 --- a/src/render/shaders/Textures.hpp +++ b/src/render/shaders/Textures.hpp @@ -7,11 +7,11 @@ inline static constexpr auto ROUNDED_SHADER_FUNC = [](const std::string colorVar // branchless baby! highp vec2 pixCoord = vec2(gl_FragCoord); - pixCoord -= topLeft + fullSize / vec2(2.0); - pixCoord *= vec2(lessThan(pixCoord, vec2(0.0))) * vec2(-2.0) + vec2(1.0); - pixCoord -= fullSize * vec2(0.5) - vec2(radius); + pixCoord -= topLeft + fullSize * 0.5; + pixCoord *= vec2(lessThan(pixCoord, vec2(0.0))) * -2.0 + 1.0; + pixCoord -= fullSize * 0.5 - radius; - if (all(greaterThan(pixCoord, vec2(0.0)))) { + if (pixCoord.x + pixCoord.y > radius) { if (ignoreCorners == 1) discard; From c6333ba79683792a6802606d524636649917ee7d Mon Sep 17 00:00:00 2001 From: Felix Dick Date: Sat, 1 Oct 2022 03:30:58 +0200 Subject: [PATCH 8/8] Remove unused ignoreCorners variable from texture shaders. --- src/render/shaders/Textures.hpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/render/shaders/Textures.hpp b/src/render/shaders/Textures.hpp index de5f0358..94abeb0d 100644 --- a/src/render/shaders/Textures.hpp +++ b/src/render/shaders/Textures.hpp @@ -13,9 +13,6 @@ inline static constexpr auto ROUNDED_SHADER_FUNC = [](const std::string colorVar if (pixCoord.x + pixCoord.y > radius) { - if (ignoreCorners == 1) - discard; - float dist = length(pixCoord); if (dist > radius) @@ -64,7 +61,6 @@ uniform vec2 fullSize; uniform float radius; uniform int primitiveMultisample; -uniform int ignoreCorners; void main() { @@ -104,7 +100,6 @@ uniform int applyTint; uniform vec3 tint; uniform int primitiveMultisample; -uniform int ignoreCorners; void main() { @@ -141,7 +136,6 @@ uniform int applyTint; uniform vec3 tint; uniform int primitiveMultisample; -uniform int ignoreCorners; void main() { @@ -227,7 +221,6 @@ uniform int applyTint; uniform vec3 tint; uniform int primitiveMultisample; -uniform int ignoreCorners; void main() {