From 8f87a9f5c5197d5eb609d12ab5d6aaaf124a132c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 19 Mar 2018 10:55:06 +0100 Subject: [PATCH 1/2] Vulkan: De-duplicate pipelines when storing cache The new variety of renderpasses with different transitions causes duplication. Hopefully drivers are smart enough to re-use work between similar pipelines as much as possible... --- Common/Vulkan/VulkanContext.h | 7 ++++--- Common/Vulkan/VulkanImage.cpp | 6 ++++++ GPU/Vulkan/PipelineManagerVulkan.cpp | 23 +++++++++++++++++------ GPU/Vulkan/PipelineManagerVulkan.h | 6 ++++++ GPU/Vulkan/ShaderManagerVulkan.cpp | 2 +- 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/Common/Vulkan/VulkanContext.h b/Common/Vulkan/VulkanContext.h index 3bb4080694..ac4d98148f 100644 --- a/Common/Vulkan/VulkanContext.h +++ b/Common/Vulkan/VulkanContext.h @@ -135,10 +135,11 @@ public: bool EnableDeviceExtension(const char *extension); VkResult CreateDevice(); - const std::string &InitError() { return init_error_; } + const std::string &InitError() const { return init_error_; } - VkDevice GetDevice() { return device_; } - VkInstance GetInstance() { return instance_; } + VkDevice GetDevice() const { return device_; } + VkInstance GetInstance() const { return instance_; } + uint32_t GetFlags() const { return flags_; } VulkanDeleteList &Delete() { return globalDeleteList_; } diff --git a/Common/Vulkan/VulkanImage.cpp b/Common/Vulkan/VulkanImage.cpp index 70b561b036..4e8b841517 100644 --- a/Common/Vulkan/VulkanImage.cpp +++ b/Common/Vulkan/VulkanImage.cpp @@ -58,6 +58,12 @@ bool VulkanTexture::CreateDirect(VkCommandBuffer cmd, int w, int h, int numMips, image_create_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; } + // The graphics debugger always "needs" TRANSFER_SRC but in practice doesn't matter - + // unless validation is on. So let's only force it on when being validated, for now. + if (vulkan_->GetFlags() & VULKAN_FLAG_VALIDATE) { + image_create_info.usage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT; + } + VkResult res = vkCreateImage(vulkan_->GetDevice(), &image_create_info, NULL, &image_); if (res != VK_SUCCESS) { _assert_(res == VK_ERROR_OUT_OF_HOST_MEMORY || res == VK_ERROR_OUT_OF_DEVICE_MEMORY || res == VK_ERROR_TOO_MANY_OBJECTS); diff --git a/GPU/Vulkan/PipelineManagerVulkan.cpp b/GPU/Vulkan/PipelineManagerVulkan.cpp index 6e9a62aeb9..70870997fb 100644 --- a/GPU/Vulkan/PipelineManagerVulkan.cpp +++ b/GPU/Vulkan/PipelineManagerVulkan.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "profiler/profiler.h" @@ -526,12 +527,14 @@ void PipelineManagerVulkan::SaveCache(FILE *file, bool saveRawPipelineCache, Sha } size_t seekPosOnFailure = ftell(file); - // Write the number of pipelines. - size = (uint32_t)pipelines_.size(); - fwrite(&size, sizeof(size), 1, file); bool failed = false; int count = 0; + // Since we don't include the full pipeline key, there can be duplicates, + // caused by things like switching from buffered to non-buffered rendering. + // Make sure the set of pipelines we write is "unique". + std::set keys; + pipelines_.Iterate([&](const VulkanPipelineKey &pkey, VulkanPipeline *value) { if (failed) return; @@ -550,10 +553,18 @@ void PipelineManagerVulkan::SaveCache(FILE *file, bool saveRawPipelineCache, Sha // NOTE: This is not a vtype, but a decoded vertex format. key.vtxFmtId = pkey.vtxFmtId; } - fwrite(&key, sizeof(key), 1, file); - count++; + keys.insert(key); }); + // Write the number of pipelines. + size = (uint32_t)keys.size(); + fwrite(&size, sizeof(size), 1, file); + + // Write the pipelines. + for (auto &key : keys) { + fwrite(&key, sizeof(key), 1, file); + } + if (failed) { ERROR_LOG(G3D, "Failed to write pipeline cache, some shader was missing"); // Write a zero in the right place so it doesn't try to load the pipelines next time. @@ -562,7 +573,7 @@ void PipelineManagerVulkan::SaveCache(FILE *file, bool saveRawPipelineCache, Sha fwrite(&size, sizeof(size), 1, file); return; } - NOTICE_LOG(G3D, "Saved Vulkan pipeline ID cache (%d pipelines).", (int)count); + NOTICE_LOG(G3D, "Saved Vulkan pipeline ID cache (%d unique pipelines/%d).", (int)keys.size(), (int)pipelines_.size()); } bool PipelineManagerVulkan::LoadCache(FILE *file, bool loadRawPipelineCache, ShaderManagerVulkan *shaderManager, DrawEngineCommon *drawEngine, VkPipelineLayout layout, VkRenderPass renderPass) { diff --git a/GPU/Vulkan/PipelineManagerVulkan.h b/GPU/Vulkan/PipelineManagerVulkan.h index 6cf98c7ed4..60a0582c38 100644 --- a/GPU/Vulkan/PipelineManagerVulkan.h +++ b/GPU/Vulkan/PipelineManagerVulkan.h @@ -17,6 +17,7 @@ #pragma once +#include #include "Common/Hashmaps.h" #include "GPU/Common/VertexDecoderCommon.h" @@ -60,6 +61,11 @@ struct StoredVulkanPipelineKey { FShaderID fShaderID; uint32_t vtxFmtId; bool useHWTransform; + + // For std::set. Better zero-initialize the struct properly for this to work. + bool operator < (const StoredVulkanPipelineKey &other) const { + return memcmp(this, &other, sizeof(*this)) < 0; + } }; enum PipelineFlags { diff --git a/GPU/Vulkan/ShaderManagerVulkan.cpp b/GPU/Vulkan/ShaderManagerVulkan.cpp index b753ebd19a..666f47aa52 100644 --- a/GPU/Vulkan/ShaderManagerVulkan.cpp +++ b/GPU/Vulkan/ShaderManagerVulkan.cpp @@ -350,7 +350,7 @@ VulkanFragmentShader *ShaderManagerVulkan::GetFragmentShaderFromModule(VkShaderM // instantaneous. #define CACHE_HEADER_MAGIC 0xff51f420 -#define CACHE_VERSION 5 +#define CACHE_VERSION 6 struct VulkanCacheHeader { uint32_t magic; uint32_t version; From 3e5757d7d754b7836445d7adcec521d3f3289f98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 19 Mar 2018 11:19:01 +0100 Subject: [PATCH 2/2] Minor improvement in pipeline-to-string (debug) --- GPU/Vulkan/PipelineManagerVulkan.cpp | 84 ++++++++++++++++++---------- GPU/Vulkan/PipelineManagerVulkan.h | 1 + 2 files changed, 55 insertions(+), 30 deletions(-) diff --git a/GPU/Vulkan/PipelineManagerVulkan.cpp b/GPU/Vulkan/PipelineManagerVulkan.cpp index 70870997fb..59c08b3300 100644 --- a/GPU/Vulkan/PipelineManagerVulkan.cpp +++ b/GPU/Vulkan/PipelineManagerVulkan.cpp @@ -313,6 +313,10 @@ VulkanPipeline *PipelineManagerVulkan::GetOrCreatePipeline(VkPipelineLayout layo PROFILE_THIS_SCOPE("pipelinebuild"); + std::string temp; + key.ToString(&temp); + NOTICE_LOG(G3D, "Creating: %s", temp.c_str()); + VulkanPipeline *pipeline = CreateVulkanPipeline( vulkan_->GetDevice(), pipelineCache_, layout, renderPass, rasterKey, decFmt, vs, fs, useHwTransform, lineWidth_); @@ -367,6 +371,25 @@ static const char *const compareOps[8] = { "ALWAYS", }; +static const char *const logicOps[] = { + "CLEAR", + "AND", + "AND_REV", + "COPY", + "AND_INV", + "NOOP", + "XOR", + "OR", + "NOR", + "EQUIV", + "INVERT", + "OR_REV", + "COPY_INV", + "OR_INV", + "NAND", + "SET", +}; + static const char *const stencilOps[8] = { "KEEP", "ZERO", @@ -412,27 +435,31 @@ std::string PipelineManagerVulkan::DebugGetObjectString(std::string id, DebugSha return ""; } + std::string str = pipelineKey.GetDescription(stringType); + return StringFromFormat("%p: %s", iter, str.c_str()); +} + +std::string VulkanPipelineKey::GetDescription(DebugShaderStringType stringType) const { switch (stringType) { case SHADER_STRING_SHORT_DESC: { std::stringstream str; - str << topologies[pipelineKey.raster.topology] << " "; - if (pipelineKey.raster.blendEnable) { - str << "Blend("; - str << "C:" << blendOps[pipelineKey.raster.blendOpColor] << "/" - << blendFactors[pipelineKey.raster.srcColor] << ":" << blendFactors[pipelineKey.raster.destColor] << " "; - if (pipelineKey.raster.blendOpAlpha != VK_BLEND_OP_ADD || - pipelineKey.raster.srcAlpha != VK_BLEND_FACTOR_ONE || - pipelineKey.raster.destAlpha != VK_BLEND_FACTOR_ZERO) { - str << "A:" << blendOps[pipelineKey.raster.blendOpAlpha] << "/" - << blendFactors[pipelineKey.raster.srcColor] << ":" << blendFactors[pipelineKey.raster.destColor] << " "; + str << topologies[raster.topology] << " "; + if (raster.blendEnable) { + str << "Blend(C:" << blendOps[raster.blendOpColor] << "/" + << blendFactors[raster.srcColor] << ":" << blendFactors[raster.destColor] << " "; + if (raster.blendOpAlpha != VK_BLEND_OP_ADD || + raster.srcAlpha != VK_BLEND_FACTOR_ONE || + raster.destAlpha != VK_BLEND_FACTOR_ZERO) { + str << "A:" << blendOps[raster.blendOpAlpha] << "/" + << blendFactors[raster.srcColor] << ":" << blendFactors[raster.destColor] << " "; } str << ") "; } - if (pipelineKey.raster.colorWriteMask != 0xF) { + if (raster.colorWriteMask != 0xF) { str << "Mask("; for (int i = 0; i < 4; i++) { - if (pipelineKey.raster.colorWriteMask & (1 << i)) { + if (raster.colorWriteMask & (1 << i)) { str << "RGBA"[i]; } else { str << "_"; @@ -440,37 +467,34 @@ std::string PipelineManagerVulkan::DebugGetObjectString(std::string id, DebugSha } str << ") "; } - if (pipelineKey.raster.depthTestEnable) { + if (raster.depthTestEnable) { str << "Depth("; - if (pipelineKey.raster.depthWriteEnable) + if (raster.depthWriteEnable) str << "W, "; - if (pipelineKey.raster.depthCompareOp) - str << compareOps[pipelineKey.raster.depthCompareOp & 7]; + if (raster.depthCompareOp) + str << compareOps[raster.depthCompareOp & 7]; str << ") "; } - if (pipelineKey.raster.stencilTestEnable) { + if (raster.stencilTestEnable) { str << "Stencil("; - str << compareOps[pipelineKey.raster.stencilCompareOp & 7] << " "; - str << stencilOps[pipelineKey.raster.stencilPassOp & 7] << "/"; - str << stencilOps[pipelineKey.raster.stencilFailOp & 7] << "/"; - str << stencilOps[pipelineKey.raster.stencilDepthFailOp& 7]; + str << compareOps[raster.stencilCompareOp & 7] << " "; + str << stencilOps[raster.stencilPassOp & 7] << "/"; + str << stencilOps[raster.stencilFailOp & 7] << "/"; + str << stencilOps[raster.stencilDepthFailOp& 7]; str << ") "; } - if (pipelineKey.raster.logicOpEnable) { - str << "Logic("; - str << ") "; + if (raster.logicOpEnable) { + str << "Logic(" << logicOps[raster.logicOp & 15] << ") "; } - if (pipelineKey.useHWTransform) { + if (useHWTransform) { str << "HWX "; } - if (pipelineKey.vtxFmtId) { - str << "V("; - str << StringFromFormat("%08x", pipelineKey.vtxFmtId); // TODO: Format nicer. - str << ") "; + if (vtxFmtId) { + str << "V(" << StringFromFormat("%08x", vtxFmtId) << ") "; // TODO: Format nicer. } else { str << "SWX "; } - return StringFromFormat("%p: %s", iter, str.str().c_str()); + return str.str(); } case SHADER_STRING_SOURCE_CODE: diff --git a/GPU/Vulkan/PipelineManagerVulkan.h b/GPU/Vulkan/PipelineManagerVulkan.h index 60a0582c38..1ef673b9b7 100644 --- a/GPU/Vulkan/PipelineManagerVulkan.h +++ b/GPU/Vulkan/PipelineManagerVulkan.h @@ -53,6 +53,7 @@ struct VulkanPipelineKey { void FromString(const std::string &str) { memcpy(this, &str[0], sizeof(*this)); } + std::string GetDescription(DebugShaderStringType stringType) const; }; struct StoredVulkanPipelineKey {