From 904ec7a5ee82ac044dd34d4251a84fc48cf62039 Mon Sep 17 00:00:00 2001 From: Chandler Newman Date: Fri, 12 Mar 2021 18:14:43 +0000 Subject: [PATCH] Add uniform and storage buffer arrays --- wgpu-core/src/binding_model.rs | 1 + wgpu-core/src/device/mod.rs | 267 +++++++++++++++++++++------------ wgpu-core/src/instance.rs | 44 ++++++ wgpu-types/src/lib.rs | 91 +++++++++++ 4 files changed, 306 insertions(+), 97 deletions(-) diff --git a/wgpu-core/src/binding_model.rs b/wgpu-core/src/binding_model.rs index fde12104c..622e6c97b 100644 --- a/wgpu-core/src/binding_model.rs +++ b/wgpu-core/src/binding_model.rs @@ -603,6 +603,7 @@ pub struct BufferBinding { #[cfg_attr(feature = "replay", derive(serde::Deserialize))] pub enum BindingResource<'a> { Buffer(BufferBinding), + BufferArray(Cow<'a, [BufferBinding]>), Sampler(SamplerId), TextureView(TextureViewId), TextureViewArray(Cow<'a, [TextureViewId]>), diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index e0c0f7eab..29ab4115a 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -1183,22 +1183,34 @@ impl Device { ty: wgt::BufferBindingType::Uniform, has_dynamic_offset: false, min_binding_size: _, - } => (&mut desc_count.uniform_buffer, None), + } => ( + &mut desc_count.uniform_buffer, + Some(wgt::Features::BUFFER_BINDING_ARRAY), + ), Bt::Buffer { ty: wgt::BufferBindingType::Uniform, has_dynamic_offset: true, min_binding_size: _, - } => (&mut desc_count.uniform_buffer_dynamic, None), + } => ( + &mut desc_count.uniform_buffer_dynamic, + Some(wgt::Features::BUFFER_BINDING_ARRAY), + ), Bt::Buffer { ty: wgt::BufferBindingType::Storage { .. }, has_dynamic_offset: false, min_binding_size: _, - } => (&mut desc_count.storage_buffer, None), + } => ( + &mut desc_count.storage_buffer, + Some(wgt::Features::BUFFER_BINDING_ARRAY), + ), Bt::Buffer { ty: wgt::BufferBindingType::Storage { .. }, has_dynamic_offset: true, min_binding_size: _, - } => (&mut desc_count.storage_buffer_dynamic, None), + } => ( + &mut desc_count.storage_buffer_dynamic, + Some(wgt::Features::BUFFER_BINDING_ARRAY), + ), Bt::Sampler { .. } => (&mut desc_count.sampler, None), Bt::Texture { .. } => ( &mut desc_count.sampled_image, @@ -1274,6 +1286,120 @@ impl Device { }) } + #[allow(clippy::too_many_arguments)] + fn create_buffer_descriptor<'a>( + bb: &binding_model::BufferBinding, + binding: u32, + decl: &wgt::BindGroupLayoutEntry, + used_buffer_ranges: &mut Vec>, + dynamic_binding_info: &mut Vec, + used: &mut TrackerSet, + storage: &'a Storage, id::BufferId>, + limits: &wgt::Limits, + ) -> Result, binding_model::CreateBindGroupError> { + use crate::binding_model::CreateBindGroupError as Error; + + let (binding_ty, dynamic, min_size) = match decl.ty { + wgt::BindingType::Buffer { + ty, + has_dynamic_offset, + min_binding_size, + } => (ty, has_dynamic_offset, min_binding_size), + _ => { + return Err(Error::WrongBindingType { + binding, + actual: decl.ty, + expected: "UniformBuffer, StorageBuffer or ReadonlyStorageBuffer", + }) + } + }; + let (pub_usage, internal_use, range_limit) = match binding_ty { + wgt::BufferBindingType::Uniform => ( + wgt::BufferUsage::UNIFORM, + resource::BufferUse::UNIFORM, + limits.max_uniform_buffer_binding_size, + ), + wgt::BufferBindingType::Storage { read_only } => ( + wgt::BufferUsage::STORAGE, + if read_only { + resource::BufferUse::STORAGE_LOAD + } else { + resource::BufferUse::STORAGE_STORE + }, + limits.max_storage_buffer_binding_size, + ), + }; + + if bb.offset % wgt::BIND_BUFFER_ALIGNMENT != 0 { + return Err(Error::UnalignedBufferOffset(bb.offset)); + } + + let buffer = used + .buffers + .use_extend(storage, bb.buffer_id, (), internal_use) + .map_err(|_| Error::InvalidBuffer(bb.buffer_id))?; + check_buffer_usage(buffer.usage, pub_usage)?; + let &(ref buffer_raw, _) = buffer + .raw + .as_ref() + .ok_or(Error::InvalidBuffer(bb.buffer_id))?; + + let (bind_size, bind_end) = match bb.size { + Some(size) => { + let end = bb.offset + size.get(); + if end > buffer.size { + return Err(Error::BindingRangeTooLarge { + buffer: bb.buffer_id, + range: bb.offset..end, + size: buffer.size, + }); + } + (size.get(), end) + } + None => (buffer.size - bb.offset, buffer.size), + }; + + if bind_size > range_limit as u64 { + return Err(Error::BufferRangeTooLarge { + binding, + given: bind_size as u32, + limit: range_limit, + }); + } + + // Record binding info for validating dynamic offsets + if dynamic { + dynamic_binding_info.push(binding_model::BindGroupDynamicBindingData { + maximum_dynamic_offset: buffer.size - bind_end, + }); + } + + if let Some(non_zero) = min_size { + let min_size = non_zero.get(); + if min_size > bind_size { + return Err(Error::BindingSizeTooSmall { + buffer: bb.buffer_id, + actual: bind_size, + min: min_size, + }); + } + } else if bind_size == 0 { + return Err(Error::BindingZeroSize(bb.buffer_id)); + } + + used_buffer_ranges.push(MemoryInitTrackerAction { + id: bb.buffer_id, + range: bb.offset..(bb.offset + bind_size), + kind: MemoryInitKind::NeedsInitializedMemory, + }); + + let sub_range = hal::buffer::SubRange { + offset: bb.offset, + size: Some(bind_size), + }; + Ok(hal::pso::Descriptor::Buffer(buffer_raw, sub_range)) + } + fn create_bind_group( &self, self_id: id::DeviceId, @@ -1317,105 +1443,52 @@ impl Device { .ok_or(Error::MissingBindingDeclaration(binding))?; let descriptors: SmallVec<[_; 1]> = match entry.resource { Br::Buffer(ref bb) => { - let (binding_ty, dynamic, min_size) = match decl.ty { - wgt::BindingType::Buffer { - ty, - has_dynamic_offset, - min_binding_size, - } => (ty, has_dynamic_offset, min_binding_size), - _ => { - return Err(Error::WrongBindingType { - binding, - actual: decl.ty, - expected: "UniformBuffer, StorageBuffer or ReadonlyStorageBuffer", - }) - } - }; - let (pub_usage, internal_use, range_limit) = match binding_ty { - wgt::BufferBindingType::Uniform => ( - wgt::BufferUsage::UNIFORM, - resource::BufferUse::UNIFORM, - self.limits.max_uniform_buffer_binding_size, - ), - wgt::BufferBindingType::Storage { read_only } => ( - wgt::BufferUsage::STORAGE, - if read_only { - resource::BufferUse::STORAGE_LOAD - } else { - resource::BufferUse::STORAGE_STORE - }, - self.limits.max_storage_buffer_binding_size, - ), - }; - - if bb.offset % wgt::BIND_BUFFER_ALIGNMENT != 0 { - return Err(Error::UnalignedBufferOffset(bb.offset)); + let buffer_desc = Self::create_buffer_descriptor( + &bb, + binding, + &decl, + &mut used_buffer_ranges, + &mut dynamic_binding_info, + &mut used, + &*buffer_guard, + &self.limits, + )?; + SmallVec::from([buffer_desc]) + } + Br::BufferArray(ref bindings_array) => { + let required_feats = wgt::Features::BUFFER_BINDING_ARRAY; + if !self.features.contains(required_feats) { + return Err(Error::MissingFeatures(required_feats)); } - let buffer = used - .buffers - .use_extend(&*buffer_guard, bb.buffer_id, (), internal_use) - .map_err(|_| Error::InvalidBuffer(bb.buffer_id))?; - check_buffer_usage(buffer.usage, pub_usage)?; - let &(ref buffer_raw, _) = buffer - .raw - .as_ref() - .ok_or(Error::InvalidBuffer(bb.buffer_id))?; - - let (bind_size, bind_end) = match bb.size { - Some(size) => { - let end = bb.offset + size.get(); - if end > buffer.size { - return Err(Error::BindingRangeTooLarge { - buffer: bb.buffer_id, - range: bb.offset..end, - size: buffer.size, - }); - } - (size.get(), end) - } - None => (buffer.size - bb.offset, buffer.size), - }; - - if bind_size > range_limit as u64 { - return Err(Error::BufferRangeTooLarge { - binding, - given: bind_size as u32, - limit: range_limit, - }); - } - - // Record binding info for validating dynamic offsets - if dynamic { - dynamic_binding_info.push(binding_model::BindGroupDynamicBindingData { - maximum_dynamic_offset: buffer.size - bind_end, - }); - } - - if let Some(non_zero) = min_size { - let min_size = non_zero.get(); - if min_size > bind_size { - return Err(Error::BindingSizeTooSmall { - buffer: bb.buffer_id, - actual: bind_size, - min: min_size, + if let Some(count) = decl.count { + let count = count.get() as usize; + let num_bindings = bindings_array.len(); + if count != num_bindings { + return Err(Error::BindingArrayLengthMismatch { + actual: num_bindings, + expected: count, }); } - } else if bind_size == 0 { - return Err(Error::BindingZeroSize(bb.buffer_id)); + } else { + return Err(Error::SingleBindingExpected); } - used_buffer_ranges.push(MemoryInitTrackerAction { - id: bb.buffer_id, - range: bb.offset..(bb.offset + bind_size), - kind: MemoryInitKind::NeedsInitializedMemory, - }); - - let sub_range = hal::buffer::SubRange { - offset: bb.offset, - size: Some(bind_size), - }; - SmallVec::from([hal::pso::Descriptor::Buffer(buffer_raw, sub_range)]) + bindings_array + .iter() + .map(|bb| { + Self::create_buffer_descriptor( + &bb, + binding, + &decl, + &mut used_buffer_ranges, + &mut dynamic_binding_info, + &mut used, + &*buffer_guard, + &self.limits, + ) + }) + .collect::>()? } Br::Sampler(id) => { match decl.ty { diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index e836a0b9a..fa832c89a 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -197,6 +197,26 @@ impl Adapter { wgt::Features::CONSERVATIVE_RASTERIZATION, adapter_features.contains(hal::Features::CONSERVATIVE_RASTERIZATION), ); + features.set( + wgt::Features::BUFFER_BINDING_ARRAY, + adapter_features.contains(hal::Features::BUFFER_DESCRIPTOR_ARRAY), + ); + features.set( + wgt::Features::UNIFORM_BUFFER_ARRAY_DYNAMIC_INDEXING, + adapter_features.contains(hal::Features::SHADER_UNIFORM_BUFFER_ARRAY_DYNAMIC_INDEXING), + ); + features.set( + wgt::Features::UNIFORM_BUFFER_ARRAY_NON_UNIFORM_INDEXING, + adapter_features.contains(hal::Features::UNIFORM_BUFFER_DESCRIPTOR_INDEXING), + ); + features.set( + wgt::Features::STORAGE_BUFFER_ARRAY_DYNAMIC_INDEXING, + adapter_features.contains(hal::Features::SHADER_STORAGE_BUFFER_ARRAY_DYNAMIC_INDEXING), + ); + features.set( + wgt::Features::STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING, + adapter_features.contains(hal::Features::STORAGE_BUFFER_DESCRIPTOR_INDEXING), + ); #[cfg(not(target_os = "ios"))] //TODO: https://github.com/gfx-rs/gfx/issues/3346 features.set(wgt::Features::ADDRESS_MODE_CLAMP_TO_BORDER, true); @@ -514,6 +534,30 @@ impl Adapter { desc.features .contains(wgt::Features::CONSERVATIVE_RASTERIZATION), ); + enabled_features.set( + hal::Features::BUFFER_DESCRIPTOR_ARRAY, + desc.features.contains(wgt::Features::BUFFER_BINDING_ARRAY), + ); + enabled_features.set( + hal::Features::SHADER_UNIFORM_BUFFER_ARRAY_DYNAMIC_INDEXING, + desc.features + .contains(wgt::Features::UNIFORM_BUFFER_ARRAY_DYNAMIC_INDEXING), + ); + enabled_features.set( + hal::Features::UNIFORM_BUFFER_DESCRIPTOR_INDEXING, + desc.features + .contains(wgt::Features::UNIFORM_BUFFER_ARRAY_NON_UNIFORM_INDEXING), + ); + enabled_features.set( + hal::Features::SHADER_STORAGE_BUFFER_ARRAY_DYNAMIC_INDEXING, + desc.features + .contains(wgt::Features::STORAGE_BUFFER_ARRAY_DYNAMIC_INDEXING), + ); + enabled_features.set( + hal::Features::STORAGE_BUFFER_DESCRIPTOR_INDEXING, + desc.features + .contains(wgt::Features::STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING), + ); let family = self .raw diff --git a/wgpu-types/src/lib.rs b/wgpu-types/src/lib.rs index a1f2762ec..09de39356 100644 --- a/wgpu-types/src/lib.rs +++ b/wgpu-types/src/lib.rs @@ -435,6 +435,97 @@ bitflags::bitflags! { /// /// This is a native only feature. const CONSERVATIVE_RASTERIZATION = 0x0000_0000_8000_0000; + /// Allows the user to create arrays of buffers in shaders: + /// + /// eg. `uniform myBuffer { .... } buffer_array[10]`. + /// + /// This capability allows them to exist and to be indexed by compile time constant + /// values. + /// + /// Supported platforms: + /// - DX12 + /// - Vulkan + /// + /// This is a native only feature. + const BUFFER_BINDING_ARRAY = 0x0000_0001_0000_0000; + /// Allows shaders to index uniform buffer arrays with dynamically uniform values: + /// + /// eg. `buffer_array[uniform_value]` + /// + /// This capability means the hardware will also support BUFFER_BINDING_ARRAY. + /// + /// Supported platforms: + /// - DX12 + /// - Vulkan's shaderUniformBufferArrayDynamicIndexing feature + /// + /// This is a native only feature. + const UNIFORM_BUFFER_ARRAY_DYNAMIC_INDEXING = 0x0000_0002_0000_0000; + /// Allows shaders to index uniform buffer arrays with dynamically non-uniform values: + /// + /// eg. `buffer_array[vertex_data]` + /// + /// In order to use this capability, the corresponding GLSL extension must be enabled like so: + /// + /// `#extension GL_EXT_nonuniform_qualifier : require` + /// + /// and then used either as `nonuniformEXT` qualifier in variable declaration: + /// + /// eg. `layout(location = 0) nonuniformEXT flat in int vertex_data;` + /// + /// or as `nonuniformEXT` constructor: + /// + /// eg. `buffer_array[nonuniformEXT(vertex_data)]` + /// + /// HLSL does not need any extension. + /// + /// This capability means the hardware will also support UNIFORM_BUFFER_ARRAY_DYNAMIC_INDEXING + /// and BUFFER_BINDING_ARRAY. + /// + /// Supported platforms: + /// - DX12 + /// - Vulkan 1.2+ (or VK_EXT_descriptor_indexing)'s shaderUniformBufferArrayNonUniformIndexing feature) + /// + /// This is a native only feature. + const UNIFORM_BUFFER_ARRAY_NON_UNIFORM_INDEXING = 0x0000_0004_0000_0000; + /// Allows shaders to index storage buffer arrays with dynamically uniform values: + /// + /// eg. `buffer_array[uniform_value]` + /// + /// This capability means the hardware will also support BUFFER_BINDING_ARRAY. + /// + /// Supported platforms: + /// - DX12 + /// - Vulkan's shaderStorageBufferArrayDynamicIndexing feature + /// + /// This is a native only feature. + const STORAGE_BUFFER_ARRAY_DYNAMIC_INDEXING = 0x0000_0008_0000_0000; + /// Allows shaders to index storage buffer arrays with dynamically non-uniform values: + /// + /// eg. `buffer_array[vertex_data]` + /// + /// In order to use this capability, the corresponding GLSL extension must be enabled like so: + /// + /// `#extension GL_EXT_nonuniform_qualifier : require` + /// + /// and then used either as `nonuniformEXT` qualifier in variable declaration: + /// + /// eg. `layout(location = 0) nonuniformEXT flat in int vertex_data;` + /// + /// or as `nonuniformEXT` constructor: + /// + /// eg. `buffer_array[nonuniformEXT(vertex_data)]` + /// + /// HLSL does not need any extension. + /// + /// This capability means the hardware will also support STORAGE_BUFFER_ARRAY_DYNAMIC_INDEXING + /// and BUFFER_BINDING_ARRAY. + /// + /// Supported platforms: + /// - DX12 + /// - Vulkan 1.2+ (or VK_EXT_descriptor_indexing)'s shaderStorageBufferArrayNonUniformIndexing feature) + /// + /// This is a native only feature. + const STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING = 0x0000_0010_0000_0000; /// Features which are part of the upstream WebGPU standard. const ALL_WEBGPU = 0x0000_0000_0000_FFFF; /// Features that are only available when targeting native (not web).