From 1aed070f8f1f6bc78292c95006e908b9527eab82 Mon Sep 17 00:00:00 2001 From: DolaMay <158788224@qq.com> Date: Thu, 30 Sep 2021 10:16:50 +0800 Subject: [PATCH] cluster native v340 code (#3923) * cluster native v340 code * pclint errors and review comments correct * compile errors * clang tidy bug fix * cluster add enable and disable api * add marco to switch cluster feature * Code review: simplify fg read logic --- native/CMakeLists.txt | 2 + .../renderer/pipeline/ClusterLightCulling.cpp | 827 ++++++++++++++++++ .../renderer/pipeline/ClusterLightCulling.h | 155 ++++ native/cocos/renderer/pipeline/Define.cpp | 2 + native/cocos/renderer/pipeline/Define.h | 8 +- .../cocos/renderer/pipeline/PipelineUBO.cpp | 7 + .../renderer/pipeline/RenderPipeline.cpp | 9 +- .../cocos/renderer/pipeline/RenderPipeline.h | 8 +- native/cocos/renderer/pipeline/common/Enum.h | 9 +- .../pipeline/deferred/DeferredPipeline.cpp | 15 +- .../pipeline/deferred/DeferredPipeline.h | 15 +- .../pipeline/deferred/LightingStage.cpp | 70 +- native/cocos/scene/Camera.h | 2 + native/tools/tojs/pipeline.ini | 2 +- 14 files changed, 1097 insertions(+), 34 deletions(-) create mode 100644 native/cocos/renderer/pipeline/ClusterLightCulling.cpp create mode 100644 native/cocos/renderer/pipeline/ClusterLightCulling.h diff --git a/native/CMakeLists.txt b/native/CMakeLists.txt index e0f9c3c72b..acc1ee0cd0 100644 --- a/native/CMakeLists.txt +++ b/native/CMakeLists.txt @@ -782,6 +782,8 @@ cocos_source_files( cocos/renderer/pipeline/BatchedBuffer.cpp cocos/renderer/pipeline/BatchedBuffer.h + cocos/renderer/pipeline/ClusterLightCulling.cpp + cocos/renderer/pipeline/ClusterLightCulling.h cocos/renderer/pipeline/Define.h cocos/renderer/pipeline/Define.cpp cocos/renderer/pipeline/GlobalDescriptorSetManager.h diff --git a/native/cocos/renderer/pipeline/ClusterLightCulling.cpp b/native/cocos/renderer/pipeline/ClusterLightCulling.cpp new file mode 100644 index 0000000000..83ce541aac --- /dev/null +++ b/native/cocos/renderer/pipeline/ClusterLightCulling.cpp @@ -0,0 +1,827 @@ +/**************************************************************************** + Copyright (c) 2020-2021 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#include "ClusterLightCulling.h" +#include "Define.h" +#include "deferred/DeferredPipeline.h" +#include "frame-graph/FrameGraph.h" + +namespace cc { +namespace pipeline { + +framegraph::StringHandle fgStrHandleClusterBuffer = framegraph::FrameGraph::stringToHandle("clusterBuffer"); +framegraph::StringHandle fgStrHandleClusterGlobalIndexBuffer = framegraph::FrameGraph::stringToHandle("globalIndexBuffer"); +framegraph::StringHandle fgStrHandleClusterLightBuffer = framegraph::FrameGraph::stringToHandle("clusterLightBuffer"); +framegraph::StringHandle fgStrHandleClusterLightIndexBuffer = framegraph::FrameGraph::stringToHandle("lightIndexBuffer"); +framegraph::StringHandle fgStrHandleClusterLightGridBuffer = framegraph::FrameGraph::stringToHandle("lightGridBuffer"); + +framegraph::StringHandle fgStrHandleClusterBuildPass = framegraph::FrameGraph::stringToHandle("clusterBuildPass"); +framegraph::StringHandle fgStrHandleClusterCullingPass = framegraph::FrameGraph::stringToHandle("clusterCullingPass"); + +ClusterLightCulling::~ClusterLightCulling() { + CC_SAFE_DESTROY(_buildingShader); + CC_SAFE_DESTROY(_buildingDescriptorSetLayout); + CC_SAFE_DESTROY(_buildingPipelineLayout); + CC_SAFE_DESTROY(_buildingPipelineState); + CC_SAFE_DESTROY(_buildingDescriptorSet); + + CC_SAFE_DESTROY(_resetCounterShader); + CC_SAFE_DESTROY(_resetCounterDescriptorSetLayout); + CC_SAFE_DESTROY(_resetCounterPipelineLayout); + CC_SAFE_DESTROY(_resetCounterPipelineState); + CC_SAFE_DESTROY(_resetCounterDescriptorSet); + + CC_SAFE_DESTROY(_cullingShader); + CC_SAFE_DESTROY(_cullingDescriptorSetLayout); + CC_SAFE_DESTROY(_cullingPipelineLayout); + CC_SAFE_DESTROY(_cullingPipelineState); + CC_SAFE_DESTROY(_cullingDescriptorSet); + + CC_SAFE_DESTROY(_constantsBuffer); + + delete _resetBarrier; +} + +void ClusterLightCulling::initialize(gfx::Device* dev) { + _device = dev; + if (!_device->hasFeature(gfx::Feature::COMPUTE_SHADER)) return; + + uint maxInvocations = _device->getCapabilities().maxComputeWorkGroupInvocations; + CCASSERT(CLUSTERS_X_THREADS * CLUSTERS_Y_THREADS * CLUSTERS_Z_THREADS <= maxInvocations, "maxInvocations is too small"); + CC_LOG_INFO(" work group size: %dx%dx%d", CLUSTERS_X_THREADS, CLUSTERS_Y_THREADS, CLUSTERS_Z_THREADS); + + _constantsBuffer = _device->createBuffer({ + gfx::BufferUsageBit::UNIFORM, + gfx::MemoryUsageBit::HOST | gfx::MemoryUsageBit::DEVICE, + 2 * sizeof(Vec4) + 2 * sizeof(Mat4), + 2 * sizeof(Vec4) + 2 * sizeof(Mat4), + gfx::BufferFlagBit::NONE, + }); + + _lightBufferStride = 4 * sizeof(Vec4); + _buildingDispatchInfo = {CLUSTERS_X / CLUSTERS_X_THREADS, CLUSTERS_Y / CLUSTERS_Y_THREADS, CLUSTERS_Z / CLUSTERS_Z_THREADS}; + _resetDispatchInfo = {1, 1, 1}; + _cullingDispatchInfo = {CLUSTERS_X / CLUSTERS_X_THREADS, CLUSTERS_Y / CLUSTERS_Y_THREADS, CLUSTERS_Z / CLUSTERS_Z_THREADS}; + + gfx::GlobalBarrierInfo resetBarrierInfo = { + { + gfx::AccessType::COMPUTE_SHADER_WRITE, + }, + { + gfx::AccessType::COMPUTE_SHADER_READ_OTHER, + }}; + _resetBarrier = _device->getGlobalBarrier(resetBarrierInfo); + + initBuildingSatge(); + initResetStage(); + initCullingStage(); + + _initialized = true; +} + +void ClusterLightCulling::update() { + if (!_initialized) return; + + auto* const sceneData = _pipeline->getPipelineSceneData(); + auto* const sharedData = sceneData->getSharedData(); + + _constants[NEAR_FAR_OFFSET + 0] = static_cast(_camera->nearClip); + _constants[NEAR_FAR_OFFSET + 1] = static_cast(_camera->farClip); + _constants[VIEW_PORT_OFFSET + 0] = _camera->viewPort.x * static_cast(_camera->width) * sharedData->shadingScale; + _constants[VIEW_PORT_OFFSET + 1] = _camera->viewPort.y * static_cast(_camera->height) * sharedData->shadingScale; + _constants[VIEW_PORT_OFFSET + 2] = _camera->viewPort.z * static_cast(_camera->width) * sharedData->shadingScale; + _constants[VIEW_PORT_OFFSET + 3] = _camera->viewPort.w * static_cast(_camera->height) * sharedData->shadingScale; + + memcpy(_constants.data() + MAT_VIEW_OFFSET, _camera->matView.m, sizeof(cc::Mat4)); + memcpy(_constants.data() + MAT_PROJ_INV_OFFSET, _camera->matProjInv.m, sizeof(cc::Mat4)); + + _constantsBuffer->update(_constants.data(), 2 * sizeof(Vec4) + 2 * sizeof(Mat4)); + updateLights(); + + uint cameraIndex = _pipeline->getPipelineUBO()->getCurrentCameraUBOOffset(); + if (cameraIndex >= _oldCamProjMats.size()) { + _rebuildClusters = true; + uint nextLength = std::max(nextPow2(static_cast(cameraIndex)), uint(1)); + _oldCamProjMats.resize(nextLength, Mat4::ZERO); + _oldCamProjMats[cameraIndex] = _camera->matProj; + } else { + _rebuildClusters = ClusterLightCulling::isProjMatChange(_camera->matProj, _oldCamProjMats[cameraIndex]); + _oldCamProjMats[cameraIndex] = _camera->matProj; + } +} + +void ClusterLightCulling::updateLights() { + if (!_pipeline) { + return; + } + + _validLights.clear(); + + scene::Sphere sphere; + const auto* const scene = _camera->scene; + for (auto* light : scene->getSphereLights()) { + sphere.setCenter(light->getPosition()); + sphere.setRadius(light->getRange()); + if (sphere.sphereFrustum(_camera->frustum)) { + _validLights.emplace_back(static_cast(light)); + } + } + + for (auto* light : scene->getSpotLights()) { + sphere.setCenter(light->getPosition()); + sphere.setRadius(light->getRange()); + if (sphere.sphereFrustum(_camera->frustum)) { + _validLights.emplace_back(static_cast(light)); + } + } + + const auto exposure = _camera->exposure; + const auto validLightCount = _validLights.size(); + auto* const sceneData = _pipeline->getPipelineSceneData(); + auto* const sharedData = sceneData->getSharedData(); + + if (validLightCount > _lightBufferCount) { + _lightBufferResized = true; + _lightBufferCount = nextPow2(static_cast(validLightCount)); + _lightBufferData.resize(16 * _lightBufferCount); + } + + for (unsigned l = 0, offset = 0; l < validLightCount; l++, offset += 16) { + auto* light = _validLights[l]; + const bool isSpotLight = scene::LightType::SPOT == light->getType(); + const auto* spotLight = isSpotLight ? static_cast(light) : nullptr; + const auto* sphereLight = isSpotLight ? nullptr : static_cast(light); + + auto index = offset + UBOForwardLight::LIGHT_POS_OFFSET; + const auto& position = isSpotLight ? spotLight->getPosition() : sphereLight->getPosition(); + _lightBufferData[index++] = position.x; + _lightBufferData[index++] = position.y; + _lightBufferData[index] = position.z; + + index = offset + UBOForwardLight::LIGHT_SIZE_RANGE_ANGLE_OFFSET; + _lightBufferData[index++] = isSpotLight ? spotLight->getSize() : sphereLight->getSize(); + _lightBufferData[index] = isSpotLight ? spotLight->getRange() : sphereLight->getRange(); + + index = offset + UBOForwardLight::LIGHT_COLOR_OFFSET; + const auto& color = light->getColor(); + if (light->getUseColorTemperature()) { + const auto& tempRGB = light->getColorTemperatureRGB(); + _lightBufferData[index++] = color.x * tempRGB.x; + _lightBufferData[index++] = color.y * tempRGB.y; + _lightBufferData[index++] = color.z * tempRGB.z; + } else { + _lightBufferData[index++] = color.x; + _lightBufferData[index++] = color.y; + _lightBufferData[index++] = color.z; + } + + float illuminance = isSpotLight ? spotLight->getIlluminance() : sphereLight->getIlluminance(); + if (sharedData->isHDR) { + _lightBufferData[index] = illuminance * sharedData->fpScale * _lightMeterScale; + } else { + _lightBufferData[index] = illuminance * exposure * _lightMeterScale; + } + + switch (light->getType()) { + case scene::LightType::SPHERE: + _lightBufferData[offset + UBOForwardLight::LIGHT_POS_OFFSET + 3] = 0; + _lightBufferData[offset + UBOForwardLight::LIGHT_SIZE_RANGE_ANGLE_OFFSET + 2] = 0; + break; + case scene::LightType::SPOT: { + _lightBufferData[offset + UBOForwardLight::LIGHT_POS_OFFSET + 3] = 1.0F; + _lightBufferData[offset + UBOForwardLight::LIGHT_SIZE_RANGE_ANGLE_OFFSET + 2] = spotLight->getSpotAngle(); + + index = offset + UBOForwardLight::LIGHT_DIR_OFFSET; + const auto& direction = spotLight->getDirection(); + _lightBufferData[index++] = direction.x; + _lightBufferData[index++] = direction.y; + _lightBufferData[index] = direction.z; + } break; + default: + break; + } + } + // the count of lights is set to cc_lightDir[0].w + _lightBufferData[3 * 4 + 3] = static_cast(validLightCount); +} + +void ClusterLightCulling::initBuildingSatge() { + ShaderStrings sources; + sources.glsl4 = StringUtil::format( + R"( + #define CLUSTERS_X 16 + #define CLUSTERS_Y 8 + + layout(set=0, binding=0, std140) uniform CCConst { + vec4 cc_nearFar; + vec4 cc_viewPort; + mat4 cc_matView; + mat4 cc_matProjInv; + }; + layout(set=0, binding=1, std430) buffer b_clustersBuffer { vec4 b_clusters[]; }; + + vec4 screen2Eye(vec4 coord) { + vec3 ndc = vec3( + 2.0 * (coord.x - cc_viewPort.x) / cc_viewPort.z - 1.0, + 2.0 * (coord.y - cc_viewPort.y) / cc_viewPort.w - 1.0, + 2.0 * coord.z - 1.0); + vec4 eye = ((cc_matProjInv) * (vec4(ndc, 1.0))); + eye = eye / eye.w; + return eye; + } + + layout(local_size_x=16, local_size_y=8, local_size_z=4) in; + void main() { + uint clusterIndex = gl_GlobalInvocationID.z * uvec3(16, 8, 4).x * uvec3(16, 8, 4).y + + gl_GlobalInvocationID.y * uvec3(16, 8, 4).x + gl_GlobalInvocationID.x; + float clusterSizeX = ceil(cc_viewPort.z / float(CLUSTERS_X)); + float clusterSizeY = ceil(cc_viewPort.w / float(CLUSTERS_Y)); + vec4 minScreen = vec4(vec2(gl_GlobalInvocationID.xy) * vec2(clusterSizeX, clusterSizeY), 1.0, 1.0); + vec4 maxScreen = vec4(vec2(gl_GlobalInvocationID.xy + uvec2(1, 1)) * vec2(clusterSizeX, clusterSizeY), 1.0, 1.0); + vec3 minEye = screen2Eye(minScreen).xyz; + vec3 maxEye = screen2Eye(maxScreen).xyz; + float clusterNear = -cc_nearFar.x * pow(cc_nearFar.y / cc_nearFar.x, float(gl_GlobalInvocationID.z) / float(24)); + float clusterFar = -cc_nearFar.x * pow(cc_nearFar.y / cc_nearFar.x, float(gl_GlobalInvocationID.z + 1u) / float(24)); + vec3 minNear = minEye * clusterNear / minEye.z; + vec3 minFar = minEye * clusterFar / minEye.z; + vec3 maxNear = maxEye * clusterNear / maxEye.z; + vec3 maxFar = maxEye * clusterFar / maxEye.z; + vec3 minBounds = min(min(minNear, minFar), min(maxNear, maxFar)); + vec3 maxBounds = max(max(minNear, minFar), max(maxNear, maxFar)); + + b_clusters[2u * clusterIndex + 0u] = vec4(minBounds, 1.0); + b_clusters[2u * clusterIndex + 1u] = vec4(maxBounds, 1.0); + })"); + sources.glsl3 = StringUtil::format( + R"( + #define CLUSTERS_X 16 + #define CLUSTERS_Y 8 + + layout(std140) uniform CCConst { + vec4 cc_nearFar; + vec4 cc_viewPort; + mat4 cc_matView; + mat4 cc_matProjInv; + }; + layout(std430, binding=1) buffer b_clustersBuffer { vec4 b_clusters[]; }; + + vec4 screen2Eye(vec4 coord) { + vec3 ndc = vec3( + 2.0 * (coord.x - cc_viewPort.x) / cc_viewPort.z - 1.0, + 2.0 * (coord.y - cc_viewPort.y) / cc_viewPort.w - 1.0, + 2.0 * coord.z - 1.0); + vec4 eye = ((cc_matProjInv) * (vec4(ndc, 1.0))); + eye = eye / eye.w; + return eye; + } + + layout(local_size_x=16, local_size_y=8, local_size_z=4) in; + void main() { + uint clusterIndex = gl_GlobalInvocationID.z * uvec3(16, 8, 4).x * uvec3(16, 8, 4).y + + gl_GlobalInvocationID.y * uvec3(16, 8, 4).x + gl_GlobalInvocationID.x; + float clusterSizeX = ceil(cc_viewPort.z / float(CLUSTERS_X)); + float clusterSizeY = ceil(cc_viewPort.w / float(CLUSTERS_Y)); + vec4 minScreen = vec4(vec2(gl_GlobalInvocationID.xy) * vec2(clusterSizeX, clusterSizeY), 1.0, 1.0); + vec4 maxScreen = vec4(vec2(gl_GlobalInvocationID.xy + uvec2(1, 1)) * vec2(clusterSizeX, clusterSizeY), 1.0, 1.0); + vec3 minEye = screen2Eye(minScreen).xyz; + vec3 maxEye = screen2Eye(maxScreen).xyz; + float clusterNear = -cc_nearFar.x * pow(cc_nearFar.y / cc_nearFar.x, float(gl_GlobalInvocationID.z) / float(24)); + float clusterFar = -cc_nearFar.x * pow(cc_nearFar.y / cc_nearFar.x, float(gl_GlobalInvocationID.z + 1u) / float(24)); + vec3 minNear = minEye * clusterNear / minEye.z; + vec3 minFar = minEye * clusterFar / minEye.z; + vec3 maxNear = maxEye * clusterNear / maxEye.z; + vec3 maxFar = maxEye * clusterFar / maxEye.z; + vec3 minBounds = min(min(minNear, minFar), min(maxNear, maxFar)); + vec3 maxBounds = max(max(minNear, minFar), max(maxNear, maxFar)); + + b_clusters[2u * clusterIndex + 0u] = vec4(minBounds, 1.0); + b_clusters[2u * clusterIndex + 1u] = vec4(maxBounds, 1.0); + })"); + // no compute support in GLES2 + + gfx::ShaderInfo shaderInfo; + shaderInfo.name = "Compute "; + shaderInfo.stages = {{gfx::ShaderStageFlagBit::COMPUTE, getShaderSource(sources)}}; + shaderInfo.blocks = { + {0, 0, "CCConst", {{"cc_nearFar", gfx::Type::FLOAT4, 1}, {"cc_viewPort", gfx::Type::FLOAT4, 1}, {"cc_matView", gfx::Type::MAT4, 1}, {"cc_matProjInv", gfx::Type::MAT4, 1}}, 1}, + }; + shaderInfo.buffers = {{0, 1, "b_clustersBuffer", 1, gfx::MemoryAccessBit::WRITE_ONLY}}; + _buildingShader = _device->createShader(shaderInfo); + + gfx::DescriptorSetLayoutInfo dslInfo; + dslInfo.bindings.push_back({0, gfx::DescriptorType::UNIFORM_BUFFER, 1, gfx::ShaderStageFlagBit::COMPUTE}); + dslInfo.bindings.push_back({1, gfx::DescriptorType::STORAGE_BUFFER, 1, gfx::ShaderStageFlagBit::COMPUTE}); + + _buildingDescriptorSetLayout = _device->createDescriptorSetLayout(dslInfo); + _buildingDescriptorSet = _device->createDescriptorSet({_buildingDescriptorSetLayout}); + + _buildingPipelineLayout = _device->createPipelineLayout({{_buildingDescriptorSetLayout}}); + + gfx::PipelineStateInfo pipelineInfo; + pipelineInfo.shader = _buildingShader; + pipelineInfo.pipelineLayout = _buildingPipelineLayout; + pipelineInfo.bindPoint = gfx::PipelineBindPoint::COMPUTE; + + _buildingPipelineState = _device->createPipelineState(pipelineInfo); +} + +void ClusterLightCulling::initResetStage() { + ShaderStrings sources; + sources.glsl4 = StringUtil::format( + R"( + layout(std430, binding = 0) buffer b_globalIndexBuffer { uint b_globalIndex[]; }; + layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; + void main() + { + if (gl_GlobalInvocationID.x == 0u) { + b_globalIndex[0] = 0u; + } + } + )"); + sources.glsl3 = StringUtil::format( + R"( + layout(std430, binding = 0) buffer b_globalIndexBuffer { uint b_globalIndex[]; }; + layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; + void main() + { + if (gl_GlobalInvocationID.x == 0u) { + b_globalIndex[0] = 0u; + } + } + )"); + // no compute support in GLES2 + + gfx::ShaderInfo shaderInfo; + shaderInfo.name = "Compute "; + shaderInfo.stages = {{gfx::ShaderStageFlagBit::COMPUTE, getShaderSource(sources)}}; + shaderInfo.buffers = {{0, 0, "b_globalIndexBuffer", 1, gfx::MemoryAccessBit::WRITE_ONLY}}; + _resetCounterShader = _device->createShader(shaderInfo); + + gfx::DescriptorSetLayoutInfo dslInfo; + dslInfo.bindings.push_back({0, gfx::DescriptorType::STORAGE_BUFFER, 1, gfx::ShaderStageFlagBit::COMPUTE}); + + _resetCounterDescriptorSetLayout = _device->createDescriptorSetLayout(dslInfo); + _resetCounterDescriptorSet = _device->createDescriptorSet({_resetCounterDescriptorSetLayout}); + + _resetCounterPipelineLayout = _device->createPipelineLayout({{_resetCounterDescriptorSetLayout}}); + + gfx::PipelineStateInfo pipelineInfo; + pipelineInfo.shader = _resetCounterShader; + pipelineInfo.pipelineLayout = _resetCounterPipelineLayout; + pipelineInfo.bindPoint = gfx::PipelineBindPoint::COMPUTE; + + _resetCounterPipelineState = _device->createPipelineState(pipelineInfo); +} + +void ClusterLightCulling::initCullingStage() { + ShaderStrings sources; + sources.glsl4 = StringUtil::format( + R"( + layout(set=0, binding=0, std140) uniform CCConst { + vec4 cc_nearFar; + vec4 cc_viewPort; + mat4 cc_matView; + mat4 cc_matProjInv; + }; + layout(set=0, binding=1, std430) readonly buffer b_ccLightsBuffer { vec4 b_ccLights[]; }; + layout(set=0, binding=2, std430) buffer b_clusterLightIndicesBuffer { uint b_clusterLightIndices[]; }; + layout(set=0, binding=3, std430) buffer b_clusterLightGridBuffer { uvec4 b_clusterLightGrid[]; }; + layout(set=0, binding=4, std430) buffer b_clustersBuffer { vec4 b_clusters[]; }; + layout(set=0, binding=5, std430) buffer b_globalIndexBuffer { uint b_globalIndex[]; }; + struct CCLight { + vec4 cc_lightPos; + vec4 cc_lightColor; + vec4 cc_lightSizeRangeAngle; + vec4 cc_lightDir; + }; + uint ccLightCount() + { + return uint(b_ccLights[3].w); + } + CCLight getCCLight(uint i) + { + CCLight light; + light.cc_lightPos = b_ccLights[4u * i + 0u]; + light.cc_lightColor = b_ccLights[4u * i + 1u]; + light.cc_lightSizeRangeAngle = b_ccLights[4u * i + 2u]; + light.cc_lightDir = b_ccLights[4u * i + 3u]; + return light; + } + struct Cluster { + vec3 minBounds; + vec3 maxBounds; + }; + struct LightGrid { + uint offset; + uint ccLights; + }; + Cluster getCluster(uint index) + { + Cluster cluster; + cluster.minBounds = b_clusters[2u * index + 0u].xyz; + cluster.maxBounds = b_clusters[2u * index + 1u].xyz; + return cluster; + } + bool ccLightIntersectsCluster(CCLight light, Cluster cluster) + { + if (light.cc_lightPos.w > 0.0) { + vec3 halfExtents = (cluster.maxBounds - cluster.minBounds) * 0.5; + vec3 center = (cluster.minBounds + cluster.maxBounds) * 0.5; + float sphereRadius = sqrt(dot(halfExtents, halfExtents)); + light.cc_lightDir = ((cc_matView) * (vec4(light.cc_lightDir.xyz, 1.0))); + light.cc_lightDir.xyz = normalize((light.cc_lightDir - ((cc_matView) * (vec4(0,0,0, 1.0)))).xyz).xyz; + vec3 v = center - light.cc_lightPos.xyz; + float lenSq = dot(v, v); + float v1Len = dot(v, light.cc_lightDir.xyz); + float cosAngle = light.cc_lightSizeRangeAngle.z; + float sinAngle = sqrt(1.0 - cosAngle * cosAngle); + float distanceClosestPoint = cosAngle * sqrt(lenSq - v1Len * v1Len) - v1Len * sinAngle; + bool angleCull = distanceClosestPoint > sphereRadius; + bool frontCull = v1Len > sphereRadius + light.cc_lightSizeRangeAngle.y; + bool backCull = v1Len < -sphereRadius; + return !(angleCull || frontCull || backCull); + + } + vec3 closest = max(cluster.minBounds, min(light.cc_lightPos.xyz, cluster.maxBounds)); + vec3 dist = closest - light.cc_lightPos.xyz; + return dot(dist, dist) <= (light.cc_lightSizeRangeAngle.y * light.cc_lightSizeRangeAngle.y); + } + shared CCLight lights[(16 * 8 * 4)]; + layout(local_size_x = 16, local_size_y = 8, local_size_z = 4) in; + void main() + { + uint visibleLights[100]; + uint visibleCount = 0u; + uint clusterIndex = gl_GlobalInvocationID.z * uvec3(16, 8, 4).x * uvec3(16, 8, 4).y + + gl_GlobalInvocationID.y * uvec3(16, 8, 4).x + gl_GlobalInvocationID.x; + Cluster cluster = getCluster(clusterIndex); + uint lightCount = ccLightCount(); + uint lightOffset = 0u; + while (lightOffset < lightCount) { + uint batchSize = min((16u * 8u * 4u), lightCount - lightOffset); + if (uint(gl_LocalInvocationIndex) < batchSize) { + uint lightIndex = lightOffset + gl_LocalInvocationIndex; + CCLight light = getCCLight(lightIndex); + light.cc_lightPos.xyz = ((cc_matView) * (vec4(light.cc_lightPos.xyz, 1.0))).xyz; + lights[gl_LocalInvocationIndex] = light; + } + barrier(); + for (uint i = 0u; i < batchSize; i++) { + if (visibleCount < 100u && ccLightIntersectsCluster(lights[i], cluster)) { + visibleLights[visibleCount] = lightOffset + i; + visibleCount++; + } + } + lightOffset += batchSize; + } + barrier(); + uint offset = 0u; + offset = atomicAdd(b_globalIndex[0], visibleCount); + for (uint i = 0u; i < visibleCount; i++) { + b_clusterLightIndices[offset + i] = visibleLights[i]; + } + b_clusterLightGrid[clusterIndex] = uvec4(offset, visibleCount, 0, 0); + })"); + sources.glsl3 = StringUtil::format( + R"( + layout(std140) uniform CCConst { + vec4 cc_nearFar; + vec4 cc_viewPort; + mat4 cc_matView; + mat4 cc_matProjInv; + }; + layout(std430, binding=1) readonly buffer b_ccLightsBuffer { vec4 b_ccLights[]; }; + layout(std430, binding=2) buffer b_clusterLightIndicesBuffer { uint b_clusterLightIndices[]; }; + layout(std430, binding=3) buffer b_clusterLightGridBuffer { uvec4 b_clusterLightGrid[]; }; + layout(std430, binding=4) buffer b_clustersBuffer { vec4 b_clusters[]; }; + layout(std430, binding=5) buffer b_globalIndexBuffer { uint b_globalIndex[]; }; + struct CCLight { + vec4 cc_lightPos; + vec4 cc_lightColor; + vec4 cc_lightSizeRangeAngle; + vec4 cc_lightDir; + }; + uint ccLightCount() + { + return uint(b_ccLights[3].w); + } + CCLight getCCLight(uint i) + { + CCLight light; + light.cc_lightPos = b_ccLights[4u * i + 0u]; + light.cc_lightColor = b_ccLights[4u * i + 1u]; + light.cc_lightSizeRangeAngle = b_ccLights[4u * i + 2u]; + light.cc_lightDir = b_ccLights[4u * i + 3u]; + return light; + } + struct Cluster { + vec3 minBounds; + vec3 maxBounds; + }; + struct LightGrid { + uint offset; + uint ccLights; + }; + Cluster getCluster(uint index) + { + Cluster cluster; + cluster.minBounds = b_clusters[2u * index + 0u].xyz; + cluster.maxBounds = b_clusters[2u * index + 1u].xyz; + return cluster; + } + bool ccLightIntersectsCluster(CCLight light, Cluster cluster) + { + if (light.cc_lightPos.w > 0.0) { + vec3 halfExtents = (cluster.maxBounds - cluster.minBounds) * 0.5; + vec3 center = (cluster.minBounds + cluster.maxBounds) * 0.5; + float sphereRadius = sqrt(dot(halfExtents, halfExtents)); + light.cc_lightDir = ((cc_matView) * (vec4(light.cc_lightDir.xyz, 1.0))); + light.cc_lightDir.xyz = normalize((light.cc_lightDir - ((cc_matView) * (vec4(0,0,0, 1.0)))).xyz).xyz; + vec3 v = center - light.cc_lightPos.xyz; + float lenSq = dot(v, v); + float v1Len = dot(v, light.cc_lightDir.xyz); + float cosAngle = light.cc_lightSizeRangeAngle.z; + float sinAngle = sqrt(1.0 - cosAngle * cosAngle); + float distanceClosestPoint = cosAngle * sqrt(lenSq - v1Len * v1Len) - v1Len * sinAngle; + bool angleCull = distanceClosestPoint > sphereRadius; + bool frontCull = v1Len > sphereRadius + light.cc_lightSizeRangeAngle.y; + bool backCull = v1Len < -sphereRadius; + return !(angleCull || frontCull || backCull); + + } + vec3 closest = max(cluster.minBounds, min(light.cc_lightPos.xyz, cluster.maxBounds)); + vec3 dist = closest - light.cc_lightPos.xyz; + return dot(dist, dist) <= (light.cc_lightSizeRangeAngle.y * light.cc_lightSizeRangeAngle.y); + } + shared CCLight lights[(16 * 8 * 4)]; + layout(local_size_x = 16, local_size_y = 8, local_size_z = 4) in; + void main() + { + uint visibleLights[100]; + uint visibleCount = 0u; + uint clusterIndex = gl_GlobalInvocationID.z * uvec3(16, 8, 4).x * uvec3(16, 8, 4).y + + gl_GlobalInvocationID.y * uvec3(16, 8, 4).x + gl_GlobalInvocationID.x; + Cluster cluster = getCluster(clusterIndex); + uint lightCount = ccLightCount(); + uint lightOffset = 0u; + while (lightOffset < lightCount) { + uint batchSize = min((16u * 8u * 4u), lightCount - lightOffset); + if (uint(gl_LocalInvocationIndex) < batchSize) { + uint lightIndex = lightOffset + gl_LocalInvocationIndex; + CCLight light = getCCLight(lightIndex); + light.cc_lightPos.xyz = ((cc_matView) * (vec4(light.cc_lightPos.xyz, 1.0))).xyz; + lights[gl_LocalInvocationIndex] = light; + } + barrier(); + for (uint i = 0u; i < batchSize; i++) { + if (visibleCount < 100u && ccLightIntersectsCluster(lights[i], cluster)) { + visibleLights[visibleCount] = lightOffset + i; + visibleCount++; + } + } + lightOffset += batchSize; + } + barrier(); + uint offset = 0u; + offset = atomicAdd(b_globalIndex[0], visibleCount); + for (uint i = 0u; i < visibleCount; i++) { + b_clusterLightIndices[offset + i] = visibleLights[i]; + } + b_clusterLightGrid[clusterIndex] = uvec4(offset, visibleCount, 0, 0); + })"); + // no compute support in GLES2 + + gfx::ShaderInfo shaderInfo; + shaderInfo.name = "Compute "; + shaderInfo.stages = {{gfx::ShaderStageFlagBit::COMPUTE, getShaderSource(sources)}}; + shaderInfo.blocks = { + {0, 0, "CCConst", {{"cc_nearFar", gfx::Type::FLOAT4, 1}, {"cc_viewPort", gfx::Type::FLOAT4, 1}, {"cc_matView", gfx::Type::MAT4, 1}, {"cc_matProjInv", gfx::Type::MAT4, 1}}, 1}, + }; + shaderInfo.buffers = {{0, 1, "b_ccLightsBuffer", 1, gfx::MemoryAccessBit::READ_ONLY}, + {0, 2, "b_clusterLightIndicesBuffer", 1, gfx::MemoryAccessBit::WRITE_ONLY}, + {0, 3, "b_clusterLightGridBuffer", 1, gfx::MemoryAccessBit::WRITE_ONLY}, + {0, 4, "b_clustersBuffer", 1, gfx::MemoryAccessBit::READ_ONLY}, + {0, 5, "b_globalIndexBuffer", 1, gfx::MemoryAccessBit::READ_WRITE}}; + _cullingShader = _device->createShader(shaderInfo); + + gfx::DescriptorSetLayoutInfo dslInfo; + dslInfo.bindings.push_back({0, gfx::DescriptorType::UNIFORM_BUFFER, 1, gfx::ShaderStageFlagBit::COMPUTE}); + dslInfo.bindings.push_back({1, gfx::DescriptorType::STORAGE_BUFFER, 1, gfx::ShaderStageFlagBit::COMPUTE}); + dslInfo.bindings.push_back({2, gfx::DescriptorType::STORAGE_BUFFER, 1, gfx::ShaderStageFlagBit::COMPUTE}); + dslInfo.bindings.push_back({3, gfx::DescriptorType::STORAGE_BUFFER, 1, gfx::ShaderStageFlagBit::COMPUTE}); + dslInfo.bindings.push_back({4, gfx::DescriptorType::STORAGE_BUFFER, 1, gfx::ShaderStageFlagBit::COMPUTE}); + dslInfo.bindings.push_back({5, gfx::DescriptorType::STORAGE_BUFFER, 1, gfx::ShaderStageFlagBit::COMPUTE}); + + _cullingDescriptorSetLayout = _device->createDescriptorSetLayout(dslInfo); + _cullingDescriptorSet = _device->createDescriptorSet({_cullingDescriptorSetLayout}); + + _cullingPipelineLayout = _device->createPipelineLayout({{_cullingDescriptorSetLayout}}); + + gfx::PipelineStateInfo pipelineInfo; + pipelineInfo.shader = _cullingShader; + pipelineInfo.pipelineLayout = _cullingPipelineLayout; + pipelineInfo.bindPoint = gfx::PipelineBindPoint::COMPUTE; + + _cullingPipelineState = _device->createPipelineState(pipelineInfo); +} + +void ClusterLightCulling::clusterLightCulling(scene::Camera* camera) { + if (!_initialized) return; + _camera = camera; + update(); // update ubo and light data + + struct DataClusterBuild { + framegraph::BufferHandle clusterBuffer; // cluster build storage buffer + framegraph::BufferHandle globalIndexBuffer; // global light index storage buffer + }; + + auto clusterBuildSetup = [&](framegraph::PassNodeBuilder& builder, DataClusterBuild& data) { + data.clusterBuffer = framegraph::BufferHandle(builder.readFromBlackboard(fgStrHandleClusterBuffer)); + if (!data.clusterBuffer.isValid()) { + // each cluster has 2 vec4, min + max position for AABB + uint clusterBufferSize = 2 * sizeof(Vec4) * CLUSTER_COUNT; + + framegraph::Buffer::Descriptor bufferInfo; + bufferInfo.usage = gfx::BufferUsageBit::STORAGE; + bufferInfo.memUsage = gfx::MemoryUsageBit::HOST | gfx::MemoryUsageBit::DEVICE; + bufferInfo.size = clusterBufferSize; + bufferInfo.stride = clusterBufferSize; + bufferInfo.flags = gfx::BufferFlagBit::NONE; + data.clusterBuffer = builder.create(fgStrHandleClusterBuffer, bufferInfo); + builder.writeToBlackboard(fgStrHandleClusterBuffer, data.clusterBuffer); + } + // only rebuild cluster necceray + if (_rebuildClusters) { + data.clusterBuffer = builder.write(data.clusterBuffer); + builder.writeToBlackboard(fgStrHandleClusterBuffer, data.clusterBuffer); + } + + data.globalIndexBuffer = framegraph::BufferHandle(builder.readFromBlackboard(fgStrHandleClusterGlobalIndexBuffer)); + if (!data.globalIndexBuffer.isValid()) { + uint atomicIndexBufferSize = sizeof(uint); + + framegraph::Buffer::Descriptor bufferInfo; + bufferInfo.usage = gfx::BufferUsageBit::STORAGE; + bufferInfo.memUsage = gfx::MemoryUsageBit::HOST | gfx::MemoryUsageBit::DEVICE; + bufferInfo.size = atomicIndexBufferSize; + bufferInfo.stride = atomicIndexBufferSize; + bufferInfo.flags = gfx::BufferFlagBit::NONE; + data.globalIndexBuffer = builder.create(fgStrHandleClusterGlobalIndexBuffer, bufferInfo); + builder.writeToBlackboard(fgStrHandleClusterGlobalIndexBuffer, data.globalIndexBuffer); + } + // atomic counter for building the light grid + // must be reset to 0 every frame + data.globalIndexBuffer = builder.write(data.globalIndexBuffer); + builder.writeToBlackboard(fgStrHandleClusterGlobalIndexBuffer, data.globalIndexBuffer); + }; + + auto clusterBuildExec = [&](DataClusterBuild const& data, const framegraph::DevicePassResourceTable& table) { + auto* cmdBuff = _pipeline->getCommandBuffers()[0]; + if (_rebuildClusters) { + // building cluster + _buildingDescriptorSet->bindBuffer(0, _constantsBuffer); + _buildingDescriptorSet->bindBuffer(1, table.getWrite(data.clusterBuffer)); + _buildingDescriptorSet->update(); + cmdBuff->bindPipelineState(const_cast(_buildingPipelineState)); + cmdBuff->bindDescriptorSet(0, const_cast(_buildingDescriptorSet)); + cmdBuff->dispatch(_buildingDispatchInfo); + } + // reset global index + _resetCounterDescriptorSet->bindBuffer(0, table.getWrite(data.globalIndexBuffer)); + _resetCounterDescriptorSet->update(); + cmdBuff->bindPipelineState(const_cast(_resetCounterPipelineState)); + cmdBuff->bindDescriptorSet(0, const_cast(_resetCounterDescriptorSet)); + cmdBuff->dispatch(_resetDispatchInfo); + cmdBuff->pipelineBarrier(_resetBarrier); + }; + + struct DataLightCulling { + framegraph::BufferHandle lightBuffer; // light storage buffer + framegraph::BufferHandle lightIndexBuffer; // light index storage buffer + framegraph::BufferHandle lightGridBuffer; // light grid storage buffer + framegraph::BufferHandle clusterBuffer; // cluster storage buffer + framegraph::BufferHandle globalIndexBuffer; // global light index storage buffer + }; + + auto lightCullingSetup = [&](framegraph::PassNodeBuilder& builder, DataLightCulling& data) { + data.lightBuffer = framegraph::BufferHandle(builder.readFromBlackboard(fgStrHandleClusterLightBuffer)); + if (!data.lightBuffer.isValid() || _lightBufferResized) { + framegraph::Buffer::Descriptor bufferInfo; + bufferInfo.usage = gfx::BufferUsageBit::STORAGE; + bufferInfo.memUsage = gfx::MemoryUsageBit::HOST | gfx::MemoryUsageBit::DEVICE; + bufferInfo.size = _lightBufferStride * _lightBufferCount; + bufferInfo.stride = _lightBufferStride; + bufferInfo.flags = gfx::BufferFlagBit::NONE; + data.lightBuffer = builder.create(fgStrHandleClusterLightBuffer, bufferInfo); + builder.writeToBlackboard(fgStrHandleClusterLightBuffer, data.lightBuffer); + _lightBufferResized = false; + } + data.lightBuffer = builder.read(data.lightBuffer); + builder.writeToBlackboard(fgStrHandleClusterLightBuffer, data.lightBuffer); + + data.lightIndexBuffer = framegraph::BufferHandle(builder.readFromBlackboard(fgStrHandleClusterLightIndexBuffer)); + if (!data.lightIndexBuffer.isValid()) { + // light indices belonging to clusters + uint lightIndicesBufferSize = MAX_LIGHTS_PER_CLUSTER * CLUSTER_COUNT * sizeof(int); + + framegraph::Buffer::Descriptor bufferInfo; + bufferInfo.usage = gfx::BufferUsageBit::STORAGE; + bufferInfo.memUsage = gfx::MemoryUsageBit::HOST | gfx::MemoryUsageBit::DEVICE; + bufferInfo.size = lightIndicesBufferSize; + bufferInfo.stride = lightIndicesBufferSize; + bufferInfo.flags = gfx::BufferFlagBit::NONE; + data.lightIndexBuffer = builder.create(fgStrHandleClusterLightIndexBuffer, bufferInfo); + builder.writeToBlackboard(fgStrHandleClusterLightIndexBuffer, data.lightIndexBuffer); + } + data.lightIndexBuffer = builder.write(data.lightIndexBuffer); + builder.writeToBlackboard(fgStrHandleClusterLightIndexBuffer, data.lightIndexBuffer); + + data.lightGridBuffer = framegraph::BufferHandle(builder.readFromBlackboard(fgStrHandleClusterLightGridBuffer)); + if (!data.lightGridBuffer.isValid()) { + // for each cluster: (start index in b_clusterLightIndices, number of point lights, empty, empty) + uint lightGridBufferSize = CLUSTER_COUNT * 4 * sizeof(uint); + + framegraph::Buffer::Descriptor bufferInfo; + bufferInfo.usage = gfx::BufferUsageBit::STORAGE; + bufferInfo.memUsage = gfx::MemoryUsageBit::HOST | gfx::MemoryUsageBit::DEVICE; + bufferInfo.size = lightGridBufferSize; + bufferInfo.stride = lightGridBufferSize; + bufferInfo.flags = gfx::BufferFlagBit::NONE; + data.lightGridBuffer = builder.create(fgStrHandleClusterLightGridBuffer, bufferInfo); + builder.writeToBlackboard(fgStrHandleClusterLightGridBuffer, data.lightGridBuffer); + } + data.lightGridBuffer = builder.write(data.lightGridBuffer); + builder.writeToBlackboard(fgStrHandleClusterLightGridBuffer, data.lightGridBuffer); + + data.clusterBuffer = framegraph::BufferHandle(builder.readFromBlackboard(fgStrHandleClusterBuffer)); + data.clusterBuffer = builder.read(data.clusterBuffer); + builder.writeToBlackboard(fgStrHandleClusterBuffer, data.clusterBuffer); + + data.globalIndexBuffer = framegraph::BufferHandle(builder.readFromBlackboard(fgStrHandleClusterGlobalIndexBuffer)); + // atomic read and write in a pass + data.globalIndexBuffer = builder.read(data.globalIndexBuffer); + builder.writeToBlackboard(fgStrHandleClusterGlobalIndexBuffer, data.globalIndexBuffer); + }; + + auto lightCullingExec = [&](DataLightCulling const& data, const framegraph::DevicePassResourceTable& table) { + auto* cmdBuff = _pipeline->getCommandBuffers()[0]; + cmdBuff->updateBuffer(table.getRead(data.lightBuffer), _lightBufferData.data(), + static_cast(_lightBufferData.size() * sizeof(float))); + + _cullingDescriptorSet->bindBuffer(0, _constantsBuffer); + _cullingDescriptorSet->bindBuffer(1, table.getRead(data.lightBuffer)); + _cullingDescriptorSet->bindBuffer(2, table.getWrite(data.lightIndexBuffer)); + _cullingDescriptorSet->bindBuffer(3, table.getWrite(data.lightGridBuffer)); + _cullingDescriptorSet->bindBuffer(4, table.getRead(data.clusterBuffer)); + _cullingDescriptorSet->bindBuffer(5, table.getRead(data.globalIndexBuffer)); + _cullingDescriptorSet->update(); + // light culling + cmdBuff->bindPipelineState(const_cast(_cullingPipelineState)); + cmdBuff->bindDescriptorSet(0, const_cast(_cullingDescriptorSet)); + cmdBuff->dispatch(_cullingDispatchInfo); + }; + + auto* pipeline = static_cast(_pipeline); + uint insertPoint = static_cast(DeferredInsertPoint::DIP_CLUSTER); + pipeline->getFrameGraph().addPass(insertPoint++, fgStrHandleClusterBuildPass, clusterBuildSetup, clusterBuildExec); + pipeline->getFrameGraph().addPass(insertPoint++, fgStrHandleClusterCullingPass, lightCullingSetup, lightCullingExec); +} + +String& ClusterLightCulling::getShaderSource(ShaderStrings& sources) { + switch (_device->getGfxAPI()) { + case gfx::API::GLES2: + return sources.glsl1; + case gfx::API::GLES3: + return sources.glsl3; + case gfx::API::METAL: + case gfx::API::VULKAN: + return sources.glsl4; + default: break; + } + return sources.glsl4; +} + +} // namespace pipeline +} // namespace cc diff --git a/native/cocos/renderer/pipeline/ClusterLightCulling.h b/native/cocos/renderer/pipeline/ClusterLightCulling.h new file mode 100644 index 0000000000..85b6b73960 --- /dev/null +++ b/native/cocos/renderer/pipeline/ClusterLightCulling.h @@ -0,0 +1,155 @@ +/**************************************************************************** + Copyright (c) 2020-2021 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ +#pragma once + +#include "cocos/renderer/gfx-base/GFXDef.h" +#include "cocos/renderer/gfx-base/GFXDevice.h" +#include "cocos/renderer/pipeline/RenderPipeline.h" +#include "cocos/scene/Pass.h" +#include "cocos/scene/RenderScene.h" + +namespace cc { +namespace pipeline { +struct ShaderStrings { + String glsl4; + String glsl3; + String glsl1; +}; + +class ClusterLightCulling { +public: + explicit ClusterLightCulling(RenderPipeline* pipeline) : _pipeline(pipeline){}; + ~ClusterLightCulling(); + + static constexpr uint CLUSTERS_X = 16; + static constexpr uint CLUSTERS_Y = 8; + static constexpr uint CLUSTERS_Z = 24; + + // limit number of threads (D3D only allows up to 1024, there might also be shared memory limitations) + // shader will be run by 6 work groups + static constexpr uint CLUSTERS_X_THREADS = 16; + static constexpr uint CLUSTERS_Y_THREADS = 8; + static constexpr uint CLUSTERS_Z_THREADS = 4; + + static constexpr uint CLUSTER_COUNT = CLUSTERS_X * CLUSTERS_Y * CLUSTERS_Z; + + static constexpr uint MAX_LIGHTS_PER_CLUSTER = 100; + + static constexpr uint MAX_LIGHTS_GLOBAL = 1000; + + void initialize(gfx::Device* dev); + + void clusterLightCulling(scene::Camera* camera); + + inline const gfx::DescriptorSet* getBuildingDescriptorSet() const { return _buildingDescriptorSet; } + inline const gfx::PipelineState* getBuildingPipelineState() const { return _buildingPipelineState; } + inline const gfx::DescriptorSet* getResetCounterDescriptorSet() const { return _resetCounterDescriptorSet; } + inline const gfx::PipelineState* getResetCounterPipelineState() const { return _resetCounterPipelineState; } + inline const gfx::DescriptorSet* getCullingDescriptorSet() const { return _cullingDescriptorSet; } + inline const gfx::PipelineState* getCullingPipelineState() const { return _cullingPipelineState; } + + inline const gfx::Buffer* getConstantsBuffer() const { return _constantsBuffer; } + + inline const gfx::DispatchInfo& getBuildingDispatchInfo() const { return _buildingDispatchInfo; } + inline const gfx::DispatchInfo& getResetCounterDispatchInfo() const { return _resetDispatchInfo; } + inline const gfx::DispatchInfo& getCullingDispatchInfo() const { return _cullingDispatchInfo; } + + inline bool isInitialized() const { return _initialized; } + +private: + String& getShaderSource(ShaderStrings& sources); + + void initBuildingSatge(); + + void initResetStage(); + + void initCullingStage(); + + void update(); + + void updateLights(); + + static bool isProjMatChange(const Mat4& curProj, const Mat4& oldProj) { + for (uint i = 0; i < sizeof(curProj.m) / sizeof(float); i++) { + if (math::IsNotEqualF(curProj.m[i], oldProj.m[i])) { + return true; + } + } + return false; + } + + gfx::Device* _device{nullptr}; + scene::Camera* _camera{nullptr}; + RenderPipeline* _pipeline{nullptr}; + + gfx::Shader* _buildingShader{nullptr}; + gfx::DescriptorSetLayout* _buildingDescriptorSetLayout{nullptr}; + gfx::PipelineLayout* _buildingPipelineLayout{nullptr}; + gfx::PipelineState* _buildingPipelineState{nullptr}; + gfx::DescriptorSet* _buildingDescriptorSet{nullptr}; + + gfx::Shader* _resetCounterShader{nullptr}; + gfx::DescriptorSetLayout* _resetCounterDescriptorSetLayout{nullptr}; + gfx::PipelineLayout* _resetCounterPipelineLayout{nullptr}; + gfx::PipelineState* _resetCounterPipelineState{nullptr}; + gfx::DescriptorSet* _resetCounterDescriptorSet{nullptr}; + + gfx::Shader* _cullingShader{nullptr}; + gfx::DescriptorSetLayout* _cullingDescriptorSetLayout{nullptr}; + gfx::PipelineLayout* _cullingPipelineLayout{nullptr}; + gfx::PipelineState* _cullingPipelineState{nullptr}; + gfx::DescriptorSet* _cullingDescriptorSet{nullptr}; + + static constexpr uint NEAR_FAR_OFFSET = 0; + static constexpr uint VIEW_PORT_OFFSET = 4; + static constexpr uint MAT_VIEW_OFFSET = 8; + static constexpr uint MAT_PROJ_INV_OFFSET = 24; + + std::array _constants{}; + gfx::Buffer* _constantsBuffer{nullptr}; + + vector _validLights; + std::vector _lightBufferData; + + gfx::GlobalBarrier* _resetBarrier{nullptr}; + + gfx::DispatchInfo _buildingDispatchInfo; + gfx::DispatchInfo _resetDispatchInfo; + gfx::DispatchInfo _cullingDispatchInfo; + + bool _lightBufferResized{false}; + uint _lightBufferStride{0}; + uint _lightBufferCount{0}; + float _lightMeterScale{10000.0F}; + + // only rebuild clusters when camera project matrix changed + bool _rebuildClusters{false}; + vector _oldCamProjMats; + + bool _initialized{false}; +}; + +} // namespace pipeline +} // namespace cc diff --git a/native/cocos/renderer/pipeline/Define.cpp b/native/cocos/renderer/pipeline/Define.cpp index 1880ff1d96..a3bf06bda1 100644 --- a/native/cocos/renderer/pipeline/Define.cpp +++ b/native/cocos/renderer/pipeline/Define.cpp @@ -115,6 +115,8 @@ const gfx::UniformBlock UBOCamera::LAYOUT = { {"cc_fogColor", gfx::Type::FLOAT4, 1}, {"cc_fogBase", gfx::Type::FLOAT4, 1}, {"cc_fogAdd", gfx::Type::FLOAT4, 1}, + {"cc_nearFar", gfx::Type::FLOAT4, 1}, + {"cc_viewPort", gfx::Type::FLOAT4, 1}, }, 1, }; diff --git a/native/cocos/renderer/pipeline/Define.h b/native/cocos/renderer/pipeline/Define.h index fc9e9826a0..4706265729 100644 --- a/native/cocos/renderer/pipeline/Define.h +++ b/native/cocos/renderer/pipeline/Define.h @@ -411,7 +411,9 @@ struct CC_DLL UBOCamera : public Object { static constexpr uint GLOBAL_FOG_COLOR_OFFSET = UBOCamera::AMBIENT_GROUND_OFFSET + 4; static constexpr uint GLOBAL_FOG_BASE_OFFSET = UBOCamera::GLOBAL_FOG_COLOR_OFFSET + 4; static constexpr uint GLOBAL_FOG_ADD_OFFSET = UBOCamera::GLOBAL_FOG_BASE_OFFSET + 4; - static constexpr uint COUNT = UBOCamera::GLOBAL_FOG_ADD_OFFSET + 4; + static constexpr uint GLOBAL_NEAR_FAR_OFFSET = UBOCamera::GLOBAL_FOG_ADD_OFFSET + 4; + static constexpr uint GLOBAL_VIEW_PORT_OFFSET = UBOCamera::GLOBAL_NEAR_FAR_OFFSET + 4; + static constexpr uint COUNT = UBOCamera::GLOBAL_VIEW_PORT_OFFSET + 4; static constexpr uint SIZE = UBOCamera::COUNT * 4; static constexpr uint BINDING = static_cast(PipelineGlobalBindings::UBO_CAMERA); static const gfx::DescriptorSetLayoutBinding DESCRIPTOR; @@ -549,5 +551,9 @@ struct CC_DLL REFLECTIONSTORAGE : public Object { static const String NAME; }; +static constexpr uint CLUSTER_LIGHT_BINDING = 4; +static constexpr uint CLUSTER_LIGHT_INDEX_BINDING = 5; +static constexpr uint CLUSTER_LIGHT_GRID_BINDING = 6; + } // namespace pipeline } // namespace cc diff --git a/native/cocos/renderer/pipeline/PipelineUBO.cpp b/native/cocos/renderer/pipeline/PipelineUBO.cpp index 43c3960ed3..8464799d9f 100644 --- a/native/cocos/renderer/pipeline/PipelineUBO.cpp +++ b/native/cocos/renderer/pipeline/PipelineUBO.cpp @@ -155,6 +155,13 @@ void PipelineUBO::updateCameraUBOView(const RenderPipeline *pipeline, float *out output[UBOCamera::GLOBAL_FOG_ADD_OFFSET + 1] = fog->range; output[UBOCamera::GLOBAL_FOG_ADD_OFFSET + 2] = fog->atten; } + output[UBOCamera::GLOBAL_NEAR_FAR_OFFSET + 0] = static_cast(camera->nearClip); + output[UBOCamera::GLOBAL_NEAR_FAR_OFFSET + 1] = static_cast(camera->farClip); + + output[UBOCamera::GLOBAL_VIEW_PORT_OFFSET + 0] = camera->viewPort.x; + output[UBOCamera::GLOBAL_VIEW_PORT_OFFSET + 1] = camera->viewPort.y; + output[UBOCamera::GLOBAL_VIEW_PORT_OFFSET + 2] = camera->viewPort.z; + output[UBOCamera::GLOBAL_VIEW_PORT_OFFSET + 3] = camera->viewPort.w; } void PipelineUBO::updateShadowUBOView(const RenderPipeline *pipeline, std::array *bufferView, const scene::Camera *camera) { diff --git a/native/cocos/renderer/pipeline/RenderPipeline.cpp b/native/cocos/renderer/pipeline/RenderPipeline.cpp index 9020449132..fe5851f4f9 100644 --- a/native/cocos/renderer/pipeline/RenderPipeline.cpp +++ b/native/cocos/renderer/pipeline/RenderPipeline.cpp @@ -52,8 +52,6 @@ RenderPipeline::RenderPipeline() : _device(gfx::Device::getInstance()) { RenderPipeline::instance = this; - generateConstantMacros(); - _globalDSManager = new GlobalDSManager(); _pipelineUBO = new PipelineUBO(); _pipelineSceneData = new PipelineSceneData(); @@ -75,6 +73,11 @@ bool RenderPipeline::activate(gfx::Swapchain * /*swapchain*/) { _pipelineUBO->activate(_device, this); _pipelineSceneData->activate(_device, this); + // generate macros here rather than construct func because _clusterEnabled + // switch may be changed in root.ts setRenderPipeline() function which is after + // pipeline construct. + generateConstantMacros(); + for (auto *const flow : _flows) { flow->activate(this); } @@ -277,10 +280,12 @@ void RenderPipeline::generateConstantMacros() { _constantMacros = StringUtil::format( R"( #define CC_DEVICE_SUPPORT_FLOAT_TEXTURE %d +#define CC_ENABLE_CLUSTERED_LIGHT_CULLING %d #define CC_DEVICE_MAX_VERTEX_UNIFORM_VECTORS %d #define CC_DEVICE_MAX_FRAGMENT_UNIFORM_VECTORS %d )", _device->hasFeature(gfx::Feature::TEXTURE_FLOAT) ? 1 : 0, + _clusterEnabled ? 1 : 0, _device->getCapabilities().maxVertexUniformVectors, _device->getCapabilities().maxFragmentUniformVectors); } diff --git a/native/cocos/renderer/pipeline/RenderPipeline.h b/native/cocos/renderer/pipeline/RenderPipeline.h index c993c7c36f..769aef59e7 100644 --- a/native/cocos/renderer/pipeline/RenderPipeline.h +++ b/native/cocos/renderer/pipeline/RenderPipeline.h @@ -101,6 +101,9 @@ public: inline scene::Model *getProfiler() const { return _profiler; } inline void setProfiler(scene::Model *value) { _profiler = value; } + inline bool getClusterEnabled() const { return _clusterEnabled; } + inline void setClusterEnabled(bool enable) { _clusterEnabled = enable; } + protected: static RenderPipeline *instance; @@ -131,7 +134,10 @@ protected: framegraph::FrameGraph _fg; map _renderPasses; - bool _bloomEnable{false}; + + // use cluster culling or not + bool _clusterEnabled{false}; + bool _bloomEnable{false}; }; } // namespace pipeline diff --git a/native/cocos/renderer/pipeline/common/Enum.h b/native/cocos/renderer/pipeline/common/Enum.h index e9f98cc980..88624a631d 100644 --- a/native/cocos/renderer/pipeline/common/Enum.h +++ b/native/cocos/renderer/pipeline/common/Enum.h @@ -6,14 +6,15 @@ enum class CommonInsertPoint { }; enum class DeferredInsertPoint { - DIP_GBUFFER = 100, - DIP_LIGHTING = 200, + DIP_CLUSTER = 80, + DIP_GBUFFER = 100, + DIP_LIGHTING = 200, DIP_TRANSPARENT = 220, - DIP_SSPR = 300, + DIP_SSPR = 300, DIP_INVALID }; enum class ForwardInsertPoint { IP_FORWARD = 100, IP_INVALID -}; \ No newline at end of file +}; diff --git a/native/cocos/renderer/pipeline/deferred/DeferredPipeline.cpp b/native/cocos/renderer/pipeline/deferred/DeferredPipeline.cpp index f6826468b4..e41a0967e6 100644 --- a/native/cocos/renderer/pipeline/deferred/DeferredPipeline.cpp +++ b/native/cocos/renderer/pipeline/deferred/DeferredPipeline.cpp @@ -59,7 +59,6 @@ framegraph::StringHandle DeferredPipeline::fgStrHandleLightingPass = framegra framegraph::StringHandle DeferredPipeline::fgStrHandleTransparentPass = framegraph::FrameGraph::stringToHandle("deferredTransparentPass"); framegraph::StringHandle DeferredPipeline::fgStrHandleSsprPass = framegraph::FrameGraph::stringToHandle("deferredSSPRPass"); - bool DeferredPipeline::initialize(const RenderPipelineInfo &info) { RenderPipeline::initialize(info); @@ -72,7 +71,6 @@ bool DeferredPipeline::initialize(const RenderPipelineInfo &info) { mainFlow->initialize(MainFlow::getInitializeInfo()); _flows.emplace_back(mainFlow); } - return true; } @@ -102,6 +100,11 @@ void DeferredPipeline::render(const vector &cameras) { sceneCulling(this, camera); _fg.reset(); + + if (_clusterEnabled) { + _clusterComp->clusterLightCulling(camera); + } + for (auto *const flow : _flows) { flow->render(camera); } @@ -162,6 +165,12 @@ bool DeferredPipeline::activeRenderer(gfx::Swapchain *swapchain) { _width = swapchain->getWidth(); _height = swapchain->getHeight(); + if (_clusterEnabled) { + // cluster component resource + _clusterComp = new ClusterLightCulling(this); + _clusterComp->initialize(this->getDevice()); + } + return true; } @@ -175,6 +184,8 @@ void DeferredPipeline::destroy() { _commandBuffers.clear(); + CC_SAFE_DELETE(_clusterComp); + RenderPipeline::destroy(); } diff --git a/native/cocos/renderer/pipeline/deferred/DeferredPipeline.h b/native/cocos/renderer/pipeline/deferred/DeferredPipeline.h index 2d9d9e6337..3dcff634a3 100644 --- a/native/cocos/renderer/pipeline/deferred/DeferredPipeline.h +++ b/native/cocos/renderer/pipeline/deferred/DeferredPipeline.h @@ -30,9 +30,10 @@ #include #include "gfx-base/GFXBuffer.h" #include "gfx-base/GFXInputAssembler.h" +#include "pipeline/ClusterLightCulling.h" #include "pipeline/RenderPipeline.h" -#include "scene/RenderWindow.h" #include "pipeline/common/Enum.h" +#include "scene/RenderWindow.h" namespace cc { namespace pipeline { @@ -59,11 +60,13 @@ public: private: bool activeRenderer(gfx::Swapchain *swapchain); - gfx::Buffer * _lightsUBO = nullptr; - LightList _validLights; - gfx::BufferList _lightBuffers; - UintList _lightIndexOffsets; - UintList _lightIndices; + gfx::Buffer * _lightsUBO = nullptr; + LightList _validLights; + gfx::BufferList _lightBuffers; + UintList _lightIndexOffsets; + UintList _lightIndices; + + ClusterLightCulling *_clusterComp{nullptr}; public: static constexpr uint GBUFFER_COUNT = 4; diff --git a/native/cocos/renderer/pipeline/deferred/LightingStage.cpp b/native/cocos/renderer/pipeline/deferred/LightingStage.cpp index 7aeb48c46d..ff03f70fc7 100644 --- a/native/cocos/renderer/pipeline/deferred/LightingStage.cpp +++ b/native/cocos/renderer/pipeline/deferred/LightingStage.cpp @@ -59,6 +59,10 @@ framegraph::StringHandle ssprCompReflectPass[MAX_REFLECTOR_SIZE]; framegraph::StringHandle ssprCompDenoisePass[MAX_REFLECTOR_SIZE]; framegraph::StringHandle ssprRenderPass[MAX_REFLECTOR_SIZE]; +framegraph::StringHandle fgStrHandleClusterLightBuffer = framegraph::FrameGraph::stringToHandle("clusterLightBuffer"); +framegraph::StringHandle fgStrHandleClusterLightIndexBuffer = framegraph::FrameGraph::stringToHandle("lightIndexBuffer"); +framegraph::StringHandle fgStrHandleClusterLightGridBuffer = framegraph::FrameGraph::stringToHandle("lightGridBuffer"); + void initStrHandle() { std::string tmp; for (int i = 0; i < MAX_REFLECTOR_SIZE; ++i) { @@ -274,15 +278,18 @@ void LightingStage::activate(RenderPipeline *pipeline, RenderFlow *flow) { _renderQueues.emplace_back(CC_NEW(RenderQueue(std::move(info)))); } - // create descriptor set/layout - gfx::DescriptorSetLayoutInfo layoutInfo = {localDescriptorSetLayout.bindings}; - _descLayout = device->createDescriptorSetLayout(layoutInfo); + // not use cluster shading, go normal deferred render path + if (!pipeline->getClusterEnabled()) { + // create descriptor set/layout + gfx::DescriptorSetLayoutInfo layoutInfo = {localDescriptorSetLayout.bindings}; + _descLayout = device->createDescriptorSetLayout(layoutInfo); - gfx::DescriptorSetInfo setInfo = {_descLayout}; - _descriptorSet = device->createDescriptorSet(setInfo); + gfx::DescriptorSetInfo setInfo = {_descLayout}; + _descriptorSet = device->createDescriptorSet(setInfo); - // create lighting buffer and view - initLightingBuffer(); + // create lighting buffer and view + initLightingBuffer(); + } _planarShadowQueue = CC_NEW(PlanarShadowQueue(_pipeline)); @@ -313,14 +320,21 @@ void LightingStage::destroy() { } void LightingStage::fgLightingPass(scene::Camera *camera) { - // lighting info, ubo - gatherLights(camera); - _descriptorSet->update(); + // lights info and ubo are updated in ClusterLightCulling::update() + // if using cluster lighting. + if (!_pipeline->getClusterEnabled()) { + // lighting info, ubo + gatherLights(camera); + _descriptorSet->update(); + } struct RenderData { - framegraph::TextureHandle gbuffer[4]; // read from gbuffer stage - framegraph::TextureHandle outputTex; // output texture + framegraph::TextureHandle gbuffer[4]; // read from gbuffer stage + framegraph::TextureHandle outputTex; // output texture framegraph::TextureHandle depth; + framegraph::BufferHandle lightBuffer; // light storage buffer + framegraph::BufferHandle lightIndexBuffer; // light index storage buffer + framegraph::BufferHandle lightGridBuffer; // light grid storage buffer }; auto * pipeline = static_cast(_pipeline); @@ -344,6 +358,16 @@ void LightingStage::fgLightingPass(scene::Camera *camera) { data.depth = builder.write(data.depth, depthAttachmentInfo); builder.writeToBlackboard(RenderPipeline::fgStrHandleOutDepthTexture, data.depth); + if (_pipeline->getClusterEnabled()) { + // read cluster and light info + data.lightBuffer = framegraph::BufferHandle(builder.readFromBlackboard(fgStrHandleClusterLightBuffer)); + builder.read(data.lightBuffer); + data.lightIndexBuffer = framegraph::BufferHandle(builder.readFromBlackboard(fgStrHandleClusterLightIndexBuffer)); + builder.read(data.lightIndexBuffer); + data.lightGridBuffer = framegraph::BufferHandle(builder.readFromBlackboard(fgStrHandleClusterLightGridBuffer)); + builder.read(data.lightGridBuffer); + } + // write to lighting output framegraph::Texture::Descriptor colorTexInfo; colorTexInfo.format = gfx::Format::RGBA16F; @@ -358,7 +382,7 @@ void LightingStage::fgLightingPass(scene::Camera *camera) { colorAttachmentInfo.clearColor = clearColor; colorAttachmentInfo.beginAccesses = {gfx::AccessType::FRAGMENT_SHADER_READ_TEXTURE}; colorAttachmentInfo.endAccesses = {gfx::AccessType::FRAGMENT_SHADER_READ_TEXTURE}; - data.outputTex = builder.write(data.outputTex, colorAttachmentInfo); + data.outputTex = builder.write(data.outputTex, colorAttachmentInfo); builder.writeToBlackboard(RenderPipeline::fgStrHandleOutColorTexture, data.outputTex); // set render area @@ -369,9 +393,13 @@ void LightingStage::fgLightingPass(scene::Camera *camera) { auto * pipeline = static_cast(_pipeline); auto *const sceneData = pipeline->getPipelineSceneData(); - auto * cmdBuff = pipeline->getCommandBuffers()[0]; - vector dynamicOffsets = {0}; - cmdBuff->bindDescriptorSet(localSet, _descriptorSet, dynamicOffsets); + auto *cmdBuff = pipeline->getCommandBuffers()[0]; + + // no need to bind localSet in cluster + if (!_pipeline->getClusterEnabled()) { + vector dynamicOffsets = {0}; + cmdBuff->bindDescriptorSet(localSet, _descriptorSet, dynamicOffsets); + } const std::array globalOffsets = {_pipeline->getPipelineUBO()->getCurrentCameraUBOOffset()}; cmdBuff->bindDescriptorSet(globalSet, pipeline->getDescriptorSet(), utils::toUint(globalOffsets.size()), globalOffsets.data()); @@ -388,6 +416,14 @@ void LightingStage::fgLightingPass(scene::Camera *camera) { pass->getDescriptorSet()->bindTexture(i, table.getRead(data.gbuffer[i])); pass->getDescriptorSet()->bindSampler(i, _defaultSampler); } + + if (_pipeline->getClusterEnabled()) { + // cluster buffer bind + pass->getDescriptorSet()->bindBuffer(CLUSTER_LIGHT_BINDING, table.getRead(data.lightBuffer)); + pass->getDescriptorSet()->bindBuffer(CLUSTER_LIGHT_INDEX_BINDING, table.getRead(data.lightIndexBuffer)); + pass->getDescriptorSet()->bindBuffer(CLUSTER_LIGHT_GRID_BINDING, table.getRead(data.lightGridBuffer)); + } + pass->getDescriptorSet()->update(); cmdBuff->bindPipelineState(pState); @@ -416,7 +452,7 @@ void LightingStage::fgTransparent(scene::Camera *camera) { colorAttachmentInfo.beginAccesses = {gfx::AccessType::FRAGMENT_SHADER_READ_TEXTURE}; colorAttachmentInfo.endAccesses = {gfx::AccessType::FRAGMENT_SHADER_READ_TEXTURE}; - data.outputTex = framegraph::TextureHandle(builder.readFromBlackboard(DeferredPipeline::fgStrHandleOutColorTexture)); + data.outputTex = framegraph::TextureHandle(builder.readFromBlackboard(DeferredPipeline::fgStrHandleOutColorTexture)); bool lightingPassValid = data.outputTex.isValid(); if (!lightingPassValid) { framegraph::Texture::Descriptor colorTexInfo; diff --git a/native/cocos/scene/Camera.h b/native/cocos/scene/Camera.h index 5a86a087fb..8ea61afd92 100644 --- a/native/cocos/scene/Camera.h +++ b/native/cocos/scene/Camera.h @@ -43,6 +43,8 @@ class RenderScene; struct Camera final { uint32_t width{0}; uint32_t height{0}; + uint32_t nearClip{0}; + uint32_t farClip{0}; uint32_t clearFlag{0}; float exposure{0.0F}; float clearDepth{0.0F}; diff --git a/native/tools/tojs/pipeline.ini b/native/tools/tojs/pipeline.ini index 01143cd199..a02b7d43b9 100644 --- a/native/tools/tojs/pipeline.ini +++ b/native/tools/tojs/pipeline.ini @@ -60,7 +60,7 @@ skip = ForwardPipeline::[getOrCreateRenderPass getLightsUBO getValidLights getLi rename_functions = -getter_setter= RenderPipeline::[globalDSManager descriptorSet descriptorSetLayout constantMacros] +getter_setter= RenderPipeline::[globalDSManager descriptorSet descriptorSetLayout constantMacros clusterEnabled] rename_classes =