Files
Seele/src/Engine/Graphics/Vulkan/RenderPass.cpp
T

152 lines
6.0 KiB
C++
Raw Normal View History

2023-10-26 18:37:29 +02:00
#include "RenderPass.h"
#include "Graphics.h"
#include "Framebuffer.h"
#include "Texture.h"
2023-11-05 10:36:01 +01:00
#include "RenderPass.h"
2020-04-12 15:47:19 +02:00
using namespace Seele;
using namespace Seele::Vulkan;
2023-11-08 23:27:21 +01:00
RenderPass::RenderPass(PGraphics graphics, Gfx::ORenderTargetLayout _layout, Gfx::PViewport viewport)
: Gfx::RenderPass(std::move(_layout))
2020-05-05 01:51:13 +02:00
, graphics(graphics)
2020-04-12 15:47:19 +02:00
{
2023-11-15 00:06:00 +01:00
renderArea.extent.width = viewport->getWidth();
renderArea.extent.height = viewport->getHeight();
2021-09-23 10:10:39 +02:00
renderArea.offset.x = viewport->getOffsetX();
renderArea.offset.y = viewport->getOffsetY();
2020-10-03 11:00:10 +02:00
subpassContents = VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS;
2020-04-12 15:47:19 +02:00
Array<VkAttachmentDescription> attachments;
Array<VkAttachmentReference> inputRefs;
Array<VkAttachmentReference> colorRefs;
VkAttachmentReference depthRef;
uint32 attachmentCounter = 0;
2023-11-08 23:27:21 +01:00
for (auto& inputAttachment : layout->inputAttachments)
2020-04-12 15:47:19 +02:00
{
PTexture2D image = inputAttachment->getTexture().cast<Texture2D>();
2023-11-15 17:42:57 +01:00
VkAttachmentDescription& desc = attachments.add() = {
.flags = 0,
.format = cast(image->getFormat()),
.loadOp = cast(inputAttachment->getLoadOp()),
.storeOp = cast(inputAttachment->getStoreOp()),
.stencilLoadOp = cast(inputAttachment->getStencilLoadOp()),
.stencilStoreOp = cast(inputAttachment->getStencilStoreOp()),
.initialLayout = image->isDepthStencil() ? VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL : VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
.finalLayout = desc.initialLayout,
};
inputRefs.add() = {
.attachment = attachmentCounter++,
.layout = desc.initialLayout,
};
2020-04-12 15:47:19 +02:00
}
2023-11-08 23:27:21 +01:00
for (auto& colorAttachment : layout->colorAttachments)
2020-04-12 15:47:19 +02:00
{
2023-11-15 17:42:57 +01:00
attachments.add() = {
.flags = 0,
.format = cast(colorAttachment->getFormat()),
.samples = (VkSampleCountFlagBits)colorAttachment->getNumSamples(),
.loadOp = cast(colorAttachment->getLoadOp()),
.storeOp = cast(colorAttachment->getStoreOp()),
.stencilLoadOp = cast(colorAttachment->getStencilLoadOp()),
.stencilStoreOp = cast(colorAttachment->getStencilStoreOp()),
.initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
};
2020-10-23 01:47:56 +02:00
VkClearValue& clearValue = clearValues.add();
2023-11-15 17:42:57 +01:00
if(attachments.back().loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR)
2020-10-23 01:47:56 +02:00
{
clearValue = cast(colorAttachment->clear);
}
2023-11-15 17:42:57 +01:00
colorRefs.add() = {
.attachment = attachmentCounter++,
.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
};
2020-04-12 15:47:19 +02:00
}
if (layout->depthAttachment != nullptr)
{
PTexture2D image = layout->depthAttachment->getTexture().cast<Texture2D>();
2023-11-15 17:42:57 +01:00
attachments.add() = {
.flags = 0,
.format = cast(image->getFormat()),
.samples = (VkSampleCountFlagBits)image->getNumSamples(),
.loadOp = cast(layout->depthAttachment->getLoadOp()),
.storeOp = cast(layout->depthAttachment->getStoreOp()),
.stencilLoadOp = cast(layout->depthAttachment->getStencilLoadOp()),
.stencilStoreOp = cast(layout->depthAttachment->getStencilStoreOp()),
.initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
};
2020-10-23 01:47:56 +02:00
VkClearValue& clearValue = clearValues.add();
2023-11-15 17:42:57 +01:00
if(attachments.back().loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR)
2020-10-23 01:47:56 +02:00
{
clearValue = cast(layout->depthAttachment->clear);
}
2023-11-15 17:42:57 +01:00
depthRef = {
.attachment = attachmentCounter++,
.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
};
2020-04-12 15:47:19 +02:00
}
2023-11-15 17:42:57 +01:00
VkSubpassDescription subPassDesc = {
.flags = 0,
.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
.inputAttachmentCount = (uint32)inputRefs.size(),
.pInputAttachments = inputRefs.data(),
.colorAttachmentCount = (uint32)colorRefs.size(),
.pColorAttachments = colorRefs.data(),
.pDepthStencilAttachment = &depthRef,
};
VkSubpassDependency dependency = {
.srcSubpass = VK_SUBPASS_EXTERNAL,
.dstSubpass = 0,
.srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
.srcAccessMask = VK_ACCESS_MEMORY_READ_BIT,
.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
};
VkRenderPassCreateInfo info = {
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
.pNext = nullptr,
.attachmentCount = (uint32)attachments.size(),
.pAttachments = attachments.data(),
.subpassCount = 1,
.pSubpasses = &subPassDesc,
.dependencyCount = 1,
.pDependencies = &dependency,
};
2020-04-12 15:47:19 +02:00
VK_CHECK(vkCreateRenderPass(graphics->getDevice(), &info, nullptr, &renderPass));
2023-11-16 22:58:47 +01:00
}
2020-04-12 15:47:19 +02:00
2023-11-16 22:58:47 +01:00
RenderPass::~RenderPass()
{
vkDestroyRenderPass(graphics->getDevice(), renderPass, nullptr);
}
uint32 RenderPass::getFramebufferHash()
{
2020-04-12 15:47:19 +02:00
FramebufferDescription description;
std::memset(&description, 0, sizeof(FramebufferDescription));
2023-11-01 23:12:30 +01:00
for (auto& inputAttachment : layout->inputAttachments)
2020-04-12 15:47:19 +02:00
{
PTexture2D tex = inputAttachment->getTexture().cast<Texture2D>();
description.inputAttachments[description.numInputAttachments++] = tex->getView();
}
2023-11-01 23:12:30 +01:00
for (auto& colorAttachment : layout->colorAttachments)
2020-04-12 15:47:19 +02:00
{
PTexture2D tex = colorAttachment->getTexture().cast<Texture2D>();
2020-05-05 01:51:13 +02:00
description.colorAttachments[description.numColorAttachments++] = tex->getView();
2020-04-12 15:47:19 +02:00
}
2020-05-05 01:51:13 +02:00
if (layout->depthAttachment != nullptr)
2020-04-12 15:47:19 +02:00
{
PTexture2D tex = layout->depthAttachment->getTexture().cast<Texture2D>();
description.depthAttachment = tex->getView();
}
2023-11-16 22:58:47 +01:00
return CRC::Calculate(&description, sizeof(FramebufferDescription), CRC::CRC_32());
2023-11-15 17:42:57 +01:00
}