2024-06-11 14:15:29 +02:00
|
|
|
#include "DepthCullingPass.h"
|
2023-11-05 10:36:01 +01:00
|
|
|
#include "Graphics/Shader.h"
|
2021-05-06 17:02:10 +02:00
|
|
|
|
|
|
|
|
using namespace Seele;
|
|
|
|
|
|
2024-05-23 14:58:14 +02:00
|
|
|
extern bool usePositionOnly;
|
2024-06-11 16:55:20 +02:00
|
|
|
extern bool useDepthCulling;
|
2024-05-23 14:58:14 +02:00
|
|
|
|
2024-06-11 14:15:29 +02:00
|
|
|
DepthCullingPass::DepthCullingPass(Gfx::PGraphics graphics, PScene scene) : RenderPass(graphics, scene) {
|
|
|
|
|
depthTextureLayout = graphics->createDescriptorLayout("pDepthAttachment");
|
|
|
|
|
depthTextureLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
|
|
|
|
.binding = 0,
|
|
|
|
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
|
|
|
|
|
.shaderStages = Gfx::SE_SHADER_STAGE_TASK_BIT_EXT | Gfx::SE_SHADER_STAGE_MESH_BIT_EXT,
|
|
|
|
|
});
|
|
|
|
|
depthTextureLayout->create();
|
|
|
|
|
|
2024-05-30 21:24:21 +02:00
|
|
|
depthPrepassLayout = graphics->createPipelineLayout("DepthPrepassLayout");
|
|
|
|
|
depthPrepassLayout->addDescriptorLayout(viewParamsLayout);
|
2024-06-11 14:15:29 +02:00
|
|
|
depthPrepassLayout->addDescriptorLayout(depthTextureLayout);
|
2024-05-30 21:24:21 +02:00
|
|
|
depthPrepassLayout->addPushConstants(Gfx::SePushConstantRange{
|
|
|
|
|
.stageFlags = Gfx::SE_SHADER_STAGE_TASK_BIT_EXT | Gfx::SE_SHADER_STAGE_VERTEX_BIT,
|
|
|
|
|
.offset = 0,
|
|
|
|
|
.size = sizeof(VertexData::DrawCallOffsets),
|
|
|
|
|
});
|
2024-06-09 12:20:04 +02:00
|
|
|
if (graphics->supportMeshShading()) {
|
2024-05-31 14:21:32 +02:00
|
|
|
graphics->getShaderCompiler()->registerRenderPass("DepthPass", Gfx::PassConfig{
|
2024-06-09 12:20:04 +02:00
|
|
|
.baseLayout = depthPrepassLayout,
|
|
|
|
|
.taskFile = "DepthCullingTask",
|
|
|
|
|
.mainFile = "DepthCullingMesh",
|
|
|
|
|
.fragmentFile = "VisibilityPass",
|
|
|
|
|
.hasFragmentShader = true,
|
|
|
|
|
.useMeshShading = true,
|
|
|
|
|
.hasTaskShader = true,
|
|
|
|
|
.useMaterial = false,
|
|
|
|
|
.useVisibility = true,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2024-05-31 14:21:32 +02:00
|
|
|
graphics->getShaderCompiler()->registerRenderPass("DepthPass", Gfx::PassConfig{
|
2024-06-09 12:20:04 +02:00
|
|
|
.baseLayout = depthPrepassLayout,
|
|
|
|
|
.taskFile = "",
|
|
|
|
|
.mainFile = "LegacyPass",
|
|
|
|
|
.fragmentFile = "VisibilityPass",
|
|
|
|
|
.hasFragmentShader = true,
|
|
|
|
|
.useMeshShading = false,
|
|
|
|
|
.hasTaskShader = false,
|
|
|
|
|
.useMaterial = false,
|
|
|
|
|
.useVisibility = true,
|
|
|
|
|
});
|
2024-05-30 21:24:21 +02:00
|
|
|
}
|
2021-05-06 17:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-11 14:15:29 +02:00
|
|
|
DepthCullingPass::~DepthCullingPass() {}
|
2021-05-06 17:02:10 +02:00
|
|
|
|
2024-06-11 14:15:29 +02:00
|
|
|
void DepthCullingPass::beginFrame(const Component::Camera& cam) { RenderPass::beginFrame(cam); }
|
|
|
|
|
|
|
|
|
|
void DepthCullingPass::render() {
|
|
|
|
|
depthAttachment.getTexture()->changeLayout(Gfx::SE_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
|
|
|
|
|
Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, Gfx::SE_ACCESS_TRANSFER_READ_BIT,
|
|
|
|
|
Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT);
|
|
|
|
|
depthMipTexture->changeLayout(Gfx::SE_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, Gfx::SE_ACCESS_SHADER_READ_BIT,
|
|
|
|
|
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT, Gfx::SE_ACCESS_TRANSFER_WRITE_BIT,
|
|
|
|
|
Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT);
|
|
|
|
|
|
|
|
|
|
graphics->copyTexture(depthAttachment.getTexture(), Gfx::PTexture2D(depthMipTexture));
|
|
|
|
|
depthMipTexture->generateMipmaps();
|
|
|
|
|
|
|
|
|
|
depthAttachment.getTexture()->changeLayout(
|
|
|
|
|
Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, Gfx::SE_ACCESS_TRANSFER_READ_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
|
|
|
|
|
Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
|
|
|
|
|
Gfx::SE_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT);
|
|
|
|
|
depthMipTexture->changeLayout(Gfx::SE_IMAGE_LAYOUT_GENERAL, Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
|
|
|
|
|
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_TASK_SHADER_BIT_EXT);
|
|
|
|
|
|
|
|
|
|
Gfx::PDescriptorSet set = depthTextureLayout->allocateDescriptorSet();
|
|
|
|
|
set->updateTexture(0, Gfx::PTexture2D(depthMipTexture));
|
|
|
|
|
set->writeChanges();
|
2021-05-06 17:02:10 +02:00
|
|
|
|
2024-05-30 21:24:21 +02:00
|
|
|
graphics->beginRenderPass(renderPass);
|
2024-06-11 16:55:20 +02:00
|
|
|
if (useDepthCulling) {
|
2024-05-09 08:41:46 +02:00
|
|
|
|
2024-06-11 16:55:20 +02:00
|
|
|
Array<Gfx::ORenderCommand> commands;
|
2024-05-30 21:24:21 +02:00
|
|
|
|
2024-06-11 16:55:20 +02:00
|
|
|
Gfx::ShaderPermutation permutation = graphics->getShaderCompiler()->getTemplate("DepthPass");
|
|
|
|
|
permutation.setPositionOnly(usePositionOnly);
|
|
|
|
|
permutation.setDepthCulling(useDepthCulling);
|
|
|
|
|
for (VertexData* vertexData : VertexData::getList()) {
|
|
|
|
|
permutation.setVertexData(vertexData->getTypeName());
|
|
|
|
|
vertexData->getInstanceDataSet()->updateBuffer(6, cullingBuffer);
|
|
|
|
|
vertexData->getInstanceDataSet()->writeChanges();
|
|
|
|
|
// Create Pipeline(VertexData)
|
|
|
|
|
// Descriptors:
|
|
|
|
|
// ViewData => global, static
|
|
|
|
|
// VertexData => per meshtype
|
|
|
|
|
// SceneData => per meshtype
|
|
|
|
|
Gfx::PermutationId id(permutation);
|
2024-05-30 21:24:21 +02:00
|
|
|
|
2024-06-11 16:55:20 +02:00
|
|
|
Gfx::ORenderCommand command = graphics->createRenderCommand("DepthRender");
|
|
|
|
|
command->setViewport(viewport);
|
|
|
|
|
|
|
|
|
|
const Gfx::ShaderCollection* collection = graphics->getShaderCompiler()->findShaders(id);
|
|
|
|
|
assert(collection != nullptr);
|
|
|
|
|
if (graphics->supportMeshShading()) {
|
|
|
|
|
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 = {
|
|
|
|
|
.vertexShader = collection->vertexShader,
|
|
|
|
|
.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);
|
|
|
|
|
}
|
|
|
|
|
command->bindDescriptor({viewParamsSet, vertexData->getVertexDataSet(), vertexData->getInstanceDataSet(), set});
|
|
|
|
|
uint32 offset = 0;
|
|
|
|
|
command->pushConstants(Gfx::SE_SHADER_STAGE_TASK_BIT_EXT | Gfx::SE_SHADER_STAGE_VERTEX_BIT, 0,
|
|
|
|
|
sizeof(VertexData::DrawCallOffsets), &offset);
|
|
|
|
|
if (graphics->supportMeshShading()) {
|
|
|
|
|
command->drawMesh(vertexData->getNumInstances(), 1, 1);
|
|
|
|
|
} else {
|
|
|
|
|
const auto& materials = vertexData->getMaterialData();
|
|
|
|
|
for (const auto& materialData : materials) {
|
|
|
|
|
for (const auto& drawCall : materialData.instances) {
|
|
|
|
|
// material not used for any active meshes, skip
|
|
|
|
|
if (materialData.instances.size() == 0)
|
|
|
|
|
continue;
|
|
|
|
|
command->bindIndexBuffer(vertexData->getIndexBuffer());
|
|
|
|
|
uint32 inst = drawCall.offsets.instanceOffset;
|
|
|
|
|
for (const auto& meshData : drawCall.instanceMeshData) {
|
|
|
|
|
// all meshlets of a mesh share the same indices offset
|
|
|
|
|
command->drawIndexed(meshData.numIndices, 1, meshData.firstIndex,
|
|
|
|
|
vertexData->getIndicesOffset(meshData.meshletOffset), inst++);
|
|
|
|
|
}
|
2024-05-30 21:24:21 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-11 16:55:20 +02:00
|
|
|
commands.add(std::move(command));
|
2024-05-30 21:24:21 +02:00
|
|
|
}
|
2024-05-08 10:51:59 +02:00
|
|
|
|
2024-06-11 16:55:20 +02:00
|
|
|
graphics->executeCommands(std::move(commands));
|
|
|
|
|
}
|
2024-05-30 21:24:21 +02:00
|
|
|
graphics->endRenderPass();
|
|
|
|
|
// Sync depth read/write with compute read
|
2024-06-09 12:20:04 +02:00
|
|
|
depthAttachment.getTexture()->pipelineBarrier(
|
2024-05-30 21:24:21 +02:00
|
|
|
Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
|
2024-06-09 12:20:04 +02:00
|
|
|
Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
|
2024-05-30 21:24:21 +02:00
|
|
|
// Sync visibility write with compute read
|
2024-06-09 12:20:04 +02:00
|
|
|
visibilityAttachment.getTexture()->pipelineBarrier(Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
|
|
|
|
|
Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT,
|
|
|
|
|
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
|
2021-05-06 17:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-11 14:15:29 +02:00
|
|
|
void DepthCullingPass::endFrame() {}
|
2021-05-06 17:02:10 +02:00
|
|
|
|
2024-06-11 14:15:29 +02:00
|
|
|
void DepthCullingPass::publishOutputs() {
|
|
|
|
|
uint32 width = viewport->getOwner()->getFramebufferWidth();
|
|
|
|
|
uint32 height = viewport->getOwner()->getFramebufferHeight();
|
|
|
|
|
uint32 mipLevels = static_cast<uint32_t>(std::floor(std::log2(std::max(width, height)))) + 1;
|
|
|
|
|
TextureCreateInfo depthMipInfo = {
|
|
|
|
|
.format = Gfx::SE_FORMAT_D32_SFLOAT, .width = width, .height = height, .mipLevels = mipLevels, .name = "DepthMipTexture"};
|
|
|
|
|
depthMipTexture = graphics->createTexture2D(depthMipInfo);
|
|
|
|
|
}
|
2021-05-06 17:02:10 +02:00
|
|
|
|
2024-06-11 14:15:29 +02:00
|
|
|
void DepthCullingPass::createRenderPass() {
|
2024-06-07 09:19:47 +02:00
|
|
|
cullingBuffer = resources->requestBuffer("CULLINGBUFFER");
|
|
|
|
|
|
2024-05-30 21:24:21 +02:00
|
|
|
depthAttachment = resources->requestRenderTarget("DEPTHPREPASS_DEPTH");
|
|
|
|
|
depthAttachment.setInitialLayout(Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
|
|
|
|
|
depthAttachment.setFinalLayout(Gfx::SE_IMAGE_LAYOUT_GENERAL);
|
|
|
|
|
depthAttachment.setLoadOp(Gfx::SE_ATTACHMENT_LOAD_OP_LOAD);
|
|
|
|
|
visibilityAttachment = resources->requestRenderTarget("VISIBILITY");
|
|
|
|
|
visibilityAttachment.setInitialLayout(Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
|
2024-06-07 09:19:47 +02:00
|
|
|
visibilityAttachment.setFinalLayout(Gfx::SE_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
2024-05-30 21:24:21 +02:00
|
|
|
visibilityAttachment.setLoadOp(Gfx::SE_ATTACHMENT_LOAD_OP_LOAD);
|
2024-05-30 16:56:22 +02:00
|
|
|
|
2024-05-30 21:24:21 +02:00
|
|
|
Gfx::RenderTargetLayout layout = Gfx::RenderTargetLayout{
|
|
|
|
|
.colorAttachments = {visibilityAttachment},
|
|
|
|
|
.depthAttachment = depthAttachment,
|
|
|
|
|
};
|
|
|
|
|
Array<Gfx::SubPassDependency> dependency = {
|
2024-06-07 14:56:42 +02:00
|
|
|
{
|
|
|
|
|
.srcSubpass = ~0U,
|
|
|
|
|
.dstSubpass = 0,
|
|
|
|
|
.srcStage = Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
|
|
|
|
|
.dstStage = Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | Gfx::SE_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
|
2024-06-09 12:20:04 +02:00
|
|
|
.srcAccess = Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
|
|
|
|
|
Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
|
|
|
|
|
.dstAccess = Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
|
|
|
|
|
Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
|
2024-06-07 14:56:42 +02:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
.srcSubpass = 0,
|
|
|
|
|
.dstSubpass = ~0U,
|
|
|
|
|
.srcStage = Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
|
|
|
|
|
.dstStage = Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | Gfx::SE_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
|
2024-06-09 12:20:04 +02:00
|
|
|
.srcAccess = Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
|
|
|
|
|
Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
|
|
|
|
|
.dstAccess = Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
|
|
|
|
|
Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
|
2024-06-07 14:56:42 +02:00
|
|
|
},
|
2024-05-30 21:24:21 +02:00
|
|
|
};
|
|
|
|
|
renderPass = graphics->createRenderPass(std::move(layout), std::move(dependency), viewport);
|
2021-05-06 17:02:10 +02:00
|
|
|
}
|