Dither.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*-------------.
  2. | :: Dither :: |
  3. '-------------*/
  4. /*
  5. Dither version 1.3.0
  6. by Christian Cann Schuldt Jensen ~ CeeJay.dk
  7. Does dithering of the greather than 8-bit per channel precision used in shaders.
  8. Note that the input from the framebuffer is 8-bit and cannot be dithered down to 8-bit.
  9. Dithering therefore only works on the effects that SweetFX applies afterwards.
  10. */
  11. #ifndef dither_method
  12. #define dither_method 1
  13. #endif
  14. float4 DitherPass( float4 colorInput, float2 tex )
  15. {
  16. float3 color = colorInput.rgb;
  17. float dither_bit = 8.0; //Number of bits per channel. Should be 8 for most monitors.
  18. //color = (tex.x*0.3+0.1); //draw a gradient for testing.
  19. //#define dither_method 2 //override method for testing purposes
  20. /*------------------------.
  21. | :: Ordered Dithering :: |
  22. '------------------------*/
  23. #if dither_method == 1 // Ordered dithering
  24. //Calculate grid position
  25. float grid_position = frac( dot(tex, (screen_size * float2(1.0/16.0,10.0/36.0) )+(0.25) ) );
  26. //Calculate how big the shift should be
  27. float dither_shift = (0.25) * (1.0 / (pow(2,dither_bit) - 1.0));
  28. //Shift the individual colors differently, thus making it even harder to see the dithering pattern
  29. float3 dither_shift_RGB = float3(dither_shift, -dither_shift, dither_shift); //subpixel dithering
  30. //modify shift acording to grid position.
  31. dither_shift_RGB = lerp(2.0 * dither_shift_RGB, -2.0 * dither_shift_RGB, grid_position); //shift acording to grid position.
  32. //shift the color by dither_shift
  33. color.rgb += dither_shift_RGB;
  34. /*-----------------------.
  35. | :: Random Dithering :: |
  36. '-----------------------*/
  37. #elif dither_method == 2 //Random dithering
  38. //Pseudo Random Number Generator
  39. // -- PRNG 1 - Reference --
  40. float seed = dot(tex, float2(12.9898,78.233)); //I could add more salt here if I wanted to
  41. float sine = sin(seed); //cos also works well. Sincos too if you want 2D noise.
  42. float noise = frac(sine * 43758.5453 + tex.x); //tex.x is just some additional salt - it can be taken out.
  43. //Calculate how big the shift should be
  44. float dither_shift = (1.0 / (pow(2,dither_bit) - 1.0)); // Using noise to determine shift. Will be 1/255 if set to 8-bit.
  45. float dither_shift_half = (dither_shift * 0.5); // The noise should vary between +- 0.5
  46. dither_shift = dither_shift * noise - dither_shift_half; // MAD
  47. //shift the color by dither_shift
  48. color.rgb += float3(-dither_shift, dither_shift, -dither_shift); //subpixel dithering
  49. /*--------------------.
  50. | :: New Dithering :: |
  51. '--------------------*/
  52. //#define dither_method 3
  53. #elif dither_method == 3 // New Ordered dithering
  54. //Calculate grid position
  55. float grid_position = frac(dot(tex,(screen_size) * float2(0.75,0.5) /*+ (0.00025)*/)); //(0.6,0.8) is good too - TODO : experiment with values
  56. //Calculate how big the shift should be
  57. float dither_shift = (0.25) * (1.0 / (pow(2,dither_bit) - 1.0)); // 0.25 seems good both when using math and when eyeballing it. So does 0.75 btw.
  58. dither_shift = lerp(2.0 * dither_shift, -2.0 * dither_shift, grid_position); //shift acording to grid position.
  59. //shift the color by dither_shift
  60. color.rgb += float3(dither_shift, -dither_shift, dither_shift); //subpixel dithering
  61. /*-------------------------.
  62. | :: A Dither Dithering :: |
  63. '-------------------------*/
  64. //#define dither_method 4
  65. #elif dither_method == 4 // New Ordered dithering
  66. #define dither_pattern 11
  67. #define dither_levels 254
  68. float x=tex.x * screen_size.x;// * 1.31;
  69. float y=tex.y * screen_size.y;// * 1.31;
  70. //Calculate grid position
  71. float c = frac(dot(tex,(screen_size) * float2(1.0/4.0,3.0/4.0) + (0.00025) )); //the + (0.00025) part is to avoid errors with the floating point math
  72. float mask;
  73. #if dither_pattern == 1
  74. mask = ((x ^ y * 149) * 1234 & 511)/511.0; //requires bitwise XOR - doesn't work
  75. #elif dither_pattern == 2
  76. mask = (((x+c*17) ^ y * 149) * 1234 & 511)/511.0; //requires bitwise XOR - doesn't work
  77. #elif dither_pattern == 3
  78. mask = 256.0 * frac(((x + y * 237) * 119)/ 256.0 ) / 255.0 ;//1.00392 * frac(0.464844 * (x + 237.0 * y)); //256.0 * frac(((x + y * 237) * 119)/ 256.0 ) / 255.0
  79. #elif dither_pattern == 4
  80. mask = (256.0 * frac((((x+c*67.0) + y * 236.0) * 119.0) / 256.0)) / 255.0; //& 255 = 256 * frac(x / 256)
  81. #elif dither_pattern == 5
  82. mask = 0.5;
  83. #elif dither_pattern == 6
  84. mask = frac( dot(tex, float2(12.9898,78.233)) * 927.5453 );
  85. #elif dither_pattern == 7
  86. mask = frac( dot(tex, (screen_size * float2(1.0/7.0,9.0/17.0))+(0.00025) ) );
  87. #elif dither_pattern == 8
  88. mask = frac( dot(tex, (screen_size * float2(5.0/7.0,3.0/17.0))+(0.00025) ) );
  89. #elif dither_pattern == 9
  90. mask = frac( dot(tex, (screen_size * float2(1.0/4.0,3.0/5.0))+(0.000025) ) );
  91. #elif dither_pattern == 10
  92. mask = frac( dot(tex, (screen_size * float2(1.0/87.0,1.0/289.0))+(0.000025) ) ); //stylish pattern - but bad for dithering
  93. #elif dither_pattern == 11
  94. //mask = frac( dot(tex, (screen_size * float2(1.0/(floor(tex.y*10.0)/100.+16.0),87.0/289.0))+(0.000025) ) ); //
  95. //mask = frac( dot(float4(tex,tex), float4((screen_size * float2(0.666/16.0,6.66/36.)),(screen_size * float2(0.3344/16.0,3.34/36.)) ) ) ); //
  96. mask = frac( dot(tex, (screen_size * float2(1.0/16.0,10.0/36.0) )+(0.25) ) ); //
  97. //(floor(tex.y*10.0)/100.0 + 3.0)
  98. #else
  99. //return input;
  100. #endif
  101. color.rgb = floor(dither_levels * color.rgb + mask) / dither_levels;
  102. color.rgb = mask.xxx;
  103. /*---------------------------------------.
  104. | :: New Dithering - grid experiments :: |
  105. '---------------------------------------*/
  106. //#define dither_method 5
  107. #elif dither_method == 5 // New Ordered dithering
  108. //Calculate grid position
  109. float grid_position = frac(dot(tex,floor(screen_size * float2(-0.5,-0.9) ) /*- (0.00025)*/ )); //(0.6,0.8) is good too - TODO : experiment with values
  110. //Calculate grid position
  111. grid_position = frac(dot(tex,floor(screen_size * float2(0.4,0.70)) /*+ grid_position*/ /*+ (0.00025)*/ )); //
  112. //Calculate how big the shift should be
  113. float dither_shift = (0.25) * (1.0 / (pow(2,dither_bit) - 1.0)); // 0.25 seems good both when using math and when eyeballing it. So does 0.75 btw.
  114. dither_shift = lerp(2.0 * dither_shift, -2.0 * dither_shift, grid_position); //shift acording to grid position.
  115. //dither_shift = (2.0 * dither_shift) * grid_position + (2.0 * dither_shift) * grid_position;
  116. //dither_shift = 4.0 * dither_shift * grid_position;
  117. //shift the color by dither_shift
  118. //color.rgb += lerp(2.0 * dither_shift_RGB, -2.0 * dither_shift_RGB, grid_position); //shift acording to grid position.
  119. color.rgb += float3(dither_shift, -dither_shift, dither_shift); //subpixel dithering
  120. /*-------------------.
  121. | :: Checkerboard :: |
  122. '-------------------*/
  123. #elif dither_method == 6 // Checkerboard Ordered dithering
  124. //Calculate grid position
  125. float grid_position = frac(dot(tex, screen_size * 0.5) + 0.25); //returns 0.25 and 0.75
  126. //Calculate how big the shift should be
  127. float dither_shift = (0.25) * (1.0 / (pow(2,dither_bit) - 1.0)); // 0.25 seems good both when using math and when eyeballing it. So does 0.75 btw.
  128. //Shift the individual colors differently, thus making it even harder to see the dithering pattern
  129. float3 dither_shift_RGB = float3(dither_shift, -dither_shift, dither_shift); //subpixel dithering
  130. //modify shift acording to grid position.
  131. dither_shift_RGB = lerp(2.0 * dither_shift_RGB, -2.0 * dither_shift_RGB, grid_position); //shift acording to grid position.
  132. //shift the color by dither_shift
  133. //color.rgb += lerp(2.0 * dither_shift_RGB, -2.0 * dither_shift_RGB, grid_position); //shift acording to grid position.
  134. color.rgb += dither_shift_RGB;
  135. #endif
  136. /*-------------------------.
  137. | :: Debugging features :: |
  138. '-------------------------*/
  139. //color.rgb = (dither_shift_RGB * 2.0 * (pow(2,dither_bit) - 1.0) ) + 0.5; //visualize the RGB shift
  140. //color.rgb = grid_position; //visualize the grid
  141. //color.rgb = noise; //visualize the noise
  142. //color.rgb = c;
  143. /*---------------------------.
  144. | :: Returning the output :: |
  145. '---------------------------*/
  146. //color = (tex.x / 2.0); //draw a undithered gradient for testing.
  147. colorInput.rgb = color.rgb;
  148. return colorInput;
  149. }