From 54d4532dae187378bd5f767651a581c2934c0782 Mon Sep 17 00:00:00 2001 From: Gabriel Majeri Date: Tue, 7 Jul 2020 19:36:42 +0300 Subject: [PATCH] [rs] Update to new `wgpu-core` render pipeline API --- wgpu/Cargo.toml | 4 +- wgpu/examples/capture/main.rs | 11 ++--- wgpu/examples/framework.rs | 17 +++----- wgpu/examples/hello-compute/main.rs | 11 ++--- wgpu/examples/hello-triangle/main.rs | 13 +++--- wgpu/examples/hello/main.rs | 11 ++--- wgpu/examples/texture-arrays/main.rs | 13 +++--- wgpu/src/backend/direct.rs | 65 +++++++--------------------- wgpu/src/backend/web.rs | 1 - wgpu/src/lib.rs | 48 ++++---------------- 10 files changed, 54 insertions(+), 140 deletions(-) diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 6e6e0b4ea3..1aecfa6acd 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -27,14 +27,14 @@ vulkan = ["wgc/gfx-backend-vulkan"] package = "wgpu-core" version = "0.5" git = "https://github.com/gfx-rs/wgpu" -rev = "43c67ac59c2dedaa4d78601bc460aab7c3257973" +rev = "cfd21d4913f8cbed84ebbd0af54bae69f4426a64" features = ["raw-window-handle"] [dependencies.wgt] package = "wgpu-types" version = "0.5" git = "https://github.com/gfx-rs/wgpu" -rev = "43c67ac59c2dedaa4d78601bc460aab7c3257973" +rev = "cfd21d4913f8cbed84ebbd0af54bae69f4426a64" [dependencies] arrayvec = "0.5" diff --git a/wgpu/examples/capture/main.rs b/wgpu/examples/capture/main.rs index 79409618c0..fc9887db6f 100644 --- a/wgpu/examples/capture/main.rs +++ b/wgpu/examples/capture/main.rs @@ -29,13 +29,10 @@ async fn create_red_image_with_dimensions( height: usize, ) -> (Device, Buffer, BufferDimensions) { let adapter = wgpu::Instance::new(wgpu::BackendBit::PRIMARY) - .request_adapter( - &wgpu::RequestAdapterOptions { - power_preference: wgpu::PowerPreference::Default, - compatible_surface: None, - }, - wgpu::UnsafeFeatures::disallow(), - ) + .request_adapter(&wgpu::RequestAdapterOptions { + power_preference: wgpu::PowerPreference::Default, + compatible_surface: None, + }) .await .unwrap(); diff --git a/wgpu/examples/framework.rs b/wgpu/examples/framework.rs index f4339599a0..da7c707954 100644 --- a/wgpu/examples/framework.rs +++ b/wgpu/examples/framework.rs @@ -30,8 +30,8 @@ pub enum ShaderStage { } pub trait Example: 'static + Sized { - fn needed_features() -> (wgpu::Features, wgpu::UnsafeFeatures) { - (wgpu::Features::empty(), wgt::UnsafeFeatures::disallow()) + fn needed_features() -> wgpu::Features { + wgpu::Features::empty() } fn init( sc_desc: &wgpu::SwapChainDescriptor, @@ -85,16 +85,13 @@ async fn setup(title: &str) -> Setup { (size, surface) }; - let (needed_features, unsafe_extensions) = E::needed_features(); + let needed_features = E::needed_features(); let adapter = instance - .request_adapter( - &wgpu::RequestAdapterOptions { - power_preference: wgpu::PowerPreference::Default, - compatible_surface: Some(&surface), - }, - unsafe_extensions, - ) + .request_adapter(&wgpu::RequestAdapterOptions { + power_preference: wgpu::PowerPreference::Default, + compatible_surface: Some(&surface), + }) .await .unwrap(); diff --git a/wgpu/examples/hello-compute/main.rs b/wgpu/examples/hello-compute/main.rs index 7e025d9197..37ad560deb 100644 --- a/wgpu/examples/hello-compute/main.rs +++ b/wgpu/examples/hello-compute/main.rs @@ -24,13 +24,10 @@ async fn execute_gpu(numbers: Vec) -> Vec { let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY); let adapter = instance - .request_adapter( - &wgpu::RequestAdapterOptions { - power_preference: wgpu::PowerPreference::Default, - compatible_surface: None, - }, - wgpu::UnsafeFeatures::disallow(), - ) + .request_adapter(&wgpu::RequestAdapterOptions { + power_preference: wgpu::PowerPreference::Default, + compatible_surface: None, + }) .await .unwrap(); diff --git a/wgpu/examples/hello-triangle/main.rs b/wgpu/examples/hello-triangle/main.rs index e95076d51c..91d1d63d33 100644 --- a/wgpu/examples/hello-triangle/main.rs +++ b/wgpu/examples/hello-triangle/main.rs @@ -9,14 +9,11 @@ async fn run(event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu:: let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY); let surface = unsafe { instance.create_surface(&window) }; let adapter = instance - .request_adapter( - &wgpu::RequestAdapterOptions { - power_preference: wgpu::PowerPreference::Default, - // Request an adapter which can render to our surface - compatible_surface: Some(&surface), - }, - wgpu::UnsafeFeatures::disallow(), - ) + .request_adapter(&wgpu::RequestAdapterOptions { + power_preference: wgpu::PowerPreference::Default, + // Request an adapter which can render to our surface + compatible_surface: Some(&surface), + }) .await .expect("Failed to find an appropiate adapter"); diff --git a/wgpu/examples/hello/main.rs b/wgpu/examples/hello/main.rs index 02e365d9c0..9b45ce5256 100644 --- a/wgpu/examples/hello/main.rs +++ b/wgpu/examples/hello/main.rs @@ -1,13 +1,10 @@ /// This example shows how to describe the adapter in use. async fn run() { let adapter = wgpu::Instance::new(wgpu::BackendBit::PRIMARY) - .request_adapter( - &wgpu::RequestAdapterOptions { - power_preference: wgpu::PowerPreference::Default, - compatible_surface: None, - }, - unsafe { wgpu::UnsafeFeatures::allow() }, - ) + .request_adapter(&wgpu::RequestAdapterOptions { + power_preference: wgpu::PowerPreference::Default, + compatible_surface: None, + }) .await .unwrap(); diff --git a/wgpu/examples/texture-arrays/main.rs b/wgpu/examples/texture-arrays/main.rs index 27f5b2e5c1..c749541e88 100644 --- a/wgpu/examples/texture-arrays/main.rs +++ b/wgpu/examples/texture-arrays/main.rs @@ -85,14 +85,11 @@ struct Example { } impl framework::Example for Example { - fn needed_features() -> (wgpu::Features, wgpu::UnsafeFeatures) { - ( - wgpu::Features::UNSIZED_BINDING_ARRAY - | wgpu::Features::SAMPLED_TEXTURE_ARRAY_NON_UNIFORM_INDEXING - | wgpu::Features::SAMPLED_TEXTURE_ARRAY_DYNAMIC_INDEXING - | wgpu::Features::SAMPLED_TEXTURE_BINDING_ARRAY, - wgpu::UnsafeFeatures::disallow(), - ) + fn needed_features() -> wgpu::Features { + wgpu::Features::UNSIZED_BINDING_ARRAY + | wgpu::Features::SAMPLED_TEXTURE_ARRAY_NON_UNIFORM_INDEXING + | wgpu::Features::SAMPLED_TEXTURE_ARRAY_DYNAMIC_INDEXING + | wgpu::Features::SAMPLED_TEXTURE_BINDING_ARRAY } fn init( sc_desc: &wgpu::SwapChainDescriptor, diff --git a/wgpu/src/backend/direct.rs b/wgpu/src/backend/direct.rs index 62c7fed3b1..f15dbb2841 100644 --- a/wgpu/src/backend/direct.rs +++ b/wgpu/src/backend/direct.rs @@ -456,14 +456,12 @@ impl crate::Context for Context { fn instance_request_adapter( &self, options: &crate::RequestAdapterOptions<'_>, - unsafe_features: wgt::UnsafeFeatures, ) -> Self::RequestAdapterFuture { let id = self.pick_adapter( &wgc::instance::RequestAdapterOptions { power_preference: options.power_preference, compatible_surface: options.compatible_surface.map(|surface| surface.id), }, - unsafe_features, wgc::instance::AdapterInputs::Mask(wgt::BackendBit::all(), |_| PhantomData), ); ready(id) @@ -475,7 +473,7 @@ impl crate::Context for Context { desc: &crate::DeviceDescriptor, trace_dir: Option<&std::path::Path>, ) -> Self::RequestDeviceFuture { - let device_id = gfx_select!(*adapter => self.adapter_request_device(*adapter, desc, trace_dir, PhantomData)); + let device_id = gfx_select!(*adapter => self.adapter_request_device(*adapter, desc, trace_dir, PhantomData)).unwrap(); ready(Ok((device_id, device_id))) } @@ -611,60 +609,29 @@ impl crate::Context for Context { wgc::span!(_guard, TRACE, "Device::create_render_pipeline wrapper"); use wgc::pipeline as pipe; - let vertex_entry_point = CString::new(desc.vertex_stage.entry_point).unwrap(); let vertex_stage = pipe::ProgrammableStageDescriptor { module: desc.vertex_stage.module.id, - entry_point: vertex_entry_point.as_ptr(), + entry_point: desc.vertex_stage.entry_point, }; - let (_fragment_entry_point, fragment_stage) = - if let Some(fragment_stage) = &desc.fragment_stage { - let fragment_entry_point = CString::new(fragment_stage.entry_point).unwrap(); - let fragment_stage = pipe::ProgrammableStageDescriptor { - module: fragment_stage.module.id, - entry_point: fragment_entry_point.as_ptr(), - }; - (fragment_entry_point, Some(fragment_stage)) - } else { - (CString::default(), None) - }; - - let temp_color_states = desc.color_states.to_vec(); - let temp_vertex_buffers = desc - .vertex_state - .vertex_buffers - .iter() - .map(|vbuf| pipe::VertexBufferLayoutDescriptor { - array_stride: vbuf.stride, - step_mode: vbuf.step_mode, - attributes: vbuf.attributes.as_ptr(), - attributes_length: vbuf.attributes.len(), - }) - .collect::>(); + let fragment_stage = + desc.fragment_stage + .as_ref() + .map(|fs| pipe::ProgrammableStageDescriptor { + module: fs.module.id, + entry_point: fs.entry_point, + }); gfx_select!(*device => self.device_create_render_pipeline( *device, &pipe::RenderPipelineDescriptor { layout: desc.layout.id, vertex_stage, - fragment_stage: fragment_stage - .as_ref() - .map_or(ptr::null(), |fs| fs as *const _), - rasterization_state: desc - .rasterization_state - .as_ref() - .map_or(ptr::null(), |p| p as *const _), + fragment_stage, + rasterization_state: desc.rasterization_state.clone(), primitive_topology: desc.primitive_topology, - color_states: temp_color_states.as_ptr(), - color_states_length: temp_color_states.len(), - depth_stencil_state: desc - .depth_stencil_state - .as_ref() - .map_or(ptr::null(), |p| p as *const _), - vertex_state: pipe::VertexStateDescriptor { - index_format: desc.vertex_state.index_format, - vertex_buffers: temp_vertex_buffers.as_ptr(), - vertex_buffers_length: temp_vertex_buffers.len(), - }, + color_states: desc.color_states, + depth_stencil_state: desc.depth_stencil_state.clone(), + vertex_state: desc.vertex_state, sample_count: desc.sample_count, sample_mask: desc.sample_mask, alpha_to_coverage_enabled: desc.alpha_to_coverage_enabled, @@ -681,15 +648,13 @@ impl crate::Context for Context { ) -> Self::ComputePipelineId { use wgc::pipeline as pipe; - let entry_point = CString::new(desc.compute_stage.entry_point).unwrap(); - gfx_select!(*device => self.device_create_compute_pipeline( *device, &pipe::ComputePipelineDescriptor { layout: desc.layout.id, compute_stage: pipe::ProgrammableStageDescriptor { module: desc.compute_stage.module.id, - entry_point: entry_point.as_ptr(), + entry_point: desc.compute_stage.entry_point, }, }, PhantomData diff --git a/wgpu/src/backend/web.rs b/wgpu/src/backend/web.rs index 5e5408fcd1..bd60eb5399 100644 --- a/wgpu/src/backend/web.rs +++ b/wgpu/src/backend/web.rs @@ -725,7 +725,6 @@ impl crate::Context for Context { fn instance_request_adapter( &self, options: &crate::RequestAdapterOptions<'_>, - _unsafe_extensions: wgt::UnsafeFeatures, ) -> Self::RequestAdapterFuture { //TODO: support this check, return `None` if the flag is not set. // It's not trivial, since we need the Future logic to have this check, diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 19f850a0db..fc726425c6 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -3,7 +3,6 @@ //! To start using the API, create an [`Instance`]. #![doc(html_logo_url = "https://raw.githubusercontent.com/gfx-rs/wgpu-rs/master/logo.png")] - #![warn(missing_docs)] mod backend; @@ -33,8 +32,9 @@ pub use wgt::{ PrimitiveTopology, RasterizationStateDescriptor, RenderBundleEncoderDescriptor, ShaderLocation, ShaderStage, StencilOperation, StencilStateFaceDescriptor, SwapChainDescriptor, SwapChainStatus, TextureAspect, TextureComponentType, TextureDataLayout, TextureDimension, - TextureFormat, TextureUsage, TextureViewDimension, UnsafeFeatures, VertexAttributeDescriptor, - VertexFormat, BIND_BUFFER_ALIGNMENT, COPY_BUFFER_ALIGNMENT, COPY_BYTES_PER_ROW_ALIGNMENT, + TextureFormat, TextureUsage, TextureViewDimension, VertexAttributeDescriptor, + VertexBufferDescriptor, VertexFormat, VertexStateDescriptor, BIND_BUFFER_ALIGNMENT, + COPY_BUFFER_ALIGNMENT, COPY_BYTES_PER_ROW_ALIGNMENT, }; use backend::Context as C; @@ -174,7 +174,6 @@ trait Context: Sized { fn instance_request_adapter( &self, options: &RequestAdapterOptions<'_>, - unsafe_extensions: UnsafeFeatures, ) -> Self::RequestAdapterFuture; fn adapter_request_device( &self, @@ -846,30 +845,6 @@ pub struct ProgrammableStageDescriptor<'a> { pub entry_point: &'a str, } -/// Describes vertex input state for a render pipeline. -#[derive(Clone, Debug)] -pub struct VertexStateDescriptor<'a> { - /// The format of any index buffers used with this pipeline. - pub index_format: IndexFormat, - - /// The format of any vertex buffers used with this pipeline. - pub vertex_buffers: &'a [VertexBufferDescriptor<'a>], -} - -/// Describes how the vertex buffer is interpreted. -#[derive(Clone, Debug)] -pub struct VertexBufferDescriptor<'a> { - /// The stride, in bytes, between elements of this buffer. - pub stride: BufferAddress, - - /// How often this vertex buffer is "stepped" forward. Can be per-vertex - /// or per-instance. - pub step_mode: InputStepMode, - - /// The list of attributes which comprise a single vertex, this can be made with [`vertex_attr_array`]. - pub attributes: &'a [VertexAttributeDescriptor], -} - /// Describes a render (graphics) pipeline. #[derive(Clone)] pub struct RenderPipelineDescriptor<'a> { @@ -1068,20 +1043,14 @@ impl Instance { /// /// # Arguments /// - /// - `unsafe_features` - Marker for allowing unsafe features. /// - `backends` - Backends from which to enumerate adapters. #[cfg(not(target_arch = "wasm32"))] - pub fn enumerate_adapters( - &self, - unsafe_features: UnsafeFeatures, - backends: BackendBit, - ) -> impl Iterator { + pub fn enumerate_adapters(&self, backends: BackendBit) -> impl Iterator { let context = Arc::clone(&self.context); self.context - .enumerate_adapters( - unsafe_features, - wgc::instance::AdapterInputs::Mask(backends, |_| PhantomData), - ) + .enumerate_adapters(wgc::instance::AdapterInputs::Mask(backends, |_| { + PhantomData + })) .into_iter() .map(move |id| crate::Adapter { id, @@ -1097,11 +1066,10 @@ impl Instance { pub fn request_adapter( &self, options: &RequestAdapterOptions<'_>, - unsafe_features: UnsafeFeatures, ) -> impl Future> + Send { let context = Arc::clone(&self.context); self.context - .instance_request_adapter(options, unsafe_features) + .instance_request_adapter(options) .map(|option| option.map(|id| Adapter { context, id })) }