Skip to content

Commit

Permalink
Implement extensions interface as described in #691
Browse files Browse the repository at this point in the history
  • Loading branch information
cwfitzgerald committed Jun 5, 2020
1 parent 581863a commit 08d857b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 10 deletions.
7 changes: 6 additions & 1 deletion player/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,11 @@ fn main() {
global.instance_create_surface(&window, wgc::id::TypedId::zip(0, 1, wgt::Backend::Empty));

let device = match actions.pop() {
Some(trace::Action::Init { desc, backend }) => {
Some(trace::Action::Init {
desc,
unsafe_extensions,
backend,
}) => {
log::info!("Initializing the device for backend: {:?}", backend);
let adapter = global
.pick_adapter(
Expand All @@ -509,6 +513,7 @@ fn main() {
gfx_select!(adapter => global.adapter_request_device(
adapter,
&desc,
unsafe_extensions,
None,
wgc::id::TypedId::zip(1, 0, wgt::Backend::Empty)
))
Expand Down
2 changes: 2 additions & 0 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ impl<B: GfxBackend> Device<B> {
hal_limits: hal::Limits,
supports_texture_d24_s8: bool,
desc: &wgt::DeviceDescriptor,
#[allow(unused)] unsafe_extensions: wgt::UnsafeExtensions,
trace_path: Option<&std::path::Path>,
) -> Self {
// don't start submission index at zero
Expand Down Expand Up @@ -243,6 +244,7 @@ impl<B: GfxBackend> Device<B> {
Ok(mut trace) => {
trace.add(Action::Init {
desc: desc.clone(),
unsafe_extensions,
backend: B::VARIANT,
});
Some(Mutex::new(trace))
Expand Down
1 change: 1 addition & 0 deletions wgpu-core/src/device/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub struct RenderPipelineDescriptor {
pub enum Action {
Init {
desc: wgt::DeviceDescriptor,
unsafe_extensions: wgt::UnsafeExtensions,
backend: wgt::Backend,
},
CreateBuffer {
Expand Down
23 changes: 17 additions & 6 deletions wgpu-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use crate::{
power, LifeGuard, Stored, MAX_BIND_GROUPS,
};

use wgt::{Backend, BackendBit, DeviceDescriptor, PowerPreference, BIND_BUFFER_ALIGNMENT};
use wgt::{
Backend, BackendBit, DeviceDescriptor, Extensions, PowerPreference, UnsafeExtensions,
BIND_BUFFER_ALIGNMENT,
};

#[cfg(feature = "replay")]
use serde::Deserialize;
Expand Down Expand Up @@ -526,7 +529,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
AdapterInfo::from_gfx(adapter.raw.info.clone(), adapter_id.backend())
}

pub fn adapter_extensions<B: GfxBackend>(&self, adapter_id: AdapterId) -> wgt::Extensions {
pub fn adapter_extensions<B: GfxBackend>(&self, adapter_id: AdapterId) -> Extensions {
let hub = B::hub(self);
let mut token = Token::root();
let (adapter_guard, _) = hub.adapters.read(&mut token);
Expand All @@ -552,6 +555,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {

wgt::Limits {
max_bind_groups: (limits.max_bound_descriptor_sets as u32).min(MAX_BIND_GROUPS as u32),
_non_exhaustive: wgt::NonExhaustive,
}
}

Expand Down Expand Up @@ -579,9 +583,18 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
&self,
adapter_id: AdapterId,
desc: &DeviceDescriptor,
unsafe_extensions: UnsafeExtensions,
trace_path: Option<&std::path::Path>,
id_in: Input<G, DeviceId>,
) -> DeviceId {
if desc.extensions.contains(Extensions::UNSAFE_EXTENSIONS) {
assert!(
unsafe_extensions.allowed(),
"Cannot enable unsafe extension without UnsafeExtensions::allow(). Enabled unsafe extensions: {:?}",
desc.extensions & Extensions::UNSAFE_EXTENSIONS
);
}

let hub = B::hub(self);
let mut token = Token::root();
let device = {
Expand All @@ -604,10 +617,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
}

// Check features needed by extensions
if desc
.extensions
.contains(wgt::Extensions::ANISOTROPIC_FILTERING)
{
if desc.extensions.contains(Extensions::ANISOTROPIC_FILTERING) {
assert!(
available_features.contains(hal::Features::SAMPLER_ANISOTROPY),
"Missing feature SAMPLER_ANISOTROPY for anisotropic filtering extension"
Expand Down Expand Up @@ -660,6 +670,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
limits,
supports_texture_d24_s8,
desc,
unsafe_extensions,
trace_path,
)
};
Expand Down
56 changes: 53 additions & 3 deletions wgpu-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,24 @@ impl From<Backend> for BackendBit {
}
}

/// This type is not to be constructed by any users of wgpu. If you construct this type, any semver
/// guarantees made by wgpu are invalidated and a non-breaking change may break your code.
///
/// If you are here trying to construct it, the solution is to use partial construction with the
/// default:
///
/// ```ignore
/// let limits = Limits {
/// max_bind_groups: 2,
/// ..Limits::default()
/// }
/// ```
#[doc(hidden)]
#[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "trace", derive(Serialize))]
#[cfg_attr(feature = "replay", derive(Deserialize))]
pub struct NonExhaustive;

bitflags::bitflags! {
#[repr(transparent)]
#[derive(Default)]
Expand All @@ -110,7 +128,33 @@ bitflags::bitflags! {
/// but it is not yet implemented.
///
/// https://github.com/gpuweb/gpuweb/issues/696
const ANISOTROPIC_FILTERING = 0x01;
const ANISOTROPIC_FILTERING = 0x0000_0001_0000_0000;
/// Extensions which are part of the upstream webgpu standard
const WEBGPU_EXTENSIONS = 0x0000_0000_0000_FFFF;
/// Extensions that require activating the unsafe extension flag
const UNSAFE_EXTENSIONS = 0xFFFF_0000_0000_0000;
/// Extensions that are only available when targeting native (not web)
const NATIVE_EXTENSIONS = 0xFFFF_FFFF_FFFF_0000;
}
}

#[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "trace", derive(Serialize))]
#[cfg_attr(feature = "replay", derive(Deserialize))]
pub struct UnsafeExtensions {
allow_unsafe: bool,
}
impl UnsafeExtensions {
pub unsafe fn allow() -> Self {
Self { allow_unsafe: true }
}
pub fn disallow() -> Self {
Self {
allow_unsafe: false,
}
}
pub fn allowed(self) -> bool {
self.allow_unsafe
}
}

Expand All @@ -120,11 +164,15 @@ bitflags::bitflags! {
#[cfg_attr(feature = "replay", derive(Deserialize))]
pub struct Limits {
pub max_bind_groups: u32,
pub _non_exhaustive: NonExhaustive,
}

impl Default for Limits {
fn default() -> Self {
Limits { max_bind_groups: 4 }
Limits {
max_bind_groups: 4,
_non_exhaustive: NonExhaustive,
}
}
}

Expand Down Expand Up @@ -941,7 +989,7 @@ impl Default for FilterMode {
}
}

#[derive(Clone, Debug, PartialEq)]
#[derive(Default, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "trace", derive(Serialize))]
#[cfg_attr(feature = "replay", derive(Deserialize))]
pub struct SamplerDescriptor<L> {
Expand All @@ -960,6 +1008,7 @@ pub struct SamplerDescriptor<L> {
///
/// Valid values: 1, 2, 4, 8, and 16.
pub anisotropy_clamp: Option<u8>,
pub _non_exhaustive: NonExhaustive,
}

impl<L> SamplerDescriptor<L> {
Expand All @@ -976,6 +1025,7 @@ impl<L> SamplerDescriptor<L> {
lod_max_clamp: self.lod_max_clamp,
compare: self.compare,
anisotropy_clamp: self.anisotropy_clamp,
_non_exhaustive: self._non_exhaustive,
}
}
}
Expand Down

0 comments on commit 08d857b

Please sign in to comment.