Skip to content

Commit

Permalink
Let VkTextureManager manage the VkHardwareTexture resources
Browse files Browse the repository at this point in the history
Let VkDescriptorSetManager manage the VkMaterial resources
Add the resources to the delete list instead of freeing them immediately as the backend cannot rely on exactly when the hardware renderer decides to destroy them
  • Loading branch information
dpjudas authored and coelckers committed Jun 22, 2022
1 parent 298c023 commit b3316fb
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 115 deletions.
25 changes: 18 additions & 7 deletions src/common/rendering/vulkan/renderer/vk_descriptorset.cpp
Expand Up @@ -41,6 +41,8 @@ VkDescriptorSetManager::VkDescriptorSetManager(VulkanFrameBuffer* fb) : fb(fb)

VkDescriptorSetManager::~VkDescriptorSetManager()
{
while (!Materials.empty())
RemoveMaterial(Materials.back());
}

void VkDescriptorSetManager::Init()
Expand Down Expand Up @@ -115,8 +117,11 @@ void VkDescriptorSetManager::UpdateFixedSet()
update.updateSets(fb->device);
}

void VkDescriptorSetManager::TextureSetPoolReset()
void VkDescriptorSetManager::ResetHWTextureSets()
{
for (auto mat : Materials)
mat->DeleteDescriptors();

auto& deleteList = fb->GetCommands()->FrameDeleteList;

for (auto& desc : TextureDescriptorPools)
Expand All @@ -131,12 +136,6 @@ void VkDescriptorSetManager::TextureSetPoolReset()
TextureDescriptorsLeft = 0;
}

void VkDescriptorSetManager::FilterModeChanged()
{
// Destroy the texture descriptors as they used the old samplers
VkMaterial::ResetAllDescriptors();
}

void VkDescriptorSetManager::CreateNullTexture()
{
ImageBuilder imgbuilder;
Expand Down Expand Up @@ -210,3 +209,15 @@ VulkanDescriptorSetLayout* VkDescriptorSetManager::GetTextureSetLayout(int numLa
layout->SetDebugName("VkDescriptorSetManager.TextureSetLayout");
return layout.get();
}

void VkDescriptorSetManager::AddMaterial(VkMaterial* texture)
{
texture->it = Materials.insert(Materials.end(), texture);
}

void VkDescriptorSetManager::RemoveMaterial(VkMaterial* texture)
{
texture->DeleteDescriptors();
texture->fb = nullptr;
Materials.erase(texture->it);
}
10 changes: 8 additions & 2 deletions src/common/rendering/vulkan/renderer/vk_descriptorset.h
Expand Up @@ -2,8 +2,10 @@
#pragma once

#include "vulkan/system/vk_objects.h"
#include <list>

class VulkanFrameBuffer;
class VkMaterial;

class VkDescriptorSetManager
{
Expand All @@ -14,8 +16,7 @@ class VkDescriptorSetManager
void Init();
void UpdateFixedSet();
void UpdateDynamicSet();
void TextureSetPoolReset();
void FilterModeChanged();
void ResetHWTextureSets();

VulkanDescriptorSetLayout* GetDynamicSetLayout() { return DynamicSetLayout.get(); }
VulkanDescriptorSetLayout* GetFixedSetLayout() { return FixedSetLayout.get(); }
Expand All @@ -29,6 +30,9 @@ class VkDescriptorSetManager

VulkanImageView* GetNullTextureView() { return NullTextureView.get(); }

void AddMaterial(VkMaterial* texture);
void RemoveMaterial(VkMaterial* texture);

private:
void CreateDynamicSet();
void CreateFixedSet();
Expand All @@ -53,4 +57,6 @@ class VkDescriptorSetManager

std::unique_ptr<VulkanImage> NullTexture;
std::unique_ptr<VulkanImageView> NullTextureView;

std::list<VkMaterial*> Materials;
};
18 changes: 6 additions & 12 deletions src/common/rendering/vulkan/system/vk_framebuffer.cpp
Expand Up @@ -86,7 +86,6 @@ VulkanFrameBuffer::~VulkanFrameBuffer()
vkDeviceWaitIdle(device->device); // make sure the GPU is no longer using any objects before RAII tears them down

// All descriptors must be destroyed before the descriptor pool in renderpass manager is destroyed
VkHardwareTexture::ResetAll();
VkHardwareBuffer::ResetAll();
PPResource::ResetAll();

Expand Down Expand Up @@ -258,12 +257,12 @@ void VulkanFrameBuffer::PrecacheMaterial(FMaterial *mat, int translation)

IHardwareTexture *VulkanFrameBuffer::CreateHardwareTexture(int numchannels)
{
return new VkHardwareTexture(numchannels);
return new VkHardwareTexture(this, numchannels);
}

FMaterial* VulkanFrameBuffer::CreateMaterial(FGameTexture* tex, int scaleflags)
{
return new VkMaterial(tex, scaleflags);
return new VkMaterial(this, tex, scaleflags);
}

IVertexBuffer *VulkanFrameBuffer::CreateVertexBuffer()
Expand Down Expand Up @@ -298,15 +297,15 @@ void VulkanFrameBuffer::SetTextureFilterMode()
{
if (mSamplerManager)
{
mDescriptorSetManager->FilterModeChanged();
mSamplerManager->FilterModeChanged();
mDescriptorSetManager->ResetHWTextureSets();
mSamplerManager->ResetHWSamplers();
}
}

void VulkanFrameBuffer::StartPrecaching()
{
// Destroy the texture descriptors to avoid problems with potentially stale textures.
VkMaterial::ResetAllDescriptors();
mDescriptorSetManager->ResetHWTextureSets();
}

void VulkanFrameBuffer::BlurScene(float amount)
Expand Down Expand Up @@ -446,12 +445,7 @@ void VulkanFrameBuffer::InitLightmap(int LMTextureSize, int LMTextureCount, TArr
int pixelsize = 8;
auto& lightmap = mActiveRenderBuffers->Lightmap;

if (lightmap.Image)
{
GetCommands()->FrameDeleteList.Images.push_back(std::move(lightmap.Image));
GetCommands()->FrameDeleteList.ImageViews.push_back(std::move(lightmap.View));
lightmap.reset();
}
lightmap.Reset(this);

ImageBuilder builder;
builder.setSize(w, h, 1, count);
Expand Down
78 changes: 16 additions & 62 deletions src/common/rendering/vulkan/textures/vk_hwtexture.cpp
Expand Up @@ -31,55 +31,36 @@
#include "vulkan/system/vk_commandbuffer.h"
#include "vulkan/textures/vk_samplers.h"
#include "vulkan/textures/vk_renderbuffers.h"
#include "vulkan/textures/vk_texture.h"
#include "vulkan/renderer/vk_descriptorset.h"
#include "vulkan/renderer/vk_postprocess.h"
#include "vulkan/shaders/vk_shader.h"
#include "vk_hwtexture.h"

VkHardwareTexture *VkHardwareTexture::First = nullptr;

VkHardwareTexture::VkHardwareTexture(int numchannels)
VkHardwareTexture::VkHardwareTexture(VulkanFrameBuffer* fb, int numchannels) : fb(fb)
{
mTexelsize = numchannels;
Next = First;
First = this;
if (Next) Next->Prev = this;
fb->GetTextureManager()->AddTexture(this);
}

VkHardwareTexture::~VkHardwareTexture()
{
if (Next) Next->Prev = Prev;
if (Prev) Prev->Next = Next;
else First = Next;

Reset();
}

void VkHardwareTexture::ResetAll()
{
for (VkHardwareTexture *cur = VkHardwareTexture::First; cur; cur = cur->Next)
cur->Reset();
if (fb)
fb->GetTextureManager()->RemoveTexture(this);
}

void VkHardwareTexture::Reset()
{
if (auto fb = GetVulkanFrameBuffer())
if (fb)
{
if (mappedSWFB)
{
mImage.Image->Unmap();
mappedSWFB = nullptr;
}

auto &deleteList = fb->GetCommands()->FrameDeleteList;
if (mImage.Image) deleteList.Images.push_back(std::move(mImage.Image));
if (mImage.View) deleteList.ImageViews.push_back(std::move(mImage.View));
for (auto &it : mImage.RSFramebuffers) deleteList.Framebuffers.push_back(std::move(it.second));
if (mDepthStencil.Image) deleteList.Images.push_back(std::move(mDepthStencil.Image));
if (mDepthStencil.View) deleteList.ImageViews.push_back(std::move(mDepthStencil.View));
for (auto &it : mDepthStencil.RSFramebuffers) deleteList.Framebuffers.push_back(std::move(it.second));
mImage.reset();
mDepthStencil.reset();
mImage.Reset(fb);
mDepthStencil.Reset(fb);
}
}

Expand All @@ -96,8 +77,6 @@ VkTextureImage *VkHardwareTexture::GetDepthStencil(FTexture *tex)
{
if (!mDepthStencil.View)
{
auto fb = GetVulkanFrameBuffer();

VkFormat format = fb->GetBuffers()->SceneDepthStencilFormat;
int w = tex->GetWidth();
int h = tex->GetHeight();
Expand Down Expand Up @@ -133,8 +112,6 @@ void VkHardwareTexture::CreateImage(FTexture *tex, int translation, int flags)
}
else
{
auto fb = GetVulkanFrameBuffer();

VkFormat format = VK_FORMAT_R8G8B8A8_UNORM;
int w = tex->GetWidth();
int h = tex->GetHeight();
Expand Down Expand Up @@ -164,8 +141,6 @@ void VkHardwareTexture::CreateTexture(int w, int h, int pixelsize, VkFormat form
if (w <= 0 || h <= 0)
throw CVulkanError("Trying to create zero size texture");

auto fb = GetVulkanFrameBuffer();

int totalSize = w * h * pixelsize;

BufferBuilder bufbuilder;
Expand Down Expand Up @@ -234,8 +209,6 @@ void VkHardwareTexture::AllocateBuffer(int w, int h, int texelsize)

if (!mImage.Image)
{
auto fb = GetVulkanFrameBuffer();

VkFormat format = texelsize == 4 ? VK_FORMAT_B8G8R8A8_UNORM : VK_FORMAT_R8_UNORM;

ImageBuilder imgbuilder;
Expand Down Expand Up @@ -283,8 +256,6 @@ unsigned int VkHardwareTexture::CreateTexture(unsigned char * buffer, int w, int

void VkHardwareTexture::CreateWipeTexture(int w, int h, const char *name)
{
auto fb = GetVulkanFrameBuffer();

VkFormat format = VK_FORMAT_B8G8R8A8_UNORM;

ImageBuilder imgbuilder;
Expand Down Expand Up @@ -331,48 +302,32 @@ void VkHardwareTexture::CreateWipeTexture(int w, int h, const char *name)
}
}

/////////////////////////////////////////////////////////////////////////////

VkMaterial* VkMaterial::First = nullptr;

VkMaterial::VkMaterial(FGameTexture* tex, int scaleflags) : FMaterial(tex, scaleflags)
VkMaterial::VkMaterial(VulkanFrameBuffer* fb, FGameTexture* tex, int scaleflags) : FMaterial(tex, scaleflags), fb(fb)
{
Next = First;
First = this;
if (Next) Next->Prev = this;
fb->GetDescriptorSetManager()->AddMaterial(this);
}

VkMaterial::~VkMaterial()
{
if (Next) Next->Prev = Prev;
if (Prev) Prev->Next = Next;
else First = Next;

DeleteDescriptors();
if (fb)
fb->GetDescriptorSetManager()->RemoveMaterial(this);
}

void VkMaterial::DeleteDescriptors()
{
if (auto fb = GetVulkanFrameBuffer())
if (fb)
{
auto& deleteList = fb->GetCommands()->FrameDeleteList;

for (auto& it : mDescriptorSets)
{
deleteList.Descriptors.push_back(std::move(it.descriptor));
}
}

mDescriptorSets.clear();
}

void VkMaterial::ResetAllDescriptors()
{
for (VkMaterial* cur = First; cur; cur = cur->Next)
cur->DeleteDescriptors();

auto fb = GetVulkanFrameBuffer();
if (fb)
fb->GetDescriptorSetManager()->TextureSetPoolReset();
mDescriptorSets.clear();
}
}

VulkanDescriptorSet* VkMaterial::GetDescriptorSet(const FMaterialState& state)
Expand All @@ -391,7 +346,6 @@ VulkanDescriptorSet* VkMaterial::GetDescriptorSet(const FMaterialState& state)

int numLayers = NumLayers();

auto fb = GetVulkanFrameBuffer();
auto descriptor = fb->GetDescriptorSetManager()->AllocateTextureDescriptorSet(max(numLayers, SHADER_MIN_REQUIRED_TEXTURE_LAYERS));

descriptor->SetDebugName("VkHardwareTexture.mDescriptorSets");
Expand Down

0 comments on commit b3316fb

Please sign in to comment.