From af406df7938dccaca1a0a4230122b3326ded286f Mon Sep 17 00:00:00 2001 From: DrChat Date: Sun, 18 Feb 2018 18:11:52 -0600 Subject: [PATCH] [Vulkan] Move the constant descriptor set to its own setup/destroy functions --- src/xenia/gpu/vulkan/buffer_cache.cc | 67 +++++++++++++++++----------- src/xenia/gpu/vulkan/buffer_cache.h | 14 +++--- 2 files changed, 51 insertions(+), 30 deletions(-) diff --git a/src/xenia/gpu/vulkan/buffer_cache.cc b/src/xenia/gpu/vulkan/buffer_cache.cc index 7076004d3..2db1b3b65 100644 --- a/src/xenia/gpu/vulkan/buffer_cache.cc +++ b/src/xenia/gpu/vulkan/buffer_cache.cc @@ -126,22 +126,34 @@ VkResult BufferCache::Initialize() { return status; } + status = CreateConstantDescriptorSet(); + if (status != VK_SUCCESS) { + return status; + } + + return VK_SUCCESS; +} + +VkResult BufferCache::CreateConstantDescriptorSet() { + VkResult status = VK_SUCCESS; + // Descriptor pool used for all of our cached descriptors. // In the steady state we don't allocate anything, so these are all manually // managed. - VkDescriptorPoolCreateInfo descriptor_pool_info; - descriptor_pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; - descriptor_pool_info.pNext = nullptr; - descriptor_pool_info.flags = + VkDescriptorPoolCreateInfo transient_descriptor_pool_info; + transient_descriptor_pool_info.sType = + VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; + transient_descriptor_pool_info.pNext = nullptr; + transient_descriptor_pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT; - descriptor_pool_info.maxSets = 1; + transient_descriptor_pool_info.maxSets = 1; VkDescriptorPoolSize pool_sizes[1]; pool_sizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC; pool_sizes[0].descriptorCount = 2; - descriptor_pool_info.poolSizeCount = 1; - descriptor_pool_info.pPoolSizes = pool_sizes; - status = vkCreateDescriptorPool(*device_, &descriptor_pool_info, nullptr, - &descriptor_pool_); + transient_descriptor_pool_info.poolSizeCount = 1; + transient_descriptor_pool_info.pPoolSizes = pool_sizes; + status = vkCreateDescriptorPool(*device_, &transient_descriptor_pool_info, + nullptr, &constant_descriptor_pool_); if (status != VK_SUCCESS) { return status; } @@ -173,8 +185,9 @@ VkResult BufferCache::Initialize() { descriptor_set_layout_info.bindingCount = static_cast(xe::countof(bindings)); descriptor_set_layout_info.pBindings = bindings; - status = vkCreateDescriptorSetLayout(*device_, &descriptor_set_layout_info, - nullptr, &descriptor_set_layout_); + status = + vkCreateDescriptorSetLayout(*device_, &descriptor_set_layout_info, + nullptr, &constant_descriptor_set_layout_); if (status != VK_SUCCESS) { return status; } @@ -185,11 +198,11 @@ VkResult BufferCache::Initialize() { VkDescriptorSetAllocateInfo set_alloc_info; set_alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; set_alloc_info.pNext = nullptr; - set_alloc_info.descriptorPool = descriptor_pool_; + set_alloc_info.descriptorPool = constant_descriptor_pool_; set_alloc_info.descriptorSetCount = 1; - set_alloc_info.pSetLayouts = &descriptor_set_layout_; + set_alloc_info.pSetLayouts = &constant_descriptor_set_layout_; status = vkAllocateDescriptorSets(*device_, &set_alloc_info, - &transient_descriptor_set_); + &constant_descriptor_set_); if (status != VK_SUCCESS) { return status; } @@ -204,7 +217,7 @@ VkResult BufferCache::Initialize() { auto& vertex_uniform_binding_write = descriptor_writes[0]; vertex_uniform_binding_write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; vertex_uniform_binding_write.pNext = nullptr; - vertex_uniform_binding_write.dstSet = transient_descriptor_set_; + vertex_uniform_binding_write.dstSet = constant_descriptor_set_; vertex_uniform_binding_write.dstBinding = 0; vertex_uniform_binding_write.dstArrayElement = 0; vertex_uniform_binding_write.descriptorCount = 1; @@ -214,7 +227,7 @@ VkResult BufferCache::Initialize() { auto& fragment_uniform_binding_write = descriptor_writes[1]; fragment_uniform_binding_write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; fragment_uniform_binding_write.pNext = nullptr; - fragment_uniform_binding_write.dstSet = transient_descriptor_set_; + fragment_uniform_binding_write.dstSet = constant_descriptor_set_; fragment_uniform_binding_write.dstBinding = 1; fragment_uniform_binding_write.dstArrayElement = 0; fragment_uniform_binding_write.descriptorCount = 1; @@ -225,6 +238,18 @@ VkResult BufferCache::Initialize() { return VK_SUCCESS; } +void xe::gpu::vulkan::BufferCache::FreeConstantDescriptorSet() { + if (constant_descriptor_set_) { + vkFreeDescriptorSets(*device_, constant_descriptor_pool_, 1, + &constant_descriptor_set_); + constant_descriptor_set_ = nullptr; + } + + VK_SAFE_DESTROY(vkDestroyDescriptorSetLayout, *device_, + constant_descriptor_set_layout_, nullptr); + VK_SAFE_DESTROY(vkDestroyDescriptorPool, *device_, constant_descriptor_pool_, + nullptr); +} void BufferCache::Shutdown() { if (mem_allocator_) { @@ -232,15 +257,7 @@ void BufferCache::Shutdown() { mem_allocator_ = nullptr; } - if (transient_descriptor_set_) { - vkFreeDescriptorSets(*device_, descriptor_pool_, 1, - &transient_descriptor_set_); - transient_descriptor_set_ = nullptr; - } - - VK_SAFE_DESTROY(vkDestroyDescriptorSetLayout, *device_, - descriptor_set_layout_, nullptr); - VK_SAFE_DESTROY(vkDestroyDescriptorPool, *device_, descriptor_pool_, nullptr); + FreeConstantDescriptorSet(); transient_buffer_->Shutdown(); VK_SAFE_DESTROY(vkFreeMemory, *device_, gpu_memory_pool_, nullptr); diff --git a/src/xenia/gpu/vulkan/buffer_cache.h b/src/xenia/gpu/vulkan/buffer_cache.h index 2f321f26f..edef36180 100644 --- a/src/xenia/gpu/vulkan/buffer_cache.h +++ b/src/xenia/gpu/vulkan/buffer_cache.h @@ -45,10 +45,10 @@ class BufferCache { // binding = 0: for use in vertex shaders // binding = 1: for use in fragment shaders VkDescriptorSet constant_descriptor_set() const { - return transient_descriptor_set_; + return constant_descriptor_set_; } VkDescriptorSetLayout constant_descriptor_set_layout() const { - return descriptor_set_layout_; + return constant_descriptor_set_layout_; } // Uploads the constants specified in the register maps to the transient @@ -106,6 +106,9 @@ class BufferCache { VmaAllocationInfo alloc_info; }; + VkResult CreateConstantDescriptorSet(); + void FreeConstantDescriptorSet(); + // Allocates a block of memory in the transient buffer. // When memory is not available fences are checked and space is reclaimed. // Returns VK_WHOLE_SIZE if requested amount of memory is not available. @@ -133,9 +136,10 @@ class BufferCache { std::unique_ptr transient_buffer_ = nullptr; std::map> transient_cache_; - VkDescriptorPool descriptor_pool_ = nullptr; - VkDescriptorSetLayout descriptor_set_layout_ = nullptr; - VkDescriptorSet transient_descriptor_set_ = nullptr; + // Descriptor set used to hold vertex/pixel shader float constants + VkDescriptorPool constant_descriptor_pool_ = nullptr; + VkDescriptorSetLayout constant_descriptor_set_layout_ = nullptr; + VkDescriptorSet constant_descriptor_set_ = nullptr; }; } // namespace vulkan