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