format_conversion.effect 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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_width;
  27. uniform float input_height;
  28. uniform float input_width_i;
  29. uniform float input_height_i;
  30. uniform float input_width_i_d2;
  31. uniform float input_height_i_d2;
  32. uniform texture2d image;
  33. sampler_state def_sampler {
  34. Filter = Linear;
  35. AddressU = Clamp;
  36. AddressV = Clamp;
  37. };
  38. struct VertInOut {
  39. float4 pos : POSITION;
  40. float2 uv : TEXCOORD0;
  41. };
  42. VertInOut VSDefault(VertInOut vert_in)
  43. {
  44. VertInOut vert_out;
  45. vert_out.pos = mul(float4(vert_in.pos.xyz, 1.0), ViewProj);
  46. vert_out.uv = vert_in.uv;
  47. return vert_out;
  48. }
  49. /* used to prevent internal GPU precision issues width fmod in particular */
  50. #define PRECISION_OFFSET 0.2
  51. float4 PSNV12(VertInOut vert_in) : TARGET
  52. {
  53. float v_mul = floor(vert_in.uv.y * input_height);
  54. float byte_offset = floor((v_mul + vert_in.uv.x) * width) * 4.0;
  55. byte_offset += PRECISION_OFFSET;
  56. float2 sample_pos[4];
  57. if (byte_offset < u_plane_offset) {
  58. #ifdef DEBUGGING
  59. return float4(1.0, 1.0, 1.0, 1.0);
  60. #endif
  61. float lum_u = floor(fmod(byte_offset, width)) * width_i;
  62. float lum_v = floor(byte_offset * width_i) * height_i;
  63. /* move to texel centers to sample the 4 pixels properly */
  64. lum_u += width_i * 0.5;
  65. lum_v += height_i * 0.5;
  66. sample_pos[0] = float2(lum_u, lum_v);
  67. sample_pos[1] = float2(lum_u += width_i, lum_v);
  68. sample_pos[2] = float2(lum_u += width_i, lum_v);
  69. sample_pos[3] = float2(lum_u + width_i, lum_v);
  70. float4x4 out_val = float4x4(
  71. image.Sample(def_sampler, sample_pos[0]),
  72. image.Sample(def_sampler, sample_pos[1]),
  73. image.Sample(def_sampler, sample_pos[2]),
  74. image.Sample(def_sampler, sample_pos[3])
  75. );
  76. return transpose(out_val)[1];
  77. } else {
  78. #ifdef DEBUGGING
  79. return float4(0.5, 0.2, 0.5, 0.2);
  80. #endif
  81. float new_offset = byte_offset - u_plane_offset;
  82. float ch_u = floor(fmod(new_offset, width)) * width_i;
  83. float ch_v = floor(new_offset * width_i) * height_d2_i;
  84. float width_i2 = width_i*2.0;
  85. /* move to the borders of each set of 4 pixels to force it
  86. * to do bilinear averaging */
  87. ch_u += width_i;
  88. ch_v += height_i;
  89. sample_pos[0] = float2(ch_u, ch_v);
  90. sample_pos[1] = float2(ch_u + width_i2, ch_v);
  91. return float4(
  92. image.Sample(def_sampler, sample_pos[0]).rb,
  93. image.Sample(def_sampler, sample_pos[1]).rb
  94. );
  95. }
  96. }
  97. float4 PSPlanar420(VertInOut vert_in) : TARGET
  98. {
  99. float v_mul = floor(vert_in.uv.y * input_height);
  100. float byte_offset = floor((v_mul + vert_in.uv.x) * width) * 4.0;
  101. byte_offset += PRECISION_OFFSET;
  102. float2 sample_pos[4];
  103. if (byte_offset < u_plane_offset) {
  104. #ifdef DEBUGGING
  105. return float4(1.0, 1.0, 1.0, 1.0);
  106. #endif
  107. float lum_u = floor(fmod(byte_offset, width)) * width_i;
  108. float lum_v = floor(byte_offset * width_i) * height_i;
  109. /* move to texel centers to sample the 4 pixels properly */
  110. lum_u += width_i * 0.5;
  111. lum_v += height_i * 0.5;
  112. sample_pos[0] = float2(lum_u, lum_v);
  113. sample_pos[1] = float2(lum_u += width_i, lum_v);
  114. sample_pos[2] = float2(lum_u += width_i, lum_v);
  115. sample_pos[3] = float2(lum_u + width_i, lum_v);
  116. } else {
  117. #ifdef DEBUGGING
  118. return ((byte_offset < v_plane_offset) ?
  119. float4(0.5, 0.5, 0.5, 0.5) :
  120. float4(0.2, 0.2, 0.2, 0.2));
  121. #endif
  122. float new_offset = byte_offset -
  123. ((byte_offset < v_plane_offset) ?
  124. u_plane_offset : v_plane_offset);
  125. float ch_u = floor(fmod(new_offset, width_d2)) * width_d2_i;
  126. float ch_v = floor(new_offset * width_d2_i) * height_d2_i;
  127. float width_i2 = width_i*2.0;
  128. /* move to the borders of each set of 4 pixels to force it
  129. * to do bilinear averaging */
  130. ch_u += width_i;
  131. ch_v += height_i;
  132. sample_pos[0] = float2(ch_u, ch_v);
  133. sample_pos[1] = float2(ch_u += width_i2, ch_v);
  134. sample_pos[2] = float2(ch_u += width_i2, ch_v);
  135. sample_pos[3] = float2(ch_u + width_i2, ch_v);
  136. }
  137. float4x4 out_val = float4x4(
  138. image.Sample(def_sampler, sample_pos[0]),
  139. image.Sample(def_sampler, sample_pos[1]),
  140. image.Sample(def_sampler, sample_pos[2]),
  141. image.Sample(def_sampler, sample_pos[3])
  142. );
  143. out_val = transpose(out_val);
  144. if (byte_offset < u_plane_offset)
  145. return out_val[1];
  146. else if (byte_offset < v_plane_offset)
  147. return out_val[0];
  148. else
  149. return out_val[2];
  150. }
  151. float4 PSPlanar444(VertInOut vert_in) : TARGET
  152. {
  153. float v_mul = floor(vert_in.uv.y * input_height);
  154. float byte_offset = floor((v_mul + vert_in.uv.x) * width) * 4.0;
  155. byte_offset += PRECISION_OFFSET;
  156. float new_byte_offset = byte_offset;
  157. if (byte_offset >= v_plane_offset)
  158. new_byte_offset -= v_plane_offset;
  159. else if (byte_offset >= u_plane_offset)
  160. new_byte_offset -= u_plane_offset;
  161. float2 sample_pos[4];
  162. float u_val = floor(fmod(new_byte_offset, width)) * width_i;
  163. float v_val = floor(new_byte_offset * width_i) * height_i;
  164. /* move to texel centers to sample the 4 pixels properly */
  165. u_val += width_i * 0.5;
  166. v_val += height_i * 0.5;
  167. sample_pos[0] = float2(u_val, v_val);
  168. sample_pos[1] = float2(u_val += width_i, v_val);
  169. sample_pos[2] = float2(u_val += width_i, v_val);
  170. sample_pos[3] = float2(u_val + width_i, v_val);
  171. float4x4 out_val = float4x4(
  172. image.Sample(def_sampler, sample_pos[0]),
  173. image.Sample(def_sampler, sample_pos[1]),
  174. image.Sample(def_sampler, sample_pos[2]),
  175. image.Sample(def_sampler, sample_pos[3])
  176. );
  177. out_val = transpose(out_val);
  178. if (byte_offset < u_plane_offset)
  179. return out_val[1];
  180. else if (byte_offset < v_plane_offset)
  181. return out_val[0];
  182. else
  183. return out_val[2];
  184. }
  185. float4 PSPacked422_Reverse(VertInOut vert_in, int u_pos, int v_pos,
  186. int y0_pos, int y1_pos) : TARGET
  187. {
  188. float y = vert_in.uv.y;
  189. float odd = floor(fmod(width * vert_in.uv.x + PRECISION_OFFSET, 2.0));
  190. float x = floor(width_d2 * vert_in.uv.x + PRECISION_OFFSET) *
  191. width_d2_i;
  192. x += input_width_i_d2;
  193. float4 texel = image.Sample(def_sampler, float2(x, y));
  194. return float4(odd > 0.5 ? texel[y1_pos] : texel[y0_pos],
  195. texel[u_pos], texel[v_pos], 1.0);
  196. }
  197. float GetOffsetColor(float offset)
  198. {
  199. float2 uv;
  200. offset += PRECISION_OFFSET;
  201. uv.x = floor(fmod(offset, input_width)) * input_width_i;
  202. uv.y = floor(offset * input_width_i) * input_height_i;
  203. uv.xy += float2(input_width_i_d2, input_height_i_d2);
  204. return image.Sample(def_sampler, uv).r;
  205. }
  206. float4 PSPlanar420_Reverse(VertInOut vert_in) : TARGET
  207. {
  208. float x = vert_in.uv.x;
  209. float y = vert_in.uv.y;
  210. float x_offset = floor(x * width + PRECISION_OFFSET);
  211. float y_offset = floor(y * height + PRECISION_OFFSET);
  212. float lum_offset = y_offset * width + x_offset + PRECISION_OFFSET;
  213. lum_offset = floor(lum_offset);
  214. float ch_offset = floor(y_offset * 0.5 + PRECISION_OFFSET) * width_d2 +
  215. (x_offset * 0.5) + PRECISION_OFFSET;
  216. ch_offset = floor(ch_offset);
  217. return float4(
  218. GetOffsetColor(lum_offset),
  219. GetOffsetColor(u_plane_offset + ch_offset),
  220. GetOffsetColor(v_plane_offset + ch_offset),
  221. 1.0
  222. );
  223. }
  224. float4 PSNV12_Reverse(VertInOut vert_in) : TARGET
  225. {
  226. float x = vert_in.uv.x;
  227. float y = vert_in.uv.y;
  228. float x_offset = floor(x * width + PRECISION_OFFSET);
  229. float y_offset = floor(y * height + PRECISION_OFFSET);
  230. float lum_offset = y_offset * width + x_offset + PRECISION_OFFSET;
  231. lum_offset = floor(lum_offset);
  232. float ch_offset = floor(y_offset * 0.5 + PRECISION_OFFSET) * width_d2 +
  233. (x_offset * 0.5);
  234. ch_offset = floor(ch_offset * 2.0 + PRECISION_OFFSET);
  235. return float4(
  236. GetOffsetColor(lum_offset),
  237. GetOffsetColor(u_plane_offset + ch_offset),
  238. GetOffsetColor(u_plane_offset + ch_offset + 1.0),
  239. 1.0
  240. );
  241. }
  242. technique Planar420
  243. {
  244. pass
  245. {
  246. vertex_shader = VSDefault(vert_in);
  247. pixel_shader = PSPlanar420(vert_in);
  248. }
  249. }
  250. technique Planar444
  251. {
  252. pass
  253. {
  254. vertex_shader = VSDefault(vert_in);
  255. pixel_shader = PSPlanar444(vert_in);
  256. }
  257. }
  258. technique NV12
  259. {
  260. pass
  261. {
  262. vertex_shader = VSDefault(vert_in);
  263. pixel_shader = PSNV12(vert_in);
  264. }
  265. }
  266. technique UYVY_Reverse
  267. {
  268. pass
  269. {
  270. vertex_shader = VSDefault(vert_in);
  271. pixel_shader = PSPacked422_Reverse(vert_in, 2, 0, 1, 3);
  272. }
  273. }
  274. technique YUY2_Reverse
  275. {
  276. pass
  277. {
  278. vertex_shader = VSDefault(vert_in);
  279. pixel_shader = PSPacked422_Reverse(vert_in, 1, 3, 2, 0);
  280. }
  281. }
  282. technique YVYU_Reverse
  283. {
  284. pass
  285. {
  286. vertex_shader = VSDefault(vert_in);
  287. pixel_shader = PSPacked422_Reverse(vert_in, 3, 1, 2, 0);
  288. }
  289. }
  290. technique I420_Reverse
  291. {
  292. pass
  293. {
  294. vertex_shader = VSDefault(vert_in);
  295. pixel_shader = PSPlanar420_Reverse(vert_in);
  296. }
  297. }
  298. technique NV12_Reverse
  299. {
  300. pass
  301. {
  302. vertex_shader = VSDefault(vert_in);
  303. pixel_shader = PSNV12_Reverse(vert_in);
  304. }
  305. }