wgpu_hal/vulkan/
adapter.rs

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