1use alloc::boxed::Box;
12use core::fmt;
13use once_cell::race::OnceBox;
14use zenpixels::SampleLayout;
15
16use crate::policy::DepthPolicy;
17use crate::strip::{ByteFastPath, StripBuf};
18
19pub(crate) type ByteLayoutDispatchFn = fn(ByteFastPath, &[u8], &mut [u8], usize) -> bool;
21pub(crate) type Rgba8PremulFn = fn(&[u8], &mut [u8], usize, bool);
22pub(crate) type StripUnpackFn = fn(SampleLayout, &[u8], &mut StripBuf, usize) -> bool;
23pub(crate) type StripPackFn = fn(SampleLayout, &StripBuf, &mut [u8], usize, DepthPolicy) -> bool;
24pub(crate) type F32ToU8SliceFn = fn(&[f32], &mut [u8], bool);
25pub(crate) type U8ToF32SliceFn = fn(&[u8], &mut [f32]);
26pub(crate) type F32ToU16SliceFn = fn(&[f32], &mut [u16], bool);
27pub(crate) type U16ToF32SliceFn = fn(&[u16], &mut [f32]);
28pub(crate) type LinF32ToSrgbU8Fn = fn(&[f32], &mut [u8], bool, bool);
29pub(crate) type Srgb8MatrixFn = fn(
30 &[u8],
31 &mut [u8],
32 usize,
33 &[[f32; 3]; 3],
34 zenpixels::ChannelOrder,
35 zenpixels::ChannelOrder,
36 bool,
37 bool,
38);
39pub(crate) type Srgb8MatrixQ14Fn = fn(
40 &[u8],
41 &mut [u8],
42 usize,
43 &crate::strip_simd::Q14MatrixTables,
44 zenpixels::ChannelOrder,
45 zenpixels::ChannelOrder,
46 bool,
47 bool,
48);
49pub(crate) type TransferSliceFn = fn(&mut [f32]);
50pub(crate) type GammaPowSliceFn = fn(&mut [f32], f32, f64);
51#[cfg(feature = "hdr-transfers")]
52pub(crate) type ScaledTransferSliceFn = fn(&mut [f32], f32);
53pub(crate) type GamutSoaFn = fn(&[[f32; 3]; 3], &mut [f32], &mut [f32], &mut [f32]);
54pub(crate) type F16DecodeFn = fn(&[u16], &mut [f32]);
55pub(crate) type F16EncodeFn = fn(&[f32], &mut [u16]);
56#[cfg(feature = "oklab")]
57pub(crate) type OklabSoaFn = fn(&[[f32; 3]; 3], &mut [f32], &mut [f32], &mut [f32]);
58#[cfg(feature = "oklab")]
59pub(crate) type SoftCompressFn = fn(&crate::tonemap::SoftCompress, &mut [[f32; 3]]);
60#[cfg(feature = "hdr-transfers")]
61pub(crate) type HlgDisplaySoaFn =
62 fn(&crate::hlg_render::PreparedHlg, &mut [f32], &mut [f32], &mut [f32], usize);
63#[cfg(feature = "gainmap")]
64pub(crate) type GainMapBlend2Fn = fn(&[f32], &[f32], f32, f32, &mut [f32]);
65#[cfg(feature = "gainmap")]
66pub(crate) type GainMapBlend4Fn = fn(&[f32], &[f32], &[f32], &[f32], [f32; 4], &mut [f32]);
67#[cfg(feature = "gainmap")]
68pub(crate) type GainMapDecodeFn = fn(&[f32], f32, f32, &mut [f32]);
69#[cfg(feature = "gainmap")]
70pub(crate) type GainMapCombineFn =
71 fn(&[f32], &[f32], &[f32], &[f32], [f32; 3], [f32; 3], bool, &mut [f32]);
72
73#[derive(Clone, Copy)]
93pub struct Accelerators {
94 pub(crate) byte_layout_exec: Option<ByteLayoutDispatchFn>,
96 pub(crate) rgba8_straight_to_premul: Option<Rgba8PremulFn>,
98 pub(crate) strip_unpack: Option<StripUnpackFn>,
100 pub(crate) strip_pack: Option<StripPackFn>,
102 pub(crate) f32_to_u8: Option<F32ToU8SliceFn>,
104 pub(crate) u8_to_f32: Option<U8ToF32SliceFn>,
106 pub(crate) f32_to_u16: Option<F32ToU16SliceFn>,
108 pub(crate) u16_to_f32: Option<U16ToF32SliceFn>,
110 pub(crate) srgb_eotf: Option<TransferSliceFn>,
112 pub(crate) srgb_oetf: Option<TransferSliceFn>,
114 pub(crate) srgb_u8_to_lin: Option<U8ToF32SliceFn>,
116 pub(crate) lin_f32_to_srgb_u8: Option<LinF32ToSrgbU8Fn>,
118 pub(crate) srgb8_matrix_exact: Option<Srgb8MatrixFn>,
120 pub(crate) srgb8_matrix_approx: Option<Srgb8MatrixQ14Fn>,
122 pub(crate) bt709_eotf: Option<TransferSliceFn>,
124 pub(crate) bt709_oetf: Option<TransferSliceFn>,
126 pub(crate) gamma22_eotf: Option<TransferSliceFn>,
128 pub(crate) gamma22_oetf: Option<TransferSliceFn>,
130 pub(crate) gamma_pow: Option<GammaPowSliceFn>,
133 pub(crate) gamut_3x3: Option<GamutSoaFn>,
135 pub(crate) f16_decode: Option<F16DecodeFn>,
137 pub(crate) f16_encode: Option<F16EncodeFn>,
139 #[cfg(feature = "oklab")]
141 pub(crate) rgb_to_oklab: Option<OklabSoaFn>,
142 #[cfg(feature = "oklab")]
144 pub(crate) oklab_to_rgb: Option<OklabSoaFn>,
145 #[cfg(feature = "oklab")]
147 pub(crate) soft_compress: Option<SoftCompressFn>,
148 #[cfg(feature = "hdr-transfers")]
150 pub(crate) pq_eotf: Option<ScaledTransferSliceFn>,
151 #[cfg(feature = "hdr-transfers")]
153 pub(crate) pq_oetf: Option<ScaledTransferSliceFn>,
154 #[cfg(feature = "hdr-transfers")]
156 pub(crate) hlg_scene_eotf: Option<TransferSliceFn>,
157 #[cfg(feature = "hdr-transfers")]
159 pub(crate) hlg_scene_oetf: Option<TransferSliceFn>,
160 #[cfg(feature = "hdr-transfers")]
162 pub(crate) hlg_display_soa: Option<HlgDisplaySoaFn>,
163 #[cfg(feature = "gainmap")]
165 pub(crate) gainmap_blend_2: Option<GainMapBlend2Fn>,
166 #[cfg(feature = "gainmap")]
168 pub(crate) gainmap_blend_4: Option<GainMapBlend4Fn>,
169 #[cfg(feature = "gainmap")]
171 pub(crate) gainmap_decode: Option<GainMapDecodeFn>,
172 #[cfg(feature = "gainmap")]
174 pub(crate) gainmap_combine: Option<GainMapCombineFn>,
175}
176
177impl fmt::Debug for Accelerators {
178 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
179 let mut ds = f.debug_struct("Accelerators");
180 ds.field("byte_layout", &self.byte_layout_exec.is_some())
181 .field("premul_u8", &self.rgba8_straight_to_premul.is_some())
182 .field("strip_simd", &self.strip_unpack.is_some())
183 .field("srgb_eotf", &self.srgb_eotf.is_some())
184 .field("srgb_oetf", &self.srgb_oetf.is_some())
185 .field("lin_f32_to_srgb_u8", &self.lin_f32_to_srgb_u8.is_some())
186 .field("srgb8_matrix_exact", &self.srgb8_matrix_exact.is_some())
187 .field("srgb8_matrix_approx", &self.srgb8_matrix_approx.is_some())
188 .field("bt709", &self.bt709_eotf.is_some())
189 .field("gamma22", &self.gamma22_eotf.is_some())
190 .field("gamut_3x3", &self.gamut_3x3.is_some())
191 .field("f16_decode", &self.f16_decode.is_some())
192 .field("f16_encode", &self.f16_encode.is_some());
193 #[cfg(feature = "oklab")]
194 ds.field("oklab", &self.rgb_to_oklab.is_some())
195 .field("soft_compress", &self.soft_compress.is_some());
196 #[cfg(feature = "hdr-transfers")]
197 ds.field("hdr_simd", &self.pq_eotf.is_some());
198 #[cfg(feature = "gainmap")]
199 ds.field("gainmap_simd", &self.gainmap_combine.is_some());
200 ds.finish()
201 }
202}
203
204impl PartialEq for Accelerators {
205 fn eq(&self, other: &Self) -> bool {
206 let base = self.byte_layout_exec.is_some() == other.byte_layout_exec.is_some()
207 && self.rgba8_straight_to_premul.is_some() == other.rgba8_straight_to_premul.is_some()
208 && self.strip_unpack.is_some() == other.strip_unpack.is_some()
209 && self.strip_pack.is_some() == other.strip_pack.is_some()
210 && self.f32_to_u8.is_some() == other.f32_to_u8.is_some()
211 && self.u8_to_f32.is_some() == other.u8_to_f32.is_some()
212 && self.f32_to_u16.is_some() == other.f32_to_u16.is_some()
213 && self.u16_to_f32.is_some() == other.u16_to_f32.is_some()
214 && self.srgb_eotf.is_some() == other.srgb_eotf.is_some()
215 && self.srgb_oetf.is_some() == other.srgb_oetf.is_some()
216 && self.srgb_u8_to_lin.is_some() == other.srgb_u8_to_lin.is_some()
217 && self.lin_f32_to_srgb_u8.is_some() == other.lin_f32_to_srgb_u8.is_some()
218 && self.srgb8_matrix_exact.is_some() == other.srgb8_matrix_exact.is_some()
219 && self.srgb8_matrix_approx.is_some() == other.srgb8_matrix_approx.is_some()
220 && self.bt709_eotf.is_some() == other.bt709_eotf.is_some()
221 && self.bt709_oetf.is_some() == other.bt709_oetf.is_some()
222 && self.gamma22_eotf.is_some() == other.gamma22_eotf.is_some()
223 && self.gamma22_oetf.is_some() == other.gamma22_oetf.is_some()
224 && self.gamma_pow.is_some() == other.gamma_pow.is_some()
225 && self.gamut_3x3.is_some() == other.gamut_3x3.is_some()
226 && self.f16_decode.is_some() == other.f16_decode.is_some()
227 && self.f16_encode.is_some() == other.f16_encode.is_some();
228 #[cfg(feature = "oklab")]
229 let base = base
230 && self.rgb_to_oklab.is_some() == other.rgb_to_oklab.is_some()
231 && self.oklab_to_rgb.is_some() == other.oklab_to_rgb.is_some()
232 && self.soft_compress.is_some() == other.soft_compress.is_some();
233 #[cfg(feature = "hdr-transfers")]
234 let base = base
235 && self.pq_eotf.is_some() == other.pq_eotf.is_some()
236 && self.pq_oetf.is_some() == other.pq_oetf.is_some()
237 && self.hlg_scene_eotf.is_some() == other.hlg_scene_eotf.is_some()
238 && self.hlg_scene_oetf.is_some() == other.hlg_scene_oetf.is_some()
239 && self.hlg_display_soa.is_some() == other.hlg_display_soa.is_some();
240 #[cfg(feature = "gainmap")]
241 let base = base
242 && self.gainmap_blend_2.is_some() == other.gainmap_blend_2.is_some()
243 && self.gainmap_blend_4.is_some() == other.gainmap_blend_4.is_some()
244 && self.gainmap_decode.is_some() == other.gainmap_decode.is_some()
245 && self.gainmap_combine.is_some() == other.gainmap_combine.is_some();
246 base
247 }
248}
249
250impl Eq for Accelerators {}
251
252impl Default for Accelerators {
253 fn default() -> Self {
254 #[cfg(feature = "fast")]
255 {
256 Self::ROW_PIPELINE
257 }
258 #[cfg(not(feature = "fast"))]
259 {
260 Self::SCALAR
261 }
262 }
263}
264
265impl Accelerators {
266 pub fn install_global(self) -> bool {
270 GLOBAL_ACCEL.set(Box::new(self)).is_ok()
271 }
272
273 pub const SCALAR: Self = Self {
275 byte_layout_exec: None,
276 rgba8_straight_to_premul: None,
277 strip_unpack: None,
278 strip_pack: None,
279 f32_to_u8: None,
280 u8_to_f32: None,
281 f32_to_u16: None,
282 u16_to_f32: None,
283 srgb_eotf: None,
284 srgb_oetf: None,
285 srgb_u8_to_lin: None,
286 lin_f32_to_srgb_u8: None,
287 srgb8_matrix_exact: None,
288 srgb8_matrix_approx: None,
289 bt709_eotf: None,
290 bt709_oetf: None,
291 gamma22_eotf: None,
292 gamma22_oetf: None,
293 gamma_pow: None,
294 gamut_3x3: None,
295 f16_decode: None,
296 f16_encode: None,
297 #[cfg(feature = "oklab")]
298 rgb_to_oklab: None,
299 #[cfg(feature = "oklab")]
300 oklab_to_rgb: None,
301 #[cfg(feature = "oklab")]
302 soft_compress: None,
303 #[cfg(feature = "hdr-transfers")]
304 pq_eotf: None,
305 #[cfg(feature = "hdr-transfers")]
306 pq_oetf: None,
307 #[cfg(feature = "hdr-transfers")]
308 hlg_scene_eotf: None,
309 #[cfg(feature = "hdr-transfers")]
310 hlg_scene_oetf: None,
311 #[cfg(feature = "hdr-transfers")]
312 hlg_display_soa: None,
313 #[cfg(feature = "gainmap")]
314 gainmap_blend_2: None,
315 #[cfg(feature = "gainmap")]
316 gainmap_blend_4: None,
317 #[cfg(feature = "gainmap")]
318 gainmap_decode: None,
319 #[cfg(feature = "gainmap")]
320 gainmap_combine: None,
321 };
322
323 pub const LAYOUT: Self = Self::SCALAR.with_byte_layout();
325
326 pub const WEB_SDR: Self = Self::SCALAR
330 .with_byte_layout()
331 .with_strip_simd()
332 .with_srgb()
333 .with_gamut_3x3()
334 .with_srgb8_matrix_internal();
335
336 pub const HDR: Self = Self::SCALAR
341 .with_strip_simd()
342 .with_gamut_3x3()
343 .with_f16()
344 .with_hdr_simd();
345
346 pub const WEB_HDR: Self = {
354 let base = Self::WEB_SDR.merge_prefer_self(Self::HDR);
355 #[cfg(feature = "gainmap")]
356 {
357 base.with_gainmap()
358 }
359 #[cfg(not(feature = "gainmap"))]
360 {
361 base
362 }
363 };
364
365 #[cfg(feature = "gainmap")]
371 pub const GAINMAP: Self = Self::SCALAR.with_gainmap();
372
373 #[cfg(feature = "oklab")]
375 pub const OKLAB: Self = Self::SCALAR.with_strip_simd().with_oklab();
376
377 pub const ROW_PIPELINE: Self = {
381 let base = Self::WEB_SDR
382 .merge_prefer_self(Self::HDR)
383 .with_bt709()
384 .with_gamma22();
385 #[cfg(feature = "oklab")]
386 {
387 base.with_oklab()
388 }
389 #[cfg(not(feature = "oklab"))]
390 {
391 base
392 }
393 };
394
395 pub const ALL: Self = {
401 #[cfg(feature = "gainmap")]
402 {
403 Self::ROW_PIPELINE.with_gainmap()
404 }
405 #[cfg(not(feature = "gainmap"))]
406 {
407 Self::ROW_PIPELINE
408 }
409 };
410
411 #[inline]
413 #[must_use]
414 pub const fn has_byte_layout(&self) -> bool {
415 self.byte_layout_exec.is_some()
416 }
417
418 #[inline]
420 #[must_use]
421 pub const fn has_strip_simd(&self) -> bool {
422 self.strip_unpack.is_some()
423 }
424
425 #[inline]
427 #[must_use]
428 pub const fn with_byte_layout(mut self) -> Self {
429 self.byte_layout_exec = Some(crate::strip::exec_garb_byte_fast_path);
430 self.rgba8_straight_to_premul = Some(crate::strip_simd::simd_rgba8_straight_to_premul);
431 self
432 }
433
434 #[inline]
436 #[must_use]
437 pub const fn with_strip_simd(mut self) -> Self {
438 self.strip_unpack = Some(crate::strip_simd::simd_unpack_strip);
439 self.strip_pack = Some(crate::strip_simd::simd_pack_strip);
440 self.f32_to_u8 = Some(crate::strip_simd::simd_f32_to_u8_slice);
441 self.u8_to_f32 = Some(crate::strip_simd::simd_u8_to_f32_slice);
442 self.f32_to_u16 = Some(crate::strip_simd::simd_f32_to_u16_slice);
443 self.u16_to_f32 = Some(crate::strip_simd::simd_u16_to_f32_slice);
444 self
445 }
446
447 #[inline]
449 #[must_use]
450 pub const fn with_srgb(mut self) -> Self {
451 self.srgb_eotf = Some(simd_srgb_eotf_slice);
452 self.srgb_oetf = Some(simd_srgb_oetf_slice);
453 self.srgb_u8_to_lin = Some(simd_srgb_u8_to_lin_slice);
454 self.lin_f32_to_srgb_u8 = Some(crate::strip_simd::simd_lin_f32_to_srgb_u8);
455 self
456 }
457
458 #[inline]
460 #[must_use]
461 pub const fn with_bt709(mut self) -> Self {
462 self.bt709_eotf = Some(crate::transfers::bt709_eotf_simd_slice);
463 self.bt709_oetf = Some(crate::transfers::bt709_oetf_simd_slice);
464 self
465 }
466
467 #[inline]
469 #[must_use]
470 pub const fn with_gamma22(mut self) -> Self {
471 self.gamma22_eotf = Some(crate::transfers::gamma22_eotf_simd_slice);
472 self.gamma22_oetf = Some(crate::transfers::gamma22_oetf_simd_slice);
473 self.gamma_pow = Some(crate::transfers::gamma_pow_simd_slice);
474 self
475 }
476
477 #[inline]
479 #[must_use]
480 pub const fn with_gamut_3x3(mut self) -> Self {
481 self.gamut_3x3 = Some(simd_gamut_3x3_soa);
482 self
483 }
484
485 #[inline]
486 #[must_use]
487 const fn with_srgb8_matrix_internal(mut self) -> Self {
488 self.srgb8_matrix_exact = Some(crate::strip_simd::simd_srgb8_matrix_exact);
489 self.srgb8_matrix_approx = Some(crate::strip_simd::simd_srgb8_matrix_approx_q14);
490 self
491 }
492
493 #[cfg(feature = "gainmap")]
500 #[inline]
501 #[must_use]
502 pub const fn with_gainmap(mut self) -> Self {
503 self.gainmap_blend_2 = Some(crate::gainmap_simd::simd_blend_rows_2);
504 self.gainmap_blend_4 = Some(crate::gainmap_simd::simd_blend_rows_4);
505 self.gainmap_decode = Some(crate::gainmap_simd::simd_decode_gain_1ch);
506 self.gainmap_combine = Some(crate::gainmap_simd::simd_combine_linear_f32);
507 self.gamma_pow = Some(crate::transfers::gamma_pow_simd_slice);
508 self
509 }
510
511 #[inline]
513 #[must_use]
514 pub const fn with_f16(mut self) -> Self {
515 self.f16_decode = Some(simd_f16_to_f32_slice);
516 self.f16_encode = Some(simd_f32_to_f16_slice);
517 self
518 }
519
520 #[cfg(feature = "oklab")]
522 #[inline]
523 #[must_use]
524 pub const fn with_oklab(mut self) -> Self {
525 self.rgb_to_oklab = Some(simd_rgb_to_oklab_soa);
526 self.oklab_to_rgb = Some(simd_oklab_to_rgb_soa);
527 self.soft_compress = Some(simd_soft_compress_strip);
528 self
529 }
530
531 #[inline]
533 #[must_use]
534 #[allow(unused_mut)]
535 pub const fn with_hdr_simd(mut self) -> Self {
536 #[cfg(feature = "hdr-transfers")]
537 {
538 self.pq_eotf = Some(crate::transfers::hdr::pq_eotf_simd_slice);
539 self.pq_oetf = Some(crate::transfers::hdr::pq_oetf_simd_slice);
540 self.hlg_scene_eotf = Some(crate::transfers::hdr::hlg_scene_eotf_simd_slice);
541 self.hlg_scene_oetf = Some(crate::transfers::hdr::hlg_scene_oetf_simd_slice);
542 self.hlg_display_soa = Some(simd_hlg_display_soa);
543 }
544 self
545 }
546
547 #[inline]
549 #[must_use]
550 #[doc(alias = "union")]
551 pub const fn merge_prefer_self(self, fallback: Self) -> Self {
552 let other = fallback;
553 macro_rules! pick {
554 ($field:ident) => {
555 if self.$field.is_some() {
556 self.$field
557 } else {
558 other.$field
559 }
560 };
561 }
562 Self {
563 byte_layout_exec: pick!(byte_layout_exec),
564 rgba8_straight_to_premul: pick!(rgba8_straight_to_premul),
565 strip_unpack: pick!(strip_unpack),
566 strip_pack: pick!(strip_pack),
567 f32_to_u8: pick!(f32_to_u8),
568 u8_to_f32: pick!(u8_to_f32),
569 f32_to_u16: pick!(f32_to_u16),
570 u16_to_f32: pick!(u16_to_f32),
571 srgb_eotf: pick!(srgb_eotf),
572 srgb_oetf: pick!(srgb_oetf),
573 srgb_u8_to_lin: pick!(srgb_u8_to_lin),
574 lin_f32_to_srgb_u8: pick!(lin_f32_to_srgb_u8),
575 srgb8_matrix_exact: pick!(srgb8_matrix_exact),
576 srgb8_matrix_approx: pick!(srgb8_matrix_approx),
577 bt709_eotf: pick!(bt709_eotf),
578 bt709_oetf: pick!(bt709_oetf),
579 gamma22_eotf: pick!(gamma22_eotf),
580 gamma22_oetf: pick!(gamma22_oetf),
581 gamma_pow: pick!(gamma_pow),
582 gamut_3x3: pick!(gamut_3x3),
583 f16_decode: pick!(f16_decode),
584 f16_encode: pick!(f16_encode),
585 #[cfg(feature = "oklab")]
586 rgb_to_oklab: pick!(rgb_to_oklab),
587 #[cfg(feature = "oklab")]
588 oklab_to_rgb: pick!(oklab_to_rgb),
589 #[cfg(feature = "oklab")]
590 soft_compress: pick!(soft_compress),
591 #[cfg(feature = "hdr-transfers")]
592 pq_eotf: pick!(pq_eotf),
593 #[cfg(feature = "hdr-transfers")]
594 pq_oetf: pick!(pq_oetf),
595 #[cfg(feature = "hdr-transfers")]
596 hlg_scene_eotf: pick!(hlg_scene_eotf),
597 #[cfg(feature = "hdr-transfers")]
598 hlg_scene_oetf: pick!(hlg_scene_oetf),
599 #[cfg(feature = "hdr-transfers")]
600 hlg_display_soa: pick!(hlg_display_soa),
601 #[cfg(feature = "gainmap")]
602 gainmap_blend_2: pick!(gainmap_blend_2),
603 #[cfg(feature = "gainmap")]
604 gainmap_blend_4: pick!(gainmap_blend_4),
605 #[cfg(feature = "gainmap")]
606 gainmap_decode: pick!(gainmap_decode),
607 #[cfg(feature = "gainmap")]
608 gainmap_combine: pick!(gainmap_combine),
609 }
610 }
611}
612
613static GLOBAL_ACCEL: OnceBox<Accelerators> = OnceBox::new();
618
619pub fn install(accel: Accelerators) -> bool {
623 accel.install_global()
624}
625
626pub fn enable_all() -> bool {
628 Accelerators::ALL.install_global()
629}
630
631pub fn enable_row_pipeline() -> bool {
633 Accelerators::ROW_PIPELINE.install_global()
634}
635
636pub fn enable_web_sdr() -> bool {
638 Accelerators::WEB_SDR.install_global()
639}
640
641pub fn enable_web_hdr() -> bool {
645 Accelerators::WEB_HDR.install_global()
646}
647
648#[inline]
650#[must_use]
651pub fn resolve(override_accel: Option<Accelerators>) -> Accelerators {
652 if let Some(a) = override_accel {
653 return a;
654 }
655 if let Some(global) = GLOBAL_ACCEL.get() {
656 return *global;
657 }
658 Accelerators::default()
659}
660
661fn simd_srgb_eotf_slice(vals: &mut [f32]) {
667 linear_srgb::default::srgb_to_linear_extended_slice(vals);
668}
669
670fn simd_srgb_oetf_slice(vals: &mut [f32]) {
671 linear_srgb::default::linear_to_srgb_extended_slice(vals);
672}
673
674fn simd_srgb_u8_to_lin_slice(src: &[u8], dst: &mut [f32]) {
675 linear_srgb::default::srgb_u8_to_linear_slice(src, dst);
676}
677
678fn simd_f16_to_f32_slice(src: &[u16], dst: &mut [f32]) {
679 crate::f16_scalar::f16_bits_to_f32_slice(src, dst);
680}
681
682fn simd_f32_to_f16_slice(src: &[f32], dst: &mut [u16]) {
683 crate::f16_scalar::f32_to_f16_bits_slice(src, dst);
684}
685
686#[cfg(feature = "oklab")]
687fn simd_soft_compress_strip(sc: &crate::tonemap::SoftCompress, pixels: &mut [[f32; 3]]) {
688 sc.apply_strip(pixels);
689}
690
691#[cfg(feature = "hdr-transfers")]
692fn simd_hlg_display_soa(
693 prepared: &crate::hlg_render::PreparedHlg,
694 r: &mut [f32],
695 g: &mut [f32],
696 b: &mut [f32],
697 len: usize,
698) {
699 prepared.apply_soa_simd(r, g, b, len);
700}
701
702fn simd_gamut_3x3_soa(m: &[[f32; 3]; 3], r: &mut [f32], g: &mut [f32], b: &mut [f32]) {
704 archmage::attuned!(
705 gamut_3x3_soa_tier(m, r, g, b),
706 [_v4x(cfg(avx512)), _v3, _neon, _wasm128, _scalar]
707 );
708}
709
710#[archmage::attune(_*_t, _v4x_t(cfg(avx512)), define(f32x8))]
711fn gamut_3x3_soa_tier(
712 token: Token,
713 m: &[[f32; 3]; 3],
714 r: &mut [f32],
715 g: &mut [f32],
716 b: &mut [f32],
717) {
718 let m00 = f32x8::splat_t(token, m[0][0]);
719 let m01 = f32x8::splat_t(token, m[0][1]);
720 let m02 = f32x8::splat_t(token, m[0][2]);
721 let m10 = f32x8::splat_t(token, m[1][0]);
722 let m11 = f32x8::splat_t(token, m[1][1]);
723 let m12 = f32x8::splat_t(token, m[1][2]);
724 let m20 = f32x8::splat_t(token, m[2][0]);
725 let m21 = f32x8::splat_t(token, m[2][1]);
726 let m22 = f32x8::splat_t(token, m[2][2]);
727
728 let len = r.len().min(g.len()).min(b.len());
729 let mut i = 0;
730 while i + 8 <= len {
731 let vr = f32x8::from_array_t(token, *<&[f32; 8]>::try_from(&r[i..i + 8]).unwrap());
732 let vg = f32x8::from_array_t(token, *<&[f32; 8]>::try_from(&g[i..i + 8]).unwrap());
733 let vb = f32x8::from_array_t(token, *<&[f32; 8]>::try_from(&b[i..i + 8]).unwrap());
734
735 let nr = (m00 * vr + m01 * vg) + m02 * vb;
737 let ng = (m10 * vr + m11 * vg) + m12 * vb;
738 let nb = (m20 * vr + m21 * vg) + m22 * vb;
739
740 r[i..i + 8].copy_from_slice(&nr.to_array());
741 g[i..i + 8].copy_from_slice(&ng.to_array());
742 b[i..i + 8].copy_from_slice(&nb.to_array());
743 i += 8;
744 }
745 while i < len {
746 let (vr, vg, vb) = (r[i], g[i], b[i]);
747 r[i] = (m[0][0] * vr + m[0][1] * vg) + m[0][2] * vb;
748 g[i] = (m[1][0] * vr + m[1][1] * vg) + m[1][2] * vb;
749 b[i] = (m[2][0] * vr + m[2][1] * vg) + m[2][2] * vb;
750 i += 1;
751 }
752}
753
754#[cfg(feature = "oklab")]
756fn simd_rgb_to_oklab_soa(m1: &[[f32; 3]; 3], r: &mut [f32], g: &mut [f32], b: &mut [f32]) {
757 archmage::attuned!(
758 rgb_to_oklab_soa_tier(m1, r, g, b),
759 [_v4x(cfg(avx512)), _v3, _neon, _wasm128, _scalar]
760 );
761}
762
763#[cfg(feature = "oklab")]
764#[archmage::attune(_*_t, _v4x_t(cfg(avx512)), define(f32x8))]
765fn rgb_to_oklab_soa_tier(
766 token: Token,
767 m1: &[[f32; 3]; 3],
768 r: &mut [f32],
769 g: &mut [f32],
770 b: &mut [f32],
771) {
772 use crate::oklab::{OKLAB_CBRT_ALL, OKLAB_FROM_LMS_CBRT};
773 use magemath::SimdMath;
774
775 let m1_00 = f32x8::splat_t(token, m1[0][0]);
776 let m1_01 = f32x8::splat_t(token, m1[0][1]);
777 let m1_02 = f32x8::splat_t(token, m1[0][2]);
778 let m1_10 = f32x8::splat_t(token, m1[1][0]);
779 let m1_11 = f32x8::splat_t(token, m1[1][1]);
780 let m1_12 = f32x8::splat_t(token, m1[1][2]);
781 let m1_20 = f32x8::splat_t(token, m1[2][0]);
782 let m1_21 = f32x8::splat_t(token, m1[2][1]);
783 let m1_22 = f32x8::splat_t(token, m1[2][2]);
784
785 let m2 = OKLAB_FROM_LMS_CBRT.rows();
786 let m2_00 = f32x8::splat_t(token, m2[0][0]);
787 let m2_01 = f32x8::splat_t(token, m2[0][1]);
788 let m2_02 = f32x8::splat_t(token, m2[0][2]);
789 let m2_10 = f32x8::splat_t(token, m2[1][0]);
790 let m2_11 = f32x8::splat_t(token, m2[1][1]);
791 let m2_12 = f32x8::splat_t(token, m2[1][2]);
792 let m2_20 = f32x8::splat_t(token, m2[2][0]);
793 let m2_21 = f32x8::splat_t(token, m2[2][1]);
794 let m2_22 = f32x8::splat_t(token, m2[2][2]);
795
796 let len = r.len().min(g.len()).min(b.len());
797 let mut i = 0;
798 while i + 8 <= len {
799 let vr = f32x8::from_array_t(token, *<&[f32; 8]>::try_from(&r[i..i + 8]).unwrap());
800 let vg = f32x8::from_array_t(token, *<&[f32; 8]>::try_from(&g[i..i + 8]).unwrap());
801 let vb = f32x8::from_array_t(token, *<&[f32; 8]>::try_from(&b[i..i + 8]).unwrap());
802
803 let l = (m1_00 * vr + m1_01 * vg) + m1_02 * vb;
804 let m = (m1_10 * vr + m1_11 * vg) + m1_12 * vb;
805 let s = (m1_20 * vr + m1_21 * vg) + m1_22 * vb;
806
807 let lc = l.cbrt::<OKLAB_CBRT_ALL>();
808 let mc = m.cbrt::<OKLAB_CBRT_ALL>();
809 let sc = s.cbrt::<OKLAB_CBRT_ALL>();
810
811 let ol = (m2_00 * lc + m2_01 * mc) + m2_02 * sc;
812 let oa = (m2_10 * lc + m2_11 * mc) + m2_12 * sc;
813 let ob = (m2_20 * lc + m2_21 * mc) + m2_22 * sc;
814
815 r[i..i + 8].copy_from_slice(&ol.to_array());
816 g[i..i + 8].copy_from_slice(&oa.to_array());
817 b[i..i + 8].copy_from_slice(&ob.to_array());
818 i += 8;
819 }
820 while i < len {
821 let [ol, oa, ob] = crate::oklab::rgb_to_oklab(r[i], g[i], b[i], m1);
822 r[i] = ol;
823 g[i] = oa;
824 b[i] = ob;
825 i += 1;
826 }
827}
828
829#[cfg(feature = "oklab")]
831fn simd_oklab_to_rgb_soa(m1_inv: &[[f32; 3]; 3], l: &mut [f32], a: &mut [f32], b: &mut [f32]) {
832 archmage::attuned!(
833 oklab_to_rgb_soa_tier(m1_inv, l, a, b),
834 [_v4x(cfg(avx512)), _v3, _neon, _wasm128, _scalar]
835 );
836}
837
838#[cfg(feature = "oklab")]
839#[archmage::attune(_*_t, _v4x_t(cfg(avx512)), define(f32x8))]
840fn oklab_to_rgb_soa_tier(
841 token: Token,
842 m1_inv: &[[f32; 3]; 3],
843 l: &mut [f32],
844 a: &mut [f32],
845 b: &mut [f32],
846) {
847 use crate::oklab::LMS_CBRT_FROM_OKLAB;
848
849 let m2i = LMS_CBRT_FROM_OKLAB.rows();
850 let k00 = f32x8::splat_t(token, m2i[0][0]);
851 let k01 = f32x8::splat_t(token, m2i[0][1]);
852 let k02 = f32x8::splat_t(token, m2i[0][2]);
853 let k10 = f32x8::splat_t(token, m2i[1][0]);
854 let k11 = f32x8::splat_t(token, m2i[1][1]);
855 let k12 = f32x8::splat_t(token, m2i[1][2]);
856 let k20 = f32x8::splat_t(token, m2i[2][0]);
857 let k21 = f32x8::splat_t(token, m2i[2][1]);
858 let k22 = f32x8::splat_t(token, m2i[2][2]);
859
860 let m00 = f32x8::splat_t(token, m1_inv[0][0]);
861 let m01 = f32x8::splat_t(token, m1_inv[0][1]);
862 let m02 = f32x8::splat_t(token, m1_inv[0][2]);
863 let m10 = f32x8::splat_t(token, m1_inv[1][0]);
864 let m11 = f32x8::splat_t(token, m1_inv[1][1]);
865 let m12 = f32x8::splat_t(token, m1_inv[1][2]);
866 let m20 = f32x8::splat_t(token, m1_inv[2][0]);
867 let m21 = f32x8::splat_t(token, m1_inv[2][1]);
868 let m22 = f32x8::splat_t(token, m1_inv[2][2]);
869
870 let len = l.len().min(a.len()).min(b.len());
871 let mut i = 0;
872 while i + 8 <= len {
873 let vl = f32x8::from_array_t(token, *<&[f32; 8]>::try_from(&l[i..i + 8]).unwrap());
874 let va = f32x8::from_array_t(token, *<&[f32; 8]>::try_from(&a[i..i + 8]).unwrap());
875 let vb = f32x8::from_array_t(token, *<&[f32; 8]>::try_from(&b[i..i + 8]).unwrap());
876
877 let lc = (k00 * vl + k01 * va) + k02 * vb;
878 let mc = (k10 * vl + k11 * va) + k12 * vb;
879 let sc = (k20 * vl + k21 * va) + k22 * vb;
880
881 let ll = lc * lc * lc;
882 let mm = mc * mc * mc;
883 let ss = sc * sc * sc;
884
885 let rr = (m00 * ll + m01 * mm) + m02 * ss;
886 let gg = (m10 * ll + m11 * mm) + m12 * ss;
887 let bb = (m20 * ll + m21 * mm) + m22 * ss;
888
889 l[i..i + 8].copy_from_slice(&rr.to_array());
890 a[i..i + 8].copy_from_slice(&gg.to_array());
891 b[i..i + 8].copy_from_slice(&bb.to_array());
892 i += 8;
893 }
894 while i < len {
895 let [rr, gg, bb] = crate::oklab::oklab_to_rgb(l[i], a[i], b[i], m1_inv);
896 l[i] = rr;
897 a[i] = gg;
898 b[i] = bb;
899 i += 1;
900 }
901}