-
-
Notifications
You must be signed in to change notification settings - Fork 77
Draw rounded rectangles using shader #2463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
40dfa00
WIP
lenemter 30599ea
"Fix" shader
lenemter 2b3375c
Slight tweaks
lenemter c4c563f
It's done
lenemter 00032fa
Cleanup
lenemter 9ace03c
Port ActiveShape and WindowSwitcherIcon
lenemter b7f4788
Fix lint
lenemter 8f050ae
Fix Fedora build
lenemter 2b23e48
Merge branch 'main' into lenemter/optimize-rounded
zeebok 57399bb
Use LGPL. Reduce diff
lenemter 369a8c8
Disconnect signals
lenemter 4e62391
Oops, fix naming
lenemter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // based on shader from mutter | ||
|
|
||
| uniform sampler2D tex; | ||
| uniform float clip_radius; | ||
| uniform vec2 actor_size; | ||
|
|
||
| float rounded_rect_coverage (vec2 p) { | ||
| float center_left = clip_radius + 1.5; | ||
| float center_right = actor_size.x - clip_radius - 0.55; | ||
| float center_x; | ||
|
|
||
| if (p.x < center_left) | ||
| center_x = center_left; | ||
| else if (p.x >= center_right) | ||
| center_x = center_right; | ||
| else | ||
| return 1.0; | ||
|
|
||
| float center_top = clip_radius + 1.5; | ||
| float center_bottom = actor_size.y - clip_radius - 0.55; | ||
| float center_y; | ||
|
|
||
| if (p.y < center_top) | ||
| center_y = center_top; | ||
| else if (p.y > center_bottom) | ||
| center_y = center_bottom; | ||
| else | ||
| return 1.0; | ||
|
|
||
| vec2 delta = p - vec2 (center_x, center_y); | ||
| float dist_squared = dot (delta, delta); | ||
|
|
||
| // Fully outside the circle | ||
| float outer_radius = clip_radius + 0.5; | ||
| if (dist_squared > (outer_radius * outer_radius)) | ||
| return 0.0; | ||
|
|
||
| // Fully inside the circle | ||
| float inner_radius = clip_radius - 0.5; | ||
| if (dist_squared <= (inner_radius * inner_radius)) | ||
| return 1.0; | ||
|
|
||
| // Only pixels on the edge of the curve need expensive antialiasing | ||
| return smoothstep (outer_radius, inner_radius, length (delta)); | ||
| } | ||
|
|
||
| void main () { | ||
| vec4 sample = texture2D (tex, cogl_tex_coord0_in.xy); | ||
|
|
||
| vec2 texture_coord = cogl_tex_coord0_in.xy * actor_size; | ||
| float res = rounded_rect_coverage (texture_coord); | ||
|
|
||
| cogl_color_out = sample * cogl_color_in * res; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * Copyright 2025 elementary, Inc. <https://elementary.io> | ||
| * SPDX-License-Identifier: GPL-3.0-or-later | ||
| */ | ||
|
|
||
| public class Gala.RoundedCornersEffect : Clutter.ShaderEffect { | ||
| private const int CLIP_RADIUS_OFFSET = 3; | ||
|
|
||
| public float clip_radius { | ||
| construct set { | ||
| set_uniform_value ("clip_radius", value + CLIP_RADIUS_OFFSET); | ||
| } | ||
| } | ||
|
|
||
| private float _monitor_scale = 1.0f; | ||
| public float monitor_scale { | ||
| get { | ||
| return _monitor_scale; | ||
| } | ||
| construct set { | ||
| _monitor_scale = value; | ||
|
|
||
| if (actor != null) { | ||
| update_actor_size (); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public RoundedCornersEffect (float clip_radius, float monitor_scale) { | ||
| Object ( | ||
| #if HAS_MUTTER48 | ||
| shader_type: Cogl.ShaderType.FRAGMENT, | ||
| #else | ||
| shader_type: Clutter.ShaderType.FRAGMENT_SHADER, | ||
| #endif | ||
| clip_radius: clip_radius, | ||
| monitor_scale: monitor_scale | ||
| ); | ||
| } | ||
|
|
||
| construct { | ||
| try { | ||
| var bytes = GLib.resources_lookup_data ("/io/elementary/desktop/gala/shaders/rounded-corners.vert", GLib.ResourceLookupFlags.NONE); | ||
| set_shader_source ((string) bytes.get_data ()); | ||
| } catch (Error e) { | ||
| critical ("Unable to load rounded-corners.vert: %s", e.message); | ||
| } | ||
| } | ||
|
|
||
| public override void set_actor (Clutter.Actor? actor) { | ||
| base.set_actor (actor); | ||
|
|
||
| if (actor == null) { | ||
| return; | ||
| } | ||
|
|
||
| actor.notify["width"].connect (update_actor_size); | ||
| actor.notify["height"].connect (update_actor_size); | ||
|
|
||
| update_actor_size (); | ||
| } | ||
|
|
||
| private void update_actor_size () requires (actor != null) { | ||
| float[] actor_size = { | ||
| actor.width * monitor_scale, | ||
| actor.height * monitor_scale | ||
| }; | ||
|
|
||
| var actor_size_value = GLib.Value (typeof (Clutter.ShaderFloat)); | ||
| Clutter.Value.set_shader_float (actor_size_value, actor_size); | ||
| set_uniform_value ("actor_size", actor_size_value); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.