2021-09-09 20:42:29 -04:00

83 lines
2.7 KiB
Plaintext

#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"
#pragma only_renderers d3d11 playstation xboxone xboxseries vulkan metal switch
#pragma multi_compile _ USE_MIN_DEPTH // Active when using MSAA
#pragma kernel KMainCoCPhysical
#pragma kernel KMainCoCManual
CBUFFER_START(cb0)
float4 _Params;
float4 _Params2;
CBUFFER_END
#define FarStart _Params.x
#define NearEnd _Params.y
#define FarRange _Params.z // 1 / (FarEnd - FarStart)
#define NearRange _Params.w // 1 / (NearStart - NearEnd)
#define NearMaxRadius _Params2.x
#define FarMaxRadius _Params2.y
TEXTURE2D_X(_DepthMinMaxAvg);
// outpute texture
RW_TEXTURE2D_X(float, _OutputTexture);
#define GROUP_RES 8u
#define GROUP_SIZE (GROUP_RES * GROUP_RES)
[numthreads(GROUP_RES, GROUP_RES, 1)]
void KMainCoCPhysical(uint3 dispatchThreadId : SV_DispatchThreadID)
{
UNITY_XR_ASSIGN_VIEW_INDEX(dispatchThreadId.z);
PositionInputs posInputs = GetPositionInput(float2(dispatchThreadId.xy), _ScreenSize.zw, uint2(GROUP_RES, GROUP_RES));
float depth = CameraDepth(_DepthMinMaxAvg, posInputs.positionSS);
// Note: the linearization of the depth is encoded directly in the MAD parameters
float CoC = _Params.w - _Params.z * depth;
if (CoC > 0)
{
const float maxFarCoC = _Params.x;
// CoC clamping for the far field
CoC = min(CoC, maxFarCoC);
}
else
{
const float maxNearCoC = _Params.y;
// CoC clamping for the near field
CoC = max(CoC, -maxNearCoC);
}
_OutputTexture[COORD_TEXTURE2D_X(posInputs.positionSS)] = CoC;
}
// Manual CoC using near & far planes
[numthreads(GROUP_RES, GROUP_RES, 1)]
void KMainCoCManual(uint3 dispatchThreadId : SV_DispatchThreadID)
{
UNITY_XR_ASSIGN_VIEW_INDEX(dispatchThreadId.z);
float depth = CameraDepth(_DepthMinMaxAvg, dispatchThreadId.xy);
// Note: we can avoid explicit linearization by merging it with the other computations
float linearEyeDepth = LinearEyeDepth(depth, _ZBufferParams);
float CoC = 0;
if (linearEyeDepth > FarStart)
{
CoC = FarMaxRadius * saturate((linearEyeDepth - FarStart) * FarRange);
}
else if (linearEyeDepth < NearEnd)
{
CoC = - NearMaxRadius * saturate((linearEyeDepth - NearEnd) * NearRange);
}
_OutputTexture[COORD_TEXTURE2D_X(dispatchThreadId.xy)] = CoC;
}