Skip to content

Commit

Permalink
Vastly simplify interface
Browse files Browse the repository at this point in the history
  • Loading branch information
cwfitzgerald committed Dec 16, 2022
1 parent 84c0217 commit 6ddc493
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 100 deletions.
10 changes: 2 additions & 8 deletions wgpu-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,22 +925,16 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.map_err(|_| InvalidAdapter)
}

pub fn adapter_correlate_presentation_timestamp<A: HalApi>(
pub fn adapter_get_presentation_timestamp<A: HalApi>(
&self,
adapter_id: AdapterId,
user_timestamp_function: &mut dyn FnMut(),
) -> Result<wgt::PresentationTimestamp, InvalidAdapter> {
let hub = A::hub(self);
let mut token = Token::root();
let (adapter_guard, _) = hub.adapters.read(&mut token);
let adapter = adapter_guard.get(adapter_id).map_err(|_| InvalidAdapter)?;

Ok(unsafe {
adapter
.raw
.adapter
.correlate_presentation_timestamp(user_timestamp_function)
})
Ok(unsafe { adapter.raw.adapter.get_presentation_timestamp() })
}

pub fn adapter_drop<A: HalApi>(&self, adapter_id: AdapterId) {
Expand Down
5 changes: 1 addition & 4 deletions wgpu-hal/src/dx11/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ impl crate::Adapter<super::Api> for super::Adapter {
todo!()
}

unsafe fn correlate_presentation_timestamp(
&self,
user_timestamp_function: &mut dyn FnMut(),
) -> wgt::PresentationTimestamp {
unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp {
todo!()
}
}
Expand Down
6 changes: 1 addition & 5 deletions wgpu-hal/src/dx12/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,7 @@ impl crate::Adapter<super::Api> for super::Adapter {
})
}

unsafe fn correlate_presentation_timestamp(
&self,
user_timestamp_function: &mut dyn FnMut(),
) -> wgt::PresentationTimestamp {
user_timestamp_function();
unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp {
wgt::PresentationTimestamp(self.presentation_timer.get_timestamp_ns())
}
}
6 changes: 1 addition & 5 deletions wgpu-hal/src/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,7 @@ impl crate::Adapter<Api> for Context {
None
}

unsafe fn correlate_presentation_timestamp(
&self,
user_timestamp_function: &mut dyn FnMut(),
) -> wgt::PresentationTimestamp {
user_timestamp_function();
unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp {
wgt::PresentationTimestamp::INVALID_TIMESTAMP
}
}
Expand Down
6 changes: 1 addition & 5 deletions wgpu-hal/src/gles/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -859,11 +859,7 @@ impl crate::Adapter<super::Api> for super::Adapter {
}
}

unsafe fn correlate_presentation_timestamp(
&self,
user_timestamp_function: &mut dyn FnMut(),
) -> wgt::PresentationTimestamp {
user_timestamp_function();
unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp {
wgt::PresentationTimestamp::INVALID_TIMESTAMP
}
}
Expand Down
13 changes: 4 additions & 9 deletions wgpu-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,10 @@ pub trait Adapter<A: Api>: Send + Sync {
/// `None` means presentation is not supported for it.
unsafe fn surface_capabilities(&self, surface: &A::Surface) -> Option<SurfaceCapabilities>;

/// Creates a [PresentationTimestamp](wgt::PresentationTimestamp) then immediately calls the user_timestamp_function,
/// allowing the user to correlate the presentation timestamp with another set of timestamps (like `Instant`s).
///
/// While user_timestamp_function is an FnMut, this is because of higher level internal details. It will only ever be called once
/// and it will unconditionally be called.
unsafe fn correlate_presentation_timestamp(
&self,
user_timestamp_function: &mut dyn FnMut(),
) -> wgt::PresentationTimestamp;
/// Creates a [`PresentationTimestamp`] using the adapter's WSI.
///
/// [`PresentationTimestamp`]: wgt::PresentationTimestamp
unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp;
}

pub trait Device<A: Api>: Send + Sync {
Expand Down
6 changes: 1 addition & 5 deletions wgpu-hal/src/metal/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,7 @@ impl crate::Adapter<super::Api> for super::Adapter {
})
}

unsafe fn correlate_presentation_timestamp(
&self,
user_tiemstamp_function: &mut dyn FnMut(),
) -> wgt::PresentationTimestamp {
user_tiemstamp_function();
unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp {
let timestamp = self.shared.presentation_timer.get_timestamp_ns();

wgt::PresentationTimestamp(timestamp)
Expand Down
7 changes: 1 addition & 6 deletions wgpu-hal/src/vulkan/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1583,10 +1583,7 @@ impl crate::Adapter<super::Api> for super::Adapter {
})
}

unsafe fn correlate_presentation_timestamp(
&self,
user_timestamp_function: &mut dyn FnMut(),
) -> wgt::PresentationTimestamp {
unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp {
// VK_GOOGLE_display_timing is the only way to get presentation
// timestamps on vulkan right now and it is only ever available
// on android and linux. This includes mac, but there's no alternative
Expand All @@ -1597,7 +1594,6 @@ impl crate::Adapter<super::Api> for super::Adapter {
tv_sec: 0,
tv_nsec: 0,
};
user_timestamp_function();
unsafe {
libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut timespec);
}
Expand All @@ -1608,7 +1604,6 @@ impl crate::Adapter<super::Api> for super::Adapter {
}
#[cfg(not(unix))]
{
user_timestamp_function();
wgt::PresentationTimestamp::INVALID_TIMESTAMP
}
}
Expand Down
25 changes: 4 additions & 21 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1063,30 +1063,13 @@ impl crate::Context for Context {
}
}

fn adapter_correlate_presentation_timestamp<F, T>(
fn adapter_get_presentation_timestamp(
&self,
adapter: &Self::AdapterId,
user_timestamp_function: F,
) -> (wgt::PresentationTimestamp, T)
where
F: FnOnce() -> T,
{
// Turns a FnOnce into an FnMut by panicing if it's ever called twice
let mut result = None;
let mut optional_function = Some(user_timestamp_function);
let mut mutable_function = || {
result = Some(optional_function.take().expect(
"correlate_presentation_timestamp function called more than once",
)())
};

) -> wgt::PresentationTimestamp {
let global = &self.0;
match wgc::gfx_select!(*adapter => global.adapter_correlate_presentation_timestamp(*adapter, &mut mutable_function))
{
Ok(timestamp) => (
timestamp,
result.expect("correlate_presentation_timestamp function never called"),
),
match wgc::gfx_select!(*adapter => global.adapter_get_presentation_timestamp(*adapter)) {
Ok(timestamp) => timestamp,
Err(err) => self.handle_error_fatal(err, "Adapter::correlate_presentation_timestamp"),
}
}
Expand Down
13 changes: 3 additions & 10 deletions wgpu/src/backend/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1322,18 +1322,11 @@ impl crate::Context for Context {
format.describe().guaranteed_format_features
}

fn adapter_correlate_presentation_timestamp<F, T>(
fn adapter_get_presentation_timestamp(
&self,
_adapter: &Self::AdapterId,
user_timestamp_function: F,
) -> (wgt::PresentationTimestamp, T)
where
F: FnOnce() -> T,
{
(
wgt::PresentationTimestamp::INVALID_TIMESTAMP,
user_timestamp_function(),
)
) -> wgt::PresentationTimestamp {
wgt::PresentationTimestamp::INVALID_TIMESTAMP
}

fn surface_get_capabilities(
Expand Down
32 changes: 10 additions & 22 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,10 @@ trait Context: Debug + Send + Sized + Sync {
adapter: &Self::AdapterId,
format: TextureFormat,
) -> TextureFormatFeatures;
fn adapter_correlate_presentation_timestamp<F, T>(
fn adapter_get_presentation_timestamp(
&self,
adapter: &Self::AdapterId,
function: F,
) -> (PresentationTimestamp, T)
where
F: FnOnce() -> T;
) -> PresentationTimestamp;

fn surface_get_capabilities(
&self,
Expand Down Expand Up @@ -2087,18 +2084,19 @@ impl Adapter {
Context::adapter_get_texture_format_features(&*self.context, &self.id, format)
}

/// Correleates presentation timestamps with a given user timestamp by generating a presentation timestamp
/// immediately after calling the user's timestamp function.
/// Generates a timestamp using the clock used by the presentation engine.
///
/// When comparing completely opaque timestamp systems, we need a way of generating timestamps that signal
/// the exact same time. This function allows you to generate a timestamp (such as an [Instant]) at the
/// exact same time as a `PresentationTimestamp` is generated, allowing you to translate from one to another.
/// the exact same time. You can do this by calling your own timestamp function immediately after a call to
/// this function. This should result in timestamps that are 0.5 to 5 microseconds apart. There are locks
/// that must be taken during the call, so don't call your function before.
///
/// ```no_run
/// # let adapter: wgpu::Adapter = panic!();
/// # let some_code = || wgpu::PresentationTimestamp::INVALID_TIMESTAMP;
/// use std::time::{Duration, Instant};
/// let (presentation, instant) = adapter.correlate_presentation_timestamp(Instant::now);
/// let presentation = adapter.get_presentation_timestamp();
/// let instant = Instant::now();
///
/// // We can now turn a new presentation timestamp into an Instant.
/// let some_pres_timestamp = some_code();
Expand All @@ -2107,18 +2105,8 @@ impl Adapter {
/// ```
//
/// [Instant]: std::time::Instant
pub fn correlate_presentation_timestamp<F, T>(
&self,
user_timestamp_function: F,
) -> (PresentationTimestamp, T)
where
F: FnOnce() -> T,
{
Context::adapter_correlate_presentation_timestamp(
&*self.context,
&self.id,
user_timestamp_function,
)
pub fn get_presentation_timestamp(&self) -> PresentationTimestamp {
Context::adapter_get_presentation_timestamp(&*self.context, &self.id)
}
}

Expand Down

0 comments on commit 6ddc493

Please sign in to comment.