2024-05-30 16:56:22 +02:00
|
|
|
#include "CachedDepthPass.h"
|
|
|
|
|
#include "Graphics/Shader.h"
|
|
|
|
|
|
|
|
|
|
using namespace Seele;
|
|
|
|
|
|
|
|
|
|
extern bool usePositionOnly;
|
|
|
|
|
extern bool useViewCulling;
|
|
|
|
|
|
|
|
|
|
CachedDepthPass::CachedDepthPass(Gfx::PGraphics graphics, PScene scene)
|
|
|
|
|
: RenderPass(graphics, scene)
|
|
|
|
|
{
|
2024-05-30 21:24:21 +02:00
|
|
|
depthPrepassLayout = graphics->createPipelineLayout("CachedDepthLayout");
|
|
|
|
|
depthPrepassLayout->addDescriptorLayout(viewParamsLayout);
|
|
|
|
|
depthPrepassLayout->addPushConstants(Gfx::SePushConstantRange{
|
|
|
|
|
.stageFlags = Gfx::SE_SHADER_STAGE_TASK_BIT_EXT | Gfx::SE_SHADER_STAGE_VERTEX_BIT,
|
|
|
|
|
.offset = 0,
|
|
|
|
|
.size = sizeof(VertexData::DrawCallOffsets),
|
|
|
|
|
});
|
|
|
|
|
if (graphics->supportMeshShading())
|
|
|
|
|
{
|
2024-05-31 14:21:32 +02:00
|
|
|
graphics->getShaderCompiler()->registerRenderPass("CachedDepthPass", Gfx::PassConfig{
|
|
|
|
|
.baseLayout = depthPrepassLayout,
|
|
|
|
|
.taskFile = "DrawListTask",
|
2024-06-07 14:56:42 +02:00
|
|
|
.mainFile = "DrawListMesh",
|
2024-05-31 14:21:32 +02:00
|
|
|
.fragmentFile = "VisibilityPass",
|
|
|
|
|
.hasFragmentShader = true,
|
|
|
|
|
.useMeshShading = true,
|
|
|
|
|
.hasTaskShader = true,
|
|
|
|
|
.useMaterial = false,
|
|
|
|
|
.useVisibility = true,
|
|
|
|
|
});
|
2024-05-30 21:24:21 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-05-31 14:21:32 +02:00
|
|
|
graphics->getShaderCompiler()->registerRenderPass("CachedDepthPass", Gfx::PassConfig{
|
|
|
|
|
.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
|
|
|
}
|
2024-05-30 16:56:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CachedDepthPass::~CachedDepthPass()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CachedDepthPass::beginFrame(const Component::Camera &cam)
|
|
|
|
|
{
|
2024-05-30 21:24:21 +02:00
|
|
|
RenderPass::beginFrame(cam);
|
2024-05-30 16:56:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CachedDepthPass::render()
|
|
|
|
|
{
|
2024-05-30 21:24:21 +02:00
|
|
|
graphics->beginRenderPass(renderPass);
|
|
|
|
|
Array<Gfx::ORenderCommand> commands;
|
2024-05-30 16:56:22 +02:00
|
|
|
|
2024-05-31 14:21:32 +02:00
|
|
|
Gfx::ShaderPermutation permutation = graphics->getShaderCompiler()->getTemplate("CachedDepthPass");
|
2024-05-30 21:24:21 +02:00
|
|
|
permutation.setPositionOnly(usePositionOnly);
|
|
|
|
|
permutation.setViewCulling(useViewCulling);
|
|
|
|
|
for (VertexData *vertexData : VertexData::getList())
|
2024-05-30 16:56:22 +02:00
|
|
|
{
|
2024-05-30 21:24:21 +02:00
|
|
|
permutation.setVertexData(vertexData->getTypeName());
|
2024-06-07 09:19:47 +02:00
|
|
|
vertexData->getInstanceDataSet()->updateBuffer(6, cullingBuffer);
|
|
|
|
|
vertexData->getInstanceDataSet()->writeChanges();
|
2024-05-30 21:24:21 +02:00
|
|
|
|
|
|
|
|
// Create Pipeline(VertexData)
|
|
|
|
|
// Descriptors:
|
|
|
|
|
// ViewData => global, static
|
|
|
|
|
// VertexData => per meshtype
|
|
|
|
|
// SceneData => per meshtype
|
|
|
|
|
Gfx::PermutationId id(permutation);
|
|
|
|
|
|
|
|
|
|
Gfx::ORenderCommand command = graphics->createRenderCommand("DepthRender");
|
|
|
|
|
command->setViewport(viewport);
|
|
|
|
|
|
|
|
|
|
const Gfx::ShaderCollection *collection = graphics->getShaderCompiler()->findShaders(id);
|
|
|
|
|
assert(collection != nullptr);
|
|
|
|
|
if (graphics->supportMeshShading())
|
2024-05-30 16:56:22 +02:00
|
|
|
{
|
2024-05-30 21:24:21 +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);
|
2024-05-30 16:56:22 +02:00
|
|
|
}
|
2024-05-30 21:24:21 +02:00
|
|
|
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);
|
2024-06-07 09:19:47 +02:00
|
|
|
command->bindDescriptor(vertexData->getVertexDataSet());
|
2024-05-30 21:24:21 +02:00
|
|
|
command->bindDescriptor(vertexData->getInstanceDataSet());
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
// material not used for any active meshes, skip
|
|
|
|
|
if (materialData.instances.size() == 0)
|
|
|
|
|
continue;
|
|
|
|
|
for (const auto &drawCall : materialData.instances)
|
|
|
|
|
{
|
|
|
|
|
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++);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
commands.add(std::move(command));
|
2024-05-30 16:56:22 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-30 21:24:21 +02:00
|
|
|
graphics->executeCommands(std::move(commands));
|
|
|
|
|
graphics->endRenderPass();
|
|
|
|
|
// Sync depth read/write with depth pass depth read
|
2024-06-07 14:56:42 +02:00
|
|
|
//depthBuffer->pipelineBarrier(
|
|
|
|
|
// Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
|
|
|
|
|
// Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
|
|
|
|
|
// Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT,
|
|
|
|
|
// Gfx::SE_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT);
|
2024-05-30 21:24:21 +02:00
|
|
|
// sync visibility write with depth pass visibility write
|
2024-06-07 14:56:42 +02:00
|
|
|
//visibilityBuffer->pipelineBarrier(
|
|
|
|
|
// Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
|
|
|
|
|
// Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
|
|
|
|
|
// Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
|
|
|
|
|
// Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT);
|
2024-05-30 16:56:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CachedDepthPass::endFrame()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CachedDepthPass::publishOutputs()
|
|
|
|
|
{
|
2024-05-30 21:24:21 +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
|
|
|
|
|
TextureCreateInfo depthBufferInfo = {
|
|
|
|
|
.format = Gfx::SE_FORMAT_D32_SFLOAT,
|
|
|
|
|
.width = viewport->getOwner()->getFramebufferWidth(),
|
|
|
|
|
.height = viewport->getOwner()->getFramebufferHeight(),
|
|
|
|
|
.usage = Gfx::SE_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
|
|
|
|
|
};
|
|
|
|
|
depthBuffer = graphics->createTexture2D(depthBufferInfo);
|
|
|
|
|
depthAttachment =
|
|
|
|
|
Gfx::RenderTargetAttachment(depthBuffer,
|
|
|
|
|
Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
|
|
|
|
|
Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE);
|
|
|
|
|
resources->registerRenderPassOutput("DEPTHPREPASS_DEPTH", depthAttachment);
|
2024-05-30 16:56:22 +02:00
|
|
|
|
2024-05-30 21:24:21 +02:00
|
|
|
TextureCreateInfo visibilityInfo = {
|
|
|
|
|
.format = Gfx::SE_FORMAT_R32_UINT,
|
|
|
|
|
.width = viewport->getOwner()->getFramebufferWidth(),
|
|
|
|
|
.height = viewport->getOwner()->getFramebufferHeight(),
|
|
|
|
|
.usage = Gfx::SE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
|
|
|
|
|
};
|
|
|
|
|
visibilityBuffer = graphics->createTexture2D(visibilityInfo);
|
|
|
|
|
visibilityAttachment =
|
|
|
|
|
Gfx::RenderTargetAttachment(visibilityBuffer,
|
|
|
|
|
Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
|
|
|
|
|
Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE);
|
|
|
|
|
resources->registerRenderPassOutput("VISIBILITY", visibilityAttachment);
|
2024-05-30 16:56:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CachedDepthPass::createRenderPass()
|
|
|
|
|
{
|
2024-06-07 09:19:47 +02:00
|
|
|
cullingBuffer = resources->requestBuffer("CULLINGBUFFER");
|
|
|
|
|
|
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,
|
|
|
|
|
.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,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
.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,
|
|
|
|
|
.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-05-30 21:24:21 +02:00
|
|
|
};
|
|
|
|
|
renderPass = graphics->createRenderPass(std::move(layout), std::move(dependency), viewport);
|
2024-05-30 16:56:22 +02:00
|
|
|
}
|