Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metal: implement get_timestamp_period #2528

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions wgpu-hal/src/metal/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl crate::Adapter<super::Api> for super::Adapter {
},
queue: super::Queue {
raw: Arc::new(Mutex::new(queue)),
shared: Arc::clone(&self.shared),
},
})
}
Expand Down Expand Up @@ -733,6 +734,7 @@ impl super::PrivateCapabilities {
} else {
None
},
supports_timestamp_period: version.at_least((10, 15), (14, 0)),
}
}

Expand Down
24 changes: 22 additions & 2 deletions wgpu-hal/src/metal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ struct PrivateCapabilities {
supports_depth_clip_control: bool,
supports_preserve_invariance: bool,
has_unified_memory: Option<bool>,
supports_timestamp_period: bool,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -274,6 +275,7 @@ pub struct Adapter {

pub struct Queue {
raw: Arc<Mutex<mtl::CommandQueue>>,
shared: Arc<AdapterShared>,
}

unsafe impl Send for Queue {}
Expand Down Expand Up @@ -388,8 +390,26 @@ impl crate::Queue<Api> for Queue {
}

unsafe fn get_timestamp_period(&self) -> f32 {
// TODO: This is hard, see https://github.com/gpuweb/gpuweb/issues/1325
1.0
if !self.shared.private_caps.supports_timestamp_period {
return 1.0;
}

use objc::{msg_send, sel, sel_impl};
let (mut cpu_timestamp0, mut gpu_timestamp0) = (0_u64, 0_u64);
let device = self.shared.device.lock().as_ptr();
let () = msg_send![device, sampleTimestamps:&mut cpu_timestamp0 gpuTimestamp:&mut gpu_timestamp0];
if cpu_timestamp0 == 0 || gpu_timestamp0 == 0 {
return 1.0;
}

let queue = &self.raw.lock();
let command_buffer = queue.new_command_buffer();
command_buffer.commit();
command_buffer.wait_until_scheduled();
let (mut cpu_timestamp1, mut gpu_timestamp1) = (0_u64, 0_u64);
let () = msg_send![device, sampleTimestamps:&mut cpu_timestamp1 gpuTimestamp:&mut gpu_timestamp1];

(cpu_timestamp1 - cpu_timestamp0) as f32 / (gpu_timestamp1 - gpu_timestamp0) as f32
}
}

Expand Down