HQ4x.shader 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * hq4x filter.
  3. * https://www.shadertoy.com/view/MslGRS
  4. * https://github.com/libretro/common-shaders/blob/master/scalehq/shaders/4xScaleHQ.cg
  5. */
  6. shader_type canvas_item;
  7. uniform bool enabled = true;
  8. uniform sampler2D SCREEN_TEXTURE : hint_screen_texture, filter_linear_mipmap;
  9. void fragment(){
  10. float mx = 1.0; // start smoothing wt.
  11. const float k = -1.10; // wt. decrease factor
  12. const float max_w = 0.75; // max filter weigth
  13. const float min_w = 0.03; // min filter weigth
  14. const float lum_add = 0.01; // effects smoothing
  15. vec4 color = texture(SCREEN_TEXTURE, SCREEN_UV);
  16. if (enabled){
  17. vec3 c = color.xyz;
  18. float x = 0.5 * SCREEN_PIXEL_SIZE.x;
  19. float y = 0.5 * SCREEN_PIXEL_SIZE.y;
  20. const vec3 dt = 1.0*vec3(1.0, 1.0, 1.0);
  21. vec2 dg1 = vec2( x, y);
  22. vec2 dg2 = vec2(-x, y);
  23. vec2 sd1 = dg1*0.5;
  24. vec2 sd2 = dg2*0.5;
  25. vec2 ddx = vec2(x,0.0);
  26. vec2 ddy = vec2(0.0,y);
  27. vec2 uv = SCREEN_UV;
  28. vec4 t1 = vec4(uv-sd1,uv-ddy);
  29. vec4 t2 = vec4(uv-sd2,uv+ddx);
  30. vec4 t3 = vec4(uv+sd1,uv+ddy);
  31. vec4 t4 = vec4(uv+sd2,uv-ddx);
  32. vec4 t5 = vec4(uv-dg1,uv-dg2);
  33. vec4 t6 = vec4(uv+dg1,uv+dg2);
  34. vec3 i1 = texture(SCREEN_TEXTURE, t1.xy).xyz;
  35. vec3 i2 = texture(SCREEN_TEXTURE, t2.xy).xyz;
  36. vec3 i3 = texture(SCREEN_TEXTURE, t3.xy).xyz;
  37. vec3 i4 = texture(SCREEN_TEXTURE, t4.xy).xyz;
  38. vec3 o1 = texture(SCREEN_TEXTURE, t5.xy).xyz;
  39. vec3 o3 = texture(SCREEN_TEXTURE, t6.xy).xyz;
  40. vec3 o2 = texture(SCREEN_TEXTURE, t5.zw).xyz;
  41. vec3 o4 = texture(SCREEN_TEXTURE, t6.zw).xyz;
  42. vec3 s1 = texture(SCREEN_TEXTURE, t1.zw).xyz;
  43. vec3 s2 = texture(SCREEN_TEXTURE, t2.zw).xyz;
  44. vec3 s3 = texture(TEXTURE, t3.zw).xyz;
  45. vec3 s4 = texture(TEXTURE, t4.zw).xyz;
  46. float ko1 = dot(abs(o1-c),dt);
  47. float ko2 = dot(abs(o2-c),dt);
  48. float ko3 = dot(abs(o3-c),dt);
  49. float ko4 = dot(abs(o4-c),dt);
  50. float k1=min(dot(abs(i1-i3),dt),max(ko1,ko3));
  51. float k2=min(dot(abs(i2-i4),dt),max(ko2,ko4));
  52. float w1 = k2; if(ko3<ko1) w1*=ko3/ko1;
  53. float w2 = k1; if(ko4<ko2) w2*=ko4/ko2;
  54. float w3 = k2; if(ko1<ko3) w3*=ko1/ko3;
  55. float w4 = k1; if(ko2<ko4) w4*=ko2/ko4;
  56. c=(w1*o1+w2*o2+w3*o3+w4*o4+0.001*c)/(w1+w2+w3+w4+0.001);
  57. w1 = k*dot(abs(i1-c)+abs(i3-c),dt)/(0.125*dot(i1+i3,dt)+lum_add);
  58. w2 = k*dot(abs(i2-c)+abs(i4-c),dt)/(0.125*dot(i2+i4,dt)+lum_add);
  59. w3 = k*dot(abs(s1-c)+abs(s3-c),dt)/(0.125*dot(s1+s3,dt)+lum_add);
  60. w4 = k*dot(abs(s2-c)+abs(s4-c),dt)/(0.125*dot(s2+s4,dt)+lum_add);
  61. w1 = clamp(w1+mx,min_w,max_w);
  62. w2 = clamp(w2+mx,min_w,max_w);
  63. w3 = clamp(w3+mx,min_w,max_w);
  64. w4 = clamp(w4+mx,min_w,max_w);
  65. color = vec4((w1*(i1+i3)+w2*(i2+i4)+w3*(s1+s3)+w4*(s2+s4)+c)/(2.0*(w1+w2+w3+w4)+1.0), color.a);
  66. }
  67. COLOR = color;
  68. }