wgpu_hal/vulkan/
adapter.rs

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