2020-08-06 00:54:43 +02:00
|
|
|
#include "BasePass.h"
|
2024-06-09 12:20:04 +02:00
|
|
|
#include "Actor/CameraActor.h"
|
|
|
|
|
#include "Component/Camera.h"
|
|
|
|
|
#include "Component/Mesh.h"
|
|
|
|
|
#include "Graphics/Command.h"
|
|
|
|
|
#include "Graphics/Descriptor.h"
|
2023-11-06 22:24:40 +01:00
|
|
|
#include "Graphics/Enums.h"
|
2020-08-06 00:54:43 +02:00
|
|
|
#include "Graphics/Graphics.h"
|
2023-11-06 22:24:40 +01:00
|
|
|
#include "Graphics/Initializer.h"
|
2023-11-05 10:36:01 +01:00
|
|
|
#include "Graphics/Shader.h"
|
2024-06-09 12:20:04 +02:00
|
|
|
#include "Graphics/StaticMeshVertexData.h"
|
|
|
|
|
#include "Material/MaterialInstance.h"
|
2021-01-19 15:30:00 +01:00
|
|
|
#include "Math/Vector.h"
|
2021-05-06 17:02:10 +02:00
|
|
|
#include "RenderGraph.h"
|
2024-06-09 12:20:04 +02:00
|
|
|
#include "Window/Window.h"
|
|
|
|
|
|
2020-08-06 00:54:43 +02:00
|
|
|
using namespace Seele;
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
BasePass::BasePass(Gfx::PGraphics graphics, PScene scene) : RenderPass(graphics, scene) {
|
2024-05-09 08:41:46 +02:00
|
|
|
basePassLayout = graphics->createPipelineLayout("BasePassLayout");
|
|
|
|
|
|
|
|
|
|
basePassLayout->addDescriptorLayout(viewParamsLayout);
|
|
|
|
|
basePassLayout->addDescriptorLayout(scene->getLightEnvironment()->getDescriptorLayout());
|
|
|
|
|
|
|
|
|
|
lightCullingLayout = graphics->createDescriptorLayout("pLightCullingData");
|
|
|
|
|
// oLightIndexList
|
2024-06-09 12:20:04 +02:00
|
|
|
lightCullingLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
|
|
|
|
.binding = 0,
|
|
|
|
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
|
|
|
|
|
});
|
2024-05-09 08:41:46 +02:00
|
|
|
// oLightGrid
|
2024-06-09 12:20:04 +02:00
|
|
|
lightCullingLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
|
|
|
|
.binding = 1,
|
|
|
|
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_IMAGE,
|
|
|
|
|
});
|
2024-05-09 08:41:46 +02:00
|
|
|
lightCullingLayout->create();
|
|
|
|
|
|
|
|
|
|
basePassLayout->addDescriptorLayout(lightCullingLayout);
|
2024-05-12 19:36:32 +02:00
|
|
|
basePassLayout->addPushConstants(Gfx::SePushConstantRange{
|
2024-05-22 10:30:45 +02:00
|
|
|
.stageFlags = Gfx::SE_SHADER_STAGE_TASK_BIT_EXT | Gfx::SE_SHADER_STAGE_VERTEX_BIT,
|
2024-05-12 19:36:32 +02:00
|
|
|
.offset = 0,
|
|
|
|
|
.size = sizeof(VertexData::DrawCallOffsets),
|
2024-06-09 12:20:04 +02:00
|
|
|
});
|
2024-05-09 08:41:46 +02:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
if (graphics->supportMeshShading()) {
|
|
|
|
|
graphics->getShaderCompiler()->registerRenderPass("BasePass", Gfx::PassConfig{
|
|
|
|
|
.baseLayout = basePassLayout,
|
|
|
|
|
.taskFile = "DrawListTask",
|
|
|
|
|
.mainFile = "DrawListMesh",
|
|
|
|
|
.fragmentFile = "BasePass",
|
|
|
|
|
.hasFragmentShader = true,
|
|
|
|
|
.useMeshShading = true,
|
|
|
|
|
.hasTaskShader = true,
|
|
|
|
|
.useMaterial = true,
|
|
|
|
|
.useVisibility = false,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
graphics->getShaderCompiler()->registerRenderPass("BasePass", Gfx::PassConfig{
|
|
|
|
|
.baseLayout = basePassLayout,
|
|
|
|
|
.taskFile = "",
|
|
|
|
|
.mainFile = "LegacyPass",
|
|
|
|
|
.fragmentFile = "BasePass",
|
|
|
|
|
.hasFragmentShader = true,
|
|
|
|
|
.useMeshShading = false,
|
|
|
|
|
.hasTaskShader = false,
|
|
|
|
|
.useMaterial = true,
|
|
|
|
|
.useVisibility = false,
|
|
|
|
|
});
|
2024-05-09 08:41:46 +02:00
|
|
|
}
|
2020-08-06 00:54:43 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
BasePass::~BasePass() {}
|
2023-10-24 15:01:09 +02:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void BasePass::beginFrame(const Component::Camera& cam) {
|
2023-11-05 10:36:01 +01:00
|
|
|
RenderPass::beginFrame(cam);
|
2020-10-03 11:00:10 +02:00
|
|
|
|
2023-11-05 10:36:01 +01:00
|
|
|
lightCullingLayout->reset();
|
2024-02-21 10:30:04 +01:00
|
|
|
opaqueCulling = lightCullingLayout->allocateDescriptorSet();
|
|
|
|
|
transparentCulling = lightCullingLayout->allocateDescriptorSet();
|
2020-10-03 11:00:10 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void BasePass::render() {
|
2024-02-21 10:30:04 +01:00
|
|
|
opaqueCulling->updateBuffer(0, oLightIndexList);
|
|
|
|
|
opaqueCulling->updateTexture(1, oLightGrid);
|
|
|
|
|
transparentCulling->updateBuffer(0, tLightIndexList);
|
|
|
|
|
transparentCulling->updateTexture(1, tLightGrid);
|
|
|
|
|
opaqueCulling->writeChanges();
|
|
|
|
|
transparentCulling->writeChanges();
|
2023-01-21 18:43:21 +01:00
|
|
|
|
2023-11-10 22:26:47 +01:00
|
|
|
graphics->beginRenderPass(renderPass);
|
2024-04-11 12:38:42 +02:00
|
|
|
Array<Gfx::ORenderCommand> commands;
|
2023-11-05 10:36:01 +01:00
|
|
|
|
2024-05-31 14:21:32 +02:00
|
|
|
Gfx::ShaderPermutation permutation = graphics->getShaderCompiler()->getTemplate("BasePass");
|
2024-06-11 16:55:20 +02:00
|
|
|
permutation.setDepthCulling(true); // always use the culling info
|
2024-06-09 12:20:04 +02:00
|
|
|
for (VertexData* vertexData : VertexData::getList()) {
|
2024-06-07 09:19:47 +02:00
|
|
|
vertexData->getInstanceDataSet()->updateBuffer(6, cullingBuffer);
|
|
|
|
|
vertexData->getInstanceDataSet()->writeChanges();
|
2024-05-12 19:36:32 +02:00
|
|
|
permutation.setVertexData(vertexData->getTypeName());
|
|
|
|
|
const auto& materials = vertexData->getMaterialData();
|
2024-06-09 12:20:04 +02:00
|
|
|
for (const auto& materialData : materials) {
|
2024-05-12 19:36:32 +02:00
|
|
|
// material not used for any active meshes, skip
|
|
|
|
|
if (materialData.instances.size() == 0)
|
|
|
|
|
continue;
|
|
|
|
|
// Create Pipeline(Material, VertexData)
|
|
|
|
|
// Descriptors:
|
|
|
|
|
// ViewData => global, static
|
|
|
|
|
// VertexData => per meshtype
|
|
|
|
|
// SceneData => per material instance
|
|
|
|
|
// LightEnv => provided by scene
|
|
|
|
|
// Material => per material
|
|
|
|
|
// LightCulling => calculated by pass
|
|
|
|
|
permutation.setMaterial(materialData.material->getName());
|
|
|
|
|
Gfx::PermutationId id(permutation);
|
|
|
|
|
|
|
|
|
|
Gfx::ORenderCommand command = graphics->createRenderCommand("BaseRender");
|
|
|
|
|
command->setViewport(viewport);
|
|
|
|
|
|
|
|
|
|
const Gfx::ShaderCollection* collection = graphics->getShaderCompiler()->findShaders(id);
|
|
|
|
|
assert(collection != nullptr);
|
2024-06-09 12:20:04 +02:00
|
|
|
if (graphics->supportMeshShading()) {
|
2024-05-22 19:20:30 +02:00
|
|
|
Gfx::MeshPipelineCreateInfo pipelineInfo = {
|
|
|
|
|
.taskShader = collection->taskShader,
|
|
|
|
|
.meshShader = collection->meshShader,
|
|
|
|
|
.fragmentShader = collection->fragmentShader,
|
|
|
|
|
.renderPass = renderPass,
|
|
|
|
|
.pipelineLayout = collection->pipelineLayout,
|
2024-06-09 12:20:04 +02:00
|
|
|
.multisampleState =
|
|
|
|
|
{
|
|
|
|
|
.samples = viewport->getSamples(),
|
|
|
|
|
},
|
|
|
|
|
.depthStencilState =
|
|
|
|
|
{
|
|
|
|
|
.depthCompareOp = Gfx::SE_COMPARE_OP_GREATER_OR_EQUAL,
|
|
|
|
|
},
|
|
|
|
|
.colorBlend =
|
|
|
|
|
{
|
|
|
|
|
.attachmentCount = 1,
|
|
|
|
|
},
|
2024-05-22 19:20:30 +02:00
|
|
|
};
|
2024-05-12 19:36:32 +02:00
|
|
|
Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo));
|
|
|
|
|
command->bindPipeline(pipeline);
|
2024-06-09 12:20:04 +02:00
|
|
|
} else {
|
2024-05-22 19:20:30 +02:00
|
|
|
Gfx::LegacyPipelineCreateInfo pipelineInfo = {
|
|
|
|
|
.vertexShader = collection->vertexShader,
|
|
|
|
|
.fragmentShader = collection->fragmentShader,
|
|
|
|
|
.renderPass = renderPass,
|
|
|
|
|
.pipelineLayout = collection->pipelineLayout,
|
2024-06-09 12:20:04 +02:00
|
|
|
.multisampleState =
|
|
|
|
|
{
|
|
|
|
|
.samples = viewport->getSamples(),
|
|
|
|
|
},
|
|
|
|
|
.depthStencilState =
|
|
|
|
|
{
|
|
|
|
|
.depthCompareOp = Gfx::SE_COMPARE_OP_GREATER_OR_EQUAL,
|
|
|
|
|
},
|
|
|
|
|
.colorBlend =
|
|
|
|
|
{
|
|
|
|
|
.attachmentCount = 1,
|
|
|
|
|
},
|
2024-05-22 19:20:30 +02:00
|
|
|
};
|
2024-05-12 19:36:32 +02:00
|
|
|
Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo));
|
|
|
|
|
command->bindPipeline(pipeline);
|
|
|
|
|
}
|
2024-06-09 12:20:04 +02:00
|
|
|
for (const auto& drawCall : materialData.instances) {
|
2024-06-11 14:15:29 +02:00
|
|
|
command->bindDescriptor({viewParamsSet, vertexData->getVertexDataSet(), vertexData->getInstanceDataSet(),
|
|
|
|
|
scene->getLightEnvironment()->getDescriptorSet(), drawCall.materialInstance->getDescriptorSet(),
|
|
|
|
|
opaqueCulling});
|
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), &drawCall.offsets);
|
|
|
|
|
if (graphics->supportMeshShading()) {
|
2024-05-22 10:30:45 +02:00
|
|
|
command->drawMesh(drawCall.instanceMeshData.size(), 1, 1);
|
2024-06-09 12:20:04 +02:00
|
|
|
} else {
|
2024-05-22 10:30:45 +02:00
|
|
|
command->bindIndexBuffer(vertexData->getIndexBuffer());
|
2024-06-09 12:20:04 +02:00
|
|
|
for (const auto& meshData : drawCall.instanceMeshData) {
|
2024-05-22 10:30:45 +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), 0);
|
2024-05-22 10:30:45 +02:00
|
|
|
}
|
2024-05-08 10:51:59 +02:00
|
|
|
}
|
|
|
|
|
}
|
2024-05-12 19:36:32 +02:00
|
|
|
commands.add(std::move(command));
|
2024-05-08 10:51:59 +02:00
|
|
|
}
|
2024-05-12 19:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
2024-04-11 12:38:42 +02:00
|
|
|
graphics->executeCommands(std::move(commands));
|
2020-08-06 00:54:43 +02:00
|
|
|
graphics->endRenderPass();
|
|
|
|
|
}
|
2020-10-03 11:00:10 +02:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void BasePass::endFrame() {}
|
2021-05-06 17:02:10 +02:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void BasePass::publishOutputs() {
|
2024-06-11 14:15:29 +02:00
|
|
|
TextureCreateInfo depthBufferInfo = {
|
|
|
|
|
.format = Gfx::SE_FORMAT_D32_SFLOAT,
|
|
|
|
|
.width = viewport->getOwner()->getFramebufferWidth(),
|
|
|
|
|
.height = viewport->getOwner()->getFramebufferHeight(),
|
|
|
|
|
.samples = viewport->getSamples(),
|
|
|
|
|
.usage = Gfx::SE_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
|
|
|
|
|
};
|
|
|
|
|
basePassDepth = graphics->createTexture2D(depthBufferInfo);
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
colorAttachment = Gfx::RenderTargetAttachment(viewport, Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
|
|
|
|
|
Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE);
|
2023-12-10 22:27:59 +01:00
|
|
|
resources->registerRenderPassOutput("BASEPASS_COLOR", colorAttachment);
|
2024-06-11 14:15:29 +02:00
|
|
|
depthAttachment =
|
|
|
|
|
Gfx::RenderTargetAttachment(basePassDepth, 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("BASEPASS_DEPTH", depthAttachment);
|
2021-05-06 17:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void BasePass::createRenderPass() {
|
2024-06-07 09:19:47 +02:00
|
|
|
cullingBuffer = resources->requestBuffer("CULLINGBUFFER");
|
|
|
|
|
|
2024-01-31 09:49:53 +01:00
|
|
|
Gfx::RenderTargetLayout layout = Gfx::RenderTargetLayout{
|
2024-06-09 12:20:04 +02:00
|
|
|
.colorAttachments = {colorAttachment},
|
2023-12-10 22:27:59 +01:00
|
|
|
.depthAttachment = depthAttachment,
|
|
|
|
|
};
|
2024-01-31 09:49:53 +01:00
|
|
|
Array<Gfx::SubPassDependency> dependency = {
|
|
|
|
|
{
|
|
|
|
|
.srcSubpass = ~0U,
|
|
|
|
|
.dstSubpass = 0,
|
2024-06-07 14:56:42 +02:00
|
|
|
.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-02-01 15:00:13 +01:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
.srcSubpass = 0,
|
|
|
|
|
.dstSubpass = ~0U,
|
2024-06-07 14:56:42 +02:00
|
|
|
.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-02-01 15:00:13 +01:00
|
|
|
},
|
2024-01-31 09:49:53 +01:00
|
|
|
};
|
2024-04-04 08:30:59 +02:00
|
|
|
renderPass = graphics->createRenderPass(std::move(layout), std::move(dependency), viewport);
|
2021-10-01 19:55:04 +02:00
|
|
|
oLightIndexList = resources->requestBuffer("LIGHTCULLING_OLIGHTLIST");
|
2023-11-05 10:36:01 +01:00
|
|
|
tLightIndexList = resources->requestBuffer("LIGHTCULLING_TLIGHTLIST");
|
2021-10-01 19:55:04 +02:00
|
|
|
oLightGrid = resources->requestTexture("LIGHTCULLING_OLIGHTGRID");
|
2023-11-05 10:36:01 +01:00
|
|
|
tLightGrid = resources->requestTexture("LIGHTCULLING_TLIGHTGRID");
|
2021-05-06 17:02:10 +02:00
|
|
|
}
|