From 4c22c5abd368367d455af06b6c64b144ca0337cd Mon Sep 17 00:00:00 2001 From: Adrian Cojocaru Date: Wed, 8 Jan 2025 19:49:52 +0200 Subject: [PATCH] SSBO fixes --- include/mbgl/vulkan/context.hpp | 8 ++------ include/mbgl/vulkan/uniform_buffer.hpp | 3 ++- src/mbgl/vulkan/buffer_resource.cpp | 13 ++++++++++++- src/mbgl/vulkan/context.cpp | 25 +++++++------------------ src/mbgl/vulkan/descriptor_set.cpp | 3 +-- src/mbgl/vulkan/uniform_buffer.cpp | 2 +- 6 files changed, 25 insertions(+), 29 deletions(-) diff --git a/include/mbgl/vulkan/context.hpp b/include/mbgl/vulkan/context.hpp index abbc9e3985a..da4a6cd94a1 100644 --- a/include/mbgl/vulkan/context.hpp +++ b/include/mbgl/vulkan/context.hpp @@ -136,9 +136,7 @@ class Context final : public gfx::Context { RenderStaticData& staticData, const std::vector& tileUBOs); - const std::unique_ptr& getDummyVertexBuffer(); - const std::unique_ptr& getDummyUniformBuffer(); - const std::unique_ptr& getDummyStorageBuffer(); + const std::unique_ptr& getDummyBuffer(); const std::unique_ptr& getDummyTexture(); const vk::DescriptorSetLayout& getDescriptorSetLayout(DescriptorSetType type); @@ -190,9 +188,7 @@ class Context final : public gfx::Context { vulkan::UniformBufferArray globalUniformBuffers; std::unordered_map descriptorPoolMap; - std::unique_ptr dummyVertexBuffer; - std::unique_ptr dummyUniformBuffer; - std::unique_ptr dummyStorageBuffer; + std::unique_ptr dummyBuffer; std::unique_ptr dummyTexture2D; vk::UniqueDescriptorSetLayout globalUniformDescriptorSetLayout; vk::UniqueDescriptorSetLayout layerUniformDescriptorSetLayout; diff --git a/include/mbgl/vulkan/uniform_buffer.hpp b/include/mbgl/vulkan/uniform_buffer.hpp index 91b648097dc..d2a1be8d94c 100644 --- a/include/mbgl/vulkan/uniform_buffer.hpp +++ b/include/mbgl/vulkan/uniform_buffer.hpp @@ -42,7 +42,8 @@ class UniformBufferArray final : public gfx::UniformBufferArray { : gfx::UniformBufferArray(std::move(other)), descriptorSetType(other.descriptorSetType), descriptorStartIndex(other.descriptorStartIndex), - descriptorBindingCount(other.descriptorBindingCount), + descriptorStorageCount(other.descriptorStorageCount), + descriptorUniformCount(other.descriptorUniformCount), descriptorSet(std::move(other.descriptorSet)) {} UniformBufferArray(const UniformBufferArray&) = delete; diff --git a/src/mbgl/vulkan/buffer_resource.cpp b/src/mbgl/vulkan/buffer_resource.cpp index 6bf949e456b..a246473468e 100644 --- a/src/mbgl/vulkan/buffer_resource.cpp +++ b/src/mbgl/vulkan/buffer_resource.cpp @@ -6,6 +6,7 @@ #include #include +#include namespace mbgl { namespace vulkan { @@ -59,7 +60,17 @@ BufferResource::BufferResource( if (usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT || usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT) { const auto& backend = context.getBackend(); const auto& deviceProps = backend.getDeviceProperties(); - const auto& align = deviceProps.limits.minUniformBufferOffsetAlignment; + + vk::DeviceSize align = 0; + if (usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT) { + align = deviceProps.limits.minUniformBufferOffsetAlignment; + } + + if (usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT) { + align = align ? std::lcm(align, deviceProps.limits.minStorageBufferOffsetAlignment) + : deviceProps.limits.minStorageBufferOffsetAlignment; + } + bufferWindowSize = (size + align - 1) & ~(align - 1); assert(bufferWindowSize != 0); diff --git a/src/mbgl/vulkan/context.cpp b/src/mbgl/vulkan/context.cpp index daf244e1e28..678ca378da4 100644 --- a/src/mbgl/vulkan/context.cpp +++ b/src/mbgl/vulkan/context.cpp @@ -563,25 +563,14 @@ bool Context::renderTileClippingMasks(gfx::RenderPass& renderPass, return true; } -const std::unique_ptr& Context::getDummyVertexBuffer() { - if (!dummyVertexBuffer) - dummyVertexBuffer = std::make_unique( - *this, nullptr, 16, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, false); - return dummyVertexBuffer; -} - -const std::unique_ptr& Context::getDummyUniformBuffer() { - if (!dummyUniformBuffer) - dummyUniformBuffer = std::make_unique( - *this, nullptr, 16, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, false); - return dummyUniformBuffer; -} +const std::unique_ptr& Context::getDummyBuffer() { + if (!dummyBuffer) { + const uint32_t usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | + VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; + dummyBuffer = std::make_unique(*this, nullptr, 16, usage, false); + } -const std::unique_ptr& Context::getDummyStorageBuffer() { - if (!dummyStorageBuffer) - dummyStorageBuffer = std::make_unique( - *this, nullptr, 16, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, false); - return dummyStorageBuffer; + return dummyBuffer; } const std::unique_ptr& Context::getDummyTexture() { diff --git a/src/mbgl/vulkan/descriptor_set.cpp b/src/mbgl/vulkan/descriptor_set.cpp index ed688f92d05..488e940728a 100644 --- a/src/mbgl/vulkan/descriptor_set.cpp +++ b/src/mbgl/vulkan/descriptor_set.cpp @@ -165,8 +165,7 @@ void UniformDescriptorSet::update(const gfx::UniformBufferArray& uniforms, .setOffset(bufferResource.getVulkanBufferOffset()) .setRange(bufferResource.getSizeInBytes()); } else { - const auto& dummyBuffer = index < descriptorStorageCount ? context.getDummyStorageBuffer() - : context.getDummyUniformBuffer(); + const auto& dummyBuffer = context.getDummyBuffer(); descriptorBufferInfo.setBuffer(dummyBuffer->getVulkanBuffer()).setOffset(0).setRange(VK_WHOLE_SIZE); } diff --git a/src/mbgl/vulkan/uniform_buffer.cpp b/src/mbgl/vulkan/uniform_buffer.cpp index 99cb4287ccf..839622a0dc3 100644 --- a/src/mbgl/vulkan/uniform_buffer.cpp +++ b/src/mbgl/vulkan/uniform_buffer.cpp @@ -79,7 +79,7 @@ void UniformBufferArray::bindDescriptorSets(CommandEncoder& encoder) { const int32_t currentIndex = encoder.getContext().getCurrentFrameResourceIndex(); const int32_t prevIndex = currentIndex == 0 ? frameCount - 1 : currentIndex - 1; - for (uint32_t i = 0; i < descriptorBindingCount; ++i) { + for (uint32_t i = 0; i < descriptorStorageCount + descriptorUniformCount; ++i) { const uint32_t index = descriptorStartIndex + i; if (!uniformBufferVector[index]) {