FXAA.h 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039
  1. /*============================================================================
  2. NVIDIA FXAA 3.11 by TIMOTHY LOTTES
  3. ------------------------------------------------------------------------------
  4. COPYRIGHT (C) 2010, 2011 NVIDIA CORPORATION. ALL RIGHTS RESERVED.
  5. ------------------------------------------------------------------------------
  6. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
  7. *AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
  8. OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
  9. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL NVIDIA
  10. OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR
  11. CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR
  12. LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION,
  13. OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE
  14. THIS SOFTWARE, EVEN IF NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  15. DAMAGES.
  16. ------------------------------------------------------------------------------
  17. INTEGRATION CHECKLIST
  18. ------------------------------------------------------------------------------
  19. (1.)
  20. In the shader source, setup defines for the desired configuration.
  21. When providing multiple shaders (for different presets),
  22. simply setup the defines differently in multiple files.
  23. Example,
  24. #define FXAA_PC 1
  25. #define FXAA_HLSL_5 1
  26. #define FXAA_QUALITY__PRESET 12
  27. Or,
  28. #define FXAA_360 1
  29. Or,
  30. #define FXAA_PS3 1
  31. Etc.
  32. (2.)
  33. Then include this file,
  34. #include "Fxaa3_11.h"
  35. (3.)
  36. Then call the FXAA pixel shader from within your desired shader.
  37. Look at the FXAA Quality FxaaPixelShader() for docs on inputs.
  38. As for FXAA 3.11 all inputs for all shaders are the same
  39. to enable easy porting between platforms.
  40. return FxaaPixelShader(...);
  41. (4.)
  42. Insure pass prior to FXAA outputs RGBL (see next section).
  43. Or use,
  44. #define FXAA_GREEN_AS_LUMA 1
  45. (5.)
  46. Setup engine to provide the following constants
  47. which are used in the FxaaPixelShader() inputs,
  48. FxaaFloat2 fxaaQualityRcpFrame,
  49. FxaaFloat4 fxaaConsoleRcpFrameOpt,
  50. FxaaFloat4 fxaaConsoleRcpFrameOpt2,
  51. FxaaFloat4 fxaaConsole360RcpFrameOpt2,
  52. FxaaFloat fxaaQualitySubpix,
  53. FxaaFloat fxaaQualityEdgeThreshold,
  54. FxaaFloat fxaaQualityEdgeThresholdMin,
  55. FxaaFloat fxaaConsoleEdgeSharpness,
  56. FxaaFloat fxaaConsoleEdgeThreshold,
  57. FxaaFloat fxaaConsoleEdgeThresholdMin,
  58. FxaaFloat4 fxaaConsole360ConstDir
  59. Look at the FXAA Quality FxaaPixelShader() for docs on inputs.
  60. (6.)
  61. Have FXAA vertex shader run as a full screen triangle,
  62. and output "pos" and "fxaaConsolePosPos"
  63. such that inputs in the pixel shader provide,
  64. // {xy} = center of pixel
  65. FxaaFloat2 pos,
  66. // {xy__} = upper left of pixel
  67. // {__zw} = lower right of pixel
  68. FxaaFloat4 fxaaConsolePosPos,
  69. (7.)
  70. Insure the texture sampler(s) used by FXAA are set to bilinear filtering.
  71. ------------------------------------------------------------------------------
  72. INTEGRATION - RGBL AND COLORSPACE
  73. ------------------------------------------------------------------------------
  74. FXAA3 requires RGBL as input unless the following is set,
  75. #define FXAA_GREEN_AS_LUMA 1
  76. In which case the engine uses green in place of luma,
  77. and requires RGB input is in a non-linear colorspace.
  78. RGB should be LDR (low dynamic range).
  79. Specifically do FXAA after tonemapping.
  80. RGB data as returned by a texture fetch can be non-linear,
  81. or linear when FXAA_GREEN_AS_LUMA is not set.
  82. Note an "sRGB format" texture counts as linear,
  83. because the result of a texture fetch is linear data.
  84. Regular "RGBA8" textures in the sRGB colorspace are non-linear.
  85. If FXAA_GREEN_AS_LUMA is not set,
  86. luma must be stored in the alpha channel prior to running FXAA.
  87. This luma should be in a perceptual space (could be gamma 2.0).
  88. Example pass before FXAA where output is gamma 2.0 encoded,
  89. color.rgb = ToneMap(color.rgb); // linear color output
  90. color.rgb = sqrt(color.rgb); // gamma 2.0 color output
  91. return color;
  92. To use FXAA,
  93. color.rgb = ToneMap(color.rgb); // linear color output
  94. color.rgb = sqrt(color.rgb); // gamma 2.0 color output
  95. color.a = dot(color.rgb, FxaaFloat3(0.299, 0.587, 0.114)); // compute luma
  96. return color;
  97. Another example where output is linear encoded,
  98. say for instance writing to an sRGB formated render target,
  99. where the render target does the conversion back to sRGB after blending,
  100. color.rgb = ToneMap(color.rgb); // linear color output
  101. return color;
  102. To use FXAA,
  103. color.rgb = ToneMap(color.rgb); // linear color output
  104. color.a = sqrt(dot(color.rgb, FxaaFloat3(0.299, 0.587, 0.114))); // compute luma
  105. return color;
  106. Getting luma correct is required for the algorithm to work correctly.
  107. ------------------------------------------------------------------------------
  108. BEING LINEARLY CORRECT?
  109. ------------------------------------------------------------------------------
  110. Applying FXAA to a framebuffer with linear RGB color will look worse.
  111. This is very counter intuitive, but happends to be true in this case.
  112. The reason is because dithering artifacts will be more visiable
  113. in a linear colorspace.
  114. ------------------------------------------------------------------------------
  115. COMPLEX INTEGRATION
  116. ------------------------------------------------------------------------------
  117. Q. What if the engine is blending into RGB before wanting to run FXAA?
  118. A. In the last opaque pass prior to FXAA,
  119. have the pass write out luma into alpha.
  120. Then blend into RGB only.
  121. FXAA should be able to run ok
  122. assuming the blending pass did not any add aliasing.
  123. This should be the common case for particles and common blending passes.
  124. A. Or use FXAA_GREEN_AS_LUMA.
  125. ============================================================================*/
  126. /*============================================================================
  127. INTEGRATION KNOBS
  128. ============================================================================*/
  129. //
  130. // FXAA_PS3 and FXAA_360 choose the console algorithm (FXAA3 CONSOLE).
  131. // FXAA_360_OPT is a prototype for the new optimized 360 version.
  132. //
  133. // 1 = Use API.
  134. // 0 = Don't use API.
  135. //
  136. /*--------------------------------------------------------------------------*/
  137. #ifndef FXAA_PS3
  138. #define FXAA_PS3 0
  139. #endif
  140. /*--------------------------------------------------------------------------*/
  141. #ifndef FXAA_360
  142. #define FXAA_360 0
  143. #endif
  144. /*--------------------------------------------------------------------------*/
  145. #ifndef FXAA_360_OPT
  146. #define FXAA_360_OPT 0
  147. #endif
  148. /*==========================================================================*/
  149. #ifndef FXAA_PC
  150. //
  151. // FXAA Quality
  152. // The high quality PC algorithm.
  153. //
  154. #define FXAA_PC 0
  155. #endif
  156. /*--------------------------------------------------------------------------*/
  157. #ifndef FXAA_PC_CONSOLE
  158. //
  159. // The console algorithm for PC is included
  160. // for developers targeting really low spec machines.
  161. // Likely better to just run FXAA_PC, and use a really low preset.
  162. //
  163. #define FXAA_PC_CONSOLE 0
  164. #endif
  165. /*--------------------------------------------------------------------------*/
  166. #ifndef FXAA_GLSL_120
  167. #define FXAA_GLSL_120 0
  168. #endif
  169. /*--------------------------------------------------------------------------*/
  170. #ifndef FXAA_GLSL_130
  171. #define FXAA_GLSL_130 0
  172. #endif
  173. /*--------------------------------------------------------------------------*/
  174. #ifndef FXAA_HLSL_3
  175. #define FXAA_HLSL_3 0
  176. #endif
  177. /*--------------------------------------------------------------------------*/
  178. #ifndef FXAA_HLSL_4
  179. #define FXAA_HLSL_4 0
  180. #endif
  181. /*--------------------------------------------------------------------------*/
  182. #ifndef FXAA_HLSL_5
  183. #define FXAA_HLSL_5 0
  184. #endif
  185. /*==========================================================================*/
  186. #ifndef FXAA_GREEN_AS_LUMA
  187. //
  188. // For those using non-linear color,
  189. // and either not able to get luma in alpha, or not wanting to,
  190. // this enables FXAA to run using green as a proxy for luma.
  191. // So with this enabled, no need to pack luma in alpha.
  192. //
  193. // This will turn off AA on anything which lacks some amount of green.
  194. // Pure red and blue or combination of only R and B, will get no AA.
  195. //
  196. // Might want to lower the settings for both,
  197. // fxaaConsoleEdgeThresholdMin
  198. // fxaaQualityEdgeThresholdMin
  199. // In order to insure AA does not get turned off on colors
  200. // which contain a minor amount of green.
  201. //
  202. // 1 = On.
  203. // 0 = Off.
  204. //
  205. #define FXAA_GREEN_AS_LUMA 0
  206. #endif
  207. /*--------------------------------------------------------------------------*/
  208. #ifndef FXAA_EARLY_EXIT
  209. //
  210. // Controls algorithm's early exit path.
  211. // On PS3 turning this ON adds 2 cycles to the shader.
  212. // On 360 turning this OFF adds 10ths of a millisecond to the shader.
  213. // Turning this off on console will result in a more blurry image.
  214. // So this defaults to on.
  215. //
  216. // 1 = On.
  217. // 0 = Off.
  218. //
  219. #define FXAA_EARLY_EXIT 1
  220. #endif
  221. /*--------------------------------------------------------------------------*/
  222. #ifndef FXAA_DISCARD
  223. //
  224. // Only valid for PC OpenGL currently.
  225. // Probably will not work when FXAA_GREEN_AS_LUMA = 1.
  226. //
  227. // 1 = Use discard on pixels which don't need AA.
  228. // For APIs which enable concurrent TEX+ROP from same surface.
  229. // 0 = Return unchanged color on pixels which don't need AA.
  230. //
  231. #define FXAA_DISCARD 0
  232. #endif
  233. /*--------------------------------------------------------------------------*/
  234. #ifndef FXAA_FAST_PIXEL_OFFSET
  235. //
  236. // Used for GLSL 120 only.
  237. //
  238. // 1 = GL API supports fast pixel offsets
  239. // 0 = do not use fast pixel offsets
  240. //
  241. #ifdef GL_EXT_gpu_shader4
  242. #define FXAA_FAST_PIXEL_OFFSET 1
  243. #endif
  244. #ifdef GL_NV_gpu_shader5
  245. #define FXAA_FAST_PIXEL_OFFSET 1
  246. #endif
  247. #ifdef GL_ARB_gpu_shader5
  248. #define FXAA_FAST_PIXEL_OFFSET 1
  249. #endif
  250. #ifndef FXAA_FAST_PIXEL_OFFSET
  251. #define FXAA_FAST_PIXEL_OFFSET 0
  252. #endif
  253. #endif
  254. /*--------------------------------------------------------------------------*/
  255. #ifndef FXAA_GATHER4_ALPHA
  256. //
  257. // 1 = API supports gather4 on alpha channel.
  258. // 0 = API does not support gather4 on alpha channel.
  259. //
  260. #if (FXAA_HLSL_5 == 1)
  261. #define FXAA_GATHER4_ALPHA 1
  262. #endif
  263. #ifdef GL_ARB_gpu_shader5
  264. #define FXAA_GATHER4_ALPHA 1
  265. #endif
  266. #ifdef GL_NV_gpu_shader5
  267. #define FXAA_GATHER4_ALPHA 1
  268. #endif
  269. #ifndef FXAA_GATHER4_ALPHA
  270. #define FXAA_GATHER4_ALPHA 0
  271. #endif
  272. #endif
  273. /*============================================================================
  274. FXAA CONSOLE PS3 - TUNING KNOBS
  275. ============================================================================*/
  276. #ifndef FXAA_CONSOLE__PS3_EDGE_SHARPNESS
  277. //
  278. // Consoles the sharpness of edges on PS3 only.
  279. // Non-PS3 tuning is done with shader input.
  280. //
  281. // Due to the PS3 being ALU bound,
  282. // there are only two safe values here: 4 and 8.
  283. // These options use the shaders ability to a free *|/ by 2|4|8.
  284. //
  285. // 8.0 is sharper
  286. // 4.0 is softer
  287. // 2.0 is really soft (good for vector graphics inputs)
  288. //
  289. #if 1
  290. #define FXAA_CONSOLE__PS3_EDGE_SHARPNESS 8.0
  291. #endif
  292. #if 0
  293. #define FXAA_CONSOLE__PS3_EDGE_SHARPNESS 4.0
  294. #endif
  295. #if 0
  296. #define FXAA_CONSOLE__PS3_EDGE_SHARPNESS 2.0
  297. #endif
  298. #endif
  299. /*--------------------------------------------------------------------------*/
  300. #ifndef FXAA_CONSOLE__PS3_EDGE_THRESHOLD
  301. //
  302. // Only effects PS3.
  303. // Non-PS3 tuning is done with shader input.
  304. //
  305. // The minimum amount of local contrast required to apply algorithm.
  306. // The console setting has a different mapping than the quality setting.
  307. //
  308. // This only applies when FXAA_EARLY_EXIT is 1.
  309. //
  310. // Due to the PS3 being ALU bound,
  311. // there are only two safe values here: 0.25 and 0.125.
  312. // These options use the shaders ability to a free *|/ by 2|4|8.
  313. //
  314. // 0.125 leaves less aliasing, but is softer
  315. // 0.25 leaves more aliasing, and is sharper
  316. //
  317. #if 1
  318. #define FXAA_CONSOLE__PS3_EDGE_THRESHOLD 0.125
  319. #else
  320. #define FXAA_CONSOLE__PS3_EDGE_THRESHOLD 0.25
  321. #endif
  322. #endif
  323. /*============================================================================
  324. FXAA QUALITY - TUNING KNOBS
  325. ------------------------------------------------------------------------------
  326. NOTE the other tuning knobs are now in the shader function inputs!
  327. ============================================================================*/
  328. #ifndef FXAA_QUALITY__PRESET
  329. //
  330. // Choose the quality preset.
  331. // This needs to be compiled into the shader as it effects code.
  332. // Best option to include multiple presets is to
  333. // in each shader define the preset, then include this file.
  334. //
  335. // OPTIONS
  336. // -----------------------------------------------------------------------
  337. // 10 to 15 - default medium dither (10=fastest, 15=highest quality)
  338. // 20 to 29 - less dither, more expensive (20=fastest, 29=highest quality)
  339. // 39 - no dither, very expensive
  340. //
  341. // NOTES
  342. // -----------------------------------------------------------------------
  343. // 12 = slightly faster then FXAA 3.9 and higher edge quality (default)
  344. // 13 = about same speed as FXAA 3.9 and better than 12
  345. // 23 = closest to FXAA 3.9 visually and performance wise
  346. // _ = the lowest digit is directly related to performance
  347. // _ = the highest digit is directly related to style
  348. //
  349. #define FXAA_QUALITY__PRESET 12
  350. #endif
  351. /*============================================================================
  352. FXAA QUALITY - PRESETS
  353. ============================================================================*/
  354. /*============================================================================
  355. FXAA QUALITY - MEDIUM DITHER PRESETS
  356. ============================================================================*/
  357. #if (FXAA_QUALITY__PRESET == 10)
  358. #define FXAA_QUALITY__PS 3
  359. #define FXAA_QUALITY__P0 1.5
  360. #define FXAA_QUALITY__P1 3.0
  361. #define FXAA_QUALITY__P2 12.0
  362. #endif
  363. /*--------------------------------------------------------------------------*/
  364. #if (FXAA_QUALITY__PRESET == 11)
  365. #define FXAA_QUALITY__PS 4
  366. #define FXAA_QUALITY__P0 1.0
  367. #define FXAA_QUALITY__P1 1.5
  368. #define FXAA_QUALITY__P2 3.0
  369. #define FXAA_QUALITY__P3 12.0
  370. #endif
  371. /*--------------------------------------------------------------------------*/
  372. #if (FXAA_QUALITY__PRESET == 12)
  373. #define FXAA_QUALITY__PS 5
  374. #define FXAA_QUALITY__P0 1.0
  375. #define FXAA_QUALITY__P1 1.5
  376. #define FXAA_QUALITY__P2 2.0
  377. #define FXAA_QUALITY__P3 4.0
  378. #define FXAA_QUALITY__P4 12.0
  379. #endif
  380. /*--------------------------------------------------------------------------*/
  381. #if (FXAA_QUALITY__PRESET == 13)
  382. #define FXAA_QUALITY__PS 6
  383. #define FXAA_QUALITY__P0 1.0
  384. #define FXAA_QUALITY__P1 1.5
  385. #define FXAA_QUALITY__P2 2.0
  386. #define FXAA_QUALITY__P3 2.0
  387. #define FXAA_QUALITY__P4 4.0
  388. #define FXAA_QUALITY__P5 12.0
  389. #endif
  390. /*--------------------------------------------------------------------------*/
  391. #if (FXAA_QUALITY__PRESET == 14)
  392. #define FXAA_QUALITY__PS 7
  393. #define FXAA_QUALITY__P0 1.0
  394. #define FXAA_QUALITY__P1 1.5
  395. #define FXAA_QUALITY__P2 2.0
  396. #define FXAA_QUALITY__P3 2.0
  397. #define FXAA_QUALITY__P4 2.0
  398. #define FXAA_QUALITY__P5 4.0
  399. #define FXAA_QUALITY__P6 12.0
  400. #endif
  401. /*--------------------------------------------------------------------------*/
  402. #if (FXAA_QUALITY__PRESET == 15)
  403. #define FXAA_QUALITY__PS 8
  404. #define FXAA_QUALITY__P0 1.0
  405. #define FXAA_QUALITY__P1 1.5
  406. #define FXAA_QUALITY__P2 2.0
  407. #define FXAA_QUALITY__P3 2.0
  408. #define FXAA_QUALITY__P4 2.0
  409. #define FXAA_QUALITY__P5 2.0
  410. #define FXAA_QUALITY__P6 4.0
  411. #define FXAA_QUALITY__P7 12.0
  412. #endif
  413. /*============================================================================
  414. FXAA QUALITY - LOW DITHER PRESETS
  415. ============================================================================*/
  416. #if (FXAA_QUALITY__PRESET == 20)
  417. #define FXAA_QUALITY__PS 3
  418. #define FXAA_QUALITY__P0 1.5
  419. #define FXAA_QUALITY__P1 2.0
  420. #define FXAA_QUALITY__P2 8.0
  421. #endif
  422. /*--------------------------------------------------------------------------*/
  423. #if (FXAA_QUALITY__PRESET == 21)
  424. #define FXAA_QUALITY__PS 4
  425. #define FXAA_QUALITY__P0 1.0
  426. #define FXAA_QUALITY__P1 1.5
  427. #define FXAA_QUALITY__P2 2.0
  428. #define FXAA_QUALITY__P3 8.0
  429. #endif
  430. /*--------------------------------------------------------------------------*/
  431. #if (FXAA_QUALITY__PRESET == 22)
  432. #define FXAA_QUALITY__PS 5
  433. #define FXAA_QUALITY__P0 1.0
  434. #define FXAA_QUALITY__P1 1.5
  435. #define FXAA_QUALITY__P2 2.0
  436. #define FXAA_QUALITY__P3 2.0
  437. #define FXAA_QUALITY__P4 8.0
  438. #endif
  439. /*--------------------------------------------------------------------------*/
  440. #if (FXAA_QUALITY__PRESET == 23)
  441. #define FXAA_QUALITY__PS 6
  442. #define FXAA_QUALITY__P0 1.0
  443. #define FXAA_QUALITY__P1 1.5
  444. #define FXAA_QUALITY__P2 2.0
  445. #define FXAA_QUALITY__P3 2.0
  446. #define FXAA_QUALITY__P4 2.0
  447. #define FXAA_QUALITY__P5 8.0
  448. #endif
  449. /*--------------------------------------------------------------------------*/
  450. #if (FXAA_QUALITY__PRESET == 24)
  451. #define FXAA_QUALITY__PS 7
  452. #define FXAA_QUALITY__P0 1.0
  453. #define FXAA_QUALITY__P1 1.5
  454. #define FXAA_QUALITY__P2 2.0
  455. #define FXAA_QUALITY__P3 2.0
  456. #define FXAA_QUALITY__P4 2.0
  457. #define FXAA_QUALITY__P5 3.0
  458. #define FXAA_QUALITY__P6 8.0
  459. #endif
  460. /*--------------------------------------------------------------------------*/
  461. #if (FXAA_QUALITY__PRESET == 25)
  462. #define FXAA_QUALITY__PS 8
  463. #define FXAA_QUALITY__P0 1.0
  464. #define FXAA_QUALITY__P1 1.5
  465. #define FXAA_QUALITY__P2 2.0
  466. #define FXAA_QUALITY__P3 2.0
  467. #define FXAA_QUALITY__P4 2.0
  468. #define FXAA_QUALITY__P5 2.0
  469. #define FXAA_QUALITY__P6 4.0
  470. #define FXAA_QUALITY__P7 8.0
  471. #endif
  472. /*--------------------------------------------------------------------------*/
  473. #if (FXAA_QUALITY__PRESET == 26)
  474. #define FXAA_QUALITY__PS 9
  475. #define FXAA_QUALITY__P0 1.0
  476. #define FXAA_QUALITY__P1 1.5
  477. #define FXAA_QUALITY__P2 2.0
  478. #define FXAA_QUALITY__P3 2.0
  479. #define FXAA_QUALITY__P4 2.0
  480. #define FXAA_QUALITY__P5 2.0
  481. #define FXAA_QUALITY__P6 2.0
  482. #define FXAA_QUALITY__P7 4.0
  483. #define FXAA_QUALITY__P8 8.0
  484. #endif
  485. /*--------------------------------------------------------------------------*/
  486. #if (FXAA_QUALITY__PRESET == 27)
  487. #define FXAA_QUALITY__PS 10
  488. #define FXAA_QUALITY__P0 1.0
  489. #define FXAA_QUALITY__P1 1.5
  490. #define FXAA_QUALITY__P2 2.0
  491. #define FXAA_QUALITY__P3 2.0
  492. #define FXAA_QUALITY__P4 2.0
  493. #define FXAA_QUALITY__P5 2.0
  494. #define FXAA_QUALITY__P6 2.0
  495. #define FXAA_QUALITY__P7 2.0
  496. #define FXAA_QUALITY__P8 4.0
  497. #define FXAA_QUALITY__P9 8.0
  498. #endif
  499. /*--------------------------------------------------------------------------*/
  500. #if (FXAA_QUALITY__PRESET == 28)
  501. #define FXAA_QUALITY__PS 11
  502. #define FXAA_QUALITY__P0 1.0
  503. #define FXAA_QUALITY__P1 1.5
  504. #define FXAA_QUALITY__P2 2.0
  505. #define FXAA_QUALITY__P3 2.0
  506. #define FXAA_QUALITY__P4 2.0
  507. #define FXAA_QUALITY__P5 2.0
  508. #define FXAA_QUALITY__P6 2.0
  509. #define FXAA_QUALITY__P7 2.0
  510. #define FXAA_QUALITY__P8 2.0
  511. #define FXAA_QUALITY__P9 4.0
  512. #define FXAA_QUALITY__P10 8.0
  513. #endif
  514. /*--------------------------------------------------------------------------*/
  515. #if (FXAA_QUALITY__PRESET == 29)
  516. #define FXAA_QUALITY__PS 12
  517. #define FXAA_QUALITY__P0 1.0
  518. #define FXAA_QUALITY__P1 1.5
  519. #define FXAA_QUALITY__P2 2.0
  520. #define FXAA_QUALITY__P3 2.0
  521. #define FXAA_QUALITY__P4 2.0
  522. #define FXAA_QUALITY__P5 2.0
  523. #define FXAA_QUALITY__P6 2.0
  524. #define FXAA_QUALITY__P7 2.0
  525. #define FXAA_QUALITY__P8 2.0
  526. #define FXAA_QUALITY__P9 2.0
  527. #define FXAA_QUALITY__P10 4.0
  528. #define FXAA_QUALITY__P11 8.0
  529. #endif
  530. /*============================================================================
  531. FXAA QUALITY - EXTREME QUALITY
  532. ============================================================================*/
  533. #if (FXAA_QUALITY__PRESET == 39)
  534. #define FXAA_QUALITY__PS 12
  535. #define FXAA_QUALITY__P0 1.0
  536. #define FXAA_QUALITY__P1 1.0
  537. #define FXAA_QUALITY__P2 1.0
  538. #define FXAA_QUALITY__P3 1.0
  539. #define FXAA_QUALITY__P4 1.0
  540. #define FXAA_QUALITY__P5 1.5
  541. #define FXAA_QUALITY__P6 2.0
  542. #define FXAA_QUALITY__P7 2.0
  543. #define FXAA_QUALITY__P8 2.0
  544. #define FXAA_QUALITY__P9 2.0
  545. #define FXAA_QUALITY__P10 4.0
  546. #define FXAA_QUALITY__P11 8.0
  547. #endif
  548. /*============================================================================
  549. API PORTING
  550. ============================================================================*/
  551. #if (FXAA_GLSL_120 == 1) || (FXAA_GLSL_130 == 1)
  552. #define FxaaBool bool
  553. #define FxaaDiscard discard
  554. #define FxaaFloat float
  555. #define FxaaFloat2 vec2
  556. #define FxaaFloat3 vec3
  557. #define FxaaFloat4 vec4
  558. #define FxaaHalf float
  559. #define FxaaHalf2 vec2
  560. #define FxaaHalf3 vec3
  561. #define FxaaHalf4 vec4
  562. #define FxaaInt2 ivec2
  563. #define FxaaSat(x) clamp(x, 0.0, 1.0)
  564. #define FxaaTex sampler2D
  565. #else
  566. #define FxaaBool bool
  567. #define FxaaDiscard clip(-1)
  568. #define FxaaFloat float
  569. #define FxaaFloat2 float2
  570. #define FxaaFloat3 float3
  571. #define FxaaFloat4 float4
  572. #define FxaaHalf half
  573. #define FxaaHalf2 half2
  574. #define FxaaHalf3 half3
  575. #define FxaaHalf4 half4
  576. #define FxaaSat(x) saturate(x)
  577. #endif
  578. /*--------------------------------------------------------------------------*/
  579. #if (FXAA_GLSL_120 == 1)
  580. // Requires,
  581. // #version 120
  582. // And at least,
  583. // #extension GL_EXT_gpu_shader4 : enable
  584. // (or set FXAA_FAST_PIXEL_OFFSET 1 to work like DX9)
  585. #define FxaaTexTop(t, p) texture2DLod(t, p, 0.0)
  586. #if (FXAA_FAST_PIXEL_OFFSET == 1)
  587. #define FxaaTexOff(t, p, o, r) texture2DLodOffset(t, p, 0.0, o)
  588. #else
  589. #define FxaaTexOff(t, p, o, r) texture2DLod(t, p + (o * r), 0.0)
  590. #endif
  591. #if (FXAA_GATHER4_ALPHA == 1)
  592. // use #extension GL_ARB_gpu_shader5 : enable
  593. #define FxaaTexAlpha4(t, p) textureGather(t, p, 3)
  594. #define FxaaTexOffAlpha4(t, p, o) textureGatherOffset(t, p, o, 3)
  595. #define FxaaTexGreen4(t, p) textureGather(t, p, 1)
  596. #define FxaaTexOffGreen4(t, p, o) textureGatherOffset(t, p, o, 1)
  597. #endif
  598. #endif
  599. /*--------------------------------------------------------------------------*/
  600. #if (FXAA_GLSL_130 == 1)
  601. // Requires "#version 130" or better
  602. #define FxaaTexTop(t, p) textureLod(t, p, 0.0)
  603. #define FxaaTexOff(t, p, o, r) textureLodOffset(t, p, 0.0, o)
  604. #if (FXAA_GATHER4_ALPHA == 1)
  605. // use #extension GL_ARB_gpu_shader5 : enable
  606. #define FxaaTexAlpha4(t, p) textureGather(t, p, 3)
  607. #define FxaaTexOffAlpha4(t, p, o) textureGatherOffset(t, p, o, 3)
  608. #define FxaaTexGreen4(t, p) textureGather(t, p, 1)
  609. #define FxaaTexOffGreen4(t, p, o) textureGatherOffset(t, p, o, 1)
  610. #endif
  611. #endif
  612. /*--------------------------------------------------------------------------*/
  613. #if (FXAA_HLSL_3 == 1) || (FXAA_360 == 1) || (FXAA_PS3 == 1)
  614. #define FxaaInt2 float2
  615. #define FxaaTex sampler2D
  616. #define FxaaTexTop(t, p) tex2Dlod(t, float4(p, 0.0, 0.0))
  617. #define FxaaTexOff(t, p, o, r) tex2Dlod(t, float4(p + (o * r), 0, 0))
  618. #endif
  619. /*--------------------------------------------------------------------------*/
  620. #if (FXAA_HLSL_4 == 1)
  621. #define FxaaInt2 int2
  622. struct FxaaTex { SamplerState smpl; Texture2D tex; };
  623. #define FxaaTexTop(t, p) t.tex.SampleLevel(t.smpl, p, 0.0)
  624. #define FxaaTexOff(t, p, o, r) t.tex.SampleLevel(t.smpl, p, 0.0, o)
  625. #endif
  626. /*--------------------------------------------------------------------------*/
  627. #if (FXAA_HLSL_5 == 1)
  628. #define FxaaInt2 int2
  629. struct FxaaTex { SamplerState smpl; Texture2D tex; };
  630. #define FxaaTexTop(t, p) t.tex.SampleLevel(t.smpl, p, 0.0)
  631. #define FxaaTexOff(t, p, o, r) t.tex.SampleLevel(t.smpl, p, 0.0, o)
  632. #define FxaaTexAlpha4(t, p) t.tex.GatherAlpha(t.smpl, p)
  633. #define FxaaTexOffAlpha4(t, p, o) t.tex.GatherAlpha(t.smpl, p, o)
  634. #define FxaaTexGreen4(t, p) t.tex.GatherGreen(t.smpl, p)
  635. #define FxaaTexOffGreen4(t, p, o) t.tex.GatherGreen(t.smpl, p, o)
  636. #endif
  637. /*============================================================================
  638. GREEN AS LUMA OPTION SUPPORT FUNCTION
  639. ============================================================================*/
  640. #if (FXAA_GREEN_AS_LUMA == 0)
  641. FxaaFloat FxaaLuma(FxaaFloat4 rgba) { return rgba.w; }
  642. #else
  643. FxaaFloat FxaaLuma(FxaaFloat4 rgba) { return Dot(rgba.rgb, Vec(0.299f, 0.587f, 0.114f));} // ESENTHEL
  644. #endif
  645. /*============================================================================
  646. FXAA3 QUALITY - PC
  647. ============================================================================*/
  648. #if (FXAA_PC == 1)
  649. /*--------------------------------------------------------------------------*/
  650. FxaaFloat4 FxaaPixelShader(
  651. //
  652. // Use noperspective interpolation here (turn off perspective interpolation).
  653. // {xy} = center of pixel
  654. FxaaFloat2 pos,
  655. //
  656. // Used only for FXAA Console, and not used on the 360 version.
  657. // Use noperspective interpolation here (turn off perspective interpolation).
  658. // {xy__} = upper left of pixel
  659. // {__zw} = lower right of pixel
  660. FxaaFloat4 fxaaConsolePosPos,
  661. //
  662. // Input color texture.
  663. // {rgb_} = color in linear or perceptual color space
  664. // if (FXAA_GREEN_AS_LUMA == 0)
  665. // {___a} = luma in perceptual color space (not linear)
  666. FxaaTex tex,
  667. //
  668. // Only used on the optimized 360 version of FXAA Console.
  669. // For everything but 360, just use the same input here as for "tex".
  670. // For 360, same texture, just alias with a 2nd sampler.
  671. // This sampler needs to have an exponent bias of -1.
  672. FxaaTex fxaaConsole360TexExpBiasNegOne,
  673. //
  674. // Only used on the optimized 360 version of FXAA Console.
  675. // For everything but 360, just use the same input here as for "tex".
  676. // For 360, same texture, just alias with a 3nd sampler.
  677. // This sampler needs to have an exponent bias of -2.
  678. FxaaTex fxaaConsole360TexExpBiasNegTwo,
  679. //
  680. // Only used on FXAA Quality.
  681. // This must be from a constant/uniform.
  682. // {x_} = 1.0/screenWidthInPixels
  683. // {_y} = 1.0/screenHeightInPixels
  684. FxaaFloat2 fxaaQualityRcpFrame,
  685. //
  686. // Only used on FXAA Console.
  687. // This must be from a constant/uniform.
  688. // This effects sub-pixel AA quality and inversely sharpness.
  689. // Where N ranges between,
  690. // N = 0.50 (default)
  691. // N = 0.33 (sharper)
  692. // {x___} = -N/screenWidthInPixels
  693. // {_y__} = -N/screenHeightInPixels
  694. // {__z_} = N/screenWidthInPixels
  695. // {___w} = N/screenHeightInPixels
  696. FxaaFloat4 fxaaConsoleRcpFrameOpt,
  697. //
  698. // Only used on FXAA Console.
  699. // Not used on 360, but used on PS3 and PC.
  700. // This must be from a constant/uniform.
  701. // {x___} = -2.0/screenWidthInPixels
  702. // {_y__} = -2.0/screenHeightInPixels
  703. // {__z_} = 2.0/screenWidthInPixels
  704. // {___w} = 2.0/screenHeightInPixels
  705. FxaaFloat4 fxaaConsoleRcpFrameOpt2,
  706. //
  707. // Only used on FXAA Console.
  708. // Only used on 360 in place of fxaaConsoleRcpFrameOpt2.
  709. // This must be from a constant/uniform.
  710. // {x___} = 8.0/screenWidthInPixels
  711. // {_y__} = 8.0/screenHeightInPixels
  712. // {__z_} = -4.0/screenWidthInPixels
  713. // {___w} = -4.0/screenHeightInPixels
  714. FxaaFloat4 fxaaConsole360RcpFrameOpt2,
  715. //
  716. // Only used on FXAA Quality.
  717. // This used to be the FXAA_QUALITY__SUBPIX define.
  718. // It is here now to allow easier tuning.
  719. // Choose the amount of sub-pixel aliasing removal.
  720. // This can effect sharpness.
  721. // 1.00 - upper limit (softer)
  722. // 0.75 - default amount of filtering
  723. // 0.50 - lower limit (sharper, less sub-pixel aliasing removal)
  724. // 0.25 - almost off
  725. // 0.00 - completely off
  726. FxaaFloat fxaaQualitySubpix,
  727. //
  728. // Only used on FXAA Quality.
  729. // This used to be the FXAA_QUALITY__EDGE_THRESHOLD define.
  730. // It is here now to allow easier tuning.
  731. // The minimum amount of local contrast required to apply algorithm.
  732. // 0.333 - too little (faster)
  733. // 0.250 - low quality
  734. // 0.166 - default
  735. // 0.125 - high quality
  736. // 0.063 - overkill (slower)
  737. FxaaFloat fxaaQualityEdgeThreshold,
  738. //
  739. // Only used on FXAA Quality.
  740. // This used to be the FXAA_QUALITY__EDGE_THRESHOLD_MIN define.
  741. // It is here now to allow easier tuning.
  742. // Trims the algorithm from processing darks.
  743. // 0.0833 - upper limit (default, the start of visible unfiltered edges)
  744. // 0.0625 - high quality (faster)
  745. // 0.0312 - visible limit (slower)
  746. // Special notes when using FXAA_GREEN_AS_LUMA,
  747. // Likely want to set this to zero.
  748. // As colors that are mostly not-green
  749. // will appear very dark in the green channel!
  750. // Tune by looking at mostly non-green content,
  751. // then start at zero and increase until aliasing is a problem.
  752. FxaaFloat fxaaQualityEdgeThresholdMin,
  753. //
  754. // Only used on FXAA Console.
  755. // This used to be the FXAA_CONSOLE__EDGE_SHARPNESS define.
  756. // It is here now to allow easier tuning.
  757. // This does not effect PS3, as this needs to be compiled in.
  758. // Use FXAA_CONSOLE__PS3_EDGE_SHARPNESS for PS3.
  759. // Due to the PS3 being ALU bound,
  760. // there are only three safe values here: 2 and 4 and 8.
  761. // These options use the shaders ability to a free *|/ by 2|4|8.
  762. // For all other platforms can be a non-power of two.
  763. // 8.0 is sharper (default!!!)
  764. // 4.0 is softer
  765. // 2.0 is really soft (good only for vector graphics inputs)
  766. FxaaFloat fxaaConsoleEdgeSharpness,
  767. //
  768. // Only used on FXAA Console.
  769. // This used to be the FXAA_CONSOLE__EDGE_THRESHOLD define.
  770. // It is here now to allow easier tuning.
  771. // This does not effect PS3, as this needs to be compiled in.
  772. // Use FXAA_CONSOLE__PS3_EDGE_THRESHOLD for PS3.
  773. // Due to the PS3 being ALU bound,
  774. // there are only two safe values here: 1/4 and 1/8.
  775. // These options use the shaders ability to a free *|/ by 2|4|8.
  776. // The console setting has a different mapping than the quality setting.
  777. // Other platforms can use other values.
  778. // 0.125 leaves less aliasing, but is softer (default!!!)
  779. // 0.25 leaves more aliasing, and is sharper
  780. FxaaFloat fxaaConsoleEdgeThreshold,
  781. //
  782. // Only used on FXAA Console.
  783. // This used to be the FXAA_CONSOLE__EDGE_THRESHOLD_MIN define.
  784. // It is here now to allow easier tuning.
  785. // Trims the algorithm from processing darks.
  786. // The console setting has a different mapping than the quality setting.
  787. // This only applies when FXAA_EARLY_EXIT is 1.
  788. // This does not apply to PS3,
  789. // PS3 was simplified to avoid more shader instructions.
  790. // 0.06 - faster but more aliasing in darks
  791. // 0.05 - default
  792. // 0.04 - slower and less aliasing in darks
  793. // Special notes when using FXAA_GREEN_AS_LUMA,
  794. // Likely want to set this to zero.
  795. // As colors that are mostly not-green
  796. // will appear very dark in the green channel!
  797. // Tune by looking at mostly non-green content,
  798. // then start at zero and increase until aliasing is a problem.
  799. FxaaFloat fxaaConsoleEdgeThresholdMin,
  800. //
  801. // Extra constants for 360 FXAA Console only.
  802. // Use zeros or anything else for other platforms.
  803. // These must be in physical constant registers and NOT immedates.
  804. // Immedates will result in compiler un-optimizing.
  805. // {xyzw} = float4(1.0, -1.0, 0.25, -0.25)
  806. FxaaFloat4 fxaaConsole360ConstDir
  807. ) {
  808. /*--------------------------------------------------------------------------*/
  809. FxaaFloat2 posM;
  810. posM.x = pos.x;
  811. posM.y = pos.y;
  812. #if (FXAA_GATHER4_ALPHA == 1)
  813. #if (FXAA_DISCARD == 0)
  814. FxaaFloat4 rgbyM = FxaaTexTop(tex, posM);
  815. FxaaFloat lumaM = FxaaLuma(rgbyM); // ESENTHEL
  816. #endif
  817. #if (FXAA_GREEN_AS_LUMA == 0)
  818. FxaaFloat4 luma4A = FxaaTexAlpha4(tex, posM);
  819. FxaaFloat4 luma4B = FxaaTexOffAlpha4(tex, posM, FxaaInt2(-1, -1));
  820. #else
  821. FxaaFloat4 luma4A = FxaaTexGreen4(tex, posM);
  822. FxaaFloat4 luma4B = FxaaTexOffGreen4(tex, posM, FxaaInt2(-1, -1));
  823. #endif
  824. #if (FXAA_DISCARD == 1)
  825. #define lumaM luma4A.w
  826. #endif
  827. #define lumaE luma4A.z
  828. #define lumaS luma4A.x
  829. #define lumaSE luma4A.y
  830. #define lumaNW luma4B.w
  831. #define lumaN luma4B.z
  832. #define lumaW luma4B.x
  833. #else
  834. FxaaFloat4 rgbyM = FxaaTexTop(tex, posM);
  835. FxaaFloat lumaM = FxaaLuma(rgbyM); // ESENTHEL
  836. FxaaFloat lumaS = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 0, 1), fxaaQualityRcpFrame.xy));
  837. FxaaFloat lumaE = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 1, 0), fxaaQualityRcpFrame.xy));
  838. FxaaFloat lumaN = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 0,-1), fxaaQualityRcpFrame.xy));
  839. FxaaFloat lumaW = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2(-1, 0), fxaaQualityRcpFrame.xy));
  840. #endif
  841. /*--------------------------------------------------------------------------*/
  842. FxaaFloat maxSM = max(lumaS, lumaM);
  843. FxaaFloat minSM = min(lumaS, lumaM);
  844. FxaaFloat maxESM = max(lumaE, maxSM);
  845. FxaaFloat minESM = min(lumaE, minSM);
  846. FxaaFloat maxWN = max(lumaN, lumaW);
  847. FxaaFloat minWN = min(lumaN, lumaW);
  848. FxaaFloat rangeMax = max(maxWN, maxESM);
  849. FxaaFloat rangeMin = min(minWN, minESM);
  850. FxaaFloat rangeMaxScaled = rangeMax * fxaaQualityEdgeThreshold;
  851. FxaaFloat range = rangeMax - rangeMin;
  852. FxaaFloat rangeMaxClamped = max(fxaaQualityEdgeThresholdMin, rangeMaxScaled);
  853. FxaaBool earlyExit = range < rangeMaxClamped;
  854. /*--------------------------------------------------------------------------*/
  855. if(earlyExit)
  856. #if (FXAA_DISCARD == 1)
  857. FxaaDiscard;
  858. #else
  859. return rgbyM;
  860. #endif
  861. /*--------------------------------------------------------------------------*/
  862. #if (FXAA_GATHER4_ALPHA == 0)
  863. FxaaFloat lumaNW = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2(-1,-1), fxaaQualityRcpFrame.xy));
  864. FxaaFloat lumaSE = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 1, 1), fxaaQualityRcpFrame.xy));
  865. FxaaFloat lumaNE = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 1,-1), fxaaQualityRcpFrame.xy));
  866. FxaaFloat lumaSW = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2(-1, 1), fxaaQualityRcpFrame.xy));
  867. #else
  868. FxaaFloat lumaNE = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2(1, -1), fxaaQualityRcpFrame.xy));
  869. FxaaFloat lumaSW = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2(-1, 1), fxaaQualityRcpFrame.xy));
  870. #endif
  871. /*--------------------------------------------------------------------------*/
  872. FxaaFloat lumaNS = lumaN + lumaS;
  873. FxaaFloat lumaWE = lumaW + lumaE;
  874. FxaaFloat subpixRcpRange = 1.0/range;
  875. FxaaFloat subpixNSWE = lumaNS + lumaWE;
  876. FxaaFloat edgeHorz1 = (-2.0 * lumaM) + lumaNS;
  877. FxaaFloat edgeVert1 = (-2.0 * lumaM) + lumaWE;
  878. /*--------------------------------------------------------------------------*/
  879. FxaaFloat lumaNESE = lumaNE + lumaSE;
  880. FxaaFloat lumaNWNE = lumaNW + lumaNE;
  881. FxaaFloat edgeHorz2 = (-2.0 * lumaE) + lumaNESE;
  882. FxaaFloat edgeVert2 = (-2.0 * lumaN) + lumaNWNE;
  883. /*--------------------------------------------------------------------------*/
  884. FxaaFloat lumaNWSW = lumaNW + lumaSW;
  885. FxaaFloat lumaSWSE = lumaSW + lumaSE;
  886. FxaaFloat edgeHorz4 = (abs(edgeHorz1) * 2.0) + abs(edgeHorz2);
  887. FxaaFloat edgeVert4 = (abs(edgeVert1) * 2.0) + abs(edgeVert2);
  888. FxaaFloat edgeHorz3 = (-2.0 * lumaW) + lumaNWSW;
  889. FxaaFloat edgeVert3 = (-2.0 * lumaS) + lumaSWSE;
  890. FxaaFloat edgeHorz = abs(edgeHorz3) + edgeHorz4;
  891. FxaaFloat edgeVert = abs(edgeVert3) + edgeVert4;
  892. /*--------------------------------------------------------------------------*/
  893. FxaaFloat subpixNWSWNESE = lumaNWSW + lumaNESE;
  894. FxaaFloat lengthSign = fxaaQualityRcpFrame.x;
  895. FxaaBool horzSpan = edgeHorz >= edgeVert;
  896. FxaaFloat subpixA = subpixNSWE * 2.0 + subpixNWSWNESE;
  897. /*--------------------------------------------------------------------------*/
  898. if(!horzSpan) lumaN = lumaW;
  899. if(!horzSpan) lumaS = lumaE;
  900. if(horzSpan) lengthSign = fxaaQualityRcpFrame.y;
  901. FxaaFloat subpixB = (subpixA * (1.0/12.0)) - lumaM;
  902. /*--------------------------------------------------------------------------*/
  903. FxaaFloat gradientN = lumaN - lumaM;
  904. FxaaFloat gradientS = lumaS - lumaM;
  905. FxaaFloat lumaNN = lumaN + lumaM;
  906. FxaaFloat lumaSS = lumaS + lumaM;
  907. FxaaBool pairN = abs(gradientN) >= abs(gradientS);
  908. FxaaFloat gradient = max(abs(gradientN), abs(gradientS));
  909. if(pairN) lengthSign = -lengthSign;
  910. FxaaFloat subpixC = FxaaSat(abs(subpixB) * subpixRcpRange);
  911. /*--------------------------------------------------------------------------*/
  912. FxaaFloat2 posB;
  913. posB.x = posM.x;
  914. posB.y = posM.y;
  915. FxaaFloat2 offNP;
  916. offNP.x = (!horzSpan) ? 0.0 : fxaaQualityRcpFrame.x;
  917. offNP.y = ( horzSpan) ? 0.0 : fxaaQualityRcpFrame.y;
  918. if(!horzSpan) posB.x += lengthSign * 0.5;
  919. if( horzSpan) posB.y += lengthSign * 0.5;
  920. /*--------------------------------------------------------------------------*/
  921. FxaaFloat2 posN;
  922. posN.x = posB.x - offNP.x * FXAA_QUALITY__P0;
  923. posN.y = posB.y - offNP.y * FXAA_QUALITY__P0;
  924. FxaaFloat2 posP;
  925. posP.x = posB.x + offNP.x * FXAA_QUALITY__P0;
  926. posP.y = posB.y + offNP.y * FXAA_QUALITY__P0;
  927. FxaaFloat subpixD = ((-2.0)*subpixC) + 3.0;
  928. FxaaFloat lumaEndN = FxaaLuma(FxaaTexTop(tex, posN));
  929. FxaaFloat subpixE = subpixC * subpixC;
  930. FxaaFloat lumaEndP = FxaaLuma(FxaaTexTop(tex, posP));
  931. /*--------------------------------------------------------------------------*/
  932. if(!pairN) lumaNN = lumaSS;
  933. FxaaFloat gradientScaled = gradient * 1.0/4.0;
  934. FxaaFloat lumaMM = lumaM - lumaNN * 0.5;
  935. FxaaFloat subpixF = subpixD * subpixE;
  936. FxaaBool lumaMLTZero = lumaMM < 0.0;
  937. /*--------------------------------------------------------------------------*/
  938. lumaEndN -= lumaNN * 0.5;
  939. lumaEndP -= lumaNN * 0.5;
  940. FxaaBool doneN = abs(lumaEndN) >= gradientScaled;
  941. FxaaBool doneP = abs(lumaEndP) >= gradientScaled;
  942. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P1;
  943. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P1;
  944. FxaaBool doneNP = (!doneN) || (!doneP);
  945. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P1;
  946. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P1;
  947. /*--------------------------------------------------------------------------*/
  948. if(doneNP) {
  949. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  950. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  951. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  952. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  953. doneN = abs(lumaEndN) >= gradientScaled;
  954. doneP = abs(lumaEndP) >= gradientScaled;
  955. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P2;
  956. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P2;
  957. doneNP = (!doneN) || (!doneP);
  958. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P2;
  959. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P2;
  960. /*--------------------------------------------------------------------------*/
  961. #if (FXAA_QUALITY__PS > 3)
  962. if(doneNP) {
  963. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  964. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  965. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  966. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  967. doneN = abs(lumaEndN) >= gradientScaled;
  968. doneP = abs(lumaEndP) >= gradientScaled;
  969. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P3;
  970. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P3;
  971. doneNP = (!doneN) || (!doneP);
  972. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P3;
  973. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P3;
  974. /*--------------------------------------------------------------------------*/
  975. #if (FXAA_QUALITY__PS > 4)
  976. if(doneNP) {
  977. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  978. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  979. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  980. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  981. doneN = abs(lumaEndN) >= gradientScaled;
  982. doneP = abs(lumaEndP) >= gradientScaled;
  983. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P4;
  984. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P4;
  985. doneNP = (!doneN) || (!doneP);
  986. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P4;
  987. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P4;
  988. /*--------------------------------------------------------------------------*/
  989. #if (FXAA_QUALITY__PS > 5)
  990. if(doneNP) {
  991. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  992. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  993. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  994. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  995. doneN = abs(lumaEndN) >= gradientScaled;
  996. doneP = abs(lumaEndP) >= gradientScaled;
  997. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P5;
  998. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P5;
  999. doneNP = (!doneN) || (!doneP);
  1000. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P5;
  1001. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P5;
  1002. /*--------------------------------------------------------------------------*/
  1003. #if (FXAA_QUALITY__PS > 6)
  1004. if(doneNP) {
  1005. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  1006. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  1007. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  1008. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  1009. doneN = abs(lumaEndN) >= gradientScaled;
  1010. doneP = abs(lumaEndP) >= gradientScaled;
  1011. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P6;
  1012. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P6;
  1013. doneNP = (!doneN) || (!doneP);
  1014. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P6;
  1015. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P6;
  1016. /*--------------------------------------------------------------------------*/
  1017. #if (FXAA_QUALITY__PS > 7)
  1018. if(doneNP) {
  1019. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  1020. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  1021. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  1022. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  1023. doneN = abs(lumaEndN) >= gradientScaled;
  1024. doneP = abs(lumaEndP) >= gradientScaled;
  1025. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P7;
  1026. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P7;
  1027. doneNP = (!doneN) || (!doneP);
  1028. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P7;
  1029. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P7;
  1030. /*--------------------------------------------------------------------------*/
  1031. #if (FXAA_QUALITY__PS > 8)
  1032. if(doneNP) {
  1033. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  1034. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  1035. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  1036. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  1037. doneN = abs(lumaEndN) >= gradientScaled;
  1038. doneP = abs(lumaEndP) >= gradientScaled;
  1039. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P8;
  1040. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P8;
  1041. doneNP = (!doneN) || (!doneP);
  1042. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P8;
  1043. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P8;
  1044. /*--------------------------------------------------------------------------*/
  1045. #if (FXAA_QUALITY__PS > 9)
  1046. if(doneNP) {
  1047. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  1048. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  1049. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  1050. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  1051. doneN = abs(lumaEndN) >= gradientScaled;
  1052. doneP = abs(lumaEndP) >= gradientScaled;
  1053. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P9;
  1054. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P9;
  1055. doneNP = (!doneN) || (!doneP);
  1056. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P9;
  1057. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P9;
  1058. /*--------------------------------------------------------------------------*/
  1059. #if (FXAA_QUALITY__PS > 10)
  1060. if(doneNP) {
  1061. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  1062. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  1063. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  1064. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  1065. doneN = abs(lumaEndN) >= gradientScaled;
  1066. doneP = abs(lumaEndP) >= gradientScaled;
  1067. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P10;
  1068. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P10;
  1069. doneNP = (!doneN) || (!doneP);
  1070. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P10;
  1071. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P10;
  1072. /*--------------------------------------------------------------------------*/
  1073. #if (FXAA_QUALITY__PS > 11)
  1074. if(doneNP) {
  1075. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  1076. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  1077. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  1078. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  1079. doneN = abs(lumaEndN) >= gradientScaled;
  1080. doneP = abs(lumaEndP) >= gradientScaled;
  1081. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P11;
  1082. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P11;
  1083. doneNP = (!doneN) || (!doneP);
  1084. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P11;
  1085. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P11;
  1086. /*--------------------------------------------------------------------------*/
  1087. #if (FXAA_QUALITY__PS > 12)
  1088. if(doneNP) {
  1089. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  1090. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  1091. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  1092. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  1093. doneN = abs(lumaEndN) >= gradientScaled;
  1094. doneP = abs(lumaEndP) >= gradientScaled;
  1095. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P12;
  1096. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P12;
  1097. doneNP = (!doneN) || (!doneP);
  1098. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P12;
  1099. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P12;
  1100. /*--------------------------------------------------------------------------*/
  1101. }
  1102. #endif
  1103. /*--------------------------------------------------------------------------*/
  1104. }
  1105. #endif
  1106. /*--------------------------------------------------------------------------*/
  1107. }
  1108. #endif
  1109. /*--------------------------------------------------------------------------*/
  1110. }
  1111. #endif
  1112. /*--------------------------------------------------------------------------*/
  1113. }
  1114. #endif
  1115. /*--------------------------------------------------------------------------*/
  1116. }
  1117. #endif
  1118. /*--------------------------------------------------------------------------*/
  1119. }
  1120. #endif
  1121. /*--------------------------------------------------------------------------*/
  1122. }
  1123. #endif
  1124. /*--------------------------------------------------------------------------*/
  1125. }
  1126. #endif
  1127. /*--------------------------------------------------------------------------*/
  1128. }
  1129. #endif
  1130. /*--------------------------------------------------------------------------*/
  1131. }
  1132. /*--------------------------------------------------------------------------*/
  1133. FxaaFloat dstN = posM.x - posN.x;
  1134. FxaaFloat dstP = posP.x - posM.x;
  1135. if(!horzSpan) dstN = posM.y - posN.y;
  1136. if(!horzSpan) dstP = posP.y - posM.y;
  1137. /*--------------------------------------------------------------------------*/
  1138. FxaaBool goodSpanN = (lumaEndN < 0.0) != lumaMLTZero;
  1139. FxaaFloat spanLength = (dstP + dstN);
  1140. FxaaBool goodSpanP = (lumaEndP < 0.0) != lumaMLTZero;
  1141. FxaaFloat spanLengthRcp = 1.0/spanLength;
  1142. /*--------------------------------------------------------------------------*/
  1143. FxaaBool directionN = dstN < dstP;
  1144. FxaaFloat dst = min(dstN, dstP);
  1145. FxaaBool goodSpan = directionN ? goodSpanN : goodSpanP;
  1146. FxaaFloat subpixG = subpixF * subpixF;
  1147. FxaaFloat pixelOffset = (dst * (-spanLengthRcp)) + 0.5;
  1148. FxaaFloat subpixH = subpixG * fxaaQualitySubpix;
  1149. /*--------------------------------------------------------------------------*/
  1150. FxaaFloat pixelOffsetGood = goodSpan ? pixelOffset : 0.0;
  1151. FxaaFloat pixelOffsetSubpix = max(pixelOffsetGood, subpixH);
  1152. if(!horzSpan) posM.x += pixelOffsetSubpix * lengthSign;
  1153. if( horzSpan) posM.y += pixelOffsetSubpix * lengthSign;
  1154. #if (FXAA_DISCARD == 1)
  1155. return FxaaTexTop(tex, posM);
  1156. #else
  1157. return FxaaTexTop(tex, posM); // ESENTHEL
  1158. #endif
  1159. }
  1160. /*==========================================================================*/
  1161. #endif
  1162. /*============================================================================
  1163. FXAA3 CONSOLE - PC VERSION
  1164. ------------------------------------------------------------------------------
  1165. Instead of using this on PC, I'd suggest just using FXAA Quality with
  1166. #define FXAA_QUALITY__PRESET 10
  1167. Or
  1168. #define FXAA_QUALITY__PRESET 20
  1169. Either are higher qualilty and almost as fast as this on modern PC GPUs.
  1170. ============================================================================*/
  1171. #if (FXAA_PC_CONSOLE == 1)
  1172. /*--------------------------------------------------------------------------*/
  1173. FxaaFloat4 FxaaPixelShader(
  1174. // See FXAA Quality FxaaPixelShader() source for docs on Inputs!
  1175. FxaaFloat2 pos,
  1176. FxaaFloat4 fxaaConsolePosPos,
  1177. FxaaTex tex,
  1178. FxaaTex fxaaConsole360TexExpBiasNegOne,
  1179. FxaaTex fxaaConsole360TexExpBiasNegTwo,
  1180. FxaaFloat2 fxaaQualityRcpFrame,
  1181. FxaaFloat4 fxaaConsoleRcpFrameOpt,
  1182. FxaaFloat4 fxaaConsoleRcpFrameOpt2,
  1183. FxaaFloat4 fxaaConsole360RcpFrameOpt2,
  1184. FxaaFloat fxaaQualitySubpix,
  1185. FxaaFloat fxaaQualityEdgeThreshold,
  1186. FxaaFloat fxaaQualityEdgeThresholdMin,
  1187. FxaaFloat fxaaConsoleEdgeSharpness,
  1188. FxaaFloat fxaaConsoleEdgeThreshold,
  1189. FxaaFloat fxaaConsoleEdgeThresholdMin,
  1190. FxaaFloat4 fxaaConsole360ConstDir
  1191. ) {
  1192. /*--------------------------------------------------------------------------*/
  1193. FxaaFloat lumaNw = FxaaLuma(FxaaTexTop(tex, fxaaConsolePosPos.xy));
  1194. FxaaFloat lumaSw = FxaaLuma(FxaaTexTop(tex, fxaaConsolePosPos.xw));
  1195. FxaaFloat lumaNe = FxaaLuma(FxaaTexTop(tex, fxaaConsolePosPos.zy));
  1196. FxaaFloat lumaSe = FxaaLuma(FxaaTexTop(tex, fxaaConsolePosPos.zw));
  1197. /*--------------------------------------------------------------------------*/
  1198. FxaaFloat4 rgbyM = FxaaTexTop(tex, pos.xy);
  1199. #if (FXAA_GREEN_AS_LUMA == 0)
  1200. FxaaFloat lumaM = rgbyM.w;
  1201. #else
  1202. FxaaFloat lumaM = rgbyM.y;
  1203. #endif
  1204. /*--------------------------------------------------------------------------*/
  1205. FxaaFloat lumaMaxNwSw = max(lumaNw, lumaSw);
  1206. lumaNe += 1.0/384.0;
  1207. FxaaFloat lumaMinNwSw = min(lumaNw, lumaSw);
  1208. /*--------------------------------------------------------------------------*/
  1209. FxaaFloat lumaMaxNeSe = max(lumaNe, lumaSe);
  1210. FxaaFloat lumaMinNeSe = min(lumaNe, lumaSe);
  1211. /*--------------------------------------------------------------------------*/
  1212. FxaaFloat lumaMax = max(lumaMaxNeSe, lumaMaxNwSw);
  1213. FxaaFloat lumaMin = min(lumaMinNeSe, lumaMinNwSw);
  1214. /*--------------------------------------------------------------------------*/
  1215. FxaaFloat lumaMaxScaled = lumaMax * fxaaConsoleEdgeThreshold;
  1216. /*--------------------------------------------------------------------------*/
  1217. FxaaFloat lumaMinM = min(lumaMin, lumaM);
  1218. FxaaFloat lumaMaxScaledClamped = max(fxaaConsoleEdgeThresholdMin, lumaMaxScaled);
  1219. FxaaFloat lumaMaxM = max(lumaMax, lumaM);
  1220. FxaaFloat dirSwMinusNe = lumaSw - lumaNe;
  1221. FxaaFloat lumaMaxSubMinM = lumaMaxM - lumaMinM;
  1222. FxaaFloat dirSeMinusNw = lumaSe - lumaNw;
  1223. if(lumaMaxSubMinM < lumaMaxScaledClamped) return rgbyM;
  1224. /*--------------------------------------------------------------------------*/
  1225. FxaaFloat2 dir;
  1226. dir.x = dirSwMinusNe + dirSeMinusNw;
  1227. dir.y = dirSwMinusNe - dirSeMinusNw;
  1228. /*--------------------------------------------------------------------------*/
  1229. FxaaFloat2 dir1 = normalize(dir.xy);
  1230. FxaaFloat4 rgbyN1 = FxaaTexTop(tex, pos.xy - dir1 * fxaaConsoleRcpFrameOpt.zw);
  1231. FxaaFloat4 rgbyP1 = FxaaTexTop(tex, pos.xy + dir1 * fxaaConsoleRcpFrameOpt.zw);
  1232. /*--------------------------------------------------------------------------*/
  1233. FxaaFloat dirAbsMinTimesC = min(abs(dir1.x), abs(dir1.y)) * fxaaConsoleEdgeSharpness;
  1234. FxaaFloat2 dir2 = clamp(dir1.xy / dirAbsMinTimesC, -2.0, 2.0);
  1235. /*--------------------------------------------------------------------------*/
  1236. FxaaFloat4 rgbyN2 = FxaaTexTop(tex, pos.xy - dir2 * fxaaConsoleRcpFrameOpt2.zw);
  1237. FxaaFloat4 rgbyP2 = FxaaTexTop(tex, pos.xy + dir2 * fxaaConsoleRcpFrameOpt2.zw);
  1238. /*--------------------------------------------------------------------------*/
  1239. FxaaFloat4 rgbyA = rgbyN1 + rgbyP1;
  1240. FxaaFloat4 rgbyB = ((rgbyN2 + rgbyP2) * 0.25) + (rgbyA * 0.25);
  1241. /*--------------------------------------------------------------------------*/
  1242. #if (FXAA_GREEN_AS_LUMA == 0)
  1243. FxaaBool twoTap = (rgbyB.w < lumaMin) || (rgbyB.w > lumaMax);
  1244. #else
  1245. FxaaBool twoTap = (rgbyB.y < lumaMin) || (rgbyB.y > lumaMax);
  1246. #endif
  1247. if(twoTap) rgbyB.xyz = rgbyA.xyz * 0.5;
  1248. return rgbyB; }
  1249. /*==========================================================================*/
  1250. #endif
  1251. /*============================================================================
  1252. FXAA3 CONSOLE - 360 PIXEL SHADER
  1253. ------------------------------------------------------------------------------
  1254. This optimized version thanks to suggestions from Andy Luedke.
  1255. Should be fully tex bound in all cases.
  1256. As of the FXAA 3.11 release, I have still not tested this code,
  1257. however I fixed a bug which was in both FXAA 3.9 and FXAA 3.10.
  1258. And note this is replacing the old unoptimized version.
  1259. If it does not work, please let me know so I can fix it.
  1260. ============================================================================*/
  1261. #if (FXAA_360 == 1)
  1262. /*--------------------------------------------------------------------------*/
  1263. [reduceTempRegUsage(4)]
  1264. float4 FxaaPixelShader(
  1265. // See FXAA Quality FxaaPixelShader() source for docs on Inputs!
  1266. FxaaFloat2 pos,
  1267. FxaaFloat4 fxaaConsolePosPos,
  1268. FxaaTex tex,
  1269. FxaaTex fxaaConsole360TexExpBiasNegOne,
  1270. FxaaTex fxaaConsole360TexExpBiasNegTwo,
  1271. FxaaFloat2 fxaaQualityRcpFrame,
  1272. FxaaFloat4 fxaaConsoleRcpFrameOpt,
  1273. FxaaFloat4 fxaaConsoleRcpFrameOpt2,
  1274. FxaaFloat4 fxaaConsole360RcpFrameOpt2,
  1275. FxaaFloat fxaaQualitySubpix,
  1276. FxaaFloat fxaaQualityEdgeThreshold,
  1277. FxaaFloat fxaaQualityEdgeThresholdMin,
  1278. FxaaFloat fxaaConsoleEdgeSharpness,
  1279. FxaaFloat fxaaConsoleEdgeThreshold,
  1280. FxaaFloat fxaaConsoleEdgeThresholdMin,
  1281. FxaaFloat4 fxaaConsole360ConstDir
  1282. ) {
  1283. /*--------------------------------------------------------------------------*/
  1284. float4 lumaNwNeSwSe;
  1285. #if (FXAA_GREEN_AS_LUMA == 0)
  1286. asm {
  1287. tfetch2D lumaNwNeSwSe.w___, tex, pos.xy, OffsetX = -0.5, OffsetY = -0.5, UseComputedLOD=false
  1288. tfetch2D lumaNwNeSwSe._w__, tex, pos.xy, OffsetX = 0.5, OffsetY = -0.5, UseComputedLOD=false
  1289. tfetch2D lumaNwNeSwSe.__w_, tex, pos.xy, OffsetX = -0.5, OffsetY = 0.5, UseComputedLOD=false
  1290. tfetch2D lumaNwNeSwSe.___w, tex, pos.xy, OffsetX = 0.5, OffsetY = 0.5, UseComputedLOD=false
  1291. };
  1292. #else
  1293. asm {
  1294. tfetch2D lumaNwNeSwSe.y___, tex, pos.xy, OffsetX = -0.5, OffsetY = -0.5, UseComputedLOD=false
  1295. tfetch2D lumaNwNeSwSe._y__, tex, pos.xy, OffsetX = 0.5, OffsetY = -0.5, UseComputedLOD=false
  1296. tfetch2D lumaNwNeSwSe.__y_, tex, pos.xy, OffsetX = -0.5, OffsetY = 0.5, UseComputedLOD=false
  1297. tfetch2D lumaNwNeSwSe.___y, tex, pos.xy, OffsetX = 0.5, OffsetY = 0.5, UseComputedLOD=false
  1298. };
  1299. #endif
  1300. /*--------------------------------------------------------------------------*/
  1301. lumaNwNeSwSe.y += 1.0/384.0;
  1302. float2 lumaMinTemp = min(lumaNwNeSwSe.xy, lumaNwNeSwSe.zw);
  1303. float2 lumaMaxTemp = max(lumaNwNeSwSe.xy, lumaNwNeSwSe.zw);
  1304. float lumaMin = min(lumaMinTemp.x, lumaMinTemp.y);
  1305. float lumaMax = max(lumaMaxTemp.x, lumaMaxTemp.y);
  1306. /*--------------------------------------------------------------------------*/
  1307. float4 rgbyM = tex2Dlod(tex, float4(pos.xy, 0.0, 0.0));
  1308. #if (FXAA_GREEN_AS_LUMA == 0)
  1309. float lumaMinM = min(lumaMin, rgbyM.w);
  1310. float lumaMaxM = max(lumaMax, rgbyM.w);
  1311. #else
  1312. float lumaMinM = min(lumaMin, rgbyM.y);
  1313. float lumaMaxM = max(lumaMax, rgbyM.y);
  1314. #endif
  1315. if((lumaMaxM - lumaMinM) < max(fxaaConsoleEdgeThresholdMin, lumaMax * fxaaConsoleEdgeThreshold)) return rgbyM;
  1316. /*--------------------------------------------------------------------------*/
  1317. float2 dir;
  1318. dir.x = dot(lumaNwNeSwSe, fxaaConsole360ConstDir.yyxx);
  1319. dir.y = dot(lumaNwNeSwSe, fxaaConsole360ConstDir.xyxy);
  1320. dir = normalize(dir);
  1321. /*--------------------------------------------------------------------------*/
  1322. float4 dir1 = dir.xyxy * fxaaConsoleRcpFrameOpt.xyzw;
  1323. /*--------------------------------------------------------------------------*/
  1324. float4 dir2;
  1325. float dirAbsMinTimesC = min(abs(dir.x), abs(dir.y)) * fxaaConsoleEdgeSharpness;
  1326. dir2 = saturate(fxaaConsole360ConstDir.zzww * dir.xyxy / dirAbsMinTimesC + 0.5);
  1327. dir2 = dir2 * fxaaConsole360RcpFrameOpt2.xyxy + fxaaConsole360RcpFrameOpt2.zwzw;
  1328. /*--------------------------------------------------------------------------*/
  1329. float4 rgbyN1 = tex2Dlod(fxaaConsole360TexExpBiasNegOne, float4(pos.xy + dir1.xy, 0.0, 0.0));
  1330. float4 rgbyP1 = tex2Dlod(fxaaConsole360TexExpBiasNegOne, float4(pos.xy + dir1.zw, 0.0, 0.0));
  1331. float4 rgbyN2 = tex2Dlod(fxaaConsole360TexExpBiasNegTwo, float4(pos.xy + dir2.xy, 0.0, 0.0));
  1332. float4 rgbyP2 = tex2Dlod(fxaaConsole360TexExpBiasNegTwo, float4(pos.xy + dir2.zw, 0.0, 0.0));
  1333. /*--------------------------------------------------------------------------*/
  1334. float4 rgbyA = rgbyN1 + rgbyP1;
  1335. float4 rgbyB = rgbyN2 + rgbyP2 + rgbyA * 0.5;
  1336. /*--------------------------------------------------------------------------*/
  1337. float4 rgbyR = ((FxaaLuma(rgbyB) - lumaMax) > 0.0) ? rgbyA : rgbyB;
  1338. rgbyR = ((FxaaLuma(rgbyB) - lumaMin) > 0.0) ? rgbyR : rgbyA;
  1339. return rgbyR; }
  1340. /*==========================================================================*/
  1341. #endif
  1342. /*============================================================================
  1343. FXAA3 CONSOLE - OPTIMIZED PS3 PIXEL SHADER (NO EARLY EXIT)
  1344. ==============================================================================
  1345. The code below does not exactly match the assembly.
  1346. I have a feeling that 12 cycles is possible, but was not able to get there.
  1347. Might have to increase register count to get full performance.
  1348. Note this shader does not use perspective interpolation.
  1349. Use the following cgc options,
  1350. --fenable-bx2 --fastmath --fastprecision --nofloatbindings
  1351. ------------------------------------------------------------------------------
  1352. NVSHADERPERF OUTPUT
  1353. ------------------------------------------------------------------------------
  1354. For reference and to aid in debug, output of NVShaderPerf should match this,
  1355. Shader to schedule:
  1356. 0: texpkb h0.w(TRUE), v5.zyxx, #0
  1357. 2: addh h2.z(TRUE), h0.w, constant(0.001953, 0.000000, 0.000000, 0.000000).x
  1358. 4: texpkb h0.w(TRUE), v5.xwxx, #0
  1359. 6: addh h0.z(TRUE), -h2, h0.w
  1360. 7: texpkb h1.w(TRUE), v5, #0
  1361. 9: addh h0.x(TRUE), h0.z, -h1.w
  1362. 10: addh h3.w(TRUE), h0.z, h1
  1363. 11: texpkb h2.w(TRUE), v5.zwzz, #0
  1364. 13: addh h0.z(TRUE), h3.w, -h2.w
  1365. 14: addh h0.x(TRUE), h2.w, h0
  1366. 15: nrmh h1.xz(TRUE), h0_n
  1367. 16: minh_m8 h0.x(TRUE), |h1|, |h1.z|
  1368. 17: maxh h4.w(TRUE), h0, h1
  1369. 18: divx h2.xy(TRUE), h1_n.xzzw, h0_n
  1370. 19: movr r1.zw(TRUE), v4.xxxy
  1371. 20: madr r2.xz(TRUE), -h1, constant(cConst5.x, cConst5.y, cConst5.z, cConst5.w).zzww, r1.zzww
  1372. 22: minh h5.w(TRUE), h0, h1
  1373. 23: texpkb h0(TRUE), r2.xzxx, #0
  1374. 25: madr r0.zw(TRUE), h1.xzxz, constant(cConst5.x, cConst5.y, cConst5.z, cConst5.w), r1
  1375. 27: maxh h4.x(TRUE), h2.z, h2.w
  1376. 28: texpkb h1(TRUE), r0.zwzz, #0
  1377. 30: addh_d2 h1(TRUE), h0, h1
  1378. 31: madr r0.xy(TRUE), -h2, constant(cConst5.x, cConst5.y, cConst5.z, cConst5.w).xyxx, r1.zwzz
  1379. 33: texpkb h0(TRUE), r0, #0
  1380. 35: minh h4.z(TRUE), h2, h2.w
  1381. 36: fenct TRUE
  1382. 37: madr r1.xy(TRUE), h2, constant(cConst5.x, cConst5.y, cConst5.z, cConst5.w).xyxx, r1.zwzz
  1383. 39: texpkb h2(TRUE), r1, #0
  1384. 41: addh_d2 h0(TRUE), h0, h2
  1385. 42: maxh h2.w(TRUE), h4, h4.x
  1386. 43: minh h2.x(TRUE), h5.w, h4.z
  1387. 44: addh_d2 h0(TRUE), h0, h1
  1388. 45: slth h2.x(TRUE), h0.w, h2
  1389. 46: sgth h2.w(TRUE), h0, h2
  1390. 47: movh h0(TRUE), h0
  1391. 48: addx.c0 rc(TRUE), h2, h2.w
  1392. 49: movh h0(c0.NE.x), h1
  1393. IPU0 ------ Simplified schedule: --------
  1394. Pass | Unit | uOp | PC: Op
  1395. -----+--------+------+-------------------------
  1396. 1 | SCT0/1 | mov | 0: TXLr h0.w, g[TEX1].zyxx, const.xxxx, TEX0;
  1397. | TEX | txl | 0: TXLr h0.w, g[TEX1].zyxx, const.xxxx, TEX0;
  1398. | SCB1 | add | 2: ADDh h2.z, h0.--w-, const.--x-;
  1399. | | |
  1400. 2 | SCT0/1 | mov | 4: TXLr h0.w, g[TEX1].xwxx, const.xxxx, TEX0;
  1401. | TEX | txl | 4: TXLr h0.w, g[TEX1].xwxx, const.xxxx, TEX0;
  1402. | SCB1 | add | 6: ADDh h0.z,-h2, h0.--w-;
  1403. | | |
  1404. 3 | SCT0/1 | mov | 7: TXLr h1.w, g[TEX1], const.xxxx, TEX0;
  1405. | TEX | txl | 7: TXLr h1.w, g[TEX1], const.xxxx, TEX0;
  1406. | SCB0 | add | 9: ADDh h0.x, h0.z---,-h1.w---;
  1407. | SCB1 | add | 10: ADDh h3.w, h0.---z, h1;
  1408. | | |
  1409. 4 | SCT0/1 | mov | 11: TXLr h2.w, g[TEX1].zwzz, const.xxxx, TEX0;
  1410. | TEX | txl | 11: TXLr h2.w, g[TEX1].zwzz, const.xxxx, TEX0;
  1411. | SCB0 | add | 14: ADDh h0.x, h2.w---, h0;
  1412. | SCB1 | add | 13: ADDh h0.z, h3.--w-,-h2.--w-;
  1413. | | |
  1414. 5 | SCT1 | mov | 15: NRMh h1.xz, h0;
  1415. | SRB | nrm | 15: NRMh h1.xz, h0;
  1416. | SCB0 | min | 16: MINh*8 h0.x, |h1|, |h1.z---|;
  1417. | SCB1 | max | 17: MAXh h4.w, h0, h1;
  1418. | | |
  1419. 6 | SCT0 | div | 18: DIVx h2.xy, h1.xz--, h0;
  1420. | SCT1 | mov | 19: MOVr r1.zw, g[TEX0].--xy;
  1421. | SCB0 | mad | 20: MADr r2.xz,-h1, const.z-w-, r1.z-w-;
  1422. | SCB1 | min | 22: MINh h5.w, h0, h1;
  1423. | | |
  1424. 7 | SCT0/1 | mov | 23: TXLr h0, r2.xzxx, const.xxxx, TEX0;
  1425. | TEX | txl | 23: TXLr h0, r2.xzxx, const.xxxx, TEX0;
  1426. | SCB0 | max | 27: MAXh h4.x, h2.z---, h2.w---;
  1427. | SCB1 | mad | 25: MADr r0.zw, h1.--xz, const, r1;
  1428. | | |
  1429. 8 | SCT0/1 | mov | 28: TXLr h1, r0.zwzz, const.xxxx, TEX0;
  1430. | TEX | txl | 28: TXLr h1, r0.zwzz, const.xxxx, TEX0;
  1431. | SCB0/1 | add | 30: ADDh/2 h1, h0, h1;
  1432. | | |
  1433. 9 | SCT0 | mad | 31: MADr r0.xy,-h2, const.xy--, r1.zw--;
  1434. | SCT1 | mov | 33: TXLr h0, r0, const.zzzz, TEX0;
  1435. | TEX | txl | 33: TXLr h0, r0, const.zzzz, TEX0;
  1436. | SCB1 | min | 35: MINh h4.z, h2, h2.--w-;
  1437. | | |
  1438. 10 | SCT0 | mad | 37: MADr r1.xy, h2, const.xy--, r1.zw--;
  1439. | SCT1 | mov | 39: TXLr h2, r1, const.zzzz, TEX0;
  1440. | TEX | txl | 39: TXLr h2, r1, const.zzzz, TEX0;
  1441. | SCB0/1 | add | 41: ADDh/2 h0, h0, h2;
  1442. | | |
  1443. 11 | SCT0 | min | 43: MINh h2.x, h5.w---, h4.z---;
  1444. | SCT1 | max | 42: MAXh h2.w, h4, h4.---x;
  1445. | SCB0/1 | add | 44: ADDh/2 h0, h0, h1;
  1446. | | |
  1447. 12 | SCT0 | set | 45: SLTh h2.x, h0.w---, h2;
  1448. | SCT1 | set | 46: SGTh h2.w, h0, h2;
  1449. | SCB0/1 | mul | 47: MOVh h0, h0;
  1450. | | |
  1451. 13 | SCT0 | mad | 48: ADDxc0_s rc, h2, h2.w---;
  1452. | SCB0/1 | mul | 49: MOVh h0(NE0.xxxx), h1;
  1453. Pass SCT TEX SCB
  1454. 1: 0% 100% 25%
  1455. 2: 0% 100% 25%
  1456. 3: 0% 100% 50%
  1457. 4: 0% 100% 50%
  1458. 5: 0% 0% 50%
  1459. 6: 100% 0% 75%
  1460. 7: 0% 100% 75%
  1461. 8: 0% 100% 100%
  1462. 9: 0% 100% 25%
  1463. 10: 0% 100% 100%
  1464. 11: 50% 0% 100%
  1465. 12: 50% 0% 100%
  1466. 13: 25% 0% 100%
  1467. MEAN: 17% 61% 67%
  1468. Pass SCT0 SCT1 TEX SCB0 SCB1
  1469. 1: 0% 0% 100% 0% 100%
  1470. 2: 0% 0% 100% 0% 100%
  1471. 3: 0% 0% 100% 100% 100%
  1472. 4: 0% 0% 100% 100% 100%
  1473. 5: 0% 0% 0% 100% 100%
  1474. 6: 100% 100% 0% 100% 100%
  1475. 7: 0% 0% 100% 100% 100%
  1476. 8: 0% 0% 100% 100% 100%
  1477. 9: 0% 0% 100% 0% 100%
  1478. 10: 0% 0% 100% 100% 100%
  1479. 11: 100% 100% 0% 100% 100%
  1480. 12: 100% 100% 0% 100% 100%
  1481. 13: 100% 0% 0% 100% 100%
  1482. MEAN: 30% 23% 61% 76% 100%
  1483. Fragment Performance Setup: Driver RSX Compiler, GPU RSX, Flags 0x5
  1484. Results 13 cycles, 3 r regs, 923,076,923 pixels/s
  1485. ============================================================================*/
  1486. #if (FXAA_PS3 == 1) && (FXAA_EARLY_EXIT == 0)
  1487. /*--------------------------------------------------------------------------*/
  1488. #pragma regcount 7
  1489. #pragma disablepc all
  1490. #pragma option O3
  1491. #pragma option OutColorPrec=fp16
  1492. #pragma texformat default RGBA8
  1493. /*==========================================================================*/
  1494. half4 FxaaPixelShader(
  1495. // See FXAA Quality FxaaPixelShader() source for docs on Inputs!
  1496. FxaaFloat2 pos,
  1497. FxaaFloat4 fxaaConsolePosPos,
  1498. FxaaTex tex,
  1499. FxaaTex fxaaConsole360TexExpBiasNegOne,
  1500. FxaaTex fxaaConsole360TexExpBiasNegTwo,
  1501. FxaaFloat2 fxaaQualityRcpFrame,
  1502. FxaaFloat4 fxaaConsoleRcpFrameOpt,
  1503. FxaaFloat4 fxaaConsoleRcpFrameOpt2,
  1504. FxaaFloat4 fxaaConsole360RcpFrameOpt2,
  1505. FxaaFloat fxaaQualitySubpix,
  1506. FxaaFloat fxaaQualityEdgeThreshold,
  1507. FxaaFloat fxaaQualityEdgeThresholdMin,
  1508. FxaaFloat fxaaConsoleEdgeSharpness,
  1509. FxaaFloat fxaaConsoleEdgeThreshold,
  1510. FxaaFloat fxaaConsoleEdgeThresholdMin,
  1511. FxaaFloat4 fxaaConsole360ConstDir
  1512. ) {
  1513. /*--------------------------------------------------------------------------*/
  1514. // (1)
  1515. half4 dir;
  1516. half4 lumaNe = h4tex2Dlod(tex, half4(fxaaConsolePosPos.zy, 0, 0));
  1517. #if (FXAA_GREEN_AS_LUMA == 0)
  1518. lumaNe.w += half(1.0/512.0);
  1519. dir.x = -lumaNe.w;
  1520. dir.z = -lumaNe.w;
  1521. #else
  1522. lumaNe.y += half(1.0/512.0);
  1523. dir.x = -lumaNe.y;
  1524. dir.z = -lumaNe.y;
  1525. #endif
  1526. /*--------------------------------------------------------------------------*/
  1527. // (2)
  1528. half4 lumaSw = h4tex2Dlod(tex, half4(fxaaConsolePosPos.xw, 0, 0));
  1529. #if (FXAA_GREEN_AS_LUMA == 0)
  1530. dir.x += lumaSw.w;
  1531. dir.z += lumaSw.w;
  1532. #else
  1533. dir.x += lumaSw.y;
  1534. dir.z += lumaSw.y;
  1535. #endif
  1536. /*--------------------------------------------------------------------------*/
  1537. // (3)
  1538. half4 lumaNw = h4tex2Dlod(tex, half4(fxaaConsolePosPos.xy, 0, 0));
  1539. #if (FXAA_GREEN_AS_LUMA == 0)
  1540. dir.x -= lumaNw.w;
  1541. dir.z += lumaNw.w;
  1542. #else
  1543. dir.x -= lumaNw.y;
  1544. dir.z += lumaNw.y;
  1545. #endif
  1546. /*--------------------------------------------------------------------------*/
  1547. // (4)
  1548. half4 lumaSe = h4tex2Dlod(tex, half4(fxaaConsolePosPos.zw, 0, 0));
  1549. #if (FXAA_GREEN_AS_LUMA == 0)
  1550. dir.x += lumaSe.w;
  1551. dir.z -= lumaSe.w;
  1552. #else
  1553. dir.x += lumaSe.y;
  1554. dir.z -= lumaSe.y;
  1555. #endif
  1556. /*--------------------------------------------------------------------------*/
  1557. // (5)
  1558. half4 dir1_pos;
  1559. dir1_pos.xy = normalize(dir.xyz).xz;
  1560. half dirAbsMinTimesC = min(abs(dir1_pos.x), abs(dir1_pos.y)) * half(FXAA_CONSOLE__PS3_EDGE_SHARPNESS);
  1561. /*--------------------------------------------------------------------------*/
  1562. // (6)
  1563. half4 dir2_pos;
  1564. dir2_pos.xy = clamp(dir1_pos.xy / dirAbsMinTimesC, half(-2.0), half(2.0));
  1565. dir1_pos.zw = pos.xy;
  1566. dir2_pos.zw = pos.xy;
  1567. half4 temp1N;
  1568. temp1N.xy = dir1_pos.zw - dir1_pos.xy * fxaaConsoleRcpFrameOpt.zw;
  1569. /*--------------------------------------------------------------------------*/
  1570. // (7)
  1571. temp1N = h4tex2Dlod(tex, half4(temp1N.xy, 0.0, 0.0));
  1572. half4 rgby1;
  1573. rgby1.xy = dir1_pos.zw + dir1_pos.xy * fxaaConsoleRcpFrameOpt.zw;
  1574. /*--------------------------------------------------------------------------*/
  1575. // (8)
  1576. rgby1 = h4tex2Dlod(tex, half4(rgby1.xy, 0.0, 0.0));
  1577. rgby1 = (temp1N + rgby1) * 0.5;
  1578. /*--------------------------------------------------------------------------*/
  1579. // (9)
  1580. half4 temp2N;
  1581. temp2N.xy = dir2_pos.zw - dir2_pos.xy * fxaaConsoleRcpFrameOpt2.zw;
  1582. temp2N = h4tex2Dlod(tex, half4(temp2N.xy, 0.0, 0.0));
  1583. /*--------------------------------------------------------------------------*/
  1584. // (10)
  1585. half4 rgby2;
  1586. rgby2.xy = dir2_pos.zw + dir2_pos.xy * fxaaConsoleRcpFrameOpt2.zw;
  1587. rgby2 = h4tex2Dlod(tex, half4(rgby2.xy, 0.0, 0.0));
  1588. rgby2 = (temp2N + rgby2) * 0.5;
  1589. /*--------------------------------------------------------------------------*/
  1590. // (11)
  1591. // compilier moves these scalar ops up to other cycles
  1592. #if (FXAA_GREEN_AS_LUMA == 0)
  1593. half lumaMin = min(min(lumaNw.w, lumaSw.w), min(lumaNe.w, lumaSe.w));
  1594. half lumaMax = max(max(lumaNw.w, lumaSw.w), max(lumaNe.w, lumaSe.w));
  1595. #else
  1596. half lumaMin = min(min(lumaNw.y, lumaSw.y), min(lumaNe.y, lumaSe.y));
  1597. half lumaMax = max(max(lumaNw.y, lumaSw.y), max(lumaNe.y, lumaSe.y));
  1598. #endif
  1599. rgby2 = (rgby2 + rgby1) * 0.5;
  1600. /*--------------------------------------------------------------------------*/
  1601. // (12)
  1602. #if (FXAA_GREEN_AS_LUMA == 0)
  1603. bool twoTapLt = rgby2.w < lumaMin;
  1604. bool twoTapGt = rgby2.w > lumaMax;
  1605. #else
  1606. bool twoTapLt = rgby2.y < lumaMin;
  1607. bool twoTapGt = rgby2.y > lumaMax;
  1608. #endif
  1609. /*--------------------------------------------------------------------------*/
  1610. // (13)
  1611. if(twoTapLt || twoTapGt) rgby2 = rgby1;
  1612. /*--------------------------------------------------------------------------*/
  1613. return rgby2; }
  1614. /*==========================================================================*/
  1615. #endif
  1616. /*============================================================================
  1617. FXAA3 CONSOLE - OPTIMIZED PS3 PIXEL SHADER (WITH EARLY EXIT)
  1618. ==============================================================================
  1619. The code mostly matches the assembly.
  1620. I have a feeling that 14 cycles is possible, but was not able to get there.
  1621. Might have to increase register count to get full performance.
  1622. Note this shader does not use perspective interpolation.
  1623. Use the following cgc options,
  1624. --fenable-bx2 --fastmath --fastprecision --nofloatbindings
  1625. Use of FXAA_GREEN_AS_LUMA currently adds a cycle (16 clks).
  1626. Will look at fixing this for FXAA 3.12.
  1627. ------------------------------------------------------------------------------
  1628. NVSHADERPERF OUTPUT
  1629. ------------------------------------------------------------------------------
  1630. For reference and to aid in debug, output of NVShaderPerf should match this,
  1631. Shader to schedule:
  1632. 0: texpkb h0.w(TRUE), v5.zyxx, #0
  1633. 2: addh h2.y(TRUE), h0.w, constant(0.001953, 0.000000, 0.000000, 0.000000).x
  1634. 4: texpkb h1.w(TRUE), v5.xwxx, #0
  1635. 6: addh h0.x(TRUE), h1.w, -h2.y
  1636. 7: texpkb h2.w(TRUE), v5.zwzz, #0
  1637. 9: minh h4.w(TRUE), h2.y, h2
  1638. 10: maxh h5.x(TRUE), h2.y, h2.w
  1639. 11: texpkb h0.w(TRUE), v5, #0
  1640. 13: addh h3.w(TRUE), -h0, h0.x
  1641. 14: addh h0.x(TRUE), h0.w, h0
  1642. 15: addh h0.z(TRUE), -h2.w, h0.x
  1643. 16: addh h0.x(TRUE), h2.w, h3.w
  1644. 17: minh h5.y(TRUE), h0.w, h1.w
  1645. 18: nrmh h2.xz(TRUE), h0_n
  1646. 19: minh_m8 h2.w(TRUE), |h2.x|, |h2.z|
  1647. 20: divx h4.xy(TRUE), h2_n.xzzw, h2_n.w
  1648. 21: movr r1.zw(TRUE), v4.xxxy
  1649. 22: maxh h2.w(TRUE), h0, h1
  1650. 23: fenct TRUE
  1651. 24: madr r0.xy(TRUE), -h2.xzzw, constant(cConst5.x, cConst5.y, cConst5.z, cConst5.w).zwzz, r1.zwzz
  1652. 26: texpkb h0(TRUE), r0, #0
  1653. 28: maxh h5.x(TRUE), h2.w, h5
  1654. 29: minh h5.w(TRUE), h5.y, h4
  1655. 30: madr r1.xy(TRUE), h2.xzzw, constant(cConst5.x, cConst5.y, cConst5.z, cConst5.w).zwzz, r1.zwzz
  1656. 32: texpkb h2(TRUE), r1, #0
  1657. 34: addh_d2 h2(TRUE), h0, h2
  1658. 35: texpkb h1(TRUE), v4, #0
  1659. 37: maxh h5.y(TRUE), h5.x, h1.w
  1660. 38: minh h4.w(TRUE), h1, h5
  1661. 39: madr r0.xy(TRUE), -h4, constant(cConst5.x, cConst5.y, cConst5.z, cConst5.w).xyxx, r1.zwzz
  1662. 41: texpkb h0(TRUE), r0, #0
  1663. 43: addh_m8 h5.z(TRUE), h5.y, -h4.w
  1664. 44: madr r2.xy(TRUE), h4, constant(cConst5.x, cConst5.y, cConst5.z, cConst5.w).xyxx, r1.zwzz
  1665. 46: texpkb h3(TRUE), r2, #0
  1666. 48: addh_d2 h0(TRUE), h0, h3
  1667. 49: addh_d2 h3(TRUE), h0, h2
  1668. 50: movh h0(TRUE), h3
  1669. 51: slth h3.x(TRUE), h3.w, h5.w
  1670. 52: sgth h3.w(TRUE), h3, h5.x
  1671. 53: addx.c0 rc(TRUE), h3.x, h3
  1672. 54: slth.c0 rc(TRUE), h5.z, h5
  1673. 55: movh h0(c0.NE.w), h2
  1674. 56: movh h0(c0.NE.x), h1
  1675. IPU0 ------ Simplified schedule: --------
  1676. Pass | Unit | uOp | PC: Op
  1677. -----+--------+------+-------------------------
  1678. 1 | SCT0/1 | mov | 0: TXLr h0.w, g[TEX1].zyxx, const.xxxx, TEX0;
  1679. | TEX | txl | 0: TXLr h0.w, g[TEX1].zyxx, const.xxxx, TEX0;
  1680. | SCB0 | add | 2: ADDh h2.y, h0.-w--, const.-x--;
  1681. | | |
  1682. 2 | SCT0/1 | mov | 4: TXLr h1.w, g[TEX1].xwxx, const.xxxx, TEX0;
  1683. | TEX | txl | 4: TXLr h1.w, g[TEX1].xwxx, const.xxxx, TEX0;
  1684. | SCB0 | add | 6: ADDh h0.x, h1.w---,-h2.y---;
  1685. | | |
  1686. 3 | SCT0/1 | mov | 7: TXLr h2.w, g[TEX1].zwzz, const.xxxx, TEX0;
  1687. | TEX | txl | 7: TXLr h2.w, g[TEX1].zwzz, const.xxxx, TEX0;
  1688. | SCB0 | max | 10: MAXh h5.x, h2.y---, h2.w---;
  1689. | SCB1 | min | 9: MINh h4.w, h2.---y, h2;
  1690. | | |
  1691. 4 | SCT0/1 | mov | 11: TXLr h0.w, g[TEX1], const.xxxx, TEX0;
  1692. | TEX | txl | 11: TXLr h0.w, g[TEX1], const.xxxx, TEX0;
  1693. | SCB0 | add | 14: ADDh h0.x, h0.w---, h0;
  1694. | SCB1 | add | 13: ADDh h3.w,-h0, h0.---x;
  1695. | | |
  1696. 5 | SCT0 | mad | 16: ADDh h0.x, h2.w---, h3.w---;
  1697. | SCT1 | mad | 15: ADDh h0.z,-h2.--w-, h0.--x-;
  1698. | SCB0 | min | 17: MINh h5.y, h0.-w--, h1.-w--;
  1699. | | |
  1700. 6 | SCT1 | mov | 18: NRMh h2.xz, h0;
  1701. | SRB | nrm | 18: NRMh h2.xz, h0;
  1702. | SCB1 | min | 19: MINh*8 h2.w, |h2.---x|, |h2.---z|;
  1703. | | |
  1704. 7 | SCT0 | div | 20: DIVx h4.xy, h2.xz--, h2.ww--;
  1705. | SCT1 | mov | 21: MOVr r1.zw, g[TEX0].--xy;
  1706. | SCB1 | max | 22: MAXh h2.w, h0, h1;
  1707. | | |
  1708. 8 | SCT0 | mad | 24: MADr r0.xy,-h2.xz--, const.zw--, r1.zw--;
  1709. | SCT1 | mov | 26: TXLr h0, r0, const.xxxx, TEX0;
  1710. | TEX | txl | 26: TXLr h0, r0, const.xxxx, TEX0;
  1711. | SCB0 | max | 28: MAXh h5.x, h2.w---, h5;
  1712. | SCB1 | min | 29: MINh h5.w, h5.---y, h4;
  1713. | | |
  1714. 9 | SCT0 | mad | 30: MADr r1.xy, h2.xz--, const.zw--, r1.zw--;
  1715. | SCT1 | mov | 32: TXLr h2, r1, const.xxxx, TEX0;
  1716. | TEX | txl | 32: TXLr h2, r1, const.xxxx, TEX0;
  1717. | SCB0/1 | add | 34: ADDh/2 h2, h0, h2;
  1718. | | |
  1719. 10 | SCT0/1 | mov | 35: TXLr h1, g[TEX0], const.xxxx, TEX0;
  1720. | TEX | txl | 35: TXLr h1, g[TEX0], const.xxxx, TEX0;
  1721. | SCB0 | max | 37: MAXh h5.y, h5.-x--, h1.-w--;
  1722. | SCB1 | min | 38: MINh h4.w, h1, h5;
  1723. | | |
  1724. 11 | SCT0 | mad | 39: MADr r0.xy,-h4, const.xy--, r1.zw--;
  1725. | SCT1 | mov | 41: TXLr h0, r0, const.zzzz, TEX0;
  1726. | TEX | txl | 41: TXLr h0, r0, const.zzzz, TEX0;
  1727. | SCB0 | mad | 44: MADr r2.xy, h4, const.xy--, r1.zw--;
  1728. | SCB1 | add | 43: ADDh*8 h5.z, h5.--y-,-h4.--w-;
  1729. | | |
  1730. 12 | SCT0/1 | mov | 46: TXLr h3, r2, const.xxxx, TEX0;
  1731. | TEX | txl | 46: TXLr h3, r2, const.xxxx, TEX0;
  1732. | SCB0/1 | add | 48: ADDh/2 h0, h0, h3;
  1733. | | |
  1734. 13 | SCT0/1 | mad | 49: ADDh/2 h3, h0, h2;
  1735. | SCB0/1 | mul | 50: MOVh h0, h3;
  1736. | | |
  1737. 14 | SCT0 | set | 51: SLTh h3.x, h3.w---, h5.w---;
  1738. | SCT1 | set | 52: SGTh h3.w, h3, h5.---x;
  1739. | SCB0 | set | 54: SLThc0 rc, h5.z---, h5;
  1740. | SCB1 | add | 53: ADDxc0_s rc, h3.---x, h3;
  1741. | | |
  1742. 15 | SCT0/1 | mul | 55: MOVh h0(NE0.wwww), h2;
  1743. | SCB0/1 | mul | 56: MOVh h0(NE0.xxxx), h1;
  1744. Pass SCT TEX SCB
  1745. 1: 0% 100% 25%
  1746. 2: 0% 100% 25%
  1747. 3: 0% 100% 50%
  1748. 4: 0% 100% 50%
  1749. 5: 50% 0% 25%
  1750. 6: 0% 0% 25%
  1751. 7: 100% 0% 25%
  1752. 8: 0% 100% 50%
  1753. 9: 0% 100% 100%
  1754. 10: 0% 100% 50%
  1755. 11: 0% 100% 75%
  1756. 12: 0% 100% 100%
  1757. 13: 100% 0% 100%
  1758. 14: 50% 0% 50%
  1759. 15: 100% 0% 100%
  1760. MEAN: 26% 60% 56%
  1761. Pass SCT0 SCT1 TEX SCB0 SCB1
  1762. 1: 0% 0% 100% 100% 0%
  1763. 2: 0% 0% 100% 100% 0%
  1764. 3: 0% 0% 100% 100% 100%
  1765. 4: 0% 0% 100% 100% 100%
  1766. 5: 100% 100% 0% 100% 0%
  1767. 6: 0% 0% 0% 0% 100%
  1768. 7: 100% 100% 0% 0% 100%
  1769. 8: 0% 0% 100% 100% 100%
  1770. 9: 0% 0% 100% 100% 100%
  1771. 10: 0% 0% 100% 100% 100%
  1772. 11: 0% 0% 100% 100% 100%
  1773. 12: 0% 0% 100% 100% 100%
  1774. 13: 100% 100% 0% 100% 100%
  1775. 14: 100% 100% 0% 100% 100%
  1776. 15: 100% 100% 0% 100% 100%
  1777. MEAN: 33% 33% 60% 86% 80%
  1778. Fragment Performance Setup: Driver RSX Compiler, GPU RSX, Flags 0x5
  1779. Results 15 cycles, 3 r regs, 800,000,000 pixels/s
  1780. ============================================================================*/
  1781. #if (FXAA_PS3 == 1) && (FXAA_EARLY_EXIT == 1)
  1782. /*--------------------------------------------------------------------------*/
  1783. #pragma regcount 7
  1784. #pragma disablepc all
  1785. #pragma option O2
  1786. #pragma option OutColorPrec=fp16
  1787. #pragma texformat default RGBA8
  1788. /*==========================================================================*/
  1789. half4 FxaaPixelShader(
  1790. // See FXAA Quality FxaaPixelShader() source for docs on Inputs!
  1791. FxaaFloat2 pos,
  1792. FxaaFloat4 fxaaConsolePosPos,
  1793. FxaaTex tex,
  1794. FxaaTex fxaaConsole360TexExpBiasNegOne,
  1795. FxaaTex fxaaConsole360TexExpBiasNegTwo,
  1796. FxaaFloat2 fxaaQualityRcpFrame,
  1797. FxaaFloat4 fxaaConsoleRcpFrameOpt,
  1798. FxaaFloat4 fxaaConsoleRcpFrameOpt2,
  1799. FxaaFloat4 fxaaConsole360RcpFrameOpt2,
  1800. FxaaFloat fxaaQualitySubpix,
  1801. FxaaFloat fxaaQualityEdgeThreshold,
  1802. FxaaFloat fxaaQualityEdgeThresholdMin,
  1803. FxaaFloat fxaaConsoleEdgeSharpness,
  1804. FxaaFloat fxaaConsoleEdgeThreshold,
  1805. FxaaFloat fxaaConsoleEdgeThresholdMin,
  1806. FxaaFloat4 fxaaConsole360ConstDir
  1807. ) {
  1808. /*--------------------------------------------------------------------------*/
  1809. // (1)
  1810. half4 rgbyNe = h4tex2Dlod(tex, half4(fxaaConsolePosPos.zy, 0, 0));
  1811. #if (FXAA_GREEN_AS_LUMA == 0)
  1812. half lumaNe = rgbyNe.w + half(1.0/512.0);
  1813. #else
  1814. half lumaNe = rgbyNe.y + half(1.0/512.0);
  1815. #endif
  1816. /*--------------------------------------------------------------------------*/
  1817. // (2)
  1818. half4 lumaSw = h4tex2Dlod(tex, half4(fxaaConsolePosPos.xw, 0, 0));
  1819. #if (FXAA_GREEN_AS_LUMA == 0)
  1820. half lumaSwNegNe = lumaSw.w - lumaNe;
  1821. #else
  1822. half lumaSwNegNe = lumaSw.y - lumaNe;
  1823. #endif
  1824. /*--------------------------------------------------------------------------*/
  1825. // (3)
  1826. half4 lumaNw = h4tex2Dlod(tex, half4(fxaaConsolePosPos.xy, 0, 0));
  1827. #if (FXAA_GREEN_AS_LUMA == 0)
  1828. half lumaMaxNwSw = max(lumaNw.w, lumaSw.w);
  1829. half lumaMinNwSw = min(lumaNw.w, lumaSw.w);
  1830. #else
  1831. half lumaMaxNwSw = max(lumaNw.y, lumaSw.y);
  1832. half lumaMinNwSw = min(lumaNw.y, lumaSw.y);
  1833. #endif
  1834. /*--------------------------------------------------------------------------*/
  1835. // (4)
  1836. half4 lumaSe = h4tex2Dlod(tex, half4(fxaaConsolePosPos.zw, 0, 0));
  1837. #if (FXAA_GREEN_AS_LUMA == 0)
  1838. half dirZ = lumaNw.w + lumaSwNegNe;
  1839. half dirX = -lumaNw.w + lumaSwNegNe;
  1840. #else
  1841. half dirZ = lumaNw.y + lumaSwNegNe;
  1842. half dirX = -lumaNw.y + lumaSwNegNe;
  1843. #endif
  1844. /*--------------------------------------------------------------------------*/
  1845. // (5)
  1846. half3 dir;
  1847. dir.y = 0.0;
  1848. #if (FXAA_GREEN_AS_LUMA == 0)
  1849. dir.x = lumaSe.w + dirX;
  1850. dir.z = -lumaSe.w + dirZ;
  1851. half lumaMinNeSe = min(lumaNe, lumaSe.w);
  1852. #else
  1853. dir.x = lumaSe.y + dirX;
  1854. dir.z = -lumaSe.y + dirZ;
  1855. half lumaMinNeSe = min(lumaNe, lumaSe.y);
  1856. #endif
  1857. /*--------------------------------------------------------------------------*/
  1858. // (6)
  1859. half4 dir1_pos;
  1860. dir1_pos.xy = normalize(dir).xz;
  1861. half dirAbsMinTimes8 = min(abs(dir1_pos.x), abs(dir1_pos.y)) * half(FXAA_CONSOLE__PS3_EDGE_SHARPNESS);
  1862. /*--------------------------------------------------------------------------*/
  1863. // (7)
  1864. half4 dir2_pos;
  1865. dir2_pos.xy = clamp(dir1_pos.xy / dirAbsMinTimes8, half(-2.0), half(2.0));
  1866. dir1_pos.zw = pos.xy;
  1867. dir2_pos.zw = pos.xy;
  1868. #if (FXAA_GREEN_AS_LUMA == 0)
  1869. half lumaMaxNeSe = max(lumaNe, lumaSe.w);
  1870. #else
  1871. half lumaMaxNeSe = max(lumaNe, lumaSe.y);
  1872. #endif
  1873. /*--------------------------------------------------------------------------*/
  1874. // (8)
  1875. half4 temp1N;
  1876. temp1N.xy = dir1_pos.zw - dir1_pos.xy * fxaaConsoleRcpFrameOpt.zw;
  1877. temp1N = h4tex2Dlod(tex, half4(temp1N.xy, 0.0, 0.0));
  1878. half lumaMax = max(lumaMaxNwSw, lumaMaxNeSe);
  1879. half lumaMin = min(lumaMinNwSw, lumaMinNeSe);
  1880. /*--------------------------------------------------------------------------*/
  1881. // (9)
  1882. half4 rgby1;
  1883. rgby1.xy = dir1_pos.zw + dir1_pos.xy * fxaaConsoleRcpFrameOpt.zw;
  1884. rgby1 = h4tex2Dlod(tex, half4(rgby1.xy, 0.0, 0.0));
  1885. rgby1 = (temp1N + rgby1) * 0.5;
  1886. /*--------------------------------------------------------------------------*/
  1887. // (10)
  1888. half4 rgbyM = h4tex2Dlod(tex, half4(pos.xy, 0.0, 0.0));
  1889. #if (FXAA_GREEN_AS_LUMA == 0)
  1890. half lumaMaxM = max(lumaMax, rgbyM.w);
  1891. half lumaMinM = min(lumaMin, rgbyM.w);
  1892. #else
  1893. half lumaMaxM = max(lumaMax, rgbyM.y);
  1894. half lumaMinM = min(lumaMin, rgbyM.y);
  1895. #endif
  1896. /*--------------------------------------------------------------------------*/
  1897. // (11)
  1898. half4 temp2N;
  1899. temp2N.xy = dir2_pos.zw - dir2_pos.xy * fxaaConsoleRcpFrameOpt2.zw;
  1900. temp2N = h4tex2Dlod(tex, half4(temp2N.xy, 0.0, 0.0));
  1901. half4 rgby2;
  1902. rgby2.xy = dir2_pos.zw + dir2_pos.xy * fxaaConsoleRcpFrameOpt2.zw;
  1903. half lumaRangeM = (lumaMaxM - lumaMinM) / FXAA_CONSOLE__PS3_EDGE_THRESHOLD;
  1904. /*--------------------------------------------------------------------------*/
  1905. // (12)
  1906. rgby2 = h4tex2Dlod(tex, half4(rgby2.xy, 0.0, 0.0));
  1907. rgby2 = (temp2N + rgby2) * 0.5;
  1908. /*--------------------------------------------------------------------------*/
  1909. // (13)
  1910. rgby2 = (rgby2 + rgby1) * 0.5;
  1911. /*--------------------------------------------------------------------------*/
  1912. // (14)
  1913. #if (FXAA_GREEN_AS_LUMA == 0)
  1914. bool twoTapLt = rgby2.w < lumaMin;
  1915. bool twoTapGt = rgby2.w > lumaMax;
  1916. #else
  1917. bool twoTapLt = rgby2.y < lumaMin;
  1918. bool twoTapGt = rgby2.y > lumaMax;
  1919. #endif
  1920. bool earlyExit = lumaRangeM < lumaMax;
  1921. bool twoTap = twoTapLt || twoTapGt;
  1922. /*--------------------------------------------------------------------------*/
  1923. // (15)
  1924. if(twoTap) rgby2 = rgby1;
  1925. if(earlyExit) rgby2 = rgbyM;
  1926. /*--------------------------------------------------------------------------*/
  1927. return rgby2; }
  1928. /*==========================================================================*/
  1929. #endif