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