deinterlace_base.effect 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright (c) 2023 Ruwen Hahn <palana@stunned.de>
  3. * John R. Bradley <jrb@turrettech.com>
  4. * Lain Bailey <lain@obsproject.com>
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include "color.effect"
  19. uniform float4x4 ViewProj;
  20. uniform texture2d image;
  21. uniform float multiplier;
  22. uniform texture2d previous_image;
  23. uniform float2 dimensions;
  24. uniform int field_order;
  25. uniform bool frame2;
  26. sampler_state textureSampler {
  27. Filter = Linear;
  28. AddressU = Clamp;
  29. AddressV = Clamp;
  30. };
  31. struct VertData {
  32. float4 pos : POSITION;
  33. float2 uv : TEXCOORD0;
  34. };
  35. int3 select(int2 texel, int x, int y)
  36. {
  37. return int3(texel + int2(x, y), 0);
  38. }
  39. float4 load_at_prev(int2 texel, int x, int y)
  40. {
  41. return previous_image.Load(select(texel, x, y));
  42. }
  43. float4 load_at_image(int2 texel, int x, int y)
  44. {
  45. return image.Load(select(texel, x, y));
  46. }
  47. float4 load_at(int2 texel, int x, int y, int field)
  48. {
  49. if(field == 0)
  50. return load_at_image(texel, x, y);
  51. else
  52. return load_at_prev(texel, x, y);
  53. }
  54. #define YADIF_UPDATE(c, level) \
  55. if(score.c < spatial_score.c) \
  56. { \
  57. spatial_score.c = score.c; \
  58. spatial_pred.c = (load_at(texel, level, -1, field) + load_at(texel, -level, 1, field)).c / 2; \
  59. #define YADIF_CHECK_ONE(level, c) \
  60. { \
  61. float4 score = abs(load_at(texel, -1 + level, 1, field) - load_at(texel, -1 - level, -1, field)) + \
  62. abs(load_at(texel, level, 1, field) - load_at(texel, -level, -1, field)) + \
  63. abs(load_at(texel, 1 + level, 1, field) - load_at(texel, 1 - level, -1, field)); \
  64. YADIF_UPDATE(c, level) } \
  65. }
  66. #define YADIF_CHECK(level) \
  67. { \
  68. float4 score = abs(load_at(texel, -1 + level, 1, field) - load_at(texel, -1 - level, -1, field)) + \
  69. abs(load_at(texel, level, 1, field) - load_at(texel, -level, -1, field)) + \
  70. abs(load_at(texel, 1 + level, 1, field) - load_at(texel, 1 - level, -1, field)); \
  71. YADIF_UPDATE(r, level) YADIF_CHECK_ONE(level * 2, r) } \
  72. YADIF_UPDATE(g, level) YADIF_CHECK_ONE(level * 2, g) } \
  73. YADIF_UPDATE(b, level) YADIF_CHECK_ONE(level * 2, b) } \
  74. YADIF_UPDATE(a, level) YADIF_CHECK_ONE(level * 2, a) } \
  75. }
  76. float4 texel_at_yadif(int2 texel, int field, bool mode0)
  77. {
  78. if((texel.y % 2) == field)
  79. return load_at(texel, 0, 0, field);
  80. #define YADIF_AVG(x_off, y_off) ((load_at_prev(texel, x_off, y_off) + load_at_image(texel, x_off, y_off))/2)
  81. float4 c = load_at(texel, 0, 1, field),
  82. d = YADIF_AVG(0, 0),
  83. e = load_at(texel, 0, -1, field);
  84. float4 temporal_diff0 = (abs(load_at_prev(texel, 0, 0) - load_at_image(texel, 0, 0))) / 2,
  85. temporal_diff1 = (abs(load_at_prev(texel, 0, 1) - c) + abs(load_at_prev(texel, 0, -1) - e)) / 2,
  86. temporal_diff2 = (abs(load_at_image(texel, 0, 1) - c) + abs(load_at_image(texel, 0, -1) - e)) / 2,
  87. diff = max(temporal_diff0, max(temporal_diff1, temporal_diff2));
  88. float4 spatial_pred = (c + e) / 2,
  89. spatial_score = abs(load_at(texel, -1, 1, field) - load_at(texel, -1, -1, field)) +
  90. abs(c - e) +
  91. abs(load_at(texel, 1, 1, field) - load_at(texel, 1, -1, field)) - 1;
  92. YADIF_CHECK(-1)
  93. YADIF_CHECK(1)
  94. if (mode0) {
  95. float4 b = YADIF_AVG(0, 2),
  96. f = YADIF_AVG(0, -2);
  97. float4 max_ = max(d - e, max(d - c, min(b - c, f - e))),
  98. min_ = min(d - e, min(d - c, max(b - c, f - e)));
  99. diff = max(diff, max(min_, -max_));
  100. } else {
  101. diff = max(diff, max(min(d - e, d - c), -max(d - e, d - c)));
  102. }
  103. #define YADIF_SPATIAL(c) \
  104. { \
  105. if(spatial_pred.c > d.c + diff.c) \
  106. spatial_pred.c = d.c + diff.c; \
  107. else if(spatial_pred.c < d.c - diff.c) \
  108. spatial_pred.c = d.c - diff.c; \
  109. }
  110. YADIF_SPATIAL(r)
  111. YADIF_SPATIAL(g)
  112. YADIF_SPATIAL(b)
  113. YADIF_SPATIAL(a)
  114. return spatial_pred;
  115. }
  116. float4 texel_at_yadif_2x(int2 texel, int field, bool mode0)
  117. {
  118. field = frame2 ? (1 - field) : field;
  119. return texel_at_yadif(texel, field, mode0);
  120. }
  121. float4 texel_at_discard(int2 texel, int field)
  122. {
  123. texel.y = texel.y / 2 * 2;
  124. return load_at_image(texel, 0, field);
  125. }
  126. float4 texel_at_discard_2x(int2 texel, int field)
  127. {
  128. field = frame2 ? field : (1 - field);
  129. return texel_at_discard(texel, field);
  130. }
  131. float4 texel_at_blend(int2 texel, int field)
  132. {
  133. return (load_at_image(texel, 0, 0) + load_at_image(texel, 0, 1)) / 2;
  134. }
  135. float4 texel_at_blend_2x(int2 texel, int field)
  136. {
  137. if (!frame2)
  138. return (load_at_image(texel, 0, 0) +
  139. load_at_prev(texel, 0, 1)) / 2;
  140. else
  141. return (load_at_image(texel, 0, 0) +
  142. load_at_image(texel, 0, 1)) / 2;
  143. }
  144. float4 texel_at_linear(int2 texel, int field)
  145. {
  146. if ((texel.y % 2) == field)
  147. return load_at_image(texel, 0, 0);
  148. return (load_at_image(texel, 0, -1) + load_at_image(texel, 0, 1)) / 2;
  149. }
  150. float4 texel_at_linear_2x(int2 texel, int field)
  151. {
  152. field = frame2 ? field : (1 - field);
  153. return texel_at_linear(texel, field);
  154. }
  155. float4 texel_at_yadif_discard(int2 texel, int field)
  156. {
  157. return (texel_at_yadif(texel, field, true) + texel_at_discard(texel, field)) / 2;
  158. }
  159. float4 texel_at_yadif_discard_2x(int2 texel, int field)
  160. {
  161. field = frame2 ? (1 - field) : field;
  162. return (texel_at_yadif(texel, field, true) + texel_at_discard(texel, field)) / 2;
  163. }
  164. int2 pixel_uv(float2 uv)
  165. {
  166. return int2(uv * dimensions);
  167. }
  168. float4 PSYadifMode0RGBA(VertData v_in) : TARGET
  169. {
  170. return texel_at_yadif(pixel_uv(v_in.uv), field_order, true);
  171. }
  172. float4 PSYadifMode0RGBA_2x(VertData v_in) : TARGET
  173. {
  174. return texel_at_yadif_2x(pixel_uv(v_in.uv), field_order, true);
  175. }
  176. float4 PSYadifMode2RGBA(VertData v_in) : TARGET
  177. {
  178. return texel_at_yadif(pixel_uv(v_in.uv), field_order, false);
  179. }
  180. float4 PSYadifMode2RGBA_2x(VertData v_in) : TARGET
  181. {
  182. return texel_at_yadif_2x(pixel_uv(v_in.uv), field_order, false);
  183. }
  184. float4 PSYadifDiscardRGBA(VertData v_in) : TARGET
  185. {
  186. return texel_at_yadif_discard(pixel_uv(v_in.uv), field_order);
  187. }
  188. float4 PSYadifDiscardRGBA_2x(VertData v_in) : TARGET
  189. {
  190. return texel_at_yadif_discard_2x(pixel_uv(v_in.uv), field_order);
  191. }
  192. float4 PSLinearRGBA(VertData v_in) : TARGET
  193. {
  194. return texel_at_linear(pixel_uv(v_in.uv), field_order);
  195. }
  196. float4 PSLinearRGBA_2x(VertData v_in) : TARGET
  197. {
  198. return texel_at_linear_2x(pixel_uv(v_in.uv), field_order);
  199. }
  200. float4 PSDiscardRGBA(VertData v_in) : TARGET
  201. {
  202. return texel_at_discard(pixel_uv(v_in.uv), field_order);
  203. }
  204. float4 PSDiscardRGBA_2x(VertData v_in) : TARGET
  205. {
  206. return texel_at_discard_2x(pixel_uv(v_in.uv), field_order);
  207. }
  208. float4 PSBlendRGBA(VertData v_in) : TARGET
  209. {
  210. return texel_at_blend(pixel_uv(v_in.uv), field_order);
  211. }
  212. float4 PSBlendRGBA_2x(VertData v_in) : TARGET
  213. {
  214. return texel_at_blend_2x(pixel_uv(v_in.uv), field_order);
  215. }
  216. VertData VSDefault(VertData v_in)
  217. {
  218. VertData vert_out;
  219. vert_out.pos = mul(float4(v_in.pos.xyz, 1.0), ViewProj);
  220. vert_out.uv = v_in.uv;
  221. return vert_out;
  222. }
  223. #define TECHNIQUE(rgba_ps, rgba_ps_multiply, rgba_ps_tonemap, rgba_ps_multiply_tonemap) \
  224. float4 rgba_ps_multiply(VertData v_in) : TARGET \
  225. { \
  226. float4 rgba = rgba_ps(v_in); \
  227. rgba.rgb *= multiplier; \
  228. return rgba; \
  229. } \
  230. float4 rgba_ps_tonemap(VertData v_in) : TARGET \
  231. { \
  232. float4 rgba = rgba_ps(v_in); \
  233. rgba.rgb = rec709_to_rec2020(rgba.rgb); \
  234. rgba.rgb = reinhard(rgba.rgb); \
  235. rgba.rgb = rec2020_to_rec709(rgba.rgb); \
  236. return rgba; \
  237. } \
  238. float4 rgba_ps_multiply_tonemap(VertData v_in) : TARGET \
  239. { \
  240. float4 rgba = rgba_ps(v_in); \
  241. rgba.rgb *= multiplier; \
  242. rgba.rgb = rec709_to_rec2020(rgba.rgb); \
  243. rgba.rgb = reinhard(rgba.rgb); \
  244. rgba.rgb = rec2020_to_rec709(rgba.rgb); \
  245. return rgba; \
  246. } \
  247. technique Draw \
  248. { \
  249. pass \
  250. { \
  251. vertex_shader = VSDefault(v_in); \
  252. pixel_shader = rgba_ps(v_in); \
  253. } \
  254. } \
  255. technique DrawMultiply \
  256. { \
  257. pass \
  258. { \
  259. vertex_shader = VSDefault(v_in); \
  260. pixel_shader = rgba_ps_multiply(v_in); \
  261. } \
  262. } \
  263. technique DrawTonemap \
  264. { \
  265. pass \
  266. { \
  267. vertex_shader = VSDefault(v_in); \
  268. pixel_shader = rgba_ps_tonemap(v_in); \
  269. } \
  270. } \
  271. technique DrawMultiplyTonemap \
  272. { \
  273. pass \
  274. { \
  275. vertex_shader = VSDefault(v_in); \
  276. pixel_shader = rgba_ps_multiply_tonemap(v_in); \
  277. } \
  278. }