merge valid punctual light culling for v3.4.0 (#4048)

* merge valid punctual light culling for v3.4.0
This commit is contained in:
troublemaker52025
2021-11-18 11:18:02 +08:00
committed by GitHub
parent 7ac99a900a
commit 7bf2fc0fba
9 changed files with 111 additions and 89 deletions
@@ -57,6 +57,8 @@ public:
inline void setDirShadowObjects(RenderObjectList &&ro) { _dirShadowObjects = std::forward<RenderObjectList>(ro); }
inline const RenderObjectList & getCastShadowObjects() const { return _castShadowObjects; }
inline void setCastShadowObjects(RenderObjectList &&ro) { _castShadowObjects = std::forward<RenderObjectList>(ro); }
inline const vector<const scene::Light*> & getValidPunctualLights() const { return _validPunctualLights; }
inline void setValidPunctualLights(vector<const scene::Light*> &&validPunctualLights) { _validPunctualLights = std::forward<vector<const scene::Light*>>(validPunctualLights); }
inline float getShadowCameraFar() const { return _shadowCameraFar; }
inline void setShadowCameraFar(float shadowDistance) { _shadowCameraFar = shadowDistance; }
inline Mat4 getMatShadowView() const { return _matShadowView; }
@@ -67,9 +69,10 @@ public:
inline void setMatShadowViewProj(const Mat4 &matShadowViewProj) { _matShadowViewProj = matShadowViewProj; }
private:
RenderObjectList _renderObjects;
RenderObjectList _dirShadowObjects;
RenderObjectList _castShadowObjects;
RenderObjectList _renderObjects;
RenderObjectList _dirShadowObjects;
RenderObjectList _castShadowObjects;
vector<const scene::Light*> _validPunctualLights;
scene::PipelineSharedSceneData *_sharedSceneData = nullptr;
RenderPipeline * _pipeline = nullptr;
@@ -39,7 +39,6 @@
#include "forward/ForwardPipeline.h"
#include "gfx-base/GFXDevice.h"
#include "scene/RenderScene.h"
#include "scene/Sphere.h"
namespace cc {
namespace pipeline {
@@ -69,14 +68,13 @@ RenderAdditiveLightQueue ::~RenderAdditiveLightQueue() {
CC_SAFE_DELETE(_batchedQueue);
CC_SAFE_DESTROY(_firstLightBufferView);
CC_SAFE_DESTROY(_lightBuffer);
destroy();
}
void RenderAdditiveLightQueue::recordCommandBuffer(gfx::Device *device, scene::Camera *camera, gfx::RenderPass *renderPass, gfx::CommandBuffer *cmdBuffer) {
_instancedQueue->recordCommandBuffer(device, renderPass, cmdBuffer);
_batchedQueue->recordCommandBuffer(device, renderPass, cmdBuffer);
bool enableOcclusionQuery = _pipeline->getOcclusionQueryEnabled();
auto offset = _pipeline->getPipelineUBO()->getCurrentCameraUBOOffset();
const bool enableOcclusionQuery = _pipeline->getOcclusionQueryEnabled();
const auto offset = _pipeline->getPipelineUBO()->getCurrentCameraUBOOffset();
for (const auto &lightPass : _lightPasses) {
const auto *const subModel = lightPass.subModel;
@@ -111,9 +109,9 @@ void RenderAdditiveLightQueue::gatherLightPasses(const scene::Camera *camera, gf
clear();
gatherValidLights(camera);
_validPunctualLights = _pipeline->getPipelineSceneData()->getValidPunctualLights();
if (_validLights.empty()) return;
if (_validPunctualLights.empty()) return;
updateUBOs(camera, cmdBuffer);
updateLightDescriptorSet(camera, cmdBuffer);
@@ -146,13 +144,9 @@ void RenderAdditiveLightQueue::gatherLightPasses(const scene::Camera *camera, gf
_batchedQueue->uploadBuffers(cmdBuffer);
}
void RenderAdditiveLightQueue::destroy() const {
}
void RenderAdditiveLightQueue::clear() {
_instancedQueue->clear();
_batchedQueue->clear();
_validLights.clear();
for (auto lightPass : _lightPasses) {
lightPass.dynamicOffsets.clear();
@@ -161,27 +155,6 @@ void RenderAdditiveLightQueue::clear() {
_lightPasses.clear();
}
void RenderAdditiveLightQueue::gatherValidLights(const scene::Camera *camera) {
const auto *const scene = camera->scene;
scene::Sphere sphere;
for (auto *light : scene->getSphereLights()) {
sphere.setCenter(light->getPosition());
sphere.setRadius(light->getRange());
if (sphere.sphereFrustum(camera->frustum)) {
_validLights.emplace_back(static_cast<scene::Light *>(light));
}
}
for (auto *light : scene->getSpotLights()) {
sphere.setCenter(light->getPosition());
sphere.setRadius(light->getRange());
if (sphere.sphereFrustum(camera->frustum)) {
_validLights.emplace_back(static_cast<scene::Light *>(light));
}
}
}
bool RenderAdditiveLightQueue::cullSphereLight(const scene::SphereLight *light, const scene::Model *model) {
return model->getWorldBounds() && !model->getWorldBounds()->aabbAabb(light->getAABB());
}
@@ -193,14 +166,14 @@ bool RenderAdditiveLightQueue::cullSpotLight(const scene::SpotLight *light, cons
void RenderAdditiveLightQueue::addRenderQueue(const scene::Pass *pass, const scene::SubModel *subModel, const scene::Model *model, uint lightPassIdx) {
const auto batchingScheme = pass->getBatchingScheme();
if (batchingScheme == scene::BatchingSchemes::INSTANCING) { // instancing
for (auto idx : _lightIndices) {
for (const auto idx : _lightIndices) {
auto *buffer = InstancedBuffer::get(subModel->getPass(lightPassIdx), idx);
buffer->merge(model, subModel, lightPassIdx);
buffer->setDynamicOffset(0, _lightBufferStride * idx);
_instancedQueue->add(buffer);
}
} else if (batchingScheme == scene::BatchingSchemes::VB_MERGING) { // vb-merging
for (auto idx : _lightIndices) {
for (const auto idx : _lightIndices) {
auto *buffer = BatchedBuffer::get(subModel->getPass(lightPassIdx), idx);
buffer->merge(subModel, lightPassIdx, model);
buffer->setDynamicOffset(0, _lightBufferStride * idx);
@@ -224,11 +197,11 @@ void RenderAdditiveLightQueue::addRenderQueue(const scene::Pass *pass, const sce
}
void RenderAdditiveLightQueue::updateUBOs(const scene::Camera *camera, gfx::CommandBuffer *cmdBuffer) {
const auto exposure = camera->exposure;
const auto validLightCount = _validLights.size();
auto *const sceneData = _pipeline->getPipelineSceneData();
const auto exposure = camera->exposure;
const auto validLightCount = _validPunctualLights.size();
const auto *sceneData = _pipeline->getPipelineSceneData();
auto *const sharedData = sceneData->getSharedData();
const auto *sharedData = sceneData->getSharedData();
const auto *shadowInfo = sharedData->shadow;
size_t offset = 0;
if (validLightCount > _lightBufferCount) {
@@ -241,10 +214,10 @@ void RenderAdditiveLightQueue::updateUBOs(const scene::Camera *camera, gfx::Comm
}
for (unsigned l = 0; l < validLightCount; l++, offset += _lightBufferElementCount) {
auto * light = _validLights[l];
const auto *light = _validPunctualLights[l];
const bool isSpotLight = scene::LightType::SPOT == light->getType();
const auto *spotLight = isSpotLight ? static_cast<scene::SpotLight *>(light) : nullptr;
const auto *sphereLight = isSpotLight ? nullptr : static_cast<scene::SphereLight *>(light);
const auto *spotLight = isSpotLight ? static_cast<const scene::SpotLight *>(light) : nullptr;
const auto *sphereLight = isSpotLight ? nullptr : static_cast<const scene::SphereLight *>(light);
auto index = offset + UBOForwardLight::LIGHT_POS_OFFSET;
const auto &position = isSpotLight ? spotLight->getPosition() : sphereLight->getPosition();
@@ -269,8 +242,8 @@ void RenderAdditiveLightQueue::updateUBOs(const scene::Camera *camera, gfx::Comm
_lightBufferData[index++] = color.z;
}
float luminanceHDR = isSpotLight ? spotLight->getLuminanceHDR() : sphereLight->getLuminanceHDR();
float luminanceLDR = isSpotLight ? spotLight->getLuminanceLDR() : sphereLight->getLuminanceLDR();
const float luminanceHDR = isSpotLight ? spotLight->getLuminanceHDR() : sphereLight->getLuminanceHDR();
const float luminanceLDR = isSpotLight ? spotLight->getLuminanceLDR() : sphereLight->getLuminanceLDR();
if (sharedData->isHDR) {
_lightBufferData[index] = luminanceHDR * exposure * _lightMeterScale;
} else {
@@ -303,7 +276,7 @@ void RenderAdditiveLightQueue::updateUBOs(const scene::Camera *camera, gfx::Comm
}
void RenderAdditiveLightQueue::updateLightDescriptorSet(const scene::Camera *camera, gfx::CommandBuffer *cmdBuffer) {
auto *const sceneData = _pipeline->getPipelineSceneData();
const auto * sceneData = _pipeline->getPipelineSceneData();
auto * shadowInfo = sceneData->getSharedData()->shadow;
const auto *const scene = camera->scene;
auto * device = gfx::Device::getInstance();
@@ -312,8 +285,8 @@ void RenderAdditiveLightQueue::updateLightDescriptorSet(const scene::Camera *cam
const float packing = hFTexture ? 0.0F : 1.0F;
const scene::Light *mainLight = scene->getMainLight();
for (uint i = 0; i < _validLights.size(); ++i) {
const auto *light = _validLights[i];
for (uint i = 0; i < _validPunctualLights.size(); ++i) {
const auto *light = _validPunctualLights[i];
auto * descriptorSet = _pipeline->getGlobalDSManager()->getOrCreateDescriptorSet(i);
if (!descriptorSet) {
continue;
@@ -388,7 +361,7 @@ void RenderAdditiveLightQueue::updateLightDescriptorSet(const scene::Camera *cam
break;
}
const float color[4] = {shadowInfo->color.x, shadowInfo->color.y, shadowInfo->color.z, shadowInfo->color.w};
float color[4] = {shadowInfo->color.x, shadowInfo->color.y, shadowInfo->color.z, shadowInfo->color.w};
memcpy(_shadowUBO.data() + UBOShadow::SHADOW_COLOR_OFFSET, &color, sizeof(float) * 4);
descriptorSet->update();
@@ -401,9 +374,9 @@ bool RenderAdditiveLightQueue::getLightPassIndex(const scene::Model *model, vect
lightPassIndices->clear();
bool hasValidLightPass = false;
for (auto *subModel : model->getSubModels()) {
for (const auto *subModel : model->getSubModels()) {
int lightPassIndex = 0;
for (auto *pass : subModel->getPasses()) {
for (const auto *pass : subModel->getPasses()) {
if (pass->getPhase() == _phaseID) {
hasValidLightPass = true;
break;
@@ -418,8 +391,8 @@ bool RenderAdditiveLightQueue::getLightPassIndex(const scene::Model *model, vect
void RenderAdditiveLightQueue::lightCulling(const scene::Model *model) {
bool isCulled = false;
for (size_t i = 0; i < _validLights.size(); i++) {
const auto *const light = _validLights[i];
for (size_t i = 0; i < _validPunctualLights.size(); i++) {
const auto *const light = _validPunctualLights[i];
switch (light->getType()) {
case scene::LightType::SPHERE:
isCulled = cullSphereLight(static_cast<const scene::SphereLight *>(light), model);
@@ -60,25 +60,22 @@ public:
void recordCommandBuffer(gfx::Device *device, scene::Camera *camera, gfx::RenderPass *renderPass, gfx::CommandBuffer *cmdBuffer);
void gatherLightPasses(const scene::Camera *camera, gfx::CommandBuffer *cmdBuffer);
void destroy() const;
private:
static bool cullSphereLight(const scene::SphereLight *light, const scene::Model *model);
static bool cullSpotLight(const scene::SpotLight *light, const scene::Model *model);
void clear();
void gatherValidLights(const scene::Camera *camera);
void addRenderQueue(const scene::Pass *pass, const scene::SubModel *subModel, const scene::Model *model, uint lightPassIdx);
void updateUBOs(const scene::Camera *camera, gfx::CommandBuffer *cmdBuffer);
void updateLightDescriptorSet(const scene::Camera *camera, gfx::CommandBuffer *cmdBuffer);
bool getLightPassIndex(const scene::Model *model, vector<uint> *lightPassIndices) const;
void lightCulling(const scene::Model *model);
gfx::DescriptorSet *getOrCreateDescriptorSet(const scene::Light *light);
RenderPipeline * _pipeline = nullptr;
vector<vector<scene::SubModel *>> _sortedSubModelsArray;
vector<vector<uint>> _sortedPSOCIArray;
vector<scene::Light *> _validLights;
vector<const scene::Light *> _validPunctualLights;
vector<uint> _lightIndices;
vector<AdditiveLightPass> _lightPasses;
vector<uint> _dynamicOffsets;
+20 -13
View File
@@ -143,22 +143,30 @@ void updateDirLight(scene::Shadow *shadows, const scene::Light *light, std::arra
memcpy(shadowUBO->data() + UBOShadow::SHADOW_COLOR_OFFSET, &color, sizeof(color));
}
void lightCollecting(scene::Camera *camera, std::vector<const scene::Light *> *validLights) {
validLights->clear();
auto * sphere = CC_NEW(scene::Sphere);
const auto * scene = camera->scene;
const scene::Light *mainLight = scene->getMainLight();
validLights->emplace_back(mainLight);
void validPunctualLightsCulling(RenderPipeline *pipeline, scene::Camera *camera) {
const auto *const scene = camera->scene;
PipelineSceneData * sceneData = pipeline->getPipelineSceneData();
vector<const scene::Light*> validPunctualLights = sceneData->getValidPunctualLights();
validPunctualLights.clear();
for (auto *spotLight : scene->getSpotLights()) {
sphere->setCenter(spotLight->getPosition());
sphere->setRadius(spotLight->getRange());
if (sphere->interset(camera->frustum)) {
validLights->emplace_back(static_cast<scene::Light *>(spotLight));
scene::Sphere sphere;
for (auto *light : scene->getSpotLights()) {
sphere.setCenter(light->getPosition());
sphere.setRadius(light->getRange());
if (sphere.sphereFrustum(camera->frustum)) {
validPunctualLights.emplace_back(static_cast<scene::Light *>(light));
}
}
CC_SAFE_DELETE(sphere)
for (auto *light : scene->getSphereLights()) {
sphere.setCenter(light->getPosition());
sphere.setRadius(light->getRange());
if (sphere.sphereFrustum(camera->frustum)) {
validPunctualLights.emplace_back(static_cast<scene::Light *>(light));
}
}
sceneData->setValidPunctualLights(std::move(validPunctualLights));
}
Mat4 getCameraWorldMatrix(const scene::Camera *camera) {
@@ -307,7 +315,6 @@ void sceneCulling(RenderPipeline *pipeline, scene::Camera *camera) {
}
}
scene::AABB ab;
RenderObjectList renderObjects;
RenderObjectList castShadowObject;
@@ -42,7 +42,7 @@ class RenderPipeline;
RenderObject genRenderObject(const scene::Model *, const scene::Camera *);
void quantizeDirLightShadowCamera(RenderPipeline *pipeline, const scene::Camera *camera, scene::Frustum *out);
void lightCollecting(scene::Camera *, std::vector<const scene::Light *> *);
void validPunctualLightsCulling(RenderPipeline *pipeline, scene::Camera *camera);
void sceneCulling(RenderPipeline *, scene::Camera *);
void updateSphereLight(scene::Shadow *shadows, const scene::Light *light, std::array<float, UBOShadow::COUNT> *);
void updateDirLight(scene::Shadow *shadows, const scene::Light *light, std::array<float, UBOShadow::COUNT> *);
@@ -67,7 +67,7 @@ void ShadowMapBatchedQueue::gatherLightPasses(gfx::DescriptorSet *globalDS, cons
} break;
case scene::LightType::SPOT: {
const auto *spotLight = dynamic_cast<const scene::SpotLight *>(light);
const auto *spotLight = static_cast<const scene::SpotLight *>(light);
const Mat4 matShadowView = light->getNode()->getWorldMatrix().getInversed();
Mat4 matShadowProj;
Mat4::createPerspective(spotLight->getSpotAngle(), spotLight->getAspect(), 0.001F, spotLight->getRange(), &matShadowProj);
@@ -83,8 +83,8 @@ bool ForwardPipeline::activate(gfx::Swapchain *swapchain) {
}
void ForwardPipeline::render(const vector<scene::Camera *> &cameras) {
auto *device = gfx::Device::getInstance();
bool enableOcclusionQuery = getOcclusionQueryEnabled();
auto * device = gfx::Device::getInstance();
const bool enableOcclusionQuery = getOcclusionQueryEnabled();
if (enableOcclusionQuery) {
device->getQueryPoolResults(_queryPools[0]);
}
@@ -101,6 +101,7 @@ void ForwardPipeline::render(const vector<scene::Camera *> &cameras) {
decideProfilerCamera(cameras);
for (auto *camera : cameras) {
validPunctualLightsCulling(this, camera);
sceneCulling(this, camera);
for (auto *const flow : _flows) {
flow->render(camera);
@@ -125,7 +126,7 @@ void ForwardPipeline::render(const vector<scene::Camera *> &cameras) {
bool ForwardPipeline::activeRenderer(gfx::Swapchain *swapchain) {
_commandBuffers.push_back(_device->getCommandBuffer());
_queryPools.push_back(_device->getQueryPool());
auto *const sharedData = _pipelineSceneData->getSharedData();
const auto *sharedData = _pipelineSceneData->getSharedData();
gfx::Sampler *const sampler = getGlobalDSManager()->getPointSampler();
@@ -33,6 +33,7 @@
#include "gfx-base/GFXFramebuffer.h"
#include "gfx-base/GFXRenderPass.h"
#include "gfx-base/GFXTexture.h"
#include "scene/RenderScene.h"
namespace cc {
namespace pipeline {
@@ -68,7 +69,7 @@ void ShadowFlow::render(scene::Camera *camera) {
auto * shadowInfo = sceneData->getSharedData()->shadow;
if (!shadowInfo->enabled || shadowInfo->shadowType != scene::ShadowType::SHADOWMAP) return;
lightCollecting(camera, &_validLights);
lightCollecting();
if (sceneData->getDirShadowObjects().empty() && sceneData->getRenderObjects().empty()) {
clearShadowMap(camera);
@@ -79,12 +80,25 @@ void ShadowFlow::render(scene::Camera *camera) {
resizeShadowMap(&shadowInfo);
}
const auto &shadowFramebufferMap = sceneData->getShadowFramebufferMap();
const auto & shadowFramebufferMap = sceneData->getShadowFramebufferMap();
const scene::DirectionalLight *mainLight = camera->scene->getMainLight();
if (mainLight) {
gfx::DescriptorSet *globalDS = _pipeline->getDescriptorSet();
if (!shadowFramebufferMap.count(mainLight)) {
initShadowFrameBuffer(_pipeline, mainLight);
}
auto *shadowFrameBuffer = shadowFramebufferMap.at(mainLight);
for (auto *stage : _stages) {
auto *shadowStage = static_cast<ShadowStage *>(stage);
shadowStage->setUsage(globalDS, mainLight, shadowFrameBuffer);
shadowStage->render(camera);
}
}
for (uint l = 0; l < _validLights.size(); ++l) {
const scene::Light *light = _validLights[l];
const bool isMainLight = light->getType() == scene::LightType::DIRECTIONAL;
gfx::DescriptorSet *globalDS = isMainLight ? _pipeline->getDescriptorSet()
: _pipeline->getGlobalDSManager()->getOrCreateDescriptorSet(l - 1);
const scene::Light *light = _validLights[l];
gfx::DescriptorSet *globalDS = _pipeline->getGlobalDSManager()->getOrCreateDescriptorSet(l);
if (!shadowFramebufferMap.count(light)) {
initShadowFrameBuffer(_pipeline, light);
@@ -93,21 +107,46 @@ void ShadowFlow::render(scene::Camera *camera) {
auto *shadowFrameBuffer = shadowFramebufferMap.at(light);
for (auto *stage : _stages) {
auto *shadowStage = dynamic_cast<ShadowStage *>(stage);
auto *shadowStage = static_cast<ShadowStage *>(stage);
shadowStage->setUsage(globalDS, light, shadowFrameBuffer);
shadowStage->render(camera);
}
}
}
void ShadowFlow::lightCollecting() {
_validLights.clear();
const vector<const scene::Light*> validPunctualLights = _pipeline->getPipelineSceneData()->getValidPunctualLights();
for (const scene::Light *light : validPunctualLights) {
if (light->getType() == scene::LightType::SPOT) {
_validLights.emplace_back(light);
}
}
}
void ShadowFlow::clearShadowMap(scene::Camera *camera) {
const auto *sceneData = _pipeline->getPipelineSceneData();
const auto &shadowFramebufferMap = sceneData->getShadowFramebufferMap();
const auto * sceneData = _pipeline->getPipelineSceneData();
const auto & shadowFramebufferMap = sceneData->getShadowFramebufferMap();
const scene::DirectionalLight *mainLight = camera->scene->getMainLight();
if (mainLight) {
gfx::DescriptorSet *globalDS = _pipeline->getDescriptorSet();
if (!shadowFramebufferMap.count(mainLight)) {
initShadowFrameBuffer(_pipeline, mainLight);
}
auto *shadowFrameBuffer = shadowFramebufferMap.at(mainLight);
for (auto *stage : _stages) {
auto *shadowStage = static_cast<ShadowStage *>(stage);
shadowStage->setUsage(globalDS, mainLight, shadowFrameBuffer);
shadowStage->render(camera);
}
}
for (uint l = 0; l < _validLights.size(); ++l) {
const scene::Light *light = _validLights[l];
const bool isMainLight = light->getType() == scene::LightType::DIRECTIONAL;
gfx::DescriptorSet *globalDS = isMainLight ? _pipeline->getDescriptorSet()
: _pipeline->getGlobalDSManager()->getOrCreateDescriptorSet(l - 1);
const scene::Light *light = _validLights[l];
gfx::DescriptorSet *globalDS = _pipeline->getGlobalDSManager()->getOrCreateDescriptorSet(l);
if (!shadowFramebufferMap.count(light)) {
continue;
@@ -115,7 +154,7 @@ void ShadowFlow::clearShadowMap(scene::Camera *camera) {
auto *shadowFrameBuffer = shadowFramebufferMap.at(light);
for (auto *stage : _stages) {
auto *shadowStage = dynamic_cast<ShadowStage *>(stage);
auto *shadowStage = static_cast<ShadowStage *>(stage);
shadowStage->setUsage(globalDS, light, shadowFrameBuffer);
shadowStage->clearFramebuffer(camera);
}
@@ -49,6 +49,8 @@ public:
void destroy() override;
private:
void lightCollecting();
void clearShadowMap(scene::Camera *camera);
void resizeShadowMap(scene::Shadow **shadowInfo);