Skip to content

Commit

Permalink
SSBO fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-cojocaru committed Jan 8, 2025
1 parent 48675cb commit 4c22c5a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 29 deletions.
8 changes: 2 additions & 6 deletions include/mbgl/vulkan/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@ class Context final : public gfx::Context {
RenderStaticData& staticData,
const std::vector<shaders::ClipUBO>& tileUBOs);

const std::unique_ptr<BufferResource>& getDummyVertexBuffer();
const std::unique_ptr<BufferResource>& getDummyUniformBuffer();
const std::unique_ptr<BufferResource>& getDummyStorageBuffer();
const std::unique_ptr<BufferResource>& getDummyBuffer();
const std::unique_ptr<Texture2D>& getDummyTexture();

const vk::DescriptorSetLayout& getDescriptorSetLayout(DescriptorSetType type);
Expand Down Expand Up @@ -190,9 +188,7 @@ class Context final : public gfx::Context {
vulkan::UniformBufferArray globalUniformBuffers;
std::unordered_map<DescriptorSetType, DescriptorPoolGrowable> descriptorPoolMap;

std::unique_ptr<BufferResource> dummyVertexBuffer;
std::unique_ptr<BufferResource> dummyUniformBuffer;
std::unique_ptr<BufferResource> dummyStorageBuffer;
std::unique_ptr<BufferResource> dummyBuffer;
std::unique_ptr<Texture2D> dummyTexture2D;
vk::UniqueDescriptorSetLayout globalUniformDescriptorSetLayout;
vk::UniqueDescriptorSetLayout layerUniformDescriptorSetLayout;
Expand Down
3 changes: 2 additions & 1 deletion include/mbgl/vulkan/uniform_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 12 additions & 1 deletion src/mbgl/vulkan/buffer_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <mbgl/util/instrumentation.hpp>

#include <algorithm>
#include <numeric>

namespace mbgl {
namespace vulkan {
Expand Down Expand Up @@ -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);
Expand Down
25 changes: 7 additions & 18 deletions src/mbgl/vulkan/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,25 +563,14 @@ bool Context::renderTileClippingMasks(gfx::RenderPass& renderPass,
return true;
}

const std::unique_ptr<BufferResource>& Context::getDummyVertexBuffer() {
if (!dummyVertexBuffer)
dummyVertexBuffer = std::make_unique<BufferResource>(
*this, nullptr, 16, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, false);
return dummyVertexBuffer;
}

const std::unique_ptr<BufferResource>& Context::getDummyUniformBuffer() {
if (!dummyUniformBuffer)
dummyUniformBuffer = std::make_unique<BufferResource>(
*this, nullptr, 16, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, false);
return dummyUniformBuffer;
}
const std::unique_ptr<BufferResource>& 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<BufferResource>(*this, nullptr, 16, usage, false);
}

const std::unique_ptr<BufferResource>& Context::getDummyStorageBuffer() {
if (!dummyStorageBuffer)
dummyStorageBuffer = std::make_unique<BufferResource>(
*this, nullptr, 16, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, false);
return dummyStorageBuffer;
return dummyBuffer;
}

const std::unique_ptr<Texture2D>& Context::getDummyTexture() {
Expand Down
3 changes: 1 addition & 2 deletions src/mbgl/vulkan/descriptor_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/vulkan/uniform_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down

0 comments on commit 4c22c5a

Please sign in to comment.