Add buffer and texture managers for handling object lifetimes

This commit is contained in:
Magnus Norddahl 2022-06-10 01:22:19 +02:00 committed by Christoph Oelckers
parent 8004532cba
commit 298c023b1d
17 changed files with 162 additions and 55 deletions

View file

@ -798,7 +798,8 @@ set (VULKAN_SOURCES
common/rendering/vulkan/system/vk_builders.cpp
common/rendering/vulkan/system/vk_framebuffer.cpp
common/rendering/vulkan/system/vk_commandbuffer.cpp
common/rendering/vulkan/system/vk_buffers.cpp
common/rendering/vulkan/system/vk_hwbuffer.cpp
common/rendering/vulkan/system/vk_buffer.cpp
common/rendering/vulkan/renderer/vk_renderstate.cpp
common/rendering/vulkan/renderer/vk_renderpass.cpp
common/rendering/vulkan/renderer/vk_streambuffer.cpp
@ -813,6 +814,7 @@ set (VULKAN_SOURCES
common/rendering/vulkan/textures/vk_pptexture.cpp
common/rendering/vulkan/textures/vk_imagetransition.cpp
common/rendering/vulkan/textures/vk_renderbuffers.cpp
common/rendering/vulkan/textures/vk_texture.cpp
common/rendering/vulkan/thirdparty/volk/volk.c
common/rendering/vulkan/thirdparty/vk_mem_alloc/vk_mem_alloc.cpp
)

View file

@ -29,7 +29,7 @@
#include "vulkan/textures/vk_hwtexture.h"
#include "vulkan/system/vk_builders.h"
#include "vulkan/system/vk_framebuffer.h"
#include "vulkan/system/vk_buffers.h"
#include "vulkan/system/vk_hwbuffer.h"
#include "vulkan/system/vk_commandbuffer.h"
#include "flatvertices.h"
#include "hw_viewpointuniforms.h"

View file

@ -24,7 +24,7 @@
#include "vulkan/shaders/vk_shader.h"
#include "vulkan/system/vk_builders.h"
#include "vulkan/system/vk_framebuffer.h"
#include "vulkan/system/vk_buffers.h"
#include "vulkan/system/vk_hwbuffer.h"
#include "vulkan/system/vk_swapchain.h"
#include "vulkan/system/vk_commandbuffer.h"
#include "vulkan/renderer/vk_renderstate.h"

View file

@ -118,7 +118,7 @@ void VkPPRenderState::RenderScreenQuad(VkPPRenderPassSetup *passSetup, VulkanDes
beginInfo.setFramebuffer(framebuffer);
beginInfo.addClearColor(0.0f, 0.0f, 0.0f, 1.0f);
VkBuffer vertexBuffers[] = { static_cast<VKVertexBuffer*>(screen->mVertexData->GetBufferObjects().first)->mBuffer->buffer };
VkBuffer vertexBuffers[] = { static_cast<VkHardwareVertexBuffer*>(screen->mVertexData->GetBufferObjects().first)->mBuffer->buffer };
VkDeviceSize offsets[] = { 0 };
cmdbuffer->beginRenderPass(beginInfo);

View file

@ -30,7 +30,7 @@
#include "vulkan/shaders/vk_ppshader.h"
#include "vulkan/system/vk_builders.h"
#include "vulkan/system/vk_framebuffer.h"
#include "vulkan/system/vk_buffers.h"
#include "vulkan/system/vk_hwbuffer.h"
#include "flatvertices.h"
#include "hw_viewpointuniforms.h"
#include "v_2ddrawer.h"

View file

@ -218,7 +218,7 @@ void VkRenderState::ApplyRenderPass(int dt)
// Find a pipeline that matches our state
VkPipelineKey pipelineKey;
pipelineKey.DrawType = dt;
pipelineKey.VertexFormat = static_cast<VKVertexBuffer*>(mVertexBuffer)->VertexFormat;
pipelineKey.VertexFormat = static_cast<VkHardwareVertexBuffer*>(mVertexBuffer)->VertexFormat;
pipelineKey.RenderStyle = mRenderStyle;
pipelineKey.DepthTest = mDepthTest;
pipelineKey.DepthWrite = mDepthTest && mDepthWrite;
@ -342,7 +342,7 @@ void VkRenderState::ApplyStreamData()
{
auto passManager = fb->GetRenderPassManager();
mStreamData.useVertexData = passManager->GetVertexFormat(static_cast<VKVertexBuffer*>(mVertexBuffer)->VertexFormat)->UseVertexData;
mStreamData.useVertexData = passManager->GetVertexFormat(static_cast<VkHardwareVertexBuffer*>(mVertexBuffer)->VertexFormat)->UseVertexData;
if (mMaterial.mMaterial && mMaterial.mMaterial->Source())
mStreamData.timer = static_cast<float>((double)(screen->FrameTime - firstFrame) * (double)mMaterial.mMaterial->Source()->GetShaderSpeed() / 1000.);
@ -414,7 +414,7 @@ void VkRenderState::ApplyVertexBuffers()
{
if ((mVertexBuffer != mLastVertexBuffer || mVertexOffsets[0] != mLastVertexOffsets[0] || mVertexOffsets[1] != mLastVertexOffsets[1]) && mVertexBuffer)
{
auto vkbuf = static_cast<VKVertexBuffer*>(mVertexBuffer);
auto vkbuf = static_cast<VkHardwareVertexBuffer*>(mVertexBuffer);
const VkVertexFormat *format = fb->GetRenderPassManager()->GetVertexFormat(vkbuf->VertexFormat);
VkBuffer vertexBuffers[2] = { vkbuf->mBuffer->buffer, vkbuf->mBuffer->buffer };
VkDeviceSize offsets[] = { mVertexOffsets[0] * format->Stride, mVertexOffsets[1] * format->Stride };
@ -426,7 +426,7 @@ void VkRenderState::ApplyVertexBuffers()
if (mIndexBuffer != mLastIndexBuffer && mIndexBuffer)
{
mCommandBuffer->bindIndexBuffer(static_cast<VKIndexBuffer*>(mIndexBuffer)->mBuffer->buffer, 0, VK_INDEX_TYPE_UINT32);
mCommandBuffer->bindIndexBuffer(static_cast<VkHardwareIndexBuffer*>(mIndexBuffer)->mBuffer->buffer, 0, VK_INDEX_TYPE_UINT32);
mLastIndexBuffer = mIndexBuffer;
}
}

View file

@ -1,7 +1,7 @@
#pragma once
#include "vulkan/system/vk_buffers.h"
#include "vulkan/system/vk_hwbuffer.h"
#include "vulkan/shaders/vk_shader.h"
#include "vulkan/renderer/vk_renderpass.h"
#include "vulkan/renderer/vk_streambuffer.h"

View file

@ -29,7 +29,7 @@ VkStreamBuffer::VkStreamBuffer(VulkanFrameBuffer* fb, size_t structSize, size_t
{
mBlockSize = static_cast<uint32_t>((structSize + screen->uniformblockalignment - 1) / screen->uniformblockalignment * screen->uniformblockalignment);
UniformBuffer = (VKDataBuffer*)fb->CreateDataBuffer(-1, false, false);
UniformBuffer = (VkHardwareDataBuffer*)fb->CreateDataBuffer(-1, false, false);
UniformBuffer->SetData(mBlockSize * count, nullptr, BufferUsageType::Persistent);
}

View file

@ -1,7 +1,7 @@
#pragma once
#include "vulkan/system/vk_buffers.h"
#include "vulkan/system/vk_hwbuffer.h"
#include "vulkan/shaders/vk_shader.h"
class VkStreamBuffer;
@ -50,7 +50,7 @@ public:
uint32_t NextStreamDataBlock();
void Reset() { mStreamDataOffset = 0; }
VKDataBuffer* UniformBuffer = nullptr;
VkHardwareDataBuffer* UniformBuffer = nullptr;
private:
uint32_t mBlockSize = 0;

View file

@ -0,0 +1,31 @@
/*
** Vulkan backend
** Copyright (c) 2016-2020 Magnus Norddahl
**
** This software is provided 'as-is', without any express or implied
** warranty. In no event will the authors be held liable for any damages
** arising from the use of this software.
**
** Permission is granted to anyone to use this software for any purpose,
** including commercial applications, and to alter it and redistribute it
** freely, subject to the following restrictions:
**
** 1. The origin of this software must not be misrepresented; you must not
** claim that you wrote the original software. If you use this software
** in a product, an acknowledgment in the product documentation would be
** appreciated but is not required.
** 2. Altered source versions must be plainly marked as such, and must not be
** misrepresented as being the original software.
** 3. This notice may not be removed or altered from any source distribution.
**
*/
#include "vk_buffer.h"
VkBufferManager::VkBufferManager(VulkanFrameBuffer* fb) : fb(fb)
{
}
VkBufferManager::~VkBufferManager()
{
}

View file

@ -0,0 +1,16 @@
#pragma once
#include "vulkan/system/vk_objects.h"
class VulkanFrameBuffer;
class VkBufferManager
{
public:
VkBufferManager(VulkanFrameBuffer* fb);
~VkBufferManager();
private:
VulkanFrameBuffer* fb = nullptr;
};

View file

@ -43,7 +43,7 @@
#include "hw_lightbuffer.h"
#include "vk_framebuffer.h"
#include "vk_buffers.h"
#include "vk_hwbuffer.h"
#include "vulkan/renderer/vk_renderstate.h"
#include "vulkan/renderer/vk_renderpass.h"
#include "vulkan/renderer/vk_descriptorset.h"
@ -54,9 +54,11 @@
#include "vulkan/textures/vk_renderbuffers.h"
#include "vulkan/textures/vk_samplers.h"
#include "vulkan/textures/vk_hwtexture.h"
#include "vulkan/textures/vk_texture.h"
#include "vulkan/system/vk_builders.h"
#include "vulkan/system/vk_swapchain.h"
#include "vulkan/system/vk_commandbuffer.h"
#include "vulkan/system/vk_buffer.h"
#include "engineerrors.h"
#include "c_dispatch.h"
@ -85,7 +87,7 @@ VulkanFrameBuffer::~VulkanFrameBuffer()
// All descriptors must be destroyed before the descriptor pool in renderpass manager is destroyed
VkHardwareTexture::ResetAll();
VKBuffer::ResetAll();
VkHardwareBuffer::ResetAll();
PPResource::ResetAll();
delete MatrixBuffer;
@ -124,6 +126,10 @@ void VulkanFrameBuffer::InitializeState()
mCommands.reset(new VkCommandBufferManager(this));
mSamplerManager.reset(new VkSamplerManager(this));
mTextureManager.reset(new VkTextureManager(this));
mBufferManager.reset(new VkBufferManager(this));
mScreenBuffers.reset(new VkRenderBuffers(this));
mSaveBuffers.reset(new VkRenderBuffers(this));
mActiveRenderBuffers = mScreenBuffers.get();
@ -145,7 +151,6 @@ void VulkanFrameBuffer::InitializeState()
StreamBuffer = new VkStreamBuffer(this, sizeof(StreamUBO), 300);
mShaderManager.reset(new VkShaderManager(device));
mSamplerManager.reset(new VkSamplerManager(this));
mDescriptorSetManager->Init();
#ifdef __APPLE__
mRenderState.reset(new VkRenderStateMolten(this));
@ -263,17 +268,17 @@ FMaterial* VulkanFrameBuffer::CreateMaterial(FGameTexture* tex, int scaleflags)
IVertexBuffer *VulkanFrameBuffer::CreateVertexBuffer()
{
return new VKVertexBuffer();
return new VkHardwareVertexBuffer();
}
IIndexBuffer *VulkanFrameBuffer::CreateIndexBuffer()
{
return new VKIndexBuffer();
return new VkHardwareIndexBuffer();
}
IDataBuffer *VulkanFrameBuffer::CreateDataBuffer(int bindingpoint, bool ssbo, bool needsresize)
{
auto buffer = new VKDataBuffer(bindingpoint, ssbo, needsresize);
auto buffer = new VkHardwareDataBuffer(bindingpoint, ssbo, needsresize);
switch (bindingpoint)
{

View file

@ -6,6 +6,8 @@
struct FRenderViewpoint;
class VkSamplerManager;
class VkBufferManager;
class VkTextureManager;
class VkShaderManager;
class VkCommandBufferManager;
class VkDescriptorSetManager;
@ -13,7 +15,7 @@ class VkRenderPassManager;
class VkRaytrace;
class VkRenderState;
class VkStreamBuffer;
class VKDataBuffer;
class VkHardwareDataBuffer;
class VkHardwareTexture;
class VkRenderBuffers;
class VkPostprocess;
@ -30,6 +32,8 @@ public:
VkCommandBufferManager* GetCommands() { return mCommands.get(); }
VkShaderManager *GetShaderManager() { return mShaderManager.get(); }
VkSamplerManager *GetSamplerManager() { return mSamplerManager.get(); }
VkBufferManager* GetBufferManager() { return mBufferManager.get(); }
VkTextureManager* GetTextureManager() { return mTextureManager.get(); }
VkDescriptorSetManager* GetDescriptorSetManager() { return mDescriptorSetManager.get(); }
VkRenderPassManager *GetRenderPassManager() { return mRenderPassManager.get(); }
VkRaytrace* GetRaytrace() { return mRaytrace.get(); }
@ -40,14 +44,14 @@ public:
unsigned int GetLightBufferBlockSize() const;
VKDataBuffer *ViewpointUBO = nullptr;
VKDataBuffer *LightBufferSSO = nullptr;
VkHardwareDataBuffer *ViewpointUBO = nullptr;
VkHardwareDataBuffer *LightBufferSSO = nullptr;
VkStreamBuffer *MatrixBuffer = nullptr;
VkStreamBuffer *StreamBuffer = nullptr;
VKDataBuffer *LightNodes = nullptr;
VKDataBuffer *LightLines = nullptr;
VKDataBuffer *LightList = nullptr;
VkHardwareDataBuffer *LightNodes = nullptr;
VkHardwareDataBuffer *LightLines = nullptr;
VkHardwareDataBuffer *LightList = nullptr;
std::unique_ptr<IIndexBuffer> FanToTrisIndexBuffer;
@ -102,8 +106,10 @@ private:
void CopyScreenToBuffer(int w, int h, uint8_t *data) override;
std::unique_ptr<VkCommandBufferManager> mCommands;
std::unique_ptr<VkShaderManager> mShaderManager;
std::unique_ptr<VkBufferManager> mBufferManager;
std::unique_ptr<VkSamplerManager> mSamplerManager;
std::unique_ptr<VkTextureManager> mTextureManager;
std::unique_ptr<VkShaderManager> mShaderManager;
std::unique_ptr<VkRenderBuffers> mScreenBuffers;
std::unique_ptr<VkRenderBuffers> mSaveBuffers;
std::unique_ptr<VkPostprocess> mPostprocess;

View file

@ -20,7 +20,7 @@
**
*/
#include "vk_buffers.h"
#include "vk_hwbuffer.h"
#include "vk_builders.h"
#include "vk_framebuffer.h"
#include "vk_commandbuffer.h"
@ -28,16 +28,16 @@
#include "vulkan/renderer/vk_descriptorset.h"
#include "engineerrors.h"
VKBuffer *VKBuffer::First = nullptr;
VkHardwareBuffer *VkHardwareBuffer::First = nullptr;
VKBuffer::VKBuffer()
VkHardwareBuffer::VkHardwareBuffer()
{
Next = First;
First = this;
if (Next) Next->Prev = this;
}
VKBuffer::~VKBuffer()
VkHardwareBuffer::~VkHardwareBuffer()
{
if (Next) Next->Prev = Prev;
if (Prev) Prev->Next = Next;
@ -56,13 +56,13 @@ VKBuffer::~VKBuffer()
}
}
void VKBuffer::ResetAll()
void VkHardwareBuffer::ResetAll()
{
for (VKBuffer *cur = VKBuffer::First; cur; cur = cur->Next)
for (VkHardwareBuffer *cur = VkHardwareBuffer::First; cur; cur = cur->Next)
cur->Reset();
}
void VKBuffer::Reset()
void VkHardwareBuffer::Reset()
{
if (mBuffer && map)
mBuffer->Unmap();
@ -70,7 +70,7 @@ void VKBuffer::Reset()
mStaging.reset();
}
void VKBuffer::SetData(size_t size, const void *data, BufferUsageType usage)
void VkHardwareBuffer::SetData(size_t size, const void *data, BufferUsageType usage)
{
auto fb = GetVulkanFrameBuffer();
@ -152,7 +152,7 @@ void VKBuffer::SetData(size_t size, const void *data, BufferUsageType usage)
buffersize = size;
}
void VKBuffer::SetSubData(size_t offset, size_t size, const void *data)
void VkHardwareBuffer::SetSubData(size_t offset, size_t size, const void *data)
{
size = max(size, (size_t)16); // For supporting zero byte buffers
@ -173,7 +173,7 @@ void VKBuffer::SetSubData(size_t offset, size_t size, const void *data)
}
}
void VKBuffer::Resize(size_t newsize)
void VkHardwareBuffer::Resize(size_t newsize)
{
newsize = max(newsize, (size_t)16); // For supporting zero byte buffers
@ -204,13 +204,13 @@ void VKBuffer::Resize(size_t newsize)
map = mBuffer->Map(0, newsize);
}
void VKBuffer::Map()
void VkHardwareBuffer::Map()
{
if (!mPersistent)
map = mBuffer->Map(0, mBuffer->size);
}
void VKBuffer::Unmap()
void VkHardwareBuffer::Unmap()
{
if (!mPersistent)
{
@ -219,7 +219,7 @@ void VKBuffer::Unmap()
}
}
void *VKBuffer::Lock(unsigned int size)
void *VkHardwareBuffer::Lock(unsigned int size)
{
size = max(size, (unsigned int)16); // For supporting zero byte buffers
@ -237,7 +237,7 @@ void *VKBuffer::Lock(unsigned int size)
return map;
}
void VKBuffer::Unlock()
void VkHardwareBuffer::Unlock()
{
if (!mBuffer)
{
@ -254,7 +254,7 @@ void VKBuffer::Unlock()
/////////////////////////////////////////////////////////////////////////////
void VKVertexBuffer::SetFormat(int numBindingPoints, int numAttributes, size_t stride, const FVertexBufferAttribute *attrs)
void VkHardwareVertexBuffer::SetFormat(int numBindingPoints, int numAttributes, size_t stride, const FVertexBufferAttribute *attrs)
{
VertexFormat = GetVulkanFrameBuffer()->GetRenderPassManager()->GetVertexFormat(numBindingPoints, numAttributes, stride, attrs);
}
@ -262,7 +262,7 @@ void VKVertexBuffer::SetFormat(int numBindingPoints, int numAttributes, size_t s
/////////////////////////////////////////////////////////////////////////////
void VKDataBuffer::BindRange(FRenderState* state, size_t start, size_t length)
void VkHardwareDataBuffer::BindRange(FRenderState* state, size_t start, size_t length)
{
static_cast<VkRenderState*>(state)->Bind(bindingpoint, (uint32_t)start);
}

View file

@ -5,16 +5,16 @@
#include "tarray.h"
#ifdef _MSC_VER
// silence bogus warning C4250: 'VKVertexBuffer': inherits 'VKBuffer::VKBuffer::SetData' via dominance
// silence bogus warning C4250: 'VkHardwareVertexBuffer': inherits 'VkHardwareBuffer::VkHardwareBuffer::SetData' via dominance
// According to internet infos, the warning is erroneously emitted in this case.
#pragma warning(disable:4250)
#endif
class VKBuffer : virtual public IBuffer
class VkHardwareBuffer : virtual public IBuffer
{
public:
VKBuffer();
~VKBuffer();
VkHardwareBuffer();
~VkHardwareBuffer();
static void ResetAll();
void Reset();
@ -36,30 +36,30 @@ public:
TArray<uint8_t> mStaticUpload;
private:
static VKBuffer *First;
VKBuffer *Prev = nullptr;
VKBuffer *Next = nullptr;
static VkHardwareBuffer *First;
VkHardwareBuffer *Prev = nullptr;
VkHardwareBuffer *Next = nullptr;
};
class VKVertexBuffer : public IVertexBuffer, public VKBuffer
class VkHardwareVertexBuffer : public IVertexBuffer, public VkHardwareBuffer
{
public:
VKVertexBuffer() { mBufferType = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; }
VkHardwareVertexBuffer() { mBufferType = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; }
void SetFormat(int numBindingPoints, int numAttributes, size_t stride, const FVertexBufferAttribute *attrs) override;
int VertexFormat = -1;
};
class VKIndexBuffer : public IIndexBuffer, public VKBuffer
class VkHardwareIndexBuffer : public IIndexBuffer, public VkHardwareBuffer
{
public:
VKIndexBuffer() { mBufferType = VK_BUFFER_USAGE_INDEX_BUFFER_BIT; }
VkHardwareIndexBuffer() { mBufferType = VK_BUFFER_USAGE_INDEX_BUFFER_BIT; }
};
class VKDataBuffer : public IDataBuffer, public VKBuffer
class VkHardwareDataBuffer : public IDataBuffer, public VkHardwareBuffer
{
public:
VKDataBuffer(int bindingpoint, bool ssbo, bool needresize) : bindingpoint(bindingpoint)
VkHardwareDataBuffer(int bindingpoint, bool ssbo, bool needresize) : bindingpoint(bindingpoint)
{
mBufferType = ssbo ? VK_BUFFER_USAGE_STORAGE_BUFFER_BIT : VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
if (needresize)

View file

@ -0,0 +1,31 @@
/*
** Vulkan backend
** Copyright (c) 2016-2020 Magnus Norddahl
**
** This software is provided 'as-is', without any express or implied
** warranty. In no event will the authors be held liable for any damages
** arising from the use of this software.
**
** Permission is granted to anyone to use this software for any purpose,
** including commercial applications, and to alter it and redistribute it
** freely, subject to the following restrictions:
**
** 1. The origin of this software must not be misrepresented; you must not
** claim that you wrote the original software. If you use this software
** in a product, an acknowledgment in the product documentation would be
** appreciated but is not required.
** 2. Altered source versions must be plainly marked as such, and must not be
** misrepresented as being the original software.
** 3. This notice may not be removed or altered from any source distribution.
**
*/
#include "vk_texture.h"
VkTextureManager::VkTextureManager(VulkanFrameBuffer* fb) : fb(fb)
{
}
VkTextureManager::~VkTextureManager()
{
}

View file

@ -0,0 +1,16 @@
#pragma once
#include "vulkan/system/vk_objects.h"
class VulkanFrameBuffer;
class VkTextureManager
{
public:
VkTextureManager(VulkanFrameBuffer* fb);
~VkTextureManager();
private:
VulkanFrameBuffer* fb = nullptr;
};