Skip to content

Commit

Permalink
Merge pull request #13110 from kircher1/dof-opts-and-cleanup
Browse files Browse the repository at this point in the history
Depth of field optimizations and cleanup
  • Loading branch information
sebavan authored Oct 13, 2022
2 parents 55991d7 + b4f8c78 commit 1650f38
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 34 deletions.
11 changes: 7 additions & 4 deletions packages/dev/core/src/PostProcesses/blurPostProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export class BlurPostProcess extends PostProcess {
* @param textureType Type of textures used when performing the post process. (default: 0)
* @param defines
* @param _blockCompilation If compilation of the shader should not be done in the constructor. The updateEffect method can be used to compile the shader at a later time. (default: false)
* @param textureFormat Format of textures used when performing the post process. (default: TEXTUREFORMAT_RGBA)
*/
constructor(
name: string,
Expand All @@ -106,14 +107,15 @@ export class BlurPostProcess extends PostProcess {
samplingMode: number = Texture.BILINEAR_SAMPLINGMODE,
engine?: Engine,
reusable?: boolean,
textureType: number = Constants.TEXTURETYPE_UNSIGNED_INT,
textureType = Constants.TEXTURETYPE_UNSIGNED_INT,
defines = "",
private _blockCompilation = false
private _blockCompilation = false,
textureFormat = Constants.TEXTUREFORMAT_RGBA
) {
super(
name,
"kernelBlur",
["delta", "direction", "cameraMinMaxZ"],
["delta", "direction"],
["circleOfConfusionSampler"],
options,
camera,
Expand All @@ -124,7 +126,8 @@ export class BlurPostProcess extends PostProcess {
textureType,
"kernelBlur",
{ varyingCount: 0, depCount: 0 },
true
true,
textureFormat
);
this._staticDefines = defines;
this.direction = direction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class CircleOfConfusionPostProcess extends PostProcess {
samplingMode?: number,
engine?: Engine,
reusable?: boolean,
textureType: number = Constants.TEXTURETYPE_UNSIGNED_INT,
textureType = Constants.TEXTURETYPE_UNSIGNED_INT,
blockCompilation = false
) {
super(
Expand Down Expand Up @@ -99,7 +99,8 @@ export class CircleOfConfusionPostProcess extends PostProcess {

effect.setFloat("focusDistance", this.focusDistance);
effect.setFloat("cocPrecalculation", cocPrecalculation);
effect.setFloat2("cameraMinMaxZ", this._depthTexture.activeCamera!.minZ, this._depthTexture.activeCamera!.maxZ);
const activeCamera = this._depthTexture.activeCamera!;
effect.setFloat2("cameraMinMaxZ", activeCamera.minZ, activeCamera.maxZ - activeCamera.minZ);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class DepthOfFieldBlurPostProcess extends BlurPostProcess {
* @param reusable If the post process can be reused on the same frame. (default: false)
* @param textureType Type of textures used when performing the post process. (default: 0)
* @param blockCompilation If compilation of the shader should not be done in the constructor. The updateEffect method can be used to compile the shader at a later time. (default: false)
* @param textureFormat Format of textures used when performing the post process. (default: TEXTUREFORMAT_RGBA)
*/
constructor(
name: string,
Expand All @@ -57,11 +58,12 @@ export class DepthOfFieldBlurPostProcess extends BlurPostProcess {
camera: Nullable<Camera>,
circleOfConfusion: PostProcess,
imageToBlur: Nullable<PostProcess> = null,
samplingMode: number = Texture.BILINEAR_SAMPLINGMODE,
samplingMode = Texture.BILINEAR_SAMPLINGMODE,
engine?: Engine,
reusable?: boolean,
textureType: number = Constants.TEXTURETYPE_UNSIGNED_INT,
blockCompilation = false
textureType = Constants.TEXTURETYPE_UNSIGNED_INT,
blockCompilation = false,
textureFormat = Constants.TEXTUREFORMAT_RGBA
) {
super(
name,
Expand All @@ -75,7 +77,8 @@ export class DepthOfFieldBlurPostProcess extends BlurPostProcess {
reusable,
textureType,
`#define DOF 1\r\n`,
blockCompilation
blockCompilation,
textureFormat
);

this.direction = direction;
Expand All @@ -86,9 +89,6 @@ export class DepthOfFieldBlurPostProcess extends BlurPostProcess {
effect.setTextureFromPostProcess("textureSampler", imageToBlur);
}
effect.setTextureFromPostProcessOutput("circleOfConfusionSampler", circleOfConfusion);
if (scene.activeCamera) {
effect.setFloat2("cameraMinMaxZ", scene.activeCamera.minZ, scene.activeCamera.maxZ);
}
});
}
}
Expand Down
18 changes: 13 additions & 5 deletions packages/dev/core/src/PostProcesses/depthOfFieldEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { CircleOfConfusionPostProcess } from "./circleOfConfusionPostProcess";
import { DepthOfFieldBlurPostProcess } from "./depthOfFieldBlurPostProcess";
import { DepthOfFieldMergePostProcess } from "./depthOfFieldMergePostProcess";
import type { Scene } from "../scene";
import { Constants } from "../Engines/constants";

/**
* Specifies the level of max blur that should be applied when using the depth of field effect
Expand Down Expand Up @@ -104,14 +105,20 @@ export class DepthOfFieldEffect extends PostProcessRenderEffect {
},
true
);

// Use R-only formats if supported to store the circle of confusion values.
// This should be more space and bandwidth efficient than using RGBA.
const engine = scene.getEngine();
const circleOfConfusionTextureFormat = engine.isWebGPU || engine.webGLVersion > 1 ? Constants.TEXTUREFORMAT_RED : Constants.TEXTUREFORMAT_RGBA;

// Circle of confusion value for each pixel is used to determine how much to blur that pixel
this._circleOfConfusion = new CircleOfConfusionPostProcess(
"circleOfConfusion",
depthTexture,
1,
null,
Texture.BILINEAR_SAMPLINGMODE,
scene.getEngine(),
engine,
false,
pipelineTextureType,
blockCompilation
Expand Down Expand Up @@ -154,10 +161,11 @@ export class DepthOfFieldEffect extends PostProcessRenderEffect {
this._circleOfConfusion,
i == 0 ? this._circleOfConfusion : null,
Texture.BILINEAR_SAMPLINGMODE,
scene.getEngine(),
engine,
false,
pipelineTextureType,
blockCompilation
blockCompilation,
i == 0 ? circleOfConfusionTextureFormat : Constants.TEXTUREFORMAT_RGBA
);
blurY.autoClear = false;
ratio = 0.75 / Math.pow(2, i);
Expand All @@ -171,7 +179,7 @@ export class DepthOfFieldEffect extends PostProcessRenderEffect {
this._circleOfConfusion,
null,
Texture.BILINEAR_SAMPLINGMODE,
scene.getEngine(),
engine,
false,
pipelineTextureType,
blockCompilation
Expand All @@ -197,7 +205,7 @@ export class DepthOfFieldEffect extends PostProcessRenderEffect {
ratio,
null,
Texture.BILINEAR_SAMPLINGMODE,
scene.getEngine(),
engine,
false,
pipelineTextureType,
blockCompilation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class DepthOfFieldMergePostProcess extends PostProcess {
samplingMode?: number,
engine?: Engine,
reusable?: boolean,
textureType: number = Constants.TEXTURETYPE_UNSIGNED_INT,
textureType = Constants.TEXTURETYPE_UNSIGNED_INT,
blockCompilation = false
) {
super(
Expand Down
9 changes: 5 additions & 4 deletions packages/dev/core/src/Shaders/circleOfConfusion.fragment.fx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ uniform sampler2D depthSampler;
// varyings
varying vec2 vUV;

// preconputed uniforms (not effect parameters)
// precomputed uniforms (not effect parameters)
// cameraMinMaxZ.y => "maxZ - minZ" i.e., the near-to-far distance.
uniform vec2 cameraMinMaxZ;

// uniforms
Expand All @@ -17,8 +18,8 @@ uniform float cocPrecalculation;
void main(void)
{
float depth = texture2D(depthSampler, vUV).r;
float pixelDistance = (cameraMinMaxZ.x + (cameraMinMaxZ.y - cameraMinMaxZ.x)*depth)*1000.0; // actual distance from the lens in scene units/1000 (eg. millimeter)
float coc = abs(cocPrecalculation* ((focusDistance - pixelDistance)/pixelDistance));
float pixelDistance = (cameraMinMaxZ.x + cameraMinMaxZ.y * depth) * 1000.0; // actual distance from the lens in scene units/1000 (eg. millimeter)
float coc = abs(cocPrecalculation * ((focusDistance - pixelDistance) / pixelDistance));
coc = clamp(coc, 0.0, 1.0);
gl_FragColor = vec4(coc, depth, coc, 1.0);
gl_FragColor = vec4(coc, coc, coc, 1.0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void main(void)
vec4 blurred1 = texture2D(blurStep1, vUV);
gl_FragColor = mix(original, blurred1, coc/0.5);
}else{
vec4 blurred0 = texture2D(blurStep0, vUV);
vec4 blurred0 = texture2D(blurStep0, vUV);
vec4 blurred1 = texture2D(blurStep1, vUV);
gl_FragColor = mix(blurred1, blurred0, (coc-0.5)/0.5);
}
Expand Down
14 changes: 4 additions & 10 deletions packages/dev/core/src/Shaders/kernelBlur.fragment.fx
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,16 @@ varying vec2 sampleCenter;
#ifdef DOF
uniform sampler2D circleOfConfusionSampler;

uniform vec2 cameraMinMaxZ;

float sampleDistance(in vec2 offset) {
float depth = texture2D(circleOfConfusionSampler, offset).g; // depth value from DepthRenderer: 0 to 1
return cameraMinMaxZ.x + (cameraMinMaxZ.y - cameraMinMaxZ.x)*depth; // actual distance from the lens
}
float sampleCoC(in vec2 offset) {
float coc = texture2D(circleOfConfusionSampler, offset).r;
return coc; // actual distance from the lens
float coc = texture2D(circleOfConfusionSampler, offset).r;
return coc; // actual distance from the lens
}
#endif

#include<kernelBlurVaryingDeclaration>[0..varyingCount]

#ifdef PACKEDFLOAT
#include<packingFunctions>
#include<packingFunctions>
#endif


Expand All @@ -33,7 +27,7 @@ void main(void)
{
float computedWeight = 0.0;

#ifdef PACKEDFLOAT
#ifdef PACKEDFLOAT
float blend = 0.;
#else
vec4 blend = vec4(0.);
Expand Down

0 comments on commit 1650f38

Please sign in to comment.