Advertisement
Guest User

Untitled

a guest
Mar 14th, 2022
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.12 KB | None | 0 0
  1. #version 130
  2.  
  3. /*
  4. Hyllian's xBR-lv3 Shader
  5.  
  6. Copyright (C) 2011-2015 Hyllian - sergiogdb@gmail.com
  7.  
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14.  
  15. The above copyright notice and this permission notice shall be included in
  16. all copies or substantial portions of the Software.
  17.  
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. THE SOFTWARE.
  25.  
  26.  
  27. Incorporates some of the ideas from SABR shader. Thanks to Joshua Street.
  28. */
  29.  
  30. // Parameter lines go here:
  31. #pragma parameter XBR_Y_WEIGHT "Y Weight" 48.0 0.0 100.0 1.0
  32. #pragma parameter XBR_EQ_THRESHOLD "EQ Threshold" 10.0 0.0 50.0 1.0
  33. #pragma parameter XBR_EQ_THRESHOLD2 "EQ Threshold 2" 2.0 0.0 4.0 1.0
  34. #pragma parameter XBR_LV2_COEFFICIENT "Lv2 Coefficient" 2.0 1.0 3.0 1.0
  35. #pragma parameter corner_type "Corner Calculation" 3.0 1.0 3.0 1.0
  36.  
  37. #if defined(VERTEX)
  38.  
  39. #if __VERSION__ >= 130
  40. #define COMPAT_VARYING out
  41. #define COMPAT_ATTRIBUTE in
  42. #define COMPAT_TEXTURE texture
  43. #else
  44. #define COMPAT_VARYING varying
  45. #define COMPAT_ATTRIBUTE attribute
  46. #define COMPAT_TEXTURE texture2D
  47. #endif
  48.  
  49. #ifdef GL_ES
  50. #define COMPAT_PRECISION mediump
  51. #else
  52. #define COMPAT_PRECISION
  53. #endif
  54.  
  55. COMPAT_ATTRIBUTE vec4 VertexCoord;
  56. COMPAT_ATTRIBUTE vec4 COLOR;
  57. COMPAT_ATTRIBUTE vec4 TexCoord;
  58. COMPAT_VARYING vec4 COL0;
  59. COMPAT_VARYING vec4 TEX0;
  60. COMPAT_VARYING vec4 t1;
  61. COMPAT_VARYING vec4 t2;
  62. COMPAT_VARYING vec4 t3;
  63. COMPAT_VARYING vec4 t4;
  64. COMPAT_VARYING vec4 t5;
  65. COMPAT_VARYING vec4 t6;
  66. COMPAT_VARYING vec4 t7;
  67.  
  68. vec4 _oPosition1;
  69. uniform mat4 MVPMatrix;
  70. uniform COMPAT_PRECISION int FrameDirection;
  71. uniform COMPAT_PRECISION int FrameCount;
  72. uniform COMPAT_PRECISION vec2 OutputSize;
  73. uniform COMPAT_PRECISION vec2 TextureSize;
  74. uniform COMPAT_PRECISION vec2 InputSize;
  75.  
  76. void main()
  77. {
  78. gl_Position = MVPMatrix * VertexCoord;
  79. COL0 = COLOR;
  80. TEX0.xy = TexCoord.xy;
  81. vec2 ps = vec2(1.0) / TextureSize.xy;
  82. float dx = ps.x;
  83. float dy = ps.y;
  84.  
  85. // A1 B1 C1
  86. // A0 A B C C4
  87. // D0 D E F F4
  88. // G0 G H I I4
  89. // G5 H5 I5
  90.  
  91. t1 = TEX0.xxxy + vec4( -dx, 0, dx,-2.0*dy); // A1 B1 C1
  92. t2 = TEX0.xxxy + vec4( -dx, 0, dx, -dy); // A B C
  93. t3 = TEX0.xxxy + vec4( -dx, 0, dx, 0); // D E F
  94. t4 = TEX0.xxxy + vec4( -dx, 0, dx, dy); // G H I
  95. t5 = TEX0.xxxy + vec4( -dx, 0, dx, 2.0*dy); // G5 H5 I5
  96. t6 = TEX0.xyyy + vec4(-2.0*dx,-dy, 0, dy); // A0 D0 G0
  97. t7 = TEX0.xyyy + vec4( 2.0*dx,-dy, 0, dy); // C4 F4 I4
  98. }
  99.  
  100. #elif defined(FRAGMENT)
  101.  
  102. #ifdef GL_ES
  103. #ifdef GL_FRAGMENT_PRECISION_HIGH
  104. precision highp float;
  105. #else
  106. precision mediump float;
  107. #endif
  108. #define COMPAT_PRECISION mediump
  109. #else
  110. #define COMPAT_PRECISION
  111. #endif
  112.  
  113. #if __VERSION__ >= 130
  114. #define COMPAT_VARYING in
  115. #define COMPAT_TEXTURE texture
  116. out COMPAT_PRECISION vec4 FragColor;
  117. #else
  118. #define COMPAT_VARYING varying
  119. #define FragColor gl_FragColor
  120. #define COMPAT_TEXTURE texture2D
  121. #endif
  122.  
  123. uniform COMPAT_PRECISION int FrameDirection;
  124. uniform COMPAT_PRECISION int FrameCount;
  125. uniform COMPAT_PRECISION vec2 OutputSize;
  126. uniform COMPAT_PRECISION vec2 TextureSize;
  127. uniform COMPAT_PRECISION vec2 InputSize;
  128. uniform sampler2D Texture;
  129. COMPAT_VARYING vec4 TEX0;
  130. COMPAT_VARYING vec4 t1;
  131. COMPAT_VARYING vec4 t2;
  132. COMPAT_VARYING vec4 t3;
  133. COMPAT_VARYING vec4 t4;
  134. COMPAT_VARYING vec4 t5;
  135. COMPAT_VARYING vec4 t6;
  136. COMPAT_VARYING vec4 t7;
  137.  
  138. // compatibility #defines
  139. #define Source Texture
  140. #define vTexCoord TEX0.xy
  141.  
  142. #define SourceSize vec4(TextureSize, 1.0 / TextureSize) //either TextureSize or InputSize
  143. #define OutputSize vec4(OutputSize, 1.0 / OutputSize)
  144.  
  145. #ifdef PARAMETER_UNIFORM
  146. uniform COMPAT_PRECISION float XBR_Y_WEIGHT;
  147. uniform COMPAT_PRECISION float XBR_EQ_THRESHOLD;
  148. uniform COMPAT_PRECISION float XBR_EQ_THRESHOLD2;
  149. uniform COMPAT_PRECISION float XBR_LV2_COEFFICIENT;
  150. uniform COMPAT_PRECISION float corner_type;
  151. #else
  152. #define XBR_Y_WEIGHT 48.0
  153. #define XBR_EQ_THRESHOLD 10.0
  154. #define XBR_EQ_THRESHOLD2 2.0
  155. #define XBR_LV2_COEFFICIENT 2.0
  156. #define corner_type 3.0
  157. #endif
  158.  
  159. const mat3 yuv = mat3(0.299, 0.587, 0.114, -0.169, -0.331, 0.499, 0.499, -0.418, -0.0813);
  160. const vec4 delta = vec4(0.4, 0.4, 0.4, 0.4);
  161.  
  162. vec4 df(vec4 A, vec4 B)
  163. {
  164. return vec4(abs(A-B));
  165. }
  166.  
  167. float c_df(vec3 c1, vec3 c2) {
  168. vec3 df = abs(c1 - c2);
  169. return df.r + df.g + df.b;
  170. }
  171.  
  172. bvec4 eq(vec4 A, vec4 B)
  173. {
  174. return lessThan(df(A, B), vec4(XBR_EQ_THRESHOLD));
  175. }
  176.  
  177. bvec4 eq2(vec4 A, vec4 B)
  178. {
  179. return lessThan(df(A, B), vec4(XBR_EQ_THRESHOLD2));
  180. }
  181.  
  182. bvec4 and(bvec4 A, bvec4 B)
  183. {
  184. return bvec4(A.x && B.x, A.y && B.y, A.z && B.z, A.w && B.w);
  185. }
  186.  
  187. bvec4 or(bvec4 A, bvec4 B)
  188. {
  189. return bvec4(A.x || B.x, A.y || B.y, A.z || B.z, A.w || B.w);
  190. }
  191.  
  192. vec4 weighted_distance(vec4 a, vec4 b, vec4 c, vec4 d, vec4 e, vec4 f, vec4 g, vec4 h)
  193. {
  194. return (df(a,b) + df(a,c) + df(d,e) + df(d,f) + 4.0*df(g,h));
  195. }
  196.  
  197. void main()
  198. {
  199. bvec4 edr, edr_left, edr_up, edr3_left, edr3_up, px; // px = pixel, edr = edge detection rule
  200. bvec4 interp_restriction_lv1, interp_restriction_lv2_left, interp_restriction_lv2_up;
  201. bvec4 interp_restriction_lv3_left, interp_restriction_lv3_up;
  202. bvec4 nc, nc30, nc60, nc45, nc15, nc75; // new_color
  203. vec4 fx, fx_left, fx_up, finalfx, fx3_left, fx3_up; // inequations of straight lines.
  204. vec3 res1, res2, pix1, pix2;
  205. float blend1, blend2;
  206.  
  207. vec2 fp = fract(vTexCoord * SourceSize.xy);
  208.  
  209. vec3 A1 = COMPAT_TEXTURE(Texture, t1.xw).rgb;
  210. vec3 B1 = COMPAT_TEXTURE(Texture, t1.yw).rgb;
  211. vec3 C1 = COMPAT_TEXTURE(Texture, t1.zw).rgb;
  212.  
  213. vec3 A = COMPAT_TEXTURE(Texture, t2.xw).rgb;
  214. vec3 B = COMPAT_TEXTURE(Texture, t2.yw).rgb;
  215. vec3 C = COMPAT_TEXTURE(Texture, t2.zw).rgb;
  216.  
  217. vec3 D = COMPAT_TEXTURE(Texture, t3.xw).rgb;
  218. vec3 E = COMPAT_TEXTURE(Texture, t3.yw).rgb;
  219. vec3 F = COMPAT_TEXTURE(Texture, t3.zw).rgb;
  220.  
  221. vec3 G = COMPAT_TEXTURE(Texture, t4.xw).rgb;
  222. vec3 H = COMPAT_TEXTURE(Texture, t4.yw).rgb;
  223. vec3 I = COMPAT_TEXTURE(Texture, t4.zw).rgb;
  224.  
  225. vec3 G5 = COMPAT_TEXTURE(Texture, t5.xw).rgb;
  226. vec3 H5 = COMPAT_TEXTURE(Texture, t5.yw).rgb;
  227. vec3 I5 = COMPAT_TEXTURE(Texture, t5.zw).rgb;
  228.  
  229. vec3 A0 = COMPAT_TEXTURE(Texture, t6.xy).rgb;
  230. vec3 D0 = COMPAT_TEXTURE(Texture, t6.xz).rgb;
  231. vec3 G0 = COMPAT_TEXTURE(Texture, t6.xw).rgb;
  232.  
  233. vec3 C4 = COMPAT_TEXTURE(Texture, t7.xy).rgb;
  234. vec3 F4 = COMPAT_TEXTURE(Texture, t7.xz).rgb;
  235. vec3 I4 = COMPAT_TEXTURE(Texture, t7.xw).rgb;
  236.  
  237. vec4 b = ((XBR_Y_WEIGHT * yuv[0]) * mat4x3(B, D, H, F));
  238. vec4 c = ((XBR_Y_WEIGHT * yuv[0]) * mat4x3(C, A, G, I));
  239. vec4 e = ((XBR_Y_WEIGHT * yuv[0]) * mat4x3(E, E, E, E));
  240. vec4 d = b.yzwx;
  241. vec4 f = b.wxyz;
  242. vec4 g = c.zwxy;
  243. vec4 h = b.zwxy;
  244. vec4 i = c.wxyz;
  245.  
  246. vec4 i4 = ((XBR_Y_WEIGHT*yuv[0]) * mat4x3(I4, C1, A0, G5));
  247. vec4 i5 = ((XBR_Y_WEIGHT*yuv[0]) * mat4x3(I5, C4, A1, G0));
  248. vec4 h5 = ((XBR_Y_WEIGHT*yuv[0]) * mat4x3(H5, F4, B1, D0));
  249. vec4 f4 = h5.yzwx;
  250.  
  251. vec4 c1 = i4.yzwx;
  252. vec4 g0 = i5.wxyz;
  253. vec4 b1 = h5.zwxy;
  254. vec4 d0 = h5.wxyz;
  255.  
  256. vec4 Ao = vec4( 1.0, -1.0, -1.0, 1.0 );
  257. vec4 Bo = vec4( 1.0, 1.0, -1.0,-1.0 );
  258. vec4 Co = vec4( 1.5, 0.5, -0.5, 0.5 );
  259. vec4 Ax = vec4( 1.0, -1.0, -1.0, 1.0 );
  260. vec4 Bx = vec4( 0.5, 2.0, -0.5,-2.0 );
  261. vec4 Cx = vec4( 1.0, 1.0, -0.5, 0.0 );
  262. vec4 Ay = vec4( 1.0, -1.0, -1.0, 1.0 );
  263. vec4 By = vec4( 2.0, 0.5, -2.0,-0.5 );
  264. vec4 Cy = vec4( 2.0, 0.0, -1.0, 0.5 );
  265.  
  266. vec4 Az = vec4( 6.0, -2.0, -6.0, 2.0 );
  267. vec4 Bz = vec4( 2.0, 6.0, -2.0, -6.0 );
  268. vec4 Cz = vec4( 5.0, 3.0, -3.0, -1.0 );
  269. vec4 Aw = vec4( 2.0, -6.0, -2.0, 6.0 );
  270. vec4 Bw = vec4( 6.0, 2.0, -6.0,-2.0 );
  271. vec4 Cw = vec4( 5.0, -1.0, -3.0, 3.0 );
  272.  
  273. fx = (Ao*fp.y+Bo*fp.x);
  274. fx_left = (Ax*fp.y+Bx*fp.x);
  275. fx_up = (Ay*fp.y+By*fp.x);
  276. fx3_left= (Az*fp.y+Bz*fp.x);
  277. fx3_up = (Aw*fp.y+Bw*fp.x);
  278.  
  279. // It uses CORNER_C if none of the others are defined.
  280. if(corner_type == 1.0)
  281. {interp_restriction_lv1 = and(notEqual(e, f), notEqual(e, h));}
  282. else if(corner_type == 2.0)
  283. {interp_restriction_lv1 = and(and(notEqual(e,f) , notEqual(e,h)) ,
  284. ( or(or(and(and(or(and(not(eq(f,b)) , not(eq(h,d))) ,
  285. eq(e,i)) , not(eq(f,i4))) , not(eq(h,i5))) , eq(e,g)) , eq(e,c)) ) );}
  286. //commenting this one out because something got broken
  287. /*else if(corner_type == 6.0)
  288. {interp_restriction_lv1 = and(and(and(notEqual(e,f) , notEqual(e,h)) ,
  289. ( or(or(and(and(or(and(not(eq(f,b)) , not(eq(h,d))) ,
  290. eq(e,i)) , not(eq(f,i4))) , not(eq(h,i5))) , eq(e,g)) ,
  291. eq(e,c)) )) , (and(or(or(or(or(and(notEqual(f,f4) , notEqual(f,i)) ,
  292. and(notEqual(h,h5) , notEqual(h,i))) , notEqual(h,g)) , notEqual(f,c)) ,
  293. eq(b,c1)) , eq(d,g0))));} */
  294. else
  295. {interp_restriction_lv1 = and(and(notEqual(e, f), notEqual(e, h)),
  296. or(or(and(not(eq(f,b)), not(eq(f,c))),
  297. and(not(eq(h,d)), not(eq(h,g)))),
  298. or(and(eq(e,i), or(and(not(eq(f,f4)), not(eq(f,i4))),
  299. and(not(eq(h,h5)), not(eq(h,i5))))),
  300. or(eq(e,g), eq(e,c)))));}
  301.  
  302. interp_restriction_lv2_left = and(notEqual(e, g), notEqual(d, g));
  303. interp_restriction_lv2_up = and(notEqual(e, c), notEqual(b, c));
  304. interp_restriction_lv3_left = and(eq2(g,g0), not(eq2(d0,g0)));
  305. interp_restriction_lv3_up = and(eq2(c,c1), not(eq2(b1,c1)));
  306.  
  307. vec4 fx45 = smoothstep(Co - delta, Co + delta, fx);
  308. vec4 fx30 = smoothstep(Cx - delta, Cx + delta, fx_left);
  309. vec4 fx60 = smoothstep(Cy - delta, Cy + delta, fx_up);
  310. vec4 fx15 = smoothstep(Cz - delta, Cz + delta, fx3_left);
  311. vec4 fx75 = smoothstep(Cw - delta, Cw + delta, fx3_up);
  312.  
  313. edr = and(lessThan(weighted_distance( e, c, g, i, h5, f4, h, f), weighted_distance( h, d, i5, f, i4, b, e, i)), interp_restriction_lv1);
  314. edr_left = and(lessThanEqual((XBR_LV2_COEFFICIENT*df(f,g)), df(h,c)), interp_restriction_lv2_left);
  315. edr_up = and(greaterThanEqual(df(f,g), (XBR_LV2_COEFFICIENT*df(h,c))), interp_restriction_lv2_up);
  316. edr3_left = interp_restriction_lv3_left;
  317. edr3_up = interp_restriction_lv3_up;
  318.  
  319. nc45 = and(edr, bvec4(fx45));
  320. nc30 = and(edr, and(edr_left, bvec4(fx30)));
  321. nc60 = and(edr, and(edr_up, bvec4(fx60)));
  322. nc15 = and(and(edr, edr_left), and(edr3_left, bvec4(fx15)));
  323. nc75 = and(and(edr, edr_up), and(edr3_up, bvec4(fx75)));
  324.  
  325. px = lessThanEqual(df(e, f), df(e, h));
  326.  
  327. nc = bvec4(nc75.x || nc15.x || nc30.x || nc60.x || nc45.x, nc75.y || nc15.y || nc30.y || nc60.y || nc45.y, nc75.z || nc15.z || nc30.z || nc60.z || nc45.z, nc75.w || nc15.w || nc30.w || nc60.w || nc45.w);
  328.  
  329. vec4 final45 = vec4(nc45) * fx45;
  330. vec4 final30 = vec4(nc30) * fx30;
  331. vec4 final60 = vec4(nc60) * fx60;
  332. vec4 final15 = vec4(nc15) * fx15;
  333. vec4 final75 = vec4(nc75) * fx75;
  334.  
  335. vec4 maximo = max(max(max(final15, final75),max(final30, final60)), final45);
  336.  
  337. if (nc.x) {pix1 = px.x ? F : H; blend1 = maximo.x;}
  338. else if (nc.y) {pix1 = px.y ? B : F; blend1 = maximo.y;}
  339. else if (nc.z) {pix1 = px.z ? D : B; blend1 = maximo.z;}
  340. else if (nc.w) {pix1 = px.w ? H : D; blend1 = maximo.w;}
  341.  
  342. if (nc.w) {pix2 = px.w ? H : D; blend2 = maximo.w;}
  343. else if (nc.z) {pix2 = px.z ? D : B; blend2 = maximo.z;}
  344. else if (nc.y) {pix2 = px.y ? B : F; blend2 = maximo.y;}
  345. else if (nc.x) {pix2 = px.x ? F : H; blend2 = maximo.x;}
  346.  
  347. res1 = mix(E, pix1, blend1);
  348. res2 = mix(E, pix2, blend2);
  349. vec3 res = mix(res1, res2, step(c_df(E, res1), c_df(E, res2)));
  350.  
  351. FragColor = vec4(res, 1.0);
  352. }
  353. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement