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