diff options
author | Felix Dick <[email protected]> | 2022-09-28 14:50:27 +0200 |
---|---|---|
committer | Felix Dick <[email protected]> | 2022-09-28 18:40:04 +0200 |
commit | 7edbaea23de36182fb57a634b95f671c353f8aa7 (patch) | |
tree | 5714b843ebf4a5143c6f085764779bd874bb7dcb /src/render/shaders | |
parent | 5ff44467d77c2167cdefbf81014b349c97fe10f1 (diff) | |
download | Hyprland-7edbaea23de36182fb57a634b95f671c353f8aa7.tar.gz Hyprland-7edbaea23de36182fb57a634b95f671c353f8aa7.zip |
Make the rounding texture shaders smaller and more efficient.
Diffstat (limited to 'src/render/shaders')
-rw-r--r-- | src/render/shaders/Textures.hpp | 227 |
1 files changed, 55 insertions, 172 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 <string> 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) { - discard; - return; - } - - float topLeftDistance = distance(topLeft, pixCoord); - - 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; - } - } - } - } - else if (pixCoord[0] > bottomRight[0]) { - // we're close right - if (pixCoord[1] < topLeft[1]) { - // top + // 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 (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) { + 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(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; - } - } + + 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; })#"; |