wgpu_hal/vulkan/
adapter.rs

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