Skip to content

Commit

Permalink
Added is_surface_supported (#1763)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lonsdaleiter authored Aug 4, 2021
1 parent 5d9c276 commit 64ffdd5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
18 changes: 18 additions & 0 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2550,6 +2550,24 @@ impl<G: GlobalIdentityHandlerFactory> ImplicitPipelineIds<'_, G> {
}

impl<G: GlobalIdentityHandlerFactory> Global<G> {
pub fn adapter_is_surface_supported<A: HalApi>(
&self,
adapter_id: id::AdapterId,
surface_id: id::SurfaceId,
) -> Result<bool, instance::IsSurfaceSupportedError> {
let hub = A::hub(self);
let mut token = Token::root();

let (mut surface_guard, mut token) = self.surfaces.write(&mut token);
let (adapter_guard, mut _token) = hub.adapters.read(&mut token);
let adapter = adapter_guard
.get(adapter_id)
.map_err(|_| instance::IsSurfaceSupportedError::InvalidAdapter)?;
let surface = surface_guard
.get_mut(surface_id)
.map_err(|_| instance::IsSurfaceSupportedError::InvalidSurface)?;
Ok(adapter.is_surface_supported(surface))
}
pub fn adapter_get_swap_chain_preferred_format<A: HalApi>(
&self,
adapter_id: id::AdapterId,
Expand Down
17 changes: 17 additions & 0 deletions wgpu-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ impl<A: HalApi> Adapter<A> {
}
}

pub fn is_surface_supported(&self, surface: &mut Surface) -> bool {
unsafe {
self.raw
.adapter
.surface_capabilities(A::get_surface_mut(surface))
}
.is_some()
}

pub fn get_swap_chain_preferred_format(
&self,
surface: &mut Surface,
Expand Down Expand Up @@ -344,6 +353,14 @@ impl<A: hal::Api> crate::hub::Resource for Adapter<A> {
}
}

#[derive(Clone, Debug, Error)]
pub enum IsSurfaceSupportedError {
#[error("invalid adapter")]
InvalidAdapter,
#[error("invalid surface")]
InvalidSurface,
}

#[derive(Clone, Debug, Error)]
pub enum GetSwapChainPreferredFormatError {
#[error("no suitable format found")]
Expand Down
12 changes: 12 additions & 0 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,18 @@ impl crate::Context for Context {
ready(Ok((device, device_id)))
}

fn adapter_is_surface_supported(
&self,
adapter: &Self::AdapterId,
surface: &Self::SurfaceId,
) -> bool {
let global = &self.0;
match wgc::gfx_select!(adapter => global.adapter_is_surface_supported(*adapter, *surface)) {
Ok(result) => result,
Err(err) => self.handle_error_fatal(err, "Adapter::is_surface_supported"),
}
}

fn adapter_get_swap_chain_preferred_format(
&self,
adapter: &Self::AdapterId,
Expand Down
10 changes: 10 additions & 0 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ trait Context: Debug + Send + Sized + Sync {
trace_dir: Option<&std::path::Path>,
) -> Self::RequestDeviceFuture;
fn instance_poll_all_devices(&self, force_wait: bool);
fn adapter_is_surface_supported(
&self,
adapter: &Self::AdapterId,
surface: &Self::SurfaceId,
) -> bool;
fn adapter_get_swap_chain_preferred_format(
&self,
adapter: &Self::AdapterId,
Expand Down Expand Up @@ -1564,6 +1569,11 @@ impl Adapter {
})
}

/// Returns whether this adapter may present to the passed surface.
pub fn is_surface_supported(&self, surface: &Surface) -> bool {
Context::adapter_is_surface_supported(&*self.context, &self.id, &surface.id)
}

/// Returns an optimal texture format to use for the [`SwapChain`] with this adapter.
///
/// Returns None if the surface is incompatible with the adapter.
Expand Down

0 comments on commit 64ffdd5

Please sign in to comment.