[rs] Implement extensions interface as described in wgpu#691

This commit is contained in:
Connor Fitzgerald
2020-06-05 02:01:41 -04:00
parent 8e96559672
commit 28c0b8f3a3
13 changed files with 41 additions and 17 deletions

View File

@@ -28,14 +28,14 @@ vulkan = ["wgc/gfx-backend-vulkan"]
package = "wgpu-core"
version = "0.5"
git = "https://github.com/gfx-rs/wgpu"
rev = "ac9587e9ced5b043abad68e260cb8c9e812cffb5"
rev = "041db60f9080769b5edc40888cf9683ccb255399"
features = ["raw-window-handle"]
[dependencies.wgt]
package = "wgpu-types"
version = "0.5"
git = "https://github.com/gfx-rs/wgpu"
rev = "ac9587e9ced5b043abad68e260cb8c9e812cffb5"
rev = "041db60f9080769b5edc40888cf9683ccb255399"
[dependencies]
arrayvec = "0.5"

View File

@@ -1,10 +1,10 @@
use std::env;
/// This example shows how to capture an image by rendering it to a texture, copying the texture to
/// a buffer, and retrieving it from the buffer. This could be used for "taking a screenshot," with
/// the added benefit that this method doesn't require a window to be created.
use std::fs::File;
use std::mem::size_of;
use std::env;
use std::io::Write;
use std::mem::size_of;
async fn run() {
let adapter = wgpu::Instance::new()
@@ -13,6 +13,7 @@ async fn run() {
power_preference: wgpu::PowerPreference::Default,
compatible_surface: None,
},
wgpu::UnsafeExtensions::disallow(),
wgpu::BackendBit::PRIMARY,
)
.await
@@ -132,7 +133,11 @@ async fn run() {
if let Ok(()) = buffer_future.await {
let padded_buffer = output_buffer.get_mapped_range(0, wgt::BufferSize::WHOLE);
let mut png_encoder = png::Encoder::new(File::create("red.png").unwrap(), width as u32, height as u32);
let mut png_encoder = png::Encoder::new(
File::create("red.png").unwrap(),
width as u32,
height as u32,
);
png_encoder.set_depth(png::BitDepth::Eight);
png_encoder.set_color(png::ColorType::RGBA);
let mut png_writer = png_encoder

View File

@@ -205,7 +205,7 @@ impl framework::Example for Example {
lod_min_clamp: 0.0,
lod_max_clamp: 100.0,
compare: None,
anisotropy_clamp: None,
..Default::default()
});
let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32);
let mx_ref: &[f32; 16] = mx_total.as_ref();

View File

@@ -6,6 +6,7 @@ async fn run() {
power_preference: wgpu::PowerPreference::Default,
compatible_surface: None,
},
unsafe { wgpu::UnsafeExtensions::allow() },
wgpu::BackendBit::PRIMARY,
)
.await

View File

@@ -66,6 +66,7 @@ async fn run_async<E: Example>(event_loop: EventLoop<()>, window: Window) {
power_preference: wgpu::PowerPreference::Default,
compatible_surface: Some(&surface),
},
wgpu::UnsafeExtensions::disallow(),
wgpu::BackendBit::PRIMARY,
)
.await

View File

@@ -29,6 +29,7 @@ async fn execute_gpu(numbers: Vec<u32>) -> Vec<u32> {
power_preference: wgpu::PowerPreference::Default,
compatible_surface: None,
},
wgpu::UnsafeExtensions::disallow(),
wgpu::BackendBit::PRIMARY,
)
.await

View File

@@ -14,6 +14,7 @@ async fn run(event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu::
power_preference: wgpu::PowerPreference::Default,
compatible_surface: Some(&surface),
},
wgpu::UnsafeExtensions::disallow(),
wgpu::BackendBit::PRIMARY,
)
.await

View File

@@ -157,7 +157,7 @@ impl Example {
lod_min_clamp: 0.0,
lod_max_clamp: 100.0,
compare: None,
anisotropy_clamp: None,
..Default::default()
});
let views = (0..mip_count)
@@ -310,7 +310,7 @@ impl framework::Example for Example {
lod_min_clamp: 0.0,
lod_max_clamp: 100.0,
compare: None,
anisotropy_clamp: None,
..Default::default()
});
let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32);
let mx_ref: &[f32; 16] = mx_total.as_ref();

View File

@@ -353,7 +353,7 @@ impl framework::Example for Example {
lod_min_clamp: -100.0,
lod_max_clamp: 100.0,
compare: Some(wgpu::CompareFunction::LessEqual),
anisotropy_clamp: None,
..Default::default()
});
let shadow_texture = device.create_texture(&wgpu::TextureDescriptor {

View File

@@ -130,7 +130,7 @@ impl framework::Example for Skybox {
lod_min_clamp: 0.0,
lod_max_clamp: 100.0,
compare: None,
anisotropy_clamp: None,
..Default::default()
});
let paths: [&'static [u8]; 6] = [

View File

@@ -223,6 +223,7 @@ impl crate::Context for Context {
fn instance_request_adapter(
&self,
options: &crate::RequestAdapterOptions<'_>,
unsafe_extensions: wgt::UnsafeExtensions,
backends: wgt::BackendBit,
) -> Self::RequestAdapterFuture {
let id = self.pick_adapter(
@@ -230,6 +231,7 @@ impl crate::Context for Context {
power_preference: options.power_preference,
compatible_surface: options.compatible_surface.map(|surface| surface.id),
},
unsafe_extensions,
wgc::instance::AdapterInputs::Mask(backends, |_| PhantomData),
);
ready(id)

View File

@@ -660,6 +660,7 @@ impl crate::Context for Context {
fn instance_request_adapter(
&self,
options: &crate::RequestAdapterOptions<'_>,
_unsafe_extensions: wgt::UnsafeExtensions,
_backends: wgt::BackendBit,
) -> Self::RequestAdapterFuture {
//TODO: support this check, return `None` if the flag is not set.
@@ -689,6 +690,11 @@ impl crate::Context for Context {
if trace_dir.is_some() {
//Error: Tracing isn't supported on the Web target
}
assert!(
!desc.extensions.intersects(crate::Extensions::ALL_NATIVE),
"The web backend doesn't support any native extensions. Enabled native extensions: {:?}",
desc.extensions & crate::Extensions::ALL_NATIVE
);
let mut mapped_desc = web_sys::GpuDeviceDescriptor::new();
// TODO: label, extensions
let mut mapped_limits = web_sys::GpuLimits::new();

View File

@@ -27,8 +27,8 @@ pub use wgt::{
RasterizationStateDescriptor, ShaderLocation, ShaderStage, StencilOperation,
StencilStateFaceDescriptor, StoreOp, SwapChainDescriptor, SwapChainStatus, TextureAspect,
TextureComponentType, TextureDataLayout, TextureDimension, TextureFormat, TextureUsage,
TextureViewDimension, VertexAttributeDescriptor, VertexFormat, BIND_BUFFER_ALIGNMENT,
COPY_BYTES_PER_ROW_ALIGNMENT,
TextureViewDimension, UnsafeExtensions, VertexAttributeDescriptor, VertexFormat,
BIND_BUFFER_ALIGNMENT, COPY_BYTES_PER_ROW_ALIGNMENT,
};
use backend::Context as C;
@@ -123,6 +123,7 @@ trait Context: Sized {
fn instance_request_adapter(
&self,
options: &RequestAdapterOptions<'_>,
unsafe_extensions: wgt::UnsafeExtensions,
backends: wgt::BackendBit,
) -> Self::RequestAdapterFuture;
fn adapter_request_device(
@@ -923,12 +924,17 @@ impl Instance {
/// Retrieves all available [`Adapter`]s that match the given backends.
#[cfg(not(target_arch = "wasm32"))]
pub fn enumerate_adapters(&self, backends: wgt::BackendBit) -> impl Iterator<Item = Adapter> {
pub fn enumerate_adapters(
&self,
unsafe_extensions: wgt::UnsafeExtensions,
backends: wgt::BackendBit,
) -> impl Iterator<Item = Adapter> {
let context = Arc::clone(&self.context);
self.context
.enumerate_adapters(wgc::instance::AdapterInputs::Mask(backends, |_| {
PhantomData
}))
.enumerate_adapters(
unsafe_extensions,
wgc::instance::AdapterInputs::Mask(backends, |_| PhantomData),
)
.into_iter()
.map(move |id| crate::Adapter {
id,
@@ -982,11 +988,12 @@ impl Instance {
pub fn request_adapter(
&self,
options: &RequestAdapterOptions<'_>,
unsafe_extensions: wgt::UnsafeExtensions,
backends: BackendBit,
) -> impl Future<Output = Option<Adapter>> + Send {
let context = Arc::clone(&self.context);
self.context
.instance_request_adapter(options, backends)
.instance_request_adapter(options, unsafe_extensions, backends)
.map(|option| option.map(|id| Adapter { context, id }))
}
}