From ddb36a35a08af849e04f7602044082d2baa0c4e8 Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Wed, 6 Jan 2016 12:52:42 +0100 Subject: [PATCH] Some descriptor binding fixes --- GPU/Vulkan/DrawEngineVulkan.cpp | 37 +++++++++++--------- GPU/Vulkan/DrawEngineVulkan.h | 19 ++++++++-- GPU/Vulkan/FragmentShaderGeneratorVulkan.cpp | 2 +- GPU/Vulkan/VertexShaderGeneratorVulkan.cpp | 6 ++-- ext/native/thin3d/VulkanContext.h | 7 ++-- 5 files changed, 47 insertions(+), 24 deletions(-) diff --git a/GPU/Vulkan/DrawEngineVulkan.cpp b/GPU/Vulkan/DrawEngineVulkan.cpp index 72e2aba8a7..aa09c7511d 100644 --- a/GPU/Vulkan/DrawEngineVulkan.cpp +++ b/GPU/Vulkan/DrawEngineVulkan.cpp @@ -93,12 +93,13 @@ DrawEngineVulkan::DrawEngineVulkan(VulkanContext *vulkan) indexGen.Setup(decIndex); + // All resources we need for PSP drawing. Usually only bindings 0 and 2-4 are populated. VkDescriptorSetLayoutBinding bindings[5]; bindings[0].descriptorCount = 1; bindings[0].pImmutableSamplers = nullptr; bindings[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; bindings[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; - bindings[0].binding = 1; + bindings[0].binding = 0; bindings[1].descriptorCount = 1; bindings[1].pImmutableSamplers = nullptr; bindings[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; @@ -118,7 +119,7 @@ DrawEngineVulkan::DrawEngineVulkan(VulkanContext *vulkan) bindings[4].pImmutableSamplers = nullptr; bindings[4].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC; bindings[4].stageFlags = VK_SHADER_STAGE_VERTEX_BIT; - bindings[4].binding = 3; + bindings[4].binding = 4; VkDevice device = vulkan_->GetDevice(); @@ -152,6 +153,7 @@ DrawEngineVulkan::DrawEngineVulkan(VulkanContext *vulkan) for (int i = 0; i < 2; i++) { VkResult res = vkCreateDescriptorPool(vulkan_->GetDevice(), &dp, nullptr, &frame_[i].descPool); assert(VK_SUCCESS == res); + frame_[i].pushData = new VulkanPushBuffer(vulkan_, 4 * 1024 * 1024); // TODO: Do something more dynamic } VkPipelineLayoutCreateInfo pl; @@ -166,6 +168,12 @@ DrawEngineVulkan::DrawEngineVulkan(VulkanContext *vulkan) assert(VK_SUCCESS == res); } +void DrawEngineVulkan::BeginFrame() { + FrameData *frame = &frame_[curFrame_ & 1]; + vkResetDescriptorPool(vulkan_->GetDevice(), frame->descPool, 0); + frame->pushData->Reset(); +} + void DrawEngineVulkan::EndFrame() { curFrame_++; } @@ -176,6 +184,11 @@ DrawEngineVulkan::~DrawEngineVulkan() { FreeMemoryPages(splineBuffer, SPLINE_BUFFER_SIZE); FreeMemoryPages(transformed, TRANSFORMED_VERTEX_BUFFER_SIZE); FreeMemoryPages(transformedExpanded, 3 * TRANSFORMED_VERTEX_BUFFER_SIZE); + + for (int i = 0; i < 2; i++) { + vulkan_->QueueDelete(frame_[i].descPool); + delete frame_[i].pushData; + } } VertexDecoder *DrawEngineVulkan::GetVertexDecoder(u32 vtype) { @@ -350,20 +363,11 @@ inline u32 ComputeMiniHashRange(const void *ptr, size_t sz) { } } -/* -struct DescriptorSetKey { - void *texture_; - void *secondaryTexture_; - - bool operator < (const DescriptorSetKey &other) const { - if (texture_ < other.texture_) return true; else if (texture_ > other.texture_) return false; - if (secondaryTexture_ < other.secondaryTexture_) return true; else if (secondaryTexture_ > other.secondaryTexture_) return false; - return false; - } -}; -*/ +VkDescriptorSet DrawEngineVulkan::GetDescriptorSet(CachedTextureVulkan *texture) { + DescriptorSetKey key; + key.texture_ = texture; + key.secondaryTexture_ = nullptr; -VkDescriptorSet DrawEngineVulkan::GetDescriptorSet() { return nullptr; } @@ -373,9 +377,10 @@ void DrawEngineVulkan::DoFlush(VkCommandBuffer cmd) { FrameData *frame = &frame_[curFrame_ & 1]; + CachedTextureVulkan *tex = nullptr; // This is not done on every drawcall, we should collect vertex data // until critical state changes. That's when we draw (flush). - VkDescriptorSet ds = GetDescriptorSet(); + VkDescriptorSet ds = GetDescriptorSet(tex); GEPrimitiveType prim = prevPrim_; // ApplyDrawState(prim); diff --git a/GPU/Vulkan/DrawEngineVulkan.h b/GPU/Vulkan/DrawEngineVulkan.h index 05f3911cb8..b14bd52477 100644 --- a/GPU/Vulkan/DrawEngineVulkan.h +++ b/GPU/Vulkan/DrawEngineVulkan.h @@ -29,7 +29,7 @@ // The format of the various uniform buffers may vary though - vertex shaders that don't skin // won't get any bone data, etc. - +#include #include #include "GPU/Vulkan/VulkanUtil.h" @@ -48,6 +48,7 @@ class ShaderManagerVulkan; class PipelineManagerVulkan; class TextureCacheVulkan; class FramebufferManagerVulkan; +class CachedTextureVulkan; // Avoiding the full include of TextureDecoder.h. #if (defined(_M_SSE) && defined(_M_X64)) || defined(ARM64) @@ -140,6 +141,7 @@ public: return pipelineLayout_; } + void BeginFrame(); void EndFrame(); private: @@ -147,7 +149,7 @@ private: void DecodeVertsStep(); void DoFlush(VkCommandBuffer cmd); - VkDescriptorSet GetDescriptorSet(); + VkDescriptorSet GetDescriptorSet(CachedTextureVulkan *texture); VertexDecoder *GetVertexDecoder(u32 vtype); @@ -157,10 +159,23 @@ private: VkDescriptorSetLayout descriptorSetLayout_; VkPipelineLayout pipelineLayout_; + struct DescriptorSetKey { + void *texture_; + void *secondaryTexture_; + + bool operator < (const DescriptorSetKey &other) const { + if (texture_ < other.texture_) return true; else if (texture_ > other.texture_) return false; + if (secondaryTexture_ < other.secondaryTexture_) return true; else if (secondaryTexture_ > other.secondaryTexture_) return false; + return false; + } + }; + // We alternate between these. struct FrameData { VkDescriptorPool descPool; VulkanPushBuffer *pushData; + // We do rolling allocation and reset instead of caching across frames. That we might do later. + std::map descSets; }; int curFrame_; diff --git a/GPU/Vulkan/FragmentShaderGeneratorVulkan.cpp b/GPU/Vulkan/FragmentShaderGeneratorVulkan.cpp index 56c3ad5947..c42b1bbc82 100644 --- a/GPU/Vulkan/FragmentShaderGeneratorVulkan.cpp +++ b/GPU/Vulkan/FragmentShaderGeneratorVulkan.cpp @@ -92,7 +92,7 @@ bool GenerateVulkanGLSLFragmentShader(const ShaderID &id, char *buffer) { const char *shading = doFlatShading ? "flat" : ""; - WRITE(p, "layout (std140, set = 0, binding = 3) uniform baseUBO {\n%s} base;\n", ub_baseStr); + WRITE(p, "layout (std140, set = 0, binding = 2) uniform baseUBO {\n%s} base;\n", ub_baseStr); if (doTexture) { WRITE(p, "layout (binding = 0) uniform sampler2D tex;\n"); } diff --git a/GPU/Vulkan/VertexShaderGeneratorVulkan.cpp b/GPU/Vulkan/VertexShaderGeneratorVulkan.cpp index 7cb9e1e0b7..dc308e344f 100644 --- a/GPU/Vulkan/VertexShaderGeneratorVulkan.cpp +++ b/GPU/Vulkan/VertexShaderGeneratorVulkan.cpp @@ -138,11 +138,11 @@ bool GenerateVulkanGLSLVertexShader(const ShaderID &id, char *buffer) { // are present and what parts aren't, but we will not be ultra detailed about it. WRITE(p, "\n"); - WRITE(p, "layout (std140, set = 0, binding = 3) uniform baseVars {\n%s\n} base;\n", ub_baseStr); + WRITE(p, "layout (std140, set = 0, binding = 2) uniform baseVars {\n%s\n} base;\n", ub_baseStr); if (enableLighting) - WRITE(p, "layout (std140, set = 0, binding = 4) uniform lightVars {\n%s\n} light;\n", ub_vs_lightsStr); + WRITE(p, "layout (std140, set = 0, binding = 3) uniform lightVars {\n%s\n} light;\n", ub_vs_lightsStr); if (enableBones) - WRITE(p, "layout (std140, set = 0, binding = 5) uniform boneVars {\n%s\n} bone;\n", ub_vs_bonesStr); + WRITE(p, "layout (std140, set = 0, binding = 4) uniform boneVars {\n%s\n} bone;\n", ub_vs_bonesStr); const char *shading = doFlatShading ? "flat " : ""; diff --git a/ext/native/thin3d/VulkanContext.h b/ext/native/thin3d/VulkanContext.h index 2f47eeb394..df3fc7d4a7 100644 --- a/ext/native/thin3d/VulkanContext.h +++ b/ext/native/thin3d/VulkanContext.h @@ -68,6 +68,7 @@ struct layer_properties { // This is a bit repetitive... class VulkanDeleteList { public: + void QueueDelete(VkDescriptorPool pool) { descPools_.push_back(pool); } void QueueDelete(VkShaderModule module) { modules_.push_back(module); } void QueueDelete(VkBuffer buffer) { buffers_.push_back(buffer); } void QueueDelete(VkBufferView bufferView) { bufferViews_.push_back(bufferView); } @@ -76,6 +77,7 @@ public: void QueueDelete(VkDeviceMemory deviceMemory) { memory_.push_back(deviceMemory); } void Ingest(VulkanDeleteList &del) { + descPools_ = std::move(del.descPools_); modules_ = std::move(del.modules_); buffers_ = std::move(del.buffers_); bufferViews_ = std::move(del.bufferViews_); @@ -85,8 +87,8 @@ public: } void PerformDeletes(VkDevice device) { - for (auto &module : modules_) { - vkDestroyShaderModule(device, module, nullptr); + for (auto &descPool : descPools_) { + vkDestroyDescriptorPool(device, descPool, nullptr); } modules_.clear(); for (auto &buf : buffers_) { @@ -112,6 +114,7 @@ public: } private: + std::vector descPools_; std::vector modules_; std::vector buffers_; std::vector bufferViews_;