Skip to content

Commit 7e658d9

Browse files
authored
FrameGraph: better support for autoCalcDepthBounds in cascaded shadow generator task (#16734)
Improved fix for #16710: * The depth texture to use for min/max reduction is now an input of the CSM task. This means that this depth texture can be shared by multiple tasks. In the previous PR, a new depth texture was always generated internally when `autoCalcDepthBounds` was **true** * The textures used for min/max reduction are created as part of the frame graph, which means that these textures can be optimized and reused by the graph I have also added support for “normalized view depth” as an output of the geometry renderer task, as this is the most optimal type for min/max depth reduction when used in CSM.
1 parent 09db23d commit 7e658d9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+758
-225
lines changed

packages/dev/core/src/Engines/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,11 @@ export class Constants {
744744
*/
745745
public static readonly PREPASS_ALBEDO_TEXTURE_TYPE = 12;
746746

747+
/**
748+
* Constant used to retrieve normalized camera view depth geometry texture
749+
*/
750+
public static readonly PREPASS_NORMALIZED_VIEW_DEPTH_TEXTURE_TYPE = 13;
751+
747752
/** Flag to create a readable buffer (the buffer can be the source of a copy) */
748753
public static readonly BUFFER_CREATIONFLAG_READ = 1;
749754
/** Flag to create a writable buffer (the buffer can be the destination of a copy) */

packages/dev/core/src/FrameGraph/Node/Blocks/Rendering/baseShadowGeneratorBlock.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ export class NodeRenderGraphBaseShadowGeneratorBlock extends NodeRenderGraphBloc
3939
this.registerInput("light", NodeRenderGraphBlockConnectionPointTypes.ShadowLight);
4040
this.registerInput("objects", NodeRenderGraphBlockConnectionPointTypes.ObjectList);
4141
this.registerInput("camera", NodeRenderGraphBlockConnectionPointTypes.Camera);
42-
this._addDependenciesInput();
42+
}
4343

44+
protected _finalizeInputOutputRegistering() {
45+
this._addDependenciesInput();
4446
this.registerOutput("generator", NodeRenderGraphBlockConnectionPointTypes.ShadowGenerator);
4547
this.registerOutput("output", NodeRenderGraphBlockConnectionPointTypes.Texture);
4648
}

packages/dev/core/src/FrameGraph/Node/Blocks/Rendering/csmShadowGeneratorBlock.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// eslint-disable-next-line import/no-internal-modules
2-
import type { Scene, FrameGraph } from "core/index";
2+
import type { Scene, FrameGraph, NodeRenderGraphConnectionPoint, FrameGraphTextureHandle, NodeRenderGraphBuildState } from "core/index";
33
import { NodeRenderGraphBaseShadowGeneratorBlock } from "./baseShadowGeneratorBlock";
44
import { RegisterClass } from "../../../../Misc/typeStore";
55
import { editableInPropertyPage, PropertyTypeForEdition } from "../../../../Decorators/nodeDecorator";
66
import { FrameGraphCascadedShadowGeneratorTask } from "../../../Tasks/Rendering/csmShadowGeneratorTask";
7+
import { NodeRenderGraphBlockConnectionPointTypes } from "../../Types/nodeRenderGraphTypes";
8+
import { DepthTextureType } from "core/Misc/thinMinMaxReducer";
79

810
/**
911
* Block that generates shadows through a shadow generator
@@ -27,6 +29,16 @@ export class NodeRenderGraphCascadedShadowGeneratorBlock extends NodeRenderGraph
2729
public constructor(name: string, frameGraph: FrameGraph, scene: Scene) {
2830
super(name, frameGraph, scene);
2931

32+
this.registerInput("geomDepth", NodeRenderGraphBlockConnectionPointTypes.AutoDetect, true);
33+
34+
this.geomDepth.addExcludedConnectionPointFromAllowedTypes(
35+
NodeRenderGraphBlockConnectionPointTypes.TextureNormalizedViewDepth |
36+
NodeRenderGraphBlockConnectionPointTypes.TextureViewDepth |
37+
NodeRenderGraphBlockConnectionPointTypes.TextureScreenDepth
38+
);
39+
40+
this._finalizeInputOutputRegistering();
41+
3042
this._frameGraphTask = new FrameGraphCascadedShadowGeneratorTask(this.name, frameGraph, scene);
3143
}
3244

@@ -134,6 +146,33 @@ export class NodeRenderGraphCascadedShadowGeneratorBlock extends NodeRenderGraph
134146
return "NodeRenderGraphCascadedShadowGeneratorBlock";
135147
}
136148

149+
/**
150+
* Gets the geometry (view / normalized view) depth component
151+
*/
152+
public get geomDepth(): NodeRenderGraphConnectionPoint {
153+
return this._inputs[3];
154+
}
155+
156+
protected override _buildBlock(state: NodeRenderGraphBuildState) {
157+
super._buildBlock(state);
158+
159+
this._frameGraphTask.depthTexture = this.geomDepth.connectedPoint?.value as FrameGraphTextureHandle;
160+
161+
if (this.geomDepth.connectedPoint) {
162+
switch (this.geomDepth.connectedPoint.type) {
163+
case NodeRenderGraphBlockConnectionPointTypes.TextureScreenDepth:
164+
this._frameGraphTask.depthTextureType = DepthTextureType.ScreenDepth;
165+
break;
166+
case NodeRenderGraphBlockConnectionPointTypes.TextureNormalizedViewDepth:
167+
this._frameGraphTask.depthTextureType = DepthTextureType.NormalizedViewDepth;
168+
break;
169+
case NodeRenderGraphBlockConnectionPointTypes.TextureViewDepth:
170+
this._frameGraphTask.depthTextureType = DepthTextureType.ViewDepth;
171+
break;
172+
}
173+
}
174+
}
175+
137176
protected override _dumpPropertiesCode() {
138177
const codes: string[] = [];
139178
codes.push(`${this._codeVariableName}.numCascades = ${this.numCascades};`);

packages/dev/core/src/FrameGraph/Node/Blocks/Rendering/geometryRendererBlock.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export class NodeRenderGraphGeometryRendererBlock extends NodeRenderGraphBlock {
3939

4040
this.registerOutput("outputDepth", NodeRenderGraphBlockConnectionPointTypes.BasedOnInput);
4141
this.registerOutput("geomViewDepth", NodeRenderGraphBlockConnectionPointTypes.TextureViewDepth);
42+
this.registerOutput("geomNormViewDepth", NodeRenderGraphBlockConnectionPointTypes.TextureNormalizedViewDepth);
4243
this.registerOutput("geomScreenDepth", NodeRenderGraphBlockConnectionPointTypes.TextureScreenDepth);
4344
this.registerOutput("geomViewNormal", NodeRenderGraphBlockConnectionPointTypes.TextureViewNormal);
4445
this.registerOutput("geomWorldNormal", NodeRenderGraphBlockConnectionPointTypes.TextureWorldNormal);
@@ -177,6 +178,13 @@ export class NodeRenderGraphGeometryRendererBlock extends NodeRenderGraphBlock {
177178
@editableInPropertyPage("View depth type", PropertyTypeForEdition.TextureType, "GEOMETRY BUFFERS")
178179
public viewDepthType = Constants.TEXTURETYPE_FLOAT;
179180

181+
// Normalized view depth
182+
@editableInPropertyPage("Normalized view depth format", PropertyTypeForEdition.TextureFormat, "GEOMETRY BUFFERS")
183+
public normalizedViewDepthFormat = Constants.TEXTUREFORMAT_RED;
184+
185+
@editableInPropertyPage("Normalized view depth type", PropertyTypeForEdition.TextureType, "GEOMETRY BUFFERS")
186+
public normalizedViewDepthType = Constants.TEXTURETYPE_HALF_FLOAT;
187+
180188
// Screen depth
181189
@editableInPropertyPage("Screen depth format", PropertyTypeForEdition.TextureFormat, "GEOMETRY BUFFERS")
182190
public screenDepthFormat = Constants.TEXTUREFORMAT_RED;
@@ -283,74 +291,82 @@ export class NodeRenderGraphGeometryRendererBlock extends NodeRenderGraphBlock {
283291
return this._outputs[1];
284292
}
285293

294+
/**
295+
* Gets the geometry normalized view depth component
296+
*/
297+
public get geomNormViewDepth(): NodeRenderGraphConnectionPoint {
298+
return this._outputs[2];
299+
}
300+
286301
/**
287302
* Gets the geometry screen depth component
288303
*/
289304
public get geomScreenDepth(): NodeRenderGraphConnectionPoint {
290-
return this._outputs[2];
305+
return this._outputs[3];
291306
}
292307

293308
/**
294309
* Gets the geometry view normal component
295310
*/
296311
public get geomViewNormal(): NodeRenderGraphConnectionPoint {
297-
return this._outputs[3];
312+
return this._outputs[4];
298313
}
299314

300315
/**
301316
* Gets the world geometry normal component
302317
*/
303318
public get geomWorldNormal(): NodeRenderGraphConnectionPoint {
304-
return this._outputs[4];
319+
return this._outputs[5];
305320
}
306321

307322
/**
308323
* Gets the geometry local position component
309324
*/
310325
public get geomLocalPosition(): NodeRenderGraphConnectionPoint {
311-
return this._outputs[5];
326+
return this._outputs[6];
312327
}
313328

314329
/**
315330
* Gets the geometry world position component
316331
*/
317332
public get geomWorldPosition(): NodeRenderGraphConnectionPoint {
318-
return this._outputs[6];
333+
return this._outputs[7];
319334
}
320335

321336
/**
322337
* Gets the geometry albedo component
323338
*/
324339
public get geomAlbedo(): NodeRenderGraphConnectionPoint {
325-
return this._outputs[7];
340+
return this._outputs[8];
326341
}
327342

328343
/**
329344
* Gets the geometry reflectivity component
330345
*/
331346
public get geomReflectivity(): NodeRenderGraphConnectionPoint {
332-
return this._outputs[8];
347+
return this._outputs[9];
333348
}
334349

335350
/**
336351
* Gets the geometry velocity component
337352
*/
338353
public get geomVelocity(): NodeRenderGraphConnectionPoint {
339-
return this._outputs[9];
354+
return this._outputs[10];
340355
}
341356

342357
/**
343358
* Gets the geometry linear velocity component
344359
*/
345360
public get geomLinearVelocity(): NodeRenderGraphConnectionPoint {
346-
return this._outputs[10];
361+
return this._outputs[11];
347362
}
348363

349364
protected override _buildBlock(state: NodeRenderGraphBuildState) {
350365
super._buildBlock(state);
351366

352367
const textureActivation = [
353368
this.geomViewDepth.isConnected,
369+
this.geomNormViewDepth.isConnected,
354370
this.geomScreenDepth.isConnected,
355371
this.geomViewNormal.isConnected,
356372
this.geomWorldNormal.isConnected,
@@ -368,6 +384,7 @@ export class NodeRenderGraphGeometryRendererBlock extends NodeRenderGraphBlock {
368384

369385
this.outputDepth.value = this._frameGraphTask.outputDepthTexture;
370386
this.geomViewDepth.value = this._frameGraphTask.geometryViewDepthTexture;
387+
this.geomNormViewDepth.value = this._frameGraphTask.geometryNormViewDepthTexture;
371388
this.geomScreenDepth.value = this._frameGraphTask.geometryScreenDepthTexture;
372389
this.geomViewNormal.value = this._frameGraphTask.geometryViewNormalTexture;
373390
this.geomWorldNormal.value = this._frameGraphTask.geometryWorldNormalTexture;
@@ -386,6 +403,7 @@ export class NodeRenderGraphGeometryRendererBlock extends NodeRenderGraphBlock {
386403

387404
const textureFormats = [
388405
this.viewDepthFormat,
406+
this.normalizedViewDepthFormat,
389407
this.screenDepthFormat,
390408
this.viewNormalFormat,
391409
this.worldNormalFormat,
@@ -398,6 +416,7 @@ export class NodeRenderGraphGeometryRendererBlock extends NodeRenderGraphBlock {
398416
];
399417
const textureTypes = [
400418
this.viewDepthType,
419+
this.normalizedViewDepthType,
401420
this.screenDepthType,
402421
this.viewNormalType,
403422
this.worldNormalType,
@@ -410,6 +429,7 @@ export class NodeRenderGraphGeometryRendererBlock extends NodeRenderGraphBlock {
410429
];
411430
const bufferTypes = [
412431
Constants.PREPASS_DEPTH_TEXTURE_TYPE,
432+
Constants.PREPASS_NORMALIZED_VIEW_DEPTH_TEXTURE_TYPE,
413433
Constants.PREPASS_SCREENSPACE_DEPTH_TEXTURE_TYPE,
414434
Constants.PREPASS_NORMAL_TEXTURE_TYPE,
415435
Constants.PREPASS_WORLD_NORMAL_TEXTURE_TYPE,
@@ -441,6 +461,8 @@ export class NodeRenderGraphGeometryRendererBlock extends NodeRenderGraphBlock {
441461
codes.push(`${this._codeVariableName}.dontRenderWhenMaterialDepthWriteIsDisabled = ${this.dontRenderWhenMaterialDepthWriteIsDisabled};`);
442462
codes.push(`${this._codeVariableName}.viewDepthFormat = ${this.viewDepthFormat};`);
443463
codes.push(`${this._codeVariableName}.viewDepthType = ${this.viewDepthType};`);
464+
codes.push(`${this._codeVariableName}.normalizedViewDepthFormat = ${this.normalizedViewDepthFormat};`);
465+
codes.push(`${this._codeVariableName}.normalizedViewDepthType = ${this.normalizedViewDepthType};`);
444466
codes.push(`${this._codeVariableName}.screenDepthFormat = ${this.screenDepthFormat};`);
445467
codes.push(`${this._codeVariableName}.screenDepthType = ${this.screenDepthType};`);
446468
codes.push(`${this._codeVariableName}.localPositionFormat = ${this.localPositionFormat};`);
@@ -471,6 +493,8 @@ export class NodeRenderGraphGeometryRendererBlock extends NodeRenderGraphBlock {
471493
serializationObject.dontRenderWhenMaterialDepthWriteIsDisabled = this.dontRenderWhenMaterialDepthWriteIsDisabled;
472494
serializationObject.viewDepthFormat = this.viewDepthFormat;
473495
serializationObject.viewDepthType = this.viewDepthType;
496+
serializationObject.normalizedViewDepthFormat = this.normalizedViewDepthFormat;
497+
serializationObject.normalizedViewDepthType = this.normalizedViewDepthType;
474498
serializationObject.screenDepthFormat = this.screenDepthFormat;
475499
serializationObject.screenDepthType = this.screenDepthType;
476500
serializationObject.localPositionFormat = this.localPositionFormat;
@@ -501,6 +525,8 @@ export class NodeRenderGraphGeometryRendererBlock extends NodeRenderGraphBlock {
501525
this.dontRenderWhenMaterialDepthWriteIsDisabled = serializationObject.dontRenderWhenMaterialDepthWriteIsDisabled;
502526
this.viewDepthFormat = serializationObject.viewDepthFormat;
503527
this.viewDepthType = serializationObject.viewDepthType;
528+
this.normalizedViewDepthFormat = serializationObject.normalizedViewDepthFormat ?? Constants.TEXTUREFORMAT_RED;
529+
this.normalizedViewDepthType = serializationObject.normalizedViewDepthType ?? Constants.TEXTURETYPE_UNSIGNED_BYTE;
504530
this.screenDepthFormat = serializationObject.screenDepthFormat;
505531
this.screenDepthType = serializationObject.screenDepthType;
506532
this.localPositionFormat = serializationObject.localPositionFormat;

packages/dev/core/src/FrameGraph/Node/Blocks/Rendering/shadowGeneratorBlock.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export class NodeRenderGraphShadowGeneratorBlock extends NodeRenderGraphBaseShad
1717
public constructor(name: string, frameGraph: FrameGraph, scene: Scene) {
1818
super(name, frameGraph, scene);
1919

20+
this._finalizeInputOutputRegistering();
21+
2022
this._frameGraphTask = new FrameGraphShadowGeneratorTask(this.name, frameGraph, scene);
2123
}
2224

packages/dev/core/src/FrameGraph/Node/Blocks/inputBlock.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export class NodeRenderGraphInputBlock extends NodeRenderGraphBlock {
7272
case NodeRenderGraphBlockConnectionPointTypes.Texture:
7373
case NodeRenderGraphBlockConnectionPointTypes.TextureViewDepth:
7474
case NodeRenderGraphBlockConnectionPointTypes.TextureScreenDepth:
75+
case NodeRenderGraphBlockConnectionPointTypes.TextureNormalizedViewDepth:
7576
case NodeRenderGraphBlockConnectionPointTypes.TextureViewNormal:
7677
case NodeRenderGraphBlockConnectionPointTypes.TextureWorldNormal:
7778
case NodeRenderGraphBlockConnectionPointTypes.TextureAlbedo:

packages/dev/core/src/FrameGraph/Node/Types/nodeRenderGraphTypes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ export enum NodeRenderGraphBlockConnectionPointTypes {
6666
TextureLocalPosition = 0x00004000,
6767
/** Linear velocity geometry texture */
6868
TextureLinearVelocity = 0x00008000,
69+
/** Normalied depth (in view space) geometry texture */
70+
TextureNormalizedViewDepth = 0x00010000,
6971

7072
/** Bit field for all textures but back buffer depth/stencil */
7173
TextureAllButBackBufferDepthStencil = 0x000ffffb,

0 commit comments

Comments
 (0)