2024-05-30 16:56:22 +02:00
|
|
|
#include "CachedDepthPass.h"
|
|
|
|
|
#include "Graphics/Shader.h"
|
2025-04-04 14:44:53 +02:00
|
|
|
#include "Graphics/Pipeline.h"
|
2024-05-30 16:56:22 +02:00
|
|
|
|
|
|
|
|
using namespace Seele;
|
|
|
|
|
|
2025-01-08 19:15:12 +01:00
|
|
|
CachedDepthPass::CachedDepthPass(Gfx::PGraphics graphics, PScene scene) : RenderPass(graphics), scene(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),
|
|
|
|
|
});
|
2024-06-09 12:20:04 +02:00
|
|
|
if (graphics->supportMeshShading()) {
|
2024-05-31 14:21:32 +02:00
|
|
|
graphics->getShaderCompiler()->registerRenderPass("CachedDepthPass", Gfx::PassConfig{
|
2024-06-09 12:20:04 +02:00
|
|
|
.baseLayout = depthPrepassLayout,
|
|
|
|
|
.taskFile = "DrawListTask",
|
|
|
|
|
.mainFile = "DrawListMesh",
|
|
|
|
|
.fragmentFile = "VisibilityPass",
|
|
|
|
|
.hasFragmentShader = true,
|
|
|
|
|
.useMeshShading = true,
|
|
|
|
|
.hasTaskShader = true,
|
|
|
|
|
.useMaterial = false,
|
|
|
|
|
.useVisibility = true,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2024-05-31 14:21:32 +02:00
|
|
|
graphics->getShaderCompiler()->registerRenderPass("CachedDepthPass", 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
|
|
|
}
|
2024-05-30 16:56:22 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
CachedDepthPass::~CachedDepthPass() {}
|
2024-05-30 16:56:22 +02:00
|
|
|
|
2024-09-03 11:03:23 +02:00
|
|
|
void CachedDepthPass::beginFrame(const Component::Camera& cam) { RenderPass::beginFrame(cam); }
|
2024-05-30 16:56:22 +02:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void CachedDepthPass::render() {
|
2025-03-09 22:42:05 +01:00
|
|
|
query->beginQuery();
|
2024-08-13 22:44:04 +02:00
|
|
|
timestamps->write(Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT, "CachedBegin");
|
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-07-05 12:02:46 +02:00
|
|
|
permutation.setPositionOnly(getGlobals().usePositionOnly);
|
2024-08-13 22:44:04 +02:00
|
|
|
permutation.setDepthCulling(true);
|
2024-06-09 12:20:04 +02:00
|
|
|
for (VertexData* vertexData : VertexData::getList()) {
|
2024-05-30 21:24:21 +02:00
|
|
|
permutation.setVertexData(vertexData->getTypeName());
|
2025-03-09 22:42:05 +01:00
|
|
|
vertexData->getInstanceDataSet()->updateBuffer(VertexData::CULLINGDATA_NAME, 0, cullingBuffer);
|
2024-06-07 09:19:47 +02:00
|
|
|
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);
|
|
|
|
|
|
2024-09-27 15:57:37 +02:00
|
|
|
Gfx::ORenderCommand command = graphics->createRenderCommand("CullingRender");
|
2024-05-30 21:24:21 +02:00
|
|
|
command->setViewport(viewport);
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
const Gfx::ShaderCollection* collection = graphics->getShaderCompiler()->findShaders(id);
|
2024-05-30 21:24:21 +02:00
|
|
|
assert(collection != nullptr);
|
2024-06-09 12:20:04 +02:00
|
|
|
if (graphics->supportMeshShading()) {
|
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,
|
2024-09-03 11:03:23 +02:00
|
|
|
.rasterizationState =
|
|
|
|
|
{
|
|
|
|
|
.cullMode = Gfx::SE_CULL_MODE_NONE,
|
|
|
|
|
},
|
2024-06-09 12:20:04 +02:00
|
|
|
.colorBlend =
|
|
|
|
|
{
|
|
|
|
|
.attachmentCount = 1,
|
|
|
|
|
},
|
2024-05-30 21:24:21 +02:00
|
|
|
};
|
|
|
|
|
Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo));
|
|
|
|
|
command->bindPipeline(pipeline);
|
2024-06-09 12:20:04 +02:00
|
|
|
} else {
|
2024-05-30 21:24:21 +02:00
|
|
|
Gfx::LegacyPipelineCreateInfo pipelineInfo = {
|
|
|
|
|
.vertexShader = collection->vertexShader,
|
|
|
|
|
.fragmentShader = collection->fragmentShader,
|
|
|
|
|
.renderPass = renderPass,
|
|
|
|
|
.pipelineLayout = collection->pipelineLayout,
|
2024-09-03 11:03:23 +02:00
|
|
|
.rasterizationState =
|
|
|
|
|
{
|
|
|
|
|
.cullMode = Gfx::SE_CULL_MODE_NONE,
|
|
|
|
|
},
|
2024-06-09 12:20:04 +02:00
|
|
|
.colorBlend =
|
|
|
|
|
{
|
|
|
|
|
.attachmentCount = 1,
|
|
|
|
|
},
|
2024-05-30 21:24:21 +02:00
|
|
|
};
|
|
|
|
|
Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo));
|
|
|
|
|
command->bindPipeline(pipeline);
|
|
|
|
|
}
|
2024-06-11 14:15:29 +02:00
|
|
|
command->bindDescriptor({viewParamsSet, vertexData->getVertexDataSet(), vertexData->getInstanceDataSet()});
|
2024-11-01 21:04:06 +01:00
|
|
|
VertexData::DrawCallOffsets offsets = {
|
|
|
|
|
.instanceOffset = 0,
|
|
|
|
|
};
|
2024-06-09 12:20:04 +02:00
|
|
|
command->pushConstants(Gfx::SE_SHADER_STAGE_TASK_BIT_EXT | Gfx::SE_SHADER_STAGE_VERTEX_BIT, 0, sizeof(VertexData::DrawCallOffsets),
|
2024-11-01 21:04:06 +01:00
|
|
|
&offsets);
|
2024-06-09 12:20:04 +02:00
|
|
|
if (graphics->supportMeshShading()) {
|
2025-03-20 20:15:38 +01:00
|
|
|
command->drawMesh((uint32)vertexData->getNumInstances(), 1, 1);
|
2024-06-09 12:20:04 +02:00
|
|
|
} else {
|
|
|
|
|
const auto& materials = vertexData->getMaterialData();
|
|
|
|
|
for (const auto& materialData : materials) {
|
2024-05-30 21:24:21 +02:00
|
|
|
// material not used for any active meshes, skip
|
|
|
|
|
if (materialData.instances.size() == 0)
|
|
|
|
|
continue;
|
2024-06-09 12:20:04 +02:00
|
|
|
for (const auto& drawCall : materialData.instances) {
|
2024-05-30 21:24:21 +02:00
|
|
|
command->bindIndexBuffer(vertexData->getIndexBuffer());
|
|
|
|
|
uint32 inst = drawCall.offsets.instanceOffset;
|
2024-06-09 12:20:04 +02:00
|
|
|
for (const auto& meshData : drawCall.instanceMeshData) {
|
2024-05-30 21:24:21 +02:00
|
|
|
// all meshlets of a mesh share the same indices offset
|
2024-06-09 12:20:04 +02:00
|
|
|
command->drawIndexed(meshData.numIndices, 1, meshData.firstIndex,
|
|
|
|
|
vertexData->getIndicesOffset(meshData.meshletOffset), inst++);
|
2024-05-30 21:24:21 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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();
|
2024-08-13 22:44:04 +02:00
|
|
|
timestamps->write(Gfx::SE_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, "CachedEnd");
|
2025-03-09 22:42:05 +01:00
|
|
|
query->endQuery();
|
2024-05-30 16:56:22 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void CachedDepthPass::endFrame() {}
|
2024-05-30 16:56:22 +02:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
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 =
|
2024-06-09 12:20:04 +02:00
|
|
|
Gfx::RenderTargetAttachment(depthBuffer, Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
|
2024-05-30 21:24:21 +02:00
|
|
|
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 =
|
2024-06-09 12:20:04 +02:00
|
|
|
Gfx::RenderTargetAttachment(visibilityBuffer, Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
|
2024-05-30 21:24:21 +02:00
|
|
|
Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE);
|
|
|
|
|
resources->registerRenderPassOutput("VISIBILITY", visibilityAttachment);
|
2024-07-01 12:17:04 +02:00
|
|
|
query = graphics->createPipelineStatisticsQuery("CachedPipelineStatistics");
|
2024-06-15 21:47:20 +02:00
|
|
|
resources->registerQueryOutput("CACHED_QUERY", query);
|
2024-07-01 12:17:04 +02:00
|
|
|
|
2024-09-03 11:03:23 +02:00
|
|
|
timestamps = graphics->createTimestampQuery(7, "CachedTS");
|
|
|
|
|
resources->registerTimestampQueryOutput("TIMESTAMPS", timestamps);
|
2024-05-30 16:56:22 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-09 12:20:04 +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,
|
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
|
|
|
};
|
2025-04-04 14:44:53 +02:00
|
|
|
renderPass = graphics->createRenderPass(std::move(layout), std::move(dependency), viewport->getRenderArea(), "CachedDepthPass");
|
2024-05-30 16:56:22 +02:00
|
|
|
}
|