New Depth of Field - Minor improvements, optimizations, and now works with auto focus
Signed-off-by: antonmic <56370189+antonmic@users.noreply.github.com>
This commit is contained in:
@@ -18,6 +18,19 @@
|
||||
}
|
||||
],
|
||||
"PassRequests": [
|
||||
{
|
||||
"Name": "AutoFocus",
|
||||
"TemplateName": "DepthOfFieldReadBackFocusDepthTemplate",
|
||||
"Connections": [
|
||||
{
|
||||
"LocalSlot": "DepthInput",
|
||||
"AttachmentRef": {
|
||||
"Pass": "Parent",
|
||||
"Attachment": "Depth"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"Name": "Downsample",
|
||||
"TemplateName": "NewDepthOfFieldDownsampleTemplate",
|
||||
|
||||
+22
-6
@@ -85,6 +85,24 @@ PSOutput MainPS(VSOutput IN)
|
||||
|
||||
float4 backgroundColor = float4(0, 0, 0, 0);
|
||||
|
||||
if(minCoc >= 0)
|
||||
{
|
||||
for(uint i = 0; i < SAMPLES_LOOP_TOTAL; ++i)
|
||||
{
|
||||
// Calculate sample offset
|
||||
float3 offset = GetOffsetUV(i, offsetMultiplier);
|
||||
|
||||
// Get sample
|
||||
float4 sampleColorCoc = PassSrg::m_colorAndCoc.Sample(PassSrg::LinearSampler, pixelUV + offset.xy).rgba;
|
||||
|
||||
// Calculate weight for sample
|
||||
float weight = CaclulateWeight(offset.z, cocRadius, sampleColorCoc.a, centerCoc);
|
||||
|
||||
// Accumulate
|
||||
color += weight * sampleColorCoc;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
float backgroundMin = min(0, centerCoc);
|
||||
color.rgb /= abs(color.a);
|
||||
@@ -112,17 +130,15 @@ PSOutput MainPS(VSOutput IN)
|
||||
backgroundColor += isBackground * sampleColorCoc;
|
||||
color += !isBackground * sampleColorCoc;
|
||||
}
|
||||
|
||||
backgroundColor.rgb /= max(backgroundColor.a, COC_EPSILON);
|
||||
}
|
||||
|
||||
float backgroundRatio = backgroundColor.a / float(SAMPLES_LOOP_TOTAL);
|
||||
float backgroundRatio = saturate( backgroundColor.a / float(SAMPLES_LOOP_TOTAL) );
|
||||
float alpha = backgroundRatio > abs(centerCoc) ? -backgroundRatio : centerCoc;
|
||||
|
||||
color.rgb /= max(color.a, COC_EPSILON);
|
||||
backgroundColor.rgb /= max(backgroundColor.a, COC_EPSILON);
|
||||
|
||||
color = color * saturate(1 - backgroundRatio) + backgroundColor * backgroundRatio;
|
||||
//color.rgb += backgroundColor.rgb;
|
||||
|
||||
color = lerp(color, backgroundColor, backgroundRatio);
|
||||
|
||||
|
||||
PSOutput OUT = (PSOutput)0;
|
||||
|
||||
+12
-3
@@ -31,6 +31,16 @@ ShaderResourceGroup PassSrg : SRG_PerPass
|
||||
AddressV = Clamp;
|
||||
AddressW = Clamp;
|
||||
};
|
||||
|
||||
Sampler PointSampler
|
||||
{
|
||||
MinFilter = Point;
|
||||
MagFilter = Point;
|
||||
MipFilter = Point;
|
||||
AddressU = Clamp;
|
||||
AddressV = Clamp;
|
||||
AddressW = Clamp;
|
||||
};
|
||||
}
|
||||
|
||||
float2 GetOffsetUV(uint index, float2 offsetMultiplier)
|
||||
@@ -44,7 +54,7 @@ PSOutput MainPS(VSOutput IN)
|
||||
float2 pixelUV = IN.m_texCoord.xy;
|
||||
|
||||
// Sample pixel being shaded
|
||||
float4 centerSample = PassSrg::m_colorAndCoc.Sample(PassSrg::LinearSampler, pixelUV).rgba;
|
||||
float4 centerSample = PassSrg::m_colorAndCoc.Sample(PassSrg::PointSampler, pixelUV).rgba;
|
||||
float centerCoc = centerSample.a;
|
||||
|
||||
// Aspect ratio = texture.x / texture.y = dimensions.x * dimensions.w
|
||||
@@ -65,7 +75,7 @@ PSOutput MainPS(VSOutput IN)
|
||||
float2 offsetUV = GetOffsetUV(i, offsetMultiplier);
|
||||
|
||||
// Get sample
|
||||
float4 sampleColorCoc = PassSrg::m_colorAndCoc.Sample(PassSrg::LinearSampler, pixelUV + offsetUV).rgba;
|
||||
float4 sampleColorCoc = PassSrg::m_colorAndCoc.Sample(PassSrg::PointSampler, pixelUV + offsetUV).rgba;
|
||||
|
||||
// Calculate weight
|
||||
float cocDiff = sampleColorCoc.a - centerCoc;
|
||||
@@ -85,4 +95,3 @@ PSOutput MainPS(VSOutput IN)
|
||||
OUT.m_color.a = centerCoc;
|
||||
return OUT;
|
||||
}
|
||||
|
||||
|
||||
@@ -149,6 +149,14 @@ namespace AZ
|
||||
float radius = (loop + 1.0f) / float(NewDepthOfFieldConstants::numberOfLoops);
|
||||
float loopCount = NewDepthOfFieldConstants::loopCounts[loop];
|
||||
|
||||
float angleOffset = 0;
|
||||
|
||||
// Every other loop slightly rotate sample ring so they don't line up
|
||||
if (loop & 1)
|
||||
{
|
||||
angleOffset = Constants::TwoPi * 0.5f / loopCount;
|
||||
}
|
||||
|
||||
for (float i = 0.0f; i < loopCount; ++i)
|
||||
{
|
||||
float angle = Constants::TwoPi * i / loopCount;
|
||||
|
||||
Reference in New Issue
Block a user