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

259 lines
11 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;
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
{
}
LightCullingPass::~LightCullingPass()
{
}
2022-11-17 16:47:42 +01: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;
2023-11-05 10:36:01 +01:00
DataSource counterReset = {
.size = sizeof(uint32),
.data = (uint8*)&reset,
2024-02-20 21:07:17 +01:00
.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
}
2022-04-15 23:45:44 +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");
2021-12-02 13:00:03 +01:00
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
}
2022-04-15 23:45:44 +02:00
void LightCullingPass::endFrame()
2021-05-06 17:02:10 +02:00
{
}
void LightCullingPass::publishOutputs()
{
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();
2021-12-02 13:00:03 +01:00
cullingDescriptorLayout = graphics->createDescriptorLayout("CullingLayout");
2023-11-11 13:56:12 +01:00
2021-12-02 13:00:03 +01:00
//DepthTexture
2023-11-11 13:56:12 +01:00
cullingDescriptorLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
2021-12-02 13:00:03 +01:00
//o_lightIndexCounter
2023-11-11 13:56:12 +01:00
cullingDescriptorLayout->addDescriptorBinding(1, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
2021-12-02 13:00:03 +01:00
//t_lightIndexCounter
2023-11-11 13:56:12 +01:00
cullingDescriptorLayout->addDescriptorBinding(2, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
2021-12-02 13:00:03 +01:00
//o_lightIndexList
2023-11-11 13:56:12 +01:00
cullingDescriptorLayout->addDescriptorBinding(3, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
2021-12-02 13:00:03 +01:00
//t_lightIndexList
2023-11-11 13:56:12 +01:00
cullingDescriptorLayout->addDescriptorBinding(4, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
2021-12-02 13:00:03 +01:00
//o_lightGrid
2023-11-11 13:56:12 +01:00
cullingDescriptorLayout->addDescriptorBinding(5, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_IMAGE);
2021-12-02 13:00:03 +01:00
//t_lightGrid
2023-11-11 13:56:12 +01:00
cullingDescriptorLayout->addDescriptorBinding(6, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_IMAGE);
2021-05-10 23:57:55 +02:00
2023-11-05 10:36:01 +01:00
lightEnv = scene->getLightEnvironment();
2021-05-10 23:57:55 +02:00
2023-11-09 22:15:51 +01:00
Gfx::OPipelineLayout cullingLayout = graphics->createPipelineLayout();
2023-11-05 10:36:01 +01:00
cullingLayout->addDescriptorLayout(0, viewParamsLayout);
2023-11-11 13:56:12 +01:00
cullingLayout->addDescriptorLayout(1, dispatchParamsLayout);
2023-11-05 10:36:01 +01:00
cullingLayout->addDescriptorLayout(2, cullingDescriptorLayout);
2023-11-11 13:56:12 +01:00
cullingLayout->addDescriptorLayout(3, lightEnv->getDescriptorLayout());
2021-12-02 13:00:03 +01:00
cullingLayout->create();
ShaderCreateInfo createInfo;
createInfo.name = "Culling";
2023-11-10 22:26:47 +01:00
createInfo.additionalModules.add("LightCulling");
createInfo.mainModule = "LightCulling";
2021-12-02 13:00:03 +01:00
createInfo.entryPoint = "cullLights";
cullingShader = graphics->createComputeShader(createInfo);
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 = cullingShader;
2023-11-09 22:15:51 +01:00
pipelineInfo.pipelineLayout = std::move(cullingLayout);
cullingPipeline = 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 = {
2022-04-15 23:45:44 +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);
2021-12-02 13:00:03 +01: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);
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-10-15 23:12:29 +02:00
void LightCullingPass::modifyRenderPassMacros(Map<const char*, const char*>&)
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));
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
2023-11-11 13:56:12 +01:00
dispatchParamsLayout = graphics->createDescriptorLayout("FrustumLayout");
dispatchParamsLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
dispatchParamsLayout->addDescriptorBinding(1, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
2023-11-09 22:15:51 +01:00
Gfx::OPipelineLayout frustumLayout = graphics->createPipelineLayout();
2023-11-05 10:36:01 +01:00
frustumLayout->addDescriptorLayout(0, viewParamsLayout);
2023-11-11 13:56:12 +01:00
frustumLayout->addDescriptorLayout(1, dispatchParamsLayout);
2021-12-02 13:00:03 +01:00
frustumLayout->create();
ShaderCreateInfo createInfo;
createInfo.name = "Frustum";
2023-11-10 22:26:47 +01:00
createInfo.additionalModules.add("ComputeFrustums");
createInfo.mainModule = "ComputeFrustums";
2021-12-02 13:00:03 +01:00
createInfo.entryPoint = "computeFrustums";
frustumShader = graphics->createComputeShader(createInfo);
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;
2023-11-09 22:15:51 +01:00
pipelineInfo.pipelineLayout = std::move(frustumLayout);
frustumPipeline = graphics->createComputePipeline(std::move(pipelineInfo));
2021-12-02 13:00:03 +01:00
2023-11-05 10:36:01 +01:00
Gfx::OUniformBuffer frustumDispatchParamsBuffer = 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 = "FrustumDispatch"
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"
2023-11-05 10:36:01 +01:00
});
2021-12-02 13:00:03 +01: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();
2021-12-02 13:00:03 +01: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));
2021-12-02 13:00:03 +01:00
frustumBuffer->pipelineBarrier(Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
2021-05-06 17:02:10 +02:00
}