From f2a9607aa4acfe8a3be660fe206af6ef6b4ec38d Mon Sep 17 00:00:00 2001 From: ChiaNing Date: Mon, 20 Jun 2022 04:54:31 -0500 Subject: [PATCH] The base version of sorting priority add (#11599) * sorting Priority add * fix CI * name change * binding add * name change to priority * bind remove Co-authored-by: ChiaNing --- cocos/3d/framework/mesh-renderer.ts | 1 + cocos/core/components/model-renderer.ts | 26 +++++++++++++++++++ cocos/core/pipeline/custom/executor.ts | 3 ++- cocos/core/pipeline/custom/web-scene.ts | 1 + cocos/core/pipeline/define.ts | 1 + cocos/core/pipeline/render-queue.ts | 4 ++- cocos/core/renderer/scene/model.ts | 14 ++++++++++ cocos/particle/particle-system.ts | 1 + native/cocos/renderer/pipeline/Define.h | 5 ++++ .../cocos/renderer/pipeline/RenderQueue.cpp | 3 ++- native/cocos/scene/Model.h | 4 +++ native/tools/tojs/scene.ini | 2 +- 12 files changed, 61 insertions(+), 4 deletions(-) diff --git a/cocos/3d/framework/mesh-renderer.ts b/cocos/3d/framework/mesh-renderer.ts index bf3b4319c1..939f7440e0 100644 --- a/cocos/3d/framework/mesh-renderer.ts +++ b/cocos/3d/framework/mesh-renderer.ts @@ -367,6 +367,7 @@ export class MeshRenderer extends ModelRenderer { } public onEnable () { + super.onEnable(); if (!this._model) { this._updateModels(); } diff --git a/cocos/core/components/model-renderer.ts b/cocos/core/components/model-renderer.ts index 1ea7950ccc..9baeaf4567 100644 --- a/cocos/core/components/model-renderer.ts +++ b/cocos/core/components/model-renderer.ts @@ -49,9 +49,23 @@ export class ModelRenderer extends Renderer { this._onVisibilityChange(val); } + /** + * @en The priority which will be applied to the committed models.(Valid only in transparent queues) + * @zh 应用于所有提交渲染的 Model 的排序优先级(只在半透明渲染队列中起效) + */ + get priority () { + return this._priority; + } + + set priority (val) { + if (val === this._priority) return; + this._updatePriority(); + } + @serializable protected _visFlags = Layers.Enum.NONE; protected _models: scene.Model[] = []; + protected _priority = 0; /** * @zh 收集组件中的 models @@ -62,6 +76,10 @@ export class ModelRenderer extends Renderer { return this._models; } + protected onEnable () { + this._updatePriority(); + } + protected _attachToScene () { } @@ -70,4 +88,12 @@ export class ModelRenderer extends Renderer { protected _onVisibilityChange (val) { } + + protected _updatePriority () { + if (this._models.length > 0) { + for (let i = 0; i < this._models.length; i++) { + this._models[i].priority = this._priority; + } + } + } } diff --git a/cocos/core/pipeline/custom/executor.ts b/cocos/core/pipeline/custom/executor.ts index 2fa05dfc62..b21a172910 100644 --- a/cocos/core/pipeline/custom/executor.ts +++ b/cocos/core/pipeline/custom/executor.ts @@ -414,6 +414,7 @@ class DevicePreSceneTask extends WebSceneTask { } const hash = (0 << 30) | pass.priority << 16 | subModel.priority << 8 | passIdx; const rp = new RenderInfo(); + rp.priority = ro.model.priority; rp.hash = hash; rp.depth = ro.depth || 0; rp.shaderId = shader.typedID; @@ -435,7 +436,7 @@ class DevicePreSceneTask extends WebSceneTask { * @zh 比较排序函数。半透明对象按优先级 -> 深度由后向前 -> Shader ID 顺序排序。 */ protected _transparentCompareFn (a: RenderInfo, b: RenderInfo) { - return (a.hash - b.hash) || (b.depth - a.depth) || (a.shaderId - b.shaderId); + return (a.priority - b.priority) || (a.hash - b.hash) || (b.depth - a.depth) || (a.shaderId - b.shaderId); } private _uploadInstanceBuffers () { diff --git a/cocos/core/pipeline/custom/web-scene.ts b/cocos/core/pipeline/custom/web-scene.ts index 0b5a1e2abe..17877e73dc 100644 --- a/cocos/core/pipeline/custom/web-scene.ts +++ b/cocos/core/pipeline/custom/web-scene.ts @@ -43,6 +43,7 @@ export class RenderObject implements IRenderObject { } } export class RenderInfo implements IRenderPass { + public priority = 0; public hash = -1; public depth = -1; public shaderId = -1; diff --git a/cocos/core/pipeline/define.ts b/cocos/core/pipeline/define.ts index 19205f21ca..762bd2d735 100644 --- a/cocos/core/pipeline/define.ts +++ b/cocos/core/pipeline/define.ts @@ -72,6 +72,7 @@ export interface IRenderObject { * @zh 渲染过程接口。 */ export interface IRenderPass { + priority: number; hash: number; depth: number; shaderId: number; diff --git a/cocos/core/pipeline/render-queue.ts b/cocos/core/pipeline/render-queue.ts index bd504977b0..6d64a658df 100644 --- a/cocos/core/pipeline/render-queue.ts +++ b/cocos/core/pipeline/render-queue.ts @@ -44,7 +44,7 @@ export function opaqueCompareFn (a: IRenderPass, b: IRenderPass) { * @zh 比较排序函数。半透明对象按优先级 -> 深度由后向前 -> Shader ID 顺序排序。 */ export function transparentCompareFn (a: IRenderPass, b: IRenderPass) { - return (a.hash - b.hash) || (b.depth - a.depth) || (a.shaderId - b.shaderId); + return (a.priority - b.priority) || (a.hash - b.hash) || (b.depth - a.depth) || (a.shaderId - b.shaderId); } /** @@ -69,6 +69,7 @@ export class RenderQueue { constructor (desc: IRenderQueueDesc) { this._passDesc = desc; this._passPool = new RecyclePool(() => ({ + priority: 0, hash: 0, depth: 0, shaderId: 0, @@ -105,6 +106,7 @@ export class RenderQueue { } const hash = (0 << 30) | pass.priority << 16 | subModel.priority << 8 | passIdx; const rp = this._passPool.add(); + rp.priority = renderObj.model.priority; rp.hash = hash; rp.depth = renderObj.depth || 0; rp.shaderId = shader.typedID; diff --git a/cocos/core/renderer/scene/model.ts b/cocos/core/renderer/scene/model.ts index 8854795f8d..c69e12c6d2 100644 --- a/cocos/core/renderer/scene/model.ts +++ b/cocos/core/renderer/scene/model.ts @@ -266,6 +266,18 @@ export class Model { this._enabled = val; } + /** + * @en Rendering priority in the transparent queue of model. + * @zh Model 在透明队列中的渲染排序优先级 + */ + get priority () : number { + return this._priority; + } + + set priority (val: number) { + this._priority = val; + } + /** * @en The type of the model * @zh 模型类型 @@ -412,6 +424,8 @@ export class Model { */ protected _visFlags = Layers.Enum.NONE; + protected _priority = 0; + /** * @internal * @en native object diff --git a/cocos/particle/particle-system.ts b/cocos/particle/particle-system.ts index afec818dcc..add9f1a3a0 100644 --- a/cocos/particle/particle-system.ts +++ b/cocos/particle/particle-system.ts @@ -961,6 +961,7 @@ export class ParticleSystem extends ModelRenderer { } protected onEnable () { + super.onEnable(); legacyCC.director.on(legacyCC.Director.EVENT_BEFORE_COMMIT, this.beforeRender, this); if (this.playOnAwake && (!EDITOR || legacyCC.GAME_VIEW)) { this.play(); diff --git a/native/cocos/renderer/pipeline/Define.h b/native/cocos/renderer/pipeline/Define.h index c983bddeb9..0711a1ebbe 100644 --- a/native/cocos/renderer/pipeline/Define.h +++ b/native/cocos/renderer/pipeline/Define.h @@ -65,6 +65,7 @@ struct CC_DLL RenderTargetInfo { }; struct CC_DLL RenderPass { + uint32_t priority = 0; uint32_t hash = 0; float depth = 0.0F; uint32_t shaderID = 0; @@ -175,6 +176,10 @@ inline bool opaqueCompareFn(const RenderPass &a, const RenderPass &b) { } inline bool transparentCompareFn(const RenderPass &a, const RenderPass &b) { + if (a.priority != b.priority) { + return a.priority < b.priority; + } + if (a.hash != b.hash) { return a.hash < b.hash; } diff --git a/native/cocos/renderer/pipeline/RenderQueue.cpp b/native/cocos/renderer/pipeline/RenderQueue.cpp index 3c9552a54a..a69d31ee5e 100644 --- a/native/cocos/renderer/pipeline/RenderQueue.cpp +++ b/native/cocos/renderer/pipeline/RenderQueue.cpp @@ -60,7 +60,8 @@ bool RenderQueue::insertRenderPass(const RenderObject &renderObj, uint32_t subMo auto modelPriority = static_cast(subModel->getPriority()); auto shaderId = static_cast(reinterpret_cast(subModel->getShader(passIdx))); const auto hash = (0 << 30) | (passPriority << 16) | (modelPriority << 8) | passIdx; - RenderPass renderPass = {hash, renderObj.depth, shaderId, passIdx, subModel}; + const auto priority = renderObj.model->getPriority(); + RenderPass renderPass = {priority, hash, renderObj.depth, shaderId, passIdx, subModel}; _queue.emplace_back(renderPass); return true; diff --git a/native/cocos/scene/Model.h b/native/cocos/scene/Model.h index 2e4581e908..31fd0936e6 100644 --- a/native/cocos/scene/Model.h +++ b/native/cocos/scene/Model.h @@ -164,6 +164,8 @@ public: inline bool isDynamicBatching() const { return _isDynamicBatching; } inline float getShadowBias() const { return _shadowBias; } inline float getShadowNormalBias() const { return _shadowNormalBias; } + inline uint32_t getPriority() const { return _priority; } + inline void setPriority(uint32_t value) { _priority = value; } void initLocalDescriptors(index_t subModelIndex); void initWorldBoundDescriptors(index_t subModelIndex); @@ -227,6 +229,8 @@ protected: IntrusivePtr _lightmap; Vec4 _lightmapUVParam; + uint32_t _priority{0}; + // For JS CallbacksInvoker _eventProcessor; bool _isCalledFromJS{false}; diff --git a/native/tools/tojs/scene.ini b/native/tools/tojs/scene.ini index d825593bc3..8829aab861 100644 --- a/native/tools/tojs/scene.ini +++ b/native/tools/tojs/scene.ini @@ -106,7 +106,7 @@ getter_setter = Root::[device:_device mainWindow curWindow tempWindow windows sc RenderScene::[name cameras? mainLight? sphereLights? spotLights? models], Skybox::[model enabled/isEnabled/setEnabled useHDR/isUseHDR/setUseHDR useIBL/isUseIBL/setUseIBL useDiffuseMap/isUseDiffuseMap/setUseDiffuseMap isRGBE/isRGBE/setRGBE envmap diffuseMap], Fog::[enabled/isEnabled/setEnabled accurate/isAccurate/setAccurate fogColor type fogDensity fogStart fogEnd fogAtten fogTop fogRange fogRange colorArray], - Model::[scene _subModels:subModels/getSubModels inited/isInited _localDataUpdated/isLocalDataUpdated/setLocalDataUpdated _worldBounds:worldBounds/getWorldBounds/setWorldBounds _modelBounds:modelBounds/getModelBounds/setModelBounds worldBoundBuffer localBuffer updateStamp isInstancingEnabled/isInstancingEnabled receiveShadow/isReceiveShadow/setReceiveShadow castShadow/isCastShadow/setCastShadow node transform visFlags enabled/isEnabled/setEnabled type instancedAttributes/getInstancedAttributeBlock/setInstancedAttributeBlock isDynamicBatching/isDynamicBatching/setDynamicBatching], + Model::[scene _subModels:subModels/getSubModels inited/isInited _localDataUpdated/isLocalDataUpdated/setLocalDataUpdated _worldBounds:worldBounds/getWorldBounds/setWorldBounds _modelBounds:modelBounds/getModelBounds/setModelBounds worldBoundBuffer localBuffer updateStamp isInstancingEnabled/isInstancingEnabled receiveShadow/isReceiveShadow/setReceiveShadow castShadow/isCastShadow/setCastShadow node transform visFlags enabled/isEnabled/setEnabled type instancedAttributes/getInstancedAttributeBlock/setInstancedAttributeBlock isDynamicBatching/isDynamicBatching/setDynamicBatching priority], SubModel::[passes shaders subMesh priority inputAssembler descriptorSet patches planarInstanceShader planarShader], ShadowsInfo::[enabled/isEnabled/setEnabled type planeDirection planeHeight shadowColor maxReceived shadowMapSize], Shadows::[enabled/isEnabled/setEnabled type normal distance shadowColor maxReceived size shadowMapDirty/isShadowMapDirty/setShadowMapDirty matLight material instancingMaterial],