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"
|
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);
|
2021-06-04 18:27:49 +02:00
|
|
|
|
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,
|
|
|
|
|
};
|
2021-12-02 13:00:03 +01:00
|
|
|
oLightIndexCounter->updateContents(counterReset);
|
|
|
|
|
tLightIndexCounter->updateContents(counterReset);
|
2021-05-06 17:02:10 +02:00
|
|
|
|
2021-12-02 13:00:03 +01:00
|
|
|
cullingDescriptorLayout->reset();
|
|
|
|
|
cullingDescriptorSet = cullingDescriptorLayout->allocateDescriptorSet();
|
2021-06-04 18:27:49 +02:00
|
|
|
|
2023-11-05 10:36:01 +01:00
|
|
|
cullingDescriptorSet->updateBuffer(0, dispatchParamsBuffer);
|
|
|
|
|
cullingDescriptorSet->updateTexture(1, depthAttachment);
|
|
|
|
|
cullingDescriptorSet->updateBuffer(2, oLightIndexCounter);
|
|
|
|
|
cullingDescriptorSet->updateBuffer(3, tLightIndexCounter);
|
|
|
|
|
cullingDescriptorSet->updateBuffer(4, oLightIndexList);
|
|
|
|
|
cullingDescriptorSet->updateBuffer(5, tLightIndexList);
|
|
|
|
|
cullingDescriptorSet->updateTexture(6, Gfx::PTexture2D(oLightGrid));
|
|
|
|
|
cullingDescriptorSet->updateTexture(7, Gfx::PTexture2D(tLightGrid));
|
|
|
|
|
cullingDescriptorSet->updateBuffer(8, frustumBuffer);
|
2022-02-24 22:38:26 +01:00
|
|
|
//std::cout << "LightCulling beginFrame()" << std::endl;
|
2022-04-15 23:45:44 +02:00
|
|
|
//co_return;
|
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
|
|
|
oLightIndexList->pipelineBarrier(
|
|
|
|
|
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
|
|
|
|
|
Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
|
|
|
|
|
oLightGrid->pipelineBarrier(
|
|
|
|
|
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
|
|
|
|
|
Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
|
|
|
|
|
depthAttachment->pipelineBarrier(
|
|
|
|
|
Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
|
|
|
|
|
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
|
|
|
|
|
depthAttachment->changeLayout(Gfx::SE_IMAGE_LAYOUT_GENERAL);
|
|
|
|
|
depthAttachment->transferOwnership(Gfx::QueueType::COMPUTE);
|
|
|
|
|
cullingDescriptorSet->updateTexture(2, depthAttachment);
|
|
|
|
|
cullingDescriptorSet->writeChanges();
|
|
|
|
|
Gfx::PComputeCommand computeCommand = graphics->createComputeCommand("CullingCommand");
|
|
|
|
|
computeCommand->bindPipeline(cullingPipeline);
|
2023-11-05 10:36:01 +01:00
|
|
|
computeCommand->bindDescriptor({ viewParamsSet, lightEnv->getDescriptorSet(), cullingDescriptorSet });
|
|
|
|
|
computeCommand->dispatch(dispatchParams.numThreadGroups.x, dispatchParams.numThreadGroups.y, dispatchParams.numThreadGroups.z);
|
2021-12-02 13:00:03 +01:00
|
|
|
Array<Gfx::PComputeCommand> commands = {computeCommand};
|
|
|
|
|
graphics->executeCommands(commands);
|
2021-05-10 23:57:55 +02:00
|
|
|
depthAttachment->changeLayout(Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
|
2021-12-02 13:00:03 +01:00
|
|
|
depthAttachment->transferOwnership(Gfx::QueueType::GRAPHICS);
|
2022-02-24 22:38:26 +01:00
|
|
|
//std::cout << "LightCulling render()" << std::endl;
|
2022-04-15 23:45:44 +02:00
|
|
|
//co_return;
|
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
|
|
|
{
|
2022-02-24 22:38:26 +01:00
|
|
|
//std::cout << "LightCulling endFrame()" << std::endl;
|
2022-04-15 23:45:44 +02:00
|
|
|
//co_return;
|
2021-05-06 17:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LightCullingPass::publishOutputs()
|
|
|
|
|
{
|
2021-12-02 13:00:03 +01:00
|
|
|
setupFrustums();
|
2021-06-04 18:27:49 +02:00
|
|
|
uint32_t viewportWidth = viewport->getSizeX();
|
2021-12-02 13:00:03 +01:00
|
|
|
uint32_t viewportHeight = viewport->getSizeY();
|
|
|
|
|
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,
|
|
|
|
|
},
|
|
|
|
|
.dynamic = false,
|
|
|
|
|
});
|
2021-06-04 18:27:49 +02:00
|
|
|
|
2021-12-02 13:00:03 +01:00
|
|
|
cullingDescriptorLayout = graphics->createDescriptorLayout("CullingLayout");
|
|
|
|
|
|
2023-11-05 10:36:01 +01:00
|
|
|
// Dispatchparams
|
2021-12-02 13:00:03 +01:00
|
|
|
cullingDescriptorLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
|
|
|
|
|
//DepthTexture
|
2023-11-05 10:36:01 +01:00
|
|
|
cullingDescriptorLayout->addDescriptorBinding(1, Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
|
2021-12-02 13:00:03 +01:00
|
|
|
//o_lightIndexCounter
|
2023-11-05 10:36:01 +01:00
|
|
|
cullingDescriptorLayout->addDescriptorBinding(2, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
|
2021-12-02 13:00:03 +01:00
|
|
|
//t_lightIndexCounter
|
2023-11-05 10:36:01 +01:00
|
|
|
cullingDescriptorLayout->addDescriptorBinding(3, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
|
2021-12-02 13:00:03 +01:00
|
|
|
//o_lightIndexList
|
2023-11-05 10:36:01 +01:00
|
|
|
cullingDescriptorLayout->addDescriptorBinding(4, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
|
2021-12-02 13:00:03 +01:00
|
|
|
//t_lightIndexList
|
2023-11-05 10:36:01 +01:00
|
|
|
cullingDescriptorLayout->addDescriptorBinding(5, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
|
2021-12-02 13:00:03 +01:00
|
|
|
//o_lightGrid
|
2023-11-05 10:36:01 +01:00
|
|
|
cullingDescriptorLayout->addDescriptorBinding(6, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_IMAGE);
|
2021-12-02 13:00:03 +01:00
|
|
|
//t_lightGrid
|
2023-11-05 10:36:01 +01:00
|
|
|
cullingDescriptorLayout->addDescriptorBinding(7, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_IMAGE);
|
2022-04-15 23:45:44 +02:00
|
|
|
//Frustums
|
2023-11-05 10:36:01 +01:00
|
|
|
cullingDescriptorLayout->addDescriptorBinding(8, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, viewportWidth * viewportHeight);
|
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);
|
|
|
|
|
cullingLayout->addDescriptorLayout(1, lightEnv->getDescriptorLayout());
|
|
|
|
|
cullingLayout->addDescriptorLayout(2, cullingDescriptorLayout);
|
2021-12-02 13:00:03 +01:00
|
|
|
cullingLayout->create();
|
|
|
|
|
|
|
|
|
|
ShaderCreateInfo createInfo;
|
|
|
|
|
createInfo.name = "Culling";
|
|
|
|
|
|
2022-11-15 12:19:11 +01:00
|
|
|
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-08-28 21:23:13 +02:00
|
|
|
ShaderBufferCreateInfo structInfo =
|
2022-04-15 23:45:44 +02:00
|
|
|
{
|
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,
|
|
|
|
|
},
|
|
|
|
|
.stride = sizeof(uint32),
|
2023-11-05 10:36:01 +01:00
|
|
|
.dynamic = true,
|
2022-04-15 23:45:44 +02:00
|
|
|
};
|
2023-08-28 21:23:13 +02:00
|
|
|
oLightIndexCounter = graphics->createShaderBuffer(structInfo);
|
|
|
|
|
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
|
|
|
|
|
* dispatchParams.numThreadGroups.z * 200,
|
|
|
|
|
.data = nullptr,
|
|
|
|
|
.owner = Gfx::QueueType::COMPUTE
|
|
|
|
|
},
|
|
|
|
|
.stride = sizeof(uint32),
|
2023-11-05 10:36:01 +01:00
|
|
|
.dynamic = false,
|
2022-04-15 23:45:44 +02:00
|
|
|
};
|
2023-08-28 21:23:13 +02:00
|
|
|
oLightIndexList = graphics->createShaderBuffer(structInfo);
|
|
|
|
|
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 = {
|
|
|
|
|
.width = dispatchParams.numThreadGroups.x,
|
|
|
|
|
.height = dispatchParams.numThreadGroups.y,
|
|
|
|
|
.format = Gfx::SE_FORMAT_R32G32_UINT,
|
|
|
|
|
.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()
|
|
|
|
|
{
|
2021-12-02 13:00:03 +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()
|
|
|
|
|
{
|
|
|
|
|
uint32_t viewportWidth = viewport->getSizeX();
|
2021-12-02 13:00:03 +01:00
|
|
|
uint32_t viewportHeight = viewport->getSizeY();
|
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-08 23:27:21 +01:00
|
|
|
frustumDescriptorLayout = graphics->createDescriptorLayout("FrustumLayout");
|
2021-12-02 13:00:03 +01:00
|
|
|
frustumDescriptorLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
|
2023-11-05 10:36:01 +01:00
|
|
|
frustumDescriptorLayout->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);
|
|
|
|
|
frustumLayout->addDescriptorLayout(1, frustumDescriptorLayout);
|
2021-12-02 13:00:03 +01:00
|
|
|
frustumLayout->create();
|
|
|
|
|
ShaderCreateInfo createInfo;
|
|
|
|
|
createInfo.name = "Frustum";
|
|
|
|
|
|
2022-11-15 12:19:11 +01:00
|
|
|
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,
|
|
|
|
|
},
|
|
|
|
|
.dynamic = false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
},
|
|
|
|
|
.stride = sizeof(Frustum),
|
2023-11-05 10:36:01 +01:00
|
|
|
.dynamic = false,
|
|
|
|
|
});
|
2021-12-02 13:00:03 +01:00
|
|
|
|
|
|
|
|
frustumDescriptorSet = frustumDescriptorLayout->allocateDescriptorSet();
|
2023-11-05 10:36:01 +01:00
|
|
|
frustumDescriptorSet->updateBuffer(0, frustumDispatchParamsBuffer);
|
|
|
|
|
frustumDescriptorSet->updateBuffer(1, frustumBuffer);
|
2021-12-02 13:00:03 +01:00
|
|
|
frustumDescriptorSet->writeChanges();
|
|
|
|
|
|
|
|
|
|
Gfx::PComputeCommand command = graphics->createComputeCommand("FrustumCommand");
|
|
|
|
|
command->bindPipeline(frustumPipeline);
|
2023-11-05 10:36:01 +01:00
|
|
|
command->bindDescriptor({ viewParamsSet, frustumDescriptorSet });
|
2021-12-02 13:00:03 +01:00
|
|
|
command->dispatch(numThreadGroups.x, numThreadGroups.y, numThreadGroups.z);
|
|
|
|
|
Array<Gfx::PComputeCommand> commands = {command};
|
|
|
|
|
graphics->executeCommands(commands);
|
|
|
|
|
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
|
|
|
}
|