2021-05-06 17:02:10 +02:00
|
|
|
#include "DepthPrepass.h"
|
2023-11-06 22:24:40 +01:00
|
|
|
#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"
|
2021-05-06 17:02:10 +02:00
|
|
|
|
|
|
|
|
using namespace Seele;
|
|
|
|
|
|
2023-10-24 15:01:09 +02:00
|
|
|
DepthPrepass::DepthPrepass(Gfx::PGraphics graphics, PScene scene)
|
|
|
|
|
: RenderPass(graphics, scene)
|
2023-11-09 22:15:51 +01:00
|
|
|
, descriptorSets(3)
|
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())
|
|
|
|
|
{
|
2024-04-19 18:23:36 +02:00
|
|
|
graphics->getShaderCompiler()->registerRenderPass(depthPrepassLayout, "DepthPass", "MeshletBasePass", false, false, "", true, true, "MeshletBasePass");
|
2023-11-22 13:18:54 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-04-19 18:23:36 +02:00
|
|
|
graphics->getShaderCompiler()->registerRenderPass(depthPrepassLayout, "DepthPass", "LegacyBasePass");
|
2023-11-22 13:18:54 +01:00
|
|
|
}
|
2021-05-06 17:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DepthPrepass::~DepthPrepass()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-17 16:47:42 +01: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);
|
|
|
|
|
descriptorSets[INDEX_VIEW_PARAMS] = viewParamsSet;
|
2021-05-06 17:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-15 23:45:44 +02:00
|
|
|
void DepthPrepass::render()
|
2021-05-06 17:02:10 +02:00
|
|
|
{
|
2023-11-05 10:36:01 +01:00
|
|
|
Gfx::ShaderPermutation permutation;
|
2023-11-07 16:55:13 +01:00
|
|
|
if(graphics->supportMeshShading())
|
2023-11-06 22:24:40 +01:00
|
|
|
{
|
2023-11-09 22:15:51 +01:00
|
|
|
permutation.setTaskFile("MeshletBasePass");
|
|
|
|
|
permutation.setMeshFile("MeshletBasePass");
|
2023-11-06 22:24:40 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-11-09 22:15:51 +01:00
|
|
|
permutation.setVertexFile("LegacyBasePass");
|
2023-11-06 22:24:40 +01:00
|
|
|
}
|
2021-05-06 17:02:10 +02:00
|
|
|
graphics->beginRenderPass(renderPass);
|
2024-04-10 10:23:06 +02:00
|
|
|
Array<Gfx::ORenderCommand> commands;
|
2023-10-24 15:01:09 +02:00
|
|
|
for (VertexData* vertexData : VertexData::getList())
|
2021-05-06 17:02:10 +02:00
|
|
|
{
|
2023-11-10 19:18:09 +01:00
|
|
|
permutation.setVertexData(vertexData->getTypeName());
|
2023-10-24 15:01:09 +02:00
|
|
|
const auto& materials = vertexData->getMaterialData();
|
|
|
|
|
for (const auto& [_, materialData] : materials)
|
|
|
|
|
{
|
|
|
|
|
// Create Pipeline(Material, VertexData)
|
|
|
|
|
// Descriptors:
|
|
|
|
|
// ViewData => global, static
|
|
|
|
|
// Material => per material
|
|
|
|
|
// VertexData => per meshtype
|
2023-10-26 18:37:29 +02:00
|
|
|
// SceneData => per material instance
|
2023-11-05 10:36:01 +01:00
|
|
|
Gfx::PermutationId id(permutation);
|
|
|
|
|
|
2024-04-10 10:23:06 +02:00
|
|
|
Gfx::ORenderCommand command = graphics->createRenderCommand("DepthRender");
|
2023-11-10 19:18:09 +01:00
|
|
|
command->setViewport(viewport);
|
2024-04-19 18:23:36 +02:00
|
|
|
|
2023-11-06 22:24:40 +01:00
|
|
|
const Gfx::ShaderCollection* collection = graphics->getShaderCompiler()->findShaders(id);
|
|
|
|
|
assert(collection != nullptr);
|
2023-11-07 16:55:13 +01:00
|
|
|
if(graphics->supportMeshShading())
|
2023-11-06 22:24:40 +01:00
|
|
|
{
|
|
|
|
|
Gfx::MeshPipelineCreateInfo pipelineInfo;
|
|
|
|
|
pipelineInfo.taskShader = collection->taskShader;
|
|
|
|
|
pipelineInfo.meshShader = collection->meshShader;
|
|
|
|
|
pipelineInfo.fragmentShader = collection->fragmentShader;
|
2024-04-19 18:23:36 +02:00
|
|
|
pipelineInfo.pipelineLayout = collection->pipelineLayout;
|
2023-11-06 22:24:40 +01:00
|
|
|
pipelineInfo.renderPass = renderPass;
|
2024-01-31 09:49:53 +01:00
|
|
|
pipelineInfo.depthStencilState.depthCompareOp = Gfx::SE_COMPARE_OP_LESS;
|
2023-12-10 22:27:59 +01:00
|
|
|
pipelineInfo.multisampleState.samples = viewport->getSamples();
|
2023-11-09 22:15:51 +01:00
|
|
|
Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo));
|
2023-11-06 22:24:40 +01:00
|
|
|
command->bindPipeline(pipeline);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Gfx::LegacyPipelineCreateInfo pipelineInfo;
|
|
|
|
|
pipelineInfo.vertexShader = collection->vertexShader;
|
|
|
|
|
pipelineInfo.fragmentShader = collection->fragmentShader;
|
2024-04-19 18:23:36 +02:00
|
|
|
pipelineInfo.pipelineLayout = collection->pipelineLayout;
|
2023-11-06 22:24:40 +01:00
|
|
|
pipelineInfo.renderPass = renderPass;
|
2024-01-31 09:49:53 +01:00
|
|
|
//pipelineInfo.depthStencilState.depthWriteEnable = false;
|
2023-11-06 22:24:40 +01:00
|
|
|
pipelineInfo.depthStencilState.depthCompareOp = Gfx::SE_COMPARE_OP_LESS_OR_EQUAL;
|
2023-12-10 22:27:59 +01:00
|
|
|
pipelineInfo.multisampleState.samples = viewport->getSamples();
|
2023-11-09 22:15:51 +01:00
|
|
|
Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo));
|
2023-11-06 22:24:40 +01:00
|
|
|
command->bindPipeline(pipeline);
|
|
|
|
|
}
|
2023-10-24 15:01:09 +02:00
|
|
|
|
|
|
|
|
descriptorSets[INDEX_VERTEX_DATA] = vertexData->getVertexDataSet();
|
2023-11-06 22:24:40 +01:00
|
|
|
for (const auto& [_, instance] : materialData.instances)
|
2023-10-24 15:01:09 +02:00
|
|
|
{
|
2023-11-09 22:15:51 +01:00
|
|
|
//descriptorSets[INDEX_MATERIAL] = instance.materialInstance->getDescriptorSet();
|
2023-10-24 15:01:09 +02:00
|
|
|
descriptorSets[INDEX_SCENE_DATA] = instance.descriptorSet;
|
|
|
|
|
command->bindDescriptor(descriptorSets);
|
2023-11-26 11:27:39 +01:00
|
|
|
if (graphics->supportMeshShading())
|
2023-11-06 22:24:40 +01:00
|
|
|
{
|
2024-04-10 08:43:56 +02:00
|
|
|
command->drawMesh(instance.meshes.size(), 1, 1);
|
2023-11-06 22:24:40 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-11-26 11:27:39 +01:00
|
|
|
command->bindIndexBuffer(vertexData->getIndexBuffer());
|
2023-11-06 22:24:40 +01:00
|
|
|
uint32 instanceOffset = 0;
|
2023-12-10 22:27:59 +01:00
|
|
|
for (const auto& [_, meshData] : instance.meshes)
|
2023-11-06 22:24:40 +01:00
|
|
|
{
|
2023-11-26 11:27:39 +01:00
|
|
|
if (meshData.numIndices > 0)
|
2023-11-09 22:15:51 +01:00
|
|
|
{
|
2023-11-26 11:27:39 +01:00
|
|
|
command->drawIndexed(meshData.numIndices, 1, meshData.firstIndex, meshData.indicesOffset, instanceOffset++);
|
2023-11-09 22:15:51 +01:00
|
|
|
}
|
2023-11-06 22:24:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-10-24 15:01:09 +02:00
|
|
|
}
|
2024-04-10 10:23:06 +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();
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-15 23:45:44 +02:00
|
|
|
void DepthPrepass::endFrame()
|
2021-05-06 17:02:10 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DepthPrepass::publishOutputs()
|
|
|
|
|
{
|
2021-09-23 10:10:39 +02:00
|
|
|
// If we render to a part of an image, the depth buffer itself must
|
|
|
|
|
// still match the size of the whole image or their coordinate systems go out of sync
|
2023-11-15 17:42:57 +01:00
|
|
|
TextureCreateInfo depthBufferInfo = {
|
|
|
|
|
.format = Gfx::SE_FORMAT_D32_SFLOAT,
|
2023-11-16 22:58:47 +01:00
|
|
|
.width = viewport->getOwner()->getFramebufferWidth(),
|
|
|
|
|
.height = viewport->getOwner()->getFramebufferHeight(),
|
2023-11-15 17:42:57 +01:00
|
|
|
.usage = Gfx::SE_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
|
|
|
|
|
};
|
2021-05-06 17:02:10 +02:00
|
|
|
depthBuffer = graphics->createTexture2D(depthBufferInfo);
|
|
|
|
|
depthAttachment =
|
2024-02-01 10:21:36 +01:00
|
|
|
Gfx::RenderTargetAttachment(depthBuffer,
|
2024-01-31 09:49:53 +01:00
|
|
|
Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_GENERAL,
|
|
|
|
|
Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE);
|
2024-02-01 10:21:36 +01:00
|
|
|
depthAttachment.clear.depthStencil.depth = 1.0f;
|
2021-10-01 19:55:04 +02:00
|
|
|
resources->registerRenderPassOutput("DEPTHPREPASS_DEPTH", depthAttachment);
|
2021-05-06 17:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DepthPrepass::createRenderPass()
|
|
|
|
|
{
|
2024-01-31 09:49:53 +01:00
|
|
|
Gfx::RenderTargetLayout layout = Gfx::RenderTargetLayout{
|
2023-12-10 22:27:59 +01:00
|
|
|
.depthAttachment = depthAttachment,
|
|
|
|
|
};
|
2024-04-04 08:30:59 +02:00
|
|
|
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,
|
2024-04-05 10:41:59 +02:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
.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,
|
2024-04-04 08:30:59 +02:00
|
|
|
}
|
2024-01-31 09:49:53 +01:00
|
|
|
};
|
2024-04-04 08:30:59 +02:00
|
|
|
renderPass = graphics->createRenderPass(std::move(layout), dependency, viewport);
|
2021-05-06 17:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-16 21:11:57 +01:00
|
|
|
void DepthPrepass::modifyRenderPassMacros(Map<const char*, const char*>&)
|
2021-05-06 17:02:10 +02:00
|
|
|
{
|
|
|
|
|
}
|