Change custom-shader to a prelude-epilogue system
Browse files at this commit1681ed16d91757d98dc3eb653b970e706ff84b78Parent: d4bed709 changed files +161 -161
- src/render_helpers/primary_gpu_pixel_shader_with_textures.rs +2 -2
- src/render_helpers/resize.rs +12 -13
- src/render_helpers/shaders/mod.rs +33 -33
- src/render_helpers/shaders/resize-epilogue.frag +8 -0
- src/render_helpers/shaders/resize-prelude.frag +21 -0
- src/render_helpers/shaders/resize.frag +6 -37
- src/render_helpers/shaders/texture.vert +2 -2
- wiki/Configuration:-Animations.md +7 -15
- wiki/examples/resize-custom-shader.frag +70 -59
src/render_helpers/primary_gpu_pixel_shader_with_textures.rsmodified+2 -2
| @@ -63,8 +63,8 @@ unsafe fn compile_program( | ||
| 63 | 63 | let vert_position = CStr::from_bytes_with_nul(b"vert_position\0").expect("NULL terminated"); |
| 64 | 64 | let matrix = CStr::from_bytes_with_nul(b"matrix\0").expect("NULL terminated"); |
| 65 | 65 | 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"); | |
| 68 | 68 | |
| 69 | 69 | Ok(PixelWithTexturesProgram(Rc::new( |
| 70 | 70 | PixelWithTexturesProgramInner { |
src/render_helpers/resize.rsmodified+12 -13
| @@ -74,10 +74,9 @@ impl ResizeRenderElement { | ||
| 74 | 74 | // Compute the transformation matrices. |
| 75 | 75 | let input_to_curr_geo = Mat3::from_scale(area_size / curr_geo_size) |
| 76 | 76 | * 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); | |
| 81 | 80 | |
| 82 | 81 | let geo_to_tex_prev = Mat3::from_translation(-tex_prev_geo_loc / tex_prev_geo_size) |
| 83 | 82 | * Mat3::from_scale(size_prev / tex_prev_geo_size * scale); |
| @@ -99,21 +98,21 @@ impl ResizeRenderElement { | ||
| 99 | 98 | Self(PrimaryGpuPixelShaderWithTexturesRenderElement::new( |
| 100 | 99 | shader, |
| 101 | 100 | 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), | |
| 104 | 103 | ]), |
| 105 | 104 | area, |
| 106 | 105 | size, |
| 107 | 106 | None, |
| 108 | 107 | result_alpha, |
| 109 | 108 | 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), | |
| 117 | 116 | ], |
| 118 | 117 | Kind::Unspecified, |
| 119 | 118 | )) |
src/render_helpers/shaders/mod.rsmodified+33 -33
| @@ -1,6 +1,8 @@ | ||
| 1 | 1 | use std::cell::RefCell; |
| 2 | 2 | |
| 3 | -use smithay::backend::renderer::gles::{GlesPixelProgram, GlesRenderer, UniformName, UniformType}; | |
| 3 | +use smithay::backend::renderer::gles::{ | |
| 4 | + GlesError, GlesPixelProgram, GlesRenderer, UniformName, UniformType, | |
| 5 | +}; | |
| 4 | 6 | |
| 5 | 7 | use super::primary_gpu_pixel_shader_with_textures::PixelWithTexturesProgram; |
| 6 | 8 | use super::renderer::NiriRenderer; |
| @@ -31,24 +33,11 @@ impl Shaders { | ||
| 31 | 33 | }) |
| 32 | 34 | .ok(); |
| 33 | 35 | |
| 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(); | |
| 52 | 41 | |
| 53 | 42 | Self { |
| 54 | 43 | gradient_border, |
| @@ -87,22 +76,33 @@ pub fn init(renderer: &mut GlesRenderer) { | ||
| 87 | 76 | } |
| 88 | 77 | } |
| 89 | 78 | |
| 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 | + | |
| 90 | 103 | pub fn set_custom_resize_program(renderer: &mut GlesRenderer, src: Option<&str>) { |
| 91 | 104 | 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) { | |
| 106 | 106 | Ok(program) => Some(program), |
| 107 | 107 | Err(err) => { |
| 108 | 108 | 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); | |
| 2 | 4 | |
| 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); | |
| 4 | 7 | |
| 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); | |
| 33 | 9 | return color; |
| 34 | 10 | } |
| 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; | ||
| 6 | 6 | attribute vec2 vert; |
| 7 | 7 | attribute vec4 vert_position; |
| 8 | 8 | |
| 9 | -varying vec2 v_coords; | |
| 9 | +varying vec2 niri_v_coords; | |
| 10 | 10 | |
| 11 | 11 | mat2 scale(vec2 scale_vec){ |
| 12 | 12 | return mat2( |
| @@ -19,7 +19,7 @@ void main() { | ||
| 19 | 19 | vec2 vert_transform_translation = vert_position.xy; |
| 20 | 20 | vec2 vert_transform_scale = vert_position.zw; |
| 21 | 21 | 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; | |
| 23 | 23 | gl_Position = vec4(matrix * position, 1.0); |
| 24 | 24 | } |
| 25 | 25 |
wiki/Configuration:-Animations.mdmodified+7 -15
| @@ -234,31 +234,23 @@ See [this example shader](./examples/resize-custom-shader.frag) for a full docum | ||
| 234 | 234 | |
| 235 | 235 | If a custom shader fails to compile, niri will print a warning and fall back to the default, or previous successfully compiled shader. |
| 236 | 236 | |
| 237 | -> [!NOTE] | |
| 237 | +> [!WARNING] | |
| 238 | 238 | > |
| 239 | 239 | > Custom shaders do not have a backwards compatibility guarantee. |
| 240 | 240 | > I may need to change their interface as I'm developing new features. |
| 241 | 241 | |
| 242 | +Example: resize will show the next (after resize) window texture right away, stretched to the current geometry. | |
| 243 | + | |
| 242 | 244 | ``` |
| 243 | 245 | animations { |
| 244 | 246 | window-resize { |
| 245 | 247 | spring damping-ratio=1.0 stiffness=800 epsilon=0.0001 |
| 246 | 248 | |
| 247 | 249 | 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; | |
| 262 | 254 | } |
| 263 | 255 | " |
| 264 | 256 | } |
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). | |
| 6 | 2 | // |
| 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. | |
| 10 | 5 | // |
| 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. | |
| 14 | 8 | |
| 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 | +} | |
| 17 | 14 | |
| 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 | |
| 19 | 18 | // window geometry. |
| 20 | 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 | +// | |
| 21 | 25 | // 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). | |
| 30 | 38 | |
| 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. | |
| 34 | 40 | |
| 35 | 41 | // Previous (before resize) window texture. |
| 36 | -uniform sampler2D tex_prev; | |
| 42 | +uniform sampler2D niri_tex_prev; | |
| 37 | 43 | |
| 38 | 44 | // Matrix that converts geometry coordinates into the previous window texture |
| 39 | 45 | // coordinates. |
| 40 | 46 | // |
| 41 | 47 | // The window texture can and will go outside the geometry (for client-side |
| 42 | 48 | // 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; | |
| 44 | 50 | |
| 45 | 51 | // Next (after resize) window texture. |
| 46 | -uniform sampler2D tex_next; | |
| 52 | +uniform sampler2D niri_tex_next; | |
| 47 | 53 | |
| 48 | 54 | // Matrix that converts geometry coordinates into the next window texture |
| 49 | 55 | // 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 | + | |
| 51 | 67 | |
| 52 | 68 | // Unclamped progress of the resize. |
| 53 | 69 | // |
| 54 | 70 | // Goes from 0 to 1 but may overshoot and oscillate. |
| 55 | -uniform float progress; | |
| 71 | +uniform float niri_progress; | |
| 56 | 72 | |
| 57 | 73 | // Clamped progress of the resize. |
| 58 | 74 | // |
| 59 | 75 | // Goes from 0 to 1, but will stop at 1 as soon as it first reaches 1. Will not |
| 60 | 76 | // overshoot or oscillate. |
| 61 | -uniform float clamped_progress; | |
| 77 | +uniform float niri_clamped_progress; | |
| 62 | 78 | |
| 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. | |
| 65 | 81 | |
| 66 | 82 | // 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; | |
| 70 | 85 | vec4 color = vec4(0.0); |
| 71 | 86 | |
| 72 | 87 | // Paint only the area inside the current geometry. |
| @@ -83,35 +98,33 @@ vec4 solid_gradient() { | ||
| 83 | 98 | |
| 84 | 99 | // Example: crossfade between previous and next texture, stretched to the |
| 85 | 100 | // 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); | |
| 88 | 105 | |
| 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); | |
| 91 | 109 | |
| 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); | |
| 96 | 111 | return color; |
| 97 | 112 | } |
| 98 | 113 | |
| 99 | 114 | // 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); | |
| 104 | 118 | return color; |
| 105 | 119 | } |
| 106 | 120 | |
| 107 | 121 | // Example: next texture, stretched to the current geometry if smaller, and |
| 108 | 122 | // 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; | |
| 112 | 125 | |
| 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; | |
| 115 | 128 | |
| 116 | 129 | // If the crop coord is smaller than the stretch coord, then the next |
| 117 | 130 | // texture size is bigger than the current geometry, which means that we |
| @@ -122,7 +135,7 @@ vec4 stretch_or_crop_next() { | ||
| 122 | 135 | if (coords_crop.y < coords_stretch.y) |
| 123 | 136 | coords.y = coords_crop.y; |
| 124 | 137 | |
| 125 | - vec4 color = texture2D(tex_next, vec2(coords)); | |
| 138 | + vec4 color = texture2D(niri_tex_next, coords.st); | |
| 126 | 139 | |
| 127 | 140 | // However, when we crop, we also want to crop out anything outside the |
| 128 | 141 | // current geometry. This is because the area of the shader is unspecified |
| @@ -142,11 +155,9 @@ vec4 stretch_or_crop_next() { | ||
| 142 | 155 | return color; |
| 143 | 156 | } |
| 144 | 157 | |
| 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) { | |
| 147 | 160 | // 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); | |
| 151 | 162 | } |
| 152 | 163 |