diff --git a/.vscode/settings.json b/.vscode/settings.json index 883465a..c7bfa32 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,7 +10,8 @@ "pathtag": "${workspaceFolder}/shader/shared/pathtag.wgsl", "ptcl": "${workspaceFolder}/shader/shared/ptcl.wgsl", "segment": "${workspaceFolder}/shader/shared/segment.wgsl", - "tile": "${workspaceFolder}/shader/shared/tile.wgsl" + "tile": "${workspaceFolder}/shader/shared/tile.wgsl", + "transform": "${workspaceFolder}/shader/shared/transform.wgsl" }, "wgsl-analyzer.diagnostics.nagaVersion": "main", "wgsl-analyzer.preprocessor.shaderDefs": [ diff --git a/crates/encoding/src/draw.rs b/crates/encoding/src/draw.rs index e26dacd..8e8b1ba 100644 --- a/crates/encoding/src/draw.rs +++ b/crates/encoding/src/draw.rs @@ -22,7 +22,7 @@ impl DrawTag { pub const LINEAR_GRADIENT: Self = Self(0x114); /// Radial gradient fill. - pub const RADIAL_GRADIENT: Self = Self(0x2dc); + pub const RADIAL_GRADIENT: Self = Self(0x29c); /// Image fill. pub const IMAGE: Self = Self(0x248); diff --git a/crates/encoding/src/encoding.rs b/crates/encoding/src/encoding.rs index b94ac69..0b2a13b 100644 --- a/crates/encoding/src/encoding.rs +++ b/crates/encoding/src/encoding.rs @@ -3,7 +3,7 @@ use super::{DrawColor, DrawTag, PathEncoder, PathTag, Transform}; -use peniko::{kurbo::Shape, BlendMode, BrushRef}; +use peniko::{kurbo::Shape, BlendMode, BrushRef, Color}; #[cfg(feature = "full")] use { @@ -281,10 +281,15 @@ impl Encoding { alpha: f32, extend: Extend, ) { - self.add_ramp(color_stops, alpha, extend); - self.draw_tags.push(DrawTag::LINEAR_GRADIENT); - self.draw_data - .extend_from_slice(bytemuck::bytes_of(&gradient)); + match self.add_ramp(color_stops, alpha, extend) { + RampStops::Empty => self.encode_color(DrawColor::new(Color::TRANSPARENT)), + RampStops::One(color) => self.encode_color(DrawColor::new(color)), + _ => { + self.draw_tags.push(DrawTag::LINEAR_GRADIENT); + self.draw_data + .extend_from_slice(bytemuck::bytes_of(&gradient)); + } + } } /// Encodes a radial gradient brush. @@ -296,10 +301,20 @@ impl Encoding { alpha: f32, extend: Extend, ) { - self.add_ramp(color_stops, alpha, extend); - self.draw_tags.push(DrawTag::RADIAL_GRADIENT); - self.draw_data - .extend_from_slice(bytemuck::bytes_of(&gradient)); + // Match Skia's epsilon for radii comparison + const SKIA_EPSILON: f32 = 1.0 / (1 << 12) as f32; + if gradient.p0 == gradient.p1 && (gradient.r0 - gradient.r1).abs() < SKIA_EPSILON { + self.encode_color(DrawColor::new(Color::TRANSPARENT)); + } + match self.add_ramp(color_stops, alpha, extend) { + RampStops::Empty => self.encode_color(DrawColor::new(Color::TRANSPARENT)), + RampStops::One(color) => self.encode_color(DrawColor::new(color)), + _ => { + self.draw_tags.push(DrawTag::RADIAL_GRADIENT); + self.draw_data + .extend_from_slice(bytemuck::bytes_of(&gradient)); + } + } } /// Encodes an image brush. @@ -354,7 +369,7 @@ impl Encoding { color_stops: impl Iterator, alpha: f32, extend: Extend, - ) { + ) -> RampStops { let offset = self.draw_data.len(); let stops_start = self.resources.color_stops.len(); if alpha != 1.0 { @@ -364,14 +379,32 @@ impl Encoding { } else { self.resources.color_stops.extend(color_stops); } - self.resources.patches.push(Patch::Ramp { - draw_data_offset: offset, - stops: stops_start..self.resources.color_stops.len(), - extend, - }); + let stops_end = self.resources.color_stops.len(); + match stops_end - stops_start { + 0 => RampStops::Empty, + 1 => RampStops::One(self.resources.color_stops.pop().unwrap().color), + _ => { + self.resources.patches.push(Patch::Ramp { + draw_data_offset: offset, + stops: stops_start..stops_end, + extend, + }); + RampStops::Many + } + } } } +/// Result for adding a sequence of color stops. +enum RampStops { + /// Color stop sequence was empty. + Empty, + /// Contained a single color stop. + One(Color), + /// More than one color stop. + Many, +} + /// Encoded data for late bound resources. #[cfg(feature = "full")] #[derive(Clone, Default)] diff --git a/examples/scenes/src/test_scenes.rs b/examples/scenes/src/test_scenes.rs index 9c95b3c..7c48364 100644 --- a/examples/scenes/src/test_scenes.rs +++ b/examples/scenes/src/test_scenes.rs @@ -31,6 +31,7 @@ pub fn test_scenes() -> SceneSet { scene!(cardioid_and_friends), scene!(animated_text: animated), scene!(gradient_extend), + scene!(two_point_radial), scene!(brush_transform: animated), scene!(blend_grid), scene!(conflation_artifacts), @@ -252,11 +253,13 @@ fn brush_transform(sb: &mut SceneBuilder, params: &mut SceneParams) { fn gradient_extend(sb: &mut SceneBuilder, params: &mut SceneParams) { fn square(sb: &mut SceneBuilder, is_radial: bool, transform: Affine, extend: Extend) { - let colors = [Color::RED, Color::GREEN, Color::BLUE]; + let colors = [Color::RED, Color::rgb8(0, 255, 0), Color::BLUE]; let width = 300f64; let height = 300f64; let gradient: Brush = if is_radial { - Gradient::new_radial((width * 0.5, height * 0.5), (width * 0.25) as f32) + let center = (width * 0.5, height * 0.5); + let radius = (width * 0.25) as f32; + Gradient::new_two_point_radial(center, radius * 0.25, center, radius) .with_stops(colors) .with_extend(extend) .into() @@ -294,10 +297,45 @@ fn gradient_extend(sb: &mut SceneBuilder, params: &mut SceneParams) { label, ); } +} + +fn two_point_radial(sb: &mut SceneBuilder, params: &mut SceneParams) { + let colors = [Color::RED, Color::rgb8(0, 255, 0), Color::BLUE]; + let extend = Extend::Reflect; + let gradient1 = Gradient::new_two_point_radial((150.0, 150.0), 50.0, (200.0, 150.0), 100.0) + .with_extend(extend) + .with_stops(colors); + let gradient2 = Gradient::new_two_point_radial((300.0, 150.0), 100.0, (150.0, 150.0), 50.0) + .with_extend(extend) + .with_stops(colors); + let gradient3 = Gradient::new_two_point_radial((300.0, 150.0), 50.0, (150.0, 150.0), 50.0) + .with_extend(extend) + .with_stops(colors); + sb.fill( + Fill::NonZero, + Affine::scale(1.5) * Affine::translate((50.0, 0.0)), + &gradient1, + None, + &Rect::new(0.0, 0.0, 400.0, 400.0), + ); + sb.fill( + Fill::NonZero, + Affine::scale(1.5) * Affine::translate((50.0, 300.0)), + &gradient2, + None, + &Rect::new(0.0, 0.0, 400.0, 400.0), + ); + sb.fill( + Fill::NonZero, + Affine::scale(1.5) * Affine::translate((50.0, 600.0)), + &gradient3, + None, + &Rect::new(0.0, 0.0, 400.0, 400.0), + ); let t = (params.time * 0.5).sin() * 0.5 + 0.5; let x_delta: f64 = t * 2000.0 - 1000.0; let gradient = - Gradient::new_two_point_radial((400.0, 500.0), 100.0, (101.0 + x_delta, 500.0), 200.0) + Gradient::new_two_point_radial((400.0, 500.0), 100.0, (101.0 + x_delta, 500.0), 0.0) .with_extend(Extend::Reflect) .with_stops([Color::GREEN, Color::WHITE, Color::RED]); sb.fill( diff --git a/shader/coarse.wgsl b/shader/coarse.wgsl index 58fe409..aeb85c2 100644 --- a/shader/coarse.wgsl +++ b/shader/coarse.wgsl @@ -376,7 +376,7 @@ fn main( } } // DRAWTAG_FILL_RAD_GRADIENT - case 0x2dcu: { + case 0x29cu: { let linewidth = bitcast(info_bin_data[di]); if write_path(tile, linewidth) { let index = scene[dd]; diff --git a/shader/draw_leaf.wgsl b/shader/draw_leaf.wgsl index beced9c..ab27619 100644 --- a/shader/draw_leaf.wgsl +++ b/shader/draw_leaf.wgsl @@ -6,6 +6,7 @@ #import clip #import drawtag #import bbox +#import transform @group(0) @binding(0) var config: Config; @@ -30,12 +31,6 @@ var clip_inp: array; let WG_SIZE = 256u; -// Possibly dedup? -struct Transform { - matrx: vec4, - translate: vec2, -} - fn read_transform(transform_base: u32, ix: u32) -> Transform { let base = transform_base + ix * 6u; let c0 = bitcast(scene[base]); @@ -110,18 +105,16 @@ fn main( // let y1 = f32(bbox.y1); // let bbox_f = vec4(x0, y0, x1, y1); let fill_mode = u32(bbox.linewidth >= 0.0); - var matrx: vec4; - var translate: vec2; + var transform = Transform(); var linewidth = bbox.linewidth; if linewidth >= 0.0 || tag_word == DRAWTAG_FILL_LIN_GRADIENT || tag_word == DRAWTAG_FILL_RAD_GRADIENT || tag_word == DRAWTAG_FILL_IMAGE { - let transform = read_transform(config.transform_base, bbox.trans_ix); - matrx = transform.matrx; - translate = transform.translate; + transform = read_transform(config.transform_base, bbox.trans_ix); } if linewidth >= 0.0 { // Note: doesn't deal with anisotropic case + let matrx = transform.matrx; linewidth *= sqrt(abs(matrx.x * matrx.w - matrx.y * matrx.z)); } switch tag_word { @@ -134,8 +127,8 @@ fn main( info[di] = bitcast(linewidth); var p0 = bitcast>(vec2(scene[dd + 1u], scene[dd + 2u])); var p1 = bitcast>(vec2(scene[dd + 3u], scene[dd + 4u])); - p0 = matrx.xy * p0.x + matrx.zw * p0.y + translate; - p1 = matrx.xy * p1.x + matrx.zw * p1.y + translate; + p0 = transform_apply(transform, p0); + p1 = transform_apply(transform, p1); let dxy = p1 - p0; let scale = 1.0 / dot(dxy, dxy); let line_xy = dxy * scale; @@ -145,48 +138,100 @@ fn main( info[di + 3u] = bitcast(line_c); } // DRAWTAG_FILL_RAD_GRADIENT - case 0x2dcu: { + case 0x29cu: { + // Two-point conical gradient implementation based + // on the algorithm at + // This epsilon matches what Skia uses + let GRADIENT_EPSILON = 1.0 / f32(1 << 12u); info[di] = bitcast(linewidth); var p0 = bitcast>(vec2(scene[dd + 1u], scene[dd + 2u])); var p1 = bitcast>(vec2(scene[dd + 3u], scene[dd + 4u])); - let r0 = bitcast(scene[dd + 5u]); - let r1 = bitcast(scene[dd + 6u]); - let inv_det = 1.0 / (matrx.x * matrx.w - matrx.y * matrx.z); - let inv_mat = inv_det * vec4(matrx.w, -matrx.y, -matrx.z, matrx.x); - let rr = r1 / (r1 - r0); - var rr1 = rr; - if r0 > 0.0 { - p0 = p0 + (p0 - p1) * rr * 0.5; - rr1 = 1.0; + var r0 = bitcast(scene[dd + 5u]); + var r1 = bitcast(scene[dd + 6u]); + let user_to_gradient = transform_inverse(transform); + // Output variables + var xform = Transform(); + var focal_x = 0.0; + var radius = 0.0; + var kind = 0u; + var flags = 0u; + if abs(r0 - r1) <= GRADIENT_EPSILON { + // When the radii are the same, emit a strip gradient + kind = RAD_GRAD_KIND_STRIP; + let scaled = r0 / distance(p0, p1); + xform = transform_mul( + two_point_to_unit_line(p0, p1), + user_to_gradient + ); + radius = scaled * scaled; + } else { + // Assume a two point conical gradient unless the centers + // are equal. + kind = RAD_GRAD_KIND_CONE; + if all(p0 == p1) { + kind = RAD_GRAD_KIND_CIRCULAR; + // Nudge p0 a bit to avoid denormals. + p0 += GRADIENT_EPSILON; + } + if r1 == 0.0 { + // If r1 == 0.0, swap the points and radii + flags |= RAD_GRAD_SWAPPED; + let tmp_p = p0; + p0 = p1; + p1 = tmp_p; + let tmp_r = r0; + r0 = r1; + r1 = tmp_r; + } + focal_x = r0 / (r0 - r1); + let one_minus_focal_x = 1.0 - focal_x; + let cf = one_minus_focal_x * p0 + focal_x * p1; + let abs_one_minus_focal_x = abs(one_minus_focal_x); + radius = r1 / (distance(cf, p1)); + let user_to_unit_line = transform_mul( + two_point_to_unit_line(cf, p1), + user_to_gradient + ); + var user_to_scaled = user_to_unit_line; + // When r == 1.0, focal point is on circle + if abs(radius - 1.0) <= GRADIENT_EPSILON { + kind = RAD_GRAD_KIND_FOCAL_ON_CIRCLE; + let scale = 0.5 * abs_one_minus_focal_x; + user_to_scaled = transform_mul( + Transform(vec4(scale, 0.0, 0.0, scale), vec2(0.0)), + user_to_unit_line + ); + } else { + let a = radius * radius - 1.0; + let scale_x = radius / a * abs_one_minus_focal_x; + let scale_y = sqrt(abs(a)) / a * abs_one_minus_focal_x; + user_to_scaled = transform_mul( + Transform(vec4(scale_x, 0.0, 0.0, scale_y), vec2(0.0)), + user_to_unit_line + ); + } + xform = user_to_scaled; } - let inv_tr = mat2x2(inv_mat.xy, inv_mat.zw) * -translate - p0; - let center1 = p1 - p0; - let ra_inv = rr1 / (r1 * r1 - dot(center1, center1)); - let c1 = center1 * ra_inv; - let ra = rr1 * ra_inv; - info[di + 1u] = bitcast(inv_mat.x); - info[di + 2u] = bitcast(inv_mat.y); - info[di + 3u] = bitcast(inv_mat.z); - info[di + 4u] = bitcast(inv_mat.w); - info[di + 5u] = bitcast(inv_tr.x); - info[di + 6u] = bitcast(inv_tr.y); - info[di + 7u] = bitcast(c1.x); - info[di + 8u] = bitcast(c1.y); - info[di + 9u] = bitcast(ra); - info[di + 10u] = bitcast(rr); + info[di + 1u] = bitcast(xform.matrx.x); + info[di + 2u] = bitcast(xform.matrx.y); + info[di + 3u] = bitcast(xform.matrx.z); + info[di + 4u] = bitcast(xform.matrx.w); + info[di + 5u] = bitcast(xform.translate.x); + info[di + 6u] = bitcast(xform.translate.y); + info[di + 7u] = bitcast(focal_x); + info[di + 8u] = bitcast(radius); + info[di + 9u] = bitcast((flags << 3u) | kind); } // DRAWTAG_FILL_IMAGE case 0x248u: { info[di] = bitcast(linewidth); - let inv_det = 1.0 / (matrx.x * matrx.w - matrx.y * matrx.z); - let inv_mat = inv_det * vec4(matrx.w, -matrx.y, -matrx.z, matrx.x); - let inv_tr = mat2x2(inv_mat.xy, inv_mat.zw) * -translate; - info[di + 1u] = bitcast(inv_mat.x); - info[di + 2u] = bitcast(inv_mat.y); - info[di + 3u] = bitcast(inv_mat.z); - info[di + 4u] = bitcast(inv_mat.w); - info[di + 5u] = bitcast(inv_tr.x); - info[di + 6u] = bitcast(inv_tr.y); + let inv = transform_inverse(transform); + info[di + 1u] = bitcast(inv.matrx.x); + info[di + 2u] = bitcast(inv.matrx.y); + info[di + 3u] = bitcast(inv.matrx.z); + info[di + 4u] = bitcast(inv.matrx.w); + info[di + 5u] = bitcast(inv.translate.x); + info[di + 6u] = bitcast(inv.translate.y); info[di + 7u] = scene[dd]; info[di + 8u] = scene[dd + 1u]; } @@ -201,3 +246,17 @@ fn main( clip_inp[m.clip_ix] = ClipInp(ix, i32(path_ix)); } } + +fn two_point_to_unit_line(p0: vec2, p1: vec2) -> Transform { + let tmp1 = from_poly2(p0, p1); + let inv = transform_inverse(tmp1); + let tmp2 = from_poly2(vec2(0.0), vec2(1.0, 0.0)); + return transform_mul(tmp2, inv); +} + +fn from_poly2(p0: vec2, p1: vec2) -> Transform { + return Transform( + vec4(p1.y - p0.y, p0.x - p1.x, p1.x - p0.x, p1.y - p0.y), + vec2(p0.x, p0.y) + ); +} diff --git a/shader/fine.wgsl b/shader/fine.wgsl index 85ed7a4..4047ebd 100644 --- a/shader/fine.wgsl +++ b/shader/fine.wgsl @@ -82,10 +82,12 @@ fn read_rad_grad(cmd_ix: u32) -> CmdRadGrad { let m3 = bitcast(info[info_offset + 3u]); let matrx = vec4(m0, m1, m2, m3); let xlat = vec2(bitcast(info[info_offset + 4u]), bitcast(info[info_offset + 5u])); - let c1 = vec2(bitcast(info[info_offset + 6u]), bitcast(info[info_offset + 7u])); - let ra = bitcast(info[info_offset + 8u]); - let roff = bitcast(info[info_offset + 9u]); - return CmdRadGrad(index, extend_mode, matrx, xlat, c1, ra, roff); + let focal_x = bitcast(info[info_offset + 6u]); + let radius = bitcast(info[info_offset + 7u]); + let flags_kind = info[info_offset + 8u]; + let flags = flags_kind >> 3u; + let kind = flags_kind & 0x7u; + return CmdRadGrad(index, extend_mode, matrx, xlat, focal_x, radius, kind, flags); } fn read_image(cmd_ix: u32) -> CmdImage { @@ -295,17 +297,45 @@ fn main( // CMD_RAD_GRAD case 7u: { let rad = read_rad_grad(cmd_ix); - let rr = rad.rr; - let roff = rr - 1.0; + let focal_x = rad.focal_x; + let one_minus_focal_x = 1.0 - focal_x; + let radius = rad.radius; + let is_strip = rad.kind == RAD_GRAD_KIND_STRIP; + let is_circular = rad.kind == RAD_GRAD_KIND_CIRCULAR; + let is_focal_on_circle = rad.kind == RAD_GRAD_KIND_FOCAL_ON_CIRCLE; + let is_swapped = (rad.flags & RAD_GRAD_SWAPPED) != 0u; + let inv_r1 = select(1.0 / radius, 0.0, is_circular); + let root_f = select(1.0, -1.0, is_swapped || one_minus_focal_x < 0.0); + let t_base_scale = select(vec2(0.0, -1.0), vec2(1.0, 1.0), is_swapped); + let t_sign = sign(one_minus_focal_x); for (var i = 0u; i < PIXELS_PER_THREAD; i += 1u) { let my_xy = vec2(xy.x + f32(i), xy.y); - // TODO: can hoist y, but for now stick to the GLSL version - let xy_xformed = rad.matrx.xy * my_xy.x + rad.matrx.zw * my_xy.y + rad.xlat; - let ba = dot(xy_xformed, rad.c1); - let ca = rad.ra * dot(xy_xformed, xy_xformed); - let t = sqrt(ba * ba + ca) - ba; - if t >= 0.0 { - let x = i32(round(extend_mode(t * rr - roff, rad.extend_mode) * f32(GRADIENT_WIDTH - 1))); + let local_xy = rad.matrx.xy * my_xy.x + rad.matrx.zw * my_xy.y + rad.xlat; + let x = local_xy.x; + let y = local_xy.y; + let xx = x * x; + let yy = y * y; + let x_inv_r1 = x * inv_r1; + var t = 0.0; + var valid = true; + if is_strip { + let a = radius - yy; + t = sqrt(a) + x; + valid = a >= 0.0; + } else if is_focal_on_circle { + t = (xx + yy) / x; + valid = t >= 0.0; + } else if radius > 1.0 { + t = sqrt(xx + yy) - x_inv_r1; + } else { + let a = xx - yy; + t = root_f * sqrt(a) - x_inv_r1; + valid = a >= 0.0 && t >= 0.0; + } + if valid { + t = extend_mode(focal_x + t_sign * t, rad.extend_mode); + t = (t_base_scale.x - t) * t_base_scale.y; + let x = i32(round(t * f32(GRADIENT_WIDTH - 1))); let fg_rgba = textureLoad(gradients, vec2(x, i32(rad.index)), 0); let fg_i = fg_rgba * area[i]; rgba[i] = rgba[i] * (1.0 - fg_i.a) + fg_i; diff --git a/shader/pathseg.wgsl b/shader/pathseg.wgsl index ec059ab..bf02419 100644 --- a/shader/pathseg.wgsl +++ b/shader/pathseg.wgsl @@ -14,6 +14,7 @@ #import config #import pathtag #import cubic +#import transform @group(0) @binding(0) var config: Config; @@ -36,7 +37,6 @@ struct AtomicPathBbox { @group(0) @binding(3) var path_bboxes: array; - @group(0) @binding(4) var cubics: array; @@ -85,11 +85,6 @@ fn read_i16_point(ix: u32) -> vec2 { return vec2(x, y); } -struct Transform { - matrx: vec4, - translate: vec2, -} - fn read_transform(transform_base: u32, ix: u32) -> Transform { let base = transform_base + ix * 6u; let c0 = bitcast(scene[base]); @@ -103,10 +98,6 @@ fn read_transform(transform_base: u32, ix: u32) -> Transform { return Transform(matrx, translate); } -fn transform_apply(transform: Transform, p: vec2) -> vec2 { - return transform.matrx.xy * p.x + transform.matrx.zw * p.y + transform.translate; -} - fn round_down(x: f32) -> i32 { return i32(floor(x)); } diff --git a/shader/shared/config.wgsl b/shader/shared/config.wgsl index 1063134..9b1cd2e 100644 --- a/shader/shared/config.wgsl +++ b/shader/shared/config.wgsl @@ -49,3 +49,12 @@ let N_TILE_Y = 16u; let N_TILE = 256u; let BLEND_STACK_SPLIT = 4u; + +// Radial gradient kinds +let RAD_GRAD_KIND_CIRCULAR = 1u; +let RAD_GRAD_KIND_STRIP = 2u; +let RAD_GRAD_KIND_FOCAL_ON_CIRCLE = 3u; +let RAD_GRAD_KIND_CONE = 4u; + +// Radial gradient flags +let RAD_GRAD_SWAPPED = 1u; diff --git a/shader/shared/drawtag.wgsl b/shader/shared/drawtag.wgsl index 0d532d1..0b8ae41 100644 --- a/shader/shared/drawtag.wgsl +++ b/shader/shared/drawtag.wgsl @@ -18,7 +18,7 @@ struct DrawMonoid { let DRAWTAG_NOP = 0u; let DRAWTAG_FILL_COLOR = 0x44u; let DRAWTAG_FILL_LIN_GRADIENT = 0x114u; -let DRAWTAG_FILL_RAD_GRADIENT = 0x2dcu; +let DRAWTAG_FILL_RAD_GRADIENT = 0x29cu; let DRAWTAG_FILL_IMAGE = 0x248u; let DRAWTAG_BEGIN_CLIP = 0x9u; let DRAWTAG_END_CLIP = 0x21u; diff --git a/shader/shared/ptcl.wgsl b/shader/shared/ptcl.wgsl index 8e80524..d5fc5d4 100644 --- a/shader/shared/ptcl.wgsl +++ b/shader/shared/ptcl.wgsl @@ -55,9 +55,10 @@ struct CmdRadGrad { extend_mode: u32, matrx: vec4, xlat: vec2, - c1: vec2, - ra: f32, - rr: f32, + focal_x: f32, + radius: f32, + kind: u32, + flags: u32, } struct CmdImage { diff --git a/shader/shared/transform.wgsl b/shader/shared/transform.wgsl new file mode 100644 index 0000000..00d7279 --- /dev/null +++ b/shader/shared/transform.wgsl @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: Apache-2.0 OR MIT OR Unlicense + +// Helpers for working with transforms. + +struct Transform { + matrx: vec4, + translate: vec2, +} + +fn transform_apply(transform: Transform, p: vec2) -> vec2 { + return transform.matrx.xy * p.x + transform.matrx.zw * p.y + transform.translate; +} + +fn transform_inverse(transform: Transform) -> Transform { + let inv_det = 1.0 / (transform.matrx.x * transform.matrx.w - transform.matrx.y * transform.matrx.z); + let inv_mat = inv_det * vec4(transform.matrx.w, -transform.matrx.y, -transform.matrx.z, transform.matrx.x); + let inv_tr = mat2x2(inv_mat.xy, inv_mat.zw) * -transform.translate; + return Transform(inv_mat, inv_tr); +} + +fn transform_mul(a: Transform, b: Transform) -> Transform { + return Transform( + a.matrx.xyxy * b.matrx.xxzz + a.matrx.zwzw * b.matrx.yyww, + a.matrx.xy * b.translate.x + a.matrx.zw * b.translate.y + a.translate + ); +} diff --git a/src/shaders.rs b/src/shaders.rs index d1cc0dc..8da1807 100644 --- a/src/shaders.rs +++ b/src/shaders.rs @@ -318,4 +318,5 @@ const SHARED_SHADERS: &[(&str, &str)] = &[ shared_shader!("ptcl"), shared_shader!("segment"), shared_shader!("tile"), + shared_shader!("transform"), ];