format_conversion.effect 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. uniform float width;
  15. uniform float height;
  16. uniform float width_i;
  17. uniform float width_d2;
  18. uniform float height_d2;
  19. uniform float width_x2_i;
  20. uniform float4 color_vec0;
  21. uniform float4 color_vec1;
  22. uniform float4 color_vec2;
  23. uniform float3 color_range_min = {0.0, 0.0, 0.0};
  24. uniform float3 color_range_max = {1.0, 1.0, 1.0};
  25. uniform texture2d image;
  26. uniform texture2d image1;
  27. uniform texture2d image2;
  28. uniform texture2d image3;
  29. sampler_state def_sampler {
  30. Filter = Linear;
  31. AddressU = Clamp;
  32. AddressV = Clamp;
  33. };
  34. struct FragPos {
  35. float4 pos : POSITION;
  36. };
  37. struct VertTexPos {
  38. float2 uv : TEXCOORD0;
  39. float4 pos : POSITION;
  40. };
  41. struct VertPosWide {
  42. float3 pos_wide : TEXCOORD0;
  43. float4 pos : POSITION;
  44. };
  45. struct VertTexPosWide {
  46. float3 uuv : TEXCOORD0;
  47. float4 pos : POSITION;
  48. };
  49. struct FragTex {
  50. float2 uv : TEXCOORD0;
  51. };
  52. struct FragPosWide {
  53. float3 pos_wide : TEXCOORD0;
  54. };
  55. struct FragTexWide {
  56. float3 uuv : TEXCOORD0;
  57. };
  58. FragPos VSPos(uint id : VERTEXID)
  59. {
  60. float idHigh = float(id >> 1);
  61. float idLow = float(id & uint(1));
  62. float x = idHigh * 4.0 - 1.0;
  63. float y = idLow * 4.0 - 1.0;
  64. FragPos vert_out;
  65. vert_out.pos = float4(x, y, 0.0, 1.0);
  66. return vert_out;
  67. }
  68. VertTexPosWide VSTexPos_Left(uint id : VERTEXID)
  69. {
  70. float idHigh = float(id >> 1);
  71. float idLow = float(id & uint(1));
  72. float x = idHigh * 4.0 - 1.0;
  73. float y = idLow * 4.0 - 1.0;
  74. float u_right = idHigh * 2.0;
  75. float u_left = u_right - width_i;
  76. float v = obs_glsl_compile ? (idLow * 2.0) : (1.0 - idLow * 2.0);
  77. VertTexPosWide vert_out;
  78. vert_out.uuv = float3(u_left, u_right, v);
  79. vert_out.pos = float4(x, y, 0.0, 1.0);
  80. return vert_out;
  81. }
  82. VertTexPos VSTexPosHalf_Reverse(uint id : VERTEXID)
  83. {
  84. float idHigh = float(id >> 1);
  85. float idLow = float(id & uint(1));
  86. float x = idHigh * 4.0 - 1.0;
  87. float y = idLow * 4.0 - 1.0;
  88. float u = idHigh * 2.0;
  89. float v = obs_glsl_compile ? (idLow * 2.0) : (1.0 - idLow * 2.0);
  90. VertTexPos vert_out;
  91. vert_out.uv = float2(width_d2 * u, height * v);
  92. vert_out.pos = float4(x, y, 0.0, 1.0);
  93. return vert_out;
  94. }
  95. VertTexPos VSTexPosHalfHalf_Reverse(uint id : VERTEXID)
  96. {
  97. float idHigh = float(id >> 1);
  98. float idLow = float(id & uint(1));
  99. float x = idHigh * 4.0 - 1.0;
  100. float y = idLow * 4.0 - 1.0;
  101. float u = idHigh * 2.0;
  102. float v = obs_glsl_compile ? (idLow * 2.0) : (1.0 - idLow * 2.0);
  103. VertTexPos vert_out;
  104. vert_out.uv = float2(width_d2 * u, height_d2 * v);
  105. vert_out.pos = float4(x, y, 0.0, 1.0);
  106. return vert_out;
  107. }
  108. VertPosWide VSPosWide_Reverse(uint id : VERTEXID)
  109. {
  110. float idHigh = float(id >> 1);
  111. float idLow = float(id & uint(1));
  112. float x = idHigh * 4.0 - 1.0;
  113. float y = idLow * 4.0 - 1.0;
  114. float u = idHigh * 2.0;
  115. float v = obs_glsl_compile ? (idLow * 2.0) : (1.0 - idLow * 2.0);
  116. VertPosWide vert_out;
  117. vert_out.pos_wide = float3(float2(width, width_d2) * u, height * v);
  118. vert_out.pos = float4(x, y, 0.0, 1.0);
  119. return vert_out;
  120. }
  121. float PS_Y(FragPos frag_in) : TARGET
  122. {
  123. float3 rgb = image.Load(int3(frag_in.pos.xy, 0)).rgb;
  124. float y = dot(color_vec0.xyz, rgb) + color_vec0.w;
  125. return y;
  126. }
  127. float2 PS_UV_Wide(FragTexWide frag_in) : TARGET
  128. {
  129. float3 rgb_left = image.Sample(def_sampler, frag_in.uuv.xz).rgb;
  130. float3 rgb_right = image.Sample(def_sampler, frag_in.uuv.yz).rgb;
  131. float3 rgb = (rgb_left + rgb_right) * 0.5;
  132. float u = dot(color_vec1.xyz, rgb) + color_vec1.w;
  133. float v = dot(color_vec2.xyz, rgb) + color_vec2.w;
  134. return float2(u, v);
  135. }
  136. float PS_U(FragPos frag_in) : TARGET
  137. {
  138. float3 rgb = image.Load(int3(frag_in.pos.xy, 0)).rgb;
  139. float u = dot(color_vec1.xyz, rgb) + color_vec1.w;
  140. return u;
  141. }
  142. float PS_V(FragPos frag_in) : TARGET
  143. {
  144. float3 rgb = image.Load(int3(frag_in.pos.xy, 0)).rgb;
  145. float v = dot(color_vec2.xyz, rgb) + color_vec2.w;
  146. return v;
  147. }
  148. float PS_U_Wide(FragTexWide frag_in) : TARGET
  149. {
  150. float3 rgb_left = image.Sample(def_sampler, frag_in.uuv.xz).rgb;
  151. float3 rgb_right = image.Sample(def_sampler, frag_in.uuv.yz).rgb;
  152. float3 rgb = (rgb_left + rgb_right) * 0.5;
  153. float u = dot(color_vec1.xyz, rgb) + color_vec1.w;
  154. return u;
  155. }
  156. float PS_V_Wide(FragTexWide frag_in) : TARGET
  157. {
  158. float3 rgb_left = image.Sample(def_sampler, frag_in.uuv.xz).rgb;
  159. float3 rgb_right = image.Sample(def_sampler, frag_in.uuv.yz).rgb;
  160. float3 rgb = (rgb_left + rgb_right) * 0.5;
  161. float v = dot(color_vec2.xyz, rgb) + color_vec2.w;
  162. return v;
  163. }
  164. float3 YUV_to_RGB(float3 yuv)
  165. {
  166. yuv = clamp(yuv, color_range_min, color_range_max);
  167. float r = dot(color_vec0.xyz, yuv) + color_vec0.w;
  168. float g = dot(color_vec1.xyz, yuv) + color_vec1.w;
  169. float b = dot(color_vec2.xyz, yuv) + color_vec2.w;
  170. return float3(r, g, b);
  171. }
  172. float3 PSUYVY_Reverse(FragTex frag_in) : TARGET
  173. {
  174. float4 y2uv = image.Load(int3(frag_in.uv.xy, 0));
  175. float2 y01 = y2uv.yw;
  176. float2 cbcr = y2uv.zx;
  177. float leftover = frac(frag_in.uv.x);
  178. float y = (leftover < 0.5) ? y01.x : y01.y;
  179. float3 yuv = float3(y, cbcr);
  180. float3 rgb = YUV_to_RGB(yuv);
  181. return rgb;
  182. }
  183. float3 PSYUY2_Reverse(FragTex frag_in) : TARGET
  184. {
  185. float4 y2uv = image.Load(int3(frag_in.uv.xy, 0));
  186. float2 y01 = y2uv.zx;
  187. float2 cbcr = y2uv.yw;
  188. float leftover = frac(frag_in.uv.x);
  189. float y = (leftover < 0.5) ? y01.x : y01.y;
  190. float3 yuv = float3(y, cbcr);
  191. float3 rgb = YUV_to_RGB(yuv);
  192. return rgb;
  193. }
  194. float3 PSYVYU_Reverse(FragTex frag_in) : TARGET
  195. {
  196. float4 y2uv = image.Load(int3(frag_in.uv.xy, 0));
  197. float2 y01 = y2uv.zx;
  198. float2 cbcr = y2uv.wy;
  199. float leftover = frac(frag_in.uv.x);
  200. float y = (leftover < 0.5) ? y01.x : y01.y;
  201. float3 yuv = float3(y, cbcr);
  202. float3 rgb = YUV_to_RGB(yuv);
  203. return rgb;
  204. }
  205. float3 PSPlanar420_Reverse(VertTexPos frag_in) : TARGET
  206. {
  207. float y = image.Load(int3(frag_in.pos.xy, 0)).x;
  208. int3 xy0_chroma = int3(frag_in.uv, 0);
  209. float cb = image1.Load(xy0_chroma).x;
  210. float cr = image2.Load(xy0_chroma).x;
  211. float3 yuv = float3(y, cb, cr);
  212. float3 rgb = YUV_to_RGB(yuv);
  213. return rgb;
  214. }
  215. float4 PSPlanar420A_Reverse(VertTexPos frag_in) : TARGET
  216. {
  217. int3 xy0_luma = int3(frag_in.pos.xy, 0);
  218. float y = image.Load(xy0_luma).x;
  219. int3 xy0_chroma = int3(frag_in.uv, 0);
  220. float cb = image1.Load(xy0_chroma).x;
  221. float cr = image2.Load(xy0_chroma).x;
  222. float alpha = image3.Load(xy0_luma).x;
  223. float3 yuv = float3(y, cb, cr);
  224. float4 rgba = float4(YUV_to_RGB(yuv), alpha);
  225. return rgba;
  226. }
  227. float3 PSPlanar422_Reverse(FragPosWide frag_in) : TARGET
  228. {
  229. float y = image.Load(int3(frag_in.pos_wide.xz, 0)).x;
  230. int3 xy0_chroma = int3(frag_in.pos_wide.yz, 0);
  231. float cb = image1.Load(xy0_chroma).x;
  232. float cr = image2.Load(xy0_chroma).x;
  233. float3 yuv = float3(y, cb, cr);
  234. float3 rgb = YUV_to_RGB(yuv);
  235. return rgb;
  236. }
  237. float4 PSPlanar422A_Reverse(FragPosWide frag_in) : TARGET
  238. {
  239. int3 xy0_luma = int3(frag_in.pos_wide.xz, 0);
  240. float y = image.Load(xy0_luma).x;
  241. int3 xy0_chroma = int3(frag_in.pos_wide.yz, 0);
  242. float cb = image1.Load(xy0_chroma).x;
  243. float cr = image2.Load(xy0_chroma).x;
  244. float alpha = image3.Load(xy0_luma).x;
  245. float3 yuv = float3(y, cb, cr);
  246. float4 rgba = float4(YUV_to_RGB(yuv), alpha);
  247. return rgba;
  248. }
  249. float3 PSPlanar444_Reverse(FragPos frag_in) : TARGET
  250. {
  251. int3 xy0 = int3(frag_in.pos.xy, 0);
  252. float y = image.Load(xy0).x;
  253. float cb = image1.Load(xy0).x;
  254. float cr = image2.Load(xy0).x;
  255. float3 yuv = float3(y, cb, cr);
  256. float3 rgb = YUV_to_RGB(yuv);
  257. return rgb;
  258. }
  259. float4 PSPlanar444A_Reverse(FragPos frag_in) : TARGET
  260. {
  261. int3 xy0 = int3(frag_in.pos.xy, 0);
  262. float y = image.Load(xy0).x;
  263. float cb = image1.Load(xy0).x;
  264. float cr = image2.Load(xy0).x;
  265. float alpha = image3.Load(xy0).x;
  266. float3 yuv = float3(y, cb, cr);
  267. float4 rgba = float4(YUV_to_RGB(yuv), alpha);
  268. return rgba;
  269. }
  270. float4 PSAYUV_Reverse(FragPos frag_in) : TARGET
  271. {
  272. float4 yuva = image.Load(int3(frag_in.pos.xy, 0));
  273. float4 rgba = float4(YUV_to_RGB(yuva.xyz), yuva.a);
  274. return rgba;
  275. }
  276. float3 PSNV12_Reverse(VertTexPos frag_in) : TARGET
  277. {
  278. float y = image.Load(int3(frag_in.pos.xy, 0)).x;
  279. float2 cbcr = image1.Load(int3(frag_in.uv, 0)).xy;
  280. float3 yuv = float3(y, cbcr);
  281. float3 rgb = YUV_to_RGB(yuv);
  282. return rgb;
  283. }
  284. float3 PSY800_Limited(FragPos frag_in) : TARGET
  285. {
  286. float limited = image.Load(int3(frag_in.pos.xy, 0)).x;
  287. float full = (255.0 / 219.0) * limited - (16.0 / 219.0);
  288. return float3(full, full, full);
  289. }
  290. float3 PSY800_Full(FragPos frag_in) : TARGET
  291. {
  292. float3 full = image.Load(int3(frag_in.pos.xy, 0)).xxx;
  293. return full;
  294. }
  295. float4 PSRGB_Limited(FragPos frag_in) : TARGET
  296. {
  297. float4 rgba = image.Load(int3(frag_in.pos.xy, 0));
  298. rgba.rgb = (255.0 / 219.0) * rgba.rgb - (16.0 / 219.0);
  299. return rgba;
  300. }
  301. float3 PSBGR3_Limited(FragPos frag_in) : TARGET
  302. {
  303. float x = frag_in.pos.x * 3.0;
  304. float y = frag_in.pos.y;
  305. float b = image.Load(int3(x - 1.0, y, 0)).x;
  306. float g = image.Load(int3(x, y, 0)).x;
  307. float r = image.Load(int3(x + 1.0, y, 0)).x;
  308. float3 rgb = float3(r, g, b);
  309. rgb = (255.0 / 219.0) * rgb - (16.0 / 219.0);
  310. return rgb;
  311. }
  312. float3 PSBGR3_Full(FragPos frag_in) : TARGET
  313. {
  314. float x = frag_in.pos.x * 3.0;
  315. float y = frag_in.pos.y;
  316. float b = image.Load(int3(x - 1.0, y, 0)).x;
  317. float g = image.Load(int3(x, y, 0)).x;
  318. float r = image.Load(int3(x + 1.0, y, 0)).x;
  319. float3 rgb = float3(r, g, b);
  320. return rgb;
  321. }
  322. technique Planar_Y
  323. {
  324. pass
  325. {
  326. vertex_shader = VSPos(id);
  327. pixel_shader = PS_Y(frag_in);
  328. }
  329. }
  330. technique Planar_U
  331. {
  332. pass
  333. {
  334. vertex_shader = VSPos(id);
  335. pixel_shader = PS_U(frag_in);
  336. }
  337. }
  338. technique Planar_V
  339. {
  340. pass
  341. {
  342. vertex_shader = VSPos(id);
  343. pixel_shader = PS_V(frag_in);
  344. }
  345. }
  346. technique Planar_U_Left
  347. {
  348. pass
  349. {
  350. vertex_shader = VSTexPos_Left(id);
  351. pixel_shader = PS_U_Wide(frag_in);
  352. }
  353. }
  354. technique Planar_V_Left
  355. {
  356. pass
  357. {
  358. vertex_shader = VSTexPos_Left(id);
  359. pixel_shader = PS_V_Wide(frag_in);
  360. }
  361. }
  362. technique NV12_Y
  363. {
  364. pass
  365. {
  366. vertex_shader = VSPos(id);
  367. pixel_shader = PS_Y(frag_in);
  368. }
  369. }
  370. technique NV12_UV
  371. {
  372. pass
  373. {
  374. vertex_shader = VSTexPos_Left(id);
  375. pixel_shader = PS_UV_Wide(frag_in);
  376. }
  377. }
  378. technique UYVY_Reverse
  379. {
  380. pass
  381. {
  382. vertex_shader = VSTexPosHalf_Reverse(id);
  383. pixel_shader = PSUYVY_Reverse(frag_in);
  384. }
  385. }
  386. technique YUY2_Reverse
  387. {
  388. pass
  389. {
  390. vertex_shader = VSTexPosHalf_Reverse(id);
  391. pixel_shader = PSYUY2_Reverse(frag_in);
  392. }
  393. }
  394. technique YVYU_Reverse
  395. {
  396. pass
  397. {
  398. vertex_shader = VSTexPosHalf_Reverse(id);
  399. pixel_shader = PSYVYU_Reverse(frag_in);
  400. }
  401. }
  402. technique I420_Reverse
  403. {
  404. pass
  405. {
  406. vertex_shader = VSTexPosHalfHalf_Reverse(id);
  407. pixel_shader = PSPlanar420_Reverse(frag_in);
  408. }
  409. }
  410. technique I40A_Reverse
  411. {
  412. pass
  413. {
  414. vertex_shader = VSTexPosHalfHalf_Reverse(id);
  415. pixel_shader = PSPlanar420A_Reverse(frag_in);
  416. }
  417. }
  418. technique I422_Reverse
  419. {
  420. pass
  421. {
  422. vertex_shader = VSPosWide_Reverse(id);
  423. pixel_shader = PSPlanar422_Reverse(frag_in);
  424. }
  425. }
  426. technique I42A_Reverse
  427. {
  428. pass
  429. {
  430. vertex_shader = VSPosWide_Reverse(id);
  431. pixel_shader = PSPlanar422A_Reverse(frag_in);
  432. }
  433. }
  434. technique I444_Reverse
  435. {
  436. pass
  437. {
  438. vertex_shader = VSPos(id);
  439. pixel_shader = PSPlanar444_Reverse(frag_in);
  440. }
  441. }
  442. technique YUVA_Reverse
  443. {
  444. pass
  445. {
  446. vertex_shader = VSPos(id);
  447. pixel_shader = PSPlanar444A_Reverse(frag_in);
  448. }
  449. }
  450. technique AYUV_Reverse
  451. {
  452. pass
  453. {
  454. vertex_shader = VSPos(id);
  455. pixel_shader = PSAYUV_Reverse(frag_in);
  456. }
  457. }
  458. technique NV12_Reverse
  459. {
  460. pass
  461. {
  462. vertex_shader = VSTexPosHalfHalf_Reverse(id);
  463. pixel_shader = PSNV12_Reverse(frag_in);
  464. }
  465. }
  466. technique Y800_Limited
  467. {
  468. pass
  469. {
  470. vertex_shader = VSPos(id);
  471. pixel_shader = PSY800_Limited(frag_in);
  472. }
  473. }
  474. technique Y800_Full
  475. {
  476. pass
  477. {
  478. vertex_shader = VSPos(id);
  479. pixel_shader = PSY800_Full(frag_in);
  480. }
  481. }
  482. technique RGB_Limited
  483. {
  484. pass
  485. {
  486. vertex_shader = VSPos(id);
  487. pixel_shader = PSRGB_Limited(frag_in);
  488. }
  489. }
  490. technique BGR3_Limited
  491. {
  492. pass
  493. {
  494. vertex_shader = VSPos(id);
  495. pixel_shader = PSBGR3_Limited(frag_in);
  496. }
  497. }
  498. technique BGR3_Full
  499. {
  500. pass
  501. {
  502. vertex_shader = VSPos(id);
  503. pixel_shader = PSBGR3_Full(frag_in);
  504. }
  505. }