Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.51 KB | None | 0 0
  1. #extension GL_EXT_gpu_shader4 : enable
  2.  
  3. // Copyright for FXAA Source
  4. //
  5. // Copyright (c) 2010 NVIDIA Corporation. All rights reserved.
  6. //
  7. // TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
  8. // *AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
  9. // OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
  10. // AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS
  11. // BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
  12. // WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
  13. // BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS)
  14. // ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF NVIDIA HAS
  15. // BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  16.  
  17. #define FXAA_GLSL_120 1
  18.  
  19. /*============================================================================
  20. FXAA
  21. ============================================================================*/
  22.  
  23. /*============================================================================
  24. API PORTING
  25. ============================================================================*/
  26. #ifndef FXAA_GLSL_120
  27. #define FXAA_GLSL_120 0
  28. #endif
  29. #ifndef FXAA_GLSL_130
  30. #define FXAA_GLSL_130 0
  31. #endif
  32.  
  33. #define int2 ivec2
  34. #define float2 vec2
  35. #define float3 vec3
  36. #define float4 vec4
  37. #define FxaaBool3 bvec3
  38. #define FxaaInt2 ivec2
  39. #define FxaaFloat2 vec2
  40. #define FxaaFloat3 vec3
  41. #define FxaaFloat4 vec4
  42. #define FxaaBool2Float(a) mix(0.0, 1.0, (a))
  43. #define FxaaPow3(x, y) pow(x, y)
  44. #define FxaaSel3(f, t, b) mix((f), (t), (b))
  45. #define FxaaTex sampler2D
  46. /*--------------------------------------------------------------------------*/
  47. #if FXAA_GLSL_120
  48. // Requires "#version 120" or better
  49. #define FxaaTexLod0(t, p) texture2DLod(t, p, 0.0)
  50. #define FxaaTexOff(t, p, o, r) texture2DLodOffset(t, p, 0.0, o)
  51. #endif
  52. /*--------------------------------------------------------------------------*/
  53. #if FXAA_GLSL_130
  54. // Requires "#version 130" or better
  55. #define FxaaTexLod0(t, p) textureLod(t, p, 0.0)
  56. #define FxaaTexOff(t, p, o, r) textureLodOffset(t, p, 0.0, o)
  57. #endif
  58. /*--------------------------------------------------------------------------*/
  59.  
  60. #define FxaaToFloat3(a) FxaaFloat3((a), (a), (a))
  61. float4 FxaaTexGrad(FxaaTex tex, float2 pos, float2 grad) {
  62. #if FXAA_GLSL_120
  63. return texture2DGrad(tex, pos.xy, grad, grad);
  64. #endif
  65. #if FXAA_GLSL_130
  66. return textureGrad(tex, pos.xy, grad, grad);
  67. #endif
  68. }
  69.  
  70. /*============================================================================
  71. SRGB KNOBS
  72. ------------------------------------------------------------------------------
  73. FXAA_SRGB_ROP - Set to 1 when applying FXAA to an sRGB back buffer (DX10/11).
  74. This will do the sRGB to linear transform,
  75. as ROP will expect linear color from this shader,
  76. and this shader works in non-linear color.
  77. ============================================================================*/
  78. #define FXAA_SRGB_ROP 0
  79.  
  80. /*============================================================================
  81. DEBUG KNOBS
  82. ------------------------------------------------------------------------------
  83. All debug knobs draw FXAA-untouched pixels in FXAA computed luma (monochrome).
  84.  
  85. FXAA_DEBUG_PASSTHROUGH - Red for pixels which are filtered by FXAA with a
  86. yellow tint on sub-pixel aliasing filtered by FXAA.
  87. FXAA_DEBUG_HORZVERT - Blue for horizontal edges, gold for vertical edges.
  88. FXAA_DEBUG_PAIR - Blue/green for the 2 pixel pair choice.
  89. FXAA_DEBUG_NEGPOS - Red/blue for which side of center of span.
  90. FXAA_DEBUG_OFFSET - Red/blue for -/+ x, gold/skyblue for -/+ y.
  91. ============================================================================*/
  92. #ifndef FXAA_DEBUG_PASSTHROUGH
  93. #define FXAA_DEBUG_PASSTHROUGH 0
  94. #endif
  95. #ifndef FXAA_DEBUG_HORZVERT
  96. #define FXAA_DEBUG_HORZVERT 0
  97. #endif
  98. #ifndef FXAA_DEBUG_PAIR
  99. #define FXAA_DEBUG_PAIR 0
  100. #endif
  101. #ifndef FXAA_DEBUG_NEGPOS
  102. #define FXAA_DEBUG_NEGPOS 0
  103. #endif
  104. #ifndef FXAA_DEBUG_OFFSET
  105. #define FXAA_DEBUG_OFFSET 0
  106. #endif
  107. /*--------------------------------------------------------------------------*/
  108. #if FXAA_DEBUG_PASSTHROUGH || FXAA_DEBUG_HORZVERT || FXAA_DEBUG_PAIR
  109. #define FXAA_DEBUG 1
  110. #endif
  111. #if FXAA_DEBUG_NEGPOS || FXAA_DEBUG_OFFSET
  112. #define FXAA_DEBUG 1
  113. #endif
  114. #ifndef FXAA_DEBUG
  115. #define FXAA_DEBUG 0
  116. #endif
  117.  
  118. /*============================================================================
  119. COMPILE-IN KNOBS
  120. ------------------------------------------------------------------------------
  121. FXAA_PRESET - Choose compile-in knob preset 0-5.
  122. ------------------------------------------------------------------------------
  123. FXAA_EDGE_THRESHOLD - The minimum amount of local contrast required
  124. to apply algorithm.
  125. 1.0/3.0 - too little
  126. 1.0/4.0 - good start
  127. 1.0/8.0 - applies to more edges
  128. 1.0/16.0 - overkill
  129. ------------------------------------------------------------------------------
  130. FXAA_EDGE_THRESHOLD_MIN - Trims the algorithm from processing darks.
  131. Perf optimization.
  132. 1.0/32.0 - visible limit (smaller isn't visible)
  133. 1.0/16.0 - good compromise
  134. 1.0/12.0 - upper limit (seeing artifacts)
  135. ------------------------------------------------------------------------------
  136. FXAA_SEARCH_STEPS - Maximum number of search steps for end of span.
  137. ------------------------------------------------------------------------------
  138. FXAA_SEARCH_ACCELERATION - How much to accelerate search,
  139. 1 - no acceleration
  140. 2 - skip by 2 pixels
  141. 3 - skip by 3 pixels
  142. 4 - skip by 4 pixels
  143. ------------------------------------------------------------------------------
  144. FXAA_SEARCH_THRESHOLD - Controls when to stop searching.
  145. 1.0/4.0 - seems to be the best quality wise
  146. ------------------------------------------------------------------------------
  147. FXAA_SUBPIX_FASTER - Turn on lower quality but faster subpix path.
  148. Not recomended, but used in preset 0.
  149. ------------------------------------------------------------------------------
  150. FXAA_SUBPIX - Toggle subpix filtering.
  151. 0 - turn off
  152. 1 - turn on
  153. 2 - turn on full (ignores FXAA_SUBPIX_TRIM and CAP)
  154. ------------------------------------------------------------------------------
  155. FXAA_SUBPIX_TRIM - Controls sub-pixel aliasing removal.
  156. 1.0/2.0 - low removal
  157. 1.0/3.0 - medium removal
  158. 1.0/4.0 - default removal
  159. 1.0/8.0 - high removal
  160. 0.0 - complete removal
  161. ------------------------------------------------------------------------------
  162. FXAA_SUBPIX_CAP - Insures fine detail is not completely removed.
  163. This is important for the transition of sub-pixel detail,
  164. like fences and wires.
  165. 3.0/4.0 - default (medium amount of filtering)
  166. 7.0/8.0 - high amount of filtering
  167. 1.0 - no capping of sub-pixel aliasing removal
  168. ============================================================================*/
  169.  
  170. float FXAA_EDGE_THRESHOLD = (1.0/8.0);
  171. float FXAA_EDGE_THRESHOLD_MIN = (1.0/24.0);
  172. int FXAA_SEARCH_STEPS = 16;
  173. int FXAA_SEARCH_ACCELERATION = 1;
  174. float FXAA_SEARCH_THRESHOLD = (1.0/4.0);
  175. int FXAA_SUBPIX = 1;
  176. int FXAA_SUBPIX_FASTER = 0;
  177. float FXAA_SUBPIX_CAP = (3.0/4.0);
  178. float FXAA_SUBPIX_TRIM = (1.0/4.0);
  179. float FXAA_SUBPIX_TRIM_SCALE = (1.0/0.75);
  180.  
  181. void FXAA_set_preset(int preset) {
  182.  
  183. if (preset > 6)
  184. preset = 6;
  185. /*--------------------------------------------------------------------------*/
  186. if (preset == 0) {
  187. FXAA_EDGE_THRESHOLD = (1.0/4.0);
  188. FXAA_EDGE_THRESHOLD_MIN = (1.0/12.0);
  189. FXAA_SEARCH_STEPS = 2;
  190. FXAA_SEARCH_ACCELERATION = 4;
  191. FXAA_SEARCH_THRESHOLD = (1.0/4.0);
  192. FXAA_SUBPIX = 1;
  193. FXAA_SUBPIX_FASTER = 1;
  194. FXAA_SUBPIX_CAP = (2.0/3.0);
  195. FXAA_SUBPIX_TRIM = (1.0/4.0);
  196. }
  197. /*--------------------------------------------------------------------------*/
  198. else if (preset == 1) {
  199. FXAA_EDGE_THRESHOLD = (1.0/8.0);
  200. FXAA_EDGE_THRESHOLD_MIN = (1.0/16.0);
  201. FXAA_SEARCH_STEPS = 4;
  202. FXAA_SEARCH_ACCELERATION = 3;
  203. FXAA_SEARCH_THRESHOLD = (1.0/4.0);
  204. FXAA_SUBPIX = 1;
  205. FXAA_SUBPIX_FASTER = 0;
  206. FXAA_SUBPIX_CAP = (3.0/4.0);
  207. FXAA_SUBPIX_TRIM = (1.0/4.0);
  208. }
  209. /*--------------------------------------------------------------------------*/
  210. else if (preset == 2) {
  211. FXAA_EDGE_THRESHOLD = (1.0/8.0);
  212. FXAA_EDGE_THRESHOLD_MIN = (1.0/24.0);
  213. FXAA_SEARCH_STEPS = 8;
  214. FXAA_SEARCH_ACCELERATION = 2;
  215. FXAA_SEARCH_THRESHOLD = (1.0/4.0);
  216. FXAA_SUBPIX = 1;
  217. FXAA_SUBPIX_FASTER = 0;
  218. FXAA_SUBPIX_CAP = (3.0/4.0);
  219. FXAA_SUBPIX_TRIM = (1.0/4.0);
  220. }
  221. /*--------------------------------------------------------------------------*/
  222. else if (preset == 3) {
  223. FXAA_EDGE_THRESHOLD = (1.0/8.0);
  224. FXAA_EDGE_THRESHOLD_MIN = (1.0/24.0);
  225. FXAA_SEARCH_STEPS = 16;
  226. FXAA_SEARCH_ACCELERATION = 1;
  227. FXAA_SEARCH_THRESHOLD = (1.0/4.0);
  228. FXAA_SUBPIX = 1;
  229. FXAA_SUBPIX_FASTER = 0;
  230. FXAA_SUBPIX_CAP = (3.0/4.0);
  231. FXAA_SUBPIX_TRIM = (1.0/4.0);
  232. }
  233. /*--------------------------------------------------------------------------*/
  234. else if (preset == 4) {
  235. FXAA_EDGE_THRESHOLD = (1.0/8.0);
  236. FXAA_EDGE_THRESHOLD_MIN = (1.0/24.0);
  237. FXAA_SEARCH_STEPS = 24;
  238. FXAA_SEARCH_ACCELERATION = 1;
  239. FXAA_SEARCH_THRESHOLD = (1.0/4.0);
  240. FXAA_SUBPIX = 1;
  241. FXAA_SUBPIX_FASTER = 0;
  242. FXAA_SUBPIX_CAP = (3.0/4.0);
  243. FXAA_SUBPIX_TRIM = (1.0/4.0);
  244. }
  245. /*--------------------------------------------------------------------------*/
  246. else if (preset == 5) {
  247. FXAA_EDGE_THRESHOLD = (1.0/8.0);
  248. FXAA_EDGE_THRESHOLD_MIN = (1.0/24.0);
  249. FXAA_SEARCH_STEPS = 32;
  250. FXAA_SEARCH_ACCELERATION = 1;
  251. FXAA_SEARCH_THRESHOLD = (1.0/4.0);
  252. FXAA_SUBPIX = 1;
  253. FXAA_SUBPIX_FASTER = 0;
  254. FXAA_SUBPIX_CAP = (7.0/8.0);
  255. FXAA_SUBPIX_TRIM = (1.0/8.0);
  256. }
  257. /*--------------------------------------------------------------------------*/
  258. else if (preset == 6) {
  259. FXAA_EDGE_THRESHOLD = (1.0/12.0);
  260. FXAA_EDGE_THRESHOLD_MIN = (1.0/24.0);
  261. FXAA_SEARCH_STEPS = 32;
  262. FXAA_SEARCH_ACCELERATION = 1;
  263. FXAA_SEARCH_THRESHOLD = (1.0/4.0);
  264. FXAA_SUBPIX = 1;
  265. FXAA_SUBPIX_FASTER = 0;
  266. FXAA_SUBPIX_CAP = (1.0);
  267. FXAA_SUBPIX_TRIM = (0.0);
  268. }
  269. /*--------------------------------------------------------------------------*/
  270. FXAA_SUBPIX_TRIM_SCALE = (1.0/(1.0 - FXAA_SUBPIX_TRIM));
  271.  
  272. }
  273.  
  274. /*============================================================================
  275. HELPERS
  276. ============================================================================*/
  277. // Return the luma, the estimation of luminance from rgb inputs.
  278. // This approximates luma using one FMA instruction,
  279. // skipping normalization and tossing out blue.
  280. // FxaaLuma() will range 0.0 to 2.963210702.
  281. float FxaaLuma(float3 rgb) {
  282. return rgb.y * (0.587/0.299) + rgb.x; }
  283. /*--------------------------------------------------------------------------*/
  284. float3 FxaaLerp3(float3 a, float3 b, float amountOfA) {
  285. return (FxaaToFloat3(-amountOfA) * b) +
  286. ((a * FxaaToFloat3(amountOfA)) + b); }
  287. /*--------------------------------------------------------------------------*/
  288. // Support any extra filtering before returning color.
  289. float3 FxaaFilterReturn(float3 rgb) {
  290. #if FXAA_SRGB_ROP
  291. // Do sRGB encoded value to linear conversion.
  292. return FxaaSel3(
  293. rgb * FxaaToFloat3(1.0/12.92),
  294. FxaaPow3(
  295. rgb * FxaaToFloat3(1.0/1.055) + FxaaToFloat3(0.055/1.055),
  296. FxaaToFloat3(2.4)),
  297. rgb > FxaaToFloat3(0.04045));
  298. #else
  299. return rgb;
  300. #endif
  301. }
  302.  
  303. /*============================================================================
  304. PIXEL SHADER
  305. ============================================================================*/
  306. float3 FxaaPixelShader(
  307. // Output of FxaaVertexShader interpolated across screen.
  308. // xy -> actual texture position {0.0 to 1.0}
  309. float2 pos,
  310. // Input texture.
  311. FxaaTex tex,
  312. // RCPFRAME SHOULD PIXEL SHADER CONSTANTS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  313. // {1.0/frameWidth, 1.0/frameHeight}
  314. float2 rcpFrame) {
  315.  
  316. /*----------------------------------------------------------------------------
  317. EARLY EXIT IF LOCAL CONTRAST BELOW EDGE DETECT LIMIT
  318. ------------------------------------------------------------------------------
  319. Majority of pixels of a typical image do not require filtering,
  320. often pixels are grouped into blocks which could benefit from early exit
  321. right at the beginning of the algorithm.
  322. Given the following neighborhood,
  323.  
  324. N
  325. W M E
  326. S
  327.  
  328. If the difference in local maximum and minimum luma (contrast "range")
  329. is lower than a threshold proportional to the maximum local luma ("rangeMax"),
  330. then the shader early exits (no visible aliasing).
  331. This threshold is clamped at a minimum value ("FXAA_EDGE_THRESHOLD_MIN")
  332. to avoid processing in really dark areas.
  333. ----------------------------------------------------------------------------*/
  334. float3 rgbN = FxaaTexOff(tex, pos.xy, FxaaInt2( 0,-1), rcpFrame).xyz;
  335. float3 rgbW = FxaaTexOff(tex, pos.xy, FxaaInt2(-1, 0), rcpFrame).xyz;
  336. float3 rgbM = FxaaTexOff(tex, pos.xy, FxaaInt2( 0, 0), rcpFrame).xyz;
  337. float3 rgbE = FxaaTexOff(tex, pos.xy, FxaaInt2( 1, 0), rcpFrame).xyz;
  338. float3 rgbS = FxaaTexOff(tex, pos.xy, FxaaInt2( 0, 1), rcpFrame).xyz;
  339. float lumaN = FxaaLuma(rgbN);
  340. float lumaW = FxaaLuma(rgbW);
  341. float lumaM = FxaaLuma(rgbM);
  342. float lumaE = FxaaLuma(rgbE);
  343. float lumaS = FxaaLuma(rgbS);
  344. float rangeMin = min(lumaM, min(min(lumaN, lumaW), min(lumaS, lumaE)));
  345. float rangeMax = max(lumaM, max(max(lumaN, lumaW), max(lumaS, lumaE)));
  346. float range = rangeMax - rangeMin;
  347. #if FXAA_DEBUG
  348. float lumaO = lumaM / (1.0 + (0.587/0.299));
  349. #endif
  350. if(range < max(FXAA_EDGE_THRESHOLD_MIN, rangeMax * FXAA_EDGE_THRESHOLD)) {
  351. #if FXAA_DEBUG
  352. return FxaaFilterReturn(FxaaToFloat3(lumaO));
  353. #endif
  354. return FxaaFilterReturn(rgbM);
  355.  
  356. }
  357.  
  358. float3 rgbL = rgbN + rgbW + rgbM + rgbE + rgbS;
  359.  
  360. if (FXAA_SUBPIX > 0) {
  361. if (FXAA_SUBPIX_FASTER != 0) {
  362. rgbL *= FxaaToFloat3(1.0/5.0);
  363. }
  364. }
  365.  
  366. /*----------------------------------------------------------------------------
  367. COMPUTE LOWPASS
  368. ------------------------------------------------------------------------------
  369. FXAA computes a local neighborhood lowpass value as follows,
  370.  
  371. (N + W + E + S)/4
  372.  
  373. Then uses the ratio of the contrast range of the lowpass
  374. and the range found in the early exit check,
  375. as a sub-pixel aliasing detection filter.
  376. When FXAA detects sub-pixel aliasing (such as single pixel dots),
  377. it later blends in "blendL" amount
  378. of a lowpass value (computed in the next section) to the final result.
  379. ----------------------------------------------------------------------------*/
  380.  
  381. float blendL = 0.0;
  382.  
  383. if (FXAA_SUBPIX > 0) {
  384. float lumaL = (lumaN + lumaW + lumaE + lumaS) * 0.25;
  385. float rangeL = abs(lumaL - lumaM);
  386.  
  387. if (FXAA_SUBPIX == 1) {
  388. blendL = max(0.0,
  389. (rangeL / range) - FXAA_SUBPIX_TRIM) * FXAA_SUBPIX_TRIM_SCALE;
  390. blendL = min(FXAA_SUBPIX_CAP, blendL);
  391. }
  392. if (FXAA_SUBPIX == 2) {
  393. blendL = rangeL / range;
  394. }
  395. #if FXAA_DEBUG_PASSTHROUGH
  396. if (FXAA_SUBPIX == 0) {
  397. blendL = 0.0;
  398. }
  399. return FxaaFilterReturn(
  400. FxaaFloat3(1.0, blendL/FXAA_SUBPIX_CAP, 0.0));
  401. #endif
  402. }
  403.  
  404. /*----------------------------------------------------------------------------
  405. CHOOSE VERTICAL OR HORIZONTAL SEARCH
  406. ------------------------------------------------------------------------------
  407. FXAA uses the following local neighborhood,
  408.  
  409. NW N NE
  410. W M E
  411. SW S SE
  412.  
  413. To compute an edge amount for both vertical and horizontal directions.
  414. Note edge detect filters like Sobel fail on single pixel lines through M.
  415. FXAA takes the weighted average magnitude of the high-pass values
  416. for rows and columns as an indication of local edge amount.
  417.  
  418. A lowpass value for anti-sub-pixel-aliasing is computed as
  419. (N+W+E+S+M+NW+NE+SW+SE)/9.
  420. This full box pattern has higher quality than other options.
  421.  
  422. Note following this block, both vertical and horizontal cases
  423. flow in parallel (reusing the horizontal variables).
  424. ----------------------------------------------------------------------------*/
  425. float3 rgbNW = FxaaTexOff(tex, pos.xy, FxaaInt2(-1,-1), rcpFrame).xyz;
  426. float3 rgbNE = FxaaTexOff(tex, pos.xy, FxaaInt2( 1,-1), rcpFrame).xyz;
  427. float3 rgbSW = FxaaTexOff(tex, pos.xy, FxaaInt2(-1, 1), rcpFrame).xyz;
  428. float3 rgbSE = FxaaTexOff(tex, pos.xy, FxaaInt2( 1, 1), rcpFrame).xyz;
  429. if ((FXAA_SUBPIX_FASTER == 0) && (FXAA_SUBPIX > 0)) {
  430. rgbL += (rgbNW + rgbNE + rgbSW + rgbSE);
  431. rgbL *= FxaaToFloat3(1.0/9.0);
  432. }
  433. float lumaNW = FxaaLuma(rgbNW);
  434. float lumaNE = FxaaLuma(rgbNE);
  435. float lumaSW = FxaaLuma(rgbSW);
  436. float lumaSE = FxaaLuma(rgbSE);
  437. float edgeVert =
  438. abs((0.25 * lumaNW) + (-0.5 * lumaN) + (0.25 * lumaNE)) +
  439. abs((0.50 * lumaW ) + (-1.0 * lumaM) + (0.50 * lumaE )) +
  440. abs((0.25 * lumaSW) + (-0.5 * lumaS) + (0.25 * lumaSE));
  441. float edgeHorz =
  442. abs((0.25 * lumaNW) + (-0.5 * lumaW) + (0.25 * lumaSW)) +
  443. abs((0.50 * lumaN ) + (-1.0 * lumaM) + (0.50 * lumaS )) +
  444. abs((0.25 * lumaNE) + (-0.5 * lumaE) + (0.25 * lumaSE));
  445. bool horzSpan = edgeHorz >= edgeVert;
  446. #if FXAA_DEBUG_HORZVERT
  447. if(horzSpan) return FxaaFilterReturn(FxaaFloat3(1.0, 0.75, 0.0));
  448. else return FxaaFilterReturn(FxaaFloat3(0.0, 0.50, 1.0));
  449. #endif
  450. float lengthSign = horzSpan ? -rcpFrame.y : -rcpFrame.x;
  451. if(!horzSpan) lumaN = lumaW;
  452. if(!horzSpan) lumaS = lumaE;
  453. float gradientN = abs(lumaN - lumaM);
  454. float gradientS = abs(lumaS - lumaM);
  455. lumaN = (lumaN + lumaM) * 0.5;
  456. lumaS = (lumaS + lumaM) * 0.5;
  457.  
  458. /*----------------------------------------------------------------------------
  459. CHOOSE SIDE OF PIXEL WHERE GRADIENT IS HIGHEST
  460. ------------------------------------------------------------------------------
  461. This chooses a pixel pair.
  462. For "horzSpan == true" this will be a vertical pair,
  463.  
  464. [N] N
  465. [M] or [M]
  466. S [S]
  467.  
  468. Note following this block, both {N,M} and {S,M} cases
  469. flow in parallel (reusing the {N,M} variables).
  470.  
  471. This pair of image rows or columns is searched below
  472. in the positive and negative direction
  473. until edge status changes
  474. (or the maximum number of search steps is reached).
  475. ----------------------------------------------------------------------------*/
  476. bool pairN = gradientN >= gradientS;
  477. #if FXAA_DEBUG_PAIR
  478. if(pairN) return FxaaFilterReturn(FxaaFloat3(0.0, 0.0, 1.0));
  479. else return FxaaFilterReturn(FxaaFloat3(0.0, 1.0, 0.0));
  480. #endif
  481. if(!pairN) lumaN = lumaS;
  482. if(!pairN) gradientN = gradientS;
  483. if(!pairN) lengthSign *= -1.0;
  484. float2 posN;
  485. posN.x = pos.x + (horzSpan ? 0.0 : lengthSign * 0.5);
  486. posN.y = pos.y + (horzSpan ? lengthSign * 0.5 : 0.0);
  487.  
  488. /*----------------------------------------------------------------------------
  489. CHOOSE SEARCH LIMITING VALUES
  490. ------------------------------------------------------------------------------
  491. Search limit (+/- gradientN) is a function of local gradient.
  492. ----------------------------------------------------------------------------*/
  493. gradientN *= FXAA_SEARCH_THRESHOLD;
  494.  
  495. /*----------------------------------------------------------------------------
  496. SEARCH IN BOTH DIRECTIONS UNTIL FIND LUMA PAIR AVERAGE IS OUT OF RANGE
  497. ------------------------------------------------------------------------------
  498. This loop searches either in vertical or horizontal directions,
  499. and in both the negative and positive direction in parallel.
  500. This loop fusion is faster than searching separately.
  501.  
  502. The search is accelerated using FXAA_SEARCH_ACCELERATION length box filter
  503. via anisotropic filtering with specified texture gradients.
  504. ----------------------------------------------------------------------------*/
  505. float2 posP = posN;
  506. float2 offNP = horzSpan ?
  507. FxaaFloat2(rcpFrame.x, 0.0) :
  508. FxaaFloat2(0.0f, rcpFrame.y);
  509. float lumaEndN = lumaN;
  510. float lumaEndP = lumaN;
  511. bool doneN = false;
  512. bool doneP = false;
  513. if (FXAA_SEARCH_ACCELERATION == 1) {
  514. posN += offNP * FxaaFloat2(-1.0, -1.0);
  515. posP += offNP * FxaaFloat2( 1.0, 1.0);
  516. }
  517. if (FXAA_SEARCH_ACCELERATION == 2 ) {
  518. posN += offNP * FxaaFloat2(-1.5, -1.5);
  519. posP += offNP * FxaaFloat2( 1.5, 1.5);
  520. offNP *= FxaaFloat2(2.0, 2.0);
  521. }
  522. if (FXAA_SEARCH_ACCELERATION == 3) {
  523. posN += offNP * FxaaFloat2(-2.0, -2.0);
  524. posP += offNP * FxaaFloat2( 2.0, 2.0);
  525. offNP *= FxaaFloat2(3.0, 3.0);
  526. }
  527. if (FXAA_SEARCH_ACCELERATION == 4) {
  528. posN += offNP * FxaaFloat2(-2.5, -2.5);
  529. posP += offNP * FxaaFloat2( 2.5, 2.5);
  530. offNP *= FxaaFloat2(4.0, 4.0);
  531. }
  532. for(int i = 0; i < FXAA_SEARCH_STEPS; i++) {
  533. if (FXAA_SEARCH_ACCELERATION == 1) {
  534. if(!doneN) lumaEndN =
  535. FxaaLuma(FxaaTexLod0(tex, posN.xy).xyz);
  536. if(!doneP) lumaEndP =
  537. FxaaLuma(FxaaTexLod0(tex, posP.xy).xyz);
  538. } else {
  539. if(!doneN) lumaEndN =
  540. FxaaLuma(FxaaTexGrad(tex, posN.xy, offNP).xyz);
  541. if(!doneP) lumaEndP =
  542. FxaaLuma(FxaaTexGrad(tex, posP.xy, offNP).xyz);
  543. }
  544. doneN = doneN || (abs(lumaEndN - lumaN) >= gradientN);
  545. doneP = doneP || (abs(lumaEndP - lumaN) >= gradientN);
  546. if(doneN && doneP) break;
  547. if(!doneN) posN -= offNP;
  548. if(!doneP) posP += offNP; }
  549.  
  550. /*----------------------------------------------------------------------------
  551. HANDLE IF CENTER IS ON POSITIVE OR NEGATIVE SIDE
  552. ------------------------------------------------------------------------------
  553. FXAA uses the pixel's position in the span
  554. in combination with the values (lumaEnd*) at the ends of the span,
  555. to determine filtering.
  556.  
  557. This step computes which side of the span the pixel is on.
  558. On negative side if dstN < dstP,
  559.  
  560. posN pos posP
  561. |-----------|------|------------------|
  562. | | | |
  563. |<--dstN--->|<---------dstP---------->|
  564. |
  565. span center
  566.  
  567. ----------------------------------------------------------------------------*/
  568. float dstN = horzSpan ? pos.x - posN.x : pos.y - posN.y;
  569. float dstP = horzSpan ? posP.x - pos.x : posP.y - pos.y;
  570. bool directionN = dstN < dstP;
  571. #if FXAA_DEBUG_NEGPOS
  572. if(directionN) return FxaaFilterReturn(FxaaFloat3(1.0, 0.0, 0.0));
  573. else return FxaaFilterReturn(FxaaFloat3(0.0, 0.0, 1.0));
  574. #endif
  575. lumaEndN = directionN ? lumaEndN : lumaEndP;
  576.  
  577. /*----------------------------------------------------------------------------
  578. CHECK IF PIXEL IS IN SECTION OF SPAN WHICH GETS NO FILTERING
  579. ------------------------------------------------------------------------------
  580. If both the pair luma at the end of the span (lumaEndN)
  581. and middle pixel luma (lumaM)
  582. are on the same side of the middle pair average luma (lumaN),
  583. then don't filter.
  584.  
  585. Cases,
  586.  
  587. (1.) "L",
  588.  
  589. lumaM
  590. |
  591. V XXXXXXXX <- other line averaged
  592. XXXXXXX[X]XXXXXXXXXXX <- source pixel line
  593. | . |
  594. --------------------------
  595. [ ]xxxxxx[x]xx[X]XXXXXX <- pair average
  596. --------------------------
  597. ^ ^ ^ ^
  598. | | | |
  599. . |<---->|<---------- no filter region
  600. . | | |
  601. . center | |
  602. . | lumaEndN
  603. . | .
  604. . lumaN .
  605. . .
  606. |<--- span -->|
  607.  
  608.  
  609. (2.) "^" and "-",
  610.  
  611. <- other line averaged
  612. XXXXX[X]XXX <- source pixel line
  613. | | |
  614. --------------------------
  615. [ ]xxxx[x]xx[ ] <- pair average
  616. --------------------------
  617. | | |
  618. |<--->|<--->|<---------- filter both sides
  619.  
  620.  
  621. (3.) "v" and inverse of "-",
  622.  
  623. XXXXXX XXXXXXXXX <- other line averaged
  624. XXXXXXXXXXX[X]XXXXXXXXXXXX <- source pixel line
  625. | | |
  626. --------------------------
  627. XXXX[X]xxxx[x]xx[X]XXXXXXX <- pair average
  628. --------------------------
  629. | | |
  630. |<--->|<--->|<---------- don't filter both!
  631.  
  632.  
  633. Note the "v" case for FXAA requires no filtering.
  634. This is because the inverse of the "-" case is the "v".
  635. Filtering "v" case turns open spans like this,
  636.  
  637. XXXXXXXXX
  638.  
  639. Into this (which is not desired),
  640.  
  641. x+. .+x
  642. XXXXXXXXX
  643.  
  644. ----------------------------------------------------------------------------*/
  645. if(((lumaM - lumaN) < 0.0) == ((lumaEndN - lumaN) < 0.0))
  646. lengthSign = 0.0;
  647.  
  648. /*----------------------------------------------------------------------------
  649. COMPUTE SUB-PIXEL OFFSET AND FILTER SPAN
  650. ------------------------------------------------------------------------------
  651. FXAA filters using a bilinear texture fetch offset
  652. from the middle pixel M towards the center of the pair (NM below).
  653. Maximum filtering will be half way between pair.
  654. Reminder, at this point in the code,
  655. the {N,M} pair is also reused for all cases: {S,M}, {W,M}, and {E,M}.
  656.  
  657. +-------+
  658. | | 0.5 offset
  659. | N | |
  660. | | V
  661. +-------+....---
  662. | |
  663. | M...|....---
  664. | | ^
  665. +-------+ |
  666. . . 0.0 offset
  667. . S .
  668. . .
  669. .........
  670.  
  671. Position on span is used to compute sub-pixel filter offset using simple ramp,
  672.  
  673. posN posP
  674. |\ |<------- 0.5 pixel offset into pair pixel
  675. | \ |
  676. | \ |
  677. ---.......|...\..........|<------- 0.25 pixel offset into pair pixel
  678. ^ | ^\ |
  679. | | | \ |
  680. V | | \ |
  681. ---.......|===|==========|<------- 0.0 pixel offset (ie M pixel)
  682. ^ . | ^ .
  683. | . pos | .
  684. | . . | .
  685. | . . center .
  686. | . . .
  687. | |<->|<---------.-------- dstN
  688. | . . .
  689. | . |<-------->|<------- dstP
  690. | . .
  691. | |<------------>|<------- spanLength
  692. |
  693. subPixelOffset
  694.  
  695. ----------------------------------------------------------------------------*/
  696. float spanLength = (dstP + dstN);
  697. dstN = directionN ? dstN : dstP;
  698. float subPixelOffset = (0.5 + (dstN * (-1.0/spanLength))) * lengthSign;
  699. #if FXAA_DEBUG_OFFSET
  700. float ox = horzSpan ? 0.0 : subPixelOffset*2.0/rcpFrame.x;
  701. float oy = horzSpan ? subPixelOffset*2.0/rcpFrame.y : 0.0;
  702. if(ox < 0.0) return FxaaFilterReturn(
  703. FxaaLerp3(FxaaToFloat3(lumaO),
  704. FxaaFloat3(1.0, 0.0, 0.0), -ox));
  705. if(ox > 0.0) return FxaaFilterReturn(
  706. FxaaLerp3(FxaaToFloat3(lumaO),
  707. FxaaFloat3(0.0, 0.0, 1.0), ox));
  708. if(oy < 0.0) return FxaaFilterReturn(
  709. FxaaLerp3(FxaaToFloat3(lumaO),
  710. FxaaFloat3(1.0, 0.6, 0.2), -oy));
  711. if(oy > 0.0) return FxaaFilterReturn(
  712. FxaaLerp3(FxaaToFloat3(lumaO),
  713. FxaaFloat3(0.2, 0.6, 1.0), oy));
  714. return FxaaFilterReturn(FxaaFloat3(lumaO, lumaO, lumaO));
  715. #endif
  716. float3 rgbF = FxaaTexLod0(tex, FxaaFloat2(
  717. pos.x + (horzSpan ? 0.0 : subPixelOffset),
  718. pos.y + (horzSpan ? subPixelOffset : 0.0))).xyz;
  719. if (FXAA_SUBPIX == 0) {
  720. return FxaaFilterReturn(rgbF);
  721. } else {
  722. return FxaaFilterReturn(FxaaLerp3(rgbL, rgbF, blendL));
  723. }
  724. }
  725.  
  726. uniform sampler2D tex0;
  727. uniform int fxaa_preset;
  728. varying vec2 rcpFrame;
  729. noperspective varying vec2 pos;
  730.  
  731. void main() {
  732. FXAA_set_preset(fxaa_preset);
  733. gl_FragColor.xyz = FxaaPixelShader(pos, tex0, rcpFrame);
  734. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement