mdapt-pass3.fs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 3
  6. by Sp00kyFox, 2013
  7. Tags the rest of the detected pattern from pass 1.
  8. */
  9. #version 150
  10. uniform sampler2D source[];
  11. uniform vec4 sourceSize[];
  12. uniform vec4 targetSize;
  13. in Vertex{
  14. vec2 texCoord;
  15. };
  16. out vec4 fragColor;
  17. float remapTo01(float v, float low, float high)
  18. {
  19. return clamp((v - low)/(high-low),0.0,1.0);
  20. }
  21. float remapFrom01(float v, float low, float high)
  22. {
  23. return round(mix(low, high, v));
  24. }
  25. void main(void) {
  26. vec2 pos = texCoord*sourceSize[0].xy; // pos = pixel position
  27. vec2 dir = sign(pos); // dir = pixel direction
  28. vec2 g1 = dir*vec2(sourceSize[0].z,0.0);
  29. vec2 g2 = dir*vec2(0.0,sourceSize[0].w);
  30. /*
  31. U2
  32. UL U1 UR
  33. L2 L1 xC R1 R2
  34. DL D1 DR
  35. D2
  36. */
  37. vec4 xC = texture(source[0], texCoord).xyzw;
  38. vec4 xL1 = texture(source[0], texCoord - g1).xyzw;
  39. vec4 xL2 = texture(source[0], texCoord - 2*g1).xyzw;
  40. vec4 xR1 = texture(source[0], texCoord + g1).xyzw;
  41. vec4 xR2 = texture(source[0], texCoord + 2*g1).xyzw;
  42. vec4 xU1 = texture(source[0], texCoord - g2).xyzw;
  43. vec4 xU2 = texture(source[0], texCoord - 2*g2).xyzw;
  44. vec4 xD1 = texture(source[0], texCoord + g2).xyzw;
  45. vec4 xD2 = texture(source[0], texCoord + 2*g2).xyzw;
  46. vec4 xUL = texture(source[0], texCoord - g1 - g2).xyzw;
  47. vec4 xUR = texture(source[0], texCoord + g1 - g2).xyzw;
  48. vec4 xDL = texture(source[0], texCoord - g1 + g2).xyzw;
  49. vec4 xDR = texture(source[0], texCoord + g1 + g2).xyzw;
  50. float C = remapFrom01(xC.w, 0, 15) - 2;
  51. float L1 = remapFrom01(xL1.w, 0, 15) - 2;
  52. float L2 = remapFrom01(xL2.w, 0, 15) - 2;
  53. float R1 = remapFrom01(xR1.w, 0, 15) - 2;
  54. float R2 = remapFrom01(xR2.w, 0, 15) - 2;
  55. float U1 = remapFrom01(xU1.w, 0, 15) - 2;
  56. float U2 = remapFrom01(xU2.w, 0, 15) - 2;
  57. float D1 = remapFrom01(xD1.w, 0, 15) - 2;
  58. float D2 = remapFrom01(xD2.w, 0, 15) - 2;
  59. float UL = remapFrom01(xUL.w, 0, 15) - 2;
  60. float UR = remapFrom01(xUR.w, 0, 15) - 2;
  61. float DL = remapFrom01(xDL.w, 0, 15) - 2;
  62. float DR = remapFrom01(xDR.w, 0, 15) - 2;
  63. /*
  64. tag values:
  65. 0 nothing
  66. checkerboard pattern
  67. 9 DL
  68. 8 DR
  69. 7 UR
  70. 6 UL
  71. 5 full
  72. horizontal two-line checkerboard
  73. 4 bottom line
  74. 3 upper line
  75. vertical two-line checkerboard
  76. 2 left line
  77. 1 right line
  78. one line dither
  79. -1 horizontal
  80. -2 vertical
  81. */
  82. // checkerboard pattern
  83. if(U1 == 5 || D1 == 5 || L1 == 5 || R1 == 5 || UL == 5 || UR == 5 || DR == 5 || DL == 5)
  84. {
  85. fragColor=vec4(xC.xyz, remapTo01(5+2, 0, 15));
  86. return ;
  87. }
  88. if(U1 == 6 || L1 == 6 || UL == 6 || UR == 6 || DL == 6) // UL
  89. {
  90. fragColor=vec4(xC.xyz, remapTo01(6+2, 0, 15));
  91. return ;
  92. }
  93. if(U1 == 7 || R1 == 7 || UL == 7 || UR == 7 || DR == 7) // UR
  94. {
  95. fragColor=vec4(xC.xyz, remapTo01(7+2, 0, 15));
  96. return ;
  97. }
  98. if(D1 == 8 || R1 == 8 || UR == 8 || DR == 8 || DL == 8) // DR
  99. {
  100. fragColor=vec4(xC.xyz, remapTo01(8+2, 0, 15));
  101. return ;
  102. }
  103. if(D1 == 9 || L1 == 9 || UL == 9 || DR == 9 || DL == 9) // DL
  104. {
  105. fragColor=vec4(xC.xyz, remapTo01(9+2, 0, 15));
  106. return ;
  107. }
  108. // horizontal two-line checkerboard
  109. if (L2 == 4 || L1 == 4 || R1 == 4 || R2 == 4 || DL == 4 || D1 == 4 || DR == 4)
  110. {
  111. fragColor=vec4(xC.xyz, remapTo01(4+2, 0, 15));
  112. return ;
  113. }
  114. if (L2 == 3 || L1 == 3 || R1 == 3 || R2 == 3 || UL == 3 || U1 == 3 || UR == 3)
  115. {
  116. fragColor=vec4(xC.xyz, remapTo01(3+2, 0, 15));
  117. return ;
  118. }
  119. // vertical two-line checkerboard
  120. if (U2 == 2 || U1 == 2 || D1 == 2 || D2 == 2 || UL == 2 || L1 == 2 || DL == 2)
  121. {
  122. fragColor=vec4(xC.xyz, remapTo01(2+2, 0, 15));
  123. return ;
  124. }
  125. if (U2 == 1 || U1 == 1 || D1 == 1 || D2 == 1 || UR == 1 || R1 == 1 || DR == 1)
  126. {
  127. fragColor=vec4(xC.xyz, remapTo01(1+2, 0, 15));
  128. return ;
  129. }
  130. if(C > 0){
  131. fragColor=xC;
  132. return ;
  133. }
  134. // horizontal one line dither
  135. if (L2 == -1 || L1 == -1 || R1 == -1 || R2 == -1)
  136. {
  137. fragColor=vec4(xC.xyz, remapTo01(-1+2, 0, 15));
  138. return ;
  139. }
  140. // vertical one line dither
  141. if (U2 == -2 || U1 == -2 || D1 == -2 || D2 == -2)
  142. {
  143. fragColor=vec4(xC.xyz, remapTo01(-2+2, 0, 15));
  144. return;
  145. }
  146. fragColor=xC;
  147. }