| 1 |
use std::path::PathBuf; |
| 2 |
|
| 3 |
use metal::*; |
| 4 |
use objc::rc::autoreleasepool; |
| 5 |
|
| 6 |
const NUM_SAMPLES: u64 = 2; |
| 7 |
|
| 8 |
fn main() { |
| 9 |
let num_elements = std::env::args() |
| 10 |
.nth(1) |
| 11 |
.map(|s| s.parse::<u32>().unwrap()) |
| 12 |
.unwrap_or(64 * 64); |
| 13 |
|
| 14 |
autoreleasepool(|| { |
| 15 |
let device = Device::system_default().expect("No device found"); |
| 16 |
let mut cpu_start = 0; |
| 17 |
let mut gpu_start = 0; |
| 18 |
device.sample_timestamps(&mut cpu_start, &mut gpu_start); |
| 19 |
|
| 20 |
let counter_sample_buffer = create_counter_sample_buffer(&device); |
| 21 |
let destination_buffer = device.new_buffer( |
| 22 |
(size_of::<u64>() * NUM_SAMPLES as usize) as u64, |
| 23 |
MTLResourceOptions::StorageModeShared, |
| 24 |
); |
| 25 |
|
| 26 |
let counter_sampling_point = MTLCounterSamplingPoint::AtStageBoundary; |
| 27 |
assert!(device.supports_counter_sampling(counter_sampling_point)); |
| 28 |
|
| 29 |
let command_queue = device.new_command_queue(); |
| 30 |
let command_buffer = command_queue.new_command_buffer(); |
| 31 |
|
| 32 |
let compute_pass_descriptor = ComputePassDescriptor::new(); |
| 33 |
handle_compute_pass_sample_buffer_attachment( |
| 34 |
compute_pass_descriptor, |
| 35 |
&counter_sample_buffer, |
| 36 |
); |
| 37 |
let encoder = |
| 38 |
command_buffer.compute_command_encoder_with_descriptor(compute_pass_descriptor); |
| 39 |
|
| 40 |
let pipeline_state = create_pipeline_state(&device); |
| 41 |
encoder.set_compute_pipeline_state(&pipeline_state); |
| 42 |
|
| 43 |
let (buffer, sum) = create_input_and_output_buffers(&device, num_elements); |
| 44 |
encoder.set_buffer(0, Some(&buffer), 0); |
| 45 |
encoder.set_buffer(1, Some(&sum), 0); |
| 46 |
|
| 47 |
let num_threads = pipeline_state.thread_execution_width(); |
| 48 |
|
| 49 |
let thread_group_count = MTLSize { |
| 50 |
width: ((num_elements as NSUInteger + num_threads) / num_threads), |
| 51 |
height: 1, |
| 52 |
depth: 1, |
| 53 |
}; |
| 54 |
|
| 55 |
let thread_group_size = MTLSize { |
| 56 |
width: num_threads, |
| 57 |
height: 1, |
| 58 |
depth: 1, |
| 59 |
}; |
| 60 |
|
| 61 |
encoder.dispatch_thread_groups(thread_group_count, thread_group_size); |
| 62 |
encoder.end_encoding(); |
| 63 |
|
| 64 |
resolve_samples_into_buffer(command_buffer, &counter_sample_buffer, &destination_buffer); |
| 65 |
|
| 66 |
command_buffer.commit(); |
| 67 |
command_buffer.wait_until_completed(); |
| 68 |
let mut cpu_end = 0; |
| 69 |
let mut gpu_end = 0; |
| 70 |
device.sample_timestamps(&mut cpu_end, &mut gpu_end); |
| 71 |
|
| 72 |
let sum = unsafe { *sum.contents().cast::<u32>() }; |
| 73 |
println!("Compute shader sum: {}", sum); |
| 74 |
|
| 75 |
assert_eq!(num_elements, sum); |
| 76 |
|
| 77 |
handle_timestamps(&destination_buffer, cpu_start, cpu_end, gpu_start, gpu_end); |
| 78 |
}); |
| 79 |
} |
| 80 |
|
| 81 |
fn create_pipeline_state(device: &Device) -> ComputePipelineState { |
| 82 |
let library_path = |
| 83 |
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("examples/compute/shaders.metallib"); |
| 84 |
let library = device.new_library_with_file(library_path).unwrap(); |
| 85 |
let kernel = library.get_function("sum", None).unwrap(); |
| 86 |
|
| 87 |
let pipeline_state_descriptor = ComputePipelineDescriptor::new(); |
| 88 |
pipeline_state_descriptor.set_compute_function(Some(&kernel)); |
| 89 |
|
| 90 |
device |
| 91 |
.new_compute_pipeline_state_with_function( |
| 92 |
pipeline_state_descriptor.compute_function().unwrap(), |
| 93 |
) |
| 94 |
.unwrap() |
| 95 |
} |
| 96 |
|
| 97 |
fn handle_compute_pass_sample_buffer_attachment( |
| 98 |
compute_pass_descriptor: &ComputePassDescriptorRef, |
| 99 |
counter_sample_buffer: &CounterSampleBufferRef, |
| 100 |
) { |
| 101 |
let sample_buffer_attachment_descriptor = compute_pass_descriptor |
| 102 |
.sample_buffer_attachments() |
| 103 |
.object_at(0) |
| 104 |
.unwrap(); |
| 105 |
|
| 106 |
sample_buffer_attachment_descriptor.set_sample_buffer(counter_sample_buffer); |
| 107 |
sample_buffer_attachment_descriptor.set_start_of_encoder_sample_index(0); |
| 108 |
sample_buffer_attachment_descriptor.set_end_of_encoder_sample_index(1); |
| 109 |
} |
| 110 |
|
| 111 |
fn resolve_samples_into_buffer( |
| 112 |
command_buffer: &CommandBufferRef, |
| 113 |
counter_sample_buffer: &CounterSampleBufferRef, |
| 114 |
destination_buffer: &BufferRef, |
| 115 |
) { |
| 116 |
let blit_encoder = command_buffer.new_blit_command_encoder(); |
| 117 |
blit_encoder.resolve_counters( |
| 118 |
counter_sample_buffer, |
| 119 |
crate::NSRange::new(0u64, NUM_SAMPLES), |
| 120 |
destination_buffer, |
| 121 |
0u64, |
| 122 |
); |
| 123 |
blit_encoder.end_encoding(); |
| 124 |
} |
| 125 |
|
| 126 |
fn handle_timestamps( |
| 127 |
resolved_sample_buffer: &BufferRef, |
| 128 |
cpu_start: u64, |
| 129 |
cpu_end: u64, |
| 130 |
gpu_start: u64, |
| 131 |
gpu_end: u64, |
| 132 |
) { |
| 133 |
let samples = unsafe { |
| 134 |
std::slice::from_raw_parts( |
| 135 |
resolved_sample_buffer.contents().cast::<u64>(), |
| 136 |
NUM_SAMPLES as usize, |
| 137 |
) |
| 138 |
}; |
| 139 |
let pass_start = samples[0]; |
| 140 |
let pass_end = samples[1]; |
| 141 |
|
| 142 |
let cpu_time_span = cpu_end - cpu_start; |
| 143 |
let gpu_time_span = gpu_end - gpu_start; |
| 144 |
|
| 145 |
let micros = microseconds_between_begin(pass_start, pass_end, gpu_time_span, cpu_time_span); |
| 146 |
println!("Compute pass duration: {} µs", micros); |
| 147 |
} |
| 148 |
|
| 149 |
fn create_counter_sample_buffer(device: &Device) -> CounterSampleBuffer { |
| 150 |
let counter_sample_buffer_desc = metal::CounterSampleBufferDescriptor::new(); |
| 151 |
counter_sample_buffer_desc.set_storage_mode(metal::MTLStorageMode::Shared); |
| 152 |
counter_sample_buffer_desc.set_sample_count(NUM_SAMPLES); |
| 153 |
let counter_sets = device.counter_sets(); |
| 154 |
|
| 155 |
let timestamp_counter = counter_sets.iter().find(|cs| cs.name() == "timestamp"); |
| 156 |
|
| 157 |
counter_sample_buffer_desc |
| 158 |
.set_counter_set(timestamp_counter.expect("No timestamp counter found")); |
| 159 |
|
| 160 |
device |
| 161 |
.new_counter_sample_buffer_with_descriptor(&counter_sample_buffer_desc) |
| 162 |
.unwrap() |
| 163 |
} |
| 164 |
|
| 165 |
fn create_input_and_output_buffers( |
| 166 |
device: &Device, |
| 167 |
num_elements: u32, |
| 168 |
) -> (metal::Buffer, metal::Buffer) { |
| 169 |
let data = vec![1u32; num_elements as usize]; |
| 170 |
|
| 171 |
let buffer = device.new_buffer_with_data( |
| 172 |
data.as_ptr().cast(), |
| 173 |
size_of_val(data.as_slice()) as u64, |
| 174 |
MTLResourceOptions::CPUCacheModeDefaultCache, |
| 175 |
); |
| 176 |
|
| 177 |
let sum = { |
| 178 |
let data = [0u32]; |
| 179 |
device.new_buffer_with_data( |
| 180 |
data.as_ptr().cast(), |
| 181 |
size_of_val(&data) as u64, |
| 182 |
MTLResourceOptions::CPUCacheModeDefaultCache, |
| 183 |
) |
| 184 |
}; |
| 185 |
(buffer, sum) |
| 186 |
} |
| 187 |
|
| 188 |
/// <https://developer.apple.com/documentation/metal/gpu_counters_and_counter_sample_buffers/converting_gpu_timestamps_into_cpu_time> |
| 189 |
fn microseconds_between_begin(begin: u64, end: u64, gpu_time_span: u64, cpu_time_span: u64) -> f64 { |
| 190 |
let time_span = (end as f64) - (begin as f64); |
| 191 |
let nanoseconds = time_span / (gpu_time_span as f64) * (cpu_time_span as f64); |
| 192 |
nanoseconds / 1000.0 |
| 193 |
} |