forge
Explore
Log in

Don't you dare go Hollow

Change custom-shader to a prelude-epilogue system

Browse files at this commit

1681ed16d91757d98dc3eb653b970e706ff84b78Ivan MolodetskikhParent: d4bed70
src/render_helpers/primary_gpu_pixel_shader_with_textures.rsmodified+2 -2
@@ -63,8 +63,8 @@ unsafe fn compile_program(
6363 let vert_position = CStr::from_bytes_with_nul(b"vert_position\0").expect("NULL terminated");
6464 let matrix = CStr::from_bytes_with_nul(b"matrix\0").expect("NULL terminated");
6565 let tex_matrix = CStr::from_bytes_with_nul(b"tex_matrix\0").expect("NULL terminated");
66- let size = CStr::from_bytes_with_nul(b"size\0").expect("NULL terminated");
67- let alpha = CStr::from_bytes_with_nul(b"alpha\0").expect("NULL terminated");
66+ let size = CStr::from_bytes_with_nul(b"niri_size\0").expect("NULL terminated");
67+ let alpha = CStr::from_bytes_with_nul(b"niri_alpha\0").expect("NULL terminated");
6868
6969 Ok(PixelWithTexturesProgram(Rc::new(
7070 PixelWithTexturesProgramInner {
src/render_helpers/resize.rsmodified+12 -13
@@ -74,10 +74,9 @@ impl ResizeRenderElement {
7474 // Compute the transformation matrices.
7575 let input_to_curr_geo = Mat3::from_scale(area_size / curr_geo_size)
7676 * Mat3::from_translation((area_loc - curr_geo_loc) / area_size);
77- let input_to_prev_geo = Mat3::from_scale(area_size / size_prev)
78- * Mat3::from_translation((area_loc - curr_geo_loc) / area_size);
79- let input_to_next_geo = Mat3::from_scale(area_size / size_next)
80- * Mat3::from_translation((area_loc - curr_geo_loc) / area_size);
77+
78+ let curr_geo_to_prev_geo = Mat3::from_scale(curr_geo_size / size_prev);
79+ let curr_geo_to_next_geo = Mat3::from_scale(curr_geo_size / size_next);
8180
8281 let geo_to_tex_prev = Mat3::from_translation(-tex_prev_geo_loc / tex_prev_geo_size)
8382 * Mat3::from_scale(size_prev / tex_prev_geo_size * scale);
@@ -99,21 +98,21 @@ impl ResizeRenderElement {
9998 Self(PrimaryGpuPixelShaderWithTexturesRenderElement::new(
10099 shader,
101100 HashMap::from([
102- (String::from("tex_prev"), texture_prev),
103- (String::from("tex_next"), texture_next),
101+ (String::from("niri_tex_prev"), texture_prev),
102+ (String::from("niri_tex_next"), texture_next),
104103 ]),
105104 area,
106105 size,
107106 None,
108107 result_alpha,
109108 vec![
110- make_uniform("input_to_curr_geo", input_to_curr_geo),
111- make_uniform("input_to_prev_geo", input_to_prev_geo),
112- make_uniform("input_to_next_geo", input_to_next_geo),
113- make_uniform("geo_to_tex_prev", geo_to_tex_prev),
114- make_uniform("geo_to_tex_next", geo_to_tex_next),
115- Uniform::new("progress", progress),
116- Uniform::new("clamped_progress", clamped_progress),
109+ make_uniform("niri_input_to_curr_geo", input_to_curr_geo),
110+ make_uniform("niri_curr_geo_to_prev_geo", curr_geo_to_prev_geo),
111+ make_uniform("niri_curr_geo_to_next_geo", curr_geo_to_next_geo),
112+ make_uniform("niri_geo_to_tex_prev", geo_to_tex_prev),
113+ make_uniform("niri_geo_to_tex_next", geo_to_tex_next),
114+ Uniform::new("niri_progress", progress),
115+ Uniform::new("niri_clamped_progress", clamped_progress),
117116 ],
118117 Kind::Unspecified,
119118 ))
src/render_helpers/shaders/mod.rsmodified+33 -33
@@ -1,6 +1,8 @@
11 use std::cell::RefCell;
22
3-use smithay::backend::renderer::gles::{GlesPixelProgram, GlesRenderer, UniformName, UniformType};
3+use smithay::backend::renderer::gles::{
4+ GlesError, GlesPixelProgram, GlesRenderer, UniformName, UniformType,
5+};
46
57 use super::primary_gpu_pixel_shader_with_textures::PixelWithTexturesProgram;
68 use super::renderer::NiriRenderer;
@@ -31,24 +33,11 @@ impl Shaders {
3133 })
3234 .ok();
3335
34- let resize = PixelWithTexturesProgram::compile(
35- renderer,
36- include_str!("resize.frag"),
37- &[
38- UniformName::new("input_to_curr_geo", UniformType::Matrix3x3),
39- UniformName::new("input_to_prev_geo", UniformType::Matrix3x3),
40- UniformName::new("input_to_next_geo", UniformType::Matrix3x3),
41- UniformName::new("geo_to_tex_prev", UniformType::Matrix3x3),
42- UniformName::new("geo_to_tex_next", UniformType::Matrix3x3),
43- UniformName::new("progress", UniformType::_1f),
44- UniformName::new("clamped_progress", UniformType::_1f),
45- ],
46- &["tex_prev", "tex_next"],
47- )
48- .map_err(|err| {
49- warn!("error compiling resize shader: {err:?}");
50- })
51- .ok();
36+ let resize = compile_resize_program(renderer, include_str!("resize.frag"))
37+ .map_err(|err| {
38+ warn!("error compiling resize shader: {err:?}");
39+ })
40+ .ok();
5241
5342 Self {
5443 gradient_border,
@@ -87,22 +76,33 @@ pub fn init(renderer: &mut GlesRenderer) {
8776 }
8877 }
8978
79+fn compile_resize_program(
80+ renderer: &mut GlesRenderer,
81+ src: &str,
82+) -> Result<PixelWithTexturesProgram, GlesError> {
83+ let mut program = include_str!("resize-prelude.frag").to_string();
84+ program.push_str(src);
85+ program.push_str(include_str!("resize-epilogue.frag"));
86+
87+ PixelWithTexturesProgram::compile(
88+ renderer,
89+ &program,
90+ &[
91+ UniformName::new("niri_input_to_curr_geo", UniformType::Matrix3x3),
92+ UniformName::new("niri_curr_geo_to_prev_geo", UniformType::Matrix3x3),
93+ UniformName::new("niri_curr_geo_to_next_geo", UniformType::Matrix3x3),
94+ UniformName::new("niri_geo_to_tex_prev", UniformType::Matrix3x3),
95+ UniformName::new("niri_geo_to_tex_next", UniformType::Matrix3x3),
96+ UniformName::new("niri_progress", UniformType::_1f),
97+ UniformName::new("niri_clamped_progress", UniformType::_1f),
98+ ],
99+ &["niri_tex_prev", "niri_tex_next"],
100+ )
101+}
102+
90103 pub fn set_custom_resize_program(renderer: &mut GlesRenderer, src: Option<&str>) {
91104 let program = if let Some(src) = src {
92- match PixelWithTexturesProgram::compile(
93- renderer,
94- src,
95- &[
96- UniformName::new("input_to_curr_geo", UniformType::Matrix3x3),
97- UniformName::new("input_to_prev_geo", UniformType::Matrix3x3),
98- UniformName::new("input_to_next_geo", UniformType::Matrix3x3),
99- UniformName::new("geo_to_tex_prev", UniformType::Matrix3x3),
100- UniformName::new("geo_to_tex_next", UniformType::Matrix3x3),
101- UniformName::new("progress", UniformType::_1f),
102- UniformName::new("clamped_progress", UniformType::_1f),
103- ],
104- &["tex_prev", "tex_next"],
105- ) {
105+ match compile_resize_program(renderer, src) {
106106 Ok(program) => Some(program),
107107 Err(err) => {
108108 warn!("error compiling custom resize shader: {err:?}");
src/render_helpers/shaders/resize-epilogue.fragadded+8 -0
@@ -0,0 +1,8 @@
1+void main() {
2+ vec3 coords_curr_geo = niri_input_to_curr_geo * vec3(niri_v_coords, 1.0);
3+ vec3 size_curr_geo = niri_input_to_curr_geo * vec3(niri_size, 1.0);
4+
5+ vec4 color = resize_color(coords_curr_geo, size_curr_geo);
6+
7+ gl_FragColor = color * niri_alpha;
8+}
src/render_helpers/shaders/resize-prelude.fragadded+21 -0
@@ -0,0 +1,21 @@
1+#version 100
2+
3+precision mediump float;
4+
5+varying vec2 niri_v_coords;
6+uniform vec2 niri_size;
7+
8+uniform mat3 niri_input_to_curr_geo;
9+uniform mat3 niri_curr_geo_to_prev_geo;
10+uniform mat3 niri_curr_geo_to_next_geo;
11+
12+uniform sampler2D niri_tex_prev;
13+uniform mat3 niri_geo_to_tex_prev;
14+
15+uniform sampler2D niri_tex_next;
16+uniform mat3 niri_geo_to_tex_next;
17+
18+uniform float niri_progress;
19+uniform float niri_clamped_progress;
20+
21+uniform float niri_alpha;
src/render_helpers/shaders/resize.fragmodified+6 -37
@@ -1,41 +1,10 @@
1-#version 100
1+vec4 resize_color(vec3 coords_curr_geo, vec3 size_curr_geo) {
2+ vec3 coords_tex_prev = niri_geo_to_tex_prev * coords_curr_geo;
3+ vec4 color_prev = texture2D(niri_tex_prev, coords_tex_prev.st);
24
3-precision mediump float;
5+ vec3 coords_tex_next = niri_geo_to_tex_next * coords_curr_geo;
6+ vec4 color_next = texture2D(niri_tex_next, coords_tex_next.st);
47
5-varying vec2 v_coords;
6-uniform vec2 size;
7-
8-uniform mat3 input_to_curr_geo;
9-uniform mat3 input_to_prev_geo;
10-uniform mat3 input_to_next_geo;
11-
12-uniform sampler2D tex_prev;
13-uniform mat3 geo_to_tex_prev;
14-
15-uniform sampler2D tex_next;
16-uniform mat3 geo_to_tex_next;
17-
18-uniform float progress;
19-uniform float clamped_progress;
20-
21-uniform float alpha;
22-
23-vec4 crossfade() {
24- vec3 coords_curr_geo = input_to_curr_geo * vec3(v_coords, 1.0);
25-
26- vec3 coords_tex_prev = geo_to_tex_prev * coords_curr_geo;
27- vec4 color_prev = texture2D(tex_prev, vec2(coords_tex_prev));
28-
29- vec3 coords_tex_next = geo_to_tex_next * coords_curr_geo;
30- vec4 color_next = texture2D(tex_next, vec2(coords_tex_next));
31-
32- vec4 color = mix(color_prev, color_next, clamped_progress);
8+ vec4 color = mix(color_prev, color_next, niri_clamped_progress);
339 return color;
3410 }
35-
36-void main() {
37- vec4 color = crossfade();
38-
39- gl_FragColor = color * alpha;
40-}
41-
src/render_helpers/shaders/texture.vertmodified+2 -2
@@ -6,7 +6,7 @@ uniform mat3 tex_matrix;
66 attribute vec2 vert;
77 attribute vec4 vert_position;
88
9-varying vec2 v_coords;
9+varying vec2 niri_v_coords;
1010
1111 mat2 scale(vec2 scale_vec){
1212 return mat2(
@@ -19,7 +19,7 @@ void main() {
1919 vec2 vert_transform_translation = vert_position.xy;
2020 vec2 vert_transform_scale = vert_position.zw;
2121 vec3 position = vec3(vert * scale(vert_transform_scale) + vert_transform_translation, 1.0);
22- v_coords = (tex_matrix * position).xy;
22+ niri_v_coords = (tex_matrix * position).xy;
2323 gl_Position = vec4(matrix * position, 1.0);
2424 }
2525
wiki/Configuration:-Animations.mdmodified+7 -15
@@ -234,31 +234,23 @@ See [this example shader](./examples/resize-custom-shader.frag) for a full docum
234234
235235 If a custom shader fails to compile, niri will print a warning and fall back to the default, or previous successfully compiled shader.
236236
237-> [!NOTE]
237+> [!WARNING]
238238 >
239239 > Custom shaders do not have a backwards compatibility guarantee.
240240 > I may need to change their interface as I'm developing new features.
241241
242+Example: resize will show the next (after resize) window texture right away, stretched to the current geometry.
243+
242244 ```
243245 animations {
244246 window-resize {
245247 spring damping-ratio=1.0 stiffness=800 epsilon=0.0001
246248
247249 custom-shader r"
248- #version 100
249- precision mediump float;
250-
251- varying vec2 v_coords;
252- uniform mat3 input_to_curr_geo;
253- uniform sampler2D tex_next;
254- uniform mat3 geo_to_tex_next;
255- uniform float alpha;
256-
257- void main() {
258- vec3 coords_curr_geo = input_to_curr_geo * vec3(v_coords, 1.0);
259- vec3 coords_tex_next = geo_to_tex_next * coords_curr_geo;
260- vec4 color = texture2D(tex_next, vec2(coords_tex_next));
261- gl_FragColor = color * alpha;
250+ vec4 resize_color(vec3 coords_curr_geo, vec3 size_curr_geo) {
251+ vec3 coords_tex_next = niri_geo_to_tex_next * coords_curr_geo;
252+ vec4 color = texture2D(niri_tex_next, coords_tex_next.st);
253+ return color;
262254 }
263255 "
264256 }
wiki/examples/resize-custom-shader.fragmodified+70 -59
@@ -1,72 +1,87 @@
1-#version 100
2-
3-precision mediump float;
4-
5-// Coordinates of the current pixel.
1+// Your shader must contain one function (see the bottom of this file).
62 //
7-// These range from 0 to 1 over the whole area of the shader. The location and
8-// the size of the area are unspecified, but niri will make it large enough to
9-// accomodate a crossfade.
3+// It should not contain any uniform definitions or anything else, as niri
4+// provides them for you.
105 //
11-// You very likely want to convert these coordinates to geometry coordinates
12-// before using them (see below).
13-varying vec2 v_coords;
6+// All symbols defined by niri will have a niri_ prefix, so don't use it for
7+// your own variables and functions.
148
15-// Pixel size of the whole area of the shader.
16-uniform vec2 size;
9+// The function that you must define looks like this:
10+vec4 resize_color(vec3 coords_curr_geo, vec3 size_curr_geo) {
11+ vec4 color = /* ...compute the color... */;
12+ return color;
13+}
1714
18-// Matrix that converts the input v_coords into coordinates inside the current
15+// It takes as input:
16+//
17+// * coords_curr_geo: coordinates of the current pixel relative to the current
1918 // window geometry.
2019 //
20+// These are homogeneous (the Z component is equal to 1) and scaled in such a
21+// way that the 0 to 1 coordinates lie within the current window geometry (in
22+// the middle of a resize). Pixels outside the window geometry will have
23+// coordinates below 0 or above 1.
24+//
2125 // The window geometry is its "visible bounds" from the user's perspective.
22-// After applying this matrix, the 0 to 1 coordinate range will correspond to
23-// the current geometry (in the middle of a resize), and pixels outside the
24-// geometry will have coordinates below 0 or above 1.
25-uniform mat3 input_to_curr_geo;
26-
27-// Matrix that converts the input v_coords into coordinates inside the previous
28-// (before resize) window geometry.
29-uniform mat3 input_to_prev_geo;
26+//
27+// The shader runs over an area of unspecified size and location, so you must
28+// expect and handle coordinates outside the [0, 1] range. The area will be
29+// large enough to accomodate a crossfade effect.
30+//
31+// * size_curr_geo: size of the current window geometry in physical pixels.
32+//
33+// It is homogeneous (the Z component is equal to 1).
34+//
35+// The function must return the color of the pixel (with premultiplied alpha).
36+// The pixel color will be further processed by niri (for example, to apply the
37+// final opacity from window rules).
3038
31-// Matrix that converts the input v_coords into coordinates inside the next
32-// (after resize) window geometry.
33-uniform mat3 input_to_next_geo;
39+// Now let's go over the uniforms that niri defines.
3440
3541 // Previous (before resize) window texture.
36-uniform sampler2D tex_prev;
42+uniform sampler2D niri_tex_prev;
3743
3844 // Matrix that converts geometry coordinates into the previous window texture
3945 // coordinates.
4046 //
4147 // The window texture can and will go outside the geometry (for client-side
4248 // decoration shadows for example), which is why this matrix is necessary.
43-uniform mat3 geo_to_tex_prev;
49+uniform mat3 niri_geo_to_tex_prev;
4450
4551 // Next (after resize) window texture.
46-uniform sampler2D tex_next;
52+uniform sampler2D niri_tex_next;
4753
4854 // Matrix that converts geometry coordinates into the next window texture
4955 // coordinates.
50-uniform mat3 geo_to_tex_next;
56+uniform mat3 niri_geo_to_tex_next;
57+
58+
59+// Matrix that converts coords_curr_geo into coordinates inside the previous
60+// (before resize) window geometry.
61+uniform mat3 niri_curr_geo_to_prev_geo;
62+
63+// Matrix that converts coords_curr_geo into coordinates inside the next
64+// (after resize) window geometry.
65+uniform mat3 niri_curr_geo_to_next_geo;
66+
5167
5268 // Unclamped progress of the resize.
5369 //
5470 // Goes from 0 to 1 but may overshoot and oscillate.
55-uniform float progress;
71+uniform float niri_progress;
5672
5773 // Clamped progress of the resize.
5874 //
5975 // Goes from 0 to 1, but will stop at 1 as soon as it first reaches 1. Will not
6076 // overshoot or oscillate.
61-uniform float clamped_progress;
77+uniform float niri_clamped_progress;
6278
63-// Additional opacity to apply to the final color.
64-uniform float alpha;
79+// Now let's look at some examples. You can copy everything below this line
80+// into your custom-shader to experiment.
6581
6682 // Example: fill the current geometry with a solid vertical gradient.
67-vec4 solid_gradient() {
68- vec3 coords = input_to_curr_geo * vec3(v_coords, 1.0);
69-
83+vec4 solid_gradient(vec3 coords_curr_geo, vec3 size_curr_geo) {
84+ vec3 coords = coords_curr_geo;
7085 vec4 color = vec4(0.0);
7186
7287 // Paint only the area inside the current geometry.
@@ -83,35 +98,33 @@ vec4 solid_gradient() {
8398
8499 // Example: crossfade between previous and next texture, stretched to the
85100 // current geometry.
86-vec4 crossfade() {
87- vec3 coords_curr_geo = input_to_curr_geo * vec3(v_coords, 1.0);
101+vec4 crossfade(vec3 coords_curr_geo, vec3 size_curr_geo) {
102+ // Convert coordinates into the texture space for sampling.
103+ vec3 coords_tex_prev = niri_geo_to_tex_prev * coords_curr_geo;
104+ vec4 color_prev = texture2D(niri_tex_prev, coords_tex_prev.st);
88105
89- vec3 coords_tex_prev = geo_to_tex_prev * coords_curr_geo;
90- vec4 color_prev = texture2D(tex_prev, vec2(coords_tex_prev));
106+ // Convert coordinates into the texture space for sampling.
107+ vec3 coords_tex_next = niri_geo_to_tex_next * coords_curr_geo;
108+ vec4 color_next = texture2D(niri_tex_next, coords_tex_next.st);
91109
92- vec3 coords_tex_next = geo_to_tex_next * coords_curr_geo;
93- vec4 color_next = texture2D(tex_next, vec2(coords_tex_next));
94-
95- vec4 color = mix(color_prev, color_next, clamped_progress);
110+ vec4 color = mix(color_prev, color_next, niri_clamped_progress);
96111 return color;
97112 }
98113
99114 // Example: next texture, stretched to the current geometry.
100-vec4 stretch_next() {
101- vec3 coords_curr_geo = input_to_curr_geo * vec3(v_coords, 1.0);
102- vec3 coords_tex_next = geo_to_tex_next * coords_curr_geo;
103- vec4 color = texture2D(tex_next, vec2(coords_tex_next));
115+vec4 stretch_next(vec3 coords_curr_geo, vec3 size_curr_geo) {
116+ vec3 coords_tex_next = niri_geo_to_tex_next * coords_curr_geo;
117+ vec4 color = texture2D(niri_tex_next, coords_tex_next.st);
104118 return color;
105119 }
106120
107121 // Example: next texture, stretched to the current geometry if smaller, and
108122 // cropped if bigger.
109-vec4 stretch_or_crop_next() {
110- vec3 coords_curr_geo = input_to_curr_geo * vec3(v_coords, 1.0);
111- vec3 coords_next_geo = input_to_next_geo * vec3(v_coords, 1.0);
123+vec4 stretch_or_crop_next(vec3 coords_curr_geo, vec3 size_curr_geo) {
124+ vec3 coords_next_geo = niri_curr_geo_to_next_geo * coords_curr_geo;
112125
113- vec3 coords_stretch = geo_to_tex_next * coords_curr_geo;
114- vec3 coords_crop = geo_to_tex_next * coords_next_geo;
126+ vec3 coords_stretch = niri_geo_to_tex_next * coords_curr_geo;
127+ vec3 coords_crop = niri_geo_to_tex_next * coords_next_geo;
115128
116129 // If the crop coord is smaller than the stretch coord, then the next
117130 // texture size is bigger than the current geometry, which means that we
@@ -122,7 +135,7 @@ vec4 stretch_or_crop_next() {
122135 if (coords_crop.y < coords_stretch.y)
123136 coords.y = coords_crop.y;
124137
125- vec4 color = texture2D(tex_next, vec2(coords));
138+ vec4 color = texture2D(niri_tex_next, coords.st);
126139
127140 // However, when we crop, we also want to crop out anything outside the
128141 // current geometry. This is because the area of the shader is unspecified
@@ -142,11 +155,9 @@ vec4 stretch_or_crop_next() {
142155 return color;
143156 }
144157
145-// The main entry point of the shader.
146-void main() {
158+// This is the function that you must define.
159+vec4 resize_color(vec3 coords_curr_geo, vec3 size_curr_geo) {
147160 // You can pick one of the example functions or write your own.
148- vec4 color = stretch_or_crop_next();
149-
150- gl_FragColor = color * alpha;
161+ return stretch_or_crop_next(coords_curr_geo, size_curr_geo);
151162 }
152163