From 31e763fdf204d6a4f55d579615ae4e923b3e83dc Mon Sep 17 00:00:00 2001 From: troublemaker52025 Date: Mon, 11 Sep 2023 17:03:55 +0800 Subject: [PATCH 1/3] Resolved error when getting length for Record type --- cocos/render-scene/scene/submodel.ts | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/cocos/render-scene/scene/submodel.ts b/cocos/render-scene/scene/submodel.ts index 66a026c2fb8..c6a4c9ce615 100644 --- a/cocos/render-scene/scene/submodel.ts +++ b/cocos/render-scene/scene/submodel.ts @@ -32,7 +32,7 @@ import { DescriptorSet, DescriptorSetInfo, Device, InputAssembler, Texture, Text import { errorID, Mat4, cclegacy } from '../../core'; import { getPhaseID } from '../../rendering/pass-phase'; import { Root } from '../../root'; -import { MacroRecord } from '../core/pass-utils'; +import { MacroRecord, overrideMacros } from '../core/pass-utils'; const _dsInfo = new DescriptorSetInfo(null!); const MAX_PASS_COUNT = 8; @@ -55,7 +55,7 @@ export class SubModel { protected _shaders: Shader[] | null = null; protected _subMesh: RenderingSubMesh | null = null; protected _patches: IMacroPatch[] | null = null; - protected _globalPatches: MacroRecord | null = null; + protected _globalPatches: MacroRecord = {}; protected _priority: RenderPriority = RenderPriority.DEFAULT; protected _inputAssembler: InputAssembler | null = null; protected _descriptorSet: DescriptorSet | null = null; @@ -296,7 +296,6 @@ export class SubModel { this.priority = RenderPriority.DEFAULT; this._patches = null; - this._globalPatches = null; this._subMesh = null; this._passes = null; @@ -330,18 +329,16 @@ export class SubModel { const root = cclegacy.director.root as Root; const pipeline = root.pipeline; const pipelinePatches = Object.entries(pipeline.macros); - if (!this._globalPatches && pipelinePatches.length === 0) { + const globalPatches = Object.entries(this._globalPatches); + if (globalPatches.length === 0 && pipelinePatches.length === 0) { return; - } else if (pipelinePatches.length) { - if (this._globalPatches) { - const globalPatches = Object.entries(this._globalPatches); - if (pipelinePatches.length === globalPatches.length) { - const patchesStateUnchanged = JSON.stringify(pipelinePatches.sort()) === JSON.stringify(globalPatches.sort()); - if (patchesStateUnchanged) return; - } - } } - this._globalPatches = pipeline.macros; + + if (pipelinePatches.length === globalPatches.length) { + const patchesStateUnchanged = JSON.stringify(pipelinePatches.sort()) === JSON.stringify(globalPatches.sort()); + if (patchesStateUnchanged) return; + } + overrideMacros(this._globalPatches, pipeline.macros); const passes = this._passes; if (!passes) { return; } From ecbdd68e0d6dc3de31cf47856095e6ef72aadc39 Mon Sep 17 00:00:00 2001 From: troublemaker52025 Date: Fri, 15 Sep 2023 15:10:14 +0800 Subject: [PATCH 2/3] Sorting on shorter patches outperforms hashing, with negative optimization on longer global patches. --- cocos/render-scene/scene/submodel.ts | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/cocos/render-scene/scene/submodel.ts b/cocos/render-scene/scene/submodel.ts index c6a4c9ce615..95b9c1f3ff1 100644 --- a/cocos/render-scene/scene/submodel.ts +++ b/cocos/render-scene/scene/submodel.ts @@ -55,7 +55,6 @@ export class SubModel { protected _shaders: Shader[] | null = null; protected _subMesh: RenderingSubMesh | null = null; protected _patches: IMacroPatch[] | null = null; - protected _globalPatches: MacroRecord = {}; protected _priority: RenderPriority = RenderPriority.DEFAULT; protected _inputAssembler: InputAssembler | null = null; protected _descriptorSet: DescriptorSet | null = null; @@ -326,20 +325,6 @@ export class SubModel { * @zh 管线更新回调 */ public onPipelineStateChanged (): void { - const root = cclegacy.director.root as Root; - const pipeline = root.pipeline; - const pipelinePatches = Object.entries(pipeline.macros); - const globalPatches = Object.entries(this._globalPatches); - if (globalPatches.length === 0 && pipelinePatches.length === 0) { - return; - } - - if (pipelinePatches.length === globalPatches.length) { - const patchesStateUnchanged = JSON.stringify(pipelinePatches.sort()) === JSON.stringify(globalPatches.sort()); - if (patchesStateUnchanged) return; - } - overrideMacros(this._globalPatches, pipeline.macros); - const passes = this._passes; if (!passes) { return; } @@ -362,6 +347,7 @@ export class SubModel { return; } else if (patches) { patches = patches.sort(); + // Sorting on shorter patches outperforms hashing, with negative optimization on longer global patches. if (this._patches && patches.length === this._patches.length) { const patchesStateUnchanged = JSON.stringify(patches) === JSON.stringify(this._patches); if (patchesStateUnchanged) return; From 7533fed45e056bd4bdb25c9b94999ffb5d80b36b Mon Sep 17 00:00:00 2001 From: troublemaker52025 Date: Fri, 15 Sep 2023 15:13:10 +0800 Subject: [PATCH 3/3] update --- cocos/render-scene/scene/submodel.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/cocos/render-scene/scene/submodel.ts b/cocos/render-scene/scene/submodel.ts index 95b9c1f3ff1..95c28fbabbd 100644 --- a/cocos/render-scene/scene/submodel.ts +++ b/cocos/render-scene/scene/submodel.ts @@ -32,7 +32,6 @@ import { DescriptorSet, DescriptorSetInfo, Device, InputAssembler, Texture, Text import { errorID, Mat4, cclegacy } from '../../core'; import { getPhaseID } from '../../rendering/pass-phase'; import { Root } from '../../root'; -import { MacroRecord, overrideMacros } from '../core/pass-utils'; const _dsInfo = new DescriptorSetInfo(null!); const MAX_PASS_COUNT = 8;