float4 res : register(c0); float2 Dist : register(c3); sampler2D colorSampler : register(s0); sampler2D depthSampler : register(s1); sampler2D blurSampler : register(s4); // Tweakables static float g_fRadius = 0.005f; static float g_fLowRadiusScaling = 0.4f; static float g_fNearPlane = 1.0f; static float g_fFocalPlane = Dist.y; static float g_fDistScale = 1.0f / pow( g_fFocalPlane , 2.5f ); static float g_fFarPlane= 128.0f; static const float2 vPoissonDisc[] = { float2(-0.326212f, -0.40581f), float2(-0.840144f, -0.07358f), float2(-0.695914f, 0.457137f), float2(-0.203345f, 0.620716f), float2(0.96234f, -0.194983f), float2(0.473434f, -0.480026f), float2(0.519456f, 0.767022f), float2(0.185461f, -0.893124f), float2(0.507431f, 0.064425f), float2(0.89642f, 0.412458f), float2(-0.32194f, -0.932615f), float2(-0.791559f, -0.59771f) }; float4 ps_blur_intensity(float2 TexCoord : TEXCOORD0) : COLOR0 { // Sample pixel depth float fDepth = tex2D(depthSampler, TexCoord).x; // Anti-aliasing float4 fDDTexCoords1 = TexCoord.xyxy, fDDTexCoords2 = TexCoord.xyxy; fDDTexCoords1.zw -= res.zw; fDDTexCoords2.zw += res.zw; float4 fDDDepth4 = fDepth - float4( tex2D(depthSampler, fDDTexCoords1.zy).x, tex2D(depthSampler, fDDTexCoords1.xw).x, tex2D(depthSampler, fDDTexCoords2.zy).x, tex2D(depthSampler, fDDTexCoords2.xw).x ); // fDDDepth4 = lerp(fDDDepth4, abs(fDDDepth4), (fDDDepth4 * fDDDepth4.zwxy) > 0.f); float2 fDeltaDepth2 = max(fDDDepth4.xy, fDDDepth4.zw); float fDeltaDepth = max(fDeltaDepth2.x, fDeltaDepth2.y); float fIntensity; if(fDepth < g_fFocalPlane) { // Close-up blur fIntensity = (fDepth - g_fFocalPlane) / (g_fFocalPlane - g_fNearPlane); } else { // Distance blur float2 fDistances = (fDepth - g_fFocalPlane); fDistances.y -= max(fDeltaDepth, 0.f); float2 fIntensities = saturate(fDistances * g_fDistScale ); fIntensity = lerp(fIntensities.y, fIntensities.x, fIntensities.y); } // Bias to valid range return float4( tex2D(colorSampler, TexCoord).xyz, saturate(0.5f + 0.5f * fIntensity) ); } float4 ps_main(float2 TexCoord : TEXCOORD0) : COLOR0 { // Center pixel depth float fDepth = tex2D(colorSampler, TexCoord).w; // Scale sampling radius float fRadius = abs(2.0f * g_fRadius * fDepth - g_fRadius); float fLowRadius = fRadius * g_fLowRadiusScaling; float4 fColor = 0.0f; float fAmount = 0.0f; // Loop over samples for(int i = 0; i < 12; i++) { // Sample on poisson disc float2 fHighSampleTexCoord = TexCoord + fRadius * vPoissonDisc[i]; float2 fLowSampleTexCoord = TexCoord + fLowRadius * vPoissonDisc[i]; // Sample blurred and unblurred texture float4 fHighSample = tex2D(colorSampler, fHighSampleTexCoord); float4 fLowSample = tex2D(blurSampler, fLowSampleTexCoord); // Blend between blurred and unblurred texture float fSampleBlurIntensity = abs(2.f * fHighSample.w - 1.f); // * saturate(1.f - 3.f * (fHighSample.w - fLowSample.w)); float4 fSample = lerp(fHighSample, fLowSample, fSampleBlurIntensity); // Compute smart weight to avoid cross-edge leaking float fWeight = fSample.w < fDepth ? abs(2.f * fSample.w - 1.f) : 1.f; // Sum up fColor += fSample * fWeight; fAmount += fWeight; } return fColor / fAmount; }