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()
mPipelineCache.destroy(mDevice);
mSamplerCache.destroy(this);
vma::DestroyAllocator(mAllocator);
mAllocator.destroy();
if (mGlslangInitialized)
{
......@@ -825,7 +825,7 @@ angle::Result RendererVk::initialize(DisplayVk *displayVk,
}
// 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.
mMemoryProperties.init(mPhysicalDevice);
......
......@@ -106,7 +106,7 @@ class RendererVk : angle::NonCopyable
}
VkDevice getDevice() const { return mDevice; }
const VmaAllocator &getAllocator() const { return mAllocator; }
const vk::Allocator &getAllocator() const { return mAllocator; }
angle::Result selectPresentQueueForSurface(DisplayVk *displayVk,
VkSurfaceKHR surface,
......@@ -376,7 +376,7 @@ class RendererVk : angle::NonCopyable
// track whether we initialized (or released) glslang
bool mGlslangInitialized;
VmaAllocator mAllocator;
vk::Allocator mAllocator;
SamplerCache mSamplerCache;
vk::ActiveHandleCounter mActiveHandleCounts;
};
......
......@@ -2151,10 +2151,14 @@ angle::Result BufferHelper::init(Context *context,
VkMemoryPropertyFlags preferredFlags =
(memoryPropertyFlags & (~VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT));
mAllocation.createBufferAndMemory(
renderer->getAllocator(), createInfo, requiredFlags, preferredFlags,
renderer->getFeatures().persistentlyMappedBuffers.enabled, &mBuffer, &mMemoryPropertyFlags);
const vk::Allocator &allocator = renderer->getAllocator();
uint32_t memoryTypeIndex = 0;
ANGLE_VK_TRY(context,
allocator.createBuffer(*createInfo, requiredFlags, preferredFlags,
renderer->getFeatures().persistentlyMappedBuffers.enabled,
&memoryTypeIndex, &mBuffer, &mAllocation));
allocator.getMemoryTypeProperties(memoryTypeIndex, &mMemoryPropertyFlags);
mCurrentQueueFamilyIndex = renderer->getQueueFamilyIndex();
if (renderer->getFeatures().allocateNonZeroMemory.enabled)
......@@ -2172,8 +2176,8 @@ angle::Result BufferHelper::init(Context *context,
// Can map the memory.
// Pick an arbitrary value to initialize non-zero memory for sanitization.
constexpr int kNonZeroInitValue = 55;
ANGLE_TRY(InitMappableAllocation(renderer->getAllocator(), &mAllocation, mSize,
kNonZeroInitValue, mMemoryPropertyFlags));
ANGLE_TRY(InitMappableAllocation(allocator, &mAllocation, mSize, kNonZeroInitValue,
mMemoryPropertyFlags));
}
}
......
......@@ -409,16 +409,18 @@ angle::Result StagingBuffer::init(Context *context, VkDeviceSize size, StagingUs
createInfo.queueFamilyIndexCount = 0;
createInfo.pQueueFamilyIndices = nullptr;
VkMemoryPropertyFlags memoryPropertyOutFlags;
VkMemoryPropertyFlags preferredFlags = 0;
VkMemoryPropertyFlags requiredFlags =
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
mAllocation.createBufferAndMemory(
context->getRenderer()->getAllocator(), &createInfo, requiredFlags, preferredFlags,
context->getRenderer()->getFeatures().persistentlyMappedBuffers.enabled, &mBuffer,
&memoryPropertyOutFlags);
RendererVk *renderer = context->getRenderer();
const vk::Allocator &allocator = renderer->getAllocator();
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);
return angle::Result::Continue;
}
......@@ -441,7 +443,7 @@ void StagingBuffer::collectGarbage(RendererVk *renderer, Serial serial)
renderer->collectGarbage(std::move(sharedUse), std::move(garbageList));
}
angle::Result InitMappableAllocation(VmaAllocator allocator,
angle::Result InitMappableAllocation(const vk::Allocator &allocator,
Allocation *allocation,
VkDeviceSize size,
int value,
......@@ -653,7 +655,7 @@ void GarbageObject::destroy(RendererVk *renderer)
vkDestroyQueryPool(device, (VkQueryPool)mHandle, nullptr);
break;
case HandleType::Allocation:
vma::FreeMemory(renderer->getAllocator(), (VmaAllocation)mHandle);
vma::FreeMemory(renderer->getAllocator().getHandle(), (VmaAllocation)mHandle);
break;
default:
UNREACHABLE();
......
......@@ -317,8 +317,8 @@ class StagingBuffer final : angle::NonCopyable
size_t mSize;
};
angle::Result InitMappableAllocation(VmaAllocator allocator,
Allocation *allcation,
angle::Result InitMappableAllocation(const vk::Allocator &allocator,
Allocation *allocation,
VkDeviceSize size,
int value,
VkMemoryPropertyFlags memoryPropertyFlags);
......
......@@ -28,6 +28,8 @@ namespace vk
// DescriptorSet
#define ANGLE_HANDLE_TYPES_X(FUNC) \
FUNC(Allocation) \
FUNC(Allocator) \
FUNC(Buffer) \
FUNC(BufferView) \
FUNC(CommandPool) \
......@@ -46,8 +48,7 @@ namespace vk
FUNC(RenderPass) \
FUNC(Sampler) \
FUNC(Semaphore) \
FUNC(ShaderModule) \
FUNC(Allocation)
FUNC(ShaderModule)
#define ANGLE_COMMA_SEP_FUNC(TYPE) TYPE,
......@@ -457,23 +458,39 @@ class DeviceMemory final : public WrappedObject<DeviceMemory, VkDeviceMemory>
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>
{
public:
Allocation() = default;
void destroy(VmaAllocator allocator);
VkResult createBufferAndMemory(VmaAllocator allocator,
const VkBufferCreateInfo *pBufferCreateInfo,
VkMemoryPropertyFlags requiredFlags,
VkMemoryPropertyFlags preferredFlags,
bool persistentlyMappedBuffers,
Buffer *buffer,
VkMemoryPropertyFlags *pMemPropertyOut);
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);
void destroy(const Allocator &allocator);
VkResult map(const Allocator &allocator, uint8_t **mapPointer) const;
void unmap(const Allocator &allocator) const;
void flush(const Allocator &allocator, VkDeviceSize offset, VkDeviceSize size);
void invalidate(const Allocator &allocator, VkDeviceSize offset, VkDeviceSize size);
private:
friend class Allocator;
};
class RenderPass final : public WrappedObject<RenderPass, VkRenderPass>
......@@ -501,6 +518,9 @@ class Buffer final : public WrappedObject<Buffer, VkBuffer>
VkResult init(VkDevice device, const VkBufferCreateInfo &createInfo);
VkResult bindMemory(VkDevice device, const DeviceMemory &deviceMemory);
void getMemoryRequirements(VkDevice device, VkMemoryRequirements *memoryRequirementsOut);
private:
friend class Allocator;
};
class BufferView final : public WrappedObject<BufferView, VkBufferView>
......@@ -1340,61 +1360,83 @@ ANGLE_INLINE void DeviceMemory::unmap(VkDevice device) const
vkUnmapMemory(device, mHandle);
}
// Allocation implementation.
ANGLE_INLINE void Allocation::destroy(VmaAllocator allocator)
// Allocator implementation.
ANGLE_INLINE void Allocator::destroy()
{
if (valid())
{
vma::FreeMemory(allocator, mHandle);
vma::DestroyAllocator(mHandle);
mHandle = VK_NULL_HANDLE;
}
}
ANGLE_INLINE VkResult Allocation::createBufferAndMemory(VmaAllocator allocator,
const VkBufferCreateInfo *pBufferCreateInfo,
VkMemoryPropertyFlags requiredFlags,
VkMemoryPropertyFlags preferredFlags,
bool persistentlyMappedBuffers,
Buffer *buffer,
VkMemoryPropertyFlags *pMemPropertyOut)
ANGLE_INLINE VkResult Allocator::init(VkPhysicalDevice physicalDevice,
VkDevice device,
VkInstance instance)
{
ASSERT(!valid());
VkResult result;
uint32_t memoryTypeIndex;
VkBuffer bufferHandle;
result =
vma::CreateBuffer(allocator, pBufferCreateInfo, requiredFlags, preferredFlags,
persistentlyMappedBuffers, &memoryTypeIndex, &bufferHandle, &mHandle);
vma::GetMemoryTypeProperties(allocator, memoryTypeIndex, pMemPropertyOut);
buffer->setHandle(bufferHandle);
return vma::InitAllocator(physicalDevice, device, instance, &mHandle);
}
ANGLE_INLINE VkResult Allocator::createBuffer(const VkBufferCreateInfo &bufferCreateInfo,
VkMemoryPropertyFlags requiredFlags,
VkMemoryPropertyFlags preferredFlags,
bool persistentlyMappedBuffers,
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());
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());
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());
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 size)
{
ASSERT(valid());
vma::InvalidateAllocation(allocator, mHandle, offset, size);
vma::InvalidateAllocation(allocator.getHandle(), mHandle, offset, size);
}
// 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