Skip to main content

wgpu_hal/vulkan/
adapter.rs

1use alloc::{borrow::ToOwned as _, boxed::Box, collections::BTreeMap, sync::Arc, vec::Vec};
2use core::{ffi::CStr, marker::PhantomData};
3
4use ash::{ext, google, khr, vk};
5use parking_lot::Mutex;
6
7use crate::{vulkan::semaphore_list::SemaphoreList, AllocationSizes};
8
9use super::semaphore_list::SemaphoreListMode;
10
11fn depth_stencil_required_flags() -> vk::FormatFeatureFlags {
12    vk::FormatFeatureFlags::SAMPLED_IMAGE | vk::FormatFeatureFlags::DEPTH_STENCIL_ATTACHMENT
13}
14
15const INDEXING_FEATURES: wgt::Features = wgt::Features::TEXTURE_BINDING_ARRAY
16    .union(wgt::Features::BUFFER_BINDING_ARRAY)
17    .union(wgt::Features::STORAGE_RESOURCE_BINDING_ARRAY)
18    .union(wgt::Features::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING)
19    .union(wgt::Features::STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING)
20    .union(wgt::Features::UNIFORM_BUFFER_BINDING_ARRAYS)
21    .union(wgt::Features::PARTIALLY_BOUND_BINDING_ARRAY);
22#[expect(rustdoc::private_intra_doc_links)]
23/// Features supported by a [`vk::PhysicalDevice`] and its extensions.
24///
25/// This is used in two phases:
26///
27/// - When enumerating adapters, this represents the features offered by the
28///   adapter. [`Instance::expose_adapter`] calls `vkGetPhysicalDeviceFeatures2`
29///   (or `vkGetPhysicalDeviceFeatures` if that is not available) to collect
30///   this information about the `VkPhysicalDevice` represented by the
31///   `wgpu_hal::ExposedAdapter`.
32///
33/// - When opening a device, this represents the features we would like to
34///   enable. At `wgpu_hal::Device` construction time,
35///   [`PhysicalDeviceFeatures::from_extensions_and_requested_features`]
36///   constructs an value of this type indicating which Vulkan features to
37///   enable, based on the `wgpu_types::Features` requested.
38///
39/// [`Instance::expose_adapter`]: super::Instance::expose_adapter
40#[derive(Debug, Default)]
41pub struct PhysicalDeviceFeatures {
42    /// Basic Vulkan 1.0 features.
43    core: vk::PhysicalDeviceFeatures,
44
45    /// Features provided by `VK_EXT_descriptor_indexing`, promoted to Vulkan 1.2.
46    pub(super) descriptor_indexing:
47        Option<vk::PhysicalDeviceDescriptorIndexingFeaturesEXT<'static>>,
48
49    /// Features provided by `VK_KHR_timeline_semaphore`, promoted to Vulkan 1.2
50    timeline_semaphore: Option<vk::PhysicalDeviceTimelineSemaphoreFeaturesKHR<'static>>,
51
52    /// Features provided by `VK_EXT_image_robustness`, promoted to Vulkan 1.3
53    image_robustness: Option<vk::PhysicalDeviceImageRobustnessFeaturesEXT<'static>>,
54
55    /// Features provided by `VK_EXT_robustness2`.
56    robustness2: Option<vk::PhysicalDeviceRobustness2FeaturesEXT<'static>>,
57
58    /// Features provided by `VK_KHR_multiview`, promoted to Vulkan 1.1.
59    multiview: Option<vk::PhysicalDeviceMultiviewFeaturesKHR<'static>>,
60
61    /// Features provided by `VK_KHR_sampler_ycbcr_conversion`, promoted to Vulkan 1.1.
62    sampler_ycbcr_conversion: Option<vk::PhysicalDeviceSamplerYcbcrConversionFeatures<'static>>,
63
64    /// Features provided by `VK_EXT_texture_compression_astc_hdr`, promoted to Vulkan 1.3.
65    astc_hdr: Option<vk::PhysicalDeviceTextureCompressionASTCHDRFeaturesEXT<'static>>,
66
67    /// Features provided by `VK_KHR_shader_float16_int8`, promoted to Vulkan 1.2
68    shader_float16_int8: Option<vk::PhysicalDeviceShaderFloat16Int8Features<'static>>,
69
70    /// Features provided by `VK_KHR_16bit_storage`, promoted to Vulkan 1.1
71    _16bit_storage: Option<vk::PhysicalDevice16BitStorageFeatures<'static>>,
72
73    /// Features provided by `VK_KHR_acceleration_structure`.
74    acceleration_structure: Option<vk::PhysicalDeviceAccelerationStructureFeaturesKHR<'static>>,
75
76    /// Features provided by `VK_KHR_buffer_device_address`, promoted to Vulkan 1.2.
77    ///
78    /// We only use this feature for
79    /// [`Features::EXPERIMENTAL_RAY_QUERY`], which requires
80    /// `VK_KHR_acceleration_structure`, which depends on
81    /// `VK_KHR_buffer_device_address`, so [`Instance::expose_adapter`] only
82    /// bothers to check if `VK_KHR_acceleration_structure` is available,
83    /// leaving this `None`.
84    ///
85    /// However, we do populate this when creating a device if
86    /// [`Features::EXPERIMENTAL_RAY_QUERY`] is requested.
87    ///
88    /// [`Instance::expose_adapter`]: super::Instance::expose_adapter
89    /// [`Features::EXPERIMENTAL_RAY_QUERY`]: wgt::Features::EXPERIMENTAL_RAY_QUERY
90    buffer_device_address: Option<vk::PhysicalDeviceBufferDeviceAddressFeaturesKHR<'static>>,
91
92    /// Features provided by `VK_KHR_ray_query`,
93    ///
94    /// Vulkan requires that the feature be present if the `VK_KHR_ray_query`
95    /// extension is present, so [`Instance::expose_adapter`] doesn't bother retrieving
96    /// this from `vkGetPhysicalDeviceFeatures2`.
97    ///
98    /// However, we do populate this when creating a device if ray tracing is requested.
99    ///
100    /// [`Instance::expose_adapter`]: super::Instance::expose_adapter
101    ray_query: Option<vk::PhysicalDeviceRayQueryFeaturesKHR<'static>>,
102
103    /// Features provided by `VK_KHR_zero_initialize_workgroup_memory`, promoted
104    /// to Vulkan 1.3.
105    zero_initialize_workgroup_memory:
106        Option<vk::PhysicalDeviceZeroInitializeWorkgroupMemoryFeatures<'static>>,
107    position_fetch: Option<vk::PhysicalDeviceRayTracingPositionFetchFeaturesKHR<'static>>,
108
109    /// Features provided by `VK_KHR_shader_atomic_int64`, promoted to Vulkan 1.2.
110    shader_atomic_int64: Option<vk::PhysicalDeviceShaderAtomicInt64Features<'static>>,
111
112    /// Features provided by `VK_EXT_shader_image_atomic_int64`
113    shader_image_atomic_int64: Option<vk::PhysicalDeviceShaderImageAtomicInt64FeaturesEXT<'static>>,
114
115    /// Features provided by `VK_EXT_shader_atomic_float`.
116    shader_atomic_float: Option<vk::PhysicalDeviceShaderAtomicFloatFeaturesEXT<'static>>,
117
118    /// Features provided by `VK_EXT_subgroup_size_control`, promoted to Vulkan 1.3.
119    subgroup_size_control: Option<vk::PhysicalDeviceSubgroupSizeControlFeatures<'static>>,
120
121    /// Features provided by `VK_KHR_maintenance4`, promoted to Vulkan 1.3.
122    maintenance4: Option<vk::PhysicalDeviceMaintenance4FeaturesKHR<'static>>,
123
124    /// Features proved by `VK_EXT_mesh_shader`
125    mesh_shader: Option<vk::PhysicalDeviceMeshShaderFeaturesEXT<'static>>,
126
127    /// Features provided by `VK_KHR_shader_integer_dot_product`, promoted to Vulkan 1.3.
128    shader_integer_dot_product:
129        Option<vk::PhysicalDeviceShaderIntegerDotProductFeaturesKHR<'static>>,
130
131    /// Features provided by `VK_KHR_fragment_shader_barycentric`
132    shader_barycentrics: Option<vk::PhysicalDeviceFragmentShaderBarycentricFeaturesKHR<'static>>,
133
134    /// Features provided by `VK_KHR_portability_subset`.
135    ///
136    /// Strictly speaking this tells us what features we *don't* have compared to core.
137    portability_subset: Option<vk::PhysicalDevicePortabilitySubsetFeaturesKHR<'static>>,
138
139    /// Features provided by `VK_KHR_cooperative_matrix`
140    cooperative_matrix: Option<vk::PhysicalDeviceCooperativeMatrixFeaturesKHR<'static>>,
141
142    /// Features provided by `VK_KHR_vulkan_memory_model`, promoted to Vulkan 1.2
143    vulkan_memory_model: Option<vk::PhysicalDeviceVulkanMemoryModelFeaturesKHR<'static>>,
144
145    shader_draw_parameters: Option<vk::PhysicalDeviceShaderDrawParametersFeatures<'static>>,
146}
147
148impl PhysicalDeviceFeatures {
149    pub fn get_core(&self) -> vk::PhysicalDeviceFeatures {
150        self.core
151    }
152
153    /// Add the members of `self` into `info.enabled_features` and its `p_next` chain.
154    pub fn add_to_device_create<'a>(
155        &'a mut self,
156        mut info: vk::DeviceCreateInfo<'a>,
157    ) -> vk::DeviceCreateInfo<'a> {
158        info = info.enabled_features(&self.core);
159        if let Some(ref mut feature) = self.descriptor_indexing {
160            info = info.push_next(feature);
161        }
162        if let Some(ref mut feature) = self.timeline_semaphore {
163            info = info.push_next(feature);
164        }
165        if let Some(ref mut feature) = self.image_robustness {
166            info = info.push_next(feature);
167        }
168        if let Some(ref mut feature) = self.robustness2 {
169            info = info.push_next(feature);
170        }
171        if let Some(ref mut feature) = self.multiview {
172            info = info.push_next(feature);
173        }
174        if let Some(ref mut feature) = self.astc_hdr {
175            info = info.push_next(feature);
176        }
177        if let Some(ref mut feature) = self.shader_float16_int8 {
178            info = info.push_next(feature);
179        }
180        if let Some(ref mut feature) = self._16bit_storage {
181            info = info.push_next(feature);
182        }
183        if let Some(ref mut feature) = self.zero_initialize_workgroup_memory {
184            info = info.push_next(feature);
185        }
186        if let Some(ref mut feature) = self.acceleration_structure {
187            info = info.push_next(feature);
188        }
189        if let Some(ref mut feature) = self.buffer_device_address {
190            info = info.push_next(feature);
191        }
192        if let Some(ref mut feature) = self.ray_query {
193            info = info.push_next(feature);
194        }
195        if let Some(ref mut feature) = self.shader_atomic_int64 {
196            info = info.push_next(feature);
197        }
198        if let Some(ref mut feature) = self.position_fetch {
199            info = info.push_next(feature);
200        }
201        if let Some(ref mut feature) = self.shader_image_atomic_int64 {
202            info = info.push_next(feature);
203        }
204        if let Some(ref mut feature) = self.shader_atomic_float {
205            info = info.push_next(feature);
206        }
207        if let Some(ref mut feature) = self.subgroup_size_control {
208            info = info.push_next(feature);
209        }
210        if let Some(ref mut feature) = self.maintenance4 {
211            info = info.push_next(feature);
212        }
213        if let Some(ref mut feature) = self.mesh_shader {
214            info = info.push_next(feature);
215        }
216        if let Some(ref mut feature) = self.shader_integer_dot_product {
217            info = info.push_next(feature);
218        }
219        if let Some(ref mut feature) = self.shader_barycentrics {
220            info = info.push_next(feature);
221        }
222        if let Some(ref mut feature) = self.portability_subset {
223            info = info.push_next(feature);
224        }
225        if let Some(ref mut feature) = self.cooperative_matrix {
226            info = info.push_next(feature);
227        }
228        if let Some(ref mut feature) = self.vulkan_memory_model {
229            info = info.push_next(feature);
230        }
231        if let Some(ref mut feature) = self.shader_draw_parameters {
232            info = info.push_next(feature);
233        }
234        info
235    }
236
237    fn supports_storage_input_output_16(&self) -> bool {
238        self._16bit_storage
239            .as_ref()
240            .map(|features| features.storage_input_output16 != 0)
241            .unwrap_or(false)
242    }
243
244    /// Create a `PhysicalDeviceFeatures` that can be used to create a logical
245    /// device.
246    ///
247    /// Return a `PhysicalDeviceFeatures` value capturing all the Vulkan
248    /// features needed for the given [`Features`], [`DownlevelFlags`], and
249    /// [`PrivateCapabilities`]. You can use the returned value's
250    /// [`add_to_device_create`] method to configure a
251    /// [`vk::DeviceCreateInfo`] to build a logical device providing those
252    /// features.
253    ///
254    /// To ensure that the returned value is able to select all the Vulkan
255    /// features needed to express `requested_features`, `downlevel_flags`, and
256    /// `private_caps`:
257    ///
258    /// - The given `enabled_extensions` set must include all the extensions
259    ///   selected by [`Adapter::required_device_extensions`] when passed
260    ///   `features`.
261    ///
262    /// - The given `device_api_version` must be the Vulkan API version of the
263    ///   physical device we will use to create the logical device.
264    ///
265    /// [`Features`]: wgt::Features
266    /// [`DownlevelFlags`]: wgt::DownlevelFlags
267    /// [`PrivateCapabilities`]: super::PrivateCapabilities
268    /// [`add_to_device_create`]: PhysicalDeviceFeatures::add_to_device_create
269    /// [`Adapter::required_device_extensions`]: super::Adapter::required_device_extensions
270    fn from_extensions_and_requested_features(
271        phd_capabilities: &PhysicalDeviceProperties,
272        phd_features: &PhysicalDeviceFeatures,
273        enabled_extensions: &[&'static CStr],
274        requested_features: wgt::Features,
275        downlevel_flags: wgt::DownlevelFlags,
276        private_caps: &super::PrivateCapabilities,
277    ) -> Self {
278        let device_api_version = phd_capabilities.device_api_version;
279        let needs_bindless = requested_features.intersects(
280            wgt::Features::TEXTURE_BINDING_ARRAY
281                | wgt::Features::BUFFER_BINDING_ARRAY
282                | wgt::Features::STORAGE_RESOURCE_BINDING_ARRAY
283                | wgt::Features::STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING
284                | wgt::Features::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING,
285        );
286        let needs_partially_bound =
287            requested_features.intersects(wgt::Features::PARTIALLY_BOUND_BINDING_ARRAY);
288
289        Self {
290            // vk::PhysicalDeviceFeatures is a struct composed of Bool32's while
291            // Features is a bitfield so we need to map everything manually
292            core: vk::PhysicalDeviceFeatures::default()
293                .robust_buffer_access(private_caps.robust_buffer_access)
294                .independent_blend(downlevel_flags.contains(wgt::DownlevelFlags::INDEPENDENT_BLEND))
295                .sample_rate_shading(
296                    downlevel_flags.contains(wgt::DownlevelFlags::MULTISAMPLED_SHADING),
297                )
298                .image_cube_array(
299                    downlevel_flags.contains(wgt::DownlevelFlags::CUBE_ARRAY_TEXTURES),
300                )
301                .draw_indirect_first_instance(
302                    requested_features.contains(wgt::Features::INDIRECT_FIRST_INSTANCE),
303                )
304                //.dual_src_blend(requested_features.contains(wgt::Features::DUAL_SRC_BLENDING))
305                .multi_draw_indirect(phd_features.core.multi_draw_indirect != 0)
306                .fill_mode_non_solid(requested_features.intersects(
307                    wgt::Features::POLYGON_MODE_LINE | wgt::Features::POLYGON_MODE_POINT,
308                ))
309                //.depth_bounds(requested_features.contains(wgt::Features::DEPTH_BOUNDS))
310                //.alpha_to_one(requested_features.contains(wgt::Features::ALPHA_TO_ONE))
311                //.multi_viewport(requested_features.contains(wgt::Features::MULTI_VIEWPORTS))
312                .sampler_anisotropy(
313                    downlevel_flags.contains(wgt::DownlevelFlags::ANISOTROPIC_FILTERING),
314                )
315                .texture_compression_etc2(
316                    requested_features.contains(wgt::Features::TEXTURE_COMPRESSION_ETC2),
317                )
318                .texture_compression_astc_ldr(
319                    requested_features.contains(wgt::Features::TEXTURE_COMPRESSION_ASTC),
320                )
321                .texture_compression_bc(
322                    requested_features.contains(wgt::Features::TEXTURE_COMPRESSION_BC),
323                    // BC provides formats for Sliced 3D
324                )
325                //.occlusion_query_precise(requested_features.contains(wgt::Features::PRECISE_OCCLUSION_QUERY))
326                .pipeline_statistics_query(
327                    requested_features.contains(wgt::Features::PIPELINE_STATISTICS_QUERY),
328                )
329                .vertex_pipeline_stores_and_atomics(
330                    requested_features.contains(wgt::Features::VERTEX_WRITABLE_STORAGE),
331                )
332                .fragment_stores_and_atomics(
333                    downlevel_flags.contains(wgt::DownlevelFlags::FRAGMENT_WRITABLE_STORAGE),
334                )
335                //.shader_image_gather_extended(
336                //.shader_storage_image_extended_formats(
337                .shader_uniform_buffer_array_dynamic_indexing(
338                    requested_features.contains(wgt::Features::BUFFER_BINDING_ARRAY),
339                )
340                .shader_storage_buffer_array_dynamic_indexing(requested_features.contains(
341                    wgt::Features::BUFFER_BINDING_ARRAY
342                        | wgt::Features::STORAGE_RESOURCE_BINDING_ARRAY,
343                ))
344                .shader_sampled_image_array_dynamic_indexing(
345                    requested_features.contains(wgt::Features::TEXTURE_BINDING_ARRAY),
346                )
347                .shader_storage_buffer_array_dynamic_indexing(requested_features.contains(
348                    wgt::Features::TEXTURE_BINDING_ARRAY
349                        | wgt::Features::STORAGE_RESOURCE_BINDING_ARRAY,
350                ))
351                //.shader_storage_image_array_dynamic_indexing(
352                .shader_clip_distance(requested_features.contains(wgt::Features::CLIP_DISTANCES))
353                //.shader_cull_distance(requested_features.contains(wgt::Features::SHADER_CULL_DISTANCE))
354                .shader_float64(requested_features.contains(wgt::Features::SHADER_F64))
355                .shader_int64(requested_features.contains(wgt::Features::SHADER_INT64))
356                .shader_int16(requested_features.contains(wgt::Features::SHADER_I16))
357                //.shader_resource_residency(requested_features.contains(wgt::Features::SHADER_RESOURCE_RESIDENCY))
358                .geometry_shader(requested_features.contains(wgt::Features::PRIMITIVE_INDEX))
359                .depth_clamp(requested_features.contains(wgt::Features::DEPTH_CLIP_CONTROL))
360                .dual_src_blend(requested_features.contains(wgt::Features::DUAL_SOURCE_BLENDING)),
361            descriptor_indexing: if requested_features.intersects(INDEXING_FEATURES) {
362                Some(
363                    vk::PhysicalDeviceDescriptorIndexingFeaturesEXT::default()
364                        .shader_sampled_image_array_non_uniform_indexing(needs_bindless)
365                        .shader_storage_image_array_non_uniform_indexing(needs_bindless)
366                        .shader_storage_buffer_array_non_uniform_indexing(needs_bindless)
367                        .descriptor_binding_sampled_image_update_after_bind(needs_bindless)
368                        .descriptor_binding_storage_image_update_after_bind(needs_bindless)
369                        .descriptor_binding_storage_buffer_update_after_bind(needs_bindless)
370                        .descriptor_binding_partially_bound(needs_partially_bound),
371                )
372            } else {
373                None
374            },
375            timeline_semaphore: if device_api_version >= vk::API_VERSION_1_2
376                || enabled_extensions.contains(&khr::timeline_semaphore::NAME)
377            {
378                Some(
379                    vk::PhysicalDeviceTimelineSemaphoreFeaturesKHR::default()
380                        .timeline_semaphore(private_caps.timeline_semaphores),
381                )
382            } else {
383                None
384            },
385            image_robustness: if device_api_version >= vk::API_VERSION_1_3
386                || enabled_extensions.contains(&ext::image_robustness::NAME)
387            {
388                Some(
389                    vk::PhysicalDeviceImageRobustnessFeaturesEXT::default()
390                        .robust_image_access(private_caps.robust_image_access),
391                )
392            } else {
393                None
394            },
395            robustness2: if enabled_extensions.contains(&ext::robustness2::NAME) {
396                Some(
397                    vk::PhysicalDeviceRobustness2FeaturesEXT::default()
398                        .robust_buffer_access2(private_caps.robust_buffer_access2)
399                        .robust_image_access2(private_caps.robust_image_access2),
400                )
401            } else {
402                None
403            },
404            multiview: if device_api_version >= vk::API_VERSION_1_1
405                || enabled_extensions.contains(&khr::multiview::NAME)
406            {
407                Some(
408                    vk::PhysicalDeviceMultiviewFeatures::default()
409                        .multiview(requested_features.contains(wgt::Features::MULTIVIEW)),
410                )
411            } else {
412                None
413            },
414            sampler_ycbcr_conversion: if device_api_version >= vk::API_VERSION_1_1
415                || enabled_extensions.contains(&khr::sampler_ycbcr_conversion::NAME)
416            {
417                Some(
418                    vk::PhysicalDeviceSamplerYcbcrConversionFeatures::default(), // .sampler_ycbcr_conversion(requested_features.contains(wgt::Features::TEXTURE_FORMAT_NV12))
419                )
420            } else {
421                None
422            },
423            astc_hdr: if enabled_extensions.contains(&ext::texture_compression_astc_hdr::NAME) {
424                Some(
425                    vk::PhysicalDeviceTextureCompressionASTCHDRFeaturesEXT::default()
426                        .texture_compression_astc_hdr(true),
427                )
428            } else {
429                None
430            },
431            shader_float16_int8: match requested_features.contains(wgt::Features::SHADER_F16) {
432                shader_float16 if shader_float16 || private_caps.shader_int8 => Some(
433                    vk::PhysicalDeviceShaderFloat16Int8Features::default()
434                        .shader_float16(shader_float16)
435                        .shader_int8(private_caps.shader_int8),
436                ),
437                _ => None,
438            },
439            _16bit_storage: if requested_features.contains(wgt::Features::SHADER_F16) {
440                Some(
441                    vk::PhysicalDevice16BitStorageFeatures::default()
442                        .storage_buffer16_bit_access(true)
443                        .storage_input_output16(phd_features.supports_storage_input_output_16())
444                        .uniform_and_storage_buffer16_bit_access(true),
445                )
446            } else {
447                None
448            },
449            acceleration_structure: if enabled_extensions
450                .contains(&khr::acceleration_structure::NAME)
451            {
452                Some(
453                    vk::PhysicalDeviceAccelerationStructureFeaturesKHR::default()
454                        .acceleration_structure(true)
455                        .descriptor_binding_acceleration_structure_update_after_bind(
456                            requested_features
457                                .contains(wgt::Features::ACCELERATION_STRUCTURE_BINDING_ARRAY),
458                        ),
459                )
460            } else {
461                None
462            },
463            buffer_device_address: if enabled_extensions.contains(&khr::buffer_device_address::NAME)
464            {
465                Some(
466                    vk::PhysicalDeviceBufferDeviceAddressFeaturesKHR::default()
467                        .buffer_device_address(true),
468                )
469            } else {
470                None
471            },
472            ray_query: if enabled_extensions.contains(&khr::ray_query::NAME) {
473                Some(vk::PhysicalDeviceRayQueryFeaturesKHR::default().ray_query(true))
474            } else {
475                None
476            },
477            zero_initialize_workgroup_memory: if device_api_version >= vk::API_VERSION_1_3
478                || enabled_extensions.contains(&khr::zero_initialize_workgroup_memory::NAME)
479            {
480                Some(
481                    vk::PhysicalDeviceZeroInitializeWorkgroupMemoryFeatures::default()
482                        .shader_zero_initialize_workgroup_memory(
483                            private_caps.zero_initialize_workgroup_memory,
484                        ),
485                )
486            } else {
487                None
488            },
489            shader_atomic_int64: if device_api_version >= vk::API_VERSION_1_2
490                || enabled_extensions.contains(&khr::shader_atomic_int64::NAME)
491            {
492                let needed = requested_features.intersects(
493                    wgt::Features::SHADER_INT64_ATOMIC_ALL_OPS
494                        | wgt::Features::SHADER_INT64_ATOMIC_MIN_MAX,
495                );
496                Some(
497                    vk::PhysicalDeviceShaderAtomicInt64Features::default()
498                        .shader_buffer_int64_atomics(needed)
499                        .shader_shared_int64_atomics(needed),
500                )
501            } else {
502                None
503            },
504            shader_image_atomic_int64: if enabled_extensions
505                .contains(&ext::shader_image_atomic_int64::NAME)
506            {
507                let needed = requested_features.intersects(wgt::Features::TEXTURE_INT64_ATOMIC);
508                Some(
509                    vk::PhysicalDeviceShaderImageAtomicInt64FeaturesEXT::default()
510                        .shader_image_int64_atomics(needed),
511                )
512            } else {
513                None
514            },
515            shader_atomic_float: if enabled_extensions.contains(&ext::shader_atomic_float::NAME) {
516                let needed = requested_features.contains(wgt::Features::SHADER_FLOAT32_ATOMIC);
517                Some(
518                    vk::PhysicalDeviceShaderAtomicFloatFeaturesEXT::default()
519                        .shader_buffer_float32_atomics(needed)
520                        .shader_buffer_float32_atomic_add(needed),
521                )
522            } else {
523                None
524            },
525            subgroup_size_control: if device_api_version >= vk::API_VERSION_1_3
526                || enabled_extensions.contains(&ext::subgroup_size_control::NAME)
527            {
528                Some(
529                    vk::PhysicalDeviceSubgroupSizeControlFeatures::default()
530                        .subgroup_size_control(true),
531                )
532            } else {
533                None
534            },
535            position_fetch: if enabled_extensions.contains(&khr::ray_tracing_position_fetch::NAME) {
536                Some(
537                    vk::PhysicalDeviceRayTracingPositionFetchFeaturesKHR::default()
538                        .ray_tracing_position_fetch(true),
539                )
540            } else {
541                None
542            },
543            mesh_shader: if enabled_extensions.contains(&ext::mesh_shader::NAME) {
544                let needed = requested_features.contains(wgt::Features::EXPERIMENTAL_MESH_SHADER);
545                let multiview_needed =
546                    requested_features.contains(wgt::Features::EXPERIMENTAL_MESH_SHADER_MULTIVIEW);
547                Some(
548                    vk::PhysicalDeviceMeshShaderFeaturesEXT::default()
549                        .mesh_shader(needed)
550                        .task_shader(needed)
551                        .multiview_mesh_shader(multiview_needed),
552                )
553            } else {
554                None
555            },
556            maintenance4: if device_api_version >= vk::API_VERSION_1_3
557                || enabled_extensions.contains(&khr::maintenance4::NAME)
558            {
559                let needed = requested_features.contains(wgt::Features::EXPERIMENTAL_MESH_SHADER);
560                Some(vk::PhysicalDeviceMaintenance4Features::default().maintenance4(needed))
561            } else {
562                None
563            },
564            shader_integer_dot_product: if device_api_version >= vk::API_VERSION_1_3
565                || enabled_extensions.contains(&khr::shader_integer_dot_product::NAME)
566            {
567                Some(
568                    vk::PhysicalDeviceShaderIntegerDotProductFeaturesKHR::default()
569                        .shader_integer_dot_product(private_caps.shader_integer_dot_product),
570                )
571            } else {
572                None
573            },
574            shader_barycentrics: if enabled_extensions
575                .contains(&khr::fragment_shader_barycentric::NAME)
576            {
577                let needed = requested_features.intersects(
578                    wgt::Features::SHADER_BARYCENTRICS | wgt::Features::SHADER_PER_VERTEX,
579                );
580                Some(
581                    vk::PhysicalDeviceFragmentShaderBarycentricFeaturesKHR::default()
582                        .fragment_shader_barycentric(needed),
583                )
584            } else {
585                None
586            },
587            portability_subset: if enabled_extensions.contains(&khr::portability_subset::NAME) {
588                let multisample_array_needed =
589                    requested_features.intersects(wgt::Features::MULTISAMPLE_ARRAY);
590
591                Some(
592                    vk::PhysicalDevicePortabilitySubsetFeaturesKHR::default()
593                        .multisample_array_image(multisample_array_needed),
594                )
595            } else {
596                None
597            },
598            cooperative_matrix: if enabled_extensions.contains(&khr::cooperative_matrix::NAME) {
599                let needed =
600                    requested_features.contains(wgt::Features::EXPERIMENTAL_COOPERATIVE_MATRIX);
601                Some(
602                    vk::PhysicalDeviceCooperativeMatrixFeaturesKHR::default()
603                        .cooperative_matrix(needed),
604                )
605            } else {
606                None
607            },
608            vulkan_memory_model: if device_api_version >= vk::API_VERSION_1_2
609                || enabled_extensions.contains(&khr::vulkan_memory_model::NAME)
610            {
611                let needed =
612                    requested_features.contains(wgt::Features::EXPERIMENTAL_COOPERATIVE_MATRIX);
613                Some(
614                    vk::PhysicalDeviceVulkanMemoryModelFeaturesKHR::default()
615                        .vulkan_memory_model(needed),
616                )
617            } else {
618                None
619            },
620            shader_draw_parameters: if device_api_version >= vk::API_VERSION_1_1 {
621                Some(
622                    vk::PhysicalDeviceShaderDrawParametersFeatures::default()
623                        .shader_draw_parameters(true),
624                )
625            } else {
626                None
627            },
628        }
629    }
630
631    /// Compute the wgpu [`Features`] and [`DownlevelFlags`] supported by a physical device.
632    ///
633    /// Given `self`, together with the instance and physical device it was
634    /// built from, and a `caps` also built from those, determine which wgpu
635    /// features and downlevel flags the device can support.
636    ///
637    /// [`Features`]: wgt::Features
638    /// [`DownlevelFlags`]: wgt::DownlevelFlags
639    fn to_wgpu(
640        &self,
641        instance: &ash::Instance,
642        phd: vk::PhysicalDevice,
643        caps: &PhysicalDeviceProperties,
644        queue_props: &vk::QueueFamilyProperties,
645    ) -> (wgt::Features, wgt::DownlevelFlags) {
646        use wgt::{DownlevelFlags as Df, Features as F};
647        let mut features = F::empty()
648            | F::MAPPABLE_PRIMARY_BUFFERS
649            | F::IMMEDIATES
650            | F::ADDRESS_MODE_CLAMP_TO_BORDER
651            | F::ADDRESS_MODE_CLAMP_TO_ZERO
652            | F::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES
653            | F::CLEAR_TEXTURE
654            | F::PIPELINE_CACHE
655            | F::SHADER_EARLY_DEPTH_TEST
656            | F::TEXTURE_ATOMIC
657            | F::PASSTHROUGH_SHADERS
658            | F::MEMORY_DECORATION_COHERENT
659            | F::MEMORY_DECORATION_VOLATILE;
660
661        let mut dl_flags = Df::COMPUTE_SHADERS
662            | Df::BASE_VERTEX
663            | Df::READ_ONLY_DEPTH_STENCIL
664            | Df::NON_POWER_OF_TWO_MIPMAPPED_TEXTURES
665            | Df::COMPARISON_SAMPLERS
666            | Df::VERTEX_STORAGE
667            | Df::FRAGMENT_STORAGE
668            | Df::DEPTH_TEXTURE_AND_BUFFER_COPIES
669            | Df::BUFFER_BINDINGS_NOT_16_BYTE_ALIGNED
670            | Df::UNRESTRICTED_INDEX_BUFFER
671            | Df::INDIRECT_EXECUTION
672            | Df::VIEW_FORMATS
673            | Df::UNRESTRICTED_EXTERNAL_TEXTURE_COPIES
674            | Df::NONBLOCKING_QUERY_RESOLVE
675            | Df::SHADER_F16_IN_F32;
676
677        dl_flags.set(
678            Df::SURFACE_VIEW_FORMATS,
679            caps.supports_extension(khr::swapchain_mutable_format::NAME),
680        );
681        dl_flags.set(Df::CUBE_ARRAY_TEXTURES, self.core.image_cube_array != 0);
682        dl_flags.set(Df::ANISOTROPIC_FILTERING, self.core.sampler_anisotropy != 0);
683        dl_flags.set(
684            Df::FRAGMENT_WRITABLE_STORAGE,
685            self.core.fragment_stores_and_atomics != 0,
686        );
687        dl_flags.set(Df::MULTISAMPLED_SHADING, self.core.sample_rate_shading != 0);
688        dl_flags.set(Df::INDEPENDENT_BLEND, self.core.independent_blend != 0);
689        dl_flags.set(
690            Df::FULL_DRAW_INDEX_UINT32,
691            self.core.full_draw_index_uint32 != 0,
692        );
693        dl_flags.set(Df::DEPTH_BIAS_CLAMP, self.core.depth_bias_clamp != 0);
694
695        features.set(
696            F::TIMESTAMP_QUERY
697                | F::TIMESTAMP_QUERY_INSIDE_ENCODERS
698                | F::TIMESTAMP_QUERY_INSIDE_PASSES,
699            // Vulkan strictly defines this as either 36-64, or zero.
700            queue_props.timestamp_valid_bits >= 36,
701        );
702        features.set(
703            F::INDIRECT_FIRST_INSTANCE,
704            self.core.draw_indirect_first_instance != 0,
705        );
706        //if self.core.dual_src_blend != 0
707        features.set(F::POLYGON_MODE_LINE, self.core.fill_mode_non_solid != 0);
708        features.set(F::POLYGON_MODE_POINT, self.core.fill_mode_non_solid != 0);
709        //if self.core.depth_bounds != 0 {
710        //if self.core.alpha_to_one != 0 {
711        //if self.core.multi_viewport != 0 {
712        features.set(
713            F::TEXTURE_COMPRESSION_ETC2,
714            self.core.texture_compression_etc2 != 0,
715        );
716        features.set(
717            F::TEXTURE_COMPRESSION_ASTC,
718            self.core.texture_compression_astc_ldr != 0,
719        );
720        features.set(
721            F::TEXTURE_COMPRESSION_BC,
722            self.core.texture_compression_bc != 0,
723        );
724        features.set(
725            F::TEXTURE_COMPRESSION_BC_SLICED_3D,
726            self.core.texture_compression_bc != 0, // BC guarantees Sliced 3D
727        );
728        features.set(
729            F::PIPELINE_STATISTICS_QUERY,
730            self.core.pipeline_statistics_query != 0,
731        );
732        features.set(
733            F::VERTEX_WRITABLE_STORAGE,
734            self.core.vertex_pipeline_stores_and_atomics != 0,
735        );
736
737        features.set(F::SHADER_F64, self.core.shader_float64 != 0);
738        features.set(F::SHADER_INT64, self.core.shader_int64 != 0);
739        features.set(F::SHADER_I16, self.core.shader_int16 != 0);
740
741        features.set(F::PRIMITIVE_INDEX, self.core.geometry_shader != 0);
742
743        if let Some(ref shader_atomic_int64) = self.shader_atomic_int64 {
744            features.set(
745                F::SHADER_INT64_ATOMIC_ALL_OPS | F::SHADER_INT64_ATOMIC_MIN_MAX,
746                shader_atomic_int64.shader_buffer_int64_atomics != 0
747                    && shader_atomic_int64.shader_shared_int64_atomics != 0,
748            );
749        }
750
751        if let Some(ref shader_image_atomic_int64) = self.shader_image_atomic_int64 {
752            features.set(
753                F::TEXTURE_INT64_ATOMIC,
754                shader_image_atomic_int64
755                    .shader_image_int64_atomics(true)
756                    .shader_image_int64_atomics
757                    != 0,
758            );
759        }
760
761        if let Some(ref shader_atomic_float) = self.shader_atomic_float {
762            features.set(
763                F::SHADER_FLOAT32_ATOMIC,
764                shader_atomic_float.shader_buffer_float32_atomics != 0
765                    && shader_atomic_float.shader_buffer_float32_atomic_add != 0,
766            );
767        }
768
769        if let Some(ref shader_barycentrics) = self.shader_barycentrics {
770            features.set(
771                F::SHADER_BARYCENTRICS | F::SHADER_PER_VERTEX,
772                shader_barycentrics.fragment_shader_barycentric != 0,
773            );
774        }
775
776        //if caps.supports_extension(khr::sampler_mirror_clamp_to_edge::NAME) {
777        //if caps.supports_extension(ext::sampler_filter_minmax::NAME) {
778        features.set(
779            F::MULTI_DRAW_INDIRECT_COUNT,
780            caps.supports_extension(khr::draw_indirect_count::NAME),
781        );
782        features.set(
783            F::CONSERVATIVE_RASTERIZATION,
784            caps.supports_extension(ext::conservative_rasterization::NAME),
785        );
786        features.set(
787            F::EXPERIMENTAL_RAY_HIT_VERTEX_RETURN,
788            caps.supports_extension(khr::ray_tracing_position_fetch::NAME),
789        );
790
791        if let Some(ref descriptor_indexing) = self.descriptor_indexing {
792            // We use update-after-bind descriptors for all bind groups containing binding arrays.
793            //
794            // In those bind groups, we allow all binding types except uniform buffers to be present.
795            //
796            // As we can only switch between update-after-bind and not on a per bind group basis,
797            // all supported binding types need to be able to be marked update after bind.
798            //
799            // As such, we enable all features as a whole, rather individually.
800            let supports_descriptor_indexing =
801                // Sampled Images
802                descriptor_indexing.shader_sampled_image_array_non_uniform_indexing != 0
803                    && descriptor_indexing.descriptor_binding_sampled_image_update_after_bind != 0
804                    // Storage Images
805                    && descriptor_indexing.shader_storage_image_array_non_uniform_indexing != 0
806                    && descriptor_indexing.descriptor_binding_storage_image_update_after_bind != 0
807                    // Storage Buffers
808                    && descriptor_indexing.shader_storage_buffer_array_non_uniform_indexing != 0
809                    && descriptor_indexing.descriptor_binding_storage_buffer_update_after_bind != 0;
810
811            let descriptor_indexing_features = F::BUFFER_BINDING_ARRAY
812                | F::TEXTURE_BINDING_ARRAY
813                | F::STORAGE_RESOURCE_BINDING_ARRAY
814                | F::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING
815                | F::STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING;
816
817            features.set(descriptor_indexing_features, supports_descriptor_indexing);
818
819            let supports_partially_bound =
820                descriptor_indexing.descriptor_binding_partially_bound != 0;
821
822            features.set(F::PARTIALLY_BOUND_BINDING_ARRAY, supports_partially_bound);
823        }
824
825        features.set(F::DEPTH_CLIP_CONTROL, self.core.depth_clamp != 0);
826        features.set(F::DUAL_SOURCE_BLENDING, self.core.dual_src_blend != 0);
827        features.set(F::CLIP_DISTANCES, self.core.shader_clip_distance != 0);
828
829        if let Some(ref multiview) = self.multiview {
830            features.set(F::MULTIVIEW, multiview.multiview != 0);
831            features.set(F::SELECTIVE_MULTIVIEW, multiview.multiview != 0);
832        }
833
834        features.set(
835            F::TEXTURE_FORMAT_16BIT_NORM,
836            is_format_16bit_norm_supported(instance, phd),
837        );
838
839        if let Some(ref astc_hdr) = self.astc_hdr {
840            features.set(
841                F::TEXTURE_COMPRESSION_ASTC_HDR,
842                astc_hdr.texture_compression_astc_hdr != 0,
843            );
844        }
845
846        if self.core.texture_compression_astc_ldr != 0 {
847            features.set(
848                F::TEXTURE_COMPRESSION_ASTC_SLICED_3D,
849                supports_astc_3d(instance, phd),
850            );
851        }
852
853        if let (Some(ref f16_i8), Some(ref bit16)) = (self.shader_float16_int8, self._16bit_storage)
854        {
855            // Note `storage_input_output16` is not required, we polyfill `f16` I/O using `f32`
856            // types when this capability is not available
857            features.set(
858                F::SHADER_F16,
859                f16_i8.shader_float16 != 0
860                    && bit16.storage_buffer16_bit_access != 0
861                    && bit16.uniform_and_storage_buffer16_bit_access != 0,
862            );
863        }
864
865        if let Some(ref subgroup) = caps.subgroup {
866            if (caps.device_api_version >= vk::API_VERSION_1_3
867                || caps.supports_extension(ext::subgroup_size_control::NAME))
868                && subgroup.supported_operations.contains(
869                    vk::SubgroupFeatureFlags::BASIC
870                        | vk::SubgroupFeatureFlags::VOTE
871                        | vk::SubgroupFeatureFlags::ARITHMETIC
872                        | vk::SubgroupFeatureFlags::BALLOT
873                        | vk::SubgroupFeatureFlags::SHUFFLE
874                        | vk::SubgroupFeatureFlags::SHUFFLE_RELATIVE
875                        | vk::SubgroupFeatureFlags::QUAD,
876                )
877            {
878                features.set(
879                    F::SUBGROUP,
880                    subgroup
881                        .supported_stages
882                        .contains(vk::ShaderStageFlags::COMPUTE | vk::ShaderStageFlags::FRAGMENT),
883                );
884                features.set(
885                    F::SUBGROUP_VERTEX,
886                    subgroup
887                        .supported_stages
888                        .contains(vk::ShaderStageFlags::VERTEX),
889                );
890                features.insert(F::SUBGROUP_BARRIER);
891            }
892        }
893
894        let supports_depth_format = |format| {
895            supports_format(
896                instance,
897                phd,
898                format,
899                vk::ImageTiling::OPTIMAL,
900                depth_stencil_required_flags(),
901            )
902        };
903
904        let texture_s8 = supports_depth_format(vk::Format::S8_UINT);
905        let texture_d32 = supports_depth_format(vk::Format::D32_SFLOAT);
906        let texture_d24_s8 = supports_depth_format(vk::Format::D24_UNORM_S8_UINT);
907        let texture_d32_s8 = supports_depth_format(vk::Format::D32_SFLOAT_S8_UINT);
908
909        let stencil8 = texture_s8 || texture_d24_s8;
910        let depth24_plus_stencil8 = texture_d24_s8 || texture_d32_s8;
911
912        dl_flags.set(
913            Df::WEBGPU_TEXTURE_FORMAT_SUPPORT,
914            stencil8 && depth24_plus_stencil8 && texture_d32,
915        );
916
917        features.set(F::DEPTH32FLOAT_STENCIL8, texture_d32_s8);
918
919        let supports_acceleration_structures = caps
920            .supports_extension(khr::deferred_host_operations::NAME)
921            && caps.supports_extension(khr::acceleration_structure::NAME)
922            && caps.supports_extension(khr::buffer_device_address::NAME);
923
924        let supports_ray_query =
925            supports_acceleration_structures && caps.supports_extension(khr::ray_query::NAME);
926        let supports_acceleration_structure_binding_array = supports_ray_query
927            && self
928                .acceleration_structure
929                .as_ref()
930                .is_some_and(|features| {
931                    features.descriptor_binding_acceleration_structure_update_after_bind != 0
932                });
933
934        features.set(
935            F::EXPERIMENTAL_RAY_QUERY
936            // Although this doesn't really require ray queries, it does not make sense to be enabled if acceleration structures
937            // aren't enabled.
938                | F::EXTENDED_ACCELERATION_STRUCTURE_VERTEX_FORMATS,
939            supports_ray_query,
940        );
941
942        // Binding arrays of TLAS are supported on Vulkan when ray queries are supported.
943        //
944        // Note: this flag is used for shader-side `binding_array<acceleration_structure>` as well as
945        // allowing `BindGroupLayoutEntry::count = Some(...)` for `BindingType::AccelerationStructure`.
946        features.set(
947            F::ACCELERATION_STRUCTURE_BINDING_ARRAY,
948            supports_acceleration_structure_binding_array,
949        );
950
951        let rg11b10ufloat_renderable = supports_format(
952            instance,
953            phd,
954            vk::Format::B10G11R11_UFLOAT_PACK32,
955            vk::ImageTiling::OPTIMAL,
956            vk::FormatFeatureFlags::COLOR_ATTACHMENT
957                | vk::FormatFeatureFlags::COLOR_ATTACHMENT_BLEND,
958        );
959        features.set(F::RG11B10UFLOAT_RENDERABLE, rg11b10ufloat_renderable);
960
961        features.set(
962            F::BGRA8UNORM_STORAGE,
963            supports_bgra8unorm_storage(instance, phd, caps.device_api_version),
964        );
965
966        features.set(
967            F::FLOAT32_FILTERABLE,
968            is_float32_filterable_supported(instance, phd),
969        );
970
971        features.set(
972            F::FLOAT32_BLENDABLE,
973            is_float32_blendable_supported(instance, phd),
974        );
975
976        if let Some(ref _sampler_ycbcr_conversion) = self.sampler_ycbcr_conversion {
977            features.set(
978                F::TEXTURE_FORMAT_NV12,
979                supports_format(
980                    instance,
981                    phd,
982                    vk::Format::G8_B8R8_2PLANE_420_UNORM,
983                    vk::ImageTiling::OPTIMAL,
984                    vk::FormatFeatureFlags::SAMPLED_IMAGE
985                        | vk::FormatFeatureFlags::TRANSFER_SRC
986                        | vk::FormatFeatureFlags::TRANSFER_DST,
987                ) && !caps
988                    .driver
989                    .map(|driver| driver.driver_id == vk::DriverId::MOLTENVK)
990                    .unwrap_or_default(),
991            );
992        }
993
994        if let Some(ref _sampler_ycbcr_conversion) = self.sampler_ycbcr_conversion {
995            features.set(
996                F::TEXTURE_FORMAT_P010,
997                supports_format(
998                    instance,
999                    phd,
1000                    vk::Format::G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16,
1001                    vk::ImageTiling::OPTIMAL,
1002                    vk::FormatFeatureFlags::SAMPLED_IMAGE
1003                        | vk::FormatFeatureFlags::TRANSFER_SRC
1004                        | vk::FormatFeatureFlags::TRANSFER_DST,
1005                ) && !caps
1006                    .driver
1007                    .map(|driver| driver.driver_id == vk::DriverId::MOLTENVK)
1008                    .unwrap_or_default(),
1009            );
1010        }
1011
1012        features.set(
1013            F::VULKAN_GOOGLE_DISPLAY_TIMING,
1014            caps.supports_extension(google::display_timing::NAME),
1015        );
1016
1017        features.set(
1018            F::VULKAN_EXTERNAL_MEMORY_WIN32,
1019            caps.supports_extension(khr::external_memory_win32::NAME),
1020        );
1021        features.set(
1022            F::EXPERIMENTAL_MESH_SHADER,
1023            caps.supports_extension(ext::mesh_shader::NAME),
1024        );
1025        features.set(
1026            F::EXPERIMENTAL_MESH_SHADER_POINTS,
1027            caps.supports_extension(ext::mesh_shader::NAME),
1028        );
1029        if let Some(ref mesh_shader) = self.mesh_shader {
1030            features.set(
1031                F::EXPERIMENTAL_MESH_SHADER_MULTIVIEW,
1032                mesh_shader.multiview_mesh_shader != 0,
1033            );
1034        }
1035
1036        // Not supported by default by `VK_KHR_portability_subset`, which we use on apple platforms.
1037        features.set(
1038            F::MULTISAMPLE_ARRAY,
1039            self.portability_subset
1040                .map(|p| p.multisample_array_image == vk::TRUE)
1041                .unwrap_or(true),
1042        );
1043        // Enable cooperative matrix if any configuration is supported
1044        features.set(
1045            F::EXPERIMENTAL_COOPERATIVE_MATRIX,
1046            !caps.cooperative_matrix_properties.is_empty(),
1047        );
1048
1049        features.set(
1050            F::SHADER_DRAW_INDEX,
1051            self.shader_draw_parameters
1052                .is_some_and(|a| a.shader_draw_parameters != 0)
1053                || caps.supports_extension(c"VK_KHR_shader_draw_parameters"),
1054        );
1055
1056        (features, dl_flags)
1057    }
1058}
1059
1060/// Vulkan "properties" structures gathered about a physical device.
1061///
1062/// This structure holds the properties of a [`vk::PhysicalDevice`]:
1063/// - the standard Vulkan device properties
1064/// - the `VkExtensionProperties` structs for all available extensions, and
1065/// - the per-extension properties structures for the available extensions that
1066///   `wgpu` cares about.
1067///
1068/// Generally, if you get it from any of these functions, it's stored
1069/// here:
1070/// - `vkEnumerateDeviceExtensionProperties`
1071/// - `vkGetPhysicalDeviceProperties`
1072/// - `vkGetPhysicalDeviceProperties2`
1073///
1074/// This also includes a copy of the device API version, since we can
1075/// use that as a shortcut for searching for an extension, if the
1076/// extension has been promoted to core in the current version.
1077///
1078/// This does not include device features; for those, see
1079/// [`PhysicalDeviceFeatures`].
1080#[derive(Default, Debug)]
1081pub struct PhysicalDeviceProperties {
1082    /// Extensions supported by the `vk::PhysicalDevice`,
1083    /// as returned by `vkEnumerateDeviceExtensionProperties`.
1084    supported_extensions: Vec<vk::ExtensionProperties>,
1085
1086    /// Properties of the `vk::PhysicalDevice`, as returned by
1087    /// `vkGetPhysicalDeviceProperties`.
1088    properties: vk::PhysicalDeviceProperties,
1089
1090    /// Additional `vk::PhysicalDevice` properties from the
1091    /// `VK_KHR_maintenance3` extension, promoted to Vulkan 1.1.
1092    maintenance_3: Option<vk::PhysicalDeviceMaintenance3Properties<'static>>,
1093
1094    /// Additional `vk::PhysicalDevice` properties from the
1095    /// `VK_KHR_maintenance4` extension, promoted to Vulkan 1.3.
1096    maintenance_4: Option<vk::PhysicalDeviceMaintenance4Properties<'static>>,
1097
1098    /// Additional `vk::PhysicalDevice` properties from the
1099    /// `VK_EXT_descriptor_indexing` extension, promoted to Vulkan 1.2.
1100    descriptor_indexing: Option<vk::PhysicalDeviceDescriptorIndexingPropertiesEXT<'static>>,
1101
1102    /// Additional `vk::PhysicalDevice` properties from the
1103    /// `VK_KHR_acceleration_structure` extension.
1104    acceleration_structure: Option<vk::PhysicalDeviceAccelerationStructurePropertiesKHR<'static>>,
1105
1106    /// Additional `vk::PhysicalDevice` properties from the
1107    /// `VK_KHR_driver_properties` extension, promoted to Vulkan 1.2.
1108    driver: Option<vk::PhysicalDeviceDriverPropertiesKHR<'static>>,
1109
1110    /// Additional `vk::PhysicalDevice` properties from Vulkan 1.1.
1111    subgroup: Option<vk::PhysicalDeviceSubgroupProperties<'static>>,
1112
1113    /// Additional `vk::PhysicalDevice` properties from the
1114    /// `VK_EXT_subgroup_size_control` extension, promoted to Vulkan 1.3.
1115    subgroup_size_control: Option<vk::PhysicalDeviceSubgroupSizeControlProperties<'static>>,
1116
1117    /// Additional `vk::PhysicalDevice` properties from the
1118    /// `VK_EXT_robustness2` extension.
1119    robustness2: Option<vk::PhysicalDeviceRobustness2PropertiesEXT<'static>>,
1120
1121    /// Additional `vk::PhysicalDevice` properties from the
1122    /// `VK_EXT_mesh_shader` extension.
1123    mesh_shader: Option<vk::PhysicalDeviceMeshShaderPropertiesEXT<'static>>,
1124
1125    /// Additional `vk::PhysicalDevice` properties from the
1126    /// `VK_KHR_multiview` extension.
1127    multiview: Option<vk::PhysicalDeviceMultiviewPropertiesKHR<'static>>,
1128
1129    /// `VK_EXT_pci_bus_info` extension.
1130    pci_bus_info: Option<vk::PhysicalDevicePCIBusInfoPropertiesEXT<'static>>,
1131
1132    /// The device API version.
1133    ///
1134    /// Which is the version of Vulkan supported for device-level functionality.
1135    ///
1136    /// It is associated with a `VkPhysicalDevice` and its children.
1137    device_api_version: u32,
1138
1139    /// Supported cooperative matrix configurations.
1140    ///
1141    /// This is determined by querying `vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR`.
1142    cooperative_matrix_properties: Vec<wgt::CooperativeMatrixProperties>,
1143}
1144
1145impl PhysicalDeviceProperties {
1146    pub fn properties(&self) -> vk::PhysicalDeviceProperties {
1147        self.properties
1148    }
1149
1150    pub fn supports_extension(&self, extension: &CStr) -> bool {
1151        self.supported_extensions
1152            .iter()
1153            .any(|ep| ep.extension_name_as_c_str() == Ok(extension))
1154    }
1155
1156    /// Map `requested_features` to the list of Vulkan extension strings required to create the logical device.
1157    fn get_required_extensions(&self, requested_features: wgt::Features) -> Vec<&'static CStr> {
1158        let mut extensions = Vec::new();
1159
1160        // Note that quite a few extensions depend on the `VK_KHR_get_physical_device_properties2` instance extension.
1161        // We enable `VK_KHR_get_physical_device_properties2` unconditionally (if available).
1162
1163        // Require `VK_KHR_swapchain`
1164        extensions.push(khr::swapchain::NAME);
1165
1166        if self.device_api_version < vk::API_VERSION_1_1 {
1167            // Require `VK_KHR_maintenance1`
1168            extensions.push(khr::maintenance1::NAME);
1169
1170            // Optional `VK_KHR_maintenance2`
1171            if self.supports_extension(khr::maintenance2::NAME) {
1172                extensions.push(khr::maintenance2::NAME);
1173            }
1174
1175            // Optional `VK_KHR_maintenance3`
1176            if self.supports_extension(khr::maintenance3::NAME) {
1177                extensions.push(khr::maintenance3::NAME);
1178            }
1179
1180            // Require `VK_KHR_storage_buffer_storage_class`
1181            extensions.push(khr::storage_buffer_storage_class::NAME);
1182
1183            // Require `VK_KHR_multiview` if the associated feature was requested
1184            if requested_features.contains(wgt::Features::MULTIVIEW) {
1185                extensions.push(khr::multiview::NAME);
1186            }
1187
1188            // Require `VK_KHR_sampler_ycbcr_conversion` if the associated feature was requested
1189            if requested_features.contains(wgt::Features::TEXTURE_FORMAT_NV12) {
1190                extensions.push(khr::sampler_ycbcr_conversion::NAME);
1191            }
1192
1193            // Require `VK_KHR_16bit_storage` if the feature `SHADER_F16` was requested
1194            if requested_features.contains(wgt::Features::SHADER_F16) {
1195                // - Feature `SHADER_F16` also requires `VK_KHR_shader_float16_int8`, but we always
1196                //   require that anyway (if it is available) below.
1197                // - `VK_KHR_16bit_storage` requires `VK_KHR_storage_buffer_storage_class`, however
1198                //   we require that one already.
1199                extensions.push(khr::_16bit_storage::NAME);
1200            }
1201
1202            if requested_features.contains(wgt::Features::SHADER_DRAW_INDEX) {
1203                extensions.push(khr::shader_draw_parameters::NAME);
1204            }
1205        }
1206
1207        if self.device_api_version < vk::API_VERSION_1_2 {
1208            // Optional `VK_KHR_image_format_list`
1209            if self.supports_extension(khr::image_format_list::NAME) {
1210                extensions.push(khr::image_format_list::NAME);
1211            }
1212
1213            // Optional `VK_KHR_driver_properties`
1214            if self.supports_extension(khr::driver_properties::NAME) {
1215                extensions.push(khr::driver_properties::NAME);
1216            }
1217
1218            // Optional `VK_KHR_timeline_semaphore`
1219            if self.supports_extension(khr::timeline_semaphore::NAME) {
1220                extensions.push(khr::timeline_semaphore::NAME);
1221            }
1222
1223            // Require `VK_EXT_descriptor_indexing` if one of the associated features was requested
1224            if requested_features.intersects(INDEXING_FEATURES) {
1225                extensions.push(ext::descriptor_indexing::NAME);
1226            }
1227
1228            // Always require `VK_KHR_shader_float16_int8` if available as it enables
1229            // Int8 optimizations. Also require it even if it's not available but
1230            // requested so that we get a corresponding error message.
1231            if requested_features.contains(wgt::Features::SHADER_F16)
1232                || self.supports_extension(khr::shader_float16_int8::NAME)
1233            {
1234                extensions.push(khr::shader_float16_int8::NAME);
1235            }
1236
1237            if requested_features.intersects(wgt::Features::EXPERIMENTAL_MESH_SHADER) {
1238                extensions.push(khr::spirv_1_4::NAME);
1239            }
1240
1241            //extensions.push(khr::sampler_mirror_clamp_to_edge::NAME);
1242            //extensions.push(ext::sampler_filter_minmax::NAME);
1243        }
1244
1245        if self.device_api_version < vk::API_VERSION_1_3 {
1246            // Optional `VK_KHR_maintenance4`
1247            if self.supports_extension(khr::maintenance4::NAME) {
1248                extensions.push(khr::maintenance4::NAME);
1249            }
1250
1251            // Optional `VK_EXT_image_robustness`
1252            if self.supports_extension(ext::image_robustness::NAME) {
1253                extensions.push(ext::image_robustness::NAME);
1254            }
1255
1256            // Require `VK_EXT_subgroup_size_control` if the associated feature was requested
1257            if requested_features.contains(wgt::Features::SUBGROUP) {
1258                extensions.push(ext::subgroup_size_control::NAME);
1259            }
1260
1261            // Optional `VK_KHR_shader_integer_dot_product`
1262            if self.supports_extension(khr::shader_integer_dot_product::NAME) {
1263                extensions.push(khr::shader_integer_dot_product::NAME);
1264            }
1265        }
1266
1267        // Optional `VK_KHR_swapchain_mutable_format`
1268        if self.supports_extension(khr::swapchain_mutable_format::NAME) {
1269            extensions.push(khr::swapchain_mutable_format::NAME);
1270        }
1271
1272        // Optional `VK_EXT_robustness2`
1273        if self.supports_extension(ext::robustness2::NAME) {
1274            extensions.push(ext::robustness2::NAME);
1275        }
1276
1277        // Optional `VK_KHR_external_memory_win32`
1278        if self.supports_extension(khr::external_memory_win32::NAME) {
1279            extensions.push(khr::external_memory_win32::NAME);
1280        }
1281
1282        // Optional `VK_KHR_external_memory_fd`
1283        if self.supports_extension(khr::external_memory_fd::NAME) {
1284            extensions.push(khr::external_memory_fd::NAME);
1285        }
1286
1287        // Optional `VK_EXT_external_memory_dma`
1288        if self.supports_extension(ext::external_memory_dma_buf::NAME) {
1289            extensions.push(ext::external_memory_dma_buf::NAME);
1290        }
1291
1292        // Optional `VK_EXT_memory_budget`
1293        if self.supports_extension(ext::memory_budget::NAME) {
1294            extensions.push(ext::memory_budget::NAME);
1295        } else {
1296            log::debug!("VK_EXT_memory_budget is not available.")
1297        }
1298
1299        // Require `VK_KHR_draw_indirect_count` if the associated feature was requested
1300        // Even though Vulkan 1.2 has promoted the extension to core, we must require the extension to avoid
1301        // large amounts of spaghetti involved with using PhysicalDeviceVulkan12Features.
1302        if requested_features.contains(wgt::Features::MULTI_DRAW_INDIRECT_COUNT) {
1303            extensions.push(khr::draw_indirect_count::NAME);
1304        }
1305
1306        // Require `VK_KHR_deferred_host_operations`, `VK_KHR_acceleration_structure` `VK_KHR_buffer_device_address` (for acceleration structures) and`VK_KHR_ray_query` if `EXPERIMENTAL_RAY_QUERY` was requested
1307        if requested_features.contains(wgt::Features::EXPERIMENTAL_RAY_QUERY) {
1308            extensions.push(khr::deferred_host_operations::NAME);
1309            extensions.push(khr::acceleration_structure::NAME);
1310            extensions.push(khr::buffer_device_address::NAME);
1311            extensions.push(khr::ray_query::NAME);
1312        }
1313
1314        if requested_features.contains(wgt::Features::EXPERIMENTAL_RAY_HIT_VERTEX_RETURN) {
1315            extensions.push(khr::ray_tracing_position_fetch::NAME)
1316        }
1317
1318        // Require `VK_EXT_conservative_rasterization` if the associated feature was requested
1319        if requested_features.contains(wgt::Features::CONSERVATIVE_RASTERIZATION) {
1320            extensions.push(ext::conservative_rasterization::NAME);
1321        }
1322
1323        // Require `VK_KHR_portability_subset` on macOS/iOS
1324        #[cfg(target_vendor = "apple")]
1325        extensions.push(khr::portability_subset::NAME);
1326
1327        // Require `VK_EXT_texture_compression_astc_hdr` if the associated feature was requested
1328        if requested_features.contains(wgt::Features::TEXTURE_COMPRESSION_ASTC_HDR) {
1329            extensions.push(ext::texture_compression_astc_hdr::NAME);
1330        }
1331
1332        // Require `VK_KHR_shader_atomic_int64` if the associated feature was requested
1333        if requested_features.intersects(
1334            wgt::Features::SHADER_INT64_ATOMIC_ALL_OPS | wgt::Features::SHADER_INT64_ATOMIC_MIN_MAX,
1335        ) {
1336            extensions.push(khr::shader_atomic_int64::NAME);
1337        }
1338
1339        // Require `VK_EXT_shader_image_atomic_int64` if the associated feature was requested
1340        if requested_features.intersects(wgt::Features::TEXTURE_INT64_ATOMIC) {
1341            extensions.push(ext::shader_image_atomic_int64::NAME);
1342        }
1343
1344        // Require `VK_EXT_shader_atomic_float` if the associated feature was requested
1345        if requested_features.contains(wgt::Features::SHADER_FLOAT32_ATOMIC) {
1346            extensions.push(ext::shader_atomic_float::NAME);
1347        }
1348
1349        // Require VK_GOOGLE_display_timing if the associated feature was requested
1350        if requested_features.contains(wgt::Features::VULKAN_GOOGLE_DISPLAY_TIMING) {
1351            extensions.push(google::display_timing::NAME);
1352        }
1353
1354        if requested_features.contains(wgt::Features::EXPERIMENTAL_MESH_SHADER) {
1355            extensions.push(ext::mesh_shader::NAME);
1356        }
1357
1358        // Require `VK_KHR_fragment_shader_barycentric` if an associated feature was requested
1359        // Vulkan bundles both barycentrics and per-vertex attributes under the same feature.
1360        if requested_features
1361            .intersects(wgt::Features::SHADER_BARYCENTRICS | wgt::Features::SHADER_PER_VERTEX)
1362        {
1363            extensions.push(khr::fragment_shader_barycentric::NAME);
1364        }
1365
1366        // Require `VK_KHR_cooperative_matrix` if the associated feature was requested
1367        if requested_features.contains(wgt::Features::EXPERIMENTAL_COOPERATIVE_MATRIX) {
1368            extensions.push(khr::cooperative_matrix::NAME);
1369        }
1370
1371        extensions
1372    }
1373
1374    fn to_wgpu_limits(&self) -> wgt::Limits {
1375        let limits = &self.properties.limits;
1376
1377        let (
1378            mut max_task_mesh_workgroup_total_count,
1379            mut max_task_mesh_workgroups_per_dimension,
1380            mut max_task_invocations_per_workgroup,
1381            mut max_task_invocations_per_dimension,
1382            mut max_mesh_invocations_per_workgroup,
1383            mut max_mesh_invocations_per_dimension,
1384            mut max_task_payload_size,
1385            mut max_mesh_output_vertices,
1386            mut max_mesh_output_primitives,
1387            mut max_mesh_output_layers,
1388            mut max_mesh_multiview_view_count,
1389        ) = Default::default();
1390        if let Some(m) = self.mesh_shader {
1391            max_task_mesh_workgroup_total_count = m
1392                .max_task_work_group_total_count
1393                .min(m.max_mesh_work_group_total_count);
1394            max_task_mesh_workgroups_per_dimension = m
1395                .max_task_work_group_count
1396                .into_iter()
1397                .chain(m.max_mesh_work_group_count)
1398                .min()
1399                .unwrap();
1400            max_task_invocations_per_workgroup = m.max_task_work_group_invocations;
1401            max_task_invocations_per_dimension =
1402                m.max_task_work_group_size.into_iter().min().unwrap();
1403            max_mesh_invocations_per_workgroup = m.max_mesh_work_group_invocations;
1404            max_mesh_invocations_per_dimension =
1405                m.max_mesh_work_group_size.into_iter().min().unwrap();
1406            max_task_payload_size = m.max_task_payload_size;
1407            max_mesh_output_vertices = m.max_mesh_output_vertices;
1408            max_mesh_output_primitives = m.max_mesh_output_primitives;
1409            max_mesh_output_layers = m.max_mesh_output_layers;
1410            max_mesh_multiview_view_count = m.max_mesh_multiview_view_count;
1411        }
1412
1413        let max_memory_allocation_size = self
1414            .maintenance_3
1415            .map(|maintenance_3| maintenance_3.max_memory_allocation_size)
1416            .unwrap_or(u64::MAX);
1417        let max_buffer_size = self
1418            .maintenance_4
1419            .map(|maintenance_4| maintenance_4.max_buffer_size)
1420            .unwrap_or(u64::MAX);
1421        let max_buffer_size = max_buffer_size.min(max_memory_allocation_size);
1422
1423        // Prevent very large buffers on mesa and most android devices, and in all cases
1424        // don't risk confusing JS by exceeding the range of a double.
1425        let is_nvidia = self.properties.vendor_id == crate::auxil::db::nvidia::VENDOR;
1426        let max_buffer_size_cap =
1427            if (cfg!(target_os = "linux") || cfg!(target_os = "android")) && !is_nvidia {
1428                i32::MAX as u64
1429            } else {
1430                1u64 << 52
1431            };
1432
1433        let max_buffer_size = max_buffer_size.min(max_buffer_size_cap);
1434
1435        let mut max_binding_array_elements = 0;
1436        let mut max_sampler_binding_array_elements = 0;
1437        if let Some(ref descriptor_indexing) = self.descriptor_indexing {
1438            max_binding_array_elements = descriptor_indexing
1439                .max_descriptor_set_update_after_bind_sampled_images
1440                .min(descriptor_indexing.max_descriptor_set_update_after_bind_storage_images)
1441                .min(descriptor_indexing.max_descriptor_set_update_after_bind_storage_buffers)
1442                .min(descriptor_indexing.max_per_stage_descriptor_update_after_bind_sampled_images)
1443                .min(descriptor_indexing.max_per_stage_descriptor_update_after_bind_storage_images)
1444                .min(
1445                    descriptor_indexing.max_per_stage_descriptor_update_after_bind_storage_buffers,
1446                );
1447
1448            max_sampler_binding_array_elements = descriptor_indexing
1449                .max_descriptor_set_update_after_bind_samplers
1450                .min(descriptor_indexing.max_per_stage_descriptor_update_after_bind_samplers);
1451        }
1452
1453        const MAX_SHADER_STAGES_PER_PIPELINE: u32 = 2;
1454
1455        // When summed, the 3 limits below must be under Vulkan's maxFragmentCombinedOutputResources.
1456        // https://gpuweb.github.io/gpuweb/correspondence/#vulkan-maxFragmentCombinedOutputResources
1457        //
1458        // - maxStorageTexturesPerShaderStage, WebGPU default: 4
1459        // - maxStorageBuffersPerShaderStage, WebGPU default: 8
1460        // - maxColorAttachments, WebGPU default: 8
1461        //
1462        // However, maxFragmentCombinedOutputResources should be ignored on
1463        // intel/nvidia/amd/imgtec since it's not reported correctly.
1464        //
1465        // https://github.com/gpuweb/gpuweb/issues/3631#issuecomment-1498747606
1466        // https://github.com/gpuweb/gpuweb/issues/4018
1467        let mut max_storage_textures_per_shader_stage = limits
1468            .max_per_stage_descriptor_storage_images
1469            .min(limits.max_descriptor_set_storage_images / MAX_SHADER_STAGES_PER_PIPELINE);
1470        let mut max_storage_buffers_per_shader_stage = limits
1471            .max_per_stage_descriptor_storage_buffers
1472            .min(limits.max_descriptor_set_storage_buffers / MAX_SHADER_STAGES_PER_PIPELINE);
1473        let mut max_color_attachments = limits
1474            .max_color_attachments
1475            .min(limits.max_fragment_output_attachments);
1476
1477        let ignore_max_fragment_combined_output_resources = [
1478            crate::auxil::db::intel::VENDOR,
1479            crate::auxil::db::nvidia::VENDOR,
1480            crate::auxil::db::amd::VENDOR,
1481            crate::auxil::db::imgtec::VENDOR,
1482        ]
1483        .contains(&self.properties.vendor_id);
1484
1485        if !ignore_max_fragment_combined_output_resources {
1486            crate::auxil::cap_limits_to_be_under_the_sum_limit(
1487                [
1488                    &mut max_storage_textures_per_shader_stage,
1489                    &mut max_storage_buffers_per_shader_stage,
1490                    &mut max_color_attachments,
1491                ],
1492                limits.max_fragment_combined_output_resources,
1493            );
1494        }
1495
1496        // When summed, the 5 limits below must be under Vulkan's maxPerStageResources.
1497        //
1498        // - maxUniformBuffersPerShaderStage, WebGPU default: 12
1499        // - maxSampledTexturesPerShaderStage, WebGPU default: 16
1500        // - maxStorageTexturesPerShaderStage, WebGPU default: 4
1501        // - maxStorageBuffersPerShaderStage, WebGPU default: 8
1502        // - maxColorAttachments, WebGPU default: 8
1503        //
1504        // Note: Vulkan's texel buffers and input attachments also count towards
1505        // maxPerStageResources but we don't make use of them.
1506        let mut max_sampled_textures_per_shader_stage = limits
1507            .max_per_stage_descriptor_sampled_images
1508            .min(limits.max_descriptor_set_sampled_images / MAX_SHADER_STAGES_PER_PIPELINE);
1509        let mut max_uniform_buffers_per_shader_stage = limits
1510            .max_per_stage_descriptor_uniform_buffers
1511            .min(limits.max_descriptor_set_uniform_buffers / MAX_SHADER_STAGES_PER_PIPELINE);
1512
1513        crate::auxil::cap_limits_to_be_under_the_sum_limit(
1514            [
1515                &mut max_sampled_textures_per_shader_stage,
1516                &mut max_uniform_buffers_per_shader_stage,
1517                &mut max_storage_textures_per_shader_stage,
1518                &mut max_storage_buffers_per_shader_stage,
1519                &mut max_color_attachments,
1520            ],
1521            limits.max_per_stage_resources,
1522        );
1523
1524        // Acceleration structure limits
1525        let mut max_blas_geometry_count = 0;
1526        let mut max_blas_primitive_count = 0;
1527        let mut max_tlas_instance_count = 0;
1528        let mut max_acceleration_structures_per_shader_stage = 0;
1529        if let Some(properties) = self.acceleration_structure {
1530            max_blas_geometry_count = properties.max_geometry_count as u32;
1531            max_blas_primitive_count = properties.max_primitive_count as u32;
1532            max_tlas_instance_count = properties.max_instance_count as u32;
1533            max_acceleration_structures_per_shader_stage = properties
1534                .max_per_stage_descriptor_acceleration_structures
1535                .min(
1536                    properties.max_descriptor_set_acceleration_structures
1537                        / MAX_SHADER_STAGES_PER_PIPELINE,
1538                );
1539        }
1540
1541        // When summed, the 6 limits below must be under Vulkan's
1542        // maxPerSetDescriptors / MAX_SHADER_STAGES_PER_PIPELINE.
1543        //
1544        // - maxUniformBuffersPerShaderStage, WebGPU default: 12
1545        // - maxSampledTexturesPerShaderStage, WebGPU default: 16
1546        // - maxStorageTexturesPerShaderStage, WebGPU default: 4
1547        // - maxStorageBuffersPerShaderStage, WebGPU default: 8
1548        // - maxSamplersPerShaderStage, WebGPU default: 16
1549        // - maxAccelerationStructuresPerShaderStage, Native only
1550        //
1551        // Note: All Vulkan's descriptor types count towards maxPerSetDescriptors but
1552        // we don't use all of them.
1553        // See https://registry.khronos.org/vulkan/specs/latest/html/vkspec.html#interfaces-resources-limits
1554        let max_per_set_descriptors = self
1555            .maintenance_3
1556            .map(|maintenance_3| maintenance_3.max_per_set_descriptors)
1557            // The lowest value seen in reports is 312, use 256 as a safe default.
1558            // https://vulkan.gpuinfo.org/displayextensionproperty.php?extensionname=VK_KHR_maintenance3&extensionproperty=maxPerSetDescriptors&platform=all
1559            // https://vulkan.gpuinfo.org/displaycoreproperty.php?core=1.1&name=maxPerSetDescriptors&platform=all
1560            .unwrap_or(256);
1561
1562        let mut max_samplers_per_shader_stage = limits
1563            .max_per_stage_descriptor_samplers
1564            .min(limits.max_descriptor_set_samplers / MAX_SHADER_STAGES_PER_PIPELINE);
1565
1566        crate::auxil::cap_limits_to_be_under_the_sum_limit(
1567            [
1568                &mut max_sampled_textures_per_shader_stage,
1569                &mut max_uniform_buffers_per_shader_stage,
1570                &mut max_storage_textures_per_shader_stage,
1571                &mut max_storage_buffers_per_shader_stage,
1572                &mut max_samplers_per_shader_stage,
1573                &mut max_acceleration_structures_per_shader_stage,
1574            ],
1575            max_per_set_descriptors / MAX_SHADER_STAGES_PER_PIPELINE,
1576        );
1577
1578        // Use max(default, maxPerSetDescriptors) since the spec requires this
1579        // limit to be at least 1000. This is ok because we already lowered
1580        // all the other relevant per stage limits so their sum is lower
1581        // than maxPerSetDescriptors.
1582        let max_bindings_per_bind_group = 1000.max(max_per_set_descriptors);
1583
1584        // TODO: programmatically determine this, if possible. It's unclear whether we can
1585        // as of https://github.com/gpuweb/gpuweb/issues/2965#issuecomment-1361315447.
1586        //
1587        // In theory some tilers may not support this much. We can't tell however, and
1588        // the driver will throw a DEVICE_REMOVED if it goes too high in usage. This is fine.
1589        let max_color_attachment_bytes_per_sample =
1590            max_color_attachments * wgt::TextureFormat::MAX_TARGET_PIXEL_BYTE_COST;
1591
1592        let max_multiview_view_count = self
1593            .multiview
1594            .map(|a| a.max_multiview_view_count.min(32))
1595            .unwrap_or(0);
1596
1597        crate::auxil::adjust_raw_limits(wgt::Limits {
1598            //
1599            // WebGPU LIMITS:
1600            // Based on https://gpuweb.github.io/gpuweb/correspondence/#limits
1601            //
1602            max_texture_dimension_1d: limits.max_image_dimension1_d,
1603            max_texture_dimension_2d: limits
1604                .max_image_dimension2_d
1605                .min(limits.max_image_dimension_cube)
1606                .min(limits.max_framebuffer_width)
1607                .min(limits.max_framebuffer_height),
1608            max_texture_dimension_3d: limits.max_image_dimension3_d,
1609            max_texture_array_layers: limits.max_image_array_layers,
1610            max_bind_groups: limits.max_bound_descriptor_sets,
1611            max_bindings_per_bind_group,
1612            max_dynamic_uniform_buffers_per_pipeline_layout: limits
1613                .max_descriptor_set_uniform_buffers_dynamic,
1614            max_dynamic_storage_buffers_per_pipeline_layout: limits
1615                .max_descriptor_set_storage_buffers_dynamic,
1616            max_samplers_per_shader_stage,
1617            max_sampled_textures_per_shader_stage,
1618            max_storage_textures_per_shader_stage,
1619            max_storage_buffers_per_shader_stage,
1620            max_uniform_buffers_per_shader_stage,
1621            max_vertex_buffers: limits.max_vertex_input_bindings,
1622            max_buffer_size,
1623            max_uniform_buffer_binding_size: limits
1624                .max_uniform_buffer_range
1625                .min(crate::auxil::MAX_I32_BINDING_SIZE)
1626                .into(),
1627            max_storage_buffer_binding_size: limits
1628                .max_storage_buffer_range
1629                .min(crate::auxil::MAX_I32_BINDING_SIZE)
1630                .into(),
1631            min_uniform_buffer_offset_alignment: limits.min_uniform_buffer_offset_alignment as u32,
1632            min_storage_buffer_offset_alignment: limits.min_storage_buffer_offset_alignment as u32,
1633            max_vertex_attributes: limits.max_vertex_input_attributes,
1634            max_vertex_buffer_array_stride: limits.max_vertex_input_binding_stride,
1635            max_inter_stage_shader_variables: limits
1636                .max_vertex_output_components
1637                .min(limits.max_fragment_input_components)
1638                / 4
1639                - 1, // -1 for position
1640            max_color_attachments,
1641            max_color_attachment_bytes_per_sample,
1642            max_compute_workgroup_storage_size: limits.max_compute_shared_memory_size,
1643            max_compute_invocations_per_workgroup: limits.max_compute_work_group_invocations,
1644            max_compute_workgroup_size_x: limits.max_compute_work_group_size[0],
1645            max_compute_workgroup_size_y: limits.max_compute_work_group_size[1],
1646            max_compute_workgroup_size_z: limits.max_compute_work_group_size[2],
1647            max_compute_workgroups_per_dimension: limits.max_compute_work_group_count[0]
1648                .min(limits.max_compute_work_group_count[1])
1649                .min(limits.max_compute_work_group_count[2]),
1650            max_immediate_size: limits.max_push_constants_size,
1651            //
1652            // NATIVE (Non-WebGPU) LIMITS:
1653            //
1654            max_non_sampler_bindings: u32::MAX,
1655
1656            max_binding_array_elements_per_shader_stage: max_binding_array_elements,
1657            max_binding_array_sampler_elements_per_shader_stage: max_sampler_binding_array_elements,
1658            max_binding_array_acceleration_structure_elements_per_shader_stage: if self
1659                .descriptor_indexing
1660                .is_some()
1661            {
1662                max_acceleration_structures_per_shader_stage
1663            } else {
1664                0
1665            },
1666
1667            max_task_mesh_workgroup_total_count,
1668            max_task_mesh_workgroups_per_dimension,
1669            max_task_invocations_per_workgroup,
1670            max_task_invocations_per_dimension,
1671
1672            max_mesh_invocations_per_workgroup,
1673            max_mesh_invocations_per_dimension,
1674
1675            max_task_payload_size,
1676            max_mesh_output_vertices,
1677            max_mesh_output_primitives,
1678            max_mesh_output_layers,
1679            max_mesh_multiview_view_count,
1680
1681            max_blas_primitive_count,
1682            max_blas_geometry_count,
1683            max_tlas_instance_count,
1684            max_acceleration_structures_per_shader_stage,
1685
1686            max_multiview_view_count,
1687        })
1688    }
1689
1690    /// Return a `wgpu_hal::Alignments` structure describing this adapter.
1691    ///
1692    /// The `using_robustness2` argument says how this adapter will implement
1693    /// `wgpu_hal`'s guarantee that shaders can only read the [accessible
1694    /// region][ar] of bindgroup's buffer bindings:
1695    ///
1696    /// - If this adapter will depend on `VK_EXT_robustness2`'s
1697    ///   `robustBufferAccess2` feature to apply bounds checks to shader buffer
1698    ///   access, `using_robustness2` must be `true`.
1699    ///
1700    /// - Otherwise, this adapter must use Naga to inject bounds checks on
1701    ///   buffer accesses, and `using_robustness2` must be `false`.
1702    ///
1703    /// [ar]: ../../struct.BufferBinding.html#accessible-region
1704    fn to_hal_alignments(&self, using_robustness2: bool) -> crate::Alignments {
1705        let limits = &self.properties.limits;
1706        crate::Alignments {
1707            buffer_copy_offset: wgt::BufferSize::new(limits.optimal_buffer_copy_offset_alignment)
1708                .unwrap(),
1709            buffer_copy_pitch: wgt::BufferSize::new(limits.optimal_buffer_copy_row_pitch_alignment)
1710                .unwrap(),
1711            uniform_bounds_check_alignment: {
1712                let alignment = if using_robustness2 {
1713                    self.robustness2
1714                        .unwrap() // if we're using it, we should have its properties
1715                        .robust_uniform_buffer_access_size_alignment
1716                } else {
1717                    // If the `robustness2` properties are unavailable, then `robustness2` is not available either Naga-injected bounds checks are precise.
1718                    1
1719                };
1720                wgt::BufferSize::new(alignment).unwrap()
1721            },
1722            raw_tlas_instance_size: 64,
1723            ray_tracing_scratch_buffer_alignment: self.acceleration_structure.map_or(
1724                0,
1725                |acceleration_structure| {
1726                    acceleration_structure.min_acceleration_structure_scratch_offset_alignment
1727                },
1728            ),
1729        }
1730    }
1731}
1732
1733impl super::InstanceShared {
1734    fn inspect(
1735        &self,
1736        phd: vk::PhysicalDevice,
1737    ) -> (PhysicalDeviceProperties, PhysicalDeviceFeatures) {
1738        let capabilities = {
1739            let mut capabilities = PhysicalDeviceProperties::default();
1740            capabilities.supported_extensions =
1741                unsafe { self.raw.enumerate_device_extension_properties(phd).unwrap() };
1742            capabilities.properties = unsafe { self.raw.get_physical_device_properties(phd) };
1743            capabilities.device_api_version = capabilities.properties.api_version;
1744
1745            let supports_multiview = capabilities.device_api_version >= vk::API_VERSION_1_1
1746                || capabilities.supports_extension(khr::multiview::NAME);
1747
1748            if let Some(ref get_device_properties) = self.get_physical_device_properties {
1749                // Get these now to avoid borrowing conflicts later
1750                let supports_maintenance3 = capabilities.device_api_version >= vk::API_VERSION_1_1
1751                    || capabilities.supports_extension(khr::maintenance3::NAME);
1752                let supports_maintenance4 = capabilities.device_api_version >= vk::API_VERSION_1_3
1753                    || capabilities.supports_extension(khr::maintenance4::NAME);
1754                let supports_descriptor_indexing = capabilities.device_api_version
1755                    >= vk::API_VERSION_1_2
1756                    || capabilities.supports_extension(ext::descriptor_indexing::NAME);
1757                let supports_driver_properties = capabilities.device_api_version
1758                    >= vk::API_VERSION_1_2
1759                    || capabilities.supports_extension(khr::driver_properties::NAME);
1760                let supports_subgroup_size_control = capabilities.device_api_version
1761                    >= vk::API_VERSION_1_3
1762                    || capabilities.supports_extension(ext::subgroup_size_control::NAME);
1763                let supports_robustness2 = capabilities.supports_extension(ext::robustness2::NAME);
1764                let supports_pci_bus_info =
1765                    capabilities.supports_extension(ext::pci_bus_info::NAME);
1766
1767                let supports_acceleration_structure =
1768                    capabilities.supports_extension(khr::acceleration_structure::NAME);
1769
1770                let supports_mesh_shader = capabilities.supports_extension(ext::mesh_shader::NAME);
1771
1772                let mut properties2 = vk::PhysicalDeviceProperties2KHR::default();
1773                if supports_maintenance3 {
1774                    let next = capabilities
1775                        .maintenance_3
1776                        .insert(vk::PhysicalDeviceMaintenance3Properties::default());
1777                    properties2 = properties2.push_next(next);
1778                }
1779
1780                if supports_maintenance4 {
1781                    let next = capabilities
1782                        .maintenance_4
1783                        .insert(vk::PhysicalDeviceMaintenance4Properties::default());
1784                    properties2 = properties2.push_next(next);
1785                }
1786
1787                if supports_descriptor_indexing {
1788                    let next = capabilities
1789                        .descriptor_indexing
1790                        .insert(vk::PhysicalDeviceDescriptorIndexingPropertiesEXT::default());
1791                    properties2 = properties2.push_next(next);
1792                }
1793
1794                if supports_acceleration_structure {
1795                    let next = capabilities
1796                        .acceleration_structure
1797                        .insert(vk::PhysicalDeviceAccelerationStructurePropertiesKHR::default());
1798                    properties2 = properties2.push_next(next);
1799                }
1800
1801                if supports_driver_properties {
1802                    let next = capabilities
1803                        .driver
1804                        .insert(vk::PhysicalDeviceDriverPropertiesKHR::default());
1805                    properties2 = properties2.push_next(next);
1806                }
1807
1808                if capabilities.device_api_version >= vk::API_VERSION_1_1 {
1809                    let next = capabilities
1810                        .subgroup
1811                        .insert(vk::PhysicalDeviceSubgroupProperties::default());
1812                    properties2 = properties2.push_next(next);
1813                }
1814
1815                if supports_subgroup_size_control {
1816                    let next = capabilities
1817                        .subgroup_size_control
1818                        .insert(vk::PhysicalDeviceSubgroupSizeControlProperties::default());
1819                    properties2 = properties2.push_next(next);
1820                }
1821
1822                if supports_robustness2 {
1823                    let next = capabilities
1824                        .robustness2
1825                        .insert(vk::PhysicalDeviceRobustness2PropertiesEXT::default());
1826                    properties2 = properties2.push_next(next);
1827                }
1828
1829                if supports_pci_bus_info {
1830                    let next = capabilities
1831                        .pci_bus_info
1832                        .insert(vk::PhysicalDevicePCIBusInfoPropertiesEXT::default());
1833                    properties2 = properties2.push_next(next);
1834                }
1835
1836                if supports_mesh_shader {
1837                    let next = capabilities
1838                        .mesh_shader
1839                        .insert(vk::PhysicalDeviceMeshShaderPropertiesEXT::default());
1840                    properties2 = properties2.push_next(next);
1841                }
1842
1843                if supports_multiview {
1844                    let next = capabilities
1845                        .multiview
1846                        .insert(vk::PhysicalDeviceMultiviewProperties::default());
1847                    properties2 = properties2.push_next(next);
1848                }
1849
1850                unsafe {
1851                    get_device_properties.get_physical_device_properties2(phd, &mut properties2)
1852                };
1853
1854                // Query cooperative matrix properties
1855                if capabilities.supports_extension(khr::cooperative_matrix::NAME) {
1856                    let coop_matrix =
1857                        khr::cooperative_matrix::Instance::new(&self.entry, &self.raw);
1858                    capabilities.cooperative_matrix_properties =
1859                        query_cooperative_matrix_properties(&coop_matrix, phd);
1860                }
1861
1862                if is_intel_igpu_outdated_for_robustness2(
1863                    capabilities.properties,
1864                    capabilities.driver,
1865                ) {
1866                    capabilities
1867                        .supported_extensions
1868                        .retain(|&x| x.extension_name_as_c_str() != Ok(ext::robustness2::NAME));
1869                    capabilities.robustness2 = None;
1870                }
1871            };
1872            capabilities
1873        };
1874
1875        let mut features = PhysicalDeviceFeatures::default();
1876        features.core = if let Some(ref get_device_properties) = self.get_physical_device_properties
1877        {
1878            let core = vk::PhysicalDeviceFeatures::default();
1879            let mut features2 = vk::PhysicalDeviceFeatures2KHR::default().features(core);
1880
1881            // `VK_KHR_multiview` is promoted to 1.1
1882            if capabilities.device_api_version >= vk::API_VERSION_1_1
1883                || capabilities.supports_extension(khr::multiview::NAME)
1884            {
1885                let next = features
1886                    .multiview
1887                    .insert(vk::PhysicalDeviceMultiviewFeatures::default());
1888                features2 = features2.push_next(next);
1889            }
1890
1891            // `VK_KHR_sampler_ycbcr_conversion` is promoted to 1.1
1892            if capabilities.device_api_version >= vk::API_VERSION_1_1
1893                || capabilities.supports_extension(khr::sampler_ycbcr_conversion::NAME)
1894            {
1895                let next = features
1896                    .sampler_ycbcr_conversion
1897                    .insert(vk::PhysicalDeviceSamplerYcbcrConversionFeatures::default());
1898                features2 = features2.push_next(next);
1899            }
1900
1901            if capabilities.supports_extension(ext::descriptor_indexing::NAME) {
1902                let next = features
1903                    .descriptor_indexing
1904                    .insert(vk::PhysicalDeviceDescriptorIndexingFeaturesEXT::default());
1905                features2 = features2.push_next(next);
1906            }
1907
1908            // `VK_KHR_timeline_semaphore` is promoted to 1.2, but has no
1909            // changes, so we can keep using the extension unconditionally.
1910            if capabilities.supports_extension(khr::timeline_semaphore::NAME) {
1911                let next = features
1912                    .timeline_semaphore
1913                    .insert(vk::PhysicalDeviceTimelineSemaphoreFeaturesKHR::default());
1914                features2 = features2.push_next(next);
1915            }
1916
1917            // `VK_KHR_shader_atomic_int64` is promoted to 1.2, but has no
1918            // changes, so we can keep using the extension unconditionally.
1919            if capabilities.device_api_version >= vk::API_VERSION_1_2
1920                || capabilities.supports_extension(khr::shader_atomic_int64::NAME)
1921            {
1922                let next = features
1923                    .shader_atomic_int64
1924                    .insert(vk::PhysicalDeviceShaderAtomicInt64Features::default());
1925                features2 = features2.push_next(next);
1926            }
1927
1928            if capabilities.supports_extension(ext::shader_image_atomic_int64::NAME) {
1929                let next = features
1930                    .shader_image_atomic_int64
1931                    .insert(vk::PhysicalDeviceShaderImageAtomicInt64FeaturesEXT::default());
1932                features2 = features2.push_next(next);
1933            }
1934            if capabilities.supports_extension(ext::shader_atomic_float::NAME) {
1935                let next = features
1936                    .shader_atomic_float
1937                    .insert(vk::PhysicalDeviceShaderAtomicFloatFeaturesEXT::default());
1938                features2 = features2.push_next(next);
1939            }
1940            if capabilities.supports_extension(ext::image_robustness::NAME) {
1941                let next = features
1942                    .image_robustness
1943                    .insert(vk::PhysicalDeviceImageRobustnessFeaturesEXT::default());
1944                features2 = features2.push_next(next);
1945            }
1946            if capabilities.supports_extension(ext::robustness2::NAME) {
1947                let next = features
1948                    .robustness2
1949                    .insert(vk::PhysicalDeviceRobustness2FeaturesEXT::default());
1950                features2 = features2.push_next(next);
1951            }
1952            if capabilities.supports_extension(ext::texture_compression_astc_hdr::NAME) {
1953                let next = features
1954                    .astc_hdr
1955                    .insert(vk::PhysicalDeviceTextureCompressionASTCHDRFeaturesEXT::default());
1956                features2 = features2.push_next(next);
1957            }
1958
1959            // `VK_KHR_shader_float16_int8` is promoted to 1.2
1960            if capabilities.device_api_version >= vk::API_VERSION_1_2
1961                || capabilities.supports_extension(khr::shader_float16_int8::NAME)
1962            {
1963                let next = features
1964                    .shader_float16_int8
1965                    .insert(vk::PhysicalDeviceShaderFloat16Int8FeaturesKHR::default());
1966                features2 = features2.push_next(next);
1967            }
1968
1969            if capabilities.supports_extension(khr::_16bit_storage::NAME) {
1970                let next = features
1971                    ._16bit_storage
1972                    .insert(vk::PhysicalDevice16BitStorageFeaturesKHR::default());
1973                features2 = features2.push_next(next);
1974            }
1975            if capabilities.supports_extension(khr::acceleration_structure::NAME) {
1976                let next = features
1977                    .acceleration_structure
1978                    .insert(vk::PhysicalDeviceAccelerationStructureFeaturesKHR::default());
1979                features2 = features2.push_next(next);
1980            }
1981
1982            if capabilities.supports_extension(khr::ray_tracing_position_fetch::NAME) {
1983                let next = features
1984                    .position_fetch
1985                    .insert(vk::PhysicalDeviceRayTracingPositionFetchFeaturesKHR::default());
1986                features2 = features2.push_next(next);
1987            }
1988
1989            // `VK_KHR_maintenance4` is promoted to 1.3
1990            if capabilities.device_api_version >= vk::API_VERSION_1_3
1991                || capabilities.supports_extension(khr::maintenance4::NAME)
1992            {
1993                let next = features
1994                    .maintenance4
1995                    .insert(vk::PhysicalDeviceMaintenance4Features::default());
1996                features2 = features2.push_next(next);
1997            }
1998
1999            // `VK_KHR_zero_initialize_workgroup_memory` is promoted to 1.3
2000            if capabilities.device_api_version >= vk::API_VERSION_1_3
2001                || capabilities.supports_extension(khr::zero_initialize_workgroup_memory::NAME)
2002            {
2003                let next = features
2004                    .zero_initialize_workgroup_memory
2005                    .insert(vk::PhysicalDeviceZeroInitializeWorkgroupMemoryFeatures::default());
2006                features2 = features2.push_next(next);
2007            }
2008
2009            // `VK_EXT_subgroup_size_control` is promoted to 1.3
2010            if capabilities.device_api_version >= vk::API_VERSION_1_3
2011                || capabilities.supports_extension(ext::subgroup_size_control::NAME)
2012            {
2013                let next = features
2014                    .subgroup_size_control
2015                    .insert(vk::PhysicalDeviceSubgroupSizeControlFeatures::default());
2016                features2 = features2.push_next(next);
2017            }
2018
2019            if capabilities.supports_extension(ext::mesh_shader::NAME) {
2020                let next = features
2021                    .mesh_shader
2022                    .insert(vk::PhysicalDeviceMeshShaderFeaturesEXT::default());
2023                features2 = features2.push_next(next);
2024            }
2025
2026            // `VK_KHR_shader_integer_dot_product` is promoted to 1.3
2027            if capabilities.device_api_version >= vk::API_VERSION_1_3
2028                || capabilities.supports_extension(khr::shader_integer_dot_product::NAME)
2029            {
2030                let next = features
2031                    .shader_integer_dot_product
2032                    .insert(vk::PhysicalDeviceShaderIntegerDotProductFeatures::default());
2033                features2 = features2.push_next(next);
2034            }
2035
2036            if capabilities.supports_extension(khr::fragment_shader_barycentric::NAME) {
2037                let next = features
2038                    .shader_barycentrics
2039                    .insert(vk::PhysicalDeviceFragmentShaderBarycentricFeaturesKHR::default());
2040                features2 = features2.push_next(next);
2041            }
2042
2043            if capabilities.supports_extension(khr::portability_subset::NAME) {
2044                let next = features
2045                    .portability_subset
2046                    .insert(vk::PhysicalDevicePortabilitySubsetFeaturesKHR::default());
2047                features2 = features2.push_next(next);
2048            }
2049
2050            if capabilities.supports_extension(khr::cooperative_matrix::NAME) {
2051                let next = features
2052                    .cooperative_matrix
2053                    .insert(vk::PhysicalDeviceCooperativeMatrixFeaturesKHR::default());
2054                features2 = features2.push_next(next);
2055            }
2056
2057            if capabilities.device_api_version >= vk::API_VERSION_1_1 {
2058                let next = features
2059                    .shader_draw_parameters
2060                    .insert(vk::PhysicalDeviceShaderDrawParametersFeatures::default());
2061                features2 = features2.push_next(next);
2062            }
2063
2064            unsafe { get_device_properties.get_physical_device_features2(phd, &mut features2) };
2065            features2.features
2066        } else {
2067            unsafe { self.raw.get_physical_device_features(phd) }
2068        };
2069
2070        (capabilities, features)
2071    }
2072}
2073
2074impl super::Instance {
2075    pub fn expose_adapter(
2076        &self,
2077        phd: vk::PhysicalDevice,
2078    ) -> Option<crate::ExposedAdapter<super::Api>> {
2079        use crate::auxil::db;
2080
2081        let (phd_capabilities, phd_features) = self.shared.inspect(phd);
2082
2083        let mem_properties = {
2084            profiling::scope!("vkGetPhysicalDeviceMemoryProperties");
2085            unsafe { self.shared.raw.get_physical_device_memory_properties(phd) }
2086        };
2087        let memory_types = &mem_properties.memory_types_as_slice();
2088        let supports_lazily_allocated = memory_types.iter().any(|mem| {
2089            mem.property_flags
2090                .contains(vk::MemoryPropertyFlags::LAZILY_ALLOCATED)
2091        });
2092
2093        let info = wgt::AdapterInfo {
2094            name: {
2095                phd_capabilities
2096                    .properties
2097                    .device_name_as_c_str()
2098                    .ok()
2099                    .and_then(|name| name.to_str().ok())
2100                    .unwrap_or("?")
2101                    .to_owned()
2102            },
2103            vendor: phd_capabilities.properties.vendor_id,
2104            device: phd_capabilities.properties.device_id,
2105            device_type: match phd_capabilities.properties.device_type {
2106                vk::PhysicalDeviceType::OTHER => wgt::DeviceType::Other,
2107                vk::PhysicalDeviceType::INTEGRATED_GPU => wgt::DeviceType::IntegratedGpu,
2108                vk::PhysicalDeviceType::DISCRETE_GPU => wgt::DeviceType::DiscreteGpu,
2109                vk::PhysicalDeviceType::VIRTUAL_GPU => wgt::DeviceType::VirtualGpu,
2110                vk::PhysicalDeviceType::CPU => wgt::DeviceType::Cpu,
2111                _ => wgt::DeviceType::Other,
2112            },
2113            device_pci_bus_id: phd_capabilities
2114                .pci_bus_info
2115                .filter(|info| info.pci_bus != 0 || info.pci_device != 0)
2116                .map(|info| {
2117                    format!(
2118                        "{:04x}:{:02x}:{:02x}.{}",
2119                        info.pci_domain, info.pci_bus, info.pci_device, info.pci_function
2120                    )
2121                })
2122                .unwrap_or_default(),
2123            driver: {
2124                phd_capabilities
2125                    .driver
2126                    .as_ref()
2127                    .and_then(|driver| driver.driver_name_as_c_str().ok())
2128                    .and_then(|name| name.to_str().ok())
2129                    .unwrap_or("?")
2130                    .to_owned()
2131            },
2132            driver_info: {
2133                phd_capabilities
2134                    .driver
2135                    .as_ref()
2136                    .and_then(|driver| driver.driver_info_as_c_str().ok())
2137                    .and_then(|name| name.to_str().ok())
2138                    .unwrap_or("?")
2139                    .to_owned()
2140            },
2141            backend: wgt::Backend::Vulkan,
2142            subgroup_min_size: phd_capabilities
2143                .subgroup_size_control
2144                .map(|subgroup_size| subgroup_size.min_subgroup_size)
2145                .unwrap_or(wgt::MINIMUM_SUBGROUP_MIN_SIZE),
2146            subgroup_max_size: phd_capabilities
2147                .subgroup_size_control
2148                .map(|subgroup_size| subgroup_size.max_subgroup_size)
2149                .unwrap_or(wgt::MAXIMUM_SUBGROUP_MAX_SIZE),
2150            transient_saves_memory: supports_lazily_allocated,
2151        };
2152        let mut workarounds = super::Workarounds::empty();
2153        {
2154            // TODO: only enable for particular devices
2155            workarounds |= super::Workarounds::SEPARATE_ENTRY_POINTS;
2156            workarounds.set(
2157                super::Workarounds::EMPTY_RESOLVE_ATTACHMENT_LISTS,
2158                phd_capabilities.properties.vendor_id == db::qualcomm::VENDOR,
2159            );
2160            workarounds.set(
2161                super::Workarounds::FORCE_FILL_BUFFER_WITH_SIZE_GREATER_4096_ALIGNED_OFFSET_16,
2162                phd_capabilities.properties.vendor_id == db::nvidia::VENDOR,
2163            );
2164        };
2165
2166        if let Some(driver) = phd_capabilities.driver {
2167            if driver.conformance_version.major == 0 {
2168                if driver.driver_id == vk::DriverId::MOLTENVK {
2169                    log::debug!("Adapter is not Vulkan compliant, but is MoltenVK, continuing");
2170                } else if self
2171                    .shared
2172                    .flags
2173                    .contains(wgt::InstanceFlags::ALLOW_UNDERLYING_NONCOMPLIANT_ADAPTER)
2174                {
2175                    log::debug!("Adapter is not Vulkan compliant: {}", info.name);
2176                } else {
2177                    log::debug!(
2178                        "Adapter is not Vulkan compliant, hiding adapter: {}",
2179                        info.name
2180                    );
2181                    return None;
2182                }
2183            }
2184        }
2185        if phd_capabilities.device_api_version == vk::API_VERSION_1_0
2186            && !phd_capabilities.supports_extension(khr::storage_buffer_storage_class::NAME)
2187        {
2188            log::debug!(
2189                "SPIR-V storage buffer class is not supported, hiding adapter: {}",
2190                info.name
2191            );
2192            return None;
2193        }
2194        if !phd_capabilities.supports_extension(khr::maintenance1::NAME)
2195            && phd_capabilities.device_api_version < vk::API_VERSION_1_1
2196        {
2197            log::debug!(
2198                "VK_KHR_maintenance1 is not supported, hiding adapter: {}",
2199                info.name
2200            );
2201            return None;
2202        }
2203
2204        let queue_families = unsafe {
2205            self.shared
2206                .raw
2207                .get_physical_device_queue_family_properties(phd)
2208        };
2209        let queue_family_properties = queue_families.first()?;
2210        let queue_flags = queue_family_properties.queue_flags;
2211        if !queue_flags.contains(vk::QueueFlags::GRAPHICS) {
2212            log::debug!("The first queue only exposes {queue_flags:?}");
2213            return None;
2214        }
2215
2216        let (available_features, mut downlevel_flags) = phd_features.to_wgpu(
2217            &self.shared.raw,
2218            phd,
2219            &phd_capabilities,
2220            queue_family_properties,
2221        );
2222
2223        if info.driver == "llvmpipe" {
2224            // The `F16_IN_F32` instructions do not normally require native `F16` support, but on
2225            // llvmpipe, they do.
2226            downlevel_flags.set(
2227                wgt::DownlevelFlags::SHADER_F16_IN_F32,
2228                available_features.contains(wgt::Features::SHADER_F16),
2229            );
2230        }
2231
2232        let has_robust_buffer_access2 = phd_features
2233            .robustness2
2234            .as_ref()
2235            .map(|r| r.robust_buffer_access2 == 1)
2236            .unwrap_or_default();
2237
2238        let alignments = phd_capabilities.to_hal_alignments(has_robust_buffer_access2);
2239
2240        let private_caps = super::PrivateCapabilities {
2241            image_view_usage: phd_capabilities.device_api_version >= vk::API_VERSION_1_1
2242                || phd_capabilities.supports_extension(khr::maintenance2::NAME),
2243            timeline_semaphores: match phd_features.timeline_semaphore {
2244                Some(features) => features.timeline_semaphore == vk::TRUE,
2245                None => phd_features
2246                    .timeline_semaphore
2247                    .is_some_and(|ext| ext.timeline_semaphore != 0),
2248            },
2249            texture_d24: supports_format(
2250                &self.shared.raw,
2251                phd,
2252                vk::Format::X8_D24_UNORM_PACK32,
2253                vk::ImageTiling::OPTIMAL,
2254                depth_stencil_required_flags(),
2255            ),
2256            texture_d24_s8: supports_format(
2257                &self.shared.raw,
2258                phd,
2259                vk::Format::D24_UNORM_S8_UINT,
2260                vk::ImageTiling::OPTIMAL,
2261                depth_stencil_required_flags(),
2262            ),
2263            texture_s8: supports_format(
2264                &self.shared.raw,
2265                phd,
2266                vk::Format::S8_UINT,
2267                vk::ImageTiling::OPTIMAL,
2268                depth_stencil_required_flags(),
2269            ),
2270            multi_draw_indirect: phd_features.core.multi_draw_indirect != 0,
2271            max_draw_indirect_count: phd_capabilities.properties.limits.max_draw_indirect_count,
2272            non_coherent_map_mask: phd_capabilities.properties.limits.non_coherent_atom_size - 1,
2273            can_present: true,
2274            //TODO: make configurable
2275            robust_buffer_access: phd_features.core.robust_buffer_access != 0,
2276            robust_image_access: match phd_features.robustness2 {
2277                Some(ref f) => f.robust_image_access2 != 0,
2278                None => phd_features
2279                    .image_robustness
2280                    .is_some_and(|ext| ext.robust_image_access != 0),
2281            },
2282            robust_buffer_access2: has_robust_buffer_access2,
2283            robust_image_access2: phd_features
2284                .robustness2
2285                .as_ref()
2286                .map(|r| r.robust_image_access2 == 1)
2287                .unwrap_or_default(),
2288            zero_initialize_workgroup_memory: phd_features
2289                .zero_initialize_workgroup_memory
2290                .is_some_and(|ext| ext.shader_zero_initialize_workgroup_memory == vk::TRUE),
2291            image_format_list: phd_capabilities.device_api_version >= vk::API_VERSION_1_2
2292                || phd_capabilities.supports_extension(khr::image_format_list::NAME),
2293            maximum_samplers: phd_capabilities
2294                .properties
2295                .limits
2296                .max_sampler_allocation_count,
2297            shader_integer_dot_product: phd_features
2298                .shader_integer_dot_product
2299                .is_some_and(|ext| ext.shader_integer_dot_product != 0),
2300            shader_int8: phd_features
2301                .shader_float16_int8
2302                .is_some_and(|features| features.shader_int8 != 0),
2303            multiview_instance_index_limit: phd_capabilities
2304                .multiview
2305                .map(|a| a.max_multiview_instance_index)
2306                .unwrap_or(0),
2307            scratch_buffer_alignment: alignments.ray_tracing_scratch_buffer_alignment,
2308        };
2309        let capabilities = crate::Capabilities {
2310            limits: phd_capabilities.to_wgpu_limits(),
2311            alignments,
2312            downlevel: wgt::DownlevelCapabilities {
2313                flags: downlevel_flags,
2314                limits: wgt::DownlevelLimits {},
2315                shader_model: wgt::ShaderModel::Sm5, //TODO?
2316            },
2317            cooperative_matrix_properties: phd_capabilities.cooperative_matrix_properties.clone(),
2318        };
2319
2320        let adapter = super::Adapter {
2321            raw: phd,
2322            instance: Arc::clone(&self.shared),
2323            //queue_families,
2324            known_memory_flags: vk::MemoryPropertyFlags::DEVICE_LOCAL
2325                | vk::MemoryPropertyFlags::HOST_VISIBLE
2326                | vk::MemoryPropertyFlags::HOST_COHERENT
2327                | vk::MemoryPropertyFlags::HOST_CACHED
2328                | vk::MemoryPropertyFlags::LAZILY_ALLOCATED,
2329            phd_capabilities,
2330            phd_features,
2331            downlevel_flags,
2332            private_caps,
2333            workarounds,
2334        };
2335
2336        Some(crate::ExposedAdapter {
2337            adapter,
2338            info,
2339            features: available_features,
2340            capabilities,
2341        })
2342    }
2343}
2344
2345impl super::Adapter {
2346    pub fn raw_physical_device(&self) -> vk::PhysicalDevice {
2347        self.raw
2348    }
2349
2350    pub fn get_physical_device_features(&self) -> &PhysicalDeviceFeatures {
2351        &self.phd_features
2352    }
2353
2354    pub fn physical_device_capabilities(&self) -> &PhysicalDeviceProperties {
2355        &self.phd_capabilities
2356    }
2357
2358    pub fn shared_instance(&self) -> &super::InstanceShared {
2359        &self.instance
2360    }
2361
2362    pub fn required_device_extensions(&self, features: wgt::Features) -> Vec<&'static CStr> {
2363        let (supported_extensions, unsupported_extensions) = self
2364            .phd_capabilities
2365            .get_required_extensions(features)
2366            .iter()
2367            .partition::<Vec<&CStr>, _>(|&&extension| {
2368                self.phd_capabilities.supports_extension(extension)
2369            });
2370
2371        if !unsupported_extensions.is_empty() {
2372            log::debug!("Missing extensions: {unsupported_extensions:?}");
2373        }
2374
2375        log::debug!("Supported extensions: {supported_extensions:?}");
2376        supported_extensions
2377    }
2378
2379    /// Create a `PhysicalDeviceFeatures` for opening a logical device with
2380    /// `features` from this adapter.
2381    ///
2382    /// The given `enabled_extensions` set must include all the extensions
2383    /// selected by [`required_device_extensions`] when passed `features`.
2384    /// Otherwise, the `PhysicalDeviceFeatures` value may not be able to select
2385    /// all the Vulkan features needed to represent `features` and this
2386    /// adapter's characteristics.
2387    ///
2388    /// Typically, you'd simply call `required_device_extensions`, and then pass
2389    /// its return value and the feature set you gave it directly to this
2390    /// function. But it's fine to add more extensions to the list.
2391    ///
2392    /// [`required_device_extensions`]: Self::required_device_extensions
2393    pub fn physical_device_features(
2394        &self,
2395        enabled_extensions: &[&'static CStr],
2396        features: wgt::Features,
2397    ) -> PhysicalDeviceFeatures {
2398        PhysicalDeviceFeatures::from_extensions_and_requested_features(
2399            &self.phd_capabilities,
2400            &self.phd_features,
2401            enabled_extensions,
2402            features,
2403            self.downlevel_flags,
2404            &self.private_caps,
2405        )
2406    }
2407
2408    /// # Safety
2409    ///
2410    /// - `raw_device` must be created from this adapter.
2411    /// - `raw_device` must be created using `family_index`, `enabled_extensions` and `physical_device_features()`
2412    /// - `enabled_extensions` must be a superset of `required_device_extensions()`.
2413    /// - If `drop_callback` is [`None`], wgpu-hal will take ownership of `raw_device`. If
2414    ///   `drop_callback` is [`Some`], `raw_device` must be valid until the callback is called.
2415    #[allow(clippy::too_many_arguments)]
2416    pub unsafe fn device_from_raw(
2417        &self,
2418        raw_device: ash::Device,
2419        drop_callback: Option<crate::DropCallback>,
2420        enabled_extensions: &[&'static CStr],
2421        features: wgt::Features,
2422        limits: &wgt::Limits,
2423        memory_hints: &wgt::MemoryHints,
2424        family_index: u32,
2425        queue_index: u32,
2426    ) -> Result<crate::OpenDevice<super::Api>, crate::DeviceError> {
2427        let mem_properties = {
2428            profiling::scope!("vkGetPhysicalDeviceMemoryProperties");
2429            unsafe {
2430                self.instance
2431                    .raw
2432                    .get_physical_device_memory_properties(self.raw)
2433            }
2434        };
2435        let memory_types = &mem_properties.memory_types_as_slice();
2436        let valid_ash_memory_types = memory_types.iter().enumerate().fold(0, |u, (i, mem)| {
2437            if self.known_memory_flags.contains(mem.property_flags) {
2438                u | (1 << i)
2439            } else {
2440                u
2441            }
2442        });
2443
2444        // Note that VK_EXT_debug_utils is an instance extension (enabled at the instance
2445        // level) but contains a few functions that can be loaded directly on the Device for a
2446        // dispatch-table-less pointer.
2447        let debug_utils_fn = if self.instance.extensions.contains(&ext::debug_utils::NAME) {
2448            Some(ext::debug_utils::Device::new(
2449                &self.instance.raw,
2450                &raw_device,
2451            ))
2452        } else {
2453            None
2454        };
2455        let indirect_count_fn = if enabled_extensions.contains(&khr::draw_indirect_count::NAME) {
2456            Some(khr::draw_indirect_count::Device::new(
2457                &self.instance.raw,
2458                &raw_device,
2459            ))
2460        } else {
2461            None
2462        };
2463        let timeline_semaphore_fn = if enabled_extensions.contains(&khr::timeline_semaphore::NAME) {
2464            Some(super::ExtensionFn::Extension(
2465                khr::timeline_semaphore::Device::new(&self.instance.raw, &raw_device),
2466            ))
2467        } else if self.phd_capabilities.device_api_version >= vk::API_VERSION_1_2 {
2468            Some(super::ExtensionFn::Promoted)
2469        } else {
2470            None
2471        };
2472        let ray_tracing_fns = if enabled_extensions.contains(&khr::acceleration_structure::NAME)
2473            && enabled_extensions.contains(&khr::buffer_device_address::NAME)
2474        {
2475            Some(super::RayTracingDeviceExtensionFunctions {
2476                acceleration_structure: khr::acceleration_structure::Device::new(
2477                    &self.instance.raw,
2478                    &raw_device,
2479                ),
2480                buffer_device_address: khr::buffer_device_address::Device::new(
2481                    &self.instance.raw,
2482                    &raw_device,
2483                ),
2484            })
2485        } else {
2486            None
2487        };
2488        let mesh_shading_fns = if enabled_extensions.contains(&ext::mesh_shader::NAME) {
2489            Some(ext::mesh_shader::Device::new(
2490                &self.instance.raw,
2491                &raw_device,
2492            ))
2493        } else {
2494            None
2495        };
2496
2497        let naga_options = {
2498            use naga::back::spv;
2499
2500            // The following capabilities are always available
2501            // see https://registry.khronos.org/vulkan/specs/1.3-extensions/html/chap52.html#spirvenv-capabilities
2502            let mut capabilities = vec![
2503                spv::Capability::Shader,
2504                spv::Capability::Matrix,
2505                spv::Capability::Sampled1D,
2506                spv::Capability::Image1D,
2507                spv::Capability::ImageQuery,
2508                spv::Capability::DerivativeControl,
2509                spv::Capability::StorageImageExtendedFormats,
2510            ];
2511
2512            if self
2513                .downlevel_flags
2514                .contains(wgt::DownlevelFlags::CUBE_ARRAY_TEXTURES)
2515            {
2516                capabilities.push(spv::Capability::SampledCubeArray);
2517            }
2518
2519            if self
2520                .downlevel_flags
2521                .contains(wgt::DownlevelFlags::MULTISAMPLED_SHADING)
2522            {
2523                capabilities.push(spv::Capability::SampleRateShading);
2524            }
2525
2526            if features.contains(wgt::Features::MULTIVIEW) {
2527                capabilities.push(spv::Capability::MultiView);
2528            }
2529
2530            if features.contains(wgt::Features::PRIMITIVE_INDEX) {
2531                capabilities.push(spv::Capability::Geometry);
2532            }
2533
2534            if features.intersects(wgt::Features::SUBGROUP | wgt::Features::SUBGROUP_VERTEX) {
2535                capabilities.push(spv::Capability::GroupNonUniform);
2536                capabilities.push(spv::Capability::GroupNonUniformVote);
2537                capabilities.push(spv::Capability::GroupNonUniformArithmetic);
2538                capabilities.push(spv::Capability::GroupNonUniformBallot);
2539                capabilities.push(spv::Capability::GroupNonUniformShuffle);
2540                capabilities.push(spv::Capability::GroupNonUniformShuffleRelative);
2541                capabilities.push(spv::Capability::GroupNonUniformQuad);
2542            }
2543
2544            if features.intersects(
2545                wgt::Features::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING
2546                    | wgt::Features::STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING
2547                    | wgt::Features::UNIFORM_BUFFER_BINDING_ARRAYS,
2548            ) {
2549                capabilities.push(spv::Capability::ShaderNonUniform);
2550            }
2551            if features.contains(wgt::Features::BGRA8UNORM_STORAGE) {
2552                capabilities.push(spv::Capability::StorageImageWriteWithoutFormat);
2553            }
2554
2555            if features.contains(wgt::Features::EXPERIMENTAL_RAY_QUERY) {
2556                capabilities.push(spv::Capability::RayQueryKHR);
2557            }
2558
2559            if features.contains(wgt::Features::SHADER_INT64) {
2560                capabilities.push(spv::Capability::Int64);
2561            }
2562
2563            if features.contains(wgt::Features::SHADER_F16) {
2564                capabilities.push(spv::Capability::Float16);
2565            }
2566
2567            if features.intersects(
2568                wgt::Features::SHADER_INT64_ATOMIC_ALL_OPS
2569                    | wgt::Features::SHADER_INT64_ATOMIC_MIN_MAX
2570                    | wgt::Features::TEXTURE_INT64_ATOMIC,
2571            ) {
2572                capabilities.push(spv::Capability::Int64Atomics);
2573            }
2574
2575            if features.intersects(wgt::Features::TEXTURE_INT64_ATOMIC) {
2576                capabilities.push(spv::Capability::Int64ImageEXT);
2577            }
2578
2579            if features.contains(wgt::Features::SHADER_FLOAT32_ATOMIC) {
2580                capabilities.push(spv::Capability::AtomicFloat32AddEXT);
2581            }
2582
2583            if features.contains(wgt::Features::CLIP_DISTANCES) {
2584                capabilities.push(spv::Capability::ClipDistance);
2585            }
2586
2587            // Vulkan bundles both barycentrics and per-vertex attributes under the same feature.
2588            if features
2589                .intersects(wgt::Features::SHADER_BARYCENTRICS | wgt::Features::SHADER_PER_VERTEX)
2590            {
2591                capabilities.push(spv::Capability::FragmentBarycentricKHR);
2592            }
2593
2594            if features.contains(wgt::Features::SHADER_DRAW_INDEX) {
2595                capabilities.push(spv::Capability::DrawParameters);
2596            }
2597
2598            let mut flags = spv::WriterFlags::empty();
2599            flags.set(
2600                spv::WriterFlags::DEBUG,
2601                self.instance.flags.contains(wgt::InstanceFlags::DEBUG),
2602            );
2603            flags.set(
2604                spv::WriterFlags::LABEL_VARYINGS,
2605                self.phd_capabilities.properties.vendor_id != crate::auxil::db::qualcomm::VENDOR,
2606            );
2607            flags.set(
2608                spv::WriterFlags::FORCE_POINT_SIZE,
2609                //Note: we could technically disable this when we are compiling separate entry points,
2610                // and we know exactly that the primitive topology is not `PointList`.
2611                // But this requires cloning the `spv::Options` struct, which has heap allocations.
2612                true, // could check `super::Workarounds::SEPARATE_ENTRY_POINTS`
2613            );
2614            flags.set(
2615                spv::WriterFlags::PRINT_ON_RAY_QUERY_INITIALIZATION_FAIL,
2616                self.instance.flags.contains(wgt::InstanceFlags::DEBUG)
2617                    && (self.instance.instance_api_version >= vk::API_VERSION_1_3
2618                        || enabled_extensions.contains(&khr::shader_non_semantic_info::NAME)),
2619            );
2620            if features.contains(wgt::Features::EXPERIMENTAL_RAY_QUERY) {
2621                capabilities.push(spv::Capability::RayQueryKHR);
2622            }
2623            if features.contains(wgt::Features::EXPERIMENTAL_RAY_HIT_VERTEX_RETURN) {
2624                capabilities.push(spv::Capability::RayQueryPositionFetchKHR)
2625            }
2626            if features.contains(wgt::Features::EXPERIMENTAL_MESH_SHADER) {
2627                capabilities.push(spv::Capability::MeshShadingEXT);
2628            }
2629            if features.contains(wgt::Features::EXPERIMENTAL_COOPERATIVE_MATRIX) {
2630                capabilities.push(spv::Capability::CooperativeMatrixKHR);
2631                // TODO: expose this more generally
2632                capabilities.push(spv::Capability::VulkanMemoryModel);
2633            }
2634            if self.private_caps.shader_integer_dot_product {
2635                // See <https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_KHR_shader_integer_dot_product.html#_new_spir_v_capabilities>.
2636                capabilities.extend(&[
2637                    spv::Capability::DotProductInputAllKHR,
2638                    spv::Capability::DotProductInput4x8BitKHR,
2639                    spv::Capability::DotProductInput4x8BitPackedKHR,
2640                    spv::Capability::DotProductKHR,
2641                ]);
2642            }
2643            if self.private_caps.shader_int8 {
2644                // See <https://registry.khronos.org/vulkan/specs/latest/man/html/VkPhysicalDeviceShaderFloat16Int8Features.html#extension-features-shaderInt8>.
2645                capabilities.extend(&[spv::Capability::Int8]);
2646            }
2647            spv::Options {
2648                lang_version: match self.phd_capabilities.device_api_version {
2649                    // Use maximum supported SPIR-V version according to
2650                    // <https://github.com/KhronosGroup/Vulkan-Docs/blob/19b7651/appendices/spirvenv.adoc?plain=1#L21-L40>.
2651                    vk::API_VERSION_1_0..vk::API_VERSION_1_1 => (1, 0),
2652                    vk::API_VERSION_1_1..vk::API_VERSION_1_2 => (1, 3),
2653                    vk::API_VERSION_1_2..vk::API_VERSION_1_3 => (1, 5),
2654                    vk::API_VERSION_1_3.. => (1, 6),
2655                    _ => unreachable!(),
2656                },
2657                flags,
2658                capabilities: Some(capabilities.iter().cloned().collect()),
2659                bounds_check_policies: naga::proc::BoundsCheckPolicies {
2660                    index: naga::proc::BoundsCheckPolicy::Restrict,
2661                    buffer: if self.private_caps.robust_buffer_access2 {
2662                        naga::proc::BoundsCheckPolicy::Unchecked
2663                    } else {
2664                        naga::proc::BoundsCheckPolicy::Restrict
2665                    },
2666                    image_load: if self.private_caps.robust_image_access {
2667                        naga::proc::BoundsCheckPolicy::Unchecked
2668                    } else {
2669                        naga::proc::BoundsCheckPolicy::Restrict
2670                    },
2671                    // TODO: support bounds checks on binding arrays
2672                    binding_array: naga::proc::BoundsCheckPolicy::Unchecked,
2673                },
2674                zero_initialize_workgroup_memory: if self
2675                    .private_caps
2676                    .zero_initialize_workgroup_memory
2677                {
2678                    spv::ZeroInitializeWorkgroupMemoryMode::Native
2679                } else {
2680                    spv::ZeroInitializeWorkgroupMemoryMode::Polyfill
2681                },
2682                force_loop_bounding: true,
2683                ray_query_initialization_tracking: true,
2684                use_storage_input_output_16: features.contains(wgt::Features::SHADER_F16)
2685                    && self.phd_features.supports_storage_input_output_16(),
2686                fake_missing_bindings: false,
2687                // We need to build this separately for each invocation, so just default it out here
2688                binding_map: BTreeMap::default(),
2689                debug_info: None,
2690                task_dispatch_limits: Some(naga::back::TaskDispatchLimits {
2691                    max_mesh_workgroups_per_dim: limits.max_task_mesh_workgroups_per_dimension,
2692                    max_mesh_workgroups_total: limits.max_task_mesh_workgroup_total_count,
2693                }),
2694                mesh_shader_primitive_indices_clamp: true,
2695            }
2696        };
2697
2698        let raw_queue = {
2699            profiling::scope!("vkGetDeviceQueue");
2700            unsafe { raw_device.get_device_queue(family_index, queue_index) }
2701        };
2702
2703        let driver_version = self
2704            .phd_capabilities
2705            .properties
2706            .driver_version
2707            .to_be_bytes();
2708        #[rustfmt::skip]
2709        let pipeline_cache_validation_key = [
2710            driver_version[0], driver_version[1], driver_version[2], driver_version[3],
2711            0, 0, 0, 0,
2712            0, 0, 0, 0,
2713            0, 0, 0, 0,
2714        ];
2715
2716        let drop_guard = crate::DropGuard::from_option(drop_callback);
2717
2718        let empty_descriptor_set_layout = unsafe {
2719            raw_device
2720                .create_descriptor_set_layout(&vk::DescriptorSetLayoutCreateInfo::default(), None)
2721                .map_err(super::map_host_device_oom_err)?
2722        };
2723
2724        let shared = Arc::new(super::DeviceShared {
2725            raw: raw_device,
2726            family_index,
2727            queue_index,
2728            raw_queue,
2729            drop_guard,
2730            instance: Arc::clone(&self.instance),
2731            physical_device: self.raw,
2732            enabled_extensions: enabled_extensions.into(),
2733            extension_fns: super::DeviceExtensionFunctions {
2734                debug_utils: debug_utils_fn,
2735                draw_indirect_count: indirect_count_fn,
2736                timeline_semaphore: timeline_semaphore_fn,
2737                ray_tracing: ray_tracing_fns,
2738                mesh_shading: mesh_shading_fns,
2739            },
2740            pipeline_cache_validation_key,
2741            vendor_id: self.phd_capabilities.properties.vendor_id,
2742            timestamp_period: self.phd_capabilities.properties.limits.timestamp_period,
2743            private_caps: self.private_caps.clone(),
2744            features,
2745            workarounds: self.workarounds,
2746            render_passes: Mutex::new(Default::default()),
2747            sampler_cache: Mutex::new(super::sampler::SamplerCache::new(
2748                self.private_caps.maximum_samplers,
2749            )),
2750            memory_allocations_counter: Default::default(),
2751
2752            texture_identity_factory: super::ResourceIdentityFactory::new(),
2753            texture_view_identity_factory: super::ResourceIdentityFactory::new(),
2754            empty_descriptor_set_layout,
2755        });
2756
2757        let relay_semaphores = super::RelaySemaphores::new(&shared)?;
2758
2759        let queue = super::Queue {
2760            raw: raw_queue,
2761            device: Arc::clone(&shared),
2762            family_index,
2763            relay_semaphores: Mutex::new(relay_semaphores),
2764            signal_semaphores: Mutex::new(SemaphoreList::new(SemaphoreListMode::Signal)),
2765        };
2766
2767        let allocation_sizes = AllocationSizes::from_memory_hints(memory_hints).into();
2768
2769        let buffer_device_address = enabled_extensions.contains(&khr::buffer_device_address::NAME);
2770
2771        let mem_allocator =
2772            gpu_allocator::vulkan::Allocator::new(&gpu_allocator::vulkan::AllocatorCreateDesc {
2773                instance: self.instance.raw.clone(),
2774                device: shared.raw.clone(),
2775                physical_device: self.raw,
2776                debug_settings: Default::default(),
2777                buffer_device_address,
2778                allocation_sizes,
2779            })?;
2780
2781        let desc_allocator = gpu_descriptor::DescriptorAllocator::new(
2782            if let Some(di) = self.phd_capabilities.descriptor_indexing {
2783                di.max_update_after_bind_descriptors_in_all_pools
2784            } else {
2785                0
2786            },
2787        );
2788
2789        let device = super::Device {
2790            shared,
2791            mem_allocator: Mutex::new(mem_allocator),
2792            desc_allocator: Mutex::new(desc_allocator),
2793            valid_ash_memory_types,
2794            naga_options,
2795            #[cfg(feature = "renderdoc")]
2796            render_doc: Default::default(),
2797            counters: Default::default(),
2798        };
2799
2800        Ok(crate::OpenDevice { device, queue })
2801    }
2802
2803    pub fn texture_format_as_raw(&self, texture_format: wgt::TextureFormat) -> vk::Format {
2804        self.private_caps.map_texture_format(texture_format)
2805    }
2806
2807    /// # Safety:
2808    /// - Same as `open` plus
2809    /// - The callback may not change anything that the device does not support.
2810    /// - The callback may not remove features.
2811    pub unsafe fn open_with_callback<'a>(
2812        &self,
2813        features: wgt::Features,
2814        limits: &wgt::Limits,
2815        memory_hints: &wgt::MemoryHints,
2816        callback: Option<Box<super::CreateDeviceCallback<'a>>>,
2817    ) -> Result<crate::OpenDevice<super::Api>, crate::DeviceError> {
2818        let mut enabled_extensions = self.required_device_extensions(features);
2819        let mut enabled_phd_features = self.physical_device_features(&enabled_extensions, features);
2820
2821        let family_index = 0; //TODO
2822        let family_info = vk::DeviceQueueCreateInfo::default()
2823            .queue_family_index(family_index)
2824            .queue_priorities(&[1.0]);
2825        let mut family_infos = Vec::from([family_info]);
2826
2827        let mut pre_info = vk::DeviceCreateInfo::default();
2828
2829        if let Some(callback) = callback {
2830            callback(super::CreateDeviceCallbackArgs {
2831                extensions: &mut enabled_extensions,
2832                device_features: &mut enabled_phd_features,
2833                queue_create_infos: &mut family_infos,
2834                create_info: &mut pre_info,
2835                _phantom: PhantomData,
2836            })
2837        }
2838
2839        let str_pointers = enabled_extensions
2840            .iter()
2841            .map(|&s| {
2842                // Safe because `enabled_extensions` entries have static lifetime.
2843                s.as_ptr()
2844            })
2845            .collect::<Vec<_>>();
2846
2847        let pre_info = pre_info
2848            .queue_create_infos(&family_infos)
2849            .enabled_extension_names(&str_pointers);
2850        let info = enabled_phd_features.add_to_device_create(pre_info);
2851        let raw_device = {
2852            profiling::scope!("vkCreateDevice");
2853            unsafe {
2854                self.instance
2855                    .raw
2856                    .create_device(self.raw, &info, None)
2857                    .map_err(map_err)?
2858            }
2859        };
2860        fn map_err(err: vk::Result) -> crate::DeviceError {
2861            match err {
2862                vk::Result::ERROR_TOO_MANY_OBJECTS => crate::DeviceError::OutOfMemory,
2863                vk::Result::ERROR_INITIALIZATION_FAILED => crate::DeviceError::Lost,
2864                vk::Result::ERROR_EXTENSION_NOT_PRESENT | vk::Result::ERROR_FEATURE_NOT_PRESENT => {
2865                    crate::hal_usage_error(err)
2866                }
2867                other => super::map_host_device_oom_and_lost_err(other),
2868            }
2869        }
2870
2871        unsafe {
2872            self.device_from_raw(
2873                raw_device,
2874                None,
2875                &enabled_extensions,
2876                features,
2877                limits,
2878                memory_hints,
2879                family_info.queue_family_index,
2880                0,
2881            )
2882        }
2883    }
2884}
2885
2886impl crate::Adapter for super::Adapter {
2887    type A = super::Api;
2888
2889    unsafe fn open(
2890        &self,
2891        features: wgt::Features,
2892        limits: &wgt::Limits,
2893        memory_hints: &wgt::MemoryHints,
2894    ) -> Result<crate::OpenDevice<super::Api>, crate::DeviceError> {
2895        unsafe { self.open_with_callback(features, limits, memory_hints, None) }
2896    }
2897
2898    unsafe fn texture_format_capabilities(
2899        &self,
2900        format: wgt::TextureFormat,
2901    ) -> crate::TextureFormatCapabilities {
2902        use crate::TextureFormatCapabilities as Tfc;
2903
2904        let vk_format = self.private_caps.map_texture_format(format);
2905        let properties = unsafe {
2906            self.instance
2907                .raw
2908                .get_physical_device_format_properties(self.raw, vk_format)
2909        };
2910        let features = properties.optimal_tiling_features;
2911
2912        let mut flags = Tfc::empty();
2913        flags.set(
2914            Tfc::SAMPLED,
2915            features.contains(vk::FormatFeatureFlags::SAMPLED_IMAGE),
2916        );
2917        flags.set(
2918            Tfc::SAMPLED_LINEAR,
2919            features.contains(vk::FormatFeatureFlags::SAMPLED_IMAGE_FILTER_LINEAR),
2920        );
2921        // flags.set(
2922        //     Tfc::SAMPLED_MINMAX,
2923        //     features.contains(vk::FormatFeatureFlags::SAMPLED_IMAGE_FILTER_MINMAX),
2924        // );
2925        flags.set(
2926            Tfc::STORAGE_READ_WRITE
2927                | Tfc::STORAGE_WRITE_ONLY
2928                | Tfc::STORAGE_READ_ONLY
2929                | Tfc::STORAGE_ATOMIC,
2930            features.contains(vk::FormatFeatureFlags::STORAGE_IMAGE),
2931        );
2932        flags.set(
2933            Tfc::STORAGE_ATOMIC,
2934            features.contains(vk::FormatFeatureFlags::STORAGE_IMAGE_ATOMIC),
2935        );
2936        flags.set(
2937            Tfc::COLOR_ATTACHMENT,
2938            features.contains(vk::FormatFeatureFlags::COLOR_ATTACHMENT),
2939        );
2940        flags.set(
2941            Tfc::COLOR_ATTACHMENT_BLEND,
2942            features.contains(vk::FormatFeatureFlags::COLOR_ATTACHMENT_BLEND),
2943        );
2944        flags.set(
2945            Tfc::DEPTH_STENCIL_ATTACHMENT,
2946            features.contains(vk::FormatFeatureFlags::DEPTH_STENCIL_ATTACHMENT),
2947        );
2948        flags.set(
2949            Tfc::COPY_SRC,
2950            features.intersects(vk::FormatFeatureFlags::TRANSFER_SRC),
2951        );
2952        flags.set(
2953            Tfc::COPY_DST,
2954            features.intersects(vk::FormatFeatureFlags::TRANSFER_DST),
2955        );
2956        flags.set(
2957            Tfc::STORAGE_ATOMIC,
2958            features.intersects(vk::FormatFeatureFlags::STORAGE_IMAGE_ATOMIC),
2959        );
2960        // Vulkan is very permissive about MSAA
2961        flags.set(Tfc::MULTISAMPLE_RESOLVE, !format.is_compressed());
2962
2963        // get the supported sample counts
2964        let format_aspect = crate::FormatAspects::from(format);
2965        let limits = self.phd_capabilities.properties.limits;
2966
2967        let sample_flags = if format_aspect.contains(crate::FormatAspects::DEPTH) {
2968            limits
2969                .framebuffer_depth_sample_counts
2970                .min(limits.sampled_image_depth_sample_counts)
2971        } else if format_aspect.contains(crate::FormatAspects::STENCIL) {
2972            limits
2973                .framebuffer_stencil_sample_counts
2974                .min(limits.sampled_image_stencil_sample_counts)
2975        } else {
2976            let first_aspect = format_aspect
2977                .iter()
2978                .next()
2979                .expect("All texture should at least one aspect")
2980                .map();
2981
2982            // We should never get depth or stencil out of this, due to the above.
2983            assert_ne!(first_aspect, wgt::TextureAspect::DepthOnly);
2984            assert_ne!(first_aspect, wgt::TextureAspect::StencilOnly);
2985
2986            match format.sample_type(Some(first_aspect), None).unwrap() {
2987                wgt::TextureSampleType::Float { .. } => limits
2988                    .framebuffer_color_sample_counts
2989                    .min(limits.sampled_image_color_sample_counts),
2990                wgt::TextureSampleType::Sint | wgt::TextureSampleType::Uint => {
2991                    limits.sampled_image_integer_sample_counts
2992                }
2993                _ => unreachable!(),
2994            }
2995        };
2996
2997        flags.set(
2998            Tfc::MULTISAMPLE_X2,
2999            sample_flags.contains(vk::SampleCountFlags::TYPE_2),
3000        );
3001        flags.set(
3002            Tfc::MULTISAMPLE_X4,
3003            sample_flags.contains(vk::SampleCountFlags::TYPE_4),
3004        );
3005        flags.set(
3006            Tfc::MULTISAMPLE_X8,
3007            sample_flags.contains(vk::SampleCountFlags::TYPE_8),
3008        );
3009        flags.set(
3010            Tfc::MULTISAMPLE_X16,
3011            sample_flags.contains(vk::SampleCountFlags::TYPE_16),
3012        );
3013
3014        flags
3015    }
3016
3017    unsafe fn surface_capabilities(
3018        &self,
3019        surface: &super::Surface,
3020    ) -> Option<crate::SurfaceCapabilities> {
3021        surface.inner.surface_capabilities(self)
3022    }
3023
3024    unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp {
3025        // VK_GOOGLE_display_timing is the only way to get presentation
3026        // timestamps on vulkan right now and it is only ever available
3027        // on android and linux. This includes mac, but there's no alternative
3028        // on mac, so this is fine.
3029        #[cfg(unix)]
3030        {
3031            let mut timespec = libc::timespec {
3032                tv_sec: 0,
3033                tv_nsec: 0,
3034            };
3035            unsafe {
3036                libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut timespec);
3037            }
3038
3039            wgt::PresentationTimestamp(
3040                timespec.tv_sec as u128 * 1_000_000_000 + timespec.tv_nsec as u128,
3041            )
3042        }
3043        #[cfg(not(unix))]
3044        {
3045            wgt::PresentationTimestamp::INVALID_TIMESTAMP
3046        }
3047    }
3048
3049    fn get_ordered_buffer_usages(&self) -> wgt::BufferUses {
3050        wgt::BufferUses::INCLUSIVE | wgt::BufferUses::MAP_WRITE
3051    }
3052
3053    // Vulkan makes very few execution ordering guarantees
3054    // see https://registry.khronos.org/vulkan/specs/latest/html/vkspec.html#synchronization-implicit
3055    // We just don't want to insert barriers between inclusive uses
3056    // See https://github.com/gfx-rs/wgpu/issues/8853
3057    fn get_ordered_texture_usages(&self) -> wgt::TextureUses {
3058        wgt::TextureUses::INCLUSIVE
3059    }
3060}
3061
3062fn is_format_16bit_norm_supported(instance: &ash::Instance, phd: vk::PhysicalDevice) -> bool {
3063    [
3064        vk::Format::R16_UNORM,
3065        vk::Format::R16_SNORM,
3066        vk::Format::R16G16_UNORM,
3067        vk::Format::R16G16_SNORM,
3068        vk::Format::R16G16B16A16_UNORM,
3069        vk::Format::R16G16B16A16_SNORM,
3070    ]
3071    .into_iter()
3072    .all(|format| {
3073        supports_format(
3074            instance,
3075            phd,
3076            format,
3077            vk::ImageTiling::OPTIMAL,
3078            vk::FormatFeatureFlags::SAMPLED_IMAGE
3079                | vk::FormatFeatureFlags::STORAGE_IMAGE
3080                | vk::FormatFeatureFlags::TRANSFER_SRC
3081                | vk::FormatFeatureFlags::TRANSFER_DST,
3082        )
3083    })
3084}
3085
3086fn is_float32_filterable_supported(instance: &ash::Instance, phd: vk::PhysicalDevice) -> bool {
3087    [
3088        vk::Format::R32_SFLOAT,
3089        vk::Format::R32G32_SFLOAT,
3090        vk::Format::R32G32B32A32_SFLOAT,
3091    ]
3092    .into_iter()
3093    .all(|format| {
3094        supports_format(
3095            instance,
3096            phd,
3097            format,
3098            vk::ImageTiling::OPTIMAL,
3099            vk::FormatFeatureFlags::SAMPLED_IMAGE_FILTER_LINEAR,
3100        )
3101    })
3102}
3103
3104fn is_float32_blendable_supported(instance: &ash::Instance, phd: vk::PhysicalDevice) -> bool {
3105    [
3106        vk::Format::R32_SFLOAT,
3107        vk::Format::R32G32_SFLOAT,
3108        vk::Format::R32G32B32A32_SFLOAT,
3109    ]
3110    .into_iter()
3111    .all(|format| {
3112        supports_format(
3113            instance,
3114            phd,
3115            format,
3116            vk::ImageTiling::OPTIMAL,
3117            vk::FormatFeatureFlags::COLOR_ATTACHMENT_BLEND,
3118        )
3119    })
3120}
3121
3122fn supports_format(
3123    instance: &ash::Instance,
3124    phd: vk::PhysicalDevice,
3125    format: vk::Format,
3126    tiling: vk::ImageTiling,
3127    features: vk::FormatFeatureFlags,
3128) -> bool {
3129    let properties = unsafe { instance.get_physical_device_format_properties(phd, format) };
3130    match tiling {
3131        vk::ImageTiling::LINEAR => properties.linear_tiling_features.contains(features),
3132        vk::ImageTiling::OPTIMAL => properties.optimal_tiling_features.contains(features),
3133        _ => false,
3134    }
3135}
3136
3137fn supports_astc_3d(instance: &ash::Instance, phd: vk::PhysicalDevice) -> bool {
3138    [
3139        vk::Format::ASTC_4X4_UNORM_BLOCK,
3140        vk::Format::ASTC_4X4_SRGB_BLOCK,
3141        vk::Format::ASTC_5X4_UNORM_BLOCK,
3142        vk::Format::ASTC_5X4_SRGB_BLOCK,
3143        vk::Format::ASTC_5X5_UNORM_BLOCK,
3144        vk::Format::ASTC_5X5_SRGB_BLOCK,
3145        vk::Format::ASTC_6X5_UNORM_BLOCK,
3146        vk::Format::ASTC_6X5_SRGB_BLOCK,
3147        vk::Format::ASTC_6X6_UNORM_BLOCK,
3148        vk::Format::ASTC_6X6_SRGB_BLOCK,
3149        vk::Format::ASTC_8X5_UNORM_BLOCK,
3150        vk::Format::ASTC_8X5_SRGB_BLOCK,
3151        vk::Format::ASTC_8X6_UNORM_BLOCK,
3152        vk::Format::ASTC_8X6_SRGB_BLOCK,
3153        vk::Format::ASTC_8X8_UNORM_BLOCK,
3154        vk::Format::ASTC_8X8_SRGB_BLOCK,
3155        vk::Format::ASTC_10X5_UNORM_BLOCK,
3156        vk::Format::ASTC_10X5_SRGB_BLOCK,
3157        vk::Format::ASTC_10X6_UNORM_BLOCK,
3158        vk::Format::ASTC_10X6_SRGB_BLOCK,
3159        vk::Format::ASTC_10X8_UNORM_BLOCK,
3160        vk::Format::ASTC_10X8_SRGB_BLOCK,
3161        vk::Format::ASTC_10X10_UNORM_BLOCK,
3162        vk::Format::ASTC_10X10_SRGB_BLOCK,
3163        vk::Format::ASTC_12X10_UNORM_BLOCK,
3164        vk::Format::ASTC_12X10_SRGB_BLOCK,
3165        vk::Format::ASTC_12X12_UNORM_BLOCK,
3166        vk::Format::ASTC_12X12_SRGB_BLOCK,
3167    ]
3168    .into_iter()
3169    .all(|format| {
3170        unsafe {
3171            instance.get_physical_device_image_format_properties(
3172                phd,
3173                format,
3174                vk::ImageType::TYPE_3D,
3175                vk::ImageTiling::OPTIMAL,
3176                vk::ImageUsageFlags::SAMPLED,
3177                vk::ImageCreateFlags::empty(),
3178            )
3179        }
3180        .is_ok()
3181    })
3182}
3183
3184fn supports_bgra8unorm_storage(
3185    instance: &ash::Instance,
3186    phd: vk::PhysicalDevice,
3187    device_api_version: u32,
3188) -> bool {
3189    // See https://github.com/KhronosGroup/Vulkan-Docs/issues/2027#issuecomment-1380608011
3190
3191    // This check gates the function call and structures used below.
3192    // TODO: check for (`VK_KHR_get_physical_device_properties2` or VK1.1) and (`VK_KHR_format_feature_flags2` or VK1.3).
3193    // Right now we only check for VK1.3.
3194    if device_api_version < vk::API_VERSION_1_3 {
3195        return false;
3196    }
3197
3198    unsafe {
3199        let mut properties3 = vk::FormatProperties3::default();
3200        let mut properties2 = vk::FormatProperties2::default().push_next(&mut properties3);
3201
3202        instance.get_physical_device_format_properties2(
3203            phd,
3204            vk::Format::B8G8R8A8_UNORM,
3205            &mut properties2,
3206        );
3207
3208        let features2 = properties2.format_properties.optimal_tiling_features;
3209        let features3 = properties3.optimal_tiling_features;
3210
3211        features2.contains(vk::FormatFeatureFlags::STORAGE_IMAGE)
3212            && features3.contains(vk::FormatFeatureFlags2::STORAGE_WRITE_WITHOUT_FORMAT)
3213    }
3214}
3215
3216// For https://github.com/gfx-rs/wgpu/issues/4599
3217// Intel iGPUs with outdated drivers can break rendering if `VK_EXT_robustness2` is used.
3218// Driver version 31.0.101.2115 works, but there's probably an earlier functional version.
3219fn is_intel_igpu_outdated_for_robustness2(
3220    props: vk::PhysicalDeviceProperties,
3221    driver: Option<vk::PhysicalDeviceDriverPropertiesKHR>,
3222) -> bool {
3223    const DRIVER_VERSION_WORKING: u32 = (101 << 14) | 2115; // X.X.101.2115
3224
3225    let is_outdated = props.vendor_id == crate::auxil::db::intel::VENDOR
3226        && props.device_type == vk::PhysicalDeviceType::INTEGRATED_GPU
3227        && props.driver_version < DRIVER_VERSION_WORKING
3228        && driver
3229            .map(|driver| driver.driver_id == vk::DriverId::INTEL_PROPRIETARY_WINDOWS)
3230            .unwrap_or_default();
3231
3232    if is_outdated {
3233        log::debug!(
3234            "Disabling robustBufferAccess2 and robustImageAccess2: IntegratedGpu Intel Driver is outdated. Found with version 0x{:X}, less than the known good version 0x{:X} (31.0.101.2115)",
3235            props.driver_version,
3236            DRIVER_VERSION_WORKING
3237        );
3238    }
3239    is_outdated
3240}
3241
3242/// Convert Vulkan component type to wgt::CooperativeScalarType.
3243fn map_vk_component_type(ty: vk::ComponentTypeKHR) -> Option<wgt::CooperativeScalarType> {
3244    match ty {
3245        vk::ComponentTypeKHR::FLOAT16 => Some(wgt::CooperativeScalarType::F16),
3246        vk::ComponentTypeKHR::FLOAT32 => Some(wgt::CooperativeScalarType::F32),
3247        vk::ComponentTypeKHR::SINT32 => Some(wgt::CooperativeScalarType::I32),
3248        vk::ComponentTypeKHR::UINT32 => Some(wgt::CooperativeScalarType::U32),
3249        _ => None,
3250    }
3251}
3252
3253/// Convert Vulkan matrix size.
3254fn map_vk_cooperative_size(size: u32) -> Option<u32> {
3255    match size {
3256        8 | 16 => Some(size),
3257        _ => None,
3258    }
3259}
3260
3261/// Query all supported cooperative matrix configurations from Vulkan.
3262fn query_cooperative_matrix_properties(
3263    coop_matrix: &khr::cooperative_matrix::Instance,
3264    phd: vk::PhysicalDevice,
3265) -> Vec<wgt::CooperativeMatrixProperties> {
3266    let vk_properties =
3267        match unsafe { coop_matrix.get_physical_device_cooperative_matrix_properties(phd) } {
3268            Ok(props) => props,
3269            Err(e) => {
3270                log::warn!("Failed to query cooperative matrix properties: {e:?}");
3271                return Vec::new();
3272            }
3273        };
3274
3275    log::debug!(
3276        "Vulkan reports {} cooperative matrix configurations",
3277        vk_properties.len()
3278    );
3279
3280    let mut result = Vec::new();
3281    for prop in &vk_properties {
3282        log::debug!(
3283            "  Vulkan coop matrix: M={} N={} K={} A={:?} B={:?} C={:?} Result={:?} scope={:?} saturating={}",
3284            prop.m_size,
3285            prop.n_size,
3286            prop.k_size,
3287            prop.a_type,
3288            prop.b_type,
3289            prop.c_type,
3290            prop.result_type,
3291            prop.scope,
3292            prop.saturating_accumulation
3293        );
3294
3295        // Only include subgroup-scoped operations (the only scope we support)
3296        if prop.scope != vk::ScopeKHR::SUBGROUP {
3297            log::debug!("    Skipped: scope is not SUBGROUP");
3298            continue;
3299        }
3300
3301        // Map sizes - skip configurations with sizes we don't support
3302        let m_size = match map_vk_cooperative_size(prop.m_size) {
3303            Some(s) => s,
3304            None => {
3305                log::debug!("    Skipped: M size {} not supported", prop.m_size);
3306                continue;
3307            }
3308        };
3309        let n_size = match map_vk_cooperative_size(prop.n_size) {
3310            Some(s) => s,
3311            None => {
3312                log::debug!("    Skipped: N size {} not supported", prop.n_size);
3313                continue;
3314            }
3315        };
3316        let k_size = match map_vk_cooperative_size(prop.k_size) {
3317            Some(s) => s,
3318            None => {
3319                log::debug!("    Skipped: K size {} not supported", prop.k_size);
3320                continue;
3321            }
3322        };
3323
3324        // Map the component types - A and B must match, C and Result must match
3325        let ab_type = match map_vk_component_type(prop.a_type) {
3326            Some(t) if Some(t) == map_vk_component_type(prop.b_type) => t,
3327            _ => {
3328                log::debug!(
3329                    "    Skipped: A/B types {:?}/{:?} not supported or don't match",
3330                    prop.a_type,
3331                    prop.b_type
3332                );
3333                continue;
3334            }
3335        };
3336        let cr_type = match map_vk_component_type(prop.c_type) {
3337            Some(t) if Some(t) == map_vk_component_type(prop.result_type) => t,
3338            _ => {
3339                log::debug!(
3340                    "    Skipped: C/Result types {:?}/{:?} not supported or don't match",
3341                    prop.c_type,
3342                    prop.result_type
3343                );
3344                continue;
3345            }
3346        };
3347
3348        log::debug!("    Accepted!");
3349        result.push(wgt::CooperativeMatrixProperties {
3350            m_size,
3351            n_size,
3352            k_size,
3353            ab_type,
3354            cr_type,
3355            saturating_accumulation: prop.saturating_accumulation != 0,
3356        });
3357    }
3358
3359    log::info!(
3360        "Found {} cooperative matrix configurations supported by wgpu",
3361        result.len()
3362    );
3363    result
3364}