Skip to main content

tinybvh_rs/
cxx_ffi.rs

1#[repr(C)]
2#[derive(Clone, Copy, Debug, PartialEq)]
3pub struct Vec4Slice {
4    data: *const i32,
5    count: u32,
6    stride: u32,
7}
8
9impl From<pas::Slice<'_, [f32; 4]>> for Vec4Slice {
10    fn from(value: pas::Slice<[f32; 4]>) -> Self {
11        Self {
12            data: value.as_ptr() as *const i32,
13            count: value.len() as u32,
14            stride: value.stride() as u32,
15        }
16    }
17}
18
19// Ensure `bvhvec4slice` always has a trivial move ctor and no destructor
20unsafe impl cxx::ExternType for Vec4Slice {
21    type Id = cxx::type_id!("tinybvh::bvhvec4slice");
22    type Kind = cxx::kind::Trivial;
23}
24// Ensure `Intersection` always has a trivial move ctor and no destructor
25unsafe impl cxx::ExternType for crate::Intersection {
26    type Id = cxx::type_id!("tinybvh::Intersection");
27    type Kind = cxx::kind::Trivial;
28}
29// Ensure `Ray` always has a trivial move ctor and no destructor
30unsafe impl cxx::ExternType for crate::Ray {
31    type Id = cxx::type_id!("tinybvh::Ray");
32    type Kind = cxx::kind::Trivial;
33}
34// Ensure `BVH::BVHNode` always has a trivial move ctor and no destructor
35unsafe impl cxx::ExternType for crate::bvh::Node {
36    type Id = cxx::type_id!("tinybvh::BVHNode");
37    type Kind = cxx::kind::Trivial;
38}
39unsafe impl cxx::ExternType for crate::mbvh::Node {
40    type Id = cxx::type_id!("tinybvh::MBVH8Node");
41    type Kind = cxx::kind::Trivial;
42}
43
44#[cxx::bridge(namespace = "tinybvh")]
45pub(crate) mod ffi {
46    unsafe extern "C++" {
47        include!("tinybvh-rs/ffi/include/tinybvh.h");
48
49        // Utils
50        pub type bvhvec4slice = super::Vec4Slice;
51        pub type Ray = crate::Ray;
52        pub fn ray_new(origin: &[f32; 3], dir: &[f32; 3]) -> Ray;
53
54        // BVH
55        pub type BVH;
56        pub type BVHNode = crate::bvh::Node;
57        pub fn BVH_new() -> UniquePtr<BVH>;
58        pub fn BVH_nodes(bvh: &BVH) -> &[BVHNode];
59        pub fn BVH_indices(bvh: &BVH) -> &[u32];
60        pub fn BVH_refittable(bvh: &BVH) -> bool;
61        pub fn BVH_setPrimitives(out: Pin<&mut BVH>, primitives: &bvhvec4slice);
62        pub fn Build(self: Pin<&mut BVH>, primitives: &bvhvec4slice);
63        pub fn BuildHQ(self: Pin<&mut BVH>, primitives: &bvhvec4slice);
64        pub fn Compact(self: Pin<&mut BVH>);
65        pub fn SplitLeafs(self: Pin<&mut BVH>, count: u32);
66        pub fn Refit(self: Pin<&mut BVH>, node_idx: u32);
67        pub fn SAHCost(self: &BVH, node_idx: u32) -> f32;
68        pub fn PrimCount(self: &BVH, node_idx: u32) -> i32;
69        pub fn Intersect(self: &BVH, original: &mut Ray) -> i32;
70
71        // MBVH8
72        pub type MBVH8;
73        pub type MBVH8Node = crate::mbvh::Node;
74        pub fn MBVH8_new() -> UniquePtr<MBVH8>;
75        pub fn MBVH8_setBVH(out: Pin<&mut MBVH8>, bvh: &BVH);
76        pub fn MBVH8_nodes(bvh: &MBVH8) -> &[MBVH8Node];
77        pub fn ConvertFrom(self: Pin<&mut MBVH8>, bvh: &BVH, compact: bool);
78        pub fn Refit(self: Pin<&mut MBVH8>, node_index: u32);
79        pub fn LeafCount(self: &MBVH8, node_index: u32) -> u32;
80
81        // BVH8_CPU
82        pub type BVH8_CPU;
83        pub fn BVH8_CPU_new() -> UniquePtr<BVH8_CPU>;
84        pub fn ConvertFrom(self: Pin<&mut BVH8_CPU>, bvh: &MBVH8);
85        pub fn Intersect(self: &BVH8_CPU, ray: &mut Ray) -> i32;
86
87        // CWBVH
88        pub type BVH8_CWBVH;
89        pub fn CWBVH_new() -> UniquePtr<BVH8_CWBVH>;
90        pub fn CWBVH_nodes(bvh: &BVH8_CWBVH) -> *const u8;
91        pub fn CWBVH_nodes_count(bvh: &BVH8_CWBVH) -> u32;
92        pub fn CWBVH_primitives(bvh: &BVH8_CWBVH) -> *const u8;
93        pub fn CWBVH_primitives_count(bvh: &BVH8_CWBVH) -> u32;
94        pub fn ConvertFrom(self: Pin<&mut BVH8_CWBVH>, bvh: &MBVH8, compact: bool);
95    }
96}