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