Skip to content

Commit

Permalink
feat: optimize some ifs in hotpath (#23)
Browse files Browse the repository at this point in the history
* feat: optimize some ifs in hotpath

* fix: optimize to unwrap_or_default for dark color and set 0.0 case for premultiplied alpha

* fix ci

---------

Co-authored-by: jabu <[email protected]>
  • Loading branch information
dt665m and jabuwu authored Mar 17, 2024
1 parent 6435376 commit 9d6eef9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 26 deletions.
28 changes: 11 additions & 17 deletions src/draw/combined.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{c::c_void, BlendMode, Color, Skeleton, SkeletonClipping};
use crate::{c::c_void, BlendMode, Skeleton, SkeletonClipping};

use super::{ColorSpace, CullDirection};

Expand Down Expand Up @@ -186,21 +186,19 @@ impl CombinedDrawer {
slot.attachment().and_then(|a| a.as_mesh())
{
let mut color = mesh_attachment.color() * slot.color() * skeleton.color();
let mut dark_color = slot.dark_color().unwrap_or_default();
if self.premultiplied_alpha {
color.premultiply_alpha();
dark_color *= color.a;
dark_color.a = 1.0;
} else {
dark_color.a = 0.;
}
color = match self.color_space {
ColorSpace::SRGB => color,
ColorSpace::Linear => color.nonlinear_to_linear(),
};

let mut dark_color = slot
.dark_color()
.unwrap_or_else(|| Color::new_rgba(0.0, 0.0, 0.0, 0.0));
if self.premultiplied_alpha {
dark_color *= color.a;
dark_color.a = 1.0;
}
dark_color = match self.color_space {
ColorSpace::SRGB => dark_color,
ColorSpace::Linear => dark_color.nonlinear_to_linear(),
Expand Down Expand Up @@ -273,23 +271,19 @@ impl CombinedDrawer {
(color, dark_color)
} else if let Some(region_attachment) = slot.attachment().and_then(|a| a.as_region()) {
let mut color = region_attachment.color() * slot.color() * skeleton.color();
let mut dark_color = slot.dark_color().unwrap_or_default();
if self.premultiplied_alpha {
color.premultiply_alpha();
dark_color *= color.a;
dark_color.a = 1.0;
} else {
dark_color.a = 0.;
}
color = match self.color_space {
ColorSpace::SRGB => color,
ColorSpace::Linear => color.nonlinear_to_linear(),
};

let mut dark_color = slot
.dark_color()
.unwrap_or_else(|| Color::new_rgba(0.0, 0.0, 0.0, 0.0));
if self.premultiplied_alpha {
dark_color *= color.a;
dark_color.a = 1.0;
} else {
dark_color.a = 0.0;
}
dark_color = match self.color_space {
ColorSpace::SRGB => dark_color,
ColorSpace::Linear => dark_color.nonlinear_to_linear(),
Expand Down
14 changes: 5 additions & 9 deletions src/draw/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,23 +284,19 @@ impl SimpleDrawer {
);

color *= slot.color() * skeleton.color();
let mut dark_color = slot.dark_color().unwrap_or_default();
if self.premultiplied_alpha {
color.premultiply_alpha();
dark_color *= color.a;
dark_color.a = 1.0;
} else {
dark_color.a = 0.;
}
color = match self.color_space {
ColorSpace::SRGB => color,
ColorSpace::Linear => color.nonlinear_to_linear(),
};

let mut dark_color = slot
.dark_color()
.unwrap_or_else(|| Color::new_rgba(0.0, 0.0, 0.0, 0.0));
if self.premultiplied_alpha {
dark_color *= color.a;
dark_color.a = 1.0;
} else {
dark_color.a = 0.0;
}
dark_color = match self.color_space {
ColorSpace::SRGB => dark_color,
ColorSpace::Linear => dark_color.nonlinear_to_linear(),
Expand Down

0 comments on commit 9d6eef9

Please sign in to comment.