Files
Seele/src/Engine/Graphics/RenderPass/LightCullingPass.cpp
T

300 lines
13 KiB
C++
Raw Normal View History

2021-05-06 17:02:10 +02:00
#include "LightCullingPass.h"
#include "Graphics/Graphics.h"
2021-05-10 23:57:55 +02:00
#include "Scene/Scene.h"
2022-11-17 16:47:42 +01:00
#include "Actor/CameraActor.h"
#include "Component/Camera.h"
2021-05-10 23:57:55 +02:00
#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;
2024-05-29 10:40:35 +02:00
extern bool useLightCulling;
2023-10-26 18:37:29 +02:00
LightCullingPass::LightCullingPass(Gfx::PGraphics graphics, PScene scene)
: RenderPass(graphics, scene)
2021-05-06 17:02:10 +02:00
{
}
2024-05-15 15:27:13 +02:00
LightCullingPass::~LightCullingPass()
2021-05-06 17:02:10 +02:00
{
2024-05-15 15:27:13 +02:00
2021-05-06 17:02:10 +02:00
}
2024-05-15 15:27:13 +02:00
void LightCullingPass::beginFrame(const Component::Camera& cam)
2021-05-06 17:02:10 +02:00
{
2023-11-05 10:36:01 +01:00
RenderPass::beginFrame(cam);
oLightIndexCounter->pipelineBarrier(
Gfx::SE_ACCESS_MEMORY_WRITE_BIT | Gfx::SE_ACCESS_MEMORY_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
Gfx::SE_ACCESS_MEMORY_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT
);
tLightIndexCounter->pipelineBarrier(
Gfx::SE_ACCESS_MEMORY_WRITE_BIT | Gfx::SE_ACCESS_MEMORY_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
Gfx::SE_ACCESS_MEMORY_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT
);
2021-12-02 13:00:03 +01:00
uint32 reset = 0;
2024-05-15 15:27:13 +02:00
ShaderBufferCreateInfo counterReset = {
.sourceData = {
.size = sizeof(uint32),
.data = (uint8*)&reset,
.owner = Gfx::QueueType::COMPUTE
}
2023-11-05 10:36:01 +01:00
};
2021-12-02 13:00:03 +01:00
oLightIndexCounter->updateContents(counterReset);
tLightIndexCounter->updateContents(counterReset);
2024-02-01 08:42:24 +01:00
oLightIndexCounter->pipelineBarrier(
Gfx::SE_ACCESS_MEMORY_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
Gfx::SE_ACCESS_MEMORY_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
tLightIndexCounter->pipelineBarrier(
Gfx::SE_ACCESS_MEMORY_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
Gfx::SE_ACCESS_MEMORY_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
2021-05-06 17:02:10 +02:00
2021-12-02 13:00:03 +01:00
cullingDescriptorLayout->reset();
cullingDescriptorSet = cullingDescriptorLayout->allocateDescriptorSet();
2021-05-06 17:02:10 +02:00
}
2024-05-15 15:27:13 +02:00
void LightCullingPass::render()
2021-05-06 17:02:10 +02:00
{
2021-12-02 13:00:03 +01:00
depthAttachment->transferOwnership(Gfx::QueueType::COMPUTE);
2023-11-11 13:56:12 +01:00
cullingDescriptorSet->updateTexture(0, depthAttachment);
2024-01-31 10:11:37 +01:00
cullingDescriptorSet->updateBuffer(1, oLightIndexCounter);
cullingDescriptorSet->updateBuffer(2, tLightIndexCounter);
cullingDescriptorSet->updateBuffer(3, oLightIndexList);
cullingDescriptorSet->updateBuffer(4, tLightIndexList);
cullingDescriptorSet->updateTexture(5, Gfx::PTexture2D(oLightGrid));
cullingDescriptorSet->updateTexture(6, Gfx::PTexture2D(tLightGrid));
2021-12-02 13:00:03 +01:00
cullingDescriptorSet->writeChanges();
2024-04-11 12:38:42 +02:00
Gfx::OComputeCommand computeCommand = graphics->createComputeCommand("CullingCommand");
2024-05-29 10:40:35 +02:00
if (useLightCulling)
{
computeCommand->bindPipeline(cullingEnabledPipeline);
}
else
{
computeCommand->bindPipeline(cullingPipeline);
}
2023-11-11 13:56:12 +01:00
computeCommand->bindDescriptor({ viewParamsSet, dispatchParamsSet, cullingDescriptorSet, lightEnv->getDescriptorSet() });
computeCommand->dispatch(dispatchParams.numThreadGroups.x, dispatchParams.numThreadGroups.y, dispatchParams.numThreadGroups.z);
2024-04-11 12:38:42 +02:00
Array<Gfx::OComputeCommand> commands;
commands.add(std::move(computeCommand));
2024-02-01 08:42:24 +01:00
//std::cout << "Execute" << std::endl;
2024-04-11 12:38:42 +02:00
graphics->executeCommands(std::move(commands));
2021-05-06 17:02:10 +02:00
}
2024-05-15 15:27:13 +02:00
void LightCullingPass::endFrame()
2021-05-06 17:02:10 +02:00
{
}
2024-05-15 15:27:13 +02:00
void LightCullingPass::publishOutputs()
2021-05-06 17:02:10 +02:00
{
2021-12-02 13:00:03 +01:00
setupFrustums();
2023-11-15 00:06:00 +01:00
uint32_t viewportWidth = viewport->getWidth();
uint32_t viewportHeight = viewport->getHeight();
2021-12-02 13:00:03 +01:00
glm::uvec3 numThreadGroups = glm::ceil(glm::vec3(viewportWidth / (float)BLOCK_SIZE, viewportHeight / (float)BLOCK_SIZE, 1));
dispatchParams.numThreadGroups = numThreadGroups;
dispatchParams.numThreads = numThreadGroups * glm::uvec3(BLOCK_SIZE, BLOCK_SIZE, 1);
2023-11-05 10:36:01 +01:00
dispatchParamsBuffer = graphics->createUniformBuffer(UniformBufferCreateInfo{
.sourceData = {
.size = sizeof(DispatchParams),
.data = (uint8*)&dispatchParams,
2024-02-20 21:07:17 +01:00
.owner = Gfx::QueueType::COMPUTE
2023-11-05 10:36:01 +01:00
},
.dynamic = false,
2024-02-20 21:07:17 +01:00
.name = "DispatchParams",
2023-11-05 10:36:01 +01:00
});
2024-02-20 21:07:17 +01:00
dispatchParamsSet = dispatchParamsLayout->allocateDescriptorSet();
dispatchParamsSet->updateBuffer(0, dispatchParamsBuffer);
dispatchParamsSet->updateBuffer(1, frustumBuffer);
dispatchParamsSet->writeChanges();
2024-04-19 18:23:36 +02:00
cullingDescriptorLayout = graphics->createDescriptorLayout("pCullingParams");
2023-11-11 13:56:12 +01:00
2021-12-02 13:00:03 +01:00
//DepthTexture
2024-05-15 15:27:13 +02:00
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 0, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE, });
2021-12-02 13:00:03 +01:00
//o_lightIndexCounter
2024-05-15 15:27:13 +02:00
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 1, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, .access = Gfx::SE_DESCRIPTOR_ACCESS_READ_WRITE_BIT });
2021-12-02 13:00:03 +01:00
//t_lightIndexCounter
2024-05-15 15:27:13 +02:00
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 2, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, .access = Gfx::SE_DESCRIPTOR_ACCESS_READ_WRITE_BIT });
2021-12-02 13:00:03 +01:00
//o_lightIndexList
2024-05-15 15:27:13 +02:00
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 3, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, .access = Gfx::SE_DESCRIPTOR_ACCESS_READ_WRITE_BIT });
2021-12-02 13:00:03 +01:00
//t_lightIndexList
2024-05-15 15:27:13 +02:00
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 4, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, .access = Gfx::SE_DESCRIPTOR_ACCESS_READ_WRITE_BIT });
2021-12-02 13:00:03 +01:00
//o_lightGrid
2024-05-15 15:27:13 +02:00
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 5, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_IMAGE, .access = Gfx::SE_DESCRIPTOR_ACCESS_READ_WRITE_BIT });
2021-12-02 13:00:03 +01:00
//t_lightGrid
2024-05-15 15:27:13 +02:00
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 6, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_IMAGE, .access = Gfx::SE_DESCRIPTOR_ACCESS_READ_WRITE_BIT });
2021-05-10 23:57:55 +02:00
2024-04-20 21:35:43 +02:00
cullingDescriptorLayout->create();
2024-05-15 15:27:13 +02:00
2023-11-05 10:36:01 +01:00
lightEnv = scene->getLightEnvironment();
2021-05-10 23:57:55 +02:00
2024-05-29 10:40:35 +02:00
{
cullingLayout = graphics->createPipelineLayout("CullingLayout");
cullingLayout->addDescriptorLayout(viewParamsLayout);
cullingLayout->addDescriptorLayout(dispatchParamsLayout);
cullingLayout->addDescriptorLayout(cullingDescriptorLayout);
cullingLayout->addDescriptorLayout(lightEnv->getDescriptorLayout());
2024-05-15 15:27:13 +02:00
2024-05-29 10:40:35 +02:00
ShaderCreateInfo createInfo = {
.name = "Culling",
.mainModule = "LightCulling",
.entryPoint = "cullLights",
.rootSignature = cullingLayout,
};
cullingShader = graphics->createComputeShader(createInfo);
cullingLayout->create();
2021-05-10 23:57:55 +02:00
2024-05-29 10:40:35 +02:00
Gfx::ComputePipelineCreateInfo pipelineInfo;
pipelineInfo.computeShader = cullingShader;
pipelineInfo.pipelineLayout = std::move(cullingLayout);
cullingPipeline = graphics->createComputePipeline(std::move(pipelineInfo));
}
2022-04-15 23:45:44 +02:00
2024-05-29 10:40:35 +02:00
{
cullingEnableLayout = graphics->createPipelineLayout("CullingEnabledLayout");
cullingEnableLayout->addDescriptorLayout(viewParamsLayout);
cullingEnableLayout->addDescriptorLayout(dispatchParamsLayout);
cullingEnableLayout->addDescriptorLayout(cullingDescriptorLayout);
cullingEnableLayout->addDescriptorLayout(lightEnv->getDescriptorLayout());
ShaderCreateInfo createInfo = {
.name = "Culling",
.mainModule = "LightCulling",
.entryPoint = "cullLights",
.rootSignature = cullingEnableLayout,
};
createInfo.defines["LIGHT_CULLING"] = "1";
cullingEnabledShader = graphics->createComputeShader(createInfo);
cullingEnableLayout->create();
Gfx::ComputePipelineCreateInfo pipelineInfo;
pipelineInfo.computeShader = cullingShader;
pipelineInfo.pipelineLayout = std::move(cullingEnableLayout);
cullingEnabledPipeline = graphics->createComputePipeline(std::move(pipelineInfo));
}
2022-04-15 23:45:44 +02:00
uint32 counterReset = 0;
2023-11-15 17:42:57 +01:00
ShaderBufferCreateInfo structInfo = {
2023-11-01 23:12:30 +01:00
.sourceData = {
2022-04-15 23:45:44 +02:00
.size = sizeof(uint32),
.data = (uint8*)&counterReset,
.owner = Gfx::QueueType::COMPUTE,
},
2023-11-15 17:42:57 +01:00
.numElements = 1,
2023-11-05 10:36:01 +01:00
.dynamic = true,
2024-02-01 08:42:24 +01:00
.name = "oLightIndexCounter",
2022-04-15 23:45:44 +02:00
};
2023-08-28 21:23:13 +02:00
oLightIndexCounter = graphics->createShaderBuffer(structInfo);
2024-02-01 08:42:24 +01:00
structInfo.name = "tLightIndexCounter";
2023-08-28 21:23:13 +02:00
tLightIndexCounter = graphics->createShaderBuffer(structInfo);
2022-04-15 23:45:44 +02:00
structInfo = {
2023-11-01 23:12:30 +01:00
.sourceData = {
2024-05-15 15:27:13 +02:00
.size = (uint32)sizeof(uint32)
* dispatchParams.numThreadGroups.x
* dispatchParams.numThreadGroups.y
2024-04-04 08:30:59 +02:00
* dispatchParams.numThreadGroups.z * 8192,
2022-04-15 23:45:44 +02:00
.data = nullptr,
.owner = Gfx::QueueType::COMPUTE
},
2023-11-05 10:36:01 +01:00
.dynamic = false,
2024-02-01 08:42:24 +01:00
.name = "oLightIndexList",
2022-04-15 23:45:44 +02:00
};
2023-08-28 21:23:13 +02:00
oLightIndexList = graphics->createShaderBuffer(structInfo);
2024-02-01 08:42:24 +01:00
structInfo.name = "tLightIndexList";
2023-08-28 21:23:13 +02:00
tLightIndexList = graphics->createShaderBuffer(structInfo);
2022-04-15 23:45:44 +02:00
resources->registerBufferOutput("LIGHTCULLING_OLIGHTLIST", oLightIndexList);
resources->registerBufferOutput("LIGHTCULLING_TLIGHTLIST", tLightIndexList);
2024-05-15 15:27:13 +02:00
2022-04-15 23:45:44 +02:00
TextureCreateInfo textureInfo = {
2023-11-15 17:42:57 +01:00
.format = Gfx::SE_FORMAT_R32G32_UINT,
2022-04-15 23:45:44 +02:00
.width = dispatchParams.numThreadGroups.x,
.height = dispatchParams.numThreadGroups.y,
.usage = Gfx::SE_IMAGE_USAGE_STORAGE_BIT,
};
oLightGrid = graphics->createTexture2D(textureInfo);
tLightGrid = graphics->createTexture2D(textureInfo);
2024-05-15 15:27:13 +02:00
2023-11-05 10:36:01 +01:00
resources->registerTextureOutput("LIGHTCULLING_OLIGHTGRID", Gfx::PTexture2D(oLightGrid));
resources->registerTextureOutput("LIGHTCULLING_TLIGHTGRID", Gfx::PTexture2D(tLightGrid));
2022-04-15 23:45:44 +02:00
}
void LightCullingPass::createRenderPass()
{
2024-02-01 10:21:36 +01:00
depthAttachment = resources->requestRenderTarget("DEPTHPREPASS_DEPTH").getTexture();
2021-05-06 17:02:10 +02:00
}
2021-05-10 23:57:55 +02:00
void LightCullingPass::setupFrustums()
{
2023-11-15 00:06:00 +01:00
uint32_t viewportWidth = viewport->getWidth();
uint32_t viewportHeight = viewport->getHeight();
2021-05-10 23:57:55 +02:00
2021-12-02 13:00:03 +01:00
glm::uvec3 numThreads = glm::ceil(glm::vec3(viewportWidth / (float)BLOCK_SIZE, viewportHeight / (float)BLOCK_SIZE, 1));
glm::uvec3 numThreadGroups = glm::ceil(glm::vec3(numThreads.x / (float)BLOCK_SIZE, numThreads.y / (float)BLOCK_SIZE, 1));
2024-05-15 15:27:13 +02:00
2023-11-05 10:36:01 +01:00
RenderPass::beginFrame(Component::Camera());
2021-12-02 13:00:03 +01:00
dispatchParams.numThreads = numThreads;
dispatchParams.numThreadGroups = numThreadGroups;
2021-05-10 23:57:55 +02:00
2024-04-19 18:23:36 +02:00
dispatchParamsLayout = graphics->createDescriptorLayout("pDispatchParams");
2024-05-15 15:27:13 +02:00
dispatchParamsLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 0, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER, });
dispatchParamsLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 1, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, .access = Gfx::SE_DESCRIPTOR_ACCESS_WRITE_ONLY_BIT });
2024-04-20 21:35:43 +02:00
frustumLayout = graphics->createPipelineLayout("FrustumLayout");
2024-04-19 18:23:36 +02:00
frustumLayout->addDescriptorLayout(viewParamsLayout);
frustumLayout->addDescriptorLayout(dispatchParamsLayout);
ShaderCreateInfo createInfo = {
.name = "Frustum",
.mainModule = "ComputeFrustums",
.entryPoint = "computeFrustums",
.rootSignature = frustumLayout,
};
std::cout << "Compiling frustumShader" << std::endl;
2021-12-02 13:00:03 +01:00
frustumShader = graphics->createComputeShader(createInfo);
2024-04-23 19:11:06 +02:00
// Have to compile shader before finalizing layout as parameters get mapped later
frustumLayout->create();
dispatchParamsLayout->create();
2021-05-10 23:57:55 +02:00
2023-10-26 18:37:29 +02:00
Gfx::ComputePipelineCreateInfo pipelineInfo;
2021-12-02 13:00:03 +01:00
pipelineInfo.computeShader = frustumShader;
2024-04-19 22:44:00 +02:00
pipelineInfo.pipelineLayout = frustumLayout;
frustumPipeline = graphics->createComputePipeline(pipelineInfo);
2024-05-15 15:27:13 +02:00
2023-11-05 10:36:01 +01:00
Gfx::OUniformBuffer frustumDispatchParamsBuffer = graphics->createUniformBuffer(UniformBufferCreateInfo{
.sourceData = {
.size = sizeof(DispatchParams),
2024-05-15 15:27:13 +02:00
.data = (uint8*)&dispatchParams,
2024-02-20 21:07:17 +01:00
.owner = Gfx::QueueType::COMPUTE
2023-11-05 10:36:01 +01:00
},
.dynamic = false,
2024-02-20 21:07:17 +01:00
.name = "FrustumDispatch"
2024-05-15 15:27:13 +02:00
});
2023-11-05 10:36:01 +01:00
frustumBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
2023-11-01 23:12:30 +01:00
.sourceData = {
2022-04-15 23:45:44 +02:00
.size = sizeof(Frustum) * numThreads.x * numThreads.y * numThreads.z,
.data = nullptr,
2024-02-20 21:07:17 +01:00
.owner = Gfx::QueueType::COMPUTE
2022-04-15 23:45:44 +02:00
},
2023-11-15 17:42:57 +01:00
.numElements = numThreads.x * numThreads.y * numThreads.z,
2023-11-05 10:36:01 +01:00
.dynamic = false,
2024-02-20 21:07:17 +01:00
.name = "FrustumBuffer"
2024-05-15 15:27:13 +02:00
});
2024-02-20 21:07:17 +01:00
Gfx::PDescriptorSet dispatchParamsSet = dispatchParamsLayout->allocateDescriptorSet();
2023-11-11 13:56:12 +01:00
dispatchParamsSet->updateBuffer(0, frustumDispatchParamsBuffer);
dispatchParamsSet->updateBuffer(1, frustumBuffer);
dispatchParamsSet->writeChanges();
2024-05-15 15:27:13 +02:00
2024-04-11 12:38:42 +02:00
Gfx::OComputeCommand command = graphics->createComputeCommand("FrustumCommand");
2021-12-02 13:00:03 +01:00
command->bindPipeline(frustumPipeline);
2023-11-11 13:56:12 +01:00
command->bindDescriptor({ viewParamsSet, dispatchParamsSet });
2021-12-02 13:00:03 +01:00
command->dispatch(numThreadGroups.x, numThreadGroups.y, numThreadGroups.z);
2024-04-11 12:38:42 +02:00
Array<Gfx::OComputeCommand> commands;
commands.add(std::move(command));
graphics->executeCommands(std::move(commands));
2024-05-15 15:27:13 +02:00
frustumBuffer->pipelineBarrier(Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
2021-12-02 13:00:03 +01:00
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
2024-04-19 18:23:36 +02:00
}