Group descriptor sets by binding frequency (#2892)

This commit is contained in:
Adrian Cojocaru
2024-10-16 18:31:15 +03:00
committed by GitHub
parent e937b0d555
commit b29c9967da
90 changed files with 908 additions and 562 deletions

View File

@@ -28,7 +28,7 @@
#include <cstring>
#ifndef MLN_VULKAN_DESCRIPTOR_POOL_SIZE
#define MLN_VULKAN_DESCRIPTOR_POOL_SIZE 10000
#define MLN_VULKAN_DESCRIPTOR_POOL_SIZE 30000
#endif
namespace mbgl {
@@ -48,7 +48,8 @@ public:
Context::Context(RendererBackend& backend_)
: gfx::Context(vulkan::maximumVertexBindingCount),
backend(backend_) {
backend(backend_),
globalUniformBuffers(DescriptorSetType::Global, 0, shaders::globalUBOCount) {
if (glslangRefCount++ == 0) {
glslang::InitializeProcess();
}
@@ -70,26 +71,28 @@ void Context::initFrameResources() {
const auto& device = backend.getDevice();
const auto frameCount = backend.getMaxFrames();
// command buffers
const vk::CommandBufferAllocateInfo allocateInfo(
backend.getCommandPool().get(), vk::CommandBufferLevel::ePrimary, frameCount);
auto commandBuffers = backend.getDevice()->allocateCommandBuffersUnique(allocateInfo);
// descriptor pool info
const std::vector<vk::DescriptorPoolSize> poolSizes = {
{vk::DescriptorType::eUniformBuffer, MLN_VULKAN_DESCRIPTOR_POOL_SIZE},
{vk::DescriptorType::eCombinedImageSampler, MLN_VULKAN_DESCRIPTOR_POOL_SIZE},
};
const auto descriptorPoolInfo = vk::DescriptorPoolCreateInfo().setPoolSizes(poolSizes).setMaxSets(
MLN_VULKAN_DESCRIPTOR_POOL_SIZE);
const auto descriptorPoolInfo = vk::DescriptorPoolCreateInfo(vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet)
.setPoolSizes(poolSizes)
.setMaxSets(MLN_VULKAN_DESCRIPTOR_POOL_SIZE);
globalDescriptorPool = device->createDescriptorPoolUnique(descriptorPoolInfo);
// command buffers
const vk::CommandBufferAllocateInfo allocateInfo(
backend.getCommandPool().get(), vk::CommandBufferLevel::ePrimary, frameCount);
auto commandBuffers = backend.getDevice()->allocateCommandBuffersUnique(allocateInfo);
frameResources.reserve(frameCount);
for (uint32_t index = 0; index < frameCount; ++index) {
frameResources.emplace_back(commandBuffers[index],
device->createDescriptorPoolUnique(descriptorPoolInfo),
device->createSemaphoreUnique({}),
device->createSemaphoreUnique({}),
device->createFenceUnique(vk::FenceCreateInfo(vk::FenceCreateFlagBits::eSignaled)));
@@ -97,7 +100,6 @@ void Context::initFrameResources() {
const auto& frame = frameResources.back();
backend.setDebugName(frame.commandBuffer.get(), "FrameCommandBuffer_" + std::to_string(index));
backend.setDebugName(frame.descriptorPool.get(), "DescriptorPool_" + std::to_string(index));
backend.setDebugName(frame.frameSemaphore.get(), "FrameSemaphore_" + std::to_string(index));
backend.setDebugName(frame.surfaceSemaphore.get(), "SurfaceSemaphore_" + std::to_string(index));
backend.setDebugName(frame.flightFrameFence.get(), "FrameFence_" + std::to_string(index));
@@ -114,12 +116,14 @@ void Context::destroyResources() {
frame.runDeletionQueue(*this);
}
globalUniformBuffers.freeDescriptorSets();
// all resources have unique handles
frameResources.clear();
}
const vk::UniqueDescriptorPool& Context::getCurrentDescriptorPool() const {
return frameResources[frameResourceIndex].descriptorPool;
const vk::UniqueDescriptorPool& Context::getGlobalDescriptorPool() const {
return globalDescriptorPool;
}
void Context::enqueueDeletion(std::function<void(const Context&)>&& function) {
@@ -131,7 +135,7 @@ void Context::enqueueDeletion(std::function<void(const Context&)>&& function) {
frameResources[frameResourceIndex].deletionQueue.push_back(std::move(function));
}
void Context::submitOneTimeCommand(const std::function<void(const vk::UniqueCommandBuffer&)>& function) {
void Context::submitOneTimeCommand(const std::function<void(const vk::UniqueCommandBuffer&)>& function) const {
const vk::CommandBufferAllocateInfo allocateInfo(
backend.getCommandPool().get(), vk::CommandBufferLevel::ePrimary, 1);
@@ -178,7 +182,6 @@ void Context::beginFrame() {
// we wait for an idle device to recreate the swapchain
// so it's a good opportunity to delete all queued items
for (auto& frame : frameResources) {
device->resetDescriptorPool(frame.descriptorPool.get());
frame.runDeletionQueue(*this);
}
@@ -194,7 +197,6 @@ void Context::beginFrame() {
waitFrame();
device->resetDescriptorPool(getCurrentDescriptorPool().get());
frame.runDeletionQueue(*this);
if (platformSurface) {
@@ -390,7 +392,10 @@ void Context::clearStencilBuffer(int32_t) {
assert(false);
}
void Context::bindGlobalUniformBuffers(gfx::RenderPass&) const noexcept {}
void Context::bindGlobalUniformBuffers(gfx::RenderPass& renderPass) const noexcept {
auto& renderPassImpl = static_cast<RenderPass&>(renderPass);
const_cast<Context*>(this)->globalUniformBuffers.bindDescriptorSets(renderPassImpl.getEncoder());
}
bool Context::renderTileClippingMasks(gfx::RenderPass& renderPass,
RenderStaticData& staticData,
@@ -517,27 +522,30 @@ const std::unique_ptr<Texture2D>& Context::getDummyTexture() {
return dummyTexture2D;
}
const vk::UniqueDescriptorSetLayout& Context::getUniformDescriptorSetLayout() {
if (!uniformDescriptorSetLayout) {
std::vector<vk::DescriptorSetLayoutBinding> bindings;
const auto stageFlags = vk::ShaderStageFlags() | vk::ShaderStageFlagBits::eVertex |
vk::ShaderStageFlagBits::eFragment;
for (size_t i = 0; i < shaders::maxUBOCountPerShader; ++i) {
bindings.push_back(vk::DescriptorSetLayoutBinding()
.setBinding(static_cast<uint32_t>(i))
.setStageFlags(stageFlags)
.setDescriptorType(vk::DescriptorType::eUniformBuffer)
.setDescriptorCount(1));
}
const auto descriptorSetLayoutCreateInfo = vk::DescriptorSetLayoutCreateInfo().setBindings(bindings);
uniformDescriptorSetLayout = backend.getDevice()->createDescriptorSetLayoutUnique(
descriptorSetLayoutCreateInfo);
backend.setDebugName(uniformDescriptorSetLayout.get(), "UniformDescriptorSetLayout");
const vk::UniqueDescriptorSetLayout& Context::buildUniformDescriptorSetLayout(vk::UniqueDescriptorSetLayout& layout,
size_t uniformCount,
const std::string& name) {
if (layout) {
return layout;
}
return uniformDescriptorSetLayout;
std::vector<vk::DescriptorSetLayoutBinding> bindings;
const auto stageFlags = vk::ShaderStageFlags() | vk::ShaderStageFlagBits::eVertex |
vk::ShaderStageFlagBits::eFragment;
for (size_t i = 0; i < uniformCount; ++i) {
bindings.push_back(vk::DescriptorSetLayoutBinding()
.setBinding(i)
.setStageFlags(stageFlags)
.setDescriptorType(vk::DescriptorType::eUniformBuffer)
.setDescriptorCount(1));
}
const auto descriptorSetLayoutCreateInfo = vk::DescriptorSetLayoutCreateInfo().setBindings(bindings);
layout = backend.getDevice()->createDescriptorSetLayoutUnique(descriptorSetLayoutCreateInfo);
backend.setDebugName(layout.get(), name);
return layout;
}
const vk::UniqueDescriptorSetLayout& Context::getImageDescriptorSetLayout() {
@@ -562,12 +570,29 @@ const vk::UniqueDescriptorSetLayout& Context::getImageDescriptorSetLayout() {
const std::vector<vk::DescriptorSetLayout>& Context::getDescriptorSetLayouts() {
if (descriptorSetLayouts.empty()) {
descriptorSetLayouts = {getUniformDescriptorSetLayout().get(), getImageDescriptorSetLayout().get()};
descriptorSetLayouts = {
buildUniformDescriptorSetLayout(
globalUniformDescriptorSetLayout, shaders::globalUBOCount, "GlobalUniformDescriptorSetLayout")
.get(),
buildUniformDescriptorSetLayout(
layerUniformDescriptorSetLayout, shaders::maxUBOCountPerLayer, "LayerUniformDescriptorSetLayout")
.get(),
buildUniformDescriptorSetLayout(drawableUniformDescriptorSetLayout,
shaders::maxUBOCountPerDrawable,
"DrawableUniformDescriptorSetLayout")
.get(),
getImageDescriptorSetLayout().get()};
}
return descriptorSetLayouts;
}
const vk::DescriptorSetLayout& Context::getDescriptorSetLayout(DescriptorSetType type) {
uint32_t index = static_cast<uint32_t>(type);
assert(index < static_cast<uint32_t>(DescriptorSetType::Count));
return getDescriptorSetLayouts()[index];
}
const vk::UniquePipelineLayout& Context::getGeneralPipelineLayout() {
if (generalPipelineLayout) return generalPipelineLayout;