-
-
Notifications
You must be signed in to change notification settings - Fork 1k
render: Improve hairline strokes and scaling strokes on WebGL and WGPU #23011
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
Merged
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| use ruffle_render::backend::ShapeHandle; | ||
|
|
||
| /// The scale ratio threshold beyond which a graphic or a drawing will be retessellated. | ||
| const RETESSELLATION_SCALE_THRESHOLD: f32 = 2.0; | ||
|
|
||
| /// The inverse of the retessellation scale threshold | ||
| /// Used to avoid computing the inverse repeatedly when comparing scale ratios against the threshold. | ||
| const RETESSELLATION_SCALE_THRESHOLD_INVERSE: f32 = 1.0 / RETESSELLATION_SCALE_THRESHOLD; | ||
|
|
||
| /// The maximum number of retessellated shapes to cache per original shape. | ||
| const RETESSELLATION_CACHE_SIZE: usize = 4; | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub(crate) struct TessellationCache { | ||
| entries: [Option<(f32, ShapeHandle)>; RETESSELLATION_CACHE_SIZE], | ||
| len: usize, | ||
| } | ||
|
|
||
| /// A cache for tessellated shapes at different scales, using a simple LRU eviction policy. | ||
| /// (LRU index: 0, MRU index: len - 1) | ||
| /// | ||
| /// The cache stores a fixed number of entries, and when it is full, the least recently | ||
| /// used entry is evicted to make room for new entries. | ||
| /// | ||
| /// This is used to avoid retessellating shapes at similar scales multiple times, which can be expensive. | ||
| impl TessellationCache { | ||
| /// Creates a new, empty tessellation cache. | ||
| pub(crate) fn new() -> Self { | ||
| Self { | ||
| entries: std::array::from_fn(|_| None), | ||
| len: 0, | ||
| } | ||
| } | ||
|
|
||
| /// Finds the cached shape handle with the closest scale to the target scale. | ||
| /// | ||
| /// If the closest scale is NOT within the retessellation threshold, | ||
| /// then `None` is returned, indicating that the shape should be retessellated at the target scale. | ||
| pub(crate) fn find_near_and_touch(&mut self, target_scale: f32) -> Option<ShapeHandle> { | ||
| let mut best_index = None; | ||
| let mut best_deviation = f32::INFINITY; | ||
|
|
||
| for index in 0..self.len { | ||
| if let Some((cached_scale, _)) = &self.entries[index] { | ||
| let ratio = f32::abs(target_scale / cached_scale); | ||
|
|
||
| // Check if the cached scale is within the retessellation threshold of the target scale. | ||
| if ratio <= RETESSELLATION_SCALE_THRESHOLD | ||
| && ratio >= RETESSELLATION_SCALE_THRESHOLD_INVERSE | ||
| { | ||
| // Choose the cached entry with the smallest deviation from the target scale. | ||
| let deviation = f32::abs(ratio - 1.0); | ||
|
|
||
| if deviation < best_deviation { | ||
| best_deviation = deviation; | ||
| best_index = Some(index); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // If we found a suitable cached entry, move it to the most recently used position. | ||
| best_index.map(|index| self.touch_entry(index)) | ||
| } | ||
|
|
||
| /// Inserts a new cached shape handle with the given scale. | ||
| /// | ||
| /// If the cache is full, the least recently used entry will be evicted to make room for the new entry. | ||
| /// We assume that the caller has already checked that the new entry is not too similar to existing entries. | ||
| pub(crate) fn insert(&mut self, scale: f32, handle: ShapeHandle) { | ||
| if self.len < RETESSELLATION_CACHE_SIZE { | ||
| // If the cache is not full, simply add the new entry at the end. | ||
| self.entries[self.len] = Some((scale, handle)); | ||
| self.len += 1; | ||
| return; | ||
| } | ||
|
|
||
| // If the cache is full, evict the least recently used entry. | ||
| for i in 1..self.len { | ||
| self.entries[i - 1] = self.entries[i].take(); | ||
| } | ||
|
|
||
| self.entries[self.len - 1] = Some((scale, handle)); | ||
| } | ||
|
|
||
| /// Returns the number of cached entries currently stored in the cache. | ||
| pub(crate) fn len(&self) -> usize { | ||
| self.len | ||
| } | ||
|
|
||
| /// Moves the entry at the given index to the most recently used position and returns its shape handle. | ||
| fn touch_entry(&mut self, index: usize) -> ShapeHandle { | ||
| if index == self.len - 1 { | ||
| // The entry is already the most recently used, so we can return its handle directly. | ||
| return self.entries[index] | ||
| .as_ref() | ||
| .expect("tessellation cache entry exists") | ||
| .1 | ||
| .clone(); | ||
| } | ||
|
|
||
| let entry = self.entries[index].take().expect("entry exists"); | ||
|
|
||
| for i in index + 1..self.len { | ||
| self.entries[i - 1] = self.entries[i].take(); | ||
| } | ||
|
|
||
| // Move touched entry to MRU position (end) | ||
| self.entries[self.len - 1] = Some(entry.clone()); | ||
| entry.1 | ||
| } | ||
| } | ||
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
Binary file modified
BIN
-11.4 KB
(7.3%)
tests/tests/swfs/from_gnash/misc-ming.all/shape_test/output.ruffle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Binary file modified
BIN
+451 Bytes
(110%)
tests/tests/swfs/from_shumway/acid/acid-mask/output.01.ruffle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+434 Bytes
(110%)
tests/tests/swfs/from_shumway/acid/acid-mask/output.05.ruffle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+419 Bytes
(110%)
tests/tests/swfs/from_shumway/acid/acid-mask/output.10.ruffle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-20.8 KB
(11%)
tests/tests/swfs/from_shumway/acid/acid-stroke-0/output.ruffle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-5.98 KB
(35%)
tests/tests/swfs/visual/simple_shapes/strokes/scale/output.ruffle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.