From 4ed2c0a313deb48a99f15e8fe0558ad3d095ca01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marvin=20L=C3=B6bel?= Date: Mon, 20 Jul 2020 18:31:05 +0200 Subject: [PATCH] Replace borrowed slices in Descriptor structs with copy on write slices (#822) * Replace &[T] in descriptor structs with Cow<[T]> * Changed label fields to use Cow<'a, str> * Deduplicate types across the tracing API Introduce the ToStatic trait * Deduplicate trace::RenderBundleDescriptor * Remove ugly ToOwned bounds * Simplifiy Action variants * Make Clone bounds on type defs less verbose * Fix a error in play.rs * Remove ToStatic, and Make Action<'a> borrow descriptors instead --- player/src/bin/play.rs | 2 +- player/src/lib.rs | 148 ++++------------------- player/tests/data/buffer-copy.ron | 4 +- player/tests/test.rs | 6 +- wgpu-core/src/binding_model.rs | 11 +- wgpu-core/src/command/bundle.rs | 2 +- wgpu-core/src/device/mod.rs | 146 +++++++---------------- wgpu-core/src/device/trace.rs | 187 +++++++----------------------- wgpu-types/src/lib.rs | 58 ++++++--- 9 files changed, 162 insertions(+), 402 deletions(-) diff --git a/player/src/bin/play.rs b/player/src/bin/play.rs index 8c1f18a79..97a7b64f3 100644 --- a/player/src/bin/play.rs +++ b/player/src/bin/play.rs @@ -119,7 +119,7 @@ fn main() { } Event::RedrawRequested(_) => loop { match actions.pop() { - Some(trace::Action::CreateSwapChain { id, desc }) => { + Some(trace::Action::CreateSwapChain(id, desc)) => { log::info!("Initializing the swapchain"); assert_eq!(id.to_surface_id(), surface); window.set_inner_size(winit::dpi::PhysicalSize::new( diff --git a/player/src/lib.rs b/player/src/lib.rs index a17ad1abb..ef69ef797 100644 --- a/player/src/lib.rs +++ b/player/src/lib.rs @@ -152,7 +152,7 @@ impl GlobalPlay for wgc::hub::Global { A::CreateSwapChain { .. } | A::PresentSwapChain(_) => { panic!("Unexpected SwapChain action: winit feature is not enabled") } - A::CreateBuffer { id, desc } => { + A::CreateBuffer(id, desc) => { let label = Label::new(&desc.label); self.device_maintain_ids::(device); self.device_create_buffer::(device, &desc.map_label(|_| label.as_ptr()), id) @@ -161,7 +161,7 @@ impl GlobalPlay for wgc::hub::Global { A::DestroyBuffer(id) => { self.buffer_destroy::(id); } - A::CreateTexture { id, desc } => { + A::CreateTexture(id, desc) => { let label = Label::new(&desc.label); self.device_maintain_ids::(device); self.device_create_texture::(device, &desc.map_label(|_| label.as_ptr()), id) @@ -186,7 +186,7 @@ impl GlobalPlay for wgc::hub::Global { A::DestroyTextureView(id) => { self.texture_view_destroy::(id); } - A::CreateSampler { id, desc } => { + A::CreateSampler(id, desc) => { let label = Label::new(&desc.label); self.device_maintain_ids::(device); self.device_create_sampler::(device, &desc.map_label(|_| label.as_ptr()), id); @@ -202,83 +202,25 @@ impl GlobalPlay for wgc::hub::Global { .unwrap(); } } - A::CreateBindGroupLayout { - id, - ref label, - ref entries, - } => { - self.device_create_bind_group_layout::( - device, - &wgt::BindGroupLayoutDescriptor { - label: Some(label), - entries, - }, - id, - ) - .unwrap(); + A::CreateBindGroupLayout(id, desc) => { + self.device_create_bind_group_layout::(device, &desc, id) + .unwrap(); } A::DestroyBindGroupLayout(id) => { self.bind_group_layout_destroy::(id); } - A::CreatePipelineLayout { - id, - bind_group_layouts, - push_constant_ranges, - } => { + A::CreatePipelineLayout(id, desc) => { self.device_maintain_ids::(device); - self.device_create_pipeline_layout::( - device, - &wgt::PipelineLayoutDescriptor { - bind_group_layouts: &bind_group_layouts, - push_constant_ranges: &push_constant_ranges, - }, - id, - ) - .unwrap(); + self.device_create_pipeline_layout::(device, &desc, id) + .unwrap(); } A::DestroyPipelineLayout(id) => { self.pipeline_layout_destroy::(id); } - A::CreateBindGroup { - id, - label, - layout_id, - entries, - } => { - use wgc::binding_model as bm; - let entry_vec = entries - .iter() - .map(|(binding, res)| wgc::binding_model::BindGroupEntry { - binding: *binding, - resource: match *res { - trace::BindingResource::Buffer { id, offset, size } => { - bm::BindingResource::Buffer(bm::BufferBinding { - buffer_id: id, - offset, - size, - }) - } - trace::BindingResource::Sampler(id) => bm::BindingResource::Sampler(id), - trace::BindingResource::TextureView(id) => { - bm::BindingResource::TextureView(id) - } - trace::BindingResource::TextureViewArray(ref binding_array) => { - bm::BindingResource::TextureViewArray(binding_array) - } - }, - }) - .collect::>(); + A::CreateBindGroup(id, desc) => { self.device_maintain_ids::(device); - self.device_create_bind_group::( - device, - &wgc::binding_model::BindGroupDescriptor { - label: Some(&label), - layout: layout_id, - entries: &entry_vec, - }, - id, - ) - .unwrap(); + self.device_create_bind_group::(device, &desc, id) + .unwrap(); } A::DestroyBindGroup(id) => { self.bind_group_destroy::(id); @@ -298,74 +240,26 @@ impl GlobalPlay for wgc::hub::Global { A::DestroyShaderModule(id) => { self.shader_module_destroy::(id); } - A::CreateComputePipeline { id, desc } => { - let compute_stage = desc.compute_stage.to_core(); + A::CreateComputePipeline(id, desc) => { self.device_maintain_ids::(device); - self.device_create_compute_pipeline::( - device, - &wgc::pipeline::ComputePipelineDescriptor { - layout: desc.layout, - compute_stage, - }, - id, - ) - .unwrap(); + self.device_create_compute_pipeline::(device, &desc, id) + .unwrap(); } A::DestroyComputePipeline(id) => { self.compute_pipeline_destroy::(id); } - A::CreateRenderPipeline { id, desc } => { - let vertex_stage = desc.vertex_stage.to_core(); - let fragment_stage = desc.fragment_stage.as_ref().map(|fs| fs.to_core()); - let vertex_buffers = desc - .vertex_state - .vertex_buffers - .iter() - .map(|vb| wgt::VertexBufferDescriptor { - stride: vb.stride, - step_mode: vb.step_mode, - attributes: &vb.attributes, - }) - .collect::>(); + A::CreateRenderPipeline(id, desc) => { self.device_maintain_ids::(device); - self.device_create_render_pipeline::( - device, - &wgc::pipeline::RenderPipelineDescriptor { - layout: desc.layout, - vertex_stage, - fragment_stage, - primitive_topology: desc.primitive_topology, - rasterization_state: desc.rasterization_state, - color_states: &desc.color_states, - depth_stencil_state: desc.depth_stencil_state, - vertex_state: wgt::VertexStateDescriptor { - index_format: desc.vertex_state.index_format, - vertex_buffers: &vertex_buffers, - }, - sample_count: desc.sample_count, - sample_mask: desc.sample_mask, - alpha_to_coverage_enabled: desc.alpha_to_coverage_enabled, - }, - id, - ) - .unwrap(); + self.device_create_render_pipeline::(device, &desc, id) + .unwrap(); } A::DestroyRenderPipeline(id) => { self.render_pipeline_destroy::(id); } A::CreateRenderBundle { id, desc, base } => { - let label = Label::new(&desc.label); - let bundle = wgc::command::RenderBundleEncoder::new( - &wgt::RenderBundleEncoderDescriptor { - label: None, - color_formats: &desc.color_formats, - depth_stencil_format: desc.depth_stencil_format, - sample_count: desc.sample_count, - }, - device, - Some(base), - ) - .unwrap(); + let label = Label::new(&desc.label.as_ref().unwrap()); + let bundle = + wgc::command::RenderBundleEncoder::new(&desc, device, Some(base)).unwrap(); self.render_bundle_encoder_finish::( bundle, &wgt::RenderBundleDescriptor { diff --git a/player/tests/data/buffer-copy.ron b/player/tests/data/buffer-copy.ron index cd3105e6e..e0b606978 100644 --- a/player/tests/data/buffer-copy.ron +++ b/player/tests/data/buffer-copy.ron @@ -10,8 +10,8 @@ ], actions: [ CreateBuffer( - id: Id(0, 1, Empty), - desc: ( + Id(0, 1, Empty), + ( label: "", size: 16, usage: ( diff --git a/player/tests/test.rs b/player/tests/test.rs index 67132ed90..408dde336 100644 --- a/player/tests/test.rs +++ b/player/tests/test.rs @@ -35,10 +35,10 @@ struct Expectation { } #[derive(serde::Deserialize)] -struct Test { +struct Test<'a> { features: wgt::Features, expectations: Vec, - actions: Vec, + actions: Vec>, } extern "C" fn map_callback(status: wgc::resource::BufferMapAsyncStatus, _user_data: *mut u8) { @@ -48,7 +48,7 @@ extern "C" fn map_callback(status: wgc::resource::BufferMapAsyncStatus, _user_da } } -impl Test { +impl Test<'_> { fn load(path: PathBuf, backend: wgt::Backend) -> Self { let backend_name = match backend { wgt::Backend::Vulkan => "Vulkan", diff --git a/wgpu-core/src/binding_model.rs b/wgpu-core/src/binding_model.rs index 8fe874317..1669252e5 100644 --- a/wgpu-core/src/binding_model.rs +++ b/wgpu-core/src/binding_model.rs @@ -17,7 +17,10 @@ use serde::Deserialize; #[cfg(feature = "trace")] use serde::Serialize; -use std::{borrow::Borrow, ops::Range}; +use std::{ + borrow::{Borrow, Cow}, + ops::Range, +}; use thiserror::Error; @@ -396,12 +399,14 @@ pub struct BufferBinding { // Note: Duplicated in `wgpu-rs` as `BindingResource` // They're different enough that it doesn't make sense to share a common type -#[derive(Debug)] +#[derive(Debug, Clone)] +#[cfg_attr(feature = "trace", derive(serde::Serialize))] +#[cfg_attr(feature = "replay", derive(serde::Deserialize))] pub enum BindingResource<'a> { Buffer(BufferBinding), Sampler(SamplerId), TextureView(TextureViewId), - TextureViewArray(&'a [TextureViewId]), + TextureViewArray(Cow<'a, [TextureViewId]>), } pub type BindGroupEntry<'a> = wgt::BindGroupEntry>; diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index fea654cad..fadb322d2 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -922,7 +922,7 @@ impl Global { let bundle = &bundle_guard[id]; trace.lock().add(trace::Action::CreateRenderBundle { id, - desc: trace::RenderBundleDescriptor::new(desc.label, &bundle.context), + desc: trace::new_render_bundle_encoder_descriptor(desc.label, &bundle.context), base: BasePass::from_ref(bundle.base.as_ref()), }); } diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 9ba76360f..f2f9d2a92 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -731,7 +731,7 @@ impl Global { if mapped_at_creation && !desc.usage.contains(wgt::BufferUsage::MAP_WRITE) { desc.usage |= wgt::BufferUsage::COPY_DST; } - trace.lock().add(trace::Action::CreateBuffer { id, desc }) + trace.lock().add(trace::Action::CreateBuffer(id, desc)) } None => (), }; @@ -915,10 +915,9 @@ impl Global { let id = hub.textures.register_identity(id_in, texture, &mut token); #[cfg(feature = "trace")] match device.trace { - Some(ref trace) => trace.lock().add(trace::Action::CreateTexture { - id, - desc: desc.map_label(own_label), - }), + Some(ref trace) => trace + .lock() + .add(trace::Action::CreateTexture(id, desc.map_label(own_label))), None => (), }; @@ -1137,10 +1136,9 @@ impl Global { let id = hub.samplers.register_identity(id_in, sampler, &mut token); #[cfg(feature = "trace")] match device.trace { - Some(ref trace) => trace.lock().add(trace::Action::CreateSampler { - id, - desc: desc.map_label(own_label), - }), + Some(ref trace) => trace + .lock() + .add(trace::Action::CreateSampler(id, desc.map_label(own_label))), None => (), }; @@ -1185,7 +1183,7 @@ impl Global { let mut token = Token::root(); let hub = B::hub(self); let mut entry_map = FastHashMap::default(); - for entry in desc.entries { + for entry in desc.entries.iter() { if entry_map.insert(entry.binding, entry.clone()).is_some() { return Err(binding_model::BindGroupLayoutError::ConflictBinding( entry.binding, @@ -1256,7 +1254,7 @@ impl Global { .raw .create_descriptor_set_layout(&raw_bindings, &[]) .unwrap(); - if let Some(label) = desc.label { + if let Some(label) = desc.label.as_ref() { device .raw .set_descriptor_set_layout_name(&mut raw_layout, label); @@ -1296,11 +1294,9 @@ impl Global { .register_identity(id_in, layout, &mut token); #[cfg(feature = "trace")] match device.trace { - Some(ref trace) => trace.lock().add(trace::Action::CreateBindGroupLayout { - id, - label: desc.label.map_or_else(String::new, str::to_string), - entries: desc.entries.to_owned(), - }), + Some(ref trace) => trace + .lock() + .add(trace::Action::CreateBindGroupLayout(id, desc.clone())), None => (), }; Ok(id) @@ -1400,7 +1396,7 @@ impl Global { let mut count_validator = binding_model::BindingTypeMaxCountValidator::default(); let pipeline_layout = { let (bind_group_layout_guard, _) = hub.bind_group_layouts.read(&mut token); - for &id in desc.bind_group_layouts { + for &id in desc.bind_group_layouts.iter() { let bind_group_layout = &bind_group_layout_guard[id]; count_validator.merge(&bind_group_layout.count_validator); } @@ -1448,11 +1444,9 @@ impl Global { .register_identity(id_in, layout, &mut token); #[cfg(feature = "trace")] match device.trace { - Some(ref trace) => trace.lock().add(trace::Action::CreatePipelineLayout { - id, - bind_group_layouts: desc.bind_group_layouts.to_owned(), - push_constant_ranges: desc.push_constant_ranges.iter().cloned().collect(), - }), + Some(ref trace) => trace + .lock() + .add(trace::Action::CreatePipelineLayout(id, desc.clone())), None => (), }; Ok(id) @@ -1526,7 +1520,7 @@ impl Global { }; // Set the descriptor set's label for easier debugging. - if let Some(label) = desc.label { + if let Some(label) = desc.label.as_ref() { unsafe { device .raw @@ -1551,7 +1545,7 @@ impl Global { //TODO: group writes into contiguous sections let mut writes = Vec::new(); - for entry in desc.entries { + for entry in desc.entries.iter() { let binding = entry.binding; // Find the corresponding declaration in the layout let decl = bind_group_layout @@ -1844,30 +1838,9 @@ impl Global { ); #[cfg(feature = "trace")] match device.trace { - Some(ref trace) => trace.lock().add(trace::Action::CreateBindGroup { - id, - label: desc.label.map_or_else(String::new, str::to_string), - layout_id: desc.layout, - entries: desc - .entries - .iter() - .map(|entry| { - let res = match entry.resource { - Br::Buffer(ref binding) => trace::BindingResource::Buffer { - id: binding.buffer_id, - offset: binding.offset, - size: binding.size, - }, - Br::TextureView(id) => trace::BindingResource::TextureView(id), - Br::Sampler(id) => trace::BindingResource::Sampler(id), - Br::TextureViewArray(ref binding_array) => { - trace::BindingResource::TextureViewArray(binding_array.to_vec()) - } - }; - (entry.binding, res) - }) - .collect(), - }), + Some(ref trace) => trace + .lock() + .add(trace::Action::CreateBindGroup(id, desc.clone())), None => (), }; @@ -2132,7 +2105,7 @@ impl Global { sc as u8 }; - let color_states = desc.color_states; + let color_states = &desc.color_states; let depth_stencil_state = desc.depth_stencil_state.as_ref(); let rasterization_state = desc.rasterization_state.as_ref(); @@ -2143,7 +2116,7 @@ impl Global { let mut interface = validation::StageInterface::default(); let mut validated_stages = wgt::ShaderStage::empty(); - let desc_vbs = desc.vertex_state.vertex_buffers; + let desc_vbs = &desc.vertex_state.vertex_buffers; let mut vertex_strides = Vec::with_capacity(desc_vbs.len()); let mut vertex_buffers = Vec::with_capacity(desc_vbs.len()); let mut attributes = Vec::new(); @@ -2162,8 +2135,8 @@ impl Global { InputStepMode::Instance => hal::pso::VertexInputRate::Instance(1), }, }); - let desc_atts = vb_state.attributes; - for attribute in desc_atts { + let desc_atts = &vb_state.attributes; + for attribute in desc_atts.iter() { if attribute.offset >= 0x10000000 { return Err( pipeline::RenderPipelineError::InvalidVertexAttributeOffset { @@ -2276,7 +2249,7 @@ impl Global { }; let vertex = { - let entry_point_name = desc.vertex_stage.entry_point; + let entry_point_name = &desc.vertex_stage.entry_point; let shader_module = &shader_module_guard[desc.vertex_stage.module]; @@ -2285,7 +2258,7 @@ impl Global { interface = validation::check_stage( module, &group_layouts, - entry_point_name, + &entry_point_name, ExecutionModel::Vertex, interface, ) @@ -2294,7 +2267,7 @@ impl Global { } hal::pso::EntryPoint:: { - entry: entry_point_name, // TODO + entry: &entry_point_name, // TODO module: &shader_module.raw, specialization: hal::pso::Specialization::EMPTY, } @@ -2302,7 +2275,7 @@ impl Global { let fragment = match &desc.fragment_stage { Some(stage) => { - let entry_point_name = stage.entry_point; + let entry_point_name = &stage.entry_point; let shader_module = &shader_module_guard[stage.module]; @@ -2312,7 +2285,7 @@ impl Global { interface = validation::check_stage( module, &group_layouts, - entry_point_name, + &entry_point_name, ExecutionModel::Fragment, interface, ) @@ -2324,7 +2297,7 @@ impl Global { } Some(hal::pso::EntryPoint:: { - entry: entry_point_name, + entry: &entry_point_name, module: &shader_module.raw, specialization: hal::pso::Specialization::EMPTY, }) @@ -2408,7 +2381,7 @@ impl Global { }; let mut flags = pipeline::PipelineFlags::empty(); - for state in color_states { + for state in color_states.iter() { if state.color_blend.uses_color() | state.alpha_blend.uses_color() { flags |= pipeline::PipelineFlags::BLEND_COLOR; } @@ -2445,35 +2418,9 @@ impl Global { #[cfg(feature = "trace")] match device.trace { - Some(ref trace) => trace.lock().add(trace::Action::CreateRenderPipeline { - id, - desc: trace::RenderPipelineDescriptor { - layout: desc.layout, - vertex_stage: trace::ProgrammableStageDescriptor::new(&desc.vertex_stage), - fragment_stage: desc - .fragment_stage - .as_ref() - .map(trace::ProgrammableStageDescriptor::new), - primitive_topology: desc.primitive_topology, - rasterization_state: rasterization_state.cloned(), - color_states: color_states.to_vec(), - depth_stencil_state: depth_stencil_state.cloned(), - vertex_state: trace::VertexStateDescriptor { - index_format: desc.vertex_state.index_format, - vertex_buffers: desc_vbs - .iter() - .map(|vbl| trace::VertexBufferDescriptor { - stride: vbl.stride, - step_mode: vbl.step_mode, - attributes: vbl.attributes.to_owned(), - }) - .collect(), - }, - sample_count: desc.sample_count, - sample_mask: desc.sample_mask, - alpha_to_coverage_enabled: desc.alpha_to_coverage_enabled, - }, - }), + Some(ref trace) => trace + .lock() + .add(trace::Action::CreateRenderPipeline(id, desc.clone())), None => (), }; Ok(id) @@ -2530,7 +2477,7 @@ impl Global { let pipeline_stage = &desc.compute_stage; let (shader_module_guard, _) = hub.shader_modules.read(&mut token); - let entry_point_name = pipeline_stage.entry_point; + let entry_point_name = &pipeline_stage.entry_point; let shader_module = &shader_module_guard[pipeline_stage.module]; @@ -2538,7 +2485,7 @@ impl Global { let _ = validation::check_stage( module, &group_layouts, - entry_point_name, + &entry_point_name, ExecutionModel::GLCompute, interface, ) @@ -2546,7 +2493,7 @@ impl Global { } let shader = hal::pso::EntryPoint:: { - entry: entry_point_name, // TODO + entry: &entry_point_name, // TODO module: &shader_module.raw, specialization: hal::pso::Specialization::EMPTY, }; @@ -2590,13 +2537,9 @@ impl Global { #[cfg(feature = "trace")] match device.trace { - Some(ref trace) => trace.lock().add(trace::Action::CreateComputePipeline { - id, - desc: trace::ComputePipelineDescriptor { - layout: desc.layout, - compute_stage: trace::ProgrammableStageDescriptor::new(&desc.compute_stage), - }, - }), + Some(ref trace) => trace + .lock() + .add(trace::Action::CreateComputePipeline(id, desc.clone())), None => (), }; Ok(id) @@ -2722,10 +2665,9 @@ impl Global { } #[cfg(feature = "trace")] match device.trace { - Some(ref trace) => trace.lock().add(Action::CreateSwapChain { - id: sc_id, - desc: desc.clone(), - }), + Some(ref trace) => trace + .lock() + .add(Action::CreateSwapChain(sc_id, desc.clone())), None => (), }; diff --git a/wgpu-core/src/device/trace.rs b/wgpu-core/src/device/trace.rs index 1865a574f..fb9a4ed1a 100644 --- a/wgpu-core/src/device/trace.rs +++ b/wgpu-core/src/device/trace.rs @@ -13,129 +13,30 @@ type FileName = String; pub const FILE_NAME: &str = "trace.ron"; -#[derive(Debug)] -#[cfg_attr(feature = "trace", derive(serde::Serialize))] -#[cfg_attr(feature = "replay", derive(serde::Deserialize))] -pub enum BindingResource { - Buffer { - id: id::BufferId, - offset: wgt::BufferAddress, - size: Option, - }, - Sampler(id::SamplerId), - TextureView(id::TextureViewId), - TextureViewArray(Vec), -} - -#[derive(Debug)] -#[cfg_attr(feature = "trace", derive(serde::Serialize))] -#[cfg_attr(feature = "replay", derive(serde::Deserialize))] -pub struct ProgrammableStageDescriptor { - pub module: id::ShaderModuleId, - pub entry_point: String, -} - #[cfg(feature = "trace")] -impl ProgrammableStageDescriptor { - pub fn new(desc: &crate::pipeline::ProgrammableStageDescriptor) -> Self { - ProgrammableStageDescriptor { - module: desc.module, - entry_point: desc.entry_point.to_string(), - } - } -} - -#[cfg(feature = "replay")] -impl ProgrammableStageDescriptor { - pub fn to_core(&self) -> crate::pipeline::ProgrammableStageDescriptor { - crate::pipeline::ProgrammableStageDescriptor { - module: self.module, - entry_point: &self.entry_point, - } +pub(crate) fn new_render_bundle_encoder_descriptor( + label: super::Label, + context: &super::RenderPassContext, +) -> wgt::RenderBundleEncoderDescriptor { + wgt::RenderBundleEncoderDescriptor { + label: Some(super::own_label(&label).into()), + color_formats: context.attachments.colors.to_vec().into(), + depth_stencil_format: context.attachments.depth_stencil, + sample_count: context.sample_count as u32, } } #[derive(Debug)] #[cfg_attr(feature = "trace", derive(serde::Serialize))] #[cfg_attr(feature = "replay", derive(serde::Deserialize))] -pub struct ComputePipelineDescriptor { - pub layout: id::PipelineLayoutId, - pub compute_stage: ProgrammableStageDescriptor, -} - -#[derive(Debug)] -#[cfg_attr(feature = "trace", derive(serde::Serialize))] -#[cfg_attr(feature = "replay", derive(serde::Deserialize))] -pub struct VertexBufferDescriptor { - pub stride: wgt::BufferAddress, - pub step_mode: wgt::InputStepMode, - pub attributes: Vec, -} - -#[derive(Debug)] -#[cfg_attr(feature = "trace", derive(serde::Serialize))] -#[cfg_attr(feature = "replay", derive(serde::Deserialize))] -pub struct VertexStateDescriptor { - pub index_format: wgt::IndexFormat, - pub vertex_buffers: Vec, -} - -#[derive(Debug)] -#[cfg_attr(feature = "trace", derive(serde::Serialize))] -#[cfg_attr(feature = "replay", derive(serde::Deserialize))] -pub struct RenderPipelineDescriptor { - pub layout: id::PipelineLayoutId, - pub vertex_stage: ProgrammableStageDescriptor, - pub fragment_stage: Option, - pub primitive_topology: wgt::PrimitiveTopology, - pub rasterization_state: Option, - pub color_states: Vec, - pub depth_stencil_state: Option, - pub vertex_state: VertexStateDescriptor, - pub sample_count: u32, - pub sample_mask: u32, - pub alpha_to_coverage_enabled: bool, -} - -#[derive(Debug)] -#[cfg_attr(feature = "trace", derive(serde::Serialize))] -#[cfg_attr(feature = "replay", derive(serde::Deserialize))] -pub struct RenderBundleDescriptor { - pub label: String, - pub color_formats: Vec, - pub depth_stencil_format: Option, - pub sample_count: u32, -} - -#[cfg(feature = "trace")] -impl RenderBundleDescriptor { - pub(crate) fn new(label: super::Label, context: &super::RenderPassContext) -> Self { - RenderBundleDescriptor { - label: super::own_label(&label), - color_formats: context.attachments.colors.to_vec(), - depth_stencil_format: context.attachments.depth_stencil, - sample_count: context.sample_count as u32, - } - } -} - -#[derive(Debug)] -#[cfg_attr(feature = "trace", derive(serde::Serialize))] -#[cfg_attr(feature = "replay", derive(serde::Deserialize))] -pub enum Action { +pub enum Action<'a> { Init { desc: wgt::DeviceDescriptor, backend: wgt::Backend, }, - CreateBuffer { - id: id::BufferId, - desc: wgt::BufferDescriptor, - }, + CreateBuffer(id::BufferId, wgt::BufferDescriptor), DestroyBuffer(id::BufferId), - CreateTexture { - id: id::TextureId, - desc: wgt::TextureDescriptor, - }, + CreateTexture(id::TextureId, wgt::TextureDescriptor), DestroyTexture(id::TextureId), CreateTextureView { id: id::TextureViewId, @@ -143,57 +44,55 @@ pub enum Action { desc: Option>, }, DestroyTextureView(id::TextureViewId), - CreateSampler { - id: id::SamplerId, - desc: wgt::SamplerDescriptor, - }, + CreateSampler(id::SamplerId, wgt::SamplerDescriptor), DestroySampler(id::SamplerId), - CreateSwapChain { - id: id::SwapChainId, - desc: wgt::SwapChainDescriptor, - }, + CreateSwapChain(id::SwapChainId, wgt::SwapChainDescriptor), GetSwapChainTexture { id: Option, parent_id: id::SwapChainId, }, PresentSwapChain(id::SwapChainId), - CreateBindGroupLayout { - id: id::BindGroupLayoutId, - label: String, - entries: Vec, - }, + CreateBindGroupLayout(id::BindGroupLayoutId, wgt::BindGroupLayoutDescriptor<'a>), DestroyBindGroupLayout(id::BindGroupLayoutId), - CreatePipelineLayout { - id: id::PipelineLayoutId, - bind_group_layouts: Vec, - push_constant_ranges: Vec, - }, + CreatePipelineLayout( + id::PipelineLayoutId, + wgt::PipelineLayoutDescriptor<'a, id::BindGroupLayoutId>, + ), DestroyPipelineLayout(id::PipelineLayoutId), - CreateBindGroup { - id: id::BindGroupId, - label: String, - layout_id: id::BindGroupLayoutId, - entries: std::collections::BTreeMap, - }, + CreateBindGroup( + id::BindGroupId, + wgt::BindGroupDescriptor< + 'a, + id::BindGroupLayoutId, + wgt::BindGroupEntry>, + >, + ), DestroyBindGroup(id::BindGroupId), CreateShaderModule { id: id::ShaderModuleId, data: FileName, }, DestroyShaderModule(id::ShaderModuleId), - CreateComputePipeline { - id: id::ComputePipelineId, - desc: ComputePipelineDescriptor, - }, + CreateComputePipeline( + id::ComputePipelineId, + wgt::ComputePipelineDescriptor< + id::PipelineLayoutId, + wgt::ProgrammableStageDescriptor<'a, id::ShaderModuleId>, + >, + ), DestroyComputePipeline(id::ComputePipelineId), - CreateRenderPipeline { - id: id::RenderPipelineId, - desc: RenderPipelineDescriptor, - }, + CreateRenderPipeline( + id::RenderPipelineId, + wgt::RenderPipelineDescriptor< + 'a, + id::PipelineLayoutId, + wgt::ProgrammableStageDescriptor<'a, id::ShaderModuleId>, + >, + ), DestroyRenderPipeline(id::RenderPipelineId), CreateRenderBundle { id: id::RenderBundleId, - desc: RenderBundleDescriptor, + desc: wgt::RenderBundleEncoderDescriptor<'a>, base: crate::command::BasePass, }, DestroyRenderBundle(id::RenderBundleId), diff --git a/wgpu-types/src/lib.rs b/wgpu-types/src/lib.rs index 70ac26784..72059b0b2 100644 --- a/wgpu-types/src/lib.rs +++ b/wgpu-types/src/lib.rs @@ -10,7 +10,7 @@ #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -use std::ops::Range; +use std::{borrow::Cow, ops::Range}; /// Integral type used for buffer offsets. pub type BufferAddress = u64; @@ -854,23 +854,27 @@ pub struct VertexAttributeDescriptor { } /// Describes how the vertex buffer is interpreted. -#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] +#[derive(Clone, Debug, Hash, Eq, PartialEq)] +#[cfg_attr(feature = "trace", derive(serde::Serialize))] +#[cfg_attr(feature = "replay", derive(serde::Deserialize))] pub struct VertexBufferDescriptor<'a> { /// The stride, in bytes, between elements of this buffer. pub stride: BufferAddress, /// How often this vertex buffer is "stepped" forward. pub step_mode: InputStepMode, /// The list of attributes which comprise a single vertex. - pub attributes: &'a [VertexAttributeDescriptor], + pub attributes: Cow<'a, [VertexAttributeDescriptor]>, } /// Describes vertex input state for a render pipeline. -#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] +#[derive(Clone, Debug, Hash, Eq, PartialEq)] +#[cfg_attr(feature = "trace", derive(serde::Serialize))] +#[cfg_attr(feature = "replay", derive(serde::Deserialize))] 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>], + pub vertex_buffers: Cow<'a, [VertexBufferDescriptor<'a>]>, } /// Vertex Format for a Vertex Attribute (input). @@ -1141,9 +1145,9 @@ pub enum SwapChainStatus { /// Describes the attachments of a render pass. #[derive(Clone, Debug, Default, PartialEq)] -pub struct RenderPassDescriptor<'a, C, D> { +pub struct RenderPassDescriptor<'a, C: Clone, D> { /// The color attachments of the render pass. - pub color_attachments: &'a [C], + pub color_attachments: Cow<'a, [C]>, /// The depth and stencil attachment of the render pass, if any. pub depth_stencil_attachment: Option, } @@ -1461,6 +1465,8 @@ impl SamplerDescriptor { /// Bindable resource and the slot to bind it to. #[derive(Clone, Debug, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "trace", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] pub struct BindGroupEntry { /// Slot for which binding provides resource. Corresponds to an entry of the same /// binding index in the [`BindGroupLayoutDescriptor`]. @@ -1471,29 +1477,33 @@ pub struct BindGroupEntry { /// Describes a group of bindings and the resources to be bound. #[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub struct BindGroupDescriptor<'a, L, B> { +#[cfg_attr(feature = "trace", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +pub struct BindGroupDescriptor<'a, L, B: Clone> { /// Debug label of the bind group. This will show up in graphics debuggers for easy identification. - pub label: Option<&'a str>, + pub label: Option>, /// The [`BindGroupLayout`] that corresponds to this bind group. pub layout: L, /// The resources to bind to this bind group. - pub entries: &'a [B], + pub entries: Cow<'a, [B]>, } /// Describes a pipeline layout. /// /// A `PipelineLayoutDescriptor` can be used to create a pipeline layout. #[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub struct PipelineLayoutDescriptor<'a, B> { +#[cfg_attr(feature = "trace", derive(Serialize))] +#[cfg_attr(feature = "replay", derive(Deserialize))] +pub struct PipelineLayoutDescriptor<'a, B: Clone> { /// Bind groups that this pipeline uses. The first entry will provide all the bindings for /// "set = 0", second entry will provide all the bindings for "set = 1" etc. - pub bind_group_layouts: &'a [B], + pub bind_group_layouts: Cow<'a, [B]>, /// Set of push constant ranges this pipeline uses. Each shader stage that uses push constants /// must define the range in push constant memory that corresponds to its single `layout(push_constant)` /// uniform block. /// /// If this array is non-empty, the [`Features::PUSH_CONSTANTS`] must be enabled. - pub push_constant_ranges: &'a [PushConstantRange], + pub push_constant_ranges: Cow<'a, [PushConstantRange]>, } /// A range of push constant memory to pass to a shader stage. @@ -1511,16 +1521,20 @@ pub struct PushConstantRange { /// Describes a programmable pipeline stage. #[derive(Clone, Debug, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "trace", derive(serde::Serialize))] +#[cfg_attr(feature = "replay", derive(serde::Deserialize))] pub struct ProgrammableStageDescriptor<'a, M> { /// The compiled shader module for this stage. pub module: M, /// The name of the entry point in the compiled shader. There must be a function that returns /// void with this name in the shader. - pub entry_point: &'a str, + pub entry_point: Cow<'a, str>, } /// Describes a render (graphics) pipeline. #[derive(Clone, Debug)] +#[cfg_attr(feature = "trace", derive(serde::Serialize))] +#[cfg_attr(feature = "replay", derive(serde::Deserialize))] pub struct RenderPipelineDescriptor<'a, L, D> { /// The layout of bind groups for this pipeline. pub layout: L, @@ -1533,7 +1547,7 @@ pub struct RenderPipelineDescriptor<'a, L, D> { /// The primitive topology used to interpret vertices. pub primitive_topology: PrimitiveTopology, /// The effect of draw calls on the color aspect of the output target. - pub color_states: &'a [ColorStateDescriptor], + pub color_states: Cow<'a, [ColorStateDescriptor]>, /// The effect of draw calls on the depth and stencil aspects of the output target, if any. pub depth_stencil_state: Option, /// The vertex input state for this pipeline. @@ -1555,6 +1569,8 @@ pub struct RenderPipelineDescriptor<'a, L, D> { /// Describes a compute pipeline. #[derive(Clone, Debug, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "trace", derive(serde::Serialize))] +#[cfg_attr(feature = "replay", derive(serde::Deserialize))] pub struct ComputePipelineDescriptor { /// The layout of bind groups for this pipeline. pub layout: L, @@ -1574,12 +1590,14 @@ pub struct CommandBufferDescriptor { /// Describes a [`RenderBundleEncoder`]. #[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "trace", derive(serde::Serialize))] +#[cfg_attr(feature = "replay", derive(serde::Deserialize))] pub struct RenderBundleEncoderDescriptor<'a> { /// Debug label of the render bundle encoder. This will show up in graphics debuggers for easy identification. - pub label: Option<&'a str>, + pub label: Option>, /// The formats of the color attachments that this render bundle is capable to rendering to. This /// must match the formats of the color attachments in the renderpass this render bundle is executed in. - pub color_formats: &'a [TextureFormat], + pub color_formats: Cow<'a, [TextureFormat]>, /// The formats of the depth attachment that this render bundle is capable to rendering to. This /// must match the formats of the depth attachments in the renderpass this render bundle is executed in. pub depth_stencil_format: Option, @@ -1841,12 +1859,14 @@ impl BindGroupLayoutEntry { /// Describes a [`BindGroupLayout`]. #[derive(Clone, Debug)] +#[cfg_attr(feature = "trace", derive(serde::Serialize))] +#[cfg_attr(feature = "replay", derive(serde::Deserialize))] pub struct BindGroupLayoutDescriptor<'a> { /// Debug label of the bind group layout. This will show up in graphics debuggers for easy identification. - pub label: Option<&'a str>, + pub label: Option>, /// Array of entries in this BindGroupLayout - pub entries: &'a [BindGroupLayoutEntry], + pub entries: Cow<'a, [BindGroupLayoutEntry]>, } /// View of a buffer which can be used to copy to/from a texture.