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

182 lines
7.0 KiB
C++
Raw Normal View History

2021-05-06 17:02:10 +02:00
#include "DepthPrepass.h"
#include "Graphics/Enums.h"
2021-05-06 17:02:10 +02:00
#include "Graphics/Graphics.h"
2023-11-05 10:36:01 +01:00
#include "Graphics/Shader.h"
2021-05-06 17:02:10 +02:00
#include "Window/Window.h"
2022-11-17 16:47:42 +01:00
#include "Component/Camera.h"
2023-10-24 15:01:09 +02:00
#include "Component/Mesh.h"
2022-11-17 16:47:42 +01:00
#include "Actor/CameraActor.h"
2021-05-06 17:02:10 +02:00
#include "Math/Vector.h"
#include "RenderGraph.h"
2023-11-15 17:42:57 +01:00
#include "Graphics/Command.h"
#include "Graphics/StaticMeshVertexData.h"
2021-05-06 17:02:10 +02:00
using namespace Seele;
2024-05-09 08:41:46 +02:00
DepthPrepass::DepthPrepass(Gfx::PGraphics graphics, PScene scene)
2023-10-24 15:01:09 +02:00
: RenderPass(graphics, scene)
2021-05-06 17:02:10 +02:00
{
2024-04-20 21:35:43 +02:00
depthPrepassLayout = graphics->createPipelineLayout("DepthPrepassLayout");
2024-04-19 18:23:36 +02:00
depthPrepassLayout->addDescriptorLayout(viewParamsLayout);
2023-11-22 13:18:54 +01:00
if (graphics->supportMeshShading())
{
graphics->getShaderCompiler()->registerRenderPass(depthPrepassLayout, "DepthPass", "MeshletPass", false, false, "", true, true, "MeshletPass");
2023-11-22 13:18:54 +01:00
}
else
{
graphics->getShaderCompiler()->registerRenderPass(depthPrepassLayout, "DepthPass", "LegacyPass");
2023-11-22 13:18:54 +01:00
}
2021-05-06 17:02:10 +02:00
}
DepthPrepass::~DepthPrepass()
2024-05-09 08:41:46 +02:00
{
2021-05-06 17:02:10 +02:00
}
2024-05-09 08:41:46 +02:00
void DepthPrepass::beginFrame(const Component::Camera& cam)
2021-05-06 17:02:10 +02:00
{
2023-11-05 10:36:01 +01:00
RenderPass::beginFrame(cam);
2021-05-06 17:02:10 +02:00
}
2024-05-09 08:41:46 +02:00
void DepthPrepass::render()
2021-05-06 17:02:10 +02:00
{
graphics->beginRenderPass(renderPass);
2024-04-10 10:23:06 +02:00
Array<Gfx::ORenderCommand> commands;
2024-05-09 08:41:46 +02:00
Gfx::ShaderPermutation permutation;
if (graphics->supportMeshShading())
2021-05-06 17:02:10 +02:00
{
2024-05-09 08:41:46 +02:00
permutation.setTaskFile("MeshletPass");
permutation.setMeshFile("MeshletPass");
}
else
{
permutation.setVertexFile("LegacyPass");
}
for (VertexData* vertexData : VertexData::getList())
{
permutation.setVertexData(vertexData->getTypeName());
const auto& materials = vertexData->getMaterialData();
for (const auto& materialData : materials)
2023-10-24 15:01:09 +02:00
{
2024-05-09 08:41:46 +02:00
// Create Pipeline(VertexData)
// Descriptors:
// ViewData => global, static
// VertexData => per meshtype
// SceneData => per material instance
Gfx::PermutationId id(permutation);
Gfx::ORenderCommand command = graphics->createRenderCommand("BaseRender");
command->setViewport(viewport);
const Gfx::ShaderCollection* collection = graphics->getShaderCompiler()->findShaders(id);
assert(collection != nullptr);
if (graphics->supportMeshShading())
{
2024-05-09 08:41:46 +02:00
Gfx::MeshPipelineCreateInfo pipelineInfo = {
.taskShader = collection->taskShader,
.meshShader = collection->meshShader,
.fragmentShader = collection->fragmentShader,
.renderPass = renderPass,
.pipelineLayout = collection->pipelineLayout,
.multisampleState = {
.samples = viewport->getSamples(),},
.depthStencilState = {
.depthCompareOp = Gfx::SE_COMPARE_OP_GREATER,
},
.colorBlend = {
.attachmentCount = 1,
},
};
Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo));
command->bindPipeline(pipeline);
}
else
{
Gfx::LegacyPipelineCreateInfo pipelineInfo;
pipelineInfo.vertexShader = collection->vertexShader;
pipelineInfo.fragmentShader = collection->fragmentShader;
pipelineInfo.pipelineLayout = collection->pipelineLayout;
pipelineInfo.renderPass = renderPass;
pipelineInfo.depthStencilState.depthCompareOp = Gfx::SE_COMPARE_OP_GREATER;
pipelineInfo.multisampleState.samples = viewport->getSamples();
pipelineInfo.colorBlend.attachmentCount = 1;
Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo));
command->bindPipeline(pipeline);
}
command->bindDescriptor(vertexData->getVertexDataSet());
command->bindDescriptor(viewParamsSet);
for (const auto& instance : materialData.instances)
{
command->bindDescriptor(vertexData->getInstanceDataSet(), { instance.descriptorOffset, instance.descriptorOffset });
2023-11-26 11:27:39 +01:00
if (graphics->supportMeshShading())
{
2024-05-09 08:41:46 +02:00
command->drawMesh(vertexData->getMeshData(instance.meshId).size(), 1, 1);
}
else
{
2024-05-09 08:41:46 +02:00
//command->bindIndexBuffer(vertexData->getIndexBuffer());
//uint32 instanceOffset = 0;
//for (const auto& meshData : vertexData->getMeshData(instance.meshId))
//{
// if (meshData.numIndices > 0)
// {
// command->drawIndexed(meshData.numIndices, 1, meshData.firstIndex, meshData.indicesOffset, instanceOffset);
// }
// instanceOffset++;
//}
}
2023-10-24 15:01:09 +02:00
}
2024-05-09 08:41:46 +02:00
commands.add(std::move(command));
2023-10-24 15:01:09 +02:00
}
2021-11-24 12:10:23 +01:00
}
2024-04-10 10:23:06 +02:00
graphics->executeCommands(std::move(commands));
2021-05-06 17:02:10 +02:00
graphics->endRenderPass();
}
2024-05-09 08:41:46 +02:00
void DepthPrepass::endFrame()
2021-05-06 17:02:10 +02:00
{
}
2024-05-09 08:41:46 +02:00
void DepthPrepass::publishOutputs()
2021-05-06 17:02:10 +02:00
{
}
2024-05-09 08:41:46 +02:00
void DepthPrepass::createRenderPass()
2021-05-06 17:02:10 +02:00
{
2024-05-09 08:41:46 +02:00
depthAttachment = resources->requestRenderTarget("DEPTHPREPASS_DEPTH");
depthAttachment.setLoadOp(Gfx::SE_ATTACHMENT_LOAD_OP_LOAD);
depthAttachment.setInitialLayout(Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
depthAttachment.setFinalLayout(Gfx::SE_IMAGE_LAYOUT_GENERAL);
Gfx::RenderTargetLayout layout = Gfx::RenderTargetLayout{
.depthAttachment = depthAttachment,
};
Array<Gfx::SubPassDependency> dependency = {
{
.srcSubpass = ~0U,
.dstSubpass = 0,
.srcStage = Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
.dstStage = Gfx::SE_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
.srcAccess = Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
.dstAccess = Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
},
{
.srcSubpass = 0,
.dstSubpass = ~0U,
.srcStage = Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
.dstStage = Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
.srcAccess = Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
.dstAccess = Gfx::SE_ACCESS_SHADER_READ_BIT,
},
{
.srcSubpass = 0,
.dstSubpass = ~0U,
.srcStage = Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
.dstStage = Gfx::SE_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
.srcAccess = Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
.dstAccess = Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT,
}
};
renderPass = graphics->createRenderPass(std::move(layout), std::move(dependency), viewport);
2021-05-06 17:02:10 +02:00
}