Skip to content

[HDRP] Physically based depth of field optimizations #5996

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 23 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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.
9 changes: 9 additions & 0 deletions com.unity.render-pipelines.core/ShaderLibrary/Random.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,13 @@ float InterleavedGradientNoise(float2 pixCoord, int frameCount)
return frac(magic.z * frac(dot(pixCoord, magic.xy)));
}

// 32-bit Xorshift random number generator
uint XorShift(inout uint rngState)
{
rngState ^= rngState << 13;
rngState ^= rngState >> 17;
rngState ^= rngState << 5;
return rngState;
}

#endif // UNITY_RANDOM_INCLUDED
1 change: 1 addition & 0 deletions com.unity.render-pipelines.high-definition/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Changed
- MaterialReimporter.ReimportAllMaterials and MaterialReimporter.ReimportAllHDShaderGraphs now batch the asset database changes to improve performance.
- Optimizations for the physically based depth of field.

### Fixed
- Fixed the volume not being assigned on some scene templates.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ This page contains an overview of new features, improvements, and issues resolve
## Added

## Updated

### Depth Of Field
HDRP version 13 includes optimizations in the physically based depth of field implementation. In particular, image regions that are out-of-focus are now computed at lower resolution, while in-focus regions retain the full resolution. For many scenes this results in significant speedup, without any visible reduction in image quality.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/PostProcessDefines.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/DepthOfFieldCommon.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RaytracingSampling.hlsl"

#pragma only_renderers d3d11 playstation xboxone xboxseries vulkan metal switch

#pragma kernel KMain

#pragma multi_compile _ ENABLE_ALPHA

#define GROUP_RES 8u
#define GROUP_SIZE (GROUP_RES * GROUP_RES)

CBUFFER_START(cb0)
float4 _Params;
CBUFFER_END

#define NumRings _Params.x
#define MaxCoCRadius _Params.y
#define Anamorphism _Params.z

// Out-of-focus areas, computed at lower res
TEXTURE2D_X(_InputNearTexture);

// Here we write the final output
RW_TEXTURE2D_X(CTYPE, _OutputTexture);

#define FORCE_POINT_SAMPLING
#define ResScale 1.0f
#define OneOverResScale 1.0f
#define MaxColorMip 0.0f
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/DoFGatherUtils.hlsl"

[numthreads(GROUP_RES, GROUP_RES, 1)]
void KMain(uint3 dispatchThreadId : SV_DispatchThreadID)
{
UNITY_XR_ASSIGN_VIEW_INDEX(dispatchThreadId.z);

PositionInputs posInputs = GetPositionInput(float2(dispatchThreadId.xy), _PostProcessScreenSize.zw, uint2(GROUP_RES, GROUP_RES));

CTYPE output = 0;
switch (GetTileClass(posInputs.positionSS))
{
case SLOW_INFOCUS_TILE:
// This tile has both in-focus and out-of-focus pixels, so we compute it at full resolution
SampleData centerSample;
centerSample.color = GetColorSample(posInputs.positionSS, 0);
centerSample.CoC = GetCoCRadius(posInputs.positionSS);

DoFTile tileData;
LoadTileData(posInputs.positionSS, centerSample, NumRings, tileData);

float4 outColor;
float outAlpha;
DoFGatherRings(posInputs, tileData, centerSample, outColor, outAlpha);
output.xyz = outColor.xyz;
#ifdef ENABLE_ALPHA
ComposeAlpha(output, centerSample.color.xyz, outAlpha);
#endif
break;
case FAST_DEFOCUS_TILE:
// This tile has only out-of-focus pixels, the output was computed at a lower resolution in a previous pass.
float2 uv = ClampAndScaleUVPostProcessTextureForPoint((posInputs.positionSS + 0.5) * _PostProcessScreenSize.zw);
output = SAMPLE_TEXTURE2D_X_LOD(_InputNearTexture, s_linear_clamp_sampler, uv, 0.0).CTYPE_SWIZZLE;
break;
default :
// FAST_INFOCUS_TILE
// This has only in-focus pixels, so don't do any DoF computations (pass through the input texture)
output = GetColorSample(posInputs.positionSS, 0);
break;
}

// Helper function to visualize tile types in case it is needed for debugging
//DebugTiles(posInputs.positionSS, output.xyz);

_OutputTexture[COORD_TEXTURE2D_X(posInputs.positionSS)] = (CTYPE)output;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading