Added is_surface_supported (#1763)

This commit is contained in:
Lonsdaleiter
2021-08-04 07:27:08 -07:00
committed by GitHub
parent 5d9c276c82
commit 64ffdd52ad
4 changed files with 57 additions and 0 deletions

View File

@@ -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,

View File

@@ -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,
@@ -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")]

View File

@@ -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,

View File

@@ -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,
@@ -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.