diff --git a/deno_webgpu/01_webgpu.js b/deno_webgpu/01_webgpu.js index 3e5f98a05..2aa056ae3 100644 --- a/deno_webgpu/01_webgpu.js +++ b/deno_webgpu/01_webgpu.js @@ -271,8 +271,7 @@ ObjectDefineProperty(GPUSupportedLimitsPrototype, privateCustomInspect, { "maxTextureDimension3D", "maxTextureArrayLayers", "maxBindGroups", - // TODO(@crowlKats): support max_bind_groups_plus_vertex_buffers - // "maxBindGroupsPlusVertexBuffers", + "maxBindGroupsPlusVertexBuffers", "maxBindingsPerBindGroup", "maxDynamicUniformBuffersPerPipelineLayout", "maxDynamicStorageBuffersPerPipelineLayout", diff --git a/deno_webgpu/adapter.rs b/deno_webgpu/adapter.rs index 4de33c698..348eb72d5 100644 --- a/deno_webgpu/adapter.rs +++ b/deno_webgpu/adapter.rs @@ -297,7 +297,10 @@ impl GPUSupportedLimits { self.0.max_bind_groups } - // TODO(@crowlKats): support max_bind_groups_plus_vertex_buffers + #[getter] + fn maxBindGroupsPlusVertexBuffers(&self) -> u32 { + self.0.max_bind_groups_plus_vertex_buffers + } #[getter] fn maxBindingsPerBindGroup(&self) -> u32 { diff --git a/wgpu-core/src/command/bind.rs b/wgpu-core/src/command/bind.rs index c07339345..0cd00ef3e 100644 --- a/wgpu-core/src/command/bind.rs +++ b/wgpu-core/src/command/bind.rs @@ -48,6 +48,10 @@ mod compat { } } + fn is_assigned(&self) -> bool { + self.assigned.is_some() + } + fn is_active(&self) -> bool { self.assigned.is_some() && self.expected.is_some() } @@ -250,6 +254,13 @@ mod compat { self.entries[index].assigned = None; } + pub fn list_assigned(&self) -> impl Iterator + '_ { + self.entries + .iter() + .enumerate() + .filter_map(|(i, e)| if e.is_assigned() { Some(i) } else { None }) + } + pub fn list_active(&self) -> impl Iterator + '_ { self.entries .iter() @@ -439,6 +450,10 @@ impl Binder { }) } + pub(super) fn last_assigned_index(&self) -> Option { + self.manager.list_assigned().last() + } + pub(super) fn list_active(&self) -> impl Iterator> + '_ { let payloads = &self.payloads; self.manager diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index cf4f0311f..645f7481f 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -1527,6 +1527,26 @@ impl State { } } + let bind_group_space_used = self.binder.last_assigned_index().map_or(0, |i| i + 1); + let vertex_buffer_space_used = self + .vertex + .iter() + .enumerate() + .filter_map(|(i, state)| state.as_ref().map(|_| i)) + .next_back() + .map_or(0, |i| i + 1); + + let bind_groups_plus_vertex_buffers = + u32::try_from(bind_group_space_used + vertex_buffer_space_used).unwrap(); + if bind_groups_plus_vertex_buffers + > self.device.limits.max_bind_groups_plus_vertex_buffers + { + return Err(DrawError::TooManyBindGroupsPlusVertexBuffers { + given: bind_groups_plus_vertex_buffers, + limit: self.device.limits.max_bind_groups_plus_vertex_buffers, + }); + } + if family == DrawCommandFamily::DrawIndexed { let pipeline = &pipeline.pipeline; let index_format = match &self.index { diff --git a/wgpu-core/src/command/draw.rs b/wgpu-core/src/command/draw.rs index 0afd1b899..1e7f28145 100644 --- a/wgpu-core/src/command/draw.rs +++ b/wgpu-core/src/command/draw.rs @@ -74,6 +74,8 @@ pub enum DrawError { MissingImmediateData { missing: naga::valid::ImmediateSlots, }, + #[error("The number of bind groups + vertex buffers {given} exceeds the limit {limit}")] + TooManyBindGroupsPlusVertexBuffers { given: u32, limit: u32 }, } impl WebGpuError for DrawError { diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 6a7179075..4dc7a1614 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -566,6 +566,27 @@ impl<'scope, 'snatch_guard, 'cmd_enc> State<'scope, 'snatch_guard, 'cmd_enc> { } } + let bind_group_space_used = self.pass.binder.last_assigned_index().map_or(0, |i| i + 1); + let vertex_buffer_space_used = self + .vertex + .buffer_sizes + .iter() + .enumerate() + .filter_map(|(i, size)| size.map(|_| i)) + .next_back() + .map_or(0, |i| i + 1); + + let bind_groups_plus_vertex_buffers = + u32::try_from(bind_group_space_used + vertex_buffer_space_used).unwrap(); + if bind_groups_plus_vertex_buffers + > pipeline.device.limits.max_bind_groups_plus_vertex_buffers + { + return Err(DrawError::TooManyBindGroupsPlusVertexBuffers { + given: bind_groups_plus_vertex_buffers, + limit: pipeline.device.limits.max_bind_groups_plus_vertex_buffers, + }); + } + if family == DrawCommandFamily::DrawIndexed { // Pipeline expects an index buffer // We have a buffer bound diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 47fba38c8..b8809a41e 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -4687,6 +4687,20 @@ impl Device { } }; + if let pipeline::RenderPipelineVertexProcessor::Vertex(ref vertex) = desc.vertex { + let bind_groups_plus_vertex_buffers = + u32::try_from(pipeline_layout.bind_group_layouts.len() + vertex.buffers.len()) + .unwrap(); + if bind_groups_plus_vertex_buffers > self.limits.max_bind_groups_plus_vertex_buffers { + return Err( + pipeline::CreateRenderPipelineError::TooManyBindGroupsPlusVertexBuffers { + given: bind_groups_plus_vertex_buffers, + limit: self.limits.max_bind_groups_plus_vertex_buffers, + }, + ); + } + } + // Multiview is only supported if the feature is enabled if let Some(mv_mask) = desc.multiview_mask { self.require_features(wgt::Features::MULTIVIEW)?; diff --git a/wgpu-core/src/limits.rs b/wgpu-core/src/limits.rs index 5448d8026..db9013b35 100644 --- a/wgpu-core/src/limits.rs +++ b/wgpu-core/src/limits.rs @@ -291,7 +291,7 @@ const UPLEVEL: Bucket = Bucket { name: "uplevel-defaults", limits: Limits { max_bind_groups: 8, - // wgpu does not implement max_bind_groups_plus_vertex_buffers + // use default max_bind_groups_plus_vertex_buffers // use default max_bindings_per_bind_group max_buffer_size: 1 << 30, // 1 GB max_color_attachment_bytes_per_sample: 64, diff --git a/wgpu-core/src/pipeline.rs b/wgpu-core/src/pipeline.rs index 64d111a95..6a8bfb960 100644 --- a/wgpu-core/src/pipeline.rs +++ b/wgpu-core/src/pipeline.rs @@ -698,6 +698,8 @@ pub enum CreateRenderPipelineError { InvalidSampleCount(u32), #[error("The number of vertex buffers {given} exceeds the limit {limit}")] TooManyVertexBuffers { given: u32, limit: u32 }, + #[error("The number of bind groups + vertex buffers {given} exceeds the limit {limit}")] + TooManyBindGroupsPlusVertexBuffers { given: u32, limit: u32 }, #[error("The total number of vertex attributes {given} exceeds the limit {limit}")] TooManyVertexAttributes { given: u32, limit: u32 }, #[error("Vertex attribute location {given} must be less than limit {limit}")] @@ -778,6 +780,7 @@ impl WebGpuError for CreateRenderPipelineError { | Self::DepthStencilState(_) | Self::InvalidSampleCount(_) | Self::TooManyVertexBuffers { .. } + | Self::TooManyBindGroupsPlusVertexBuffers { .. } | Self::TooManyVertexAttributes { .. } | Self::VertexAttributeLocationTooLarge { .. } | Self::VertexStrideTooLarge { .. } diff --git a/wgpu-hal/src/auxil/mod.rs b/wgpu-hal/src/auxil/mod.rs index 9d3d3d06c..128e200e1 100644 --- a/wgpu-hal/src/auxil/mod.rs +++ b/wgpu-hal/src/auxil/mod.rs @@ -128,6 +128,11 @@ pub(crate) fn adjust_raw_limits(mut limits: wgt::Limits) -> wgt::Limits { limits.max_vertex_buffers = limits .max_vertex_buffers .min(crate::MAX_VERTEX_BUFFERS as u32); + // Once we allow the 2 limits above to be higher than 24 we should use + // `cap_limits_to_be_under_the_sum_limit` to cap them under + // `max_bind_groups_plus_vertex_buffers`. + const { assert!(crate::MAX_BIND_GROUPS + crate::MAX_VERTEX_BUFFERS == 24) }; + limits.max_bind_groups_plus_vertex_buffers = limits.max_bind_groups_plus_vertex_buffers.min(24); limits.max_color_attachments = limits .max_color_attachments .min(crate::MAX_COLOR_ATTACHMENTS as u32); diff --git a/wgpu-hal/src/dx12/adapter.rs b/wgpu-hal/src/dx12/adapter.rs index e21e303c1..2d11c3de9 100644 --- a/wgpu-hal/src/dx12/adapter.rs +++ b/wgpu-hal/src/dx12/adapter.rs @@ -894,7 +894,9 @@ impl super::Adapter { max_texture_dimension_3d: Direct3D12::D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION, // 2048 max_texture_array_layers: Direct3D12::D3D12_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION, - // No real limit. + // No limit. + max_bind_groups_plus_vertex_buffers: u32::MAX, + // No limit. max_bindings_per_bind_group: u32::MAX, max_sampled_textures_per_shader_stage, max_samplers_per_shader_stage, diff --git a/wgpu-hal/src/gles/adapter.rs b/wgpu-hal/src/gles/adapter.rs index 3d793d52e..d8f14d42e 100644 --- a/wgpu-hal/src/gles/adapter.rs +++ b/wgpu-hal/src/gles/adapter.rs @@ -714,8 +714,10 @@ impl super::Adapter { max_texture_array_layers: unsafe { gl.get_parameter_i32(glow::MAX_ARRAY_TEXTURE_LAYERS) } as u32, - max_bind_groups: crate::MAX_BIND_GROUPS as u32, - // No real limit. + max_bind_groups: u32::MAX, + // No limit. + max_bind_groups_plus_vertex_buffers: u32::MAX, + // No limit. max_bindings_per_bind_group: u32::MAX, max_dynamic_uniform_buffers_per_pipeline_layout: max_uniform_buffers_per_shader_stage, max_dynamic_storage_buffers_per_pipeline_layout: max_storage_buffers_per_shader_stage, diff --git a/wgpu-hal/src/metal/adapter.rs b/wgpu-hal/src/metal/adapter.rs index e26cb3fcc..fe7ed916b 100644 --- a/wgpu-hal/src/metal/adapter.rs +++ b/wgpu-hal/src/metal/adapter.rs @@ -1316,9 +1316,11 @@ impl super::CapabilitiesQuery { max_texture_dimension_2d: self.max_texture_size as u32, max_texture_dimension_3d: self.max_texture_3d_size as u32, max_texture_array_layers: self.max_texture_layers as u32, - // No real limit. - max_bind_groups: 8, - // No real limit. + // No limit. + max_bind_groups: u32::MAX, + // No limit. Once we start using argument buffers we should set this appropriately. + max_bind_groups_plus_vertex_buffers: u32::MAX, + // No limit. max_bindings_per_bind_group: u32::MAX, // No limit, use maxUniformBuffersPerShaderStage. max_dynamic_uniform_buffers_per_pipeline_layout: MAX_UNIFORM_BUFFERS_PER_SHADER_STAGE, diff --git a/wgpu-hal/src/vulkan/adapter.rs b/wgpu-hal/src/vulkan/adapter.rs index bdc71b01d..3fcbbf359 100644 --- a/wgpu-hal/src/vulkan/adapter.rs +++ b/wgpu-hal/src/vulkan/adapter.rs @@ -1634,6 +1634,8 @@ impl PhysicalDeviceProperties { max_texture_dimension_3d: limits.max_image_dimension3_d, max_texture_array_layers: limits.max_image_array_layers, max_bind_groups: limits.max_bound_descriptor_sets, + // No limit. + max_bind_groups_plus_vertex_buffers: u32::MAX, max_bindings_per_bind_group, max_dynamic_uniform_buffers_per_pipeline_layout: limits .max_descriptor_set_uniform_buffers_dynamic, diff --git a/wgpu-info/src/human.rs b/wgpu-info/src/human.rs index 5a406153e..1e642c49c 100644 --- a/wgpu-info/src/human.rs +++ b/wgpu-info/src/human.rs @@ -151,6 +151,7 @@ fn print_adapter(output: &mut impl io::Write, report: &AdapterReport, idx: usize max_texture_dimension_3d, max_texture_array_layers, max_bind_groups, + max_bind_groups_plus_vertex_buffers, max_bindings_per_bind_group, max_dynamic_uniform_buffers_per_pipeline_layout, max_dynamic_storage_buffers_per_pipeline_layout, @@ -208,6 +209,7 @@ fn print_adapter(output: &mut impl io::Write, report: &AdapterReport, idx: usize writeln!(output, "\t\t Max Texture Dimension 3d: {max_texture_dimension_3d}")?; writeln!(output, "\t\t Max Texture Array Layers: {max_texture_array_layers}")?; writeln!(output, "\t\t Max Bind Groups: {max_bind_groups}")?; + writeln!(output, "\t\t Max Bind Groups Plus Vertex Buffers: {max_bind_groups_plus_vertex_buffers}")?; writeln!(output, "\t\t Max Bindings Per Bind Group: {max_bindings_per_bind_group}")?; writeln!(output, "\t\t Max Dynamic Uniform Buffers Per Pipeline Layout: {max_dynamic_uniform_buffers_per_pipeline_layout}")?; writeln!(output, "\t\t Max Dynamic Storage Buffers Per Pipeline Layout: {max_dynamic_storage_buffers_per_pipeline_layout}")?; diff --git a/wgpu-types/src/limits.rs b/wgpu-types/src/limits.rs index 1c7f9f8bf..853423909 100644 --- a/wgpu-types/src/limits.rs +++ b/wgpu-types/src/limits.rs @@ -22,6 +22,7 @@ macro_rules! with_limits { $macro_name!(max_texture_dimension_3d, Ordering::Less); $macro_name!(max_texture_array_layers, Ordering::Less); $macro_name!(max_bind_groups, Ordering::Less); + $macro_name!(max_bind_groups_plus_vertex_buffers, Ordering::Less); $macro_name!(max_bindings_per_bind_group, Ordering::Less); $macro_name!( max_dynamic_uniform_buffers_per_pipeline_layout, @@ -146,6 +147,9 @@ pub struct Limits { pub max_texture_array_layers: u32, /// Amount of bind groups that can be attached to a pipeline at the same time. Defaults to 4. Higher is "better". pub max_bind_groups: u32, + /// The maximum number of bind group and vertex buffer slots used simultaneously, counting any empty slots below the highest index. + /// Defaults to 24. Higher is "better". + pub max_bind_groups_plus_vertex_buffers: u32, /// Maximum binding index allowed in `create_bind_group_layout`. Defaults to 1000. Higher is "better". pub max_bindings_per_bind_group: u32, /// Amount of uniform buffer bindings that can be dynamic in a single pipeline. Defaults to 8. Higher is "better". @@ -333,6 +337,7 @@ impl Limits { /// max_texture_dimension_3d: 2048, /// max_texture_array_layers: 256, /// max_bind_groups: 4, + /// max_bind_groups_plus_vertex_buffers: 24, /// max_bindings_per_bind_group: 1000, /// max_dynamic_uniform_buffers_per_pipeline_layout: 8, /// max_dynamic_storage_buffers_per_pipeline_layout: 4, @@ -394,6 +399,7 @@ impl Limits { max_texture_dimension_3d: 2048, max_texture_array_layers: 256, max_bind_groups: 4, + max_bind_groups_plus_vertex_buffers: 24, max_bindings_per_bind_group: 1000, max_dynamic_uniform_buffers_per_pipeline_layout: 8, max_dynamic_storage_buffers_per_pipeline_layout: 4, @@ -459,6 +465,7 @@ impl Limits { /// max_texture_dimension_3d: 256, // * /// max_texture_array_layers: 256, /// max_bind_groups: 4, + /// max_bind_groups_plus_vertex_buffers: 24, /// max_bindings_per_bind_group: 1000, /// max_dynamic_uniform_buffers_per_pipeline_layout: 8, /// max_dynamic_storage_buffers_per_pipeline_layout: 4, @@ -540,6 +547,7 @@ impl Limits { /// max_texture_dimension_3d: 256, // * /// max_texture_array_layers: 256, /// max_bind_groups: 4, + /// max_bind_groups_plus_vertex_buffers: 24, /// max_bindings_per_bind_group: 1000, /// max_dynamic_uniform_buffers_per_pipeline_layout: 8, /// max_dynamic_storage_buffers_per_pipeline_layout: 0, // + @@ -637,6 +645,7 @@ impl Limits { max_texture_dimension_3d: ALLOC_MAX_U32, max_texture_array_layers: ALLOC_MAX_U32, max_bind_groups: ALLOC_MAX_U32, + max_bind_groups_plus_vertex_buffers: ALLOC_MAX_U32, max_bindings_per_bind_group: ALLOC_MAX_U32, max_dynamic_uniform_buffers_per_pipeline_layout: ALLOC_MAX_U32, max_dynamic_storage_buffers_per_pipeline_layout: ALLOC_MAX_U32, diff --git a/wgpu/src/backend/webgpu.rs b/wgpu/src/backend/webgpu.rs index 8512966c6..f1dd95228 100644 --- a/wgpu/src/backend/webgpu.rs +++ b/wgpu/src/backend/webgpu.rs @@ -818,6 +818,7 @@ fn map_wgt_limits(limits: webgpu_sys::GpuSupportedLimits) -> wgt::Limits { max_texture_dimension_3d: limits.max_texture_dimension_3d(), max_texture_array_layers: limits.max_texture_array_layers(), max_bind_groups: limits.max_bind_groups(), + max_bind_groups_plus_vertex_buffers: limits.max_bind_groups_plus_vertex_buffers(), max_bindings_per_bind_group: limits.max_bindings_per_bind_group(), max_dynamic_uniform_buffers_per_pipeline_layout: limits .max_dynamic_uniform_buffers_per_pipeline_layout(), @@ -925,7 +926,7 @@ fn map_js_sys_limits(limits: &wgt::Limits) -> js_sys::Object { (maxTextureDimension3D, max_texture_dimension_3d), (maxTextureArrayLayers, max_texture_array_layers), (maxBindGroups, max_bind_groups), - // TODO: (maxBindGroupsPlusVertexBuffers, max_bind_groups_plus_vertex_buffers), + (maxBindGroupsPlusVertexBuffers, max_bind_groups_plus_vertex_buffers), (maxBindingsPerBindGroup, max_bindings_per_bind_group), (maxDynamicUniformBuffersPerPipelineLayout, max_dynamic_uniform_buffers_per_pipeline_layout), (maxDynamicStorageBuffersPerPipelineLayout, max_dynamic_storage_buffers_per_pipeline_layout),