From e4419da54e33a279566a3e6d253838b7632d4a71 Mon Sep 17 00:00:00 2001 From: "r.kuznetsov" Date: Mon, 4 Feb 2019 17:02:44 +0300 Subject: [PATCH] [vulkan] Fixed memory mapping --- drape/vulkan/vulkan_gpu_buffer_impl.cpp | 23 ++--- drape/vulkan/vulkan_memory_manager.cpp | 107 ++++++++++++----------- drape/vulkan/vulkan_memory_manager.hpp | 38 ++++---- drape/vulkan/vulkan_mesh_object_impl.cpp | 22 ++--- drape/vulkan/vulkan_object_manager.cpp | 42 +++++++++ drape/vulkan/vulkan_object_manager.hpp | 24 +++++ drape/vulkan/vulkan_staging_buffer.cpp | 29 +++--- drape/vulkan/vulkan_texture.cpp | 3 +- shaders/vulkan_program_params.cpp | 32 +++---- shaders/vulkan_program_params.hpp | 2 +- 10 files changed, 177 insertions(+), 145 deletions(-) diff --git a/drape/vulkan/vulkan_gpu_buffer_impl.cpp b/drape/vulkan/vulkan_gpu_buffer_impl.cpp index f52931c44c..a444f26980 100644 --- a/drape/vulkan/vulkan_gpu_buffer_impl.cpp +++ b/drape/vulkan/vulkan_gpu_buffer_impl.cpp @@ -24,7 +24,6 @@ void * VulkanGPUBuffer::Map(ref_ptr context, uint32_t element uint32_t elementCount) { CHECK(m_objectManager != nullptr, ()); - VkDevice device = context->GetDevice(); uint32_t const elementSize = GetElementSize(); uint32_t const mappingSizeInBytes = elementCount * elementSize; @@ -120,26 +119,14 @@ void VulkanGPUBuffer::Resize(ref_ptr context, void const * da sizeInBytes, 0 /* batcherHash */); if (data != nullptr) { - void * gpuPtr = nullptr; - CHECK_VK_CALL(vkMapMemory(device, m_geometryBuffer.m_allocation->m_memory, - m_geometryBuffer.m_allocation->m_alignedOffset, - m_geometryBuffer.m_allocation->m_alignedSize, 0, &gpuPtr)); + void * gpuPtr = m_objectManager->Map(m_geometryBuffer); memcpy(gpuPtr, data, sizeInBytes); - if (!m_geometryBuffer.m_allocation->m_isCoherent) - { - VkMappedMemoryRange mappedRange = {}; - mappedRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE; - mappedRange.memory = m_geometryBuffer.m_allocation->m_memory; - mappedRange.offset = m_geometryBuffer.m_allocation->m_alignedOffset; - mappedRange.size = m_geometryBuffer.m_allocation->m_alignedSize; - CHECK_VK_CALL(vkFlushMappedMemoryRanges(device, 1, &mappedRange)); - } - vkUnmapMemory(device, m_geometryBuffer.m_allocation->m_memory); + m_objectManager->Flush(m_geometryBuffer); + m_objectManager->Unmap(m_geometryBuffer); } - CHECK_VK_CALL(vkBindBufferMemory(device, m_geometryBuffer.m_buffer, - m_geometryBuffer.m_allocation->m_memory, - m_geometryBuffer.m_allocation->m_alignedOffset)); + CHECK_VK_CALL(vkBindBufferMemory(device, m_geometryBuffer.m_buffer, m_geometryBuffer.GetMemory(), + m_geometryBuffer.GetAlignedOffset())); // If we have already set up data, we have to call SetDataSize. if (data != nullptr) diff --git a/drape/vulkan/vulkan_memory_manager.cpp b/drape/vulkan/vulkan_memory_manager.cpp index df60cd11fc..619d3c2e2a 100644 --- a/drape/vulkan/vulkan_memory_manager.cpp +++ b/drape/vulkan/vulkan_memory_manager.cpp @@ -17,9 +17,9 @@ namespace std::array const kMinBlockSizeInBytes = { 64 * 1024, // Geometry - 32 * 1024, // Uniform - 0, // Staging - 0, // Image + 0, // Uniform (no minimal size) + 0, // Staging (no minimal size) + 0, // Image (no minimal size) }; std::array const kDesiredSizeInBytes = @@ -56,6 +56,12 @@ VkMemoryPropertyFlags GetMemoryPropertyFlags(VulkanMemoryManager::ResourceType r } return 0; } + +bool Less(drape_ptr const & b1, + drape_ptr const & b2) +{ + return b1->m_blockSize < b2->m_blockSize; +} } // namespace VulkanMemoryManager::VulkanMemoryManager(VkDevice device, VkPhysicalDeviceLimits const & deviceLimits, @@ -70,12 +76,12 @@ VulkanMemoryManager::~VulkanMemoryManager() for (size_t i = 0; i < kResourcesCount; ++i) { for (auto const & b : m_freeBlocks[i]) - vkFreeMemory(m_device, b.m_memory, nullptr); + vkFreeMemory(m_device, b->m_memory, nullptr); for (auto const & p : m_memory[i]) { for (auto const & b : p.second) - vkFreeMemory(m_device, b.m_memory, nullptr); + vkFreeMemory(m_device, b->m_memory, nullptr); } } } @@ -137,36 +143,37 @@ VulkanMemoryManager::AllocationPtr VulkanMemoryManager::Allocate(ResourceType re { CHECK(!it->second.empty(), ()); auto & block = it->second.back(); - auto const alignedOffset = GetAligned(block.m_freeOffset, GetOffsetAlignment(resourceType)); + auto const alignedOffset = GetAligned(block->m_freeOffset, GetOffsetAlignment(resourceType)); // There is space in the current block. - if (block.m_blockSize <= alignedOffset + alignedSize) + if (!block->m_isBlocked && (block->m_blockSize <= alignedOffset + alignedSize)) { - block.m_freeOffset = alignedOffset + alignedSize; - block.m_allocationCounter++; - return std::make_shared(resourceType, blockHash, block.m_memory, - alignedOffset, alignedSize, block.m_isCoherent); + block->m_freeOffset = alignedOffset + alignedSize; + block->m_allocationCounter++; + return std::make_shared(resourceType, blockHash, alignedOffset, alignedSize, + make_ref(block)); } } // Looking for a block in free ones. auto & fm = m_freeBlocks[static_cast(resourceType)]; // Free blocks array must be sorted by size. - MemoryBlock refBlock; - refBlock.m_blockSize = alignedSize; - auto const freeBlockIt = std::upper_bound(fm.begin(), fm.end(), refBlock); + static drape_ptr refBlock = make_unique_dp(); + refBlock->m_blockSize = alignedSize; + auto const freeBlockIt = std::upper_bound(fm.begin(), fm.end(), refBlock, &Less); if (freeBlockIt != fm.end()) { - MemoryBlock freeBlock = *freeBlockIt; - CHECK_EQUAL(freeBlock.m_allocationCounter, 0, ()); - CHECK_EQUAL(freeBlock.m_freeOffset, 0, ()); - CHECK_LESS_OR_EQUAL(alignedSize, freeBlock.m_blockSize, ()); + drape_ptr freeBlock = std::move(*freeBlockIt); + CHECK_EQUAL(freeBlock->m_allocationCounter, 0, ()); + CHECK_EQUAL(freeBlock->m_freeOffset, 0, ()); + CHECK_LESS_OR_EQUAL(alignedSize, freeBlock->m_blockSize, ()); + CHECK(!freeBlock->m_isBlocked, ()); fm.erase(freeBlockIt); - freeBlock.m_freeOffset = alignedSize; - freeBlock.m_allocationCounter++; - auto p = std::make_shared(resourceType, blockHash, freeBlock.m_memory, - 0, alignedSize, freeBlock.m_isCoherent); + freeBlock->m_freeOffset = alignedSize; + freeBlock->m_allocationCounter++; + auto p = std::make_shared(resourceType, blockHash, 0, alignedSize, + make_ref(freeBlock)); m[blockHash].push_back(std::move(freeBlock)); return p; } @@ -203,15 +210,15 @@ VulkanMemoryManager::AllocationPtr VulkanMemoryManager::Allocate(ResourceType re // Attach block. auto & m = m_memory[static_cast(resourceType)]; - MemoryBlock newBlock; - newBlock.m_memory = memory; - newBlock.m_blockSize = blockSize; - newBlock.m_freeOffset = alignedSize; - newBlock.m_allocationCounter++; - newBlock.m_isCoherent = ((flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) != 0); + auto newBlock = make_unique_dp(); + newBlock->m_memory = memory; + newBlock->m_blockSize = blockSize; + newBlock->m_freeOffset = alignedSize; + newBlock->m_allocationCounter++; + newBlock->m_isCoherent = ((flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) != 0); - auto p = std::make_shared(resourceType, blockHash, newBlock.m_memory, - 0, alignedSize, newBlock.m_isCoherent); + auto p = std::make_shared(resourceType, blockHash, 0, alignedSize, + make_ref(newBlock)); m[blockHash].push_back(std::move(newBlock)); return p; } @@ -225,20 +232,22 @@ void VulkanMemoryManager::BeginDeallocationSession() void VulkanMemoryManager::Deallocate(AllocationPtr ptr) { CHECK(ptr, ()); + CHECK(!ptr->m_memoryBlock->m_isBlocked, ()); auto const kResourceIndex = static_cast(ptr->m_resourceType); auto & m = m_memory[kResourceIndex]; auto const it = m.find(ptr->m_blockHash); CHECK(it != m.end(), ()); auto blockIt = std::find_if(it->second.begin(), it->second.end(), - [&ptr](MemoryBlock const & b) + [&ptr](drape_ptr const & b) { - return b.m_memory == ptr->m_memory; + ASSERT(ptr->m_memoryBlock != nullptr, ()); + return b->m_memory == ptr->m_memoryBlock->m_memory; }); CHECK(blockIt != it->second.end(), ()); - CHECK_GREATER(blockIt->m_allocationCounter, 0, ()); - blockIt->m_allocationCounter--; + CHECK_GREATER((*blockIt)->m_allocationCounter, 0, ()); + (*blockIt)->m_allocationCounter--; - if (blockIt->m_allocationCounter == 0) + if ((*blockIt)->m_allocationCounter == 0) { if (m_isInDeallocationSession) { @@ -248,20 +257,20 @@ void VulkanMemoryManager::Deallocate(AllocationPtr ptr) } else { - MemoryBlock memoryBlock = *blockIt; + drape_ptr memoryBlock = std::move(*blockIt); it->second.erase(blockIt); if (m_sizes[kResourceIndex] > kDesiredSizeInBytes[kResourceIndex]) { - CHECK_LESS_OR_EQUAL(memoryBlock.m_blockSize, m_sizes[kResourceIndex], ()); - m_sizes[kResourceIndex] -= memoryBlock.m_blockSize; - vkFreeMemory(m_device, memoryBlock.m_memory, nullptr); + CHECK_LESS_OR_EQUAL(memoryBlock->m_blockSize, m_sizes[kResourceIndex], ()); + m_sizes[kResourceIndex] -= memoryBlock->m_blockSize; + vkFreeMemory(m_device, memoryBlock->m_memory, nullptr); } else { - memoryBlock.m_freeOffset = 0; + memoryBlock->m_freeOffset = 0; auto & fm = m_freeBlocks[kResourceIndex]; fm.push_back(std::move(memoryBlock)); - std::sort(fm.begin(), fm.end()); + std::sort(fm.begin(), fm.end(), &Less); } } } @@ -282,27 +291,27 @@ void VulkanMemoryManager::EndDeallocationSession() auto & fm = m_freeBlocks[i]; auto & m = m_memory[i]; m[i].erase(std::remove_if(m[i].begin(), m[i].end(), - [this, &fm, i](MemoryBlock const & b) + [this, &fm, i](drape_ptr & b) { - if (b.m_allocationCounter == 0) + if (b->m_allocationCounter == 0) { if (m_sizes[i] > kDesiredSizeInBytes[i]) { - CHECK_LESS_OR_EQUAL(b.m_blockSize, m_sizes[i], ()); - m_sizes[i] -= b.m_blockSize; - vkFreeMemory(m_device, b.m_memory, nullptr); + CHECK_LESS_OR_EQUAL(b->m_blockSize, m_sizes[i], ()); + m_sizes[i] -= b->m_blockSize; + vkFreeMemory(m_device, b->m_memory, nullptr); } else { - MemoryBlock block = b; - block.m_freeOffset = 0; + auto block = std::move(b); + block->m_freeOffset = 0; fm.push_back(std::move(block)); } return true; } return false; }), m[i].end()); - std::sort(fm.begin(), fm.end()); + std::sort(fm.begin(), fm.end(), &Less); } } } // namespace vulkan diff --git a/drape/vulkan/vulkan_memory_manager.hpp b/drape/vulkan/vulkan_memory_manager.hpp index c95250c643..3cc333c578 100644 --- a/drape/vulkan/vulkan_memory_manager.hpp +++ b/drape/vulkan/vulkan_memory_manager.hpp @@ -1,5 +1,7 @@ #pragma once +#include "drape/pointers.hpp" + #include #include @@ -36,23 +38,31 @@ public: static size_t constexpr kResourcesCount = static_cast(VulkanMemoryManager::ResourceType::Count); + struct MemoryBlock + { + VkDeviceMemory m_memory = {}; + uint32_t m_blockSize = 0; + uint32_t m_freeOffset = 0; + uint32_t m_allocationCounter = 0; + bool m_isCoherent = false; + bool m_isBlocked = false; + }; + struct Allocation { uint64_t const m_blockHash; - VkDeviceMemory const m_memory; uint32_t const m_alignedOffset; uint32_t const m_alignedSize; ResourceType const m_resourceType; - bool const m_isCoherent; + ref_ptr m_memoryBlock; - Allocation(ResourceType resourceType, uint64_t blockHash, VkDeviceMemory memory, - uint32_t offset, uint32_t size, bool isCoherent) + Allocation(ResourceType resourceType, uint64_t blockHash, uint32_t offset, + uint32_t size, ref_ptr memoryBlock) : m_blockHash(blockHash) - , m_memory(memory) , m_alignedOffset(offset) , m_alignedSize(size) , m_resourceType(resourceType) - , m_isCoherent(isCoherent) + , m_memoryBlock(memoryBlock) {} }; @@ -78,19 +88,9 @@ private: bool m_isInDeallocationSession = false; uint32_t m_deallocationSessionMask = 0; - struct MemoryBlock - { - VkDeviceMemory m_memory = {}; - uint32_t m_blockSize = 0; - uint32_t m_freeOffset = 0; - uint32_t m_allocationCounter = 0; - bool m_isCoherent = false; - - bool operator<(MemoryBlock const & b) const { return m_blockSize < b.m_blockSize; } - }; - - std::array>, kResourcesCount> m_memory; - std::array, kResourcesCount> m_freeBlocks; + using MemoryBlocks = std::vector>; + std::array, kResourcesCount> m_memory; + std::array m_freeBlocks; std::array m_sizes = {}; }; } // namespace vulkan diff --git a/drape/vulkan/vulkan_mesh_object_impl.cpp b/drape/vulkan/vulkan_mesh_object_impl.cpp index c17958a95c..cffadd4efa 100644 --- a/drape/vulkan/vulkan_mesh_object_impl.cpp +++ b/drape/vulkan/vulkan_mesh_object_impl.cpp @@ -39,24 +39,14 @@ public: sizeof(m_mesh->m_buffers[i].m_data[0])); m_geometryBuffers[i] = m_objectManager->CreateBuffer(VulkanMemoryManager::ResourceType::Geometry, sizeInBytes, 0 /* batcherHash */); - void * gpuPtr = nullptr; - CHECK_VK_CALL(vkMapMemory(device, m_geometryBuffers[i].m_allocation->m_memory, - m_geometryBuffers[i].m_allocation->m_alignedOffset, - m_geometryBuffers[i].m_allocation->m_alignedSize, 0, &gpuPtr)); + void * gpuPtr = m_objectManager->Map(m_geometryBuffers[i]); memcpy(gpuPtr, m_mesh->m_buffers[i].m_data.data(), sizeInBytes); - if (!m_geometryBuffers[i].m_allocation->m_isCoherent) - { - VkMappedMemoryRange mappedRange = {}; - mappedRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE; - mappedRange.memory = m_geometryBuffers[i].m_allocation->m_memory; - mappedRange.offset = m_geometryBuffers[i].m_allocation->m_alignedOffset; - mappedRange.size = m_geometryBuffers[i].m_allocation->m_alignedSize; - CHECK_VK_CALL(vkFlushMappedMemoryRanges(device, 1, &mappedRange)); - } - vkUnmapMemory(device, m_geometryBuffers[i].m_allocation->m_memory); + m_objectManager->Flush(m_geometryBuffers[i]); + m_objectManager->Unmap(m_geometryBuffers[i]); + CHECK_VK_CALL(vkBindBufferMemory(device, m_geometryBuffers[i].m_buffer, - m_geometryBuffers[i].m_allocation->m_memory, - m_geometryBuffers[i].m_allocation->m_alignedOffset)); + m_geometryBuffers[i].GetMemory(), + m_geometryBuffers[i].GetAlignedOffset())); } } diff --git a/drape/vulkan/vulkan_object_manager.cpp b/drape/vulkan/vulkan_object_manager.cpp index dc5194cd27..6ce98d5397 100644 --- a/drape/vulkan/vulkan_object_manager.cpp +++ b/drape/vulkan/vulkan_object_manager.cpp @@ -107,6 +107,7 @@ VulkanObject VulkanObjectManager::CreateImage(VkImageUsageFlagBits usageFlagBits void VulkanObjectManager::DestroyObject(VulkanObject object) { std::lock_guard lock(m_mutex); + CHECK(!object.m_allocation->m_memoryBlock->m_isBlocked, ()); m_queueToDestroy.push_back(std::move(object)); } @@ -149,5 +150,46 @@ void VulkanObjectManager::CollectObjects() m_memoryManager.EndDeallocationSession(); m_queueToDestroy.clear(); } + +uint8_t * VulkanObjectManager::Map(VulkanObject object) +{ + std::lock_guard lock(m_mutex); + CHECK(!object.m_allocation->m_memoryBlock->m_isBlocked, ()); + + CHECK(object.m_buffer != 0 || object.m_image != 0, ()); + uint8_t * ptr = nullptr; + CHECK_VK_CALL(vkMapMemory(m_device, object.GetMemory(), object.GetAlignedOffset(), + object.GetAlignedSize(), 0, reinterpret_cast(&ptr))); + object.m_allocation->m_memoryBlock->m_isBlocked = true; + return ptr; +} + +void VulkanObjectManager::Flush(VulkanObject object, uint32_t offset, uint32_t size) +{ + std::lock_guard lock(m_mutex); + + if (object.m_allocation->m_memoryBlock->m_isCoherent) + return; + + CHECK(object.m_allocation->m_memoryBlock->m_isBlocked, ()); + + VkMappedMemoryRange mappedRange = {}; + mappedRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE; + mappedRange.memory = object.GetMemory(); + mappedRange.offset = object.GetAlignedOffset() + offset; + if (size == 0) + mappedRange.size = object.GetAlignedSize(); + else + mappedRange.size = mappedRange.offset + size; + CHECK_VK_CALL(vkFlushMappedMemoryRanges(m_device, 1, &mappedRange)); +} + +void VulkanObjectManager::Unmap(VulkanObject object) +{ + std::lock_guard lock(m_mutex); + CHECK(object.m_allocation->m_memoryBlock->m_isBlocked, ()); + vkUnmapMemory(m_device, object.GetMemory()); + object.m_allocation->m_memoryBlock->m_isBlocked = false; +} } // namespace vulkan } // namespace dp diff --git a/drape/vulkan/vulkan_object_manager.hpp b/drape/vulkan/vulkan_object_manager.hpp index 8a1dfc3c33..8fadbf8b57 100644 --- a/drape/vulkan/vulkan_object_manager.hpp +++ b/drape/vulkan/vulkan_object_manager.hpp @@ -3,6 +3,8 @@ #include "drape/pointers.hpp" #include "drape/vulkan/vulkan_memory_manager.hpp" +#include "base/assert.hpp" + #include #include @@ -21,6 +23,25 @@ struct VulkanObject VkImage m_image = {}; VkImageView m_imageView = {}; VulkanMemoryManager::AllocationPtr m_allocation; + + VkDeviceMemory GetMemory() const + { + ASSERT(m_allocation != nullptr, ()); + ASSERT(m_allocation->m_memoryBlock != nullptr, ()); + return m_allocation->m_memoryBlock->m_memory; + } + + uint32_t GetAlignedOffset() const + { + ASSERT(m_allocation != nullptr, ()); + return m_allocation->m_alignedOffset; + } + + uint32_t GetAlignedSize() const + { + ASSERT(m_allocation != nullptr, ()); + return m_allocation->m_alignedSize; + } }; class VulkanStagingBuffer; @@ -38,6 +59,9 @@ public: VulkanObject CreateImage(VkImageUsageFlagBits usageFlagBits, VkFormat format, VkImageAspectFlags aspectFlags, uint32_t width, uint32_t height); + uint8_t * Map(VulkanObject object); + void Flush(VulkanObject object, uint32_t offset = 0, uint32_t size = 0); + void Unmap(VulkanObject object); ref_ptr GetDefaultStagingBuffer() const; void FlushDefaultStagingBuffer(); diff --git a/drape/vulkan/vulkan_staging_buffer.cpp b/drape/vulkan/vulkan_staging_buffer.cpp index 5c607fa158..3bb42edda2 100644 --- a/drape/vulkan/vulkan_staging_buffer.cpp +++ b/drape/vulkan/vulkan_staging_buffer.cpp @@ -16,28 +16,24 @@ VulkanStagingBuffer::VulkanStagingBuffer(ref_ptr objectMana , m_sizeInBytes(sizeInBytes) { auto constexpr kStagingBuffer = VulkanMemoryManager::ResourceType::Staging; - VkDevice device = objectManager->GetDevice(); + VkDevice device = m_objectManager->GetDevice(); auto const & mm = m_objectManager->GetMemoryManager(); - m_object = objectManager->CreateBuffer(kStagingBuffer, sizeInBytes, 0 /* batcherHash */); + m_object = m_objectManager->CreateBuffer(kStagingBuffer, sizeInBytes, 0 /* batcherHash */); VkMemoryRequirements memReqs = {}; vkGetBufferMemoryRequirements(device, m_object.m_buffer, &memReqs); m_sizeAlignment = mm.GetSizeAlignment(memReqs); m_offsetAlignment = mm.GetOffsetAlignment(kStagingBuffer); - CHECK_VK_CALL(vkBindBufferMemory(device, m_object.m_buffer, - m_object.m_allocation->m_memory, - m_object.m_allocation->m_alignedOffset)); + CHECK_VK_CALL(vkBindBufferMemory(device, m_object.m_buffer, m_object.GetMemory(), + m_object.GetAlignedOffset())); - CHECK_VK_CALL(vkMapMemory(device, m_object.m_allocation->m_memory, - m_object.m_allocation->m_alignedOffset, - m_object.m_allocation->m_alignedSize, 0, - reinterpret_cast(&m_pointer))); + m_pointer = m_objectManager->Map(m_object); } VulkanStagingBuffer::~VulkanStagingBuffer() { - vkUnmapMemory(m_objectManager->GetDevice(), m_object.m_allocation->m_memory); + m_objectManager->Unmap(m_object); m_objectManager->DestroyObject(m_object); } @@ -60,8 +56,7 @@ VulkanStagingBuffer::StagingData VulkanStagingBuffer::Reserve(uint32_t sizeInByt // Update offset and align it. m_offset += alignedSize; - m_offset = std::min(mm.GetAligned(m_offset, m_offsetAlignment), - m_object.m_allocation->m_alignedSize); + m_offset = std::min(mm.GetAligned(m_offset, m_offsetAlignment), m_object.GetAlignedSize()); StagingData result; result.m_stagingBuffer = m_object.m_buffer; @@ -86,15 +81,11 @@ VulkanStagingBuffer::StagingData const & VulkanStagingBuffer::GetReservationById void VulkanStagingBuffer::Flush() { - if (m_object.m_allocation->m_isCoherent || m_offset == 0) + if (m_offset == 0) return; - VkMappedMemoryRange mappedRange = {}; - mappedRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE; - mappedRange.memory = m_object.m_allocation->m_memory; - mappedRange.offset = m_object.m_allocation->m_alignedOffset; - mappedRange.size = mappedRange.offset + m_offset; - CHECK_VK_CALL(vkFlushMappedMemoryRanges(m_objectManager->GetDevice(), 1, &mappedRange)); + auto const size = m_offset; + m_objectManager->Flush(m_object, 0 /* offset */, size); } void VulkanStagingBuffer::Reset() diff --git a/drape/vulkan/vulkan_texture.cpp b/drape/vulkan/vulkan_texture.cpp index 3e1755625d..a72d40abe5 100644 --- a/drape/vulkan/vulkan_texture.cpp +++ b/drape/vulkan/vulkan_texture.cpp @@ -150,8 +150,7 @@ void VulkanTexture::Create(ref_ptr context, Params const & VK_IMAGE_ASPECT_COLOR_BIT, params.m_width, params.m_height); CHECK_VK_CALL(vkBindImageMemory(vulkanContext->GetDevice(), m_textureObject.m_image, - m_textureObject.m_allocation->m_memory, - m_textureObject.m_allocation->m_alignedOffset)); + m_textureObject.GetMemory(), m_textureObject.GetAlignedOffset())); } } diff --git a/shaders/vulkan_program_params.cpp b/shaders/vulkan_program_params.cpp index 89e927237f..66ecc2b3f0 100644 --- a/shaders/vulkan_program_params.cpp +++ b/shaders/vulkan_program_params.cpp @@ -26,14 +26,10 @@ VulkanProgramParamsSetter::UniformBuffer CreateUniformBuffer(VkDevice device, sizeAlignment = objectManager->GetMemoryManager().GetSizeAlignment(memReqs); offsetAlignment = objectManager->GetMemoryManager().GetOffsetAlignment(kUniformBuffer); - CHECK_VK_CALL(vkBindBufferMemory(device, result.m_object.m_buffer, - result.m_object.m_allocation->m_memory, - result.m_object.m_allocation->m_alignedOffset)); + CHECK_VK_CALL(vkBindBufferMemory(device, result.m_object.m_buffer, result.m_object.GetMemory(), + result.m_object.GetAlignedOffset())); - CHECK_VK_CALL(vkMapMemory(device, result.m_object.m_allocation->m_memory, - result.m_object.m_allocation->m_alignedOffset, - result.m_object.m_allocation->m_alignedSize, 0, - reinterpret_cast(&result.m_pointer))); + result.m_pointer = objectManager->Map(result.m_object); return result; } } // namespace @@ -46,9 +42,9 @@ VulkanProgramParamsSetter::VulkanProgramParamsSetter(ref_ptrRegisterHandler(VulkanBaseContext::HandlerType::PrePresent, - [this](ref_ptr c) + [this](ref_ptr) { - Flush(c); + Flush(); }); m_finishHandlerId = context->RegisterHandler(VulkanBaseContext::HandlerType::PostPresent, [this](ref_ptr) @@ -64,10 +60,9 @@ VulkanProgramParamsSetter::~VulkanProgramParamsSetter() void VulkanProgramParamsSetter::Destroy(ref_ptr context) { - VkDevice device = context->GetDevice(); for (auto & b : m_uniformBuffers) { - vkUnmapMemory(device, b.m_object.m_allocation->m_memory); + m_objectManager->Unmap(b.m_object); m_objectManager->DestroyObject(b.m_object); } m_uniformBuffers.clear(); @@ -75,20 +70,15 @@ void VulkanProgramParamsSetter::Destroy(ref_ptr c context->UnregisterHandler(m_flushHandlerId); } -void VulkanProgramParamsSetter::Flush(ref_ptr context) +void VulkanProgramParamsSetter::Flush() { - VkDevice device = context->GetDevice(); for (auto & ub : m_uniformBuffers) { - if (ub.m_object.m_allocation->m_isCoherent || ub.m_freeOffset == 0) + if (ub.m_freeOffset == 0) continue; - VkMappedMemoryRange mappedRange = {}; - mappedRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE; - mappedRange.memory = ub.m_object.m_allocation->m_memory; - mappedRange.offset = ub.m_object.m_allocation->m_alignedOffset; - mappedRange.size = mappedRange.offset + ub.m_freeOffset; - CHECK_VK_CALL(vkFlushMappedMemoryRanges(device, 1, &mappedRange)); + auto const size = ub.m_freeOffset; + m_objectManager->Flush(ub.m_object, 0 /* offset */, size); } } @@ -132,7 +122,7 @@ void VulkanProgramParamsSetter::ApplyBytes(ref_ptrm_alignedSize); + m_uniformBuffers[index].m_object.GetAlignedSize()); memcpy(ptr, data, sizeInBytes); diff --git a/shaders/vulkan_program_params.hpp b/shaders/vulkan_program_params.hpp index b884426a0b..0fee30fc3b 100644 --- a/shaders/vulkan_program_params.hpp +++ b/shaders/vulkan_program_params.hpp @@ -30,7 +30,7 @@ public: ~VulkanProgramParamsSetter() override; void Destroy(ref_ptr context); - void Flush(ref_ptr context); + void Flush(); void Finish(); void Apply(ref_ptr context, ref_ptr program,