gfx/layers/d3d11/CompositorD3D11.fx

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:be4d2d99c279
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 typedef float4 rect;
7
8 float4x4 mLayerTransform : register(vs, c0);
9 float4x4 mProjection : register(vs, c4);
10 float4 vRenderTargetOffset : register(vs, c8);
11 rect vTextureCoords : register(vs, c9);
12 rect vLayerQuad : register(vs, c10);
13 rect vMaskQuad : register(vs, c11);
14
15 float4 fLayerColor : register(ps, c0);
16 float fLayerOpacity : register(ps, c1);
17
18 sampler sSampler : register(ps, s0);
19
20 Texture2D tRGB;
21 Texture2D tY;
22 Texture2D tCb;
23 Texture2D tCr;
24 Texture2D tRGBWhite;
25 // Always bind this to slot 3 since this is always available!
26 Texture2D tMask : register(ps, t3);
27
28 struct VS_INPUT {
29 float2 vPosition : POSITION;
30 };
31
32 struct VS_OUTPUT {
33 float4 vPosition : SV_Position;
34 float2 vTexCoords : TEXCOORD0;
35 };
36
37 struct VS_MASK_OUTPUT {
38 float4 vPosition : SV_Position;
39 float2 vTexCoords : TEXCOORD0;
40 float2 vMaskCoords : TEXCOORD1;
41 };
42
43 struct VS_MASK_3D_OUTPUT {
44 float4 vPosition : SV_Position;
45 float2 vTexCoords : TEXCOORD0;
46 float3 vMaskCoords : TEXCOORD1;
47 };
48
49 struct PS_OUTPUT {
50 float4 vSrc;
51 float4 vAlpha;
52 };
53
54 float2 TexCoords(const float2 aPosition)
55 {
56 float2 result;
57 const float2 size = vTextureCoords.zw;
58 result.x = vTextureCoords.x + aPosition.x * size.x;
59 result.y = vTextureCoords.y + aPosition.y * size.y;
60
61 return result;
62 }
63
64 SamplerState LayerTextureSamplerLinear
65 {
66 Filter = MIN_MAG_MIP_LINEAR;
67 AddressU = Clamp;
68 AddressV = Clamp;
69 };
70
71 float4 TransformedPosition(float2 aInPosition)
72 {
73 // the current vertex's position on the quad
74 float4 position = float4(0, 0, 0, 1);
75
76 // We use 4 component floats to uniquely describe a rectangle, by the structure
77 // of x, y, width, height. This allows us to easily generate the 4 corners
78 // of any rectangle from the 4 corners of the 0,0-1,1 quad that we use as the
79 // stream source for our LayerQuad vertex shader. We do this by doing:
80 // Xout = x + Xin * width
81 // Yout = y + Yin * height
82 float2 size = vLayerQuad.zw;
83 position.x = vLayerQuad.x + aInPosition.x * size.x;
84 position.y = vLayerQuad.y + aInPosition.y * size.y;
85
86 position = mul(mLayerTransform, position);
87
88 return position;
89 }
90
91 float4 VertexPosition(float4 aTransformedPosition)
92 {
93 float4 result;
94 result.w = aTransformedPosition.w;
95 result.xyz = aTransformedPosition.xyz / aTransformedPosition.w;
96 result -= vRenderTargetOffset;
97 result.xyz *= result.w;
98
99 result = mul(mProjection, result);
100
101 return result;
102 }
103
104 VS_OUTPUT LayerQuadVS(const VS_INPUT aVertex)
105 {
106 VS_OUTPUT outp;
107 float4 position = TransformedPosition(aVertex.vPosition);
108
109 outp.vPosition = VertexPosition(position);
110 outp.vTexCoords = TexCoords(aVertex.vPosition.xy);
111
112 return outp;
113 }
114
115 VS_MASK_OUTPUT LayerQuadMaskVS(const VS_INPUT aVertex)
116 {
117 VS_MASK_OUTPUT outp;
118 float4 position = TransformedPosition(aVertex.vPosition);
119
120 outp.vPosition = VertexPosition(position);
121
122 // calculate the position on the mask texture
123 outp.vMaskCoords.x = (position.x - vMaskQuad.x) / vMaskQuad.z;
124 outp.vMaskCoords.y = (position.y - vMaskQuad.y) / vMaskQuad.w;
125
126 outp.vTexCoords = TexCoords(aVertex.vPosition.xy);
127
128 return outp;
129 }
130
131 VS_MASK_3D_OUTPUT LayerQuadMask3DVS(const VS_INPUT aVertex)
132 {
133 VS_MASK_3D_OUTPUT outp;
134 float4 position = TransformedPosition(aVertex.vPosition);
135
136 outp.vPosition = VertexPosition(position);
137
138 // calculate the position on the mask texture
139 position.xyz /= position.w;
140 outp.vMaskCoords.x = (position.x - vMaskQuad.x) / vMaskQuad.z;
141 outp.vMaskCoords.y = (position.y - vMaskQuad.y) / vMaskQuad.w;
142 // We use the w coord to do non-perspective correct interpolation:
143 // the quad might be transformed in 3D, in which case it will have some
144 // perspective. The graphics card will do perspective-correct interpolation
145 // of the texture, but our mask is already transformed and so we require
146 // linear interpolation. Therefore, we must correct the interpolation
147 // ourselves, we do this by multiplying all coords by w here, and dividing by
148 // w in the pixel shader (post-interpolation), we pass w in outp.vMaskCoords.z.
149 // See http://en.wikipedia.org/wiki/Texture_mapping#Perspective_correctness
150 outp.vMaskCoords.z = 1;
151 outp.vMaskCoords *= position.w;
152
153 outp.vTexCoords = TexCoords(aVertex.vPosition.xy);
154
155 return outp;
156 }
157
158 float4 RGBAShaderMask(const VS_MASK_OUTPUT aVertex) : SV_Target
159 {
160 float2 maskCoords = aVertex.vMaskCoords;
161 float mask = tMask.Sample(sSampler, maskCoords).a;
162 return tRGB.Sample(sSampler, aVertex.vTexCoords) * fLayerOpacity * mask;
163 }
164
165 float4 RGBAShaderMask3D(const VS_MASK_3D_OUTPUT aVertex) : SV_Target
166 {
167 float2 maskCoords = aVertex.vMaskCoords.xy / aVertex.vMaskCoords.z;
168 float mask = tMask.Sample(LayerTextureSamplerLinear, maskCoords).a;
169 return tRGB.Sample(sSampler, aVertex.vTexCoords) * fLayerOpacity * mask;
170 }
171
172 float4 RGBShaderMask(const VS_MASK_OUTPUT aVertex) : SV_Target
173 {
174 float4 result;
175 result = tRGB.Sample(sSampler, aVertex.vTexCoords) * fLayerOpacity;
176 result.a = fLayerOpacity;
177
178 float2 maskCoords = aVertex.vMaskCoords;
179 float mask = tMask.Sample(sSampler, maskCoords).a;
180 return result * mask;
181 }
182
183 float4 CalculateYCbCrColor(const float2 aTexCoords)
184 {
185 float4 yuv;
186 float4 color;
187
188 yuv.r = tCr.Sample(sSampler, aTexCoords).a - 0.5;
189 yuv.g = tY.Sample(sSampler, aTexCoords).a - 0.0625;
190 yuv.b = tCb.Sample(sSampler, aTexCoords).a - 0.5;
191
192 color.r = yuv.g * 1.164 + yuv.r * 1.596;
193 color.g = yuv.g * 1.164 - 0.813 * yuv.r - 0.391 * yuv.b;
194 color.b = yuv.g * 1.164 + yuv.b * 2.018;
195 color.a = 1.0f;
196
197 return color;
198 }
199
200 float4 YCbCrShaderMask(const VS_MASK_OUTPUT aVertex) : SV_Target
201 {
202 float2 maskCoords = aVertex.vMaskCoords;
203 float mask = tMask.Sample(sSampler, maskCoords).a;
204
205 return CalculateYCbCrColor(aVertex.vTexCoords) * fLayerOpacity * mask;
206 }
207
208 PS_OUTPUT ComponentAlphaShaderMask(const VS_MASK_OUTPUT aVertex) : SV_Target
209 {
210 PS_OUTPUT result;
211
212 result.vSrc = tRGB.Sample(sSampler, aVertex.vTexCoords);
213 result.vAlpha = 1.0 - tRGBWhite.Sample(sSampler, aVertex.vTexCoords) + result.vSrc;
214 result.vSrc.a = result.vAlpha.g;
215
216 float2 maskCoords = aVertex.vMaskCoords;
217 float mask = tMask.Sample(sSampler, maskCoords).a;
218 result.vSrc *= fLayerOpacity * mask;
219 result.vAlpha *= fLayerOpacity * mask;
220
221 return result;
222 }
223
224 float4 SolidColorShaderMask(const VS_MASK_OUTPUT aVertex) : SV_Target
225 {
226 float2 maskCoords = aVertex.vMaskCoords;
227 float mask = tMask.Sample(sSampler, maskCoords).a;
228 return fLayerColor * mask;
229 }
230
231 /*
232 * Un-masked versions
233 *************************************************************
234 */
235 float4 RGBAShader(const VS_OUTPUT aVertex) : SV_Target
236 {
237 return tRGB.Sample(sSampler, aVertex.vTexCoords) * fLayerOpacity;
238 }
239
240 float4 RGBShader(const VS_OUTPUT aVertex) : SV_Target
241 {
242 float4 result;
243 result = tRGB.Sample(sSampler, aVertex.vTexCoords) * fLayerOpacity;
244 result.a = fLayerOpacity;
245 return result;
246 }
247
248 float4 YCbCrShader(const VS_OUTPUT aVertex) : SV_Target
249 {
250 return CalculateYCbCrColor(aVertex.vTexCoords) * fLayerOpacity;
251 }
252
253 PS_OUTPUT ComponentAlphaShader(const VS_OUTPUT aVertex) : SV_Target
254 {
255 PS_OUTPUT result;
256
257 result.vSrc = tRGB.Sample(sSampler, aVertex.vTexCoords);
258 result.vAlpha = 1.0 - tRGBWhite.Sample(sSampler, aVertex.vTexCoords) + result.vSrc;
259 result.vSrc.a = result.vAlpha.g;
260 result.vSrc *= fLayerOpacity;
261 result.vAlpha *= fLayerOpacity;
262 return result;
263 }
264
265 float4 SolidColorShader(const VS_OUTPUT aVertex) : SV_Target
266 {
267 return fLayerColor;
268 }

mercurial