format_conversion.effect 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 PSNV12(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. float4x4 out_val = float4x4(
  70. image.Sample(def_sampler, sample_pos[0]),
  71. image.Sample(def_sampler, sample_pos[1]),
  72. image.Sample(def_sampler, sample_pos[2]),
  73. image.Sample(def_sampler, sample_pos[3])
  74. );
  75. return transpose(out_val)[1];
  76. } else {
  77. #ifdef DEBUGGING
  78. return float4(0.5, 0.2, 0.5, 0.2);
  79. #endif
  80. float new_offset = byte_offset - u_plane_offset;
  81. float ch_u = floor(fmod(new_offset, width)) * width_i;
  82. float ch_v = floor(new_offset * width_i) * height_d2_i;
  83. float width_i2 = width_i*2.0;
  84. /* move to the borders of each set of 4 pixels to force it
  85. * to do bilinear averaging */
  86. ch_u += width_i;
  87. ch_v += height_i;
  88. sample_pos[0] = float2(ch_u, ch_v);
  89. sample_pos[1] = float2(ch_u + width_i2, ch_v);
  90. return float4(
  91. image.Sample(def_sampler, sample_pos[0]).rb,
  92. image.Sample(def_sampler, sample_pos[1]).rb
  93. );
  94. }
  95. }
  96. float4 PSPlanar420(VertInOut vert_in) : TARGET
  97. {
  98. #ifdef _OPENGL
  99. float v_mul = floor((1.0 - vert_in.uv.y) * input_height);
  100. #else
  101. float v_mul = floor(vert_in.uv.y * input_height);
  102. #endif
  103. float byte_offset = floor((v_mul + vert_in.uv.x) * width) * 4.0;
  104. byte_offset += PRECISION_OFFSET;
  105. float2 sample_pos[4];
  106. if (byte_offset < u_plane_offset) {
  107. #ifdef DEBUGGING
  108. return float4(1.0, 1.0, 1.0, 1.0);
  109. #endif
  110. float lum_u = floor(fmod(byte_offset, width)) * width_i;
  111. float lum_v = floor(byte_offset * width_i) * height_i;
  112. /* move to texel centers to sample the 4 pixels properly */
  113. lum_u += width_i * 0.5;
  114. lum_v += height_i * 0.5;
  115. sample_pos[0] = float2(lum_u, lum_v);
  116. sample_pos[1] = float2(lum_u += width_i, lum_v);
  117. sample_pos[2] = float2(lum_u += width_i, lum_v);
  118. sample_pos[3] = float2(lum_u + width_i, lum_v);
  119. } else {
  120. #ifdef DEBUGGING
  121. return ((byte_offset < v_plane_offset) ?
  122. float4(0.5, 0.5, 0.5, 0.5) :
  123. float4(0.2, 0.2, 0.2, 0.2));
  124. #endif
  125. float new_offset = byte_offset -
  126. ((byte_offset < v_plane_offset) ?
  127. u_plane_offset : v_plane_offset);
  128. float ch_u = floor(fmod(new_offset, width_d2)) * width_d2_i;
  129. float ch_v = floor(new_offset * width_d2_i) * height_d2_i;
  130. float width_i2 = width_i*2.0;
  131. /* move to the borders of each set of 4 pixels to force it
  132. * to do bilinear averaging */
  133. ch_u += width_i;
  134. ch_v += height_i;
  135. sample_pos[0] = float2(ch_u, ch_v);
  136. sample_pos[1] = float2(ch_u += width_i2, ch_v);
  137. sample_pos[2] = float2(ch_u += width_i2, ch_v);
  138. sample_pos[3] = float2(ch_u + width_i2, ch_v);
  139. }
  140. float4x4 out_val = float4x4(
  141. image.Sample(def_sampler, sample_pos[0]),
  142. image.Sample(def_sampler, sample_pos[1]),
  143. image.Sample(def_sampler, sample_pos[2]),
  144. image.Sample(def_sampler, sample_pos[3])
  145. );
  146. out_val = transpose(out_val);
  147. if (byte_offset < u_plane_offset)
  148. return out_val[1];
  149. else if (byte_offset < v_plane_offset)
  150. return out_val[0];
  151. else
  152. return out_val[2];
  153. }
  154. float4 PSPacked422_Reverse(VertInOut vert_in, int u_pos, int v_pos,
  155. int y0_pos, int y1_pos) : TARGET
  156. {
  157. float y = vert_in.uv.y;
  158. #ifdef _OPENGL
  159. y = 1. - y;
  160. #endif
  161. float odd = floor(fmod(width * vert_in.uv.x + PRECISION_OFFSET, 2.0));
  162. float x = floor(width_d2 * vert_in.uv.x + PRECISION_OFFSET) *
  163. width_d2_i;
  164. float4 texel = image.Sample(def_sampler, float2(x, y));
  165. return float4(odd > 0.5 ? texel[y1_pos] : texel[y0_pos],
  166. texel[u_pos], texel[v_pos], 1.0);
  167. }
  168. technique Planar420
  169. {
  170. pass
  171. {
  172. vertex_shader = VSDefault(vert_in);
  173. pixel_shader = PSPlanar420(vert_in);
  174. }
  175. }
  176. technique NV12
  177. {
  178. pass
  179. {
  180. vertex_shader = VSDefault(vert_in);
  181. pixel_shader = PSNV12(vert_in);
  182. }
  183. }
  184. technique UYUV_Reverse
  185. {
  186. pass
  187. {
  188. vertex_shader = VSDefault(vert_in);
  189. pixel_shader = PSPacked422_Reverse(vert_in, 0, 2, 1, 3);
  190. }
  191. }
  192. technique YUY2_Reverse
  193. {
  194. pass
  195. {
  196. vertex_shader = VSDefault(vert_in);
  197. pixel_shader = PSPacked422_Reverse(vert_in, 1, 3, 0, 2);
  198. }
  199. }
  200. technique YVYU_Reverse
  201. {
  202. pass
  203. {
  204. vertex_shader = VSDefault(vert_in);
  205. pixel_shader = PSPacked422_Reverse(vert_in, 3, 1, 0, 2);
  206. }
  207. }