Debian Codegraph @

third_party/rust/metal/examples/events/main.rs

raw
1 // Copyright 2020 GFX developers
2 //
3 // Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4 // http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5 // http://opensource.org/licenses/MIT>, at your option. This file may not be
6 // copied, modified, or distributed except according to those terms.
7
8 use dispatch::{Queue, QueueAttribute};
9 use metal::*;
10
11 /// This example replicates `Synchronizing Events Between a GPU and the CPU` article.
12 /// See https://developer.apple.com/documentation/metal/synchronization/synchronizing_events_between_a_gpu_and_the_cpu
13 fn main() {
14 let device = Device::system_default().expect("No device found");
15
16 let command_queue = device.new_command_queue();
17 let command_buffer = command_queue.new_command_buffer();
18
19 // Shareable event
20 let shared_event = device.new_shared_event();
21
22 // Shareable event listener
23 let my_queue = Queue::create(
24 "com.example.apple-samplecode.MyQueue",
25 QueueAttribute::Serial,
26 );
27
28 // Enable `dispatch` feature to use dispatch queues,
29 // otherwise unsafe `from_queue_handle` is available for use with native APIs.
30 let shared_event_listener = SharedEventListener::from_queue(&my_queue);
31
32 // Register CPU work
33 let notify_block = block::ConcreteBlock::new(move |evt: &SharedEventRef, val: u64| {
34 println!("Got notification from GPU: {}", val);
35 evt.set_signaled_value(3);
36 });
37
38 shared_event.notify(&shared_event_listener, 2, notify_block.copy());
39
40 // Encode GPU work
41 command_buffer.encode_signal_event(&shared_event, 1);
42 command_buffer.encode_signal_event(&shared_event, 2);
43 command_buffer.encode_wait_for_event(&shared_event, 3);
44
45 command_buffer.commit();
46
47 command_buffer.wait_until_completed();
48
49 println!("Done");
50 }