From 03f451aadb5ee091f00a719fd39125df4d7c1f67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=85=B5?= <49358166+xubing0906@users.noreply.github.com> Date: Mon, 5 Jun 2023 15:47:16 +0800 Subject: [PATCH] fix planar reflection update submodel macro patches (#15316) --- cocos/render-scene/core/pass.ts | 2 +- cocos/render-scene/scene/shadows.ts | 2 +- cocos/render-scene/scene/submodel.ts | 2 +- .../render-reflection-probe-queue.ts | 21 +++++++++------- .../pipeline/ReflectionProbeBatchedQueue.cpp | 24 ++++++++++++------- .../pipeline/ReflectionProbeBatchedQueue.h | 6 +++-- 6 files changed, 34 insertions(+), 23 deletions(-) diff --git a/cocos/render-scene/core/pass.ts b/cocos/render-scene/core/pass.ts index 06cde220a4..475454605a 100644 --- a/cocos/render-scene/core/pass.ts +++ b/cocos/render-scene/core/pass.ts @@ -515,7 +515,7 @@ export class Pass { * @zh 结合指定的编译宏组合获取当前 Pass 的 Shader Variant * @param patches The macro patches */ - public getShaderVariant (patches: IMacroPatch[] | null = null): Shader | null { + public getShaderVariant (patches: Readonly = null): Shader | null { if (!this._shader && !this.tryCompile()) { console.warn('pass resources incomplete'); return null; diff --git a/cocos/render-scene/scene/shadows.ts b/cocos/render-scene/scene/shadows.ts index a172103cd3..51afd895d7 100644 --- a/cocos/render-scene/scene/shadows.ts +++ b/cocos/render-scene/scene/shadows.ts @@ -337,7 +337,7 @@ export class Shadows { * @param patches The macro patches for the shader * @returns The shader for the planar shadow */ - public getPlanarShader (patches: IMacroPatch[] | null): Shader | null { + public getPlanarShader (patches: Readonly): Shader | null { if (!this._material) { this._material = new Material(); this._material.initialize({ effectName: 'pipeline/planar-shadow' }); diff --git a/cocos/render-scene/scene/submodel.ts b/cocos/render-scene/scene/submodel.ts index ae4344663b..7015ad92d8 100644 --- a/cocos/render-scene/scene/submodel.ts +++ b/cocos/render-scene/scene/submodel.ts @@ -159,7 +159,7 @@ export class SubModel { * @en The macro patches for the shaders * @zh 着色器程序所用的宏定义组合 */ - get patches (): IMacroPatch[] | null { + get patches (): Readonly { return this._patches; } diff --git a/cocos/rendering/render-reflection-probe-queue.ts b/cocos/rendering/render-reflection-probe-queue.ts index a76133ba3f..67ef5eb8b3 100644 --- a/cocos/rendering/render-reflection-probe-queue.ts +++ b/cocos/rendering/render-reflection-probe-queue.ts @@ -79,6 +79,7 @@ export class RenderReflectionProbeQueue { private _shaderArray: Shader[] = []; private _rgbeSubModelsArray: SubModel[]=[] private _instancedQueue: RenderInstancedQueue; + private _patches: IMacroPatch[] = []; public constructor (pipeline: PipelineRuntime) { this._pipeline = pipeline; @@ -151,12 +152,13 @@ export class RenderReflectionProbeQueue { const batchingScheme = pass.batchingScheme; if (!bUseReflectPass) { - let patches: IMacroPatch[] | null = subModel.patches; + this._patches = []; + this._patches = this._patches.concat(subModel.patches!); const useRGBEPatchs: IMacroPatch[] = [ { name: CC_USE_RGBE_OUTPUT, value: true }, ]; - patches = patches ? patches.concat(useRGBEPatchs) : useRGBEPatchs; - subModel.onMacroPatchesStateChanged(patches); + this._patches = this._patches.concat(useRGBEPatchs); + subModel.onMacroPatchesStateChanged(this._patches); this._rgbeSubModelsArray.push(subModel); } @@ -199,18 +201,19 @@ export class RenderReflectionProbeQueue { } public resetRGBEMacro () { for (let i = 0; i < this._rgbeSubModelsArray.length; i++) { + this._patches = []; const subModel = this._rgbeSubModelsArray[i]; // eslint-disable-next-line prefer-const - let patches: IMacroPatch[] | null = subModel.patches; - if (!patches) continue; - for (let j = 0; j < patches.length; j++) { - const patch = patches[j]; + this._patches = this._patches.concat(subModel.patches!); + if (!this._patches) continue; + for (let j = 0; j < this._patches.length; j++) { + const patch = this._patches[j]; if (patch.name === CC_USE_RGBE_OUTPUT) { - patches.splice(j, 1); + this._patches.splice(j, 1); break; } } - subModel.onMacroPatchesStateChanged(patches); + subModel.onMacroPatchesStateChanged(this._patches); } } } diff --git a/native/cocos/renderer/pipeline/ReflectionProbeBatchedQueue.cpp b/native/cocos/renderer/pipeline/ReflectionProbeBatchedQueue.cpp index 7738e05fda..eceff37175 100644 --- a/native/cocos/renderer/pipeline/ReflectionProbeBatchedQueue.cpp +++ b/native/cocos/renderer/pipeline/ReflectionProbeBatchedQueue.cpp @@ -130,9 +130,12 @@ void ReflectionProbeBatchedQueue::add(const scene::Model *model) { const auto batchingScheme = pass->getBatchingScheme(); if (!bUseReflectPass) { - auto patches = const_cast &>(subModel->getPatches()); - patches.emplace_back(MACRO_PATCH_RGBE_OUTPUT); - subModel->onMacroPatchesStateChanged(patches); + _patches.clear(); + for (const auto &patch : subModel->getPatches()) { + _patches.push_back(patch); + } + _patches.emplace_back(MACRO_PATCH_RGBE_OUTPUT); + subModel->onMacroPatchesStateChanged(_patches); _rgbeSubModels.emplace_back(subModel); } @@ -148,7 +151,7 @@ void ReflectionProbeBatchedQueue::add(const scene::Model *model) { } } -void ReflectionProbeBatchedQueue::recordCommandBuffer(gfx::Device *device, gfx::RenderPass *renderPass, gfx::CommandBuffer *cmdBuffer) const { +void ReflectionProbeBatchedQueue::recordCommandBuffer(gfx::Device *device, gfx::RenderPass *renderPass, gfx::CommandBuffer *cmdBuffer) { _instancedQueue->recordCommandBuffer(device, renderPass, cmdBuffer); for (size_t i = 0; i < _subModels.size(); i++) { @@ -167,13 +170,16 @@ void ReflectionProbeBatchedQueue::recordCommandBuffer(gfx::Device *device, gfx:: resetMacro(); if (_instancedQueue) _instancedQueue->clear(); } -void ReflectionProbeBatchedQueue::resetMacro() const { +void ReflectionProbeBatchedQueue::resetMacro() { for (const auto &subModel : _rgbeSubModels) { - auto patches = const_cast &>(subModel->getPatches()); - for (auto iter = patches.begin(); iter != patches.end(); iter++) { + _patches.clear(); + for (const auto &patch : subModel->getPatches()) { + _patches.push_back(patch); + } + for (auto iter = _patches.begin(); iter != _patches.end(); iter++) { if (iter->name == CC_USE_RGBE_OUTPUT) { - patches.erase(iter); - const_cast(subModel)->onMacroPatchesStateChanged(patches); + _patches.erase(iter); + const_cast(subModel)->onMacroPatchesStateChanged(_patches); break; } } diff --git a/native/cocos/renderer/pipeline/ReflectionProbeBatchedQueue.h b/native/cocos/renderer/pipeline/ReflectionProbeBatchedQueue.h index 7631a8736e..e86996f6a8 100644 --- a/native/cocos/renderer/pipeline/ReflectionProbeBatchedQueue.h +++ b/native/cocos/renderer/pipeline/ReflectionProbeBatchedQueue.h @@ -26,6 +26,7 @@ #include "Define.h" #include "cocos/base/Macros.h" +#include "scene/Define.h" namespace cc { namespace scene { class Camera; @@ -48,9 +49,9 @@ public: void clear(); void gatherRenderObjects(const scene::Camera *, gfx::CommandBuffer *, const scene::ReflectionProbe *probe); void add(const scene::Model *); - void recordCommandBuffer(gfx::Device *, gfx::RenderPass *, gfx::CommandBuffer *) const; + void recordCommandBuffer(gfx::Device *, gfx::RenderPass *, gfx::CommandBuffer *); - void resetMacro() const; + void resetMacro(); bool isUseReflectMapPass(const scene::SubModel *subModel) const; @@ -71,6 +72,7 @@ private: uint32_t _phaseID{0}; uint32_t _phaseReflectMapID{0}; ccstd::vector _rgbeSubModels; + ccstd::vector _patches; }; } // namespace pipeline