don't store context/stagepool in each VulkanBuffer

This reduces the size of VulkanBuffer by 16 bytes, instead we just
pass those objects when needed.
This commit is contained in:
Mathias Agopian
2021-07-13 17:33:22 -07:00
committed by Mathias Agopian
parent c135b4aa19
commit 46ff9a433c
6 changed files with 55 additions and 26 deletions
@@ -214,8 +214,11 @@ void VulkanBlitter::shutdown() noexcept {
delete mDepthResolveProgram;
mDepthResolveProgram = nullptr;
delete mTriangleBuffer;
mTriangleBuffer = nullptr;
if (mTriangleBuffer) {
mTriangleBuffer->terminate(mContext);
delete mTriangleBuffer;
mTriangleBuffer = nullptr;
}
delete mParamsBuffer;
mParamsBuffer = nullptr;
@@ -261,7 +264,8 @@ void VulkanBlitter::lazyInit() noexcept {
mTriangleBuffer = new VulkanBuffer(mContext, mStagePool, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
sizeof(kTriangleVertices));
mTriangleBuffer->loadFromCpu(kTriangleVertices, 0, sizeof(kTriangleVertices));
mTriangleBuffer->loadFromCpu(mContext, mStagePool,
kTriangleVertices, 0, sizeof(kTriangleVertices));
mParamsBuffer = new VulkanUniformBuffer(mContext, mStagePool,
sizeof(BlitterUniforms), backend::BufferUsage::STATIC);
+19 -11
View File
@@ -25,7 +25,7 @@ namespace filament {
namespace backend {
VulkanBuffer::VulkanBuffer(VulkanContext& context, VulkanStagePool& stagePool,
VkBufferUsageFlags usage, uint32_t numBytes) : mContext(context), mStagePool(stagePool) {
VkBufferUsageFlags usage, uint32_t numBytes) {
// Create the VkBuffer.
VkBufferCreateInfo bufferInfo {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
@@ -38,19 +38,27 @@ VulkanBuffer::VulkanBuffer(VulkanContext& context, VulkanStagePool& stagePool,
}
VulkanBuffer::~VulkanBuffer() {
vmaDestroyBuffer(mContext.allocator, mGpuBuffer, mGpuMemory);
assert_invariant(mGpuMemory == VK_NULL_HANDLE);
assert_invariant(mGpuBuffer == VK_NULL_HANDLE);
}
void VulkanBuffer::loadFromCpu(const void* cpuData, uint32_t byteOffset, uint32_t numBytes) const {
assert_invariant(byteOffset == 0);
VulkanStage const* stage = mStagePool.acquireStage(numBytes);
void* mapped;
vmaMapMemory(mContext.allocator, stage->memory, &mapped);
memcpy(mapped, cpuData, numBytes);
vmaUnmapMemory(mContext.allocator, stage->memory);
vmaFlushAllocation(mContext.allocator, stage->memory, byteOffset, numBytes);
void VulkanBuffer::terminate(VulkanContext& context) {
vmaDestroyBuffer(context.allocator, mGpuBuffer, mGpuMemory);
mGpuMemory = VK_NULL_HANDLE;
mGpuBuffer = VK_NULL_HANDLE;
}
const VkCommandBuffer cmdbuffer = mContext.commands->get().cmdbuffer;
void VulkanBuffer::loadFromCpu(VulkanContext& context, VulkanStagePool& stagePool,
const void* cpuData, uint32_t byteOffset, uint32_t numBytes) const {
assert_invariant(byteOffset == 0);
VulkanStage const* stage = stagePool.acquireStage(numBytes);
void* mapped;
vmaMapMemory(context.allocator, stage->memory, &mapped);
memcpy(mapped, cpuData, numBytes);
vmaUnmapMemory(context.allocator, stage->memory);
vmaFlushAllocation(context.allocator, stage->memory, byteOffset, numBytes);
const VkCommandBuffer cmdbuffer = context.commands->get().cmdbuffer;
VkBufferCopy region { .size = numBytes };
vkCmdCopyBuffer(cmdbuffer, stage->buffer, mGpuBuffer, 1, &region);
+3 -3
View File
@@ -29,11 +29,11 @@ public:
VulkanBuffer(VulkanContext& context, VulkanStagePool& stagePool, VkBufferUsageFlags usage,
uint32_t numBytes);
~VulkanBuffer();
void loadFromCpu(const void* cpuData, uint32_t byteOffset, uint32_t numBytes) const;
void terminate(VulkanContext& context);
void loadFromCpu(VulkanContext& context, VulkanStagePool& stagePool,
const void* cpuData, uint32_t byteOffset, uint32_t numBytes) const;
VkBuffer getGpuBuffer() const { return mGpuBuffer; }
private:
VulkanContext& mContext;
VulkanStagePool& mStagePool;
VmaAllocation mGpuMemory = VK_NULL_HANDLE;
VkBuffer mGpuBuffer = VK_NULL_HANDLE;
};
+6 -6
View File
@@ -454,8 +454,8 @@ void VulkanDriver::createIndexBufferR(Handle<HwIndexBuffer> ibh,
auto elementSize = (uint8_t) getElementTypeSize(elementType);
auto indexBuffer = construct_handle<VulkanIndexBuffer>(mHandleMap, ibh, mContext, mStagePool,
elementSize, indexCount);
mDisposer.createDisposable(indexBuffer, [this, ibh] () {
destruct_handle<VulkanIndexBuffer>(mHandleMap, ibh);
mDisposer.createDisposable(indexBuffer, [this, ibh]() {
destruct_handle<VulkanIndexBuffer>(mContext, mHandleMap, ibh);
});
}
@@ -470,8 +470,8 @@ void VulkanDriver::createBufferObjectR(Handle<HwBufferObject> boh,
uint32_t byteCount, BufferObjectBinding bindingType, BufferUsage usage) {
auto bufferObject = construct_handle<VulkanBufferObject>(mHandleMap, boh,
mContext, mStagePool, byteCount, bindingType, usage);
mDisposer.createDisposable(bufferObject, [this, boh] () {
destruct_handle<VulkanBufferObject>(mHandleMap, boh);
mDisposer.createDisposable(bufferObject, [this, boh]() {
destruct_handle<VulkanBufferObject>(mContext, mHandleMap, boh);
});
}
@@ -859,7 +859,7 @@ void VulkanDriver::setVertexBufferObject(Handle<HwVertexBuffer> vbh, uint32_t in
void VulkanDriver::updateIndexBuffer(Handle<HwIndexBuffer> ibh, BufferDescriptor&& p,
uint32_t byteOffset) {
auto ib = handle_cast<VulkanIndexBuffer>(mHandleMap, ibh);
ib->buffer.loadFromCpu(p.buffer, byteOffset, p.size);
ib->buffer.loadFromCpu(mContext, mStagePool, p.buffer, byteOffset, p.size);
mDisposer.acquire(ib);
scheduleDestroy(std::move(p));
}
@@ -867,7 +867,7 @@ void VulkanDriver::updateIndexBuffer(Handle<HwIndexBuffer> ibh, BufferDescriptor
void VulkanDriver::updateBufferObject(Handle<HwBufferObject> boh, BufferDescriptor&& bd,
uint32_t byteOffset) {
auto bo = handle_cast<VulkanBufferObject>(mHandleMap, boh);
bo->buffer.loadFromCpu(bd.buffer, byteOffset, bd.size);
bo->buffer.loadFromCpu(mContext, mStagePool, bd.buffer, byteOffset, bd.size);
mDisposer.acquire(bo);
scheduleDestroy(std::move(bd));
}
@@ -144,6 +144,21 @@ private:
handleMap.erase(handle.getId());
}
// This version of destruct_handle take a VulkanContext and calls a terminate(VulkanContext&)
// on the handle before calling the dtor
template<typename Dp, typename B>
void destruct_handle(VulkanContext& context, HandleMap& handleMap, const Handle<B>& handle) noexcept {
std::lock_guard<std::mutex> lock(mHandleMapMutex);
// Call the destructor, remove the blob, don't bother reclaiming the integer id.
auto iter = handleMap.find(handle.getId());
assert_invariant(iter != handleMap.end());
Blob& blob = iter->second;
assert_invariant(blob.size() == sizeof(Dp));
reinterpret_cast<Dp*>(blob.data())->terminate(context);
reinterpret_cast<Dp*>(blob.data())->~Dp();
handleMap.erase(handle.getId());
}
void refreshSwapChain();
void collectGarbage();
+5 -3
View File
@@ -90,15 +90,17 @@ struct VulkanIndexBuffer : public HwIndexBuffer {
buffer(context, stagePool,
VK_BUFFER_USAGE_INDEX_BUFFER_BIT, elementSize * indexCount),
indexType(elementSize == 2 ? VK_INDEX_TYPE_UINT16 : VK_INDEX_TYPE_UINT32) {}
const VulkanBuffer buffer;
void terminate(VulkanContext& context) { buffer.terminate(context); }
VulkanBuffer buffer;
const VkIndexType indexType;
};
struct VulkanBufferObject : public HwBufferObject {
VulkanBufferObject(VulkanContext& context, VulkanStagePool& stagePool,
uint32_t byteCount, BufferObjectBinding bindingType, BufferUsage usage);
const VulkanBuffer buffer;
BufferObjectBinding bindingType;
void terminate(VulkanContext& context) { buffer.terminate(context); }
VulkanBuffer buffer;
const BufferObjectBinding bindingType;
};
struct VulkanUniformBuffer : public HwUniformBuffer {