wgpu_hal/vulkan/
adapter.rs

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