li_wgpu_hal/vulkan/
adapter.rs

1use super::conv;
2
3use ash::{extensions::khr, vk};
4use parking_lot::Mutex;
5
6use std::{collections::BTreeMap, ffi::CStr, sync::Arc};
7
8fn depth_stencil_required_flags() -> vk::FormatFeatureFlags {
9    vk::FormatFeatureFlags::SAMPLED_IMAGE | vk::FormatFeatureFlags::DEPTH_STENCIL_ATTACHMENT
10}
11
12//TODO: const fn?
13fn indexing_features() -> wgt::Features {
14    wgt::Features::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING
15        | wgt::Features::UNIFORM_BUFFER_AND_STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING
16        | wgt::Features::PARTIALLY_BOUND_BINDING_ARRAY
17}
18
19/// Aggregate of the `vk::PhysicalDevice*Features` structs used by `gfx`.
20#[derive(Debug, Default)]
21pub struct PhysicalDeviceFeatures {
22    core: vk::PhysicalDeviceFeatures,
23    pub(super) descriptor_indexing: Option<vk::PhysicalDeviceDescriptorIndexingFeaturesEXT>,
24    imageless_framebuffer: Option<vk::PhysicalDeviceImagelessFramebufferFeaturesKHR>,
25    timeline_semaphore: Option<vk::PhysicalDeviceTimelineSemaphoreFeaturesKHR>,
26    image_robustness: Option<vk::PhysicalDeviceImageRobustnessFeaturesEXT>,
27    robustness2: Option<vk::PhysicalDeviceRobustness2FeaturesEXT>,
28    multiview: Option<vk::PhysicalDeviceMultiviewFeaturesKHR>,
29    astc_hdr: Option<vk::PhysicalDeviceTextureCompressionASTCHDRFeaturesEXT>,
30    shader_float16: Option<(
31        vk::PhysicalDeviceShaderFloat16Int8Features,
32        vk::PhysicalDevice16BitStorageFeatures,
33    )>,
34    zero_initialize_workgroup_memory:
35        Option<vk::PhysicalDeviceZeroInitializeWorkgroupMemoryFeatures>,
36}
37
38// This is safe because the structs have `p_next: *mut c_void`, which we null out/never read.
39unsafe impl Send for PhysicalDeviceFeatures {}
40unsafe impl Sync for PhysicalDeviceFeatures {}
41
42impl PhysicalDeviceFeatures {
43    /// Add the members of `self` into `info.enabled_features` and its `p_next` chain.
44    pub fn add_to_device_create_builder<'a>(
45        &'a mut self,
46        mut info: vk::DeviceCreateInfoBuilder<'a>,
47    ) -> vk::DeviceCreateInfoBuilder<'a> {
48        info = info.enabled_features(&self.core);
49        if let Some(ref mut feature) = self.descriptor_indexing {
50            info = info.push_next(feature);
51        }
52        if let Some(ref mut feature) = self.imageless_framebuffer {
53            info = info.push_next(feature);
54        }
55        if let Some(ref mut feature) = self.timeline_semaphore {
56            info = info.push_next(feature);
57        }
58        if let Some(ref mut feature) = self.image_robustness {
59            info = info.push_next(feature);
60        }
61        if let Some(ref mut feature) = self.robustness2 {
62            info = info.push_next(feature);
63        }
64        if let Some(ref mut feature) = self.astc_hdr {
65            info = info.push_next(feature);
66        }
67        if let Some((ref mut f16_i8_feature, ref mut _16bit_feature)) = self.shader_float16 {
68            info = info.push_next(f16_i8_feature);
69            info = info.push_next(_16bit_feature);
70        }
71        if let Some(ref mut feature) = self.zero_initialize_workgroup_memory {
72            info = info.push_next(feature);
73        }
74        info
75    }
76
77    /// Create a `PhysicalDeviceFeatures` that will be used to create a logical device.
78    ///
79    /// `requested_features` should be the same as what was used to generate `enabled_extensions`.
80    fn from_extensions_and_requested_features(
81        device_api_version: u32,
82        enabled_extensions: &[&'static CStr],
83        requested_features: wgt::Features,
84        downlevel_flags: wgt::DownlevelFlags,
85        private_caps: &super::PrivateCapabilities,
86    ) -> Self {
87        let needs_sampled_image_non_uniform = requested_features.contains(
88            wgt::Features::TEXTURE_BINDING_ARRAY
89                | wgt::Features::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING,
90        );
91        let needs_storage_buffer_non_uniform = requested_features.contains(
92            wgt::Features::BUFFER_BINDING_ARRAY
93                | wgt::Features::STORAGE_RESOURCE_BINDING_ARRAY
94                | wgt::Features::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING,
95        );
96        let needs_uniform_buffer_non_uniform = requested_features.contains(
97            wgt::Features::TEXTURE_BINDING_ARRAY
98                | wgt::Features::UNIFORM_BUFFER_AND_STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING,
99        );
100        let needs_storage_image_non_uniform = requested_features.contains(
101            wgt::Features::TEXTURE_BINDING_ARRAY
102                | wgt::Features::STORAGE_RESOURCE_BINDING_ARRAY
103                | wgt::Features::UNIFORM_BUFFER_AND_STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING,
104        );
105        let needs_partially_bound =
106            requested_features.intersects(wgt::Features::PARTIALLY_BOUND_BINDING_ARRAY);
107
108        Self {
109            // vk::PhysicalDeviceFeatures is a struct composed of Bool32's while
110            // Features is a bitfield so we need to map everything manually
111            core: vk::PhysicalDeviceFeatures::builder()
112                .robust_buffer_access(private_caps.robust_buffer_access)
113                .independent_blend(downlevel_flags.contains(wgt::DownlevelFlags::INDEPENDENT_BLEND))
114                .sample_rate_shading(
115                    downlevel_flags.contains(wgt::DownlevelFlags::MULTISAMPLED_SHADING),
116                )
117                .image_cube_array(
118                    downlevel_flags.contains(wgt::DownlevelFlags::CUBE_ARRAY_TEXTURES),
119                )
120                .draw_indirect_first_instance(
121                    requested_features.contains(wgt::Features::INDIRECT_FIRST_INSTANCE),
122                )
123                //.dual_src_blend(requested_features.contains(wgt::Features::DUAL_SRC_BLENDING))
124                .multi_draw_indirect(
125                    requested_features.contains(wgt::Features::MULTI_DRAW_INDIRECT),
126                )
127                .fill_mode_non_solid(requested_features.intersects(
128                    wgt::Features::POLYGON_MODE_LINE | wgt::Features::POLYGON_MODE_POINT,
129                ))
130                //.depth_bounds(requested_features.contains(wgt::Features::DEPTH_BOUNDS))
131                //.alpha_to_one(requested_features.contains(wgt::Features::ALPHA_TO_ONE))
132                //.multi_viewport(requested_features.contains(wgt::Features::MULTI_VIEWPORTS))
133                .sampler_anisotropy(
134                    downlevel_flags.contains(wgt::DownlevelFlags::ANISOTROPIC_FILTERING),
135                )
136                .texture_compression_etc2(
137                    requested_features.contains(wgt::Features::TEXTURE_COMPRESSION_ETC2),
138                )
139                .texture_compression_astc_ldr(
140                    requested_features.contains(wgt::Features::TEXTURE_COMPRESSION_ASTC),
141                )
142                .texture_compression_bc(
143                    requested_features.contains(wgt::Features::TEXTURE_COMPRESSION_BC),
144                )
145                //.occlusion_query_precise(requested_features.contains(wgt::Features::PRECISE_OCCLUSION_QUERY))
146                .pipeline_statistics_query(
147                    requested_features.contains(wgt::Features::PIPELINE_STATISTICS_QUERY),
148                )
149                .vertex_pipeline_stores_and_atomics(
150                    requested_features.contains(wgt::Features::VERTEX_WRITABLE_STORAGE),
151                )
152                .fragment_stores_and_atomics(
153                    downlevel_flags.contains(wgt::DownlevelFlags::FRAGMENT_WRITABLE_STORAGE),
154                )
155                //.shader_image_gather_extended(
156                //.shader_storage_image_extended_formats(
157                .shader_uniform_buffer_array_dynamic_indexing(
158                    requested_features.contains(wgt::Features::BUFFER_BINDING_ARRAY),
159                )
160                .shader_storage_buffer_array_dynamic_indexing(requested_features.contains(
161                    wgt::Features::BUFFER_BINDING_ARRAY
162                        | wgt::Features::STORAGE_RESOURCE_BINDING_ARRAY,
163                ))
164                .shader_sampled_image_array_dynamic_indexing(
165                    requested_features.contains(wgt::Features::TEXTURE_BINDING_ARRAY),
166                )
167                .shader_storage_buffer_array_dynamic_indexing(requested_features.contains(
168                    wgt::Features::TEXTURE_BINDING_ARRAY
169                        | wgt::Features::STORAGE_RESOURCE_BINDING_ARRAY,
170                ))
171                //.shader_storage_image_array_dynamic_indexing(
172                //.shader_clip_distance(requested_features.contains(wgt::Features::SHADER_CLIP_DISTANCE))
173                //.shader_cull_distance(requested_features.contains(wgt::Features::SHADER_CULL_DISTANCE))
174                .shader_float64(requested_features.contains(wgt::Features::SHADER_F64))
175                //.shader_int64(requested_features.contains(wgt::Features::SHADER_INT64))
176                .shader_int16(requested_features.contains(wgt::Features::SHADER_I16))
177                //.shader_resource_residency(requested_features.contains(wgt::Features::SHADER_RESOURCE_RESIDENCY))
178                .geometry_shader(requested_features.contains(wgt::Features::SHADER_PRIMITIVE_INDEX))
179                .depth_clamp(requested_features.contains(wgt::Features::DEPTH_CLIP_CONTROL))
180                .dual_src_blend(requested_features.contains(wgt::Features::DUAL_SOURCE_BLENDING))
181                .build(),
182            descriptor_indexing: if requested_features.intersects(indexing_features()) {
183                Some(
184                    vk::PhysicalDeviceDescriptorIndexingFeaturesEXT::builder()
185                        .shader_sampled_image_array_non_uniform_indexing(
186                            needs_sampled_image_non_uniform,
187                        )
188                        .shader_storage_image_array_non_uniform_indexing(
189                            needs_storage_image_non_uniform,
190                        )
191                        .shader_uniform_buffer_array_non_uniform_indexing(
192                            needs_uniform_buffer_non_uniform,
193                        )
194                        .shader_storage_buffer_array_non_uniform_indexing(
195                            needs_storage_buffer_non_uniform,
196                        )
197                        .descriptor_binding_partially_bound(needs_partially_bound)
198                        .build(),
199                )
200            } else {
201                None
202            },
203            imageless_framebuffer: if device_api_version >= vk::API_VERSION_1_2
204                || enabled_extensions.contains(&vk::KhrImagelessFramebufferFn::name())
205            {
206                Some(
207                    vk::PhysicalDeviceImagelessFramebufferFeaturesKHR::builder()
208                        .imageless_framebuffer(private_caps.imageless_framebuffers)
209                        .build(),
210                )
211            } else {
212                None
213            },
214            timeline_semaphore: if device_api_version >= vk::API_VERSION_1_2
215                || enabled_extensions.contains(&vk::KhrTimelineSemaphoreFn::name())
216            {
217                Some(
218                    vk::PhysicalDeviceTimelineSemaphoreFeaturesKHR::builder()
219                        .timeline_semaphore(private_caps.timeline_semaphores)
220                        .build(),
221                )
222            } else {
223                None
224            },
225            image_robustness: if device_api_version >= vk::API_VERSION_1_3
226                || enabled_extensions.contains(&vk::ExtImageRobustnessFn::name())
227            {
228                Some(
229                    vk::PhysicalDeviceImageRobustnessFeaturesEXT::builder()
230                        .robust_image_access(private_caps.robust_image_access)
231                        .build(),
232                )
233            } else {
234                None
235            },
236            robustness2: if enabled_extensions.contains(&vk::ExtRobustness2Fn::name()) {
237                // Note: enabling `robust_buffer_access2` isn't requires, strictly speaking
238                // since we can enable `robust_buffer_access` all the time. But it improves
239                // program portability, so we opt into it if they are supported.
240                Some(
241                    vk::PhysicalDeviceRobustness2FeaturesEXT::builder()
242                        .robust_buffer_access2(private_caps.robust_buffer_access2)
243                        .robust_image_access2(private_caps.robust_image_access2)
244                        .build(),
245                )
246            } else {
247                None
248            },
249            multiview: if device_api_version >= vk::API_VERSION_1_1
250                || enabled_extensions.contains(&vk::KhrMultiviewFn::name())
251            {
252                Some(
253                    vk::PhysicalDeviceMultiviewFeatures::builder()
254                        .multiview(requested_features.contains(wgt::Features::MULTIVIEW))
255                        .build(),
256                )
257            } else {
258                None
259            },
260            astc_hdr: if enabled_extensions.contains(&vk::ExtTextureCompressionAstcHdrFn::name()) {
261                Some(
262                    vk::PhysicalDeviceTextureCompressionASTCHDRFeaturesEXT::builder()
263                        .texture_compression_astc_hdr(true)
264                        .build(),
265                )
266            } else {
267                None
268            },
269            shader_float16: if requested_features.contains(wgt::Features::SHADER_F16) {
270                Some((
271                    vk::PhysicalDeviceShaderFloat16Int8Features::builder()
272                        .shader_float16(true)
273                        .build(),
274                    vk::PhysicalDevice16BitStorageFeatures::builder()
275                        .storage_buffer16_bit_access(true)
276                        .uniform_and_storage_buffer16_bit_access(true)
277                        .build(),
278                ))
279            } else {
280                None
281            },
282            zero_initialize_workgroup_memory: if device_api_version >= vk::API_VERSION_1_3
283                || enabled_extensions.contains(&vk::KhrZeroInitializeWorkgroupMemoryFn::name())
284            {
285                Some(
286                    vk::PhysicalDeviceZeroInitializeWorkgroupMemoryFeatures::builder()
287                        .shader_zero_initialize_workgroup_memory(
288                            private_caps.zero_initialize_workgroup_memory,
289                        )
290                        .build(),
291                )
292            } else {
293                None
294            },
295        }
296    }
297
298    fn to_wgpu(
299        &self,
300        instance: &ash::Instance,
301        phd: vk::PhysicalDevice,
302        caps: &PhysicalDeviceCapabilities,
303    ) -> (wgt::Features, wgt::DownlevelFlags) {
304        use crate::auxil::db;
305        use wgt::{DownlevelFlags as Df, Features as F};
306        let mut features = F::empty()
307            | F::SPIRV_SHADER_PASSTHROUGH
308            | F::MAPPABLE_PRIMARY_BUFFERS
309            | F::PUSH_CONSTANTS
310            | F::ADDRESS_MODE_CLAMP_TO_BORDER
311            | F::ADDRESS_MODE_CLAMP_TO_ZERO
312            | F::TIMESTAMP_QUERY
313            | F::TIMESTAMP_QUERY_INSIDE_PASSES
314            | F::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES
315            | F::CLEAR_TEXTURE;
316
317        let mut dl_flags = Df::COMPUTE_SHADERS
318            | Df::BASE_VERTEX
319            | Df::READ_ONLY_DEPTH_STENCIL
320            | Df::NON_POWER_OF_TWO_MIPMAPPED_TEXTURES
321            | Df::COMPARISON_SAMPLERS
322            | Df::VERTEX_STORAGE
323            | Df::FRAGMENT_STORAGE
324            | Df::DEPTH_TEXTURE_AND_BUFFER_COPIES
325            | Df::BUFFER_BINDINGS_NOT_16_BYTE_ALIGNED
326            | Df::UNRESTRICTED_INDEX_BUFFER
327            | Df::INDIRECT_EXECUTION
328            | Df::VIEW_FORMATS
329            | Df::UNRESTRICTED_EXTERNAL_TEXTURE_COPIES
330            | Df::NONBLOCKING_QUERY_RESOLVE;
331
332        dl_flags.set(
333            Df::SURFACE_VIEW_FORMATS,
334            caps.supports_extension(vk::KhrSwapchainMutableFormatFn::name()),
335        );
336        dl_flags.set(Df::CUBE_ARRAY_TEXTURES, self.core.image_cube_array != 0);
337        dl_flags.set(Df::ANISOTROPIC_FILTERING, self.core.sampler_anisotropy != 0);
338        dl_flags.set(
339            Df::FRAGMENT_WRITABLE_STORAGE,
340            self.core.fragment_stores_and_atomics != 0,
341        );
342        dl_flags.set(Df::MULTISAMPLED_SHADING, self.core.sample_rate_shading != 0);
343        dl_flags.set(Df::INDEPENDENT_BLEND, self.core.independent_blend != 0);
344        dl_flags.set(
345            Df::FULL_DRAW_INDEX_UINT32,
346            self.core.full_draw_index_uint32 != 0,
347        );
348        dl_flags.set(Df::DEPTH_BIAS_CLAMP, self.core.depth_bias_clamp != 0);
349
350        features.set(
351            F::INDIRECT_FIRST_INSTANCE,
352            self.core.draw_indirect_first_instance != 0,
353        );
354        //if self.core.dual_src_blend != 0
355        features.set(F::MULTI_DRAW_INDIRECT, self.core.multi_draw_indirect != 0);
356        features.set(F::POLYGON_MODE_LINE, self.core.fill_mode_non_solid != 0);
357        features.set(F::POLYGON_MODE_POINT, self.core.fill_mode_non_solid != 0);
358        //if self.core.depth_bounds != 0 {
359        //if self.core.alpha_to_one != 0 {
360        //if self.core.multi_viewport != 0 {
361        features.set(
362            F::TEXTURE_COMPRESSION_ETC2,
363            self.core.texture_compression_etc2 != 0,
364        );
365        features.set(
366            F::TEXTURE_COMPRESSION_ASTC,
367            self.core.texture_compression_astc_ldr != 0,
368        );
369        features.set(
370            F::TEXTURE_COMPRESSION_BC,
371            self.core.texture_compression_bc != 0,
372        );
373        features.set(
374            F::PIPELINE_STATISTICS_QUERY,
375            self.core.pipeline_statistics_query != 0,
376        );
377        features.set(
378            F::VERTEX_WRITABLE_STORAGE,
379            self.core.vertex_pipeline_stores_and_atomics != 0,
380        );
381        //if self.core.shader_image_gather_extended != 0 {
382        //if self.core.shader_storage_image_extended_formats != 0 {
383        features.set(
384            F::BUFFER_BINDING_ARRAY,
385            self.core.shader_uniform_buffer_array_dynamic_indexing != 0,
386        );
387        features.set(
388            F::TEXTURE_BINDING_ARRAY,
389            self.core.shader_sampled_image_array_dynamic_indexing != 0,
390        );
391        features.set(F::SHADER_PRIMITIVE_INDEX, self.core.geometry_shader != 0);
392        if Self::all_features_supported(
393            &features,
394            &[
395                (
396                    F::BUFFER_BINDING_ARRAY,
397                    self.core.shader_storage_buffer_array_dynamic_indexing,
398                ),
399                (
400                    F::TEXTURE_BINDING_ARRAY,
401                    self.core.shader_storage_image_array_dynamic_indexing,
402                ),
403            ],
404        ) {
405            features.insert(F::STORAGE_RESOURCE_BINDING_ARRAY);
406        }
407        //if self.core.shader_storage_image_array_dynamic_indexing != 0 {
408        //if self.core.shader_clip_distance != 0 {
409        //if self.core.shader_cull_distance != 0 {
410        features.set(F::SHADER_F64, self.core.shader_float64 != 0);
411        //if self.core.shader_int64 != 0 {
412        features.set(F::SHADER_I16, self.core.shader_int16 != 0);
413
414        //if caps.supports_extension(vk::KhrSamplerMirrorClampToEdgeFn::name()) {
415        //if caps.supports_extension(vk::ExtSamplerFilterMinmaxFn::name()) {
416        features.set(
417            F::MULTI_DRAW_INDIRECT_COUNT,
418            caps.supports_extension(vk::KhrDrawIndirectCountFn::name()),
419        );
420        features.set(
421            F::CONSERVATIVE_RASTERIZATION,
422            caps.supports_extension(vk::ExtConservativeRasterizationFn::name()),
423        );
424
425        let intel_windows = caps.properties.vendor_id == db::intel::VENDOR && cfg!(windows);
426
427        if let Some(ref descriptor_indexing) = self.descriptor_indexing {
428            const STORAGE: F = F::STORAGE_RESOURCE_BINDING_ARRAY;
429            if Self::all_features_supported(
430                &features,
431                &[
432                    (
433                        F::TEXTURE_BINDING_ARRAY,
434                        descriptor_indexing.shader_sampled_image_array_non_uniform_indexing,
435                    ),
436                    (
437                        F::BUFFER_BINDING_ARRAY | STORAGE,
438                        descriptor_indexing.shader_storage_buffer_array_non_uniform_indexing,
439                    ),
440                ],
441            ) {
442                features.insert(F::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING);
443            }
444            if Self::all_features_supported(
445                &features,
446                &[
447                    (
448                        F::BUFFER_BINDING_ARRAY,
449                        descriptor_indexing.shader_uniform_buffer_array_non_uniform_indexing,
450                    ),
451                    (
452                        F::TEXTURE_BINDING_ARRAY | STORAGE,
453                        descriptor_indexing.shader_storage_image_array_non_uniform_indexing,
454                    ),
455                ],
456            ) {
457                features.insert(F::UNIFORM_BUFFER_AND_STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING);
458            }
459            if descriptor_indexing.descriptor_binding_partially_bound != 0 && !intel_windows {
460                features |= F::PARTIALLY_BOUND_BINDING_ARRAY;
461            }
462        }
463
464        features.set(F::DEPTH_CLIP_CONTROL, self.core.depth_clamp != 0);
465        features.set(F::DUAL_SOURCE_BLENDING, self.core.dual_src_blend != 0);
466
467        if let Some(ref multiview) = self.multiview {
468            features.set(F::MULTIVIEW, multiview.multiview != 0);
469        }
470
471        features.set(
472            F::TEXTURE_FORMAT_16BIT_NORM,
473            is_format_16bit_norm_supported(instance, phd),
474        );
475
476        if let Some(ref astc_hdr) = self.astc_hdr {
477            features.set(
478                F::TEXTURE_COMPRESSION_ASTC_HDR,
479                astc_hdr.texture_compression_astc_hdr != 0,
480            );
481        }
482
483        if let Some((ref f16_i8, ref bit16)) = self.shader_float16 {
484            features.set(
485                F::SHADER_F16,
486                f16_i8.shader_float16 != 0
487                    && bit16.storage_buffer16_bit_access != 0
488                    && bit16.uniform_and_storage_buffer16_bit_access != 0,
489            );
490        }
491
492        let supports_depth_format = |format| {
493            supports_format(
494                instance,
495                phd,
496                format,
497                vk::ImageTiling::OPTIMAL,
498                depth_stencil_required_flags(),
499            )
500        };
501
502        let texture_s8 = supports_depth_format(vk::Format::S8_UINT);
503        let texture_d32 = supports_depth_format(vk::Format::D32_SFLOAT);
504        let texture_d24_s8 = supports_depth_format(vk::Format::D24_UNORM_S8_UINT);
505        let texture_d32_s8 = supports_depth_format(vk::Format::D32_SFLOAT_S8_UINT);
506
507        let stencil8 = texture_s8 || texture_d24_s8;
508        let depth24_plus_stencil8 = texture_d24_s8 || texture_d32_s8;
509
510        dl_flags.set(
511            Df::WEBGPU_TEXTURE_FORMAT_SUPPORT,
512            stencil8 && depth24_plus_stencil8 && texture_d32,
513        );
514
515        features.set(F::DEPTH32FLOAT_STENCIL8, texture_d32_s8);
516
517        let rg11b10ufloat_renderable = supports_format(
518            instance,
519            phd,
520            vk::Format::B10G11R11_UFLOAT_PACK32,
521            vk::ImageTiling::OPTIMAL,
522            vk::FormatFeatureFlags::COLOR_ATTACHMENT
523                | vk::FormatFeatureFlags::COLOR_ATTACHMENT_BLEND,
524        );
525        features.set(F::RG11B10UFLOAT_RENDERABLE, rg11b10ufloat_renderable);
526        features.set(F::SHADER_UNUSED_VERTEX_OUTPUT, true);
527
528        features.set(
529            F::BGRA8UNORM_STORAGE,
530            supports_bgra8unorm_storage(instance, phd, caps.device_api_version),
531        );
532
533        (features, dl_flags)
534    }
535
536    fn all_features_supported(
537        features: &wgt::Features,
538        implications: &[(wgt::Features, vk::Bool32)],
539    ) -> bool {
540        implications
541            .iter()
542            .all(|&(flag, support)| !features.contains(flag) || support != 0)
543    }
544}
545
546/// Information gathered about a physical device capabilities.
547#[derive(Default)]
548pub struct PhysicalDeviceCapabilities {
549    supported_extensions: Vec<vk::ExtensionProperties>,
550    properties: vk::PhysicalDeviceProperties,
551    maintenance_3: Option<vk::PhysicalDeviceMaintenance3Properties>,
552    descriptor_indexing: Option<vk::PhysicalDeviceDescriptorIndexingPropertiesEXT>,
553    driver: Option<vk::PhysicalDeviceDriverPropertiesKHR>,
554    /// The device API version.
555    ///
556    /// Which is the version of Vulkan supported for device-level functionality.
557    ///
558    /// It is associated with a `VkPhysicalDevice` and its children.
559    device_api_version: u32,
560}
561
562// This is safe because the structs have `p_next: *mut c_void`, which we null out/never read.
563unsafe impl Send for PhysicalDeviceCapabilities {}
564unsafe impl Sync for PhysicalDeviceCapabilities {}
565
566impl PhysicalDeviceCapabilities {
567    pub fn properties(&self) -> vk::PhysicalDeviceProperties {
568        self.properties
569    }
570
571    pub fn supports_extension(&self, extension: &CStr) -> bool {
572        use crate::auxil::cstr_from_bytes_until_nul;
573        self.supported_extensions
574            .iter()
575            .any(|ep| cstr_from_bytes_until_nul(&ep.extension_name) == Some(extension))
576    }
577
578    /// Map `requested_features` to the list of Vulkan extension strings required to create the logical device.
579    fn get_required_extensions(&self, requested_features: wgt::Features) -> Vec<&'static CStr> {
580        let mut extensions = Vec::new();
581
582        // Note that quite a few extensions depend on the `VK_KHR_get_physical_device_properties2` instance extension.
583        // We enable `VK_KHR_get_physical_device_properties2` unconditionally (if available).
584
585        // Require `VK_KHR_swapchain`
586        extensions.push(vk::KhrSwapchainFn::name());
587
588        if self.device_api_version < vk::API_VERSION_1_1 {
589            // Require either `VK_KHR_maintenance1` or `VK_AMD_negative_viewport_height`
590            if self.supports_extension(vk::KhrMaintenance1Fn::name()) {
591                extensions.push(vk::KhrMaintenance1Fn::name());
592            } else {
593                // `VK_AMD_negative_viewport_height` is obsoleted by `VK_KHR_maintenance1` and must not be enabled alongside it
594                extensions.push(vk::AmdNegativeViewportHeightFn::name());
595            }
596
597            // Optional `VK_KHR_maintenance2`
598            if self.supports_extension(vk::KhrMaintenance2Fn::name()) {
599                extensions.push(vk::KhrMaintenance2Fn::name());
600            }
601
602            // Optional `VK_KHR_maintenance3`
603            if self.supports_extension(vk::KhrMaintenance3Fn::name()) {
604                extensions.push(vk::KhrMaintenance3Fn::name());
605            }
606
607            // Require `VK_KHR_storage_buffer_storage_class`
608            extensions.push(vk::KhrStorageBufferStorageClassFn::name());
609
610            // Require `VK_KHR_multiview` if the associated feature was requested
611            if requested_features.contains(wgt::Features::MULTIVIEW) {
612                extensions.push(vk::KhrMultiviewFn::name());
613            }
614        }
615
616        if self.device_api_version < vk::API_VERSION_1_2 {
617            // Optional `VK_KHR_image_format_list`
618            if self.supports_extension(vk::KhrImageFormatListFn::name()) {
619                extensions.push(vk::KhrImageFormatListFn::name());
620            }
621
622            // Optional `VK_KHR_imageless_framebuffer`
623            if self.supports_extension(vk::KhrImagelessFramebufferFn::name()) {
624                extensions.push(vk::KhrImagelessFramebufferFn::name());
625                // Require `VK_KHR_maintenance2` due to it being a dependency
626                if self.device_api_version < vk::API_VERSION_1_1 {
627                    extensions.push(vk::KhrMaintenance2Fn::name());
628                }
629            }
630
631            // Optional `VK_KHR_driver_properties`
632            if self.supports_extension(vk::KhrDriverPropertiesFn::name()) {
633                extensions.push(vk::KhrDriverPropertiesFn::name());
634            }
635
636            // Optional `VK_KHR_timeline_semaphore`
637            if self.supports_extension(vk::KhrTimelineSemaphoreFn::name()) {
638                extensions.push(vk::KhrTimelineSemaphoreFn::name());
639            }
640
641            // Require `VK_EXT_descriptor_indexing` if one of the associated features was requested
642            if requested_features.intersects(indexing_features()) {
643                extensions.push(vk::ExtDescriptorIndexingFn::name());
644            }
645
646            // Require `VK_KHR_shader_float16_int8` and `VK_KHR_16bit_storage` if the associated feature was requested
647            if requested_features.contains(wgt::Features::SHADER_F16) {
648                extensions.push(vk::KhrShaderFloat16Int8Fn::name());
649                // `VK_KHR_16bit_storage` requires `VK_KHR_storage_buffer_storage_class`, however we require that one already
650                if self.device_api_version < vk::API_VERSION_1_1 {
651                    extensions.push(vk::Khr16bitStorageFn::name());
652                }
653            }
654
655            //extensions.push(vk::KhrSamplerMirrorClampToEdgeFn::name());
656            //extensions.push(vk::ExtSamplerFilterMinmaxFn::name());
657        }
658
659        if self.device_api_version < vk::API_VERSION_1_3 {
660            // Optional `VK_EXT_image_robustness`
661            if self.supports_extension(vk::ExtImageRobustnessFn::name()) {
662                extensions.push(vk::ExtImageRobustnessFn::name());
663            }
664        }
665
666        // Optional `VK_KHR_swapchain_mutable_format`
667        if self.supports_extension(vk::KhrSwapchainMutableFormatFn::name()) {
668            extensions.push(vk::KhrSwapchainMutableFormatFn::name());
669        }
670
671        // Optional `VK_EXT_robustness2`
672        if self.supports_extension(vk::ExtRobustness2Fn::name()) {
673            extensions.push(vk::ExtRobustness2Fn::name());
674        }
675
676        // Require `VK_KHR_draw_indirect_count` if the associated feature was requested
677        // Even though Vulkan 1.2 has promoted the extension to core, we must require the extension to avoid
678        // large amounts of spaghetti involved with using PhysicalDeviceVulkan12Features.
679        if requested_features.contains(wgt::Features::MULTI_DRAW_INDIRECT_COUNT) {
680            extensions.push(vk::KhrDrawIndirectCountFn::name());
681        }
682
683        // Require `VK_EXT_conservative_rasterization` if the associated feature was requested
684        if requested_features.contains(wgt::Features::CONSERVATIVE_RASTERIZATION) {
685            extensions.push(vk::ExtConservativeRasterizationFn::name());
686        }
687
688        // Require `VK_KHR_portability_subset` on macOS/iOS
689        #[cfg(any(target_os = "macos", target_os = "ios"))]
690        extensions.push(vk::KhrPortabilitySubsetFn::name());
691
692        // Require `VK_EXT_texture_compression_astc_hdr` if the associated feature was requested
693        if requested_features.contains(wgt::Features::TEXTURE_COMPRESSION_ASTC_HDR) {
694            extensions.push(vk::ExtTextureCompressionAstcHdrFn::name());
695        }
696
697        extensions
698    }
699
700    fn to_wgpu_limits(&self) -> wgt::Limits {
701        let limits = &self.properties.limits;
702
703        let max_compute_workgroup_sizes = limits.max_compute_work_group_size;
704        let max_compute_workgroups_per_dimension = limits.max_compute_work_group_count[0]
705            .min(limits.max_compute_work_group_count[1])
706            .min(limits.max_compute_work_group_count[2]);
707
708        // Prevent very large buffers on mesa and most android devices.
709        let is_nvidia = self.properties.vendor_id == crate::auxil::db::nvidia::VENDOR;
710        let max_buffer_size =
711            if (cfg!(target_os = "linux") || cfg!(target_os = "android")) && !is_nvidia {
712                i32::MAX as u64
713            } else {
714                u64::MAX
715            };
716
717        wgt::Limits {
718            max_texture_dimension_1d: limits.max_image_dimension1_d,
719            max_texture_dimension_2d: limits.max_image_dimension2_d,
720            max_texture_dimension_3d: limits.max_image_dimension3_d,
721            max_texture_array_layers: limits.max_image_array_layers,
722            max_bind_groups: limits
723                .max_bound_descriptor_sets
724                .min(crate::MAX_BIND_GROUPS as u32),
725            max_bindings_per_bind_group: wgt::Limits::default().max_bindings_per_bind_group,
726            max_dynamic_uniform_buffers_per_pipeline_layout: limits
727                .max_descriptor_set_uniform_buffers_dynamic,
728            max_dynamic_storage_buffers_per_pipeline_layout: limits
729                .max_descriptor_set_storage_buffers_dynamic,
730            max_sampled_textures_per_shader_stage: limits.max_per_stage_descriptor_sampled_images,
731            max_samplers_per_shader_stage: limits.max_per_stage_descriptor_samplers,
732            max_storage_buffers_per_shader_stage: limits.max_per_stage_descriptor_storage_buffers,
733            max_storage_textures_per_shader_stage: limits.max_per_stage_descriptor_storage_images,
734            max_uniform_buffers_per_shader_stage: limits.max_per_stage_descriptor_uniform_buffers,
735            max_uniform_buffer_binding_size: limits
736                .max_uniform_buffer_range
737                .min(crate::auxil::MAX_I32_BINDING_SIZE),
738            max_storage_buffer_binding_size: limits
739                .max_storage_buffer_range
740                .min(crate::auxil::MAX_I32_BINDING_SIZE),
741            max_vertex_buffers: limits
742                .max_vertex_input_bindings
743                .min(crate::MAX_VERTEX_BUFFERS as u32),
744            max_vertex_attributes: limits.max_vertex_input_attributes,
745            max_vertex_buffer_array_stride: limits.max_vertex_input_binding_stride,
746            max_push_constant_size: limits.max_push_constants_size,
747            min_uniform_buffer_offset_alignment: limits.min_uniform_buffer_offset_alignment as u32,
748            min_storage_buffer_offset_alignment: limits.min_storage_buffer_offset_alignment as u32,
749            max_inter_stage_shader_components: limits
750                .max_vertex_output_components
751                .min(limits.max_fragment_input_components),
752            max_compute_workgroup_storage_size: limits.max_compute_shared_memory_size,
753            max_compute_invocations_per_workgroup: limits.max_compute_work_group_invocations,
754            max_compute_workgroup_size_x: max_compute_workgroup_sizes[0],
755            max_compute_workgroup_size_y: max_compute_workgroup_sizes[1],
756            max_compute_workgroup_size_z: max_compute_workgroup_sizes[2],
757            max_compute_workgroups_per_dimension,
758            max_buffer_size,
759            max_non_sampler_bindings: std::u32::MAX,
760        }
761    }
762
763    fn to_hal_alignments(&self) -> crate::Alignments {
764        let limits = &self.properties.limits;
765        crate::Alignments {
766            buffer_copy_offset: wgt::BufferSize::new(limits.optimal_buffer_copy_offset_alignment)
767                .unwrap(),
768            buffer_copy_pitch: wgt::BufferSize::new(limits.optimal_buffer_copy_row_pitch_alignment)
769                .unwrap(),
770        }
771    }
772}
773
774impl super::InstanceShared {
775    #[allow(trivial_casts)] // false positives
776    fn inspect(
777        &self,
778        phd: vk::PhysicalDevice,
779    ) -> (PhysicalDeviceCapabilities, PhysicalDeviceFeatures) {
780        let capabilities = {
781            let mut capabilities = PhysicalDeviceCapabilities::default();
782            capabilities.supported_extensions =
783                unsafe { self.raw.enumerate_device_extension_properties(phd).unwrap() };
784            capabilities.properties = unsafe { self.raw.get_physical_device_properties(phd) };
785            capabilities.device_api_version = capabilities.properties.api_version;
786
787            if let Some(ref get_device_properties) = self.get_physical_device_properties {
788                // Get these now to avoid borrowing conflicts later
789                let supports_maintenance3 = capabilities.device_api_version >= vk::API_VERSION_1_1
790                    || capabilities.supports_extension(vk::KhrMaintenance3Fn::name());
791                let supports_descriptor_indexing = capabilities.device_api_version
792                    >= vk::API_VERSION_1_2
793                    || capabilities.supports_extension(vk::ExtDescriptorIndexingFn::name());
794                let supports_driver_properties = capabilities.device_api_version
795                    >= vk::API_VERSION_1_2
796                    || capabilities.supports_extension(vk::KhrDriverPropertiesFn::name());
797
798                let mut builder = vk::PhysicalDeviceProperties2KHR::builder();
799                if supports_maintenance3 {
800                    capabilities.maintenance_3 =
801                        Some(vk::PhysicalDeviceMaintenance3Properties::default());
802                    builder = builder.push_next(capabilities.maintenance_3.as_mut().unwrap());
803                }
804
805                if supports_descriptor_indexing {
806                    let next = capabilities
807                        .descriptor_indexing
808                        .insert(vk::PhysicalDeviceDescriptorIndexingPropertiesEXT::default());
809                    builder = builder.push_next(next);
810                }
811
812                if supports_driver_properties {
813                    let next = capabilities
814                        .driver
815                        .insert(vk::PhysicalDeviceDriverPropertiesKHR::default());
816                    builder = builder.push_next(next);
817                }
818
819                let mut properties2 = builder.build();
820                unsafe {
821                    get_device_properties.get_physical_device_properties2(phd, &mut properties2);
822                }
823            };
824            capabilities
825        };
826
827        let mut features = PhysicalDeviceFeatures::default();
828        features.core = if let Some(ref get_device_properties) = self.get_physical_device_properties
829        {
830            let core = vk::PhysicalDeviceFeatures::default();
831            let mut builder = vk::PhysicalDeviceFeatures2KHR::builder().features(core);
832
833            // `VK_KHR_multiview` is promoted to 1.1
834            if capabilities.device_api_version >= vk::API_VERSION_1_1
835                || capabilities.supports_extension(vk::KhrMultiviewFn::name())
836            {
837                let next = features
838                    .multiview
839                    .insert(vk::PhysicalDeviceMultiviewFeatures::default());
840                builder = builder.push_next(next);
841            }
842
843            if capabilities.supports_extension(vk::ExtDescriptorIndexingFn::name()) {
844                let next = features
845                    .descriptor_indexing
846                    .insert(vk::PhysicalDeviceDescriptorIndexingFeaturesEXT::default());
847                builder = builder.push_next(next);
848            }
849
850            // `VK_KHR_imageless_framebuffer` is promoted to 1.2, but has no changes, so we can keep using the extension unconditionally.
851            if capabilities.supports_extension(vk::KhrImagelessFramebufferFn::name()) {
852                let next = features
853                    .imageless_framebuffer
854                    .insert(vk::PhysicalDeviceImagelessFramebufferFeaturesKHR::default());
855                builder = builder.push_next(next);
856            }
857
858            // `VK_KHR_timeline_semaphore` is promoted to 1.2, but has no changes, so we can keep using the extension unconditionally.
859            if capabilities.supports_extension(vk::KhrTimelineSemaphoreFn::name()) {
860                let next = features
861                    .timeline_semaphore
862                    .insert(vk::PhysicalDeviceTimelineSemaphoreFeaturesKHR::default());
863                builder = builder.push_next(next);
864            }
865
866            if capabilities.supports_extension(vk::ExtImageRobustnessFn::name()) {
867                let next = features
868                    .image_robustness
869                    .insert(vk::PhysicalDeviceImageRobustnessFeaturesEXT::default());
870                builder = builder.push_next(next);
871            }
872            if capabilities.supports_extension(vk::ExtRobustness2Fn::name()) {
873                let next = features
874                    .robustness2
875                    .insert(vk::PhysicalDeviceRobustness2FeaturesEXT::default());
876                builder = builder.push_next(next);
877            }
878            if capabilities.supports_extension(vk::ExtTextureCompressionAstcHdrFn::name()) {
879                let next = features
880                    .astc_hdr
881                    .insert(vk::PhysicalDeviceTextureCompressionASTCHDRFeaturesEXT::default());
882                builder = builder.push_next(next);
883            }
884            if capabilities.supports_extension(vk::KhrShaderFloat16Int8Fn::name())
885                && capabilities.supports_extension(vk::Khr16bitStorageFn::name())
886            {
887                let next = features.shader_float16.insert((
888                    vk::PhysicalDeviceShaderFloat16Int8FeaturesKHR::default(),
889                    vk::PhysicalDevice16BitStorageFeaturesKHR::default(),
890                ));
891                builder = builder.push_next(&mut next.0);
892                builder = builder.push_next(&mut next.1);
893            }
894
895            // `VK_KHR_zero_initialize_workgroup_memory` is promoted to 1.3
896            if capabilities.device_api_version >= vk::API_VERSION_1_3
897                || capabilities.supports_extension(vk::KhrZeroInitializeWorkgroupMemoryFn::name())
898            {
899                let next = features
900                    .zero_initialize_workgroup_memory
901                    .insert(vk::PhysicalDeviceZeroInitializeWorkgroupMemoryFeatures::default());
902                builder = builder.push_next(next);
903            }
904
905            let mut features2 = builder.build();
906            unsafe {
907                get_device_properties.get_physical_device_features2(phd, &mut features2);
908            }
909            features2.features
910        } else {
911            unsafe { self.raw.get_physical_device_features(phd) }
912        };
913
914        (capabilities, features)
915    }
916}
917
918impl super::Instance {
919    pub fn expose_adapter(
920        &self,
921        phd: vk::PhysicalDevice,
922    ) -> Option<crate::ExposedAdapter<super::Api>> {
923        use crate::auxil::cstr_from_bytes_until_nul;
924        use crate::auxil::db;
925
926        let (phd_capabilities, phd_features) = self.shared.inspect(phd);
927
928        let info = wgt::AdapterInfo {
929            name: {
930                cstr_from_bytes_until_nul(&phd_capabilities.properties.device_name)
931                    .and_then(|info| info.to_str().ok())
932                    .unwrap_or("?")
933                    .to_owned()
934            },
935            vendor: phd_capabilities.properties.vendor_id,
936            device: phd_capabilities.properties.device_id,
937            device_type: match phd_capabilities.properties.device_type {
938                ash::vk::PhysicalDeviceType::OTHER => wgt::DeviceType::Other,
939                ash::vk::PhysicalDeviceType::INTEGRATED_GPU => wgt::DeviceType::IntegratedGpu,
940                ash::vk::PhysicalDeviceType::DISCRETE_GPU => wgt::DeviceType::DiscreteGpu,
941                ash::vk::PhysicalDeviceType::VIRTUAL_GPU => wgt::DeviceType::VirtualGpu,
942                ash::vk::PhysicalDeviceType::CPU => wgt::DeviceType::Cpu,
943                _ => wgt::DeviceType::Other,
944            },
945            driver: {
946                phd_capabilities
947                    .driver
948                    .as_ref()
949                    .and_then(|driver| cstr_from_bytes_until_nul(&driver.driver_name))
950                    .and_then(|name| name.to_str().ok())
951                    .unwrap_or("?")
952                    .to_owned()
953            },
954            driver_info: {
955                phd_capabilities
956                    .driver
957                    .as_ref()
958                    .and_then(|driver| cstr_from_bytes_until_nul(&driver.driver_info))
959                    .and_then(|name| name.to_str().ok())
960                    .unwrap_or("?")
961                    .to_owned()
962            },
963            backend: wgt::Backend::Vulkan,
964        };
965
966        let (available_features, downlevel_flags) =
967            phd_features.to_wgpu(&self.shared.raw, phd, &phd_capabilities);
968        let mut workarounds = super::Workarounds::empty();
969        {
970            // see https://github.com/gfx-rs/gfx/issues/1930
971            let _is_windows_intel_dual_src_bug = cfg!(windows)
972                && phd_capabilities.properties.vendor_id == db::intel::VENDOR
973                && (phd_capabilities.properties.device_id & db::intel::DEVICE_KABY_LAKE_MASK
974                    == db::intel::DEVICE_KABY_LAKE_MASK
975                    || phd_capabilities.properties.device_id & db::intel::DEVICE_SKY_LAKE_MASK
976                        == db::intel::DEVICE_SKY_LAKE_MASK);
977            // TODO: only enable for particular devices
978            workarounds |= super::Workarounds::SEPARATE_ENTRY_POINTS;
979            workarounds.set(
980                super::Workarounds::EMPTY_RESOLVE_ATTACHMENT_LISTS,
981                phd_capabilities.properties.vendor_id == db::qualcomm::VENDOR,
982            );
983            workarounds.set(
984                super::Workarounds::FORCE_FILL_BUFFER_WITH_SIZE_GREATER_4096_ALIGNED_OFFSET_16,
985                phd_capabilities.properties.vendor_id == db::nvidia::VENDOR,
986            );
987        };
988
989        if phd_capabilities.device_api_version == vk::API_VERSION_1_0
990            && !phd_capabilities.supports_extension(vk::KhrStorageBufferStorageClassFn::name())
991        {
992            log::warn!(
993                "SPIR-V storage buffer class is not supported, hiding adapter: {}",
994                info.name
995            );
996            return None;
997        }
998        if !phd_capabilities.supports_extension(vk::AmdNegativeViewportHeightFn::name())
999            && !phd_capabilities.supports_extension(vk::KhrMaintenance1Fn::name())
1000            && phd_capabilities.device_api_version < vk::API_VERSION_1_1
1001        {
1002            log::warn!(
1003                "viewport Y-flip is not supported, hiding adapter: {}",
1004                info.name
1005            );
1006            return None;
1007        }
1008
1009        let queue_families = unsafe {
1010            self.shared
1011                .raw
1012                .get_physical_device_queue_family_properties(phd)
1013        };
1014        let queue_flags = queue_families.first()?.queue_flags;
1015        if !queue_flags.contains(vk::QueueFlags::GRAPHICS) {
1016            log::warn!("The first queue only exposes {:?}", queue_flags);
1017            return None;
1018        }
1019
1020        let private_caps = super::PrivateCapabilities {
1021            flip_y_requires_shift: phd_capabilities.device_api_version >= vk::API_VERSION_1_1
1022                || phd_capabilities.supports_extension(vk::KhrMaintenance1Fn::name()),
1023            imageless_framebuffers: match phd_features.imageless_framebuffer {
1024                Some(features) => features.imageless_framebuffer == vk::TRUE,
1025                None => phd_features
1026                    .imageless_framebuffer
1027                    .map_or(false, |ext| ext.imageless_framebuffer != 0),
1028            },
1029            image_view_usage: phd_capabilities.device_api_version >= vk::API_VERSION_1_1
1030                || phd_capabilities.supports_extension(vk::KhrMaintenance2Fn::name()),
1031            timeline_semaphores: match phd_features.timeline_semaphore {
1032                Some(features) => features.timeline_semaphore == vk::TRUE,
1033                None => phd_features
1034                    .timeline_semaphore
1035                    .map_or(false, |ext| ext.timeline_semaphore != 0),
1036            },
1037            texture_d24: supports_format(
1038                &self.shared.raw,
1039                phd,
1040                vk::Format::X8_D24_UNORM_PACK32,
1041                vk::ImageTiling::OPTIMAL,
1042                depth_stencil_required_flags(),
1043            ),
1044            texture_d24_s8: supports_format(
1045                &self.shared.raw,
1046                phd,
1047                vk::Format::D24_UNORM_S8_UINT,
1048                vk::ImageTiling::OPTIMAL,
1049                depth_stencil_required_flags(),
1050            ),
1051            texture_s8: supports_format(
1052                &self.shared.raw,
1053                phd,
1054                vk::Format::S8_UINT,
1055                vk::ImageTiling::OPTIMAL,
1056                depth_stencil_required_flags(),
1057            ),
1058            non_coherent_map_mask: phd_capabilities.properties.limits.non_coherent_atom_size - 1,
1059            can_present: true,
1060            //TODO: make configurable
1061            robust_buffer_access: phd_features.core.robust_buffer_access != 0,
1062            robust_image_access: match phd_features.robustness2 {
1063                Some(ref f) => f.robust_image_access2 != 0,
1064                None => phd_features
1065                    .image_robustness
1066                    .map_or(false, |ext| ext.robust_image_access != 0),
1067            },
1068            robust_buffer_access2: phd_features
1069                .robustness2
1070                .as_ref()
1071                .map(|r| r.robust_buffer_access2 == 1)
1072                .unwrap_or_default(),
1073            robust_image_access2: phd_features
1074                .robustness2
1075                .as_ref()
1076                .map(|r| r.robust_image_access2 == 1)
1077                .unwrap_or_default(),
1078            zero_initialize_workgroup_memory: phd_features
1079                .zero_initialize_workgroup_memory
1080                .map_or(false, |ext| {
1081                    ext.shader_zero_initialize_workgroup_memory == vk::TRUE
1082                }),
1083            image_format_list: phd_capabilities.device_api_version >= vk::API_VERSION_1_2
1084                || phd_capabilities.supports_extension(vk::KhrImageFormatListFn::name()),
1085        };
1086        let capabilities = crate::Capabilities {
1087            limits: phd_capabilities.to_wgpu_limits(),
1088            alignments: phd_capabilities.to_hal_alignments(),
1089            downlevel: wgt::DownlevelCapabilities {
1090                flags: downlevel_flags,
1091                limits: wgt::DownlevelLimits {},
1092                shader_model: wgt::ShaderModel::Sm5, //TODO?
1093            },
1094        };
1095
1096        let adapter = super::Adapter {
1097            raw: phd,
1098            instance: Arc::clone(&self.shared),
1099            //queue_families,
1100            known_memory_flags: vk::MemoryPropertyFlags::DEVICE_LOCAL
1101                | vk::MemoryPropertyFlags::HOST_VISIBLE
1102                | vk::MemoryPropertyFlags::HOST_COHERENT
1103                | vk::MemoryPropertyFlags::HOST_CACHED
1104                | vk::MemoryPropertyFlags::LAZILY_ALLOCATED,
1105            phd_capabilities,
1106            //phd_features,
1107            downlevel_flags,
1108            private_caps,
1109            workarounds,
1110        };
1111
1112        Some(crate::ExposedAdapter {
1113            adapter,
1114            info,
1115            features: available_features,
1116            capabilities,
1117        })
1118    }
1119}
1120
1121impl super::Adapter {
1122    pub fn raw_physical_device(&self) -> ash::vk::PhysicalDevice {
1123        self.raw
1124    }
1125
1126    pub fn physical_device_capabilities(&self) -> &PhysicalDeviceCapabilities {
1127        &self.phd_capabilities
1128    }
1129
1130    pub fn shared_instance(&self) -> &super::InstanceShared {
1131        &self.instance
1132    }
1133
1134    pub fn required_device_extensions(&self, features: wgt::Features) -> Vec<&'static CStr> {
1135        let (supported_extensions, unsupported_extensions) = self
1136            .phd_capabilities
1137            .get_required_extensions(features)
1138            .iter()
1139            .partition::<Vec<&CStr>, _>(|&&extension| {
1140                self.phd_capabilities.supports_extension(extension)
1141            });
1142
1143        if !unsupported_extensions.is_empty() {
1144            log::warn!("Missing extensions: {:?}", unsupported_extensions);
1145        }
1146
1147        log::debug!("Supported extensions: {:?}", supported_extensions);
1148        supported_extensions
1149    }
1150
1151    /// `features` must be the same features used to create `enabled_extensions`.
1152    pub fn physical_device_features(
1153        &self,
1154        enabled_extensions: &[&'static CStr],
1155        features: wgt::Features,
1156    ) -> PhysicalDeviceFeatures {
1157        PhysicalDeviceFeatures::from_extensions_and_requested_features(
1158            self.phd_capabilities.device_api_version,
1159            enabled_extensions,
1160            features,
1161            self.downlevel_flags,
1162            &self.private_caps,
1163        )
1164    }
1165
1166    /// # Safety
1167    ///
1168    /// - `raw_device` must be created from this adapter.
1169    /// - `raw_device` must be created using `family_index`, `enabled_extensions` and `physical_device_features()`
1170    /// - `enabled_extensions` must be a superset of `required_device_extensions()`.
1171    #[allow(clippy::too_many_arguments)]
1172    pub unsafe fn device_from_raw(
1173        &self,
1174        raw_device: ash::Device,
1175        handle_is_owned: bool,
1176        enabled_extensions: &[&'static CStr],
1177        features: wgt::Features,
1178        family_index: u32,
1179        queue_index: u32,
1180    ) -> Result<crate::OpenDevice<super::Api>, crate::DeviceError> {
1181        let mem_properties = {
1182            profiling::scope!("vkGetPhysicalDeviceMemoryProperties");
1183            unsafe {
1184                self.instance
1185                    .raw
1186                    .get_physical_device_memory_properties(self.raw)
1187            }
1188        };
1189        let memory_types =
1190            &mem_properties.memory_types[..mem_properties.memory_type_count as usize];
1191        let valid_ash_memory_types = memory_types.iter().enumerate().fold(0, |u, (i, mem)| {
1192            if self.known_memory_flags.contains(mem.property_flags) {
1193                u | (1 << i)
1194            } else {
1195                u
1196            }
1197        });
1198
1199        let swapchain_fn = khr::Swapchain::new(&self.instance.raw, &raw_device);
1200
1201        let indirect_count_fn = if enabled_extensions.contains(&khr::DrawIndirectCount::name()) {
1202            Some(khr::DrawIndirectCount::new(&self.instance.raw, &raw_device))
1203        } else {
1204            None
1205        };
1206        let timeline_semaphore_fn = if enabled_extensions.contains(&khr::TimelineSemaphore::name())
1207        {
1208            Some(super::ExtensionFn::Extension(khr::TimelineSemaphore::new(
1209                &self.instance.raw,
1210                &raw_device,
1211            )))
1212        } else if self.phd_capabilities.device_api_version >= vk::API_VERSION_1_2 {
1213            Some(super::ExtensionFn::Promoted)
1214        } else {
1215            None
1216        };
1217
1218        let naga_options = {
1219            use naga::back::spv;
1220
1221            // The following capabilities are always available
1222            // see https://registry.khronos.org/vulkan/specs/1.3-extensions/html/chap52.html#spirvenv-capabilities
1223            let mut capabilities = vec![
1224                spv::Capability::Shader,
1225                spv::Capability::Matrix,
1226                spv::Capability::Sampled1D,
1227                spv::Capability::Image1D,
1228                spv::Capability::ImageQuery,
1229                spv::Capability::DerivativeControl,
1230                spv::Capability::StorageImageExtendedFormats,
1231            ];
1232
1233            if self
1234                .downlevel_flags
1235                .contains(wgt::DownlevelFlags::CUBE_ARRAY_TEXTURES)
1236            {
1237                capabilities.push(spv::Capability::SampledCubeArray);
1238            }
1239
1240            if self
1241                .downlevel_flags
1242                .contains(wgt::DownlevelFlags::MULTISAMPLED_SHADING)
1243            {
1244                capabilities.push(spv::Capability::SampleRateShading);
1245            }
1246
1247            if features.contains(wgt::Features::MULTIVIEW) {
1248                capabilities.push(spv::Capability::MultiView);
1249            }
1250
1251            if features.contains(wgt::Features::SHADER_PRIMITIVE_INDEX) {
1252                capabilities.push(spv::Capability::Geometry);
1253            }
1254
1255            if features.intersects(
1256                wgt::Features::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING
1257                    | wgt::Features::UNIFORM_BUFFER_AND_STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING,
1258            ) {
1259                capabilities.push(spv::Capability::ShaderNonUniform);
1260            }
1261            if features.contains(wgt::Features::BGRA8UNORM_STORAGE) {
1262                capabilities.push(spv::Capability::StorageImageWriteWithoutFormat);
1263            }
1264
1265            let mut flags = spv::WriterFlags::empty();
1266            flags.set(
1267                spv::WriterFlags::DEBUG,
1268                self.instance.flags.contains(wgt::InstanceFlags::DEBUG),
1269            );
1270            flags.set(
1271                spv::WriterFlags::LABEL_VARYINGS,
1272                self.phd_capabilities.properties.vendor_id != crate::auxil::db::qualcomm::VENDOR,
1273            );
1274            flags.set(
1275                spv::WriterFlags::FORCE_POINT_SIZE,
1276                //Note: we could technically disable this when we are compiling separate entry points,
1277                // and we know exactly that the primitive topology is not `PointList`.
1278                // But this requires cloning the `spv::Options` struct, which has heap allocations.
1279                true, // could check `super::Workarounds::SEPARATE_ENTRY_POINTS`
1280            );
1281            spv::Options {
1282                lang_version: (1, 0),
1283                flags,
1284                capabilities: Some(capabilities.iter().cloned().collect()),
1285                bounds_check_policies: naga::proc::BoundsCheckPolicies {
1286                    index: naga::proc::BoundsCheckPolicy::Restrict,
1287                    buffer: if self.private_caps.robust_buffer_access {
1288                        naga::proc::BoundsCheckPolicy::Unchecked
1289                    } else {
1290                        naga::proc::BoundsCheckPolicy::Restrict
1291                    },
1292                    image_load: if self.private_caps.robust_image_access {
1293                        naga::proc::BoundsCheckPolicy::Unchecked
1294                    } else {
1295                        naga::proc::BoundsCheckPolicy::Restrict
1296                    },
1297                    image_store: naga::proc::BoundsCheckPolicy::Unchecked,
1298                    // TODO: support bounds checks on binding arrays
1299                    binding_array: naga::proc::BoundsCheckPolicy::Unchecked,
1300                },
1301                zero_initialize_workgroup_memory: if self
1302                    .private_caps
1303                    .zero_initialize_workgroup_memory
1304                {
1305                    spv::ZeroInitializeWorkgroupMemoryMode::Native
1306                } else {
1307                    spv::ZeroInitializeWorkgroupMemoryMode::Polyfill
1308                },
1309                // We need to build this separately for each invocation, so just default it out here
1310                binding_map: BTreeMap::default(),
1311                debug_info: None,
1312            }
1313        };
1314
1315        let raw_queue = {
1316            profiling::scope!("vkGetDeviceQueue");
1317            unsafe { raw_device.get_device_queue(family_index, queue_index) }
1318        };
1319
1320        let shared = Arc::new(super::DeviceShared {
1321            raw: raw_device,
1322            family_index,
1323            queue_index,
1324            raw_queue,
1325            handle_is_owned,
1326            instance: Arc::clone(&self.instance),
1327            physical_device: self.raw,
1328            enabled_extensions: enabled_extensions.into(),
1329            extension_fns: super::DeviceExtensionFunctions {
1330                draw_indirect_count: indirect_count_fn,
1331                timeline_semaphore: timeline_semaphore_fn,
1332            },
1333            vendor_id: self.phd_capabilities.properties.vendor_id,
1334            timestamp_period: self.phd_capabilities.properties.limits.timestamp_period,
1335            private_caps: self.private_caps.clone(),
1336            workarounds: self.workarounds,
1337            render_passes: Mutex::new(Default::default()),
1338            framebuffers: Mutex::new(Default::default()),
1339        });
1340        let mut relay_semaphores = [vk::Semaphore::null(); 2];
1341        for sem in relay_semaphores.iter_mut() {
1342            unsafe {
1343                *sem = shared
1344                    .raw
1345                    .create_semaphore(&vk::SemaphoreCreateInfo::builder(), None)?
1346            };
1347        }
1348        let queue = super::Queue {
1349            raw: raw_queue,
1350            swapchain_fn,
1351            device: Arc::clone(&shared),
1352            family_index,
1353            relay_semaphores,
1354            relay_index: None,
1355        };
1356
1357        let mem_allocator = {
1358            let limits = self.phd_capabilities.properties.limits;
1359            let config = gpu_alloc::Config::i_am_prototyping(); //TODO
1360            let max_memory_allocation_size =
1361                if let Some(maintenance_3) = self.phd_capabilities.maintenance_3 {
1362                    maintenance_3.max_memory_allocation_size
1363                } else {
1364                    u64::max_value()
1365                };
1366            let properties = gpu_alloc::DeviceProperties {
1367                max_memory_allocation_count: limits.max_memory_allocation_count,
1368                max_memory_allocation_size,
1369                non_coherent_atom_size: limits.non_coherent_atom_size,
1370                memory_types: memory_types
1371                    .iter()
1372                    .map(|memory_type| gpu_alloc::MemoryType {
1373                        props: gpu_alloc::MemoryPropertyFlags::from_bits_truncate(
1374                            memory_type.property_flags.as_raw() as u8,
1375                        ),
1376                        heap: memory_type.heap_index,
1377                    })
1378                    .collect(),
1379                memory_heaps: mem_properties.memory_heaps
1380                    [..mem_properties.memory_heap_count as usize]
1381                    .iter()
1382                    .map(|&memory_heap| gpu_alloc::MemoryHeap {
1383                        size: memory_heap.size,
1384                    })
1385                    .collect(),
1386                buffer_device_address: false,
1387            };
1388            gpu_alloc::GpuAllocator::new(config, properties)
1389        };
1390        let desc_allocator = gpu_descriptor::DescriptorAllocator::new(
1391            if let Some(di) = self.phd_capabilities.descriptor_indexing {
1392                di.max_update_after_bind_descriptors_in_all_pools
1393            } else {
1394                0
1395            },
1396        );
1397
1398        let device = super::Device {
1399            shared,
1400            mem_allocator: Mutex::new(mem_allocator),
1401            desc_allocator: Mutex::new(desc_allocator),
1402            valid_ash_memory_types,
1403            naga_options,
1404            #[cfg(feature = "renderdoc")]
1405            render_doc: Default::default(),
1406        };
1407
1408        Ok(crate::OpenDevice { device, queue })
1409    }
1410}
1411
1412impl crate::Adapter<super::Api> for super::Adapter {
1413    unsafe fn open(
1414        &self,
1415        features: wgt::Features,
1416        _limits: &wgt::Limits,
1417    ) -> Result<crate::OpenDevice<super::Api>, crate::DeviceError> {
1418        let enabled_extensions = self.required_device_extensions(features);
1419        let mut enabled_phd_features = self.physical_device_features(&enabled_extensions, features);
1420
1421        let family_index = 0; //TODO
1422        let family_info = vk::DeviceQueueCreateInfo::builder()
1423            .queue_family_index(family_index)
1424            .queue_priorities(&[1.0])
1425            .build();
1426        let family_infos = [family_info];
1427
1428        let str_pointers = enabled_extensions
1429            .iter()
1430            .map(|&s| {
1431                // Safe because `enabled_extensions` entries have static lifetime.
1432                s.as_ptr()
1433            })
1434            .collect::<Vec<_>>();
1435
1436        let pre_info = vk::DeviceCreateInfo::builder()
1437            .queue_create_infos(&family_infos)
1438            .enabled_extension_names(&str_pointers);
1439        let info = enabled_phd_features
1440            .add_to_device_create_builder(pre_info)
1441            .build();
1442        let raw_device = {
1443            profiling::scope!("vkCreateDevice");
1444            unsafe { self.instance.raw.create_device(self.raw, &info, None)? }
1445        };
1446
1447        unsafe {
1448            self.device_from_raw(
1449                raw_device,
1450                true,
1451                &enabled_extensions,
1452                features,
1453                family_info.queue_family_index,
1454                0,
1455            )
1456        }
1457    }
1458
1459    unsafe fn texture_format_capabilities(
1460        &self,
1461        format: wgt::TextureFormat,
1462    ) -> crate::TextureFormatCapabilities {
1463        use crate::TextureFormatCapabilities as Tfc;
1464
1465        let vk_format = self.private_caps.map_texture_format(format);
1466        let properties = unsafe {
1467            self.instance
1468                .raw
1469                .get_physical_device_format_properties(self.raw, vk_format)
1470        };
1471        let features = properties.optimal_tiling_features;
1472
1473        let mut flags = Tfc::empty();
1474        flags.set(
1475            Tfc::SAMPLED,
1476            features.contains(vk::FormatFeatureFlags::SAMPLED_IMAGE),
1477        );
1478        flags.set(
1479            Tfc::SAMPLED_LINEAR,
1480            features.contains(vk::FormatFeatureFlags::SAMPLED_IMAGE_FILTER_LINEAR),
1481        );
1482        // flags.set(
1483        //     Tfc::SAMPLED_MINMAX,
1484        //     features.contains(vk::FormatFeatureFlags::SAMPLED_IMAGE_FILTER_MINMAX),
1485        // );
1486        flags.set(
1487            Tfc::STORAGE | Tfc::STORAGE_READ_WRITE,
1488            features.contains(vk::FormatFeatureFlags::STORAGE_IMAGE),
1489        );
1490        flags.set(
1491            Tfc::STORAGE_ATOMIC,
1492            features.contains(vk::FormatFeatureFlags::STORAGE_IMAGE_ATOMIC),
1493        );
1494        flags.set(
1495            Tfc::COLOR_ATTACHMENT,
1496            features.contains(vk::FormatFeatureFlags::COLOR_ATTACHMENT),
1497        );
1498        flags.set(
1499            Tfc::COLOR_ATTACHMENT_BLEND,
1500            features.contains(vk::FormatFeatureFlags::COLOR_ATTACHMENT_BLEND),
1501        );
1502        flags.set(
1503            Tfc::DEPTH_STENCIL_ATTACHMENT,
1504            features.contains(vk::FormatFeatureFlags::DEPTH_STENCIL_ATTACHMENT),
1505        );
1506        flags.set(
1507            Tfc::COPY_SRC,
1508            features.intersects(vk::FormatFeatureFlags::TRANSFER_SRC),
1509        );
1510        flags.set(
1511            Tfc::COPY_DST,
1512            features.intersects(vk::FormatFeatureFlags::TRANSFER_DST),
1513        );
1514        // Vulkan is very permissive about MSAA
1515        flags.set(Tfc::MULTISAMPLE_RESOLVE, !format.is_compressed());
1516
1517        // get the supported sample counts
1518        let format_aspect = crate::FormatAspects::from(format);
1519        let limits = self.phd_capabilities.properties.limits;
1520
1521        let sample_flags = if format_aspect.contains(crate::FormatAspects::DEPTH) {
1522            limits
1523                .framebuffer_depth_sample_counts
1524                .min(limits.sampled_image_depth_sample_counts)
1525        } else if format_aspect.contains(crate::FormatAspects::STENCIL) {
1526            limits
1527                .framebuffer_stencil_sample_counts
1528                .min(limits.sampled_image_stencil_sample_counts)
1529        } else {
1530            match format.sample_type(None).unwrap() {
1531                wgt::TextureSampleType::Float { filterable: _ } => limits
1532                    .framebuffer_color_sample_counts
1533                    .min(limits.sampled_image_color_sample_counts),
1534                wgt::TextureSampleType::Sint | wgt::TextureSampleType::Uint => {
1535                    limits.sampled_image_integer_sample_counts
1536                }
1537                _ => unreachable!(),
1538            }
1539        };
1540
1541        flags.set(
1542            Tfc::MULTISAMPLE_X2,
1543            sample_flags.contains(vk::SampleCountFlags::TYPE_2),
1544        );
1545        flags.set(
1546            Tfc::MULTISAMPLE_X4,
1547            sample_flags.contains(vk::SampleCountFlags::TYPE_4),
1548        );
1549        flags.set(
1550            Tfc::MULTISAMPLE_X8,
1551            sample_flags.contains(vk::SampleCountFlags::TYPE_8),
1552        );
1553        flags.set(
1554            Tfc::MULTISAMPLE_X16,
1555            sample_flags.contains(vk::SampleCountFlags::TYPE_16),
1556        );
1557
1558        flags
1559    }
1560
1561    unsafe fn surface_capabilities(
1562        &self,
1563        surface: &super::Surface,
1564    ) -> Option<crate::SurfaceCapabilities> {
1565        if !self.private_caps.can_present {
1566            return None;
1567        }
1568        let queue_family_index = 0; //TODO
1569        {
1570            profiling::scope!("vkGetPhysicalDeviceSurfaceSupportKHR");
1571            match unsafe {
1572                surface.functor.get_physical_device_surface_support(
1573                    self.raw,
1574                    queue_family_index,
1575                    surface.raw,
1576                )
1577            } {
1578                Ok(true) => (),
1579                Ok(false) => return None,
1580                Err(e) => {
1581                    log::error!("get_physical_device_surface_support: {}", e);
1582                    return None;
1583                }
1584            }
1585        }
1586
1587        let caps = {
1588            profiling::scope!("vkGetPhysicalDeviceSurfaceCapabilitiesKHR");
1589            match unsafe {
1590                surface
1591                    .functor
1592                    .get_physical_device_surface_capabilities(self.raw, surface.raw)
1593            } {
1594                Ok(caps) => caps,
1595                Err(e) => {
1596                    log::error!("get_physical_device_surface_capabilities: {}", e);
1597                    return None;
1598                }
1599            }
1600        };
1601
1602        // If image count is 0, the support number of images is unlimited.
1603        let max_image_count = if caps.max_image_count == 0 {
1604            !0
1605        } else {
1606            caps.max_image_count
1607        };
1608
1609        // `0xFFFFFFFF` indicates that the extent depends on the created swapchain.
1610        let current_extent = if caps.current_extent.width != !0 && caps.current_extent.height != !0
1611        {
1612            Some(wgt::Extent3d {
1613                width: caps.current_extent.width,
1614                height: caps.current_extent.height,
1615                depth_or_array_layers: 1,
1616            })
1617        } else {
1618            None
1619        };
1620
1621        let min_extent = wgt::Extent3d {
1622            width: caps.min_image_extent.width,
1623            height: caps.min_image_extent.height,
1624            depth_or_array_layers: 1,
1625        };
1626
1627        let max_extent = wgt::Extent3d {
1628            width: caps.max_image_extent.width,
1629            height: caps.max_image_extent.height,
1630            depth_or_array_layers: caps.max_image_array_layers,
1631        };
1632
1633        let raw_present_modes = {
1634            profiling::scope!("vkGetPhysicalDeviceSurfacePresentModesKHR");
1635            match unsafe {
1636                surface
1637                    .functor
1638                    .get_physical_device_surface_present_modes(self.raw, surface.raw)
1639            } {
1640                Ok(present_modes) => present_modes,
1641                Err(e) => {
1642                    log::error!("get_physical_device_surface_present_modes: {}", e);
1643                    Vec::new()
1644                }
1645            }
1646        };
1647
1648        let raw_surface_formats = {
1649            profiling::scope!("vkGetPhysicalDeviceSurfaceFormatsKHR");
1650            match unsafe {
1651                surface
1652                    .functor
1653                    .get_physical_device_surface_formats(self.raw, surface.raw)
1654            } {
1655                Ok(formats) => formats,
1656                Err(e) => {
1657                    log::error!("get_physical_device_surface_formats: {}", e);
1658                    Vec::new()
1659                }
1660            }
1661        };
1662
1663        let formats = raw_surface_formats
1664            .into_iter()
1665            .filter_map(conv::map_vk_surface_formats)
1666            .collect();
1667        Some(crate::SurfaceCapabilities {
1668            formats,
1669            swap_chain_sizes: caps.min_image_count..=max_image_count,
1670            current_extent,
1671            extents: min_extent..=max_extent,
1672            usage: conv::map_vk_image_usage(caps.supported_usage_flags),
1673            present_modes: raw_present_modes
1674                .into_iter()
1675                .flat_map(conv::map_vk_present_mode)
1676                .collect(),
1677            composite_alpha_modes: conv::map_vk_composite_alpha(caps.supported_composite_alpha),
1678        })
1679    }
1680
1681    unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp {
1682        // VK_GOOGLE_display_timing is the only way to get presentation
1683        // timestamps on vulkan right now and it is only ever available
1684        // on android and linux. This includes mac, but there's no alternative
1685        // on mac, so this is fine.
1686        #[cfg(unix)]
1687        {
1688            let mut timespec = libc::timespec {
1689                tv_sec: 0,
1690                tv_nsec: 0,
1691            };
1692            unsafe {
1693                libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut timespec);
1694            }
1695
1696            wgt::PresentationTimestamp(
1697                timespec.tv_sec as u128 * 1_000_000_000 + timespec.tv_nsec as u128,
1698            )
1699        }
1700        #[cfg(not(unix))]
1701        {
1702            wgt::PresentationTimestamp::INVALID_TIMESTAMP
1703        }
1704    }
1705}
1706
1707fn is_format_16bit_norm_supported(instance: &ash::Instance, phd: vk::PhysicalDevice) -> bool {
1708    let tiling = vk::ImageTiling::OPTIMAL;
1709    let features = vk::FormatFeatureFlags::SAMPLED_IMAGE
1710        | vk::FormatFeatureFlags::STORAGE_IMAGE
1711        | vk::FormatFeatureFlags::TRANSFER_SRC
1712        | vk::FormatFeatureFlags::TRANSFER_DST;
1713    let r16unorm = supports_format(instance, phd, vk::Format::R16_UNORM, tiling, features);
1714    let r16snorm = supports_format(instance, phd, vk::Format::R16_SNORM, tiling, features);
1715    let rg16unorm = supports_format(instance, phd, vk::Format::R16G16_UNORM, tiling, features);
1716    let rg16snorm = supports_format(instance, phd, vk::Format::R16G16_SNORM, tiling, features);
1717    let rgba16unorm = supports_format(
1718        instance,
1719        phd,
1720        vk::Format::R16G16B16A16_UNORM,
1721        tiling,
1722        features,
1723    );
1724    let rgba16snorm = supports_format(
1725        instance,
1726        phd,
1727        vk::Format::R16G16B16A16_SNORM,
1728        tiling,
1729        features,
1730    );
1731
1732    r16unorm && r16snorm && rg16unorm && rg16snorm && rgba16unorm && rgba16snorm
1733}
1734
1735fn supports_format(
1736    instance: &ash::Instance,
1737    phd: vk::PhysicalDevice,
1738    format: vk::Format,
1739    tiling: vk::ImageTiling,
1740    features: vk::FormatFeatureFlags,
1741) -> bool {
1742    let properties = unsafe { instance.get_physical_device_format_properties(phd, format) };
1743    match tiling {
1744        vk::ImageTiling::LINEAR => properties.linear_tiling_features.contains(features),
1745        vk::ImageTiling::OPTIMAL => properties.optimal_tiling_features.contains(features),
1746        _ => false,
1747    }
1748}
1749
1750fn supports_bgra8unorm_storage(
1751    instance: &ash::Instance,
1752    phd: vk::PhysicalDevice,
1753    device_api_version: u32,
1754) -> bool {
1755    // See https://github.com/KhronosGroup/Vulkan-Docs/issues/2027#issuecomment-1380608011
1756
1757    // This check gates the function call and structures used below.
1758    // TODO: check for (`VK_KHR_get_physical_device_properties2` or VK1.1) and (`VK_KHR_format_feature_flags2` or VK1.3).
1759    // Right now we only check for VK1.3.
1760    if device_api_version < vk::API_VERSION_1_3 {
1761        return false;
1762    }
1763
1764    unsafe {
1765        let mut properties3 = vk::FormatProperties3::default();
1766        let mut properties2 = vk::FormatProperties2::builder().push_next(&mut properties3);
1767
1768        instance.get_physical_device_format_properties2(
1769            phd,
1770            vk::Format::B8G8R8A8_UNORM,
1771            &mut properties2,
1772        );
1773
1774        let features2 = properties2.format_properties.optimal_tiling_features;
1775        let features3 = properties3.optimal_tiling_features;
1776
1777        features2.contains(vk::FormatFeatureFlags::STORAGE_IMAGE)
1778            && features3.contains(vk::FormatFeatureFlags2::STORAGE_WRITE_WITHOUT_FORMAT)
1779    }
1780}