hal: remove the cows

This commit is contained in:
Dzmitry Malyshau 2021-06-08 15:18:06 -04:00
parent 502c57501f
commit a0b51ce35c
5 changed files with 45 additions and 63 deletions

View File

@ -750,7 +750,7 @@ impl<'a, A: HalApi> RenderPassInfo<'a, A> {
let hal_desc = hal::RenderPassDescriptor { let hal_desc = hal::RenderPassDescriptor {
label, label,
color_attachments: colors.iter().cloned().collect(), color_attachments: &colors,
depth_stencil_attachment: depth_stencil, depth_stencil_attachment: depth_stencil,
}; };
unsafe { unsafe {

View File

@ -11,8 +11,7 @@ use crate::{
pipeline, resource, swap_chain, pipeline, resource, swap_chain,
track::{BufferState, TextureSelector, TextureState, TrackerSet, UsageConflict}, track::{BufferState, TextureSelector, TextureState, TrackerSet, UsageConflict},
validation::{self, check_buffer_usage, check_texture_usage}, validation::{self, check_buffer_usage, check_texture_usage},
CowHelpers as _, FastHashMap, Label, LabelHelpers as _, LifeGuard, MultiRefCount, Stored, FastHashMap, Label, LabelHelpers as _, LifeGuard, MultiRefCount, Stored, SubmissionIndex,
SubmissionIndex,
}; };
use arrayvec::ArrayVec; use arrayvec::ArrayVec;
@ -957,7 +956,7 @@ impl<A: HalApi> Device<A> {
let hal_bindings = entry_map.values().cloned().collect::<Vec<_>>(); let hal_bindings = entry_map.values().cloned().collect::<Vec<_>>();
let hal_desc = hal::BindGroupLayoutDescriptor { let hal_desc = hal::BindGroupLayoutDescriptor {
label, label,
entries: Cow::Owned(hal_bindings), entries: &hal_bindings,
}; };
let raw = unsafe { let raw = unsafe {
self.raw self.raw
@ -1432,7 +1431,7 @@ impl<A: HalApi> Device<A> {
let hal_desc = hal::BindGroupDescriptor { let hal_desc = hal::BindGroupDescriptor {
label: desc.label.borrow_option(), label: desc.label.borrow_option(),
layout: &layout.raw, layout: &layout.raw,
entries: hal_entries.into(), entries: &hal_entries,
}; };
let raw = unsafe { let raw = unsafe {
self.raw self.raw
@ -1522,14 +1521,15 @@ impl<A: HalApi> Device<A> {
.validate(&self.limits) .validate(&self.limits)
.map_err(Error::TooManyBindings)?; .map_err(Error::TooManyBindings)?;
let bgl_vec = desc
.bind_group_layouts
.iter()
.map(|&id| &bgl_guard.get(id).unwrap().raw)
.collect::<Vec<_>>();
let hal_desc = hal::PipelineLayoutDescriptor { let hal_desc = hal::PipelineLayoutDescriptor {
label: desc.label.borrow_option(), label: desc.label.borrow_option(),
bind_group_layouts: desc bind_group_layouts: &bgl_vec,
.bind_group_layouts push_constant_ranges: desc.push_constant_ranges.as_ref(),
.iter()
.map(|&id| &bgl_guard.get(id).unwrap().raw)
.collect(),
push_constant_ranges: desc.push_constant_ranges.reborrow(),
}; };
let raw = unsafe { let raw = unsafe {
@ -1688,7 +1688,7 @@ impl<A: HalApi> Device<A> {
label: desc.label.borrow_option(), label: desc.label.borrow_option(),
layout: &layout.raw, layout: &layout.raw,
stage: hal::ProgrammableStage { stage: hal::ProgrammableStage {
entry_point: desc.stage.entry_point.reborrow(), entry_point: desc.stage.entry_point.as_ref(),
module: &shader_module.raw, module: &shader_module.raw,
}, },
}; };
@ -1779,7 +1779,7 @@ impl<A: HalApi> Device<A> {
vertex_buffers.alloc().init(hal::VertexBufferLayout { vertex_buffers.alloc().init(hal::VertexBufferLayout {
array_stride: vb_state.array_stride, array_stride: vb_state.array_stride,
step_mode: vb_state.step_mode, step_mode: vb_state.step_mode,
attributes: Cow::Borrowed(vb_state.attributes.as_ref()), attributes: vb_state.attributes.as_ref(),
}); });
for attribute in vb_state.attributes.iter() { for attribute in vb_state.attributes.iter() {
@ -1960,7 +1960,7 @@ impl<A: HalApi> Device<A> {
hal::ProgrammableStage { hal::ProgrammableStage {
module: &shader_module.raw, module: &shader_module.raw,
entry_point: stage.entry_point.reborrow(), entry_point: stage.entry_point.as_ref(),
} }
}; };
@ -2005,7 +2005,7 @@ impl<A: HalApi> Device<A> {
Some(hal::ProgrammableStage { Some(hal::ProgrammableStage {
module: &shader_module.raw, module: &shader_module.raw,
entry_point: fragment.stage.entry_point.reborrow(), entry_point: fragment.stage.entry_point.as_ref(),
}) })
} }
None => None, None => None,
@ -2063,13 +2063,13 @@ impl<A: HalApi> Device<A> {
let pipeline_desc = hal::RenderPipelineDescriptor { let pipeline_desc = hal::RenderPipelineDescriptor {
label: desc.label.borrow_option(), label: desc.label.borrow_option(),
layout: &layout.raw, layout: &layout.raw,
vertex_buffers: vertex_buffers.into(), vertex_buffers: &vertex_buffers,
vertex_stage, vertex_stage,
primitive: desc.primitive, primitive: desc.primitive,
depth_stencil: desc.depth_stencil.clone(), depth_stencil: desc.depth_stencil.clone(),
multisample: desc.multisample, multisample: desc.multisample,
fragment_stage, fragment_stage,
color_targets: Cow::Borrowed(color_targets), color_targets: color_targets,
}; };
let raw = let raw =
unsafe { self.raw.create_render_pipeline(&pipeline_desc) }.map_err( unsafe { self.raw.create_render_pipeline(&pipeline_desc) }.map_err(

View File

@ -76,15 +76,6 @@ impl<'a> LabelHelpers<'a> for Label<'a> {
} }
} }
trait CowHelpers<'a> {
fn reborrow(&'a self) -> Self;
}
impl<'a, T: ToOwned + ?Sized> CowHelpers<'a> for Cow<'a, T> {
fn reborrow(&'a self) -> Self {
Cow::Borrowed(self.as_ref())
}
}
/// Reference count object that is 1:1 with each reference. /// Reference count object that is 1:1 with each reference.
#[derive(Debug)] #[derive(Debug)]
struct RefCount(ptr::NonNull<AtomicUsize>); struct RefCount(ptr::NonNull<AtomicUsize>);

View File

@ -2,13 +2,7 @@ extern crate wgpu_hal as hal;
use hal::{Adapter as _, CommandBuffer as _, Device as _, Instance as _, Queue as _, Surface as _}; use hal::{Adapter as _, CommandBuffer as _, Device as _, Instance as _, Queue as _, Surface as _};
use std::{ use std::{borrow::Borrow, iter, mem, num::NonZeroU32, ptr, time::Instant};
borrow::{Borrow, Cow},
iter, mem,
num::NonZeroU32,
ptr,
time::Instant,
};
const MAX_BUNNIES: usize = 1 << 20; const MAX_BUNNIES: usize = 1 << 20;
const BUNNY_SIZE: f32 = 0.15 * 256.0; const BUNNY_SIZE: f32 = 0.15 * 256.0;
@ -104,7 +98,7 @@ impl<A: hal::Api> Example<A> {
let global_bgl_desc = hal::BindGroupLayoutDescriptor { let global_bgl_desc = hal::BindGroupLayoutDescriptor {
label: None, label: None,
entries: Cow::Borrowed(&[ entries: &[
wgt::BindGroupLayoutEntry { wgt::BindGroupLayoutEntry {
binding: 0, binding: 0,
visibility: wgt::ShaderStage::VERTEX, visibility: wgt::ShaderStage::VERTEX,
@ -134,14 +128,14 @@ impl<A: hal::Api> Example<A> {
}, },
count: None, count: None,
}, },
]), ],
}; };
let global_bind_group_layout = let global_bind_group_layout =
unsafe { device.create_bind_group_layout(&global_bgl_desc).unwrap() }; unsafe { device.create_bind_group_layout(&global_bgl_desc).unwrap() };
let local_bgl_desc = hal::BindGroupLayoutDescriptor { let local_bgl_desc = hal::BindGroupLayoutDescriptor {
entries: Cow::Borrowed(&[wgt::BindGroupLayoutEntry { entries: &[wgt::BindGroupLayoutEntry {
binding: 0, binding: 0,
visibility: wgt::ShaderStage::VERTEX, visibility: wgt::ShaderStage::VERTEX,
ty: wgt::BindingType::Buffer { ty: wgt::BindingType::Buffer {
@ -150,7 +144,7 @@ impl<A: hal::Api> Example<A> {
min_binding_size: wgt::BufferSize::new(mem::size_of::<Locals>() as _), min_binding_size: wgt::BufferSize::new(mem::size_of::<Locals>() as _),
}, },
count: None, count: None,
}]), }],
label: None, label: None,
}; };
let local_bind_group_layout = let local_bind_group_layout =
@ -158,11 +152,8 @@ impl<A: hal::Api> Example<A> {
let pipeline_layout_desc = hal::PipelineLayoutDescriptor { let pipeline_layout_desc = hal::PipelineLayoutDescriptor {
label: None, label: None,
bind_group_layouts: Cow::Borrowed(&[ bind_group_layouts: &[&global_bind_group_layout, &local_bind_group_layout],
&global_bind_group_layout, push_constant_ranges: &[],
&local_bind_group_layout,
]),
push_constant_ranges: Cow::Borrowed(&[]),
}; };
let pipeline_layout = unsafe { let pipeline_layout = unsafe {
device device
@ -175,12 +166,12 @@ impl<A: hal::Api> Example<A> {
layout: &pipeline_layout, layout: &pipeline_layout,
vertex_stage: hal::ProgrammableStage { vertex_stage: hal::ProgrammableStage {
module: &shader, module: &shader,
entry_point: Cow::Borrowed("vs_main"), entry_point: "vs_main",
}, },
vertex_buffers: Cow::Borrowed(&[]), vertex_buffers: &[],
fragment_stage: Some(hal::ProgrammableStage { fragment_stage: Some(hal::ProgrammableStage {
module: &shader, module: &shader,
entry_point: Cow::Borrowed("fs_main"), entry_point: "fs_main",
}), }),
primitive: wgt::PrimitiveState { primitive: wgt::PrimitiveState {
topology: wgt::PrimitiveTopology::TriangleStrip, topology: wgt::PrimitiveTopology::TriangleStrip,
@ -188,11 +179,11 @@ impl<A: hal::Api> Example<A> {
}, },
depth_stencil: None, depth_stencil: None,
multisample: wgt::MultisampleState::default(), multisample: wgt::MultisampleState::default(),
color_targets: Cow::Borrowed(&[wgt::ColorTargetState { color_targets: &[wgt::ColorTargetState {
format: surface_config.format, format: surface_config.format,
blend: Some(wgt::BlendState::ALPHA_BLENDING), blend: Some(wgt::BlendState::ALPHA_BLENDING),
write_mask: wgt::ColorWrite::default(), write_mask: wgt::ColorWrite::default(),
}]), }],
}; };
let pipeline = unsafe { device.create_render_pipeline(&pipeline_desc).unwrap() }; let pipeline = unsafe { device.create_render_pipeline(&pipeline_desc).unwrap() };
@ -341,7 +332,7 @@ impl<A: hal::Api> Example<A> {
let global_group_desc = hal::BindGroupDescriptor { let global_group_desc = hal::BindGroupDescriptor {
label: Some("global"), label: Some("global"),
layout: &global_bind_group_layout, layout: &global_bind_group_layout,
entries: Cow::Borrowed(&[ entries: &[
hal::BindGroupEntry { hal::BindGroupEntry {
binding: 0, binding: 0,
resource: hal::BindingResource::Buffers( resource: hal::BindingResource::Buffers(
@ -359,7 +350,7 @@ impl<A: hal::Api> Example<A> {
binding: 2, binding: 2,
resource: hal::BindingResource::Sampler(&sampler), resource: hal::BindingResource::Sampler(&sampler),
}, },
]), ],
}; };
unsafe { device.create_bind_group(&global_group_desc).unwrap() } unsafe { device.create_bind_group(&global_group_desc).unwrap() }
}; };
@ -373,12 +364,12 @@ impl<A: hal::Api> Example<A> {
let local_group_desc = hal::BindGroupDescriptor { let local_group_desc = hal::BindGroupDescriptor {
label: Some("local"), label: Some("local"),
layout: &local_bind_group_layout, layout: &local_bind_group_layout,
entries: Cow::Borrowed(&[hal::BindGroupEntry { entries: &[hal::BindGroupEntry {
binding: 0, binding: 0,
resource: hal::BindingResource::Buffers( resource: hal::BindingResource::Buffers(
iter::once(local_buffer_binding).collect(), iter::once(local_buffer_binding).collect(),
), ),
}]), }],
}; };
unsafe { device.create_bind_group(&local_group_desc).unwrap() } unsafe { device.create_bind_group(&local_group_desc).unwrap() }
}; };
@ -493,7 +484,7 @@ impl<A: hal::Api> Example<A> {
}; };
let pass_desc = hal::RenderPassDescriptor { let pass_desc = hal::RenderPassDescriptor {
label: None, label: None,
color_attachments: Cow::Borrowed(&[hal::ColorAttachment { color_attachments: &[hal::ColorAttachment {
target: hal::Attachment { target: hal::Attachment {
view: &surface_tex_view, view: &surface_tex_view,
usage: hal::TextureUse::COLOR_TARGET, usage: hal::TextureUse::COLOR_TARGET,
@ -507,7 +498,7 @@ impl<A: hal::Api> Example<A> {
b: 0.3, b: 0.3,
a: 1.0, a: 1.0,
}, },
}]), }],
depth_stencil_attachment: None, depth_stencil_attachment: None,
}; };
unsafe { unsafe {

View File

@ -47,7 +47,7 @@ pub mod api {
} }
use std::{ use std::{
borrow::{Borrow, Cow}, borrow::Borrow,
fmt, fmt,
num::NonZeroU8, num::NonZeroU8,
ops::{Range, RangeInclusive}, ops::{Range, RangeInclusive},
@ -688,14 +688,14 @@ pub struct SamplerDescriptor<'a> {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct BindGroupLayoutDescriptor<'a> { pub struct BindGroupLayoutDescriptor<'a> {
pub label: Label<'a>, pub label: Label<'a>,
pub entries: Cow<'a, [wgt::BindGroupLayoutEntry]>, pub entries: &'a [wgt::BindGroupLayoutEntry],
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct PipelineLayoutDescriptor<'a, A: Api> { pub struct PipelineLayoutDescriptor<'a, A: Api> {
pub label: Label<'a>, pub label: Label<'a>,
pub bind_group_layouts: Cow<'a, [&'a A::BindGroupLayout]>, pub bind_group_layouts: &'a [&'a A::BindGroupLayout],
pub push_constant_ranges: Cow<'a, [wgt::PushConstantRange]>, pub push_constant_ranges: &'a [wgt::PushConstantRange],
} }
#[derive(Debug)] #[derive(Debug)]
@ -744,7 +744,7 @@ pub struct BindGroupEntry<'a, A: Api> {
pub struct BindGroupDescriptor<'a, A: Api> { pub struct BindGroupDescriptor<'a, A: Api> {
pub label: Label<'a>, pub label: Label<'a>,
pub layout: &'a A::BindGroupLayout, pub layout: &'a A::BindGroupLayout,
pub entries: Cow<'a, [BindGroupEntry<'a, A>]>, pub entries: &'a [BindGroupEntry<'a, A>],
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -771,7 +771,7 @@ pub struct ProgrammableStage<'a, A: Api> {
pub module: &'a A::ShaderModule, pub module: &'a A::ShaderModule,
/// The name of the entry point in the compiled shader. There must be a function that returns /// The name of the entry point in the compiled shader. There must be a function that returns
/// void with this name in the shader. /// void with this name in the shader.
pub entry_point: Cow<'a, str>, pub entry_point: &'a str,
} }
// Rust gets confused about the impl requirements for `A` // Rust gets confused about the impl requirements for `A`
@ -802,7 +802,7 @@ pub struct VertexBufferLayout<'a> {
/// How often this vertex buffer is "stepped" forward. /// How often this vertex buffer is "stepped" forward.
pub step_mode: wgt::InputStepMode, pub step_mode: wgt::InputStepMode,
/// The list of attributes which comprise a single vertex. /// The list of attributes which comprise a single vertex.
pub attributes: Cow<'a, [wgt::VertexAttribute]>, pub attributes: &'a [wgt::VertexAttribute],
} }
/// Describes a render (graphics) pipeline. /// Describes a render (graphics) pipeline.
@ -812,7 +812,7 @@ pub struct RenderPipelineDescriptor<'a, A: Api> {
/// The layout of bind groups for this pipeline. /// The layout of bind groups for this pipeline.
pub layout: &'a A::PipelineLayout, pub layout: &'a A::PipelineLayout,
/// The format of any vertex buffers used with this pipeline. /// The format of any vertex buffers used with this pipeline.
pub vertex_buffers: Cow<'a, [VertexBufferLayout<'a>]>, pub vertex_buffers: &'a [VertexBufferLayout<'a>],
/// The vertex stage for this pipeline. /// The vertex stage for this pipeline.
pub vertex_stage: ProgrammableStage<'a, A>, pub vertex_stage: ProgrammableStage<'a, A>,
/// The properties of the pipeline at the primitive assembly and rasterization level. /// The properties of the pipeline at the primitive assembly and rasterization level.
@ -824,7 +824,7 @@ pub struct RenderPipelineDescriptor<'a, A: Api> {
/// The fragment stage for this pipeline. /// The fragment stage for this pipeline.
pub fragment_stage: Option<ProgrammableStage<'a, A>>, pub fragment_stage: Option<ProgrammableStage<'a, A>>,
/// The effect of draw calls on the color aspect of the output target. /// The effect of draw calls on the color aspect of the output target.
pub color_targets: Cow<'a, [wgt::ColorTargetState]>, pub color_targets: &'a [wgt::ColorTargetState],
} }
/// Specifies how the alpha channel of the textures should be handled during (martin mouv i step) /// Specifies how the alpha channel of the textures should be handled during (martin mouv i step)
@ -969,7 +969,7 @@ pub struct DepthStencilAttachment<'a, A: Api> {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct RenderPassDescriptor<'a, A: Api> { pub struct RenderPassDescriptor<'a, A: Api> {
pub label: Label<'a>, pub label: Label<'a>,
pub color_attachments: Cow<'a, [ColorAttachment<'a, A>]>, pub color_attachments: &'a [ColorAttachment<'a, A>],
pub depth_stencil_attachment: Option<DepthStencilAttachment<'a, A>>, pub depth_stencil_attachment: Option<DepthStencilAttachment<'a, A>>,
} }