mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
[Optimize] Optimize the logic of the UBO code and fix the issue where the profiler does not display in special circumstances. (#17291)
This commit is contained in:
@@ -1309,36 +1309,7 @@ export function updatePerPassUBO (layout: string, user: RenderData): void {
|
||||
bindGlobalDesc(descriptorSet, bindId, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function mergeSrcToDstDesc (fromDesc, toDesc, isForce = false): number[] {
|
||||
fromDesc.update();
|
||||
const fromGpuDesc = fromDesc.gpuDescriptorSet;
|
||||
const toGpuDesc = toDesc.gpuDescriptorSet;
|
||||
const extResId: number[] = [];
|
||||
if (isForce) {
|
||||
toGpuDesc.gpuDescriptors = fromGpuDesc.gpuDescriptors;
|
||||
toGpuDesc.descriptorIndices = fromGpuDesc.descriptorIndices;
|
||||
return extResId;
|
||||
}
|
||||
for (let i = 0; i < toGpuDesc.gpuDescriptors.length; i++) {
|
||||
const fromRes = fromGpuDesc.gpuDescriptors[i];
|
||||
if (!fromRes) continue;
|
||||
const currRes = toGpuDesc.gpuDescriptors[i];
|
||||
if (!currRes.gpuBuffer && fromRes.gpuBuffer) {
|
||||
currRes.gpuBuffer = fromRes.gpuBuffer;
|
||||
extResId.push(i);
|
||||
} else if ('gpuTextureView' in currRes && !currRes.gpuTextureView) {
|
||||
currRes.gpuTextureView = fromRes.gpuTextureView;
|
||||
currRes.gpuSampler = fromRes.gpuSampler;
|
||||
extResId.push(i);
|
||||
} else if ('gpuTexture' in currRes && !currRes.gpuTexture) {
|
||||
currRes.gpuTexture = fromRes.gpuTexture;
|
||||
currRes.gpuSampler = fromRes.gpuSampler;
|
||||
extResId.push(i);
|
||||
}
|
||||
}
|
||||
return extResId;
|
||||
descriptorSet.update();
|
||||
}
|
||||
|
||||
const _varianceArray: number[] = [0.0484, 0.187, 0.567, 1.99, 7.41];
|
||||
|
||||
@@ -132,9 +132,7 @@ import { VectorGraphColorMap } from './effect';
|
||||
import {
|
||||
bool,
|
||||
getDescriptorSetDataFromLayout,
|
||||
getDescriptorSetDataFromLayoutId,
|
||||
getRenderArea,
|
||||
mergeSrcToDstDesc,
|
||||
updateGlobalDescBinding,
|
||||
} from './define';
|
||||
import { RenderReflectionProbeQueue } from '../render-reflection-probe-queue';
|
||||
@@ -941,7 +939,7 @@ class DeviceRenderPass implements RecordingInterface {
|
||||
this._deviceQueues.set(queue.queueId, queue);
|
||||
}
|
||||
preRecord (): void {
|
||||
context.descriptorSet = getDescriptorSetDataFromLayout(this.layoutName)!.descriptorSet;
|
||||
context.passDescriptorSet = getDescriptorSetDataFromLayout(this.layoutName)!.descriptorSet;
|
||||
}
|
||||
protected _applyRenderLayout (input: [string, ComputeView[]]): void {
|
||||
const stageName = context.renderGraph.getLayout(this.rasterPassInfo.id);
|
||||
@@ -977,6 +975,7 @@ class DeviceRenderPass implements RecordingInterface {
|
||||
if (!profiler || !profiler.enabled) {
|
||||
return;
|
||||
}
|
||||
const profilerDesc = context.profilerDescriptorSet;
|
||||
const renderPass = this._renderPass;
|
||||
const cmdBuff = context.commandBuffer;
|
||||
const submodel = profiler.subModels[0];
|
||||
@@ -996,6 +995,7 @@ class DeviceRenderPass implements RecordingInterface {
|
||||
cmdBuff.setViewport(profilerViewport);
|
||||
cmdBuff.setScissor(rect);
|
||||
cmdBuff.bindPipelineState(pso);
|
||||
cmdBuff.bindDescriptorSet(SetIndex.GLOBAL, profilerDesc);
|
||||
cmdBuff.bindDescriptorSet(SetIndex.MATERIAL, pass.descriptorSet);
|
||||
cmdBuff.bindDescriptorSet(SetIndex.LOCAL, submodel.descriptorSet);
|
||||
cmdBuff.bindInputAssembler(ia);
|
||||
@@ -1025,10 +1025,10 @@ class DeviceRenderPass implements RecordingInterface {
|
||||
this.clearDepth,
|
||||
this.clearStencil,
|
||||
);
|
||||
if (context.descriptorSet) {
|
||||
if (context.passDescriptorSet) {
|
||||
cmdBuff.bindDescriptorSet(
|
||||
SetIndex.GLOBAL,
|
||||
context.descriptorSet,
|
||||
context.passDescriptorSet,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1181,7 +1181,7 @@ class DeviceComputePass implements RecordingInterface {
|
||||
}
|
||||
}
|
||||
preRecord (): void {
|
||||
// nothing to do
|
||||
context.passDescriptorSet = getDescriptorSetDataFromLayout(this.layoutName)!.descriptorSet;
|
||||
}
|
||||
postRecord (): void {
|
||||
// nothing to do
|
||||
@@ -1220,11 +1220,10 @@ class DeviceComputePass implements RecordingInterface {
|
||||
// record common buffer
|
||||
record (): void {
|
||||
const cmdBuff = context.commandBuffer;
|
||||
|
||||
if (context.descriptorSet) {
|
||||
if (context.passDescriptorSet) {
|
||||
cmdBuff.bindDescriptorSet(
|
||||
SetIndex.GLOBAL,
|
||||
context.descriptorSet,
|
||||
context.passDescriptorSet,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1330,11 +1329,6 @@ class DeviceRenderScene implements RecordingInterface {
|
||||
const queueRenderData = context.renderGraph.getData(queueId)!;
|
||||
this._updateGlobal(queueRenderData);
|
||||
|
||||
const layoutName = context.renderGraph.getLayout(rasterId);
|
||||
const descSetData = getDescriptorSetDataFromLayout(layoutName);
|
||||
if (context.descriptorSet) {
|
||||
mergeSrcToDstDesc(descSetData!.descriptorSet, context.descriptorSet, true);
|
||||
}
|
||||
this._currentQueue.isUpdateUBO = true;
|
||||
|
||||
const batches = this.camera!.scene!.batches;
|
||||
@@ -1374,7 +1368,6 @@ class DeviceRenderScene implements RecordingInterface {
|
||||
const shader = pass.getShaderVariant();
|
||||
const devicePass = this._currentQueue.devicePass;
|
||||
const screenIa: InputAssembler = this._currentQueue.blitDesc!.screenQuad!.quadIA!;
|
||||
const globalDesc = context.descriptorSet;
|
||||
let pso: PipelineState | null = null;
|
||||
if (pass !== null && shader !== null && screenIa !== null) {
|
||||
pso = PipelineStateManager.getOrCreatePipelineState(
|
||||
@@ -1413,9 +1406,6 @@ class DeviceRenderScene implements RecordingInterface {
|
||||
const sceneId = this.graphScene.sceneID;
|
||||
const sceneRenderData = context.renderGraph.getData(sceneId)!;
|
||||
if (sceneRenderData) this._updateGlobal(sceneRenderData);
|
||||
const layoutName = context.renderGraph.getLayout(rasterId);
|
||||
const descSetData = getDescriptorSetDataFromLayout(layoutName);
|
||||
mergeSrcToDstDesc(descSetData!.descriptorSet, context.descriptorSet, true);
|
||||
this._currentQueue.isUpdateUBO = true;
|
||||
}
|
||||
|
||||
@@ -1731,7 +1721,8 @@ class ExecutorContext {
|
||||
this.pools = new ExecutorPools(this);
|
||||
this.blit = new BlitInfo(this);
|
||||
this.culling = new SceneCulling();
|
||||
this.descriptorSet = descriptorSet;
|
||||
this.passDescriptorSet = descriptorSet;
|
||||
this.profilerDescriptorSet = getDescriptorSetDataFromLayout('default')!.descriptorSet!;
|
||||
}
|
||||
reset (): void {
|
||||
this.culling.clear();
|
||||
@@ -1768,7 +1759,8 @@ class ExecutorContext {
|
||||
width: number;
|
||||
height: number;
|
||||
cullCamera;
|
||||
descriptorSet: DescriptorSet | null;
|
||||
passDescriptorSet: DescriptorSet | null;
|
||||
profilerDescriptorSet: DescriptorSet;
|
||||
}
|
||||
|
||||
export class Executor {
|
||||
|
||||
@@ -1,12 +1,163 @@
|
||||
import { RecyclePool, assert, cclegacy } from '../../core';
|
||||
import { CommandBuffer, DescriptorSet, Device, PipelineState, RenderPass, deviceManager } from '../../gfx';
|
||||
import { IMacroPatch, Pass } from '../../render-scene';
|
||||
|
||||
import { LightType, Model, SubModel } from '../../render-scene/scene';
|
||||
import { SetIndex } from '../define';
|
||||
import { Mat4, RecyclePool, IVec4Like, IMat4Like, IVec2Like,
|
||||
Color as CoreColor, assert, cclegacy, Quat, Vec4, Vec2, Vec3, toRadian } from '../../core';
|
||||
import { Color, CommandBuffer, DescriptorSet, Buffer, Device, PipelineState, RenderPass, Sampler, Texture, deviceManager } from '../../gfx';
|
||||
import { IMacroPatch, Pass, RenderScene } from '../../render-scene';
|
||||
import { Camera, CSMLevel, DirectionalLight, Light, LightType, Model, PCFType, PointLight,
|
||||
RangedDirectionalLight, Shadows, ShadowType, SphereLight, SpotLight, SubModel } from '../../render-scene/scene';
|
||||
import { Root } from '../../root';
|
||||
import { SetIndex, supportsR32FloatTexture } from '../define';
|
||||
import { InstancedBuffer } from '../instanced-buffer';
|
||||
import { PipelineStateManager } from '../pipeline-state-manager';
|
||||
import { SceneFlags } from './types';
|
||||
import { LayoutGraphData } from './layout-graph';
|
||||
import { RenderData } from './render-graph';
|
||||
import { SceneFlags, UpdateFrequency } from './types';
|
||||
import { Setter } from './pipeline';
|
||||
import { PipelineSceneData } from '../pipeline-scene-data';
|
||||
import { CSMLayers, CSMShadowLayer } from '../shadow/csm-layers';
|
||||
import { builtinResMgr } from '../../asset/asset-manager';
|
||||
import { TextureCube } from '../../asset/assets';
|
||||
|
||||
const _uboVec = new Vec4();
|
||||
const _uboVec3 = new Vec3();
|
||||
const _uboCol = new Color();
|
||||
const _matView = new Mat4();
|
||||
const _mulMatView = new Mat4();
|
||||
export function setTextureUBOView (setter: WebSetter, camera: Camera | null, cfg: Readonly<PipelineSceneData>, layout = 'default'): void {
|
||||
const skybox = cfg.skybox;
|
||||
const director = cclegacy.director;
|
||||
const root = director.root;
|
||||
const pipeline = root.pipeline;
|
||||
if (skybox.reflectionMap) {
|
||||
const texture = skybox.reflectionMap.getGFXTexture()!;
|
||||
const sampler: Sampler = root.device.getSampler(skybox.reflectionMap.getSamplerInfo());
|
||||
setter.setTexture('cc_environment', texture);
|
||||
setter.setSampler('cc_environment', sampler);
|
||||
} else {
|
||||
const envmap = skybox.envmap ? skybox.envmap : builtinResMgr.get<TextureCube>('default-cube-texture');
|
||||
if (envmap) {
|
||||
const texture = envmap.getGFXTexture()!;
|
||||
const sampler: Sampler = root.device.getSampler(envmap.getSamplerInfo());
|
||||
setter.setTexture('cc_environment', texture);
|
||||
setter.setSampler('cc_environment', sampler);
|
||||
}
|
||||
}
|
||||
const diffuseMap = skybox.diffuseMap ? skybox.diffuseMap : builtinResMgr.get<TextureCube>('default-cube-texture');
|
||||
if (diffuseMap) {
|
||||
const texture = diffuseMap.getGFXTexture()!;
|
||||
const sampler: Sampler = root.device.getSampler(diffuseMap.getSamplerInfo());
|
||||
setter.setTexture('cc_diffuseMap', texture);
|
||||
setter.setSampler('cc_diffuseMap', sampler);
|
||||
}
|
||||
if (!setter.hasSampler('cc_shadowMap')) {
|
||||
setter.setSampler('cc_shadowMap', pipeline.defaultSampler as Sampler);
|
||||
}
|
||||
if (!setter.hasTexture('cc_shadowMap')) {
|
||||
setter.setTexture('cc_shadowMap', pipeline.defaultShadowTexture as Texture);
|
||||
}
|
||||
if (!setter.hasSampler('cc_spotShadowMap')) {
|
||||
setter.setSampler('cc_spotShadowMap', pipeline.defaultSampler as Sampler);
|
||||
}
|
||||
if (!setter.hasTexture('cc_spotShadowMap')) {
|
||||
setter.setTexture('cc_spotShadowMap', pipeline.defaultShadowTexture as Texture);
|
||||
}
|
||||
}
|
||||
|
||||
export function setCameraUBOValues (
|
||||
setter: WebSetter,
|
||||
camera: Readonly<Camera> | null,
|
||||
cfg: Readonly<PipelineSceneData>,
|
||||
scene: RenderScene | null,
|
||||
layoutName = 'default',
|
||||
): void {
|
||||
const director = cclegacy.director;
|
||||
const root = director.root;
|
||||
const pipeline = root.pipeline;
|
||||
const shadowInfo = cfg.shadows;
|
||||
const skybox = cfg.skybox;
|
||||
const shadingScale = cfg.shadingScale;
|
||||
// Camera
|
||||
if (camera) {
|
||||
setter.setMat4('cc_matView', camera.matView);
|
||||
setter.setMat4('cc_matViewInv', camera.node.worldMatrix);
|
||||
setter.setMat4('cc_matProj', camera.matProj);
|
||||
setter.setMat4('cc_matProjInv', camera.matProjInv);
|
||||
setter.setMat4('cc_matViewProj', camera.matViewProj);
|
||||
setter.setMat4('cc_matViewProjInv', camera.matViewProjInv);
|
||||
_uboVec.set(
|
||||
camera.surfaceTransform,
|
||||
camera.cameraUsage,
|
||||
Math.cos(toRadian(skybox.getRotationAngle())),
|
||||
Math.sin(toRadian(skybox.getRotationAngle())),
|
||||
);
|
||||
setter.setVec4('cc_surfaceTransform', _uboVec);
|
||||
_uboVec.set(camera.exposure, 1.0 / camera.exposure, cfg.isHDR ? 1.0 : 0.0, 1.0 / Camera.standardExposureValue);
|
||||
setter.setVec4('cc_exposure', _uboVec);
|
||||
}
|
||||
if (camera) {
|
||||
_uboVec.set(camera.position.x, camera.position.y, camera.position.z, pipeline.getCombineSignY() as number);
|
||||
} else {
|
||||
_uboVec.set(0, 0, 0, pipeline.getCombineSignY() as number);
|
||||
}
|
||||
setter.setVec4('cc_cameraPos', _uboVec);
|
||||
_uboVec.set(cfg.shadingScale, cfg.shadingScale, 1.0 / cfg.shadingScale, 1.0 / cfg.shadingScale);
|
||||
setter.setVec4('cc_screenScale', _uboVec);
|
||||
const mainLight = scene && scene.mainLight;
|
||||
if (mainLight) {
|
||||
const shadowEnable = (mainLight.shadowEnabled && shadowInfo.type === ShadowType.ShadowMap) ? 1.0 : 0.0;
|
||||
_uboVec.set(mainLight.direction.x, mainLight.direction.y, mainLight.direction.z, shadowEnable);
|
||||
setter.setVec4('cc_mainLitDir', _uboVec);
|
||||
let r = mainLight.color.x;
|
||||
let g = mainLight.color.y;
|
||||
let b = mainLight.color.z;
|
||||
if (mainLight.useColorTemperature) {
|
||||
r *= mainLight.colorTemperatureRGB.x;
|
||||
g *= mainLight.colorTemperatureRGB.y;
|
||||
b *= mainLight.colorTemperatureRGB.z;
|
||||
}
|
||||
let w = mainLight.illuminance;
|
||||
if (cfg.isHDR && camera) {
|
||||
w *= camera.exposure;
|
||||
}
|
||||
_uboVec.set(r, g, b, w);
|
||||
setter.setVec4('cc_mainLitColor', _uboVec);
|
||||
} else {
|
||||
_uboVec.set(0, 0, 1, 0);
|
||||
setter.setVec4('cc_mainLitDir', _uboVec);
|
||||
_uboVec.set(0, 0, 0, 0);
|
||||
setter.setVec4('cc_mainLitColor', _uboVec);
|
||||
}
|
||||
|
||||
const ambient = cfg.ambient;
|
||||
const skyColor = ambient.skyColor;
|
||||
if (cfg.isHDR) {
|
||||
skyColor.w = ambient.skyIllum * (camera ? camera.exposure : 1);
|
||||
} else {
|
||||
skyColor.w = ambient.skyIllum;
|
||||
}
|
||||
_uboVec.set(skyColor.x, skyColor.y, skyColor.z, skyColor.w);
|
||||
setter.setVec4('cc_ambientSky', _uboVec);
|
||||
_uboVec.set(ambient.groundAlbedo.x, ambient.groundAlbedo.y, ambient.groundAlbedo.z, skybox.envmap ? skybox.envmap?.mipmapLevel : 1.0);
|
||||
setter.setVec4('cc_ambientGround', _uboVec);
|
||||
const fog = cfg.fog;
|
||||
const colorTempRGB = fog.colorArray;
|
||||
_uboVec.set(colorTempRGB.x, colorTempRGB.y, colorTempRGB.z, colorTempRGB.z);
|
||||
setter.setVec4('cc_fogColor', _uboVec);
|
||||
_uboVec.set(fog.fogStart, fog.fogEnd, fog.fogDensity, 0.0);
|
||||
setter.setVec4('cc_fogBase', _uboVec);
|
||||
_uboVec.set(fog.fogTop, fog.fogRange, fog.fogAtten, 0.0);
|
||||
setter.setVec4('cc_fogAdd', _uboVec);
|
||||
if (camera) {
|
||||
_uboVec.set(camera.nearClip, camera.farClip, camera.getClipSpaceMinz(), 0.0);
|
||||
setter.setVec4('cc_nearFar', _uboVec);
|
||||
_uboVec.set(
|
||||
camera.viewport.x,
|
||||
camera.viewport.y,
|
||||
shadingScale * camera.window.width * camera.viewport.z,
|
||||
shadingScale * camera.window.height * camera.viewport.w,
|
||||
);
|
||||
setter.setVec4('cc_viewPort', _uboVec);
|
||||
}
|
||||
}
|
||||
|
||||
export class DrawInstance {
|
||||
subModel: SubModel | null;
|
||||
@@ -122,6 +273,540 @@ export class ProbeHelperQueue {
|
||||
}
|
||||
}
|
||||
|
||||
export function setShadowUBOLightView (
|
||||
setter: WebSetter,
|
||||
camera: Camera | null,
|
||||
light: Light,
|
||||
csmLevel: number,
|
||||
layout = 'default',
|
||||
): void {
|
||||
const director = cclegacy.director;
|
||||
const pipeline = (director.root as Root).pipeline;
|
||||
const device = pipeline.device;
|
||||
const sceneData = pipeline.pipelineSceneData;
|
||||
|
||||
const shadowInfo = sceneData.shadows;
|
||||
if (shadowInfo.type === ShadowType.Planar) {
|
||||
return;
|
||||
}
|
||||
const csmLayers = sceneData.csmLayers;
|
||||
const packing = supportsR32FloatTexture(device) ? 0.0 : 1.0;
|
||||
const cap = pipeline.device.capabilities;
|
||||
// ShadowMap
|
||||
if (shadowInfo.enabled) {
|
||||
if (shadowInfo.type === ShadowType.ShadowMap) {
|
||||
// update CSM layers
|
||||
if (light && light.node && light.type === LightType.DIRECTIONAL) {
|
||||
csmLayers.update(sceneData, camera!);
|
||||
}
|
||||
}
|
||||
}
|
||||
switch (light.type) {
|
||||
case LightType.DIRECTIONAL: {
|
||||
const mainLight = light as DirectionalLight;
|
||||
if (shadowInfo.enabled && mainLight && mainLight.shadowEnabled) {
|
||||
if (shadowInfo.type === ShadowType.ShadowMap) {
|
||||
let near = 0.1;
|
||||
let far = 0;
|
||||
let matShadowView: Mat4;
|
||||
let matShadowProj: Mat4;
|
||||
let matShadowViewProj: Mat4;
|
||||
let levelCount = 0;
|
||||
if (mainLight.shadowFixedArea || mainLight.csmLevel === CSMLevel.LEVEL_1) {
|
||||
matShadowView = csmLayers.specialLayer.matShadowView;
|
||||
matShadowProj = csmLayers.specialLayer.matShadowProj;
|
||||
matShadowViewProj = csmLayers.specialLayer.matShadowViewProj;
|
||||
if (mainLight.shadowFixedArea) {
|
||||
near = mainLight.shadowNear;
|
||||
far = mainLight.shadowFar;
|
||||
levelCount = 0;
|
||||
} else {
|
||||
near = 0.1;
|
||||
far = csmLayers.specialLayer.shadowCameraFar;
|
||||
levelCount = 1;
|
||||
}
|
||||
_uboVec.set(LightType.DIRECTIONAL, packing, mainLight.shadowNormalBias, 0);
|
||||
setter.setVec4('cc_shadowLPNNInfo', _uboVec);
|
||||
} else {
|
||||
const layer = csmLayers.layers[csmLevel];
|
||||
matShadowView = layer.matShadowView;
|
||||
matShadowProj = layer.matShadowProj;
|
||||
matShadowViewProj = layer.matShadowViewProj;
|
||||
|
||||
near = layer.splitCameraNear;
|
||||
far = layer.splitCameraFar;
|
||||
levelCount = mainLight.csmLevel;
|
||||
}
|
||||
setter.setMat4('cc_matLightView', matShadowView);
|
||||
_uboVec.set(matShadowProj.m10, matShadowProj.m14, matShadowProj.m11, matShadowProj.m15);
|
||||
setter.setVec4('cc_shadowProjDepthInfo', _uboVec);
|
||||
_uboVec.set(matShadowProj.m00, matShadowProj.m05, 1.0 / matShadowProj.m00, 1.0 / matShadowProj.m05);
|
||||
setter.setVec4('cc_shadowProjInfo', _uboVec);
|
||||
setter.setMat4('cc_matLightViewProj', matShadowViewProj);
|
||||
_uboVec.set(near, far, 0, 1.0 - mainLight.shadowSaturation);
|
||||
setter.setVec4('cc_shadowNFLSInfo', _uboVec);
|
||||
_uboVec.set(LightType.DIRECTIONAL, packing, mainLight.shadowNormalBias, levelCount);
|
||||
setter.setVec4('cc_shadowLPNNInfo', _uboVec);
|
||||
_uboVec.set(shadowInfo.size.x, shadowInfo.size.y, mainLight.shadowPcf, mainLight.shadowBias);
|
||||
setter.setVec4('cc_shadowWHPBInfo', _uboVec);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case LightType.SPOT: {
|
||||
const spotLight = light as SpotLight;
|
||||
if (shadowInfo.enabled && spotLight && spotLight.shadowEnabled) {
|
||||
Mat4.invert(_matView, spotLight.node!.getWorldMatrix());
|
||||
setter.setMat4('cc_matLightView', _matView);
|
||||
Mat4.perspective(
|
||||
_mulMatView,
|
||||
spotLight.angle,
|
||||
1.0,
|
||||
0.001,
|
||||
spotLight.range,
|
||||
true,
|
||||
cap.clipSpaceMinZ,
|
||||
cap.clipSpaceSignY,
|
||||
0,
|
||||
);
|
||||
const matShadowInvProj: Mat4 = _mulMatView.clone().invert();
|
||||
const matShadowProj: Mat4 = _mulMatView.clone();
|
||||
|
||||
Mat4.multiply(_matView, _mulMatView, _matView);
|
||||
setter.setMat4('cc_matLightViewProj', _matView);
|
||||
_uboVec.set(0.01, (light as SpotLight).range, 0.0, 0.0);
|
||||
setter.setVec4('cc_shadowNFLSInfo', _uboVec);
|
||||
_uboVec.set(shadowInfo.size.x, shadowInfo.size.y, spotLight.shadowPcf, spotLight.shadowBias);
|
||||
setter.setVec4('cc_shadowWHPBInfo', _uboVec);
|
||||
_uboVec.set(LightType.SPOT, packing, spotLight.shadowNormalBias, 0.0);
|
||||
setter.setVec4('cc_shadowLPNNInfo', _uboVec);
|
||||
_uboVec.set(matShadowProj.m10, matShadowProj.m14, matShadowProj.m11, matShadowProj.m15);
|
||||
setter.setVec4('cc_shadowProjDepthInfo', _uboVec);
|
||||
_uboVec.set(matShadowInvProj.m10, matShadowInvProj.m14, matShadowInvProj.m11, matShadowInvProj.m15);
|
||||
setter.setVec4('cc_shadowInvProjDepthInfo', _uboVec);
|
||||
_uboVec.set(matShadowProj.m00, matShadowProj.m05, 1.0 / matShadowProj.m00, 1.0 / matShadowProj.m05);
|
||||
setter.setVec4('cc_shadowProjInfo', _uboVec);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case LightType.SPHERE: {
|
||||
_uboVec.set(shadowInfo.size.x, shadowInfo.size.y, 1.0, 0.0);
|
||||
setter.setVec4('cc_shadowWHPBInfo', _uboVec);
|
||||
_uboVec.set(LightType.SPHERE, packing, 0.0, 0.0);
|
||||
setter.setVec4('cc_shadowLPNNInfo', _uboVec);
|
||||
break;
|
||||
}
|
||||
case LightType.POINT: {
|
||||
_uboVec.set(shadowInfo.size.x, shadowInfo.size.y, 1.0, 0.0);
|
||||
setter.setVec4('cc_shadowWHPBInfo', _uboVec);
|
||||
_uboVec.set(LightType.POINT, packing, 0.0, 0.0);
|
||||
setter.setVec4('cc_shadowLPNNInfo', _uboVec);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
}
|
||||
_uboCol.set(shadowInfo.shadowColor.x, shadowInfo.shadowColor.y, shadowInfo.shadowColor.z, shadowInfo.shadowColor.w);
|
||||
setter.setColor('cc_shadowColor', _uboCol);
|
||||
}
|
||||
|
||||
function getPCFRadius (shadowInfo: Shadows, mainLight: DirectionalLight): number {
|
||||
const shadowMapSize = shadowInfo.size.x;
|
||||
switch (mainLight.shadowPcf) {
|
||||
case PCFType.HARD:
|
||||
return 0.0;
|
||||
case PCFType.SOFT:
|
||||
return 1.0 / (shadowMapSize * 0.5);
|
||||
case PCFType.SOFT_2X:
|
||||
return 2.0 / (shadowMapSize * 0.5);
|
||||
case PCFType.SOFT_4X:
|
||||
return 3.0 / (shadowMapSize * 0.5);
|
||||
default:
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
export function setShadowUBOView (setter: WebSetter, camera: Camera | null, layout = 'default'): void {
|
||||
const director = cclegacy.director;
|
||||
const pipeline = director.root.pipeline;
|
||||
const device: Device = pipeline.device;
|
||||
const scene = director.getScene();
|
||||
const mainLight: DirectionalLight = camera && camera.scene ? camera.scene.mainLight : scene ? scene.renderScene.mainLight : null;
|
||||
const sceneData = pipeline.pipelineSceneData;
|
||||
const shadowInfo: Shadows = sceneData.shadows;
|
||||
const csmLayers: CSMLayers = sceneData.csmLayers;
|
||||
const csmSupported = sceneData.csmSupported;
|
||||
const packing = supportsR32FloatTexture(device) ? 0.0 : 1.0;
|
||||
if (mainLight && shadowInfo.enabled) {
|
||||
if (shadowInfo.type === ShadowType.ShadowMap) {
|
||||
if (mainLight.shadowEnabled) {
|
||||
if (mainLight.shadowFixedArea || mainLight.csmLevel === CSMLevel.LEVEL_1 || !csmSupported) {
|
||||
const matShadowView: Mat4 = csmLayers.specialLayer.matShadowView;
|
||||
const matShadowProj: Mat4 = csmLayers.specialLayer.matShadowProj;
|
||||
const matShadowViewProj: Mat4 = csmLayers.specialLayer.matShadowViewProj;
|
||||
const near: number = mainLight.shadowNear;
|
||||
const far: number = mainLight.shadowFar;
|
||||
setter.setMat4('cc_matLightView', matShadowView);
|
||||
_uboVec.set(matShadowProj.m10, matShadowProj.m14, matShadowProj.m11, matShadowProj.m15);
|
||||
setter.setVec4('cc_shadowProjDepthInfo', _uboVec);
|
||||
_uboVec.set(matShadowProj.m00, matShadowProj.m05, 1.0 / matShadowProj.m00, 1.0 / matShadowProj.m05);
|
||||
setter.setVec4('cc_shadowProjInfo', _uboVec);
|
||||
setter.setMat4('cc_matLightViewProj', matShadowViewProj);
|
||||
_uboVec.set(near, far, 0, 1.0 - mainLight.shadowSaturation);
|
||||
setter.setVec4('cc_shadowNFLSInfo', _uboVec);
|
||||
_uboVec.set(LightType.DIRECTIONAL, packing, mainLight.shadowNormalBias, 0);
|
||||
setter.setVec4('cc_shadowLPNNInfo', _uboVec);
|
||||
} else {
|
||||
const layerThreshold = getPCFRadius(shadowInfo, mainLight);
|
||||
for (let i = 0; i < mainLight.csmLevel; i++) {
|
||||
const layer: CSMShadowLayer = csmLayers.layers[i];
|
||||
const matShadowView: Mat4 = layer.matShadowView;
|
||||
_uboVec.set(matShadowView.m00, matShadowView.m04, matShadowView.m08, layerThreshold);
|
||||
setter.setVec4('cc_csmViewDir0', _uboVec, i);
|
||||
_uboVec.set(matShadowView.m01, matShadowView.m05, matShadowView.m09, layer.splitCameraNear);
|
||||
setter.setVec4('cc_csmViewDir1', _uboVec, i);
|
||||
_uboVec.set(matShadowView.m02, matShadowView.m06, matShadowView.m10, layer.splitCameraFar);
|
||||
setter.setVec4('cc_csmViewDir2', _uboVec, i);
|
||||
|
||||
const csmAtlas = layer.csmAtlas;
|
||||
setter.setVec4('cc_csmAtlas', csmAtlas, i);
|
||||
|
||||
const matShadowViewProj = layer.matShadowViewProj;
|
||||
setter.setMat4('cc_matCSMViewProj', matShadowViewProj, i);
|
||||
const matShadowProj = layer.matShadowProj;
|
||||
_uboVec.set(matShadowProj.m10, matShadowProj.m14, matShadowProj.m11, matShadowProj.m15);
|
||||
setter.setVec4('cc_csmProjDepthInfo', _uboVec, i);
|
||||
|
||||
_uboVec.set(matShadowProj.m00, matShadowProj.m05, 1.0 / matShadowProj.m00, 1.0 / matShadowProj.m05);
|
||||
setter.setVec4('cc_csmProjInfo', _uboVec, i);
|
||||
}
|
||||
_uboVec.set(mainLight.csmTransitionRange, 0, 0, 0);
|
||||
setter.setVec4('cc_csmSplitsInfo', _uboVec);
|
||||
_uboVec.set(0, 0, 0, 1.0 - mainLight.shadowSaturation);
|
||||
setter.setVec4('cc_shadowNFLSInfo', _uboVec);
|
||||
_uboVec.set(LightType.DIRECTIONAL, packing, mainLight.shadowNormalBias, mainLight.csmLevel);
|
||||
setter.setVec4('cc_shadowLPNNInfo', _uboVec);
|
||||
}
|
||||
_uboVec.set(shadowInfo.size.x, shadowInfo.size.y, mainLight.shadowPcf, mainLight.shadowBias);
|
||||
setter.setVec4('cc_shadowWHPBInfo', _uboVec);
|
||||
}
|
||||
} else {
|
||||
Vec3.normalize(_uboVec3, shadowInfo.normal);
|
||||
_uboVec.set(_uboVec3.x, _uboVec3.y, _uboVec3.z, -shadowInfo.distance);
|
||||
setter.setVec4('cc_planarNDInfo', _uboVec);
|
||||
|
||||
_uboVec.set(0, 0, 0, shadowInfo.planeBias);
|
||||
setter.setVec4('cc_shadowWHPBInfo', _uboVec);
|
||||
}
|
||||
setter.setMathColor('cc_shadowColor', shadowInfo.shadowColor);
|
||||
}
|
||||
}
|
||||
|
||||
export class WebSetter implements Setter {
|
||||
constructor (data: RenderData, lg: LayoutGraphData) {
|
||||
this._data = data;
|
||||
this._lg = lg;
|
||||
}
|
||||
get name (): string {
|
||||
return '';
|
||||
}
|
||||
set name (name: string) {
|
||||
// noop
|
||||
}
|
||||
|
||||
public setMat4 (name: string, mat: Mat4, idx = 0): void {
|
||||
WebSetter.setMat4(this._lg, this._data, name, mat, idx);
|
||||
}
|
||||
|
||||
public static setMat4 (lg: LayoutGraphData, data: RenderData, name: string, mat: Mat4, idx = 0): void {
|
||||
const info = WebSetter.getConstantInfo(lg, data, name);
|
||||
Mat4.toArray(info.dataArr, mat, idx * 16);
|
||||
data.constants.set(info.constantID, info.dataArr);
|
||||
}
|
||||
|
||||
public setQuaternion (name: string, quat: Quat, idx = 0): void {
|
||||
WebSetter.setQuaternion(this._lg, this._data, name, quat, idx);
|
||||
}
|
||||
public static setQuaternion (lg: LayoutGraphData, data: RenderData, name: string, quat: Quat, idx = 0): void {
|
||||
const info = WebSetter.getConstantInfo(lg, data, name);
|
||||
Quat.toArray(info.dataArr, quat, idx * 4);
|
||||
data.constants.set(info.constantID, info.dataArr);
|
||||
}
|
||||
public setColor (name: string, color: Color, idx = 0): void {
|
||||
WebSetter.setColor(this._lg, this._data, name, color, idx);
|
||||
}
|
||||
|
||||
public static setColor (lg: LayoutGraphData, data: RenderData, name: string, color: Color, idx = 0): void {
|
||||
const info = WebSetter.getConstantInfo(lg, data, name);
|
||||
const currIdx = idx * 4;
|
||||
info.dataArr[0 + currIdx] = color.x;
|
||||
info.dataArr[1 + currIdx] = color.y;
|
||||
info.dataArr[2 + currIdx] = color.z;
|
||||
info.dataArr[3 + currIdx] = color.w;
|
||||
data.constants.set(info.constantID, info.dataArr);
|
||||
}
|
||||
|
||||
public setMathColor (name: string, color: CoreColor, idx = 0): void {
|
||||
WebSetter.setMathColor(this._lg, this._data, name, color, idx);
|
||||
}
|
||||
public static setMathColor (lg: LayoutGraphData, data: RenderData, name: string, color: CoreColor, idx = 0): void {
|
||||
const info = WebSetter.getConstantInfo(lg, data, name);
|
||||
CoreColor.toArray(info.dataArr, color, idx * 4);
|
||||
data.constants.set(info.constantID, info.dataArr);
|
||||
}
|
||||
public static getConstantInfo (lg: LayoutGraphData, data: RenderData, name: string): { constantID: number, dataArr: Array<number>} {
|
||||
const constantID = lg.constantIndex.get(name)!;
|
||||
if (constantID === undefined) {
|
||||
throw new Error(`Constant with name ${name} not found.`);
|
||||
}
|
||||
const dataArr = data.constants.get(constantID)! || [];
|
||||
return { constantID, dataArr };
|
||||
}
|
||||
public setVec4 (name: string, vec: Vec4, idx = 0): void {
|
||||
WebSetter.setVec4(this._lg, this._data, name, vec, idx);
|
||||
}
|
||||
public static setVec4 (lg: LayoutGraphData, data: RenderData, name: string, vec: Vec4, idx = 0): void {
|
||||
const info = WebSetter.getConstantInfo(lg, data, name);
|
||||
Vec4.toArray(info.dataArr, vec, idx * 4);
|
||||
data.constants.set(info.constantID, info.dataArr);
|
||||
}
|
||||
public setVec2 (name: string, vec: Vec2, idx = 0): void {
|
||||
WebSetter.setVec2(this._lg, this._data, name, vec, idx);
|
||||
}
|
||||
public static setVec2 (lg: LayoutGraphData, data: RenderData, name: string, vec: Vec2, idx = 0): void {
|
||||
const info = WebSetter.getConstantInfo(lg, data, name);
|
||||
Vec2.toArray(info.dataArr, vec, idx * 2);
|
||||
data.constants.set(info.constantID, info.dataArr);
|
||||
}
|
||||
|
||||
public setFloat (name: string, v: number, idx = 0): void {
|
||||
WebSetter.setFloat(this._lg, this._data, name, v, idx);
|
||||
}
|
||||
public static setFloat (lg: LayoutGraphData, data: RenderData, name: string, v: number, idx = 0): void {
|
||||
const info = WebSetter.getConstantInfo(lg, data, name);
|
||||
info.dataArr[0 + idx] = v;
|
||||
data.constants.set(info.constantID, info.dataArr);
|
||||
}
|
||||
public setArrayBuffer (name: string, arrayBuffer: ArrayBuffer): void {
|
||||
WebSetter.setArrayBuffer(this._lg, this._data, name, arrayBuffer);
|
||||
}
|
||||
public static setArrayBuffer (lg: LayoutGraphData, data: RenderData, name: string, arrayBuffer: ArrayBuffer): void {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
public setBuffer (name: string, buffer: Buffer): void {
|
||||
WebSetter.setBuffer(this._lg, this._data, name, buffer);
|
||||
}
|
||||
public static setBuffer (lg: LayoutGraphData, data: RenderData, name: string, buffer: Buffer): void {
|
||||
const num = lg.attributeIndex.get(name)!;
|
||||
data.buffers.set(num, buffer);
|
||||
}
|
||||
public setTexture (name: string, texture: Texture): void {
|
||||
WebSetter.setTexture(this._lg, this._data, name, texture);
|
||||
}
|
||||
public static setTexture (lg: LayoutGraphData, data: RenderData, name: string, texture: Texture): void {
|
||||
const num = lg.attributeIndex.get(name)!;
|
||||
data.textures.set(num, texture);
|
||||
}
|
||||
public setReadWriteBuffer (name: string, buffer: Buffer): void {
|
||||
WebSetter.setReadWriteBuffer(this._lg, this._data, name, buffer);
|
||||
}
|
||||
public static setReadWriteBuffer (lg: LayoutGraphData, data: RenderData, name: string, buffer: Buffer): void {
|
||||
const num = lg.attributeIndex.get(name)!;
|
||||
data.buffers.set(num, buffer);
|
||||
}
|
||||
public setReadWriteTexture (name: string, texture: Texture): void {
|
||||
WebSetter.setReadWriteTexture(this._lg, this._data, name, texture);
|
||||
}
|
||||
public static setReadWriteTexture (lg: LayoutGraphData, data: RenderData, name: string, texture: Texture): void {
|
||||
const num = lg.attributeIndex.get(name)!;
|
||||
data.textures.set(num, texture);
|
||||
}
|
||||
public setSampler (name: string, sampler: Sampler): void {
|
||||
WebSetter.setSampler(this._lg, this._data, name, sampler);
|
||||
}
|
||||
public static setSampler (lg: LayoutGraphData, data: RenderData, name: string, sampler: Sampler): void {
|
||||
const num = lg.attributeIndex.get(name)!;
|
||||
data.samplers.set(num, sampler);
|
||||
}
|
||||
|
||||
public getParentLayout (): string {
|
||||
const director = cclegacy.director;
|
||||
const root = director.root;
|
||||
const pipeline = root.pipeline;
|
||||
const parId = pipeline.renderGraph!.getParent(this._vertID);
|
||||
const layoutName = pipeline.renderGraph!.getLayout(parId) as string;
|
||||
return layoutName;
|
||||
}
|
||||
|
||||
public getCurrentLayout (): string {
|
||||
const director = cclegacy.director;
|
||||
const root = director.root;
|
||||
const pipeline = root.pipeline;
|
||||
const layoutName = pipeline.renderGraph!.getLayout(this._vertID) as string;
|
||||
return layoutName;
|
||||
}
|
||||
|
||||
public setBuiltinCameraConstants (camera: Camera): void {
|
||||
const director = cclegacy.director;
|
||||
const root = director.root;
|
||||
const pipeline = root.pipeline;
|
||||
const layoutName = this.getParentLayout();
|
||||
setCameraUBOValues(this, camera, pipeline.pipelineSceneData as PipelineSceneData, camera.scene, layoutName);
|
||||
}
|
||||
public setBuiltinShadowMapConstants (light: Light, numLevels?: number): void {
|
||||
setShadowUBOView(this, null, this.getParentLayout());
|
||||
}
|
||||
public setBuiltinDirectionalLightFrustumConstants (camera: Camera, light: DirectionalLight, csmLevel = 0): void {
|
||||
setShadowUBOLightView(this, camera, light, csmLevel);
|
||||
}
|
||||
public setBuiltinSpotLightFrustumConstants (light: SpotLight): void {
|
||||
setShadowUBOLightView(this, null, light, 0);
|
||||
}
|
||||
public setBuiltinDirectionalLightConstants (light: DirectionalLight, camera: Camera): void {
|
||||
this.setBuiltinShadowMapConstants(light);
|
||||
}
|
||||
public setBuiltinSphereLightConstants (light: SphereLight, camera: Camera): void {
|
||||
const director = cclegacy.director;
|
||||
const pipeline = (director.root as Root).pipeline;
|
||||
const sceneData = pipeline.pipelineSceneData;
|
||||
_uboVec.set(light.position.x, light.position.y, light.position.z, LightType.SPHERE);
|
||||
this.setVec4('cc_lightPos', _uboVec);
|
||||
|
||||
_uboVec.set(light.size, light.range, 0.0, 0.0);
|
||||
this.setVec4('cc_lightSizeRangeAngle', _uboVec);
|
||||
|
||||
const isHDR = sceneData.isHDR;
|
||||
const lightMeterScale = 10000.0;
|
||||
_uboVec.set(light.color.x, light.color.y, light.color.z, 0);
|
||||
if (light.useColorTemperature) {
|
||||
const finalColor = light.finalColor;
|
||||
_uboVec.x = finalColor.x;
|
||||
_uboVec.y = finalColor.y;
|
||||
_uboVec.z = finalColor.z;
|
||||
}
|
||||
if (isHDR) {
|
||||
_uboVec.w = (light).luminance * camera.exposure * lightMeterScale;
|
||||
} else {
|
||||
_uboVec.w = (light).luminance;
|
||||
}
|
||||
this.setVec4('cc_lightColor', _uboVec);
|
||||
}
|
||||
public setBuiltinSpotLightConstants (light: SpotLight, camera: Camera): void {
|
||||
const director = cclegacy.director;
|
||||
const pipeline = (director.root as Root).pipeline;
|
||||
const sceneData = pipeline.pipelineSceneData;
|
||||
|
||||
const shadowInfo = sceneData.shadows;
|
||||
_uboVec.set(light.position.x, light.position.y, light.position.z, LightType.SPOT);
|
||||
this.setVec4('cc_lightPos', _uboVec);
|
||||
_uboVec.set(
|
||||
light.size,
|
||||
light.range,
|
||||
light.spotAngle,
|
||||
(shadowInfo.enabled && light.shadowEnabled && shadowInfo.type === ShadowType.ShadowMap) ? 1 : 0,
|
||||
);
|
||||
this.setVec4('cc_lightSizeRangeAngle', _uboVec);
|
||||
_uboVec.set(light.direction.x, light.direction.y, light.direction.z, 0);
|
||||
this.setVec4('cc_lightDir', _uboVec);
|
||||
const isHDR = sceneData.isHDR;
|
||||
const lightMeterScale = 10000.0;
|
||||
_uboVec.set(light.color.x, light.color.y, light.color.z, 0);
|
||||
if (light.useColorTemperature) {
|
||||
const finalColor = light.finalColor;
|
||||
_uboVec.x = finalColor.x;
|
||||
_uboVec.y = finalColor.y;
|
||||
_uboVec.z = finalColor.z;
|
||||
}
|
||||
if (isHDR) {
|
||||
_uboVec.w = (light).luminance * camera.exposure * lightMeterScale;
|
||||
} else {
|
||||
_uboVec.w = (light).luminance;
|
||||
}
|
||||
this.setVec4('cc_lightColor', _uboVec);
|
||||
_uboVec.set(0, 0, 0, light.angleAttenuationStrength);
|
||||
this.setVec4('cc_lightBoundingSizeVS', _uboVec);
|
||||
}
|
||||
public setBuiltinPointLightConstants (light: PointLight, camera: Camera): void {
|
||||
const director = cclegacy.director;
|
||||
const pipeline = (director.root as Root).pipeline;
|
||||
const sceneData = pipeline.pipelineSceneData;
|
||||
_uboVec.set(light.position.x, light.position.y, light.position.z, LightType.POINT);
|
||||
this.setVec4('cc_lightPos', _uboVec);
|
||||
_uboVec.set(0.0, light.range, 0.0, 0.0);
|
||||
this.setVec4('cc_lightSizeRangeAngle', _uboVec);
|
||||
const isHDR = sceneData.isHDR;
|
||||
const lightMeterScale = 10000.0;
|
||||
if (light.useColorTemperature) {
|
||||
const finalColor = light.finalColor;
|
||||
_uboVec.x = finalColor.x;
|
||||
_uboVec.y = finalColor.y;
|
||||
_uboVec.z = finalColor.z;
|
||||
}
|
||||
if (isHDR) {
|
||||
_uboVec.w = (light).luminance * camera.exposure * lightMeterScale;
|
||||
} else {
|
||||
_uboVec.w = (light).luminance;
|
||||
}
|
||||
_uboVec.set(light.color.x, light.color.y, light.color.z, 0);
|
||||
this.setVec4('cc_lightColor', _uboVec);
|
||||
}
|
||||
public setBuiltinRangedDirectionalLightConstants (light: RangedDirectionalLight, camera: Camera): void {
|
||||
const director = cclegacy.director;
|
||||
const pipeline = (director.root as Root).pipeline;
|
||||
const sceneData = pipeline.pipelineSceneData;
|
||||
_uboVec.set(light.position.x, light.position.y, light.position.z, LightType.RANGED_DIRECTIONAL);
|
||||
this.setVec4('cc_lightPos', _uboVec);
|
||||
|
||||
_uboVec.set(light.right.x, light.right.y, light.right.z, 0.0);
|
||||
this.setVec4('cc_lightSizeRangeAngle', _uboVec);
|
||||
|
||||
_uboVec.set(light.direction.x, light.direction.y, light.direction.z, 0);
|
||||
this.setVec4('cc_lightDir', _uboVec);
|
||||
const scale = light.scale;
|
||||
_uboVec.set(scale.x * 0.5, scale.y * 0.5, scale.z * 0.5, 0);
|
||||
this.setVec4('cc_lightBoundingSizeVS', _uboVec);
|
||||
const isHDR = sceneData.isHDR;
|
||||
_uboVec.set(light.color.x, light.color.y, light.color.z, 0);
|
||||
if (light.useColorTemperature) {
|
||||
const finalColor = light.finalColor;
|
||||
_uboVec.x = finalColor.x;
|
||||
_uboVec.y = finalColor.y;
|
||||
_uboVec.z = finalColor.z;
|
||||
}
|
||||
if (isHDR) {
|
||||
_uboVec.w = light.illuminance * camera.exposure;
|
||||
} else {
|
||||
_uboVec.w = light.illuminance;
|
||||
}
|
||||
this.setVec4('cc_lightColor', _uboVec);
|
||||
}
|
||||
public hasSampler (name: string): boolean {
|
||||
const id = this._lg.constantIndex.get(name);
|
||||
if (id === undefined) {
|
||||
return false;
|
||||
}
|
||||
return this._data.samplers.has(id);
|
||||
}
|
||||
public hasTexture (name: string): boolean {
|
||||
const id = this._lg.constantIndex.get(name);
|
||||
if (id === undefined) {
|
||||
return false;
|
||||
}
|
||||
return this._data.textures.has(id);
|
||||
}
|
||||
public setCustomBehavior (name: string): void {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
|
||||
// protected
|
||||
protected _data: RenderData;
|
||||
protected _lg: LayoutGraphData;
|
||||
protected _vertID: number = -1;
|
||||
protected _currBlock;
|
||||
protected _currStage: string = '';
|
||||
protected _currFrequency: UpdateFrequency = UpdateFrequency.PER_PASS;
|
||||
protected _currCount;
|
||||
protected _currConstant: number[] = [];
|
||||
}
|
||||
|
||||
export class RenderDrawQueue {
|
||||
instances: Array<DrawInstance> = new Array<DrawInstance>();
|
||||
|
||||
|
||||
@@ -25,8 +25,8 @@
|
||||
/* eslint-disable max-len */
|
||||
import { systemInfo } from 'pal/system-info';
|
||||
import { DEBUG, EDITOR } from 'internal:constants';
|
||||
import { Buffer, DescriptorSetLayout, Device, Feature, Format, FormatFeatureBit, Sampler, Swapchain, Texture, ClearFlagBit, DescriptorSet, deviceManager, Viewport, API, CommandBuffer, Type, SamplerInfo, Filter, Address, DescriptorSetInfo, LoadOp, StoreOp, ShaderStageFlagBit, BufferInfo, TextureInfo, TextureType, UniformBlock, ResolveMode, SampleCount, Color, ComparisonFunc } from '../../gfx';
|
||||
import { Mat4, Quat, toRadian, Vec2, Vec3, Vec4, assert, macro, cclegacy, IVec4Like, IMat4Like, IVec2Like, Color as CoreColor, RecyclePool } from '../../core';
|
||||
import { DescriptorSetLayout, Device, Feature, Format, FormatFeatureBit, Sampler, Swapchain, Texture, ClearFlagBit, DescriptorSet, deviceManager, Viewport, API, CommandBuffer, Type, SamplerInfo, Filter, Address, DescriptorSetInfo, LoadOp, StoreOp, ShaderStageFlagBit, BufferInfo, TextureInfo, TextureType, UniformBlock, ResolveMode, SampleCount, Color, ComparisonFunc } from '../../gfx';
|
||||
import { Mat4, Vec3, Vec4, assert, macro, cclegacy, Color as CoreColor, RecyclePool } from '../../core';
|
||||
import { AccessType, AttachmentType, CopyPair, LightInfo, LightingMode, MovePair, QueueHint, RenderCommonObjectPool, RenderCommonObjectPoolSettings, ResolvePair, ResourceDimension, ResourceFlags, ResourceResidency, SceneFlags, UpdateFrequency, UploadPair } from './types';
|
||||
import { ComputePass, CopyPass, MovePass, RasterPass, RasterSubpass, RenderData, RenderGraph, RenderGraphComponent, RenderGraphValue, RenderQueue, RenderSwapchain, ResourceDesc, ResourceGraph, ResourceGraphValue, ResourceStates, ResourceTraits, SceneData, Subpass, PersistentBuffer, RenderGraphObjectPool, RenderGraphObjectPoolSettings, CullingFlags, ManagedResource, ManagedBuffer } from './render-graph';
|
||||
import { ComputePassBuilder, ComputeQueueBuilder, BasicPipeline, PipelineBuilder, RenderQueueBuilder, RenderSubpassBuilder, PipelineType, BasicRenderPassBuilder, PipelineCapabilities, BasicMultisampleRenderPassBuilder, Setter, SceneBuilder } from './pipeline';
|
||||
@@ -36,7 +36,7 @@ import { Light, LightType } from '../../render-scene/scene/light';
|
||||
import { DescriptorSetData, LayoutGraphData } from './layout-graph';
|
||||
import { Executor } from './executor';
|
||||
import { RenderWindow } from '../../render-scene/core/render-window';
|
||||
import { MacroRecord, RenderScene } from '../../render-scene';
|
||||
import { MacroRecord } from '../../render-scene';
|
||||
import { GlobalDSManager } from '../global-descriptor-set-manager';
|
||||
import { getDefaultShadowTexture, supportsR32FloatTexture, supportsRGBA16HalfFloatTexture, UBOSkinning } from '../define';
|
||||
import { OS } from '../../../pal/system-info/enum-type';
|
||||
@@ -51,18 +51,13 @@ import { DebugViewCompositeType } from '../debug-view';
|
||||
import { buildReflectionProbePass } from './define';
|
||||
import { createGfxDescriptorSetsAndPipelines } from './layout-graph-utils';
|
||||
import { Root } from '../../root';
|
||||
import { CSMLayers, CSMShadowLayer } from '../shadow/csm-layers';
|
||||
import { Scene } from '../../scene-graph';
|
||||
import { Director } from '../../game';
|
||||
import { ReflectionProbeManager } from '../../3d';
|
||||
import { legacyCC } from '../../core/global-exports';
|
||||
import { WebSetter, setCameraUBOValues, setShadowUBOLightView, setShadowUBOView, setTextureUBOView } from './web-pipeline-types';
|
||||
|
||||
const _uboVec = new Vec4();
|
||||
const _uboVec3 = new Vec3();
|
||||
const _uboCol = new Color();
|
||||
const _matView = new Mat4();
|
||||
const _mulMatView = new Mat4();
|
||||
const uniformOffset = -1;
|
||||
const _samplerPointInfo = new SamplerInfo(
|
||||
Filter.POINT,
|
||||
Filter.POINT,
|
||||
@@ -148,495 +143,6 @@ class PipelinePool {
|
||||
}
|
||||
let pipelinePool: PipelinePool;
|
||||
let renderGraphPool: RenderGraphObjectPool;
|
||||
export class WebSetter implements Setter {
|
||||
constructor (data: RenderData, lg: LayoutGraphData) {
|
||||
this._data = data;
|
||||
this._lg = lg;
|
||||
}
|
||||
get name (): string {
|
||||
return '';
|
||||
}
|
||||
set name (name: string) {
|
||||
// noop
|
||||
}
|
||||
|
||||
public setMat4 (name: string, mat: Mat4, idx = 0): void {
|
||||
const info = this.getConstantInfo(name);
|
||||
Mat4.toArray(info.dataArr, mat, idx * 16);
|
||||
this._data.constants.set(info.constantID, info.dataArr);
|
||||
}
|
||||
|
||||
public setQuaternion (name: string, quat: Quat, idx = 0): void {
|
||||
const info = this.getConstantInfo(name);
|
||||
Quat.toArray(info.dataArr, quat, idx * 4);
|
||||
this._data.constants.set(info.constantID, info.dataArr);
|
||||
}
|
||||
public setColor (name: string, color: Color, idx = 0): void {
|
||||
const info = this.getConstantInfo(name);
|
||||
const currIdx = idx * 4;
|
||||
info.dataArr[0 + currIdx] = color.x;
|
||||
info.dataArr[1 + currIdx] = color.y;
|
||||
info.dataArr[2 + currIdx] = color.z;
|
||||
info.dataArr[3 + currIdx] = color.w;
|
||||
this._data.constants.set(info.constantID, info.dataArr);
|
||||
}
|
||||
|
||||
public setMathColor (name: string, color: CoreColor, idx = 0): void {
|
||||
const info = this.getConstantInfo(name);
|
||||
CoreColor.toArray(info.dataArr, color, idx * 4);
|
||||
this._data.constants.set(info.constantID, info.dataArr);
|
||||
}
|
||||
public getConstantInfo (name: string): { constantID: number, dataArr: Array<number>} {
|
||||
const constantID = this._lg.constantIndex.get(name)!;
|
||||
if (constantID === undefined) {
|
||||
throw new Error(`Constant with name ${name} not found.`);
|
||||
}
|
||||
const dataArr = this._data.constants.get(constantID)! || [];
|
||||
return { constantID, dataArr };
|
||||
}
|
||||
public setVec4 (name: string, vec: Vec4, idx = 0): void {
|
||||
const info = this.getConstantInfo(name);
|
||||
Vec4.toArray(info.dataArr, vec, idx * 4);
|
||||
this._data.constants.set(info.constantID, info.dataArr);
|
||||
}
|
||||
public setVec2 (name: string, vec: Vec2, idx = 0): void {
|
||||
const info = this.getConstantInfo(name);
|
||||
Vec2.toArray(info.dataArr, vec, idx * 2);
|
||||
this._data.constants.set(info.constantID, info.dataArr);
|
||||
}
|
||||
|
||||
public setFloat (name: string, v: number, idx = 0): void {
|
||||
const info = this.getConstantInfo(name);
|
||||
info.dataArr[0 + idx] = v;
|
||||
this._data.constants.set(info.constantID, info.dataArr);
|
||||
}
|
||||
public setArrayBuffer (name: string, arrayBuffer: ArrayBuffer): void {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
|
||||
public setBuffer (name: string, buffer: Buffer): void {
|
||||
const num = this._lg.attributeIndex.get(name)!;
|
||||
this._data.buffers.set(num, buffer);
|
||||
}
|
||||
public setTexture (name: string, texture: Texture): void {
|
||||
const num = this._lg.attributeIndex.get(name)!;
|
||||
this._data.textures.set(num, texture);
|
||||
}
|
||||
public setReadWriteBuffer (name: string, buffer: Buffer): void {
|
||||
const num = this._lg.attributeIndex.get(name)!;
|
||||
this._data.buffers.set(num, buffer);
|
||||
}
|
||||
public setReadWriteTexture (name: string, texture: Texture): void {
|
||||
const num = this._lg.attributeIndex.get(name)!;
|
||||
this._data.textures.set(num, texture);
|
||||
}
|
||||
public setSampler (name: string, sampler: Sampler): void {
|
||||
const num = this._lg.attributeIndex.get(name)!;
|
||||
this._data.samplers.set(num, sampler);
|
||||
}
|
||||
|
||||
public getParentLayout (): string {
|
||||
const director = cclegacy.director;
|
||||
const root = director.root;
|
||||
const pipeline = root.pipeline as WebPipeline;
|
||||
const parId = pipeline.renderGraph!.getParent(this._vertID);
|
||||
const layoutName = pipeline.renderGraph!.getLayout(parId);
|
||||
return layoutName;
|
||||
}
|
||||
|
||||
public getCurrentLayout (): string {
|
||||
const director = cclegacy.director;
|
||||
const root = director.root;
|
||||
const pipeline = root.pipeline as WebPipeline;
|
||||
const layoutName = pipeline.renderGraph!.getLayout(this._vertID);
|
||||
return layoutName;
|
||||
}
|
||||
|
||||
public setBuiltinCameraConstants (camera: Camera): void {
|
||||
const director = cclegacy.director;
|
||||
const root = director.root;
|
||||
const pipeline = root.pipeline as WebPipeline;
|
||||
const layoutName = this.getParentLayout();
|
||||
setCameraUBOValues(this, camera, pipeline.pipelineSceneData, camera.scene, layoutName);
|
||||
}
|
||||
public setBuiltinShadowMapConstants (light: Light, numLevels?: number): void {
|
||||
setShadowUBOView(this, null, this.getParentLayout());
|
||||
}
|
||||
public setBuiltinDirectionalLightFrustumConstants (camera: Camera, light: DirectionalLight, csmLevel = 0): void {
|
||||
setShadowUBOLightView(this, camera, light, csmLevel);
|
||||
}
|
||||
public setBuiltinSpotLightFrustumConstants (light: SpotLight): void {
|
||||
setShadowUBOLightView(this, null, light, 0);
|
||||
}
|
||||
public setBuiltinDirectionalLightConstants (light: DirectionalLight, camera: Camera): void {
|
||||
this.setBuiltinShadowMapConstants(light);
|
||||
}
|
||||
public setBuiltinSphereLightConstants (light: SphereLight, camera: Camera): void {
|
||||
const director = cclegacy.director;
|
||||
const pipeline = (director.root as Root).pipeline;
|
||||
const sceneData = pipeline.pipelineSceneData;
|
||||
_uboVec.set(light.position.x, light.position.y, light.position.z, LightType.SPHERE);
|
||||
this.setVec4('cc_lightPos', _uboVec);
|
||||
|
||||
_uboVec.set(light.size, light.range, 0.0, 0.0);
|
||||
this.setVec4('cc_lightSizeRangeAngle', _uboVec);
|
||||
|
||||
const isHDR = sceneData.isHDR;
|
||||
const lightMeterScale = 10000.0;
|
||||
_uboVec.set(light.color.x, light.color.y, light.color.z, 0);
|
||||
if (light.useColorTemperature) {
|
||||
const finalColor = light.finalColor;
|
||||
_uboVec.x = finalColor.x;
|
||||
_uboVec.y = finalColor.y;
|
||||
_uboVec.z = finalColor.z;
|
||||
}
|
||||
if (isHDR) {
|
||||
_uboVec.w = (light).luminance * camera.exposure * lightMeterScale;
|
||||
} else {
|
||||
_uboVec.w = (light).luminance;
|
||||
}
|
||||
this.setVec4('cc_lightColor', _uboVec);
|
||||
}
|
||||
public setBuiltinSpotLightConstants (light: SpotLight, camera: Camera): void {
|
||||
const director = cclegacy.director;
|
||||
const pipeline = (director.root as Root).pipeline;
|
||||
const sceneData = pipeline.pipelineSceneData;
|
||||
|
||||
const shadowInfo = sceneData.shadows;
|
||||
_uboVec.set(light.position.x, light.position.y, light.position.z, LightType.SPOT);
|
||||
this.setVec4('cc_lightPos', _uboVec);
|
||||
_uboVec.set(light.size, light.range, light.spotAngle, (shadowInfo.enabled && light.shadowEnabled && shadowInfo.type === ShadowType.ShadowMap) ? 1 : 0);
|
||||
this.setVec4('cc_lightSizeRangeAngle', _uboVec);
|
||||
_uboVec.set(light.direction.x, light.direction.y, light.direction.z, 0);
|
||||
this.setVec4('cc_lightDir', _uboVec);
|
||||
const isHDR = sceneData.isHDR;
|
||||
const lightMeterScale = 10000.0;
|
||||
_uboVec.set(light.color.x, light.color.y, light.color.z, 0);
|
||||
if (light.useColorTemperature) {
|
||||
const finalColor = light.finalColor;
|
||||
_uboVec.x = finalColor.x;
|
||||
_uboVec.y = finalColor.y;
|
||||
_uboVec.z = finalColor.z;
|
||||
}
|
||||
if (isHDR) {
|
||||
_uboVec.w = (light).luminance * camera.exposure * lightMeterScale;
|
||||
} else {
|
||||
_uboVec.w = (light).luminance;
|
||||
}
|
||||
this.setVec4('cc_lightColor', _uboVec);
|
||||
_uboVec.set(0, 0, 0, light.angleAttenuationStrength);
|
||||
this.setVec4('cc_lightBoundingSizeVS', _uboVec);
|
||||
}
|
||||
public setBuiltinPointLightConstants (light: PointLight, camera: Camera): void {
|
||||
const director = cclegacy.director;
|
||||
const pipeline = (director.root as Root).pipeline;
|
||||
const sceneData = pipeline.pipelineSceneData;
|
||||
_uboVec.set(light.position.x, light.position.y, light.position.z, LightType.POINT);
|
||||
this.setVec4('cc_lightPos', _uboVec);
|
||||
_uboVec.set(0.0, light.range, 0.0, 0.0);
|
||||
this.setVec4('cc_lightSizeRangeAngle', _uboVec);
|
||||
const isHDR = sceneData.isHDR;
|
||||
const lightMeterScale = 10000.0;
|
||||
if (light.useColorTemperature) {
|
||||
const finalColor = light.finalColor;
|
||||
_uboVec.x = finalColor.x;
|
||||
_uboVec.y = finalColor.y;
|
||||
_uboVec.z = finalColor.z;
|
||||
}
|
||||
if (isHDR) {
|
||||
_uboVec.w = (light).luminance * camera.exposure * lightMeterScale;
|
||||
} else {
|
||||
_uboVec.w = (light).luminance;
|
||||
}
|
||||
_uboVec.set(light.color.x, light.color.y, light.color.z, 0);
|
||||
this.setVec4('cc_lightColor', _uboVec);
|
||||
}
|
||||
public setBuiltinRangedDirectionalLightConstants (light: RangedDirectionalLight, camera: Camera): void {
|
||||
const director = cclegacy.director;
|
||||
const pipeline = (director.root as Root).pipeline;
|
||||
const sceneData = pipeline.pipelineSceneData;
|
||||
_uboVec.set(light.position.x, light.position.y, light.position.z, LightType.RANGED_DIRECTIONAL);
|
||||
this.setVec4('cc_lightPos', _uboVec);
|
||||
|
||||
_uboVec.set(light.right.x, light.right.y, light.right.z, 0.0);
|
||||
this.setVec4('cc_lightSizeRangeAngle', _uboVec);
|
||||
|
||||
_uboVec.set(light.direction.x, light.direction.y, light.direction.z, 0);
|
||||
this.setVec4('cc_lightDir', _uboVec);
|
||||
const scale = light.scale;
|
||||
_uboVec.set(scale.x * 0.5, scale.y * 0.5, scale.z * 0.5, 0);
|
||||
this.setVec4('cc_lightBoundingSizeVS', _uboVec);
|
||||
const isHDR = sceneData.isHDR;
|
||||
_uboVec.set(light.color.x, light.color.y, light.color.z, 0);
|
||||
if (light.useColorTemperature) {
|
||||
const finalColor = light.finalColor;
|
||||
_uboVec.x = finalColor.x;
|
||||
_uboVec.y = finalColor.y;
|
||||
_uboVec.z = finalColor.z;
|
||||
}
|
||||
if (isHDR) {
|
||||
_uboVec.w = light.illuminance * camera.exposure;
|
||||
} else {
|
||||
_uboVec.w = light.illuminance;
|
||||
}
|
||||
this.setVec4('cc_lightColor', _uboVec);
|
||||
}
|
||||
public hasSampler (name: string): boolean {
|
||||
const id = this._lg.constantIndex.get(name);
|
||||
if (id === undefined) {
|
||||
return false;
|
||||
}
|
||||
return this._data.samplers.has(id);
|
||||
}
|
||||
public hasTexture (name: string): boolean {
|
||||
const id = this._lg.constantIndex.get(name);
|
||||
if (id === undefined) {
|
||||
return false;
|
||||
}
|
||||
return this._data.textures.has(id);
|
||||
}
|
||||
public setCustomBehavior (name: string): void {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
|
||||
// protected
|
||||
protected _data: RenderData;
|
||||
protected _lg: LayoutGraphData;
|
||||
protected _vertID: number = -1;
|
||||
protected _currBlock;
|
||||
protected _currStage: string = '';
|
||||
protected _currFrequency: UpdateFrequency = UpdateFrequency.PER_PASS;
|
||||
protected _currCount;
|
||||
protected _currConstant: number[] = [];
|
||||
}
|
||||
|
||||
function setShadowUBOLightView (
|
||||
setter: WebSetter,
|
||||
camera: Camera | null,
|
||||
light: Light,
|
||||
csmLevel: number,
|
||||
layout = 'default',
|
||||
): void {
|
||||
const director = cclegacy.director;
|
||||
const pipeline = (director.root as Root).pipeline;
|
||||
const device = pipeline.device;
|
||||
const sceneData = pipeline.pipelineSceneData;
|
||||
|
||||
const shadowInfo = sceneData.shadows;
|
||||
if (shadowInfo.type === ShadowType.Planar) {
|
||||
return;
|
||||
}
|
||||
const csmLayers = sceneData.csmLayers;
|
||||
const packing = supportsR32FloatTexture(device) ? 0.0 : 1.0;
|
||||
const cap = pipeline.device.capabilities;
|
||||
// ShadowMap
|
||||
if (shadowInfo.enabled) {
|
||||
if (shadowInfo.type === ShadowType.ShadowMap) {
|
||||
// update CSM layers
|
||||
if (light && light.node && light.type === LightType.DIRECTIONAL) {
|
||||
csmLayers.update(sceneData, camera!);
|
||||
}
|
||||
}
|
||||
}
|
||||
switch (light.type) {
|
||||
case LightType.DIRECTIONAL: {
|
||||
const mainLight = light as DirectionalLight;
|
||||
if (shadowInfo.enabled && mainLight && mainLight.shadowEnabled) {
|
||||
if (shadowInfo.type === ShadowType.ShadowMap) {
|
||||
let near = 0.1;
|
||||
let far = 0;
|
||||
let matShadowView: Mat4;
|
||||
let matShadowProj: Mat4;
|
||||
let matShadowViewProj: Mat4;
|
||||
let levelCount = 0;
|
||||
if (mainLight.shadowFixedArea || mainLight.csmLevel === CSMLevel.LEVEL_1) {
|
||||
matShadowView = csmLayers.specialLayer.matShadowView;
|
||||
matShadowProj = csmLayers.specialLayer.matShadowProj;
|
||||
matShadowViewProj = csmLayers.specialLayer.matShadowViewProj;
|
||||
if (mainLight.shadowFixedArea) {
|
||||
near = mainLight.shadowNear;
|
||||
far = mainLight.shadowFar;
|
||||
levelCount = 0;
|
||||
} else {
|
||||
near = 0.1;
|
||||
far = csmLayers.specialLayer.shadowCameraFar;
|
||||
levelCount = 1;
|
||||
}
|
||||
_uboVec.set(LightType.DIRECTIONAL, packing, mainLight.shadowNormalBias, 0);
|
||||
setter.setVec4('cc_shadowLPNNInfo', _uboVec);
|
||||
} else {
|
||||
const layer = csmLayers.layers[csmLevel];
|
||||
matShadowView = layer.matShadowView;
|
||||
matShadowProj = layer.matShadowProj;
|
||||
matShadowViewProj = layer.matShadowViewProj;
|
||||
|
||||
near = layer.splitCameraNear;
|
||||
far = layer.splitCameraFar;
|
||||
levelCount = mainLight.csmLevel;
|
||||
}
|
||||
setter.setMat4('cc_matLightView', matShadowView);
|
||||
_uboVec.set(matShadowProj.m10, matShadowProj.m14, matShadowProj.m11, matShadowProj.m15);
|
||||
setter.setVec4('cc_shadowProjDepthInfo', _uboVec);
|
||||
_uboVec.set(matShadowProj.m00, matShadowProj.m05, 1.0 / matShadowProj.m00, 1.0 / matShadowProj.m05);
|
||||
setter.setVec4('cc_shadowProjInfo', _uboVec);
|
||||
setter.setMat4('cc_matLightViewProj', matShadowViewProj);
|
||||
_uboVec.set(near, far, 0, 1.0 - mainLight.shadowSaturation);
|
||||
setter.setVec4('cc_shadowNFLSInfo', _uboVec);
|
||||
_uboVec.set(LightType.DIRECTIONAL, packing, mainLight.shadowNormalBias, levelCount);
|
||||
setter.setVec4('cc_shadowLPNNInfo', _uboVec);
|
||||
_uboVec.set(shadowInfo.size.x, shadowInfo.size.y, mainLight.shadowPcf, mainLight.shadowBias);
|
||||
setter.setVec4('cc_shadowWHPBInfo', _uboVec);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case LightType.SPOT: {
|
||||
const spotLight = light as SpotLight;
|
||||
if (shadowInfo.enabled && spotLight && spotLight.shadowEnabled) {
|
||||
Mat4.invert(_matView, spotLight.node!.getWorldMatrix());
|
||||
setter.setMat4('cc_matLightView', _matView);
|
||||
Mat4.perspective(
|
||||
_mulMatView,
|
||||
spotLight.angle,
|
||||
1.0,
|
||||
0.001,
|
||||
spotLight.range,
|
||||
true,
|
||||
cap.clipSpaceMinZ,
|
||||
cap.clipSpaceSignY,
|
||||
0,
|
||||
);
|
||||
const matShadowInvProj: Mat4 = _mulMatView.clone().invert();
|
||||
const matShadowProj: Mat4 = _mulMatView.clone();
|
||||
|
||||
Mat4.multiply(_matView, _mulMatView, _matView);
|
||||
setter.setMat4('cc_matLightViewProj', _matView);
|
||||
_uboVec.set(0.01, (light as SpotLight).range, 0.0, 0.0);
|
||||
setter.setVec4('cc_shadowNFLSInfo', _uboVec);
|
||||
_uboVec.set(shadowInfo.size.x, shadowInfo.size.y, spotLight.shadowPcf, spotLight.shadowBias);
|
||||
setter.setVec4('cc_shadowWHPBInfo', _uboVec);
|
||||
_uboVec.set(LightType.SPOT, packing, spotLight.shadowNormalBias, 0.0);
|
||||
setter.setVec4('cc_shadowLPNNInfo', _uboVec);
|
||||
_uboVec.set(matShadowProj.m10, matShadowProj.m14, matShadowProj.m11, matShadowProj.m15);
|
||||
setter.setVec4('cc_shadowProjDepthInfo', _uboVec);
|
||||
_uboVec.set(matShadowInvProj.m10, matShadowInvProj.m14, matShadowInvProj.m11, matShadowInvProj.m15);
|
||||
setter.setVec4('cc_shadowInvProjDepthInfo', _uboVec);
|
||||
_uboVec.set(matShadowProj.m00, matShadowProj.m05, 1.0 / matShadowProj.m00, 1.0 / matShadowProj.m05);
|
||||
setter.setVec4('cc_shadowProjInfo', _uboVec);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case LightType.SPHERE: {
|
||||
_uboVec.set(shadowInfo.size.x, shadowInfo.size.y, 1.0, 0.0);
|
||||
setter.setVec4('cc_shadowWHPBInfo', _uboVec);
|
||||
_uboVec.set(LightType.SPHERE, packing, 0.0, 0.0);
|
||||
setter.setVec4('cc_shadowLPNNInfo', _uboVec);
|
||||
break;
|
||||
}
|
||||
case LightType.POINT: {
|
||||
_uboVec.set(shadowInfo.size.x, shadowInfo.size.y, 1.0, 0.0);
|
||||
setter.setVec4('cc_shadowWHPBInfo', _uboVec);
|
||||
_uboVec.set(LightType.POINT, packing, 0.0, 0.0);
|
||||
setter.setVec4('cc_shadowLPNNInfo', _uboVec);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
}
|
||||
_uboCol.set(shadowInfo.shadowColor.x, shadowInfo.shadowColor.y, shadowInfo.shadowColor.z, shadowInfo.shadowColor.w);
|
||||
setter.setColor('cc_shadowColor', _uboCol);
|
||||
}
|
||||
|
||||
function getPCFRadius (shadowInfo: Shadows, mainLight: DirectionalLight): number {
|
||||
const shadowMapSize = shadowInfo.size.x;
|
||||
switch (mainLight.shadowPcf) {
|
||||
case PCFType.HARD:
|
||||
return 0.0;
|
||||
case PCFType.SOFT:
|
||||
return 1.0 / (shadowMapSize * 0.5);
|
||||
case PCFType.SOFT_2X:
|
||||
return 2.0 / (shadowMapSize * 0.5);
|
||||
case PCFType.SOFT_4X:
|
||||
return 3.0 / (shadowMapSize * 0.5);
|
||||
default:
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
function setShadowUBOView (setter: WebSetter, camera: Camera | null, layout = 'default'): void {
|
||||
const director = cclegacy.director;
|
||||
const pipeline = director.root.pipeline;
|
||||
const device: Device = pipeline.device;
|
||||
const scene = director.getScene();
|
||||
const mainLight: DirectionalLight = camera && camera.scene ? camera.scene.mainLight : scene ? scene.renderScene.mainLight : null;
|
||||
const sceneData = pipeline.pipelineSceneData;
|
||||
const shadowInfo: Shadows = sceneData.shadows;
|
||||
const csmLayers: CSMLayers = sceneData.csmLayers;
|
||||
const csmSupported = sceneData.csmSupported;
|
||||
const packing = supportsR32FloatTexture(device) ? 0.0 : 1.0;
|
||||
if (mainLight && shadowInfo.enabled) {
|
||||
if (shadowInfo.type === ShadowType.ShadowMap) {
|
||||
if (mainLight.shadowEnabled) {
|
||||
if (mainLight.shadowFixedArea || mainLight.csmLevel === CSMLevel.LEVEL_1 || !csmSupported) {
|
||||
const matShadowView: Mat4 = csmLayers.specialLayer.matShadowView;
|
||||
const matShadowProj: Mat4 = csmLayers.specialLayer.matShadowProj;
|
||||
const matShadowViewProj: Mat4 = csmLayers.specialLayer.matShadowViewProj;
|
||||
const near: number = mainLight.shadowNear;
|
||||
const far: number = mainLight.shadowFar;
|
||||
setter.setMat4('cc_matLightView', matShadowView);
|
||||
_uboVec.set(matShadowProj.m10, matShadowProj.m14, matShadowProj.m11, matShadowProj.m15);
|
||||
setter.setVec4('cc_shadowProjDepthInfo', _uboVec);
|
||||
_uboVec.set(matShadowProj.m00, matShadowProj.m05, 1.0 / matShadowProj.m00, 1.0 / matShadowProj.m05);
|
||||
setter.setVec4('cc_shadowProjInfo', _uboVec);
|
||||
setter.setMat4('cc_matLightViewProj', matShadowViewProj);
|
||||
_uboVec.set(near, far, 0, 1.0 - mainLight.shadowSaturation);
|
||||
setter.setVec4('cc_shadowNFLSInfo', _uboVec);
|
||||
_uboVec.set(LightType.DIRECTIONAL, packing, mainLight.shadowNormalBias, 0);
|
||||
setter.setVec4('cc_shadowLPNNInfo', _uboVec);
|
||||
} else {
|
||||
const layerThreshold = getPCFRadius(shadowInfo, mainLight);
|
||||
for (let i = 0; i < mainLight.csmLevel; i++) {
|
||||
const layer: CSMShadowLayer = csmLayers.layers[i];
|
||||
const matShadowView: Mat4 = layer.matShadowView;
|
||||
_uboVec.set(matShadowView.m00, matShadowView.m04, matShadowView.m08, layerThreshold);
|
||||
setter.setVec4('cc_csmViewDir0', _uboVec, i);
|
||||
_uboVec.set(matShadowView.m01, matShadowView.m05, matShadowView.m09, layer.splitCameraNear);
|
||||
setter.setVec4('cc_csmViewDir1', _uboVec, i);
|
||||
_uboVec.set(matShadowView.m02, matShadowView.m06, matShadowView.m10, layer.splitCameraFar);
|
||||
setter.setVec4('cc_csmViewDir2', _uboVec, i);
|
||||
|
||||
const csmAtlas = layer.csmAtlas;
|
||||
setter.setVec4('cc_csmAtlas', csmAtlas, i);
|
||||
|
||||
const matShadowViewProj = layer.matShadowViewProj;
|
||||
setter.setMat4('cc_matCSMViewProj', matShadowViewProj, i);
|
||||
const matShadowProj = layer.matShadowProj;
|
||||
_uboVec.set(matShadowProj.m10, matShadowProj.m14, matShadowProj.m11, matShadowProj.m15);
|
||||
setter.setVec4('cc_csmProjDepthInfo', _uboVec, i);
|
||||
|
||||
_uboVec.set(matShadowProj.m00, matShadowProj.m05, 1.0 / matShadowProj.m00, 1.0 / matShadowProj.m05);
|
||||
setter.setVec4('cc_csmProjInfo', _uboVec, i);
|
||||
}
|
||||
_uboVec.set(mainLight.csmTransitionRange, 0, 0, 0);
|
||||
setter.setVec4('cc_csmSplitsInfo', _uboVec);
|
||||
_uboVec.set(0, 0, 0, 1.0 - mainLight.shadowSaturation);
|
||||
setter.setVec4('cc_shadowNFLSInfo', _uboVec);
|
||||
_uboVec.set(LightType.DIRECTIONAL, packing, mainLight.shadowNormalBias, mainLight.csmLevel);
|
||||
setter.setVec4('cc_shadowLPNNInfo', _uboVec);
|
||||
}
|
||||
_uboVec.set(shadowInfo.size.x, shadowInfo.size.y, mainLight.shadowPcf, mainLight.shadowBias);
|
||||
setter.setVec4('cc_shadowWHPBInfo', _uboVec);
|
||||
}
|
||||
} else {
|
||||
Vec3.normalize(_uboVec3, shadowInfo.normal);
|
||||
_uboVec.set(_uboVec3.x, _uboVec3.y, _uboVec3.z, -shadowInfo.distance);
|
||||
setter.setVec4('cc_planarNDInfo', _uboVec);
|
||||
|
||||
_uboVec.set(0, 0, 0, shadowInfo.planeBias);
|
||||
setter.setVec4('cc_shadowWHPBInfo', _uboVec);
|
||||
}
|
||||
setter.setMathColor('cc_shadowColor', shadowInfo.shadowColor);
|
||||
}
|
||||
}
|
||||
|
||||
function setComputeConstants (setter: WebSetter, layoutName: string): void {
|
||||
const director = cclegacy.director;
|
||||
@@ -645,129 +151,6 @@ function setComputeConstants (setter: WebSetter, layoutName: string): void {
|
||||
// setter.addConstant('CCConst', layoutName);
|
||||
}
|
||||
|
||||
function setCameraUBOValues (
|
||||
setter: WebSetter,
|
||||
camera: Readonly<Camera> | null,
|
||||
cfg: Readonly<PipelineSceneData>,
|
||||
scene: RenderScene | null,
|
||||
layoutName = 'default',
|
||||
): void {
|
||||
const director = cclegacy.director;
|
||||
const root = director.root;
|
||||
const pipeline = root.pipeline as WebPipeline;
|
||||
const shadowInfo = cfg.shadows;
|
||||
const skybox = cfg.skybox;
|
||||
const shadingScale = cfg.shadingScale;
|
||||
// Camera
|
||||
if (camera) {
|
||||
setter.setMat4('cc_matView', camera.matView);
|
||||
setter.setMat4('cc_matViewInv', camera.node.worldMatrix);
|
||||
setter.setMat4('cc_matProj', camera.matProj);
|
||||
setter.setMat4('cc_matProjInv', camera.matProjInv);
|
||||
setter.setMat4('cc_matViewProj', camera.matViewProj);
|
||||
setter.setMat4('cc_matViewProjInv', camera.matViewProjInv);
|
||||
_uboVec.set(camera.surfaceTransform, camera.cameraUsage, Math.cos(toRadian(skybox.getRotationAngle())), Math.sin(toRadian(skybox.getRotationAngle())));
|
||||
setter.setVec4('cc_surfaceTransform', _uboVec);
|
||||
_uboVec.set(camera.exposure, 1.0 / camera.exposure, cfg.isHDR ? 1.0 : 0.0, 1.0 / Camera.standardExposureValue);
|
||||
setter.setVec4('cc_exposure', _uboVec);
|
||||
}
|
||||
if (camera) { _uboVec.set(camera.position.x, camera.position.y, camera.position.z, pipeline.getCombineSignY()); } else { _uboVec.set(0, 0, 0, pipeline.getCombineSignY()); }
|
||||
setter.setVec4('cc_cameraPos', _uboVec);
|
||||
_uboVec.set(cfg.shadingScale, cfg.shadingScale, 1.0 / cfg.shadingScale, 1.0 / cfg.shadingScale);
|
||||
setter.setVec4('cc_screenScale', _uboVec);
|
||||
const mainLight = scene && scene.mainLight;
|
||||
if (mainLight) {
|
||||
const shadowEnable = (mainLight.shadowEnabled && shadowInfo.type === ShadowType.ShadowMap) ? 1.0 : 0.0;
|
||||
_uboVec.set(mainLight.direction.x, mainLight.direction.y, mainLight.direction.z, shadowEnable);
|
||||
setter.setVec4('cc_mainLitDir', _uboVec);
|
||||
let r = mainLight.color.x;
|
||||
let g = mainLight.color.y;
|
||||
let b = mainLight.color.z;
|
||||
if (mainLight.useColorTemperature) {
|
||||
r *= mainLight.colorTemperatureRGB.x;
|
||||
g *= mainLight.colorTemperatureRGB.y;
|
||||
b *= mainLight.colorTemperatureRGB.z;
|
||||
}
|
||||
let w = mainLight.illuminance;
|
||||
if (cfg.isHDR && camera) {
|
||||
w *= camera.exposure;
|
||||
}
|
||||
_uboVec.set(r, g, b, w);
|
||||
setter.setVec4('cc_mainLitColor', _uboVec);
|
||||
} else {
|
||||
_uboVec.set(0, 0, 1, 0);
|
||||
setter.setVec4('cc_mainLitDir', _uboVec);
|
||||
_uboVec.set(0, 0, 0, 0);
|
||||
setter.setVec4('cc_mainLitColor', _uboVec);
|
||||
}
|
||||
|
||||
const ambient = cfg.ambient;
|
||||
const skyColor = ambient.skyColor;
|
||||
if (cfg.isHDR) {
|
||||
skyColor.w = ambient.skyIllum * (camera ? camera.exposure : 1);
|
||||
} else {
|
||||
skyColor.w = ambient.skyIllum;
|
||||
}
|
||||
_uboVec.set(skyColor.x, skyColor.y, skyColor.z, skyColor.w);
|
||||
setter.setVec4('cc_ambientSky', _uboVec);
|
||||
_uboVec.set(ambient.groundAlbedo.x, ambient.groundAlbedo.y, ambient.groundAlbedo.z, skybox.envmap ? skybox.envmap?.mipmapLevel : 1.0);
|
||||
setter.setVec4('cc_ambientGround', _uboVec);
|
||||
const fog = cfg.fog;
|
||||
const colorTempRGB = fog.colorArray;
|
||||
_uboVec.set(colorTempRGB.x, colorTempRGB.y, colorTempRGB.z, colorTempRGB.z);
|
||||
setter.setVec4('cc_fogColor', _uboVec);
|
||||
_uboVec.set(fog.fogStart, fog.fogEnd, fog.fogDensity, 0.0);
|
||||
setter.setVec4('cc_fogBase', _uboVec);
|
||||
_uboVec.set(fog.fogTop, fog.fogRange, fog.fogAtten, 0.0);
|
||||
setter.setVec4('cc_fogAdd', _uboVec);
|
||||
if (camera) {
|
||||
_uboVec.set(camera.nearClip, camera.farClip, camera.getClipSpaceMinz(), 0.0);
|
||||
setter.setVec4('cc_nearFar', _uboVec);
|
||||
_uboVec.set(camera.viewport.x, camera.viewport.y, shadingScale * camera.window.width * camera.viewport.z, shadingScale * camera.window.height * camera.viewport.w);
|
||||
setter.setVec4('cc_viewPort', _uboVec);
|
||||
}
|
||||
}
|
||||
|
||||
function setTextureUBOView (setter: WebSetter, camera: Camera | null, cfg: Readonly<PipelineSceneData>, layout = 'default'): void {
|
||||
const skybox = cfg.skybox;
|
||||
const director = cclegacy.director;
|
||||
const root = director.root;
|
||||
const pipeline = root.pipeline as WebPipeline;
|
||||
if (skybox.reflectionMap) {
|
||||
const texture = skybox.reflectionMap.getGFXTexture()!;
|
||||
const sampler: Sampler = root.device.getSampler(skybox.reflectionMap.getSamplerInfo());
|
||||
setter.setTexture('cc_environment', texture);
|
||||
setter.setSampler('cc_environment', sampler);
|
||||
} else {
|
||||
const envmap = skybox.envmap ? skybox.envmap : builtinResMgr.get<TextureCube>('default-cube-texture');
|
||||
if (envmap) {
|
||||
const texture = envmap.getGFXTexture()!;
|
||||
const sampler: Sampler = root.device.getSampler(envmap.getSamplerInfo());
|
||||
setter.setTexture('cc_environment', texture);
|
||||
setter.setSampler('cc_environment', sampler);
|
||||
}
|
||||
}
|
||||
const diffuseMap = skybox.diffuseMap ? skybox.diffuseMap : builtinResMgr.get<TextureCube>('default-cube-texture');
|
||||
if (diffuseMap) {
|
||||
const texture = diffuseMap.getGFXTexture()!;
|
||||
const sampler: Sampler = root.device.getSampler(diffuseMap.getSamplerInfo());
|
||||
setter.setTexture('cc_diffuseMap', texture);
|
||||
setter.setSampler('cc_diffuseMap', sampler);
|
||||
}
|
||||
if (!setter.hasSampler('cc_shadowMap')) {
|
||||
setter.setSampler('cc_shadowMap', pipeline.defaultSampler);
|
||||
}
|
||||
if (!setter.hasTexture('cc_shadowMap')) {
|
||||
setter.setTexture('cc_shadowMap', pipeline.defaultShadowTexture);
|
||||
}
|
||||
if (!setter.hasSampler('cc_spotShadowMap')) {
|
||||
setter.setSampler('cc_spotShadowMap', pipeline.defaultSampler);
|
||||
}
|
||||
if (!setter.hasTexture('cc_spotShadowMap')) {
|
||||
setter.setTexture('cc_spotShadowMap', pipeline.defaultShadowTexture);
|
||||
}
|
||||
}
|
||||
|
||||
function getTextureType (dimension: ResourceDimension, arraySize: number): TextureType {
|
||||
switch (dimension) {
|
||||
case ResourceDimension.TEXTURE1D:
|
||||
@@ -1807,6 +1190,7 @@ export class WebPipeline implements BasicPipeline {
|
||||
this._globalDescriptorSetLayout = this._globalDescSetData.descriptorSetLayout;
|
||||
this._globalDescriptorSetInfo = new DescriptorSetInfo(this._globalDescriptorSetLayout!);
|
||||
this._globalDescriptorSet = this._device.createDescriptorSet(this._globalDescriptorSetInfo);
|
||||
this._profilerDescriptorSet = this._device.createDescriptorSet(this._globalDescriptorSetInfo);
|
||||
this._globalDSManager.globalDescriptorSet = this.globalDescriptorSet;
|
||||
this._compileMaterial();
|
||||
this.setMacroBool('CC_USE_HDR', this._pipelineSceneData.isHDR);
|
||||
@@ -1879,6 +1263,9 @@ export class WebPipeline implements BasicPipeline {
|
||||
public get descriptorSet (): DescriptorSet {
|
||||
return this._globalDSManager.globalDescriptorSet;
|
||||
}
|
||||
public get profilerDescriptorSet (): DescriptorSet {
|
||||
return this._profilerDescriptorSet!;
|
||||
}
|
||||
public get globalDescriptorSet (): DescriptorSet {
|
||||
return this._globalDescriptorSet!;
|
||||
}
|
||||
@@ -2283,6 +1670,7 @@ export class WebPipeline implements BasicPipeline {
|
||||
private _globalDescriptorSet: DescriptorSet | null = null;
|
||||
private _globalDescriptorSetInfo: DescriptorSetInfo | null = null;
|
||||
private _globalDescriptorSetLayout: DescriptorSetLayout | null = null;
|
||||
private _profilerDescriptorSet: DescriptorSet | null = null;
|
||||
private readonly _macros: MacroRecord = {};
|
||||
private readonly _pipelineSceneData: PipelineSceneData = new PipelineSceneData();
|
||||
private _constantMacros = '';
|
||||
|
||||
Reference in New Issue
Block a user