Skip to content

Commit

Permalink
Move more code into the correct managers
Browse files Browse the repository at this point in the history
  • Loading branch information
dpjudas authored and coelckers committed Jun 22, 2022
1 parent 69cfadf commit ba88303
Show file tree
Hide file tree
Showing 21 changed files with 267 additions and 250 deletions.
62 changes: 58 additions & 4 deletions src/common/rendering/vulkan/renderer/vk_descriptorset.cpp
Expand Up @@ -27,6 +27,7 @@
#include "vulkan/textures/vk_samplers.h"
#include "vulkan/textures/vk_renderbuffers.h"
#include "vulkan/textures/vk_hwtexture.h"
#include "vulkan/textures/vk_texture.h"
#include "vulkan/system/vk_builders.h"
#include "vulkan/system/vk_framebuffer.h"
#include "vulkan/system/vk_hwbuffer.h"
Expand Down Expand Up @@ -123,13 +124,12 @@ void VkDescriptorSetManager::ResetHWTextureSets()
for (auto mat : Materials)
mat->DeleteDescriptors();

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

auto deleteList = fb->GetCommands()->DrawDeleteList.get();
for (auto& desc : TextureDescriptorPools)
{
deleteList.DescriptorPools.push_back(std::move(desc));
deleteList->Add(std::move(desc));
}
deleteList.Descriptors.push_back(std::move(NullTextureDescriptorSet));
deleteList->Add(std::move(NullTextureDescriptorSet));

NullTextureDescriptorSet.reset();
TextureDescriptorPools.clear();
Expand Down Expand Up @@ -222,3 +222,57 @@ void VkDescriptorSetManager::RemoveMaterial(VkMaterial* texture)
texture->fb = nullptr;
Materials.erase(texture->it);
}

VulkanDescriptorSet* VkDescriptorSetManager::GetInput(VkPPRenderPassSetup* passSetup, const TArray<PPTextureInput>& textures, bool bindShadowMapBuffers)
{
auto descriptors = AllocatePPDescriptorSet(passSetup->DescriptorLayout.get());
descriptors->SetDebugName("VkPostprocess.descriptors");

WriteDescriptors write;
VkImageTransition imageTransition;

for (unsigned int index = 0; index < textures.Size(); index++)
{
const PPTextureInput& input = textures[index];
VulkanSampler* sampler = fb->GetSamplerManager()->Get(input.Filter, input.Wrap);
VkTextureImage* tex = fb->GetTextureManager()->GetTexture(input.Type, input.Texture);

write.addCombinedImageSampler(descriptors.get(), index, tex->DepthOnlyView ? tex->DepthOnlyView.get() : tex->View.get(), sampler, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
imageTransition.addImage(tex, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, false);
}

if (bindShadowMapBuffers)
{
write.addBuffer(descriptors.get(), LIGHTNODES_BINDINGPOINT, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, fb->GetBufferManager()->LightNodes->mBuffer.get());
write.addBuffer(descriptors.get(), LIGHTLINES_BINDINGPOINT, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, fb->GetBufferManager()->LightLines->mBuffer.get());
write.addBuffer(descriptors.get(), LIGHTLIST_BINDINGPOINT, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, fb->GetBufferManager()->LightList->mBuffer.get());
}

write.updateSets(fb->device);
imageTransition.execute(fb->GetCommands()->GetDrawCommands());

VulkanDescriptorSet* set = descriptors.get();
fb->GetCommands()->DrawDeleteList->Add(std::move(descriptors));
return set;
}

std::unique_ptr<VulkanDescriptorSet> VkDescriptorSetManager::AllocatePPDescriptorSet(VulkanDescriptorSetLayout* layout)
{
if (PPDescriptorPool)
{
auto descriptors = PPDescriptorPool->tryAllocate(layout);
if (descriptors)
return descriptors;

fb->GetCommands()->DrawDeleteList->Add(std::move(PPDescriptorPool));
}

DescriptorPoolBuilder builder;
builder.addPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 200);
builder.addPoolSize(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 4);
builder.setMaxSets(100);
PPDescriptorPool = builder.create(fb->device);
PPDescriptorPool->SetDebugName("PPDescriptorPool");

return PPDescriptorPool->allocate(layout);
}
8 changes: 8 additions & 0 deletions src/common/rendering/vulkan/renderer/vk_descriptorset.h
Expand Up @@ -6,6 +6,8 @@

class VulkanFrameBuffer;
class VkMaterial;
class PPTextureInput;
class VkPPRenderPassSetup;

class VkDescriptorSetManager
{
Expand All @@ -28,6 +30,8 @@ class VkDescriptorSetManager

std::unique_ptr<VulkanDescriptorSet> AllocateTextureDescriptorSet(int numLayers);

VulkanDescriptorSet* GetInput(VkPPRenderPassSetup* passSetup, const TArray<PPTextureInput>& textures, bool bindShadowMapBuffers);

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

void AddMaterial(VkMaterial* texture);
Expand All @@ -38,6 +42,8 @@ class VkDescriptorSetManager
void CreateFixedSet();
void CreateNullTexture();

std::unique_ptr<VulkanDescriptorSet> AllocatePPDescriptorSet(VulkanDescriptorSetLayout* layout);

VulkanFrameBuffer* fb = nullptr;

std::unique_ptr<VulkanDescriptorSetLayout> DynamicSetLayout;
Expand All @@ -47,6 +53,8 @@ class VkDescriptorSetManager
std::unique_ptr<VulkanDescriptorPool> DynamicDescriptorPool;
std::unique_ptr<VulkanDescriptorPool> FixedDescriptorPool;

std::unique_ptr<VulkanDescriptorPool> PPDescriptorPool;

int TextureDescriptorSetsLeft = 0;
int TextureDescriptorsLeft = 0;
std::vector<std::unique_ptr<VulkanDescriptorPool>> TextureDescriptorPools;
Expand Down
22 changes: 0 additions & 22 deletions src/common/rendering/vulkan/renderer/vk_postprocess.cpp
Expand Up @@ -286,28 +286,6 @@ void VkPostprocess::UpdateShadowMap()
}
}

std::unique_ptr<VulkanDescriptorSet> VkPostprocess::AllocateDescriptorSet(VulkanDescriptorSetLayout *layout)
{
if (mDescriptorPool)
{
auto descriptors = mDescriptorPool->tryAllocate(layout);
if (descriptors)
return descriptors;

fb->GetCommands()->FrameDeleteList.DescriptorPools.push_back(std::move(mDescriptorPool));
}

DescriptorPoolBuilder builder;
builder.addPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 200);
builder.addPoolSize(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 4);
builder.setMaxSets(100);
mDescriptorPool = builder.create(fb->device);
mDescriptorPool->SetDebugName("VkPostprocess.mDescriptorPool");

return mDescriptorPool->allocate(layout);
}

void VkPostprocess::NextEye(int eyeCount)
{
}

5 changes: 2 additions & 3 deletions src/common/rendering/vulkan/renderer/vk_postprocess.h
Expand Up @@ -38,14 +38,13 @@ class VkPostprocess
void BlitCurrentToImage(VkTextureImage *image, VkImageLayout finallayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
void DrawPresentTexture(const IntRect &box, bool applyGamma, bool screenshot);

int GetCurrentPipelineImage() const { return mCurrentPipelineImage; }

private:
void NextEye(int eyeCount);

std::unique_ptr<VulkanDescriptorSet> AllocateDescriptorSet(VulkanDescriptorSetLayout *layout);

VulkanFrameBuffer* fb = nullptr;

std::unique_ptr<VulkanDescriptorPool> mDescriptorPool;
int mCurrentPipelineImage = 0;

friend class VkPPRenderState;
Expand Down
151 changes: 7 additions & 144 deletions src/common/rendering/vulkan/renderer/vk_pprenderstate.cpp
Expand Up @@ -30,7 +30,9 @@
#include "vulkan/textures/vk_pptexture.h"
#include "vulkan/textures/vk_renderbuffers.h"
#include "vulkan/textures/vk_samplers.h"
#include "vulkan/textures/vk_texture.h"
#include "vulkan/renderer/vk_renderstate.h"
#include "vulkan/renderer/vk_descriptorset.h"
#include "flatvertices.h"

VkPPRenderState::VkPPRenderState(VulkanFrameBuffer* fb) : fb(fb)
Expand All @@ -49,19 +51,17 @@ void VkPPRenderState::PopGroup()

void VkPPRenderState::Draw()
{
auto pp = fb->GetPostprocess();

fb->GetRenderState()->EndRenderPass();

VkPPRenderPassKey key;
key.BlendMode = BlendMode;
key.InputTextures = Textures.Size();
key.Uniforms = Uniforms.Data.Size();
key.Shader = GetVkShader(Shader);
key.Shader = fb->GetShaderManager()->GetVkShader(Shader);
key.SwapChain = (Output.Type == PPTextureType::SwapChain);
key.ShadowMapBuffers = ShadowMapBuffers;
if (Output.Type == PPTextureType::PPTexture)
key.OutputFormat = GetVkTexture(Output.Texture)->Format;
key.OutputFormat = fb->GetTextureManager()->GetTextureFormat(Output.Texture);
else if (Output.Type == PPTextureType::SwapChain)
key.OutputFormat = fb->GetCommands()->swapChain->swapChainFormat.format;
else if (Output.Type == PPTextureType::ShadowMap)
Expand All @@ -83,14 +83,15 @@ void VkPPRenderState::Draw()
auto passSetup = fb->GetRenderPassManager()->GetPPRenderPass(key);

int framebufferWidth = 0, framebufferHeight = 0;
VulkanDescriptorSet *input = GetInput(passSetup, Textures, ShadowMapBuffers);
VulkanFramebuffer *output = GetOutput(passSetup, Output, key.StencilTest, framebufferWidth, framebufferHeight);
VulkanDescriptorSet *input = fb->GetDescriptorSetManager()->GetInput(passSetup, Textures, ShadowMapBuffers);
VulkanFramebuffer *output = fb->GetBuffers()->GetOutput(passSetup, Output, key.StencilTest, framebufferWidth, framebufferHeight);

RenderScreenQuad(passSetup, input, output, framebufferWidth, framebufferHeight, Viewport.left, Viewport.top, Viewport.width, Viewport.height, Uniforms.Data.Data(), Uniforms.Data.Size(), key.StencilTest);

// Advance to next PP texture if our output was sent there
if (Output.Type == PPTextureType::NextPipelineTexture)
{
auto pp = fb->GetPostprocess();
pp->mCurrentPipelineImage = (pp->mCurrentPipelineImage + 1) % VkRenderBuffers::NumPipelineImages;
}
}
Expand Down Expand Up @@ -135,141 +136,3 @@ void VkPPRenderState::RenderScreenQuad(VkPPRenderPassSetup *passSetup, VulkanDes
cmdbuffer->draw(3, 1, FFlatVertexBuffer::PRESENT_INDEX, 0);
cmdbuffer->endRenderPass();
}

VulkanDescriptorSet *VkPPRenderState::GetInput(VkPPRenderPassSetup *passSetup, const TArray<PPTextureInput> &textures, bool bindShadowMapBuffers)
{
auto descriptors = fb->GetPostprocess()->AllocateDescriptorSet(passSetup->DescriptorLayout.get());
descriptors->SetDebugName("VkPostprocess.descriptors");

WriteDescriptors write;
VkImageTransition imageTransition;

for (unsigned int index = 0; index < textures.Size(); index++)
{
const PPTextureInput &input = textures[index];
VulkanSampler *sampler = fb->GetSamplerManager()->Get(input.Filter, input.Wrap);
VkTextureImage *tex = GetTexture(input.Type, input.Texture);

write.addCombinedImageSampler(descriptors.get(), index, tex->DepthOnlyView ? tex->DepthOnlyView.get() : tex->View.get(), sampler, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
imageTransition.addImage(tex, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, false);
}

if (bindShadowMapBuffers)
{
write.addBuffer(descriptors.get(), LIGHTNODES_BINDINGPOINT, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, fb->GetBufferManager()->LightNodes->mBuffer.get());
write.addBuffer(descriptors.get(), LIGHTLINES_BINDINGPOINT, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, fb->GetBufferManager()->LightLines->mBuffer.get());
write.addBuffer(descriptors.get(), LIGHTLIST_BINDINGPOINT, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, fb->GetBufferManager()->LightList->mBuffer.get());
}

write.updateSets(fb->device);
imageTransition.execute(fb->GetCommands()->GetDrawCommands());

VulkanDescriptorSet *set = descriptors.get();
fb->GetCommands()->FrameDeleteList.Descriptors.push_back(std::move(descriptors));
return set;
}

VulkanFramebuffer *VkPPRenderState::GetOutput(VkPPRenderPassSetup *passSetup, const PPOutput &output, bool stencilTest, int &framebufferWidth, int &framebufferHeight)
{
VkTextureImage *tex = GetTexture(output.Type, output.Texture);

VkImageView view;
std::unique_ptr<VulkanFramebuffer> *framebufferptr = nullptr;
int w, h;
if (tex)
{
VkImageTransition imageTransition;
imageTransition.addImage(tex, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, output.Type == PPTextureType::NextPipelineTexture);
if (stencilTest)
imageTransition.addImage(&fb->GetBuffers()->SceneDepthStencil, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, false);
imageTransition.execute(fb->GetCommands()->GetDrawCommands());

view = tex->View->view;
w = tex->Image->width;
h = tex->Image->height;
framebufferptr = &tex->PPFramebuffer;
}
else
{
view = fb->GetCommands()->swapChain->swapChainImageViews[fb->GetCommands()->presentImageIndex];
framebufferptr = &fb->GetCommands()->swapChain->framebuffers[fb->GetCommands()->presentImageIndex];
w = fb->GetCommands()->swapChain->actualExtent.width;
h = fb->GetCommands()->swapChain->actualExtent.height;
}

auto &framebuffer = *framebufferptr;
if (!framebuffer)
{
FramebufferBuilder builder;
builder.setRenderPass(passSetup->RenderPass.get());
builder.setSize(w, h);
builder.addAttachment(view);
if (stencilTest)
builder.addAttachment(fb->GetBuffers()->SceneDepthStencil.View.get());
framebuffer = builder.create(fb->device);
}

framebufferWidth = w;
framebufferHeight = h;
return framebuffer.get();
}

VkTextureImage *VkPPRenderState::GetTexture(const PPTextureType &type, PPTexture *pptexture)
{
if (type == PPTextureType::CurrentPipelineTexture || type == PPTextureType::NextPipelineTexture)
{
int idx = fb->GetPostprocess()->mCurrentPipelineImage;
if (type == PPTextureType::NextPipelineTexture)
idx = (idx + 1) % VkRenderBuffers::NumPipelineImages;

return &fb->GetBuffers()->PipelineImage[idx];
}
else if (type == PPTextureType::PPTexture)
{
auto vktex = GetVkTexture(pptexture);
return &vktex->TexImage;
}
else if (type == PPTextureType::SceneColor)
{
return &fb->GetBuffers()->SceneColor;
}
else if (type == PPTextureType::SceneNormal)
{
return &fb->GetBuffers()->SceneNormal;
}
else if (type == PPTextureType::SceneFog)
{
return &fb->GetBuffers()->SceneFog;
}
else if (type == PPTextureType::SceneDepth)
{
return &fb->GetBuffers()->SceneDepthStencil;
}
else if (type == PPTextureType::ShadowMap)
{
return &fb->GetBuffers()->Shadowmap;
}
else if (type == PPTextureType::SwapChain)
{
return nullptr;
}
else
{
I_FatalError("VkPPRenderState::GetTexture not implemented yet for this texture type");
return nullptr;
}
}

VkPPShader *VkPPRenderState::GetVkShader(PPShader *shader)
{
if (!shader->Backend)
shader->Backend = std::make_unique<VkPPShader>(fb, shader);
return static_cast<VkPPShader*>(shader->Backend.get());
}

VkPPTexture *VkPPRenderState::GetVkTexture(PPTexture *texture)
{
if (!texture->Backend)
texture->Backend = std::make_unique<VkPPTexture>(fb, texture);
return static_cast<VkPPTexture*>(texture->Backend.get());
}
8 changes: 0 additions & 8 deletions src/common/rendering/vulkan/renderer/vk_pprenderstate.h
Expand Up @@ -23,13 +23,5 @@ class VkPPRenderState : public PPRenderState
private:
void RenderScreenQuad(VkPPRenderPassSetup *passSetup, VulkanDescriptorSet *descriptorSet, VulkanFramebuffer *framebuffer, int framebufferWidth, int framebufferHeight, int x, int y, int width, int height, const void *pushConstants, uint32_t pushConstantsSize, bool stencilTest);

VulkanDescriptorSet *GetInput(VkPPRenderPassSetup *passSetup, const TArray<PPTextureInput> &textures, bool bindShadowMapBuffers);
VulkanFramebuffer *GetOutput(VkPPRenderPassSetup *passSetup, const PPOutput &output, bool stencilTest, int &framebufferWidth, int &framebufferHeight);

VkPPShader *GetVkShader(PPShader *shader);
VkPPTexture *GetVkTexture(PPTexture *texture);

VkTextureImage *GetTexture(const PPTextureType &type, PPTexture *tex);

VulkanFrameBuffer* fb = nullptr;
};

0 comments on commit ba88303

Please sign in to comment.