-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Reimplement background checkerboard rendering #4034
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
timon-schelling
wants to merge
16
commits into
master
Choose a base branch
from
redo-transparent-bg-render
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
dd10b26
Revert
timon-schelling 6b50d3e
WIP
timon-schelling d8a5e80
Remove Background Unit struct
timon-schelling 109c39b
Refactor
timon-schelling cfe6429
Unrevert
timon-schelling aeb2044
Add Texture Cache
timon-schelling 3005ff3
Review
timon-schelling 58e3313
Destroy textures
timon-schelling fd25571
Add memo node
timon-schelling 400dede
Two pipelines
timon-schelling 4713c4e
One node
timon-schelling 635965b
One node improvements
timon-schelling b9ddfd0
Cleanup
timon-schelling 3398e72
Cleanup
timon-schelling 64feff4
Improve
timon-schelling 5d44cca
Fix
timon-schelling File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| use crate::renderer::{RenderParams, SvgRender}; | ||
| use core_types::transform::Footprint; | ||
| use core_types::uuid::generate_uuid; | ||
| use dyn_any::DynAny; | ||
| use glam::DVec2; | ||
| use glam::{DAffine2, IVec2}; | ||
| use std::fmt::Write; | ||
| use std::sync::{Arc, LazyLock}; | ||
|
|
||
| #[derive(Debug, Default, Clone, PartialEq, DynAny, serde::Serialize, serde::Deserialize)] | ||
| pub struct Background { | ||
| pub location: IVec2, | ||
| pub dimensions: IVec2, | ||
| } | ||
|
|
||
| pub trait RenderBackground { | ||
| fn render_background_to_vello(&self, scene: &mut vello::Scene, transform: DAffine2, render_params: &RenderParams); | ||
| fn render_background_svg(&self, render: &mut SvgRender, render_params: &RenderParams); | ||
| } | ||
|
|
||
| impl RenderBackground for Background { | ||
| fn render_background_to_vello(&self, scene: &mut vello::Scene, transform: DAffine2, render_params: &RenderParams) { | ||
| let rect = background_rect(self); | ||
| checkerboard_fill_vello(scene, transform, rect, DVec2::new(rect.x0, rect.y0), render_params.viewport_zoom); | ||
| } | ||
|
|
||
| fn render_background_svg(&self, render: &mut SvgRender, render_params: &RenderParams) { | ||
| let rect = background_rect(self); | ||
| checkerboard_fill_svg(render, rect, DVec2::new(rect.x0, rect.y0), render_params.viewport_zoom, "checkered-artboard"); | ||
| } | ||
| } | ||
|
|
||
| impl RenderBackground for Vec<Background> { | ||
| fn render_background_to_vello(&self, scene: &mut vello::Scene, transform: DAffine2, render_params: &RenderParams) { | ||
| if self.is_empty() { | ||
| let Some(rect) = viewport_rect(render_params.footprint, render_params.scale) else { return }; | ||
| checkerboard_fill_vello(scene, transform, rect, DVec2::ZERO, render_params.viewport_zoom); | ||
| return; | ||
| } | ||
|
|
||
| for background in self { | ||
| background.render_background_to_vello(scene, transform, render_params); | ||
| } | ||
| } | ||
|
|
||
| fn render_background_svg(&self, render: &mut SvgRender, render_params: &RenderParams) { | ||
| if self.is_empty() { | ||
| let Some(rect) = viewport_rect(render_params.footprint, render_params.scale) else { return }; | ||
| checkerboard_fill_svg(render, rect, DVec2::ZERO, render_params.viewport_zoom, "checkered-viewport"); | ||
| return; | ||
| } | ||
|
|
||
| for background in self { | ||
| background.render_background_svg(render, render_params); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn checkerboard_fill_vello(scene: &mut vello::Scene, transform: DAffine2, rect: kurbo::Rect, pattern_origin: DVec2, viewport_zoom: f64) { | ||
| if viewport_zoom <= 0. { | ||
| return; | ||
| } | ||
|
|
||
| let brush = vello::peniko::Brush::Image(vello::peniko::ImageBrush { | ||
| image: vello::peniko::ImageData { | ||
| data: vello::peniko::Blob::new(CHECKERBOARD_IMAGE_DATA.clone()), | ||
| format: vello::peniko::ImageFormat::Rgba8, | ||
| width: 16, | ||
| height: 16, | ||
| alpha_type: vello::peniko::ImageAlphaType::Alpha, | ||
| }, | ||
| sampler: vello::peniko::ImageSampler { | ||
| x_extend: vello::peniko::Extend::Repeat, | ||
| y_extend: vello::peniko::Extend::Repeat, | ||
| quality: vello::peniko::ImageQuality::Low, | ||
| alpha: 1., | ||
| }, | ||
| }); | ||
| let brush_transform = kurbo::Affine::scale(1. / viewport_zoom).then_translate(kurbo::Vec2::new(pattern_origin.x, pattern_origin.y)); | ||
| scene.fill(vello::peniko::Fill::NonZero, kurbo::Affine::new(transform.to_cols_array()), &brush, Some(brush_transform), &rect); | ||
| } | ||
|
|
||
| fn checkerboard_fill_svg(render: &mut SvgRender, rect: kurbo::Rect, pattern_origin: DVec2, viewport_zoom: f64, checker_id_prefix: &str) { | ||
| if viewport_zoom <= 0. { | ||
| return; | ||
| } | ||
|
|
||
| let checker_id = format!("{checker_id_prefix}-{}", generate_uuid()); | ||
|
|
||
| let svg_defs: &mut String = &mut render.svg_defs; | ||
| let pattern_id: &str = &checker_id; | ||
|
|
||
| let cell_size = 8. / viewport_zoom; | ||
| let pattern_size = cell_size * 2.; | ||
|
|
||
| write!( | ||
| svg_defs, | ||
| r##"<pattern id="{pattern_id}" x="{}" y="{}" width="{pattern_size}" height="{pattern_size}" patternUnits="userSpaceOnUse"><rect width="{pattern_size}" height="{pattern_size}" fill="#ffffff" /><rect x="{cell_size}" y="0" width="{cell_size}" height="{cell_size}" fill="#cccccc" /><rect x="0" y="{cell_size}" width="{cell_size}" height="{cell_size}" fill="#cccccc" /></pattern>"##, | ||
| pattern_origin.x, | ||
| pattern_origin.y, | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| render.leaf_tag("rect", |attributes| { | ||
| attributes.push("x", rect.x0.to_string()); | ||
| attributes.push("y", rect.y0.to_string()); | ||
| attributes.push("width", rect.width().to_string()); | ||
| attributes.push("height", rect.height().to_string()); | ||
| attributes.push("fill", format!("url(#{checker_id})")); | ||
| }); | ||
| } | ||
|
|
||
| fn background_rect(background: &Background) -> kurbo::Rect { | ||
| let [a, b] = [background.location.as_dvec2(), background.location.as_dvec2() + background.dimensions.as_dvec2()]; | ||
| kurbo::Rect::new(a.x.min(b.x), a.y.min(b.y), a.x.max(b.x), a.y.max(b.y)) | ||
| } | ||
|
|
||
| fn viewport_rect(footprint: Footprint, scale: f64) -> Option<kurbo::Rect> { | ||
| if scale <= 0. { | ||
| return None; | ||
| } | ||
|
|
||
| let logical_resolution = footprint.resolution.as_dvec2() / scale; | ||
| let logical_footprint = Footprint { | ||
| resolution: logical_resolution.round().as_uvec2().max(glam::UVec2::ONE), | ||
| ..footprint | ||
| }; | ||
| let bounds = logical_footprint.viewport_bounds_in_local_space(); | ||
| let min = bounds.start.floor(); | ||
| let max = bounds.end.ceil(); | ||
|
|
||
| if !(min.is_finite() && max.is_finite()) { | ||
| return None; | ||
| } | ||
|
|
||
| Some(kurbo::Rect::new(min.x, min.y, max.x, max.y)) | ||
| } | ||
|
|
||
| /// Cached 16x16 transparency checkerboard image data (four 8x8 cells of #ffffff and #cccccc). | ||
| static CHECKERBOARD_IMAGE_DATA: LazyLock<Arc<Vec<u8>>> = LazyLock::new(|| { | ||
| const SIZE: u32 = 16; | ||
| const HALF: u32 = 8; | ||
|
|
||
| let mut data = vec![0_u8; (SIZE * SIZE * 4) as usize]; | ||
| for y in 0..SIZE { | ||
| for x in 0..SIZE { | ||
| let is_light = ((x / HALF) + (y / HALF)).is_multiple_of(2); | ||
| let value = if is_light { 0xff } else { 0xcc }; | ||
| let index = ((y * SIZE + x) * 4) as usize; | ||
| data[index] = value; | ||
| data[index + 1] = value; | ||
| data[index + 2] = value; | ||
| data[index + 3] = 0xff; | ||
| } | ||
| } | ||
|
|
||
| Arc::new(data) | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| pub mod background; | ||
| pub mod convert_usvg_path; | ||
| pub mod render_ext; | ||
| mod renderer; | ||
| pub mod to_peniko; | ||
|
|
||
| pub use background::RenderBackground; | ||
| pub use renderer::*; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.