format_conversion.effect 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /******************************************************************************
  2. Copyright (C) 2014 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. //#define DEBUGGING
  15. uniform float4x4 ViewProj;
  16. uniform float u_plane_offset;
  17. uniform float v_plane_offset;
  18. uniform float width;
  19. uniform float height;
  20. uniform float width_i;
  21. uniform float height_i;
  22. uniform float width_d2;
  23. uniform float height_d2;
  24. uniform float width_d2_i;
  25. uniform float height_d2_i;
  26. uniform float input_height;
  27. uniform texture2d image;
  28. sampler_state def_sampler {
  29. Filter = Linear;
  30. AddressU = Clamp;
  31. AddressV = Clamp;
  32. };
  33. struct VertInOut {
  34. float4 pos : POSITION;
  35. float2 uv : TEXCOORD0;
  36. };
  37. VertInOut VSDefault(VertInOut vert_in)
  38. {
  39. VertInOut vert_out;
  40. vert_out.pos = mul(float4(vert_in.pos.xyz, 1.0), ViewProj);
  41. vert_out.uv = vert_in.uv;
  42. return vert_out;
  43. }
  44. /* used to prevent internal GPU precision issues width fmod in particular */
  45. #define PRECISION_OFFSET 0.1
  46. float4 PSPlanar420(VertInOut vert_in) : TARGET
  47. {
  48. #ifdef _OPENGL
  49. float v_mul = floor((1.0 - vert_in.uv.y) * input_height);
  50. #else
  51. float v_mul = floor(vert_in.uv.y * input_height);
  52. #endif
  53. float byte_offset = floor((v_mul + vert_in.uv.x) * width) * 4.0;
  54. byte_offset += PRECISION_OFFSET;
  55. float2 sample_pos[4];
  56. if (byte_offset < u_plane_offset) {
  57. #ifdef DEBUGGING
  58. return float4(1.0, 1.0, 1.0, 1.0);
  59. #endif
  60. float lum_u = floor(fmod(byte_offset, width)) * width_i;
  61. float lum_v = floor(byte_offset * width_i) * height_i;
  62. /* move to texel centers to sample the 4 pixels properly */
  63. lum_u += width_i * 0.5;
  64. lum_v += height_i * 0.5;
  65. sample_pos[0] = float2(lum_u, lum_v);
  66. sample_pos[1] = float2(lum_u += width_i, lum_v);
  67. sample_pos[2] = float2(lum_u += width_i, lum_v);
  68. sample_pos[3] = float2(lum_u + width_i, lum_v);
  69. } else {
  70. #ifdef DEBUGGING
  71. return ((byte_offset < v_plane_offset) ?
  72. float4(0.5, 0.5, 0.5, 0.5) :
  73. float4(0.2, 0.2, 0.2, 0.2));
  74. #endif
  75. float new_offset = byte_offset -
  76. ((byte_offset < v_plane_offset) ?
  77. u_plane_offset : v_plane_offset);
  78. float ch_u = floor(fmod(new_offset, width_d2)) * width_d2_i;
  79. float ch_v = floor(new_offset * width_d2_i) * height_d2_i;
  80. float width_i2 = width_i*2.0;
  81. /* move to the borders of each set of 4 pixels to force it
  82. * to do bilinear averaging */
  83. ch_u += width_i;
  84. ch_v += height_i;
  85. sample_pos[0] = float2(ch_u, ch_v);
  86. sample_pos[1] = float2(ch_u += width_i2, ch_v);
  87. sample_pos[2] = float2(ch_u += width_i2, ch_v);
  88. sample_pos[3] = float2(ch_u + width_i2, ch_v);
  89. }
  90. float4x4 out_val = float4x4(
  91. image.Sample(def_sampler, sample_pos[0]),
  92. image.Sample(def_sampler, sample_pos[1]),
  93. image.Sample(def_sampler, sample_pos[2]),
  94. image.Sample(def_sampler, sample_pos[3])
  95. );
  96. out_val = transpose(out_val);
  97. if (byte_offset < u_plane_offset)
  98. return out_val[1];
  99. else if (byte_offset < v_plane_offset)
  100. return out_val[0];
  101. else
  102. return out_val[2];
  103. }
  104. technique Planar420
  105. {
  106. pass
  107. {
  108. vertex_shader = VSDefault(vert_in);
  109. pixel_shader = PSPlanar420(vert_in);
  110. }
  111. }