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

267 lines
12 KiB
C++
Raw Normal View History

2023-10-26 18:37:29 +02:00
#include "RenderPass.h"
2024-06-09 10:44:24 +02:00
#include "CRC.h"
2024-06-09 12:20:04 +02:00
#include "Command.h"
#include "Framebuffer.h"
#include "Graphics.h"
#include "Resources.h"
#include "Texture.h"
2020-04-12 15:47:19 +02:00
using namespace Seele;
using namespace Seele::Vulkan;
2024-06-09 12:20:04 +02:00
RenderPass::RenderPass(PGraphics graphics, Gfx::RenderTargetLayout _layout, Array<Gfx::SubPassDependency> _dependencies,
2024-07-17 09:33:37 +02:00
Gfx::PViewport viewport, std::string name)
2024-06-09 12:20:04 +02:00
: Gfx::RenderPass(std::move(_layout), std::move(_dependencies)), graphics(graphics) {
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;
2023-12-10 22:27:59 +01:00
Array<VkAttachmentDescription2> attachments;
Array<VkAttachmentReference2> inputRefs;
Array<VkAttachmentReference2> colorRefs;
Array<VkAttachmentReference2> resolveRefs;
VkAttachmentReference2 depthRef;
VkAttachmentReference2 depthResolveRef;
2020-04-12 15:47:19 +02:00
uint32 attachmentCounter = 0;
2024-06-09 12:20:04 +02:00
for (auto& inputAttachment : layout.inputAttachments) {
2024-02-01 10:21:36 +01:00
PTexture2D image = inputAttachment.getTexture().cast<Texture2D>();
2023-12-10 22:27:59 +01:00
VkAttachmentDescription2& desc = attachments.add() = {
.sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2,
.pNext = nullptr,
2023-11-15 17:42:57 +01:00
.flags = 0,
.format = cast(image->getFormat()),
2024-02-01 10:21:36 +01:00
.loadOp = cast(inputAttachment.getLoadOp()),
.storeOp = cast(inputAttachment.getStoreOp()),
.stencilLoadOp = cast(inputAttachment.getStencilLoadOp()),
.stencilStoreOp = cast(inputAttachment.getStencilStoreOp()),
.initialLayout = cast(inputAttachment.getInitialLayout()),
.finalLayout = cast(inputAttachment.getFinalLayout()),
2023-11-15 17:42:57 +01:00
};
2024-06-09 12:20:04 +02:00
2023-11-15 17:42:57 +01:00
inputRefs.add() = {
.attachment = attachmentCounter++,
.layout = desc.initialLayout,
};
2020-04-12 15:47:19 +02:00
}
2024-06-09 12:20:04 +02:00
for (auto& colorAttachment : layout.colorAttachments) {
2023-11-15 17:42:57 +01:00
attachments.add() = {
2023-12-10 22:27:59 +01:00
.sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2,
.pNext = nullptr,
2023-11-15 17:42:57 +01:00
.flags = 0,
2024-02-01 10:21:36 +01:00
.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 = cast(colorAttachment.getInitialLayout()),
.finalLayout = cast(colorAttachment.getFinalLayout()),
2023-11-15 17:42:57 +01:00
};
2024-06-09 12:20:04 +02:00
2020-10-23 01:47:56 +02:00
VkClearValue& clearValue = clearValues.add();
2024-06-09 12:20:04 +02:00
if (attachments.back().loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) {
2024-02-01 10:21:36 +01:00
clearValue = cast(colorAttachment.clear);
2020-10-23 01:47:56 +02:00
}
2023-11-15 17:42:57 +01:00
colorRefs.add() = {
2023-12-10 22:27:59 +01:00
.sType = VK_STRUCTURE_TYPE_ATTACHMENT_REFERENCE_2,
.pNext = nullptr,
.attachment = attachmentCounter++,
.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
};
}
2024-06-09 12:20:04 +02:00
for (auto& resolveAttachment : layout.resolveAttachments) {
2023-12-10 22:27:59 +01:00
attachments.add() = {
.sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2,
.pNext = nullptr,
.flags = 0,
2024-02-01 10:21:36 +01:00
.format = cast(resolveAttachment.getFormat()),
2024-07-05 12:02:46 +02:00
.samples = (VkSampleCountFlagBits)resolveAttachment.getTexture()->getNumSamples(),
2024-02-01 10:21:36 +01:00
.loadOp = cast(resolveAttachment.getLoadOp()),
.storeOp = cast(resolveAttachment.getStoreOp()),
.stencilLoadOp = cast(resolveAttachment.getStencilLoadOp()),
.stencilStoreOp = cast(resolveAttachment.getStencilStoreOp()),
.initialLayout = cast(resolveAttachment.getInitialLayout()),
.finalLayout = cast(resolveAttachment.getFinalLayout()),
2023-12-10 22:27:59 +01:00
};
VkClearValue& clearValue = clearValues.add();
2024-06-09 12:20:04 +02:00
if (attachments.back().loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) {
2024-02-01 10:21:36 +01:00
clearValue = cast(resolveAttachment.clear);
2023-12-10 22:27:59 +01:00
}
resolveRefs.add() = {
.sType = VK_STRUCTURE_TYPE_ATTACHMENT_REFERENCE_2,
.pNext = nullptr,
2023-11-15 17:42:57 +01:00
.attachment = attachmentCounter++,
.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
};
2020-04-12 15:47:19 +02:00
}
2024-06-09 12:20:04 +02:00
if (layout.depthAttachment.getTexture() != nullptr) {
2024-02-01 10:21:36 +01:00
PTexture2D image = layout.depthAttachment.getTexture().cast<Texture2D>();
2023-11-15 17:42:57 +01:00
attachments.add() = {
2023-12-10 22:27:59 +01:00
.sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2,
.pNext = nullptr,
2023-11-15 17:42:57 +01:00
.flags = 0,
.format = cast(image->getFormat()),
.samples = (VkSampleCountFlagBits)image->getNumSamples(),
2024-02-01 10:21:36 +01:00
.loadOp = cast(layout.depthAttachment.getLoadOp()),
.storeOp = cast(layout.depthAttachment.getStoreOp()),
.stencilLoadOp = cast(layout.depthAttachment.getStencilLoadOp()),
.stencilStoreOp = cast(layout.depthAttachment.getStencilStoreOp()),
.initialLayout = cast(layout.depthAttachment.getInitialLayout()),
.finalLayout = cast(layout.depthAttachment.getFinalLayout()),
2023-11-15 17:42:57 +01:00
};
2023-12-10 22:27:59 +01:00
2020-10-23 01:47:56 +02:00
VkClearValue& clearValue = clearValues.add();
2024-06-09 12:20:04 +02:00
if (attachments.back().loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) {
2024-02-01 10:21:36 +01:00
clearValue = cast(layout.depthAttachment.clear);
2020-10-23 01:47:56 +02:00
}
2023-11-15 17:42:57 +01:00
depthRef = {
2023-12-10 22:27:59 +01:00
.sType = VK_STRUCTURE_TYPE_ATTACHMENT_REFERENCE_2,
.pNext = nullptr,
2023-11-15 17:42:57 +01:00
.attachment = attachmentCounter++,
.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
};
2020-04-12 15:47:19 +02:00
}
2024-06-09 12:20:04 +02:00
if (layout.depthResolveAttachment.getTexture() != nullptr) {
2024-02-01 10:21:36 +01:00
PTexture2D image = layout.depthResolveAttachment.getTexture().cast<Texture2D>();
2023-12-10 22:27:59 +01:00
attachments.add() = {
.sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2,
.pNext = nullptr,
.flags = 0,
.format = cast(image->getFormat()),
.samples = (VkSampleCountFlagBits)image->getNumSamples(),
2024-02-01 10:21:36 +01:00
.loadOp = cast(layout.depthResolveAttachment.getLoadOp()),
.storeOp = cast(layout.depthResolveAttachment.getStoreOp()),
.stencilLoadOp = cast(layout.depthResolveAttachment.getStencilLoadOp()),
.stencilStoreOp = cast(layout.depthResolveAttachment.getStencilStoreOp()),
.initialLayout = cast(layout.depthResolveAttachment.getInitialLayout()),
.finalLayout = cast(layout.depthResolveAttachment.getFinalLayout()),
2023-12-10 22:27:59 +01:00
};
2024-07-05 12:02:46 +02:00
VkClearValue& clearValue = clearValues.add();
if (attachments.back().loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) {
clearValue = cast(layout.depthResolveAttachment.clear);
}
2023-12-10 22:27:59 +01:00
depthResolveRef = {
.sType = VK_STRUCTURE_TYPE_ATTACHMENT_REFERENCE_2,
.pNext = nullptr,
.attachment = attachmentCounter++,
.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
};
}
2024-06-09 12:20:04 +02:00
2023-12-10 22:27:59 +01:00
VkSubpassDescriptionDepthStencilResolve depthResolve = {
.sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE,
.pNext = nullptr,
.depthResolveMode = VK_RESOLVE_MODE_AVERAGE_BIT,
.stencilResolveMode = VK_RESOLVE_MODE_AVERAGE_BIT,
.pDepthStencilResolveAttachment = &depthResolveRef,
};
VkSubpassDescription2 subPassDesc = {
.sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION_2,
2024-02-01 10:21:36 +01:00
.pNext = layout.depthResolveAttachment.getTexture() != nullptr ? &depthResolve : nullptr,
2023-11-15 17:42:57 +01:00
.flags = 0,
.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
.inputAttachmentCount = (uint32)inputRefs.size(),
.pInputAttachments = inputRefs.data(),
.colorAttachmentCount = (uint32)colorRefs.size(),
.pColorAttachments = colorRefs.data(),
2023-12-10 22:27:59 +01:00
.pResolveAttachments = resolveRefs.size() > 0 ? resolveRefs.data() : nullptr,
2023-11-15 17:42:57 +01:00
.pDepthStencilAttachment = &depthRef,
};
Array<VkSubpassDependency2> dep;
2024-06-09 12:20:04 +02:00
for (const auto& d : dependencies) {
dep.add() = {
.sType = VK_STRUCTURE_TYPE_SUBPASS_DEPENDENCY_2,
.pNext = nullptr,
.srcSubpass = d.srcSubpass,
.dstSubpass = d.dstSubpass,
.srcStageMask = d.srcStage,
.dstStageMask = d.dstStage,
.srcAccessMask = d.srcAccess,
.dstAccessMask = d.dstAccess,
.dependencyFlags = 0,
};
}
2023-12-10 22:27:59 +01:00
VkRenderPassCreateInfo2 info = {
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2,
2023-11-15 17:42:57 +01:00
.pNext = nullptr,
.attachmentCount = (uint32)attachments.size(),
.pAttachments = attachments.data(),
.subpassCount = 1,
.pSubpasses = &subPassDesc,
.dependencyCount = (uint32)dep.size(),
.pDependencies = dep.data(),
2023-11-15 17:42:57 +01:00
};
2020-04-12 15:47:19 +02:00
2023-12-10 22:27:59 +01:00
VK_CHECK(vkCreateRenderPass2(graphics->getDevice(), &info, nullptr, &renderPass));
2024-07-17 09:33:37 +02:00
VkDebugUtilsObjectNameInfoEXT nameInfo = {
.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
.pNext = nullptr,
.objectType = VK_OBJECT_TYPE_RENDER_PASS,
.objectHandle = (uint64)renderPass,
.pObjectName = name.c_str(),
};
assert(!name.empty());
vkSetDebugUtilsObjectNameEXT(graphics->getDevice(), &nameInfo);
2023-11-16 22:58:47 +01:00
}
2020-04-12 15:47:19 +02:00
2024-06-09 12:20:04 +02:00
RenderPass::~RenderPass() {
2024-05-15 15:27:13 +02:00
vkDestroyRenderPass(graphics->getDevice(), renderPass, nullptr);
2024-06-09 12:20:04 +02:00
// graphics->getDestructionManager()->queueRenderPass(graphics->getGraphicsCommands()->getCommands(), renderPass);
2023-11-16 22:58:47 +01:00
}
2024-06-09 12:20:04 +02:00
uint32 RenderPass::getFramebufferHash() {
2020-04-12 15:47:19 +02:00
FramebufferDescription description;
std::memset(&description, 0, sizeof(FramebufferDescription));
2024-06-09 12:20:04 +02:00
for (auto& inputAttachment : layout.inputAttachments) {
2024-02-01 10:21:36 +01:00
PTexture2D tex = inputAttachment.getTexture().cast<Texture2D>();
2020-04-12 15:47:19 +02:00
description.inputAttachments[description.numInputAttachments++] = tex->getView();
}
2024-06-09 12:20:04 +02:00
for (auto& colorAttachment : layout.colorAttachments) {
2024-02-01 10:21:36 +01: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
}
2024-07-05 12:02:46 +02:00
for (auto& resolveAttachment : layout.resolveAttachments) {
PTexture2D tex = resolveAttachment.getTexture().cast<Texture2D>();
description.resolveAttachments[description.numResolveAttachments++] = tex->getView();
}
2024-06-09 12:20:04 +02:00
if (layout.depthAttachment.getTexture() != nullptr) {
2024-02-01 10:21:36 +01:00
PTexture2D tex = layout.depthAttachment.getTexture().cast<Texture2D>();
2020-04-12 15:47:19 +02:00
description.depthAttachment = tex->getView();
}
2024-07-05 12:02:46 +02:00
if (layout.depthResolveAttachment.getTexture() != nullptr) {
PTexture2D tex = layout.depthResolveAttachment.getTexture().cast<Texture2D>();
description.depthResolveAttachment = 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
}
2024-07-05 12:02:46 +02:00
void RenderPass::endRenderPass() {
2024-06-09 12:20:04 +02:00
for (auto& inputAttachment : layout.inputAttachments) {
2024-02-01 10:21:36 +01:00
PTexture2D tex = inputAttachment.getTexture().cast<Texture2D>();
tex->setLayout(inputAttachment.getFinalLayout());
}
2024-06-09 12:20:04 +02:00
for (auto& colorAttachment : layout.colorAttachments) {
2024-02-01 10:21:36 +01:00
PTexture2D tex = colorAttachment.getTexture().cast<Texture2D>();
tex->setLayout(colorAttachment.getFinalLayout());
}
2024-07-05 12:02:46 +02:00
for (auto& resolveAttachment : layout.resolveAttachments) {
PTexture2D tex = resolveAttachment.getTexture().cast<Texture2D>();
tex->setLayout(resolveAttachment.getFinalLayout());
}
2024-06-09 12:20:04 +02:00
if (layout.depthAttachment.getTexture() != nullptr) {
2024-02-01 10:21:36 +01:00
PTexture2D tex = layout.depthAttachment.getTexture().cast<Texture2D>();
tex->setLayout(layout.depthAttachment.getFinalLayout());
}
2024-07-05 12:02:46 +02:00
if (layout.depthResolveAttachment.getTexture() != nullptr) {
PTexture2D tex = layout.depthResolveAttachment.getTexture().cast<Texture2D>();
tex->setLayout(layout.depthResolveAttachment.getFinalLayout());
}
}