From 8b42d8312373b6ae19a04b1630ece769fd6ed50d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Thu, 9 Nov 2017 16:58:59 +0100 Subject: [PATCH] Vulkan: Remove duplicate depalShaderCache, fix a number of instances where we didn't use the deleter properly. Plus more logging, of course. --- Common/Vulkan/VulkanContext.cpp | 2 ++ Common/Vulkan/VulkanContext.h | 2 ++ Common/Vulkan/VulkanMemory.cpp | 1 - GPU/Vulkan/DepalettizeShaderVulkan.cpp | 14 +++++++++++--- GPU/Vulkan/DepalettizeShaderVulkan.h | 1 + GPU/Vulkan/DrawEngineVulkan.cpp | 4 ++-- GPU/Vulkan/FramebufferVulkan.cpp | 3 +-- GPU/Vulkan/FramebufferVulkan.h | 1 - GPU/Vulkan/GPU_Vulkan.cpp | 2 +- GPU/Vulkan/VulkanUtil.cpp | 4 ++-- ext/native/thin3d/VulkanQueueRunner.cpp | 11 ++++++----- ext/native/thin3d/VulkanRenderManager.cpp | 9 +++++++-- ext/native/thin3d/thin3d_vulkan.cpp | 8 ++++---- ext/native/thread/prioritizedworkqueue.cpp | 4 +++- 14 files changed, 42 insertions(+), 24 deletions(-) diff --git a/Common/Vulkan/VulkanContext.cpp b/Common/Vulkan/VulkanContext.cpp index e89cf7ea0e..acc0cbff90 100644 --- a/Common/Vulkan/VulkanContext.cpp +++ b/Common/Vulkan/VulkanContext.cpp @@ -219,6 +219,7 @@ bool VulkanContext::InitObjects() { } void VulkanContext::DestroyObjects() { + ILOG("VulkanContext::DestroyObjects (including swapchain)"); if (swapchain_ != VK_NULL_HANDLE) vkDestroySwapchainKHR(device_, swapchain_, nullptr); swapchain_ = VK_NULL_HANDLE; @@ -584,6 +585,7 @@ void VulkanContext::InitSurfaceAndroid(ANativeWindow *wnd, int width, int height } void VulkanContext::ReinitSurfaceAndroid(int width, int height) { + ILOG("Destroying and reiniting Android Vulkan surface"); if (surface_ != VK_NULL_HANDLE) { vkDestroySurfaceKHR(instance_, surface_, nullptr); surface_ = VK_NULL_HANDLE; diff --git a/Common/Vulkan/VulkanContext.h b/Common/Vulkan/VulkanContext.h index 6be9c6809d..4b195ad1de 100644 --- a/Common/Vulkan/VulkanContext.h +++ b/Common/Vulkan/VulkanContext.h @@ -71,6 +71,7 @@ class VulkanDeleteList { }; public: + // NOTE: These all take reference handles so they can zero the input value. void QueueDeleteCommandPool(VkCommandPool &pool) { cmdPools_.push_back(pool); pool = VK_NULL_HANDLE; } void QueueDeleteDescriptorPool(VkDescriptorPool &pool) { descPools_.push_back(pool); pool = VK_NULL_HANDLE; } void QueueDeleteShaderModule(VkShaderModule &module) { modules_.push_back(module); module = VK_NULL_HANDLE; } @@ -122,6 +123,7 @@ public: } void PerformDeletes(VkDevice device) { + ILOG("PerformDeletes"); for (auto &cmdPool : cmdPools_) { vkDestroyCommandPool(device, cmdPool, nullptr); } diff --git a/Common/Vulkan/VulkanMemory.cpp b/Common/Vulkan/VulkanMemory.cpp index 260be7d9d6..57b219b472 100644 --- a/Common/Vulkan/VulkanMemory.cpp +++ b/Common/Vulkan/VulkanMemory.cpp @@ -78,7 +78,6 @@ void VulkanPushBuffer::Destroy(VulkanContext *vulkan) { vulkan->Delete().QueueDeleteBuffer(info.buffer); vulkan->Delete().QueueDeleteDeviceMemory(info.deviceMemory); } - buffers_.clear(); } diff --git a/GPU/Vulkan/DepalettizeShaderVulkan.cpp b/GPU/Vulkan/DepalettizeShaderVulkan.cpp index 513252c4c1..d4022f86ca 100644 --- a/GPU/Vulkan/DepalettizeShaderVulkan.cpp +++ b/GPU/Vulkan/DepalettizeShaderVulkan.cpp @@ -60,19 +60,27 @@ static VkFormat GetClutDestFormat(GEPaletteFormat format, VkComponentMapping *co DepalShaderCacheVulkan::DepalShaderCacheVulkan(Draw::DrawContext *draw, VulkanContext *vulkan) : draw_(draw), vulkan_(vulkan) { - std::string errors; - vshader_ = CompileShaderModule(vulkan_, VK_SHADER_STAGE_VERTEX_BIT, depal_vs, &errors); - assert(vshader_ != VK_NULL_HANDLE); + DeviceRestore(draw, vulkan); } DepalShaderCacheVulkan::~DepalShaderCacheVulkan() { + DeviceLost(); +} + +void DepalShaderCacheVulkan::DeviceLost() { Clear(); vulkan_->Delete().QueueDeleteShaderModule(vshader_); + vshader_ = nullptr; + draw_ = nullptr; + vulkan_ = nullptr; } void DepalShaderCacheVulkan::DeviceRestore(Draw::DrawContext *draw, VulkanContext *vulkan) { draw_ = draw; vulkan_ = vulkan; + std::string errors; + vshader_ = CompileShaderModule(vulkan_, VK_SHADER_STAGE_VERTEX_BIT, depal_vs, &errors); + assert(vshader_ != VK_NULL_HANDLE); } DepalShaderVulkan *DepalShaderCacheVulkan::GetDepalettizeShader(uint32_t clutMode, GEBufferFormat pixelFormat) { diff --git a/GPU/Vulkan/DepalettizeShaderVulkan.h b/GPU/Vulkan/DepalettizeShaderVulkan.h index 76e6d48f81..f06057e0c1 100644 --- a/GPU/Vulkan/DepalettizeShaderVulkan.h +++ b/GPU/Vulkan/DepalettizeShaderVulkan.h @@ -52,6 +52,7 @@ class DepalShaderCacheVulkan : public DepalShaderCacheCommon { public: DepalShaderCacheVulkan(Draw::DrawContext *draw, VulkanContext *vulkan); ~DepalShaderCacheVulkan(); + void DeviceLost(); void DeviceRestore(Draw::DrawContext *draw, VulkanContext *vulkan); // This also uploads the palette and binds the correct texture. diff --git a/GPU/Vulkan/DrawEngineVulkan.cpp b/GPU/Vulkan/DrawEngineVulkan.cpp index 6d48626a08..22ff3d91b7 100644 --- a/GPU/Vulkan/DrawEngineVulkan.cpp +++ b/GPU/Vulkan/DrawEngineVulkan.cpp @@ -249,10 +249,10 @@ void DrawEngineVulkan::DestroyDeviceObjects() { if (nullSampler_ != VK_NULL_HANDLE) vulkan_->Delete().QueueDeleteSampler(nullSampler_); if (pipelineLayout_ != VK_NULL_HANDLE) - vkDestroyPipelineLayout(vulkan_->GetDevice(), pipelineLayout_, nullptr); + vulkan_->Delete().QueueDeletePipelineLayout(pipelineLayout_); pipelineLayout_ = VK_NULL_HANDLE; if (descriptorSetLayout_ != VK_NULL_HANDLE) - vkDestroyDescriptorSetLayout(vulkan_->GetDevice(), descriptorSetLayout_, nullptr); + vulkan_->Delete().QueueDeleteDescriptorSetLayout(descriptorSetLayout_); descriptorSetLayout_ = VK_NULL_HANDLE; if (nullTexture_) { nullTexture_->Destroy(); diff --git a/GPU/Vulkan/FramebufferVulkan.cpp b/GPU/Vulkan/FramebufferVulkan.cpp index 3de17428f0..9cb3019553 100644 --- a/GPU/Vulkan/FramebufferVulkan.cpp +++ b/GPU/Vulkan/FramebufferVulkan.cpp @@ -88,8 +88,7 @@ FramebufferManagerVulkan::FramebufferManagerVulkan(Draw::DrawContext *draw, Vulk convBufSize_(0), textureCacheVulkan_(nullptr), shaderManagerVulkan_(nullptr), - pipelinePostShader_(VK_NULL_HANDLE), - depalVulkan_(draw, vulkan) { + pipelinePostShader_(VK_NULL_HANDLE) { InitDeviceObjects(); diff --git a/GPU/Vulkan/FramebufferVulkan.h b/GPU/Vulkan/FramebufferVulkan.h index 2269283919..98ac4d9e25 100644 --- a/GPU/Vulkan/FramebufferVulkan.h +++ b/GPU/Vulkan/FramebufferVulkan.h @@ -120,7 +120,6 @@ private: DrawEngineVulkan *drawEngineVulkan_; VulkanPushBuffer *push_; - DepalShaderCacheVulkan depalVulkan_; enum { MAX_COMMAND_BUFFERS = 32, }; diff --git a/GPU/Vulkan/GPU_Vulkan.cpp b/GPU/Vulkan/GPU_Vulkan.cpp index f2e16024c6..ecb4c340c3 100644 --- a/GPU/Vulkan/GPU_Vulkan.cpp +++ b/GPU/Vulkan/GPU_Vulkan.cpp @@ -807,7 +807,7 @@ void GPU_Vulkan::DeviceLost() { drawEngine_.DeviceLost(); pipelineManager_->DeviceLost(); textureCacheVulkan_->DeviceLost(); - depalShaderCache_.Clear(); + depalShaderCache_.DeviceLost(); shaderManagerVulkan_->ClearShaders(); } diff --git a/GPU/Vulkan/VulkanUtil.cpp b/GPU/Vulkan/VulkanUtil.cpp index 01d001a0e3..662f5196c1 100644 --- a/GPU/Vulkan/VulkanUtil.cpp +++ b/GPU/Vulkan/VulkanUtil.cpp @@ -46,11 +46,11 @@ void Vulkan2D::DestroyDeviceObjects() { VkDevice device = vulkan_->GetDevice(); if (descriptorSetLayout_ != VK_NULL_HANDLE) { - vkDestroyDescriptorSetLayout(device, descriptorSetLayout_, nullptr); + vulkan_->Delete().QueueDeleteDescriptorSetLayout(descriptorSetLayout_); descriptorSetLayout_ = VK_NULL_HANDLE; } if (pipelineLayout_ != VK_NULL_HANDLE) { - vkDestroyPipelineLayout(device, pipelineLayout_, nullptr); + vulkan_->Delete().QueueDeletePipelineLayout(pipelineLayout_); pipelineLayout_ = VK_NULL_HANDLE; } diff --git a/ext/native/thin3d/VulkanQueueRunner.cpp b/ext/native/thin3d/VulkanQueueRunner.cpp index 82be5de2a3..4b566d12e6 100644 --- a/ext/native/thin3d/VulkanQueueRunner.cpp +++ b/ext/native/thin3d/VulkanQueueRunner.cpp @@ -39,6 +39,7 @@ void VulkanQueueRunner::CreateDeviceObjects() { void VulkanQueueRunner::DestroyDeviceObjects() { ILOG("VulkanQueueRuner::DestroyDeviceObjects"); VkDevice device = vulkan_->GetDevice(); + vulkan_->Delete().QueueDeleteDeviceMemory(readbackMemory_); vkFreeMemory(device, readbackMemory_, nullptr); readbackMemory_ = VK_NULL_HANDLE; vulkan_->Delete().QueueDeleteBuffer(readbackBuffer_); @@ -46,11 +47,11 @@ void VulkanQueueRunner::DestroyDeviceObjects() { for (int i = 0; i < ARRAY_SIZE(renderPasses_); i++) { assert(renderPasses_[i] != VK_NULL_HANDLE); - vkDestroyRenderPass(device, renderPasses_[i], nullptr); + vulkan_->Delete().QueueDeleteRenderPass(renderPasses_[i]); renderPasses_[i] = VK_NULL_HANDLE; } assert(backbufferRenderPass_ != VK_NULL_HANDLE); - vkDestroyRenderPass(device, backbufferRenderPass_, nullptr); + vulkan_->Delete().QueueDeleteRenderPass(backbufferRenderPass_); backbufferRenderPass_ = VK_NULL_HANDLE; } @@ -425,8 +426,8 @@ void VulkanQueueRunner::PerformRenderPass(const VKRStep &step, VkCommandBuffer c VkClearRect rc{}; rc.baseArrayLayer = 0; rc.layerCount = 1; - rc.rect.extent.width = curWidth; - rc.rect.extent.height = curHeight; + rc.rect.extent.width = (uint32_t)curWidth; + rc.rect.extent.height = (uint32_t)curHeight; VkClearAttachment attachments[2]; if (c.clear.clearMask & VK_IMAGE_ASPECT_COLOR_BIT) { VkClearAttachment &attachment = attachments[numAttachments++]; @@ -442,7 +443,7 @@ void VulkanQueueRunner::PerformRenderPass(const VKRStep &step, VkCommandBuffer c attachment.aspectMask |= VK_IMAGE_ASPECT_DEPTH_BIT; } if (c.clear.clearMask & VK_IMAGE_ASPECT_STENCIL_BIT) { - attachment.clearValue.depthStencil.stencil = c.clear.clearStencil; + attachment.clearValue.depthStencil.stencil = (uint32_t)c.clear.clearStencil; attachment.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT; } } diff --git a/ext/native/thin3d/VulkanRenderManager.cpp b/ext/native/thin3d/VulkanRenderManager.cpp index 4683807bb0..253627888d 100644 --- a/ext/native/thin3d/VulkanRenderManager.cpp +++ b/ext/native/thin3d/VulkanRenderManager.cpp @@ -194,7 +194,7 @@ void VulkanRenderManager::CreateBackbuffers() { run_ = true; // Won't necessarily be 0. threadInitFrame_ = vulkan_->GetCurFrame(); - VLOG("starting thread"); + ILOG("Starting Vulkan submission thread"); thread_ = std::thread(&VulkanRenderManager::ThreadFunc, this); } } @@ -215,7 +215,7 @@ void VulkanRenderManager::StopThread() { } } thread_.join(); - VLOG("thread joined."); + ILOG("Vulkan submission thread joined."); // Wait for any fences to finish and be resignaled, so we don't have sync issues. for (int i = 0; i < vulkan_->GetInflightFrames(); i++) { @@ -228,6 +228,8 @@ void VulkanRenderManager::StopThread() { frameData.push_condVar.wait(lock); } } + } else { + ILOG("Vulkan submission thread was already stopped."); } } @@ -331,6 +333,9 @@ void VulkanRenderManager::BeginFrame() { // Must be after the fence - this performs deletes. VLOG("PUSH: BeginFrame %d", curFrame); + if (!run_) { + WLOG("BeginFrame while !run_!"); + } vulkan_->BeginFrame(); insideFrame_ = true; diff --git a/ext/native/thin3d/thin3d_vulkan.cpp b/ext/native/thin3d/thin3d_vulkan.cpp index 1925f057c3..fffc1f7a1b 100644 --- a/ext/native/thin3d/thin3d_vulkan.cpp +++ b/ext/native/thin3d/thin3d_vulkan.cpp @@ -193,7 +193,7 @@ public: const std::string &GetSource() const { return source_; } ~VKShaderModule() { if (module_) { - vkDestroyShaderModule(device_, module_, nullptr); + vulkan_->Delete().QueueDeleteShaderModule(module_); } } VkShaderModule Get() const { return module_; } @@ -202,7 +202,7 @@ public: } private: - VkDevice device_; + VulkanContext *vulkan_; VkShaderModule module_; VkShaderStageFlagBits vkstage_; bool ok_; @@ -211,9 +211,9 @@ private: }; bool VKShaderModule::Compile(VulkanContext *vulkan, ShaderLanguage language, const uint8_t *data, size_t size) { + vulkan_ = vulkan; // We'll need this to free it later. - device_ = vulkan->GetDevice(); - this->source_ = (const char *)data; + source_ = (const char *)data; std::vector spirv; if (!GLSLtoSPV(vkstage_, source_.c_str(), spirv)) { return false; diff --git a/ext/native/thread/prioritizedworkqueue.cpp b/ext/native/thread/prioritizedworkqueue.cpp index f3b6d3f0e7..1583738f40 100644 --- a/ext/native/thread/prioritizedworkqueue.cpp +++ b/ext/native/thread/prioritizedworkqueue.cpp @@ -31,7 +31,9 @@ void PrioritizedWorkQueue::Flush() { flush_count++; } queue_.clear(); - ILOG("Flushed %d un-executed tasks", flush_count); + if (flush_count > 0) { + ILOG("PrioritizedWorkQueue: Flushed %d un-executed tasks", flush_count); + } } bool PrioritizedWorkQueue::WaitUntilDone(bool all) {