forked from Scthe/nanite-webgpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.ts
314 lines (273 loc) · 10.7 KB
/
renderer.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import { Mat4, mat4 } from 'wgpu-matrix';
import { RenderUniformsBuffer } from './passes/renderUniformsBuffer.ts';
import {
Dimensions,
createCameraProjectionMat,
debounce,
getViewProjectionMatrix,
} from './utils/index.ts';
import Input from './sys_web/input.ts';
import {
CONFIG,
DEPTH_FORMAT,
HDR_RENDER_TEX_FORMAT,
isSoftwareRasterizerEnabled,
} from './constants.ts';
import { DrawNanitesPass } from './passes/naniteCpu/drawNanitesPass.ts';
import { Camera } from './camera.ts';
import { PassCtx } from './passes/passCtx.ts';
import { DbgMeshoptimizerPass } from './passes/debug/dbgMeshoptimizerPass.ts';
import { DbgMeshoptimizerMeshletsPass } from './passes/debug/dbgMeshoptimizerMeshletsPass.ts';
import { RasterizeHwPass } from './passes/rasterizeHw/rasterizeHwPass.ts';
import { CullMeshletsPass } from './passes/cullMeshlets/cullMeshletsPass.ts';
import { GpuProfiler } from './gpuProfiler.ts';
import { Scene } from './scene/scene.ts';
import { Frustum } from './utils/frustum.ts';
import {
assertIsGPUTextureView,
ensureIntegerDimensions,
} from './utils/webgpu.ts';
import { DepthPyramidPass } from './passes/depthPyramid/depthPyramidPass.ts';
import { DepthPyramidDebugDrawPass } from './passes/depthPyramid/depthPyramidDebugDrawPass.ts';
import { CullInstancesPass } from './passes/cullInstances/cullInstancesPass.ts';
import { NaniteBillboardPass } from './passes/naniteBillboard/naniteBillboardPass.ts';
import { PresentPass } from './passes/presentPass/presentPass.ts';
import { RasterizeSwPass } from './passes/rasterizeSw/rasterizeSwPass.ts';
import { RasterizeCombine } from './passes/rasterizeCombine/rasterizeCombine.ts';
import { DrawGroundPass } from './passes/drawGroundPass.ts';
export class Renderer {
private readonly renderUniformBuffer: RenderUniformsBuffer;
public readonly cameraCtrl: Camera;
private readonly cameraFrustum: Frustum = new Frustum();
private projectionMat: Mat4;
private readonly _viewMatrix = mat4.identity(); // cached to prevent allocs.
public readonly viewportSize: Dimensions = { width: 0, height: 0 };
private frameIdx = 0;
// render target textures
private depthTexture: GPUTexture = undefined!; // see this.handleViewportResize()
private depthTextureView: GPUTextureView = undefined!; // see this.handleViewportResize()
private hdrRenderTexture: GPUTexture = undefined!; // see this.handleViewportResize()
private hdrRenderTextureView: GPUTextureView = undefined!; // see this.handleViewportResize()
// passes
private readonly drawMeshPass: DrawNanitesPass;
private readonly rasterizeHwPass: RasterizeHwPass;
private readonly rasterizeSwPass: RasterizeSwPass;
private readonly cullMeshletsPass: CullMeshletsPass;
private readonly cullInstancesPass: CullInstancesPass;
private readonly naniteBillboardPass: NaniteBillboardPass;
private readonly rasterizeCombine: RasterizeCombine;
private readonly drawGroundPass: DrawGroundPass;
private readonly presentPass: PresentPass;
// depth pyramid
private readonly depthPyramidPass: DepthPyramidPass;
private readonly depthPyramidDebugDrawPass: DepthPyramidDebugDrawPass;
// debug
private readonly dbgMeshoptimizerPass: DbgMeshoptimizerPass;
private readonly dbgMeshoptimizerMeshletsPass: DbgMeshoptimizerMeshletsPass;
constructor(
private readonly device: GPUDevice,
viewportSize: Dimensions,
preferredCanvasFormat: GPUTextureFormat,
private readonly profiler?: GpuProfiler
) {
this.renderUniformBuffer = new RenderUniformsBuffer(device);
this.drawMeshPass = new DrawNanitesPass(device, HDR_RENDER_TEX_FORMAT);
this.rasterizeHwPass = new RasterizeHwPass(device, HDR_RENDER_TEX_FORMAT);
this.rasterizeSwPass = new RasterizeSwPass(device);
this.cullMeshletsPass = new CullMeshletsPass(device);
this.cullInstancesPass = new CullInstancesPass(device);
this.naniteBillboardPass = new NaniteBillboardPass(
device,
HDR_RENDER_TEX_FORMAT
);
this.rasterizeCombine = new RasterizeCombine(device, HDR_RENDER_TEX_FORMAT);
this.depthPyramidPass = new DepthPyramidPass(device);
this.depthPyramidDebugDrawPass = new DepthPyramidDebugDrawPass(
device,
HDR_RENDER_TEX_FORMAT
);
this.drawGroundPass = new DrawGroundPass(HDR_RENDER_TEX_FORMAT);
this.presentPass = new PresentPass(device, preferredCanvasFormat);
// geometry debug passes
this.dbgMeshoptimizerPass = new DbgMeshoptimizerPass(
device,
HDR_RENDER_TEX_FORMAT,
this.renderUniformBuffer
);
this.dbgMeshoptimizerMeshletsPass = new DbgMeshoptimizerMeshletsPass(
device,
HDR_RENDER_TEX_FORMAT,
this.renderUniformBuffer
);
this.cameraCtrl = new Camera();
this.projectionMat = createCameraProjectionMat(viewportSize);
this.handleViewportResize(viewportSize);
}
updateCamera(deltaTime: number, input: Input): Mat4 {
this.cameraCtrl.update(deltaTime, input);
}
cmdRender(
cmdBuf: GPUCommandEncoder,
scene: Scene,
screenTexture: GPUTextureView
) {
assertIsGPUTextureView(screenTexture);
const viewMatrix = this.cameraCtrl.viewMatrix;
const vpMatrix = getViewProjectionMatrix(
viewMatrix,
this.projectionMat,
this._viewMatrix
);
this.cameraFrustum.update(vpMatrix);
const [_depthPyramidTex, depthPyramidTexView] =
this.depthPyramidPass.verifyResultTexture(
this.device,
this.depthTexture,
this.depthTextureView
);
const ctx: PassCtx = {
frameIdx: this.frameIdx,
cmdBuf,
viewport: this.viewportSize,
scene,
hdrRenderTexture: this.hdrRenderTextureView,
rasterizerSwResult: this.rasterizeSwPass.resultBuffer,
softwareRasterizerEnabled: isSoftwareRasterizerEnabled(),
device: this.device,
profiler: this.profiler,
viewMatrix,
vpMatrix,
projMatrix: this.projectionMat,
cameraFrustum: this.cameraFrustum,
cameraPositionWorldSpace: this.cameraCtrl.positionWorldSpace,
depthTexture: this.depthTextureView,
prevFrameDepthPyramidTexture: depthPyramidTexView,
globalUniforms: this.renderUniformBuffer,
depthPyramidSampler: this.depthPyramidPass.depthSampler,
};
this.renderUniformBuffer.update(ctx);
if (CONFIG.displayMode === 'dbg-lod') {
this.dbgMeshoptimizerPass.draw(ctx);
} else if (
CONFIG.displayMode === 'dbg-lod-meshlets' ||
CONFIG.displayMode === 'dbg-nanite-meshlets'
) {
this.dbgMeshoptimizerMeshletsPass.draw(ctx);
} else {
// draw nanite - calc visibility either CPU or GPU
if (CONFIG.nanite.render.naniteDevice === 'gpu') {
this.cmdDrawNanite_GPU(ctx);
} else {
this.cmdDrawNanite_CPU(ctx);
}
}
this.presentPass.cmdDraw(ctx, screenTexture);
this.frameIdx += 1;
}
private cmdDrawNanite_CPU(ctx: PassCtx) {
const { naniteObjects } = ctx.scene;
this.drawMeshPass.initFrameStats();
// draw objects
for (let i = 0; i < naniteObjects.length; i++) {
const naniteObject = naniteObjects[i];
const loadOp: GPULoadOp = i == 0 ? 'clear' : 'load';
this.drawMeshPass.draw(ctx, naniteObject, loadOp);
}
// draw ground
if (CONFIG.drawGround) {
this.drawGroundPass.cmdDrawGround(ctx, 'load');
}
this.drawMeshPass.uploadFrameStats(ctx);
}
private cmdDrawNanite_GPU(ctx: PassCtx) {
const { naniteObjects } = ctx.scene;
const softwareRasterizeEnabled = ctx.softwareRasterizerEnabled;
if (softwareRasterizeEnabled) {
this.rasterizeSwPass.clearFramebuffer(ctx);
}
// draw objects
for (let i = 0; i < naniteObjects.length; i++) {
const naniteObject = naniteObjects[i];
const loadOp: GPULoadOp = i == 0 ? 'clear' : 'load';
if (!CONFIG.nanite.render.freezeGPU_Visibilty) {
if (CONFIG.cullingInstances.enabled) {
this.cullInstancesPass.cmdCullInstances(ctx, naniteObject);
}
this.cullMeshletsPass.cmdCullMeshlets(ctx, naniteObject);
}
// draw: hardware
this.rasterizeHwPass.cmdHardwareRasterize(ctx, naniteObject, loadOp);
// draw: software
if (softwareRasterizeEnabled) {
this.rasterizeSwPass.cmdSoftwareRasterize(ctx, naniteObject);
}
// draw: impostors
if (CONFIG.cullingInstances.enabled) {
this.naniteBillboardPass.cmdRenderBillboards(ctx, naniteObject, 'load');
}
}
// combine hardware + software rasterizer results
if (softwareRasterizeEnabled) {
this.rasterizeCombine.cmdCombineRasterResults(ctx);
}
// draw ground
if (CONFIG.drawGround) {
this.drawGroundPass.cmdDrawGround(ctx, 'load');
}
// depth pyramid
const pyramidOk = this.depthPyramidPass.cmdCreateDepthPyramid(
ctx,
this.depthTexture,
this.depthTextureView
);
CONFIG.nanite.render.hasValidDepthPyramid = pyramidOk;
if (CONFIG.displayMode === 'dbg-depth-pyramid') {
this.depthPyramidDebugDrawPass.cmdDraw(ctx);
}
}
private handleViewportResize = (viewportSize: Dimensions) => {
viewportSize = ensureIntegerDimensions(viewportSize);
console.log(`Viewport resize`, viewportSize);
CONFIG.nanite.render.hasValidDepthPyramid = false;
this.viewportSize.width = viewportSize.width;
this.viewportSize.height = viewportSize.height;
this.projectionMat = createCameraProjectionMat(viewportSize);
if (this.depthTexture) {
this.depthTexture.destroy();
}
if (this.hdrRenderTexture) {
this.hdrRenderTexture.destroy();
}
const vpStr = `${viewportSize.width}x${viewportSize.height}`;
this.hdrRenderTexture = this.device.createTexture({
label: `hdr-texture-${vpStr}`,
size: [viewportSize.width, viewportSize.height],
format: HDR_RENDER_TEX_FORMAT,
usage:
GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
});
this.hdrRenderTextureView = this.hdrRenderTexture.createView();
this.depthTexture = this.device.createTexture({
label: `depth-texture-${vpStr}`,
size: [viewportSize.width, viewportSize.height],
format: DEPTH_FORMAT,
usage:
GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
});
this.depthTextureView = this.depthTexture.createView();
// reset bindings that used texture
this.depthPyramidDebugDrawPass.onViewportResize();
this.cullMeshletsPass.onViewportResize();
this.cullInstancesPass.onViewportResize();
this.rasterizeSwPass.onViewportResize(this.device, viewportSize);
this.depthPyramidPass.verifyResultTexture(
this.device,
this.depthTexture,
this.depthTextureView,
true
);
this.rasterizeCombine.onViewportResize();
this.presentPass.onViewportResize();
};
onCanvasResize = debounce(this.handleViewportResize, 500);
}