Files
cocos4/cocos/rendering/custom/define.ts
T

197 lines
7.3 KiB
TypeScript

import { ClearFlagBit, Color, Format, LoadOp, Rect, StoreOp, Viewport } from '../../gfx/base/define';
import { Camera, CSMLevel, DirectionalLight, Light, LightType, ShadowType, SKYBOX_FLAG, SpotLight } from '../../render-scene/scene';
import { intersect, Sphere } from '../../core/geometry';
import { supportsR32FloatTexture } from '../define';
import { Pipeline } from './pipeline';
import { AccessType, AttachmentType, LightInfo, QueueHint, RasterView, ResourceResidency, SceneFlags } from './types';
// Anti-aliasing type, other types will be gradually added in the future
export enum AntiAliasing {
NONE,
FXAA,
}
export function validPunctualLightsCulling (pipeline: Pipeline, camera: Camera) {
const sceneData = pipeline.pipelineSceneData;
const validPunctualLights = sceneData.validPunctualLights;
validPunctualLights.length = 0;
const _sphere = Sphere.create(0, 0, 0, 1);
const { spotLights } = camera.scene!;
for (let i = 0; i < spotLights.length; i++) {
const light = spotLights[i];
if (light.baked) {
continue;
}
Sphere.set(_sphere, light.position.x, light.position.y, light.position.z, light.range);
if (intersect.sphereFrustum(_sphere, camera.frustum)) {
validPunctualLights.push(light);
}
}
const { sphereLights } = camera.scene!;
for (let i = 0; i < sphereLights.length; i++) {
const light = sphereLights[i];
if (light.baked) {
continue;
}
Sphere.set(_sphere, light.position.x, light.position.y, light.position.z, light.range);
if (intersect.sphereFrustum(_sphere, camera.frustum)) {
validPunctualLights.push(light);
}
}
}
const _cameras: Camera[] = [];
export function getCameraUniqueID (camera: Camera) {
if (!_cameras.includes(camera)) {
_cameras.push(camera);
}
return _cameras.indexOf(camera);
}
export function getLoadOpOfClearFlag (clearFlag: ClearFlagBit, attachment: AttachmentType): LoadOp {
let loadOp = LoadOp.CLEAR;
if (!(clearFlag & ClearFlagBit.COLOR)
&& attachment === AttachmentType.RENDER_TARGET) {
if (clearFlag & SKYBOX_FLAG) {
loadOp = LoadOp.DISCARD;
} else {
loadOp = LoadOp.LOAD;
}
}
if ((clearFlag & ClearFlagBit.DEPTH_STENCIL) !== ClearFlagBit.DEPTH_STENCIL
&& attachment === AttachmentType.DEPTH_STENCIL) {
if (!(clearFlag & ClearFlagBit.DEPTH)) loadOp = LoadOp.LOAD;
if (!(clearFlag & ClearFlagBit.STENCIL)) loadOp = LoadOp.LOAD;
}
return loadOp;
}
export function getRenderArea (camera: Camera, width: number, height: number, light: Light | null = null, level = 0): Rect {
const out = new Rect();
const vp = camera.viewport;
const w = width;
const h = height;
out.x = vp.x * w;
out.y = vp.y * h;
out.width = vp.width * w;
out.height = vp.height * h;
if (light) {
switch (light.type) {
case LightType.DIRECTIONAL: {
const mainLight = light as DirectionalLight;
if (mainLight.shadowFixedArea || mainLight.csmLevel === CSMLevel.LEVEL_1) {
out.x = 0;
out.y = 0;
out.width = w;
out.height = h;
} else {
out.x = level % 2 * 0.5 * w;
out.y = (1 - Math.floor(level / 2)) * 0.5 * h;
out.width = 0.5 * w;
out.height = 0.5 * h;
}
break;
}
case LightType.SPOT: {
out.x = 0;
out.y = 0;
out.width = w;
out.height = h;
break;
}
default:
}
}
return out;
}
export function buildShadowPass (passName: Readonly<string>,
ppl: Pipeline,
camera: Camera, light: Light, level: number,
width: Readonly<number>, height: Readonly<number>) {
const device = ppl.device;
const shadowMapName = passName;
if (!ppl.containsResource(shadowMapName)) {
const format = supportsR32FloatTexture(device) ? Format.R32F : Format.RGBA8;
ppl.addRenderTarget(shadowMapName, format, width, height, ResourceResidency.MANAGED);
ppl.addDepthStencil(`${shadowMapName}Depth`, Format.DEPTH_STENCIL, width, height, ResourceResidency.MANAGED);
}
const pass = ppl.addRasterPass(width, height, 'default', passName);
pass.addRasterView(shadowMapName, new RasterView('_',
AccessType.WRITE, AttachmentType.RENDER_TARGET,
LoadOp.CLEAR, StoreOp.STORE,
ClearFlagBit.COLOR,
new Color(1, 1, 1, camera.clearColor.w)));
pass.addRasterView(`${shadowMapName}Depth`, new RasterView('_',
AccessType.WRITE, AttachmentType.DEPTH_STENCIL,
LoadOp.CLEAR, StoreOp.DISCARD,
ClearFlagBit.DEPTH_STENCIL,
new Color(camera.clearDepth, camera.clearStencil, 0, 0)));
const rect = getRenderArea(camera, width, height, light, level);
pass.setViewport(new Viewport(rect.x, rect.y, rect.width, rect.height));
const queue = pass.addQueue(QueueHint.RENDER_OPAQUE);
queue.addSceneOfCamera(camera, new LightInfo(light, level),
SceneFlags.SHADOW_CASTER);
}
class CameraInfo {
shadowEnabled = false;
mainLightShadowNames = new Array<string>();
spotLightShadowNames = new Array<string>();
}
export function buildShadowPasses (cameraName: string, camera: Camera, ppl: Pipeline): CameraInfo {
const pipeline = ppl;
const shadowInfo = pipeline.pipelineSceneData.shadows;
const validPunctualLights = ppl.pipelineSceneData.validPunctualLights;
const cameraInfo = new CameraInfo();
const shadows = ppl.pipelineSceneData.shadows;
if (!shadowInfo.enabled || shadowInfo.type !== ShadowType.ShadowMap) { return cameraInfo; }
cameraInfo.shadowEnabled = true;
const _validLights: Light[] = [];
let n = 0;
let m = 0;
for (;n < shadowInfo.maxReceived && m < validPunctualLights.length;) {
const light = validPunctualLights[m];
if (light.type === LightType.SPOT) {
const spotLight = light as SpotLight;
if (spotLight.shadowEnabled) {
_validLights.push(light);
n++;
}
}
m++;
}
const { mainLight } = camera.scene!;
// build shadow map
const mapWidth = shadows.size.x;
const mapHeight = shadows.size.y;
if (mainLight && mainLight.shadowEnabled) {
cameraInfo.mainLightShadowNames[0] = `MainLightShadow${cameraName}`;
if (mainLight.shadowFixedArea) {
buildShadowPass(cameraInfo.mainLightShadowNames[0], ppl,
camera, mainLight, 0, mapWidth, mapHeight);
} else {
const csmLevel = pipeline.pipelineSceneData.csmSupported ? mainLight.csmLevel : 1;
for (let i = 0; i < csmLevel; i++) {
cameraInfo.mainLightShadowNames[i] = `MainLightShadow${cameraName}`;
buildShadowPass(cameraInfo.mainLightShadowNames[i], ppl,
camera, mainLight, i, mapWidth, mapHeight);
}
}
}
for (let l = 0; l < _validLights.length; l++) {
const light = _validLights[l];
const passName = `SpotLightShadow${l.toString()}${cameraName}`;
cameraInfo.spotLightShadowNames[l] = passName;
buildShadowPass(passName, ppl,
camera, light, 0, mapWidth, mapHeight);
}
return cameraInfo;
}