mirror of
https://github.com/gfx-rs/wgpu.git
synced 2026-04-22 03:02:01 -04:00
[rs] Update to new wgpu-core render pipeline API
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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<E: Example>(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();
|
||||
|
||||
|
||||
@@ -24,13 +24,10 @@ async fn execute_gpu(numbers: Vec<u32>) -> Vec<u32> {
|
||||
|
||||
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();
|
||||
|
||||
|
||||
@@ -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");
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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::<Vec<_>>();
|
||||
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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<Item = Adapter> {
|
||||
pub fn enumerate_adapters(&self, backends: BackendBit) -> impl Iterator<Item = Adapter> {
|
||||
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<Output = Option<Adapter>> + 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 }))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user