Skip to main content

PolygonAccelerationStructure

Struct PolygonAccelerationStructure 

Source
pub struct PolygonAccelerationStructure { /* private fields */ }

Implementations§

Source§

impl PolygonAccelerationStructure

Source

pub const fn as_ptr(&self) -> *mut c_void

Source§

impl PolygonAccelerationStructure

Source

pub fn new(device: &MetalDevice) -> Option<Self>

Examples found in repository?
examples/04_ray_intersection.rs (line 59)
41fn main() {
42    let device = MetalDevice::system_default().expect("no Metal device available");
43    let queue = device.new_command_queue().expect("command queue");
44
45    let vertices = [
46        [-1.0_f32, -1.0, 0.0, 0.0],
47        [1.0_f32, -1.0, 0.0, 0.0],
48        [0.0_f32, 1.0, 0.0, 0.0],
49    ];
50    let vertex_buffer = device
51        .new_buffer(
52            core::mem::size_of_val(&vertices),
53            resource_options::STORAGE_MODE_SHARED,
54        )
55        .expect("vertex buffer");
56    let _ = vertex_buffer.write_bytes(as_bytes(&vertices));
57
58    let acceleration_structure =
59        PolygonAccelerationStructure::new(&device).expect("polygon acceleration structure");
60    acceleration_structure.set_polygon_type(polygon_type::TRIANGLE);
61    acceleration_structure.set_vertex_stride(core::mem::size_of::<[f32; 4]>());
62    acceleration_structure.set_index_type(data_type::UINT32);
63    acceleration_structure.set_vertex_buffer(Some(&vertex_buffer));
64    acceleration_structure.set_index_buffer(None);
65    acceleration_structure.set_polygon_count(1);
66    acceleration_structure.rebuild();
67    assert_eq!(
68        acceleration_structure.status(),
69        acceleration_structure_status::BUILT,
70        "acceleration structure should build successfully"
71    );
72
73    let ray = PackedRayOriginDirection {
74        origin: PackedFloat3 {
75            x: 0.0,
76            y: 0.0,
77            z: 1.0,
78        },
79        direction: PackedFloat3 {
80            x: 0.0,
81            y: 0.0,
82            z: -1.0,
83        },
84    };
85    let miss = IntersectionDistancePrimitiveIndex {
86        distance: -1.0,
87        primitive_index: u32::MAX,
88    };
89
90    let ray_buffer = device
91        .new_buffer(
92            core::mem::size_of::<PackedRayOriginDirection>(),
93            resource_options::STORAGE_MODE_SHARED,
94        )
95        .expect("ray buffer");
96    let intersection_buffer = device
97        .new_buffer(
98            core::mem::size_of::<IntersectionDistancePrimitiveIndex>(),
99            resource_options::STORAGE_MODE_SHARED,
100        )
101        .expect("intersection buffer");
102    let _ = ray_buffer.write_bytes(as_bytes(&[ray]));
103    let _ = intersection_buffer.write_bytes(as_bytes(&[miss]));
104
105    let intersector = RayIntersector::new(&device).expect("ray intersector");
106    intersector.set_cull_mode(cull_mode::NONE);
107    intersector.set_ray_data_type(ray_data_type::PACKED_ORIGIN_DIRECTION);
108    intersector.set_intersection_data_type(intersection_data_type::DISTANCE_PRIMITIVE_INDEX);
109    assert!(
110        intersector.recommended_minimum_ray_batch_size(1) >= 1,
111        "recommended ray batch size should be positive"
112    );
113
114    let command_buffer = queue.new_command_buffer().expect("command buffer");
115    intersector.encode_intersection(
116        &command_buffer,
117        intersection_type::NEAREST,
118        &ray_buffer,
119        0,
120        &intersection_buffer,
121        0,
122        1,
123        &acceleration_structure,
124    );
125    command_buffer.commit();
126    command_buffer.wait_until_completed();
127
128    let intersection = read_struct::<IntersectionDistancePrimitiveIndex>(&intersection_buffer);
129    assert!(
130        (intersection.distance - 1.0).abs() < 1.0e-4,
131        "unexpected hit distance: {intersection:?}"
132    );
133    assert_eq!(
134        intersection.primitive_index, 0,
135        "expected to hit triangle 0"
136    );
137
138    let svgf = SVGF::new(&device).expect("svgf");
139    svgf.set_depth_weight(0.75);
140    svgf.set_normal_weight(64.0);
141    svgf.set_luminance_weight(2.5);
142    svgf.set_channel_count(3);
143    svgf.set_channel_count2(1);
144    assert!((svgf.depth_weight() - 0.75).abs() < f32::EPSILON);
145    assert!((svgf.normal_weight() - 64.0).abs() < f32::EPSILON);
146    assert!((svgf.luminance_weight() - 2.5).abs() < f32::EPSILON);
147    assert_eq!(svgf.channel_count(), 3);
148    assert_eq!(svgf.channel_count2(), 1);
149
150    println!(
151        "ray smoke passed: distance={:.3} primitive_index={} batch_size={}",
152        intersection.distance,
153        intersection.primitive_index,
154        intersector.recommended_minimum_ray_batch_size(1)
155    );
156}
Source

pub fn polygon_type(&self) -> usize

Source

pub fn set_polygon_type(&self, polygon_type: usize)

Examples found in repository?
examples/04_ray_intersection.rs (line 60)
41fn main() {
42    let device = MetalDevice::system_default().expect("no Metal device available");
43    let queue = device.new_command_queue().expect("command queue");
44
45    let vertices = [
46        [-1.0_f32, -1.0, 0.0, 0.0],
47        [1.0_f32, -1.0, 0.0, 0.0],
48        [0.0_f32, 1.0, 0.0, 0.0],
49    ];
50    let vertex_buffer = device
51        .new_buffer(
52            core::mem::size_of_val(&vertices),
53            resource_options::STORAGE_MODE_SHARED,
54        )
55        .expect("vertex buffer");
56    let _ = vertex_buffer.write_bytes(as_bytes(&vertices));
57
58    let acceleration_structure =
59        PolygonAccelerationStructure::new(&device).expect("polygon acceleration structure");
60    acceleration_structure.set_polygon_type(polygon_type::TRIANGLE);
61    acceleration_structure.set_vertex_stride(core::mem::size_of::<[f32; 4]>());
62    acceleration_structure.set_index_type(data_type::UINT32);
63    acceleration_structure.set_vertex_buffer(Some(&vertex_buffer));
64    acceleration_structure.set_index_buffer(None);
65    acceleration_structure.set_polygon_count(1);
66    acceleration_structure.rebuild();
67    assert_eq!(
68        acceleration_structure.status(),
69        acceleration_structure_status::BUILT,
70        "acceleration structure should build successfully"
71    );
72
73    let ray = PackedRayOriginDirection {
74        origin: PackedFloat3 {
75            x: 0.0,
76            y: 0.0,
77            z: 1.0,
78        },
79        direction: PackedFloat3 {
80            x: 0.0,
81            y: 0.0,
82            z: -1.0,
83        },
84    };
85    let miss = IntersectionDistancePrimitiveIndex {
86        distance: -1.0,
87        primitive_index: u32::MAX,
88    };
89
90    let ray_buffer = device
91        .new_buffer(
92            core::mem::size_of::<PackedRayOriginDirection>(),
93            resource_options::STORAGE_MODE_SHARED,
94        )
95        .expect("ray buffer");
96    let intersection_buffer = device
97        .new_buffer(
98            core::mem::size_of::<IntersectionDistancePrimitiveIndex>(),
99            resource_options::STORAGE_MODE_SHARED,
100        )
101        .expect("intersection buffer");
102    let _ = ray_buffer.write_bytes(as_bytes(&[ray]));
103    let _ = intersection_buffer.write_bytes(as_bytes(&[miss]));
104
105    let intersector = RayIntersector::new(&device).expect("ray intersector");
106    intersector.set_cull_mode(cull_mode::NONE);
107    intersector.set_ray_data_type(ray_data_type::PACKED_ORIGIN_DIRECTION);
108    intersector.set_intersection_data_type(intersection_data_type::DISTANCE_PRIMITIVE_INDEX);
109    assert!(
110        intersector.recommended_minimum_ray_batch_size(1) >= 1,
111        "recommended ray batch size should be positive"
112    );
113
114    let command_buffer = queue.new_command_buffer().expect("command buffer");
115    intersector.encode_intersection(
116        &command_buffer,
117        intersection_type::NEAREST,
118        &ray_buffer,
119        0,
120        &intersection_buffer,
121        0,
122        1,
123        &acceleration_structure,
124    );
125    command_buffer.commit();
126    command_buffer.wait_until_completed();
127
128    let intersection = read_struct::<IntersectionDistancePrimitiveIndex>(&intersection_buffer);
129    assert!(
130        (intersection.distance - 1.0).abs() < 1.0e-4,
131        "unexpected hit distance: {intersection:?}"
132    );
133    assert_eq!(
134        intersection.primitive_index, 0,
135        "expected to hit triangle 0"
136    );
137
138    let svgf = SVGF::new(&device).expect("svgf");
139    svgf.set_depth_weight(0.75);
140    svgf.set_normal_weight(64.0);
141    svgf.set_luminance_weight(2.5);
142    svgf.set_channel_count(3);
143    svgf.set_channel_count2(1);
144    assert!((svgf.depth_weight() - 0.75).abs() < f32::EPSILON);
145    assert!((svgf.normal_weight() - 64.0).abs() < f32::EPSILON);
146    assert!((svgf.luminance_weight() - 2.5).abs() < f32::EPSILON);
147    assert_eq!(svgf.channel_count(), 3);
148    assert_eq!(svgf.channel_count2(), 1);
149
150    println!(
151        "ray smoke passed: distance={:.3} primitive_index={} batch_size={}",
152        intersection.distance,
153        intersection.primitive_index,
154        intersector.recommended_minimum_ray_batch_size(1)
155    );
156}
Source

pub fn vertex_stride(&self) -> usize

Source

pub fn set_vertex_stride(&self, vertex_stride: usize)

Examples found in repository?
examples/04_ray_intersection.rs (line 61)
41fn main() {
42    let device = MetalDevice::system_default().expect("no Metal device available");
43    let queue = device.new_command_queue().expect("command queue");
44
45    let vertices = [
46        [-1.0_f32, -1.0, 0.0, 0.0],
47        [1.0_f32, -1.0, 0.0, 0.0],
48        [0.0_f32, 1.0, 0.0, 0.0],
49    ];
50    let vertex_buffer = device
51        .new_buffer(
52            core::mem::size_of_val(&vertices),
53            resource_options::STORAGE_MODE_SHARED,
54        )
55        .expect("vertex buffer");
56    let _ = vertex_buffer.write_bytes(as_bytes(&vertices));
57
58    let acceleration_structure =
59        PolygonAccelerationStructure::new(&device).expect("polygon acceleration structure");
60    acceleration_structure.set_polygon_type(polygon_type::TRIANGLE);
61    acceleration_structure.set_vertex_stride(core::mem::size_of::<[f32; 4]>());
62    acceleration_structure.set_index_type(data_type::UINT32);
63    acceleration_structure.set_vertex_buffer(Some(&vertex_buffer));
64    acceleration_structure.set_index_buffer(None);
65    acceleration_structure.set_polygon_count(1);
66    acceleration_structure.rebuild();
67    assert_eq!(
68        acceleration_structure.status(),
69        acceleration_structure_status::BUILT,
70        "acceleration structure should build successfully"
71    );
72
73    let ray = PackedRayOriginDirection {
74        origin: PackedFloat3 {
75            x: 0.0,
76            y: 0.0,
77            z: 1.0,
78        },
79        direction: PackedFloat3 {
80            x: 0.0,
81            y: 0.0,
82            z: -1.0,
83        },
84    };
85    let miss = IntersectionDistancePrimitiveIndex {
86        distance: -1.0,
87        primitive_index: u32::MAX,
88    };
89
90    let ray_buffer = device
91        .new_buffer(
92            core::mem::size_of::<PackedRayOriginDirection>(),
93            resource_options::STORAGE_MODE_SHARED,
94        )
95        .expect("ray buffer");
96    let intersection_buffer = device
97        .new_buffer(
98            core::mem::size_of::<IntersectionDistancePrimitiveIndex>(),
99            resource_options::STORAGE_MODE_SHARED,
100        )
101        .expect("intersection buffer");
102    let _ = ray_buffer.write_bytes(as_bytes(&[ray]));
103    let _ = intersection_buffer.write_bytes(as_bytes(&[miss]));
104
105    let intersector = RayIntersector::new(&device).expect("ray intersector");
106    intersector.set_cull_mode(cull_mode::NONE);
107    intersector.set_ray_data_type(ray_data_type::PACKED_ORIGIN_DIRECTION);
108    intersector.set_intersection_data_type(intersection_data_type::DISTANCE_PRIMITIVE_INDEX);
109    assert!(
110        intersector.recommended_minimum_ray_batch_size(1) >= 1,
111        "recommended ray batch size should be positive"
112    );
113
114    let command_buffer = queue.new_command_buffer().expect("command buffer");
115    intersector.encode_intersection(
116        &command_buffer,
117        intersection_type::NEAREST,
118        &ray_buffer,
119        0,
120        &intersection_buffer,
121        0,
122        1,
123        &acceleration_structure,
124    );
125    command_buffer.commit();
126    command_buffer.wait_until_completed();
127
128    let intersection = read_struct::<IntersectionDistancePrimitiveIndex>(&intersection_buffer);
129    assert!(
130        (intersection.distance - 1.0).abs() < 1.0e-4,
131        "unexpected hit distance: {intersection:?}"
132    );
133    assert_eq!(
134        intersection.primitive_index, 0,
135        "expected to hit triangle 0"
136    );
137
138    let svgf = SVGF::new(&device).expect("svgf");
139    svgf.set_depth_weight(0.75);
140    svgf.set_normal_weight(64.0);
141    svgf.set_luminance_weight(2.5);
142    svgf.set_channel_count(3);
143    svgf.set_channel_count2(1);
144    assert!((svgf.depth_weight() - 0.75).abs() < f32::EPSILON);
145    assert!((svgf.normal_weight() - 64.0).abs() < f32::EPSILON);
146    assert!((svgf.luminance_weight() - 2.5).abs() < f32::EPSILON);
147    assert_eq!(svgf.channel_count(), 3);
148    assert_eq!(svgf.channel_count2(), 1);
149
150    println!(
151        "ray smoke passed: distance={:.3} primitive_index={} batch_size={}",
152        intersection.distance,
153        intersection.primitive_index,
154        intersector.recommended_minimum_ray_batch_size(1)
155    );
156}
Source

pub fn index_type(&self) -> u32

Source

pub fn set_index_type(&self, index_type: u32)

Examples found in repository?
examples/04_ray_intersection.rs (line 62)
41fn main() {
42    let device = MetalDevice::system_default().expect("no Metal device available");
43    let queue = device.new_command_queue().expect("command queue");
44
45    let vertices = [
46        [-1.0_f32, -1.0, 0.0, 0.0],
47        [1.0_f32, -1.0, 0.0, 0.0],
48        [0.0_f32, 1.0, 0.0, 0.0],
49    ];
50    let vertex_buffer = device
51        .new_buffer(
52            core::mem::size_of_val(&vertices),
53            resource_options::STORAGE_MODE_SHARED,
54        )
55        .expect("vertex buffer");
56    let _ = vertex_buffer.write_bytes(as_bytes(&vertices));
57
58    let acceleration_structure =
59        PolygonAccelerationStructure::new(&device).expect("polygon acceleration structure");
60    acceleration_structure.set_polygon_type(polygon_type::TRIANGLE);
61    acceleration_structure.set_vertex_stride(core::mem::size_of::<[f32; 4]>());
62    acceleration_structure.set_index_type(data_type::UINT32);
63    acceleration_structure.set_vertex_buffer(Some(&vertex_buffer));
64    acceleration_structure.set_index_buffer(None);
65    acceleration_structure.set_polygon_count(1);
66    acceleration_structure.rebuild();
67    assert_eq!(
68        acceleration_structure.status(),
69        acceleration_structure_status::BUILT,
70        "acceleration structure should build successfully"
71    );
72
73    let ray = PackedRayOriginDirection {
74        origin: PackedFloat3 {
75            x: 0.0,
76            y: 0.0,
77            z: 1.0,
78        },
79        direction: PackedFloat3 {
80            x: 0.0,
81            y: 0.0,
82            z: -1.0,
83        },
84    };
85    let miss = IntersectionDistancePrimitiveIndex {
86        distance: -1.0,
87        primitive_index: u32::MAX,
88    };
89
90    let ray_buffer = device
91        .new_buffer(
92            core::mem::size_of::<PackedRayOriginDirection>(),
93            resource_options::STORAGE_MODE_SHARED,
94        )
95        .expect("ray buffer");
96    let intersection_buffer = device
97        .new_buffer(
98            core::mem::size_of::<IntersectionDistancePrimitiveIndex>(),
99            resource_options::STORAGE_MODE_SHARED,
100        )
101        .expect("intersection buffer");
102    let _ = ray_buffer.write_bytes(as_bytes(&[ray]));
103    let _ = intersection_buffer.write_bytes(as_bytes(&[miss]));
104
105    let intersector = RayIntersector::new(&device).expect("ray intersector");
106    intersector.set_cull_mode(cull_mode::NONE);
107    intersector.set_ray_data_type(ray_data_type::PACKED_ORIGIN_DIRECTION);
108    intersector.set_intersection_data_type(intersection_data_type::DISTANCE_PRIMITIVE_INDEX);
109    assert!(
110        intersector.recommended_minimum_ray_batch_size(1) >= 1,
111        "recommended ray batch size should be positive"
112    );
113
114    let command_buffer = queue.new_command_buffer().expect("command buffer");
115    intersector.encode_intersection(
116        &command_buffer,
117        intersection_type::NEAREST,
118        &ray_buffer,
119        0,
120        &intersection_buffer,
121        0,
122        1,
123        &acceleration_structure,
124    );
125    command_buffer.commit();
126    command_buffer.wait_until_completed();
127
128    let intersection = read_struct::<IntersectionDistancePrimitiveIndex>(&intersection_buffer);
129    assert!(
130        (intersection.distance - 1.0).abs() < 1.0e-4,
131        "unexpected hit distance: {intersection:?}"
132    );
133    assert_eq!(
134        intersection.primitive_index, 0,
135        "expected to hit triangle 0"
136    );
137
138    let svgf = SVGF::new(&device).expect("svgf");
139    svgf.set_depth_weight(0.75);
140    svgf.set_normal_weight(64.0);
141    svgf.set_luminance_weight(2.5);
142    svgf.set_channel_count(3);
143    svgf.set_channel_count2(1);
144    assert!((svgf.depth_weight() - 0.75).abs() < f32::EPSILON);
145    assert!((svgf.normal_weight() - 64.0).abs() < f32::EPSILON);
146    assert!((svgf.luminance_weight() - 2.5).abs() < f32::EPSILON);
147    assert_eq!(svgf.channel_count(), 3);
148    assert_eq!(svgf.channel_count2(), 1);
149
150    println!(
151        "ray smoke passed: distance={:.3} primitive_index={} batch_size={}",
152        intersection.distance,
153        intersection.primitive_index,
154        intersector.recommended_minimum_ray_batch_size(1)
155    );
156}
Source

pub fn set_vertex_buffer(&self, buffer: Option<&MetalBuffer>)

Examples found in repository?
examples/04_ray_intersection.rs (line 63)
41fn main() {
42    let device = MetalDevice::system_default().expect("no Metal device available");
43    let queue = device.new_command_queue().expect("command queue");
44
45    let vertices = [
46        [-1.0_f32, -1.0, 0.0, 0.0],
47        [1.0_f32, -1.0, 0.0, 0.0],
48        [0.0_f32, 1.0, 0.0, 0.0],
49    ];
50    let vertex_buffer = device
51        .new_buffer(
52            core::mem::size_of_val(&vertices),
53            resource_options::STORAGE_MODE_SHARED,
54        )
55        .expect("vertex buffer");
56    let _ = vertex_buffer.write_bytes(as_bytes(&vertices));
57
58    let acceleration_structure =
59        PolygonAccelerationStructure::new(&device).expect("polygon acceleration structure");
60    acceleration_structure.set_polygon_type(polygon_type::TRIANGLE);
61    acceleration_structure.set_vertex_stride(core::mem::size_of::<[f32; 4]>());
62    acceleration_structure.set_index_type(data_type::UINT32);
63    acceleration_structure.set_vertex_buffer(Some(&vertex_buffer));
64    acceleration_structure.set_index_buffer(None);
65    acceleration_structure.set_polygon_count(1);
66    acceleration_structure.rebuild();
67    assert_eq!(
68        acceleration_structure.status(),
69        acceleration_structure_status::BUILT,
70        "acceleration structure should build successfully"
71    );
72
73    let ray = PackedRayOriginDirection {
74        origin: PackedFloat3 {
75            x: 0.0,
76            y: 0.0,
77            z: 1.0,
78        },
79        direction: PackedFloat3 {
80            x: 0.0,
81            y: 0.0,
82            z: -1.0,
83        },
84    };
85    let miss = IntersectionDistancePrimitiveIndex {
86        distance: -1.0,
87        primitive_index: u32::MAX,
88    };
89
90    let ray_buffer = device
91        .new_buffer(
92            core::mem::size_of::<PackedRayOriginDirection>(),
93            resource_options::STORAGE_MODE_SHARED,
94        )
95        .expect("ray buffer");
96    let intersection_buffer = device
97        .new_buffer(
98            core::mem::size_of::<IntersectionDistancePrimitiveIndex>(),
99            resource_options::STORAGE_MODE_SHARED,
100        )
101        .expect("intersection buffer");
102    let _ = ray_buffer.write_bytes(as_bytes(&[ray]));
103    let _ = intersection_buffer.write_bytes(as_bytes(&[miss]));
104
105    let intersector = RayIntersector::new(&device).expect("ray intersector");
106    intersector.set_cull_mode(cull_mode::NONE);
107    intersector.set_ray_data_type(ray_data_type::PACKED_ORIGIN_DIRECTION);
108    intersector.set_intersection_data_type(intersection_data_type::DISTANCE_PRIMITIVE_INDEX);
109    assert!(
110        intersector.recommended_minimum_ray_batch_size(1) >= 1,
111        "recommended ray batch size should be positive"
112    );
113
114    let command_buffer = queue.new_command_buffer().expect("command buffer");
115    intersector.encode_intersection(
116        &command_buffer,
117        intersection_type::NEAREST,
118        &ray_buffer,
119        0,
120        &intersection_buffer,
121        0,
122        1,
123        &acceleration_structure,
124    );
125    command_buffer.commit();
126    command_buffer.wait_until_completed();
127
128    let intersection = read_struct::<IntersectionDistancePrimitiveIndex>(&intersection_buffer);
129    assert!(
130        (intersection.distance - 1.0).abs() < 1.0e-4,
131        "unexpected hit distance: {intersection:?}"
132    );
133    assert_eq!(
134        intersection.primitive_index, 0,
135        "expected to hit triangle 0"
136    );
137
138    let svgf = SVGF::new(&device).expect("svgf");
139    svgf.set_depth_weight(0.75);
140    svgf.set_normal_weight(64.0);
141    svgf.set_luminance_weight(2.5);
142    svgf.set_channel_count(3);
143    svgf.set_channel_count2(1);
144    assert!((svgf.depth_weight() - 0.75).abs() < f32::EPSILON);
145    assert!((svgf.normal_weight() - 64.0).abs() < f32::EPSILON);
146    assert!((svgf.luminance_weight() - 2.5).abs() < f32::EPSILON);
147    assert_eq!(svgf.channel_count(), 3);
148    assert_eq!(svgf.channel_count2(), 1);
149
150    println!(
151        "ray smoke passed: distance={:.3} primitive_index={} batch_size={}",
152        intersection.distance,
153        intersection.primitive_index,
154        intersector.recommended_minimum_ray_batch_size(1)
155    );
156}
Source

pub fn vertex_buffer_offset(&self) -> usize

Source

pub fn set_vertex_buffer_offset(&self, offset: usize)

Source

pub fn set_index_buffer(&self, buffer: Option<&MetalBuffer>)

Examples found in repository?
examples/04_ray_intersection.rs (line 64)
41fn main() {
42    let device = MetalDevice::system_default().expect("no Metal device available");
43    let queue = device.new_command_queue().expect("command queue");
44
45    let vertices = [
46        [-1.0_f32, -1.0, 0.0, 0.0],
47        [1.0_f32, -1.0, 0.0, 0.0],
48        [0.0_f32, 1.0, 0.0, 0.0],
49    ];
50    let vertex_buffer = device
51        .new_buffer(
52            core::mem::size_of_val(&vertices),
53            resource_options::STORAGE_MODE_SHARED,
54        )
55        .expect("vertex buffer");
56    let _ = vertex_buffer.write_bytes(as_bytes(&vertices));
57
58    let acceleration_structure =
59        PolygonAccelerationStructure::new(&device).expect("polygon acceleration structure");
60    acceleration_structure.set_polygon_type(polygon_type::TRIANGLE);
61    acceleration_structure.set_vertex_stride(core::mem::size_of::<[f32; 4]>());
62    acceleration_structure.set_index_type(data_type::UINT32);
63    acceleration_structure.set_vertex_buffer(Some(&vertex_buffer));
64    acceleration_structure.set_index_buffer(None);
65    acceleration_structure.set_polygon_count(1);
66    acceleration_structure.rebuild();
67    assert_eq!(
68        acceleration_structure.status(),
69        acceleration_structure_status::BUILT,
70        "acceleration structure should build successfully"
71    );
72
73    let ray = PackedRayOriginDirection {
74        origin: PackedFloat3 {
75            x: 0.0,
76            y: 0.0,
77            z: 1.0,
78        },
79        direction: PackedFloat3 {
80            x: 0.0,
81            y: 0.0,
82            z: -1.0,
83        },
84    };
85    let miss = IntersectionDistancePrimitiveIndex {
86        distance: -1.0,
87        primitive_index: u32::MAX,
88    };
89
90    let ray_buffer = device
91        .new_buffer(
92            core::mem::size_of::<PackedRayOriginDirection>(),
93            resource_options::STORAGE_MODE_SHARED,
94        )
95        .expect("ray buffer");
96    let intersection_buffer = device
97        .new_buffer(
98            core::mem::size_of::<IntersectionDistancePrimitiveIndex>(),
99            resource_options::STORAGE_MODE_SHARED,
100        )
101        .expect("intersection buffer");
102    let _ = ray_buffer.write_bytes(as_bytes(&[ray]));
103    let _ = intersection_buffer.write_bytes(as_bytes(&[miss]));
104
105    let intersector = RayIntersector::new(&device).expect("ray intersector");
106    intersector.set_cull_mode(cull_mode::NONE);
107    intersector.set_ray_data_type(ray_data_type::PACKED_ORIGIN_DIRECTION);
108    intersector.set_intersection_data_type(intersection_data_type::DISTANCE_PRIMITIVE_INDEX);
109    assert!(
110        intersector.recommended_minimum_ray_batch_size(1) >= 1,
111        "recommended ray batch size should be positive"
112    );
113
114    let command_buffer = queue.new_command_buffer().expect("command buffer");
115    intersector.encode_intersection(
116        &command_buffer,
117        intersection_type::NEAREST,
118        &ray_buffer,
119        0,
120        &intersection_buffer,
121        0,
122        1,
123        &acceleration_structure,
124    );
125    command_buffer.commit();
126    command_buffer.wait_until_completed();
127
128    let intersection = read_struct::<IntersectionDistancePrimitiveIndex>(&intersection_buffer);
129    assert!(
130        (intersection.distance - 1.0).abs() < 1.0e-4,
131        "unexpected hit distance: {intersection:?}"
132    );
133    assert_eq!(
134        intersection.primitive_index, 0,
135        "expected to hit triangle 0"
136    );
137
138    let svgf = SVGF::new(&device).expect("svgf");
139    svgf.set_depth_weight(0.75);
140    svgf.set_normal_weight(64.0);
141    svgf.set_luminance_weight(2.5);
142    svgf.set_channel_count(3);
143    svgf.set_channel_count2(1);
144    assert!((svgf.depth_weight() - 0.75).abs() < f32::EPSILON);
145    assert!((svgf.normal_weight() - 64.0).abs() < f32::EPSILON);
146    assert!((svgf.luminance_weight() - 2.5).abs() < f32::EPSILON);
147    assert_eq!(svgf.channel_count(), 3);
148    assert_eq!(svgf.channel_count2(), 1);
149
150    println!(
151        "ray smoke passed: distance={:.3} primitive_index={} batch_size={}",
152        intersection.distance,
153        intersection.primitive_index,
154        intersector.recommended_minimum_ray_batch_size(1)
155    );
156}
Source

pub fn index_buffer_offset(&self) -> usize

Source

pub fn set_index_buffer_offset(&self, offset: usize)

Source

pub fn polygon_count(&self) -> usize

Source

pub fn set_polygon_count(&self, count: usize)

Examples found in repository?
examples/04_ray_intersection.rs (line 65)
41fn main() {
42    let device = MetalDevice::system_default().expect("no Metal device available");
43    let queue = device.new_command_queue().expect("command queue");
44
45    let vertices = [
46        [-1.0_f32, -1.0, 0.0, 0.0],
47        [1.0_f32, -1.0, 0.0, 0.0],
48        [0.0_f32, 1.0, 0.0, 0.0],
49    ];
50    let vertex_buffer = device
51        .new_buffer(
52            core::mem::size_of_val(&vertices),
53            resource_options::STORAGE_MODE_SHARED,
54        )
55        .expect("vertex buffer");
56    let _ = vertex_buffer.write_bytes(as_bytes(&vertices));
57
58    let acceleration_structure =
59        PolygonAccelerationStructure::new(&device).expect("polygon acceleration structure");
60    acceleration_structure.set_polygon_type(polygon_type::TRIANGLE);
61    acceleration_structure.set_vertex_stride(core::mem::size_of::<[f32; 4]>());
62    acceleration_structure.set_index_type(data_type::UINT32);
63    acceleration_structure.set_vertex_buffer(Some(&vertex_buffer));
64    acceleration_structure.set_index_buffer(None);
65    acceleration_structure.set_polygon_count(1);
66    acceleration_structure.rebuild();
67    assert_eq!(
68        acceleration_structure.status(),
69        acceleration_structure_status::BUILT,
70        "acceleration structure should build successfully"
71    );
72
73    let ray = PackedRayOriginDirection {
74        origin: PackedFloat3 {
75            x: 0.0,
76            y: 0.0,
77            z: 1.0,
78        },
79        direction: PackedFloat3 {
80            x: 0.0,
81            y: 0.0,
82            z: -1.0,
83        },
84    };
85    let miss = IntersectionDistancePrimitiveIndex {
86        distance: -1.0,
87        primitive_index: u32::MAX,
88    };
89
90    let ray_buffer = device
91        .new_buffer(
92            core::mem::size_of::<PackedRayOriginDirection>(),
93            resource_options::STORAGE_MODE_SHARED,
94        )
95        .expect("ray buffer");
96    let intersection_buffer = device
97        .new_buffer(
98            core::mem::size_of::<IntersectionDistancePrimitiveIndex>(),
99            resource_options::STORAGE_MODE_SHARED,
100        )
101        .expect("intersection buffer");
102    let _ = ray_buffer.write_bytes(as_bytes(&[ray]));
103    let _ = intersection_buffer.write_bytes(as_bytes(&[miss]));
104
105    let intersector = RayIntersector::new(&device).expect("ray intersector");
106    intersector.set_cull_mode(cull_mode::NONE);
107    intersector.set_ray_data_type(ray_data_type::PACKED_ORIGIN_DIRECTION);
108    intersector.set_intersection_data_type(intersection_data_type::DISTANCE_PRIMITIVE_INDEX);
109    assert!(
110        intersector.recommended_minimum_ray_batch_size(1) >= 1,
111        "recommended ray batch size should be positive"
112    );
113
114    let command_buffer = queue.new_command_buffer().expect("command buffer");
115    intersector.encode_intersection(
116        &command_buffer,
117        intersection_type::NEAREST,
118        &ray_buffer,
119        0,
120        &intersection_buffer,
121        0,
122        1,
123        &acceleration_structure,
124    );
125    command_buffer.commit();
126    command_buffer.wait_until_completed();
127
128    let intersection = read_struct::<IntersectionDistancePrimitiveIndex>(&intersection_buffer);
129    assert!(
130        (intersection.distance - 1.0).abs() < 1.0e-4,
131        "unexpected hit distance: {intersection:?}"
132    );
133    assert_eq!(
134        intersection.primitive_index, 0,
135        "expected to hit triangle 0"
136    );
137
138    let svgf = SVGF::new(&device).expect("svgf");
139    svgf.set_depth_weight(0.75);
140    svgf.set_normal_weight(64.0);
141    svgf.set_luminance_weight(2.5);
142    svgf.set_channel_count(3);
143    svgf.set_channel_count2(1);
144    assert!((svgf.depth_weight() - 0.75).abs() < f32::EPSILON);
145    assert!((svgf.normal_weight() - 64.0).abs() < f32::EPSILON);
146    assert!((svgf.luminance_weight() - 2.5).abs() < f32::EPSILON);
147    assert_eq!(svgf.channel_count(), 3);
148    assert_eq!(svgf.channel_count2(), 1);
149
150    println!(
151        "ray smoke passed: distance={:.3} primitive_index={} batch_size={}",
152        intersection.distance,
153        intersection.primitive_index,
154        intersector.recommended_minimum_ray_batch_size(1)
155    );
156}
Source

pub fn usage(&self) -> usize

Source

pub fn set_usage(&self, usage: usize)

Source

pub fn status(&self) -> usize

Examples found in repository?
examples/04_ray_intersection.rs (line 68)
41fn main() {
42    let device = MetalDevice::system_default().expect("no Metal device available");
43    let queue = device.new_command_queue().expect("command queue");
44
45    let vertices = [
46        [-1.0_f32, -1.0, 0.0, 0.0],
47        [1.0_f32, -1.0, 0.0, 0.0],
48        [0.0_f32, 1.0, 0.0, 0.0],
49    ];
50    let vertex_buffer = device
51        .new_buffer(
52            core::mem::size_of_val(&vertices),
53            resource_options::STORAGE_MODE_SHARED,
54        )
55        .expect("vertex buffer");
56    let _ = vertex_buffer.write_bytes(as_bytes(&vertices));
57
58    let acceleration_structure =
59        PolygonAccelerationStructure::new(&device).expect("polygon acceleration structure");
60    acceleration_structure.set_polygon_type(polygon_type::TRIANGLE);
61    acceleration_structure.set_vertex_stride(core::mem::size_of::<[f32; 4]>());
62    acceleration_structure.set_index_type(data_type::UINT32);
63    acceleration_structure.set_vertex_buffer(Some(&vertex_buffer));
64    acceleration_structure.set_index_buffer(None);
65    acceleration_structure.set_polygon_count(1);
66    acceleration_structure.rebuild();
67    assert_eq!(
68        acceleration_structure.status(),
69        acceleration_structure_status::BUILT,
70        "acceleration structure should build successfully"
71    );
72
73    let ray = PackedRayOriginDirection {
74        origin: PackedFloat3 {
75            x: 0.0,
76            y: 0.0,
77            z: 1.0,
78        },
79        direction: PackedFloat3 {
80            x: 0.0,
81            y: 0.0,
82            z: -1.0,
83        },
84    };
85    let miss = IntersectionDistancePrimitiveIndex {
86        distance: -1.0,
87        primitive_index: u32::MAX,
88    };
89
90    let ray_buffer = device
91        .new_buffer(
92            core::mem::size_of::<PackedRayOriginDirection>(),
93            resource_options::STORAGE_MODE_SHARED,
94        )
95        .expect("ray buffer");
96    let intersection_buffer = device
97        .new_buffer(
98            core::mem::size_of::<IntersectionDistancePrimitiveIndex>(),
99            resource_options::STORAGE_MODE_SHARED,
100        )
101        .expect("intersection buffer");
102    let _ = ray_buffer.write_bytes(as_bytes(&[ray]));
103    let _ = intersection_buffer.write_bytes(as_bytes(&[miss]));
104
105    let intersector = RayIntersector::new(&device).expect("ray intersector");
106    intersector.set_cull_mode(cull_mode::NONE);
107    intersector.set_ray_data_type(ray_data_type::PACKED_ORIGIN_DIRECTION);
108    intersector.set_intersection_data_type(intersection_data_type::DISTANCE_PRIMITIVE_INDEX);
109    assert!(
110        intersector.recommended_minimum_ray_batch_size(1) >= 1,
111        "recommended ray batch size should be positive"
112    );
113
114    let command_buffer = queue.new_command_buffer().expect("command buffer");
115    intersector.encode_intersection(
116        &command_buffer,
117        intersection_type::NEAREST,
118        &ray_buffer,
119        0,
120        &intersection_buffer,
121        0,
122        1,
123        &acceleration_structure,
124    );
125    command_buffer.commit();
126    command_buffer.wait_until_completed();
127
128    let intersection = read_struct::<IntersectionDistancePrimitiveIndex>(&intersection_buffer);
129    assert!(
130        (intersection.distance - 1.0).abs() < 1.0e-4,
131        "unexpected hit distance: {intersection:?}"
132    );
133    assert_eq!(
134        intersection.primitive_index, 0,
135        "expected to hit triangle 0"
136    );
137
138    let svgf = SVGF::new(&device).expect("svgf");
139    svgf.set_depth_weight(0.75);
140    svgf.set_normal_weight(64.0);
141    svgf.set_luminance_weight(2.5);
142    svgf.set_channel_count(3);
143    svgf.set_channel_count2(1);
144    assert!((svgf.depth_weight() - 0.75).abs() < f32::EPSILON);
145    assert!((svgf.normal_weight() - 64.0).abs() < f32::EPSILON);
146    assert!((svgf.luminance_weight() - 2.5).abs() < f32::EPSILON);
147    assert_eq!(svgf.channel_count(), 3);
148    assert_eq!(svgf.channel_count2(), 1);
149
150    println!(
151        "ray smoke passed: distance={:.3} primitive_index={} batch_size={}",
152        intersection.distance,
153        intersection.primitive_index,
154        intersector.recommended_minimum_ray_batch_size(1)
155    );
156}
Source

pub fn rebuild(&self)

Examples found in repository?
examples/04_ray_intersection.rs (line 66)
41fn main() {
42    let device = MetalDevice::system_default().expect("no Metal device available");
43    let queue = device.new_command_queue().expect("command queue");
44
45    let vertices = [
46        [-1.0_f32, -1.0, 0.0, 0.0],
47        [1.0_f32, -1.0, 0.0, 0.0],
48        [0.0_f32, 1.0, 0.0, 0.0],
49    ];
50    let vertex_buffer = device
51        .new_buffer(
52            core::mem::size_of_val(&vertices),
53            resource_options::STORAGE_MODE_SHARED,
54        )
55        .expect("vertex buffer");
56    let _ = vertex_buffer.write_bytes(as_bytes(&vertices));
57
58    let acceleration_structure =
59        PolygonAccelerationStructure::new(&device).expect("polygon acceleration structure");
60    acceleration_structure.set_polygon_type(polygon_type::TRIANGLE);
61    acceleration_structure.set_vertex_stride(core::mem::size_of::<[f32; 4]>());
62    acceleration_structure.set_index_type(data_type::UINT32);
63    acceleration_structure.set_vertex_buffer(Some(&vertex_buffer));
64    acceleration_structure.set_index_buffer(None);
65    acceleration_structure.set_polygon_count(1);
66    acceleration_structure.rebuild();
67    assert_eq!(
68        acceleration_structure.status(),
69        acceleration_structure_status::BUILT,
70        "acceleration structure should build successfully"
71    );
72
73    let ray = PackedRayOriginDirection {
74        origin: PackedFloat3 {
75            x: 0.0,
76            y: 0.0,
77            z: 1.0,
78        },
79        direction: PackedFloat3 {
80            x: 0.0,
81            y: 0.0,
82            z: -1.0,
83        },
84    };
85    let miss = IntersectionDistancePrimitiveIndex {
86        distance: -1.0,
87        primitive_index: u32::MAX,
88    };
89
90    let ray_buffer = device
91        .new_buffer(
92            core::mem::size_of::<PackedRayOriginDirection>(),
93            resource_options::STORAGE_MODE_SHARED,
94        )
95        .expect("ray buffer");
96    let intersection_buffer = device
97        .new_buffer(
98            core::mem::size_of::<IntersectionDistancePrimitiveIndex>(),
99            resource_options::STORAGE_MODE_SHARED,
100        )
101        .expect("intersection buffer");
102    let _ = ray_buffer.write_bytes(as_bytes(&[ray]));
103    let _ = intersection_buffer.write_bytes(as_bytes(&[miss]));
104
105    let intersector = RayIntersector::new(&device).expect("ray intersector");
106    intersector.set_cull_mode(cull_mode::NONE);
107    intersector.set_ray_data_type(ray_data_type::PACKED_ORIGIN_DIRECTION);
108    intersector.set_intersection_data_type(intersection_data_type::DISTANCE_PRIMITIVE_INDEX);
109    assert!(
110        intersector.recommended_minimum_ray_batch_size(1) >= 1,
111        "recommended ray batch size should be positive"
112    );
113
114    let command_buffer = queue.new_command_buffer().expect("command buffer");
115    intersector.encode_intersection(
116        &command_buffer,
117        intersection_type::NEAREST,
118        &ray_buffer,
119        0,
120        &intersection_buffer,
121        0,
122        1,
123        &acceleration_structure,
124    );
125    command_buffer.commit();
126    command_buffer.wait_until_completed();
127
128    let intersection = read_struct::<IntersectionDistancePrimitiveIndex>(&intersection_buffer);
129    assert!(
130        (intersection.distance - 1.0).abs() < 1.0e-4,
131        "unexpected hit distance: {intersection:?}"
132    );
133    assert_eq!(
134        intersection.primitive_index, 0,
135        "expected to hit triangle 0"
136    );
137
138    let svgf = SVGF::new(&device).expect("svgf");
139    svgf.set_depth_weight(0.75);
140    svgf.set_normal_weight(64.0);
141    svgf.set_luminance_weight(2.5);
142    svgf.set_channel_count(3);
143    svgf.set_channel_count2(1);
144    assert!((svgf.depth_weight() - 0.75).abs() < f32::EPSILON);
145    assert!((svgf.normal_weight() - 64.0).abs() < f32::EPSILON);
146    assert!((svgf.luminance_weight() - 2.5).abs() < f32::EPSILON);
147    assert_eq!(svgf.channel_count(), 3);
148    assert_eq!(svgf.channel_count2(), 1);
149
150    println!(
151        "ray smoke passed: distance={:.3} primitive_index={} batch_size={}",
152        intersection.distance,
153        intersection.primitive_index,
154        intersector.recommended_minimum_ray_batch_size(1)
155    );
156}
Source

pub fn encode_refit(&self, command_buffer: &CommandBuffer)

Trait Implementations§

Source§

impl Drop for PolygonAccelerationStructure

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Send for PolygonAccelerationStructure

Source§

impl Sync for PolygonAccelerationStructure

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.