diff --git a/piet-gpu/bin/winit.rs b/piet-gpu/bin/winit.rs index 420ffa5..5eb5be6 100644 --- a/piet-gpu/bin/winit.rs +++ b/piet-gpu/bin/winit.rs @@ -41,7 +41,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)?; 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 d1ca8dd..6bd53b3 100644 Binary files a/piet-gpu/shader/elements.spv and b/piet-gpu/shader/elements.spv differ diff --git a/piet-gpu/src/lib.rs b/piet-gpu/src/lib.rs index 29a1fa4..8e5be4d 100644 --- a/piet-gpu/src/lib.rs +++ b/piet-gpu/src/lib.rs @@ -127,25 +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. - rc.fill( - diamond(Point::new(1024.0, 100.0)), - &Color::Rgba32(0xff0000ff), - ); - rc.fill( - diamond(Point::new(1024.0, 125.0)), - &Color::Rgba32(0x00ba0080), - ); + // 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(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 e33974c..74503ef 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, LineSeg, QuadSeg, SetFillMode, 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)] @@ -139,7 +137,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 { @@ -177,8 +182,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(); @@ -280,8 +284,7 @@ impl RenderContext for PietGpuRenderContext { _image: &Self::Image, _rect: impl Into, _interp: InterpolationMode, - ) { - } + ) {} fn draw_image_area( &mut self, @@ -289,8 +292,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) {} @@ -321,7 +323,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| { @@ -339,7 +341,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; @@ -587,3 +589,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