fxaa-hq.chunk 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #include <common/texture/texture-lod>
  2. #define int2 vec2
  3. #define float2 vec2
  4. #define float3 vec3
  5. #define float4 vec4
  6. #define FxaaBool3 bvec3
  7. #define FxaaInt2 vec2
  8. #define FxaaFloat2 vec2
  9. #define FxaaFloat3 vec3
  10. #define FxaaFloat4 vec4
  11. #define FxaaBool2Float(a) mix(0.0, 1.0, (a))
  12. #define FxaaPow3(x, y) pow(x, y)
  13. #define FxaaSel3(f, t, b) mix((f), (t), (b))
  14. #define FxaaToFloat3(a) FxaaFloat3((a), (a), (a))
  15. float4 FxaaTexLod0(sampler2D tex, float2 pos) {
  16. #if defined(CC_USE_WGPU)
  17. return textureLod(tex, pos.xy, 0.0);
  18. #else
  19. return texture(tex, pos.xy);
  20. #endif
  21. }
  22. float4 FxaaTexGrad(sampler2D tex, float2 pos, float2 grad) {
  23. return fragTextureGrad(tex, pos.xy, grad, grad);
  24. }
  25. float4 FxaaTexOff(sampler2D tex, float2 pos, int2 off, float2 rcpFrame) {
  26. return FxaaTexLod0(tex, pos.xy + vec2(off.x, off.y) * rcpFrame);
  27. }
  28. #define FXAA_SRGB_ROP 0
  29. #ifndef FXAA_PRESET
  30. #define FXAA_PRESET 3
  31. #endif
  32. #if (FXAA_PRESET == 0)
  33. #define FXAA_EDGE_THRESHOLD (1.0/4.0)
  34. #define FXAA_EDGE_THRESHOLD_MIN (1.0/12.0)
  35. #define FXAA_SEARCH_STEPS 2
  36. #define FXAA_SEARCH_ACCELERATION 4
  37. #define FXAA_SEARCH_THRESHOLD (1.0/4.0)
  38. #define FXAA_SUBPIX 1
  39. #define FXAA_SUBPIX_FASTER 1
  40. #define FXAA_SUBPIX_CAP (2.0/3.0)
  41. #define FXAA_SUBPIX_TRIM (1.0/4.0)
  42. #endif
  43. #if (FXAA_PRESET == 1)
  44. #define FXAA_EDGE_THRESHOLD (1.0/8.0)
  45. #define FXAA_EDGE_THRESHOLD_MIN (1.0/16.0)
  46. #define FXAA_SEARCH_STEPS 4
  47. #define FXAA_SEARCH_ACCELERATION 3
  48. #define FXAA_SEARCH_THRESHOLD (1.0/4.0)
  49. #define FXAA_SUBPIX 1
  50. #define FXAA_SUBPIX_FASTER 0
  51. #define FXAA_SUBPIX_CAP (3.0/4.0)
  52. #define FXAA_SUBPIX_TRIM (1.0/4.0)
  53. #endif
  54. #if (FXAA_PRESET == 2)
  55. #define FXAA_EDGE_THRESHOLD (1.0/8.0)
  56. #define FXAA_EDGE_THRESHOLD_MIN (1.0/24.0)
  57. #define FXAA_SEARCH_STEPS 8
  58. #define FXAA_SEARCH_ACCELERATION 2
  59. #define FXAA_SEARCH_THRESHOLD (1.0/4.0)
  60. #define FXAA_SUBPIX 1
  61. #define FXAA_SUBPIX_FASTER 0
  62. #define FXAA_SUBPIX_CAP (3.0/4.0)
  63. #define FXAA_SUBPIX_TRIM (1.0/4.0)
  64. #endif
  65. #if (FXAA_PRESET == 3)
  66. #define FXAA_EDGE_THRESHOLD (1.0/8.0)
  67. #define FXAA_EDGE_THRESHOLD_MIN (1.0/24.0)
  68. #define FXAA_SEARCH_STEPS 16
  69. #define FXAA_SEARCH_ACCELERATION 1
  70. #define FXAA_SEARCH_THRESHOLD (1.0/4.0)
  71. #define FXAA_SUBPIX 1
  72. #define FXAA_SUBPIX_FASTER 0
  73. #define FXAA_SUBPIX_CAP (3.0/4.0)
  74. #define FXAA_SUBPIX_TRIM (1.0/4.0)
  75. #endif
  76. #if (FXAA_PRESET == 4)
  77. #define FXAA_EDGE_THRESHOLD (1.0/8.0)
  78. #define FXAA_EDGE_THRESHOLD_MIN (1.0/24.0)
  79. #define FXAA_SEARCH_STEPS 24
  80. #define FXAA_SEARCH_ACCELERATION 1
  81. #define FXAA_SEARCH_THRESHOLD (1.0/4.0)
  82. #define FXAA_SUBPIX 1
  83. #define FXAA_SUBPIX_FASTER 0
  84. #define FXAA_SUBPIX_CAP (3.0/4.0)
  85. #define FXAA_SUBPIX_TRIM (1.0/4.0)
  86. #endif
  87. #if (FXAA_PRESET == 5)
  88. #define FXAA_EDGE_THRESHOLD (1.0/8.0)
  89. #define FXAA_EDGE_THRESHOLD_MIN (1.0/24.0)
  90. #define FXAA_SEARCH_STEPS 32
  91. #define FXAA_SEARCH_ACCELERATION 1
  92. #define FXAA_SEARCH_THRESHOLD (1.0/4.0)
  93. #define FXAA_SUBPIX 1
  94. #define FXAA_SUBPIX_FASTER 0
  95. #define FXAA_SUBPIX_CAP (3.0/4.0)
  96. #define FXAA_SUBPIX_TRIM (1.0/4.0)
  97. #endif
  98. #define FXAA_SUBPIX_TRIM_SCALE (1.0/(1.0 - FXAA_SUBPIX_TRIM))
  99. float FxaaLuma(float3 rgb) {
  100. return rgb.y * (0.587/0.299) + rgb.x; }
  101. float3 FxaaLerp3(float3 a, float3 b, float amountOfA) {
  102. return (FxaaToFloat3(-amountOfA) * b) +
  103. ((a * FxaaToFloat3(amountOfA)) + b); }
  104. float3 FxaaFilterReturn(float3 rgb) {
  105. #if FXAA_SRGB_ROP
  106. return FxaaSel3(
  107. rgb * FxaaToFloat3(1.0/12.92),
  108. FxaaPow3(
  109. rgb * FxaaToFloat3(1.0/1.055) + FxaaToFloat3(0.055/1.055),
  110. FxaaToFloat3(2.4)),
  111. rgb > FxaaToFloat3(0.04045));
  112. #else
  113. return rgb;
  114. #endif
  115. }
  116. float3 FxaaPixelShader(float2 pos, sampler2D tex, float2 rcpFrame) {
  117. float3 rgbN = FxaaTexOff(tex, pos.xy, FxaaInt2( 0,-1), rcpFrame).xyz;
  118. float3 rgbW = FxaaTexOff(tex, pos.xy, FxaaInt2(-1, 0), rcpFrame).xyz;
  119. float3 rgbM = FxaaTexOff(tex, pos.xy, FxaaInt2( 0, 0), rcpFrame).xyz;
  120. float3 rgbE = FxaaTexOff(tex, pos.xy, FxaaInt2( 1, 0), rcpFrame).xyz;
  121. float3 rgbS = FxaaTexOff(tex, pos.xy, FxaaInt2( 0, 1), rcpFrame).xyz;
  122. float lumaN = FxaaLuma(rgbN);
  123. float lumaW = FxaaLuma(rgbW);
  124. float lumaM = FxaaLuma(rgbM);
  125. float lumaE = FxaaLuma(rgbE);
  126. float lumaS = FxaaLuma(rgbS);
  127. float rangeMin = min(lumaM, min(min(lumaN, lumaW), min(lumaS, lumaE)));
  128. float rangeMax = max(lumaM, max(max(lumaN, lumaW), max(lumaS, lumaE)));
  129. float range = rangeMax - rangeMin;
  130. #if FXAA_DEBUG
  131. float lumaO = lumaM / (1.0 + (0.587/0.299));
  132. #endif
  133. if(range < max(FXAA_EDGE_THRESHOLD_MIN, rangeMax * FXAA_EDGE_THRESHOLD)) {
  134. #if FXAA_DEBUG
  135. return FxaaFilterReturn(FxaaToFloat3(lumaO));
  136. #endif
  137. return FxaaFilterReturn(rgbM); }
  138. #if FXAA_SUBPIX > 0
  139. #if FXAA_SUBPIX_FASTER
  140. float3 rgbL = (rgbN + rgbW + rgbE + rgbS + rgbM) *
  141. FxaaToFloat3(1.0/5.0);
  142. #else
  143. float3 rgbL = rgbN + rgbW + rgbM + rgbE + rgbS;
  144. #endif
  145. #endif
  146. #if FXAA_SUBPIX != 0
  147. float lumaL = (lumaN + lumaW + lumaE + lumaS) * 0.25;
  148. float rangeL = abs(lumaL - lumaM);
  149. #endif
  150. #if FXAA_SUBPIX == 1
  151. float blendL = max(0.0,
  152. (rangeL / range) - FXAA_SUBPIX_TRIM) * FXAA_SUBPIX_TRIM_SCALE;
  153. blendL = min(FXAA_SUBPIX_CAP, blendL);
  154. #endif
  155. #if FXAA_SUBPIX == 2
  156. float blendL = rangeL / range;
  157. #endif
  158. float3 rgbNW = FxaaTexOff(tex, pos.xy, FxaaInt2(-1,-1), rcpFrame).xyz;
  159. float3 rgbNE = FxaaTexOff(tex, pos.xy, FxaaInt2( 1,-1), rcpFrame).xyz;
  160. float3 rgbSW = FxaaTexOff(tex, pos.xy, FxaaInt2(-1, 1), rcpFrame).xyz;
  161. float3 rgbSE = FxaaTexOff(tex, pos.xy, FxaaInt2( 1, 1), rcpFrame).xyz;
  162. #if (FXAA_SUBPIX_FASTER == 0) && (FXAA_SUBPIX > 0)
  163. rgbL += (rgbNW + rgbNE + rgbSW + rgbSE);
  164. rgbL *= FxaaToFloat3(1.0/9.0);
  165. #endif
  166. float lumaNW = FxaaLuma(rgbNW);
  167. float lumaNE = FxaaLuma(rgbNE);
  168. float lumaSW = FxaaLuma(rgbSW);
  169. float lumaSE = FxaaLuma(rgbSE);
  170. float edgeVert =
  171. abs((0.25 * lumaNW) + (-0.5 * lumaN) + (0.25 * lumaNE)) +
  172. abs((0.50 * lumaW ) + (-1.0 * lumaM) + (0.50 * lumaE )) +
  173. abs((0.25 * lumaSW) + (-0.5 * lumaS) + (0.25 * lumaSE));
  174. float edgeHorz =
  175. abs((0.25 * lumaNW) + (-0.5 * lumaW) + (0.25 * lumaSW)) +
  176. abs((0.50 * lumaN ) + (-1.0 * lumaM) + (0.50 * lumaS )) +
  177. abs((0.25 * lumaNE) + (-0.5 * lumaE) + (0.25 * lumaSE));
  178. bool horzSpan = edgeHorz >= edgeVert;
  179. float lengthSign = horzSpan ? -rcpFrame.y : -rcpFrame.x;
  180. if(!horzSpan) lumaN = lumaW;
  181. if(!horzSpan) lumaS = lumaE;
  182. float gradientN = abs(lumaN - lumaM);
  183. float gradientS = abs(lumaS - lumaM);
  184. lumaN = (lumaN + lumaM) * 0.5;
  185. lumaS = (lumaS + lumaM) * 0.5;
  186. bool pairN = gradientN >= gradientS;
  187. if(!pairN) lumaN = lumaS;
  188. if(!pairN) gradientN = gradientS;
  189. if(!pairN) lengthSign *= -1.0;
  190. float2 posN;
  191. posN.x = pos.x + (horzSpan ? 0.0 : lengthSign * 0.5);
  192. posN.y = pos.y + (horzSpan ? lengthSign * 0.5 : 0.0);
  193. gradientN *= FXAA_SEARCH_THRESHOLD;
  194. float2 posP = posN;
  195. float2 offNP = horzSpan ?
  196. FxaaFloat2(rcpFrame.x, 0.0) :
  197. FxaaFloat2(0.0, rcpFrame.y);
  198. float lumaEndN = lumaN;
  199. float lumaEndP = lumaN;
  200. bool doneN = false;
  201. bool doneP = false;
  202. #if FXAA_SEARCH_ACCELERATION == 1
  203. posN += offNP * FxaaFloat2(-1.0, -1.0);
  204. posP += offNP * FxaaFloat2( 1.0, 1.0);
  205. #endif
  206. #if FXAA_SEARCH_ACCELERATION == 2
  207. posN += offNP * FxaaFloat2(-1.5, -1.5);
  208. posP += offNP * FxaaFloat2( 1.5, 1.5);
  209. offNP *= FxaaFloat2(2.0, 2.0);
  210. #endif
  211. #if FXAA_SEARCH_ACCELERATION == 3
  212. posN += offNP * FxaaFloat2(-2.0, -2.0);
  213. posP += offNP * FxaaFloat2( 2.0, 2.0);
  214. offNP *= FxaaFloat2(3.0, 3.0);
  215. #endif
  216. #if FXAA_SEARCH_ACCELERATION == 4
  217. posN += offNP * FxaaFloat2(-2.5, -2.5);
  218. posP += offNP * FxaaFloat2( 2.5, 2.5);
  219. offNP *= FxaaFloat2(4.0, 4.0);
  220. #endif
  221. for(int i = 0; i < FXAA_SEARCH_STEPS; i++) {
  222. #if FXAA_SEARCH_ACCELERATION == 1
  223. if(!doneN) lumaEndN =
  224. FxaaLuma(FxaaTexLod0(tex, posN.xy).xyz);
  225. if(!doneP) lumaEndP =
  226. FxaaLuma(FxaaTexLod0(tex, posP.xy).xyz);
  227. #else
  228. if(!doneN) lumaEndN =
  229. FxaaLuma(FxaaTexGrad(tex, posN.xy, offNP).xyz);
  230. if(!doneP) lumaEndP =
  231. FxaaLuma(FxaaTexGrad(tex, posP.xy, offNP).xyz);
  232. #endif
  233. doneN = doneN || (abs(lumaEndN - lumaN) >= gradientN);
  234. doneP = doneP || (abs(lumaEndP - lumaN) >= gradientN);
  235. if(doneN && doneP) break;
  236. if(!doneN) posN -= offNP;
  237. if(!doneP) posP += offNP; }
  238. float dstN = horzSpan ? pos.x - posN.x : pos.y - posN.y;
  239. float dstP = horzSpan ? posP.x - pos.x : posP.y - pos.y;
  240. bool directionN = dstN < dstP;
  241. lumaEndN = directionN ? lumaEndN : lumaEndP;
  242. if(((lumaM - lumaN) < 0.0) == ((lumaEndN - lumaN) < 0.0))
  243. lengthSign = 0.0;
  244. float spanLength = (dstP + dstN);
  245. dstN = directionN ? dstN : dstP;
  246. float subPixelOffset = (0.5 + (dstN * (-1.0/spanLength))) * lengthSign;
  247. float3 rgbF = FxaaTexLod0(tex, FxaaFloat2(
  248. pos.x + (horzSpan ? 0.0 : subPixelOffset),
  249. pos.y + (horzSpan ? subPixelOffset : 0.0))).xyz;
  250. #if FXAA_SUBPIX == 0
  251. return FxaaFilterReturn(rgbF);
  252. #else
  253. return FxaaFilterReturn(FxaaLerp3(rgbL, rgbF, blendL));
  254. #endif
  255. }