diff --git a/cocos/core/geometry/frustum.ts b/cocos/core/geometry/frustum.ts index f78450ba96..c4cc99f4a5 100644 --- a/cocos/core/geometry/frustum.ts +++ b/cocos/core/geometry/frustum.ts @@ -4,6 +4,7 @@ */ import { Mat4, Vec3 } from '../math'; +import { FrustumHandle, FrustumPool, FrustumView, NULL_HANDLE } from '../renderer/core/memory-pools'; import enums from './enums'; import plane from './plane'; @@ -215,3 +216,30 @@ export class frustum { plane.fromPoints(this.planes[0], this.vertices[7], this.vertices[6], this.vertices[5]); } } + +/** + * @en + * Record frustum to shared memory. + * @zh + * 记录 frustum 数据到共享内存。并不是每个 frustum 都是需要记录到共享内存的。 + * @param handle The frustum handle + * @param frstm The frustum object + */ +export function recordFrustumToSharedMemory (handle: FrustumHandle, frstm: frustum) { + if (!frstm || handle === NULL_HANDLE) { + return; + } + + const vertices = frstm.vertices; + let vertexOffset = FrustumView.VERTICES as const; + for (let i = 0; i < 8; ++i) { + FrustumPool.setVec3(handle, vertexOffset, vertices[i]); + vertexOffset += 3; + } + + const planes = frstm.planes; + let planeOffset = FrustumView.PLANES as const; + for (let i = 0; i < 6; i++, planeOffset += 4) { + FrustumPool.setVec4(handle, planeOffset, planes[i]); + } +} diff --git a/cocos/core/renderer/core/memory-pools.ts b/cocos/core/renderer/core/memory-pools.ts index c2efa2a87f..6e9d080cee 100644 --- a/cocos/core/renderer/core/memory-pools.ts +++ b/cocos/core/renderer/core/memory-pools.ts @@ -543,6 +543,7 @@ enum PoolType { ATTRIBUTE_ARRAY, FLAT_BUFFER_ARRAY, INSTANCED_BUFFER_ARRAY, + LIGHT_ARRAY, // raw buffer RAW_BUFFER = 300, } @@ -581,6 +582,7 @@ export type SphereHandle = IHandle; export type SubMeshHandle = IHandle; export type FlatBufferHandle = IHandle; export type FlatBufferArrayHandle = IHandle; +export type LightArrayHandle = IHandle; // don't reuse any of these data-only structs, for GFX objects may directly reference them export const RasterizerStatePool = new ObjectPool(PoolType.RASTERIZER_STATE, (_: never[]) => new GFXRasterizerState()); @@ -611,14 +613,11 @@ export const FramebufferPool = new ObjectPool(PoolType.FRAMEBUFFER, (obj: GFXFramebuffer) => obj && obj.destroy(), ); -export const SubModelArrayPool = new TypedArrayPool - (PoolType.SUB_MODEL_ARRAY, Uint32Array, 8, 4); -export const ModelArrayPool = new TypedArrayPool - (PoolType.MODEL_ARRAY, Uint32Array, 32, 16); -export const AttributeArrayPool = new TypedArrayPool - (PoolType.ATTRIBUTE_ARRAY, Uint32Array, 8, 4); -export const FlatBufferArrayPool = new TypedArrayPool - (PoolType.FLAT_BUFFER_ARRAY, Uint32Array, 8, 4); +export const SubModelArrayPool = new TypedArrayPool(PoolType.SUB_MODEL_ARRAY, Uint32Array, 8, 4); +export const ModelArrayPool = new TypedArrayPool(PoolType.MODEL_ARRAY, Uint32Array, 32, 16); +export const AttributeArrayPool = new TypedArrayPool(PoolType.ATTRIBUTE_ARRAY, Uint32Array, 8, 4); +export const FlatBufferArrayPool = new TypedArrayPool(PoolType.FLAT_BUFFER_ARRAY, Uint32Array, 8, 4); +export const LightArrayPool = new TypedArrayPool(PoolType.LIGHT_ARRAY, Uint32Array, 8, 4); export const RawBufferPool = new BufferAllocator(PoolType.RAW_BUFFER); @@ -788,18 +787,24 @@ const aabbViewDataType: BufferDataTypeManifest = { export const AABBPool = new BufferPool(PoolType.AABB, aabbViewDataType, AABBView); export enum SceneView { - MAIN_LIGHT, // TODO + MAIN_LIGHT, // handle MODEL_ARRAY, // array handle + SPHERE_LIGHT_ARRAY, // array handle + SPOT_LIGHT_ARRAY, // array handle COUNT, } interface ISceneViewType extends BufferTypeManifest { [SceneView.MAIN_LIGHT]: LightHandle; [SceneView.MODEL_ARRAY]: ModelArrayHandle; + [SceneView.SPHERE_LIGHT_ARRAY]: LightArrayHandle; + [SceneView.SPOT_LIGHT_ARRAY]: LightArrayHandle; [SceneView.COUNT]: never; } const sceneViewDataType: BufferDataTypeManifest = { [SceneView.MAIN_LIGHT]: BufferDataType.UINT32, [SceneView.MODEL_ARRAY]: BufferDataType.UINT32, + [SceneView.SPHERE_LIGHT_ARRAY]: BufferDataType.UINT32, + [SceneView.SPOT_LIGHT_ARRAY]: BufferDataType.UINT32, [SceneView.COUNT]: BufferDataType.NEVER } // Theoretically we only have to declare the type view here while all the other arguments can be inferred. @@ -1141,27 +1146,48 @@ export enum LightView { USE_COLOR_TEMPERATURE, ILLUMINANCE, NODE, // handle + RANGE, + TYPE, + AABB, // handle + FRUSTUM, // handle + SIZE, + SPOT_ANGLE, DIRECTION, // Vec3 - COLOR = 6, // Vec3 - COLOR_TEMPERATURE_RGB = 9, // Vec3 - COUNT = 12 + COLOR = 12, // Vec3 + COLOR_TEMPERATURE_RGB = 15, // Vec3 + POSITION = 18, // Vec3 + COUNT = 21 } interface ILightViewType extends BufferTypeManifest { [LightView.USE_COLOR_TEMPERATURE]: number; [LightView.ILLUMINANCE]: number; [LightView.NODE]:NodeHandle; + [LightView.RANGE]:number; + [LightView.TYPE]:number; + [LightView.AABB]:AABBHandle; + [LightView.FRUSTUM]:FrustumHandle; + [LightView.SIZE]:number; + [LightView.SPOT_ANGLE]:number; [LightView.DIRECTION]: Vec3; [LightView.COLOR]: Vec3; [LightView.COLOR_TEMPERATURE_RGB]: Vec3; + [LightView.POSITION]: Vec3; [LightView.COUNT]: never; } const lightViewDataType: BufferDataTypeManifest = { [LightView.USE_COLOR_TEMPERATURE]: BufferDataType.UINT32, [LightView.ILLUMINANCE]: BufferDataType.FLOAT32, [LightView.NODE]: BufferDataType.UINT32, + [LightView.RANGE]: BufferDataType.FLOAT32, + [LightView.TYPE]: BufferDataType.UINT32, + [LightView.AABB]: BufferDataType.UINT32, + [LightView.FRUSTUM]: BufferDataType.UINT32, + [LightView.SIZE]: BufferDataType.FLOAT32, + [LightView.SPOT_ANGLE]: BufferDataType.FLOAT32, [LightView.DIRECTION]: BufferDataType.FLOAT32, [LightView.COLOR]: BufferDataType.FLOAT32, [LightView.COLOR_TEMPERATURE_RGB]: BufferDataType.FLOAT32, + [LightView.POSITION]: BufferDataType.FLOAT32, [LightView.COUNT]: BufferDataType.NEVER } // Theoretically we only have to declare the type view here while all the other arguments can be inferred. diff --git a/cocos/core/renderer/scene/camera.ts b/cocos/core/renderer/scene/camera.ts index 7465cb395f..67aadaaff1 100644 --- a/cocos/core/renderer/scene/camera.ts +++ b/cocos/core/renderer/scene/camera.ts @@ -10,6 +10,7 @@ import { legacyCC } from '../../global-exports'; import { RenderWindow } from '../core/render-window'; import { CameraHandle, CameraPool, CameraView, FrustumHandle, FrustumPool, FrustumView, NULL_HANDLE, SceneHandle } from '../core/memory-pools'; import { JSB } from 'internal:constants'; +import { recordFrustumToSharedMemory } from '../../geometry/frustum'; export enum CameraFOVAxis { VERTICAL, @@ -284,7 +285,7 @@ export class Camera { this._frustum.update(this._matViewProj, this._matViewProjInv); CameraPool.setMat4(this._poolHandle, CameraView.MAT_VIEW_PROJ, this._matViewProj); CameraPool.setMat4(this._poolHandle, CameraView.MAT_VIEW_PROJ_INV, this._matViewProjInv); - this.recordFrustumInSharedMemory(); + recordFrustumToSharedMemory(this._frustumHandle, this._frustum); } this._isProjDirty = false; @@ -520,7 +521,7 @@ export class Camera { set frustum (val) { this._frustum = val; - this.recordFrustumInSharedMemory(); + recordFrustumToSharedMemory(this._frustumHandle, val); } get frustum () { @@ -786,25 +787,4 @@ export class Camera { const ev100 = Math.log2((this._apertureValue * this._apertureValue) / this._shutterValue * 100.0 / this._isoValue); CameraPool.set(this._poolHandle, CameraView.EXPOSURE, 0.833333 / Math.pow(2.0, ev100)); } - - private recordFrustumInSharedMemory () { - const frustumHandle = this._frustumHandle; - const frstm = this._frustum; - if (!frstm || frustumHandle === NULL_HANDLE) { - return; - } - - const vertices = frstm.vertices; - let vertexOffset = FrustumView.VERTICES as const; - for (let i = 0; i < 8; ++i) { - FrustumPool.setVec3(frustumHandle, vertexOffset, vertices[i]); - vertexOffset += 3; - } - - const planes = frstm.planes; - let planeOffset = FrustumView.PLANES as const; - for (let i = 0; i < 6; i++, planeOffset += 4) { - FrustumPool.setVec4(frustumHandle, planeOffset, planes[i]); - } - } } diff --git a/cocos/core/renderer/scene/render-scene.ts b/cocos/core/renderer/scene/render-scene.ts index 2c181b606f..4f30b5d32e 100644 --- a/cocos/core/renderer/scene/render-scene.ts +++ b/cocos/core/renderer/scene/render-scene.ts @@ -14,7 +14,7 @@ import { SpotLight } from './spot-light'; import { PREVIEW } from 'internal:constants'; import { TransformBit } from '../../scene-graph/node-enum'; import { legacyCC } from '../../global-exports'; -import { ScenePool, SceneView, ModelArrayPool, ModelArrayHandle, SceneHandle, NULL_HANDLE, freeHandleArray, ModelPool } from '../core/memory-pools'; +import { ScenePool, SceneView, ModelArrayPool, ModelArrayHandle, SceneHandle, NULL_HANDLE, freeHandleArray, ModelPool,LightArrayHandle, LightArrayPool } from '../core/memory-pools'; export interface IRenderSceneInfo { name: string; @@ -112,6 +112,8 @@ export class RenderScene { private _modelId: number = 0; private _scenePoolHandle: SceneHandle = NULL_HANDLE; private _modelArrayHandle: ModelArrayHandle = NULL_HANDLE; + private _sphereLightsHandle: LightArrayHandle = NULL_HANDLE; + private _spotLightsHandle: LightArrayHandle = NULL_HANDLE; constructor (root: Root) { this._root = root; @@ -166,6 +168,14 @@ export class RenderScene { ScenePool.free(this._scenePoolHandle); this._scenePoolHandle = NULL_HANDLE; } + if (this._sphereLightsHandle) { + LightArrayPool.free(this._sphereLightsHandle); + this._sphereLightsHandle = NULL_HANDLE; + } + if (this._spotLightsHandle) { + LightArrayPool.free(this._spotLightsHandle); + this._spotLightsHandle = NULL_HANDLE; + } } public addCamera (cam: Camera) { @@ -227,6 +237,7 @@ export class RenderScene { public addSphereLight (pl: SphereLight) { pl.attachToScene(this); this._sphereLights.push(pl); + LightArrayPool.push(this._sphereLightsHandle, pl.handle); } public removeSphereLight (pl: SphereLight) { @@ -234,6 +245,7 @@ export class RenderScene { if (this._sphereLights[i] === pl) { pl.detachFromScene(); this._sphereLights.splice(i, 1); + LightArrayPool.erase(this._sphereLightsHandle, i) return; } } @@ -242,6 +254,7 @@ export class RenderScene { public addSpotLight (sl: SpotLight) { sl.attachToScene(this); this._spotLights.push(sl); + LightArrayPool.push(this._spotLightsHandle, sl.handle); } public removeSpotLight (sl: SpotLight) { @@ -249,6 +262,7 @@ export class RenderScene { if (this._spotLights[i] === sl) { sl.detachFromScene(); this._spotLights.splice(i, 1); + LightArrayPool.erase(this._spotLightsHandle, i); return; } } @@ -259,6 +273,7 @@ export class RenderScene { this._sphereLights[i].detachFromScene(); } this._sphereLights.length = 0; + LightArrayPool.clear(this._sphereLightsHandle); } public removeSpotLights () { @@ -266,6 +281,7 @@ export class RenderScene { this._spotLights[i].detachFromScene(); } this._spotLights = []; + LightArrayPool.clear(this._spotLightsHandle); } public addModel (m: Model) { @@ -501,6 +517,12 @@ export class RenderScene { this._modelArrayHandle = ModelArrayPool.alloc(); this._scenePoolHandle = ScenePool.alloc(); ScenePool.set(this._scenePoolHandle, SceneView.MODEL_ARRAY, this._modelArrayHandle); + + this._spotLightsHandle = LightArrayPool.alloc(); + ScenePool.set(this._scenePoolHandle, SceneView.SPOT_LIGHT_ARRAY, this._spotLightsHandle); + + this._sphereLightsHandle = LightArrayPool.alloc(); + ScenePool.set(this._scenePoolHandle, SceneView.SPHERE_LIGHT_ARRAY, this._sphereLightsHandle); } } } diff --git a/cocos/core/renderer/scene/sphere-light.ts b/cocos/core/renderer/scene/sphere-light.ts index 98e80914f6..37cc9b9132 100644 --- a/cocos/core/renderer/scene/sphere-light.ts +++ b/cocos/core/renderer/scene/sphere-light.ts @@ -1,7 +1,7 @@ import { aabb } from '../../geometry'; import { Vec3 } from '../../math'; import { Light, LightType, nt2lm } from './light'; -import { LightPool, LightView } from '../core/memory-pools'; +import { AABBHandle, AABBPool, AABBView, LightPool, LightView, NULL_HANDLE } from '../core/memory-pools'; export class SphereLight extends Light { @@ -12,20 +12,20 @@ export class SphereLight extends Light { } set size (size: number) { - this._size = size; + LightPool.set(this._handle, LightView.SIZE, size); } get size (): number { - return this._size; + return LightPool.get(this._handle, LightView.SIZE); } set range (range: number) { - this._range = range; + LightPool.set(this._handle, LightView.RANGE, range); this._needUpdate = true; } get range (): number { - return this._range; + return LightPool.get(this._handle, LightView.RANGE); } set luminance (lum: number) { @@ -40,10 +40,9 @@ export class SphereLight extends Light { return this._aabb; } - protected _size: number = 0.15; - protected _range: number = 1.0; protected _pos: Vec3; protected _aabb: aabb; + protected _hAABB: AABBHandle = NULL_HANDLE; constructor () { super(); @@ -54,14 +53,32 @@ export class SphereLight extends Light { public initialize () { super.initialize(); - LightPool.set(this._handle, LightView.ILLUMINANCE, 1700 / nt2lm(this._size)); + this._hAABB = AABBPool.alloc(); + const size = 0.15; + LightPool.set(this._handle, LightView.SIZE, size); + LightPool.set(this._handle, LightView.RANGE, 1.0); + LightPool.set(this._handle, LightView.AABB, this._hAABB); + LightPool.set(this._handle, LightView.ILLUMINANCE, 1700 / nt2lm(size)); } public update () { if (this._node && (this._node.hasChangedFlags || this._needUpdate)) { this._node.getWorldPosition(this._pos); - aabb.set(this._aabb, this._pos.x, this._pos.y, this._pos.z, this._range, this._range, this._range); + const range = LightPool.get(this._handle, LightView.RANGE); + aabb.set(this._aabb, this._pos.x, this._pos.y, this._pos.z, range, range, range); this._needUpdate = false; + + LightPool.setVec3(this._handle, LightView.POSITION, this._pos); + AABBPool.setVec3(this._hAABB, AABBView.CENTER, this._aabb.center); + AABBPool.setVec3(this._hAABB, AABBView.HALF_EXTENSION, this._aabb.halfExtents); } } + + public destroy () { + if (this._hAABB) { + AABBPool.free(this._hAABB); + this._hAABB = NULL_HANDLE; + } + return super.destroy(); + } } diff --git a/cocos/core/renderer/scene/spot-light.ts b/cocos/core/renderer/scene/spot-light.ts index aefd55a022..921aa3a164 100644 --- a/cocos/core/renderer/scene/spot-light.ts +++ b/cocos/core/renderer/scene/spot-light.ts @@ -1,7 +1,8 @@ import { aabb, frustum } from '../../geometry'; import { Mat4, Quat, Vec3 } from '../../math'; import { Light, LightType, nt2lm } from './light'; -import { LightPool, LightView } from '../core/memory-pools'; +import { AABBHandle, AABBPool, AABBView, FrustumHandle, FrustumPool, LightPool, LightView, NULL_HANDLE } from '../core/memory-pools'; +import { recordFrustumToSharedMemory } from '../../geometry/frustum'; const _forward = new Vec3(0, 0, -1); const _qt = new Quat(); @@ -20,26 +21,28 @@ export class SpotLight extends Light { protected _frustum: frustum; protected _angle: number = 0; protected _needUpdate = false; + protected _hAABB: AABBHandle = NULL_HANDLE; + protected _hFrustum: FrustumHandle = NULL_HANDLE; get position () { return this._pos; } set size (size: number) { - this._size = size; + LightPool.set(this._handle, LightView.SIZE, size); } get size (): number { - return this._size; + return LightPool.get(this._handle, LightView.SIZE); } set range (range: number) { - this._range = range; + LightPool.set(this._handle, LightView.RANGE, range); this._needUpdate = true; } get range (): number { - return this._range; + return LightPool.get(this._handle, LightView.RANGE); } set luminance (lum: number) { @@ -55,12 +58,12 @@ export class SpotLight extends Light { } get spotAngle () { - return this._spotAngle; + return LightPool.get(this._handle, LightView.SPOT_ANGLE); } set spotAngle (val: number) { this._angle = val; - this._spotAngle = Math.cos(val * 0.5); + LightPool.set(this._handle, LightView.SPOT_ANGLE, Math.cos(val * 0.5)) this._needUpdate = true; } @@ -82,7 +85,15 @@ export class SpotLight extends Light { public initialize () { super.initialize(); - LightPool.set(this._handle, LightView.ILLUMINANCE, 1700 / nt2lm(this._size)); + this._hAABB = AABBPool.alloc(); + this._hFrustum = FrustumPool.alloc(); + const size = 0.15; + LightPool.set(this._handle, LightView.SIZE, size); + LightPool.set(this._handle, LightView.AABB, this._hAABB); + LightPool.set(this._handle, LightView.ILLUMINANCE, 1700 / nt2lm(size)); + LightPool.set(this._handle, LightView.RANGE, 5.0); + LightPool.set(this._handle, LightView.RANGE, Math.cos(Math.PI / 6)); + LightPool.setVec3(this._handle, LightView.DIRECTION, this._dir); } public update () { @@ -105,6 +116,23 @@ export class SpotLight extends Light { this._frustum.update(_matViewProj, _matViewProjInv); this._needUpdate = false; + + LightPool.setVec3(this._handle, LightView.DIRECTION, this._pos); + AABBPool.setVec3(this._hAABB, AABBView.CENTER, this._aabb.center); + AABBPool.setVec3(this._hAABB, AABBView.HALF_EXTENSION, this._aabb.halfExtents); + recordFrustumToSharedMemory(this._hFrustum, this._frustum); } } + + public destroy () { + if (this._hAABB) { + AABBPool.free(this._hAABB); + this._hAABB = NULL_HANDLE; + } + if (this._hFrustum) { + FrustumPool.free(this._hFrustum); + this._hFrustum = NULL_HANDLE; + } + return super.destroy(); + } }