Skip to content

Commit

Permalink
Speed up improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed Aug 2, 2024
1 parent 518b828 commit 765e065
Show file tree
Hide file tree
Showing 21 changed files with 537 additions and 186 deletions.
6 changes: 3 additions & 3 deletions src/app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ fn main() {
{
let mut lab_store: Vec<f32> = vec![];
let store_stride = width as usize * components * std::mem::size_of::<f32>();
lab_store.resize(width as usize * components * height as usize, 0f32);
lab_store.resize(width as usize * components * height as usize, 0.);
let src_stride = width * components as u32;
let start_time = Instant::now();
rgb_to_oklab(
rgb_to_linear(
src_bytes,
src_stride,
&mut lab_store,
Expand Down Expand Up @@ -101,7 +101,7 @@ fn main() {
// }

let start_time = Instant::now();
oklab_to_rgb(
linear_to_rgb(
&lab_store,
store_stride as u32,
&mut dst_slice,
Expand Down
12 changes: 12 additions & 0 deletions src/gamma_curves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ pub enum TransferFunction {
Gamma2p8,
}

impl From<u8> for TransferFunction {
fn from(value: u8) -> Self {
match value {
0 => TransferFunction::Srgb,
1 => TransferFunction::Rec709,
2 => TransferFunction::Gamma2p2,
3 => TransferFunction::Gamma2p8,
_ => TransferFunction::Srgb,
}
}
}

impl TransferFunction {
#[inline]
pub fn get_linearize_function(&self) -> fn(f32) -> f32 {
Expand Down
31 changes: 30 additions & 1 deletion src/image_to_linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,36 @@ fn channels_to_linear<const CHANNELS_CONFIGURATION: u8, const USE_ALPHA: bool>(
target_feature = "neon"
))]
{
_wide_row_handle = Some(neon_channels_to_linear::<CHANNELS_CONFIGURATION, USE_ALPHA>);
_wide_row_handle = match transfer_function {
TransferFunction::Srgb => Some(
neon_channels_to_linear::<
CHANNELS_CONFIGURATION,
USE_ALPHA,
{ TransferFunction::Srgb as u8 },
>,
),
TransferFunction::Rec709 => Some(
neon_channels_to_linear::<
CHANNELS_CONFIGURATION,
USE_ALPHA,
{ TransferFunction::Rec709 as u8 },
>,
),
TransferFunction::Gamma2p2 => Some(
neon_channels_to_linear::<
CHANNELS_CONFIGURATION,
USE_ALPHA,
{ TransferFunction::Gamma2p2 as u8 },
>,
),
TransferFunction::Gamma2p8 => Some(
neon_channels_to_linear::<
CHANNELS_CONFIGURATION,
USE_ALPHA,
{ TransferFunction::Gamma2p8 as u8 },
>,
),
};
}

for _ in 0..height as usize {
Expand Down
37 changes: 34 additions & 3 deletions src/image_to_linear_u8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,40 @@ fn channels_to_linear<const CHANNELS_CONFIGURATION: u8, const USE_ALPHA: bool>(
target_feature = "neon"
))]
{
_wide_row_handler = Some(
neon_image_linear_to_u8::neon_channels_to_linear_u8::<CHANNELS_CONFIGURATION, USE_ALPHA>,
);
_wide_row_handler = match transfer_function {
TransferFunction::Srgb => Some(
neon_channels_to_linear_u8::<
CHANNELS_CONFIGURATION,
USE_ALPHA,
{ TransferFunction::Srgb as u8 },
true,
>,
),
TransferFunction::Rec709 => Some(
neon_channels_to_linear_u8::<
CHANNELS_CONFIGURATION,
USE_ALPHA,
{ TransferFunction::Rec709 as u8 },
true,
>,
),
TransferFunction::Gamma2p2 => Some(
neon_channels_to_linear_u8::<
CHANNELS_CONFIGURATION,
USE_ALPHA,
{ TransferFunction::Gamma2p2 as u8 },
true,
>,
),
TransferFunction::Gamma2p8 => Some(
neon_channels_to_linear_u8::<
CHANNELS_CONFIGURATION,
USE_ALPHA,
{ TransferFunction::Gamma2p8 as u8 },
true,
>,
),
};
}

#[cfg(all(
Expand Down
31 changes: 30 additions & 1 deletion src/image_to_oklab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,36 @@ fn channels_to_oklab<const CHANNELS_CONFIGURATION: u8, const TARGET: u8>(
target_feature = "neon"
))]
{
_wide_row_handle = Some(neon_image_to_oklab::<CHANNELS_CONFIGURATION, TARGET>);
_wide_row_handle = match transfer_function {
TransferFunction::Srgb => Some(
neon_image_to_oklab::<
CHANNELS_CONFIGURATION,
TARGET,
{ TransferFunction::Srgb as u8 },
>,
),
TransferFunction::Rec709 => Some(
neon_image_to_oklab::<
CHANNELS_CONFIGURATION,
TARGET,
{ TransferFunction::Rec709 as u8 },
>,
),
TransferFunction::Gamma2p2 => Some(
neon_image_to_oklab::<
CHANNELS_CONFIGURATION,
TARGET,
{ TransferFunction::Gamma2p2 as u8 },
>,
),
TransferFunction::Gamma2p8 => Some(
neon_image_to_oklab::<
CHANNELS_CONFIGURATION,
TARGET,
{ TransferFunction::Gamma2p8 as u8 },
>,
),
};
}

#[cfg(all(
Expand Down
36 changes: 34 additions & 2 deletions src/image_to_xyz_lab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,40 @@ fn channels_to_xyz<const CHANNELS_CONFIGURATION: u8, const USE_ALPHA: bool, cons
target_feature = "neon"
))]
{
_wide_row_handler =
Some(neon_channels_to_xyz_or_lab::<CHANNELS_CONFIGURATION, USE_ALPHA, TARGET>);
_wide_row_handler = match transfer_function {
TransferFunction::Srgb => Some(
neon_channels_to_xyz_or_lab::<
CHANNELS_CONFIGURATION,
USE_ALPHA,
TARGET,
{ TransferFunction::Srgb as u8 },
>,
),
TransferFunction::Rec709 => Some(
neon_channels_to_xyz_or_lab::<
CHANNELS_CONFIGURATION,
USE_ALPHA,
TARGET,
{ TransferFunction::Rec709 as u8 },
>,
),
TransferFunction::Gamma2p2 => Some(
neon_channels_to_xyz_or_lab::<
CHANNELS_CONFIGURATION,
USE_ALPHA,
TARGET,
{ TransferFunction::Gamma2p2 as u8 },
>,
),
TransferFunction::Gamma2p8 => Some(
neon_channels_to_xyz_or_lab::<
CHANNELS_CONFIGURATION,
USE_ALPHA,
TARGET,
{ TransferFunction::Gamma2p8 as u8 },
>,
),
};
}

#[cfg(all(
Expand Down
31 changes: 30 additions & 1 deletion src/image_xyza_laba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,36 @@ fn channels_to_xyz_with_alpha<const CHANNELS_CONFIGURATION: u8, const TARGET: u8
target_feature = "neon"
))]
{
_wide_row_handler = Some(neon_channels_to_xyza_or_laba::<CHANNELS_CONFIGURATION, TARGET>);
_wide_row_handler = match transfer_function {
TransferFunction::Srgb => Some(
neon_channels_to_xyza_or_laba::<
CHANNELS_CONFIGURATION,
TARGET,
{ TransferFunction::Srgb as u8 },
>,
),
TransferFunction::Rec709 => Some(
neon_channels_to_xyza_or_laba::<
CHANNELS_CONFIGURATION,
TARGET,
{ TransferFunction::Rec709 as u8 },
>,
),
TransferFunction::Gamma2p2 => Some(
neon_channels_to_xyza_or_laba::<
CHANNELS_CONFIGURATION,
TARGET,
{ TransferFunction::Gamma2p2 as u8 },
>,
),
TransferFunction::Gamma2p8 => Some(
neon_channels_to_xyza_or_laba::<
CHANNELS_CONFIGURATION,
TARGET,
{ TransferFunction::Gamma2p8 as u8 },
>,
),
};
}

let channels = image_configuration.get_channels_count();
Expand Down
31 changes: 30 additions & 1 deletion src/linear_to_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,36 @@ fn linear_to_gamma_channels<const CHANNELS_CONFIGURATION: u8, const USE_ALPHA: b
target_feature = "neon"
))]
{
_wide_row_handle = Some(neon_linear_to_gamma::<CHANNELS_CONFIGURATION, USE_ALPHA>);
_wide_row_handle = match transfer_function {
TransferFunction::Srgb => Some(
neon_linear_to_gamma::<
CHANNELS_CONFIGURATION,
USE_ALPHA,
{ TransferFunction::Srgb as u8 },
>,
),
TransferFunction::Rec709 => Some(
neon_linear_to_gamma::<
CHANNELS_CONFIGURATION,
USE_ALPHA,
{ TransferFunction::Rec709 as u8 },
>,
),
TransferFunction::Gamma2p2 => Some(
neon_linear_to_gamma::<
CHANNELS_CONFIGURATION,
USE_ALPHA,
{ TransferFunction::Gamma2p2 as u8 },
>,
),
TransferFunction::Gamma2p8 => Some(
neon_linear_to_gamma::<
CHANNELS_CONFIGURATION,
USE_ALPHA,
{ TransferFunction::Gamma2p8 as u8 },
>,
),
};
}

let mut src_offset = 0usize;
Expand Down
37 changes: 34 additions & 3 deletions src/linear_to_image_u8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,40 @@ fn linear_to_gamma_channels<const CHANNELS_CONFIGURATION: u8, const USE_ALPHA: b
target_feature = "neon"
))]
{
_wide_row_handler = Some(
neon_image_linear_to_u8::neon_channels_to_linear_u8::<CHANNELS_CONFIGURATION, USE_ALPHA>,
);
_wide_row_handler = match transfer_function {
TransferFunction::Srgb => Some(
neon_channels_to_linear_u8::<
CHANNELS_CONFIGURATION,
USE_ALPHA,
{ TransferFunction::Srgb as u8 },
false,
>,
),
TransferFunction::Rec709 => Some(
neon_channels_to_linear_u8::<
CHANNELS_CONFIGURATION,
USE_ALPHA,
{ TransferFunction::Rec709 as u8 },
false,
>,
),
TransferFunction::Gamma2p2 => Some(
neon_channels_to_linear_u8::<
CHANNELS_CONFIGURATION,
USE_ALPHA,
{ TransferFunction::Gamma2p2 as u8 },
false,
>,
),
TransferFunction::Gamma2p8 => Some(
neon_channels_to_linear_u8::<
CHANNELS_CONFIGURATION,
USE_ALPHA,
{ TransferFunction::Gamma2p8 as u8 },
false,
>,
),
};
}

for _ in 0..height as usize {
Expand Down
10 changes: 8 additions & 2 deletions src/neon/image_to_oklab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,26 @@ macro_rules! triple_to_oklab {
}

#[inline(always)]
pub unsafe fn neon_image_to_oklab<const CHANNELS_CONFIGURATION: u8, const TARGET: u8>(
pub unsafe fn neon_image_to_oklab<
const CHANNELS_CONFIGURATION: u8,
const TARGET: u8,
const TRANSFER_FUNCTION: u8,
>(
start_cx: usize,
src: *const u8,
src_offset: usize,
width: u32,
dst: *mut f32,
dst_offset: usize,
transfer_function: TransferFunction,
_: TransferFunction,
) -> usize {
let target: OklabTarget = TARGET.into();
let image_configuration: ImageConfiguration = CHANNELS_CONFIGURATION.into();
let channels = image_configuration.get_channels_count();
let mut cx = start_cx;

let transfer_function: TransferFunction = TRANSFER_FUNCTION.into();

let transfer = get_neon_linear_transfer(transfer_function);

let dst_ptr = (dst as *mut u8).add(dst_offset) as *mut f32;
Expand Down
Loading

0 comments on commit 765e065

Please sign in to comment.