Guest User

Untitled

a guest
Jul 4th, 2011
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.42 KB | None | 0 0
  1. #extension GL_EXT_gpu_shader4 : enable
  2.  
  3. #define FXAA_PC 0
  4.  
  5.  
  6. /*==========================================================================*/
  7. #ifndef FXAA_PC
  8. //
  9. // FXAA Quality
  10. // The high quality PC algorithm.
  11. //
  12. #define FXAA_PC 0
  13. #endif
  14. /*--------------------------------------------------------------------------*/
  15. #ifndef FXAA_PC_CONSOLE
  16. //
  17. // The console algorithm for PC is included
  18. // for developers targeting really low spec machines.
  19. //
  20. #define FXAA_PC_CONSOLE 1
  21. #endif
  22. /*--------------------------------------------------------------------------*/
  23. #ifndef FXAA_GLSL_120
  24. #define FXAA_GLSL_120 0
  25. #endif
  26. /*--------------------------------------------------------------------------*/
  27. #ifndef FXAA_GLSL_130
  28. #define FXAA_GLSL_130 1
  29. #endif
  30.  
  31. /*==========================================================================*/
  32. #ifndef FXAA_EARLY_EXIT
  33. #define FXAA_EARLY_EXIT 0
  34. #endif
  35. /*--------------------------------------------------------------------------*/
  36. #ifndef FXAA_DISCARD
  37. #define FXAA_DISCARD 1
  38. #endif
  39. /*--------------------------------------------------------------------------*/
  40. #ifndef FXAA_LINEAR
  41. #define FXAA_LINEAR 0
  42. #endif
  43. /*--------------------------------------------------------------------------*/
  44. #ifndef FXAA_FAST_PIXEL_OFFSET
  45. #ifdef GL_EXT_gpu_shader4
  46. #define FXAA_FAST_PIXEL_OFFSET 1
  47. #endif
  48. #ifdef GL_NV_gpu_shader5
  49. #define FXAA_FAST_PIXEL_OFFSET 1
  50. #endif
  51. #ifdef GL_ARB_gpu_shader5
  52. #define FXAA_FAST_PIXEL_OFFSET 1
  53. #endif
  54. #ifndef FXAA_FAST_PIXEL_OFFSET
  55. #define FXAA_FAST_PIXEL_OFFSET 0
  56. #endif
  57. #endif
  58. /*--------------------------------------------------------------------------*/
  59. #ifndef FXAA_GATHER4_ALPHA
  60. #ifdef GL_ARB_gpu_shader5
  61. #define FXAA_GATHER4_ALPHA 1
  62. #endif
  63. #ifdef GL_NV_gpu_shader5
  64. #define FXAA_GATHER4_ALPHA 1
  65. #endif
  66. #ifndef FXAA_GATHER4_ALPHA
  67. #define FXAA_GATHER4_ALPHA 0
  68. #endif
  69. #endif
  70.  
  71. /*============================================================================
  72. FXAA CONSOLE - TUNING KNOBS
  73. ============================================================================*/
  74. #ifndef FXAA_CONSOLE__EDGE_SHARPNESS
  75. #if 0
  76. #define FXAA_CONSOLE__EDGE_SHARPNESS 8.0
  77. #else
  78. #define FXAA_CONSOLE__EDGE_SHARPNESS 4.0
  79. #endif
  80. #endif
  81. /*--------------------------------------------------------------------------*/
  82. #ifndef FXAA_CONSOLE__EDGE_THRESHOLD
  83. #if 0
  84. #define FXAA_CONSOLE__EDGE_THRESHOLD 0.125
  85. #else
  86. #define FXAA_CONSOLE__EDGE_THRESHOLD 0.25
  87. #endif
  88. #endif
  89. /*--------------------------------------------------------------------------*/
  90. #ifndef FXAA_CONSOLE__EDGE_THRESHOLD_MIN
  91. #define FXAA_CONSOLE__EDGE_THRESHOLD_MIN 0.05
  92. #endif
  93.  
  94. /*============================================================================
  95. FXAA QUALITY - TUNING KNOBS
  96. ============================================================================*/
  97. #ifndef FXAA_QUALITY__EDGE_THRESHOLD
  98. #define FXAA_QUALITY__EDGE_THRESHOLD (1.0/8.0)
  99. #endif
  100. /*--------------------------------------------------------------------------*/
  101. #ifndef FXAA_QUALITY__EDGE_THRESHOLD_MIN
  102. #define FXAA_QUALITY__EDGE_THRESHOLD_MIN (1.0/16.0)
  103. #endif
  104. /*--------------------------------------------------------------------------*/
  105. #ifndef FXAA_QUALITY__SUBPIX_CAP
  106. #define FXAA_QUALITY__SUBPIX_CAP (7.0/8.0)
  107. #endif
  108. /*--------------------------------------------------------------------------*/
  109. #ifndef FXAA_QUALITY__SUBPIX_TRIM
  110. #define FXAA_QUALITY__SUBPIX_TRIM (1.0/8.0)
  111. #endif
  112.  
  113.  
  114. /*============================================================================
  115.  
  116. API PORTING
  117.  
  118. ============================================================================*/
  119. #if FXAA_GLSL_120
  120. // Requires,
  121. // #version 120
  122. // And at least,
  123. // #extension GL_EXT_gpu_shader4 : enable
  124. // (or set FXAA_FAST_PIXEL_OFFSET 1 to work like DX9)
  125. #define half float
  126. #define half2 vec2
  127. #define half3 vec3
  128. #define half4 vec4
  129. #define int2 ivec2
  130. #define float2 vec2
  131. #define float3 vec3
  132. #define float4 vec4
  133. #define FxaaInt2 ivec2
  134. #define FxaaFloat2 vec2
  135. #define FxaaFloat3 vec3
  136. #define FxaaFloat4 vec4
  137. #define FxaaDiscard discard
  138. #define FxaaDot3(a, b) dot(a, b)
  139. #define FxaaSat(x) clamp(x, 0.0, 1.0)
  140. #define FxaaLerp(x,y,s) mix(x,y,s)
  141. #define FxaaTex sampler2D
  142. #define FxaaTexTop(t, p) texture2DLod(t, p, 0.0)
  143. #if (FXAA_FAST_PIXEL_OFFSET == 1)
  144. #define FxaaTexOff(t, p, o, r) texture2DLodOffset(t, p, 0.0, o)
  145. #else
  146. #define FxaaTexOff(t, p, o, r) texture2DLod(t, p + (o * r), 0.0)
  147. #endif
  148. #if (FXAA_GATHER4_ALPHA == 1)
  149. // use #extension GL_ARB_gpu_shader5 : enable
  150. #define FxaaTexAlpha4(t, p, r) textureGather(t, p, 3)
  151. #define FxaaTexOffAlpha4(t, p, o, r) textureGatherOffset(t, p, o, 3)
  152. #endif
  153. #endif
  154. /*--------------------------------------------------------------------------*/
  155. #if FXAA_GLSL_130
  156. // Requires "#version 130" or better
  157. #define half float
  158. #define half2 vec2
  159. #define half3 vec3
  160. #define half4 vec4
  161. #define int2 ivec2
  162. #define float2 vec2
  163. #define float3 vec3
  164. #define float4 vec4
  165. #define FxaaInt2 ivec2
  166. #define FxaaFloat2 vec2
  167. #define FxaaFloat3 vec3
  168. #define FxaaFloat4 vec4
  169. #define FxaaDiscard discard
  170. #define FxaaDot3(a, b) dot(a, b)
  171. #define FxaaSat(x) clamp(x, 0.0, 1.0)
  172. #define FxaaLerp(x,y,s) mix(x,y,s)
  173. #define FxaaTex sampler2D
  174. #define FxaaTexTop(t, p) textureLod(t, p, 0.0)
  175. #define FxaaTexOff(t, p, o, r) textureLodOffset(t, p, 0.0, o)
  176. #if (FXAA_GATHER4_ALPHA == 1)
  177. // use #extension GL_ARB_gpu_shader5 : enable
  178. #define FxaaTexAlpha4(t, p, r) textureGather(t, p, 3)
  179. #define FxaaTexOffAlpha4(t, p, o, r) textureGatherOffset(t, p, o, 3)
  180. #endif
  181. #endif
  182.  
  183. /*============================================================================
  184.  
  185. FXAA3 CONSOLE - PC PIXEL SHADER
  186.  
  187. ------------------------------------------------------------------------------
  188. Using a modified version of the PS3 version here to best target old hardware.
  189. ============================================================================*/
  190. #if (FXAA_PC_CONSOLE == 1)
  191. /*--------------------------------------------------------------------------*/
  192. half4 FxaaPixelShader(
  193. // {xy} = center of pixel
  194. float2 pos,
  195. // {xy__} = upper left of pixel
  196. // {__zw} = lower right of pixel
  197. float4 posPos,
  198. // {rgb_} = color in linear or perceptual color space
  199. // {___a} = alpha output is junk value
  200. FxaaTex tex,
  201. // This must be from a constant/uniform.
  202. // {xy} = rcpFrame not used on PC version of FXAA Console
  203. float2 rcpFrame,
  204. // This must be from a constant/uniform.
  205. // {x___} = 2.0/screenWidthInPixels
  206. // {_y__} = 2.0/screenHeightInPixels
  207. // {__z_} = 0.5/screenWidthInPixels
  208. // {___w} = 0.5/screenHeightInPixels
  209. float4 rcpFrameOpt
  210. ) {
  211. /*--------------------------------------------------------------------------*/
  212. half4 dir;
  213. dir.y = 0.0;
  214. half4 lumaNe = FxaaTexTop(tex, posPos.zy);
  215. lumaNe.g += half(1.0/384.0);
  216. dir.x = -lumaNe.g;
  217. dir.z = -lumaNe.g;
  218. /*--------------------------------------------------------------------------*/
  219. half4 lumaSw = FxaaTexTop(tex, posPos.xw);
  220. dir.x += lumaSw.g;
  221. dir.z += lumaSw.g;
  222. /*--------------------------------------------------------------------------*/
  223. half4 lumaNw = FxaaTexTop(tex, posPos.xy);
  224. dir.x -= lumaNw.g;
  225. dir.z += lumaNw.g;
  226. /*--------------------------------------------------------------------------*/
  227. half4 lumaSe = FxaaTexTop(tex, posPos.zw);
  228. dir.x += lumaSe.g;
  229. dir.z -= lumaSe.g;
  230. /*==========================================================================*/
  231. #if (FXAA_EARLY_EXIT == 1)
  232. half4 rgbyM = FxaaTexTop(tex, pos.xy);
  233. /*--------------------------------------------------------------------------*/
  234. half lumaMin = min(min(lumaNw.g, lumaSw.g), min(lumaNe.g, lumaSe.g));
  235. half lumaMax = max(max(lumaNw.g, lumaSw.g), max(lumaNe.g, lumaSe.g));
  236. /*--------------------------------------------------------------------------*/
  237. half lumaMinM = min(lumaMin, rgbyM.g);
  238. half lumaMaxM = max(lumaMax, rgbyM.g);
  239. /*--------------------------------------------------------------------------*/
  240. if((lumaMaxM - lumaMinM) < max(FXAA_CONSOLE__EDGE_THRESHOLD_MIN, lumaMax * FXAA_CONSOLE__EDGE_THRESHOLD))
  241. #if (FXAA_DISCARD == 1)
  242. FxaaDiscard;
  243. #else
  244. return rgbyM;
  245. #endif
  246. #endif
  247. /*==========================================================================*/
  248. half4 dir1_pos;
  249. dir1_pos.xy = normalize(dir.xyz).xz;
  250. half dirAbsMinTimesC = min(abs(dir1_pos.x), abs(dir1_pos.y)) * half(FXAA_CONSOLE__EDGE_SHARPNESS);
  251. /*--------------------------------------------------------------------------*/
  252. half4 dir2_pos;
  253. dir2_pos.xy = clamp(dir1_pos.xy / dirAbsMinTimesC, half(-2.0), half(2.0));
  254. dir1_pos.zw = pos.xy;
  255. dir2_pos.zw = pos.xy;
  256. half4 temp1N;
  257. temp1N.xy = dir1_pos.zw - dir1_pos.xy * rcpFrameOpt.zw;
  258. /*--------------------------------------------------------------------------*/
  259. temp1N = FxaaTexTop(tex, temp1N.xy);
  260. half4 rgby1;
  261. rgby1.xy = dir1_pos.zw + dir1_pos.xy * rcpFrameOpt.zw;
  262. /*--------------------------------------------------------------------------*/
  263. rgby1 = FxaaTexTop(tex, rgby1.xy);
  264. rgby1 = (temp1N + rgby1) * 0.5;
  265. /*--------------------------------------------------------------------------*/
  266. half4 temp2N;
  267. temp2N.xy = dir2_pos.zw - dir2_pos.xy * rcpFrameOpt.xy;
  268. temp2N = FxaaTexTop(tex, temp2N.xy);
  269. /*--------------------------------------------------------------------------*/
  270. half4 rgby2;
  271. rgby2.xy = dir2_pos.zw + dir2_pos.xy * rcpFrameOpt.xy;
  272. rgby2 = FxaaTexTop(tex, rgby2.xy);
  273. rgby2 = (temp2N + rgby2) * 0.5;
  274. /*--------------------------------------------------------------------------*/
  275. #if (FXAA_EARLY_EXIT == 0)
  276. half lumaMin = min(min(lumaNw.g, lumaSw.g), min(lumaNe.g, lumaSe.g));
  277. half lumaMax = max(max(lumaNw.g, lumaSw.g), max(lumaNe.g, lumaSe.g));
  278. #endif
  279. rgby2 = (rgby2 + rgby1) * 0.5;
  280. /*--------------------------------------------------------------------------*/
  281. bool twoTapLt = rgby2.g < lumaMin;
  282. bool twoTapGt = rgby2.g > lumaMax;
  283. /*--------------------------------------------------------------------------*/
  284. if(twoTapLt || twoTapGt) rgby2 = rgby1;
  285. /*--------------------------------------------------------------------------*/
  286. return rgby2; }
  287. /*==========================================================================*/
  288. #endif
  289.  
  290.  
  291.  
  292. /*============================================================================
  293.  
  294. FXAA3 QUALITY - PC
  295.  
  296. ============================================================================*/
  297. #if (FXAA_PC == 1)
  298. /*--------------------------------------------------------------------------*/
  299. float4 FxaaPixelShader(
  300. // {xy} = center of pixel
  301. float2 pos,
  302. // {xyzw} = not used on FXAA3 Quality
  303. float4 posPos,
  304. // {rgb_} = color in linear or perceptual color space
  305. // {___a} = luma in perceptual color space (not linear)
  306. FxaaTex tex,
  307. // This must be from a constant/uniform.
  308. // {x_} = 1.0/screenWidthInPixels
  309. // {_y} = 1.0/screenHeightInPixels
  310. float2 rcpFrame,
  311. // {xyzw} = not used on FXAA3 Quality
  312. float4 rcpFrameOpt
  313. ) {
  314. /*--------------------------------------------------------------------------*/
  315. #if (FXAA_GATHER4_ALPHA == 1)
  316. float4 luma4A = FxaaTexOffAlpha4(tex, pos.xy, FxaaInt2(-1, -1), rcpFrame.xy);
  317. #if (FXAA_DISCARD == 0)
  318. float4 rgbyM = FxaaTexTop(tex, pos.xy);
  319. #endif
  320. float4 luma4B = FxaaTexAlpha4(tex, pos.xy, rcpFrame.xy);
  321. float lumaNE = FxaaTexOff(tex, pos.xy, FxaaInt2(1, -1), rcpFrame.xy).g;
  322. float lumaSW = FxaaTexOff(tex, pos.xy, FxaaInt2(-1, 1), rcpFrame.xy).g;
  323. float lumaNW = luma4A.g;
  324. float lumaN = luma4A.z;
  325. float lumaW = luma4A.x;
  326. float lumaM = luma4A.y;
  327. float lumaE = luma4B.z;
  328. float lumaS = luma4B.x;
  329. float lumaSE = luma4B.y;
  330. #else
  331. float lumaN = FxaaTexOff(tex, pos.xy, FxaaInt2(0, -1), rcpFrame.xy).g;
  332. float lumaW = FxaaTexOff(tex, pos.xy, FxaaInt2(-1, 0), rcpFrame.xy).g;
  333. float4 rgbyM = FxaaTexTop(tex, pos.xy);
  334. float lumaE = FxaaTexOff(tex, pos.xy, FxaaInt2( 1, 0), rcpFrame.xy).g;
  335. float lumaS = FxaaTexOff(tex, pos.xy, FxaaInt2( 0, 1), rcpFrame.xy).g;
  336. float lumaM = rgbyM.g;
  337. #endif
  338. /*--------------------------------------------------------------------------*/
  339. float rangeMin = min(lumaM, min(min(lumaN, lumaW), min(lumaS, lumaE)));
  340. float rangeMax = max(lumaM, max(max(lumaN, lumaW), max(lumaS, lumaE)));
  341. float range = rangeMax - rangeMin;
  342. /*--------------------------------------------------------------------------*/
  343. if(range < max(FXAA_QUALITY__EDGE_THRESHOLD_MIN, rangeMax * FXAA_QUALITY__EDGE_THRESHOLD))
  344. #if (FXAA_DISCARD == 1)
  345. FxaaDiscard;
  346. #else
  347. return rgbyM;
  348. #endif
  349. /*--------------------------------------------------------------------------*/
  350. #if (FXAA_GATHER4_ALPHA == 0)
  351. float lumaNW = FxaaTexOff(tex, pos.xy, FxaaInt2(-1,-1), rcpFrame.xy).g;
  352. float lumaNE = FxaaTexOff(tex, pos.xy, FxaaInt2( 1,-1), rcpFrame.xy).g;
  353. float lumaSW = FxaaTexOff(tex, pos.xy, FxaaInt2(-1, 1), rcpFrame.xy).g;
  354. float lumaSE = FxaaTexOff(tex, pos.xy, FxaaInt2( 1, 1), rcpFrame.xy).g;
  355. #endif
  356. /*--------------------------------------------------------------------------*/
  357. #define FXAA_QUALITY__SUBPIX_TRIM_SCALE (1.0/(1.0 - FXAA_QUALITY__SUBPIX_TRIM))
  358. /*--------------------------------------------------------------------------*/
  359. float lumaL = (lumaN + lumaW + lumaE + lumaS) * 0.25;
  360. float rangeL = abs(lumaL - lumaM);
  361. float blendL = FxaaSat((rangeL / range) - FXAA_QUALITY__SUBPIX_TRIM) * FXAA_QUALITY__SUBPIX_TRIM_SCALE;
  362. blendL = min(FXAA_QUALITY__SUBPIX_CAP, blendL);
  363. /*--------------------------------------------------------------------------*/
  364. float edgeVert =
  365. abs(lumaNW + (-2.0 * lumaN) + lumaNE) +
  366. 2.0 * abs(lumaW + (-2.0 * lumaM) + lumaE ) +
  367. abs(lumaSW + (-2.0 * lumaS) + lumaSE);
  368. float edgeHorz =
  369. abs(lumaNW + (-2.0 * lumaW) + lumaSW) +
  370. 2.0 * abs(lumaN + (-2.0 * lumaM) + lumaS ) +
  371. abs(lumaNE + (-2.0 * lumaE) + lumaSE);
  372. bool horzSpan = edgeHorz >= edgeVert;
  373. /*--------------------------------------------------------------------------*/
  374. float lengthSign = horzSpan ? -rcpFrame.y : -rcpFrame.x;
  375. if(!horzSpan) lumaN = lumaW;
  376. if(!horzSpan) lumaS = lumaE;
  377. float gradientN = abs(lumaN - lumaM);
  378. float gradientS = abs(lumaS - lumaM);
  379. lumaN = (lumaN + lumaM) * 0.5;
  380. lumaS = (lumaS + lumaM) * 0.5;
  381. /*--------------------------------------------------------------------------*/
  382. bool pairN = gradientN >= gradientS;
  383. if(!pairN) lumaN = lumaS;
  384. if(!pairN) gradientN = gradientS;
  385. if(!pairN) lengthSign *= -1.0;
  386. float2 posN;
  387. posN.x = pos.x + (horzSpan ? 0.0 : lengthSign * 0.5);
  388. posN.y = pos.y + (horzSpan ? lengthSign * 0.5 : 0.0);
  389. /*--------------------------------------------------------------------------*/
  390. #define FXAA_SEARCH_STEPS 6
  391. #define FXAA_SEARCH_THRESHOLD (1.0/4.0)
  392. /*--------------------------------------------------------------------------*/
  393. gradientN *= FXAA_SEARCH_THRESHOLD;
  394. /*--------------------------------------------------------------------------*/
  395. float2 posP = posN;
  396. float2 offNP = horzSpan ?
  397. FxaaFloat2(rcpFrame.x, 0.0) :
  398. FxaaFloat2(0.0f, rcpFrame.y);
  399. float lumaEndN;
  400. float lumaEndP;
  401. bool doneN = false;
  402. bool doneP = false;
  403. posN += offNP * (-1.5);
  404. posP += offNP * ( 1.5);
  405. for(int i = 0; i < FXAA_SEARCH_STEPS; i++) {
  406. lumaEndN = FxaaTexTop(tex, posN.xy).g;
  407. lumaEndP = FxaaTexTop(tex, posP.xy).g;
  408. bool doneN2 = abs(lumaEndN - lumaN) >= gradientN;
  409. bool doneP2 = abs(lumaEndP - lumaN) >= gradientN;
  410. if(doneN2 && !doneN) posN += offNP;
  411. if(doneP2 && !doneP) posP -= offNP;
  412. if(doneN2 && doneP2) break;
  413. doneN = doneN2;
  414. doneP = doneP2;
  415. if(!doneN) posN -= offNP * 2.0;
  416. if(!doneP) posP += offNP * 2.0; }
  417. /*--------------------------------------------------------------------------*/
  418. float dstN = horzSpan ? pos.x - posN.x : pos.y - posN.y;
  419. float dstP = horzSpan ? posP.x - pos.x : posP.y - pos.y;
  420. /*--------------------------------------------------------------------------*/
  421. bool directionN = dstN < dstP;
  422. lumaEndN = directionN ? lumaEndN : lumaEndP;
  423. /*--------------------------------------------------------------------------*/
  424. if(((lumaM - lumaN) < 0.0) == ((lumaEndN - lumaN) < 0.0))
  425. lengthSign = 0.0;
  426. /*--------------------------------------------------------------------------*/
  427. float spanLength = (dstP + dstN);
  428. dstN = directionN ? dstN : dstP;
  429. float subPixelOffset = 0.5 + (dstN * (-1.0/spanLength));
  430. subPixelOffset += blendL * (1.0/8.0);
  431. subPixelOffset *= lengthSign;
  432. float3 rgbF = FxaaTexTop(tex, FxaaFloat2(
  433. pos.x + (horzSpan ? 0.0 : subPixelOffset),
  434. pos.y + (horzSpan ? subPixelOffset : 0.0))).xyz;
  435. /*--------------------------------------------------------------------------*/
  436. #if (FXAA_LINEAR == 1)
  437. lumaL *= lumaL;
  438. #endif
  439. float lumaF = dot(rgbF, float3(0.299, 0.587, 0.114)) + (1.0/(65536.0*256.0));
  440. float lumaB = FxaaLerp(lumaF, lumaL, blendL);
  441. float scale = min(4.0, lumaB/lumaF);
  442. rgbF *= scale;
  443. return float4(rgbF, lumaM); }
  444. /*==========================================================================*/
  445. #endif
  446.  
  447. uniform sampler2D tex0;
  448.  
  449. varying vec2 v_rcpFrame;
  450. varying vec4 v_rcpFrameOpt;
  451. varying vec2 v_pos;
  452. varying vec4 v_posPos;
  453.  
  454. void main() {
  455. gl_FragColor = FxaaPixelShader(v_pos, v_posPos, tex0, v_rcpFrame, v_rcpFrameOpt);
  456. }
Advertisement
Add Comment
Please, Sign In to add comment