Commit b947498b by Jamie Madill Committed by Commit Bot

Vulkan: Clean up VMA wrapper classes.

Make these more consistent with the rest of the wrapper classes. Also handle some VkResult errors that were being ignored. Will pave the way for better handling of buffer allocation error conditions. Bug: chromium:1086532 Change-Id: Idc5b3f0e2945b1f44f152d33e8cc572f83a6b658 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2219136 Commit-Queue: Jamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarMohan Maiya <m.maiya@samsung.com>
parent 558882a1
...@@ -505,7 +505,7 @@ void RendererVk::onDestroy() ...@@ -505,7 +505,7 @@ void RendererVk::onDestroy()
mPipelineCache.destroy(mDevice); mPipelineCache.destroy(mDevice);
mSamplerCache.destroy(this); mSamplerCache.destroy(this);
vma::DestroyAllocator(mAllocator); mAllocator.destroy();
if (mGlslangInitialized) if (mGlslangInitialized)
{ {
...@@ -825,7 +825,7 @@ angle::Result RendererVk::initialize(DisplayVk *displayVk, ...@@ -825,7 +825,7 @@ angle::Result RendererVk::initialize(DisplayVk *displayVk,
} }
// Create VMA allocator // Create VMA allocator
ANGLE_VK_TRY(displayVk, vma::InitAllocator(mPhysicalDevice, mDevice, mInstance, &mAllocator)); ANGLE_VK_TRY(displayVk, mAllocator.init(mPhysicalDevice, mDevice, mInstance));
// Store the physical device memory properties so we can find the right memory pools. // Store the physical device memory properties so we can find the right memory pools.
mMemoryProperties.init(mPhysicalDevice); mMemoryProperties.init(mPhysicalDevice);
......
...@@ -106,7 +106,7 @@ class RendererVk : angle::NonCopyable ...@@ -106,7 +106,7 @@ class RendererVk : angle::NonCopyable
} }
VkDevice getDevice() const { return mDevice; } VkDevice getDevice() const { return mDevice; }
const VmaAllocator &getAllocator() const { return mAllocator; } const vk::Allocator &getAllocator() const { return mAllocator; }
angle::Result selectPresentQueueForSurface(DisplayVk *displayVk, angle::Result selectPresentQueueForSurface(DisplayVk *displayVk,
VkSurfaceKHR surface, VkSurfaceKHR surface,
...@@ -376,7 +376,7 @@ class RendererVk : angle::NonCopyable ...@@ -376,7 +376,7 @@ class RendererVk : angle::NonCopyable
// track whether we initialized (or released) glslang // track whether we initialized (or released) glslang
bool mGlslangInitialized; bool mGlslangInitialized;
VmaAllocator mAllocator; vk::Allocator mAllocator;
SamplerCache mSamplerCache; SamplerCache mSamplerCache;
vk::ActiveHandleCounter mActiveHandleCounts; vk::ActiveHandleCounter mActiveHandleCounts;
}; };
......
...@@ -2151,10 +2151,14 @@ angle::Result BufferHelper::init(Context *context, ...@@ -2151,10 +2151,14 @@ angle::Result BufferHelper::init(Context *context,
VkMemoryPropertyFlags preferredFlags = VkMemoryPropertyFlags preferredFlags =
(memoryPropertyFlags & (~VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)); (memoryPropertyFlags & (~VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT));
mAllocation.createBufferAndMemory( const vk::Allocator &allocator = renderer->getAllocator();
renderer->getAllocator(), createInfo, requiredFlags, preferredFlags, uint32_t memoryTypeIndex = 0;
renderer->getFeatures().persistentlyMappedBuffers.enabled, &mBuffer, &mMemoryPropertyFlags); ANGLE_VK_TRY(context,
allocator.createBuffer(*createInfo, requiredFlags, preferredFlags,
renderer->getFeatures().persistentlyMappedBuffers.enabled,
&memoryTypeIndex, &mBuffer, &mAllocation));
allocator.getMemoryTypeProperties(memoryTypeIndex, &mMemoryPropertyFlags);
mCurrentQueueFamilyIndex = renderer->getQueueFamilyIndex(); mCurrentQueueFamilyIndex = renderer->getQueueFamilyIndex();
if (renderer->getFeatures().allocateNonZeroMemory.enabled) if (renderer->getFeatures().allocateNonZeroMemory.enabled)
...@@ -2172,8 +2176,8 @@ angle::Result BufferHelper::init(Context *context, ...@@ -2172,8 +2176,8 @@ angle::Result BufferHelper::init(Context *context,
// Can map the memory. // Can map the memory.
// Pick an arbitrary value to initialize non-zero memory for sanitization. // Pick an arbitrary value to initialize non-zero memory for sanitization.
constexpr int kNonZeroInitValue = 55; constexpr int kNonZeroInitValue = 55;
ANGLE_TRY(InitMappableAllocation(renderer->getAllocator(), &mAllocation, mSize, ANGLE_TRY(InitMappableAllocation(allocator, &mAllocation, mSize, kNonZeroInitValue,
kNonZeroInitValue, mMemoryPropertyFlags)); mMemoryPropertyFlags));
} }
} }
......
...@@ -409,16 +409,18 @@ angle::Result StagingBuffer::init(Context *context, VkDeviceSize size, StagingUs ...@@ -409,16 +409,18 @@ angle::Result StagingBuffer::init(Context *context, VkDeviceSize size, StagingUs
createInfo.queueFamilyIndexCount = 0; createInfo.queueFamilyIndexCount = 0;
createInfo.pQueueFamilyIndices = nullptr; createInfo.pQueueFamilyIndices = nullptr;
VkMemoryPropertyFlags memoryPropertyOutFlags;
VkMemoryPropertyFlags preferredFlags = 0; VkMemoryPropertyFlags preferredFlags = 0;
VkMemoryPropertyFlags requiredFlags = VkMemoryPropertyFlags requiredFlags =
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
mAllocation.createBufferAndMemory( RendererVk *renderer = context->getRenderer();
context->getRenderer()->getAllocator(), &createInfo, requiredFlags, preferredFlags, const vk::Allocator &allocator = renderer->getAllocator();
context->getRenderer()->getFeatures().persistentlyMappedBuffers.enabled, &mBuffer,
&memoryPropertyOutFlags);
uint32_t memoryTypeIndex = 0;
ANGLE_VK_TRY(context,
allocator.createBuffer(createInfo, requiredFlags, preferredFlags,
renderer->getFeatures().persistentlyMappedBuffers.enabled,
&memoryTypeIndex, &mBuffer, &mAllocation));
mSize = static_cast<size_t>(size); mSize = static_cast<size_t>(size);
return angle::Result::Continue; return angle::Result::Continue;
} }
...@@ -441,7 +443,7 @@ void StagingBuffer::collectGarbage(RendererVk *renderer, Serial serial) ...@@ -441,7 +443,7 @@ void StagingBuffer::collectGarbage(RendererVk *renderer, Serial serial)
renderer->collectGarbage(std::move(sharedUse), std::move(garbageList)); renderer->collectGarbage(std::move(sharedUse), std::move(garbageList));
} }
angle::Result InitMappableAllocation(VmaAllocator allocator, angle::Result InitMappableAllocation(const vk::Allocator &allocator,
Allocation *allocation, Allocation *allocation,
VkDeviceSize size, VkDeviceSize size,
int value, int value,
...@@ -653,7 +655,7 @@ void GarbageObject::destroy(RendererVk *renderer) ...@@ -653,7 +655,7 @@ void GarbageObject::destroy(RendererVk *renderer)
vkDestroyQueryPool(device, (VkQueryPool)mHandle, nullptr); vkDestroyQueryPool(device, (VkQueryPool)mHandle, nullptr);
break; break;
case HandleType::Allocation: case HandleType::Allocation:
vma::FreeMemory(renderer->getAllocator(), (VmaAllocation)mHandle); vma::FreeMemory(renderer->getAllocator().getHandle(), (VmaAllocation)mHandle);
break; break;
default: default:
UNREACHABLE(); UNREACHABLE();
......
...@@ -317,8 +317,8 @@ class StagingBuffer final : angle::NonCopyable ...@@ -317,8 +317,8 @@ class StagingBuffer final : angle::NonCopyable
size_t mSize; size_t mSize;
}; };
angle::Result InitMappableAllocation(VmaAllocator allocator, angle::Result InitMappableAllocation(const vk::Allocator &allocator,
Allocation *allcation, Allocation *allocation,
VkDeviceSize size, VkDeviceSize size,
int value, int value,
VkMemoryPropertyFlags memoryPropertyFlags); VkMemoryPropertyFlags memoryPropertyFlags);
......
...@@ -28,6 +28,8 @@ namespace vk ...@@ -28,6 +28,8 @@ namespace vk
// DescriptorSet // DescriptorSet
#define ANGLE_HANDLE_TYPES_X(FUNC) \ #define ANGLE_HANDLE_TYPES_X(FUNC) \
FUNC(Allocation) \
FUNC(Allocator) \
FUNC(Buffer) \ FUNC(Buffer) \
FUNC(BufferView) \ FUNC(BufferView) \
FUNC(CommandPool) \ FUNC(CommandPool) \
...@@ -46,8 +48,7 @@ namespace vk ...@@ -46,8 +48,7 @@ namespace vk
FUNC(RenderPass) \ FUNC(RenderPass) \
FUNC(Sampler) \ FUNC(Sampler) \
FUNC(Semaphore) \ FUNC(Semaphore) \
FUNC(ShaderModule) \ FUNC(ShaderModule)
FUNC(Allocation)
#define ANGLE_COMMA_SEP_FUNC(TYPE) TYPE, #define ANGLE_COMMA_SEP_FUNC(TYPE) TYPE,
...@@ -457,23 +458,39 @@ class DeviceMemory final : public WrappedObject<DeviceMemory, VkDeviceMemory> ...@@ -457,23 +458,39 @@ class DeviceMemory final : public WrappedObject<DeviceMemory, VkDeviceMemory>
void unmap(VkDevice device) const; void unmap(VkDevice device) const;
}; };
class Allocator : public WrappedObject<Allocator, VmaAllocator>
{
public:
Allocator() = default;
void destroy();
VkResult init(VkPhysicalDevice physicalDevice, VkDevice device, VkInstance instance);
// Initializes the buffer handle and memory allocation.
VkResult createBuffer(const VkBufferCreateInfo &bufferCreateInfo,
VkMemoryPropertyFlags requiredFlags,
VkMemoryPropertyFlags preferredFlags,
bool persistentlyMappedBuffers,
uint32_t *memoryTypeIndexOut,
Buffer *bufferOut,
Allocation *allocationOut) const;
void getMemoryTypeProperties(uint32_t memoryTypeIndex, VkMemoryPropertyFlags *flagsOut) const;
};
class Allocation final : public WrappedObject<Allocation, VmaAllocation> class Allocation final : public WrappedObject<Allocation, VmaAllocation>
{ {
public: public:
Allocation() = default; Allocation() = default;
void destroy(VmaAllocator allocator); void destroy(const Allocator &allocator);
VkResult createBufferAndMemory(VmaAllocator allocator, VkResult map(const Allocator &allocator, uint8_t **mapPointer) const;
const VkBufferCreateInfo *pBufferCreateInfo, void unmap(const Allocator &allocator) const;
VkMemoryPropertyFlags requiredFlags, void flush(const Allocator &allocator, VkDeviceSize offset, VkDeviceSize size);
VkMemoryPropertyFlags preferredFlags, void invalidate(const Allocator &allocator, VkDeviceSize offset, VkDeviceSize size);
bool persistentlyMappedBuffers,
Buffer *buffer, private:
VkMemoryPropertyFlags *pMemPropertyOut); friend class Allocator;
VkResult map(VmaAllocator allocator, uint8_t **mapPointer) const;
void unmap(VmaAllocator allocator) const;
void flush(VmaAllocator allocator, VkDeviceSize offset, VkDeviceSize size);
void invalidate(VmaAllocator allocator, VkDeviceSize offset, VkDeviceSize size);
}; };
class RenderPass final : public WrappedObject<RenderPass, VkRenderPass> class RenderPass final : public WrappedObject<RenderPass, VkRenderPass>
...@@ -501,6 +518,9 @@ class Buffer final : public WrappedObject<Buffer, VkBuffer> ...@@ -501,6 +518,9 @@ class Buffer final : public WrappedObject<Buffer, VkBuffer>
VkResult init(VkDevice device, const VkBufferCreateInfo &createInfo); VkResult init(VkDevice device, const VkBufferCreateInfo &createInfo);
VkResult bindMemory(VkDevice device, const DeviceMemory &deviceMemory); VkResult bindMemory(VkDevice device, const DeviceMemory &deviceMemory);
void getMemoryRequirements(VkDevice device, VkMemoryRequirements *memoryRequirementsOut); void getMemoryRequirements(VkDevice device, VkMemoryRequirements *memoryRequirementsOut);
private:
friend class Allocator;
}; };
class BufferView final : public WrappedObject<BufferView, VkBufferView> class BufferView final : public WrappedObject<BufferView, VkBufferView>
...@@ -1340,61 +1360,83 @@ ANGLE_INLINE void DeviceMemory::unmap(VkDevice device) const ...@@ -1340,61 +1360,83 @@ ANGLE_INLINE void DeviceMemory::unmap(VkDevice device) const
vkUnmapMemory(device, mHandle); vkUnmapMemory(device, mHandle);
} }
// Allocation implementation. // Allocator implementation.
ANGLE_INLINE void Allocation::destroy(VmaAllocator allocator) ANGLE_INLINE void Allocator::destroy()
{ {
if (valid()) if (valid())
{ {
vma::FreeMemory(allocator, mHandle); vma::DestroyAllocator(mHandle);
mHandle = VK_NULL_HANDLE; mHandle = VK_NULL_HANDLE;
} }
} }
ANGLE_INLINE VkResult Allocation::createBufferAndMemory(VmaAllocator allocator, ANGLE_INLINE VkResult Allocator::init(VkPhysicalDevice physicalDevice,
const VkBufferCreateInfo *pBufferCreateInfo, VkDevice device,
VkMemoryPropertyFlags requiredFlags, VkInstance instance)
VkMemoryPropertyFlags preferredFlags,
bool persistentlyMappedBuffers,
Buffer *buffer,
VkMemoryPropertyFlags *pMemPropertyOut)
{ {
ASSERT(!valid()); ASSERT(!valid());
VkResult result; return vma::InitAllocator(physicalDevice, device, instance, &mHandle);
uint32_t memoryTypeIndex; }
VkBuffer bufferHandle;
result = ANGLE_INLINE VkResult Allocator::createBuffer(const VkBufferCreateInfo &bufferCreateInfo,
vma::CreateBuffer(allocator, pBufferCreateInfo, requiredFlags, preferredFlags, VkMemoryPropertyFlags requiredFlags,
persistentlyMappedBuffers, &memoryTypeIndex, &bufferHandle, &mHandle); VkMemoryPropertyFlags preferredFlags,
vma::GetMemoryTypeProperties(allocator, memoryTypeIndex, pMemPropertyOut); bool persistentlyMappedBuffers,
buffer->setHandle(bufferHandle); uint32_t *memoryTypeIndexOut,
Buffer *bufferOut,
Allocation *allocationOut) const
{
ASSERT(valid());
ASSERT(bufferOut && !bufferOut->valid());
ASSERT(allocationOut && !allocationOut->valid());
return vma::CreateBuffer(mHandle, &bufferCreateInfo, requiredFlags, preferredFlags,
persistentlyMappedBuffers, memoryTypeIndexOut, &bufferOut->mHandle,
&allocationOut->mHandle);
}
ANGLE_INLINE void Allocator::getMemoryTypeProperties(uint32_t memoryTypeIndex,
VkMemoryPropertyFlags *flagsOut) const
{
ASSERT(valid());
vma::GetMemoryTypeProperties(mHandle, memoryTypeIndex, flagsOut);
}
return result; // Allocation implementation.
ANGLE_INLINE void Allocation::destroy(const Allocator &allocator)
{
if (valid())
{
vma::FreeMemory(allocator.getHandle(), mHandle);
mHandle = VK_NULL_HANDLE;
}
} }
ANGLE_INLINE VkResult Allocation::map(VmaAllocator allocator, uint8_t **mapPointer) const ANGLE_INLINE VkResult Allocation::map(const Allocator &allocator, uint8_t **mapPointer) const
{ {
ASSERT(valid()); ASSERT(valid());
return vma::MapMemory(allocator, mHandle, (void **)mapPointer); return vma::MapMemory(allocator.getHandle(), mHandle, (void **)mapPointer);
} }
ANGLE_INLINE void Allocation::unmap(VmaAllocator allocator) const ANGLE_INLINE void Allocation::unmap(const Allocator &allocator) const
{ {
ASSERT(valid()); ASSERT(valid());
vma::UnmapMemory(allocator, mHandle); vma::UnmapMemory(allocator.getHandle(), mHandle);
} }
ANGLE_INLINE void Allocation::flush(VmaAllocator allocator, VkDeviceSize offset, VkDeviceSize size) ANGLE_INLINE void Allocation::flush(const Allocator &allocator,
VkDeviceSize offset,
VkDeviceSize size)
{ {
ASSERT(valid()); ASSERT(valid());
vma::FlushAllocation(allocator, mHandle, offset, size); vma::FlushAllocation(allocator.getHandle(), mHandle, offset, size);
} }
ANGLE_INLINE void Allocation::invalidate(VmaAllocator allocator, ANGLE_INLINE void Allocation::invalidate(const Allocator &allocator,
VkDeviceSize offset, VkDeviceSize offset,
VkDeviceSize size) VkDeviceSize size)
{ {
ASSERT(valid()); ASSERT(valid());
vma::InvalidateAllocation(allocator, mHandle, offset, size); vma::InvalidateAllocation(allocator.getHandle(), mHandle, offset, size);
} }
// RenderPass implementation. // RenderPass implementation.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment