Skip to content

Commit

Permalink
refine code
Browse files Browse the repository at this point in the history
  • Loading branch information
2youyou2 committed May 16, 2024
1 parent d64529b commit 45bd05c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 44 deletions.
8 changes: 8 additions & 0 deletions native/cocos/core/Root.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
#include "scene/SpotLight.h"
#include "scene/Skybox.h"

#include "renderer/pipeline/PipelineUBO.h"

namespace cc {

namespace {
Expand Down Expand Up @@ -354,6 +356,12 @@ bool Root::setRenderPipeline(pipeline::RenderPipeline *rppl /* = nullptr*/) {
return true;
}

void Root::updateGlobalLightsUBOs(ccstd::vector<scene::Light*>& lights)
{
auto ubo = _pipeline->getPipelineUBO();
ubo->updateGlobalLightsUBOs(lights);
}

void Root::onGlobalPipelineStateChanged() {
for (const auto &scene : _scenes) {
scene->onGlobalPipelineStateChanged();
Expand Down
2 changes: 2 additions & 0 deletions native/cocos/core/Root.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ class Root final {

void frameSync();

void updateGlobalLightsUBOs(ccstd::vector<scene::Light *>& lights);

private:
void frameMoveBegin();
void frameMoveProcess(bool isNeedUpdateScene, int32_t totalFrames);
Expand Down
19 changes: 10 additions & 9 deletions native/cocos/renderer/pipeline/PipelineUBO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,6 @@ void PipelineUBO::updateShadowUBOView(const RenderPipeline *pipeline, ccstd::arr

memcpy(sv.data() + UBOShadow::SHADOW_COLOR_OFFSET, shadowInfo->getShadowColor4f().data(), sizeof(float) * 4);
}

PipelineUBO::updateGlobalLightsUBOs(pipeline, csmBufferView, camera);
}

void PipelineUBO::updateShadowUBOLightView(const RenderPipeline *pipeline, ccstd::array<float, UBOShadow::COUNT> *shadowBufferView,
Expand Down Expand Up @@ -557,13 +555,16 @@ void PipelineUBO::updateMultiCameraUBO(GlobalDSManager *globalDSMgr, const ccstd
_currentCameraUBOOffset = 0;
}

void PipelineUBO::updateGlobalLightsUBOs(const RenderPipeline *pipeline, ccstd::array<float, UBOCSM::COUNT> *csmBufferView, const scene::Camera *camera) {
auto _lightBufferData = csmBufferView->data();
const auto exposure = camera->getExposure();
const auto sceneData = pipeline->getPipelineSceneData();
void PipelineUBO::updateGlobalLightsUBOs(ccstd::vector<scene::Light *> &lights) {

const auto exposure = 0.00002604165625;
//const auto exposure = camera->getExposure();

auto _lightBufferData = _csmUBO.data();
const auto sceneData = _pipeline->getPipelineSceneData();
const auto isHDR = sceneData->isHDR();
const auto shadowInfo = sceneData->getShadows();
const std::vector<const cc::scene::Light *>& validPunctualLights = sceneData->getValidPunctualLights();
//const std::vector<const cc::scene::Light *> &lights = sceneData->getValidPunctualLights();

#define UBOForwardLight UBOCSM
const float _lightMeterScale = 10000.0;
Expand All @@ -573,9 +574,9 @@ void PipelineUBO::updateGlobalLightsUBOs(const RenderPipeline *pipeline, ccstd::
_lightBufferData[offset + UBOForwardLight::LIGHT_SIZE_RANGE_ANGLE_OFFSET + 3] = 1000;
}

for (int l = 0; l < validPunctualLights.size(); l++) {
for (int l = 0; l < lights.size(); l++) {
int offset = l * 4;
const auto *light = validPunctualLights[l];
const auto *light = lights[l];

Vec3 position = Vec3(0.0F, 0.0F, 0.0F);
float size = 0.F;
Expand Down
2 changes: 1 addition & 1 deletion native/cocos/renderer/pipeline/PipelineUBO.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class CC_DLL PipelineUBO final {
uint32_t getCurrentCameraUBOOffset() const;
void incCameraUBOOffset();

static void updateGlobalLightsUBOs(const RenderPipeline *pipeline, ccstd::array<float, UBOCSM::COUNT> *csmBufferView, const scene::Camera *camera);
void updateGlobalLightsUBOs(ccstd::vector<scene::Light *> &lights);

private:
static float getPCFRadius(const scene::Shadows *shadowInfo, const scene::DirectionalLight *dirLight);
Expand Down
39 changes: 6 additions & 33 deletions native/cocos/scene/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,44 +250,13 @@ void Model::updateUBOs(uint32_t stamp) {
}
}

_localBuffer->write(_lightIndices, sizeof(float) * pipeline::UBOLocal::GLOBAL_LIGHTING_INDICES);

_localBuffer->update();
const bool enableOcclusionQuery = Root::getInstance()->getPipeline()->isOcclusionQueryEnabled();
if (enableOcclusionQuery) {
updateWorldBoundUBOs();
}

const ccstd::vector<const scene::Light *> &lights = pipeline->getPipelineSceneData()->getValidPunctualLights();

int idx = 0;

for (int i = 0; i < 4; i++) {
_lightIndicesArray[i] = -1;
}

for (int i = 0; i < lights.size(); i++) {
auto l = lights.at(i);
if (((l->getVisibility() & _node->getLayer()) == _node->getLayer())) {
if (l->getType() == cc::scene::LightType::SPHERE) {
if (cullSphereLight((scene::SphereLight*)l, this)) {
continue;
}
} else if (l->getType() == cc::scene::LightType::SPOT) {
if (cullSpotLight((scene::SpotLight *)l, this)) {
continue;
}
}

_lightIndicesArray[idx++] = i;
if (idx > 3) {
break;
}
}
}

_lightIndices.set(_lightIndicesArray[0], _lightIndicesArray[1], _lightIndicesArray[2], _lightIndicesArray[3]);

_localBuffer->write(_lightIndices, sizeof(float) * pipeline::UBOLocal::GLOBAL_LIGHTING_INDICES);
}
}

Expand Down Expand Up @@ -593,11 +562,15 @@ void Model::initLocalDescriptors(index_t /*subModelIndex*/) {
}
}

void Model::updateLightIndices() {
void Model::updateLightIndices(ccstd::vector<float> lightIndices) {
if (_node == nullptr) {
return;
}


_lightIndices.set(lightIndices[0], lightIndices[1], lightIndices[2], lightIndices[3]);


_localDataUpdated = true;
}

Expand Down
2 changes: 1 addition & 1 deletion native/cocos/scene/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class Model : public RefCounted {
void updateReflectionProbeDataMap(Texture2D *texture);
void updateReflectionProbeBlendCubemap(TextureCube *texture);

void updateLightIndices();
void updateLightIndices(ccstd::vector<float> lightIndices);

inline void attachToScene(RenderScene *scene) {
_scene = scene;
Expand Down

0 comments on commit 45bd05c

Please sign in to comment.