[765] | 1 | // Number of outer filter taps
|
---|
| 2 | #define NUM_DOF_TAPS 12
|
---|
| 3 | float2 filterTaps[NUM_DOF_TAPS];
|
---|
| 4 |
|
---|
| 5 |
|
---|
| 6 | #define SAMPLER_LINEAR(g_samplerMap, g_txMap); \
|
---|
| 7 | sampler2D g_samplerMap = sampler_state { \
|
---|
| 8 | Texture = <g_txMap>; \
|
---|
| 9 | MinFilter = Linear; \
|
---|
| 10 | MagFilter = Linear; \
|
---|
| 11 | MipFilter = Linear; \
|
---|
| 12 | AddressU = WRAP; \
|
---|
| 13 | AddressV = WRAP; \
|
---|
| 14 | };
|
---|
| 15 |
|
---|
| 16 | texture ColorMap;
|
---|
| 17 | SAMPLER_LINEAR(ColorMapSampler, ColorMap); |
---|
| 18 | texture DepthMap; |
---|
| 19 | SAMPLER_LINEAR(DepthMapSampler, DepthMap); |
---|
| 20 | |
---|
| 21 | struct VS_INPUT { |
---|
| 22 | float3 Position : POSITION;
|
---|
| 23 | }; |
---|
| 24 | |
---|
| 25 | struct VS_OUTPUT { |
---|
| 26 | float4 hPosition : POSITION; // point in normalized device space before homogeneous division |
---|
| 27 | float2 TexCoord : TEXCOORD0; |
---|
| 28 | }; |
---|
| 29 |
|
---|
| 30 | VS_OUTPUT DepthVS(VS_INPUT IN)
|
---|
| 31 | {
|
---|
| 32 | VS_OUTPUT output;
|
---|
| 33 |
|
---|
| 34 | output.hPosition = half4(IN.Position, 1);
|
---|
| 35 |
|
---|
| 36 | output.TexCoord = IN.Position.xy * 0.5f + 0.5f;
|
---|
| 37 | output.TexCoord.y *= -1;
|
---|
| 38 |
|
---|
| 39 | return output;
|
---|
| 40 | } |
---|
| 41 | //------------------------------------------------------------------------------------ |
---|
| 42 | // |
---|
| 43 | // |
---|
| 44 | // |
---|
| 45 | //------------------------------------------------------------------------------------ |
---|
| 46 | const float maxCoC = 5; |
---|
| 47 | float4 DepthPS(VS_OUTPUT IN) : COLOR |
---|
| 48 | { |
---|
| 49 | // Get center sample
|
---|
| 50 | float4 colorSum = tex2D(ColorMapSampler, IN.TexCoord);
|
---|
| 51 | float2 centerDepthBlur = tex2D(DepthMapSampler, IN.TexCoord);
|
---|
| 52 |
|
---|
| 53 | // Compute CoC size based on blurriness
|
---|
| 54 | float sizeCoC = centerDepthBlur.y * maxCoC;
|
---|
| 55 |
|
---|
| 56 | float totalContribution = 1.0f;
|
---|
| 57 |
|
---|
| 58 | // Run through all taps
|
---|
| 59 | for (int i = 0; i < NUM_DOF_TAPS; i++)
|
---|
| 60 | {
|
---|
| 61 | // Compute tap coordinates
|
---|
| 62 | float2 tapCoord = IN.TexCoord + filterTaps[i] * sizeCoC;
|
---|
| 63 |
|
---|
| 64 | // Fetch tap sample
|
---|
| 65 | float4 tapColor = tex2D(ColorMapSampler, tapCoord);
|
---|
| 66 | float2 tapDepthBlur = tex2D(DepthMapSampler, tapCoord);
|
---|
| 67 |
|
---|
| 68 | // Compute tap contribution
|
---|
| 69 | float tapContribution = (tapDepthBlur.x > centerDepthBlur.x) ? 1.0f : tapDepthBlur.y;
|
---|
| 70 |
|
---|
| 71 | // Accumulate color and contribution
|
---|
| 72 | colorSum += tapColor * tapContribution;
|
---|
| 73 | totalContribution += tapContribution;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | // Normalize to get proper luminance
|
---|
| 77 | float4 finalColor = colorSum / totalContribution;
|
---|
| 78 |
|
---|
| 79 | return finalColor;
|
---|
| 80 | }
|
---|
| 81 | //------------------------------------------------------------------------------------
|
---|
| 82 | // Technique definitions
|
---|
| 83 | //------------------------------------------------------------------------------------
|
---|
| 84 | technique DepthOfField
|
---|
| 85 | {
|
---|
| 86 | pass p0
|
---|
| 87 | {
|
---|
| 88 | VertexShader = compile vs_3_0 DepthVS();
|
---|
| 89 | PixelShader = compile ps_3_0 DepthPS();
|
---|
| 90 | }
|
---|
| 91 | } |
---|