mdapt-pass1.fs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // This is a port of the original CG shader to the quark format
  2. // the original shader can be found here :
  3. // https://github.com/libretro/common-shaders/tree/master/dithering/mdapt-4p
  4. /*
  5. Merge Dithering and Pseudo Transparency Shader v1.5 - Pass 1
  6. by Sp00kyFox, 2013
  7. Finds specific patterns and tags their central pixel.
  8. */
  9. #version 150
  10. //#define HORI
  11. //#define VERT
  12. #define dtt vec3(65536,255,1)
  13. #define eq_threshold 5.0
  14. uniform sampler2D source[];
  15. uniform vec4 sourceSize[];
  16. uniform vec4 targetSize;
  17. in Vertex{
  18. vec2 texCoord;
  19. };
  20. out vec4 fragColor;
  21. float reduce(vec3 color)
  22. {
  23. return dot(color, dtt);
  24. }
  25. float df(float A, float B)
  26. {
  27. return abs(A-B);
  28. }
  29. bool eq(float A, float B)
  30. {
  31. return (A == B);
  32. }
  33. float remapTo01(float v, float low, float high)
  34. {
  35. return clamp((v - low)/(high-low),0.0,1.0);
  36. }
  37. void main(void) {
  38. vec2 pos = texCoord*sourceSize[0].xy; // pos = pixel position
  39. vec2 dir = sign(pos); // dir = pixel direction
  40. vec2 g1 = dir*vec2(sourceSize[0].z,0.0);
  41. vec2 g2 = dir*vec2(0.0,sourceSize[0].w);;
  42. /*
  43. U3
  44. UUL U2 UUR
  45. ULL UL U1 UR URR
  46. L3 L2 L1 C R1 R2 R3
  47. DLL DL D1 DR DRR
  48. DDL D2 DDR
  49. D3
  50. */
  51. vec3 c = texture(source[0], texCoord).xyz;
  52. vec3 l1 = texture(source[0], texCoord - g1).xyz;
  53. vec3 l2 = texture(source[0], texCoord - 2*g1).xyz;
  54. vec3 r1 = texture(source[0], texCoord + g1).xyz;
  55. vec3 r2 = texture(source[0], texCoord + 2*g1).xyz;
  56. vec3 u1 = texture(source[0], texCoord - g2).xyz;
  57. vec3 u2 = texture(source[0], texCoord - 2*g2).xyz;
  58. vec3 d1 = texture(source[0], texCoord + g2).xyz;
  59. vec3 d2 = texture(source[0], texCoord + 2*g2).xyz;
  60. vec3 ul = texture(source[0], texCoord - g1 - g2).xyz;
  61. vec3 ur = texture(source[0], texCoord + g1 - g2).xyz;
  62. vec3 dl = texture(source[0], texCoord - g1 + g2).xyz;
  63. vec3 dr = texture(source[0], texCoord + g1 + g2).xyz;
  64. vec3 ull = texture(source[0], texCoord - 2*g1 - g2).xyz;
  65. vec3 uul = texture(source[0], texCoord - g1 - 2*g2).xyz;
  66. vec3 uur = texture(source[0], texCoord + g1 - 2*g2).xyz;
  67. vec3 urr = texture(source[0], texCoord + 2*g1 - g2).xyz;
  68. vec3 drr = texture(source[0], texCoord + 2*g1 + g2).xyz;
  69. vec3 ddr = texture(source[0], texCoord + g1 + 2*g2).xyz;
  70. vec3 ddl = texture(source[0], texCoord - g1 + 2*g2).xyz;
  71. vec3 dll = texture(source[0], texCoord - 2*g1 + g2).xyz;
  72. vec3 l3 = texture(source[0], texCoord - 3*g1).xyz;
  73. vec3 r3 = texture(source[0], texCoord + 3*g1).xyz;
  74. vec3 u3 = texture(source[0], texCoord - 3*g2).xyz;
  75. vec3 d3 = texture(source[0], texCoord + 3*g2).xyz;
  76. float C = reduce( c );
  77. float L1 = reduce( l1 ); float U1 = reduce( u1 );
  78. float L2 = reduce( l2 ); float U2 = reduce( u2 );
  79. float R1 = reduce( r1 ); float D1 = reduce( d1 );
  80. float R2 = reduce( r2 ); float D2 = reduce( d2 );
  81. float UL = reduce( ul ); float L3 = reduce( l3 );
  82. float UR = reduce( ur ); float R3 = reduce( r3 );
  83. float DL = reduce( dl ); float U3 = reduce( u3 );
  84. float DR = reduce( dr ); float D3 = reduce( d3 );
  85. float ULL = reduce( ull ); float DRR = reduce( drr );
  86. float UUL = reduce( uul ); float DDR = reduce( ddr );
  87. float UUR = reduce( uur ); float DDL = reduce( ddl );
  88. float URR = reduce( urr ); float DLL = reduce( dll );
  89. /*
  90. tag values:
  91. 0 nothing
  92. checkerboard pattern
  93. 9 DL
  94. 8 DR
  95. 7 UR
  96. 6 UL
  97. 5 full
  98. horizontal two-line checkerboard
  99. 4 bottom line
  100. 3 upper line
  101. vertical two-line checkerboard
  102. 2 left line
  103. 1 right line
  104. one line dither
  105. -1 horizontal
  106. -2 vertical
  107. */
  108. float type=0;
  109. // checkerboard pattern
  110. if(!eq(C,D1) && !eq(C,U1) && !eq(C,L1) && !eq(C,R1))
  111. {
  112. if(eq(C,UL))
  113. {
  114. if(eq(C,UR))
  115. {
  116. if(eq(C,DR))
  117. {
  118. if(eq(C,DL))
  119. type = 5;
  120. else if(!eq(D1,L1) || eq(D1,DL))
  121. type = 9;
  122. }
  123. else
  124. {
  125. if(eq(C,DL) && (!eq(D1,R1) || eq(D1,DR)))
  126. type = 8;
  127. }
  128. }
  129. else
  130. {
  131. if(eq(C,DR) && eq(C,DL) && (!eq(U1,R1) || eq(U1,UR)))
  132. type = 7;
  133. }
  134. }
  135. else if(eq(C,UR) && eq(C,DR) && eq(C,DL) && (!eq(U1,L1) || eq(U1,UL)))
  136. type = 6;
  137. }
  138. // horizontal two-line checkerboard
  139. else if(eq(C,L2) && eq(C,R2) && eq(C,UL) && eq(C,UR) && !eq(C,L1) && !eq(C,R1) && !eq(C,ULL) && !eq(C,U1) && !eq(C,URR))
  140. {
  141. type = 4;
  142. }
  143. else if(eq(C,L2) && eq(C,R2) && eq(C,DL) && eq(C,DR) && !eq(C,L1) && !eq(C,R1) && !eq(C,DLL) && !eq(C,D1) && !eq(C,DRR))
  144. {
  145. type = 3;
  146. }
  147. // vertical two-line checkerboard
  148. else if(eq(C,U2) && eq(C,D2) && eq(C,UR) && eq(C,DR) && !eq(C,U1) && !eq(C,D1) && !eq(C,R1) && !eq(C,UUR) && !eq(C,DDR))
  149. {
  150. type = 2;
  151. }
  152. else if(eq(C,U2) && eq(C,D2) && eq(C,UL) && eq(C,DL) && !eq(C,U1) && !eq(C,D1) && !eq(C,L1) && !eq(C,UUL) && !eq(C,DDL))
  153. {
  154. type = 1;
  155. }
  156. #ifdef HORI
  157. // horizontal one line dither
  158. else if(eq(C,L2) && eq(C,R2) && eq(L1,R1) && !eq(C,L1) && !eq(C,L3) && !eq(C,R3))
  159. type = -1;
  160. #endif
  161. #ifdef VERT
  162. // vertical one line dither
  163. else if(eq(C,U2) && eq(C,D2) && eq(U1,D1) && !eq(C,U1) && !eq(C,U3) && !eq(C,D3))
  164. type = -2;
  165. #endif
  166. fragColor=vec4(c, remapTo01(type+2, 0, 15));
  167. }