Various vulkan buffer cleanups.

This is just a few miscellaneous buffer changes to make future changes
around switching buffer classes cleaner.

Bug: skia:11226
Change-Id: Icba3b9b5b357956d78f95398cdce9e8733080028
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/364616
Reviewed-by: Jim Van Verth <jvanverth@google.com>
Commit-Queue: Greg Daniel <egdaniel@google.com>
This commit is contained in:
Greg Daniel 2021-02-02 10:37:29 -05:00 committed by Skia Commit-Bot
parent 805eee00d9
commit dd520a3f7d
6 changed files with 42 additions and 59 deletions

View File

@ -19,6 +19,25 @@
#define VALIDATE() do {} while(false)
#endif
using BufferUsage = GrVkMemoryAllocator::BufferUsage;
static BufferUsage get_buffer_usage(GrVkBuffer::Type type, bool dynamic) {
switch (type) {
case GrVkBuffer::kVertex_Type: // fall through
case GrVkBuffer::kIndex_Type: // fall through
case GrVkBuffer::kIndirect_Type: // fall through
case GrVkBuffer::kTexel_Type:
return dynamic ? BufferUsage::kCpuWritesGpuReads : BufferUsage::kGpuOnly;
case GrVkBuffer::kUniform_Type: // fall through
case GrVkBuffer::kCopyRead_Type:
SkASSERT(dynamic);
return BufferUsage::kCpuWritesGpuReads;
case GrVkBuffer::kCopyWrite_Type:
return BufferUsage::kGpuWritesCpuReads;
}
SK_ABORT("Invalid GrVkBuffer::Type");
}
const GrVkBuffer::Resource* GrVkBuffer::Create(GrVkGpu* gpu, const Desc& desc) {
SkASSERT(!gpu->protectedContext() || (gpu->protectedContext() == desc.fDynamic));
VkBuffer buffer;
@ -68,16 +87,15 @@ const GrVkBuffer::Resource* GrVkBuffer::Create(GrVkGpu* gpu, const Desc& desc) {
if (!GrVkMemory::AllocAndBindBufferMemory(gpu,
buffer,
desc.fType,
desc.fDynamic,
get_buffer_usage(desc.fType, desc.fDynamic),
&alloc)) {
return nullptr;
}
const GrVkBuffer::Resource* resource = new GrVkBuffer::Resource(gpu, buffer, alloc, desc.fType);
const GrVkBuffer::Resource* resource = new GrVkBuffer::Resource(gpu, buffer, alloc);
if (!resource) {
VK_CALL(gpu, DestroyBuffer(gpu->device(), buffer, nullptr));
GrVkMemory::FreeBufferMemory(gpu, desc.fType, alloc);
GrVkMemory::FreeBufferMemory(gpu, alloc);
return nullptr;
}
@ -111,7 +129,7 @@ void GrVkBuffer::Resource::freeGPUData() const {
SkASSERT(fBuffer);
SkASSERT(fAlloc.fMemory);
VK_CALL(fGpu, DestroyBuffer(fGpu->device(), fBuffer, nullptr));
GrVkMemory::FreeBufferMemory(fGpu, fType, fAlloc);
GrVkMemory::FreeBufferMemory(fGpu, fAlloc);
}
void GrVkBuffer::vkRelease(GrVkGpu* gpu) {
@ -153,21 +171,12 @@ void GrVkBuffer::internalMap(GrVkGpu* gpu, size_t size, bool* createdNewBuffer)
SkASSERT(!this->vkIsMapped());
if (!fResource->unique()) {
if (fDesc.fDynamic) {
// in use by the command buffer, so we need to create a new one
fResource->recycle();
fResource = this->createResource(gpu, fDesc);
if (createdNewBuffer) {
*createdNewBuffer = true;
}
} else {
SkASSERT(fMapPtr);
this->addMemoryBarrier(gpu,
buffer_type_to_access_flags(fDesc.fType),
VK_ACCESS_TRANSFER_WRITE_BIT,
VK_PIPELINE_STAGE_VERTEX_INPUT_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT,
false);
SkASSERT(fDesc.fDynamic);
// in use by the command buffer, so we need to create a new one
fResource->recycle();
fResource = this->createResource(gpu, fDesc);
if (createdNewBuffer) {
*createdNewBuffer = true;
}
}
@ -179,9 +188,8 @@ void GrVkBuffer::internalMap(GrVkGpu* gpu, size_t size, bool* createdNewBuffer)
fMapPtr = GrVkMemory::MapAlloc(gpu, alloc);
} else {
if (!fMapPtr) {
fMapPtr = new unsigned char[this->size()];
}
SkASSERT(!fMapPtr);
fMapPtr = new unsigned char[this->size()];
}
VALIDATE();

View File

@ -57,8 +57,8 @@ protected:
class Resource : public GrVkRecycledResource {
public:
Resource(GrVkGpu* gpu, VkBuffer buf, const GrVkAlloc& alloc, Type type)
: GrVkRecycledResource(gpu), fBuffer(buf), fAlloc(alloc), fType(type) {}
Resource(GrVkGpu* gpu, VkBuffer buf, const GrVkAlloc& alloc)
: GrVkRecycledResource(gpu), fBuffer(buf), fAlloc(alloc) {}
#ifdef SK_TRACE_MANAGED_RESOURCES
void dumpInfo() const override {
@ -67,7 +67,6 @@ protected:
#endif
VkBuffer fBuffer;
GrVkAlloc fAlloc;
Type fType;
protected:
void freeGPUData() const override;

View File

@ -7,40 +7,18 @@
#include "src/gpu/vk/GrVkMemory.h"
#include "include/gpu/vk/GrVkMemoryAllocator.h"
#include "src/gpu/vk/GrVkGpu.h"
#include "src/gpu/vk/GrVkUtil.h"
using AllocationPropertyFlags = GrVkMemoryAllocator::AllocationPropertyFlags;
using BufferUsage = GrVkMemoryAllocator::BufferUsage;
static BufferUsage get_buffer_usage(GrVkBuffer::Type type, bool dynamic) {
switch (type) {
case GrVkBuffer::kVertex_Type: // fall through
case GrVkBuffer::kIndex_Type: // fall through
case GrVkBuffer::kIndirect_Type: // fall through
case GrVkBuffer::kTexel_Type:
return dynamic ? BufferUsage::kCpuWritesGpuReads : BufferUsage::kGpuOnly;
case GrVkBuffer::kUniform_Type: // fall through
case GrVkBuffer::kCopyRead_Type:
SkASSERT(dynamic);
return BufferUsage::kCpuWritesGpuReads;
case GrVkBuffer::kCopyWrite_Type:
return BufferUsage::kGpuWritesCpuReads;
}
SK_ABORT("Invalid GrVkBuffer::Type");
}
bool GrVkMemory::AllocAndBindBufferMemory(GrVkGpu* gpu,
VkBuffer buffer,
GrVkBuffer::Type type,
bool dynamic,
GrVkMemoryAllocator::BufferUsage usage,
GrVkAlloc* alloc) {
GrVkMemoryAllocator* allocator = gpu->memoryAllocator();
GrVkBackendMemory memory = 0;
GrVkMemoryAllocator::BufferUsage usage = get_buffer_usage(type, dynamic);
AllocationPropertyFlags propFlags;
if (usage == GrVkMemoryAllocator::BufferUsage::kCpuWritesGpuReads) {
// In general it is always fine (and often better) to keep buffers always mapped.
@ -68,15 +46,14 @@ bool GrVkMemory::AllocAndBindBufferMemory(GrVkGpu* gpu,
GR_VK_CALL_RESULT(gpu, err, BindBufferMemory(gpu->device(), buffer, alloc->fMemory,
alloc->fOffset));
if (err) {
FreeBufferMemory(gpu, type, *alloc);
FreeBufferMemory(gpu, *alloc);
return false;
}
return true;
}
void GrVkMemory::FreeBufferMemory(const GrVkGpu* gpu, GrVkBuffer::Type type,
const GrVkAlloc& alloc) {
void GrVkMemory::FreeBufferMemory(const GrVkGpu* gpu, const GrVkAlloc& alloc) {
SkASSERT(alloc.fBackendMemory);
GrVkMemoryAllocator* allocator = gpu->memoryAllocator();
allocator->freeMemory(alloc.fBackendMemory);

View File

@ -8,6 +8,7 @@
#ifndef GrVkMemory_DEFINED
#define GrVkMemory_DEFINED
#include "include/gpu/vk/GrVkMemoryAllocator.h"
#include "include/gpu/vk/GrVkTypes.h"
#include "include/private/SkTArray.h"
#include "src/gpu/vk/GrVkBuffer.h"
@ -21,10 +22,9 @@ namespace GrVkMemory {
*/
bool AllocAndBindBufferMemory(GrVkGpu* gpu,
VkBuffer buffer,
GrVkBuffer::Type type,
bool dynamic,
GrVkMemoryAllocator::BufferUsage,
GrVkAlloc* alloc);
void FreeBufferMemory(const GrVkGpu* gpu, GrVkBuffer::Type type, const GrVkAlloc& alloc);
void FreeBufferMemory(const GrVkGpu* gpu, const GrVkAlloc& alloc);
bool AllocAndBindImageMemory(GrVkGpu* gpu,
VkImage image,

View File

@ -68,8 +68,7 @@ const GrManagedResource* GrVkUniformBuffer::CreateResource(GrVkGpu* gpu, size_t
if (!GrVkMemory::AllocAndBindBufferMemory(gpu,
buffer,
kUniform_Type,
true, // dynamic
GrVkMemoryAllocator::BufferUsage::kCpuWritesGpuReads,
&alloc)) {
VK_CALL(gpu, DestroyBuffer(gpu->device(), buffer, nullptr));
return nullptr;
@ -78,7 +77,7 @@ const GrManagedResource* GrVkUniformBuffer::CreateResource(GrVkGpu* gpu, size_t
const GrVkDescriptorSet* descriptorSet = gpu->resourceProvider().getUniformDescriptorSet();
if (!descriptorSet) {
VK_CALL(gpu, DestroyBuffer(gpu->device(), buffer, nullptr));
GrVkMemory::FreeBufferMemory(gpu, kUniform_Type, alloc);
GrVkMemory::FreeBufferMemory(gpu, alloc);
return nullptr;
}

View File

@ -45,7 +45,7 @@ private:
public:
Resource(GrVkGpu* gpu, VkBuffer buf, const GrVkAlloc& alloc,
const GrVkDescriptorSet* descSet)
: INHERITED(gpu, buf, alloc, kUniform_Type)
: INHERITED(gpu, buf, alloc)
, fDescriptorSet(descSet) {}
void freeGPUData() const override;