forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB9d8f4f8Ivan Molodetskikh1 month ago2997 commits

resize_custom_shader.frag211 lines, 8.1 KiBHistoryRawDownload
1// Your shader must contain one function (see the bottom of this file).
2//
3// It should not contain any uniform definitions or anything else, as niri
4// provides them for you.
5//
6// All symbols defined by niri will have a niri_ prefix, so don't use it for
7// your own variables and functions.
8
9// The function that you must define looks like this:
10vec4 resize_color(vec3 coords_curr_geo, vec3 size_curr_geo) {
11 vec4 color = /* ...compute the color... */;
12 return color;
13}
14
15// It takes as input:
16//
17// * coords_curr_geo: coordinates of the current pixel relative to the current
18// window geometry.
19//
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//
25// The window geometry is its "visible bounds" from the user's perspective.
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 accommodate a crossfade effect.
30//
31// * size_curr_geo: size of the current window geometry in logical 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).
38
39// Now let's go over the uniforms that niri defines.
40//
41// You should only rely on the uniforms documented here. Any other uniforms can
42// change or be removed without notice.
43
44// Previous (before resize) window texture.
45uniform sampler2D niri_tex_prev;
46
47// Matrix that converts geometry coordinates into the previous window texture
48// coordinates.
49//
50// The window texture can and will go outside the geometry (for client-side
51// decoration shadows for example), which is why this matrix is necessary.
52uniform mat3 niri_geo_to_tex_prev;
53
54// Next (after resize) window texture.
55uniform sampler2D niri_tex_next;
56
57// Matrix that converts geometry coordinates into the next window texture
58// coordinates.
59uniform mat3 niri_geo_to_tex_next;
60
61
62// Matrix that converts coords_curr_geo into coordinates inside the previous
63// (before resize) window geometry.
64uniform mat3 niri_curr_geo_to_prev_geo;
65
66// Matrix that converts coords_curr_geo into coordinates inside the next
67// (after resize) window geometry.
68uniform mat3 niri_curr_geo_to_next_geo;
69
70
71// Unclamped progress of the animation.
72//
73// Goes from 0 to 1 but may overshoot and oscillate.
74uniform float niri_progress;
75
76// Clamped progress of the animation.
77//
78// Goes from 0 to 1, but will stop at 1 as soon as it first reaches 1. Will not
79// overshoot or oscillate.
80uniform float niri_clamped_progress;
81
82// Now let's look at some examples. You can copy everything below this line
83// into your custom-shader to experiment.
84
85// Example: fill the current geometry with a solid vertical gradient.
86vec4 solid_gradient(vec3 coords_curr_geo, vec3 size_curr_geo) {
87 vec3 coords = coords_curr_geo;
88 vec4 color = vec4(0.0);
89
90 // Paint only the area inside the current geometry.
91 if (0.0 <= coords.x && coords.x <= 1.0
92 && 0.0 <= coords.y && coords.y <= 1.0)
93 {
94 vec4 from = vec4(1.0, 0.0, 0.0, 1.0);
95 vec4 to = vec4(0.0, 1.0, 0.0, 1.0);
96 color = mix(from, to, coords.y);
97 }
98
99 return color;
100}
101
102// Example: crossfade between previous and next texture, stretched to the
103// current geometry.
104vec4 crossfade(vec3 coords_curr_geo, vec3 size_curr_geo) {
105 // Convert coordinates into the texture space for sampling.
106 vec3 coords_tex_prev = niri_geo_to_tex_prev * coords_curr_geo;
107 vec4 color_prev = texture2D(niri_tex_prev, coords_tex_prev.st);
108
109 // Convert coordinates into the texture space for sampling.
110 vec3 coords_tex_next = niri_geo_to_tex_next * coords_curr_geo;
111 vec4 color_next = texture2D(niri_tex_next, coords_tex_next.st);
112
113 vec4 color = mix(color_prev, color_next, niri_clamped_progress);
114 return color;
115}
116
117// Example: next texture, stretched to the current geometry.
118vec4 stretch_next(vec3 coords_curr_geo, vec3 size_curr_geo) {
119 vec3 coords_tex_next = niri_geo_to_tex_next * coords_curr_geo;
120 vec4 color = texture2D(niri_tex_next, coords_tex_next.st);
121 return color;
122}
123
124// Example: next texture, stretched to the current geometry if smaller, and
125// cropped if bigger.
126vec4 stretch_or_crop_next(vec3 coords_curr_geo, vec3 size_curr_geo) {
127 vec3 coords_next_geo = niri_curr_geo_to_next_geo * coords_curr_geo;
128
129 vec3 coords_stretch = niri_geo_to_tex_next * coords_curr_geo;
130 vec3 coords_crop = niri_geo_to_tex_next * coords_next_geo;
131
132 // We can crop if the current window size is smaller than the next window
133 // size. One way to tell is by comparing to 1.0 the X and Y scaling
134 // coefficients in the current-to-next transformation matrix.
135 bool can_crop_by_x = niri_curr_geo_to_next_geo[0][0] <= 1.0;
136 bool can_crop_by_y = niri_curr_geo_to_next_geo[1][1] <= 1.0;
137
138 vec3 coords = coords_stretch;
139 if (can_crop_by_x)
140 coords.x = coords_crop.x;
141 if (can_crop_by_y)
142 coords.y = coords_crop.y;
143
144 vec4 color = texture2D(niri_tex_next, coords.st);
145
146 // However, when we crop, we also want to crop out anything outside the
147 // current geometry. This is because the area of the shader is unspecified
148 // and usually bigger than the current geometry, so if we don't fill pixels
149 // outside with transparency, the texture will leak out.
150 //
151 // When stretching, this is not an issue because the area outside will
152 // correspond to client-side decoration shadows, which are already supposed
153 // to be outside.
154 if (can_crop_by_x && (coords_curr_geo.x < 0.0 || 1.0 < coords_curr_geo.x))
155 color = vec4(0.0);
156 if (can_crop_by_y && (coords_curr_geo.y < 0.0 || 1.0 < coords_curr_geo.y))
157 color = vec4(0.0);
158
159 return color;
160}
161
162// Example: cropped next texture if it's bigger than the current geometry, and
163// crossfade between previous and next texture otherwise.
164vec4 crossfade_or_crop_next(vec3 coords_curr_geo, vec3 size_curr_geo) {
165 vec3 coords_next_geo = niri_curr_geo_to_next_geo * coords_curr_geo;
166 vec3 coords_prev_geo = niri_curr_geo_to_prev_geo * coords_curr_geo;
167
168 vec3 coords_crop = niri_geo_to_tex_next * coords_next_geo;
169 vec3 coords_stretch = niri_geo_to_tex_next * coords_curr_geo;
170 vec3 coords_stretch_prev = niri_geo_to_tex_prev * coords_curr_geo;
171
172 // We can crop if the current window size is smaller than the next window
173 // size. One way to tell is by comparing to 1.0 the X and Y scaling
174 // coefficients in the current-to-next transformation matrix.
175 bool can_crop_by_x = niri_curr_geo_to_next_geo[0][0] <= 1.0;
176 bool can_crop_by_y = niri_curr_geo_to_next_geo[1][1] <= 1.0;
177 bool crop = can_crop_by_x && can_crop_by_y;
178
179 vec4 color;
180
181 if (crop) {
182 // However, when we crop, we also want to crop out anything outside the
183 // current geometry. This is because the area of the shader is unspecified
184 // and usually bigger than the current geometry, so if we don't fill pixels
185 // outside with transparency, the texture will leak out.
186 //
187 // When crossfading, this is not an issue because the area outside will
188 // correspond to client-side decoration shadows, which are already supposed
189 // to be outside.
190 if (coords_curr_geo.x < 0.0 || 1.0 < coords_curr_geo.x ||
191 coords_curr_geo.y < 0.0 || 1.0 < coords_curr_geo.y) {
192 color = vec4(0.0);
193 } else {
194 color = texture2D(niri_tex_next, coords_crop.st);
195 }
196 } else {
197 // If we can't crop, then crossfade.
198 color = texture2D(niri_tex_next, coords_stretch.st);
199 vec4 color_prev = texture2D(niri_tex_prev, coords_stretch_prev.st);
200 color = mix(color_prev, color, niri_clamped_progress);
201 }
202
203 return color;
204}
205
206// This is the function that you must define.
207vec4 resize_color(vec3 coords_curr_geo, vec3 size_curr_geo) {
208 // You can pick one of the example functions or write your own.
209 return stretch_or_crop_next(coords_curr_geo, size_curr_geo);
210}
211