pub struct RayIntersector { /* private fields */ }Implementations§
Source§impl RayIntersector
impl RayIntersector
Sourcepub fn new(device: &MetalDevice) -> Option<Self>
pub fn new(device: &MetalDevice) -> Option<Self>
Examples found in repository?
examples/04_ray_intersection.rs (line 105)
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}pub fn cull_mode(&self) -> usize
Sourcepub fn set_cull_mode(&self, cull_mode: usize)
pub fn set_cull_mode(&self, cull_mode: usize)
Examples found in repository?
examples/04_ray_intersection.rs (line 106)
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}pub fn front_facing_winding(&self) -> usize
pub fn set_front_facing_winding(&self, winding: usize)
pub fn ray_stride(&self) -> usize
pub fn set_ray_stride(&self, stride: usize)
pub fn intersection_stride(&self) -> usize
pub fn set_intersection_stride(&self, stride: usize)
pub fn ray_data_type(&self) -> usize
Sourcepub fn set_ray_data_type(&self, data_type: usize)
pub fn set_ray_data_type(&self, data_type: usize)
Examples found in repository?
examples/04_ray_intersection.rs (line 107)
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}pub fn intersection_data_type(&self) -> usize
Sourcepub fn set_intersection_data_type(&self, data_type: usize)
pub fn set_intersection_data_type(&self, data_type: usize)
Examples found in repository?
examples/04_ray_intersection.rs (line 108)
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}Sourcepub fn recommended_minimum_ray_batch_size(&self, ray_count: usize) -> usize
pub fn recommended_minimum_ray_batch_size(&self, ray_count: usize) -> usize
Examples found in repository?
examples/04_ray_intersection.rs (line 110)
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}Sourcepub fn encode_intersection(
&self,
command_buffer: &CommandBuffer,
intersection_type: usize,
ray_buffer: &MetalBuffer,
ray_buffer_offset: usize,
intersection_buffer: &MetalBuffer,
intersection_buffer_offset: usize,
ray_count: usize,
acceleration_structure: &PolygonAccelerationStructure,
)
pub fn encode_intersection( &self, command_buffer: &CommandBuffer, intersection_type: usize, ray_buffer: &MetalBuffer, ray_buffer_offset: usize, intersection_buffer: &MetalBuffer, intersection_buffer_offset: usize, ray_count: usize, acceleration_structure: &PolygonAccelerationStructure, )
Examples found in repository?
examples/04_ray_intersection.rs (lines 115-124)
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}Trait Implementations§
Source§impl Drop for RayIntersector
impl Drop for RayIntersector
impl Send for RayIntersector
impl Sync for RayIntersector
Auto Trait Implementations§
impl Freeze for RayIntersector
impl RefUnwindSafe for RayIntersector
impl Unpin for RayIntersector
impl UnsafeUnpin for RayIntersector
impl UnwindSafe for RayIntersector
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more