use cap_limits_to_be_under_the_sum_limit to clamp per-stage limits

This commit is contained in:
teoxoy authored and Teodor Tanasoaia committed 2026-03-13 16:24:43 +01:00
1 parent 3f90204744
commit 59eb0ee70c
1 file changed
+30 -20
+30 -20
View File
@@ -141,8 +141,8 @@ pub(crate) fn apply_hal_limits(mut limits: wgt::Limits) -> wgt::Limits {
// Vulkan and DX12 sometimes report large values for the "max resources per stage"
// limits. WebGPU requires that consuming the maximum number of resources of every type
// in every stage cannot exceed `max_bindings_per_bind_group`. To ensure this, we clamp
// per-stage limit at an equal division of `max_bindings_per_bind_group`.
// in every stage cannot exceed `max_bindings_per_bind_group`. To ensure this, we must
// clamp per-stage limits.
//
// It is not expected that this clamping adjusts limits that are in a range where an
// application might reach them. (The main dilemma here being how WebGPU will eventually
@@ -150,24 +150,16 @@ pub(crate) fn apply_hal_limits(mut limits: wgt::Limits) -> wgt::Limits {
// its native-only extension; those are not touched here.)
//
// See <https://gpuweb.github.io/gpuweb/#adapter-capability-guarantees>.
const BINDING_TYPE_AND_STAGE_COUNT: u32 = 10;
let max_bindings = limits.max_bindings_per_bind_group / BINDING_TYPE_AND_STAGE_COUNT;
let mut did_clamp = false;
let mut clamp = |limit: &mut u32| {
if *limit > max_bindings {
*limit = max_bindings;
did_clamp = true;
}
};
clamp(&mut limits.max_dynamic_uniform_buffers_per_pipeline_layout);
clamp(&mut limits.max_dynamic_storage_buffers_per_pipeline_layout);
clamp(&mut limits.max_sampled_textures_per_shader_stage);
clamp(&mut limits.max_samplers_per_shader_stage);
clamp(&mut limits.max_storage_buffers_per_shader_stage);
clamp(&mut limits.max_storage_textures_per_shader_stage);
clamp(&mut limits.max_uniform_buffers_per_shader_stage);
if did_clamp
&& limits.max_bindings_per_bind_group < wgt::Limits::defaults().max_bindings_per_bind_group
const MAX_SHADER_STAGES_PER_PIPELINE: u32 = 2;
let max_per_stage_resources =
limits.max_bindings_per_bind_group / MAX_SHADER_STAGES_PER_PIPELINE;
if limits.max_sampled_textures_per_shader_stage
+ limits.max_samplers_per_shader_stage
+ limits.max_storage_buffers_per_shader_stage
+ limits.max_storage_textures_per_shader_stage
+ limits.max_uniform_buffers_per_shader_stage
> max_per_stage_resources
{
// This is the "adjusted limits that are in a range where an application might reach
// them" case. (In reality, even something quite a bit less than the default
@@ -177,6 +169,24 @@ pub(crate) fn apply_hal_limits(mut limits: wgt::Limits) -> wgt::Limits {
);
}
cap_limits_to_be_under_the_sum_limit(
[
&mut limits.max_sampled_textures_per_shader_stage,
&mut limits.max_samplers_per_shader_stage,
&mut limits.max_storage_buffers_per_shader_stage,
&mut limits.max_storage_textures_per_shader_stage,
&mut limits.max_uniform_buffers_per_shader_stage,
],
max_per_stage_resources,
);
limits.max_dynamic_uniform_buffers_per_pipeline_layout = limits
.max_dynamic_uniform_buffers_per_pipeline_layout
.min(limits.max_uniform_buffers_per_shader_stage);
limits.max_dynamic_storage_buffers_per_pipeline_layout = limits
.max_dynamic_storage_buffers_per_pipeline_layout
.min(limits.max_storage_buffers_per_shader_stage);
// Round some limits down to the WebGPU alignment requirement, to avoid
// suggesting values that won't work. (In particular, the CTS queries limits
// and then tests the exact limit value.)