diff --git a/cocos/core/pipeline/forward/forward-flow.ts b/cocos/core/pipeline/forward/forward-flow.ts index d16149174b..b9c11cc158 100644 --- a/cocos/core/pipeline/forward/forward-flow.ts +++ b/cocos/core/pipeline/forward/forward-flow.ts @@ -36,7 +36,7 @@ import { ForwardStage } from './forward-stage'; import { ForwardPipeline } from './forward-pipeline'; import { RenderPipeline } from '../render-pipeline'; import { Camera } from '../../renderer/scene'; -import { sceneCulling } from './scene-culling'; +import { sceneCulling } from '../scene-culling'; /** * @en The forward flow in forward render pipeline diff --git a/cocos/core/pipeline/forward/forward-pipeline.ts b/cocos/core/pipeline/forward/forward-pipeline.ts index d2f5aca32d..d8dcc17e72 100644 --- a/cocos/core/pipeline/forward/forward-pipeline.ts +++ b/cocos/core/pipeline/forward/forward-pipeline.ts @@ -33,29 +33,17 @@ import { RenderPipeline, IRenderPipelineInfo } from '../render-pipeline'; import { ForwardFlow } from './forward-flow'; import { RenderTextureConfig, MaterialConfig } from '../pipeline-serialization'; import { ShadowFlow } from '../shadow/shadow-flow'; -import { IRenderObject, UBOGlobal, UBOShadow, UBOCamera, UNIFORM_SHADOWMAP_BINDING, UNIFORM_SPOT_LIGHTING_MAP_TEXTURE_BINDING } from '../define'; -import { BufferUsageBit, MemoryUsageBit, ClearFlag, Filter, Address, StoreOp } from '../../gfx/define'; +import { UBOGlobal, UBOShadow, UBOCamera, UNIFORM_SHADOWMAP_BINDING, UNIFORM_SPOT_LIGHTING_MAP_TEXTURE_BINDING } from '../define'; +import { ClearFlag, Filter, Address, StoreOp } from '../../gfx/define'; import { ColorAttachment, DepthStencilAttachment, RenderPass, LoadOp, TextureLayout, - RenderPassInfo, BufferInfo, Feature, Framebuffer } from '../../gfx'; + RenderPassInfo, Feature } from '../../gfx'; import { SKYBOX_FLAG } from '../../renderer/scene/camera'; -import { legacyCC } from '../../global-exports'; -import { Color, Mat4, Vec3, Vec4 } from '../../math'; -import { Fog } from '../../renderer/scene/fog'; -import { Ambient } from '../../renderer/scene/ambient'; -import { Skybox } from '../../renderer/scene/skybox'; -import { Shadows, ShadowType } from '../../renderer/scene/shadows'; -import { getShadowWorldMatrix, updatePlanarPROJ } from './scene-culling'; -import { Light } from '../../renderer/scene/light'; import { genSamplerHash, samplerLib } from '../../renderer/core/sampler-lib'; import { builtinResMgr } from '../../builtin'; import { Texture2D } from '../../assets/texture-2d'; import { Camera } from '../../renderer/scene'; import { errorID } from '../../platform/debug'; -const matShadowView = new Mat4(); -const matShadowViewProj = new Mat4(); -const vec3_center = new Vec3(); - const _samplerInfo = [ Filter.LINEAR, Filter.LINEAR, @@ -71,36 +59,6 @@ const _samplerInfo = [ */ @ccclass('ForwardPipeline') export class ForwardPipeline extends RenderPipeline { - get isHDR () { - return this._isHDR; - } - - set isHDR (val) { - if (this._isHDR === val) { - return; - } - - this._isHDR = val; - const defaultGlobalUBOData = this._cameraUBO; - defaultGlobalUBOData[UBOCamera.EXPOSURE_OFFSET + 2] = this._isHDR ? 1.0 : 0.0; - } - - get shadingScale (): number { - return this._shadingScale; - } - - get fpScale (): number { - return this._fpScale; - } - - /** - * @en Get shadow UBO. - * @zh 获取阴影UBO。 - */ - get shadowUBO (): Float32Array { - return this._shadowUBO; - } - @type([RenderTextureConfig]) @serializable @displayOrder(2) @@ -110,26 +68,7 @@ export class ForwardPipeline extends RenderPipeline { @serializable @displayOrder(3) protected materials: MaterialConfig[] = []; - - public fog: Fog = new Fog(); - public ambient: Ambient = new Ambient(); - public skybox: Skybox = new Skybox(); - public shadows: Shadows = new Shadows(); - /** - * @en The list for render objects, only available after the scene culling of the current frame. - * @zh 渲染对象数组,仅在当前帧的场景剔除完成后有效。 - * @readonly - */ - public renderObjects: IRenderObject[] = []; - public shadowObjects: IRenderObject[] = []; - public shadowFrameBufferMap: Map = new Map(); - protected _isHDR = false; - protected _shadingScale = 1.0; - protected _fpScale: number = 1.0 / 1024.0; protected _renderPasses = new Map(); - protected _globalUBO = new Float32Array(UBOGlobal.COUNT); - protected _cameraUBO = new Float32Array(UBOCamera.COUNT); - protected _shadowUBO = new Float32Array(UBOShadow.COUNT); public initialize (info: IRenderPipelineInfo): boolean { super.initialize(info); @@ -164,11 +103,11 @@ export class ForwardPipeline extends RenderPipeline { public render (cameras: Camera[]) { this._commandBuffers[0].begin(); - this.updateGlobalUBO(); + this._pipelineUBO.updateGlobalUBO(); for (let i = 0; i < cameras.length; i++) { const camera = cameras[i]; if (camera.scene) { - this.updateCameraUBO(camera); + this._pipelineUBO.updateCameraUBO(camera); for (let j = 0; j < this._flows.length; j++) { this._flows[j].render(camera); } @@ -212,66 +151,11 @@ export class ForwardPipeline extends RenderPipeline { return renderPass; } - /** - * @en Update all UBOs - * @zh 更新全部 UBO。 - */ - public updateGlobalUBO () { - this._descriptorSet.update(); - const root = legacyCC.director.root; - const fv = this._globalUBO; - const device = this.device; - - const shadingWidth = Math.floor(device.width); - const shadingHeight = Math.floor(device.height); - - // update UBOGlobal - fv[UBOGlobal.TIME_OFFSET] = root.cumulativeTime; - fv[UBOGlobal.TIME_OFFSET + 1] = root.frameTime; - fv[UBOGlobal.TIME_OFFSET + 2] = legacyCC.director.getTotalFrames(); - - fv[UBOGlobal.SCREEN_SIZE_OFFSET] = device.width; - fv[UBOGlobal.SCREEN_SIZE_OFFSET + 1] = device.height; - fv[UBOGlobal.SCREEN_SIZE_OFFSET + 2] = 1.0 / device.width; - fv[UBOGlobal.SCREEN_SIZE_OFFSET + 3] = 1.0 / device.height; - - fv[UBOGlobal.NATIVE_SIZE_OFFSET] = shadingWidth; - fv[UBOGlobal.NATIVE_SIZE_OFFSET + 1] = shadingHeight; - fv[UBOGlobal.NATIVE_SIZE_OFFSET + 2] = 1.0 / fv[UBOGlobal.NATIVE_SIZE_OFFSET]; - fv[UBOGlobal.NATIVE_SIZE_OFFSET + 3] = 1.0 / fv[UBOGlobal.NATIVE_SIZE_OFFSET + 1]; - - this._commandBuffers[0].updateBuffer(this._descriptorSet.getBuffer(UBOGlobal.BINDING), this._globalUBO); - } - private _activeRenderer () { const device = this.device; this._commandBuffers.push(device.commandBuffer); - const globalUBO = device.createBuffer(new BufferInfo( - BufferUsageBit.UNIFORM | BufferUsageBit.TRANSFER_DST, - MemoryUsageBit.HOST | MemoryUsageBit.DEVICE, - UBOGlobal.SIZE, - UBOGlobal.SIZE, - )); - this._descriptorSet.bindBuffer(UBOGlobal.BINDING, globalUBO); - - const cameraUBO = device.createBuffer(new BufferInfo( - BufferUsageBit.UNIFORM | BufferUsageBit.TRANSFER_DST, - MemoryUsageBit.HOST | MemoryUsageBit.DEVICE, - UBOCamera.SIZE, - UBOCamera.SIZE, - )); - this._descriptorSet.bindBuffer(UBOCamera.BINDING, cameraUBO); - - const shadowUBO = device.createBuffer(new BufferInfo( - BufferUsageBit.UNIFORM | BufferUsageBit.TRANSFER_DST, - MemoryUsageBit.HOST | MemoryUsageBit.DEVICE, - UBOShadow.SIZE, - UBOShadow.SIZE, - )); - this._descriptorSet.bindBuffer(UBOShadow.BINDING, shadowUBO); - const shadowMapSamplerHash = genSamplerHash(_samplerInfo); const shadowMapSampler = samplerLib.getSampler(device, shadowMapSamplerHash); this._descriptorSet.bindSampler(UNIFORM_SHADOWMAP_BINDING, shadowMapSampler); @@ -281,149 +165,12 @@ export class ForwardPipeline extends RenderPipeline { this._descriptorSet.update(); // update global defines when all states initialized. - this.macros.CC_USE_HDR = this._isHDR; + this.macros.CC_USE_HDR = this._pipelineSceneData.isHDR; this.macros.CC_SUPPORT_FLOAT_TEXTURE = this.device.hasFeature(Feature.TEXTURE_FLOAT); return true; } - public updateShadowUBO (camera: Camera) { - this._descriptorSet.update(); - const mainLight = camera.scene!.mainLight; - const device = this.device; - const shadowInfo = this.shadows; - - if (shadowInfo.enabled) { - if (mainLight && shadowInfo.type === ShadowType.ShadowMap) { - if (this.shadowFrameBufferMap.has(mainLight)) { - this._descriptorSet.bindTexture(UNIFORM_SHADOWMAP_BINDING, this.shadowFrameBufferMap.get(mainLight)!.colorTextures[0]!); - } - - // light view - let shadowCameraView: Mat4; - - // light proj - let x = 0; - let y = 0; - let far = 0; - if (shadowInfo.autoAdapt) { - shadowCameraView = getShadowWorldMatrix(this, mainLight.node!.getWorldRotation()!, mainLight.direction, vec3_center); - // if orthoSize is the smallest, auto calculate orthoSize. - const radius = shadowInfo.sphere.radius; - x = radius * shadowInfo.aspect; - y = radius; - - const halfFar = Vec3.distance(shadowInfo.sphere.center, vec3_center); - far = Math.min(halfFar * Shadows.COEFFICIENT_OF_EXPANSION, Shadows.MAX_FAR); - } else { - shadowCameraView = mainLight.node!.getWorldMatrix(); - - x = shadowInfo.orthoSize * shadowInfo.aspect; - y = shadowInfo.orthoSize; - - far = shadowInfo.far; - } - - Mat4.invert(matShadowView, shadowCameraView!); - - const projectionSignY = device.screenSpaceSignY * device.UVSpaceSignY; // always offscreen - Mat4.ortho(matShadowViewProj, -x, x, -y, y, shadowInfo.near, far, - device.clipSpaceMinZ, projectionSignY); - Mat4.multiply(matShadowViewProj, matShadowViewProj, matShadowView); - Mat4.toArray(this._shadowUBO, matShadowViewProj, UBOShadow.MAT_LIGHT_VIEW_PROJ_OFFSET); - - this._shadowUBO[UBOShadow.SHADOW_INFO_OFFSET] = shadowInfo.size.x; - this._shadowUBO[UBOShadow.SHADOW_INFO_OFFSET + 1] = shadowInfo.size.y; - this._shadowUBO[UBOShadow.SHADOW_INFO_OFFSET + 2] = shadowInfo.pcf; - this._shadowUBO[UBOShadow.SHADOW_INFO_OFFSET + 3] = shadowInfo.bias; - } else if (mainLight && shadowInfo.type === ShadowType.Planar) { - updatePlanarPROJ(shadowInfo, mainLight, this._shadowUBO); - } - - Color.toArray(this._shadowUBO, shadowInfo.shadowColor, UBOShadow.SHADOW_COLOR_OFFSET); - this._commandBuffers[0].updateBuffer(this._descriptorSet.getBuffer(UBOShadow.BINDING), this._shadowUBO); - } - } - - public updateCameraUBO (camera: Camera) { - const device = this.device; - const scene = camera.scene ? camera.scene : legacyCC.director.getScene().renderScene; - const mainLight = scene.mainLight; - const ambient = this.ambient; - const fog = this.fog; - const shadingWidth = Math.floor(device.width); - const shadingHeight = Math.floor(device.height); - // update camera ubo - const cv = this._cameraUBO; - const exposure = camera.exposure; - - cv[UBOCamera.SCREEN_SCALE_OFFSET] = camera.width / shadingWidth * this.shadingScale; - cv[UBOCamera.SCREEN_SCALE_OFFSET + 1] = camera.height / shadingHeight * this.shadingScale; - cv[UBOCamera.SCREEN_SCALE_OFFSET + 2] = 1.0 / cv[UBOCamera.SCREEN_SCALE_OFFSET]; - cv[UBOCamera.SCREEN_SCALE_OFFSET + 3] = 1.0 / cv[UBOCamera.SCREEN_SCALE_OFFSET + 1]; - - cv[UBOCamera.EXPOSURE_OFFSET] = exposure; - cv[UBOCamera.EXPOSURE_OFFSET + 1] = 1.0 / exposure; - cv[UBOCamera.EXPOSURE_OFFSET + 2] = this._isHDR ? 1.0 : 0.0; - cv[UBOCamera.EXPOSURE_OFFSET + 3] = this._fpScale / exposure; - - if (mainLight) { - Vec3.toArray(cv, mainLight.direction, UBOCamera.MAIN_LIT_DIR_OFFSET); - Vec3.toArray(cv, mainLight.color, UBOCamera.MAIN_LIT_COLOR_OFFSET); - if (mainLight.useColorTemperature) { - const colorTempRGB = mainLight.colorTemperatureRGB; - cv[UBOCamera.MAIN_LIT_COLOR_OFFSET] *= colorTempRGB.x; - cv[UBOCamera.MAIN_LIT_COLOR_OFFSET + 1] *= colorTempRGB.y; - cv[UBOCamera.MAIN_LIT_COLOR_OFFSET + 2] *= colorTempRGB.z; - } - - if (this._isHDR) { - cv[UBOCamera.MAIN_LIT_COLOR_OFFSET + 3] = mainLight.illuminance * this._fpScale; - } else { - cv[UBOCamera.MAIN_LIT_COLOR_OFFSET + 3] = mainLight.illuminance * exposure; - } - } else { - Vec3.toArray(cv, Vec3.UNIT_Z, UBOCamera.MAIN_LIT_DIR_OFFSET); - Vec4.toArray(cv, Vec4.ZERO, UBOCamera.MAIN_LIT_COLOR_OFFSET); - } - - const skyColor = ambient.colorArray; - if (this._isHDR) { - skyColor[3] = ambient.skyIllum * this._fpScale; - } else { - skyColor[3] = ambient.skyIllum * exposure; - } - cv.set(skyColor, UBOCamera.AMBIENT_SKY_OFFSET); - cv.set(ambient.albedoArray, UBOCamera.AMBIENT_GROUND_OFFSET); - - Mat4.toArray(cv, camera.matView, UBOCamera.MAT_VIEW_OFFSET); - Mat4.toArray(cv, camera.node.worldMatrix, UBOCamera.MAT_VIEW_INV_OFFSET); - Mat4.toArray(cv, camera.matProj, UBOCamera.MAT_PROJ_OFFSET); - Mat4.toArray(cv, camera.matProjInv, UBOCamera.MAT_PROJ_INV_OFFSET); - Mat4.toArray(cv, camera.matViewProj, UBOCamera.MAT_VIEW_PROJ_OFFSET); - Mat4.toArray(cv, camera.matViewProjInv, UBOCamera.MAT_VIEW_PROJ_INV_OFFSET); - Vec3.toArray(cv, camera.position, UBOCamera.CAMERA_POS_OFFSET); - let projectionSignY = device.screenSpaceSignY; - if (camera.window!.hasOffScreenAttachments) { - projectionSignY *= device.UVSpaceSignY; // need flipping if drawing on render targets - } - cv[UBOCamera.CAMERA_POS_OFFSET + 3] = projectionSignY; - - if (fog.enabled) { - cv.set(fog.colorArray, UBOCamera.GLOBAL_FOG_COLOR_OFFSET); - - cv[UBOCamera.GLOBAL_FOG_BASE_OFFSET] = fog.fogStart; - cv[UBOCamera.GLOBAL_FOG_BASE_OFFSET + 1] = fog.fogEnd; - cv[UBOCamera.GLOBAL_FOG_BASE_OFFSET + 2] = fog.fogDensity; - - cv[UBOCamera.GLOBAL_FOG_ADD_OFFSET] = fog.fogTop; - cv[UBOCamera.GLOBAL_FOG_ADD_OFFSET + 1] = fog.fogRange; - cv[UBOCamera.GLOBAL_FOG_ADD_OFFSET + 2] = fog.fogAtten; - } - - this._commandBuffers[0].updateBuffer(this._descriptorSet.getBuffer(UBOCamera.BINDING), this._cameraUBO); - } - private destroyUBOs () { if (this._descriptorSet) { this._descriptorSet.getBuffer(UBOGlobal.BINDING).destroy(); @@ -448,11 +195,6 @@ export class ForwardPipeline extends RenderPipeline { this._commandBuffers.length = 0; - this.ambient.destroy(); - this.skybox.destroy(); - this.fog.destroy(); - this.shadows.destroy(); - return super.destroy(); } } diff --git a/cocos/core/pipeline/forward/forward-stage.ts b/cocos/core/pipeline/forward/forward-stage.ts index c69d75cbc9..32ecbf429a 100644 --- a/cocos/core/pipeline/forward/forward-stage.ts +++ b/cocos/core/pipeline/forward/forward-stage.ts @@ -147,7 +147,7 @@ export class ForwardStage extends RenderStage { const device = pipeline.device; this._renderQueues.forEach(this.renderQueueClearFunc); - const renderObjects = pipeline.renderObjects; + const renderObjects = pipeline.pipelineSceneData.renderObjects; let m = 0; let p = 0; let k = 0; for (let i = 0; i < renderObjects.length; ++i) { const ro = renderObjects[i]; @@ -186,18 +186,19 @@ export class ForwardStage extends RenderStage { this._planarQueue.gatherShadowPasses(camera, cmdBuff); const vp = camera.viewport; + const sceneData = pipeline.pipelineSceneData; // render area is not oriented const w = camera.window!.hasOnScreenAttachments && device.surfaceTransform % 2 ? camera.height : camera.width; const h = camera.window!.hasOnScreenAttachments && device.surfaceTransform % 2 ? camera.width : camera.height; this._renderArea.x = vp.x * w; this._renderArea.y = vp.y * h; - this._renderArea.width = vp.width * w * pipeline.shadingScale; - this._renderArea.height = vp.height * h * pipeline.shadingScale; + this._renderArea.width = vp.width * w * sceneData.shadingScale; + this._renderArea.height = vp.height * h * sceneData.shadingScale; if (camera.clearFlag & ClearFlag.COLOR) { - if (pipeline.isHDR) { + if (sceneData.isHDR) { SRGBToLinear(colors[0], camera.clearColor); - const scale = pipeline.fpScale / camera.exposure; + const scale = sceneData.fpScale / camera.exposure; colors[0].x *= scale; colors[0].y *= scale; colors[0].z *= scale; diff --git a/cocos/core/pipeline/forward/planar-shadow-queue.ts b/cocos/core/pipeline/forward/planar-shadow-queue.ts index c3f56c7b1e..8730d6138e 100644 --- a/cocos/core/pipeline/forward/planar-shadow-queue.ts +++ b/cocos/core/pipeline/forward/planar-shadow-queue.ts @@ -47,18 +47,20 @@ export class PlanarShadowQueue { } public gatherShadowPasses (camera: Camera, cmdBuff: CommandBuffer) { - const shadows = this._pipeline.shadows; + const pipelineSceneData = this._pipeline.pipelineSceneData; + const pipelineUBO = this._pipeline.pipelineUBO; + const shadows = pipelineSceneData.shadows; this._pendingModels.length = 0; if (!shadows.enabled || shadows.type !== ShadowType.Planar) { return; } - this._pipeline.updateShadowUBO(camera); + pipelineUBO.updateShadowUBO(camera); const scene = camera.scene!; const frstm = camera.frustum; const shadowVisible = (camera.visibility & Layers.BitMask.DEFAULT) !== 0; if (!scene.mainLight || !shadowVisible) { return; } - const models = this._pipeline.renderObjects; + const models = pipelineSceneData.renderObjects; const instancedBuffer = InstancedBuffer.get(shadows.instancingMaterial.passes[0]); this._instancedQueue.clear(); this._instancedQueue.queue.add(instancedBuffer); @@ -81,7 +83,7 @@ export class PlanarShadowQueue { } public recordCommandBuffer (device: Device, renderPass: RenderPass, cmdBuff: CommandBuffer) { - const shadows = this._pipeline.shadows; + const shadows = this._pipeline.pipelineSceneData.shadows; if (!shadows.enabled || shadows.type !== ShadowType.Planar || !this._pendingModels.length) { return; } this._instancedQueue.recordCommandBuffer(device, renderPass, cmdBuff); diff --git a/cocos/core/pipeline/index.jsb.ts b/cocos/core/pipeline/index.jsb.ts index 14e7deb5d8..859c766576 100644 --- a/cocos/core/pipeline/index.jsb.ts +++ b/cocos/core/pipeline/index.jsb.ts @@ -27,33 +27,146 @@ declare const nr: any; import { getPhaseID } from './pass-phase' -import { RenderWindow } from '../renderer/core/render-window'; +import { setClassName } from '../../core/utils/js'; +import { PipelineSceneData } from './pipeline-scene-data'; nr.getPhaseID = getPhaseID; export const RenderPipeline = nr.RenderPipeline; export const RenderFlow = nr.RenderFlow; export const RenderStage = nr.RenderStage; - -export const ForwardPipeline = nr.ForwardPipeline; -export const ForwardFlow = nr.ForwardFlow; -export const ForwardStage = nr.ForwardStage; -export const ShadowFlow = nr.ShadowFlow; -export const ShadowStage = nr.ShadowStage; - export const InstancedBuffer = nr.InstancedBuffer; export const PipelineStateManager = nr.PipelineStateManager; -const RenderWindowProto = RenderWindow.prototype; -Object.assign(RenderWindowProto, { - extractRenderCameras() { - const cameras = []; - for (let j = 0; j < this._cameras.length; j++) { - const camera = this._cameras[j]; - if (camera.enabled) { - camera.update(); - cameras.push(camera.handle); - } +let instancedBufferProto = nr.InstancedBuffer; +let oldGetFunc = instancedBufferProto.get; +instancedBufferProto.get = function(pass) { + return oldGetFunc.call(this, pass.handle); +}; + +let getOrCreatePipelineState = nr.PipelineStateManager.getOrCreatePipelineState; +nr.PipelineStateManager.getOrCreatePipelineState = function(device, pass, shader, renderPass, ia) { + return getOrCreatePipelineState.call(this, pass.handle, shader, renderPass, ia); +} + +export function createDefaultPipeline () { + const pipeline = new nr.ForwardPipeline(); + const info = new nr.RenderPipelineInfo(0, []); + pipeline.initialize(info); + return pipeline; +} + +// ForwardPipeline +export class ForwardPipeline extends nr.ForwardPipeline { + public pipelineSceneData = new PipelineSceneData(); + + constructor() { + super(); + this._tag = 0; + this._flows = []; + this.renderTextures = []; + this.materials = []; + } + + public init() { + this.setPipelineSharedSceneData(this.pipelineSceneData.handle); + for (let i = 0; i < this._flows.length; i++) { + this._flows[i].init(); } - return cameras; - }, -}) + const info = new nr.RenderPipelineInfo(this._tag, this._flows); + this.initialize(info); + } + + public destroy () { + this.pipelineSceneData.destroy(); + } +} +// hook to invoke init after deserialization +ForwardPipeline.prototype.onAfterDeserialize_JSB = ForwardPipeline.prototype.init; + +export class ForwardFlow extends nr.ForwardFlow { + constructor() { + super(); + this._name = 0; + this._priority = 0; + this._tag = 0; + this._stages = []; + } + init() { + for (let i = 0; i < this._stages.length; i++) { + this._stages[i].init(); + } + const info = new nr.RenderFlowInfo(this._name, this._priority, this._tag, this._stages); + this.initialize(info); + } +} + +export class ShadowFlow extends nr.ShadowFlow { + constructor() { + super(); + this._name = 0; + this._priority = 0; + this._tag = 0; + this._stages = []; + } + init() { + for (let i = 0; i < this._stages.length; i++) { + this._stages[i].init(); + } + const info = new nr.RenderFlowInfo(this._name, this._priority, this._tag, this._stages); + this.initialize(info); + } +} + +export class ForwardStage extends nr.ForwardStage { + constructor() { + super(); + this._name = 0; + this._priority = 0; + this._tag = 0; + this.renderQueues = []; + } + public init() { + const queues = []; + for (let i = 0; i < this.renderQueues.length; i++) { + queues.push(this.renderQueues[i].init()); + } + const info = new nr.RenderStageInfo(this._name, this._priority, this._tag, queues); + this.initialize(info); + } +} + +export class ShadowStage extends nr.ShadowStage { + constructor() { + super(); + this._name = 0; + this._priority = 0; + this._tag = 0; + } + public init() { + const info = new nr.RenderStageInfo(this._name, this._priority, this._tag, []); + this.initialize(info); + } +} + +export class RenderQueueDesc { + public isTransparent = false; + public sortMode = 0; + public stages = []; + + constructor() { + this.isTransparent = false; + this.sortMode = 0; + this.stages = []; + } + + public init() { + return new nr.RenderQueueDesc(this.isTransparent, this.sortMode, this.stages); + } +} + +setClassName('ForwardPipeline', ForwardPipeline); +setClassName('ForwardFlow', ForwardFlow); +setClassName('ShadowFlow', ShadowFlow); +setClassName('ForwardStage', ForwardStage); +setClassName('ShadowStage', ShadowStage); +setClassName('RenderQueueDesc', RenderQueueDesc); diff --git a/cocos/core/pipeline/index.ts b/cocos/core/pipeline/index.ts index 6164cdd571..93dbd8963b 100644 --- a/cocos/core/pipeline/index.ts +++ b/cocos/core/pipeline/index.ts @@ -29,6 +29,7 @@ */ import * as pipeline from './define'; +import { ForwardPipeline } from './forward/forward-pipeline'; export { pipeline }; @@ -46,3 +47,9 @@ export { ShadowStage } from './shadow/shadow-stage'; export { InstancedBuffer } from './instanced-buffer'; export { PipelineStateManager } from './pipeline-state-manager'; + +export function createDefaultPipeline () { + const rppl = new ForwardPipeline(); + rppl.initialize({ flows: [] }); + return rppl; +} diff --git a/cocos/core/pipeline/pipeline-scene-data.ts b/cocos/core/pipeline/pipeline-scene-data.ts new file mode 100644 index 0000000000..26938f9345 --- /dev/null +++ b/cocos/core/pipeline/pipeline-scene-data.ts @@ -0,0 +1,106 @@ +/* + Copyright (c) 2020 Xiamen Yaji Software Co., Ltd. + + https://www.cocos.com/ + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + */ + +import { Fog } from '../renderer/scene/fog'; +import { Ambient } from '../renderer/scene/ambient'; +import { Skybox } from '../renderer/scene/skybox'; +import { Shadows } from '../renderer/scene/shadows'; +import { IRenderObject } from './define'; +import { Device, Framebuffer } from '../gfx'; +import { RenderPipeline } from './render-pipeline'; +import { Light } from '../renderer/scene/light'; +import { PipelineSceneDataPool, PipelineSceneDataHandle, PipelineSceneDataView } from '../renderer/core/memory-pools'; + +export class PipelineSceneData { + public get handle () { + return this._handle; + } + /** + * @en Is open HDR. + * @zh 是否开启 HDR。 + * @readonly + */ + public get isHDR () { + return PipelineSceneDataPool.get(this._handle, PipelineSceneDataView.IS_HDR) as unknown as boolean; + } + + public set isHDR (val: boolean) { + PipelineSceneDataPool.set(this._handle, PipelineSceneDataView.IS_HDR, val ? 1 : 0); + } + public get shadingScale () { + return PipelineSceneDataPool.get(this._handle, PipelineSceneDataView.SHADING_SCALE); + } + + public set shadingScale (val: number) { + PipelineSceneDataPool.set(this._handle, PipelineSceneDataView.SHADING_SCALE, val); + } + public get fpScale () { + return PipelineSceneDataPool.get(this._handle, PipelineSceneDataView.FP_SCALE); + } + + public set fpScale (val: number) { + PipelineSceneDataPool.set(this._handle, PipelineSceneDataView.FP_SCALE, val); + } + public fog: Fog = new Fog(); + public ambient: Ambient = new Ambient(); + public skybox: Skybox = new Skybox(); + public shadows: Shadows = new Shadows(); + /** + * @en The list for render objects, only available after the scene culling of the current frame. + * @zh 渲染对象数组,仅在当前帧的场景剔除完成后有效。 + */ + public renderObjects: IRenderObject[] = []; + public shadowObjects: IRenderObject[] = []; + public shadowFrameBufferMap: Map = new Map(); + protected declare _device: Device; + protected declare _pipeline: RenderPipeline; + protected declare _handle: PipelineSceneDataHandle; + + constructor () { + this._handle = PipelineSceneDataPool.alloc(); + PipelineSceneDataPool.set(this._handle, PipelineSceneDataView.AMBIENT, this.ambient.handle); + PipelineSceneDataPool.set(this._handle, PipelineSceneDataView.SKYBOX, this.skybox.handle); + PipelineSceneDataPool.set(this._handle, PipelineSceneDataView.FOG, this.fog.handle); + PipelineSceneDataPool.set(this._handle, PipelineSceneDataView.SHADOW, this.shadows.handle); + PipelineSceneDataPool.set(this._handle, PipelineSceneDataView.IS_HDR, 0); + PipelineSceneDataPool.set(this._handle, PipelineSceneDataView.SHADING_SCALE, 1.0); + PipelineSceneDataPool.set(this._handle, PipelineSceneDataView.FP_SCALE, 1.0 / 1024.0); + } + + public activate (device: Device, pipeline: RenderPipeline) { + this._device = device; + this._pipeline = pipeline; + } + + public destroy () { + this.ambient.destroy(); + this.skybox.destroy(); + this.fog.destroy(); + this.shadows.destroy(); + if (this._handle) { + PipelineSceneDataPool.free(this._handle); + } + } +} diff --git a/cocos/core/pipeline/pipeline-ubo.ts b/cocos/core/pipeline/pipeline-ubo.ts new file mode 100644 index 0000000000..31a419e748 --- /dev/null +++ b/cocos/core/pipeline/pipeline-ubo.ts @@ -0,0 +1,347 @@ +/* + Copyright (c) 2020 Xiamen Yaji Software Co., Ltd. + + https://www.cocos.com/ + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + */ + +import { UBOGlobal, UBOShadow, UBOCamera, UNIFORM_SHADOWMAP_BINDING } from './define'; +import { Device, BufferInfo } from '../gfx'; +import { Camera } from '../renderer/scene/camera'; +import { Mat4, Vec3, Vec4, Color } from '../math'; +import { RenderPipeline } from './render-pipeline'; +import { legacyCC } from '../global-exports'; +import { Shadows, ShadowType } from '../renderer/scene/shadows'; +import { getShadowWorldMatrix, updatePlanarPROJ } from './scene-culling'; +import { BufferUsageBit, MemoryUsageBit } from '../gfx/define'; +import { Light, LightType } from '../renderer/scene/light'; + +const matShadowView = new Mat4(); +const matShadowViewProj = new Mat4(); +const vec3_center = new Vec3(); +const vec4ShadowInfo = new Vec4(); +const _shadowCameraView = new Mat4(); + +export class PipelineUBO { + public static updateGlobalUBOView (pipeline: RenderPipeline, bufferView: Float32Array) { + const device = pipeline.device; + const root = legacyCC.director.root; + const fv = bufferView; + + const shadingWidth = Math.floor(device.width); + const shadingHeight = Math.floor(device.height); + + // update UBOGlobal + fv[UBOGlobal.TIME_OFFSET] = root.cumulativeTime; + fv[UBOGlobal.TIME_OFFSET + 1] = root.frameTime; + fv[UBOGlobal.TIME_OFFSET + 2] = legacyCC.director.getTotalFrames(); + + fv[UBOGlobal.SCREEN_SIZE_OFFSET] = device.width; + fv[UBOGlobal.SCREEN_SIZE_OFFSET + 1] = device.height; + fv[UBOGlobal.SCREEN_SIZE_OFFSET + 2] = 1.0 / device.width; + fv[UBOGlobal.SCREEN_SIZE_OFFSET + 3] = 1.0 / device.height; + + fv[UBOGlobal.NATIVE_SIZE_OFFSET] = shadingWidth; + fv[UBOGlobal.NATIVE_SIZE_OFFSET + 1] = shadingHeight; + fv[UBOGlobal.NATIVE_SIZE_OFFSET + 2] = 1.0 / fv[UBOGlobal.NATIVE_SIZE_OFFSET]; + fv[UBOGlobal.NATIVE_SIZE_OFFSET + 3] = 1.0 / fv[UBOGlobal.NATIVE_SIZE_OFFSET + 1]; + } + + public static updateCameraUBOView (pipeline: RenderPipeline, bufferView: Float32Array, camera: Camera) { + const device = pipeline.device; + const scene = camera.scene ? camera.scene : legacyCC.director.getScene().renderScene; + const mainLight = scene.mainLight; + const sceneData = pipeline.pipelineSceneData; + const ambient = sceneData.ambient; + const fog = sceneData.fog; + const shadingWidth = Math.floor(device.width); + const shadingHeight = Math.floor(device.height); + const cv = bufferView; + const exposure = camera.exposure; + const isHDR = sceneData.isHDR; + const shadingScale = sceneData.shadingScale; + const fpScale = sceneData.fpScale; + + // update camera ubo + cv[UBOCamera.SCREEN_SCALE_OFFSET] = camera.width / shadingWidth * shadingScale; + cv[UBOCamera.SCREEN_SCALE_OFFSET + 1] = camera.height / shadingHeight * shadingScale; + cv[UBOCamera.SCREEN_SCALE_OFFSET + 2] = 1.0 / cv[UBOCamera.SCREEN_SCALE_OFFSET]; + cv[UBOCamera.SCREEN_SCALE_OFFSET + 3] = 1.0 / cv[UBOCamera.SCREEN_SCALE_OFFSET + 1]; + + cv[UBOCamera.EXPOSURE_OFFSET] = exposure; + cv[UBOCamera.EXPOSURE_OFFSET + 1] = 1.0 / exposure; + cv[UBOCamera.EXPOSURE_OFFSET + 2] = isHDR ? 1.0 : 0.0; + cv[UBOCamera.EXPOSURE_OFFSET + 3] = fpScale / exposure; + + if (mainLight) { + Vec3.toArray(cv, mainLight.direction, UBOCamera.MAIN_LIT_DIR_OFFSET); + Vec3.toArray(cv, mainLight.color, UBOCamera.MAIN_LIT_COLOR_OFFSET); + if (mainLight.useColorTemperature) { + const colorTempRGB = mainLight.colorTemperatureRGB; + cv[UBOCamera.MAIN_LIT_COLOR_OFFSET] *= colorTempRGB.x; + cv[UBOCamera.MAIN_LIT_COLOR_OFFSET + 1] *= colorTempRGB.y; + cv[UBOCamera.MAIN_LIT_COLOR_OFFSET + 2] *= colorTempRGB.z; + } + + if (isHDR) { + cv[UBOCamera.MAIN_LIT_COLOR_OFFSET + 3] = mainLight.illuminance * fpScale; + } else { + cv[UBOCamera.MAIN_LIT_COLOR_OFFSET + 3] = mainLight.illuminance * exposure; + } + } else { + Vec3.toArray(cv, Vec3.UNIT_Z, UBOCamera.MAIN_LIT_DIR_OFFSET); + Vec4.toArray(cv, Vec4.ZERO, UBOCamera.MAIN_LIT_COLOR_OFFSET); + } + + const skyColor = ambient.colorArray; + if (isHDR) { + skyColor[3] = ambient.skyIllum * fpScale; + } else { + skyColor[3] = ambient.skyIllum * exposure; + } + cv.set(skyColor, UBOCamera.AMBIENT_SKY_OFFSET); + cv.set(ambient.albedoArray, UBOCamera.AMBIENT_GROUND_OFFSET); + + Mat4.toArray(cv, camera.matView, UBOCamera.MAT_VIEW_OFFSET); + Mat4.toArray(cv, camera.node.worldMatrix, UBOCamera.MAT_VIEW_INV_OFFSET); + Mat4.toArray(cv, camera.matProj, UBOCamera.MAT_PROJ_OFFSET); + Mat4.toArray(cv, camera.matProjInv, UBOCamera.MAT_PROJ_INV_OFFSET); + Mat4.toArray(cv, camera.matViewProj, UBOCamera.MAT_VIEW_PROJ_OFFSET); + Mat4.toArray(cv, camera.matViewProjInv, UBOCamera.MAT_VIEW_PROJ_INV_OFFSET); + Vec3.toArray(cv, camera.position, UBOCamera.CAMERA_POS_OFFSET); + let projectionSignY = device.screenSpaceSignY; + if (camera.window!.hasOffScreenAttachments) { + projectionSignY *= device.UVSpaceSignY; // need flipping if drawing on render targets + } + cv[UBOCamera.CAMERA_POS_OFFSET + 3] = projectionSignY; + + if (fog.enabled) { + cv.set(fog.colorArray, UBOCamera.GLOBAL_FOG_COLOR_OFFSET); + + cv[UBOCamera.GLOBAL_FOG_BASE_OFFSET] = fog.fogStart; + cv[UBOCamera.GLOBAL_FOG_BASE_OFFSET + 1] = fog.fogEnd; + cv[UBOCamera.GLOBAL_FOG_BASE_OFFSET + 2] = fog.fogDensity; + + cv[UBOCamera.GLOBAL_FOG_ADD_OFFSET] = fog.fogTop; + cv[UBOCamera.GLOBAL_FOG_ADD_OFFSET + 1] = fog.fogRange; + cv[UBOCamera.GLOBAL_FOG_ADD_OFFSET + 2] = fog.fogAtten; + } + } + + public static updateShadowUBOView (pipeline: RenderPipeline, bufferView: Float32Array, camera: Camera) { + const device = pipeline.device; + const mainLight = camera.scene!.mainLight; + const sceneData = pipeline.pipelineSceneData; + const shadowInfo = sceneData.shadows; + const sv = bufferView; + + if (shadowInfo.enabled) { + if (mainLight && shadowInfo.type === ShadowType.ShadowMap) { + // light view + let shadowCameraView: Mat4; + + // light proj + let x = 0; + let y = 0; + let far = 0; + if (shadowInfo.autoAdapt) { + shadowCameraView = getShadowWorldMatrix(pipeline, mainLight.node!.getWorldRotation()!, mainLight.direction, vec3_center); + // if orthoSize is the smallest, auto calculate orthoSize. + const radius = shadowInfo.sphere.radius; + x = radius * shadowInfo.aspect; + y = radius; + + const halfFar = Vec3.distance(shadowInfo.sphere.center, vec3_center); + far = Math.min(halfFar * Shadows.COEFFICIENT_OF_EXPANSION, Shadows.MAX_FAR); + } else { + shadowCameraView = mainLight.node!.getWorldMatrix(); + + x = shadowInfo.orthoSize * shadowInfo.aspect; + y = shadowInfo.orthoSize; + + far = shadowInfo.far; + } + + Mat4.invert(matShadowView, shadowCameraView!); + + const projectionSignY = device.screenSpaceSignY * device.UVSpaceSignY; // always offscreen + Mat4.ortho(matShadowViewProj, -x, x, -y, y, shadowInfo.near, far, + device.clipSpaceMinZ, projectionSignY); + Mat4.multiply(matShadowViewProj, matShadowViewProj, matShadowView); + Mat4.toArray(sv, matShadowViewProj, UBOShadow.MAT_LIGHT_VIEW_PROJ_OFFSET); + + sv[UBOShadow.SHADOW_INFO_OFFSET] = shadowInfo.size.x; + sv[UBOShadow.SHADOW_INFO_OFFSET + 1] = shadowInfo.size.y; + sv[UBOShadow.SHADOW_INFO_OFFSET + 2] = shadowInfo.pcf; + sv[UBOShadow.SHADOW_INFO_OFFSET + 3] = shadowInfo.bias; + } else if (mainLight && shadowInfo.type === ShadowType.Planar) { + updatePlanarPROJ(shadowInfo, mainLight, sv); + } + + Color.toArray(sv, shadowInfo.shadowColor, UBOShadow.SHADOW_COLOR_OFFSET); + } + } + + public static updateShadowUBOLightView (pipeline: RenderPipeline, bufferView: Float32Array, light: Light) { + const device = pipeline.device; + const shadowInfo = pipeline.pipelineSceneData.shadows; + const sv = bufferView; + let _x = 0; let _y = 0; let _far = 0; + let shadowCameraView: Mat4; + switch (light.type) { + case LightType.DIRECTIONAL: + // light view + (light).update(); + // light proj + if (shadowInfo.autoAdapt) { + const node = (light).node; + if (node) { + shadowCameraView = getShadowWorldMatrix(pipeline, node.getWorldRotation(), (light as any).direction, vec3_center); + } + // if orthoSize is the smallest, auto calculate orthoSize. + const radius = shadowInfo.sphere.radius; + _x = radius * shadowInfo.aspect; + _y = radius; + + const halfFar = Vec3.distance(shadowInfo.sphere.center, vec3_center); + _far = Math.min(halfFar * Shadows.COEFFICIENT_OF_EXPANSION, Shadows.MAX_FAR); + } else { + shadowCameraView = (light as any).node.getWorldMatrix(); + + _x = shadowInfo.orthoSize * shadowInfo.aspect; + _y = shadowInfo.orthoSize; + + _far = shadowInfo.far; + } + + Mat4.invert(matShadowView, _shadowCameraView); + + Mat4.ortho(matShadowViewProj, -_x, _x, -_y, _y, shadowInfo.near, _far, + device.clipSpaceMinZ, device.screenSpaceSignY * device.UVSpaceSignY); + break; + case LightType.SPOT: + // light view + Mat4.invert(matShadowView, (light as any).node.getWorldMatrix()); + + // light proj + Mat4.perspective(matShadowViewProj, (light as any).spotAngle, (light as any).aspect, 0.001, (light as any).range); + break; + default: + } + // light viewProj + Mat4.multiply(matShadowViewProj, matShadowViewProj, matShadowView); + + Mat4.toArray(sv, matShadowViewProj, UBOShadow.MAT_LIGHT_VIEW_PROJ_OFFSET); + + Color.toArray(sv, shadowInfo.shadowColor, UBOShadow.SHADOW_COLOR_OFFSET); + + vec4ShadowInfo.set(shadowInfo.size.x, shadowInfo.size.y, shadowInfo.pcf, shadowInfo.bias); + Vec4.toArray(sv, vec4ShadowInfo, UBOShadow.SHADOW_INFO_OFFSET); + } + + protected _globalUBO = new Float32Array(UBOGlobal.COUNT); + protected _cameraUBO = new Float32Array(UBOCamera.COUNT); + protected _shadowUBO = new Float32Array(UBOShadow.COUNT); + protected declare _device: Device; + protected declare _pipeline: RenderPipeline; + + public activate (device: Device, pipeline: RenderPipeline) { + this._device = device; + this._pipeline = pipeline; + const ds = this._pipeline.descriptorSet; + + const globalUBO = device.createBuffer(new BufferInfo( + BufferUsageBit.UNIFORM | BufferUsageBit.TRANSFER_DST, + MemoryUsageBit.HOST | MemoryUsageBit.DEVICE, + UBOGlobal.SIZE, + UBOGlobal.SIZE, + )); + ds.bindBuffer(UBOGlobal.BINDING, globalUBO); + + const cameraUBO = device.createBuffer(new BufferInfo( + BufferUsageBit.UNIFORM | BufferUsageBit.TRANSFER_DST, + MemoryUsageBit.HOST | MemoryUsageBit.DEVICE, + UBOCamera.SIZE, + UBOCamera.SIZE, + )); + ds.bindBuffer(UBOCamera.BINDING, cameraUBO); + + const shadowUBO = device.createBuffer(new BufferInfo( + BufferUsageBit.UNIFORM | BufferUsageBit.TRANSFER_DST, + MemoryUsageBit.HOST | MemoryUsageBit.DEVICE, + UBOShadow.SIZE, + UBOShadow.SIZE, + )); + ds.bindBuffer(UBOShadow.BINDING, shadowUBO); + } + + /** + * @en Update all UBOs + * @zh 更新全部 UBO。 + */ + public updateGlobalUBO () { + const ds = this._pipeline.descriptorSet; + const cmdBuffer = this._pipeline.commandBuffers; + ds.update(); + PipelineUBO.updateGlobalUBOView(this._pipeline, this._globalUBO); + cmdBuffer[0].updateBuffer(ds.getBuffer(UBOGlobal.BINDING), this._globalUBO); + } + + public updateCameraUBO (camera: Camera) { + const ds = this._pipeline.descriptorSet; + const cmdBuffer = this._pipeline.commandBuffers; + PipelineUBO.updateCameraUBOView(this._pipeline, this._cameraUBO, camera); + cmdBuffer[0].updateBuffer(ds.getBuffer(UBOCamera.BINDING), this._cameraUBO); + } + + public updateShadowUBO (camera: Camera) { + const sceneData = this._pipeline.pipelineSceneData; + const shadowInfo = sceneData.shadows; + if (!shadowInfo.enabled) return; + const ds = this._pipeline.descriptorSet; + const cmdBuffer = this._pipeline.commandBuffers; + const shadowFrameBufferMap = sceneData.shadowFrameBufferMap; + const mainLight = camera.scene!.mainLight; + ds.update(); + if (mainLight && shadowFrameBufferMap.has(mainLight)) { + ds.bindTexture(UNIFORM_SHADOWMAP_BINDING, shadowFrameBufferMap.get(mainLight)!.colorTextures[0]!); + } + PipelineUBO.updateShadowUBOView(this._pipeline, this._shadowUBO, camera); + cmdBuffer[0].updateBuffer(ds.getBuffer(UBOShadow.BINDING), this._shadowUBO); + } + + public updateShadowUBOLight (light: Light) { + const ds = this._pipeline.descriptorSet; + PipelineUBO.updateShadowUBOLightView(this._pipeline, this._shadowUBO, light); + ds.getBuffer(UBOShadow.BINDING).update(this._shadowUBO); + } + + public updateShadowUBORange (offset: number, data: Mat4 | Color) { + if (data instanceof Mat4) { + Mat4.toArray(this._shadowUBO, data, offset); + } else if (data instanceof Color) { + Color.toArray(this._shadowUBO, data, offset); + } + } + + public destroy () { + } +} diff --git a/cocos/core/pipeline/render-additive-light-queue.ts b/cocos/core/pipeline/render-additive-light-queue.ts index bda1f2ee97..ff8973c952 100644 --- a/cocos/core/pipeline/render-additive-light-queue.ts +++ b/cocos/core/pipeline/render-additive-light-queue.ts @@ -29,7 +29,7 @@ */ import { BatchedBuffer } from './batched-buffer'; import { BatchingSchemes } from '../renderer/core/pass'; -import { ForwardPipeline } from './forward/forward-pipeline'; +import { RenderPipeline } from './render-pipeline'; import { InstancedBuffer } from './instanced-buffer'; import { Model } from '../renderer/scene/model'; import { PipelineStateManager } from './pipeline-state-manager'; @@ -47,13 +47,13 @@ import { SpotLight } from '../renderer/scene/spot-light'; import { SubModel } from '../renderer/scene/submodel'; import { getPhaseID } from './pass-phase'; import { Light, LightType } from '../renderer/scene/light'; -import { IRenderObject, SetIndex, UBOForwardLight, UBOGlobal, UBOShadow, UBOCamera, UNIFORM_SHADOWMAP_BINDING, UNIFORM_SPOT_LIGHTING_MAP_TEXTURE_BINDING } from './define'; -import { legacyCC } from '../global-exports'; +import { SetIndex, UBOForwardLight, UBOGlobal, UBOShadow, UBOCamera, UNIFORM_SHADOWMAP_BINDING, UNIFORM_SPOT_LIGHTING_MAP_TEXTURE_BINDING } from './define'; import { genSamplerHash, samplerLib } from '../renderer/core/sampler-lib'; import { builtinResMgr } from '../builtin/builtin-res-mgr'; import { Texture2D } from '../assets/texture-2d'; -import { updatePlanarPROJ } from './forward/scene-culling'; +import { updatePlanarPROJ } from './scene-culling'; import { Camera } from '../renderer/scene'; +import { PipelineUBO } from './pipeline-ubo'; const _samplerInfo = [ Filter.LINEAR, @@ -113,53 +113,29 @@ function getLightPassIndices (subModels: SubModel[], lightPassIndices: number[]) * @zh 叠加光照队列。 */ export class RenderAdditiveLightQueue { - private _pipeline: ForwardPipeline; + private _pipeline: RenderPipeline; private _device: Device; - private _validLights: Light[] = []; - private _lightPasses: IAdditiveLightPass[] = []; - private _descriptorSetMap: Map = new Map(); - private _globalUBO = new Float32Array(UBOGlobal.COUNT); - private _cameraUBO = new Float32Array(UBOCamera.COUNT); - private _shadowUBO = new Float32Array(UBOShadow.COUNT); - private _lightBufferCount = 16; - private _lightBufferStride: number; - private _lightBufferElementCount: number; - private _lightBuffer: Buffer; - private _firstLightBufferView: Buffer; - private _lightBufferData: Float32Array; - - private _isHDR: boolean; - - private _fpScale: number; - - private _renderObjects: IRenderObject[]; - private _instancedQueue: RenderInstancedQueue; - private _batchedQueue: RenderBatchedQueue; - private _lightMeterScale = 10000.0; private _sampler: Sampler | null = null; - constructor (pipeline: ForwardPipeline) { + constructor (pipeline: RenderPipeline) { this._pipeline = pipeline; this._device = pipeline.device; - this._isHDR = pipeline.isHDR; - this._fpScale = pipeline.fpScale; - this._renderObjects = pipeline.renderObjects; this._instancedQueue = new RenderInstancedQueue(); this._batchedQueue = new RenderBatchedQueue(); @@ -249,9 +225,9 @@ export class RenderAdditiveLightQueue { this._updateUBOs(camera, cmdBuff); this._updateLightDescriptorSet(camera, cmdBuff); - - for (let i = 0; i < this._renderObjects.length; i++) { - const ro = this._renderObjects[i]; + const renderObjects = this._pipeline.pipelineSceneData.renderObjects; + for (let i = 0; i < renderObjects.length; i++) { + const ro = renderObjects[i]; const { model } = ro; const { subModels } = model; if (!getLightPassIndices(subModels, _lightPassIndices)) { continue; } @@ -348,16 +324,18 @@ export class RenderAdditiveLightQueue { // update light DescriptorSet protected _updateLightDescriptorSet (camera: Camera, cmdBuff: CommandBuffer) { - const shadowInfo = this._pipeline.shadows; + const sceneData = this._pipeline.pipelineSceneData; + const shadowInfo = sceneData.shadows; + const shadowFrameBufferMap = sceneData.shadowFrameBufferMap; const mainLight = camera.scene!.mainLight; + PipelineUBO.updateGlobalUBOView(this._pipeline, this._globalUBO); + PipelineUBO.updateCameraUBOView(this._pipeline, this._cameraUBO, camera); + for (let i = 0; i < this._validLights.length; i++) { const light = this._validLights[i]; const descriptorSet = this._getOrCreateDescriptorSet(light); if (!descriptorSet) { continue; } - - this._updateGlobalDescriptorSet(camera, cmdBuff); - switch (light.type) { case LightType.SPHERE: // planar PROJ @@ -394,9 +372,9 @@ export class RenderAdditiveLightQueue { this._shadowUBO[UBOShadow.SHADOW_INFO_OFFSET + 2] = shadowInfo.pcf; this._shadowUBO[UBOShadow.SHADOW_INFO_OFFSET + 3] = shadowInfo.bias; // Spot light sampler binding - if (this._pipeline.shadowFrameBufferMap.has(light)) { - if (this._pipeline.shadowFrameBufferMap.has(light)) { - const texture = this._pipeline.shadowFrameBufferMap.get(light)?.colorTextures[0]; + if (shadowFrameBufferMap.has(light)) { + if (shadowFrameBufferMap.has(light)) { + const texture = shadowFrameBufferMap.get(light)?.colorTextures[0]; if (texture) { descriptorSet.bindTexture(UNIFORM_SPOT_LIGHTING_MAP_TEXTURE_BINDING, texture); } @@ -413,111 +391,11 @@ export class RenderAdditiveLightQueue { } } - protected _updateGlobalDescriptorSet (camera: Camera, cmdBuff: CommandBuffer) { - const root = legacyCC.director.root; - const device = this._pipeline.device; - const fv = this._globalUBO; - - const shadingWidth = Math.floor(device.width); - const shadingHeight = Math.floor(device.height); - - // update UBOGlobal - fv[UBOGlobal.TIME_OFFSET] = root.cumulativeTime; - fv[UBOGlobal.TIME_OFFSET + 1] = root.frameTime; - fv[UBOGlobal.TIME_OFFSET + 2] = legacyCC.director.getTotalFrames(); - - fv[UBOGlobal.SCREEN_SIZE_OFFSET] = device.width; - fv[UBOGlobal.SCREEN_SIZE_OFFSET + 1] = device.height; - fv[UBOGlobal.SCREEN_SIZE_OFFSET + 2] = 1.0 / device.width; - fv[UBOGlobal.SCREEN_SIZE_OFFSET + 3] = 1.0 / device.height; - - fv[UBOGlobal.NATIVE_SIZE_OFFSET] = shadingWidth; - fv[UBOGlobal.NATIVE_SIZE_OFFSET + 1] = shadingHeight; - fv[UBOGlobal.NATIVE_SIZE_OFFSET + 2] = 1.0 / fv[UBOGlobal.NATIVE_SIZE_OFFSET]; - fv[UBOGlobal.NATIVE_SIZE_OFFSET + 3] = 1.0 / fv[UBOGlobal.NATIVE_SIZE_OFFSET + 1]; - - this._updateCameraUBO(camera); - } - - protected _updateCameraUBO (camera: Camera) { - const pipeline = this._pipeline; - const scene = camera.scene!; - const mainLight = scene.mainLight; - const ambient = pipeline.ambient; - const shadingScale = pipeline.shadingScale; - const device = this._pipeline.device; - const shadingWidth = Math.floor(device.width); - const shadingHeight = Math.floor(device.height); - const fog = pipeline.fog; - - const cv = this._cameraUBO; - cv[UBOCamera.SCREEN_SCALE_OFFSET] = camera.width / shadingWidth * shadingScale; - cv[UBOCamera.SCREEN_SCALE_OFFSET + 1] = camera.height / shadingHeight * shadingScale; - cv[UBOCamera.SCREEN_SCALE_OFFSET + 2] = 1.0 / cv[UBOCamera.SCREEN_SCALE_OFFSET]; - cv[UBOCamera.SCREEN_SCALE_OFFSET + 3] = 1.0 / cv[UBOCamera.SCREEN_SCALE_OFFSET + 1]; - - const exposure = camera.exposure; - cv[UBOCamera.EXPOSURE_OFFSET] = exposure; - cv[UBOCamera.EXPOSURE_OFFSET + 1] = 1.0 / exposure; - cv[UBOCamera.EXPOSURE_OFFSET + 2] = this._isHDR ? 1.0 : 0.0; - cv[UBOCamera.EXPOSURE_OFFSET + 3] = this._fpScale / exposure; - - if (mainLight) { - Vec3.toArray(cv, mainLight.direction, UBOCamera.MAIN_LIT_DIR_OFFSET); - Vec3.toArray(cv, mainLight.color, UBOCamera.MAIN_LIT_COLOR_OFFSET); - if (mainLight.useColorTemperature) { - const colorTempRGB = mainLight.colorTemperatureRGB; - cv[UBOCamera.MAIN_LIT_COLOR_OFFSET] *= colorTempRGB.x; - cv[UBOCamera.MAIN_LIT_COLOR_OFFSET + 1] *= colorTempRGB.y; - cv[UBOCamera.MAIN_LIT_COLOR_OFFSET + 2] *= colorTempRGB.z; - } - - if (this._isHDR) { - cv[UBOCamera.MAIN_LIT_COLOR_OFFSET + 3] = mainLight.illuminance * this._fpScale; - } else { - cv[UBOCamera.MAIN_LIT_COLOR_OFFSET + 3] = mainLight.illuminance * exposure; - } - } else { - Vec3.toArray(cv, Vec3.UNIT_Z, UBOCamera.MAIN_LIT_DIR_OFFSET); - Vec4.toArray(cv, Vec4.ZERO, UBOCamera.MAIN_LIT_COLOR_OFFSET); - } - Mat4.toArray(cv, camera.matView, UBOCamera.MAT_VIEW_OFFSET); - Mat4.toArray(cv, camera.node.worldMatrix, UBOCamera.MAT_VIEW_INV_OFFSET); - Mat4.toArray(cv, camera.matProj, UBOCamera.MAT_PROJ_OFFSET); - Mat4.toArray(cv, camera.matProjInv, UBOCamera.MAT_PROJ_INV_OFFSET); - Mat4.toArray(cv, camera.matViewProj, UBOCamera.MAT_VIEW_PROJ_OFFSET); - Mat4.toArray(cv, camera.matViewProjInv, UBOCamera.MAT_VIEW_PROJ_INV_OFFSET); - Vec3.toArray(cv, camera.position, UBOCamera.CAMERA_POS_OFFSET); - let projectionSignY = device.screenSpaceSignY; - if (camera.window!.hasOffScreenAttachments) { - projectionSignY *= device.UVSpaceSignY; // need flipping if drawing on render targets - } - cv[UBOCamera.CAMERA_POS_OFFSET + 3] = projectionSignY; - - const skyColor = ambient.colorArray; - if (this._isHDR) { - skyColor[3] = ambient.skyIllum * this._fpScale; - } else { - skyColor[3] = ambient.skyIllum * exposure; - } - cv.set(skyColor, UBOCamera.AMBIENT_SKY_OFFSET); - cv.set(ambient.albedoArray, UBOCamera.AMBIENT_GROUND_OFFSET); - - if (fog.enabled) { - cv.set(fog.colorArray, UBOCamera.GLOBAL_FOG_COLOR_OFFSET); - - cv[UBOCamera.GLOBAL_FOG_BASE_OFFSET] = fog.fogStart; - cv[UBOCamera.GLOBAL_FOG_BASE_OFFSET + 1] = fog.fogEnd; - cv[UBOCamera.GLOBAL_FOG_BASE_OFFSET + 2] = fog.fogDensity; - - cv[UBOCamera.GLOBAL_FOG_ADD_OFFSET] = fog.fogTop; - cv[UBOCamera.GLOBAL_FOG_ADD_OFFSET + 1] = fog.fogRange; - cv[UBOCamera.GLOBAL_FOG_ADD_OFFSET + 2] = fog.fogAtten; - } - } - protected _updateUBOs (camera: Camera, cmdBuff: CommandBuffer) { const { exposure } = camera; + const sceneData = this._pipeline.pipelineSceneData; + const isHDR = sceneData.isHDR; + const fpScale = sceneData.fpScale; if (this._validLights.length > this._lightBufferCount) { this._firstLightBufferView.destroy(); @@ -551,8 +429,8 @@ export class RenderAdditiveLightQueue { _vec4Array[1] *= tempRGB.y; _vec4Array[2] *= tempRGB.z; } - if (this._isHDR) { - _vec4Array[3] = (light as SphereLight).luminance * this._fpScale * this._lightMeterScale; + if (isHDR) { + _vec4Array[3] = (light as SphereLight).luminance * fpScale * this._lightMeterScale; } else { _vec4Array[3] = (light as SphereLight).luminance * exposure * this._lightMeterScale; } @@ -579,8 +457,8 @@ export class RenderAdditiveLightQueue { _vec4Array[1] *= tempRGB.y; _vec4Array[2] *= tempRGB.z; } - if (this._isHDR) { - _vec4Array[3] = (light as SpotLight).luminance * this._fpScale * this._lightMeterScale; + if (isHDR) { + _vec4Array[3] = (light as SpotLight).luminance * fpScale * this._lightMeterScale; } else { _vec4Array[3] = (light as SpotLight).luminance * exposure * this._lightMeterScale; } diff --git a/cocos/core/pipeline/render-pipeline.ts b/cocos/core/pipeline/render-pipeline.ts index a2edc9d707..6b850bbc20 100644 --- a/cocos/core/pipeline/render-pipeline.ts +++ b/cocos/core/pipeline/render-pipeline.ts @@ -36,6 +36,8 @@ import { MacroRecord } from '../renderer/core/pass-utils'; import { Device, DescriptorSet, CommandBuffer, DescriptorSetLayout, DescriptorSetLayoutInfo, DescriptorSetInfo } from '../gfx'; import { globalDescriptorSetLayout } from './define'; import { Camera } from '../renderer/scene/camera'; +import { PipelineUBO } from './pipeline-ubo'; +import { PipelineSceneData } from './pipeline-scene-data'; /** * @en Render pipeline information descriptor @@ -120,10 +122,20 @@ export abstract class RenderPipeline extends Asset { return this._commandBuffers; } + get pipelineUBO () { + return this._pipelineUBO; + } + + get pipelineSceneData () { + return this._pipelineSceneData; + } + protected _device!: Device; protected _descriptorSetLayout!: DescriptorSetLayout; protected _descriptorSet!: DescriptorSet; protected _commandBuffers: CommandBuffer[] = []; + protected _pipelineUBO = new PipelineUBO(); + protected _pipelineSceneData = new PipelineSceneData(); /** * @en The initialization process, user shouldn't use it in most case, only useful when need to generate render pipeline programmatically. @@ -142,11 +154,11 @@ export abstract class RenderPipeline extends Asset { */ public activate (): boolean { this._device = legacyCC.director.root.device; - const layoutInfo = new DescriptorSetLayoutInfo(globalDescriptorSetLayout.bindings); this._descriptorSetLayout = this._device.createDescriptorSetLayout(layoutInfo); - this._descriptorSet = this._device.createDescriptorSet(new DescriptorSetInfo(this._descriptorSetLayout)); + this._pipelineUBO.activate(this._device, this); + this._pipelineSceneData.activate(this._device, this); for (let i = 0; i < this._flows.length; i++) { this._flows[i].activate(this); @@ -188,6 +200,8 @@ export abstract class RenderPipeline extends Asset { this._commandBuffers[i].destroy(); } this._commandBuffers.length = 0; + this._pipelineUBO.destroy(); + this._pipelineSceneData.destroy(); return super.destroy(); } diff --git a/cocos/core/pipeline/render-shadow-map-batched-queue.ts b/cocos/core/pipeline/render-shadow-map-batched-queue.ts index 4a4d4c2632..2a4ba3ab58 100644 --- a/cocos/core/pipeline/render-shadow-map-batched-queue.ts +++ b/cocos/core/pipeline/render-shadow-map-batched-queue.ts @@ -29,32 +29,22 @@ */ import { SubModel } from '../renderer/scene/submodel'; -import { IRenderObject, SetIndex, UBOShadow } from './define'; -import { Device, RenderPass, Buffer, Shader, CommandBuffer, DescriptorSet } from '../gfx'; +import { SetIndex, UBOShadow } from './define'; +import { Device, RenderPass, Buffer, Shader, CommandBuffer } from '../gfx'; import { getPhaseID } from './pass-phase'; import { PipelineStateManager } from './pipeline-state-manager'; import { ShaderPool, SubModelPool, SubModelView, ShaderHandle } from '../renderer/core/memory-pools'; import { Pass, BatchingSchemes } from '../renderer/core/pass'; import { RenderInstancedQueue } from './render-instanced-queue'; - import { InstancedBuffer } from './instanced-buffer'; import { RenderBatchedQueue } from './render-batched-queue'; import { BatchedBuffer } from './batched-buffer'; -import { Color, Mat4, Vec4, Vec3 } from '../math'; -import { Shadows, ShadowType } from '../renderer/scene/shadows'; +import { ShadowType } from '../renderer/scene/shadows'; import { ForwardPipeline } from './forward/forward-pipeline'; import { Light, LightType } from '../renderer/scene/light'; import { SpotLight } from '../renderer/scene/spot-light'; import { intersect } from '../geometry'; import { Model } from '../renderer/scene/model'; -import { DirectionalLight } from '../renderer/scene/directional-light'; -import { getShadowWorldMatrix } from './forward/scene-culling'; - -const _matShadowView = new Mat4(); -const _matShadowViewProj = new Mat4(); -const _vec4ShadowInfo = new Vec4(); -const _vec3Center = new Vec3(); -let _shadowCameraView = new Mat4(); const _phaseID = getPhaseID('shadow-caster'); const _shadowPassIndices: number[] = []; @@ -85,37 +75,24 @@ export class RenderShadowMapBatchedQueue { private _subModelsArray: SubModel[] = []; private _passArray: Pass[] = []; private _shaderArray: Shader[] = []; - private _shadowMapBuffer: Buffer; - - // changes - private _device: Device; - private _shadowInfo: Shadows; - private _descriptorSet: DescriptorSet; - private _shadowObjects: IRenderObject[]; - private _shadowUBO: Float32Array; private _instancedQueue: RenderInstancedQueue; private _batchedQueue: RenderBatchedQueue; public constructor (pipeline: ForwardPipeline) { this._pipeline = pipeline; - this._device = pipeline.device; - this._shadowInfo = pipeline.shadows; - this._descriptorSet = pipeline.descriptorSet; - this._shadowObjects = pipeline.shadowObjects; - this._shadowUBO = pipeline.shadowUBO; - this._shadowMapBuffer = pipeline.descriptorSet.getBuffer(UBOShadow.BINDING); - this._instancedQueue = new RenderInstancedQueue(); this._batchedQueue = new RenderBatchedQueue(); } public gatherLightPasses (light: Light, cmdBuff: CommandBuffer) { this.clear(); - if (light && this._shadowInfo.enabled && this._shadowInfo.type === ShadowType.ShadowMap) { - this._updateUBOs(light); + const shadowInfo = this._pipeline.pipelineSceneData.shadows; + const shadowObjects = this._pipeline.pipelineSceneData.shadowObjects; + if (light && shadowInfo.enabled && shadowInfo.type === ShadowType.ShadowMap) { + this._pipeline.pipelineUBO.updateShadowUBOLight(light); - for (let i = 0; i < this._shadowObjects.length; i++) { - const ro = this._shadowObjects[i]; + for (let i = 0; i < shadowObjects.length; i++) { + const ro = shadowObjects[i]; const model = ro.model; if (!getShadowPassIndex(model.subModels, _shadowPassIndices)) { continue; } @@ -149,12 +126,13 @@ export class RenderShadowMapBatchedQueue { public add (model: Model, cmdBuff: CommandBuffer, _shadowPassIndices: number[]) { const subModels = model.subModels; + const shadowMapBuffer = this._pipeline.descriptorSet.getBuffer(UBOShadow.BINDING); for (let j = 0; j < subModels.length; j++) { const subModel = subModels[j]; const shadowPassIdx = _shadowPassIndices[j]; const pass = subModel.passes[shadowPassIdx]; const batchingScheme = pass.batchingScheme; - subModel.descriptorSet.bindBuffer(UBOShadow.BINDING, this._shadowMapBuffer); + subModel.descriptorSet.bindBuffer(UBOShadow.BINDING, shadowMapBuffer); subModel.descriptorSet.update(); if (batchingScheme === BatchingSchemes.INSTANCING) { // instancing @@ -200,62 +178,4 @@ export class RenderShadowMapBatchedQueue { cmdBuff.draw(ia); } } - - private _updateUBOs (light: Light) { - let _x = 0; - let _y = 0; - let _far = 0; - switch (light.type) { - case LightType.DIRECTIONAL: - // light view - (light as DirectionalLight).update(); - - // light proj - if (this._shadowInfo.autoAdapt) { - const node = (light as DirectionalLight).node; - if (node) { - _shadowCameraView = getShadowWorldMatrix(this._pipeline, node.getWorldRotation(), (light as DirectionalLight).direction, _vec3Center); - } - // if orthoSize is the smallest, auto calculate orthoSize. - const radius = this._shadowInfo.sphere.radius; - _x = radius * this._shadowInfo.aspect; - _y = radius; - - const halfFar = Vec3.distance(this._shadowInfo.sphere.center, _vec3Center); - _far = Math.min(halfFar * Shadows.COEFFICIENT_OF_EXPANSION, Shadows.MAX_FAR); - } else { - _shadowCameraView = (light as DirectionalLight).node!.getWorldMatrix(); - - _x = this._shadowInfo.orthoSize * this._shadowInfo.aspect; - _y = this._shadowInfo.orthoSize; - - _far = this._shadowInfo.far; - } - - Mat4.invert(_matShadowView, _shadowCameraView); - - Mat4.ortho(_matShadowViewProj, -_x, _x, -_y, _y, this._shadowInfo.near, _far, - this._device.clipSpaceMinZ, this._device.screenSpaceSignY * this._device.UVSpaceSignY); - break; - case LightType.SPOT: - // light view - Mat4.invert(_matShadowView, (light as SpotLight).node!.getWorldMatrix()); - - // light proj - Mat4.perspective(_matShadowViewProj, (light as SpotLight).spotAngle, (light as SpotLight).aspect, 0.001, (light as SpotLight).range); - break; - default: - } - // light viewProj - Mat4.multiply(_matShadowViewProj, _matShadowViewProj, _matShadowView); - - Mat4.toArray(this._shadowUBO, _matShadowViewProj, UBOShadow.MAT_LIGHT_VIEW_PROJ_OFFSET); - - Color.toArray(this._shadowUBO, this._shadowInfo.shadowColor, UBOShadow.SHADOW_COLOR_OFFSET); - - _vec4ShadowInfo.set(this._shadowInfo.size.x, this._shadowInfo.size.y, this._shadowInfo.pcf, this._shadowInfo.bias); - Vec4.toArray(this._shadowUBO, _vec4ShadowInfo, UBOShadow.SHADOW_INFO_OFFSET); - - this._descriptorSet.getBuffer(UBOShadow.BINDING).update(this._shadowUBO); - } } diff --git a/cocos/core/pipeline/forward/scene-culling.ts b/cocos/core/pipeline/scene-culling.ts similarity index 80% rename from cocos/core/pipeline/forward/scene-culling.ts rename to cocos/core/pipeline/scene-culling.ts index 8bfea39b0e..7281fd3b31 100644 --- a/cocos/core/pipeline/forward/scene-culling.ts +++ b/cocos/core/pipeline/scene-culling.ts @@ -28,16 +28,15 @@ * @hidden */ -import { AABB, intersect, Sphere } from '../../geometry'; -import { Model } from '../../renderer/scene/model'; -import { Camera, SKYBOX_FLAG } from '../../renderer/scene/camera'; -import { Layers } from '../../scene-graph/layers'; -import { Vec3, Mat4, Quat, Color } from '../../math'; -import { ForwardPipeline } from './forward-pipeline'; -import { Pool } from '../../memop'; -import { IRenderObject, UBOShadow } from '../define'; -import { ShadowType, Shadows } from '../../renderer/scene/shadows'; -import { SphereLight, DirectionalLight, Light } from '../../renderer/scene'; +import { AABB, intersect, Sphere } from '../geometry'; +import { Model } from '../renderer/scene/model'; +import { Camera, SKYBOX_FLAG } from '../renderer/scene/camera'; +import { Vec3, Mat4, Quat, Color } from '../math'; +import { RenderPipeline } from './render-pipeline'; +import { Pool } from '../memop'; +import { IRenderObject, UBOShadow } from './define'; +import { ShadowType, Shadows } from '../renderer/scene/shadows'; +import { SphereLight, DirectionalLight, Light } from '../renderer/scene'; const _tempVec3 = new Vec3(); const _dir_negate = new Vec3(); @@ -75,8 +74,8 @@ function getCastShadowRenderObject (model: Model, camera: Camera) { return ro; } -export function getShadowWorldMatrix (pipeline: ForwardPipeline, rotation: Quat, dir: Vec3, out: Vec3) { - const shadows = pipeline.shadows; +export function getShadowWorldMatrix (pipeline: RenderPipeline, rotation: Quat, dir: Vec3, out: Vec3) { + const shadows = pipeline.pipelineSceneData.shadows; Vec3.negate(_dir_negate, dir); const distance: number = shadows.sphere.radius * Shadows.COEFFICIENT_OF_EXPANSION; Vec3.multiplyScalar(_vec3_p, _dir_negate, distance); @@ -88,8 +87,8 @@ export function getShadowWorldMatrix (pipeline: ForwardPipeline, rotation: Quat, return _mat4_trans; } -function updateSphereLight (pipeline: ForwardPipeline, light: SphereLight) { - const shadows = pipeline.shadows; +function updateSphereLight (pipeline: RenderPipeline, light: SphereLight) { + const shadows = pipeline.pipelineSceneData.shadows; const pos = light.node!.worldPosition; const n = shadows.normal; const d = shadows.distance + 0.001; // avoid z-fighting @@ -114,11 +113,11 @@ function updateSphereLight (pipeline: ForwardPipeline, light: SphereLight) { m.m14 = lz * d; m.m15 = NdL; - Mat4.toArray(pipeline.shadowUBO, shadows.matLight, UBOShadow.MAT_LIGHT_PLANE_PROJ_OFFSET); + pipeline.pipelineUBO.updateShadowUBORange(UBOShadow.MAT_LIGHT_PLANE_PROJ_OFFSET, shadows.matLight); } -function updateDirLight (pipeline: ForwardPipeline, light: DirectionalLight) { - const shadows = pipeline.shadows; +function updateDirLight (pipeline: RenderPipeline, light: DirectionalLight) { + const shadows = pipeline.pipelineSceneData.shadows; const dir = light.direction; const n = shadows.normal; const d = shadows.distance + 0.001; // avoid z-fighting @@ -143,7 +142,7 @@ function updateDirLight (pipeline: ForwardPipeline, light: DirectionalLight) { m.m14 = lz * d; m.m15 = 1; - Mat4.toArray(pipeline.shadowUBO, shadows.matLight, UBOShadow.MAT_LIGHT_PLANE_PROJ_OFFSET); + pipeline.pipelineUBO.updateShadowUBORange(UBOShadow.MAT_LIGHT_PLANE_PROJ_OFFSET, shadows.matLight); } export function updatePlanarPROJ (shadowInfo: Shadows, light: DirectionalLight, shadowUBO: Float32Array) { @@ -192,10 +191,10 @@ export function lightCollecting (camera: Camera, lightNumber: number) { return _validLights; } -export function shadowCollecting (pipeline: ForwardPipeline, camera: Camera) { +export function shadowCollecting (pipeline: RenderPipeline, camera: Camera) { const scene = camera.scene!; - const shadows = pipeline.shadows; - const shadowObjects = pipeline.shadowObjects; + const shadows = pipeline.pipelineSceneData.shadows; + const shadowObjects = pipeline.pipelineSceneData.shadowObjects; shadowPool.freeArray(shadowObjects); shadowObjects.length = 0; _castBoundsInited = false; @@ -221,16 +220,18 @@ export function shadowCollecting (pipeline: ForwardPipeline, camera: Camera) { if (_castWorldBounds) { AABB.toBoundingSphere(shadows.sphere, _castWorldBounds); } } -export function sceneCulling (pipeline: ForwardPipeline, camera: Camera) { +export function sceneCulling (pipeline: RenderPipeline, camera: Camera) { const scene = camera.scene!; const mainLight = scene.mainLight; - const shadows = pipeline.shadows; + const sceneData = pipeline.pipelineSceneData; + const shadows = sceneData.shadows; + const skybox = sceneData.skybox; - const renderObjects = pipeline.renderObjects; + const renderObjects = sceneData.renderObjects; roPool.freeArray(renderObjects); renderObjects.length = 0; if (shadows.enabled) { - Color.toArray(pipeline.shadowUBO, shadows.shadowColor, UBOShadow.SHADOW_COLOR_OFFSET); + pipeline.pipelineUBO.updateShadowUBORange(UBOShadow.SHADOW_COLOR_OFFSET, shadows.shadowColor); } if (mainLight) { @@ -239,8 +240,8 @@ export function sceneCulling (pipeline: ForwardPipeline, camera: Camera) { } } - if (pipeline.skybox.enabled && pipeline.skybox.model && (camera.clearFlag & SKYBOX_FLAG)) { - renderObjects.push(getRenderObject(pipeline.skybox.model, camera)); + if (skybox.enabled && skybox.model && (camera.clearFlag & SKYBOX_FLAG)) { + renderObjects.push(getRenderObject(skybox.model, camera)); } const models = scene.models; diff --git a/cocos/core/pipeline/shadow/shadow-flow.ts b/cocos/core/pipeline/shadow/shadow-flow.ts index 013783eb3b..38093c258a 100644 --- a/cocos/core/pipeline/shadow/shadow-flow.ts +++ b/cocos/core/pipeline/shadow/shadow-flow.ts @@ -40,7 +40,7 @@ import { RenderFlowTag } from '../pipeline-serialization'; import { ForwardPipeline } from '../forward/forward-pipeline'; import { ShadowType } from '../../renderer/scene/shadows'; import { Light } from '../../renderer/scene/light'; -import { lightCollecting, shadowCollecting } from '../forward/scene-culling'; +import { lightCollecting, shadowCollecting } from '../scene-culling'; import { Vec2 } from '../../math'; import { Camera } from '../../renderer/scene'; @@ -76,21 +76,23 @@ export class ShadowFlow extends RenderFlow { public render (camera: Camera) { const pipeline = this._pipeline as ForwardPipeline; - const shadowInfo = pipeline.shadows; + const shadowInfo = pipeline.pipelineSceneData.shadows; + const shadowFrameBufferMap = pipeline.pipelineSceneData.shadowFrameBufferMap; + const shadowObjects = pipeline.pipelineSceneData.shadowObjects; if (!shadowInfo.enabled || shadowInfo.type !== ShadowType.ShadowMap) { return; } const validLights = lightCollecting(camera, shadowInfo.maxReceived); shadowCollecting(pipeline, camera); - if (pipeline.shadowObjects.length === 0) return; + if (shadowObjects.length === 0) return; for (let l = 0; l < validLights.length; l++) { const light = validLights[l]; - if (!pipeline.shadowFrameBufferMap.has(light)) { + if (!shadowFrameBufferMap.has(light)) { this._initShadowFrameBuffer(pipeline, light); } - const shadowFrameBuffer = pipeline.shadowFrameBufferMap.get(light); + const shadowFrameBuffer = shadowFrameBufferMap.get(light); if (shadowInfo.shadowMapDirty) { this.resizeShadowMap(light, shadowInfo.size); } for (let i = 0; i < this._stages.length; i++) { @@ -102,12 +104,13 @@ export class ShadowFlow extends RenderFlow { // After the shadowMap rendering of all lights is completed, // restore the ShadowUBO data of the main light. - pipeline.updateShadowUBO(camera); + pipeline.pipelineUBO.updateShadowUBO(camera); } public destroy () { super.destroy(); - const shadowFrameBuffers = Array.from((this._pipeline as ForwardPipeline).shadowFrameBufferMap.values()); + const shadowFrameBufferMap = this._pipeline.pipelineSceneData.shadowFrameBufferMap; + const shadowFrameBuffers = Array.from(shadowFrameBufferMap.values()); for (let i = 0; i < shadowFrameBuffers.length; i++) { const frameBuffer = shadowFrameBuffers[i]; @@ -125,14 +128,16 @@ export class ShadowFlow extends RenderFlow { frameBuffer.destroy(); } - (this._pipeline as ForwardPipeline).shadowFrameBufferMap.clear(); + shadowFrameBufferMap.clear(); if (this._shadowRenderPass) { this._shadowRenderPass.destroy(); } } public _initShadowFrameBuffer (pipeline: ForwardPipeline, light: Light) { const device = pipeline.device; - const shadowMapSize = pipeline.shadows.size; + const shadows = pipeline.pipelineSceneData.shadows; + const shadowMapSize = shadows.size; + const shadowFrameBufferMap = pipeline.pipelineSceneData.shadowFrameBufferMap; if (!this._shadowRenderPass) { const colorAttachment = new ColorAttachment(); @@ -181,16 +186,17 @@ export class ShadowFlow extends RenderFlow { )); // Cache frameBuffer - pipeline.shadowFrameBufferMap.set(light, shadowFrameBuffer); + shadowFrameBufferMap.set(light, shadowFrameBuffer); } private resizeShadowMap (light: Light, size: Vec2) { const width = size.x; const height = size.y; - const pipeline = this._pipeline as ForwardPipeline; + const pipeline = this._pipeline; + const shadowFrameBufferMap = pipeline.pipelineSceneData.shadowFrameBufferMap; - if (pipeline.shadowFrameBufferMap.has(light)) { - const frameBuffer = pipeline.shadowFrameBufferMap.get(light); + if (shadowFrameBufferMap.has(light)) { + const frameBuffer = shadowFrameBufferMap.get(light); if (!frameBuffer) { return; } diff --git a/cocos/core/pipeline/shadow/shadow-stage.ts b/cocos/core/pipeline/shadow/shadow-stage.ts index 9e9584020a..842f12d995 100644 --- a/cocos/core/pipeline/shadow/shadow-stage.ts +++ b/cocos/core/pipeline/shadow/shadow-stage.ts @@ -78,8 +78,10 @@ export class ShadowStage extends RenderStage { } public render (camera: Camera) { - const pipeline = this._pipeline as ForwardPipeline; - const shadowInfo = pipeline.shadows; + const pipeline = this._pipeline; + const pipelineSceneData = pipeline.pipelineSceneData; + const shadowInfo = pipelineSceneData.shadows; + const shadingScale = pipelineSceneData.shadingScale; const cmdBuff = pipeline.commandBuffers[0]; @@ -90,8 +92,8 @@ export class ShadowStage extends RenderStage { const shadowMapSize = shadowInfo.size; this._renderArea.x = vp.x * shadowMapSize.x; this._renderArea.y = vp.y * shadowMapSize.y; - this._renderArea.width = vp.width * shadowMapSize.x * pipeline.shadingScale; - this._renderArea.height = vp.height * shadowMapSize.y * pipeline.shadingScale; + this._renderArea.width = vp.width * shadowMapSize.x * shadingScale; + this._renderArea.height = vp.height * shadowMapSize.y * shadingScale; const device = pipeline.device; const renderPass = this._shadowFrameBuffer.renderPass; diff --git a/cocos/core/renderer/core/memory-pools.ts b/cocos/core/renderer/core/memory-pools.ts index d5cbab0055..1834e8b1fb 100644 --- a/cocos/core/renderer/core/memory-pools.ts +++ b/cocos/core/renderer/core/memory-pools.ts @@ -541,6 +541,7 @@ export enum PoolType { BLEND_TARGET, BLEND_STATE, BATCH_2D, + PIPELINE_SCENE_DATA, // arrays SUB_MODEL_ARRAY = 200, MODEL_ARRAY, @@ -595,6 +596,7 @@ export type BlendTargetHandle = IHandle; export type BlendStateHandle = IHandle; export type BatchHandle2D = IHandle; export type UIBatchArrayHandle = IHandle; +export type PipelineSceneDataHandle = IHandle; // TODO: could use Labeled Tuple Element feature here after next babel update (required TS4.0+ support) export const ShaderPool = new ObjectPool(PoolType.SHADER, @@ -1232,6 +1234,43 @@ export const ShadowsPool = new BufferPool { + [PipelineSceneDataView.SHADOW]: ShadowsHandle; + [PipelineSceneDataView.SKYBOX]: SkyboxHandle; + [PipelineSceneDataView.AMBIENT]: AmbientHandle; + [PipelineSceneDataView.FOG]: FogHandle; + [PipelineSceneDataView.IS_HDR]: number; + [PipelineSceneDataView.SHADING_SCALE]: number; + [PipelineSceneDataView.FP_SCALE]: number; + [PipelineSceneDataView.COUNT]: never; +} +const pipelineSceneDataType: BufferDataTypeManifest = { + [PipelineSceneDataView.SHADOW]: BufferDataType.UINT32, + [PipelineSceneDataView.SKYBOX]: BufferDataType.UINT32, + [PipelineSceneDataView.AMBIENT]: BufferDataType.UINT32, + [PipelineSceneDataView.FOG]: BufferDataType.UINT32, + [PipelineSceneDataView.IS_HDR]: BufferDataType.UINT32, + [PipelineSceneDataView.SHADING_SCALE]: BufferDataType.UINT32, + [PipelineSceneDataView.FP_SCALE]: BufferDataType.UINT32, + [PipelineSceneDataView.COUNT]: BufferDataType.NEVER, +}; +// Theoretically we only have to declare the type view here while all the other arguments can be inferred. +// but before the official support of Partial Type Argument Inference releases, (microsoft/TypeScript#26349) +// we'll have to explicitly declare all these types. +export const PipelineSceneDataPool = new BufferPool( + PoolType.PIPELINE_SCENE_DATA, pipelineSceneDataType, PipelineSceneDataView, 1, +); + export enum LightView { USE_COLOR_TEMPERATURE, ILLUMINANCE, diff --git a/cocos/core/renderer/scene/skybox.ts b/cocos/core/renderer/scene/skybox.ts index d5ac7a0d1e..94a0be13ac 100644 --- a/cocos/core/renderer/scene/skybox.ts +++ b/cocos/core/renderer/scene/skybox.ts @@ -131,12 +131,13 @@ export class Skybox { public activate () { const pipeline = legacyCC.director.root.pipeline; + const ambient = pipeline.pipelineSceneData.ambient; this._globalDescriptorSet = pipeline.descriptorSet; this._default = builtinResMgr.get('default-cube-texture'); if (!this._model) { this._model = legacyCC.director.root.createModel(legacyCC.renderer.scene.Model) as Model; - // @ts-ignore skybox don't need local buffers + // @ts-expect-error private member access this._model._initLocalDescriptors = () => {}; } @@ -144,7 +145,7 @@ export class Skybox { if (!this._envmap) { this._envmap = this._default; } - pipeline.ambient.groundAlbedo[3] = this._envmap.mipmapLevel; + ambient.groundAlbedo[3] = this._envmap.mipmapLevel; if (!skybox_material) { const mat = new Material(); diff --git a/cocos/core/root.ts b/cocos/core/root.ts index ea25fdde24..0b93b7ba1e 100644 --- a/cocos/core/root.ts +++ b/cocos/core/root.ts @@ -30,7 +30,7 @@ import { builtinResMgr } from './builtin'; import { Pool } from './memop'; -import { RenderPipeline, ForwardPipeline } from './pipeline'; +import { RenderPipeline, createDefaultPipeline } from './pipeline'; import { Camera, Light, Model } from './renderer/scene'; import { DataPoolManager } from '../3d/skeletal-animation/data-pool-manager'; import { LightType } from './renderer/scene/light'; @@ -307,7 +307,7 @@ export class Root { public setRenderPipeline (rppl: RenderPipeline): boolean { if (!rppl) { - rppl = this.createDefaultPipeline(); + rppl = createDefaultPipeline(); } this._pipeline = rppl; if (!this._pipeline.activate()) { @@ -331,12 +331,6 @@ export class Root { return true; } - public createDefaultPipeline () { - const rppl = new ForwardPipeline(); - rppl.initialize({ flows: [] }); - return rppl; - } - public onGlobalPipelineStateChanged () { for (let i = 0; i < this._scenes.length; i++) { this._scenes[i].onGlobalPipelineStateChanged(); diff --git a/cocos/core/scene-graph/scene-globals.ts b/cocos/core/scene-graph/scene-globals.ts index 714f254f58..6652a7b355 100644 --- a/cocos/core/scene-graph/scene-globals.ts +++ b/cocos/core/scene-graph/scene-globals.ts @@ -666,11 +666,11 @@ export class SceneGlobals { } public activate () { - const pipeline = legacyCC.director.root.pipeline; - this.ambient.activate(pipeline.ambient); - this.skybox.activate(pipeline.skybox); - this.shadows.activate(pipeline.shadows); - this.fog.activate(pipeline.fog); + const sceneData = legacyCC.director.root.pipeline.pipelineSceneData; + this.ambient.activate(sceneData.ambient); + this.skybox.activate(sceneData.skybox); + this.shadows.activate(sceneData.shadows); + this.fog.activate(sceneData.fog); } } legacyCC.SceneGlobals = SceneGlobals; diff --git a/platforms/native/engine/index.js b/platforms/native/engine/index.js index 7753e5af25..a769e58adf 100644 --- a/platforms/native/engine/index.js +++ b/platforms/native/engine/index.js @@ -33,7 +33,6 @@ require('./jsb-videoplayer.js'); require('./jsb-webview.js'); require('./jsb-audio.js'); require('./jsb-editbox.js'); -require('./jsb-pipeline.js'); require('./jsb-safearea.js'); require('./jsb-editor-support.js') require('./jsb-spine-skeleton.js') diff --git a/platforms/native/engine/jsb-pipeline.js b/platforms/native/engine/jsb-pipeline.js deleted file mode 100644 index c772cc2f96..0000000000 --- a/platforms/native/engine/jsb-pipeline.js +++ /dev/null @@ -1,174 +0,0 @@ -/**************************************************************************** - Copyright (c) 2019 Xiamen Yaji Software Co., Ltd. - - https://www.cocos.com/ - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated engine source code (the "Software"), a limited, - worldwide, royalty-free, non-assignable, revocable and non-exclusive license - to use Cocos Creator solely to develop games on your target platforms. You - shall not use Cocos Creator software for developing other software or tools - that's used for developing games. You are not granted to publish, distribute, - sublicense, and/or sell copies of Cocos Creator. - - The software or tools in this License Agreement are licensed, not sold. - Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to - you. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - ****************************************************************************/ - -(function() { -if (cc.ForwardPipeline) return; - -class ForwardPipeline extends nr.ForwardPipeline { - constructor() { - super(); - this._tag = 0; - this._flows = []; - this.renderTextures = []; - this.materials = []; - } - - destroy () { - this.fog.destroy(); - this.ambient.destroy(); - this.skybox.destroy(); - this.shadows.destroy(); - } - - init() { - this.fog = new cc.Fog(); - this.ambient = new cc.Ambient(); - this.skybox = new cc.Skybox(); - this.shadows = new cc.Shadows(); - this.setFog(this.fog.handle); - this.setAmbient(this.ambient.handle); - this.setSkybox(this.skybox.handle); - this.setShadows(this.shadows.handle); - for (let i = 0; i < this._flows.length; i++) { - this._flows[i].init(); - } - let info = new nr.RenderPipelineInfo(this._tag, this._flows); - this.initialize(info); - } -} -// hook to invoke init after deserialization -ForwardPipeline.prototype.onAfterDeserialize_JSB = ForwardPipeline.prototype.init; - -class ForwardFlow extends nr.ForwardFlow { - constructor() { - super(); - this._name = 0; - this._priority = 0; - this._tag = 0; - this._stages = []; - } - init() { - for (let i = 0; i < this._stages.length; i++) { - this._stages[i].init(); - } - let info = new nr.RenderFlowInfo( - this._name, this._priority, this._tag, this._stages); - this.initialize(info); - } -} - -class ShadowFlow extends nr.ShadowFlow { - constructor() { - super(); - this._name = 0; - this._priority = 0; - this._tag = 0; - this._stages = []; - } - init() { - for (let i = 0; i < this._stages.length; i++) { - this._stages[i].init(); - } - let info = new nr.RenderFlowInfo( - this._name, this._priority, this._tag, this._stages); - this.initialize(info); - } -} - -class ForwardStage extends nr.ForwardStage { - constructor() { - super(); - this._name = 0; - this._priority = 0; - this._tag = 0; - this.renderQueues = []; - } - init() { - const queues = []; - for (let i = 0; i < this.renderQueues.length; i++) { - queues.push(this.renderQueues[i].init()); - } - let info = - new nr.RenderStageInfo(this._name, this._priority, this._tag, queues); - this.initialize(info); - } -} - -class ShadowStage extends nr.ShadowStage { - constructor() { - super(); - this._name = 0; - this._priority = 0; - this._tag = 0; - } - init() { - let info = - new nr.RenderStageInfo(this._name, this._priority, this._tag, []); - this.initialize(info); - } -} - -let instancedBufferProto = nr.InstancedBuffer; -let oldGetFunc = instancedBufferProto.get; -instancedBufferProto.get = function(pass) { - return oldGetFunc.call(this, pass.handle); -}; - -class RenderQueueDesc { - constructor() { - this.isTransparent = false; - this.sortMode = 0; - this.stages = []; - } - - init() { - let desc = - new nr.RenderQueueDesc(this.isTransparent, this.sortMode, this.stages); - return desc; - } -} -cc.js.setClassName('ForwardPipeline', ForwardPipeline); -cc.js.setClassName('ForwardFlow', ForwardFlow); -cc.js.setClassName('ShadowFlow', ShadowFlow); -cc.js.setClassName('ForwardStage', ForwardStage); -cc.js.setClassName('ShadowStage', ShadowStage); -cc.js.setClassName('RenderQueueDesc', RenderQueueDesc); - -let getOrCreatePipelineState = nr.PipelineStateManager.getOrCreatePipelineState; -nr.PipelineStateManager.getOrCreatePipelineState = function(device, pass, shader, renderPass, ia) { - return getOrCreatePipelineState.call(this, pass.handle, shader, renderPass, ia); -} - -const RootProto = cc.Root.prototype; -Object.assign(RootProto, { - createDefaultPipeline() { - const pipeline = new nr.ForwardPipeline(); - const info = new nr.RenderPipelineInfo(0, []); - pipeline.initialize(info); - return pipeline; - }, -}) -})();