81 lines
2.7 KiB
Plaintext
81 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/DepthOfFieldCommon.hlsl"
|
|
|
|
#pragma only_renderers d3d11 playstation xboxone xboxseries vulkan metal switch
|
|
|
|
#pragma multi_compile _ USE_MIN_DEPTH // Active when using MSAA
|
|
|
|
#pragma kernel KMainPhysical
|
|
#pragma kernel KMainManual
|
|
|
|
TEXTURE2D_X(_DepthMinMaxAvg);
|
|
|
|
RW_TEXTURE2D_X(float, _OutputCoCTexture);
|
|
|
|
CBUFFER_START(cb0)
|
|
float4 _Params;
|
|
CBUFFER_END
|
|
|
|
#define FocusDist _Params.x
|
|
#define MaxCoC _Params.y
|
|
|
|
#define NearStart _Params.x
|
|
#define NearEnd _Params.y
|
|
#define FarStart _Params.z
|
|
#define FarEnd _Params.w
|
|
|
|
#define FIX_NEAR_BLEND 0
|
|
#define GROUP_SIZE 8
|
|
|
|
float GetFixedNearBlend(float linearEyeDepth)
|
|
{
|
|
#if FIX_NEAR_BLEND
|
|
// We can't rely on the actual CoC to blur the foreground as we have a fixed number of samples
|
|
// so an object close to the camera would appear less blurry than another further away with the
|
|
// same CoC
|
|
return 1.0 - saturate(linearEyeDepth / (NearEnd * NearEnd));
|
|
#else
|
|
return 1.0;
|
|
#endif
|
|
}
|
|
|
|
// Physical CoC
|
|
// "A Lens and Aperture Camera Model for Synthetic Image Generation" [Potmesil81]
|
|
[numthreads(GROUP_SIZE, GROUP_SIZE, 1)]
|
|
void KMainPhysical(uint3 dispatchThreadId : SV_DispatchThreadID)
|
|
{
|
|
UNITY_XR_ASSIGN_VIEW_INDEX(dispatchThreadId.z);
|
|
|
|
float depth = CameraDepth(_DepthMinMaxAvg, dispatchThreadId.xy);
|
|
float linearEyeDepth = LinearEyeDepth(depth, _ZBufferParams);
|
|
float coc = (1.0 - FocusDist / max(linearEyeDepth, 1e-6f)) * MaxCoC;
|
|
|
|
float nearBlend = GetFixedNearBlend(linearEyeDepth);
|
|
|
|
float nearCoC = clamp(coc * nearBlend, -1.0, 0.0);
|
|
float farCoC = saturate(coc);
|
|
|
|
_OutputCoCTexture[COORD_TEXTURE2D_X(dispatchThreadId.xy)] = farCoC + nearCoC;
|
|
}
|
|
|
|
// Manual CoC using near & far planes
|
|
// This will accentuate rendering artifacts if used incorrectly
|
|
[numthreads(GROUP_SIZE, GROUP_SIZE, 1)]
|
|
void KMainManual(uint3 dispatchThreadId : SV_DispatchThreadID)
|
|
{
|
|
UNITY_XR_ASSIGN_VIEW_INDEX(dispatchThreadId.z);
|
|
float depth = CameraDepth(_DepthMinMaxAvg, dispatchThreadId.xy);
|
|
float linearEyeDepth = LinearEyeDepth(depth, _ZBufferParams);
|
|
|
|
float nearBlend = GetFixedNearBlend(linearEyeDepth);
|
|
|
|
float nearCoC = (linearEyeDepth - NearEnd) / (NearStart - NearEnd);
|
|
nearCoC = saturate(nearCoC * nearBlend);
|
|
|
|
float farCoC = (linearEyeDepth - FarStart) / (FarEnd - FarStart);
|
|
farCoC = saturate(farCoC);
|
|
|
|
_OutputCoCTexture[COORD_TEXTURE2D_X(dispatchThreadId.xy)] = -nearCoC + farCoC;
|
|
}
|