wgpu_hal/vulkan/
adapter.rs

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