[Vulkan] Better handling of device lost events (present fatal error dialog)

This commit is contained in:
DrChat
2017-12-18 14:27:00 -06:00
parent 76b577148d
commit b5d647d540
14 changed files with 154 additions and 65 deletions

View File

@@ -271,7 +271,11 @@ void Blitter::BlitTexture2D(VkCommandBuffer command_buffer, VkFence fence,
// Acquire and update a descriptor set for this image.
auto set = descriptor_pool_->AcquireEntry(descriptor_set_layout_);
assert_not_null(set);
if (!set) {
assert_always();
descriptor_pool_->CancelBatch();
return;
}
VkWriteDescriptorSet write;
write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;

View File

@@ -50,7 +50,9 @@ class BaseFencedPool {
while (pending_batch_list_head_) {
auto batch = pending_batch_list_head_;
assert_not_null(batch->fence);
if (vkGetFenceStatus(device_, batch->fence) == VK_SUCCESS) {
VkResult status = vkGetFenceStatus(device_, batch->fence);
if (status == VK_SUCCESS || status == VK_ERROR_DEVICE_LOST) {
// Batch has completed. Reclaim.
pending_batch_list_head_ = batch->next;
if (batch == pending_batch_list_tail_) {

View File

@@ -37,11 +37,12 @@ VulkanContext::VulkanContext(VulkanProvider* provider, Window* target_window)
: GraphicsContext(provider, target_window) {}
VulkanContext::~VulkanContext() {
VkResult status;
auto provider = static_cast<VulkanProvider*>(provider_);
auto device = provider->device();
{
std::lock_guard<std::mutex> queue_lock(device->primary_queue_mutex());
vkQueueWaitIdle(device->primary_queue());
status = vkQueueWaitIdle(device->primary_queue());
}
immediate_drawer_.reset();
swap_chain_.reset();
@@ -132,6 +133,8 @@ void VulkanContext::BeginSwap() {
auto provider = static_cast<VulkanProvider*>(provider_);
auto device = provider->device();
VkResult status;
// If we have a window see if it's been resized since we last swapped.
// If it has been, we'll need to reinitialize the swap chain before we
// start touching it.
@@ -143,13 +146,17 @@ void VulkanContext::BeginSwap() {
}
}
// Acquire the next image and set it up for use.
swap_chain_->Begin();
if (!context_lost_) {
// Acquire the next image and set it up for use.
status = swap_chain_->Begin();
if (status == VK_ERROR_DEVICE_LOST) {
context_lost_ = true;
}
}
// TODO(benvanik): use a fence instead? May not be possible with target image.
std::lock_guard<std::mutex> queue_lock(device->primary_queue_mutex());
auto err = vkQueueWaitIdle(device->primary_queue());
CheckResult(err, "vkQueueWaitIdle");
status = vkQueueWaitIdle(device->primary_queue());
}
void VulkanContext::EndSwap() {
@@ -157,15 +164,21 @@ void VulkanContext::EndSwap() {
auto provider = static_cast<VulkanProvider*>(provider_);
auto device = provider->device();
// Notify the presentation engine the image is ready.
// The contents must be in a coherent state.
swap_chain_->End();
VkResult status;
if (!context_lost_) {
// Notify the presentation engine the image is ready.
// The contents must be in a coherent state.
status = swap_chain_->End();
if (status == VK_ERROR_DEVICE_LOST) {
context_lost_ = true;
}
}
// Wait until the queue is idle.
// TODO(benvanik): is this required?
std::lock_guard<std::mutex> queue_lock(device->primary_queue_mutex());
auto err = vkQueueWaitIdle(device->primary_queue());
CheckResult(err, "vkQueueWaitIdle");
status = vkQueueWaitIdle(device->primary_queue());
}
std::unique_ptr<RawImage> VulkanContext::Capture() {

View File

@@ -38,11 +38,16 @@ class VulkanContext : public GraphicsContext {
bool MakeCurrent() override;
void ClearCurrent() override;
bool WasLost() override { return context_lost_; }
void BeginSwap() override;
void EndSwap() override;
std::unique_ptr<RawImage> Capture() override;
protected:
bool context_lost_ = false;
private:
friend class VulkanProvider;

View File

@@ -388,14 +388,18 @@ void VulkanSwapChain::Shutdown() {
}
}
bool VulkanSwapChain::Begin() {
VkResult VulkanSwapChain::Begin() {
wait_and_signal_semaphores_.clear();
VkResult status;
// Get the index of the next available swapchain image.
auto err =
status =
vkAcquireNextImageKHR(*device_, handle, 0, image_available_semaphore_,
nullptr, &current_buffer_index_);
CheckResult(err, "vkAcquireNextImageKHR");
if (status != VK_SUCCESS) {
return status;
}
// Wait for the acquire semaphore to be signaled so that the following
// operations know they can start modifying the image.
@@ -414,10 +418,12 @@ bool VulkanSwapChain::Begin() {
wait_submit_info.pSignalSemaphores = &image_usage_semaphore_;
{
std::lock_guard<std::mutex> queue_lock(device_->primary_queue_mutex());
err =
status =
vkQueueSubmit(device_->primary_queue(), 1, &wait_submit_info, nullptr);
}
CheckResult(err, "vkQueueSubmit");
if (status != VK_SUCCESS) {
return status;
}
// Reset all command buffers.
vkResetCommandBuffer(render_cmd_buffer_, 0);
@@ -441,13 +447,19 @@ bool VulkanSwapChain::Begin() {
begin_info.flags = VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT |
VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
begin_info.pInheritanceInfo = &inherit_info;
err = vkBeginCommandBuffer(render_cmd_buffer_, &begin_info);
CheckResult(err, "vkBeginCommandBuffer");
status = vkBeginCommandBuffer(render_cmd_buffer_, &begin_info);
CheckResult(status, "vkBeginCommandBuffer");
if (status != VK_SUCCESS) {
return status;
}
// Start recording the copy command buffer as well.
begin_info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
err = vkBeginCommandBuffer(copy_cmd_buffer_, &begin_info);
CheckResult(err, "vkBeginCommandBuffer");
status = vkBeginCommandBuffer(copy_cmd_buffer_, &begin_info);
CheckResult(status, "vkBeginCommandBuffer");
if (status != VK_SUCCESS) {
return status;
}
// First: Issue a command to clear the render target.
VkImageSubresourceRange clear_range = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
@@ -466,27 +478,42 @@ bool VulkanSwapChain::Begin() {
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &clear_color, 1,
&clear_range);
return true;
return VK_SUCCESS;
}
bool VulkanSwapChain::End() {
VkResult VulkanSwapChain::End() {
auto& current_buffer = buffers_[current_buffer_index_];
VkResult status;
auto err = vkEndCommandBuffer(render_cmd_buffer_);
CheckResult(err, "vkEndCommandBuffer");
status = vkEndCommandBuffer(render_cmd_buffer_);
CheckResult(status, "vkEndCommandBuffer");
if (status != VK_SUCCESS) {
return status;
}
err = vkEndCommandBuffer(copy_cmd_buffer_);
CheckResult(err, "vkEndCommandBuffer");
status = vkEndCommandBuffer(copy_cmd_buffer_);
CheckResult(status, "vkEndCommandBuffer");
if (status != VK_SUCCESS) {
return status;
}
// Build primary command buffer.
vkResetCommandBuffer(cmd_buffer_, 0);
status = vkResetCommandBuffer(cmd_buffer_, 0);
CheckResult(status, "vkResetCommandBuffer");
if (status != VK_SUCCESS) {
return status;
}
VkCommandBufferBeginInfo begin_info;
begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
begin_info.pNext = nullptr;
begin_info.flags = 0;
begin_info.pInheritanceInfo = nullptr;
vkBeginCommandBuffer(cmd_buffer_, &begin_info);
status = vkBeginCommandBuffer(cmd_buffer_, &begin_info);
CheckResult(status, "vkBeginCommandBuffer");
if (status != VK_SUCCESS) {
return status;
}
// Transition the image to a format we can copy to.
VkImageMemoryBarrier pre_image_copy_barrier;
@@ -580,9 +607,13 @@ bool VulkanSwapChain::End() {
current_buffer.image_layout = post_image_memory_barrier.newLayout;
vkEndCommandBuffer(cmd_buffer_);
VkPipelineStageFlags wait_dst_stage = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
status = vkEndCommandBuffer(cmd_buffer_);
CheckResult(status, "vkEndCommandBuffer");
if (status != VK_SUCCESS) {
return status;
}
VkPipelineStageFlags wait_dst_stage = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
std::vector<VkSemaphore> semaphores;
for (size_t i = 0; i < wait_and_signal_semaphores_.size(); i++) {
semaphores.push_back(wait_and_signal_semaphores_[i]);
@@ -603,10 +634,13 @@ bool VulkanSwapChain::End() {
render_submit_info.pSignalSemaphores = semaphores.data();
{
std::lock_guard<std::mutex> queue_lock(device_->primary_queue_mutex());
err = vkQueueSubmit(device_->primary_queue(), 1, &render_submit_info,
nullptr);
status = vkQueueSubmit(device_->primary_queue(), 1, &render_submit_info,
nullptr);
}
if (status != VK_SUCCESS) {
return status;
}
CheckResult(err, "vkQueueSubmit");
// Queue the present of our current image.
const VkSwapchainKHR swap_chains[] = {handle};
@@ -622,27 +656,30 @@ bool VulkanSwapChain::End() {
present_info.pResults = nullptr;
{
std::lock_guard<std::mutex> queue_lock(device_->primary_queue_mutex());
err = vkQueuePresentKHR(device_->primary_queue(), &present_info);
status = vkQueuePresentKHR(device_->primary_queue(), &present_info);
}
switch (err) {
switch (status) {
case VK_SUCCESS:
break;
case VK_SUBOPTIMAL_KHR:
// We are not rendering at the right size - but the presentation engine
// will scale the output for us.
status = VK_SUCCESS;
break;
case VK_ERROR_OUT_OF_DATE_KHR:
// Lost presentation ability; need to recreate the swapchain.
// TODO(benvanik): recreate swapchain.
assert_always("Swapchain recreation not implemented");
break;
case VK_ERROR_DEVICE_LOST:
// Fatal. Device lost.
break;
default:
XELOGE("Failed to queue present: %s", to_string(err));
XELOGE("Failed to queue present: %s", to_string(status));
assert_always("Unexpected queue present failure");
return false;
}
return true;
return status;
}
} // namespace vulkan

View File

@@ -57,9 +57,9 @@ class VulkanSwapChain {
void WaitAndSignalSemaphore(VkSemaphore sem);
// Begins the swap operation, preparing state for rendering.
bool Begin();
VkResult Begin();
// Ends the swap operation, finalizing rendering and presenting the results.
bool End();
VkResult End();
private:
struct Buffer {

View File

@@ -198,6 +198,11 @@ void Window::OnPaint(UIEvent* e) {
ImGui::NewFrame();
context_->BeginSwap();
if (context_->WasLost()) {
on_context_lost(e);
return;
}
ForEachListener([e](auto listener) { listener->OnPainting(e); });
on_painting(e);
ForEachListener([e](auto listener) { listener->OnPaint(e); });

View File

@@ -117,6 +117,8 @@ class Window {
Delegate<UIEvent*> on_painting;
Delegate<UIEvent*> on_paint;
Delegate<UIEvent*> on_painted;
Delegate<UIEvent*> on_context_lost;
Delegate<FileDropEvent*> on_file_drop;
Delegate<KeyEvent*> on_key_down;