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