mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
fix multi-light source rendering translucent material for v3.4.0 (#3910)
* fix merge error * update * update ds to globalDS * safe destroy * fix ci error * fix ci error
This commit is contained in:
committed by
GitHub
parent
b00ecadad6
commit
666b8cb0c0
@@ -427,11 +427,10 @@ void PipelineUBO::updateShadowUBO(const scene::Camera *camera) {
|
||||
cmdBuffer->updateBuffer(ds->getBuffer(UBOShadow::BINDING), _shadowUBO.data(), UBOShadow::SIZE);
|
||||
}
|
||||
|
||||
void PipelineUBO::updateShadowUBOLight(const scene::Light *light) {
|
||||
const auto *ds = _pipeline->getDescriptorSet();
|
||||
void PipelineUBO::updateShadowUBOLight(gfx::DescriptorSet *globalDS, const scene::Light *light) {
|
||||
auto *const cmdBuffer = _pipeline->getCommandBuffers()[0];
|
||||
PipelineUBO::updateShadowUBOLightView(_pipeline, &_shadowUBO, light);
|
||||
cmdBuffer->updateBuffer(ds->getBuffer(UBOShadow::BINDING), _shadowUBO.data(), UBOShadow::SIZE);
|
||||
cmdBuffer->updateBuffer(globalDS->getBuffer(UBOShadow::BINDING), _shadowUBO.data(), UBOShadow::SIZE);
|
||||
}
|
||||
|
||||
void PipelineUBO::updateShadowUBORange(uint offset, const Mat4 *data) {
|
||||
|
||||
@@ -54,7 +54,7 @@ public:
|
||||
void updateCameraUBO(const scene::Camera *camera);
|
||||
void updateMultiCameraUBO(const vector<scene::Camera *> &cameras);
|
||||
void updateShadowUBO(const scene::Camera *camera);
|
||||
void updateShadowUBOLight(const scene::Light *light);
|
||||
void updateShadowUBOLight(gfx::DescriptorSet *globalDS, const scene::Light *light);
|
||||
void updateShadowUBORange(uint offset, const Mat4 *data);
|
||||
|
||||
uint getCurrentCameraUBOOffset() const;
|
||||
|
||||
@@ -48,7 +48,7 @@ ShadowMapBatchedQueue::ShadowMapBatchedQueue(RenderPipeline *pipeline)
|
||||
_batchedQueue = CC_NEW(RenderBatchedQueue);
|
||||
}
|
||||
|
||||
void ShadowMapBatchedQueue::gatherLightPasses(const scene::Camera *camera, const scene::Light *light, gfx::CommandBuffer *cmdBuffer) {
|
||||
void ShadowMapBatchedQueue::gatherLightPasses(gfx::DescriptorSet *globalDS, const scene::Camera *camera, const scene::Light *light, gfx::CommandBuffer *cmdBuffer) {
|
||||
clear();
|
||||
|
||||
const PipelineSceneData *sceneData = _pipeline->getPipelineSceneData();
|
||||
@@ -56,7 +56,7 @@ void ShadowMapBatchedQueue::gatherLightPasses(const scene::Camera *camera, const
|
||||
const RenderObjectList & dirShadowObjects = sceneData->getDirShadowObjects();
|
||||
const RenderObjectList & castShadowObjects = sceneData->getCastShadowObjects();
|
||||
if (light && shadowInfo->enabled && shadowInfo->shadowType == scene::ShadowType::SHADOWMAP) {
|
||||
_pipeline->getPipelineUBO()->updateShadowUBOLight(light);
|
||||
_pipeline->getPipelineUBO()->updateShadowUBOLight(globalDS, light);
|
||||
|
||||
switch (light->getType()) {
|
||||
case scene::LightType::DIRECTIONAL: {
|
||||
|
||||
@@ -47,7 +47,7 @@ public:
|
||||
void destroy();
|
||||
|
||||
void clear();
|
||||
void gatherLightPasses(const scene::Camera *, const scene::Light *, gfx::CommandBuffer *);
|
||||
void gatherLightPasses(gfx::DescriptorSet *, const scene::Camera *, const scene::Light *, gfx::CommandBuffer *);
|
||||
void add(const scene::Model *, gfx::CommandBuffer *);
|
||||
void recordCommandBuffer(gfx::Device *, gfx::RenderPass *, gfx::CommandBuffer *) const;
|
||||
|
||||
|
||||
@@ -35,9 +35,7 @@
|
||||
#include "ForwardPipeline.h"
|
||||
#include "pipeline/common/UIPhase.h"
|
||||
#include "gfx-base/GFXCommandBuffer.h"
|
||||
#include "gfx-base/GFXDevice.h"
|
||||
#include "gfx-base/GFXFramebuffer.h"
|
||||
#include "gfx-base/GFXQueue.h"
|
||||
|
||||
namespace cc {
|
||||
namespace pipeline {
|
||||
@@ -69,9 +67,9 @@ void ForwardStage::activate(RenderPipeline *pipeline, RenderFlow *flow) {
|
||||
RenderStage::activate(pipeline, flow);
|
||||
|
||||
for (const auto &descriptor : _renderQueueDescriptors) {
|
||||
uint phase = convertPhase(descriptor.stages);
|
||||
RenderQueueSortFunc sortFunc = convertQueueSortFunc(descriptor.sortMode);
|
||||
RenderQueueCreateInfo info = {descriptor.isTransparent, phase, sortFunc};
|
||||
const uint phase = convertPhase(descriptor.stages);
|
||||
const RenderQueueSortFunc sortFunc = convertQueueSortFunc(descriptor.sortMode);
|
||||
RenderQueueCreateInfo info = {descriptor.isTransparent, phase, sortFunc};
|
||||
_renderQueues.emplace_back(CC_NEW(RenderQueue(std::move(info))));
|
||||
}
|
||||
|
||||
@@ -92,27 +90,23 @@ void ForwardStage::destroy() {
|
||||
void ForwardStage::dispenseRenderObject2Queues() {
|
||||
_instancedQueue->clear();
|
||||
_batchedQueue->clear();
|
||||
auto * pipeline = static_cast<ForwardPipeline *>(_pipeline);
|
||||
auto *const sceneData = _pipeline->getPipelineSceneData();
|
||||
auto *const sharedData = sceneData->getSharedData();
|
||||
|
||||
const auto *sceneData = _pipeline->getPipelineSceneData();
|
||||
const auto &renderObjects = sceneData->getRenderObjects();
|
||||
|
||||
for (auto *queue : _renderQueues) {
|
||||
queue->clear();
|
||||
}
|
||||
|
||||
uint subModelIdx = 0;
|
||||
uint passIdx = 0;
|
||||
size_t k = 0;
|
||||
for (auto ro : renderObjects) {
|
||||
const auto *const model = ro.model;
|
||||
const auto & subModels = model->getSubModels();
|
||||
auto subModelCount = subModels.size();
|
||||
for (subModelIdx = 0; subModelIdx < subModelCount; ++subModelIdx) {
|
||||
const auto subModelCount = subModels.size();
|
||||
for (uint subModelIdx = 0; subModelIdx < subModelCount; ++subModelIdx) {
|
||||
const auto &subModel = subModels[subModelIdx];
|
||||
const auto &passes = subModel->getPasses();
|
||||
auto passCount = passes.size();
|
||||
for (passIdx = 0; passIdx < passCount; ++passIdx) {
|
||||
const auto passCount = passes.size();
|
||||
for (uint passIdx = 0; passIdx < passCount; ++passIdx) {
|
||||
const auto &pass = passes[passIdx];
|
||||
if (pass->getPhase() != _phaseID) continue;
|
||||
if (pass->getBatchingScheme() == scene::BatchingSchemes::INSTANCING) {
|
||||
@@ -124,8 +118,8 @@ void ForwardStage::dispenseRenderObject2Queues() {
|
||||
batchedBuffer->merge(subModel, passIdx, model);
|
||||
_batchedQueue->add(batchedBuffer);
|
||||
} else {
|
||||
for (k = 0; k < _renderQueues.size(); k++) {
|
||||
_renderQueues[k]->insertRenderPass(ro, subModelIdx, passIdx);
|
||||
for (auto *renderQueue : _renderQueues) {
|
||||
renderQueue->insertRenderPass(ro, subModelIdx, passIdx);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -150,6 +144,7 @@ void ForwardStage::render(scene::Camera *camera) {
|
||||
_renderArea = pipeline->getRenderArea(camera, false);
|
||||
// Command 'updateBuffer' must be recorded outside render passes, cannot put them in execute lambda
|
||||
dispenseRenderObject2Queues();
|
||||
pipeline->getPipelineUBO()->updateShadowUBO(camera);
|
||||
auto *cmdBuff{pipeline->getCommandBuffers()[0]};
|
||||
|
||||
_instancedQueue->uploadBuffers(cmdBuff);
|
||||
@@ -229,7 +224,10 @@ void ForwardStage::render(scene::Camera *camera) {
|
||||
_renderQueues[0]->recordCommandBuffer(_device, renderPass, cmdBuff);
|
||||
_instancedQueue->recordCommandBuffer(_device, renderPass, cmdBuff);
|
||||
_batchedQueue->recordCommandBuffer(_device, renderPass, cmdBuff);
|
||||
|
||||
_additiveLightQueue->recordCommandBuffer(_device, renderPass, cmdBuff);
|
||||
|
||||
cmdBuff->bindDescriptorSet(globalSet, _pipeline->getDescriptorSet(), 1, &offset);
|
||||
_planarShadowQueue->recordCommandBuffer(_device, renderPass, cmdBuff);
|
||||
_renderQueues[1]->recordCommandBuffer(_device, renderPass, cmdBuff);
|
||||
}
|
||||
|
||||
@@ -80,7 +80,12 @@ void ShadowFlow::render(scene::Camera *camera) {
|
||||
}
|
||||
|
||||
const auto &shadowFramebufferMap = sceneData->getShadowFramebufferMap();
|
||||
for (const auto *light : _validLights) {
|
||||
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);
|
||||
|
||||
if (!shadowFramebufferMap.count(light)) {
|
||||
initShadowFrameBuffer(_pipeline, light);
|
||||
}
|
||||
@@ -89,20 +94,21 @@ void ShadowFlow::render(scene::Camera *camera) {
|
||||
|
||||
for (auto *stage : _stages) {
|
||||
auto *shadowStage = dynamic_cast<ShadowStage *>(stage);
|
||||
shadowStage->setUseData(light, shadowFrameBuffer);
|
||||
shadowStage->setUsage(globalDS, light, shadowFrameBuffer);
|
||||
shadowStage->render(camera);
|
||||
}
|
||||
}
|
||||
|
||||
// After the shadowMap rendering of all lights is completed,
|
||||
// restore the ShadowUBO data of the main light.
|
||||
_pipeline->getPipelineUBO()->updateShadowUBO(camera);
|
||||
}
|
||||
|
||||
void ShadowFlow::clearShadowMap(scene::Camera *camera) {
|
||||
auto * sceneData = _pipeline->getPipelineSceneData();
|
||||
const auto *sceneData = _pipeline->getPipelineSceneData();
|
||||
const auto &shadowFramebufferMap = sceneData->getShadowFramebufferMap();
|
||||
for (const auto *light : _validLights) {
|
||||
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);
|
||||
|
||||
if (!shadowFramebufferMap.count(light)) {
|
||||
continue;
|
||||
}
|
||||
@@ -110,18 +116,18 @@ void ShadowFlow::clearShadowMap(scene::Camera *camera) {
|
||||
auto *shadowFrameBuffer = shadowFramebufferMap.at(light);
|
||||
for (auto *stage : _stages) {
|
||||
auto *shadowStage = dynamic_cast<ShadowStage *>(stage);
|
||||
shadowStage->setUseData(light, shadowFrameBuffer);
|
||||
shadowStage->setUsage(globalDS, light, shadowFrameBuffer);
|
||||
shadowStage->clearFramebuffer(camera);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ShadowFlow::resizeShadowMap(scene::Shadow **shadowInfo) {
|
||||
auto * sceneData = _pipeline->getPipelineSceneData();
|
||||
auto * device = gfx::Device::getInstance();
|
||||
const auto width = static_cast<uint>((*shadowInfo)->size.x);
|
||||
const auto height = static_cast<uint>((*shadowInfo)->size.y);
|
||||
const auto format = supportsHalfFloatTexture(device) ? gfx::Format::R32F : gfx::Format::RGBA8;
|
||||
const auto *sceneData = _pipeline->getPipelineSceneData();
|
||||
auto * device = gfx::Device::getInstance();
|
||||
const auto width = static_cast<uint>((*shadowInfo)->size.x);
|
||||
const auto height = static_cast<uint>((*shadowInfo)->size.y);
|
||||
const auto format = supportsHalfFloatTexture(device) ? gfx::Format::R32F : gfx::Format::RGBA8;
|
||||
|
||||
for (const auto &pair : sceneData->getShadowFramebufferMap()) {
|
||||
gfx::Framebuffer *framebuffer = pair.second;
|
||||
@@ -131,7 +137,7 @@ void ShadowFlow::resizeShadowMap(scene::Shadow **shadowInfo) {
|
||||
}
|
||||
|
||||
auto renderTargets = framebuffer->getColorTextures();
|
||||
for (auto *renderTarget : renderTargets) {
|
||||
for (const auto *renderTarget : renderTargets) {
|
||||
CC_DELETE(renderTarget);
|
||||
}
|
||||
renderTargets.clear();
|
||||
@@ -241,7 +247,7 @@ void ShadowFlow::initShadowFrameBuffer(RenderPipeline *pipeline, const scene::Li
|
||||
}
|
||||
|
||||
void ShadowFlow::destroy() {
|
||||
for (auto rpPair : renderPassHashMap) {
|
||||
for (const auto &rpPair : renderPassHashMap) {
|
||||
rpPair.second->destroy();
|
||||
}
|
||||
renderPassHashMap.clear();
|
||||
|
||||
@@ -66,7 +66,7 @@ void ShadowStage::render(scene::Camera *camera) {
|
||||
|
||||
auto *cmdBuffer = _pipeline->getCommandBuffers()[0];
|
||||
|
||||
_additiveShadowQueue->gatherLightPasses(camera, _light, cmdBuffer);
|
||||
_additiveShadowQueue->gatherLightPasses(_globalDS, camera, _light, cmdBuffer);
|
||||
|
||||
const auto shadowMapSize = shadowInfo->size;
|
||||
const auto &viewport = camera->viewPort;
|
||||
@@ -82,13 +82,17 @@ void ShadowStage::render(scene::Camera *camera) {
|
||||
_clearColors, camera->clearDepth, camera->clearStencil);
|
||||
|
||||
const std::array<uint, 1> globalOffsets = {_pipeline->getPipelineUBO()->getCurrentCameraUBOOffset()};
|
||||
cmdBuffer->bindDescriptorSet(globalSet, _pipeline->getDescriptorSet(), utils::toUint(globalOffsets.size()), globalOffsets.data());
|
||||
cmdBuffer->bindDescriptorSet(globalSet, _globalDS, utils::toUint(globalOffsets.size()), globalOffsets.data());
|
||||
_additiveShadowQueue->recordCommandBuffer(_device, renderPass, cmdBuffer);
|
||||
|
||||
cmdBuffer->endRenderPass();
|
||||
}
|
||||
|
||||
void ShadowStage::destroy() {
|
||||
_framebuffer = nullptr;
|
||||
_globalDS = nullptr;
|
||||
_light = nullptr;
|
||||
|
||||
CC_SAFE_DESTROY(_additiveShadowQueue);
|
||||
|
||||
RenderStage::destroy();
|
||||
|
||||
@@ -45,10 +45,11 @@ public:
|
||||
void activate(RenderPipeline *pipeline, RenderFlow *flow) override;
|
||||
|
||||
inline void setFramebuffer(gfx::Framebuffer *framebuffer) { _framebuffer = framebuffer; }
|
||||
inline void setUseData(const scene::Light *light, gfx::Framebuffer *framebuffer) {
|
||||
inline void setUsage(gfx::DescriptorSet *globalDS, const scene::Light *light, gfx::Framebuffer *framebuffer) {
|
||||
_globalDS = globalDS;
|
||||
_light = light;
|
||||
_framebuffer = framebuffer;
|
||||
};
|
||||
}
|
||||
|
||||
void clearFramebuffer(scene::Camera *camera);
|
||||
|
||||
@@ -56,8 +57,9 @@ private:
|
||||
static RenderStageInfo initInfo;
|
||||
|
||||
gfx::Rect _renderArea;
|
||||
const scene::Light *_light = nullptr;
|
||||
gfx::Framebuffer * _framebuffer = nullptr;
|
||||
gfx::DescriptorSet *_globalDS = nullptr;
|
||||
const scene::Light *_light = nullptr;
|
||||
gfx::Framebuffer * _framebuffer = nullptr;
|
||||
|
||||
ShadowMapBatchedQueue *_additiveShadowQueue = nullptr;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user