Skip to main content

opendefocus_shared/
lib.rs

1#![warn(unused_extern_crates)]
2#![cfg_attr(target_arch = "spirv", no_std)]
3#![cfg_attr(
4    target_arch = "spirv",
5    allow(internal_features),
6    feature(asm_experimental_arch, core_intrinsics, lang_items, repr_simd)
7)]
8
9// Convencience macros for branchless structure and compile-time optimization
10//
11// Basically sort of same as inline, but with the benefit of forcing the compiler to do so
12// With the cost of larger binary size, sorry! Performance is more important here imho.
13
14#[macro_export]
15macro_rules! f32_from_bool {
16    ($a:expr) => {{
17        #[cfg(not(target_arch = "spirv"))]
18        { f32::from($a) }
19        #[cfg(target_arch = "spirv")]
20        { $a as u32 as f32 }
21    }};
22}
23
24/// Branchless if else expression
25#[macro_export]
26macro_rules! if_else {
27    ($condition:expr, $then:expr, $otherwise:expr) => {{ (f32_from_bool!($condition) * ($then)) + ((!$condition as u32 as f32) * ($otherwise)) }};
28}
29
30#[macro_export]
31macro_rules! max {
32    ($a:expr, $b:expr) => {{ if_else!($a > $b, $a, $b) }};
33}
34
35#[macro_export]
36macro_rules! min {
37    ($a:expr, $b:expr) => {{ if_else!($a < $b, $a, $b) }};
38}
39
40#[macro_export]
41macro_rules! clamp {
42    ($value:expr, $low:expr, $high:expr) => {{ min!(max!($value, $low), $high) }};
43}
44
45
46use glam::UVec2;
47mod internal_settings;
48pub use crate::internal_settings::{
49    AxialAberration, AxialAberrationType, ConvolveSettings, GlobalFlags, NonUniformFlags,
50};
51pub mod math;
52
53#[cfg(not(any(target_arch = "spirv")))]
54pub mod cpu_image;
55
56pub const WORKGROUP_SIZE: u32 = 16;
57pub const OUTPUT_CHANNELS: usize = 5;
58
59#[derive(Copy, Clone, Debug)]
60pub struct ThreadId {
61    x: u32,
62    y: u32,
63}
64
65impl ThreadId {
66    pub fn new(x: u32, y: u32) -> Self {
67        Self { x, y }
68    }
69
70    /// Calculates 2D coordinates from a thread ID based on the resolution.
71    pub fn get_coordinates(&self) -> UVec2 {
72        UVec2::new(
73            self.x,
74            self.y,
75        )
76    }
77}
78