Merge pull request #4735 from cg110/fix_web_server_mem_leak
[vuplus_xbmc] / system / shaders / convolutionsep-6x6_d3d.fx
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 texture g_Texture;
22 texture g_KernelTexture;
23 texture g_IntermediateTexture;
24 float2  g_StepXY_P0;
25 float2  g_StepXY_P1;
26
27 sampler RGBSampler =
28   sampler_state {
29     Texture = <g_Texture>;
30     AddressU = CLAMP;
31     AddressV = CLAMP;
32     MipFilter = LINEAR;
33     MinFilter = POINT;
34     MagFilter = POINT;
35   };
36
37 sampler KernelSampler =
38   sampler_state
39   {
40     Texture = <g_KernelTexture>;
41     AddressU = CLAMP;
42     AddressV = CLAMP;
43     MipFilter = LINEAR;
44     MinFilter = LINEAR;
45     MagFilter = LINEAR;
46   };
47
48 sampler IntermediateSampler =
49   sampler_state {
50     Texture = <g_IntermediateTexture>;
51     AddressU = CLAMP;
52     AddressV = CLAMP;
53     MipFilter = LINEAR;
54     MinFilter = POINT;
55     MagFilter = POINT;
56   };
57
58 struct VS_OUTPUT
59 {
60   float4 Position   : POSITION;
61   float2 TextureUV  : TEXCOORD0;
62 };
63
64 struct PS_OUTPUT
65 {
66   float4 RGBColor : COLOR0;
67 };
68
69 half3 weight(float pos)
70 {
71   half3 w;
72 #ifdef HAS_RGBA
73   w = tex1D(KernelSampler, pos).rgb;
74 #else
75   w = tex1D(KernelSampler, pos).bgr;
76 #endif
77
78 #ifdef HAS_FLOAT_TEXTURE
79   return w;
80 #else
81   return w * 2.0 - 1.0;
82 #endif
83 }
84
85 half3 pixel(sampler samp, float xpos, float ypos)
86 {
87   return tex2D(samp, float2(xpos, ypos)).rgb;
88 }
89
90 // Code for first pass - horizontal
91
92 half3 getLine(float ypos, float3 xpos1, float3 xpos2, half3 linetaps1, half3 linetaps2)
93 {
94   return
95     pixel(RGBSampler, xpos1.r, ypos) * linetaps1.r +
96     pixel(RGBSampler, xpos1.g, ypos) * linetaps2.r +
97     pixel(RGBSampler, xpos1.b, ypos) * linetaps1.g +
98     pixel(RGBSampler, xpos2.r, ypos) * linetaps2.g +
99     pixel(RGBSampler, xpos2.g, ypos) * linetaps1.b +
100     pixel(RGBSampler, xpos2.b, ypos) * linetaps2.b;
101 }
102
103 PS_OUTPUT CONVOLUTION6x6Horiz(VS_OUTPUT In)
104 {
105   PS_OUTPUT OUT;
106
107   float2 pos = In.TextureUV + g_StepXY_P0 * 0.5;
108   float2 f = frac(pos / g_StepXY_P0);
109
110   half3 linetaps1 = weight((1.0 - f.x) / 2.0);
111   half3 linetaps2 = weight((1.0 - f.x) / 2.0 + 0.5);
112
113   // kernel generation code made sure taps add up to 1, no need to adjust here.
114
115   float2 xystart;
116   xystart.x = (-2.0 - f.x) * g_StepXY_P0.x + In.TextureUV.x;
117   xystart.y = In.TextureUV.y;
118
119   float3 xpos1 = float3(
120       xystart.x,
121       xystart.x + g_StepXY_P0.x,
122       xystart.x + g_StepXY_P0.x * 2.0);
123   float3 xpos2 = half3(
124       xystart.x + g_StepXY_P0.x * 3.0,
125       xystart.x + g_StepXY_P0.x * 4.0,
126       xystart.x + g_StepXY_P0.x * 5.0);
127
128   OUT.RGBColor.rgb = getLine(xystart.y, xpos1, xpos2, linetaps1, linetaps2);
129   OUT.RGBColor.a = 1.0;
130
131   return OUT;
132 }
133
134 // Code for second pass - vertical
135
136 half3 getRow(float xpos, float3 ypos1, float3 ypos2, half3 columntaps1, half3 columntaps2)
137 {
138   return
139     pixel(IntermediateSampler, xpos, ypos1.r) * columntaps1.r +
140     pixel(IntermediateSampler, xpos, ypos1.g) * columntaps2.r +
141     pixel(IntermediateSampler, xpos, ypos1.b) * columntaps1.g +
142     pixel(IntermediateSampler, xpos, ypos2.r) * columntaps2.g +
143     pixel(IntermediateSampler, xpos, ypos2.g) * columntaps1.b +
144     pixel(IntermediateSampler, xpos, ypos2.b) * columntaps2.b;
145 }
146
147 PS_OUTPUT CONVOLUTION6x6Vert(VS_OUTPUT In)
148 {
149   PS_OUTPUT OUT;
150
151   float2 pos = In.TextureUV + g_StepXY_P1 * 0.5;
152   float2 f = frac(pos / g_StepXY_P1);
153
154   half3 columntaps1 = weight((1.0 - f.y) / 2.0);
155   half3 columntaps2 = weight((1.0 - f.y) / 2.0 + 0.5);
156
157   // kernel generation code made sure taps add up to 1, no need to adjust here.
158
159   float2 xystart;
160   xystart.x = In.TextureUV.x;
161   xystart.y = (-2.0 - f.y) * g_StepXY_P1.y + In.TextureUV.y;
162
163   float3 ypos1 = float3(
164       xystart.y,
165       xystart.y + g_StepXY_P1.y,
166       xystart.y + g_StepXY_P1.y * 2.0);
167   float3 ypos2 = half3(
168       xystart.y + g_StepXY_P1.y * 3.0,
169       xystart.y + g_StepXY_P1.y * 4.0,
170       xystart.y + g_StepXY_P1.y * 5.0);
171
172   OUT.RGBColor.rgb = getRow(xystart.x, ypos1, ypos2, columntaps1, columntaps2);
173   OUT.RGBColor.a = 1.0;
174
175   return OUT;
176 }
177
178 technique SCALER_T
179 {
180   pass P0
181   {
182     PixelShader  = compile ps_3_0 CONVOLUTION6x6Horiz();
183     ZEnable = False;
184     FillMode = Solid;
185     FogEnable = False;
186   }
187   pass P1
188   {
189     PixelShader  = compile ps_3_0 CONVOLUTION6x6Vert();
190     ZEnable = False;
191     FillMode = Solid;
192     FogEnable = False;
193   }
194 };