From d1554aed37955f3e340b303e1215004c558a53f9 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Thu, 2 May 2019 01:35:04 +0200 Subject: [PATCH] - VMA_MEMORY_USAGE_CPU_TO_GPU does not require VK_MEMORY_PROPERTY_HOST_COHERENT_BIT to bet set. Current implementation requires this flag as vkFlushMappedMemoryRanges is never called. --- src/rendering/vulkan/system/vk_buffers.cpp | 10 +++++-- src/rendering/vulkan/system/vk_builders.h | 29 +++++++++++++++++++ .../vulkan/textures/vk_hwtexture.cpp | 5 +++- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/rendering/vulkan/system/vk_buffers.cpp b/src/rendering/vulkan/system/vk_buffers.cpp index d02899568..cc570d812 100644 --- a/src/rendering/vulkan/system/vk_buffers.cpp +++ b/src/rendering/vulkan/system/vk_buffers.cpp @@ -76,7 +76,10 @@ void VKBuffer::SetData(size_t size, const void *data, bool staticdata) mPersistent = screen->BuffersArePersistent(); BufferBuilder builder; - builder.setUsage(mBufferType, VMA_MEMORY_USAGE_CPU_TO_GPU, mPersistent ? VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT : 0); + builder.setUsage(mBufferType, VMA_MEMORY_USAGE_UNKNOWN, mPersistent ? VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT : 0); + builder.setMemoryType( + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); builder.setSize(size); mBuffer = builder.create(fb->device); @@ -127,7 +130,10 @@ void VKBuffer::Resize(size_t newsize) // Create new buffer BufferBuilder builder; - builder.setUsage(mBufferType, VMA_MEMORY_USAGE_CPU_TO_GPU, VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT); + builder.setUsage(mBufferType, VMA_MEMORY_USAGE_UNKNOWN, VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT); + builder.setMemoryType( + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); builder.setSize(newsize); mBuffer = builder.create(fb->device); buffersize = newsize; diff --git a/src/rendering/vulkan/system/vk_builders.h b/src/rendering/vulkan/system/vk_builders.h index 5353d3153..af5b54c82 100644 --- a/src/rendering/vulkan/system/vk_builders.h +++ b/src/rendering/vulkan/system/vk_builders.h @@ -42,11 +42,13 @@ public: void setSamples(VkSampleCountFlagBits samples); void setFormat(VkFormat format); void setUsage(VkImageUsageFlags imageUsage, VmaMemoryUsage memoryUsage = VMA_MEMORY_USAGE_GPU_ONLY, VmaAllocationCreateFlags allocFlags = 0); + void setMemoryType(VkMemoryPropertyFlags requiredFlags, VkMemoryPropertyFlags preferredFlags, uint32_t memoryTypeBits = 0); void setLinearTiling(); bool isFormatSupported(VulkanDevice *device); std::unique_ptr create(VulkanDevice *device); + std::unique_ptr tryCreate(VulkanDevice *device); private: VkImageCreateInfo imageInfo = {}; @@ -92,6 +94,7 @@ public: void setSize(size_t size); void setUsage(VkBufferUsageFlags bufferUsage, VmaMemoryUsage memoryUsage = VMA_MEMORY_USAGE_GPU_ONLY, VmaAllocationCreateFlags allocFlags = 0); + void setMemoryType(VkMemoryPropertyFlags requiredFlags, VkMemoryPropertyFlags preferredFlags, uint32_t memoryTypeBits = 0); std::unique_ptr create(VulkanDevice *device); @@ -400,6 +403,13 @@ inline void ImageBuilder::setUsage(VkImageUsageFlags usage, VmaMemoryUsage memor allocInfo.flags = allocFlags; } +inline void ImageBuilder::setMemoryType(VkMemoryPropertyFlags requiredFlags, VkMemoryPropertyFlags preferredFlags, uint32_t memoryTypeBits) +{ + allocInfo.requiredFlags = requiredFlags; + allocInfo.preferredFlags = preferredFlags; + allocInfo.memoryTypeBits = memoryTypeBits; +} + inline bool ImageBuilder::isFormatSupported(VulkanDevice *device) { VkImageFormatProperties properties = { }; @@ -426,6 +436,18 @@ inline std::unique_ptr ImageBuilder::create(VulkanDevice *device) return std::make_unique(device, image, allocation, imageInfo.extent.width, imageInfo.extent.height, imageInfo.mipLevels); } +inline std::unique_ptr ImageBuilder::tryCreate(VulkanDevice *device) +{ + VkImage image; + VmaAllocation allocation; + + VkResult result = vmaCreateImage(device->allocator, &imageInfo, &allocInfo, &image, &allocation, nullptr); + if (result != VK_SUCCESS) + return nullptr; + + return std::make_unique(device, image, allocation, imageInfo.extent.width, imageInfo.extent.height, imageInfo.mipLevels); +} + ///////////////////////////////////////////////////////////////////////////// inline ImageViewBuilder::ImageViewBuilder() @@ -547,6 +569,13 @@ inline void BufferBuilder::setUsage(VkBufferUsageFlags bufferUsage, VmaMemoryUsa allocInfo.flags = allocFlags; } +inline void BufferBuilder::setMemoryType(VkMemoryPropertyFlags requiredFlags, VkMemoryPropertyFlags preferredFlags, uint32_t memoryTypeBits) +{ + allocInfo.requiredFlags = requiredFlags; + allocInfo.preferredFlags = preferredFlags; + allocInfo.memoryTypeBits = memoryTypeBits; +} + inline std::unique_ptr BufferBuilder::create(VulkanDevice *device) { VkBuffer buffer; diff --git a/src/rendering/vulkan/textures/vk_hwtexture.cpp b/src/rendering/vulkan/textures/vk_hwtexture.cpp index f7571e1f7..9a093e706 100644 --- a/src/rendering/vulkan/textures/vk_hwtexture.cpp +++ b/src/rendering/vulkan/textures/vk_hwtexture.cpp @@ -337,8 +337,11 @@ void VkHardwareTexture::AllocateBuffer(int w, int h, int texelsize) ImageBuilder imgbuilder; imgbuilder.setFormat(format); imgbuilder.setSize(w, h); - imgbuilder.setUsage(VK_IMAGE_USAGE_SAMPLED_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU, VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT); imgbuilder.setLinearTiling(); + imgbuilder.setUsage(VK_IMAGE_USAGE_SAMPLED_BIT, VMA_MEMORY_USAGE_UNKNOWN, VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT); + imgbuilder.setMemoryType( + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); mImage = imgbuilder.create(fb->device); mImage->SetDebugName("VkHardwareTexture.mImage"); mImageLayout = VK_IMAGE_LAYOUT_GENERAL;