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