From 4fc07050ea6422010c307cecd533ebc19bf8ee2a Mon Sep 17 00:00:00 2001 From: Adrian Cojocaru Date: Mon, 16 Dec 2024 16:05:54 +0200 Subject: [PATCH 1/7] Duplicate vertex buffers when updating --- src/mbgl/vulkan/upload_pass.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/mbgl/vulkan/upload_pass.cpp b/src/mbgl/vulkan/upload_pass.cpp index 57b05eb64a0..13517d494a6 100644 --- a/src/mbgl/vulkan/upload_pass.cpp +++ b/src/mbgl/vulkan/upload_pass.cpp @@ -96,11 +96,11 @@ const gfx::UniqueVertexBufferResource& UploadPass::getBuffer(const gfx::VertexVe // If it's changed, update it if (rawBufSize <= resource.getSizeInBytes()) { - if (forceUpdate || vec->isModifiedAfter(resource.getLastUpdated())) { - updateVertexBufferResource(resource, rawBufPtr, rawBufSize); - resource.setLastUpdated(vec->getLastModified()); + if (vec->isModifiedAfter(resource.getLastUpdated())) { + //updateVertexBufferResource(resource, rawBufPtr, rawBufSize); + } else { + return rawData->resource; } - return rawData->resource; } } // Otherwise, create a new one @@ -108,6 +108,10 @@ const gfx::UniqueVertexBufferResource& UploadPass::getBuffer(const gfx::VertexVe auto buffer = std::make_unique(); buffer->resource = createVertexBufferResource(rawBufPtr, rawBufSize, usage, /*persistent=*/false); vec->setBuffer(std::move(buffer)); + + auto* rawData = static_cast(vec->getBuffer()); + auto& resource = static_cast(*rawData->resource); + resource.setLastUpdated(vec->getLastModified()); return static_cast(vec->getBuffer())->resource; } } From c6f08c67251c1008d12c228d8970f8dee8b7074b Mon Sep 17 00:00:00 2001 From: Adrian Cojocaru Date: Mon, 16 Dec 2024 16:08:53 +0200 Subject: [PATCH 2/7] Debug and cleanup --- include/mbgl/vulkan/renderer_backend.hpp | 2 + include/mbgl/vulkan/uniform_buffer.hpp | 13 +++- src/mbgl/vulkan/context.cpp | 2 +- src/mbgl/vulkan/renderable_resource.cpp | 87 ++++++++++++------------ src/mbgl/vulkan/renderer_backend.cpp | 48 +++++++++++-- 5 files changed, 99 insertions(+), 53 deletions(-) diff --git a/include/mbgl/vulkan/renderer_backend.hpp b/include/mbgl/vulkan/renderer_backend.hpp index 7cf05a272cc..ab6aca0ced1 100644 --- a/include/mbgl/vulkan/renderer_backend.hpp +++ b/include/mbgl/vulkan/renderer_backend.hpp @@ -70,6 +70,8 @@ class RendererBackend : public gfx::RendererBackend { void startFrameCapture(); void endFrameCapture(); + void setFrameCaptureLoop(bool value); + void triggerFrameCapture(uint32_t frameCount = 1, uint32_t frameDelay = 0); protected: std::unique_ptr createContext() override; diff --git a/include/mbgl/vulkan/uniform_buffer.hpp b/include/mbgl/vulkan/uniform_buffer.hpp index 83854bee3a2..5c82917e3f0 100644 --- a/include/mbgl/vulkan/uniform_buffer.hpp +++ b/include/mbgl/vulkan/uniform_buffer.hpp @@ -15,6 +15,7 @@ class UniformBuffer final : public gfx::UniformBuffer { ~UniformBuffer() override; const BufferResource& getBufferResource() const { return buffer; } + BufferResource& mutableBufferResource() { return buffer; } UniformBuffer clone() const { return {buffer.clone()}; } @@ -35,14 +36,20 @@ class UniformBufferArray final : public gfx::UniformBufferArray { descriptorStartIndex(descriptorStartIndex_), descriptorBindingCount(descriptorBindingCount_) {} - UniformBufferArray(UniformBufferArray&& other) - : gfx::UniformBufferArray(std::move(other)) {} + UniformBufferArray(UniformBufferArray&& other) noexcept + : gfx::UniformBufferArray(std::move(other)), + descriptorSetType(other.descriptorSetType), + descriptorStartIndex(other.descriptorStartIndex), + descriptorBindingCount(other.descriptorBindingCount), + descriptorSet(std::move(other.descriptorSet)) {} + UniformBufferArray(const UniformBufferArray&) = delete; - UniformBufferArray& operator=(UniformBufferArray&& other) { + UniformBufferArray& operator=(UniformBufferArray&& other) noexcept { gfx::UniformBufferArray::operator=(std::move(other)); return *this; } + UniformBufferArray& operator=(const UniformBufferArray& other) { gfx::UniformBufferArray::operator=(other); return *this; diff --git a/src/mbgl/vulkan/context.cpp b/src/mbgl/vulkan/context.cpp index e3055c4054f..b78d9d315bb 100644 --- a/src/mbgl/vulkan/context.cpp +++ b/src/mbgl/vulkan/context.cpp @@ -559,7 +559,7 @@ bool Context::renderTileClippingMasks(gfx::RenderPass& renderPass, const std::unique_ptr& Context::getDummyVertexBuffer() { if (!dummyVertexBuffer) dummyVertexBuffer = std::make_unique( - *this, nullptr, 16, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, false); + *this, nullptr, 16, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, false); return dummyVertexBuffer; } diff --git a/src/mbgl/vulkan/renderable_resource.cpp b/src/mbgl/vulkan/renderable_resource.cpp index 052010f14e9..e39df3aaa0d 100644 --- a/src/mbgl/vulkan/renderable_resource.cpp +++ b/src/mbgl/vulkan/renderable_resource.cpp @@ -219,7 +219,8 @@ void SurfaceRenderableResource::initDepthStencil() { .setViewType(vk::ImageViewType::e2D) .setFormat(depthFormat) .setComponents(vk::ComponentMapping()) // defaults to vk::ComponentSwizzle::eIdentity - .setSubresourceRange(vk::ImageSubresourceRange(vk::ImageAspectFlagBits::eDepth, 0, 1, 0, 1)); + .setSubresourceRange(vk::ImageSubresourceRange( + vk::ImageAspectFlagBits::eDepth | vk::ImageAspectFlagBits::eStencil, 0, 1, 0, 1)); depthAllocation->imageView = device->createImageViewUnique(imageViewCreateInfo); @@ -307,28 +308,30 @@ void SurfaceRenderableResource::init(uint32_t w, uint32_t h) { // create render pass const auto colorLayout = surface ? vk::ImageLayout::ePresentSrcKHR : vk::ImageLayout::eTransferSrcOptimal; - const auto colorAttachment = vk::AttachmentDescription(vk::AttachmentDescriptionFlags()) - .setFormat(colorFormat) - .setSamples(vk::SampleCountFlagBits::e1) - .setLoadOp(vk::AttachmentLoadOp::eClear) - .setStoreOp(vk::AttachmentStoreOp::eStore) - .setStencilLoadOp(vk::AttachmentLoadOp::eDontCare) - .setStencilStoreOp(vk::AttachmentStoreOp::eDontCare) - .setInitialLayout(vk::ImageLayout::eUndefined) - .setFinalLayout(colorLayout); - - const vk::AttachmentReference colorAttachmentRef(0, vk::ImageLayout::eColorAttachmentOptimal); - const auto depthAttachment = vk::AttachmentDescription() - .setFormat(depthFormat) - .setSamples(vk::SampleCountFlagBits::e1) - .setLoadOp(vk::AttachmentLoadOp::eClear) - .setStoreOp(vk::AttachmentStoreOp::eDontCare) - .setStencilLoadOp(vk::AttachmentLoadOp::eClear) - .setStencilStoreOp(vk::AttachmentStoreOp::eDontCare) - .setInitialLayout(vk::ImageLayout::eUndefined) - .setFinalLayout(vk::ImageLayout::eDepthStencilAttachmentOptimal); + const std::array attachments = { + vk::AttachmentDescription() + .setFormat(colorFormat) + .setSamples(vk::SampleCountFlagBits::e1) + .setLoadOp(vk::AttachmentLoadOp::eClear) + .setStoreOp(vk::AttachmentStoreOp::eStore) + .setStencilLoadOp(vk::AttachmentLoadOp::eDontCare) + .setStencilStoreOp(vk::AttachmentStoreOp::eDontCare) + .setInitialLayout(vk::ImageLayout::eUndefined) + .setFinalLayout(colorLayout), + + vk::AttachmentDescription() + .setFormat(depthFormat) + .setSamples(vk::SampleCountFlagBits::e1) + .setLoadOp(vk::AttachmentLoadOp::eClear) + .setStoreOp(vk::AttachmentStoreOp::eDontCare) + .setStencilLoadOp(vk::AttachmentLoadOp::eClear) + .setStencilStoreOp(vk::AttachmentStoreOp::eDontCare) + .setInitialLayout(vk::ImageLayout::eUndefined) + .setFinalLayout(vk::ImageLayout::eDepthStencilAttachmentOptimal) + }; + const vk::AttachmentReference colorAttachmentRef(0, vk::ImageLayout::eColorAttachmentOptimal); const vk::AttachmentReference depthAttachmentRef(1, vk::ImageLayout::eDepthStencilAttachmentOptimal); const auto subpass = vk::SubpassDescription() @@ -337,28 +340,28 @@ void SurfaceRenderableResource::init(uint32_t w, uint32_t h) { .setColorAttachments(colorAttachmentRef) .setPDepthStencilAttachment(&depthAttachmentRef); - const auto subpassSrcStageMask = vk::PipelineStageFlags() | vk::PipelineStageFlagBits::eColorAttachmentOutput | - vk::PipelineStageFlagBits::eLateFragmentTests; - - const auto subpassDstStageMask = vk::PipelineStageFlags() | vk::PipelineStageFlagBits::eColorAttachmentOutput | - vk::PipelineStageFlagBits::eEarlyFragmentTests; - - const auto subpassSrcAccessMask = vk::AccessFlags() | vk::AccessFlagBits::eDepthStencilAttachmentWrite; - - const auto subpassDstAccessMask = vk::AccessFlags() | vk::AccessFlagBits::eColorAttachmentWrite | - vk::AccessFlagBits::eDepthStencilAttachmentWrite; - - const auto subpassDependency = vk::SubpassDependency() - .setSrcSubpass(VK_SUBPASS_EXTERNAL) - .setDstSubpass(0) - .setSrcStageMask(subpassSrcStageMask) - .setDstStageMask(subpassDstStageMask) - .setSrcAccessMask(subpassSrcAccessMask) - .setDstAccessMask(subpassDstAccessMask); - - const std::array attachments = {colorAttachment, depthAttachment}; + const std::array dependencies = { + vk::SubpassDependency() + .setSrcSubpass(VK_SUBPASS_EXTERNAL) + .setDstSubpass(0) + .setSrcStageMask(vk::PipelineStageFlagBits::eColorAttachmentOutput) + .setDstStageMask(vk::PipelineStageFlagBits::eColorAttachmentOutput) + .setSrcAccessMask({}) + .setDstAccessMask(vk::AccessFlagBits::eColorAttachmentWrite), + + vk::SubpassDependency() + .setSrcSubpass(VK_SUBPASS_EXTERNAL) + .setDstSubpass(0) + .setSrcStageMask(vk::PipelineStageFlagBits::eEarlyFragmentTests | + vk::PipelineStageFlagBits::eLateFragmentTests) + .setDstStageMask(vk::PipelineStageFlagBits::eEarlyFragmentTests | + vk::PipelineStageFlagBits::eLateFragmentTests) + .setSrcAccessMask({}) + .setDstAccessMask(vk::AccessFlagBits::eDepthStencilAttachmentWrite), + }; + const auto renderPassCreateInfo = - vk::RenderPassCreateInfo().setAttachments(attachments).setSubpasses(subpass).setDependencies(subpassDependency); + vk::RenderPassCreateInfo().setAttachments(attachments).setSubpasses(subpass).setDependencies(dependencies); renderPass = device->createRenderPassUnique(renderPassCreateInfo); diff --git a/src/mbgl/vulkan/renderer_backend.cpp b/src/mbgl/vulkan/renderer_backend.cpp index e2b44708133..5e185ebe286 100644 --- a/src/mbgl/vulkan/renderer_backend.cpp +++ b/src/mbgl/vulkan/renderer_backend.cpp @@ -56,7 +56,13 @@ VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE #endif #include "renderdoc_app.h" -static RENDERDOC_API_1_1_2* g_rdoc_api = nullptr; + +static struct { + RENDERDOC_API_1_1_2* api = nullptr; + bool loop = false; + int32_t frameDelay = 0; + uint32_t frameCaptureCount = 0; +} g_rdoc; #endif @@ -183,13 +189,13 @@ void RendererBackend::initFrameCapture() { #ifdef _WIN32 if (HMODULE mod = GetModuleHandleA("renderdoc.dll")) { pRENDERDOC_GetAPI RENDERDOC_GetAPI = (pRENDERDOC_GetAPI)GetProcAddress(mod, "RENDERDOC_GetAPI"); - int ret = RENDERDOC_GetAPI(eRENDERDOC_API_Version_1_1_2, (void**)&g_rdoc_api); + int ret = RENDERDOC_GetAPI(eRENDERDOC_API_Version_1_1_2, (void**)&g_rdoc.api); assert(ret == 1); } #elif __unix__ if (void* mod = dlopen("librenderdoc.so", RTLD_NOW | RTLD_NOLOAD)) { pRENDERDOC_GetAPI RENDERDOC_GetAPI = (pRENDERDOC_GetAPI)dlsym(mod, "RENDERDOC_GetAPI"); - int ret = RENDERDOC_GetAPI(eRENDERDOC_API_Version_1_1_2, (void**)&g_rdoc_api); + int ret = RENDERDOC_GetAPI(eRENDERDOC_API_Version_1_1_2, (void**)&g_rdoc.api); assert(ret == 1); } #endif @@ -199,19 +205,47 @@ void RendererBackend::initFrameCapture() { void RendererBackend::startFrameCapture() { #ifdef ENABLE_RENDERDOC_FRAME_CAPTURE - if (g_rdoc_api) { + if (!g_rdoc.api) { + return; + } + + if (g_rdoc.loop) { RENDERDOC_DevicePointer devicePtr = RENDERDOC_DEVICEPOINTER_FROM_VKINSTANCE(instance->operator VkInstance_T*()); - g_rdoc_api->StartFrameCapture(devicePtr, nullptr); + g_rdoc.api->StartFrameCapture(devicePtr, nullptr); + } else { + + if (g_rdoc.frameCaptureCount > 0 && g_rdoc.frameDelay == 0) { + g_rdoc.api->TriggerMultiFrameCapture(g_rdoc.frameCaptureCount); + } + + --g_rdoc.frameDelay; } #endif } void RendererBackend::endFrameCapture() { #ifdef ENABLE_RENDERDOC_FRAME_CAPTURE - if (g_rdoc_api) { + if (g_rdoc.api && g_rdoc.loop) { RENDERDOC_DevicePointer devicePtr = RENDERDOC_DEVICEPOINTER_FROM_VKINSTANCE(instance->operator VkInstance_T*()); - g_rdoc_api->EndFrameCapture(devicePtr, nullptr); + g_rdoc.api->EndFrameCapture(devicePtr, nullptr); + } +#endif +} + +void RendererBackend::setFrameCaptureLoop(bool value) { +#ifdef ENABLE_RENDERDOC_FRAME_CAPTURE + g_rdoc.loop = value; +#endif +} + +void RendererBackend::triggerFrameCapture(uint32_t frameCount, uint32_t frameDelay) { +#ifdef ENABLE_RENDERDOC_FRAME_CAPTURE + if (!g_rdoc.api) { + return; } + + g_rdoc.frameCaptureCount = frameCount; + g_rdoc.frameDelay = frameDelay; #endif } From fd10156822ba07e909857f63e846a0b43fcb8464 Mon Sep 17 00:00:00 2001 From: Adrian Cojocaru Date: Mon, 16 Dec 2024 16:10:38 +0200 Subject: [PATCH 3/7] Update uniform sub-buffers to the latest data before using --- include/mbgl/vulkan/buffer_resource.hpp | 14 +++-- src/mbgl/vulkan/buffer_resource.cpp | 72 ++++++++++++++++++++----- src/mbgl/vulkan/uniform_buffer.cpp | 16 ++++++ 3 files changed, 83 insertions(+), 19 deletions(-) diff --git a/include/mbgl/vulkan/buffer_resource.hpp b/include/mbgl/vulkan/buffer_resource.hpp index 2d59abff4fa..890d4b61d70 100644 --- a/include/mbgl/vulkan/buffer_resource.hpp +++ b/include/mbgl/vulkan/buffer_resource.hpp @@ -51,14 +51,18 @@ class BufferResource { void update(const void* data, std::size_t size, std::size_t offset) noexcept; std::size_t getSizeInBytes() const noexcept { return size; } - const void* contents() const noexcept { return (raw.empty() ? nullptr : raw.data()); } + const void* contents() const noexcept; + const void* contents(uint8_t resourceIndex) const noexcept; Context& getContext() const noexcept { return context; } const vk::Buffer& getVulkanBuffer() const noexcept { return bufferAllocation->buffer; } std::size_t getVulkanBufferOffset() const noexcept; - std::size_t getVulkanBufferSize() const noexcept; + std::size_t getVulkanBufferOffset(uint8_t resourceIndex) const noexcept; + // update the current sub-buffer with the latest data + void updateVulkanBuffer(); + void updateVulkanBuffer(const int8_t destination, const uint8_t source); - bool isValid() const noexcept { return !raw.empty(); } + bool isValid() const noexcept { return !!bufferAllocation; } operator bool() const noexcept { return isValid(); } bool operator!() const noexcept { return !isValid(); } @@ -69,14 +73,14 @@ class BufferResource { protected: Context& context; - std::vector raw; std::size_t size; std::uint32_t usage; - std::uint16_t version = 0; + VersionType version = 0; bool persistent; SharedBufferAllocation bufferAllocation; size_t bufferWindowSize = 0; + std::vector bufferWindowVersions; }; } // namespace vulkan diff --git a/src/mbgl/vulkan/buffer_resource.cpp b/src/mbgl/vulkan/buffer_resource.cpp index 2f9ccb4a45b..c3006289890 100644 --- a/src/mbgl/vulkan/buffer_resource.cpp +++ b/src/mbgl/vulkan/buffer_resource.cpp @@ -64,7 +64,10 @@ BufferResource::BufferResource( assert(bufferWindowSize != 0); - totalSize = bufferWindowSize * backend.getMaxFrames(); + const auto frameCount = backend.getMaxFrames(); + totalSize = bufferWindowSize * frameCount; + + bufferWindowVersions = std::vector(frameCount, 0); } const auto bufferInfo = vk::BufferCreateInfo() @@ -87,9 +90,7 @@ BufferResource::BufferResource( vmaMapMemory(allocator, bufferAllocation->allocation, &bufferAllocation->mappedBuffer); if (data) { - raw.resize(size); - std::memcpy(raw.data(), data, size); - std::memcpy(static_cast(bufferAllocation->mappedBuffer) + getVulkanBufferOffset(), data, size); + update(data, size, 0); } if (isValid()) { @@ -104,12 +105,13 @@ BufferResource::BufferResource( BufferResource::BufferResource(BufferResource&& other) noexcept : context(other.context), - raw(std::move(other.raw)), size(other.size), usage(other.usage), persistent(other.persistent), bufferAllocation(std::move(other.bufferAllocation)), - bufferWindowSize(other.bufferWindowSize) { + bufferWindowSize(other.bufferWindowSize), + version(other.version), + bufferWindowVersions(std::move(other.bufferWindowVersions)) { other.bufferAllocation = nullptr; } @@ -134,7 +136,7 @@ BufferResource& BufferResource::operator=(BufferResource&& other) noexcept { context.renderingStats().numBuffers--; context.renderingStats().memBuffers -= size; }; - raw = std::move(other.raw); + size = other.size; usage = other.usage; persistent = other.persistent; @@ -152,21 +154,63 @@ void BufferResource::update(const void* newData, std::size_t updateSize, std::si return; } - auto& stats = context.renderingStats(); + uint8_t* data = static_cast(bufferAllocation->mappedBuffer) + getVulkanBufferOffset() + offset; + std::memcpy(data, newData, updateSize); - std::memcpy(raw.data() + offset, newData, updateSize); - std::memcpy( - static_cast(bufferAllocation->mappedBuffer) + getVulkanBufferOffset() + offset, newData, updateSize); + auto& stats = context.renderingStats(); stats.bufferUpdateBytes += updateSize; - stats.bufferUpdates++; version++; + + if (bufferWindowSize) { + const auto frameIndex = context.getCurrentFrameResourceIndex(); + bufferWindowVersions[frameIndex] = version; + } +} + +const void* BufferResource::contents() const noexcept { + return contents(context.getCurrentFrameResourceIndex()); +} + +const void* BufferResource::contents(uint8_t resourceIndex) const noexcept { + if (!isValid()) { + return nullptr; + } + + return static_cast(bufferAllocation->mappedBuffer) + getVulkanBufferOffset(resourceIndex); } std::size_t BufferResource::getVulkanBufferOffset() const noexcept { - if (bufferWindowSize > 0) return 0; + return getVulkanBufferOffset(context.getCurrentFrameResourceIndex()); +} + +std::size_t BufferResource::getVulkanBufferOffset(std::uint8_t resourceIndex) const noexcept { + assert(context.getBackend().getMaxFrames() >= resourceIndex); + return bufferWindowSize ? resourceIndex * bufferWindowSize : 0; +} + +void BufferResource::updateVulkanBuffer() { + const auto frameCount = context.getBackend().getMaxFrames(); + + const int8_t currentIndex = context.getCurrentFrameResourceIndex(); + const int8_t prevIndex = currentIndex == 0 ? frameCount - 1 : currentIndex - 1; + + updateVulkanBuffer(currentIndex, prevIndex); +} + +void BufferResource::updateVulkanBuffer(const int8_t destination, const uint8_t source) { + if (!bufferWindowSize) { + return; + } + + if (bufferWindowVersions[destination] < bufferWindowVersions[source]) { + uint8_t* dstData = static_cast(bufferAllocation->mappedBuffer) + bufferWindowSize * destination; + uint8_t* srcData = static_cast(bufferAllocation->mappedBuffer) + bufferWindowSize * source; + + std::memcpy(dstData, srcData, size); - return context.getCurrentFrameResourceIndex() * bufferWindowSize; + bufferWindowVersions[destination] = bufferWindowVersions[source]; + } } } // namespace vulkan diff --git a/src/mbgl/vulkan/uniform_buffer.cpp b/src/mbgl/vulkan/uniform_buffer.cpp index b01e0facc90..0f102317d71 100644 --- a/src/mbgl/vulkan/uniform_buffer.cpp +++ b/src/mbgl/vulkan/uniform_buffer.cpp @@ -74,6 +74,22 @@ void UniformBufferArray::bindDescriptorSets(CommandEncoder& encoder) { } descriptorSet->update(*this, descriptorStartIndex, descriptorBindingCount); + + const auto frameCount = encoder.getContext().getBackend().getMaxFrames(); + 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) { + const uint32_t index = descriptorStartIndex + i; + + if (!uniformBufferVector[index]) { + continue; + } + + auto& buff = static_cast(uniformBufferVector[index].get())->mutableBufferResource(); + buff.updateVulkanBuffer(currentIndex, prevIndex); + } + descriptorSet->bind(encoder); } From 08a080442b6d987787da48ca526bc0059b2e5231 Mon Sep 17 00:00:00 2001 From: Adrian Cojocaru Date: Mon, 6 Jan 2025 17:44:28 +0200 Subject: [PATCH 4/7] Compile errors --- src/mbgl/vulkan/buffer_resource.cpp | 2 +- src/mbgl/vulkan/renderer_backend.cpp | 4 ++-- src/mbgl/vulkan/upload_pass.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mbgl/vulkan/buffer_resource.cpp b/src/mbgl/vulkan/buffer_resource.cpp index c3006289890..0d5f7169282 100644 --- a/src/mbgl/vulkan/buffer_resource.cpp +++ b/src/mbgl/vulkan/buffer_resource.cpp @@ -107,10 +107,10 @@ BufferResource::BufferResource(BufferResource&& other) noexcept : context(other.context), size(other.size), usage(other.usage), + version(other.version), persistent(other.persistent), bufferAllocation(std::move(other.bufferAllocation)), bufferWindowSize(other.bufferWindowSize), - version(other.version), bufferWindowVersions(std::move(other.bufferWindowVersions)) { other.bufferAllocation = nullptr; } diff --git a/src/mbgl/vulkan/renderer_backend.cpp b/src/mbgl/vulkan/renderer_backend.cpp index 5e185ebe286..bfb18548987 100644 --- a/src/mbgl/vulkan/renderer_backend.cpp +++ b/src/mbgl/vulkan/renderer_backend.cpp @@ -232,13 +232,13 @@ void RendererBackend::endFrameCapture() { #endif } -void RendererBackend::setFrameCaptureLoop(bool value) { +void RendererBackend::setFrameCaptureLoop([[maybe_unused]] bool value) { #ifdef ENABLE_RENDERDOC_FRAME_CAPTURE g_rdoc.loop = value; #endif } -void RendererBackend::triggerFrameCapture(uint32_t frameCount, uint32_t frameDelay) { +void RendererBackend::triggerFrameCapture([[maybe_unused]] uint32_t frameCount, [[maybe_unused]] uint32_t frameDelay) { #ifdef ENABLE_RENDERDOC_FRAME_CAPTURE if (!g_rdoc.api) { return; diff --git a/src/mbgl/vulkan/upload_pass.cpp b/src/mbgl/vulkan/upload_pass.cpp index 13517d494a6..8bbaa692899 100644 --- a/src/mbgl/vulkan/upload_pass.cpp +++ b/src/mbgl/vulkan/upload_pass.cpp @@ -85,7 +85,7 @@ static const std::unique_ptr noBuffer; const gfx::UniqueVertexBufferResource& UploadPass::getBuffer(const gfx::VertexVectorBasePtr& vec, const gfx::BufferUsageType usage, - bool forceUpdate) { + [[maybe_unused]] bool forceUpdate) { if (vec) { const auto* rawBufPtr = vec->getRawData(); const auto rawBufSize = vec->getRawCount() * vec->getRawSize(); From 6b977ef4bc55ee0f34696812baf77c759e96eeda Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 7 Jan 2025 14:56:14 +0000 Subject: [PATCH 5/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/mbgl/vulkan/buffer_resource.cpp | 2 +- src/mbgl/vulkan/renderable_resource.cpp | 7 +++---- src/mbgl/vulkan/renderer_backend.cpp | 5 ++--- src/mbgl/vulkan/uniform_buffer.cpp | 2 +- src/mbgl/vulkan/upload_pass.cpp | 2 +- 5 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/mbgl/vulkan/buffer_resource.cpp b/src/mbgl/vulkan/buffer_resource.cpp index 0d5f7169282..d2b57731b84 100644 --- a/src/mbgl/vulkan/buffer_resource.cpp +++ b/src/mbgl/vulkan/buffer_resource.cpp @@ -202,7 +202,7 @@ void BufferResource::updateVulkanBuffer(const int8_t destination, const uint8_t if (!bufferWindowSize) { return; } - + if (bufferWindowVersions[destination] < bufferWindowVersions[source]) { uint8_t* dstData = static_cast(bufferAllocation->mappedBuffer) + bufferWindowSize * destination; uint8_t* srcData = static_cast(bufferAllocation->mappedBuffer) + bufferWindowSize * source; diff --git a/src/mbgl/vulkan/renderable_resource.cpp b/src/mbgl/vulkan/renderable_resource.cpp index e39df3aaa0d..f129a617684 100644 --- a/src/mbgl/vulkan/renderable_resource.cpp +++ b/src/mbgl/vulkan/renderable_resource.cpp @@ -328,8 +328,7 @@ void SurfaceRenderableResource::init(uint32_t w, uint32_t h) { .setStencilLoadOp(vk::AttachmentLoadOp::eClear) .setStencilStoreOp(vk::AttachmentStoreOp::eDontCare) .setInitialLayout(vk::ImageLayout::eUndefined) - .setFinalLayout(vk::ImageLayout::eDepthStencilAttachmentOptimal) - }; + .setFinalLayout(vk::ImageLayout::eDepthStencilAttachmentOptimal)}; const vk::AttachmentReference colorAttachmentRef(0, vk::ImageLayout::eColorAttachmentOptimal); const vk::AttachmentReference depthAttachmentRef(1, vk::ImageLayout::eDepthStencilAttachmentOptimal); @@ -348,7 +347,7 @@ void SurfaceRenderableResource::init(uint32_t w, uint32_t h) { .setDstStageMask(vk::PipelineStageFlagBits::eColorAttachmentOutput) .setSrcAccessMask({}) .setDstAccessMask(vk::AccessFlagBits::eColorAttachmentWrite), - + vk::SubpassDependency() .setSrcSubpass(VK_SUBPASS_EXTERNAL) .setDstSubpass(0) @@ -359,7 +358,7 @@ void SurfaceRenderableResource::init(uint32_t w, uint32_t h) { .setSrcAccessMask({}) .setDstAccessMask(vk::AccessFlagBits::eDepthStencilAttachmentWrite), }; - + const auto renderPassCreateInfo = vk::RenderPassCreateInfo().setAttachments(attachments).setSubpasses(subpass).setDependencies(dependencies); diff --git a/src/mbgl/vulkan/renderer_backend.cpp b/src/mbgl/vulkan/renderer_backend.cpp index bfb18548987..b549487dc6a 100644 --- a/src/mbgl/vulkan/renderer_backend.cpp +++ b/src/mbgl/vulkan/renderer_backend.cpp @@ -206,18 +206,17 @@ void RendererBackend::initFrameCapture() { void RendererBackend::startFrameCapture() { #ifdef ENABLE_RENDERDOC_FRAME_CAPTURE if (!g_rdoc.api) { - return; + return; } if (g_rdoc.loop) { RENDERDOC_DevicePointer devicePtr = RENDERDOC_DEVICEPOINTER_FROM_VKINSTANCE(instance->operator VkInstance_T*()); g_rdoc.api->StartFrameCapture(devicePtr, nullptr); } else { - if (g_rdoc.frameCaptureCount > 0 && g_rdoc.frameDelay == 0) { g_rdoc.api->TriggerMultiFrameCapture(g_rdoc.frameCaptureCount); } - + --g_rdoc.frameDelay; } #endif diff --git a/src/mbgl/vulkan/uniform_buffer.cpp b/src/mbgl/vulkan/uniform_buffer.cpp index 0f102317d71..4c74630a6b1 100644 --- a/src/mbgl/vulkan/uniform_buffer.cpp +++ b/src/mbgl/vulkan/uniform_buffer.cpp @@ -81,7 +81,7 @@ void UniformBufferArray::bindDescriptorSets(CommandEncoder& encoder) { for (uint32_t i = 0; i < descriptorBindingCount; ++i) { const uint32_t index = descriptorStartIndex + i; - + if (!uniformBufferVector[index]) { continue; } diff --git a/src/mbgl/vulkan/upload_pass.cpp b/src/mbgl/vulkan/upload_pass.cpp index 8bbaa692899..d2f6a356491 100644 --- a/src/mbgl/vulkan/upload_pass.cpp +++ b/src/mbgl/vulkan/upload_pass.cpp @@ -97,7 +97,7 @@ const gfx::UniqueVertexBufferResource& UploadPass::getBuffer(const gfx::VertexVe // If it's changed, update it if (rawBufSize <= resource.getSizeInBytes()) { if (vec->isModifiedAfter(resource.getLastUpdated())) { - //updateVertexBufferResource(resource, rawBufPtr, rawBufSize); + // updateVertexBufferResource(resource, rawBufPtr, rawBufSize); } else { return rawData->resource; } From 4c22c5abd368367d455af06b6c64b144ca0337cd Mon Sep 17 00:00:00 2001 From: Adrian Cojocaru Date: Wed, 8 Jan 2025 19:49:52 +0200 Subject: [PATCH 6/7] 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]) { From 89b06191df195bb6aef84ddd735d118069047dfb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 8 Jan 2025 17:50:32 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/mbgl/vulkan/buffer_resource.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mbgl/vulkan/buffer_resource.cpp b/src/mbgl/vulkan/buffer_resource.cpp index a246473468e..b920627a55f 100644 --- a/src/mbgl/vulkan/buffer_resource.cpp +++ b/src/mbgl/vulkan/buffer_resource.cpp @@ -64,8 +64,8 @@ BufferResource::BufferResource( 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;