-
-
Notifications
You must be signed in to change notification settings - Fork 79
Introduce and use Gala.SimpleShaderEffect #2776
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
Draft
lenemter
wants to merge
4
commits into
main
Choose a base branch
from
lenemter/rewrite-shader-effect
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,142 @@ | ||
| /* | ||
| * Copyright 2026 elementary, Inc. <https://elementary.io> | ||
| * SPDX-License-Identifier: GPL-3.0-or-later | ||
| */ | ||
|
|
||
| /** | ||
| * {@link Clutter.Effect} implementation to apply shaders quickly and easily. | ||
| * The main difference between Gala.SimpleShaderEffect and Clutter.ShaderEffect is that | ||
| * we don't use {@link Clutter.OffscreenEffect} that enlarges the texture for fractional scaling which | ||
| * can produce some graphical glitches. | ||
| */ | ||
| public abstract class Gala.SimpleShaderEffect : Clutter.Effect { | ||
| /** | ||
| * Fallback shader that outputs the original content. | ||
| */ | ||
| public const string FALLBACK_SHADER = "uniform sampler2D tex; void main () { cogl_color_out = texture2D (tex, cogl_tex_coord0_in.xy); }"; | ||
|
|
||
| private Cogl.Program program; | ||
| private Cogl.Pipeline pipeline; | ||
| private Cogl.Framebuffer framebuffer; | ||
| private Cogl.Texture texture; | ||
| private int texture_width; | ||
| private int texture_height; | ||
|
|
||
| protected SimpleShaderEffect (string shader_source) { | ||
| unowned var ctx = Clutter.get_default_backend ().get_cogl_context (); | ||
|
|
||
| var shader = Cogl.Shader.create (FRAGMENT); | ||
| shader.source (shader_source); | ||
|
|
||
| program = Cogl.Program.create (); | ||
| program.attach_shader (shader); | ||
| program.link (); | ||
|
|
||
| pipeline = new Cogl.Pipeline (ctx); | ||
| pipeline.set_user_program (program); | ||
| } | ||
|
|
||
| private bool update_framebuffer () { | ||
| var actor_box = actor.get_allocation_box (); | ||
| var new_width = (int) actor_box.get_width (); | ||
| var new_height = (int) actor_box.get_height (); | ||
|
|
||
| if (new_width <= 0 || new_height <= 0) { | ||
| warning ("SimpleShaderEffect: Couldn't update framebuffers, incorrect size"); | ||
| return false; | ||
| } | ||
|
|
||
| if (texture_width == new_width && texture_height == new_height && framebuffer != null) { | ||
| return true; | ||
| } | ||
|
|
||
| unowned var ctx = Clutter.get_default_backend ().get_cogl_context (); | ||
|
|
||
| #if HAS_MUTTER46 | ||
| texture = new Cogl.Texture2D.with_size (ctx, new_width, new_height); | ||
| #else | ||
| var surface = new Cairo.ImageSurface (Cairo.Format.ARGB32, new_width, new_height); | ||
| try { | ||
| texture = new Cogl.Texture2D.from_data (ctx, new_width, new_height, Cogl.PixelFormat.BGRA_8888_PRE, surface.get_stride (), surface.get_data ()); | ||
| } catch (Error e) { | ||
| critical ("SimpleShaderEffect: Couldn't create texture: %s", e.message); | ||
| return false; | ||
| } | ||
| #endif | ||
|
|
||
| pipeline.set_layer_texture (0, texture); | ||
| framebuffer = new Cogl.Offscreen.with_texture (texture); | ||
|
|
||
| Graphene.Matrix projection = {}; | ||
| projection.init_translate ({ -new_width / 2.0f, -new_height / 2.0f, 0.0f }); | ||
| projection.scale (2.0f / new_width, -2.0f / new_height, 1.0f); | ||
|
|
||
| framebuffer.set_projection_matrix (projection); | ||
|
|
||
| texture_width = new_width; | ||
| texture_height = new_height; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| public override void paint_node (Clutter.PaintNode node, Clutter.PaintContext paint_context, Clutter.EffectPaintFlags flags) { | ||
| var actor_node = new Clutter.ActorNode (actor, 255); | ||
|
|
||
| if (BYPASS_EFFECT in flags || !update_framebuffer ()) { | ||
| node.add_child (actor_node); | ||
| return; | ||
| } | ||
|
|
||
| var layer_node = new Clutter.LayerNode.to_framebuffer (framebuffer, pipeline); | ||
| layer_node.add_rectangle ({0, 0, texture_width, texture_height}); | ||
| layer_node.add_child (actor_node); | ||
|
|
||
| node.add_child (layer_node); | ||
| } | ||
|
|
||
| private bool get_and_validate_uniform_location (string uniform, out int uniform_location) { | ||
| uniform_location = program.get_uniform_location (uniform); | ||
|
|
||
| if (uniform_location == -1) { | ||
| warning ("Can't update uniform '%s'", uniform); | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| protected void set_uniform_1f (string uniform, float value) { | ||
| int uniform_location; | ||
| if (get_and_validate_uniform_location (uniform, out uniform_location)) { | ||
| program.set_uniform_1f (uniform_location, value); | ||
| } | ||
| } | ||
|
|
||
| protected void set_uniform_1i (string uniform, int value) { | ||
| int uniform_location; | ||
| if (get_and_validate_uniform_location (uniform, out uniform_location)) { | ||
| program.set_uniform_1i (uniform_location, value); | ||
| } | ||
| } | ||
|
|
||
| protected void set_uniform_float (string uniform, int n_components, float[] value) { | ||
| int uniform_location; | ||
| if (get_and_validate_uniform_location (uniform, out uniform_location)) { | ||
| program.set_uniform_float (uniform_location, n_components, value); | ||
| } | ||
| } | ||
|
|
||
| protected void set_uniform_int (string uniform, int n_components, int[] value) { | ||
| int uniform_location; | ||
| if (get_and_validate_uniform_location (uniform, out uniform_location)) { | ||
| program.set_uniform_int (uniform_location, n_components, value); | ||
| } | ||
| } | ||
|
|
||
| protected void set_uniform_matrix (string uniform, int dimensions, bool transpose, float[] value) { | ||
| int uniform_location; | ||
| if (get_and_validate_uniform_location (uniform, out uniform_location)) { | ||
| program.set_uniform_matrix (uniform_location, dimensions, transpose, 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ig there must be a reason why they do this could you maybe explain why we don't need it in this case?