From 8c0ce39c9d7e95660d8d88f65a1d769600b41189 Mon Sep 17 00:00:00 2001 From: xubing <58895777@qq.com> Date: Tue, 19 Sep 2023 11:00:29 +0800 Subject: [PATCH 1/7] upload property from asset --- cocos/asset/assets/effect-asset.ts | 4 ++++ cocos/asset/assets/material.ts | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/cocos/asset/assets/effect-asset.ts b/cocos/asset/assets/effect-asset.ts index 06d4fac7039..217bf410824 100644 --- a/cocos/asset/assets/effect-asset.ts +++ b/cocos/asset/assets/effect-asset.ts @@ -34,6 +34,7 @@ import { Asset } from './asset'; import { cclegacy, warnID } from '../../core'; import { ProgramLibrary } from '../../rendering/custom/private'; import { addEffectDefaultProperties, getCombinationDefines } from '../../render-scene/core/program-utils'; +import { MaterialPropertyFull } from './material'; export declare namespace EffectAsset { export interface IPropertyInfo { @@ -324,6 +325,9 @@ export class EffectAsset extends Asset { @editorOnly public hideInEditor = false; + @serializable + public props: Record[] = []; + /** * @en The loaded callback which should be invoked by the [[AssetManager]], will automatically register the effect. * @zh 通过 [[AssetManager]] 加载完成时的回调,将自动注册 effect 资源。 diff --git a/cocos/asset/assets/material.ts b/cocos/asset/assets/material.ts index 8bdd53715ba..a878158f4b7 100644 --- a/cocos/asset/assets/material.ts +++ b/cocos/asset/assets/material.ts @@ -454,6 +454,7 @@ export class Material extends Asset { for (const p in props) { this._uploadProperty(pass, p, props[p]); } + this._uploadPropertyFromEffectAsset(pass); }); } else { for (let i = 0; i < this._props.length; i++) { this._props[i] = {}; } @@ -462,6 +463,20 @@ export class Material extends Asset { this._hash = Material.getHash(this); } + /** + * @engineInternal + */ + protected _uploadPropertyFromEffectAsset (pass: Pass): void { + if (this._effectAsset) { + for (let i = 0; i < this._effectAsset.props.length; i++) { + const props = this._effectAsset.props[i]; + for (const p in props) { + this._uploadProperty(pass, p, props[p]); + } + } + } + } + /** * @engineInternal */ From a04ecc162bb11ed3aca6aed89e733d9596e2e086 Mon Sep 17 00:00:00 2001 From: xubing <58895777@qq.com> Date: Wed, 20 Sep 2023 17:17:54 +0800 Subject: [PATCH 2/7] upload property from asset --- cocos/asset/assets/material.ts | 18 ++---------------- cocos/render-scene/core/pass.ts | 1 + 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/cocos/asset/assets/material.ts b/cocos/asset/assets/material.ts index a878158f4b7..5081b977985 100644 --- a/cocos/asset/assets/material.ts +++ b/cocos/asset/assets/material.ts @@ -448,13 +448,13 @@ export class Material extends Asset { this._passes.forEach((pass, i): void => { let props = this._props[i]; if (!props) { props = this._props[i] = {}; } + Object.assign(props, this._effectAsset!.props[i]); if (pass.propertyIndex !== undefined) { - Object.assign(props, this._props[pass.propertyIndex]); + Object.assign(props, this._props[pass.propertyIndex], this._effectAsset!.props[pass.propertyIndex]); } for (const p in props) { this._uploadProperty(pass, p, props[p]); } - this._uploadPropertyFromEffectAsset(pass); }); } else { for (let i = 0; i < this._props.length; i++) { this._props[i] = {}; } @@ -463,20 +463,6 @@ export class Material extends Asset { this._hash = Material.getHash(this); } - /** - * @engineInternal - */ - protected _uploadPropertyFromEffectAsset (pass: Pass): void { - if (this._effectAsset) { - for (let i = 0; i < this._effectAsset.props.length; i++) { - const props = this._effectAsset.props[i]; - for (const p in props) { - this._uploadProperty(pass, p, props[p]); - } - } - } - } - /** * @engineInternal */ diff --git a/cocos/render-scene/core/pass.ts b/cocos/render-scene/core/pass.ts index c09b83e9396..fa329ad5794 100644 --- a/cocos/render-scene/core/pass.ts +++ b/cocos/render-scene/core/pass.ts @@ -434,6 +434,7 @@ export class Pass { const value = info && info.value; const texName = value ? `${value as string}${getStringFromType(type)}` : getDefaultFromType(type) as string; const textureBase = builtinResMgr.get(texName); + if (!textureBase) return; const texture = textureBase && textureBase.getGFXTexture()!; const samplerInfo = info && info.samplerHash !== undefined ? Sampler.unpackFromHash(info.samplerHash) : textureBase && textureBase.getSamplerInfo(); From a0f1f3a7569440961df657f9c4b91974bc1804fb Mon Sep 17 00:00:00 2001 From: xubing <58895777@qq.com> Date: Tue, 10 Oct 2023 16:53:21 +0800 Subject: [PATCH 3/7] set texture in resetTexture function --- cocos/asset/assets/material.ts | 4 ++-- cocos/render-scene/core/pass.ts | 21 +++++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/cocos/asset/assets/material.ts b/cocos/asset/assets/material.ts index 5081b977985..4d84ec6dc02 100644 --- a/cocos/asset/assets/material.ts +++ b/cocos/asset/assets/material.ts @@ -429,6 +429,7 @@ export class Material extends Asset { } if (passInfo.switch && !defines[passInfo.switch]) { continue; } const pass = new Pass(cclegacy.director.root); + pass.effectProps = this._effectAsset!.props; pass.initialize(passInfo); passes.push(pass); } @@ -448,9 +449,8 @@ export class Material extends Asset { this._passes.forEach((pass, i): void => { let props = this._props[i]; if (!props) { props = this._props[i] = {}; } - Object.assign(props, this._effectAsset!.props[i]); if (pass.propertyIndex !== undefined) { - Object.assign(props, this._props[pass.propertyIndex], this._effectAsset!.props[pass.propertyIndex]); + Object.assign(props, this._props[pass.propertyIndex]); } for (const p in props) { this._uploadProperty(pass, p, props[p]); diff --git a/cocos/render-scene/core/pass.ts b/cocos/render-scene/core/pass.ts index fa329ad5794..1c1657e3101 100644 --- a/cocos/render-scene/core/pass.ts +++ b/cocos/render-scene/core/pass.ts @@ -42,6 +42,7 @@ import { import { RenderPassStage, RenderPriority, SetIndex } from '../../rendering/define'; import { InstancedBuffer } from '../../rendering/instanced-buffer'; import { ProgramLibrary } from '../../rendering/custom/private'; +import { MaterialPropertyFull } from '../../asset/assets/material'; export interface IPassInfoFull extends EffectAsset.IPassInfo { // generated part @@ -203,12 +204,17 @@ export class Pass { protected _root: Root; protected _device: Device; protected _rootBufferDirty = false; + protected _effectProps: Record[] = []; constructor (root: Root) { this._root = root; this._device = deviceManager.gfxDevice; } + set effectProps (props: Record[]) { + Object.assign(this._effectProps, props); + } + /** * @en Initialize the pass with given pass info, shader will be compiled in the init process * @zh 根据指定参数初始化当前 pass,shader 会在这一阶段就尝试编译。 @@ -433,8 +439,7 @@ export class Pass { const info = this._properties[name]; const value = info && info.value; const texName = value ? `${value as string}${getStringFromType(type)}` : getDefaultFromType(type) as string; - const textureBase = builtinResMgr.get(texName); - if (!textureBase) return; + const textureBase = builtinResMgr.get(texName) || this._getTextureFromEffectProps(name); const texture = textureBase && textureBase.getGFXTexture()!; const samplerInfo = info && info.samplerHash !== undefined ? Sampler.unpackFromHash(info.samplerHash) : textureBase && textureBase.getSamplerInfo(); @@ -601,6 +606,18 @@ export class Pass { // eslint-disable-next-line @typescript-eslint/no-empty-function public endChangeStatesSilently (): void {} + protected _getTextureFromEffectProps (name: string): TextureBase | null { + for (let i = 0; i < this._effectProps.length; i++) { + const props = this._effectProps[i]; + for (const p in props) { + if (p === name) { + return props[p] as TextureBase; + } + } + } + return null; + } + protected _doInit (info: IPassInfoFull, copyDefines = false): void { this._priority = RenderPriority.DEFAULT; this._stage = RenderPassStage.DEFAULT; From 5a1290da7bb781c59b2ea109049c1fd644fc3012 Mon Sep 17 00:00:00 2001 From: xubing <58895777@qq.com> Date: Tue, 10 Oct 2023 19:36:46 +0800 Subject: [PATCH 4/7] add props to pass --- cocos/asset/assets/effect-asset.ts | 6 ++---- cocos/asset/assets/material.ts | 1 - cocos/render-scene/core/pass.ts | 21 ++------------------- 3 files changed, 4 insertions(+), 24 deletions(-) diff --git a/cocos/asset/assets/effect-asset.ts b/cocos/asset/assets/effect-asset.ts index 217bf410824..665c17e0c2d 100644 --- a/cocos/asset/assets/effect-asset.ts +++ b/cocos/asset/assets/effect-asset.ts @@ -35,13 +35,14 @@ import { cclegacy, warnID } from '../../core'; import { ProgramLibrary } from '../../rendering/custom/private'; import { addEffectDefaultProperties, getCombinationDefines } from '../../render-scene/core/program-utils'; import { MaterialPropertyFull } from './material'; +import { TextureBase } from './texture-base'; export declare namespace EffectAsset { export interface IPropertyInfo { type: number; // auto-extracted from shader handleInfo?: [string, number, number]; // auto-generated from 'target' samplerHash?: number; // auto-generated from 'sampler' - value?: number[] | string; // default value + value?: number[] | string | TextureBase; // default value linear?: boolean; // whether to convert the input to linear space first before applying } // Pass instance itself are compliant to IPassStates too @@ -325,9 +326,6 @@ export class EffectAsset extends Asset { @editorOnly public hideInEditor = false; - @serializable - public props: Record[] = []; - /** * @en The loaded callback which should be invoked by the [[AssetManager]], will automatically register the effect. * @zh 通过 [[AssetManager]] 加载完成时的回调,将自动注册 effect 资源。 diff --git a/cocos/asset/assets/material.ts b/cocos/asset/assets/material.ts index 4d84ec6dc02..8bdd53715ba 100644 --- a/cocos/asset/assets/material.ts +++ b/cocos/asset/assets/material.ts @@ -429,7 +429,6 @@ export class Material extends Asset { } if (passInfo.switch && !defines[passInfo.switch]) { continue; } const pass = new Pass(cclegacy.director.root); - pass.effectProps = this._effectAsset!.props; pass.initialize(passInfo); passes.push(pass); } diff --git a/cocos/render-scene/core/pass.ts b/cocos/render-scene/core/pass.ts index 1c1657e3101..1ef629adc63 100644 --- a/cocos/render-scene/core/pass.ts +++ b/cocos/render-scene/core/pass.ts @@ -204,17 +204,12 @@ export class Pass { protected _root: Root; protected _device: Device; protected _rootBufferDirty = false; - protected _effectProps: Record[] = []; constructor (root: Root) { this._root = root; this._device = deviceManager.gfxDevice; } - set effectProps (props: Record[]) { - Object.assign(this._effectProps, props); - } - /** * @en Initialize the pass with given pass info, shader will be compiled in the init process * @zh 根据指定参数初始化当前 pass,shader 会在这一阶段就尝试编译。 @@ -438,8 +433,8 @@ export class Pass { const binding = Pass.getBindingFromHandle(handle); const info = this._properties[name]; const value = info && info.value; - const texName = value ? `${value as string}${getStringFromType(type)}` : getDefaultFromType(type) as string; - const textureBase = builtinResMgr.get(texName) || this._getTextureFromEffectProps(name); + // eslint-disable-next-line max-len + const textureBase = typeof value === 'string' ? builtinResMgr.get(value ? `${value}${getStringFromType(type)}` : getDefaultFromType(type) as string) : value as TextureBase; const texture = textureBase && textureBase.getGFXTexture()!; const samplerInfo = info && info.samplerHash !== undefined ? Sampler.unpackFromHash(info.samplerHash) : textureBase && textureBase.getSamplerInfo(); @@ -606,18 +601,6 @@ export class Pass { // eslint-disable-next-line @typescript-eslint/no-empty-function public endChangeStatesSilently (): void {} - protected _getTextureFromEffectProps (name: string): TextureBase | null { - for (let i = 0; i < this._effectProps.length; i++) { - const props = this._effectProps[i]; - for (const p in props) { - if (p === name) { - return props[p] as TextureBase; - } - } - } - return null; - } - protected _doInit (info: IPassInfoFull, copyDefines = false): void { this._priority = RenderPriority.DEFAULT; this._stage = RenderPassStage.DEFAULT; From 8e7fa3d19898f06645a893dddefc2d630c3d3065 Mon Sep 17 00:00:00 2001 From: xubing <58895777@qq.com> Date: Tue, 10 Oct 2023 19:38:58 +0800 Subject: [PATCH 5/7] remove import --- cocos/asset/assets/effect-asset.ts | 1 - cocos/render-scene/core/pass.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/cocos/asset/assets/effect-asset.ts b/cocos/asset/assets/effect-asset.ts index 665c17e0c2d..ab330a8145d 100644 --- a/cocos/asset/assets/effect-asset.ts +++ b/cocos/asset/assets/effect-asset.ts @@ -34,7 +34,6 @@ import { Asset } from './asset'; import { cclegacy, warnID } from '../../core'; import { ProgramLibrary } from '../../rendering/custom/private'; import { addEffectDefaultProperties, getCombinationDefines } from '../../render-scene/core/program-utils'; -import { MaterialPropertyFull } from './material'; import { TextureBase } from './texture-base'; export declare namespace EffectAsset { diff --git a/cocos/render-scene/core/pass.ts b/cocos/render-scene/core/pass.ts index 1ef629adc63..d27fdbe9f63 100644 --- a/cocos/render-scene/core/pass.ts +++ b/cocos/render-scene/core/pass.ts @@ -42,7 +42,6 @@ import { import { RenderPassStage, RenderPriority, SetIndex } from '../../rendering/define'; import { InstancedBuffer } from '../../rendering/instanced-buffer'; import { ProgramLibrary } from '../../rendering/custom/private'; -import { MaterialPropertyFull } from '../../asset/assets/material'; export interface IPassInfoFull extends EffectAsset.IPassInfo { // generated part From f502f55af15b7d7e0c7d962075ebb3530bcdc10b Mon Sep 17 00:00:00 2001 From: xubing <58895777@qq.com> Date: Wed, 11 Oct 2023 09:35:45 +0800 Subject: [PATCH 6/7] fix ci error --- cocos/asset/assets/effect-asset.jsb.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cocos/asset/assets/effect-asset.jsb.ts b/cocos/asset/assets/effect-asset.jsb.ts index 3cc9133abe0..da5e592f58c 100644 --- a/cocos/asset/assets/effect-asset.jsb.ts +++ b/cocos/asset/assets/effect-asset.jsb.ts @@ -29,6 +29,7 @@ import type { EffectAsset as JsbEffectAsset } from './effect-asset'; import type { BlendState, DepthStencilState, RasterizerState, DynamicStateFlags, PrimitiveMode, ShaderStageFlags, Type, Uniform, MemoryAccess, Format} from "../../gfx/index.jsb"; import type { RenderPassStage } from '../../rendering/define'; import type { MacroRecord } from '../../render-scene/core/pass-utils'; +import { TextureBase } from './texture-base'; declare const jsb: any; @@ -50,7 +51,7 @@ export declare namespace EffectAsset { type: number; // auto-extracted from shader handleInfo?: [string, number, number]; // auto-generated from 'target' samplerHash?: number; // auto-generated from 'sampler' - value?: number[] | string; // default value + value?: number[] | string | TextureBase; // default value linear?: boolean; // whether to convert the input to linear space first before applying } // Pass instance itself are compliant to IPassStates too From f2bd029b16b0982a6074afbdfa62282219172ffb Mon Sep 17 00:00:00 2001 From: xubing <58895777@qq.com> Date: Wed, 11 Oct 2023 10:54:22 +0800 Subject: [PATCH 7/7] fix ci error --- cocos/render-scene/core/pass.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cocos/render-scene/core/pass.ts b/cocos/render-scene/core/pass.ts index d27fdbe9f63..17df53bd899 100644 --- a/cocos/render-scene/core/pass.ts +++ b/cocos/render-scene/core/pass.ts @@ -432,8 +432,12 @@ export class Pass { const binding = Pass.getBindingFromHandle(handle); const info = this._properties[name]; const value = info && info.value; - // eslint-disable-next-line max-len - const textureBase = typeof value === 'string' ? builtinResMgr.get(value ? `${value}${getStringFromType(type)}` : getDefaultFromType(type) as string) : value as TextureBase; + let textureBase: TextureBase; + if (typeof value === 'string') { + textureBase = builtinResMgr.get(`${value}${getStringFromType(type)}`); + } else { + textureBase = value as TextureBase || builtinResMgr.get(getDefaultFromType(type) as string); + } const texture = textureBase && textureBase.getGFXTexture()!; const samplerInfo = info && info.samplerHash !== undefined ? Sampler.unpackFromHash(info.samplerHash) : textureBase && textureBase.getSamplerInfo();