[wgpu-hal] Upgrade to ash 0.38 (#5504)

This commit is contained in:
Marijn Suijten 2024-05-13 17:57:44 +02:00 committed by GitHub
parent ca91744955
commit 8879733875
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 607 additions and 714 deletions

View File

@ -257,6 +257,7 @@ By @atlv24 and @cwfitzgerald in [#5154](https://github.com/gfx-rs/wgpu/pull/5154
- Set object labels when the DEBUG flag is set, even if the VALIDATION flag is disabled. By @DJMcNab in [#5345](https://github.com/gfx-rs/wgpu/pull/5345). - Set object labels when the DEBUG flag is set, even if the VALIDATION flag is disabled. By @DJMcNab in [#5345](https://github.com/gfx-rs/wgpu/pull/5345).
- Add safety check to `wgpu_hal::vulkan::CommandEncoder` to make sure `discard_encoding` is not called in the closed state. By @villuna in [#5557](https://github.com/gfx-rs/wgpu/pull/5557) - Add safety check to `wgpu_hal::vulkan::CommandEncoder` to make sure `discard_encoding` is not called in the closed state. By @villuna in [#5557](https://github.com/gfx-rs/wgpu/pull/5557)
- Fix SPIR-V type capability requests to not depend on `LocalType` caching. By @atlv24 in [#5590](https://github.com/gfx-rs/wgpu/pull/5590) - Fix SPIR-V type capability requests to not depend on `LocalType` caching. By @atlv24 in [#5590](https://github.com/gfx-rs/wgpu/pull/5590)
- Upgrade `ash` to `0.38`. By @MarijnS95 in [#5504](https://github.com/gfx-rs/wgpu/pull/5504).
#### Tests #### Tests

6
Cargo.lock generated
View File

@ -227,11 +227,11 @@ checksum = "175571dd1d178ced59193a6fc02dde1b972eb0bc56c892cde9beeceac5bf0f6b"
[[package]] [[package]]
name = "ash" name = "ash"
version = "0.37.3+1.3.251" version = "0.38.0+1.3.281"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39e9c3835d686b0a6084ab4234fcd1b07dbf6e4767dce60874b12356a25ecd4a" checksum = "0bb44936d800fea8f016d7f2311c6a4f97aebd5dc86f09906139ec848cf3a46f"
dependencies = [ dependencies = [
"libloading 0.7.4", "libloading 0.8.3",
] ]
[[package]] [[package]]

View File

@ -133,7 +133,7 @@ objc = "0.2.5"
# Vulkan dependencies # Vulkan dependencies
android_system_properties = "0.1.1" android_system_properties = "0.1.1"
ash = "0.37.3" ash = "0.38.0"
gpu-alloc = "0.6" gpu-alloc = "0.6"
gpu-descriptor = "0.3" gpu-descriptor = "0.3"

View File

@ -55,6 +55,7 @@ gles = [
"dep:khronos-egl", "dep:khronos-egl",
"dep:libloading", "dep:libloading",
"dep:ndk-sys", "dep:ndk-sys",
"winapi/libloaderapi",
] ]
dx12 = [ dx12 = [
"naga/hlsl-out", "naga/hlsl-out",
@ -68,6 +69,7 @@ dx12 = [
"winapi/d3d12shader", "winapi/d3d12shader",
"winapi/d3d12sdklayers", "winapi/d3d12sdklayers",
"winapi/dxgi1_6", "winapi/dxgi1_6",
"winapi/errhandlingapi",
] ]
# TODO: This is a separate feature until Mozilla okays windows-rs, see https://github.com/gfx-rs/wgpu/issues/3207 for the tracking issue. # TODO: This is a separate feature until Mozilla okays windows-rs, see https://github.com/gfx-rs/wgpu/issues/3207 for the tracking issue.
windows_rs = ["dep:gpu-allocator"] windows_rs = ["dep:gpu-allocator"]
@ -118,7 +120,7 @@ version = "0.20.0"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies] [target.'cfg(not(target_arch = "wasm32"))'.dependencies]
# backend: Vulkan # backend: Vulkan
ash = { version = "0.37.3", optional = true } ash = { version = "0.38.0", optional = true }
gpu-alloc = { version = "0.6", optional = true } gpu-alloc = { version = "0.6", optional = true }
gpu-descriptor = { version = "0.3", optional = true } gpu-descriptor = { version = "0.3", optional = true }
smallvec = { version = "1", optional = true, features = ["union"] } smallvec = { version = "1", optional = true, features = ["union"] }
@ -146,7 +148,6 @@ glutin_wgl_sys = { version = "0.5", optional = true }
winapi = { version = "0.3", features = [ winapi = { version = "0.3", features = [
"profileapi", "profileapi",
"libloaderapi",
"windef", "windef",
"winuser", "winuser",
"dcomp", "dcomp",

View File

@ -115,24 +115,3 @@ impl crate::TextureCopy {
self.size = self.size.min(&max_src_size).min(&max_dst_size); self.size = self.size.min(&max_src_size).min(&max_dst_size);
} }
} }
/// Construct a `CStr` from a byte slice, up to the first zero byte.
///
/// Return a `CStr` extending from the start of `bytes` up to and
/// including the first zero byte. If there is no zero byte in
/// `bytes`, return `None`.
///
/// This can be removed when `CStr::from_bytes_until_nul` is stabilized.
/// ([#95027](https://github.com/rust-lang/rust/issues/95027))
#[allow(dead_code)]
pub(crate) fn cstr_from_bytes_until_nul(bytes: &[std::os::raw::c_char]) -> Option<&std::ffi::CStr> {
if bytes.contains(&0) {
// Safety for `CStr::from_ptr`:
// - We've ensured that the slice does contain a null terminator.
// - The range is valid to read, because the slice covers it.
// - The memory won't be changed, because the slice borrows it.
unsafe { Some(std::ffi::CStr::from_ptr(bytes.as_ptr())) }
} else {
None
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
use super::conv; use super::conv;
use arrayvec::ArrayVec; use arrayvec::ArrayVec;
use ash::{extensions::ext, vk}; use ash::vk;
use std::{mem, ops::Range, slice}; use std::{mem, ops::Range, slice};
@ -39,12 +39,6 @@ impl super::Texture {
} }
} }
impl super::DeviceShared {
fn debug_messenger(&self) -> Option<&ext::DebugUtils> {
Some(&self.instance.debug_utils.as_ref()?.extension)
}
}
impl super::CommandEncoder { impl super::CommandEncoder {
fn write_pass_end_timestamp_if_requested(&mut self) { fn write_pass_end_timestamp_if_requested(&mut self) {
if let Some((query_set, index)) = self.end_of_pass_timer_query.take() { if let Some((query_set, index)) = self.end_of_pass_timer_query.take() {
@ -65,10 +59,9 @@ impl crate::CommandEncoder for super::CommandEncoder {
unsafe fn begin_encoding(&mut self, label: crate::Label) -> Result<(), crate::DeviceError> { unsafe fn begin_encoding(&mut self, label: crate::Label) -> Result<(), crate::DeviceError> {
if self.free.is_empty() { if self.free.is_empty() {
let vk_info = vk::CommandBufferAllocateInfo::builder() let vk_info = vk::CommandBufferAllocateInfo::default()
.command_pool(self.raw) .command_pool(self.raw)
.command_buffer_count(ALLOCATION_GRANULARITY) .command_buffer_count(ALLOCATION_GRANULARITY);
.build();
let cmd_buf_vec = unsafe { self.device.raw.allocate_command_buffers(&vk_info)? }; let cmd_buf_vec = unsafe { self.device.raw.allocate_command_buffers(&vk_info)? };
self.free.extend(cmd_buf_vec); self.free.extend(cmd_buf_vec);
} }
@ -76,20 +69,13 @@ impl crate::CommandEncoder for super::CommandEncoder {
// Set the name unconditionally, since there might be a // Set the name unconditionally, since there might be a
// previous name assigned to this. // previous name assigned to this.
unsafe { unsafe { self.device.set_object_name(raw, label.unwrap_or_default()) };
self.device.set_object_name(
vk::ObjectType::COMMAND_BUFFER,
raw,
label.unwrap_or_default(),
)
};
// Reset this in case the last renderpass was never ended. // Reset this in case the last renderpass was never ended.
self.rpass_debug_marker_active = false; self.rpass_debug_marker_active = false;
let vk_info = vk::CommandBufferBeginInfo::builder() let vk_info = vk::CommandBufferBeginInfo::default()
.flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT) .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT);
.build();
unsafe { self.device.raw.begin_command_buffer(raw, &vk_info) }?; unsafe { self.device.raw.begin_command_buffer(raw, &vk_info) }?;
self.active = raw; self.active = raw;
@ -145,12 +131,11 @@ impl crate::CommandEncoder for super::CommandEncoder {
dst_stages |= dst_stage; dst_stages |= dst_stage;
vk_barriers.push( vk_barriers.push(
vk::BufferMemoryBarrier::builder() vk::BufferMemoryBarrier::default()
.buffer(bar.buffer.raw) .buffer(bar.buffer.raw)
.size(vk::WHOLE_SIZE) .size(vk::WHOLE_SIZE)
.src_access_mask(src_access) .src_access_mask(src_access)
.dst_access_mask(dst_access) .dst_access_mask(dst_access),
.build(),
) )
} }
@ -192,14 +177,13 @@ impl crate::CommandEncoder for super::CommandEncoder {
dst_stages |= dst_stage; dst_stages |= dst_stage;
vk_barriers.push( vk_barriers.push(
vk::ImageMemoryBarrier::builder() vk::ImageMemoryBarrier::default()
.image(bar.texture.raw) .image(bar.texture.raw)
.subresource_range(range) .subresource_range(range)
.src_access_mask(src_access) .src_access_mask(src_access)
.dst_access_mask(dst_access) .dst_access_mask(dst_access)
.old_layout(src_layout) .old_layout(src_layout)
.new_layout(dst_layout) .new_layout(dst_layout),
.build(),
); );
} }
@ -442,7 +426,7 @@ impl crate::CommandEncoder for super::CommandEncoder {
Some(buffer) => ray_tracing_functions Some(buffer) => ray_tracing_functions
.buffer_device_address .buffer_device_address
.get_buffer_device_address( .get_buffer_device_address(
&vk::BufferDeviceAddressInfo::builder().buffer(buffer.raw), &vk::BufferDeviceAddressInfo::default().buffer(buffer.raw),
), ),
None => panic!("Buffers are required to build acceleration structures"), None => panic!("Buffers are required to build acceleration structures"),
} }
@ -469,23 +453,24 @@ impl crate::CommandEncoder for super::CommandEncoder {
for desc in descriptors { for desc in descriptors {
let (geometries, ranges) = match *desc.entries { let (geometries, ranges) = match *desc.entries {
crate::AccelerationStructureEntries::Instances(ref instances) => { crate::AccelerationStructureEntries::Instances(ref instances) => {
let instance_data = vk::AccelerationStructureGeometryInstancesDataKHR::builder( let instance_data = vk::AccelerationStructureGeometryInstancesDataKHR::default(
// TODO: Code is so large that rustfmt refuses to treat this... :(
) )
.data(vk::DeviceOrHostAddressConstKHR { .data(vk::DeviceOrHostAddressConstKHR {
device_address: get_device_address(instances.buffer), device_address: get_device_address(instances.buffer),
}); });
let geometry = vk::AccelerationStructureGeometryKHR::builder() let geometry = vk::AccelerationStructureGeometryKHR::default()
.geometry_type(vk::GeometryTypeKHR::INSTANCES) .geometry_type(vk::GeometryTypeKHR::INSTANCES)
.geometry(vk::AccelerationStructureGeometryDataKHR { .geometry(vk::AccelerationStructureGeometryDataKHR {
instances: *instance_data, instances: instance_data,
}); });
let range = vk::AccelerationStructureBuildRangeInfoKHR::builder() let range = vk::AccelerationStructureBuildRangeInfoKHR::default()
.primitive_count(instances.count) .primitive_count(instances.count)
.primitive_offset(instances.offset); .primitive_offset(instances.offset);
(smallvec::smallvec![*geometry], smallvec::smallvec![*range]) (smallvec::smallvec![geometry], smallvec::smallvec![range])
} }
crate::AccelerationStructureEntries::Triangles(ref in_geometries) => { crate::AccelerationStructureEntries::Triangles(ref in_geometries) => {
let mut ranges = smallvec::SmallVec::< let mut ranges = smallvec::SmallVec::<
@ -496,7 +481,7 @@ impl crate::CommandEncoder for super::CommandEncoder {
>::with_capacity(in_geometries.len()); >::with_capacity(in_geometries.len());
for triangles in in_geometries { for triangles in in_geometries {
let mut triangle_data = let mut triangle_data =
vk::AccelerationStructureGeometryTrianglesDataKHR::builder() vk::AccelerationStructureGeometryTrianglesDataKHR::default()
.vertex_data(vk::DeviceOrHostAddressConstKHR { .vertex_data(vk::DeviceOrHostAddressConstKHR {
device_address: get_device_address(triangles.vertex_buffer), device_address: get_device_address(triangles.vertex_buffer),
}) })
@ -504,7 +489,7 @@ impl crate::CommandEncoder for super::CommandEncoder {
.max_vertex(triangles.vertex_count) .max_vertex(triangles.vertex_count)
.vertex_stride(triangles.vertex_stride); .vertex_stride(triangles.vertex_stride);
let mut range = vk::AccelerationStructureBuildRangeInfoKHR::builder(); let mut range = vk::AccelerationStructureBuildRangeInfoKHR::default();
if let Some(ref indices) = triangles.indices { if let Some(ref indices) = triangles.indices {
triangle_data = triangle_data triangle_data = triangle_data
@ -528,7 +513,7 @@ impl crate::CommandEncoder for super::CommandEncoder {
ray_tracing_functions ray_tracing_functions
.buffer_device_address .buffer_device_address
.get_buffer_device_address( .get_buffer_device_address(
&vk::BufferDeviceAddressInfo::builder() &vk::BufferDeviceAddressInfo::default()
.buffer(transform.buffer.raw), .buffer(transform.buffer.raw),
) )
}; };
@ -540,17 +525,17 @@ impl crate::CommandEncoder for super::CommandEncoder {
range = range.transform_offset(transform.offset); range = range.transform_offset(transform.offset);
} }
let geometry = vk::AccelerationStructureGeometryKHR::builder() let geometry = vk::AccelerationStructureGeometryKHR::default()
.geometry_type(vk::GeometryTypeKHR::TRIANGLES) .geometry_type(vk::GeometryTypeKHR::TRIANGLES)
.geometry(vk::AccelerationStructureGeometryDataKHR { .geometry(vk::AccelerationStructureGeometryDataKHR {
triangles: *triangle_data, triangles: triangle_data,
}) })
.flags(conv::map_acceleration_structure_geometry_flags( .flags(conv::map_acceleration_structure_geometry_flags(
triangles.flags, triangles.flags,
)); ));
geometries.push(*geometry); geometries.push(geometry);
ranges.push(*range); ranges.push(range);
} }
(geometries, ranges) (geometries, ranges)
} }
@ -562,25 +547,25 @@ impl crate::CommandEncoder for super::CommandEncoder {
[vk::AccelerationStructureGeometryKHR; CAPACITY_INNER], [vk::AccelerationStructureGeometryKHR; CAPACITY_INNER],
>::with_capacity(in_geometries.len()); >::with_capacity(in_geometries.len());
for aabb in in_geometries { for aabb in in_geometries {
let aabbs_data = vk::AccelerationStructureGeometryAabbsDataKHR::builder() let aabbs_data = vk::AccelerationStructureGeometryAabbsDataKHR::default()
.data(vk::DeviceOrHostAddressConstKHR { .data(vk::DeviceOrHostAddressConstKHR {
device_address: get_device_address(aabb.buffer), device_address: get_device_address(aabb.buffer),
}) })
.stride(aabb.stride); .stride(aabb.stride);
let range = vk::AccelerationStructureBuildRangeInfoKHR::builder() let range = vk::AccelerationStructureBuildRangeInfoKHR::default()
.primitive_count(aabb.count) .primitive_count(aabb.count)
.primitive_offset(aabb.offset); .primitive_offset(aabb.offset);
let geometry = vk::AccelerationStructureGeometryKHR::builder() let geometry = vk::AccelerationStructureGeometryKHR::default()
.geometry_type(vk::GeometryTypeKHR::AABBS) .geometry_type(vk::GeometryTypeKHR::AABBS)
.geometry(vk::AccelerationStructureGeometryDataKHR { .geometry(vk::AccelerationStructureGeometryDataKHR {
aabbs: *aabbs_data, aabbs: aabbs_data,
}) })
.flags(conv::map_acceleration_structure_geometry_flags(aabb.flags)); .flags(conv::map_acceleration_structure_geometry_flags(aabb.flags));
geometries.push(*geometry); geometries.push(geometry);
ranges.push(*range); ranges.push(range);
} }
(geometries, ranges) (geometries, ranges)
} }
@ -593,7 +578,7 @@ impl crate::CommandEncoder for super::CommandEncoder {
ray_tracing_functions ray_tracing_functions
.buffer_device_address .buffer_device_address
.get_buffer_device_address( .get_buffer_device_address(
&vk::BufferDeviceAddressInfo::builder().buffer(desc.scratch_buffer.raw), &vk::BufferDeviceAddressInfo::default().buffer(desc.scratch_buffer.raw),
) )
}; };
let ty = match *desc.entries { let ty = match *desc.entries {
@ -602,7 +587,7 @@ impl crate::CommandEncoder for super::CommandEncoder {
} }
_ => vk::AccelerationStructureTypeKHR::BOTTOM_LEVEL, _ => vk::AccelerationStructureTypeKHR::BOTTOM_LEVEL,
}; };
let mut geometry_info = vk::AccelerationStructureBuildGeometryInfoKHR::builder() let mut geometry_info = vk::AccelerationStructureBuildGeometryInfoKHR::default()
.ty(ty) .ty(ty)
.mode(conv::map_acceleration_structure_build_mode(desc.mode)) .mode(conv::map_acceleration_structure_build_mode(desc.mode))
.flags(conv::map_acceleration_structure_flags(desc.flags)) .flags(conv::map_acceleration_structure_flags(desc.flags))
@ -618,7 +603,7 @@ impl crate::CommandEncoder for super::CommandEncoder {
.raw; .raw;
} }
geometry_infos.push(*geometry_info); geometry_infos.push(geometry_info);
} }
for (i, geometry_info) in geometry_infos.iter_mut().enumerate() { for (i, geometry_info) in geometry_infos.iter_mut().enumerate() {
@ -649,10 +634,9 @@ impl crate::CommandEncoder for super::CommandEncoder {
src_stage | vk::PipelineStageFlags::TOP_OF_PIPE, src_stage | vk::PipelineStageFlags::TOP_OF_PIPE,
dst_stage | vk::PipelineStageFlags::BOTTOM_OF_PIPE, dst_stage | vk::PipelineStageFlags::BOTTOM_OF_PIPE,
vk::DependencyFlags::empty(), vk::DependencyFlags::empty(),
&[vk::MemoryBarrier::builder() &[vk::MemoryBarrier::default()
.src_access_mask(src_access) .src_access_mask(src_access)
.dst_access_mask(dst_access) .dst_access_mask(dst_access)],
.build()],
&[], &[],
&[], &[],
) )
@ -754,17 +738,13 @@ impl crate::CommandEncoder for super::CommandEncoder {
.make_framebuffer(fb_key, raw_pass, desc.label) .make_framebuffer(fb_key, raw_pass, desc.label)
.unwrap(); .unwrap();
let mut vk_info = vk::RenderPassBeginInfo::builder() let mut vk_info = vk::RenderPassBeginInfo::default()
.render_pass(raw_pass) .render_pass(raw_pass)
.render_area(render_area) .render_area(render_area)
.clear_values(&vk_clear_values) .clear_values(&vk_clear_values)
.framebuffer(raw_framebuffer); .framebuffer(raw_framebuffer);
let mut vk_attachment_info = if caps.imageless_framebuffers { let mut vk_attachment_info = if caps.imageless_framebuffers {
Some( Some(vk::RenderPassAttachmentBeginInfo::default().attachments(&vk_image_views))
vk::RenderPassAttachmentBeginInfo::builder()
.attachments(&vk_image_views)
.build(),
)
} else { } else {
None None
}; };
@ -859,21 +839,21 @@ impl crate::CommandEncoder for super::CommandEncoder {
} }
unsafe fn insert_debug_marker(&mut self, label: &str) { unsafe fn insert_debug_marker(&mut self, label: &str) {
if let Some(ext) = self.device.debug_messenger() { if let Some(ext) = self.device.extension_fns.debug_utils.as_ref() {
let cstr = self.temp.make_c_str(label); let cstr = self.temp.make_c_str(label);
let vk_label = vk::DebugUtilsLabelEXT::builder().label_name(cstr).build(); let vk_label = vk::DebugUtilsLabelEXT::default().label_name(cstr);
unsafe { ext.cmd_insert_debug_utils_label(self.active, &vk_label) }; unsafe { ext.cmd_insert_debug_utils_label(self.active, &vk_label) };
} }
} }
unsafe fn begin_debug_marker(&mut self, group_label: &str) { unsafe fn begin_debug_marker(&mut self, group_label: &str) {
if let Some(ext) = self.device.debug_messenger() { if let Some(ext) = self.device.extension_fns.debug_utils.as_ref() {
let cstr = self.temp.make_c_str(group_label); let cstr = self.temp.make_c_str(group_label);
let vk_label = vk::DebugUtilsLabelEXT::builder().label_name(cstr).build(); let vk_label = vk::DebugUtilsLabelEXT::default().label_name(cstr);
unsafe { ext.cmd_begin_debug_utils_label(self.active, &vk_label) }; unsafe { ext.cmd_begin_debug_utils_label(self.active, &vk_label) };
} }
} }
unsafe fn end_debug_marker(&mut self) { unsafe fn end_debug_marker(&mut self) {
if let Some(ext) = self.device.debug_messenger() { if let Some(ext) = self.device.extension_fns.debug_utils.as_ref() {
unsafe { ext.cmd_end_debug_utils_label(self.active) }; unsafe { ext.cmd_end_debug_utils_label(self.active) };
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -7,10 +7,7 @@ use std::{
}; };
use arrayvec::ArrayVec; use arrayvec::ArrayVec;
use ash::{ use ash::{ext, khr, vk};
extensions::{ext, khr},
vk,
};
use parking_lot::RwLock; use parking_lot::RwLock;
unsafe extern "system" fn debug_utils_messenger_callback( unsafe extern "system" fn debug_utils_messenger_callback(
@ -33,10 +30,10 @@ unsafe extern "system" fn debug_utils_messenger_callback(
// https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/5671 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/5671
// Versions 1.3.240 through 1.3.250 return a spurious error here if // Versions 1.3.240 through 1.3.250 return a spurious error here if
// the debug range start and end appear in different command buffers. // the debug range start and end appear in different command buffers.
let khronos_validation_layer = const KHRONOS_VALIDATION_LAYER: &CStr =
CStr::from_bytes_with_nul(b"Khronos Validation Layer\0").unwrap(); unsafe { CStr::from_bytes_with_nul_unchecked(b"Khronos Validation Layer\0") };
if let Some(layer_properties) = user_data.validation_layer_properties.as_ref() { if let Some(layer_properties) = user_data.validation_layer_properties.as_ref() {
if layer_properties.layer_description.as_ref() == khronos_validation_layer if layer_properties.layer_description.as_ref() == KHRONOS_VALIDATION_LAYER
&& layer_properties.layer_spec_version >= vk::make_api_version(0, 1, 3, 240) && layer_properties.layer_spec_version >= vk::make_api_version(0, 1, 3, 240)
&& layer_properties.layer_spec_version <= vk::make_api_version(0, 1, 3, 250) && layer_properties.layer_spec_version <= vk::make_api_version(0, 1, 3, 250)
{ {
@ -47,7 +44,7 @@ unsafe extern "system" fn debug_utils_messenger_callback(
// Silence Vulkan Validation error "VUID-VkSwapchainCreateInfoKHR-pNext-07781" // Silence Vulkan Validation error "VUID-VkSwapchainCreateInfoKHR-pNext-07781"
// This happens when a surface is configured with a size outside the allowed extent. // This happens when a surface is configured with a size outside the allowed extent.
// It's s false positive due to the inherent racy-ness of surface resizing. // It's a false positive due to the inherent racy-ness of surface resizing.
const VUID_VKSWAPCHAINCREATEINFOKHR_PNEXT_07781: i32 = 0x4c8929c1; const VUID_VKSWAPCHAINCREATEINFOKHR_PNEXT_07781: i32 = 0x4c8929c1;
if cd.message_id_number == VUID_VKSWAPCHAINCREATEINFOKHR_PNEXT_07781 { if cd.message_id_number == VUID_VKSWAPCHAINCREATEINFOKHR_PNEXT_07781 {
return vk::FALSE; return vk::FALSE;
@ -74,16 +71,9 @@ unsafe extern "system" fn debug_utils_messenger_callback(
_ => log::Level::Warn, _ => log::Level::Warn,
}; };
let message_id_name = if cd.p_message_id_name.is_null() { let message_id_name =
Cow::from("") unsafe { cd.message_id_name_as_c_str() }.map_or(Cow::Borrowed(""), CStr::to_string_lossy);
} else { let message = unsafe { cd.message_as_c_str() }.map_or(Cow::Borrowed(""), CStr::to_string_lossy);
unsafe { CStr::from_ptr(cd.p_message_id_name) }.to_string_lossy()
};
let message = if cd.p_message.is_null() {
Cow::from("")
} else {
unsafe { CStr::from_ptr(cd.p_message) }.to_string_lossy()
};
let _ = std::panic::catch_unwind(|| { let _ = std::panic::catch_unwind(|| {
log::log!( log::log!(
@ -101,10 +91,7 @@ unsafe extern "system" fn debug_utils_messenger_callback(
unsafe { slice::from_raw_parts(cd.p_queue_labels, cd.queue_label_count as usize) }; unsafe { slice::from_raw_parts(cd.p_queue_labels, cd.queue_label_count as usize) };
let names = labels let names = labels
.iter() .iter()
.flat_map(|dul_obj| { .flat_map(|dul_obj| unsafe { dul_obj.label_name_as_c_str() }.map(CStr::to_string_lossy))
unsafe { dul_obj.p_label_name.as_ref() }
.map(|lbl| unsafe { CStr::from_ptr(lbl) }.to_string_lossy())
})
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let _ = std::panic::catch_unwind(|| { let _ = std::panic::catch_unwind(|| {
@ -117,10 +104,7 @@ unsafe extern "system" fn debug_utils_messenger_callback(
unsafe { slice::from_raw_parts(cd.p_cmd_buf_labels, cd.cmd_buf_label_count as usize) }; unsafe { slice::from_raw_parts(cd.p_cmd_buf_labels, cd.cmd_buf_label_count as usize) };
let names = labels let names = labels
.iter() .iter()
.flat_map(|dul_obj| { .flat_map(|dul_obj| unsafe { dul_obj.label_name_as_c_str() }.map(CStr::to_string_lossy))
unsafe { dul_obj.p_label_name.as_ref() }
.map(|lbl| unsafe { CStr::from_ptr(lbl) }.to_string_lossy())
})
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let _ = std::panic::catch_unwind(|| { let _ = std::panic::catch_unwind(|| {
@ -134,9 +118,8 @@ unsafe extern "system" fn debug_utils_messenger_callback(
let names = labels let names = labels
.iter() .iter()
.map(|obj_info| { .map(|obj_info| {
let name = unsafe { obj_info.p_object_name.as_ref() } let name = unsafe { obj_info.object_name_as_c_str() }
.map(|name| unsafe { CStr::from_ptr(name) }.to_string_lossy()) .map_or(Cow::Borrowed("?"), CStr::to_string_lossy);
.unwrap_or(Cow::Borrowed("?"));
format!( format!(
"(type: {:?}, hndl: 0x{:x}, name: {})", "(type: {:?}, hndl: 0x{:x}, name: {})",
@ -158,9 +141,9 @@ unsafe extern "system" fn debug_utils_messenger_callback(
} }
impl super::DebugUtilsCreateInfo { impl super::DebugUtilsCreateInfo {
fn to_vk_create_info(&self) -> vk::DebugUtilsMessengerCreateInfoEXTBuilder<'_> { fn to_vk_create_info(&self) -> vk::DebugUtilsMessengerCreateInfoEXT<'_> {
let user_data_ptr: *const super::DebugUtilsMessengerUserData = &*self.callback_data; let user_data_ptr: *const super::DebugUtilsMessengerUserData = &*self.callback_data;
vk::DebugUtilsMessengerCreateInfoEXT::builder() vk::DebugUtilsMessengerCreateInfoEXT::default()
.message_severity(self.severity) .message_severity(self.severity)
.message_type(self.message_type) .message_type(self.message_type)
.user_data(user_data_ptr as *mut _) .user_data(user_data_ptr as *mut _)
@ -220,7 +203,7 @@ impl super::Instance {
) -> Result<Vec<vk::ExtensionProperties>, crate::InstanceError> { ) -> Result<Vec<vk::ExtensionProperties>, crate::InstanceError> {
let instance_extensions = { let instance_extensions = {
profiling::scope!("vkEnumerateInstanceExtensionProperties"); profiling::scope!("vkEnumerateInstanceExtensionProperties");
entry.enumerate_instance_extension_properties(layer_name) unsafe { entry.enumerate_instance_extension_properties(layer_name) }
}; };
instance_extensions.map_err(|e| { instance_extensions.map_err(|e| {
crate::InstanceError::with_source( crate::InstanceError::with_source(
@ -254,7 +237,7 @@ impl super::Instance {
let mut extensions: Vec<&'static CStr> = Vec::new(); let mut extensions: Vec<&'static CStr> = Vec::new();
// VK_KHR_surface // VK_KHR_surface
extensions.push(khr::Surface::name()); extensions.push(khr::surface::NAME);
// Platform-specific WSI extensions // Platform-specific WSI extensions
if cfg!(all( if cfg!(all(
@ -263,45 +246,46 @@ impl super::Instance {
not(target_os = "macos") not(target_os = "macos")
)) { )) {
// VK_KHR_xlib_surface // VK_KHR_xlib_surface
extensions.push(khr::XlibSurface::name()); extensions.push(khr::xlib_surface::NAME);
// VK_KHR_xcb_surface // VK_KHR_xcb_surface
extensions.push(khr::XcbSurface::name()); extensions.push(khr::xcb_surface::NAME);
// VK_KHR_wayland_surface // VK_KHR_wayland_surface
extensions.push(khr::WaylandSurface::name()); extensions.push(khr::wayland_surface::NAME);
} }
if cfg!(target_os = "android") { if cfg!(target_os = "android") {
// VK_KHR_android_surface // VK_KHR_android_surface
extensions.push(khr::AndroidSurface::name()); extensions.push(khr::android_surface::NAME);
} }
if cfg!(target_os = "windows") { if cfg!(target_os = "windows") {
// VK_KHR_win32_surface // VK_KHR_win32_surface
extensions.push(khr::Win32Surface::name()); extensions.push(khr::win32_surface::NAME);
} }
if cfg!(target_os = "macos") { if cfg!(target_os = "macos") {
// VK_EXT_metal_surface // VK_EXT_metal_surface
extensions.push(ext::MetalSurface::name()); extensions.push(ext::metal_surface::NAME);
extensions.push(vk::KhrPortabilityEnumerationFn::name()); extensions.push(khr::portability_enumeration::NAME);
} }
if flags.contains(wgt::InstanceFlags::DEBUG) { if flags.contains(wgt::InstanceFlags::DEBUG) {
// VK_EXT_debug_utils // VK_EXT_debug_utils
extensions.push(ext::DebugUtils::name()); extensions.push(ext::debug_utils::NAME);
} }
// VK_EXT_swapchain_colorspace // VK_EXT_swapchain_colorspace
// Provides wide color gamut // Provides wide color gamut
extensions.push(vk::ExtSwapchainColorspaceFn::name()); extensions.push(ext::swapchain_colorspace::NAME);
// VK_KHR_get_physical_device_properties2 // VK_KHR_get_physical_device_properties2
// Even though the extension was promoted to Vulkan 1.1, we still require the extension // Even though the extension was promoted to Vulkan 1.1, we still require the extension
// so that we don't have to conditionally use the functions provided by the 1.1 instance // so that we don't have to conditionally use the functions provided by the 1.1 instance
extensions.push(vk::KhrGetPhysicalDeviceProperties2Fn::name()); extensions.push(khr::get_physical_device_properties2::NAME);
// Only keep available extensions. // Only keep available extensions.
extensions.retain(|&ext| { extensions.retain(|&ext| {
if instance_extensions.iter().any(|inst_ext| { if instance_extensions
crate::auxil::cstr_from_bytes_until_nul(&inst_ext.extension_name) == Some(ext) .iter()
}) { .any(|inst_ext| inst_ext.extension_name_as_c_str() == Ok(ext))
{
true true
} else { } else {
log::warn!("Unable to find extension: {}", ext.to_string_lossy()); log::warn!("Unable to find extension: {}", ext.to_string_lossy());
@ -336,10 +320,10 @@ impl super::Instance {
log::debug!("Instance version: 0x{:x}", instance_api_version); log::debug!("Instance version: 0x{:x}", instance_api_version);
let debug_utils = if let Some(debug_utils_create_info) = debug_utils_create_info { let debug_utils = if let Some(debug_utils_create_info) = debug_utils_create_info {
if extensions.contains(&ext::DebugUtils::name()) { if extensions.contains(&ext::debug_utils::NAME) {
log::info!("Enabling debug utils"); log::info!("Enabling debug utils");
let extension = ext::DebugUtils::new(&entry, &raw_instance); let extension = ext::debug_utils::Instance::new(&entry, &raw_instance);
let vk_info = debug_utils_create_info.to_vk_create_info(); let vk_info = debug_utils_create_info.to_vk_create_info();
let messenger = let messenger =
unsafe { extension.create_debug_utils_messenger(&vk_info, None) }.unwrap(); unsafe { extension.create_debug_utils_messenger(&vk_info, None) }.unwrap();
@ -362,9 +346,9 @@ impl super::Instance {
}; };
let get_physical_device_properties = let get_physical_device_properties =
if extensions.contains(&khr::GetPhysicalDeviceProperties2::name()) { if extensions.contains(&khr::get_physical_device_properties2::NAME) {
log::debug!("Enabling device properties2"); log::debug!("Enabling device properties2");
Some(khr::GetPhysicalDeviceProperties2::new( Some(khr::get_physical_device_properties2::Instance::new(
&entry, &entry,
&raw_instance, &raw_instance,
)) ))
@ -388,21 +372,21 @@ impl super::Instance {
}) })
} }
#[allow(dead_code)]
fn create_surface_from_xlib( fn create_surface_from_xlib(
&self, &self,
dpy: *mut vk::Display, dpy: *mut vk::Display,
window: vk::Window, window: vk::Window,
) -> Result<super::Surface, crate::InstanceError> { ) -> Result<super::Surface, crate::InstanceError> {
if !self.shared.extensions.contains(&khr::XlibSurface::name()) { if !self.shared.extensions.contains(&khr::xlib_surface::NAME) {
return Err(crate::InstanceError::new(String::from( return Err(crate::InstanceError::new(String::from(
"Vulkan driver does not support VK_KHR_xlib_surface", "Vulkan driver does not support VK_KHR_xlib_surface",
))); )));
} }
let surface = { let surface = {
let xlib_loader = khr::XlibSurface::new(&self.shared.entry, &self.shared.raw); let xlib_loader =
let info = vk::XlibSurfaceCreateInfoKHR::builder() khr::xlib_surface::Instance::new(&self.shared.entry, &self.shared.raw);
let info = vk::XlibSurfaceCreateInfoKHR::default()
.flags(vk::XlibSurfaceCreateFlagsKHR::empty()) .flags(vk::XlibSurfaceCreateFlagsKHR::empty())
.window(window) .window(window)
.dpy(dpy); .dpy(dpy);
@ -414,21 +398,20 @@ impl super::Instance {
Ok(self.create_surface_from_vk_surface_khr(surface)) Ok(self.create_surface_from_vk_surface_khr(surface))
} }
#[allow(dead_code)]
fn create_surface_from_xcb( fn create_surface_from_xcb(
&self, &self,
connection: *mut vk::xcb_connection_t, connection: *mut vk::xcb_connection_t,
window: vk::xcb_window_t, window: vk::xcb_window_t,
) -> Result<super::Surface, crate::InstanceError> { ) -> Result<super::Surface, crate::InstanceError> {
if !self.shared.extensions.contains(&khr::XcbSurface::name()) { if !self.shared.extensions.contains(&khr::xcb_surface::NAME) {
return Err(crate::InstanceError::new(String::from( return Err(crate::InstanceError::new(String::from(
"Vulkan driver does not support VK_KHR_xcb_surface", "Vulkan driver does not support VK_KHR_xcb_surface",
))); )));
} }
let surface = { let surface = {
let xcb_loader = khr::XcbSurface::new(&self.shared.entry, &self.shared.raw); let xcb_loader = khr::xcb_surface::Instance::new(&self.shared.entry, &self.shared.raw);
let info = vk::XcbSurfaceCreateInfoKHR::builder() let info = vk::XcbSurfaceCreateInfoKHR::default()
.flags(vk::XcbSurfaceCreateFlagsKHR::empty()) .flags(vk::XcbSurfaceCreateFlagsKHR::empty())
.window(window) .window(window)
.connection(connection); .connection(connection);
@ -440,25 +423,21 @@ impl super::Instance {
Ok(self.create_surface_from_vk_surface_khr(surface)) Ok(self.create_surface_from_vk_surface_khr(surface))
} }
#[allow(dead_code)]
fn create_surface_from_wayland( fn create_surface_from_wayland(
&self, &self,
display: *mut c_void, display: *mut vk::wl_display,
surface: *mut c_void, surface: *mut vk::wl_surface,
) -> Result<super::Surface, crate::InstanceError> { ) -> Result<super::Surface, crate::InstanceError> {
if !self if !self.shared.extensions.contains(&khr::wayland_surface::NAME) {
.shared
.extensions
.contains(&khr::WaylandSurface::name())
{
return Err(crate::InstanceError::new(String::from( return Err(crate::InstanceError::new(String::from(
"Vulkan driver does not support VK_KHR_wayland_surface", "Vulkan driver does not support VK_KHR_wayland_surface",
))); )));
} }
let surface = { let surface = {
let w_loader = khr::WaylandSurface::new(&self.shared.entry, &self.shared.raw); let w_loader =
let info = vk::WaylandSurfaceCreateInfoKHR::builder() khr::wayland_surface::Instance::new(&self.shared.entry, &self.shared.raw);
let info = vk::WaylandSurfaceCreateInfoKHR::default()
.flags(vk::WaylandSurfaceCreateFlagsKHR::empty()) .flags(vk::WaylandSurfaceCreateFlagsKHR::empty())
.display(display) .display(display)
.surface(surface); .surface(surface);
@ -469,26 +448,22 @@ impl super::Instance {
Ok(self.create_surface_from_vk_surface_khr(surface)) Ok(self.create_surface_from_vk_surface_khr(surface))
} }
#[allow(dead_code)]
fn create_surface_android( fn create_surface_android(
&self, &self,
window: *const c_void, window: *mut vk::ANativeWindow,
) -> Result<super::Surface, crate::InstanceError> { ) -> Result<super::Surface, crate::InstanceError> {
if !self if !self.shared.extensions.contains(&khr::android_surface::NAME) {
.shared
.extensions
.contains(&khr::AndroidSurface::name())
{
return Err(crate::InstanceError::new(String::from( return Err(crate::InstanceError::new(String::from(
"Vulkan driver does not support VK_KHR_android_surface", "Vulkan driver does not support VK_KHR_android_surface",
))); )));
} }
let surface = { let surface = {
let a_loader = khr::AndroidSurface::new(&self.shared.entry, &self.shared.raw); let a_loader =
let info = vk::AndroidSurfaceCreateInfoKHR::builder() khr::android_surface::Instance::new(&self.shared.entry, &self.shared.raw);
let info = vk::AndroidSurfaceCreateInfoKHR::default()
.flags(vk::AndroidSurfaceCreateFlagsKHR::empty()) .flags(vk::AndroidSurfaceCreateFlagsKHR::empty())
.window(window as *mut _); .window(window);
unsafe { a_loader.create_android_surface(&info, None) }.expect("AndroidSurface failed") unsafe { a_loader.create_android_surface(&info, None) }.expect("AndroidSurface failed")
}; };
@ -496,24 +471,24 @@ impl super::Instance {
Ok(self.create_surface_from_vk_surface_khr(surface)) Ok(self.create_surface_from_vk_surface_khr(surface))
} }
#[allow(dead_code)]
fn create_surface_from_hwnd( fn create_surface_from_hwnd(
&self, &self,
hinstance: *mut c_void, hinstance: vk::HINSTANCE,
hwnd: *mut c_void, hwnd: vk::HWND,
) -> Result<super::Surface, crate::InstanceError> { ) -> Result<super::Surface, crate::InstanceError> {
if !self.shared.extensions.contains(&khr::Win32Surface::name()) { if !self.shared.extensions.contains(&khr::win32_surface::NAME) {
return Err(crate::InstanceError::new(String::from( return Err(crate::InstanceError::new(String::from(
"Vulkan driver does not support VK_KHR_win32_surface", "Vulkan driver does not support VK_KHR_win32_surface",
))); )));
} }
let surface = { let surface = {
let info = vk::Win32SurfaceCreateInfoKHR::builder() let info = vk::Win32SurfaceCreateInfoKHR::default()
.flags(vk::Win32SurfaceCreateFlagsKHR::empty()) .flags(vk::Win32SurfaceCreateFlagsKHR::empty())
.hinstance(hinstance) .hinstance(hinstance)
.hwnd(hwnd); .hwnd(hwnd);
let win32_loader = khr::Win32Surface::new(&self.shared.entry, &self.shared.raw); let win32_loader =
khr::win32_surface::Instance::new(&self.shared.entry, &self.shared.raw);
unsafe { unsafe {
win32_loader win32_loader
.create_win32_surface(&info, None) .create_win32_surface(&info, None)
@ -529,7 +504,7 @@ impl super::Instance {
&self, &self,
view: *mut c_void, view: *mut c_void,
) -> Result<super::Surface, crate::InstanceError> { ) -> Result<super::Surface, crate::InstanceError> {
if !self.shared.extensions.contains(&ext::MetalSurface::name()) { if !self.shared.extensions.contains(&ext::metal_surface::NAME) {
return Err(crate::InstanceError::new(String::from( return Err(crate::InstanceError::new(String::from(
"Vulkan driver does not support VK_EXT_metal_surface", "Vulkan driver does not support VK_EXT_metal_surface",
))); )));
@ -540,11 +515,11 @@ impl super::Instance {
}; };
let surface = { let surface = {
let metal_loader = ext::MetalSurface::new(&self.shared.entry, &self.shared.raw); let metal_loader =
let vk_info = vk::MetalSurfaceCreateInfoEXT::builder() ext::metal_surface::Instance::new(&self.shared.entry, &self.shared.raw);
let vk_info = vk::MetalSurfaceCreateInfoEXT::default()
.flags(vk::MetalSurfaceCreateFlagsEXT::empty()) .flags(vk::MetalSurfaceCreateFlagsEXT::empty())
.layer(layer as *mut _) .layer(layer as *mut _);
.build();
unsafe { metal_loader.create_metal_surface(&vk_info, None).unwrap() } unsafe { metal_loader.create_metal_surface(&vk_info, None).unwrap() }
}; };
@ -553,7 +528,7 @@ impl super::Instance {
} }
fn create_surface_from_vk_surface_khr(&self, surface: vk::SurfaceKHR) -> super::Surface { fn create_surface_from_vk_surface_khr(&self, surface: vk::SurfaceKHR) -> super::Surface {
let functor = khr::Surface::new(&self.shared.entry, &self.shared.raw); let functor = khr::surface::Instance::new(&self.shared.entry, &self.shared.raw);
super::Surface { super::Surface {
raw: surface, raw: surface,
functor, functor,
@ -584,7 +559,6 @@ impl crate::Instance for super::Instance {
unsafe fn init(desc: &crate::InstanceDescriptor) -> Result<Self, crate::InstanceError> { unsafe fn init(desc: &crate::InstanceDescriptor) -> Result<Self, crate::InstanceError> {
profiling::scope!("Init Vulkan Backend"); profiling::scope!("Init Vulkan Backend");
use crate::auxil::cstr_from_bytes_until_nul;
let entry = unsafe { let entry = unsafe {
profiling::scope!("Load vk library"); profiling::scope!("Load vk library");
@ -595,7 +569,7 @@ impl crate::Instance for super::Instance {
})?; })?;
let version = { let version = {
profiling::scope!("vkEnumerateInstanceVersion"); profiling::scope!("vkEnumerateInstanceVersion");
entry.try_enumerate_instance_version() unsafe { entry.try_enumerate_instance_version() }
}; };
let instance_api_version = match version { let instance_api_version = match version {
// Vulkan 1.1+ // Vulkan 1.1+
@ -610,7 +584,7 @@ impl crate::Instance for super::Instance {
}; };
let app_name = CString::new(desc.name).unwrap(); let app_name = CString::new(desc.name).unwrap();
let app_info = vk::ApplicationInfo::builder() let app_info = vk::ApplicationInfo::default()
.application_name(app_name.as_c_str()) .application_name(app_name.as_c_str())
.application_version(1) .application_version(1)
.engine_name(CStr::from_bytes_with_nul(b"wgpu-hal\0").unwrap()) .engine_name(CStr::from_bytes_with_nul(b"wgpu-hal\0").unwrap())
@ -636,7 +610,7 @@ impl crate::Instance for super::Instance {
let instance_layers = { let instance_layers = {
profiling::scope!("vkEnumerateInstanceLayerProperties"); profiling::scope!("vkEnumerateInstanceLayerProperties");
entry.enumerate_instance_layer_properties() unsafe { entry.enumerate_instance_layer_properties() }
}; };
let instance_layers = instance_layers.map_err(|e| { let instance_layers = instance_layers.map_err(|e| {
log::debug!("enumerate_instance_layer_properties: {:?}", e); log::debug!("enumerate_instance_layer_properties: {:?}", e);
@ -652,7 +626,7 @@ impl crate::Instance for super::Instance {
) -> Option<&'layers vk::LayerProperties> { ) -> Option<&'layers vk::LayerProperties> {
instance_layers instance_layers
.iter() .iter()
.find(|inst_layer| cstr_from_bytes_until_nul(&inst_layer.layer_name) == Some(name)) .find(|inst_layer| inst_layer.layer_name_as_c_str() == Ok(name))
} }
let validation_layer_name = let validation_layer_name =
@ -668,9 +642,9 @@ impl crate::Instance for super::Instance {
// Convert all the names of the extensions into an iterator of CStrs. // Convert all the names of the extensions into an iterator of CStrs.
let mut ext_names = exts let mut ext_names = exts
.iter() .iter()
.filter_map(|ext| cstr_from_bytes_until_nul(&ext.extension_name)); .filter_map(|ext| ext.extension_name_as_c_str().ok());
// Find the validation features extension. // Find the validation features extension.
ext_names.any(|ext_name| ext_name == vk::ExtValidationFeaturesFn::name()) ext_names.any(|ext_name| ext_name == ext::validation_features::NAME)
} else { } else {
false false
}; };
@ -688,7 +662,7 @@ impl crate::Instance for super::Instance {
let mut layers: Vec<&'static CStr> = Vec::new(); let mut layers: Vec<&'static CStr> = Vec::new();
let has_debug_extension = extensions.contains(&ext::DebugUtils::name()); let has_debug_extension = extensions.contains(&ext::debug_utils::NAME);
let mut debug_user_data = has_debug_extension.then(|| { let mut debug_user_data = has_debug_extension.then(|| {
// Put the callback data on the heap, to ensure it will never be // Put the callback data on the heap, to ensure it will never be
// moved. // moved.
@ -708,11 +682,10 @@ impl crate::Instance for super::Instance {
if let Some(debug_user_data) = debug_user_data.as_mut() { if let Some(debug_user_data) = debug_user_data.as_mut() {
debug_user_data.validation_layer_properties = debug_user_data.validation_layer_properties =
Some(super::ValidationLayerProperties { Some(super::ValidationLayerProperties {
layer_description: cstr_from_bytes_until_nul( layer_description: layer_properties
&layer_properties.description, .description_as_c_str()
) .unwrap()
.unwrap() .to_owned(),
.to_owned(),
layer_spec_version: layer_properties.spec_version, layer_spec_version: layer_properties.spec_version,
}); });
} }
@ -746,9 +719,7 @@ impl crate::Instance for super::Instance {
callback_data, callback_data,
}; };
let vk_create_info = create_info.to_vk_create_info().build(); Some(create_info)
Some((create_info, vk_create_info))
} else { } else {
None None
}; };
@ -780,7 +751,7 @@ impl crate::Instance for super::Instance {
// Avoid VUID-VkInstanceCreateInfo-flags-06559: Only ask the instance to // Avoid VUID-VkInstanceCreateInfo-flags-06559: Only ask the instance to
// enumerate incomplete Vulkan implementations (which we need on Mac) if // enumerate incomplete Vulkan implementations (which we need on Mac) if
// we managed to find the extension that provides the flag. // we managed to find the extension that provides the flag.
if extensions.contains(&vk::KhrPortabilityEnumerationFn::name()) { if extensions.contains(&khr::portability_enumeration::NAME) {
flags |= vk::InstanceCreateFlags::ENUMERATE_PORTABILITY_KHR; flags |= vk::InstanceCreateFlags::ENUMERATE_PORTABILITY_KHR;
} }
let vk_instance = { let vk_instance = {
@ -793,14 +764,17 @@ impl crate::Instance for super::Instance {
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let mut create_info = vk::InstanceCreateInfo::builder() let mut create_info = vk::InstanceCreateInfo::default()
.flags(flags) .flags(flags)
.application_info(&app_info) .application_info(&app_info)
.enabled_layer_names(&str_pointers[..layers.len()]) .enabled_layer_names(&str_pointers[..layers.len()])
.enabled_extension_names(&str_pointers[layers.len()..]); .enabled_extension_names(&str_pointers[layers.len()..]);
if let Some(&mut (_, ref mut vk_create_info)) = debug_utils.as_mut() { let mut debug_utils_create_info = debug_utils
create_info = create_info.push_next(vk_create_info); .as_mut()
.map(|create_info| create_info.to_vk_create_info());
if let Some(debug_utils_create_info) = debug_utils_create_info.as_mut() {
create_info = create_info.push_next(debug_utils_create_info);
} }
// Enable explicit validation features if available // Enable explicit validation features if available
@ -820,7 +794,7 @@ impl crate::Instance for super::Instance {
.push(vk::ValidationFeatureEnableEXT::GPU_ASSISTED_RESERVE_BINDING_SLOT); .push(vk::ValidationFeatureEnableEXT::GPU_ASSISTED_RESERVE_BINDING_SLOT);
} }
validation_features = vk::ValidationFeaturesEXT::builder() validation_features = vk::ValidationFeaturesEXT::default()
.enabled_validation_features(&validation_feature_list); .enabled_validation_features(&validation_feature_list);
create_info = create_info.push_next(&mut validation_features); create_info = create_info.push_next(&mut validation_features);
} }
@ -843,7 +817,7 @@ impl crate::Instance for super::Instance {
vk_instance, vk_instance,
instance_api_version, instance_api_version,
android_sdk_version, android_sdk_version,
debug_utils.map(|(i, _)| i), debug_utils,
extensions, extensions,
desc.flags, desc.flags,
has_nv_optimus, has_nv_optimus,
@ -859,13 +833,15 @@ impl crate::Instance for super::Instance {
) -> Result<super::Surface, crate::InstanceError> { ) -> Result<super::Surface, crate::InstanceError> {
use raw_window_handle::{RawDisplayHandle as Rdh, RawWindowHandle as Rwh}; use raw_window_handle::{RawDisplayHandle as Rdh, RawWindowHandle as Rwh};
// TODO: Replace with ash-window, which also lazy-loads the extension based on handle type
match (window_handle, display_handle) { match (window_handle, display_handle) {
(Rwh::Wayland(handle), Rdh::Wayland(display)) => { (Rwh::Wayland(handle), Rdh::Wayland(display)) => {
self.create_surface_from_wayland(display.display.as_ptr(), handle.surface.as_ptr()) self.create_surface_from_wayland(display.display.as_ptr(), handle.surface.as_ptr())
} }
(Rwh::Xlib(handle), Rdh::Xlib(display)) => { (Rwh::Xlib(handle), Rdh::Xlib(display)) => {
let display = display.display.expect("Display pointer is not set."); let display = display.display.expect("Display pointer is not set.");
self.create_surface_from_xlib(display.as_ptr() as *mut *const c_void, handle.window) self.create_surface_from_xlib(display.as_ptr(), handle.window)
} }
(Rwh::Xcb(handle), Rdh::Xcb(display)) => { (Rwh::Xcb(handle), Rdh::Xcb(display)) => {
let connection = display.connection.expect("Pointer to X-Server is not set."); let connection = display.connection.expect("Pointer to X-Server is not set.");
@ -874,22 +850,23 @@ impl crate::Instance for super::Instance {
(Rwh::AndroidNdk(handle), _) => { (Rwh::AndroidNdk(handle), _) => {
self.create_surface_android(handle.a_native_window.as_ptr()) self.create_surface_android(handle.a_native_window.as_ptr())
} }
#[cfg(windows)]
(Rwh::Win32(handle), _) => { (Rwh::Win32(handle), _) => {
use winapi::um::libloaderapi::GetModuleHandleW; let hinstance = handle.hinstance.ok_or_else(|| {
crate::InstanceError::new(String::from(
let hinstance = unsafe { GetModuleHandleW(std::ptr::null()) }; "Vulkan requires raw-window-handle's Win32::hinstance to be set",
self.create_surface_from_hwnd(hinstance as *mut _, handle.hwnd.get() as *mut _) ))
})?;
self.create_surface_from_hwnd(hinstance.get(), handle.hwnd.get())
} }
#[cfg(all(target_os = "macos", feature = "metal"))] #[cfg(all(target_os = "macos", feature = "metal"))]
(Rwh::AppKit(handle), _) (Rwh::AppKit(handle), _)
if self.shared.extensions.contains(&ext::MetalSurface::name()) => if self.shared.extensions.contains(&ext::metal_surface::NAME) =>
{ {
self.create_surface_from_view(handle.ns_view.as_ptr()) self.create_surface_from_view(handle.ns_view.as_ptr())
} }
#[cfg(all(target_os = "ios", feature = "metal"))] #[cfg(all(target_os = "ios", feature = "metal"))]
(Rwh::UiKit(handle), _) (Rwh::UiKit(handle), _)
if self.shared.extensions.contains(&ext::MetalSurface::name()) => if self.shared.extensions.contains(&ext::metal_surface::NAME) =>
{ {
self.create_surface_from_view(handle.ui_view.as_ptr()) self.create_surface_from_view(handle.ui_view.as_ptr())
} }

View File

@ -33,7 +33,7 @@ mod instance;
use std::{ use std::{
borrow::Borrow, borrow::Borrow,
ffi::CStr, ffi::{CStr, CString},
fmt, fmt,
num::NonZeroU32, num::NonZeroU32,
sync::{ sync::{
@ -43,10 +43,7 @@ use std::{
}; };
use arrayvec::ArrayVec; use arrayvec::ArrayVec;
use ash::{ use ash::{ext, khr, vk};
extensions::{ext, khr},
vk,
};
use parking_lot::{Mutex, RwLock}; use parking_lot::{Mutex, RwLock};
const MILLIS_TO_NANOS: u64 = 1_000_000; const MILLIS_TO_NANOS: u64 = 1_000_000;
@ -83,7 +80,7 @@ impl crate::Api for Api {
} }
struct DebugUtils { struct DebugUtils {
extension: ext::DebugUtils, extension: ext::debug_utils::Instance,
messenger: vk::DebugUtilsMessengerEXT, messenger: vk::DebugUtilsMessengerEXT,
/// Owning pointer to the debug messenger callback user data. /// Owning pointer to the debug messenger callback user data.
@ -106,7 +103,7 @@ pub struct DebugUtilsCreateInfo {
/// DebugUtilsMessenger for their workarounds /// DebugUtilsMessenger for their workarounds
struct ValidationLayerProperties { struct ValidationLayerProperties {
/// Validation layer description, from `vk::LayerProperties`. /// Validation layer description, from `vk::LayerProperties`.
layer_description: std::ffi::CString, layer_description: CString,
/// Validation layer specification version, from `vk::LayerProperties`. /// Validation layer specification version, from `vk::LayerProperties`.
layer_spec_version: u32, layer_spec_version: u32,
@ -132,7 +129,7 @@ pub struct InstanceShared {
drop_guard: Option<crate::DropGuard>, drop_guard: Option<crate::DropGuard>,
flags: wgt::InstanceFlags, flags: wgt::InstanceFlags,
debug_utils: Option<DebugUtils>, debug_utils: Option<DebugUtils>,
get_physical_device_properties: Option<khr::GetPhysicalDeviceProperties2>, get_physical_device_properties: Option<khr::get_physical_device_properties2::Instance>,
entry: ash::Entry, entry: ash::Entry,
has_nv_optimus: bool, has_nv_optimus: bool,
android_sdk_version: u32, android_sdk_version: u32,
@ -152,7 +149,7 @@ pub struct Instance {
struct Swapchain { struct Swapchain {
raw: vk::SwapchainKHR, raw: vk::SwapchainKHR,
raw_flags: vk::SwapchainCreateFlagsKHR, raw_flags: vk::SwapchainCreateFlagsKHR,
functor: khr::Swapchain, functor: khr::swapchain::Device,
device: Arc<DeviceShared>, device: Arc<DeviceShared>,
images: Vec<vk::Image>, images: Vec<vk::Image>,
config: crate::SurfaceConfiguration, config: crate::SurfaceConfiguration,
@ -166,7 +163,7 @@ struct Swapchain {
pub struct Surface { pub struct Surface {
raw: vk::SurfaceKHR, raw: vk::SurfaceKHR,
functor: khr::Surface, functor: khr::surface::Instance,
instance: Arc<InstanceShared>, instance: Arc<InstanceShared>,
swapchain: RwLock<Option<Swapchain>>, swapchain: RwLock<Option<Swapchain>>,
} }
@ -205,14 +202,15 @@ enum ExtensionFn<T> {
} }
struct DeviceExtensionFunctions { struct DeviceExtensionFunctions {
draw_indirect_count: Option<khr::DrawIndirectCount>, debug_utils: Option<ext::debug_utils::Device>,
timeline_semaphore: Option<ExtensionFn<khr::TimelineSemaphore>>, draw_indirect_count: Option<khr::draw_indirect_count::Device>,
timeline_semaphore: Option<ExtensionFn<khr::timeline_semaphore::Device>>,
ray_tracing: Option<RayTracingDeviceExtensionFunctions>, ray_tracing: Option<RayTracingDeviceExtensionFunctions>,
} }
struct RayTracingDeviceExtensionFunctions { struct RayTracingDeviceExtensionFunctions {
acceleration_structure: khr::AccelerationStructure, acceleration_structure: khr::acceleration_structure::Device,
buffer_device_address: khr::BufferDeviceAddress, buffer_device_address: khr::buffer_device_address::Device,
} }
/// Set of internal capabilities, which don't show up in the exposed /// Set of internal capabilities, which don't show up in the exposed
@ -361,7 +359,7 @@ pub struct Device {
pub struct Queue { pub struct Queue {
raw: vk::Queue, raw: vk::Queue,
swapchain_fn: khr::Swapchain, swapchain_fn: khr::swapchain::Device,
device: Arc<DeviceShared>, device: Arc<DeviceShared>,
family_index: u32, family_index: u32,
/// We use a redundant chain of semaphores to pass on the signal /// We use a redundant chain of semaphores to pass on the signal
@ -452,13 +450,10 @@ pub struct BindGroup {
#[derive(Default)] #[derive(Default)]
struct Temp { struct Temp {
marker: Vec<u8>, marker: Vec<u8>,
buffer_barriers: Vec<vk::BufferMemoryBarrier>, buffer_barriers: Vec<vk::BufferMemoryBarrier<'static>>,
image_barriers: Vec<vk::ImageMemoryBarrier>, image_barriers: Vec<vk::ImageMemoryBarrier<'static>>,
} }
unsafe impl Send for Temp {}
unsafe impl Sync for Temp {}
impl Temp { impl Temp {
fn clear(&mut self) { fn clear(&mut self) {
self.marker.clear(); self.marker.clear();
@ -638,7 +633,7 @@ impl Fence {
fn get_latest( fn get_latest(
&self, &self,
device: &ash::Device, device: &ash::Device,
extension: Option<&ExtensionFn<khr::TimelineSemaphore>>, extension: Option<&ExtensionFn<khr::timeline_semaphore::Device>>,
) -> Result<crate::FenceValue, crate::DeviceError> { ) -> Result<crate::FenceValue, crate::DeviceError> {
match *self { match *self {
Self::TimelineSemaphore(raw) => unsafe { Self::TimelineSemaphore(raw) => unsafe {
@ -684,9 +679,7 @@ impl Fence {
} }
if free.len() != base_free { if free.len() != base_free {
active.retain(|&(value, _)| value > latest); active.retain(|&(value, _)| value > latest);
unsafe { unsafe { device.reset_fences(&free[base_free..]) }?
device.reset_fences(&free[base_free..])?;
}
} }
*last_completed = latest; *last_completed = latest;
} }
@ -749,7 +742,7 @@ impl crate::Queue for Queue {
None => unsafe { None => unsafe {
self.device self.device
.raw .raw
.create_fence(&vk::FenceCreateInfo::builder(), None)? .create_fence(&vk::FenceCreateInfo::default(), None)?
}, },
}; };
active.push((value, fence_raw)); active.push((value, fence_raw));
@ -762,7 +755,7 @@ impl crate::Queue for Queue {
.map(|cmd| cmd.raw) .map(|cmd| cmd.raw)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let mut vk_info = vk::SubmitInfo::builder().command_buffers(&vk_cmd_buffers); let mut vk_info = vk::SubmitInfo::default().command_buffers(&vk_cmd_buffers);
vk_info = vk_info vk_info = vk_info
.wait_semaphores(&wait_semaphores) .wait_semaphores(&wait_semaphores)
@ -773,7 +766,7 @@ impl crate::Queue for Queue {
if !signal_values.is_empty() { if !signal_values.is_empty() {
vk_timeline_info = vk_timeline_info =
vk::TimelineSemaphoreSubmitInfo::builder().signal_semaphore_values(&signal_values); vk::TimelineSemaphoreSubmitInfo::default().signal_semaphore_values(&signal_values);
vk_info = vk_info.push_next(&mut vk_timeline_info); vk_info = vk_info.push_next(&mut vk_timeline_info);
} }
@ -781,7 +774,7 @@ impl crate::Queue for Queue {
unsafe { unsafe {
self.device self.device
.raw .raw
.queue_submit(self.raw, &[vk_info.build()], fence_raw)? .queue_submit(self.raw, &[vk_info], fence_raw)?
}; };
Ok(()) Ok(())
} }
@ -796,7 +789,7 @@ impl crate::Queue for Queue {
let swapchains = [ssc.raw]; let swapchains = [ssc.raw];
let image_indices = [texture.index]; let image_indices = [texture.index];
let mut vk_info = vk::PresentInfoKHR::builder() let mut vk_info = vk::PresentInfoKHR::default()
.swapchains(&swapchains) .swapchains(&swapchains)
.image_indices(&image_indices); .image_indices(&image_indices);