diff --git a/Common/GPU/Vulkan/VulkanRenderManager.cpp b/Common/GPU/Vulkan/VulkanRenderManager.cpp index ba2c64f758..0d857844b6 100644 --- a/Common/GPU/Vulkan/VulkanRenderManager.cpp +++ b/Common/GPU/Vulkan/VulkanRenderManager.cpp @@ -449,6 +449,10 @@ void VulkanRenderManager::CompileThreadFunc() { if (!run_) { break; } + + // TODO: Here we can sort the pending pipelines by vertex and fragment shaders, + // and split up further. + // Those with the same pairs of shaders should be on the same thread. for (auto &entry : toCompile) { switch (entry.type) { case CompileQueueEntry::Type::GRAPHICS: diff --git a/Common/Thread/Promise.h b/Common/Thread/Promise.h index fe2ddb3807..78cfd65f59 100644 --- a/Common/Thread/Promise.h +++ b/Common/Thread/Promise.h @@ -33,6 +33,7 @@ public: // Has ownership over the data. Single use. // TODO: Split Mailbox (rx_ and tx_) up into separate proxy objects. // NOTE: Poll/BlockUntilReady should only be used from one thread. +// TODO: Make movable? template class Promise { public: @@ -47,6 +48,13 @@ public: return promise; } + static Promise *AlreadyDone(T data) { + Promise *promise = new Promise(); + promise->data_ = data; + promise->ready_ = true; + return promise; + } + ~Promise() { // A promise should have been fulfilled before it's destroyed. _assert_(ready_); @@ -85,7 +93,8 @@ public: private: Promise() {} + // Promise can only be constructed in Spawn. T data_ = nullptr; bool ready_ = false; - Mailbox *rx_; + Mailbox *rx_ = nullptr; }; diff --git a/GPU/Vulkan/PipelineManagerVulkan.cpp b/GPU/Vulkan/PipelineManagerVulkan.cpp index 86fa279b9c..f513f6ddd8 100644 --- a/GPU/Vulkan/PipelineManagerVulkan.cpp +++ b/GPU/Vulkan/PipelineManagerVulkan.cpp @@ -260,13 +260,13 @@ static VulkanPipeline *CreateVulkanPipeline(VulkanRenderManager *renderManager, ss[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; ss[0].stage = VK_SHADER_STAGE_VERTEX_BIT; ss[0].pSpecializationInfo = nullptr; - ss[0].module = vs->GetModule(); + ss[0].module = vs->GetModule()->BlockUntilReady(); ss[0].pName = "main"; ss[0].flags = 0; ss[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; ss[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT; ss[1].pSpecializationInfo = nullptr; - ss[1].module = fs->GetModule(); + ss[1].module = fs->GetModule()->BlockUntilReady(); ss[1].pName = "main"; ss[1].flags = 0; @@ -369,8 +369,8 @@ VulkanPipeline *PipelineManagerVulkan::GetOrCreatePipeline(VulkanRenderManager * key.raster = rasterKey; key.renderPass = renderPass; key.useHWTransform = useHwTransform; - key.vShader = vs->GetModule(); - key.fShader = fs->GetModule(); + key.vShader = vs->GetModule()->BlockUntilReady(); + key.fShader = fs->GetModule()->BlockUntilReady(); key.vtxFmtId = useHwTransform ? decFmt->id : 0; auto iter = pipelines_.Get(key); diff --git a/GPU/Vulkan/ShaderManagerVulkan.cpp b/GPU/Vulkan/ShaderManagerVulkan.cpp index 400f4d180c..5c30151515 100644 --- a/GPU/Vulkan/ShaderManagerVulkan.cpp +++ b/GPU/Vulkan/ShaderManagerVulkan.cpp @@ -42,7 +42,7 @@ #include "GPU/Vulkan/DrawEngineVulkan.h" #include "GPU/Vulkan/FramebufferManagerVulkan.h" -VkShaderModule CompileShaderModule(VulkanContext *vulkan, VkShaderStageFlagBits stage, const char *code) { +Promise *CompileShaderModule(VulkanContext *vulkan, VkShaderStageFlagBits stage, const char *code) { PROFILE_THIS_SCOPE("shadercomp"); std::string errorMessage; @@ -67,11 +67,10 @@ VkShaderModule CompileShaderModule(VulkanContext *vulkan, VkShaderStageFlagBits #endif Reporting::ReportMessage("Vulkan error in shader compilation: info: %s / code: %s", errorMessage.c_str(), code); } else { + VkShaderModule shaderModule; + success = vulkan->CreateShaderModule(spirv, &shaderModule); #ifdef SHADERLOG - OutputDebugStringA(LineNumberString(code).c_str()); -#endif -#ifdef SHADERLOG - OutputDebugStringA("OK\n"); + OutputDebugStringA("OK"); #endif } @@ -79,7 +78,7 @@ VkShaderModule CompileShaderModule(VulkanContext *vulkan, VkShaderStageFlagBits vulkan->CreateShaderModule(spirv, &shaderModule); } - return shaderModule; + return Promise::AlreadyDone(shaderModule); } @@ -96,8 +95,9 @@ VulkanFragmentShader::VulkanFragmentShader(VulkanContext *vulkan, FShaderID id, } VulkanFragmentShader::~VulkanFragmentShader() { - if (module_ != VK_NULL_HANDLE) { - vulkan_->Delete().QueueDeleteShaderModule(module_); + if (module_) { + VkShaderModule shaderModule = module_->BlockUntilReady(); + vulkan_->Delete().QueueDeleteShaderModule(shaderModule); } } @@ -120,13 +120,14 @@ VulkanVertexShader::VulkanVertexShader(VulkanContext *vulkan, VShaderID id, cons failed_ = true; return; } else { - VERBOSE_LOG(G3D, "Compiled vertex shader:\n%s\n", (const char *)code); + VERBOSE_LOG(G3D, "Compiled fragment shader:\n%s\n", (const char *)code); } } VulkanVertexShader::~VulkanVertexShader() { - if (module_ != VK_NULL_HANDLE) { - vulkan_->Delete().QueueDeleteShaderModule(module_); + if (module_) { + VkShaderModule shaderModule = module_->BlockUntilReady(); + vulkan_->Delete().QueueDeleteShaderModule(shaderModule); } } @@ -334,7 +335,9 @@ std::string ShaderManagerVulkan::DebugGetShaderString(std::string id, DebugShade VulkanVertexShader *ShaderManagerVulkan::GetVertexShaderFromModule(VkShaderModule module) { VulkanVertexShader *vs = nullptr; vsCache_.Iterate([&](const VShaderID &id, VulkanVertexShader *shader) { - if (shader->GetModule() == module) + Promise *p = shader->GetModule(); + VkShaderModule m = p->BlockUntilReady(); + if (m == module) vs = shader; }); return vs; @@ -343,7 +346,9 @@ VulkanVertexShader *ShaderManagerVulkan::GetVertexShaderFromModule(VkShaderModul VulkanFragmentShader *ShaderManagerVulkan::GetFragmentShaderFromModule(VkShaderModule module) { VulkanFragmentShader *fs = nullptr; fsCache_.Iterate([&](const FShaderID &id, VulkanFragmentShader *shader) { - if (shader->GetModule() == module) + Promise *p = shader->GetModule(); + VkShaderModule m = p->BlockUntilReady(); + if (m == module) fs = shader; }); return fs; diff --git a/GPU/Vulkan/ShaderManagerVulkan.h b/GPU/Vulkan/ShaderManagerVulkan.h index ff7e5d8c5c..5ab37fa723 100644 --- a/GPU/Vulkan/ShaderManagerVulkan.h +++ b/GPU/Vulkan/ShaderManagerVulkan.h @@ -20,6 +20,7 @@ #include #include +#include "Common/Thread/Promise.h" #include "Common/Data/Collections/Hashmaps.h" #include "Common/GPU/Vulkan/VulkanMemory.h" #include "GPU/Common/ShaderCommon.h" @@ -43,11 +44,11 @@ public: bool Failed() const { return failed_; } std::string GetShaderString(DebugShaderStringType type) const; - VkShaderModule GetModule() const { return module_; } + Promise *GetModule() { return module_; } const FShaderID &GetID() { return id_; } protected: - VkShaderModule module_ = VK_NULL_HANDLE; + Promise *module_; VulkanContext *vulkan_; std::string source_; @@ -66,11 +67,11 @@ public: bool UseHWTransform() const { return useHWTransform_; } std::string GetShaderString(DebugShaderStringType type) const; - VkShaderModule GetModule() const { return module_; } + Promise *GetModule() { return module_; } const VShaderID &GetID() { return id_; } protected: - VkShaderModule module_ = VK_NULL_HANDLE; + Promise *module_ = nullptr; VulkanContext *vulkan_; std::string source_;