2021-05-06 17:02:10 +02:00
|
|
|
#include "LightCullingPass.h"
|
2022-11-17 16:47:42 +01:00
|
|
|
#include "Actor/CameraActor.h"
|
|
|
|
|
#include "Component/Camera.h"
|
2023-11-15 17:42:57 +01:00
|
|
|
#include "Graphics/Command.h"
|
2024-06-09 12:20:04 +02:00
|
|
|
#include "Graphics/Graphics.h"
|
|
|
|
|
#include "RenderGraph.h"
|
|
|
|
|
#include "Scene/Scene.h"
|
|
|
|
|
|
2021-05-06 17:02:10 +02:00
|
|
|
using namespace Seele;
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
LightCullingPass::LightCullingPass(Gfx::PGraphics graphics, PScene scene) : RenderPass(graphics, scene) {}
|
2021-05-06 17:02:10 +02:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
LightCullingPass::~LightCullingPass() {}
|
2024-05-15 15:27:13 +02:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void LightCullingPass::beginFrame(const Component::Camera& cam) {
|
2023-11-05 10:36:01 +01:00
|
|
|
RenderPass::beginFrame(cam);
|
2021-06-04 18:27:49 +02:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
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-06-04 21:34:14 +02:00
|
|
|
oLightIndexCounter->rotateBuffer(sizeof(uint32));
|
2024-08-08 22:14:25 +02:00
|
|
|
oLightIndexCounter->updateContents(0, sizeof(uint32), &reset);
|
2024-06-04 21:34:14 +02:00
|
|
|
tLightIndexCounter->rotateBuffer(sizeof(uint32));
|
2024-08-08 22:14:25 +02:00
|
|
|
tLightIndexCounter->updateContents(0, sizeof(uint32), &reset);
|
2024-06-09 12:20:04 +02: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-06-09 12:20:04 +02:00
|
|
|
void LightCullingPass::render() {
|
2024-06-15 21:47:20 +02:00
|
|
|
query->beginQuery();
|
2024-08-13 22:44:04 +02:00
|
|
|
timestamps->write(Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT, "LightCullBegin");
|
2024-07-05 12:02:46 +02:00
|
|
|
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);
|
|
|
|
|
tLightGrid->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);
|
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-07-05 12:02:46 +02:00
|
|
|
if (getGlobals().useLightCulling) {
|
2024-05-29 10:40:35 +02:00
|
|
|
computeCommand->bindPipeline(cullingEnabledPipeline);
|
2024-06-09 12:20:04 +02:00
|
|
|
} else {
|
2024-05-29 10:40:35 +02:00
|
|
|
computeCommand->bindPipeline(cullingPipeline);
|
|
|
|
|
}
|
2024-06-09 12:20:04 +02:00
|
|
|
computeCommand->bindDescriptor({viewParamsSet, dispatchParamsSet, cullingDescriptorSet, lightEnv->getDescriptorSet()});
|
2024-01-31 09:49:53 +01:00
|
|
|
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-06-09 12:20:04 +02:00
|
|
|
// std::cout << "Execute" << std::endl;
|
2024-04-11 12:38:42 +02:00
|
|
|
graphics->executeCommands(std::move(commands));
|
2024-08-13 22:44:04 +02:00
|
|
|
timestamps->write(Gfx::SE_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, "LightCullEnd");
|
2024-06-15 21:47:20 +02:00
|
|
|
query->endQuery();
|
2024-06-09 12:20:04 +02:00
|
|
|
oLightIndexList->pipelineBarrier(Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
|
|
|
|
|
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
|
|
|
|
|
tLightIndexList->pipelineBarrier(Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
|
|
|
|
|
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
|
|
|
|
|
oLightGrid->pipelineBarrier(Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT,
|
|
|
|
|
Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
|
|
|
|
|
tLightGrid->pipelineBarrier(Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT,
|
|
|
|
|
Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
|
2021-05-06 17:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void LightCullingPass::endFrame() {}
|
2021-05-06 17:02:10 +02:00
|
|
|
|
2024-06-09 12:20:04 +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{
|
2024-07-05 12:02:46 +02:00
|
|
|
.sourceData =
|
|
|
|
|
{
|
|
|
|
|
.size = sizeof(DispatchParams),
|
|
|
|
|
.data = (uint8*)&dispatchParams,
|
|
|
|
|
.owner = Gfx::QueueType::COMPUTE,
|
|
|
|
|
},
|
2023-11-05 10:36:01 +01:00
|
|
|
.dynamic = false,
|
2024-02-20 21:07:17 +01:00
|
|
|
.name = "DispatchParams",
|
2024-06-09 12:20:04 +02:00
|
|
|
});
|
2024-07-05 12:02:46 +02:00
|
|
|
dispatchParamsBuffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
|
|
|
|
|
Gfx::SE_ACCESS_UNIFORM_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
|
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
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
// DepthTexture
|
|
|
|
|
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
|
|
|
|
.binding = 0,
|
|
|
|
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
|
|
|
|
|
});
|
|
|
|
|
// o_lightIndexCounter
|
|
|
|
|
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
|
|
|
|
.binding = 1, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, .access = Gfx::SE_DESCRIPTOR_ACCESS_READ_WRITE_BIT});
|
|
|
|
|
// t_lightIndexCounter
|
|
|
|
|
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
|
|
|
|
.binding = 2, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, .access = Gfx::SE_DESCRIPTOR_ACCESS_READ_WRITE_BIT});
|
|
|
|
|
// o_lightIndexList
|
|
|
|
|
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
|
|
|
|
.binding = 3, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, .access = Gfx::SE_DESCRIPTOR_ACCESS_READ_WRITE_BIT});
|
|
|
|
|
// t_lightIndexList
|
|
|
|
|
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
|
|
|
|
.binding = 4, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, .access = Gfx::SE_DESCRIPTOR_ACCESS_READ_WRITE_BIT});
|
|
|
|
|
// o_lightGrid
|
|
|
|
|
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
|
|
|
|
.binding = 5, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_IMAGE, .access = Gfx::SE_DESCRIPTOR_ACCESS_READ_WRITE_BIT});
|
|
|
|
|
// t_lightGrid
|
|
|
|
|
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-07-10 21:07:10 +02:00
|
|
|
ShaderCompilationInfo createInfo = {
|
2024-05-29 10:40:35 +02:00
|
|
|
.name = "Culling",
|
2024-07-10 21:07:10 +02:00
|
|
|
.modules = {"LightCulling"},
|
|
|
|
|
.entryPoints = {{"cullLights", "LightCulling"}},
|
2024-05-29 10:40:35 +02:00
|
|
|
.rootSignature = cullingLayout,
|
|
|
|
|
};
|
2024-07-10 21:07:10 +02:00
|
|
|
graphics->beginShaderCompilation(createInfo);
|
|
|
|
|
cullingShader = graphics->createComputeShader({0});
|
2024-05-29 10:40:35 +02:00
|
|
|
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());
|
2024-06-09 12:20:04 +02:00
|
|
|
|
2024-07-10 21:07:10 +02:00
|
|
|
ShaderCompilationInfo createInfo = {
|
2024-05-29 10:40:35 +02:00
|
|
|
.name = "Culling",
|
2024-07-10 21:07:10 +02:00
|
|
|
.modules = {"LightCulling"},
|
|
|
|
|
.entryPoints = {{"cullLights", "LightCulling"}},
|
2024-05-29 10:40:35 +02:00
|
|
|
.rootSignature = cullingEnableLayout,
|
|
|
|
|
};
|
|
|
|
|
createInfo.defines["LIGHT_CULLING"] = "1";
|
2024-07-10 21:07:10 +02:00
|
|
|
graphics->beginShaderCompilation(createInfo);
|
|
|
|
|
cullingEnabledShader = graphics->createComputeShader({0});
|
2024-05-29 10:40:35 +02:00
|
|
|
cullingEnableLayout->create();
|
2024-06-09 12:20:04 +02:00
|
|
|
|
2024-05-29 10:40:35 +02:00
|
|
|
Gfx::ComputePipelineCreateInfo pipelineInfo;
|
|
|
|
|
pipelineInfo.computeShader = cullingShader;
|
|
|
|
|
pipelineInfo.pipelineLayout = std::move(cullingEnableLayout);
|
|
|
|
|
cullingEnabledPipeline = graphics->createComputePipeline(std::move(pipelineInfo));
|
|
|
|
|
}
|
2024-06-09 12:20:04 +02:00
|
|
|
|
2022-04-15 23:45:44 +02:00
|
|
|
uint32 counterReset = 0;
|
2023-11-15 17:42:57 +01:00
|
|
|
ShaderBufferCreateInfo structInfo = {
|
2024-06-09 12:20:04 +02:00
|
|
|
.sourceData =
|
|
|
|
|
{
|
|
|
|
|
.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 = {
|
2024-06-13 22:47:51 +02:00
|
|
|
.sourceData =
|
|
|
|
|
{
|
|
|
|
|
.size = (uint32)sizeof(uint32) * dispatchParams.numThreadGroups.x * dispatchParams.numThreadGroups.y *
|
|
|
|
|
dispatchParams.numThreadGroups.z * 8192,
|
|
|
|
|
.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-07-05 12:02:46 +02:00
|
|
|
oLightGrid->changeLayout(Gfx::SE_IMAGE_LAYOUT_GENERAL, Gfx::SE_ACCESS_NONE, Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
|
|
|
|
|
Gfx::SE_ACCESS_SHADER_READ_BIT | Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
|
|
|
|
|
tLightGrid->changeLayout(Gfx::SE_IMAGE_LAYOUT_GENERAL, Gfx::SE_ACCESS_NONE, Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
|
|
|
|
|
Gfx::SE_ACCESS_SHADER_READ_BIT | Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
|
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));
|
2024-07-01 12:17:04 +02:00
|
|
|
|
|
|
|
|
query = graphics->createPipelineStatisticsQuery("LightCullPipelineStatistics");
|
|
|
|
|
resources->registerQueryOutput("LIGHTCULL_QUERY", query);
|
2022-04-15 23:45:44 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-15 21:47:20 +02:00
|
|
|
void LightCullingPass::createRenderPass() {
|
2024-09-03 11:03:23 +02:00
|
|
|
timestamps = resources->requestTimestampQuery("TIMESTAMPS");
|
2024-06-15 21:47:20 +02:00
|
|
|
depthAttachment = resources->requestRenderTarget("DEPTHPREPASS_DEPTH").getTexture();
|
|
|
|
|
}
|
2022-04-15 23:45:44 +02:00
|
|
|
|
2024-06-09 12:20:04 +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());
|
2024-07-05 12:02:46 +02:00
|
|
|
viewParamsBuffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
|
|
|
|
|
Gfx::SE_ACCESS_UNIFORM_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
|
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-06-09 12:20:04 +02:00
|
|
|
dispatchParamsLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
|
|
|
|
.binding = 0,
|
|
|
|
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
|
|
|
});
|
|
|
|
|
dispatchParamsLayout->addDescriptorBinding(Gfx::DescriptorBinding{
|
2024-06-13 22:47:51 +02:00
|
|
|
.binding = 1,
|
|
|
|
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
|
|
|
|
|
.access = Gfx::SE_DESCRIPTOR_ACCESS_WRITE_ONLY_BIT,
|
|
|
|
|
});
|
2024-08-28 17:54:14 +02:00
|
|
|
dispatchParamsLayout->create();
|
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);
|
2024-07-10 21:07:10 +02:00
|
|
|
ShaderCompilationInfo createInfo = {
|
2024-04-19 18:23:36 +02:00
|
|
|
.name = "Frustum",
|
2024-07-10 21:07:10 +02:00
|
|
|
.modules = {"ComputeFrustums"},
|
|
|
|
|
.entryPoints = {{"computeFrustums", "ComputeFrustums"}},
|
2024-04-19 18:23:36 +02:00
|
|
|
.rootSignature = frustumLayout,
|
|
|
|
|
};
|
2024-07-10 21:07:10 +02:00
|
|
|
graphics->beginShaderCompilation(createInfo);
|
|
|
|
|
frustumShader = graphics->createComputeShader({0});
|
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{
|
2024-06-13 22:47:51 +02:00
|
|
|
.sourceData =
|
|
|
|
|
{
|
|
|
|
|
.size = sizeof(DispatchParams),
|
|
|
|
|
.data = (uint8*)&dispatchParams,
|
|
|
|
|
.owner = Gfx::QueueType::COMPUTE,
|
|
|
|
|
},
|
2023-11-05 10:36:01 +01:00
|
|
|
.dynamic = false,
|
2024-06-13 22:47:51 +02:00
|
|
|
.name = "FrustumDispatch",
|
|
|
|
|
});
|
2024-06-18 23:33:03 +02:00
|
|
|
frustumDispatchParamsBuffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
|
|
|
|
|
Gfx::SE_ACCESS_UNIFORM_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
|
2024-06-13 22:47:51 +02:00
|
|
|
frustumBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
|
|
|
|
|
.sourceData =
|
|
|
|
|
{
|
|
|
|
|
.size = sizeof(Frustum) * numThreads.x * numThreads.y * numThreads.z,
|
|
|
|
|
.data = nullptr,
|
|
|
|
|
.owner = Gfx::QueueType::COMPUTE,
|
|
|
|
|
},
|
|
|
|
|
.numElements = numThreads.x * numThreads.y * numThreads.z,
|
|
|
|
|
.dynamic = false,
|
|
|
|
|
.name = "FrustumBuffer",
|
|
|
|
|
});
|
2024-06-18 23:33:03 +02:00
|
|
|
frustumBuffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT, Gfx::SE_ACCESS_SHADER_WRITE_BIT,
|
|
|
|
|
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
|
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);
|
2024-06-09 12:20:04 +02: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,
|
2024-06-09 12:20:04 +02:00
|
|
|
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
|
2024-04-19 18:23:36 +02:00
|
|
|
}
|