From eb86456f31b792a128c4a554fec134451432f854 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Thu, 8 Apr 2021 19:55:44 +0200 Subject: [PATCH 1/3] elements.comp: don't modify BeginClip bounding box The BeginClip and EndClip bounding boxes are absolute and must pairwise match. I mistakenly modified the BeginClip bounding box for stroked clips. Signed-off-by: Elias Naur --- piet-gpu/shader/elements.comp | 4 ++-- piet-gpu/shader/elements.spv | Bin 71296 -> 71120 bytes 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/piet-gpu/shader/elements.comp b/piet-gpu/shader/elements.comp index 7fb02fc..56ad6e2 100644 --- a/piet-gpu/shader/elements.comp +++ b/piet-gpu/shader/elements.comp @@ -386,10 +386,10 @@ void main() { case Element_BeginClip: Clip begin_clip = Element_BeginClip_read(this_ref); AnnoBeginClip anno_begin_clip; + // This is the absolute bbox, it's been transformed during encoding. + anno_begin_clip.bbox = begin_clip.bbox; if (is_stroke) { vec2 lw = get_linewidth(st); - // This is the absolute bbox, it's been transformed during encoding. - anno_begin_clip.bbox = begin_clip.bbox + vec4(-lw, lw); anno_begin_clip.linewidth = st.linewidth * sqrt(abs(st.mat.x * st.mat.w - st.mat.y * st.mat.z)); } else { anno_begin_clip.bbox = begin_clip.bbox; diff --git a/piet-gpu/shader/elements.spv b/piet-gpu/shader/elements.spv index d1ca8dd4f2d9240477bf14ab198f8eff4b1beb20..6bd53b3ec06e76cc0bb53bf1053246fa7bb70323 100644 GIT binary patch delta 2410 zcmZ9OOKeni6voeVX4;v-QYK9^nl22%ZbXzZnn+Y)U1&5h(ztSkO)UwL@)$_EFy>Z^ zx^v;(xG|x=5i7zJ6}4ckQpFdb_yAGr0~ADq589$uzu%pE%^b4}-IeY>tMT{h z_(yg8Kpj6chiC0G@!qv}uk2Z~dcB1U%WcYgB)qR&3Qo-N%-HEVer68O+F9{UmF~40 zSFNkms>|AxYbz_gtKy0q=FPo>raGRl@oMXpV-2YV)x%eq=C+fPXnDl z)L>BqV;)$$96nDt>=i$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A=YaGy+v}A;d z31xp)+9Jg)RSX4%FVTU#T(;+gFH+&rv2(1^nq_ZOfnE@PGHYSgx?HeH7*1YdUlhha zi1}t=G`GJL+m|WV0{L$d$A_P5FUOjea9(>w8nOJ`@71i`ABi}&s6xy1+$txMl7t{R z*+ZHP+iSus7$4tENthGVq$F$>jvw|WZFZY5g*q9oexNqftmBEgBOF~_$iFVzj>vaf z-5K`x6dK=!k(ce_7pS4HJ$i_0Owa6GH?!`b2-ah|+< zgJEJ_ZE;A-V43=^aCuST`1myM3D4K?S>Si;gqP34Y|U!w6+Xzc1xrq^6_0 z<4|~HO>M^qQVwaXS82FOJ`_eS_K`UHZF;?WW9_gw8L_pfXoHG9B8<*KxKU3y zEqhD{;AnWq+CuP&l%pD)L1r5kM&rlsMCN>83`5&bTpD)pgsC6f$ zoY3&r{UnT5-nyT~(aT$RN}PC-l=MFbUx+&l4XR+kBv@^D_2$+eW~eRCcb?L-m1C6pc=F8}}l From f4be74c07f106ba9973925e1b193ae9a11911269 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Sat, 10 Apr 2021 18:12:21 +0200 Subject: [PATCH 2/3] winit: fix n_trans count Signed-off-by: Elias Naur --- piet-gpu/bin/winit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/piet-gpu/bin/winit.rs b/piet-gpu/bin/winit.rs index 51004cd..e8a7f32 100644 --- a/piet-gpu/bin/winit.rs +++ b/piet-gpu/bin/winit.rs @@ -40,7 +40,7 @@ fn main() -> Result<(), Error> { render_scene(&mut ctx); let n_paths = ctx.path_count(); let n_pathseg = ctx.pathseg_count(); - let n_trans = ctx.pathseg_count(); + let n_trans = ctx.trans_count(); let scene = ctx.get_scene_buf(); let renderer = Renderer::new(&session, scene, n_paths, n_pathseg, n_trans)?; From 0637e2d6e507cb6c15a3cb78b1106fa0cba9abe4 Mon Sep 17 00:00:00 2001 From: Tatsuyuki Ishi Date: Sun, 11 Apr 2021 13:20:40 +0900 Subject: [PATCH 3/3] Encode premultiplied alpha in render_ctx.rs --- piet-gpu/src/lib.rs | 10 ++----- piet-gpu/src/render_ctx.rs | 58 +++++++++++++++++++++++++------------- 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/piet-gpu/src/lib.rs b/piet-gpu/src/lib.rs index 3e46eae..3a2b33e 100644 --- a/piet-gpu/src/lib.rs +++ b/piet-gpu/src/lib.rs @@ -127,16 +127,12 @@ fn render_clip_test(rc: &mut impl RenderContext) { #[allow(unused)] fn render_alpha_test(rc: &mut impl RenderContext) { - // Alpha compositing tests. kernel4 expects colors encoded in alpha-premultiplied sRGB: - // - // [α,sRGB(α⋅R),sRGB(α⋅G),sRGB(α⋅B)] - // - // See also http://ssp.impulsetrain.com/gamma-premult.html. + // Alpha compositing tests. rc.fill(diamond(Point::new(1024.0, 100.0)), &Color::Rgba32(0xff0000ff)); - rc.fill(diamond(Point::new(1024.0, 125.0)), &Color::Rgba32(0x00ba0080)); + rc.fill(diamond(Point::new(1024.0, 125.0)), &Color::Rgba32(0x00ff0080)); rc.save(); rc.clip(diamond(Point::new(1024.0, 150.0))); - rc.fill(diamond(Point::new(1024.0, 175.0)), &Color::Rgba32(0x0000ba80)); + rc.fill(diamond(Point::new(1024.0, 175.0)), &Color::Rgba32(0x0000ff80)); rc.restore(); } diff --git a/piet-gpu/src/render_ctx.rs b/piet-gpu/src/render_ctx.rs index e80d67d..ab3d777 100644 --- a/piet-gpu/src/render_ctx.rs +++ b/piet-gpu/src/render_ctx.rs @@ -1,21 +1,19 @@ use std::{borrow::Cow, ops::RangeBounds}; -use piet_gpu_types::encoder::{Encode, Encoder}; - -use piet_gpu_types::scene::{ - Clip, CubicSeg, Element, FillColor, SetFillMode, LineSeg, QuadSeg, SetLineWidth, Transform, -}; - use piet::{ - kurbo::{Affine, Insets, PathEl, Point, Rect, Shape, Size}, - HitTestPosition, TextAttribute, TextStorage, + HitTestPosition, + kurbo::{Affine, Insets, PathEl, Point, Rect, Shape, Size}, TextAttribute, TextStorage, }; - use piet::{ Color, Error, FixedGradient, FontFamily, HitTestPoint, ImageFormat, InterpolationMode, IntoBrush, LineMetric, RenderContext, StrokeStyle, Text, TextLayout, TextLayoutBuilder, }; +use piet_gpu_types::encoder::{Encode, Encoder}; +use piet_gpu_types::scene::{ + Clip, CubicSeg, Element, FillColor, LineSeg, QuadSeg, SetFillMode, SetLineWidth, Transform, +}; + pub struct PietGpuImage; #[derive(Clone)] @@ -70,7 +68,7 @@ struct ClipElement { bbox: Option, } -#[derive(Clone,Copy,PartialEq)] +#[derive(Clone, Copy, PartialEq)] enum FillMode { // Fill path according to the non-zero winding rule. Nonzero = 0, @@ -138,7 +136,14 @@ impl RenderContext for PietGpuRenderContext { } fn solid_brush(&mut self, color: Color) -> Self::Brush { - PietGpuBrush::Solid(color.as_rgba_u32()) + // kernel4 expects colors encoded in alpha-premultiplied sRGB: + // + // [α,sRGB(α⋅R),sRGB(α⋅G),sRGB(α⋅B)] + // + // See also http://ssp.impulsetrain.com/gamma-premult.html. + let (r, g, b, a) = color.as_rgba(); + let premul = Color::rgba(to_srgb(from_srgb(r) * a), to_srgb(from_srgb(g) * a), to_srgb(from_srgb(b) * a), a); + PietGpuBrush::Solid(premul.as_rgba_u32()) } fn gradient(&mut self, _gradient: impl Into) -> Result { @@ -176,8 +181,7 @@ impl RenderContext for PietGpuRenderContext { _brush: &impl IntoBrush, _width: f64, _style: &StrokeStyle, - ) { - } + ) {} fn fill(&mut self, shape: impl Shape, brush: &impl IntoBrush) { let brush = brush.make_brush(self, || shape.bounding_box()).into_owned(); @@ -279,8 +283,7 @@ impl RenderContext for PietGpuRenderContext { _image: &Self::Image, _rect: impl Into, _interp: InterpolationMode, - ) { - } + ) {} fn draw_image_area( &mut self, @@ -288,8 +291,7 @@ impl RenderContext for PietGpuRenderContext { _src_rect: impl Into, _dst_rect: impl Into, _interp: InterpolationMode, - ) { - } + ) {} fn blurred_rect(&mut self, _rect: Rect, _blur_radius: f64, _brush: &impl IntoBrush) {} @@ -320,7 +322,7 @@ impl PietGpuRenderContext { self.pathseg_count += 1; } - fn encode_path(&mut self, path: impl Iterator, is_fill: bool) { + fn encode_path(&mut self, path: impl Iterator, is_fill: bool) { if is_fill { self.encode_path_inner(path.flat_map(|el| { match el { @@ -335,7 +337,7 @@ impl PietGpuRenderContext { } } - fn encode_path_inner(&mut self, path: impl Iterator) { + fn encode_path_inner(&mut self, path: impl Iterator) { let flatten = false; if flatten { let mut start_pt = None; @@ -583,3 +585,21 @@ fn to_scene_transform(transform: Affine) -> Transform { translate: [c[4] as f32, c[5] as f32], } } + +fn to_srgb(f: f64) -> f64 { + if f <= 0.0031308 { + f * 12.92 + } else { + let a = 0.055; + (1. + a) * f64::powf(f, f64::recip(2.4)) - a + } +} + +fn from_srgb(f: f64) -> f64 { + if f <= 0.04045 { + f / 12.92 + } else { + let a = 0.055; + f64::powf((f + a) * f64::recip(1. + a), 2.4) + } +} \ No newline at end of file