forked from cocos/cocos-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshadow-stage.ts
169 lines (147 loc) · 6.78 KB
/
shadow-stage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
https://www.cocos.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import { ccclass } from 'cc.decorator';
import { Color, Rect, Framebuffer, DescriptorSet } from '../../gfx';
import { IRenderStageInfo, RenderStage } from '../render-stage';
import { ForwardStagePriority } from '../enum';
import { RenderShadowMapBatchedQueue } from '../render-shadow-map-batched-queue';
import { ForwardPipeline } from '../forward/forward-pipeline';
import { SetIndex } from '../define';
import { Light, LightType } from '../../render-scene/scene/light';
import { ShadowFlow } from './shadow-flow';
import { Camera, CSMLevel, DirectionalLight } from '../../render-scene/scene';
const colors: Color[] = [new Color(1, 1, 1, 1)];
/**
* @en Shadow map render stage
* @zh 阴影渲染阶段。
*/
@ccclass('ShadowStage')
export class ShadowStage extends RenderStage {
/**
* @en A common initialization info for shadow map render stage
* @zh 一个通用的 ShadowStage 的初始化信息对象
*/
public static initInfo: IRenderStageInfo = {
name: 'ShadowStage',
priority: ForwardStagePriority.FORWARD,
tag: 0,
};
/**
* @en Sets the render shadow map info
* @zh 设置阴影渲染信息
* @param light
* @param shadowFrameBuffer
* @param level 层级
*/
public setUsage (globalDS: DescriptorSet, light: Light, shadowFrameBuffer: Framebuffer, level = 0) {
this._globalDS = globalDS;
this._light = light;
this._shadowFrameBuffer = shadowFrameBuffer;
this._level = level;
}
private _additiveShadowQueue!: RenderShadowMapBatchedQueue;
private _shadowFrameBuffer: Framebuffer | null = null;
private _renderArea = new Rect();
private _light: Light | null = null;
private _globalDS: DescriptorSet | null = null;
private _level = 0;
private _isShadowMapCleared = false;
public destroy () {
this._shadowFrameBuffer = null;
this._globalDS = null;
this._light = null;
this._additiveShadowQueue?.clear();
}
public clearFramebuffer (camera: Camera) {
if (!this._light || !this._shadowFrameBuffer || this._isShadowMapCleared) { return; }
colors[0].w = camera.clearColor.w;
const pipeline = this._pipeline as ForwardPipeline;
const pipelineSceneData = pipeline.pipelineSceneData;
const shadingScale = pipelineSceneData.shadingScale;
const shadowInfo = pipelineSceneData.shadows;
const vp = camera.viewport;
const shadowMapSize = shadowInfo.size;
this._renderArea.x = vp.x * shadowMapSize.x;
this._renderArea.y = vp.y * shadowMapSize.y;
this._renderArea.width = vp.width * shadowMapSize.x * shadingScale;
this._renderArea.height = vp.height * shadowMapSize.y * shadingScale;
const cmdBuff = pipeline.commandBuffers[0];
const renderPass = this._shadowFrameBuffer.renderPass;
cmdBuff.beginRenderPass(renderPass, this._shadowFrameBuffer, this._renderArea,
colors, camera.clearDepth, camera.clearStencil);
cmdBuff.endRenderPass();
this._isShadowMapCleared = true;
}
public render (camera: Camera) {
const pipeline = this._pipeline;
const pipelineSceneData = pipeline.pipelineSceneData;
const shadowInfo = pipelineSceneData.shadows;
const descriptorSet = this._globalDS!;
const cmdBuff = pipeline.commandBuffers[0];
const level = this._level;
const device = pipeline.device;
if (!this._light || !this._shadowFrameBuffer) { return; }
this._pipeline.pipelineUBO.updateShadowUBOLight(descriptorSet, this._light, level);
this._additiveShadowQueue.gatherLightPasses(camera, this._light, cmdBuff, level);
const shadowMapSize = shadowInfo.size;
switch (this._light.type) {
case LightType.DIRECTIONAL: {
const mainLight = this._light as DirectionalLight;
if (mainLight.shadowFixedArea || mainLight.csmLevel === CSMLevel.LEVEL_1 || !pipelineSceneData.csmSupported) {
this._renderArea.x = 0;
this._renderArea.y = 0;
this._renderArea.width = shadowMapSize.x;
this._renderArea.height = shadowMapSize.y;
} else {
const screenSpaceSignY = device.capabilities.screenSpaceSignY;
this._renderArea.x = level % 2 * 0.5 * shadowMapSize.x;
if (screenSpaceSignY > 0.0) {
this._renderArea.y = (1 - Math.floor(level / 2)) * 0.5 * shadowMapSize.y;
} else {
this._renderArea.y = Math.floor(level / 2) * 0.5 * shadowMapSize.y;
}
this._renderArea.width = 0.5 * shadowMapSize.x;
this._renderArea.height = 0.5 * shadowMapSize.y;
}
break;
}
case LightType.SPOT: {
this._renderArea.x = 0;
this._renderArea.y = 0;
this._renderArea.width = shadowMapSize.x;
this._renderArea.height = shadowMapSize.y;
break;
}
default:
}
const renderPass = this._shadowFrameBuffer.renderPass;
cmdBuff.beginRenderPass(renderPass, this._shadowFrameBuffer, this._renderArea,
colors, camera.clearDepth, camera.clearStencil);
cmdBuff.bindDescriptorSet(SetIndex.GLOBAL, descriptorSet);
this._additiveShadowQueue.recordCommandBuffer(device, renderPass, cmdBuff);
cmdBuff.endRenderPass();
this._isShadowMapCleared = false;
}
public activate (pipeline: ForwardPipeline, flow: ShadowFlow) {
super.activate(pipeline, flow);
this._additiveShadowQueue = new RenderShadowMapBatchedQueue(pipeline);
this._isShadowMapCleared = false;
}
}