From a82062595aa4e444b0ebefcd98f41a737c61cea8 Mon Sep 17 00:00:00 2001 From: Alex S Date: Sun, 20 Jun 2021 06:53:03 +0300 Subject: [PATCH] Code review: Remove .size field from BindGroupEntry, since this information can be obtained from the layout. --- wgpu-core/src/device/mod.rs | 13 ++++++------- wgpu-hal/examples/halmark/main.rs | 4 ---- wgpu-hal/src/lib.rs | 1 - wgpu-hal/src/metal/device.rs | 7 ++++--- wgpu-hal/src/vulkan/device.rs | 20 +++++++++++++------- wgpu-hal/src/vulkan/mod.rs | 2 +- 6 files changed, 24 insertions(+), 23 deletions(-) diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 962c535c9..39a9ea0ba 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -1216,7 +1216,7 @@ impl Device { .entries .get(&binding) .ok_or(Error::MissingBindingDeclaration(binding))?; - let (res_index, size) = match entry.resource { + let res_index = match entry.resource { Br::Buffer(ref bb) => { let bb = Self::create_buffer_binding( &bb, @@ -1231,7 +1231,7 @@ impl Device { let res_index = hal_buffers.len(); hal_buffers.push(bb); - (res_index, 1) + res_index } Br::BufferArray(ref bindings_array) => { if let Some(count) = decl.count { @@ -1261,7 +1261,7 @@ impl Device { )?; hal_buffers.push(bb); } - (res_index, bindings_array.len()) + res_index } Br::Sampler(id) => { match decl.ty { @@ -1293,7 +1293,7 @@ impl Device { let res_index = hal_samplers.len(); hal_samplers.push(&sampler.raw); - (res_index, 1) + res_index } _ => { return Err(Error::WrongBindingType { @@ -1341,7 +1341,7 @@ impl Device { view: &view.raw, usage: internal_use, }); - (res_index, 1) + res_index } Br::TextureViewArray(ref bindings_array) => { if let Some(count) = decl.count { @@ -1394,14 +1394,13 @@ impl Device { }); } - (res_index, bindings_array.len()) + res_index } }; hal_entries.push(hal::BindGroupEntry { binding, resource_index: res_index as u32, - size: size as u32, }); } diff --git a/wgpu-hal/examples/halmark/main.rs b/wgpu-hal/examples/halmark/main.rs index 98839ee2f..d3cea796a 100644 --- a/wgpu-hal/examples/halmark/main.rs +++ b/wgpu-hal/examples/halmark/main.rs @@ -396,17 +396,14 @@ impl Example { hal::BindGroupEntry { binding: 0, resource_index: 0, - size: 1, }, hal::BindGroupEntry { binding: 1, resource_index: 0, - size: 1, }, hal::BindGroupEntry { binding: 2, resource_index: 0, - size: 1, }, ], }; @@ -428,7 +425,6 @@ impl Example { entries: &[hal::BindGroupEntry { binding: 0, resource_index: 0, - size: 1, }], }; unsafe { device.create_bind_group(&local_group_desc).unwrap() } diff --git a/wgpu-hal/src/lib.rs b/wgpu-hal/src/lib.rs index cc96f1da5..414240673 100644 --- a/wgpu-hal/src/lib.rs +++ b/wgpu-hal/src/lib.rs @@ -803,7 +803,6 @@ impl Clone for TextureBinding<'_, A> { pub struct BindGroupEntry { pub binding: u32, pub resource_index: u32, - pub size: u32, } /// BindGroup descriptor. diff --git a/wgpu-hal/src/metal/device.rs b/wgpu-hal/src/metal/device.rs index 2fd001a6b..3dde83019 100644 --- a/wgpu-hal/src/metal/device.rs +++ b/wgpu-hal/src/metal/device.rs @@ -570,6 +570,7 @@ impl crate::Device for super::Device { let stage_bit = map_naga_stage(stage); let mut dynamic_offsets_count = 0u32; for (entry, layout) in desc.entries.iter().zip(desc.layout.entries.iter()) { + let size = layout.count.map_or(1, |c| c.get()); if let wgt::BindingType::Buffer { has_dynamic_offset: true, .. @@ -586,7 +587,7 @@ impl crate::Device for super::Device { has_dynamic_offset, .. } => { - debug_assert_eq!(entry.size, 1); + debug_assert_eq!(size, 1); let source = &desc.buffers[entry.resource_index as usize]; let remaining_size = wgt::BufferSize::new(source.buffer.size - source.offset); @@ -616,13 +617,13 @@ impl crate::Device for super::Device { } wgt::BindingType::Texture { .. } | wgt::BindingType::StorageTexture { .. } => { let start = entry.resource_index; - let end = entry.resource_index + entry.size; + let end = start + size; bg.textures.extend( desc.textures[start as usize..end as usize] .iter() .map(|tex| tex.view.as_raw()), ); - counter.textures += entry.size; + counter.textures += size; } } } diff --git a/wgpu-hal/src/vulkan/device.rs b/wgpu-hal/src/vulkan/device.rs index f7ae1e440..0873a614a 100644 --- a/wgpu-hal/src/vulkan/device.rs +++ b/wgpu-hal/src/vulkan/device.rs @@ -866,10 +866,13 @@ impl crate::Device for super::Device { if entry.binding as usize >= types.len() { types.resize( entry.binding as usize + 1, - vk::DescriptorType::INPUT_ATTACHMENT, + (vk::DescriptorType::INPUT_ATTACHMENT, 0), ); } - types[entry.binding as usize] = conv::map_binding_type(entry.ty); + types[entry.binding as usize] = ( + conv::map_binding_type(entry.ty), + entry.count.map_or(1, |c| c.get()), + ); match entry.ty { wgt::BindingType::Buffer { @@ -910,8 +913,8 @@ impl crate::Device for super::Device { .iter() .map(|entry| vk::DescriptorSetLayoutBinding { binding: entry.binding, - descriptor_type: types[entry.binding as usize], - descriptor_count: entry.count.map_or(1, |c| c.get()), + descriptor_type: types[entry.binding as usize].0, + descriptor_count: types[entry.binding as usize].1, stage_flags: conv::map_shader_stage(entry.visibility), p_immutable_samplers: ptr::null(), }) @@ -1006,7 +1009,10 @@ impl crate::Device for super::Device { let mut sampler_infos = Vec::with_capacity(desc.samplers.len()); let mut image_infos = Vec::with_capacity(desc.textures.len()); for entry in desc.entries { - let ty = desc.layout.types[entry.binding as usize]; + let (ty, size) = desc.layout.types[entry.binding as usize]; + if size == 0 { + continue; // empty slot + } let mut write = vk::WriteDescriptorSet::builder() .dst_set(*set.raw()) .dst_binding(entry.binding) @@ -1024,7 +1030,7 @@ impl crate::Device for super::Device { vk::DescriptorType::SAMPLED_IMAGE | vk::DescriptorType::STORAGE_IMAGE => { let index = image_infos.len(); let start = entry.resource_index; - let end = entry.resource_index + entry.size; + let end = start + size; image_infos.extend(desc.textures[start as usize..end as usize].iter().map( |binding| { let layout = @@ -1043,7 +1049,7 @@ impl crate::Device for super::Device { | vk::DescriptorType::STORAGE_BUFFER_DYNAMIC => { let index = buffer_infos.len(); let start = entry.resource_index; - let end = entry.resource_index + entry.size; + let end = start + size; buffer_infos.extend(desc.buffers[start as usize..end as usize].iter().map( |binding| { vk::DescriptorBufferInfo::builder() diff --git a/wgpu-hal/src/vulkan/mod.rs b/wgpu-hal/src/vulkan/mod.rs index c8feb4b18..ffc6d66c6 100644 --- a/wgpu-hal/src/vulkan/mod.rs +++ b/wgpu-hal/src/vulkan/mod.rs @@ -271,7 +271,7 @@ pub struct Sampler { pub struct BindGroupLayout { raw: vk::DescriptorSetLayout, desc_count: gpu_descriptor::DescriptorTotalCount, - types: Vec, + types: Vec<(vk::DescriptorType, u32)>, } #[derive(Debug)]