SMAA.h 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  1. /**
  2. * Copyright (C) 2011 Jorge Jimenez (jorge@iryoku.com)
  3. * Copyright (C) 2011 Belen Masia (bmasia@unizar.es)
  4. * Copyright (C) 2011 Jose I. Echevarria (joseignacioechevarria@gmail.com)
  5. * Copyright (C) 2011 Fernando Navarro (fernandn@microsoft.com)
  6. * Copyright (C) 2011 Diego Gutierrez (diegog@unizar.es)
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution:
  18. *
  19. * "Uses SMAA. Copyright (C) 2011 by Jorge Jimenez, Jose I. Echevarria,
  20. * Belen Masia, Fernando Navarro and Diego Gutierrez."
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
  23. * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  25. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
  26. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. * The views and conclusions contained in the software and documentation are
  35. * those of the authors and should not be interpreted as representing official
  36. * policies, either expressed or implied, of the copyright holders.
  37. */
  38. /**
  39. * _______ ___ ___ ___ ___
  40. * / || \/ | / \ / \
  41. * | (---- | \ / | / ^ \ / ^ \
  42. * \ \ | |\/| | / /_\ \ / /_\ \
  43. * ----) | | | | | / _____ \ / _____ \
  44. * |_______/ |__| |__| /__/ \__\ /__/ \__\
  45. *
  46. * E N H A N C E D
  47. * S U B P I X E L M O R P H O L O G I C A L A N T I A L I A S I N G
  48. *
  49. * http://www.iryoku.com/smaa/
  50. *
  51. * Hi, welcome aboard!
  52. *
  53. * Here you'll find instructions to get the shader up and running as fast as
  54. * possible.
  55. *
  56. * The shader has three passes, chained together as follows:
  57. *
  58. * |input|------------------·
  59. * v |
  60. * [ SMAA*EdgeDetection ] |
  61. * v |
  62. * |edgesTex| |
  63. * v |
  64. * [ SMAABlendingWeightCalculation ] |
  65. * v |
  66. * |blendTex| |
  67. * v |
  68. * [ SMAANeighborhoodBlending ] <------·
  69. * v
  70. * |output|
  71. *
  72. * Note that each [pass] has its own vertex and pixel shader.
  73. *
  74. * You've three edge detection methods to choose from: luma, color or depth.
  75. * They represent different quality/performance and anti-aliasing/sharpness
  76. * tradeoffs, so our recommendation is for you to choose the one that best
  77. * suits your particular scenario:
  78. *
  79. * - Depth edge detection is usually the fastest but it may miss some edges.
  80. *
  81. * - Luma edge detection is usually more expensive than depth edge detection,
  82. * but catches visible edges that depth edge detection can miss.
  83. *
  84. * - Color edge detection is usually the most expensive one but catches
  85. * chroma-only edges.
  86. *
  87. * For quickstarters: just use luma edge detection.
  88. *
  89. * The general advice is to not rush the integration process and ensure each
  90. * step is done correctly (don't try to integrate SMAA T2x with predicated edge
  91. * detection from the start!). Ok then, let's go!
  92. *
  93. * 1. The first step is to create two RGBA temporal framebuffers for holding
  94. * |edgesTex| and |blendTex|.
  95. *
  96. * In DX10, you can use a RG framebuffer for the edges texture, but in our
  97. * experience it yields worse performance.
  98. *
  99. * On the Xbox 360, you can use the same framebuffer for resolving both
  100. * |edgesTex| and |blendTex|, as they aren't needed simultaneously.
  101. *
  102. * 2. Both temporal framebuffers |edgesTex| and |blendTex| must be cleared
  103. * each frame. Do not forget to clear the alpha channel!
  104. *
  105. * 3. The next step is loading the two supporting precalculated textures,
  106. * 'areaTex' and 'searchTex'. You'll find them in the 'Textures' folder as
  107. * C++ headers, and also as regular DDS files. They'll be needed for the
  108. * 'SMAABlendingWeightCalculation' pass.
  109. *
  110. * If you use the C++ headers, be sure to load them in the format specified
  111. * inside of them.
  112. *
  113. * 4. In DX9, all samplers must be set to linear filtering and clamp, with the
  114. * exception of 'searchTex', which must be set to point filtering.
  115. *
  116. * 5. All texture reads and buffer writes must be non-sRGB, with the exception
  117. * of the input read and the output write of input in
  118. * 'SMAANeighborhoodBlending' (and only in this pass!). If sRGB reads in
  119. * this last pass are not possible, the technique will work anyway, but
  120. * will perform antialiasing in gamma space.
  121. *
  122. * IMPORTANT: for best results the input read for the color/luma edge
  123. * detection should *NOT* be sRGB.
  124. *
  125. * 6. Before including SMAA.h you'll have to setup the framebuffer pixel size,
  126. * the target and any optional configuration defines. Optionally you can
  127. * use a preset.
  128. *
  129. * You have three targets available:
  130. * SMAA_HLSL_3
  131. * SMAA_HLSL_4
  132. * SMAA_HLSL_4_1
  133. * SMAA_GLSL_3 *
  134. * SMAA_GLSL_4 *
  135. *
  136. * * (See SMAA_ONLY_COMPILE_VS below).
  137. *
  138. * And four presets:
  139. * SMAA_PRESET_LOW (%60 of the quality)
  140. * SMAA_PRESET_MEDIUM (%80 of the quality)
  141. * SMAA_PRESET_HIGH (%95 of the quality)
  142. * SMAA_PRESET_ULTRA (%99 of the quality)
  143. *
  144. * For example:
  145. * #define SMAA_PIXEL_SIZE float2(1.0 / 1280.0, 1.0 / 720.0)
  146. * #define SMAA_HLSL_4 1
  147. * #define SMAA_PRESET_HIGH 1
  148. * #include "SMAA.h"
  149. *
  150. * 7. Then, you'll have to setup the passes as indicated in the scheme above.
  151. * You can take a look into SMAA.fx, to see how we did it for our demo.
  152. * Checkout the function wrappers, you may want to copy-paste them!
  153. *
  154. * 8. It's recommended to validate the produced |edgesTex| and |blendTex|.
  155. * It's advised to not continue with the implementation until both buffers
  156. * are verified to produce identical results to our reference demo.
  157. *
  158. * 9. After you get the last pass to work, it's time to optimize. You'll have
  159. * to initialize a stencil buffer in the first pass (discard is already in
  160. * the code), then mask execution by using it the second pass. The last
  161. * pass should be executed in all pixels.
  162. *
  163. *
  164. * After this point you can choose to enable predicated thresholding,
  165. * temporal supersampling and motion blur integration:
  166. *
  167. * a) If you want to use predicated thresholding, take a look into
  168. * SMAA_PREDICATION; you'll need to pass an extra texture in the edge
  169. * detection pass.
  170. *
  171. * b) If you want to enable temporal supersampling:
  172. *
  173. * 1. The first step is to render using subpixel jitters. I won't go into
  174. * detail, but it's as simple as moving each vertex position in the
  175. * vertex shader, you can check how we do it in our DX10 demo.
  176. *
  177. * 2. Then, you must setup the temporal resolve. You may want to take a look
  178. * into SMAAResolve for resolving 2x modes. After you get it working, you'll
  179. * probably see ghosting everywhere. But fear not, you can enable the
  180. * CryENGINE temporal reprojection by setting the SMAA_REPROJECTION macro.
  181. *
  182. * 3. The next step is to apply SMAA to each subpixel jittered frame, just as
  183. * done for 1x.
  184. *
  185. * 4. At this point you should already have something usable, but for best
  186. * results the proper area textures must be set depending on current jitter.
  187. * For this, the parameter 'subsampleIndices' of
  188. * 'SMAABlendingWeightCalculationPS' must be set as follows, for our T2x
  189. * mode:
  190. *
  191. * @SUBSAMPLE_INDICES
  192. *
  193. * | jitter | subsampleIndices |
  194. * +------------------------+------------------------|
  195. * | float2(-0.25f, 0.25f) | int4(1, 1, 1, 0) |
  196. * | float2( 0.25f, -0.25f) | int4(2, 2, 2, 0) |
  197. * OR
  198. * +------------------------+------------------------|
  199. * | float2( 0.25f, 0.25f) | int4(2, 1, 0, 2) |
  200. * | float2(-0.25f, -0.25f) | int4(1, 2, 0, 1) |
  201. *
  202. * These jitter positions assume a bottom-to-top y axis.
  203. *
  204. * More information about temporal supersampling here:
  205. * http://iryoku.com/aacourse/downloads/13-Anti-Aliasing-Methods-in-CryENGINE-3.pdf
  206. *
  207. * c) If motion blur is used, you may want to do the edge detection pass
  208. * together with motion blur. This has two advantages:
  209. *
  210. * 1. Pixels under heavy motion can be omitted from the edge detection process.
  211. * For these pixels we can just store "no edge", as motion blur will take
  212. * care of them.
  213. * 2. The center pixel tap is reused.
  214. *
  215. * Note that in this case depth testing should be used instead of stenciling,
  216. * as we have to write all the pixels in the motion blur pass.
  217. *
  218. * That's it!
  219. */
  220. //-----------------------------------------------------------------------------
  221. // SMAA Presets
  222. #if SMAA_PRESET_LOW == 1
  223. #define SMAA_THRESHOLD 0.15
  224. #define SMAA_MAX_SEARCH_STEPS 4
  225. #define SMAA_MAX_SEARCH_STEPS_DIAG 0
  226. #define SMAA_CORNER_ROUNDING 100
  227. #elif SMAA_PRESET_MEDIUM == 1
  228. #define SMAA_THRESHOLD 0.1
  229. #define SMAA_MAX_SEARCH_STEPS 8
  230. #define SMAA_MAX_SEARCH_STEPS_DIAG 0
  231. #define SMAA_CORNER_ROUNDING 100
  232. #elif SMAA_PRESET_HIGH == 1
  233. #define SMAA_THRESHOLD 0.1
  234. #define SMAA_MAX_SEARCH_STEPS 16
  235. #define SMAA_MAX_SEARCH_STEPS_DIAG 8
  236. #define SMAA_CORNER_ROUNDING 25
  237. #elif SMAA_PRESET_ULTRA == 1
  238. #define SMAA_THRESHOLD 0.05
  239. #define SMAA_MAX_SEARCH_STEPS 32
  240. #define SMAA_MAX_SEARCH_STEPS_DIAG 16
  241. #define SMAA_CORNER_ROUNDING 25
  242. #endif
  243. //-----------------------------------------------------------------------------
  244. // Configurable Defines
  245. /**
  246. * SMAA_THRESHOLD specifies the threshold or sensitivity to edges.
  247. * Lowering this value you will be able to detect more edges at the expense of
  248. * performance.
  249. *
  250. * Range: [0, 0.5]
  251. * 0.1 is a reasonable value, and allows to catch most visible edges.
  252. * 0.05 is a rather overkill value, that allows to catch 'em all.
  253. *
  254. * If temporal supersampling is used, 0.2 could be a reasonable value, as low
  255. * contrast edges are properly filtered by just 2x.
  256. */
  257. #ifndef SMAA_THRESHOLD
  258. #define SMAA_THRESHOLD 0.1
  259. #endif
  260. /**
  261. * SMAA_DEPTH_THRESHOLD specifies the threshold for depth edge detection.
  262. *
  263. * Range: depends on the depth range of the scene.
  264. */
  265. #ifndef SMAA_DEPTH_THRESHOLD
  266. #define SMAA_DEPTH_THRESHOLD (0.1 * SMAA_THRESHOLD)
  267. #endif
  268. /**
  269. * SMAA_MAX_SEARCH_STEPS specifies the maximum steps performed in the
  270. * horizontal/vertical pattern searches, at each side of the pixel.
  271. *
  272. * In number of pixels, it's actually the double. So the maximum line length
  273. * perfectly handled by, for example 16, is 64 (by perfectly, we meant that
  274. * longer lines won't look as good, but still antialiased).
  275. *
  276. * Range: [0, 98]
  277. */
  278. #ifndef SMAA_MAX_SEARCH_STEPS
  279. #define SMAA_MAX_SEARCH_STEPS 16
  280. #endif
  281. /**
  282. * SMAA_MAX_SEARCH_STEPS_DIAG specifies the maximum steps performed in the
  283. * diagonal pattern searches, at each side of the pixel. In this case we jump
  284. * one pixel at time, instead of two.
  285. *
  286. * Range: [0, 20]; set it to 0 to disable diagonal processing.
  287. *
  288. * On high-end machines it is cheap (between a 0.8x and 0.9x slower for 16
  289. * steps), but it can have a significant impact on older machines.
  290. */
  291. #ifndef SMAA_MAX_SEARCH_STEPS_DIAG
  292. #define SMAA_MAX_SEARCH_STEPS_DIAG 8
  293. #endif
  294. /**
  295. * SMAA_CORNER_ROUNDING specifies how much sharp corners will be rounded.
  296. *
  297. * Range: [0, 100]; set it to 100 to disable corner detection.
  298. */
  299. #ifndef SMAA_CORNER_ROUNDING
  300. #define SMAA_CORNER_ROUNDING 25
  301. #endif
  302. /**
  303. * Predicated thresholding allows to better preserve texture details and to
  304. * improve performance, by decreasing the number of detected edges using an
  305. * additional buffer like the light accumulation buffer, object ids or even the
  306. * depth buffer (the depth buffer usage may be limited to indoor or short range
  307. * scenes).
  308. *
  309. * It locally decreases the luma or color threshold if an edge is found in an
  310. * additional buffer (so the global threshold can be higher).
  311. *
  312. * This method was developed by Playstation EDGE MLAA team, and used in
  313. * Killzone 3, by using the light accumulation buffer. More information here:
  314. * http://iryoku.com/aacourse/downloads/06-MLAA-on-PS3.pptx
  315. */
  316. #ifndef SMAA_PREDICATION
  317. #define SMAA_PREDICATION 0
  318. #endif
  319. /**
  320. * Threshold to be used in the additional predication buffer.
  321. *
  322. * Range: depends on the input, so you'll have to find the magic number that
  323. * works for you.
  324. */
  325. #ifndef SMAA_PREDICATION_THRESHOLD
  326. #define SMAA_PREDICATION_THRESHOLD 0.01
  327. #endif
  328. /**
  329. * How much to scale the global threshold used for luma or color edge
  330. * detection when using predication.
  331. *
  332. * Range: [1, 5]
  333. */
  334. #ifndef SMAA_PREDICATION_SCALE
  335. #define SMAA_PREDICATION_SCALE 2.0
  336. #endif
  337. /**
  338. * How much to locally decrease the threshold.
  339. *
  340. * Range: [0, 1]
  341. */
  342. #ifndef SMAA_PREDICATION_STRENGTH
  343. #define SMAA_PREDICATION_STRENGTH 0.4
  344. #endif
  345. /**
  346. * Temporal reprojection allows to remove ghosting artifacts when using
  347. * temporal supersampling. We use the CryEngine 3 method which also introduces
  348. * velocity weighting. This feature is of extreme importance for totally
  349. * removing ghosting. More information here:
  350. * http://iryoku.com/aacourse/downloads/13-Anti-Aliasing-Methods-in-CryENGINE-3.pdf
  351. *
  352. * Note that you'll need to setup a velocity buffer for enabling reprojection.
  353. * For static geometry, saving the previous depth buffer is a viable
  354. * alternative.
  355. */
  356. #ifndef SMAA_REPROJECTION
  357. #define SMAA_REPROJECTION 0
  358. #endif
  359. /**
  360. * SMAA_REPROJECTION_WEIGHT_SCALE controls the velocity weighting. It allows to
  361. * remove ghosting trails behind the moving object, which are not removed by
  362. * just using reprojection. Using low values will exhibit ghosting, while using
  363. * high values will disable temporal supersampling under motion.
  364. *
  365. * Behind the scenes, velocity weighting removes temporal supersampling when
  366. * the velocity of the subsamples differs (meaning they are different objects).
  367. *
  368. * Range: [0, 80]
  369. */
  370. #define SMAA_REPROJECTION_WEIGHT_SCALE 30.0
  371. /**
  372. * In the last pass we leverage bilinear filtering to avoid some lerps.
  373. * However, bilinear filtering is done in gamma space in DX9, under DX9
  374. * hardware (but not in DX9 code running on DX10 hardware), which gives
  375. * inaccurate results.
  376. *
  377. * So, if you are in DX9, under DX9 hardware, and do you want accurate linear
  378. * blending, you must set this flag to 1.
  379. *
  380. * It's ignored when using SMAA_HLSL_4, and of course, only has sense when
  381. * using sRGB read and writes on the last pass.
  382. */
  383. #ifndef SMAA_DIRECTX9_LINEAR_BLEND
  384. #define SMAA_DIRECTX9_LINEAR_BLEND 0
  385. #endif
  386. /**
  387. * On ATI compilers, discard cannot be used in vertex shaders. Thus, they need
  388. * to be compiled separately. These macros allow to easily accomplish it.
  389. */
  390. #ifndef SMAA_ONLY_COMPILE_VS
  391. #define SMAA_ONLY_COMPILE_VS 0
  392. #endif
  393. #ifndef SMAA_ONLY_COMPILE_PS
  394. #define SMAA_ONLY_COMPILE_PS 0
  395. #endif
  396. //-----------------------------------------------------------------------------
  397. // Non-Configurable Defines
  398. #ifndef SMAA_AREATEX_MAX_DISTANCE
  399. #define SMAA_AREATEX_MAX_DISTANCE 16
  400. #endif
  401. #ifndef SMAA_AREATEX_MAX_DISTANCE_DIAG
  402. #define SMAA_AREATEX_MAX_DISTANCE_DIAG 20
  403. #endif
  404. #define SMAA_AREATEX_PIXEL_SIZE (1.0 / float2(160.0, 240.0))
  405. #define SMAA_AREATEX_SUBTEX_SIZE (1.0 / 3.0)
  406. //-----------------------------------------------------------------------------
  407. // Porting Functions
  408. #if SMAA_HLSL_3 == 1
  409. #define SMAATexture2D sampler2D
  410. //#define SMAASampleLevelZero(tex, coord) tex2Dlod(tex, float4(coord, 0.0, 0.0))
  411. #define SMAASampleLevelZero(tex, coord) tex2Dlod(tex, float4(coord, coord)) //CJ nice reduction of ALU
  412. //#define SMAASampleLevelZeroPoint(tex, coord) tex2Dlod(tex, float4(coord, 0.0, 0.0))
  413. #define SMAASampleLevelZeroPoint(tex, coord) tex2Dlod(tex, float4(coord, coord)) //CJ nice reduction of ALU
  414. #define SMAASample(tex, coord) tex2D(tex, coord)
  415. #define SMAASamplePoint(tex, coord) tex2D(tex, coord)
  416. //#define SMAASampleLevelZeroOffset(tex, coord, offset) tex2Dlod(tex, float4(coord + offset * SMAA_PIXEL_SIZE, 0.0, 0.0))
  417. #define SMAASampleLevelZeroOffset(tex, coord, offset) tex2Dlod(tex, float4(coord + offset * SMAA_PIXEL_SIZE,coord + offset * SMAA_PIXEL_SIZE)) //CJ nice reduction of ALU
  418. #define SMAASampleOffset(tex, coord, offset) tex2D(tex, coord + offset * SMAA_PIXEL_SIZE)
  419. #define SMAALerp(a, b, t) lerp(a, b, t)
  420. #define SMAASaturate(a) saturate(a)
  421. #define SMAAMad(a, b, c) mad(a, b, c)
  422. #define SMAA_FLATTEN [flatten]
  423. #define SMAA_BRANCH [branch]
  424. #endif
  425. #if SMAA_HLSL_4 == 1 || SMAA_HLSL_4_1 == 1
  426. SamplerState LinearSampler { Filter = MIN_MAG_LINEAR_MIP_POINT; AddressU = Clamp; AddressV = Clamp; };
  427. SamplerState PointSampler { Filter = MIN_MAG_MIP_POINT; AddressU = Clamp; AddressV = Clamp; };
  428. #define SMAATexture2D Texture2D
  429. //#define SMAASampleLevelZero(tex, coord) tex.SampleLevel(LinearSampler, coord, 0)
  430. #define SMAASampleLevelZero(tex, coord) tex.SampleLevel(LinearSampler, coord, coord.x) //CJ
  431. //#define SMAASampleLevelZeroPoint(tex, coord) tex.SampleLevel(PointSampler, coord, 0)
  432. #define SMAASampleLevelZeroPoint(tex, coord) tex.SampleLevel(PointSampler, coord, coord.x) //CJ
  433. //#define SMAASample(tex, coord) SMAASampleLevelZero(tex, coord)
  434. #define SMAASample(tex, coord) tex.Sample(LinearSampler, coord) //CJ
  435. //#define SMAASamplePoint(tex, coord) SMAASampleLevelZeroPoint(tex, coord)
  436. #define SMAASamplePoint(tex, coord) tex.Sample(PointSampler, coord) //CJ
  437. //#define SMAASampleLevelZeroOffset(tex, coord, offset) tex.SampleLevel(LinearSampler, coord, 0, offset)
  438. #define SMAASampleLevelZeroOffset(tex, coord, offset) tex.SampleLevel(LinearSampler, coord, coord.x, offset) //CJ
  439. //#define SMAASampleOffset(tex, coord, offset) SMAASampleLevelZeroOffset(tex, coord, offset)
  440. #define SMAASampleOffset(tex, coord, offset) tex.Sample(LinearSampler, coord, offset) //CJ
  441. #define SMAALerp(a, b, t) lerp(a, b, t)
  442. #define SMAASaturate(a) saturate(a)
  443. #define SMAAMad(a, b, c) mad(a, b, c)
  444. #define SMAA_FLATTEN [flatten]
  445. #define SMAA_BRANCH [branch]
  446. #endif
  447. #if SMAA_HLSL_4_1 == 1
  448. #define SMAAGather(tex, coord) tex.Gather(LinearSampler, coord, 0)
  449. #endif
  450. #if SMAA_GLSL_3 == 1 || SMAA_GLSL_4 == 1
  451. #define SMAATexture2D sampler2D
  452. #define SMAASampleLevelZero(tex, coord) textureLod(tex, coord, 0.0)
  453. #define SMAASampleLevelZeroPoint(tex, coord) textureLod(tex, coord, 0.0)
  454. #define SMAASample(tex, coord) texture(tex, coord)
  455. #define SMAASamplePoint(tex, coord) texture(tex, coord)
  456. #define SMAASampleLevelZeroOffset(tex, coord, offset) textureLodOffset(tex, coord, 0.0, offset)
  457. #define SMAASampleOffset(tex, coord, offset) texture(tex, coord, offset)
  458. #define SMAALerp(a, b, t) mix(a, b, t)
  459. #define SMAASaturate(a) clamp(a, 0.0, 1.0)
  460. #define SMAA_FLATTEN
  461. #define SMAA_BRANCH
  462. #define float2 vec2
  463. #define float3 vec3
  464. #define float4 vec4
  465. #define int2 ivec2
  466. #define int3 ivec3
  467. #define int4 ivec4
  468. #endif
  469. #if SMAA_GLSL_3 == 1
  470. #define SMAAMad(a, b, c) (a * b + c)
  471. #endif
  472. #if SMAA_GLSL_4 == 1
  473. #define SMAAMad(a, b, c) fma(a, b, c)
  474. #define SMAAGather(tex, coord) textureGather(tex, coord)
  475. #endif
  476. //-----------------------------------------------------------------------------
  477. // Misc functions
  478. /**
  479. * Gathers current pixel, and the top-left neighbors.
  480. */
  481. float3 SMAAGatherNeighbours(float2 texcoord,
  482. float4 offset[3],
  483. SMAATexture2D tex) {
  484. #if SMAA_HLSL_4_1 == 1
  485. return SMAAGather(tex, texcoord + SMAA_PIXEL_SIZE * float2(-0.5, -0.5)).grb;
  486. #else
  487. float P = SMAASample(tex, texcoord).r;
  488. float Pleft = SMAASample(tex, offset[0].xy).r;
  489. float Ptop = SMAASample(tex, offset[0].zw).r;
  490. return float3(P, Pleft, Ptop);
  491. #endif
  492. }
  493. /**
  494. * Adjusts the threshold by means of predication.
  495. */
  496. float2 SMAACalculatePredicatedThreshold(float2 texcoord,
  497. float4 offset[3],
  498. SMAATexture2D colorTex,
  499. SMAATexture2D predicationTex) {
  500. float3 neighbours = SMAAGatherNeighbours(texcoord, offset, predicationTex);
  501. float2 delta = abs(neighbours.xx - float2(neighbours.y, neighbours.z));
  502. float2 edges = step(SMAA_PREDICATION_THRESHOLD, delta);
  503. return SMAA_PREDICATION_SCALE * SMAA_THRESHOLD * (1.0 - SMAA_PREDICATION_STRENGTH * edges);
  504. }
  505. #if SMAA_ONLY_COMPILE_PS == 0
  506. //-----------------------------------------------------------------------------
  507. // Vertex Shaders
  508. /**
  509. * Edge Detection Vertex Shader
  510. */
  511. void SMAAEdgeDetectionVS(float4 position,
  512. out float4 svPosition,
  513. inout float2 texcoord,
  514. out float4 offset[3]) {
  515. svPosition = position;
  516. offset[0] = texcoord.xyxy + SMAA_PIXEL_SIZE.xyxy * float4(-1.0, 0.0, 0.0, -1.0);
  517. offset[1] = texcoord.xyxy + SMAA_PIXEL_SIZE.xyxy * float4( 1.0, 0.0, 0.0, 1.0);
  518. offset[2] = texcoord.xyxy + SMAA_PIXEL_SIZE.xyxy * float4(-2.0, 0.0, 0.0, -2.0);
  519. }
  520. /**
  521. * Blend Weight Calculation Vertex Shader
  522. */
  523. void SMAABlendWeightCalculationVS(float4 position,
  524. out float4 svPosition,
  525. inout float2 texcoord,
  526. out float2 pixcoord,
  527. out float4 offset[3]) {
  528. svPosition = position;
  529. pixcoord = texcoord / SMAA_PIXEL_SIZE;
  530. // We will use these offsets for the searches later on (see @PSEUDO_GATHER4):
  531. offset[0] = texcoord.xyxy + SMAA_PIXEL_SIZE.xyxy * float4(-0.25, -0.125, 1.25, -0.125);
  532. offset[1] = texcoord.xyxy + SMAA_PIXEL_SIZE.xyxy * float4(-0.125, -0.25, -0.125, 1.25);
  533. // And these for the searches, they indicate the ends of the loops:
  534. offset[2] = float4(offset[0].xz, offset[1].yw) +
  535. float4(-2.0, 2.0, -2.0, 2.0) *
  536. SMAA_PIXEL_SIZE.xxyy * float(SMAA_MAX_SEARCH_STEPS);
  537. }
  538. /**
  539. * Neighborhood Blending Vertex Shader
  540. */
  541. void SMAANeighborhoodBlendingVS(float4 position,
  542. out float4 svPosition,
  543. inout float2 texcoord,
  544. out float4 offset[2]) {
  545. svPosition = position;
  546. offset[0] = texcoord.xyxy + SMAA_PIXEL_SIZE.xyxy * float4(-1.0, 0.0, 0.0, -1.0);
  547. offset[1] = texcoord.xyxy + SMAA_PIXEL_SIZE.xyxy * float4( 1.0, 0.0, 0.0, 1.0);
  548. }
  549. /**
  550. * Resolve Vertex Shader
  551. */
  552. void SMAAResolveVS(float4 position,
  553. out float4 svPosition,
  554. inout float2 texcoord) {
  555. svPosition = position;
  556. }
  557. #endif // SMAA_ONLY_COMPILE_PS == 0
  558. #if SMAA_ONLY_COMPILE_VS == 0
  559. //-----------------------------------------------------------------------------
  560. // Edge Detection Pixel Shaders (First Pass)
  561. /**
  562. * Luma Edge Detection
  563. *
  564. * IMPORTANT NOTICE: luma edge detection requires gamma-corrected colors, and
  565. * thus 'colorTex' should be a non-sRGB texture.
  566. */
  567. float4 SMAALumaEdgeDetectionPS(float2 texcoord,
  568. float4 offset[3],
  569. SMAATexture2D colorTex
  570. #if SMAA_PREDICATION == 1
  571. , SMAATexture2D predicationTex
  572. #endif
  573. ) {
  574. // Calculate the threshold:
  575. #if SMAA_PREDICATION == 1
  576. float2 threshold = SMAACalculatePredicatedThreshold(texcoord, offset, colorTex, predicationTex);
  577. #else
  578. float2 threshold = float2(SMAA_THRESHOLD, SMAA_THRESHOLD);
  579. #endif
  580. // Calculate lumas:
  581. float3 weights = float3(0.2126, 0.7152, 0.0722);
  582. float L = dot(SMAASample(colorTex, texcoord).rgb, weights);
  583. float Lleft = dot(SMAASample(colorTex, offset[0].xy).rgb, weights);
  584. float Ltop = dot(SMAASample(colorTex, offset[0].zw).rgb, weights);
  585. // We do the usual threshold:
  586. float4 delta;
  587. delta.xy = abs(L - float2(Lleft, Ltop));
  588. float2 edges = step(threshold, delta.xy);
  589. // Then discard if there is no edge:
  590. //if (dot(edges, float2(1.0, 1.0)) == 0.0)
  591. //SMAA_BRANCH if ((edges.x + edges.y) == 0.0) //CJ
  592. SMAA_BRANCH if (edges.x == -edges.y) //CJ
  593. {
  594. discard;
  595. }
  596. else //CJ
  597. { //CJ
  598. // Calculate right and bottom deltas:
  599. float Lright = dot(SMAASample(colorTex, offset[1].xy).rgb, weights);
  600. float Lbottom = dot(SMAASample(colorTex, offset[1].zw).rgb, weights);
  601. delta.zw = abs(L - float2(Lright, Lbottom));
  602. /*
  603. // Calculate the maximum delta in the direct neighborhood:
  604. float2 maxDelta = max(delta.xy, delta.zw);
  605. maxDelta = max(maxDelta.xx, maxDelta.yy);
  606. */
  607. // Calculate the maximum delta in the direct neighborhood: //CJ
  608. float2 maxDelta = max(delta.xy, delta.zw);
  609. maxDelta.x = max(maxDelta.x, maxDelta.y);
  610. /*
  611. // Calculate the maximum delta in the direct neighborhood:
  612. float2 maxDelta;
  613. maxDelta.x = max(delta.x, delta.y);
  614. maxDelta.y = max(delta.z, delta.w);
  615. maxDelta.x = max(maxDelta.x, maxDelta.y);
  616. */
  617. if (!any(abs(maxDelta.xx) - abs(2.0 * delta.xy)))
  618. //if ((2.0 * delta.x) > maxDelta.x || (2.0 * delta.y) > maxDelta.x)
  619. {
  620. // Calculate left-left and top-top deltas:
  621. float Lleftleft = dot(SMAASample(colorTex, offset[2].xy).rgb, weights);
  622. float Ltoptop = dot(SMAASample(colorTex, offset[2].zw).rgb, weights);
  623. delta.zw = abs(float2(Lleft, Ltop) - float2(Lleftleft, Ltoptop));
  624. /*
  625. // Calculate the final maximum delta:
  626. maxDelta = max(maxDelta.xy, delta.zw);
  627. */
  628. // Calculate the final maximum delta:
  629. maxDelta = max(maxDelta.xx, delta.zw); //CJ
  630. } //CJ
  631. /**
  632. * Each edge with a delta in luma of less than 50% of the maximum luma
  633. * surrounding this pixel is discarded. This allows to eliminate spurious
  634. * crossing edges, and is based on the fact that, if there is too much
  635. * contrast in a direction, that will hide contrast in the other
  636. * neighbors.
  637. * This is done after the discard intentionally as this situation doesn't
  638. * happen too frequently (but it's important to do as it prevents some
  639. * edges from going undetected).
  640. */
  641. //edges.xy *= step(0.5 * maxDelta, delta.xy);
  642. edges.xy *= step(maxDelta, 2.0 * delta.xy); //CJ
  643. } //CJ
  644. return float4(edges, 0.0, 0.0);
  645. }
  646. /**
  647. * Color Edge Detection
  648. *
  649. * IMPORTANT NOTICE: color edge detection requires gamma-corrected colors, and
  650. * thus 'colorTex' should be a non-sRGB texture.
  651. */
  652. float4 SMAAColorEdgeDetectionPS(float2 texcoord,
  653. float4 offset[3],
  654. SMAATexture2D colorTex
  655. #if SMAA_PREDICATION == 1
  656. , SMAATexture2D predicationTex
  657. #endif
  658. ) {
  659. // Calculate the threshold:
  660. #if SMAA_PREDICATION == 1
  661. float2 threshold = SMAACalculatePredicatedThreshold(texcoord, offset, colorTex, predicationTex);
  662. #else
  663. float2 threshold = float2(SMAA_THRESHOLD, SMAA_THRESHOLD);
  664. #endif
  665. // Calculate color deltas:
  666. float4 delta;
  667. float3 C = SMAASample(colorTex, texcoord).rgb;
  668. float3 Cleft = SMAASample(colorTex, offset[0].xy).rgb;
  669. float3 t = abs(C - Cleft);
  670. delta.x = max(max(t.r, t.g), t.b);
  671. float3 Ctop = SMAASample(colorTex, offset[0].zw).rgb;
  672. t = abs(C - Ctop);
  673. delta.y = max(max(t.r, t.g), t.b);
  674. // We do the usual threshold:
  675. float2 edges = step(threshold, delta.xy);
  676. // Then discard if there is no edge:
  677. //if (dot(edges, float2(1.0, 1.0)) == 0.0)
  678. if ((edges.x + edges.y) == 0.0) //CJ - branching here seems to gain more speed than is lost by the overhead of branching. This would depend on the scene though.
  679. //SMAA_BRANCH if (edges.x == -edges.y)
  680. {
  681. discard;
  682. }
  683. else //CJ
  684. { //CJ
  685. // Calculate right and bottom deltas:
  686. float3 Cright = SMAASample(colorTex, offset[1].xy).rgb;
  687. t = abs(C - Cright);
  688. delta.z = max(max(t.r, t.g), t.b);
  689. float3 Cbottom = SMAASample(colorTex, offset[1].zw).rgb;
  690. t = abs(C - Cbottom);
  691. delta.w = max(max(t.r, t.g), t.b);
  692. // Calculate the maximum delta in the direct neighborhood:
  693. float maxDelta = max(max(max(delta.x, delta.y), delta.z), delta.w);
  694. // Calculate left-left and top-top deltas:
  695. float3 Cleftleft = SMAASample(colorTex, offset[2].xy).rgb;
  696. t = abs(C - Cleftleft);
  697. delta.z = max(max(t.r, t.g), t.b);
  698. float3 Ctoptop = SMAASample(colorTex, offset[2].zw).rgb;
  699. t = abs(C - Ctoptop);
  700. delta.w = max(max(t.r, t.g), t.b);
  701. // Calculate the final maximum delta:
  702. maxDelta = max(max(maxDelta, delta.z), delta.w);
  703. // Local contrast adaptation in action:
  704. //edges.xy *= step(0.5 * maxDelta, delta.xy);
  705. edges.xy *= step(maxDelta, delta.xy * 2.0); //CJ a little faster
  706. } //CJ
  707. return float4(edges, 0.0, 0.0);
  708. }
  709. /**
  710. * Depth Edge Detection
  711. */
  712. float4 SMAADepthEdgeDetectionPS(float2 texcoord,
  713. float4 offset[3],
  714. SMAATexture2D depthTex) {
  715. float3 neighbours = SMAAGatherNeighbours(texcoord, offset, depthTex);
  716. float2 delta = abs(neighbours.xx - float2(neighbours.y, neighbours.z));
  717. float2 edges = step(SMAA_DEPTH_THRESHOLD, delta);
  718. //if (dot(edges, float2(1.0, 1.0)) == 0.0)
  719. //if ((edges.x + edges.y) == 0.0) //CJ
  720. if (edges.x == -edges.y) //CJ
  721. discard;
  722. return float4(edges, 0.0, 0.0);
  723. }
  724. //-----------------------------------------------------------------------------
  725. // Diagonal Search Functions
  726. #if SMAA_MAX_SEARCH_STEPS_DIAG > 0 || SMAA_FORCE_DIAGONAL_DETECTION == 1
  727. /**
  728. * These functions allows to perform diagonal pattern searches.
  729. */
  730. /*
  731. float SMAASearchDiag1(SMAATexture2D edgesTex, float2 texcoord, float2 dir, float c) {
  732. //texcoord += dir * SMAA_PIXEL_SIZE;
  733. texcoord = dir * SMAA_PIXEL_SIZE + texcoord; //CJ - no change
  734. float2 e = float2(0.0, 0.0);
  735. float i;
  736. for (i = 0.0; i < float(SMAA_MAX_SEARCH_STEPS_DIAG); i++) {
  737. e.rg = SMAASampleLevelZero(edgesTex, texcoord).rg;
  738. //SMAA_FLATTEN if (dot(e, float2(1.0, 1.0)) < 1.9) break;
  739. SMAA_FLATTEN if ((e.r + e.g) < 1.9) break; //CJ fewer amd ASM
  740. texcoord = dir * SMAA_PIXEL_SIZE + texcoord; //CJ - no change
  741. }
  742. return float(e.g > 0.9) * c + i; //CJ no change
  743. }
  744. */
  745. float SMAASearchDiag1(SMAATexture2D edgesTex, float2 texcoord, float2 dir, float c) { //CJ
  746. float2 e = float2(0.0, 0.0);
  747. float i = 0.0;
  748. float2 thisway = dir * SMAA_PIXEL_SIZE;
  749. texcoord = thisway + texcoord; //CJ - no change
  750. [fastopt] //not glsl compatible I think
  751. while (i < float(SMAA_MAX_SEARCH_STEPS_DIAG)) { //hmm I'm not sure GLSL can do while loops - also a do while loop might be better
  752. e.rg = SMAASampleLevelZero(edgesTex, texcoord).rg;
  753. SMAA_FLATTEN if ((e.r + e.g) < 1.9) break; //CJ fewer amd ASM - no fewer cycles
  754. texcoord = thisway + texcoord; //CJ - no change
  755. i++;
  756. }
  757. return float(e.g > 0.9) * c + i; //CJ no change
  758. }
  759. float SMAASearchDiag2(SMAATexture2D edgesTex, float2 texcoord, float2 dir, float c) {
  760. texcoord += dir * SMAA_PIXEL_SIZE;
  761. float2 e = float2(0.0, 0.0);
  762. float i;
  763. for (i = 0.0; i < float(SMAA_MAX_SEARCH_STEPS_DIAG); i++) {
  764. e.g = SMAASampleLevelZero(edgesTex, texcoord).g;
  765. e.r = SMAASampleLevelZeroOffset(edgesTex, texcoord, int2(1, 0)).r;
  766. //SMAA_FLATTEN if (dot(e, float2(1.0, 1.0)) < 1.9) break;
  767. SMAA_FLATTEN if ((e.r + e.g) < 1.9) break; //CJ fewer amd ASM - no fewer cycles,but possible performance gain - check
  768. texcoord = dir * SMAA_PIXEL_SIZE + texcoord; //CJ - no change
  769. }
  770. return i + float(e.g > 0.9) * c;
  771. }
  772. /**
  773. * Similar to SMAAArea, this calculates the area corresponding to a certain
  774. * diagonal distance and crossing edges 'e'.
  775. */
  776. float2 SMAAAreaDiag(SMAATexture2D areaTex, float2 dist, float2 e, float offset) {
  777. float2 texcoord = float(SMAA_AREATEX_MAX_DISTANCE_DIAG) * e + dist;
  778. // We do a scale and bias for mapping to texel space:
  779. texcoord = SMAA_AREATEX_PIXEL_SIZE * texcoord + (0.5 * SMAA_AREATEX_PIXEL_SIZE);
  780. // Diagonal areas are on the second half of the texture:
  781. texcoord.x += 0.5;
  782. // Move to proper place, according to the subpixel offset:
  783. texcoord.y += SMAA_AREATEX_SUBTEX_SIZE * offset;
  784. // Do it!
  785. #if SMAA_HLSL_3 == 1
  786. return SMAASampleLevelZero(areaTex, texcoord).ra;
  787. #else
  788. return SMAASampleLevelZero(areaTex, texcoord).rg;
  789. #endif
  790. }
  791. /**
  792. * This searches for diagonal patterns and returns the corresponding weights.
  793. */
  794. float2 SMAACalculateDiagWeights(SMAATexture2D edgesTex, SMAATexture2D areaTex, float2 texcoord, float2 e, int4 subsampleIndices) {
  795. float2 weights = float2(0.0, 0.0);
  796. float2 d;
  797. d.x = e.r > 0.0? SMAASearchDiag1(edgesTex, texcoord, float2(-1.0, 1.0), 1.0) : 0.0; //CJ TODO try e.r ? instead of e.r > 0.0?
  798. d.y = SMAASearchDiag1(edgesTex, texcoord, float2(1.0, -1.0), 0.0);
  799. SMAA_BRANCH
  800. if (d.r + d.g > 2.0) { // d.r + d.g + 1 > 3
  801. float4 coords = SMAAMad(float4(-d.r, d.r, d.g, -d.g), SMAA_PIXEL_SIZE.xyxy, texcoord.xyxy);
  802. float4 c;
  803. c.x = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2(-1, 0)).g;
  804. c.y = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2( 0, 0)).r;
  805. c.z = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1, 0)).g;
  806. c.w = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1, -1)).r;
  807. float2 e = 2.0 * c.xz + c.yw;
  808. float t = float(SMAA_MAX_SEARCH_STEPS_DIAG) - 1.0;
  809. e *= step(d.rg, float2(t, t));
  810. weights += SMAAAreaDiag(areaTex, d, e, float(subsampleIndices.z));
  811. }
  812. d.x = SMAASearchDiag2(edgesTex, texcoord, float2(-1.0, -1.0), 0.0);
  813. float right = SMAASampleLevelZeroOffset(edgesTex, texcoord, int2(1, 0)).r;
  814. d.y = right > 0.0? SMAASearchDiag2(edgesTex, texcoord, float2(1.0, 1.0), 1.0) : 0.0;
  815. SMAA_BRANCH
  816. if (d.r + d.g > 2.0) { // d.r + d.g + 1 > 3
  817. float4 coords = SMAAMad(float4(-d.r, -d.r, d.g, d.g), SMAA_PIXEL_SIZE.xyxy, texcoord.xyxy);
  818. float4 c;
  819. c.x = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2(-1, 0)).g;
  820. c.y = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2( 0, -1)).r;
  821. c.zw = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1, 0)).gr;
  822. float2 e = 2.0 * c.xz + c.yw;
  823. float t = float(SMAA_MAX_SEARCH_STEPS_DIAG) - 1.0;
  824. e *= step(d.rg, float2(t, t));
  825. weights += SMAAAreaDiag(areaTex, d, e, float(subsampleIndices.w)).gr;
  826. }
  827. return weights;
  828. }
  829. #endif
  830. //-----------------------------------------------------------------------------
  831. // Horizontal/Vertical Search Functions
  832. /**
  833. * This allows to determine how much length should we add in the last step
  834. * of the searches. It takes the bilinearly interpolated edge (see
  835. * @PSEUDO_GATHER4), and adds 0, 1 or 2, depending on which edges and
  836. * crossing edges are active.
  837. */
  838. float SMAASearchLength(SMAATexture2D searchTex, float2 e, float bias, float scale) {
  839. // Not required if searchTex accesses are set to point:
  840. // float2 SEARCH_TEX_PIXEL_SIZE = 1.0 / float2(66.0, 33.0);
  841. // e = float2(bias, 0.0) + 0.5 * SEARCH_TEX_PIXEL_SIZE +
  842. // e * float2(scale, 1.0) * float2(64.0, 32.0) * SEARCH_TEX_PIXEL_SIZE;
  843. e.r = e.r * scale + bias; //CJ
  844. return 255.0 * SMAASampleLevelZeroPoint(searchTex, e).r;
  845. }
  846. /**
  847. * Horizontal/vertical search functions for the 2nd pass.
  848. */
  849. float SMAASearchXLeft(SMAATexture2D edgesTex, SMAATexture2D searchTex, float2 texcoord, float end) {
  850. /**
  851. * @PSEUDO_GATHER4
  852. * This texcoord has been offset by (-0.25, -0.125) in the vertex shader to
  853. * sample between edge, thus fetching four edges in a row.
  854. * Sampling with different offsets in each direction allows to disambiguate
  855. * which edges are active from the four fetched ones.
  856. */
  857. float2 e = float2(0.0, 1.0);
  858. while (texcoord.x > end &&
  859. e.g > 0.8281 && // Is there some edge not activated?
  860. e.r == 0.0) { // Or is there a crossing edge that breaks the line?
  861. e = SMAASampleLevelZero(edgesTex, texcoord).rg;
  862. texcoord -= float2(2.0, 0.0) * SMAA_PIXEL_SIZE;
  863. //texcoord = float2(-2.0, 0.0) * SMAA_PIXEL_SIZE + texcoord; //CJ - no change
  864. }
  865. // We correct the previous (-0.25, -0.125) offset we applied:
  866. texcoord.x += 0.25 * SMAA_PIXEL_SIZE.x;
  867. // The searches are bias by 1, so adjust the coords accordingly:
  868. texcoord.x += SMAA_PIXEL_SIZE.x;
  869. // Disambiguate the length added by the last step:
  870. texcoord.x += 2.0 * SMAA_PIXEL_SIZE.x; // Undo last step
  871. //texcoord.x = 3.25 * SMAA_PIXEL_SIZE.x + texcoord.x; //CJ
  872. //texcoord.x -= SMAA_PIXEL_SIZE.x * SMAASearchLength(searchTex, e, 0.0, 0.5);
  873. texcoord.x = -SMAA_PIXEL_SIZE.x * SMAASearchLength(searchTex, e, 0.0, 0.5) + texcoord.x; //CJ
  874. return texcoord.x;
  875. }
  876. float SMAASearchXRight(SMAATexture2D edgesTex, SMAATexture2D searchTex, float2 texcoord, float end) {
  877. float2 e = float2(0.0, 1.0);
  878. while (texcoord.x < end &&
  879. e.g > 0.8281 && // Is there some edge not activated?
  880. e.r == 0.0) { // Or is there a crossing edge that breaks the line?
  881. e = SMAASampleLevelZero(edgesTex, texcoord).rg;
  882. //texcoord += float2(2.0, 0.0) * SMAA_PIXEL_SIZE;
  883. texcoord = float2(2.0, 0.0) * SMAA_PIXEL_SIZE + texcoord; //CJ - no change
  884. }
  885. texcoord.x -= 0.25 * SMAA_PIXEL_SIZE.x;
  886. texcoord.x -= SMAA_PIXEL_SIZE.x;
  887. texcoord.x -= 2.0 * SMAA_PIXEL_SIZE.x;
  888. //texcoord.x += SMAA_PIXEL_SIZE.x * SMAASearchLength(searchTex, e, 0.5, 0.5);
  889. texcoord.x = SMAA_PIXEL_SIZE.x * SMAASearchLength(searchTex, e, 0.5, 0.5) + texcoord.x; //CJ - no change
  890. return texcoord.x;
  891. }
  892. float SMAASearchYUp(SMAATexture2D edgesTex, SMAATexture2D searchTex, float2 texcoord, float end) {
  893. float2 e = float2(1.0, 0.0);
  894. while (texcoord.y > end &&
  895. e.r > 0.8281 && // Is there some edge not activated?
  896. e.g == 0.0) { // Or is there a crossing edge that breaks the line?
  897. e = SMAASampleLevelZero(edgesTex, texcoord).rg;
  898. texcoord -= float2(0.0, 2.0) * SMAA_PIXEL_SIZE;
  899. }
  900. texcoord.y += 0.25 * SMAA_PIXEL_SIZE.y;
  901. texcoord.y += SMAA_PIXEL_SIZE.y;
  902. texcoord.y += 2.0 * SMAA_PIXEL_SIZE.y;
  903. //texcoord.y -= SMAA_PIXEL_SIZE.y * SMAASearchLength(searchTex, e.gr, 0.0, 0.5);
  904. texcoord.y = -SMAA_PIXEL_SIZE.y * SMAASearchLength(searchTex, e.gr, 0.0, 0.5) + texcoord.y; //CJ - no change
  905. return texcoord.y;
  906. }
  907. float SMAASearchYDown(SMAATexture2D edgesTex, SMAATexture2D searchTex, float2 texcoord, float end) {
  908. float2 e = float2(1.0, 0.0);
  909. while (texcoord.y < end &&
  910. e.r > 0.8281 && // Is there some edge not activated?
  911. e.g == 0.0) { // Or is there a crossing edge that breaks the line?
  912. e = SMAASampleLevelZero(edgesTex, texcoord).rg;
  913. texcoord += float2(0.0, 2.0) * SMAA_PIXEL_SIZE;
  914. }
  915. texcoord.y -= 0.25 * SMAA_PIXEL_SIZE.y;
  916. texcoord.y -= SMAA_PIXEL_SIZE.y;
  917. texcoord.y -= 2.0 * SMAA_PIXEL_SIZE.y;
  918. //texcoord.y += SMAA_PIXEL_SIZE.y * SMAASearchLength(searchTex, e.gr, 0.5, 0.5);
  919. texcoord.y = SMAA_PIXEL_SIZE.y * SMAASearchLength(searchTex, e.gr, 0.5, 0.5) + texcoord.y; //CJ - no change
  920. return texcoord.y;
  921. }
  922. /**
  923. * Ok, we have the distance and both crossing edges. So, what are the areas
  924. * at each side of current edge?
  925. */
  926. float2 SMAAArea(SMAATexture2D areaTex, float2 dist, float e1, float e2, float offset) {
  927. // Rounding prevents precision errors of bilinear filtering:
  928. float2 texcoord = float(SMAA_AREATEX_MAX_DISTANCE) * round(4.0 * float2(e1, e2)) + dist;
  929. // We do a scale and bias for mapping to texel space:
  930. texcoord = SMAA_AREATEX_PIXEL_SIZE * texcoord + (0.5 * SMAA_AREATEX_PIXEL_SIZE);
  931. // Move to proper place, according to the subpixel offset:
  932. //texcoord.y += SMAA_AREATEX_SUBTEX_SIZE * offset;
  933. texcoord.y = SMAA_AREATEX_SUBTEX_SIZE * offset + texcoord.y; //CJ
  934. // Do it!
  935. #if SMAA_HLSL_3 == 1
  936. return SMAASampleLevelZero(areaTex, texcoord).ra;
  937. #else
  938. return SMAASampleLevelZero(areaTex, texcoord).rg;
  939. #endif
  940. }
  941. //-----------------------------------------------------------------------------
  942. // Corner Detection Functions
  943. void SMAADetectHorizontalCornerPattern(SMAATexture2D edgesTex, inout float2 weights, float2 texcoord, float2 d) {
  944. #if SMAA_CORNER_ROUNDING < 100 || SMAA_FORCE_CORNER_DETECTION == 1
  945. float4 coords = SMAAMad(float4(d.x, 0.0, d.y, 0.0), //CJ - this look ripe for a rewrite
  946. SMAA_PIXEL_SIZE.xyxy, texcoord.xyxy);
  947. float2 e;
  948. /*
  949. e.r = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2(0.0, 1.0)).r;
  950. bool left = abs(d.x) < abs(d.y);
  951. e.g = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2(0.0, -2.0)).r;
  952. if (left) weights *= SMAASaturate(float(SMAA_CORNER_ROUNDING) / 100.0 + 1.0 - e);
  953. e.r = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2(1.0, 1.0)).r;
  954. e.g = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2(1.0, -2.0)).r;
  955. if (!left) weights *= SMAASaturate(float(SMAA_CORNER_ROUNDING) / 100.0 + 1.0 - e);
  956. */
  957. //bool left = abs(d.x) < abs(d.y);
  958. SMAA_BRANCH //CJ - test if this branch is faster than original code
  959. if (abs(d.x) < abs(d.y)) //if (left)
  960. {
  961. e.r = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2(0.0, 1.0)).r;
  962. e.g = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2(0.0, -2.0)).r;
  963. }
  964. else
  965. {
  966. e.r = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2(1.0, 1.0)).r;
  967. e.g = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2(1.0, -2.0)).r;
  968. }
  969. //weights *= SMAASaturate(float(SMAA_CORNER_ROUNDING) / 100.0 + 1.0 - e);
  970. weights = SMAASaturate(-e * weights + (1.0 + float(SMAA_CORNER_ROUNDING) * 0.01) * weights); //CJ - should compile to a mad when SMAA_CORNER_ROUNDING is 0
  971. #endif
  972. }
  973. void SMAADetectVerticalCornerPattern(SMAATexture2D edgesTex, inout float2 weights, float2 texcoord, float2 d) {
  974. #if SMAA_CORNER_ROUNDING < 100 || SMAA_FORCE_CORNER_DETECTION == 1
  975. float4 coords = SMAAMad(float4(0.0, d.x, 0.0, d.y),
  976. SMAA_PIXEL_SIZE.xyxy, texcoord.xyxy);
  977. float2 e;
  978. /*
  979. e.r = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2( 1.0, 0.0)).g;
  980. bool left = abs(d.x) < abs(d.y);
  981. e.g = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2(-2.0, 0.0)).g;
  982. if (left) weights *= SMAASaturate(float(SMAA_CORNER_ROUNDING) / 100.0 + 1.0 - e);
  983. e.r = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1.0, 1.0)).g;
  984. e.g = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2(-2.0, 1.0)).g;
  985. if (!left) weights *= SMAASaturate(float(SMAA_CORNER_ROUNDING) / 100.0 + 1.0 - e);
  986. */
  987. SMAA_BRANCH //CJ - test if this branch is faster than original code
  988. if (abs(d.x) < abs(d.y)) //if (left)
  989. {
  990. e.r = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2( 1.0, 0.0)).g;
  991. e.g = SMAASampleLevelZeroOffset(edgesTex, coords.xy, int2(-2.0, 0.0)).g;
  992. }
  993. else
  994. {
  995. e.r = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2( 1.0, 1.0)).g;
  996. e.g = SMAASampleLevelZeroOffset(edgesTex, coords.zw, int2(-2.0, 1.0)).g;
  997. }
  998. weights *= SMAASaturate(float(SMAA_CORNER_ROUNDING) / 100.0 + 1.0 - e);
  999. #endif
  1000. }
  1001. //-----------------------------------------------------------------------------
  1002. // Blending Weight Calculation Pixel Shader (Second Pass)
  1003. float4 SMAABlendingWeightCalculationPS(float2 texcoord,
  1004. float2 pixcoord,
  1005. float4 offset[3],
  1006. SMAATexture2D edgesTex,
  1007. SMAATexture2D areaTex,
  1008. SMAATexture2D searchTex,
  1009. int4 subsampleIndices) { // Just pass zero for SMAA 1x, see @SUBSAMPLE_INDICES.
  1010. float4 weights = float4(0.0, 0.0, 0.0, 0.0);
  1011. float2 e = SMAASample(edgesTex, texcoord).rg;
  1012. SMAA_BRANCH
  1013. //if (e.g > 0.0) { // Edge at north
  1014. if (e.g != -e.r) { // Edge at north //CJ - if neither e.g or e.r is 0
  1015. #if SMAA_MAX_SEARCH_STEPS_DIAG > 0 || SMAA_FORCE_DIAGONAL_DETECTION == 1
  1016. // Diagonals have both north and west edges, so searching for them in
  1017. // one of the boundaries is enough.
  1018. weights.rg = SMAACalculateDiagWeights(edgesTex, areaTex, texcoord, e, subsampleIndices);
  1019. // We give priority to diagonals, so if we find a diagonal we skip
  1020. // horizontal/vertical processing.
  1021. SMAA_BRANCH
  1022. //if (dot(weights.rg, float2(1.0, 1.0)) == 0.0) {
  1023. //if (!(weights.r + weights.g)) { //CJ
  1024. //if ((weights.r + weights.g) == 0.0) { //CJ one instruction shorter
  1025. if (weights.r == -weights.g) { //CJ one instruction shorter - same asm as above
  1026. #endif
  1027. float2 d;
  1028. // Find the distance to the left:
  1029. float2 coords;
  1030. coords.x = SMAASearchXLeft(edgesTex, searchTex, offset[0].xy, offset[2].x);
  1031. coords.y = offset[1].y; // offset[1].y = texcoord.y - 0.25 * SMAA_PIXEL_SIZE.y (@CROSSING_OFFSET)
  1032. d.x = coords.x;
  1033. // Now fetch the left crossing edges, two at a time using bilinear
  1034. // filtering. Sampling at -0.25 (see @CROSSING_OFFSET) enables to
  1035. // discern what value each edge has:
  1036. float e1 = SMAASampleLevelZero(edgesTex, coords).r;
  1037. // Find the distance to the right:
  1038. coords.x = SMAASearchXRight(edgesTex, searchTex, offset[0].zw, offset[2].y);
  1039. d.y = coords.x;
  1040. // We want the distances to be in pixel units (doing this here allow to
  1041. // better interleave arithmetic and memory accesses):
  1042. //d = d / SMAA_PIXEL_SIZE.x - pixcoord.x;
  1043. d = d * (1.0 / SMAA_PIXEL_SIZE.x) - pixcoord.x; //CJ - no change
  1044. // SMAAArea below needs a sqrt, as the areas texture is compressed
  1045. // quadratically:
  1046. float2 sqrt_d = sqrt(abs(d));
  1047. // Fetch the right crossing edges:
  1048. float e2 = SMAASampleLevelZeroOffset(edgesTex, coords, int2(1, 0)).r;
  1049. // Ok, we know how this pattern looks like, now it is time for getting
  1050. // the actual area:
  1051. weights.rg = SMAAArea(areaTex, sqrt_d, e1, e2, float(subsampleIndices.y));
  1052. // Fix corners:
  1053. SMAADetectHorizontalCornerPattern(edgesTex, weights.rg, texcoord, d);
  1054. #if SMAA_MAX_SEARCH_STEPS_DIAG > 0 || SMAA_FORCE_DIAGONAL_DETECTION == 1
  1055. } else
  1056. e.r = 0.0; // Skip vertical processing.
  1057. #endif
  1058. SMAA_BRANCH
  1059. if (e.r > 0.0) { // Edge at west
  1060. float2 d;
  1061. // Find the distance to the top:
  1062. float2 coords;
  1063. coords.y = SMAASearchYUp(edgesTex, searchTex, offset[1].xy, offset[2].z);
  1064. coords.x = offset[0].x; // offset[1].x = texcoord.x - 0.25 * SMAA_PIXEL_SIZE.x;
  1065. d.x = coords.y;
  1066. // Fetch the top crossing edges:
  1067. float e1 = SMAASampleLevelZero(edgesTex, coords).g;
  1068. // Find the distance to the bottom:
  1069. coords.y = SMAASearchYDown(edgesTex, searchTex, offset[1].zw, offset[2].w);
  1070. d.y = coords.y;
  1071. // We want the distances to be in pixel units:
  1072. d = d / SMAA_PIXEL_SIZE.y - pixcoord.y;
  1073. // SMAAArea below needs a sqrt, as the areas texture is compressed
  1074. // quadratically:
  1075. float2 sqrt_d = sqrt(abs(d));
  1076. // Fetch the bottom crossing edges:
  1077. float e2 = SMAASampleLevelZeroOffset(edgesTex, coords, int2(0, 1)).g;
  1078. // Get the area for this direction:
  1079. weights.ba = SMAAArea(areaTex, sqrt_d, e1, e2, float(subsampleIndices.x));
  1080. // Fix corners:
  1081. SMAADetectVerticalCornerPattern(edgesTex, weights.ba, texcoord, d);
  1082. }
  1083. } // CJ
  1084. return weights;
  1085. }
  1086. //-----------------------------------------------------------------------------
  1087. // Neighborhood Blending Pixel Shader (Third Pass)
  1088. float4 SMAANeighborhoodBlendingPS(float2 texcoord,
  1089. float4 offset[2],
  1090. SMAATexture2D colorTex,
  1091. SMAATexture2D blendTex) {
  1092. // Fetch the blending weights for current pixel:
  1093. float4 a;
  1094. a.xz = SMAASample(blendTex, texcoord).xz;
  1095. a.y = SMAASample(blendTex, offset[1].zw).g;
  1096. a.w = SMAASample(blendTex, offset[1].xy).a;
  1097. // Is there any blending weight with a value greater than 0.0?
  1098. SMAA_BRANCH
  1099. //if (dot(a, float4(1.0, 1.0, 1.0, 1.0)) < 1e-5)
  1100. // return SMAASampleLevelZero(colorTex, texcoord);
  1101. if (((a.x + a.z) + (a.y + a.w)) < 1e-5) //CJ - reduces number of instruction slots
  1102. return SMAASampleLevelZero(colorTex, texcoord); //CJ
  1103. else {
  1104. //float4 color = float4(0.0, 0.0, 0.0, 0.0);
  1105. // Up to 4 lines can be crossing a pixel (one through each edge). We
  1106. // favor blending by choosing the line with the maximum weight for each
  1107. // direction:
  1108. float2 offset;
  1109. offset.x = a.a > a.b? a.a : -a.b; // left vs. right
  1110. offset.y = a.g > a.r? a.g : -a.r; // top vs. bottom
  1111. /*
  1112. // Then we go in the direction that has the maximum weight:
  1113. if (abs(offset.x) > abs(offset.y)) // horizontal vs. vertical
  1114. offset.y = 0.0;
  1115. else
  1116. offset.x = 0.0;
  1117. */
  1118. // Then we go in the direction that has the maximum weight:
  1119. bool2 whichway = saturate(abs(offset.xy) - abs(offset.yx)); // CJ - same # of instructions but one GPR less
  1120. //float2 whichway = step( abs(offset.xy) , abs(offset.yx) ); //CJ same as above - but now with a possible error
  1121. offset = offset * whichway; //CJ
  1122. #if SMAA_REPROJECTION == 1
  1123. // Fetch the opposite color and lerp by hand:
  1124. float4 C = SMAASampleLevelZero(colorTex, texcoord);
  1125. texcoord += sign(offset) * SMAA_PIXEL_SIZE;
  1126. //texcoord = whichway * SMAA_PIXEL_SIZE + texcoord; //CJ faster - but beware that I have not been able to test this. It didn't work for the SMAA_DIRECTX9_LINEAR_BLEND == 1 path though.
  1127. float4 Cop = SMAASampleLevelZero(colorTex, texcoord);
  1128. float s = abs(offset.x) > abs(offset.y)? abs(offset.x) : abs(offset.y);
  1129. //float s = max(abs(offset.x),abs(offset.y)); //CJ
  1130. // Unpack the velocity values:
  1131. C.a *= C.a;
  1132. Cop.a *= Cop.a;
  1133. // Lerp the colors:
  1134. float4 Caa = SMAALerp(C, Cop, s);
  1135. // Unpack velocity and return the resulting value:
  1136. Caa.a = sqrt(Caa.a);
  1137. return Caa;
  1138. #else
  1139. #if SMAA_HLSL_4 == 1 || SMAA_DIRECTX9_LINEAR_BLEND == 0
  1140. // We exploit bilinear filtering to mix current pixel with the chosen
  1141. // neighbor:
  1142. texcoord += offset * SMAA_PIXEL_SIZE;
  1143. return SMAASampleLevelZero(colorTex, texcoord);
  1144. #else
  1145. // Fetch the opposite color and lerp by hand:
  1146. float4 C = SMAASampleLevelZero(colorTex, texcoord);
  1147. texcoord += sign(offset) * SMAA_PIXEL_SIZE;
  1148. //texcoord = whichway * SMAA_PIXEL_SIZE + texcoord; //CJ faster this way, but causes less smooth lines and I haven't been able to figure out why yet
  1149. float4 Cop = SMAASampleLevelZero(colorTex, texcoord);
  1150. float s = abs(offset.x) > abs(offset.y)? abs(offset.x) : abs(offset.y);
  1151. //float s = max(abs(offset.x),abs(offset.y)); //CJ - No change. Results in same ASM as above
  1152. //float s = offset.x + offset.y; //CJ at this point we should be sure that either offset.x or .y is 0 so we can just add them - again no improvement in ASM :(
  1153. return SMAALerp(C, Cop, abs(s));
  1154. #endif
  1155. #endif
  1156. }
  1157. }
  1158. //-----------------------------------------------------------------------------
  1159. // Temporal Resolve Pixel Shader (Optional Fourth Pass)
  1160. float4 SMAAResolvePS(float2 texcoord,
  1161. SMAATexture2D colorTexCurr,
  1162. SMAATexture2D colorTexPrev
  1163. #if SMAA_REPROJECTION == 1
  1164. , SMAATexture2D velocityTex
  1165. #endif
  1166. ) {
  1167. #if SMAA_REPROJECTION == 1
  1168. // Velocity is calculated from previous to current position, so we need to
  1169. // inverse it:
  1170. float2 velocity = -SMAASample(velocityTex, texcoord).rg;
  1171. // Fetch current pixel:
  1172. float4 current = SMAASample(colorTexCurr, texcoord);
  1173. // Reproject current coordinates and fetch previous pixel:
  1174. float4 previous = SMAASample(colorTexPrev, texcoord + velocity);
  1175. // Attenuate the previous pixel if the velocity is different:
  1176. float delta = abs(current.a * current.a - previous.a * previous.a) / 5.0;
  1177. //float delta = abs(dot(float2(current.a,current.a),float(-previous.a,previous.a)) / 5.0; //CJ
  1178. //float delta = 0.20 * (current.a + previous.a) * abs(current.a - previous.a) //CJ
  1179. float weight = 0.5 * SMAASaturate(1.0 - (sqrt(delta) * SMAA_REPROJECTION_WEIGHT_SCALE));
  1180. // Blend the pixels according to the calculated weight:
  1181. return SMAALerp(current, previous, weight);
  1182. #else
  1183. // Just blend the pixels:
  1184. float4 current = SMAASample(colorTexCurr, texcoord);
  1185. float4 previous = SMAASample(colorTexPrev, texcoord);
  1186. return SMAALerp(current, previous, 0.5);
  1187. #endif
  1188. }
  1189. //-----------------------------------------------------------------------------
  1190. #endif // SMAA_ONLY_COMPILE_VS == 0