diff --git a/cocos/core/root.jsb.ts b/cocos/core/root.jsb.ts index bb618a900c..f66de6fdf3 100644 --- a/cocos/core/root.jsb.ts +++ b/cocos/core/root.jsb.ts @@ -44,6 +44,14 @@ Object.defineProperty(rootProto, 'dataPoolManager', { }, }); +Object.defineProperty(rootProto, 'usesCustomPipeline', { + configurable: true, + enumerable: true, + get () { + return this._usesCustomPipeline; + } +}); + Object.defineProperty(rootProto, 'pipeline', { configurable: true, enumerable: true, @@ -76,8 +84,11 @@ rootProto._ctor = function (device: Device) { this._modelPools = new Map(); this._lightPools = new Map(); this._batcher = null; + this._usesCustomPipeline = false; this._pipeline = null; this._pipelineEvent = new DummyPipelineEvent(); + this._classicPipeline = null; + this._customPipeline = null; this._registerListeners(); }; @@ -180,12 +191,16 @@ rootProto.setRenderPipeline = function (pipeline) { pipeline = new nr.ForwardPipeline(); pipeline.init(); } - this._pipeline = pipeline; - this._pipeline.geometryRenderer.activate(this._device, this._pipeline); - if (!this._customPipeline) { + if (this._usesCustomPipeline) { this._customPipeline = createCustomPipeline(); + this._pipeline = this._customPipeline; + } else { + this._classicPipeline = pipeline; + this._pipeline = this._classicPipeline; } + this._pipeline.geometryRenderer.activate(this._device, this._pipeline); + return oldSetPipeline.call(this, pipeline); } diff --git a/cocos/core/root.ts b/cocos/core/root.ts index 4a7b1463b7..33621e41a0 100644 --- a/cocos/core/root.ts +++ b/cocos/core/root.ts @@ -115,6 +115,14 @@ export class Root { return this._windows; } + /** + * @zh + * 启用自定义渲染管线 + */ + public get usesCustomPipeline (): boolean { + return this._usesCustomPipeline; + } + /** * @zh * 渲染管线 @@ -219,8 +227,10 @@ export class Root { private _mainWindow: RenderWindow | null = null; private _curWindow: RenderWindow | null = null; private _tempWindow: RenderWindow | null = null; - private _pipeline: RenderPipeline | null = null; + private _usesCustomPipeline = false; + private _pipeline: PipelineRuntime | null = null; private _pipelineEvent: IPipelineEvent | null = null; + private _classicPipeline: RenderPipeline | null = null; private _customPipeline: Pipeline | null = null; private _batcher: Batcher2D | null = null; private _dataPoolMgr: DataPoolManager; @@ -311,6 +321,9 @@ export class Root { } public setRenderPipeline (rppl: RenderPipeline): boolean { + //----------------------------------------------- + // prepare classic pipeline + //----------------------------------------------- if (rppl instanceof DeferredPipeline) { this._useDeferredPipeline = true; } @@ -320,25 +333,42 @@ export class Root { rppl = createDefaultPipeline(); isCreateDefaultPipeline = true; } - this._pipeline = rppl; - this._pipelineEvent = rppl; + // now cluster just enabled in deferred pipeline if (!this._useDeferredPipeline || !this.device.hasFeature(Feature.COMPUTE_SHADER)) { // disable cluster - this._pipeline.clusterEnabled = false; + rppl.clusterEnabled = false; + } + rppl.bloomEnabled = false; + + //----------------------------------------------- + // choose pipeline + //----------------------------------------------- + if (this.usesCustomPipeline) { + this._customPipeline = createCustomPipeline(); + isCreateDefaultPipeline = true; + this._pipeline = this._customPipeline!; + } else { + this._classicPipeline = rppl; + this._pipeline = this._classicPipeline; + this._pipelineEvent = this._classicPipeline; } - this._pipeline.bloomEnabled = false; if (!this._pipeline.activate(this._mainWindow!.swapchain)) { if (isCreateDefaultPipeline) { this._pipeline.destroy(); } + this._classicPipeline = null; + this._customPipeline = null; this._pipeline = null; this._pipelineEvent = null; return false; } + //----------------------------------------------- + // pipeline initialization completed + //----------------------------------------------- const scene = legacyCC.director.getScene(); if (scene) { scene.globals.activate(); @@ -353,11 +383,6 @@ export class Root { } } - // TODO: refactor custom pipeline initialization - if (!this._customPipeline) { - this._customPipeline = createCustomPipeline(); - } - return true; }