Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

FXAA3_11.hlsl 88KB

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