Skip to content

Fix Resolution Issues for Physically Based Depth of Field #4848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions com.unity.render-pipelines.high-definition/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fixed the transparent cutoff not working properly in semi-transparent and color shadows (case 1340234).
- Fixed object outline flickering with TAA.
- Fixed issue with sky settings being ignored when using the recorder and path tracing (case 1340507).
- Fixed some resolution aliasing for physically based depth of field (case 1340551).
- Fixed an issue with resolution dependence for physically based depth of field.

### Changed
- Changed Window/Render Pipeline/HD Render Pipeline Wizard to Window/Rendering/HDRP Wizard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ RW_TEXTURE2D_X(CTYPE, _OutputTexture);
// A set of Defines to fine-tune the algorithm
#define ADAPTIVE_SAMPLING
#define STRATIFY
#define FORCE_POINT_SAMPLING
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember correctly, trilinear sampling can introduce some "leaking" between in-focus and out-of-focus regions, that's why I was forcing point sampling. Ideally this choice should be per tile (use trilinear only in tiles without in-focus pixels), but since we don't do that now, I think it's more safe to use point sampling.

#define RING_OCCLUSION
#define PER_TILE_BG_FG_CLASSIFICATION
#define PHYSICAL_WEIGHTS

#define FORCE_POINT_SAMPLING

#define GROUP_RES 8u
#define GROUP_SIZE (GROUP_RES * GROUP_RES)
Expand Down Expand Up @@ -73,9 +72,16 @@ float GetCoCRadius(int2 positionSS)
CTYPE GetColorSample(float2 sampleTC, float lod)
{
#ifndef FORCE_POINT_SAMPLING
return SAMPLE_TEXTURE2D_X_LOD(_InputTexture, s_trilinear_clamp_sampler, ClampAndScaleUVForBilinearPostProcessTexture(sampleTC * _PostProcessScreenSize.zw), lod).CTYPE_SWIZZLE;
float texelsToClamp = (1u << (uint)ceil(lod)) + 1;
float2 uv = ClampAndScaleUVPostProcessTexture(sampleTC * _PostProcessScreenSize.zw, _PostProcessScreenSize.zw, texelsToClamp);

// Trilinear sampling can introduce some "leaking" between in-focus and out-of-focus regions, hence why we force point
// sampling. Ideally, this choice should be per-tile (use trilinear only in tiles without in-focus pixels), but until
// we do this, it is more safe to point sample.
return SAMPLE_TEXTURE2D_X_LOD(_InputTexture, s_trilinear_clamp_sampler, uv, lod).CTYPE_SWIZZLE;
#else
return LOAD_TEXTURE2D_X(_InputTexture, sampleTC).CTYPE_SWIZZLE;
float2 uv = ClampAndScaleUVPostProcessTextureForPoint(sampleTC * _PostProcessScreenSize.zw);
return SAMPLE_TEXTURE2D_X_LOD(_InputTexture, s_point_clamp_sampler, uv, 0.0).CTYPE_SWIZZLE;
#endif
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2413,14 +2413,19 @@ static void ReprojectCoCHistory(in DepthOfFieldParameters parameters, CommandBuf

static void DoPhysicallyBasedDepthOfField(in DepthOfFieldParameters dofParameters, CommandBuffer cmd, RTHandle source, RTHandle destination, RTHandle fullresCoC, RTHandle prevCoCHistory, RTHandle nextCoCHistory, RTHandle motionVecTexture, RTHandle sourcePyramid, RTHandle depthBuffer, RTHandle minMaxCoCPing, RTHandle minMaxCoCPong, bool taaEnabled)
{
float scale = 1f / (float)dofParameters.resolution;
int targetWidth = Mathf.RoundToInt(dofParameters.viewportSize.x * scale);
int targetHeight = Mathf.RoundToInt(dofParameters.viewportSize.y * scale);
// Currently Physically Based DoF is performed at "full" resolution (ie does not utilize DepthOfFieldResolution)
// However, to produce similar results when switching between various resolutions, or dynamic resolution,
// we must incorporate resolution independence, fitted with a 1920x1080 reference resolution.
var scale = dofParameters.viewportSize / new Vector2(1920f, 1080f);
float resolutionScale = Mathf.Min(scale.x, scale.y) * 2f;

float farMaxBlur = resolutionScale * dofParameters.farMaxBlur;
float nearMaxBlur = resolutionScale * dofParameters.nearMaxBlur;

// Map the old "max radius" parameters to a bigger range, so we can work on more challenging scenes, [0, 16] --> [0, 64]
Vector2 cocLimit = new Vector2(
Mathf.Max(4 * dofParameters.farMaxBlur, 0.01f),
Mathf.Max(4 * dofParameters.nearMaxBlur, 0.01f));
Mathf.Max(4 * farMaxBlur, 0.01f),
Mathf.Max(4 * nearMaxBlur, 0.01f));
float maxCoc = Mathf.Max(cocLimit.x, cocLimit.y);

ComputeShader cs;
Expand Down