Skip to main content

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