From 57615344e4e0d7c696de9a531fc504a9dba04131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Tue, 16 Jan 2018 17:32:50 +0100 Subject: [PATCH] Fix for deletes, add some debugging stuff --- Common/Vulkan/VulkanContext.cpp | 32 +++++++++++----------- ext/native/thin3d/GLQueueRunner.cpp | 2 ++ ext/native/thin3d/GLRenderManager.cpp | 39 ++++++++++++++------------- ext/native/thin3d/GLRenderManager.h | 12 +++++++++ 4 files changed, 51 insertions(+), 34 deletions(-) diff --git a/Common/Vulkan/VulkanContext.cpp b/Common/Vulkan/VulkanContext.cpp index 6f3cfb5d55..8854970e9d 100644 --- a/Common/Vulkan/VulkanContext.cpp +++ b/Common/Vulkan/VulkanContext.cpp @@ -1090,22 +1090,22 @@ const char *VulkanResultToString(VkResult res) { } void VulkanDeleteList::Take(VulkanDeleteList &del) { - assert(cmdPools_.size() == 0); - assert(descPools_.size() == 0); - assert(modules_.size() == 0); - assert(buffers_.size() == 0); - assert(bufferViews_.size() == 0); - assert(images_.size() == 0); - assert(imageViews_.size() == 0); - assert(deviceMemory_.size() == 0); - assert(samplers_.size() == 0); - assert(pipelines_.size() == 0); - assert(pipelineCaches_.size() == 0); - assert(renderPasses_.size() == 0); - assert(framebuffers_.size() == 0); - assert(pipelineLayouts_.size() == 0); - assert(descSetLayouts_.size() == 0); - assert(callbacks_.size() == 0); + assert(cmdPools_.empty()); + assert(descPools_.empty()); + assert(modules_.empty()); + assert(buffers_.empty()); + assert(bufferViews_.empty()); + assert(images_.empty()); + assert(imageViews_.empty()); + assert(deviceMemory_.empty()); + assert(samplers_.empty()); + assert(pipelines_.empty()); + assert(pipelineCaches_.empty()); + assert(renderPasses_.empty()); + assert(framebuffers_.empty()); + assert(pipelineLayouts_.empty()); + assert(descSetLayouts_.empty()); + assert(callbacks_.empty()); cmdPools_ = std::move(del.cmdPools_); descPools_ = std::move(del.descPools_); modules_ = std::move(del.modules_); diff --git a/ext/native/thin3d/GLQueueRunner.cpp b/ext/native/thin3d/GLQueueRunner.cpp index ee4dfb2737..4b6bedada7 100644 --- a/ext/native/thin3d/GLQueueRunner.cpp +++ b/ext/native/thin3d/GLQueueRunner.cpp @@ -187,6 +187,7 @@ void GLQueueRunner::RunInitSteps(const std::vector &steps) { case GLRInitStepType::TEXTURE_IMAGE: { GLRTexture *tex = step.texture_image.texture; + _assert_msg_(G3D, tex->canary == GLRTexture::CanaryValue, "Canary has bad value %08x in texture %p", tex->canary, tex); CHECK_GL_ERROR_IF_DEBUG(); if (boundTexture != tex->texture) { glBindTexture(tex->target, tex->texture); @@ -624,6 +625,7 @@ void GLQueueRunner::PerformRenderPass(const GLRStep &step) { } if (c.texture.texture) { if (curTex[slot] != c.texture.texture) { + _assert_msg_(G3D, c.texture.texture->canary == GLRTexture::CanaryValue, "Canary has bad value %08x in texture %p", c.texture.texture->canary, c.texture.texture); glBindTexture(c.texture.texture->target, c.texture.texture->texture); curTex[slot] = c.texture.texture; } diff --git a/ext/native/thin3d/GLRenderManager.cpp b/ext/native/thin3d/GLRenderManager.cpp index b4c9535601..d4532489c9 100644 --- a/ext/native/thin3d/GLRenderManager.cpp +++ b/ext/native/thin3d/GLRenderManager.cpp @@ -12,6 +12,7 @@ #endif void GLDeleter::Perform() { + deleterMutex_.lock(); for (auto shader : shaders) { delete shader; } @@ -36,6 +37,7 @@ void GLDeleter::Perform() { delete framebuffer; } framebuffers.clear(); + deleterMutex_.unlock(); } GLRenderManager::GLRenderManager() { @@ -62,6 +64,7 @@ GLRenderManager::~GLRenderManager() { void GLRenderManager::ThreadStartup() { queueRunner_.CreateDeviceObjects(); + threadFrame_ = threadInitFrame_; } void GLRenderManager::ThreadEnd() { @@ -70,25 +73,24 @@ void GLRenderManager::ThreadEnd() { void GLRenderManager::ThreadFunc() { ThreadStartup(); - int threadFrame = threadInitFrame_; while (true) { { if (nextFrame) { - threadFrame++; - if (threadFrame >= MAX_INFLIGHT_FRAMES) - threadFrame = 0; + threadFrame_++; + if (threadFrame_ >= MAX_INFLIGHT_FRAMES) + threadFrame_ = 0; } - FrameData &frameData = frameData_[threadFrame]; + FrameData &frameData = frameData_[threadFrame_]; std::unique_lock lock(frameData.pull_mutex); while (!frameData.readyForRun && run_) { - VLOG("PULL: Waiting for frame[%d].readyForRun", threadFrame); + VLOG("PULL: Waiting for frame[%d].readyForRun", threadFrame_); frameData.pull_condVar.wait(lock); } if (!frameData.readyForRun && !run_) { // This means we're out of frames to render and run_ is false, so bail. break; } - VLOG("PULL: frame[%d].readyForRun = false", threadFrame); + VLOG("PULL: frame[%d].readyForRun = false", threadFrame_); frameData.readyForRun = false; // Previously we had a quick exit here that avoided calling Run() if run_ was suddenly false, // but that created a race condition where frames could end up not finished properly on resize etc. @@ -97,15 +99,15 @@ void GLRenderManager::ThreadFunc() { nextFrame = frameData.type == GLRRunType::END; assert(frameData.type == GLRRunType::END || frameData.type == GLRRunType::SYNC); } - VLOG("PULL: Running frame %d", threadFrame); + VLOG("PULL: Running frame %d", threadFrame_); if (firstFrame) { - ILOG("Running first frame (%d)", threadFrame); + ILOG("Running first frame (%d)", threadFrame_); firstFrame = false; } - Run(threadFrame); - VLOG("PULL: Finished frame %d", threadFrame); + Run(threadFrame_); + VLOG("PULL: Finished frame %d", threadFrame_); } - queueRunner_.DestroyDeviceObjects(); + ThreadEnd(); VLOG("PULL: Quitting"); } @@ -113,7 +115,6 @@ void GLRenderManager::ThreadFunc() { void GLRenderManager::StopThread() { // Since we don't control the thread directly, this will only pause the thread. - if (useThread_ && run_) { run_ = false; for (int i = 0; i < MAX_INFLIGHT_FRAMES; i++) { @@ -274,6 +275,7 @@ void GLRenderManager::BeginFrame() { VLOG("PUSH: Fencing %d", curFrame); + // vkWaitForFences(device, 1, &frameData.fence, true, UINT64_MAX); // vkResetFences(device, 1, &frameData.fence); // glFenceSync(...) @@ -313,7 +315,6 @@ void GLRenderManager::Finish() { frameData.pull_condVar.notify_all(); } - // vulkan_->EndFrame(); frameData_[curFrame_].deleter.Take(deleter_); curFrame_++; @@ -328,10 +329,6 @@ void GLRenderManager::BeginSubmitFrame(int frame) { if (!frameData.hasBegun) { frameData.hasBegun = true; } - - // This should be the best time to perform deletes for the next frame. - FrameData &nextFrameData = frameData_[(frame + 1) % MAX_INFLIGHT_FRAMES]; - nextFrameData.deleter.Perform(); } void GLRenderManager::Submit(int frame, bool triggerFence) { @@ -340,8 +337,14 @@ void GLRenderManager::Submit(int frame, bool triggerFence) { // In GL, submission happens automatically in Run(). // When !triggerFence, we notify after syncing with Vulkan. + + // Putting deletes here is safe but only because OpenGL has its own delete handling.. + // ideally we'd like to wait a frame or two. + frameData.deleter.Perform(); + if (useThread_ && triggerFence) { VLOG("PULL: Frame %d.readyForFence = true", frame); + std::unique_lock lock(frameData.push_mutex); frameData.readyForFence = true; frameData.push_condVar.notify_all(); diff --git a/ext/native/thin3d/GLRenderManager.h b/ext/native/thin3d/GLRenderManager.h index d6716f626b..dd8b3b29c5 100644 --- a/ext/native/thin3d/GLRenderManager.h +++ b/ext/native/thin3d/GLRenderManager.h @@ -111,7 +111,12 @@ public: if (texture) { glDeleteTextures(1, &texture); } + canary = 0xd31373d; // deleted } + enum { + CanaryValue = 0x12345678, + }; + uint32_t canary = CanaryValue; GLuint texture = 0; // Could also trust OpenGL defaults I guess.. GLenum target = 0xFFFF; @@ -149,6 +154,8 @@ public: void Perform(); void Take(GLDeleter &other) { + deleterMutex_.lock(); + _assert_msg_(G3D, shaders.empty() && programs.empty() && buffers.empty() && textures.empty() && inputLayouts.empty() && framebuffers.empty(), "Deleter already has stuff"); shaders = std::move(other.shaders); programs = std::move(other.programs); buffers = std::move(other.buffers); @@ -161,6 +168,7 @@ public: other.textures.clear(); other.inputLayouts.clear(); other.framebuffers.clear(); + deleterMutex_.unlock(); } std::vector shaders; @@ -169,6 +177,7 @@ public: std::vector textures; std::vector inputLayouts; std::vector framebuffers; + std::mutex deleterMutex_; }; class GLRInputLayout { @@ -687,6 +696,9 @@ private: int threadInitFrame_ = 0; GLQueueRunner queueRunner_; + // Thread state + int threadFrame_; + bool nextFrame = false; bool firstFrame = true;