Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Vulkan error handling more robust #6119

Merged
merged 6 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ impl From<hal::DeviceError> for DeviceError {
hal::DeviceError::Lost => DeviceError::Lost,
hal::DeviceError::OutOfMemory => DeviceError::OutOfMemory,
hal::DeviceError::ResourceCreationFailed => DeviceError::ResourceCreationFailed,
hal::DeviceError::Unexpected => DeviceError::Lost,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions wgpu-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ impl Adapter {
hal::DeviceError::Lost => RequestDeviceError::DeviceLost,
hal::DeviceError::OutOfMemory => RequestDeviceError::OutOfMemory,
hal::DeviceError::ResourceCreationFailed => RequestDeviceError::Internal,
hal::DeviceError::Unexpected => RequestDeviceError::DeviceLost,
})?;

self.create_device_and_queue_from_hal(open, desc, instance_flags, trace_path)
Expand Down
2 changes: 2 additions & 0 deletions wgpu-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ pub enum DeviceError {
Lost,
#[error("Creation of a resource failed for a reason other than running out of memory.")]
ResourceCreationFailed,
#[error("Unexpected error variant (driver implementation is at fault)")]
Unexpected,
}

#[derive(Clone, Debug, Eq, PartialEq, Error)]
Expand Down
17 changes: 16 additions & 1 deletion wgpu-hal/src/vulkan/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1985,8 +1985,23 @@ impl crate::Adapter for super::Adapter {
let info = enabled_phd_features.add_to_device_create(pre_info);
let raw_device = {
profiling::scope!("vkCreateDevice");
unsafe { self.instance.raw.create_device(self.raw, &info, None)? }
unsafe {
self.instance
.raw
.create_device(self.raw, &info, None)
.map_err(map_err)?
}
};
fn map_err(err: vk::Result) -> crate::DeviceError {
match err {
vk::Result::ERROR_TOO_MANY_OBJECTS => crate::DeviceError::OutOfMemory,
vk::Result::ERROR_INITIALIZATION_FAILED => crate::DeviceError::Lost,
vk::Result::ERROR_EXTENSION_NOT_PRESENT | vk::Result::ERROR_FEATURE_NOT_PRESENT => {
super::hal_usage_error(err)
}
other => super::map_host_device_oom_and_lost_err(other),
}
}

unsafe {
self.device_from_raw(
Expand Down
17 changes: 14 additions & 3 deletions wgpu-hal/src/vulkan/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ impl crate::CommandEncoder for super::CommandEncoder {
let vk_info = vk::CommandBufferAllocateInfo::default()
.command_pool(self.raw)
.command_buffer_count(ALLOCATION_GRANULARITY);
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)
.map_err(super::map_host_device_oom_err)?
};
self.free.extend(cmd_buf_vec);
}
let raw = self.free.pop().unwrap();
Expand All @@ -76,7 +81,8 @@ impl crate::CommandEncoder for super::CommandEncoder {

let vk_info = vk::CommandBufferBeginInfo::default()
.flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT);
unsafe { self.device.raw.begin_command_buffer(raw, &vk_info) }?;
unsafe { self.device.raw.begin_command_buffer(raw, &vk_info) }
.map_err(super::map_host_device_oom_err)?;
self.active = raw;

Ok(())
Expand All @@ -85,7 +91,12 @@ impl crate::CommandEncoder for super::CommandEncoder {
unsafe fn end_encoding(&mut self) -> Result<super::CommandBuffer, crate::DeviceError> {
let raw = self.active;
self.active = vk::CommandBuffer::null();
unsafe { self.device.raw.end_command_buffer(raw) }?;
unsafe { self.device.raw.end_command_buffer(raw) }.map_err(map_err)?;
fn map_err(err: vk::Result) -> crate::DeviceError {
// We don't use VK_KHR_video_encode_queue
// VK_ERROR_INVALID_VIDEO_STD_PARAMETERS_KHR
super::map_host_device_oom_err(err)
}
Ok(super::CommandBuffer { raw })
}

Expand Down
Loading
Loading