| 1 |
use metal::*; |
| 2 |
|
| 3 |
#[repr(C)] |
| 4 |
struct Vertex { |
| 5 |
xyz: [f32; 3], |
| 6 |
} |
| 7 |
|
| 8 |
type Ray = mps::MPSRayOriginMinDistanceDirectionMaxDistance; |
| 9 |
type Intersection = mps::MPSIntersectionDistancePrimitiveIndexCoordinates; |
| 10 |
|
| 11 |
// Original example taken from https://sergeyreznik.github.io/metal-ray-tracer/part-1/index.html |
| 12 |
fn main() { |
| 13 |
let device = Device::system_default().expect("No device found"); |
| 14 |
|
| 15 |
let library_path = |
| 16 |
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("examples/mps/shaders.metallib"); |
| 17 |
let library = device |
| 18 |
.new_library_with_file(library_path) |
| 19 |
.expect("Failed to load shader library"); |
| 20 |
|
| 21 |
let generate_rays_pipeline = create_pipeline("generateRays", &library, &device); |
| 22 |
|
| 23 |
let queue = device.new_command_queue(); |
| 24 |
let command_buffer = queue.new_command_buffer(); |
| 25 |
|
| 26 |
// Simple vertex/index buffer data |
| 27 |
|
| 28 |
let vertices = [ |
| 29 |
Vertex { |
| 30 |
xyz: [0.25, 0.25, 0.0], |
| 31 |
}, |
| 32 |
Vertex { |
| 33 |
xyz: [0.75, 0.25, 0.0], |
| 34 |
}, |
| 35 |
Vertex { |
| 36 |
xyz: [0.50, 0.75, 0.0], |
| 37 |
}, |
| 38 |
]; |
| 39 |
|
| 40 |
let indices = [0, 1, 2]; |
| 41 |
|
| 42 |
// Vertex data should be stored in private or managed buffers on discrete GPU systems (AMD, NVIDIA). |
| 43 |
// Private buffers are stored entirely in GPU memory and cannot be accessed by the CPU. Managed |
| 44 |
// buffers maintain a copy in CPU memory and a copy in GPU memory. |
| 45 |
let buffer_opts = MTLResourceOptions::StorageModeManaged; |
| 46 |
|
| 47 |
let vertex_buffer = device.new_buffer_with_data( |
| 48 |
vertices.as_ptr().cast(), |
| 49 |
size_of_val(&vertices) as u64, |
| 50 |
buffer_opts, |
| 51 |
); |
| 52 |
|
| 53 |
let index_buffer = device.new_buffer_with_data( |
| 54 |
indices.as_ptr().cast(), |
| 55 |
size_of_val(&indices) as u64, |
| 56 |
buffer_opts, |
| 57 |
); |
| 58 |
|
| 59 |
// Build an acceleration structure using our vertex and index buffers containing the single triangle. |
| 60 |
let acceleration_structure = mps::TriangleAccelerationStructure::from_device(&device) |
| 61 |
.expect("Failed to create acceleration structure"); |
| 62 |
|
| 63 |
acceleration_structure.set_vertex_buffer(Some(&vertex_buffer)); |
| 64 |
acceleration_structure.set_vertex_stride(size_of::<Vertex>() as u64); |
| 65 |
acceleration_structure.set_index_buffer(Some(&index_buffer)); |
| 66 |
acceleration_structure.set_index_type(mps::MPSDataType::UInt32); |
| 67 |
acceleration_structure.set_triangle_count(1); |
| 68 |
acceleration_structure.set_usage(mps::MPSAccelerationStructureUsage::None); |
| 69 |
acceleration_structure.rebuild(); |
| 70 |
|
| 71 |
let ray_intersector = |
| 72 |
mps::RayIntersector::from_device(&device).expect("Failed to create ray intersector"); |
| 73 |
|
| 74 |
ray_intersector.set_ray_stride(size_of::<Ray>() as u64); |
| 75 |
ray_intersector.set_ray_data_type(mps::MPSRayDataType::OriginMinDistanceDirectionMaxDistance); |
| 76 |
ray_intersector.set_intersection_stride(size_of::<Intersection>() as u64); |
| 77 |
ray_intersector.set_intersection_data_type( |
| 78 |
mps::MPSIntersectionDataType::DistancePrimitiveIndexCoordinates, |
| 79 |
); |
| 80 |
|
| 81 |
// Create a buffer to hold generated rays and intersection results |
| 82 |
let ray_count = 1024; |
| 83 |
let ray_buffer = device.new_buffer( |
| 84 |
(size_of::<Ray>() * ray_count) as u64, |
| 85 |
MTLResourceOptions::StorageModePrivate, |
| 86 |
); |
| 87 |
|
| 88 |
let intersection_buffer = device.new_buffer( |
| 89 |
(size_of::<Intersection>() * ray_count) as u64, |
| 90 |
MTLResourceOptions::StorageModePrivate, |
| 91 |
); |
| 92 |
|
| 93 |
// Run the compute shader to generate rays |
| 94 |
let encoder = command_buffer.new_compute_command_encoder(); |
| 95 |
encoder.set_buffer(0, Some(&ray_buffer), 0); |
| 96 |
encoder.set_compute_pipeline_state(&generate_rays_pipeline); |
| 97 |
encoder.dispatch_thread_groups( |
| 98 |
MTLSize { |
| 99 |
width: 4, |
| 100 |
height: 4, |
| 101 |
depth: 1, |
| 102 |
}, |
| 103 |
MTLSize { |
| 104 |
width: 8, |
| 105 |
height: 8, |
| 106 |
depth: 1, |
| 107 |
}, |
| 108 |
); |
| 109 |
encoder.end_encoding(); |
| 110 |
|
| 111 |
// Intersect rays with triangles inside acceleration structure |
| 112 |
ray_intersector.encode_intersection_to_command_buffer( |
| 113 |
&command_buffer, |
| 114 |
mps::MPSIntersectionType::Nearest, |
| 115 |
&ray_buffer, |
| 116 |
0, |
| 117 |
&intersection_buffer, |
| 118 |
0, |
| 119 |
ray_count as u64, |
| 120 |
&acceleration_structure, |
| 121 |
); |
| 122 |
|
| 123 |
command_buffer.commit(); |
| 124 |
command_buffer.wait_until_completed(); |
| 125 |
|
| 126 |
println!("Done"); |
| 127 |
} |
| 128 |
|
| 129 |
fn create_pipeline(func: &str, library: &LibraryRef, device: &DeviceRef) -> ComputePipelineState { |
| 130 |
// Create compute pipelines will will execute code on the GPU |
| 131 |
let compute_descriptor = ComputePipelineDescriptor::new(); |
| 132 |
|
| 133 |
// Set to YES to allow compiler to make certain optimizations |
| 134 |
compute_descriptor.set_thread_group_size_is_multiple_of_thread_execution_width(true); |
| 135 |
|
| 136 |
let function = library.get_function(func, None).unwrap(); |
| 137 |
compute_descriptor.set_compute_function(Some(&function)); |
| 138 |
|
| 139 |
let pipeline = device |
| 140 |
.new_compute_pipeline_state(&compute_descriptor) |
| 141 |
.unwrap(); |
| 142 |
|
| 143 |
pipeline |
| 144 |
} |