aboutsummaryrefslogtreecommitdiff
path: root/system/shaders/yuv2rgb_d3d.fx
blob: 7621c60b7e69523764b08b786f3c50bd9e3801cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 *      Copyright (C) 2005-2013 Team XBMC
 *      http://xbmc.org
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with XBMC; see the file COPYING.  If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 */

texture2D  g_YTexture;
texture2D  g_UTexture;
texture2D  g_VTexture;
float4x4   g_ColorMatrix;
float2     g_StepXY;
float      g_viewportWidth;
float      g_viewportHeight;


SamplerState YUVSampler : IMMUTABLE
{
  AddressU = CLAMP;
  AddressV = CLAMP;
  Filter   = MIN_MAG_MIP_LINEAR;
};
#ifdef NV12_SNORM_UV
SamplerState UVSamplerSNORM : IMMUTABLE
{
  AddressU = CLAMP;
  AddressV = CLAMP;
  Filter   = MIN_MAG_MIP_POINT;
};
#endif

struct VS_INPUT
{
  float4 Position   : POSITION;
  float2 TextureY   : TEXCOORD0;
  float2 TextureU   : TEXCOORD1;
  float2 TextureV   : TEXCOORD2;
};

struct VS_OUTPUT
{
  float4 Position   : SV_POSITION;
  float2 TextureY   : TEXCOORD0;
  float2 TextureU   : TEXCOORD1;
  float2 TextureV   : TEXCOORD2;
};

VS_OUTPUT VS(VS_INPUT In)
{
  VS_OUTPUT output = (VS_OUTPUT)0;
  output.Position.x =  (In.Position.x / (g_viewportWidth  / 2.0)) - 1;
  output.Position.y = -(In.Position.y / (g_viewportHeight / 2.0)) + 1;
  output.Position.z = output.Position.z;
  output.Position.w = 1.0;
  output.TextureY   = In.TextureY;
  output.TextureU   = In.TextureU;
  output.TextureV   = In.TextureV;

  return output;
}

#ifdef NV12_SNORM_UV
inline float unormU(float c)
{
  c *= 0.5;
  if (c < 0.0) c += 1.0;
  return saturate(c);
}
inline float2 unormUV(float2 rg)
{
  return float2(unormU(rg.x), unormU(rg.y));
}
#endif

float4 YUV2RGB(VS_OUTPUT In) : SV_TARGET
{
#if defined(XBMC_YV12) //|| defined(XBMC_NV12)
  float4 YUV = float4(g_YTexture.Sample(YUVSampler, In.TextureY).r
                    , g_UTexture.Sample(YUVSampler, In.TextureU).r
                    , g_VTexture.Sample(YUVSampler, In.TextureV).r
                    , 1.0);
#elif defined(XBMC_NV12)
  float4 YUV = float4(g_YTexture.Sample(YUVSampler, In.TextureY).r
  #if defined(NV12_SNORM_UV)
                    , unormUV(g_UTexture.Sample(UVSamplerSNORM, In.TextureU).rg)
  #else
                    , g_UTexture.Sample(YUVSampler, In.TextureU).rg
  #endif
                    , 1.0);
#elif defined(XBMC_YUY2) || defined(XBMC_UYVY)
  // The HLSL compiler is smart enough to optimize away these redundant assignments.
  // That way the code is almost identical to the OGL shader.
  float2 stepxy = g_StepXY;
  float2 pos    = In.TextureY;
  pos           = float2(pos.x - (stepxy.x * 0.25), pos.y);
  float2 f      = frac(pos / stepxy);

  //y axis will be correctly interpolated by opengl
  //x axis will not, so we grab two pixels at the center of two columns and interpolate ourselves
  float4 c1 = g_YTexture.Sample(YUVSampler, float2(pos.x + ((0.5 - f.x) * stepxy.x), pos.y));
  float4 c2 = g_YTexture.Sample(YUVSampler, float2(pos.x + ((1.5 - f.x) * stepxy.x), pos.y));

  /* each pixel has two Y subpixels and one UV subpixel
      YUV  Y  YUV
      check if we're left or right of the middle Y subpixel and interpolate accordingly*/
  #if defined(XBMC_YUY2) // BGRA = YUYV
    float  leftY  = lerp(c1.b, c1.r, f.x * 2.0);
    float  rightY = lerp(c1.r, c2.b, f.x * 2.0 - 1.0);
    float2 outUV  = lerp(c1.ga, c2.ga, f.x);
  #elif defined(XBMC_UYVY) // BGRA = UYVY
    float  leftY  = lerp(c1.g, c1.a, f.x * 2.0);
    float  rightY = lerp(c1.a, c2.g, f.x * 2.0 - 1.0);
    float2 outUV  = lerp(c1.br, c2.br, f.x);
  #endif
    float  outY   = lerp(leftY, rightY, step(0.5, f.x));
    float4 YUV    = float4(outY, outUV, 1.0);
#endif

  return float4(mul(YUV, g_ColorMatrix).rgb, 1.0);
}

technique10 YUV2RGB_T
{
  pass P0
  {
    SetVertexShader( CompileShader( vs_4_0_level_9_1, VS() ) );
    SetGeometryShader( NULL );
    SetPixelShader( CompileShader( ps_4_0_level_9_1, YUV2RGB() ) );
  }
};