Skip to content

Commit

Permalink
Unify encoding of Scene/SceneFragment (#426)
Browse files Browse the repository at this point in the history
In a move toward dropping the SceneFragment and SceneBuilder types, this unifies the underlying encoding of Scene and SceneFragment. Specifically, we no longer encode an initial transform and style for Scene.

This causes the transform and style indices in the path tag monoid to be off by one. I chose the simple approach of subtracting 1 from each in the combine_tag_monoid() functions in both GPU and CPU code to correct this.
  • Loading branch information
dfrg committed Feb 13, 2024
1 parent 89d1f21 commit 6f0621e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
11 changes: 6 additions & 5 deletions crates/encoding/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ use {
};

/// Encoded data streams for a scene.
///
/// # Invariants
///
/// * At least one transform and style must be encoded before any path data
/// or draw object.
#[derive(Clone, Default)]
pub struct Encoding {
/// The path tag stream.
Expand Down Expand Up @@ -65,7 +70,7 @@ impl Encoding {
}

/// Clears the encoding.
pub fn reset(&mut self, is_fragment: bool) {
pub fn reset(&mut self) {
self.transforms.clear();
self.path_tags.clear();
self.path_data.clear();
Expand All @@ -79,10 +84,6 @@ impl Encoding {
self.flags = 0;
#[cfg(feature = "full")]
self.resources.reset();
if !is_fragment {
self.transforms.push(Transform::IDENTITY);
self.styles.push(Style::from_fill(Fill::NonZero));
}
}

/// Appends another encoding to this one with an optional transform.
Expand Down
2 changes: 1 addition & 1 deletion crates/encoding/src/glyph_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct GlyphCache {

impl GlyphCache {
pub fn clear(&mut self) {
self.encoding.reset(true);
self.encoding.reset();
self.glyphs.clear();
}

Expand Down
6 changes: 6 additions & 0 deletions shader/flatten.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,12 @@ fn compute_tag_monoid(ix: u32) -> PathTagData {
// TODO: this can be a read buf overflow. Conditionalize by tag byte?
tm = combine_tag_monoid(tag_monoids[ix >> 2u], tm);
var tag_byte = (tag_word >> shift) & 0xffu;
// We no longer encode an initial transform and style so these
// are off by one.
// Note: an alternative would be to adjust config.transform_base and
// config.style_base.
tm.trans_ix -= 1u;
tm.style_ix -= STYLE_SIZE_IN_WORDS;
return PathTagData(tag_byte, tm);
}

Expand Down
4 changes: 4 additions & 0 deletions src/cpu_shader/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,10 @@ fn compute_tag_monoid(ix: usize, pathtags: &[u32], tag_monoids: &[PathMonoid]) -
if tag_byte != 0 {
tm = tag_monoids[ix >> 2].combine(&tm);
}
// We no longer encode an initial transform and style so these
// are off by one.
tm.trans_ix -= 1;
tm.style_ix -= core::mem::size_of::<Style>() as u32 / 4;
PathTagData {
tag_byte,
monoid: tm,
Expand Down
8 changes: 4 additions & 4 deletions src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,18 @@ impl<'a> SceneBuilder<'a> {
/// Creates a new builder for filling a scene. Any current content in the scene
/// will be cleared.
pub fn for_scene(scene: &'a mut Scene) -> Self {
Self::new(&mut scene.data, false)
Self::new(&mut scene.data)
}

/// Creates a new builder for filling a scene fragment. Any current content in
/// the fragment will be cleared.
pub fn for_fragment(fragment: &'a mut SceneFragment) -> Self {
Self::new(&mut fragment.data, true)
Self::new(&mut fragment.data)
}

/// Creates a new builder for constructing a scene.
fn new(scene: &'a mut Encoding, is_fragment: bool) -> Self {
scene.reset(is_fragment);
fn new(scene: &'a mut Encoding) -> Self {
scene.reset();
Self { scene }
}

Expand Down

0 comments on commit 6f0621e

Please sign in to comment.