struct VS_OUT { float4 hPosition : POSITION; float2 texCoord : TEXCOORD0; }; VS_OUT Default_VS(float4 position : POSITION, float4 texCoord : TEXCOORD0) { VS_OUT OUT; OUT.hPosition = float4(sign(position.xy),0,1); OUT.texCoord = (float2(OUT.hPosition.x, -OUT.hPosition.y) + 1.0) / 2.0; return OUT; } #define NUM_DOF_TAPS 12 float4 DoF_PS(VS_OUT IN, uniform float width, uniform float height, //uniform float focalDist, uniform float focalRange, uniform float maxCoC, uniform sampler2D ImageTexture: register(s0), uniform sampler2D DepthTexture: register(s1) ) : COLOR { float2 filterTaps[NUM_DOF_TAPS]; float dx = 1.0f / width; float dy = 1.0f / height; filterTaps[0] = float2(-0.326212f * dx, -0.405805f * dy); filterTaps[1] = float2(-0.840144f * dx, -0.07358f * dy); filterTaps[2] = float2(-0.695914f * dx, 0.457137f * dy); filterTaps[3] = float2(-0.203345f * dx, 0.620716f * dy); filterTaps[4] = float2(0.96234f * dx, -0.194983f * dy); filterTaps[5] = float2(0.473434f * dx, -0.480026f * dy); filterTaps[6] = float2(0.519456f * dx, 0.767022f * dy); filterTaps[7] = float2(0.185461f * dx, -0.893124f * dy); filterTaps[8] = float2(0.507431f * dx, 0.064425f * dy); filterTaps[9] = float2(0.89642f * dx, 0.412458f * dy); filterTaps[10] = float2(-0.32194f * dx, -0.932615f * dy); filterTaps[11] = float2(-0.791559f * dx, -0.597705f * dy); IN.texCoord += float2(0.5/width, 0.5/height); float focalDist = tex2D(DepthTexture, float2(0.5,0.5)).x; float4 color = tex2D(ImageTexture, IN.texCoord); float centerDepth = color.a;//tex2D(DepthTexture, IN.texCoord).x; //return abs(centerDepth - focalDist); float blur = saturate(abs(centerDepth - focalDist) * focalRange); float sizeCoC = blur * maxCoC; float totalContribution = 1.0f; // Run through all taps for (int i = 0; i < NUM_DOF_TAPS; i++) { // Compute tap coordinates float2 tapCoord = IN.texCoord + filterTaps[i] * sizeCoC; // Fetch tap sample float4 tapColor = tex2D(ImageTexture, tapCoord); float tapDepth = tapColor.a;//tex2D(DepthTexture, tapCoord).x; float blur = saturate(abs(tapDepth - focalDist) * focalRange); // Compute tap contribution float tapContribution = (tapDepth > centerDepth) ? 1.0f : blur; // Accumulate color and contribution color += tapColor * tapContribution; totalContribution += tapContribution; } // Normalize to get proper luminance float4 finalColor = color / totalContribution; //return tex2D(ImageTexture, IN.texCoord).r; return finalColor; } float4 sampleCenter_PS(VS_OUT IN,uniform sampler2D ImageTexture: register(s0), uniform float blending):COLOR { float dx = 1.0 / 640.0; float dy = 1.0 / 480.0; float sample = 0; sample += tex2D(ImageTexture, float2(0.5, 0.5)).a; sample += tex2D(ImageTexture, float2(0.5, 0.5) + float2(dx, 0)).a; sample += tex2D(ImageTexture, float2(0.5, 0.5) + float2(dx, dy)).a; sample += tex2D(ImageTexture, float2(0.5, 0.5) + float2(0, dy)).a; sample += tex2D(ImageTexture, float2(0.5, 0.5) + float2(-dx, 0)).a; sample += tex2D(ImageTexture, float2(0.5, 0.5) + float2(-dx, -dy)).a; sample += tex2D(ImageTexture, float2(0.5, 0.5) + float2(0, -dy)).a; sample += tex2D(ImageTexture, float2(0.5, 0.5) + float2(-dx, dy)).a; sample += tex2D(ImageTexture, float2(0.5, 0.5) + float2(dx, -dy)).a; sample /= 9.0; return float4(sample, sample, sample, blending); }