|
| 1 | +/* YUV-to-RGBA Effect |
| 2 | + * Written by Ethan "flibitijibibo" Lee |
| 3 | + * http://www.flibitijibibo.com/ |
| 4 | + * |
| 5 | + * This effect is based on the YUV-to-RGBA GLSL shader found in SDL. |
| 6 | + * Thus, it also released under the zlib license: |
| 7 | + * http://libsdl.org/license.php |
| 8 | + */ |
| 9 | + |
| 10 | +sampler samp0 : register(s0); |
| 11 | +sampler samp1 : register(s1); |
| 12 | +sampler samp2 : register(s2); |
| 13 | + |
| 14 | +float4 RescaleFactor : register(ps, c0) = float4(1, 1, 1, 1); |
| 15 | + |
| 16 | +void VS(inout float2 tex : TEXCOORD0, |
| 17 | + inout float4 pos : SV_Position) |
| 18 | +{ |
| 19 | + pos.w = 1.0; |
| 20 | +} |
| 21 | + |
| 22 | +float4 PS(float2 tex : TEXCOORD0) : SV_Target0 |
| 23 | +{ |
| 24 | + const float3 offset = float3(-0.0625, -0.5, -0.5); |
| 25 | + |
| 26 | + /* More info about colorspace conversion: |
| 27 | + * http://www.equasys.de/colorconversion.html |
| 28 | + * http://www.equasys.de/colorformat.html |
| 29 | + */ |
| 30 | +#if 1 |
| 31 | + /* ITU-R BT.709 */ |
| 32 | + const float3 Rcoeff = float3(1.164, 0.000, 1.793); |
| 33 | + const float3 Gcoeff = float3(1.164, -0.213, -0.533); |
| 34 | + const float3 Bcoeff = float3(1.164, 2.112, 0.000); |
| 35 | +#else |
| 36 | + /* ITU-R BT.601 */ |
| 37 | + const float3 Rcoeff = float3(1.164, 0.000, 1.596); |
| 38 | + const float3 Gcoeff = float3(1.164, -0.391, -0.813); |
| 39 | + const float3 Bcoeff = float3(1.164, 2.018, 0.000); |
| 40 | +#endif |
| 41 | + |
| 42 | + float3 yuv; |
| 43 | + yuv.x = tex2D(samp0, tex).r; |
| 44 | + yuv.y = tex2D(samp1, tex).r; |
| 45 | + yuv.z = tex2D(samp2, tex).r; |
| 46 | + yuv *= RescaleFactor; |
| 47 | + yuv += offset; |
| 48 | + |
| 49 | + float4 rgba; |
| 50 | + rgba.x = dot(yuv, Rcoeff); |
| 51 | + rgba.y = dot(yuv, Gcoeff); |
| 52 | + rgba.z = dot(yuv, Bcoeff); |
| 53 | + rgba.w = 1.0; |
| 54 | + return rgba; |
| 55 | +} |
| 56 | + |
| 57 | +Technique T |
| 58 | +{ |
| 59 | + Pass P |
| 60 | + { |
| 61 | + VertexShader = compile vs_3_0 VS(); |
| 62 | + PixelShader = compile ps_3_0 PS(); |
| 63 | + } |
| 64 | +} |
0 commit comments