wgpu_hal/auxil/
mod.rs

1#[cfg(dx12)]
2pub(super) mod dxgi;
3
4#[cfg(all(native, feature = "renderdoc"))]
5pub(super) mod renderdoc;
6
7pub mod db {
8    pub mod amd {
9        /// cbindgen:ignore
10        pub const VENDOR: u32 = 0x1002;
11    }
12    pub mod apple {
13        /// cbindgen:ignore
14        pub const VENDOR: u32 = 0x106B;
15    }
16    pub mod arm {
17        /// cbindgen:ignore
18        pub const VENDOR: u32 = 0x13B5;
19    }
20    pub mod broadcom {
21        /// cbindgen:ignore
22        pub const VENDOR: u32 = 0x14E4;
23    }
24    pub mod imgtec {
25        /// cbindgen:ignore
26        pub const VENDOR: u32 = 0x1010;
27    }
28    pub mod intel {
29        /// cbindgen:ignore
30        pub const VENDOR: u32 = 0x8086;
31        pub const DEVICE_KABY_LAKE_MASK: u32 = 0x5900;
32        pub const DEVICE_SKY_LAKE_MASK: u32 = 0x1900;
33    }
34    pub mod mesa {
35        // Mesa does not actually have a PCI vendor id.
36        //
37        // To match Vulkan, we use the VkVendorId for Mesa in the gles backend so that lavapipe (Vulkan) and
38        // llvmpipe (OpenGL) have the same vendor id.
39        /// cbindgen:ignore
40        pub const VENDOR: u32 = 0x10005;
41    }
42    pub mod nvidia {
43        /// cbindgen:ignore
44        pub const VENDOR: u32 = 0x10DE;
45    }
46    pub mod qualcomm {
47        /// cbindgen:ignore
48        pub const VENDOR: u32 = 0x5143;
49    }
50}
51
52/// Maximum binding size for the shaders that only support `i32` indexing.
53/// Interestingly, the index itself can't reach that high, because the minimum
54/// element size is 4 bytes, but the compiler toolchain still computes the
55/// offset at some intermediate point, internally, as i32.
56pub const MAX_I32_BINDING_SIZE: u32 = (1 << 31) - 1;
57
58pub use wgpu_naga_bridge::map_naga_stage;
59
60impl crate::CopyExtent {
61    pub fn map_extent_to_copy_size(extent: &wgt::Extent3d, dim: wgt::TextureDimension) -> Self {
62        Self {
63            width: extent.width,
64            height: extent.height,
65            depth: match dim {
66                wgt::TextureDimension::D1 | wgt::TextureDimension::D2 => 1,
67                wgt::TextureDimension::D3 => extent.depth_or_array_layers,
68            },
69        }
70    }
71
72    pub fn min(&self, other: &Self) -> Self {
73        Self {
74            width: self.width.min(other.width),
75            height: self.height.min(other.height),
76            depth: self.depth.min(other.depth),
77        }
78    }
79
80    // Get the copy size at a specific mipmap level. This doesn't make most sense,
81    // since the copy extents are provided *for* a mipmap level to start with.
82    // But backends use `CopyExtent` more sparingly, and this piece is shared.
83    pub fn at_mip_level(&self, level: u32) -> Self {
84        Self {
85            width: (self.width >> level).max(1),
86            height: (self.height >> level).max(1),
87            depth: (self.depth >> level).max(1),
88        }
89    }
90}
91
92impl crate::TextureCopyBase {
93    pub fn max_copy_size(&self, full_size: &crate::CopyExtent) -> crate::CopyExtent {
94        let mip = full_size.at_mip_level(self.mip_level);
95        crate::CopyExtent {
96            width: mip.width - self.origin.x,
97            height: mip.height - self.origin.y,
98            depth: mip.depth - self.origin.z,
99        }
100    }
101}
102
103impl crate::BufferTextureCopy {
104    pub fn clamp_size_to_virtual(&mut self, full_size: &crate::CopyExtent) {
105        let max_size = self.texture_base.max_copy_size(full_size);
106        self.size = self.size.min(&max_size);
107    }
108}
109
110impl crate::TextureCopy {
111    pub fn clamp_size_to_virtual(
112        &mut self,
113        full_src_size: &crate::CopyExtent,
114        full_dst_size: &crate::CopyExtent,
115    ) {
116        let max_src_size = self.src_base.max_copy_size(full_src_size);
117        let max_dst_size = self.dst_base.max_copy_size(full_dst_size);
118        self.size = self.size.min(&max_src_size).min(&max_dst_size);
119    }
120}
121
122/// Adjust `limits` to honor HAL-imposed maximums and comply with WebGPU's
123/// adapter capability guarantees.
124#[cfg_attr(not(any_backend), allow(dead_code))]
125pub(crate) fn adjust_raw_limits(mut limits: wgt::Limits) -> wgt::Limits {
126    // Apply hal limits.
127    limits.max_bind_groups = limits.max_bind_groups.min(crate::MAX_BIND_GROUPS as u32);
128    limits.max_vertex_buffers = limits
129        .max_vertex_buffers
130        .min(crate::MAX_VERTEX_BUFFERS as u32);
131    // Once we allow the 2 limits above to be higher than 24 we should use
132    // `cap_limits_to_be_under_the_sum_limit` to cap them under
133    // `max_bind_groups_plus_vertex_buffers`.
134    const { assert!(crate::MAX_BIND_GROUPS + crate::MAX_VERTEX_BUFFERS == 24) };
135    limits.max_bind_groups_plus_vertex_buffers = limits.max_bind_groups_plus_vertex_buffers.min(24);
136    limits.max_color_attachments = limits
137        .max_color_attachments
138        .min(crate::MAX_COLOR_ATTACHMENTS as u32);
139
140    // Adjust limits according to WebGPU adapter capability guarantees.
141    // See <https://gpuweb.github.io/gpuweb/#adapter-capability-guarantees>.
142
143    // WebGPU requires maxBindingsPerBindGroup to be at least the sum of all
144    // per-stage limits multiplied with the maximum shader stages per pipeline.
145    //
146    // Since backends already report their maximum maxBindingsPerBindGroup,
147    // we need to lower all per-stage limits to satisfy this guarantee.
148    const MAX_SHADER_STAGES_PER_PIPELINE: u32 = 2;
149    let max_per_stage_resources =
150        limits.max_bindings_per_bind_group / MAX_SHADER_STAGES_PER_PIPELINE;
151
152    cap_limits_to_be_under_the_sum_limit(
153        [
154            &mut limits.max_sampled_textures_per_shader_stage,
155            &mut limits.max_uniform_buffers_per_shader_stage,
156            &mut limits.max_storage_textures_per_shader_stage,
157            &mut limits.max_storage_buffers_per_shader_stage,
158            &mut limits.max_samplers_per_shader_stage,
159            &mut limits.max_acceleration_structures_per_shader_stage,
160        ],
161        max_per_stage_resources,
162    );
163
164    // Not required by the spec but dynamic buffers count
165    // towards non-dynamic buffer limits as well.
166    limits.max_dynamic_uniform_buffers_per_pipeline_layout = limits
167        .max_dynamic_uniform_buffers_per_pipeline_layout
168        .min(limits.max_uniform_buffers_per_shader_stage);
169    limits.max_dynamic_storage_buffers_per_pipeline_layout = limits
170        .max_dynamic_storage_buffers_per_pipeline_layout
171        .min(limits.max_storage_buffers_per_shader_stage);
172
173    limits.min_uniform_buffer_offset_alignment = limits.min_uniform_buffer_offset_alignment.max(32);
174    limits.min_storage_buffer_offset_alignment = limits.min_storage_buffer_offset_alignment.max(32);
175
176    limits.max_uniform_buffer_binding_size = limits
177        .max_uniform_buffer_binding_size
178        .min(limits.max_buffer_size);
179    limits.max_storage_buffer_binding_size = limits
180        .max_storage_buffer_binding_size
181        .min(limits.max_buffer_size);
182
183    limits.max_storage_buffer_binding_size &= !(u64::from(wgt::STORAGE_BINDING_SIZE_ALIGNMENT) - 1);
184    limits.max_vertex_buffer_array_stride &= !(wgt::VERTEX_ALIGNMENT as u32 - 1);
185
186    let x = limits.max_compute_workgroup_size_x;
187    let y = limits.max_compute_workgroup_size_y;
188    let z = limits.max_compute_workgroup_size_z;
189    let m = limits.max_compute_invocations_per_workgroup;
190    limits.max_compute_workgroup_size_x = x.min(m);
191    limits.max_compute_workgroup_size_y = y.min(m);
192    limits.max_compute_workgroup_size_z = z.min(m);
193    limits.max_compute_invocations_per_workgroup = m.min(x.saturating_mul(y).saturating_mul(z));
194
195    limits.max_immediate_size = limits.max_immediate_size.min(256);
196
197    limits
198}
199
200/// Evenly allocates space to each limit,
201/// capping them only if strictly necessary.
202pub fn cap_limits_to_be_under_the_sum_limit<const N: usize>(
203    mut limits: [&mut u32; N],
204    sum_limit: u32,
205) {
206    limits.sort();
207
208    let mut rem_limit = sum_limit;
209    let mut divisor = limits.len() as u32;
210    for limit_to_adjust in limits {
211        let limit = rem_limit / divisor;
212        *limit_to_adjust = (*limit_to_adjust).min(limit);
213        rem_limit -= *limit_to_adjust;
214        divisor -= 1;
215    }
216}
217
218#[cfg(test)]
219mod tests {
220    use super::*;
221
222    #[test]
223    fn test_cap_limits_to_be_under_the_sum_limit() {
224        test([3, 3, 3], 3, [1, 1, 1]);
225        test([3, 2, 1], 3, [1, 1, 1]);
226        test([1, 2, 3], 6, [1, 2, 3]);
227        test([1, 2, 3], 3, [1, 1, 1]);
228        test([1, 8, 100], 6, [1, 2, 3]);
229        test([2, 80, 80], 6, [2, 2, 2]);
230        test([2, 80, 80], 12, [2, 5, 5]);
231
232        #[track_caller]
233        fn test<const N: usize>(mut input: [u32; N], limit: u32, output: [u32; N]) {
234            cap_limits_to_be_under_the_sum_limit(input.each_mut(), limit);
235            assert_eq!(input, output);
236        }
237    }
238}