Optimize: add #[inline(always)] to various low-level things

saves up to 20% (text tesselation), and at least 5% overall
This commit is contained in:
Emil Ernerfeldt 2021-03-28 23:16:19 +02:00
parent ccc501f302
commit 46425f1e38
10 changed files with 144 additions and 7 deletions

View file

@ -61,6 +61,7 @@ impl Id {
format!("{:04X}", self.0 as u16) format!("{:04X}", self.0 as u16)
} }
#[inline(always)]
pub(crate) fn value(&self) -> u64 { pub(crate) fn value(&self) -> u64 {
self.0 self.0
} }

View file

@ -92,11 +92,13 @@ pub trait One {
fn one() -> Self; fn one() -> Self;
} }
impl One for f32 { impl One for f32 {
#[inline(always)]
fn one() -> Self { fn one() -> Self {
1.0 1.0
} }
} }
impl One for f64 { impl One for f64 {
#[inline(always)]
fn one() -> Self { fn one() -> Self {
1.0 1.0
} }
@ -121,6 +123,7 @@ impl Real for f64 {}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/// Linear interpolation. /// Linear interpolation.
#[inline(always)]
pub fn lerp<R, T>(range: RangeInclusive<R>, t: T) -> R pub fn lerp<R, T>(range: RangeInclusive<R>, t: T) -> R
where where
T: Real + Mul<R, Output = R>, T: Real + Mul<R, Output = R>,
@ -309,9 +312,11 @@ pub trait NumExt {
macro_rules! impl_num_ext { macro_rules! impl_num_ext {
($t: ty) => { ($t: ty) => {
impl NumExt for $t { impl NumExt for $t {
#[inline(always)]
fn at_least(self, lower_limit: Self) -> Self { fn at_least(self, lower_limit: Self) -> Self {
self.max(lower_limit) self.max(lower_limit)
} }
#[inline(always)]
fn at_most(self, upper_limit: Self) -> Self { fn at_most(self, upper_limit: Self) -> Self {
self.min(upper_limit) self.min(upper_limit)
} }

View file

@ -20,9 +20,13 @@ macro_rules! impl_numeric_float {
const INTEGRAL: bool = false; const INTEGRAL: bool = false;
const MIN: Self = std::$t::MIN; const MIN: Self = std::$t::MIN;
const MAX: Self = std::$t::MAX; const MAX: Self = std::$t::MAX;
#[inline(always)]
fn to_f64(self) -> f64 { fn to_f64(self) -> f64 {
self as f64 self as f64
} }
#[inline(always)]
fn from_f64(num: f64) -> Self { fn from_f64(num: f64) -> Self {
num as Self num as Self
} }
@ -36,9 +40,13 @@ macro_rules! impl_numeric_integer {
const INTEGRAL: bool = true; const INTEGRAL: bool = true;
const MIN: Self = std::$t::MIN; const MIN: Self = std::$t::MIN;
const MAX: Self = std::$t::MAX; const MAX: Self = std::$t::MAX;
#[inline(always)]
fn to_f64(self) -> f64 { fn to_f64(self) -> f64 {
self as f64 self as f64
} }
#[inline(always)]
fn from_f64(num: f64) -> Self { fn from_f64(num: f64) -> Self {
num as Self num as Self
} }

View file

@ -26,24 +26,28 @@ pub const fn pos2(x: f32, y: f32) -> Pos2 {
// Compatibility and convenience conversions to and from [f32; 2]: // Compatibility and convenience conversions to and from [f32; 2]:
impl From<[f32; 2]> for Pos2 { impl From<[f32; 2]> for Pos2 {
#[inline(always)]
fn from(v: [f32; 2]) -> Self { fn from(v: [f32; 2]) -> Self {
Self { x: v[0], y: v[1] } Self { x: v[0], y: v[1] }
} }
} }
impl From<&[f32; 2]> for Pos2 { impl From<&[f32; 2]> for Pos2 {
#[inline(always)]
fn from(v: &[f32; 2]) -> Self { fn from(v: &[f32; 2]) -> Self {
Self { x: v[0], y: v[1] } Self { x: v[0], y: v[1] }
} }
} }
impl From<Pos2> for [f32; 2] { impl From<Pos2> for [f32; 2] {
#[inline(always)]
fn from(v: Pos2) -> Self { fn from(v: Pos2) -> Self {
[v.x, v.y] [v.x, v.y]
} }
} }
impl From<&Pos2> for [f32; 2] { impl From<&Pos2> for [f32; 2] {
#[inline(always)]
fn from(v: &Pos2) -> Self { fn from(v: &Pos2) -> Self {
[v.x, v.y] [v.x, v.y]
} }
@ -53,24 +57,28 @@ impl From<&Pos2> for [f32; 2] {
// Compatibility and convenience conversions to and from (f32, f32): // Compatibility and convenience conversions to and from (f32, f32):
impl From<(f32, f32)> for Pos2 { impl From<(f32, f32)> for Pos2 {
#[inline(always)]
fn from(v: (f32, f32)) -> Self { fn from(v: (f32, f32)) -> Self {
Self { x: v.0, y: v.1 } Self { x: v.0, y: v.1 }
} }
} }
impl From<&(f32, f32)> for Pos2 { impl From<&(f32, f32)> for Pos2 {
#[inline(always)]
fn from(v: &(f32, f32)) -> Self { fn from(v: &(f32, f32)) -> Self {
Self { x: v.0, y: v.1 } Self { x: v.0, y: v.1 }
} }
} }
impl From<Pos2> for (f32, f32) { impl From<Pos2> for (f32, f32) {
#[inline(always)]
fn from(v: Pos2) -> Self { fn from(v: Pos2) -> Self {
(v.x, v.y) (v.x, v.y)
} }
} }
impl From<&Pos2> for (f32, f32) { impl From<&Pos2> for (f32, f32) {
#[inline(always)]
fn from(v: &Pos2) -> Self { fn from(v: &Pos2) -> Self {
(v.x, v.y) (v.x, v.y)
} }
@ -95,6 +103,7 @@ impl Pos2 {
/// The vector from origin to this position. /// The vector from origin to this position.
/// `p.to_vec2()` is equivalent to `p - Pos2::default()`. /// `p.to_vec2()` is equivalent to `p - Pos2::default()`.
#[inline(always)]
pub fn to_vec2(self) -> Vec2 { pub fn to_vec2(self) -> Vec2 {
Vec2 { Vec2 {
x: self.x, x: self.x,
@ -153,6 +162,7 @@ impl Pos2 {
impl std::ops::Index<usize> for Pos2 { impl std::ops::Index<usize> for Pos2 {
type Output = f32; type Output = f32;
fn index(&self, index: usize) -> &f32 { fn index(&self, index: usize) -> &f32 {
match index { match index {
0 => &self.x, 0 => &self.x,
@ -175,6 +185,7 @@ impl std::ops::IndexMut<usize> for Pos2 {
impl Eq for Pos2 {} impl Eq for Pos2 {}
impl AddAssign<Vec2> for Pos2 { impl AddAssign<Vec2> for Pos2 {
#[inline(always)]
fn add_assign(&mut self, rhs: Vec2) { fn add_assign(&mut self, rhs: Vec2) {
*self = Pos2 { *self = Pos2 {
x: self.x + rhs.x, x: self.x + rhs.x,
@ -184,6 +195,7 @@ impl AddAssign<Vec2> for Pos2 {
} }
impl SubAssign<Vec2> for Pos2 { impl SubAssign<Vec2> for Pos2 {
#[inline(always)]
fn sub_assign(&mut self, rhs: Vec2) { fn sub_assign(&mut self, rhs: Vec2) {
*self = Pos2 { *self = Pos2 {
x: self.x - rhs.x, x: self.x - rhs.x,
@ -194,6 +206,8 @@ impl SubAssign<Vec2> for Pos2 {
impl Add<Vec2> for Pos2 { impl Add<Vec2> for Pos2 {
type Output = Pos2; type Output = Pos2;
#[inline(always)]
fn add(self, rhs: Vec2) -> Pos2 { fn add(self, rhs: Vec2) -> Pos2 {
Pos2 { Pos2 {
x: self.x + rhs.x, x: self.x + rhs.x,
@ -204,6 +218,8 @@ impl Add<Vec2> for Pos2 {
impl Sub for Pos2 { impl Sub for Pos2 {
type Output = Vec2; type Output = Vec2;
#[inline(always)]
fn sub(self, rhs: Pos2) -> Vec2 { fn sub(self, rhs: Pos2) -> Vec2 {
Vec2 { Vec2 {
x: self.x - rhs.x, x: self.x - rhs.x,
@ -214,6 +230,8 @@ impl Sub for Pos2 {
impl Sub<Vec2> for Pos2 { impl Sub<Vec2> for Pos2 {
type Output = Pos2; type Output = Pos2;
#[inline(always)]
fn sub(self, rhs: Vec2) -> Pos2 { fn sub(self, rhs: Vec2) -> Pos2 {
Pos2 { Pos2 {
x: self.x - rhs.x, x: self.x - rhs.x,

View file

@ -74,6 +74,7 @@ impl Rect {
Self::NAN Self::NAN
} }
#[inline(always)]
pub const fn from_min_max(min: Pos2, max: Pos2) -> Self { pub const fn from_min_max(min: Pos2, max: Pos2) -> Self {
Rect { min, max } Rect { min, max }
} }
@ -196,11 +197,9 @@ impl Rect {
} }
#[must_use] #[must_use]
#[inline(always)]
pub fn contains(&self, p: Pos2) -> bool { pub fn contains(&self, p: Pos2) -> bool {
self.min.x <= p.x self.min.x <= p.x && p.x <= self.max.x && self.min.y <= p.y && p.y <= self.max.y
&& p.x <= self.min.x + self.size().x
&& self.min.y <= p.y
&& p.y <= self.min.y + self.size().y
} }
/// Return the given points clamped to be inside the rectangle /// Return the given points clamped to be inside the rectangle
@ -234,18 +233,25 @@ impl Rect {
} }
} }
#[inline(always)]
pub fn center(&self) -> Pos2 { pub fn center(&self) -> Pos2 {
Pos2 { Pos2 {
x: self.min.x + self.size().x / 2.0, x: (self.min.x + self.max.x) / 2.0,
y: self.min.y + self.size().y / 2.0, y: (self.min.y + self.max.y) / 2.0,
} }
} }
#[inline(always)]
pub fn size(&self) -> Vec2 { pub fn size(&self) -> Vec2 {
self.max - self.min self.max - self.min
} }
#[inline(always)]
pub fn width(&self) -> f32 { pub fn width(&self) -> f32 {
self.max.x - self.min.x self.max.x - self.min.x
} }
#[inline(always)]
pub fn height(&self) -> f32 { pub fn height(&self) -> f32 {
self.max.y - self.min.y self.max.y - self.min.y
} }
@ -292,6 +298,7 @@ impl Rect {
} }
/// `max.x < min.x` or `max.y < min.y`. /// `max.x < min.x` or `max.y < min.y`.
#[inline(always)]
pub fn is_negative(&self) -> bool { pub fn is_negative(&self) -> bool {
self.max.x < self.min.x || self.max.y < self.min.y self.max.x < self.min.x || self.max.y < self.min.y
} }
@ -314,79 +321,106 @@ impl Rect {
/// ## Convenience functions (assumes origin is towards left top): /// ## Convenience functions (assumes origin is towards left top):
impl Rect { impl Rect {
/// `min.x` /// `min.x
#[inline(always)]
pub fn left(&self) -> f32 { pub fn left(&self) -> f32 {
self.min.x self.min.x
} }
/// `min.x` /// `min.x`
#[inline(always)]
pub fn left_mut(&mut self) -> &mut f32 { pub fn left_mut(&mut self) -> &mut f32 {
&mut self.min.x &mut self.min.x
} }
/// `min.x` /// `min.x`
#[inline(always)]
pub fn set_left(&mut self, x: f32) { pub fn set_left(&mut self, x: f32) {
self.min.x = x; self.min.x = x;
} }
/// `max.x` /// `max.x`
#[inline(always)]
pub fn right(&self) -> f32 { pub fn right(&self) -> f32 {
self.max.x self.max.x
} }
/// `max.x` /// `max.x`
#[inline(always)]
pub fn right_mut(&mut self) -> &mut f32 { pub fn right_mut(&mut self) -> &mut f32 {
&mut self.max.x &mut self.max.x
} }
/// `max.x` /// `max.x`
#[inline(always)]
pub fn set_right(&mut self, x: f32) { pub fn set_right(&mut self, x: f32) {
self.max.x = x; self.max.x = x;
} }
/// `min.y` /// `min.y`
#[inline(always)]
pub fn top(&self) -> f32 { pub fn top(&self) -> f32 {
self.min.y self.min.y
} }
/// `min.y` /// `min.y`
#[inline(always)]
pub fn top_mut(&mut self) -> &mut f32 { pub fn top_mut(&mut self) -> &mut f32 {
&mut self.min.y &mut self.min.y
} }
/// `min.y` /// `min.y`
#[inline(always)]
pub fn set_top(&mut self, y: f32) { pub fn set_top(&mut self, y: f32) {
self.min.y = y; self.min.y = y;
} }
/// `max.y` /// `max.y`
#[inline(always)]
pub fn bottom(&self) -> f32 { pub fn bottom(&self) -> f32 {
self.max.y self.max.y
} }
/// `max.y` /// `max.y`
#[inline(always)]
pub fn bottom_mut(&mut self) -> &mut f32 { pub fn bottom_mut(&mut self) -> &mut f32 {
&mut self.max.y &mut self.max.y
} }
/// `max.y` /// `max.y`
#[inline(always)]
pub fn set_bottom(&mut self, y: f32) { pub fn set_bottom(&mut self, y: f32) {
self.max.y = y; self.max.y = y;
} }
#[inline(always)]
pub fn left_top(&self) -> Pos2 { pub fn left_top(&self) -> Pos2 {
pos2(self.left(), self.top()) pos2(self.left(), self.top())
} }
#[inline(always)]
pub fn center_top(&self) -> Pos2 { pub fn center_top(&self) -> Pos2 {
pos2(self.center().x, self.top()) pos2(self.center().x, self.top())
} }
#[inline(always)]
pub fn right_top(&self) -> Pos2 { pub fn right_top(&self) -> Pos2 {
pos2(self.right(), self.top()) pos2(self.right(), self.top())
} }
#[inline(always)]
pub fn left_center(&self) -> Pos2 { pub fn left_center(&self) -> Pos2 {
pos2(self.left(), self.center().y) pos2(self.left(), self.center().y)
} }
#[inline(always)]
pub fn right_center(&self) -> Pos2 { pub fn right_center(&self) -> Pos2 {
pos2(self.right(), self.center().y) pos2(self.right(), self.center().y)
} }
#[inline(always)]
pub fn left_bottom(&self) -> Pos2 { pub fn left_bottom(&self) -> Pos2 {
pos2(self.left(), self.bottom()) pos2(self.left(), self.bottom())
} }
#[inline(always)]
pub fn center_bottom(&self) -> Pos2 { pub fn center_bottom(&self) -> Pos2 {
pos2(self.center().x, self.bottom()) pos2(self.center().x, self.bottom())
} }
#[inline(always)]
pub fn right_bottom(&self) -> Pos2 { pub fn right_bottom(&self) -> Pos2 {
pos2(self.right(), self.bottom()) pos2(self.right(), self.bottom())
} }

View file

@ -23,24 +23,28 @@ pub const fn vec2(x: f32, y: f32) -> Vec2 {
// Compatibility and convenience conversions to and from [f32; 2]: // Compatibility and convenience conversions to and from [f32; 2]:
impl From<[f32; 2]> for Vec2 { impl From<[f32; 2]> for Vec2 {
#[inline(always)]
fn from(v: [f32; 2]) -> Self { fn from(v: [f32; 2]) -> Self {
Self { x: v[0], y: v[1] } Self { x: v[0], y: v[1] }
} }
} }
impl From<&[f32; 2]> for Vec2 { impl From<&[f32; 2]> for Vec2 {
#[inline(always)]
fn from(v: &[f32; 2]) -> Self { fn from(v: &[f32; 2]) -> Self {
Self { x: v[0], y: v[1] } Self { x: v[0], y: v[1] }
} }
} }
impl From<Vec2> for [f32; 2] { impl From<Vec2> for [f32; 2] {
#[inline(always)]
fn from(v: Vec2) -> Self { fn from(v: Vec2) -> Self {
[v.x, v.y] [v.x, v.y]
} }
} }
impl From<&Vec2> for [f32; 2] { impl From<&Vec2> for [f32; 2] {
#[inline(always)]
fn from(v: &Vec2) -> Self { fn from(v: &Vec2) -> Self {
[v.x, v.y] [v.x, v.y]
} }
@ -50,24 +54,28 @@ impl From<&Vec2> for [f32; 2] {
// Compatibility and convenience conversions to and from (f32, f32): // Compatibility and convenience conversions to and from (f32, f32):
impl From<(f32, f32)> for Vec2 { impl From<(f32, f32)> for Vec2 {
#[inline(always)]
fn from(v: (f32, f32)) -> Self { fn from(v: (f32, f32)) -> Self {
Self { x: v.0, y: v.1 } Self { x: v.0, y: v.1 }
} }
} }
impl From<&(f32, f32)> for Vec2 { impl From<&(f32, f32)> for Vec2 {
#[inline(always)]
fn from(v: &(f32, f32)) -> Self { fn from(v: &(f32, f32)) -> Self {
Self { x: v.0, y: v.1 } Self { x: v.0, y: v.1 }
} }
} }
impl From<Vec2> for (f32, f32) { impl From<Vec2> for (f32, f32) {
#[inline(always)]
fn from(v: Vec2) -> Self { fn from(v: Vec2) -> Self {
(v.x, v.y) (v.x, v.y)
} }
} }
impl From<&Vec2> for (f32, f32) { impl From<&Vec2> for (f32, f32) {
#[inline(always)]
fn from(v: &Vec2) -> Self { fn from(v: &Vec2) -> Self {
(v.x, v.y) (v.x, v.y)
} }
@ -92,16 +100,20 @@ impl Vec2 {
Self::INFINITY Self::INFINITY
} }
#[inline(always)]
pub const fn new(x: f32, y: f32) -> Self { pub const fn new(x: f32, y: f32) -> Self {
Self { x, y } Self { x, y }
} }
/// Set both `x` and `y` to the same value. /// Set both `x` and `y` to the same value.
#[inline(always)]
pub const fn splat(v: f32) -> Self { pub const fn splat(v: f32) -> Self {
Self { x: v, y: v } Self { x: v, y: v }
} }
/// Safe normalize: returns zero if input is zero.
#[must_use] #[must_use]
#[inline(always)]
pub fn normalized(self) -> Self { pub fn normalized(self) -> Self {
let len = self.length(); let len = self.length();
if len <= 0.0 { if len <= 0.0 {
@ -118,10 +130,12 @@ impl Vec2 {
vec2(self.y, -self.x) vec2(self.y, -self.x)
} }
#[inline(always)]
pub fn length(self) -> f32 { pub fn length(self) -> f32 {
self.x.hypot(self.y) self.x.hypot(self.y)
} }
#[inline(always)]
pub fn length_sq(self) -> f32 { pub fn length_sq(self) -> f32 {
self.x * self.x + self.y * self.y self.x * self.x + self.y * self.y
} }
@ -129,6 +143,7 @@ impl Vec2 {
/// Create a unit vector with the given angle (in radians). /// Create a unit vector with the given angle (in radians).
/// * An angle of zero gives the unit X axis. /// * An angle of zero gives the unit X axis.
/// * An angle of 𝞃/4 = 90° gives the unit Y axis. /// * An angle of 𝞃/4 = 90° gives the unit Y axis.
#[inline(always)]
pub fn angled(angle: f32) -> Self { pub fn angled(angle: f32) -> Self {
vec2(angle.cos(), angle.sin()) vec2(angle.cos(), angle.sin())
} }
@ -191,6 +206,7 @@ impl Vec2 {
impl std::ops::Index<usize> for Vec2 { impl std::ops::Index<usize> for Vec2 {
type Output = f32; type Output = f32;
fn index(&self, index: usize) -> &f32 { fn index(&self, index: usize) -> &f32 {
match index { match index {
0 => &self.x, 0 => &self.x,
@ -215,12 +231,14 @@ impl Eq for Vec2 {}
impl Neg for Vec2 { impl Neg for Vec2 {
type Output = Vec2; type Output = Vec2;
#[inline(always)]
fn neg(self) -> Vec2 { fn neg(self) -> Vec2 {
vec2(-self.x, -self.y) vec2(-self.x, -self.y)
} }
} }
impl AddAssign for Vec2 { impl AddAssign for Vec2 {
#[inline(always)]
fn add_assign(&mut self, rhs: Vec2) { fn add_assign(&mut self, rhs: Vec2) {
*self = Vec2 { *self = Vec2 {
x: self.x + rhs.x, x: self.x + rhs.x,
@ -230,6 +248,7 @@ impl AddAssign for Vec2 {
} }
impl SubAssign for Vec2 { impl SubAssign for Vec2 {
#[inline(always)]
fn sub_assign(&mut self, rhs: Vec2) { fn sub_assign(&mut self, rhs: Vec2) {
*self = Vec2 { *self = Vec2 {
x: self.x - rhs.x, x: self.x - rhs.x,
@ -240,6 +259,8 @@ impl SubAssign for Vec2 {
impl Add for Vec2 { impl Add for Vec2 {
type Output = Vec2; type Output = Vec2;
#[inline(always)]
fn add(self, rhs: Vec2) -> Vec2 { fn add(self, rhs: Vec2) -> Vec2 {
Vec2 { Vec2 {
x: self.x + rhs.x, x: self.x + rhs.x,
@ -250,6 +271,8 @@ impl Add for Vec2 {
impl Sub for Vec2 { impl Sub for Vec2 {
type Output = Vec2; type Output = Vec2;
#[inline(always)]
fn sub(self, rhs: Vec2) -> Vec2 { fn sub(self, rhs: Vec2) -> Vec2 {
Vec2 { Vec2 {
x: self.x - rhs.x, x: self.x - rhs.x,
@ -261,6 +284,8 @@ impl Sub for Vec2 {
/// Element-wise multiplication /// Element-wise multiplication
impl Mul<Vec2> for Vec2 { impl Mul<Vec2> for Vec2 {
type Output = Vec2; type Output = Vec2;
#[inline(always)]
fn mul(self, vec: Vec2) -> Vec2 { fn mul(self, vec: Vec2) -> Vec2 {
Vec2 { Vec2 {
x: self.x * vec.x, x: self.x * vec.x,
@ -272,6 +297,8 @@ impl Mul<Vec2> for Vec2 {
/// Element-wise division /// Element-wise division
impl Div<Vec2> for Vec2 { impl Div<Vec2> for Vec2 {
type Output = Vec2; type Output = Vec2;
#[inline(always)]
fn div(self, rhs: Vec2) -> Vec2 { fn div(self, rhs: Vec2) -> Vec2 {
Vec2 { Vec2 {
x: self.x / rhs.x, x: self.x / rhs.x,
@ -281,6 +308,7 @@ impl Div<Vec2> for Vec2 {
} }
impl MulAssign<f32> for Vec2 { impl MulAssign<f32> for Vec2 {
#[inline(always)]
fn mul_assign(&mut self, rhs: f32) { fn mul_assign(&mut self, rhs: f32) {
self.x *= rhs; self.x *= rhs;
self.y *= rhs; self.y *= rhs;
@ -289,6 +317,8 @@ impl MulAssign<f32> for Vec2 {
impl Mul<f32> for Vec2 { impl Mul<f32> for Vec2 {
type Output = Vec2; type Output = Vec2;
#[inline(always)]
fn mul(self, factor: f32) -> Vec2 { fn mul(self, factor: f32) -> Vec2 {
Vec2 { Vec2 {
x: self.x * factor, x: self.x * factor,
@ -299,6 +329,8 @@ impl Mul<f32> for Vec2 {
impl Mul<Vec2> for f32 { impl Mul<Vec2> for f32 {
type Output = Vec2; type Output = Vec2;
#[inline(always)]
fn mul(self, vec: Vec2) -> Vec2 { fn mul(self, vec: Vec2) -> Vec2 {
Vec2 { Vec2 {
x: self * vec.x, x: self * vec.x,
@ -309,6 +341,8 @@ impl Mul<Vec2> for f32 {
impl Div<f32> for Vec2 { impl Div<f32> for Vec2 {
type Output = Vec2; type Output = Vec2;
#[inline(always)]
fn div(self, factor: f32) -> Vec2 { fn div(self, factor: f32) -> Vec2 {
Vec2 { Vec2 {
x: self.x / factor, x: self.x / factor,

View file

@ -100,19 +100,27 @@ impl Color32 {
Self([l, l, l, 0]) Self([l, l, l, 0])
} }
#[inline(always)]
pub fn is_opaque(&self) -> bool { pub fn is_opaque(&self) -> bool {
self.a() == 255 self.a() == 255
} }
#[inline(always)]
pub fn r(&self) -> u8 { pub fn r(&self) -> u8 {
self.0[0] self.0[0]
} }
#[inline(always)]
pub fn g(&self) -> u8 { pub fn g(&self) -> u8 {
self.0[1] self.0[1]
} }
#[inline(always)]
pub fn b(&self) -> u8 { pub fn b(&self) -> u8 {
self.0[2] self.0[2]
} }
#[inline(always)]
pub fn a(&self) -> u8 { pub fn a(&self) -> u8 {
self.0[3] self.0[3]
} }
@ -129,11 +137,13 @@ impl Color32 {
} }
/// Premultiplied RGBA /// Premultiplied RGBA
#[inline(always)]
pub fn to_array(&self) -> [u8; 4] { pub fn to_array(&self) -> [u8; 4] {
[self.r(), self.g(), self.b(), self.a()] [self.r(), self.g(), self.b(), self.a()]
} }
/// Premultiplied RGBA /// Premultiplied RGBA
#[inline(always)]
pub fn to_tuple(&self) -> (u8, u8, u8, u8) { pub fn to_tuple(&self) -> (u8, u8, u8, u8) {
(self.r(), self.g(), self.b(), self.a()) (self.r(), self.g(), self.b(), self.a())
} }
@ -212,6 +222,7 @@ impl Rgba {
} }
/// Multiply with e.g. 0.5 to make us half transparent /// Multiply with e.g. 0.5 to make us half transparent
#[inline(always)]
pub fn multiply(self, alpha: f32) -> Self { pub fn multiply(self, alpha: f32) -> Self {
Self([ Self([
alpha * self[0], alpha * self[0],
@ -221,15 +232,22 @@ impl Rgba {
]) ])
} }
#[inline(always)]
pub fn r(&self) -> f32 { pub fn r(&self) -> f32 {
self.0[0] self.0[0]
} }
#[inline(always)]
pub fn g(&self) -> f32 { pub fn g(&self) -> f32 {
self.0[1] self.0[1]
} }
#[inline(always)]
pub fn b(&self) -> f32 { pub fn b(&self) -> f32 {
self.0[2] self.0[2]
} }
#[inline(always)]
pub fn a(&self) -> f32 { pub fn a(&self) -> f32 {
self.0[3] self.0[3]
} }
@ -258,6 +276,8 @@ impl Rgba {
impl std::ops::Add for Rgba { impl std::ops::Add for Rgba {
type Output = Rgba; type Output = Rgba;
#[inline(always)]
fn add(self, rhs: Rgba) -> Rgba { fn add(self, rhs: Rgba) -> Rgba {
Rgba([ Rgba([
self[0] + rhs[0], self[0] + rhs[0],
@ -270,6 +290,8 @@ impl std::ops::Add for Rgba {
impl std::ops::Mul<Rgba> for Rgba { impl std::ops::Mul<Rgba> for Rgba {
type Output = Rgba; type Output = Rgba;
#[inline(always)]
fn mul(self, other: Rgba) -> Rgba { fn mul(self, other: Rgba) -> Rgba {
Rgba([ Rgba([
self[0] * other[0], self[0] * other[0],
@ -282,6 +304,8 @@ impl std::ops::Mul<Rgba> for Rgba {
impl std::ops::Mul<f32> for Rgba { impl std::ops::Mul<f32> for Rgba {
type Output = Rgba; type Output = Rgba;
#[inline(always)]
fn mul(self, factor: f32) -> Rgba { fn mul(self, factor: f32) -> Rgba {
Rgba([ Rgba([
self[0] * factor, self[0] * factor,
@ -294,6 +318,8 @@ impl std::ops::Mul<f32> for Rgba {
impl std::ops::Mul<Rgba> for f32 { impl std::ops::Mul<Rgba> for f32 {
type Output = Rgba; type Output = Rgba;
#[inline(always)]
fn mul(self, rgba: Rgba) -> Rgba { fn mul(self, rgba: Rgba) -> Rgba {
Rgba([ Rgba([
self * rgba[0], self * rgba[0],

View file

@ -92,6 +92,7 @@ impl Mesh {
} }
} }
#[inline(always)]
pub fn colored_vertex(&mut self, pos: Pos2, color: Color32) { pub fn colored_vertex(&mut self, pos: Pos2, color: Color32) {
debug_assert!(self.texture_id == TextureId::Egui); debug_assert!(self.texture_id == TextureId::Egui);
self.vertices.push(Vertex { self.vertices.push(Vertex {
@ -102,6 +103,7 @@ impl Mesh {
} }
/// Add a triangle. /// Add a triangle.
#[inline(always)]
pub fn add_triangle(&mut self, a: u32, b: u32, c: u32) { pub fn add_triangle(&mut self, a: u32, b: u32, c: u32) {
self.indices.push(a); self.indices.push(a);
self.indices.push(b); self.indices.push(b);
@ -110,12 +112,14 @@ impl Mesh {
/// Make room for this many additional triangles (will reserve 3x as many indices). /// Make room for this many additional triangles (will reserve 3x as many indices).
/// See also `reserve_vertices`. /// See also `reserve_vertices`.
#[inline(always)]
pub fn reserve_triangles(&mut self, additional_triangles: usize) { pub fn reserve_triangles(&mut self, additional_triangles: usize) {
self.indices.reserve(3 * additional_triangles); self.indices.reserve(3 * additional_triangles);
} }
/// Make room for this many additional vertices. /// Make room for this many additional vertices.
/// See also `reserve_triangles`. /// See also `reserve_triangles`.
#[inline(always)]
pub fn reserve_vertices(&mut self, additional: usize) { pub fn reserve_vertices(&mut self, additional: usize) {
self.vertices.reserve(additional); self.vertices.reserve(additional);
} }
@ -151,6 +155,7 @@ impl Mesh {
} }
/// Uniformly colored rectangle. /// Uniformly colored rectangle.
#[inline(always)]
pub fn add_colored_rect(&mut self, rect: Rect, color: Color32) { pub fn add_colored_rect(&mut self, rect: Rect, color: Color32) {
debug_assert!(self.texture_id == TextureId::Egui); debug_assert!(self.texture_id == TextureId::Egui);
self.add_rect_with_uv(rect, [WHITE_UV, WHITE_UV].into(), color) self.add_rect_with_uv(rect, [WHITE_UV, WHITE_UV].into(), color)

View file

@ -34,10 +34,12 @@ pub struct PathPoint {
struct Path(Vec<PathPoint>); struct Path(Vec<PathPoint>);
impl Path { impl Path {
#[inline(always)]
pub fn clear(&mut self) { pub fn clear(&mut self) {
self.0.clear(); self.0.clear();
} }
#[inline(always)]
pub fn reserve(&mut self, additional: usize) { pub fn reserve(&mut self, additional: usize) {
self.0.reserve(additional) self.0.reserve(additional)
} }

View file

@ -138,10 +138,12 @@ impl FontImpl {
} }
/// Height of one row of text. In points /// Height of one row of text. In points
#[inline(always)]
pub fn row_height(&self) -> f32 { pub fn row_height(&self) -> f32 {
self.height_in_points self.height_in_points
} }
#[inline(always)]
pub fn pixels_per_point(&self) -> f32 { pub fn pixels_per_point(&self) -> f32 {
self.pixels_per_point self.pixels_per_point
} }
@ -202,11 +204,13 @@ impl Font {
slf slf
} }
#[inline]
pub fn round_to_pixel(&self, point: f32) -> f32 { pub fn round_to_pixel(&self, point: f32) -> f32 {
(point * self.pixels_per_point).round() / self.pixels_per_point (point * self.pixels_per_point).round() / self.pixels_per_point
} }
/// Height of one row of text. In points /// Height of one row of text. In points
#[inline(always)]
pub fn row_height(&self) -> f32 { pub fn row_height(&self) -> f32 {
self.row_height self.row_height
} }