Skip to content
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

Add time uniform to glsl shaders #330

Merged
merged 3 commits into from
Mar 10, 2020
Merged

Add time uniform to glsl shaders #330

merged 3 commits into from
Mar 10, 2020

Conversation

resloved
Copy link
Contributor

@resloved resloved commented Mar 9, 2020

Added an updating uniform that can be used to animate GLSL shaders. As discussed in #295

After looking at the different options available already in picom I was able to have a pretty limited changeset to get the effect I wanted. If you want to test it you can use this shader I wrote:

uniform float opacity;
uniform bool invert_color;
uniform sampler2D tex;
uniform float time;

float amt = 2000.0;

void main() {
        float pct = mod(time, amt)/amt;
        vec2 pos = gl_TexCoord[0].st;
        vec4 c = texture2D(tex, pos);
        float a = pos.x + pos.y;
        if (a < pct * 4.0 && a > pct * 4.0 - 0.5 * pct)
           c *= vec4(2, 2, 2, 1);
        else if (a < pct * 4.0 - 0.8 * pct && a > pct * 3.0)
           c *= vec4(2, 2, 2, 1);
        if (invert_color)
           c = vec4(vec3(c.a, c.a, c.a) - vec3(c), c.a);
        c *= opacity;
        gl_FragColor = c;
}

Then run picom with these changes and the following flags (This assumes you have the previous shader saved to ~/shine.glsl):

picom --sw-opti --benchmark 100000 --no-use-damage --backend glx --glx-fshader-win "$(cat ~/shine.glsl)"

It should look something like this:

https://streamable.com/e0x7r

Some issues:

  • Using benchmark is obviously really hacky. Changes from the shader won't queue a redraw, so we can't wait on draw_idle to be enabled. Either these changes would need to affect draw_idle or there would need to be a nicer way to ignore it.
  • Similarly I have to ignore damage as changes from the shader are ignored. Not being idle can really tear up performance and finding a solution is probably necessary. Luckily --sw-opti cuts the cost significantly for me, but it doesn't fix everything.

I really think with a few more options you could do some pretty powerful stuff, but it might be out of the scope of this pull request. Some basic stuff that comes to mind:

  • Timer since window was created
  • Timer since window was focused/last focused
  • Window dimensions
  • And the numerous other values that a window can have (class, name, etc.)

@yshui
Copy link
Owner

yshui commented Mar 9, 2020

Looks mostly fine, can you fix the build failures?

@yshui
Copy link
Owner

yshui commented Mar 9, 2020

What do you think is better for animation, time or frame count? Should we have both?

@resloved
Copy link
Contributor Author

resloved commented Mar 9, 2020

Traditionally using the time is better as its consistent across different levels of performance and refresh rate. Ideally it lets you write one shader that works across platforms.

@yshui
Copy link
Owner

yshui commented Mar 10, 2020

Thanks, I see no problem of this.

It would be nice if you can also implement this for the new glx backend.

@yshui yshui merged commit 9547d7a into yshui:next Mar 10, 2020
@sabrehagen
Copy link

These days the glsl code in the first post of this PR gets the following error:

[ 08/23/2023 12:55:32.616 gl_win_shader_from_stringv ERROR ] Failed to create GLSL program.
[ 08/23/2023 12:55:32.616 initialize_backend WARN ] Failed to create shader for shader file /home/jackson/.config/picom/shaders/shine.glsl, this shader will not be used

config:

backend = "glx"
window-shader-fg-rule = [
  "shine.glsl:class_g = 'XTerm'"
];

Is there anything else I need to do? Or is the code the issue, and no longer supported?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants