ffx_fsr1.h 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  1. // This file is part of the FidelityFX SDK.
  2. //
  3. // Copyright (c) 2022 Advanced Micro Devices, Inc. All rights reserved.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. #ifdef __clang__
  22. #pragma clang diagnostic ignored "-Wunused-variable"
  23. #endif
  24. /// Setup required constant values for EASU (works on CPU or GPU).
  25. ///
  26. /// @param [out] con0
  27. /// @param [out] con1
  28. /// @param [out] con2
  29. /// @param [out] con3
  30. /// @param [in] inputViewportInPixelsX The rendered image resolution being upscaled in X dimension.
  31. /// @param [in] inputViewportInPixelsY The rendered image resolution being upscaled in Y dimension.
  32. /// @param [in] inputSizeInPixelsX The resolution of the resource containing the input image (useful for dynamic resolution) in X dimension.
  33. /// @param [in] inputSizeInPixelsY The resolution of the resource containing the input image (useful for dynamic resolution) in Y dimension.
  34. /// @param [in] outputSizeInPixelsX The display resolution which the input image gets upscaled to in X dimension.
  35. /// @param [in] outputSizeInPixelsY The display resolution which the input image gets upscaled to in Y dimension.
  36. ///
  37. /// @ingroup FSR1
  38. FFX_STATIC void ffxFsrPopulateEasuConstants(
  39. FFX_PARAMETER_INOUT FfxUInt32x4 con0,
  40. FFX_PARAMETER_INOUT FfxUInt32x4 con1,
  41. FFX_PARAMETER_INOUT FfxUInt32x4 con2,
  42. FFX_PARAMETER_INOUT FfxUInt32x4 con3,
  43. FFX_PARAMETER_IN FfxFloat32 inputViewportInPixelsX,
  44. FFX_PARAMETER_IN FfxFloat32 inputViewportInPixelsY,
  45. FFX_PARAMETER_IN FfxFloat32 inputSizeInPixelsX,
  46. FFX_PARAMETER_IN FfxFloat32 inputSizeInPixelsY,
  47. FFX_PARAMETER_IN FfxFloat32 outputSizeInPixelsX,
  48. FFX_PARAMETER_IN FfxFloat32 outputSizeInPixelsY)
  49. {
  50. // Output integer position to a pixel position in viewport.
  51. con0[0] = ffxAsUInt32(inputViewportInPixelsX * ffxReciprocal(outputSizeInPixelsX));
  52. con0[1] = ffxAsUInt32(inputViewportInPixelsY * ffxReciprocal(outputSizeInPixelsY));
  53. con0[2] = ffxAsUInt32(FfxFloat32(0.5) * inputViewportInPixelsX * ffxReciprocal(outputSizeInPixelsX) - FfxFloat32(0.5));
  54. con0[3] = ffxAsUInt32(FfxFloat32(0.5) * inputViewportInPixelsY * ffxReciprocal(outputSizeInPixelsY) - FfxFloat32(0.5));
  55. // Viewport pixel position to normalized image space.
  56. // This is used to get upper-left of 'F' tap.
  57. con1[0] = ffxAsUInt32(ffxReciprocal(inputSizeInPixelsX));
  58. con1[1] = ffxAsUInt32(ffxReciprocal(inputSizeInPixelsY));
  59. // Centers of gather4, first offset from upper-left of 'F'.
  60. // +---+---+
  61. // | | |
  62. // +--(0)--+
  63. // | b | c |
  64. // +---F---+---+---+
  65. // | e | f | g | h |
  66. // +--(1)--+--(2)--+
  67. // | i | j | k | l |
  68. // +---+---+---+---+
  69. // | n | o |
  70. // +--(3)--+
  71. // | | |
  72. // +---+---+
  73. con1[2] = ffxAsUInt32(FfxFloat32(1.0) * ffxReciprocal(inputSizeInPixelsX));
  74. con1[3] = ffxAsUInt32(FfxFloat32(-1.0) * ffxReciprocal(inputSizeInPixelsY));
  75. // These are from (0) instead of 'F'.
  76. con2[0] = ffxAsUInt32(FfxFloat32(-1.0) * ffxReciprocal(inputSizeInPixelsX));
  77. con2[1] = ffxAsUInt32(FfxFloat32(2.0) * ffxReciprocal(inputSizeInPixelsY));
  78. con2[2] = ffxAsUInt32(FfxFloat32(1.0) * ffxReciprocal(inputSizeInPixelsX));
  79. con2[3] = ffxAsUInt32(FfxFloat32(2.0) * ffxReciprocal(inputSizeInPixelsY));
  80. con3[0] = ffxAsUInt32(FfxFloat32(0.0) * ffxReciprocal(inputSizeInPixelsX));
  81. con3[1] = ffxAsUInt32(FfxFloat32(4.0) * ffxReciprocal(inputSizeInPixelsY));
  82. con3[2] = con3[3] = 0;
  83. }
  84. /// Setup required constant values for EASU (works on CPU or GPU).
  85. ///
  86. /// @param [out] con0
  87. /// @param [out] con1
  88. /// @param [out] con2
  89. /// @param [out] con3
  90. /// @param [in] inputViewportInPixelsX The resolution of the input in the X dimension.
  91. /// @param [in] inputViewportInPixelsY The resolution of the input in the Y dimension.
  92. /// @param [in] inputSizeInPixelsX The input size in pixels in the X dimension.
  93. /// @param [in] inputSizeInPixelsY The input size in pixels in the Y dimension.
  94. /// @param [in] outputSizeInPixelsX The output size in pixels in the X dimension.
  95. /// @param [in] outputSizeInPixelsY The output size in pixels in the Y dimension.
  96. /// @param [in] inputOffsetInPixelsX The input image offset in the X dimension into the resource containing it (useful for dynamic resolution).
  97. /// @param [in] inputOffsetInPixelsY The input image offset in the Y dimension into the resource containing it (useful for dynamic resolution).
  98. ///
  99. /// @ingroup FSR1
  100. FFX_STATIC void ffxFsrPopulateEasuConstantsOffset(
  101. FFX_PARAMETER_INOUT FfxUInt32x4 con0,
  102. FFX_PARAMETER_INOUT FfxUInt32x4 con1,
  103. FFX_PARAMETER_INOUT FfxUInt32x4 con2,
  104. FFX_PARAMETER_INOUT FfxUInt32x4 con3,
  105. FFX_PARAMETER_IN FfxFloat32 inputViewportInPixelsX,
  106. FFX_PARAMETER_IN FfxFloat32 inputViewportInPixelsY,
  107. FFX_PARAMETER_IN FfxFloat32 inputSizeInPixelsX,
  108. FFX_PARAMETER_IN FfxFloat32 inputSizeInPixelsY,
  109. FFX_PARAMETER_IN FfxFloat32 outputSizeInPixelsX,
  110. FFX_PARAMETER_IN FfxFloat32 outputSizeInPixelsY,
  111. FFX_PARAMETER_IN FfxFloat32 inputOffsetInPixelsX,
  112. FFX_PARAMETER_IN FfxFloat32 inputOffsetInPixelsY)
  113. {
  114. ffxFsrPopulateEasuConstants(
  115. con0,
  116. con1,
  117. con2,
  118. con3,
  119. inputViewportInPixelsX,
  120. inputViewportInPixelsY,
  121. inputSizeInPixelsX,
  122. inputSizeInPixelsY,
  123. outputSizeInPixelsX,
  124. outputSizeInPixelsY);
  125. // override
  126. con0[2] = ffxAsUInt32(FfxFloat32(0.5) * inputViewportInPixelsX * ffxReciprocal(outputSizeInPixelsX) - FfxFloat32(0.5) + inputOffsetInPixelsX);
  127. con0[3] = ffxAsUInt32(FfxFloat32(0.5) * inputViewportInPixelsY * ffxReciprocal(outputSizeInPixelsY) - FfxFloat32(0.5) + inputOffsetInPixelsY);
  128. }
  129. #if defined(FFX_GPU) && defined(FFX_FSR_EASU_FLOAT)
  130. // Input callback prototypes, need to be implemented by calling shader
  131. FfxFloat32x4 FsrEasuRF(FfxFloat32x2 p);
  132. FfxFloat32x4 FsrEasuGF(FfxFloat32x2 p);
  133. FfxFloat32x4 FsrEasuBF(FfxFloat32x2 p);
  134. // Filtering for a given tap for the scalar.
  135. void fsrEasuTapFloat(
  136. FFX_PARAMETER_INOUT FfxFloat32x3 accumulatedColor, // Accumulated color, with negative lobe.
  137. FFX_PARAMETER_INOUT FfxFloat32 accumulatedWeight, // Accumulated weight.
  138. FFX_PARAMETER_IN FfxFloat32x2 pixelOffset, // Pixel offset from resolve position to tap.
  139. FFX_PARAMETER_IN FfxFloat32x2 gradientDirection, // Gradient direction.
  140. FFX_PARAMETER_IN FfxFloat32x2 length, // Length.
  141. FFX_PARAMETER_IN FfxFloat32 negativeLobeStrength, // Negative lobe strength.
  142. FFX_PARAMETER_IN FfxFloat32 clippingPoint, // Clipping point.
  143. FFX_PARAMETER_IN FfxFloat32x3 color) // Tap color.
  144. {
  145. // Rotate offset by direction.
  146. FfxFloat32x2 rotatedOffset;
  147. rotatedOffset.x = (pixelOffset.x * (gradientDirection.x)) + (pixelOffset.y * gradientDirection.y);
  148. rotatedOffset.y = (pixelOffset.x * (-gradientDirection.y)) + (pixelOffset.y * gradientDirection.x);
  149. // Anisotropy.
  150. rotatedOffset *= length;
  151. // Compute distance^2.
  152. FfxFloat32 distanceSquared = rotatedOffset.x * rotatedOffset.x + rotatedOffset.y * rotatedOffset.y;
  153. // Limit to the window as at corner, 2 taps can easily be outside.
  154. distanceSquared = ffxMin(distanceSquared, clippingPoint);
  155. // Approximation of lancos2 without sin() or rcp(), or sqrt() to get x.
  156. // (25/16 * (2/5 * x^2 - 1)^2 - (25/16 - 1)) * (1/4 * x^2 - 1)^2
  157. // |_______________________________________| |_______________|
  158. // base window
  159. // The general form of the 'base' is,
  160. // (a*(b*x^2-1)^2-(a-1))
  161. // Where 'a=1/(2*b-b^2)' and 'b' moves around the negative lobe.
  162. FfxFloat32 weightB = FfxFloat32(2.0 / 5.0) * distanceSquared + FfxFloat32(-1.0);
  163. FfxFloat32 weightA = negativeLobeStrength * distanceSquared + FfxFloat32(-1.0);
  164. weightB *= weightB;
  165. weightA *= weightA;
  166. weightB = FfxFloat32(25.0 / 16.0) * weightB + FfxFloat32(-(25.0 / 16.0 - 1.0));
  167. FfxFloat32 weight = weightB * weightA;
  168. // Do weighted average.
  169. accumulatedColor += color * weight;
  170. accumulatedWeight += weight;
  171. }
  172. // Accumulate direction and length.
  173. void fsrEasuSetFloat(
  174. FFX_PARAMETER_INOUT FfxFloat32x2 direction,
  175. FFX_PARAMETER_INOUT FfxFloat32 length,
  176. FFX_PARAMETER_IN FfxFloat32x2 pp,
  177. FFX_PARAMETER_IN FfxBoolean biS,
  178. FFX_PARAMETER_IN FfxBoolean biT,
  179. FFX_PARAMETER_IN FfxBoolean biU,
  180. FFX_PARAMETER_IN FfxBoolean biV,
  181. FFX_PARAMETER_IN FfxFloat32 lA,
  182. FFX_PARAMETER_IN FfxFloat32 lB,
  183. FFX_PARAMETER_IN FfxFloat32 lC,
  184. FFX_PARAMETER_IN FfxFloat32 lD,
  185. FFX_PARAMETER_IN FfxFloat32 lE)
  186. {
  187. // Compute bilinear weight, branches factor out as predicates are compiler time immediates.
  188. // s t
  189. // u v
  190. FfxFloat32 weight = FfxFloat32(0.0);
  191. if (biS)
  192. weight = (FfxFloat32(1.0) - pp.x) * (FfxFloat32(1.0) - pp.y);
  193. if (biT)
  194. weight = pp.x * (FfxFloat32(1.0) - pp.y);
  195. if (biU)
  196. weight = (FfxFloat32(1.0) - pp.x) * pp.y;
  197. if (biV)
  198. weight = pp.x * pp.y;
  199. // Direction is the '+' diff.
  200. // a
  201. // b c d
  202. // e
  203. // Then takes magnitude from abs average of both sides of 'c'.
  204. // Length converts gradient reversal to 0, smoothly to non-reversal at 1, shaped, then adding horz and vert terms.
  205. FfxFloat32 dc = lD - lC;
  206. FfxFloat32 cb = lC - lB;
  207. FfxFloat32 lengthX = max(abs(dc), abs(cb));
  208. lengthX = ffxApproximateReciprocal(lengthX);
  209. FfxFloat32 directionX = lD - lB;
  210. direction.x += directionX * weight;
  211. lengthX = ffxSaturate(abs(directionX) * lengthX);
  212. lengthX *= lengthX;
  213. length += lengthX * weight;
  214. // Repeat for the y axis.
  215. FfxFloat32 ec = lE - lC;
  216. FfxFloat32 ca = lC - lA;
  217. FfxFloat32 lengthY = max(abs(ec), abs(ca));
  218. lengthY = ffxApproximateReciprocal(lengthY);
  219. FfxFloat32 directionY = lE - lA;
  220. direction.y += directionY * weight;
  221. lengthY = ffxSaturate(abs(directionY) * lengthY);
  222. lengthY *= lengthY;
  223. length += lengthY * weight;
  224. }
  225. /// Apply edge-aware spatial upsampling using 32bit floating point precision calculations.
  226. ///
  227. /// @param [out] outPixel The computed color of a pixel.
  228. /// @param [in] integerPosition Integer pixel position within the output.
  229. /// @param [in] con0 The first constant value generated by <c><i>ffxFsrPopulateEasuConstants</i></c>.
  230. /// @param [in] con1 The second constant value generated by <c><i>ffxFsrPopulateEasuConstants</i></c>.
  231. /// @param [in] con2 The third constant value generated by <c><i>ffxFsrPopulateEasuConstants</i></c>.
  232. /// @param [in] con3 The fourth constant value generated by <c><i>ffxFsrPopulateEasuConstants</i></c>.
  233. ///
  234. /// @ingroup FSR
  235. void ffxFsrEasuFloat(
  236. FFX_PARAMETER_OUT FfxFloat32x3 pix,
  237. FFX_PARAMETER_IN FfxUInt32x2 ip,
  238. FFX_PARAMETER_IN FfxUInt32x4 con0,
  239. FFX_PARAMETER_IN FfxUInt32x4 con1,
  240. FFX_PARAMETER_IN FfxUInt32x4 con2,
  241. FFX_PARAMETER_IN FfxUInt32x4 con3)
  242. {
  243. // Get position of 'f'.
  244. FfxFloat32x2 pp = FfxFloat32x2(ip) * ffxAsFloat(con0.xy) + ffxAsFloat(con0.zw);
  245. FfxFloat32x2 fp = floor(pp);
  246. pp -= fp;
  247. // 12-tap kernel.
  248. // b c
  249. // e f g h
  250. // i j k l
  251. // n o
  252. // Gather 4 ordering.
  253. // a b
  254. // r g
  255. // For packed FP16, need either {rg} or {ab} so using the following setup for gather in all versions,
  256. // a b <- unused (z)
  257. // r g
  258. // a b a b
  259. // r g r g
  260. // a b
  261. // r g <- unused (z)
  262. // Allowing dead-code removal to remove the 'z's.
  263. FfxFloat32x2 p0 = fp * ffxAsFloat(con1.xy) + ffxAsFloat(con1.zw);
  264. // These are from p0 to avoid pulling two constants on pre-Navi hardware.
  265. FfxFloat32x2 p1 = p0 + ffxAsFloat(con2.xy);
  266. FfxFloat32x2 p2 = p0 + ffxAsFloat(con2.zw);
  267. FfxFloat32x2 p3 = p0 + ffxAsFloat(con3.xy);
  268. FfxFloat32x4 bczzR = FsrEasuRF(p0);
  269. FfxFloat32x4 bczzG = FsrEasuGF(p0);
  270. FfxFloat32x4 bczzB = FsrEasuBF(p0);
  271. FfxFloat32x4 ijfeR = FsrEasuRF(p1);
  272. FfxFloat32x4 ijfeG = FsrEasuGF(p1);
  273. FfxFloat32x4 ijfeB = FsrEasuBF(p1);
  274. FfxFloat32x4 klhgR = FsrEasuRF(p2);
  275. FfxFloat32x4 klhgG = FsrEasuGF(p2);
  276. FfxFloat32x4 klhgB = FsrEasuBF(p2);
  277. FfxFloat32x4 zzonR = FsrEasuRF(p3);
  278. FfxFloat32x4 zzonG = FsrEasuGF(p3);
  279. FfxFloat32x4 zzonB = FsrEasuBF(p3);
  280. // Simplest multi-channel approximate luma possible (luma times 2, in 2 FMA/MAD).
  281. FfxFloat32x4 bczzL = bczzB * ffxBroadcast4(0.5) + (bczzR * ffxBroadcast4(0.5) + bczzG);
  282. FfxFloat32x4 ijfeL = ijfeB * ffxBroadcast4(0.5) + (ijfeR * ffxBroadcast4(0.5) + ijfeG);
  283. FfxFloat32x4 klhgL = klhgB * ffxBroadcast4(0.5) + (klhgR * ffxBroadcast4(0.5) + klhgG);
  284. FfxFloat32x4 zzonL = zzonB * ffxBroadcast4(0.5) + (zzonR * ffxBroadcast4(0.5) + zzonG);
  285. // Rename.
  286. FfxFloat32 bL = bczzL.x;
  287. FfxFloat32 cL = bczzL.y;
  288. FfxFloat32 iL = ijfeL.x;
  289. FfxFloat32 jL = ijfeL.y;
  290. FfxFloat32 fL = ijfeL.z;
  291. FfxFloat32 eL = ijfeL.w;
  292. FfxFloat32 kL = klhgL.x;
  293. FfxFloat32 lL = klhgL.y;
  294. FfxFloat32 hL = klhgL.z;
  295. FfxFloat32 gL = klhgL.w;
  296. FfxFloat32 oL = zzonL.z;
  297. FfxFloat32 nL = zzonL.w;
  298. // Accumulate for bilinear interpolation.
  299. FfxFloat32x2 dir = ffxBroadcast2(0.0);
  300. FfxFloat32 len = FfxFloat32(0.0);
  301. fsrEasuSetFloat(dir, len, pp, FFX_TRUE, FFX_FALSE, FFX_FALSE, FFX_FALSE, bL, eL, fL, gL, jL);
  302. fsrEasuSetFloat(dir, len, pp, FFX_FALSE, FFX_TRUE, FFX_FALSE, FFX_FALSE, cL, fL, gL, hL, kL);
  303. fsrEasuSetFloat(dir, len, pp, FFX_FALSE, FFX_FALSE, FFX_TRUE, FFX_FALSE, fL, iL, jL, kL, nL);
  304. fsrEasuSetFloat(dir, len, pp, FFX_FALSE, FFX_FALSE, FFX_FALSE, FFX_TRUE, gL, jL, kL, lL, oL);
  305. // Normalize with approximation, and cleanup close to zero.
  306. FfxFloat32x2 dir2 = dir * dir;
  307. FfxFloat32 dirR = dir2.x + dir2.y;
  308. FfxUInt32 zro = dirR < FfxFloat32(1.0 / 32768.0);
  309. dirR = ffxApproximateReciprocalSquareRoot(dirR);
  310. dirR = zro ? FfxFloat32(1.0) : dirR;
  311. dir.x = zro ? FfxFloat32(1.0) : dir.x;
  312. dir *= ffxBroadcast2(dirR);
  313. // Transform from {0 to 2} to {0 to 1} range, and shape with square.
  314. len = len * FfxFloat32(0.5);
  315. len *= len;
  316. // Stretch kernel {1.0 vert|horz, to sqrt(2.0) on diagonal}.
  317. FfxFloat32 stretch = (dir.x * dir.x + dir.y * dir.y) * ffxApproximateReciprocal(max(abs(dir.x), abs(dir.y)));
  318. // Anisotropic length after rotation,
  319. // x := 1.0 lerp to 'stretch' on edges
  320. // y := 1.0 lerp to 2x on edges
  321. FfxFloat32x2 len2 = FfxFloat32x2(FfxFloat32(1.0) + (stretch - FfxFloat32(1.0)) * len, FfxFloat32(1.0) + FfxFloat32(-0.5) * len);
  322. // Based on the amount of 'edge',
  323. // the window shifts from +/-{sqrt(2.0) to slightly beyond 2.0}.
  324. FfxFloat32 lob = FfxFloat32(0.5) + FfxFloat32((1.0 / 4.0 - 0.04) - 0.5) * len;
  325. // Set distance^2 clipping point to the end of the adjustable window.
  326. FfxFloat32 clp = ffxApproximateReciprocal(lob);
  327. // Accumulation mixed with min/max of 4 nearest.
  328. // b c
  329. // e f g h
  330. // i j k l
  331. // n o
  332. FfxFloat32x3 min4 =
  333. ffxMin(ffxMin3(FfxFloat32x3(ijfeR.z, ijfeG.z, ijfeB.z), FfxFloat32x3(klhgR.w, klhgG.w, klhgB.w), FfxFloat32x3(ijfeR.y, ijfeG.y, ijfeB.y)),
  334. FfxFloat32x3(klhgR.x, klhgG.x, klhgB.x));
  335. FfxFloat32x3 max4 =
  336. max(ffxMax3(FfxFloat32x3(ijfeR.z, ijfeG.z, ijfeB.z), FfxFloat32x3(klhgR.w, klhgG.w, klhgB.w), FfxFloat32x3(ijfeR.y, ijfeG.y, ijfeB.y)), FfxFloat32x3(klhgR.x, klhgG.x, klhgB.x));
  337. // Accumulation.
  338. FfxFloat32x3 aC = ffxBroadcast3(0.0);
  339. FfxFloat32 aW = FfxFloat32(0.0);
  340. fsrEasuTapFloat(aC, aW, FfxFloat32x2(0.0, -1.0) - pp, dir, len2, lob, clp, FfxFloat32x3(bczzR.x, bczzG.x, bczzB.x)); // b
  341. fsrEasuTapFloat(aC, aW, FfxFloat32x2(1.0, -1.0) - pp, dir, len2, lob, clp, FfxFloat32x3(bczzR.y, bczzG.y, bczzB.y)); // c
  342. fsrEasuTapFloat(aC, aW, FfxFloat32x2(-1.0, 1.0) - pp, dir, len2, lob, clp, FfxFloat32x3(ijfeR.x, ijfeG.x, ijfeB.x)); // i
  343. fsrEasuTapFloat(aC, aW, FfxFloat32x2(0.0, 1.0) - pp, dir, len2, lob, clp, FfxFloat32x3(ijfeR.y, ijfeG.y, ijfeB.y)); // j
  344. fsrEasuTapFloat(aC, aW, FfxFloat32x2(0.0, 0.0) - pp, dir, len2, lob, clp, FfxFloat32x3(ijfeR.z, ijfeG.z, ijfeB.z)); // f
  345. fsrEasuTapFloat(aC, aW, FfxFloat32x2(-1.0, 0.0) - pp, dir, len2, lob, clp, FfxFloat32x3(ijfeR.w, ijfeG.w, ijfeB.w)); // e
  346. fsrEasuTapFloat(aC, aW, FfxFloat32x2(1.0, 1.0) - pp, dir, len2, lob, clp, FfxFloat32x3(klhgR.x, klhgG.x, klhgB.x)); // k
  347. fsrEasuTapFloat(aC, aW, FfxFloat32x2(2.0, 1.0) - pp, dir, len2, lob, clp, FfxFloat32x3(klhgR.y, klhgG.y, klhgB.y)); // l
  348. fsrEasuTapFloat(aC, aW, FfxFloat32x2(2.0, 0.0) - pp, dir, len2, lob, clp, FfxFloat32x3(klhgR.z, klhgG.z, klhgB.z)); // h
  349. fsrEasuTapFloat(aC, aW, FfxFloat32x2(1.0, 0.0) - pp, dir, len2, lob, clp, FfxFloat32x3(klhgR.w, klhgG.w, klhgB.w)); // g
  350. fsrEasuTapFloat(aC, aW, FfxFloat32x2(1.0, 2.0) - pp, dir, len2, lob, clp, FfxFloat32x3(zzonR.z, zzonG.z, zzonB.z)); // o
  351. fsrEasuTapFloat(aC, aW, FfxFloat32x2(0.0, 2.0) - pp, dir, len2, lob, clp, FfxFloat32x3(zzonR.w, zzonG.w, zzonB.w)); // n
  352. // Normalize and dering.
  353. pix = ffxMin(max4, max(min4, aC * ffxBroadcast3(rcp(aW))));
  354. }
  355. #endif // #if defined(FFX_GPU) && defined(FFX_FSR_EASU_FLOAT)
  356. #if defined(FFX_GPU) && FFX_HALF == 1 && defined(FFX_FSR_EASU_HALF)
  357. // Input callback prototypes, need to be implemented by calling shader
  358. FfxFloat16x4 FsrEasuRH(FfxFloat32x2 p);
  359. FfxFloat16x4 FsrEasuGH(FfxFloat32x2 p);
  360. FfxFloat16x4 FsrEasuBH(FfxFloat32x2 p);
  361. // This runs 2 taps in parallel.
  362. void FsrEasuTapH(
  363. FFX_PARAMETER_INOUT FfxFloat16x2 aCR,
  364. FFX_PARAMETER_INOUT FfxFloat16x2 aCG,
  365. FFX_PARAMETER_INOUT FfxFloat16x2 aCB,
  366. FFX_PARAMETER_INOUT FfxFloat16x2 aW,
  367. FFX_PARAMETER_IN FfxFloat16x2 offX,
  368. FFX_PARAMETER_IN FfxFloat16x2 offY,
  369. FFX_PARAMETER_IN FfxFloat16x2 dir,
  370. FFX_PARAMETER_IN FfxFloat16x2 len,
  371. FFX_PARAMETER_IN FfxFloat16 lob,
  372. FFX_PARAMETER_IN FfxFloat16 clp,
  373. FFX_PARAMETER_IN FfxFloat16x2 cR,
  374. FFX_PARAMETER_IN FfxFloat16x2 cG,
  375. FFX_PARAMETER_IN FfxFloat16x2 cB)
  376. {
  377. FfxFloat16x2 vX, vY;
  378. vX = offX * dir.xx + offY * dir.yy;
  379. vY = offX * (-dir.yy) + offY * dir.xx;
  380. vX *= len.x;
  381. vY *= len.y;
  382. FfxFloat16x2 d2 = vX * vX + vY * vY;
  383. d2 = min(d2, FFX_BROADCAST_FLOAT16X2(clp));
  384. FfxFloat16x2 wB = FFX_BROADCAST_FLOAT16X2(2.0 / 5.0) * d2 + FFX_BROADCAST_FLOAT16X2(-1.0);
  385. FfxFloat16x2 wA = FFX_BROADCAST_FLOAT16X2(lob) * d2 + FFX_BROADCAST_FLOAT16X2(-1.0);
  386. wB *= wB;
  387. wA *= wA;
  388. wB = FFX_BROADCAST_FLOAT16X2(25.0 / 16.0) * wB + FFX_BROADCAST_FLOAT16X2(-(25.0 / 16.0 - 1.0));
  389. FfxFloat16x2 w = wB * wA;
  390. aCR += cR * w;
  391. aCG += cG * w;
  392. aCB += cB * w;
  393. aW += w;
  394. }
  395. // This runs 2 taps in parallel.
  396. void FsrEasuSetH(
  397. FFX_PARAMETER_INOUT FfxFloat16x2 dirPX,
  398. FFX_PARAMETER_INOUT FfxFloat16x2 dirPY,
  399. FFX_PARAMETER_INOUT FfxFloat16x2 lenP,
  400. FFX_PARAMETER_IN FfxFloat16x2 pp,
  401. FFX_PARAMETER_IN FfxBoolean biST,
  402. FFX_PARAMETER_IN FfxBoolean biUV,
  403. FFX_PARAMETER_IN FfxFloat16x2 lA,
  404. FFX_PARAMETER_IN FfxFloat16x2 lB,
  405. FFX_PARAMETER_IN FfxFloat16x2 lC,
  406. FFX_PARAMETER_IN FfxFloat16x2 lD,
  407. FFX_PARAMETER_IN FfxFloat16x2 lE)
  408. {
  409. FfxFloat16x2 w = FFX_BROADCAST_FLOAT16X2(0.0);
  410. if (biST)
  411. w = (FfxFloat16x2(1.0, 0.0) + FfxFloat16x2(-pp.x, pp.x)) * FFX_BROADCAST_FLOAT16X2(FFX_BROADCAST_FLOAT16(1.0) - pp.y);
  412. if (biUV)
  413. w = (FfxFloat16x2(1.0, 0.0) + FfxFloat16x2(-pp.x, pp.x)) * FFX_BROADCAST_FLOAT16X2(pp.y);
  414. // ABS is not free in the packed FP16 path.
  415. FfxFloat16x2 dc = lD - lC;
  416. FfxFloat16x2 cb = lC - lB;
  417. FfxFloat16x2 lenX = max(abs(dc), abs(cb));
  418. lenX = ffxReciprocalHalf(lenX);
  419. FfxFloat16x2 dirX = lD - lB;
  420. dirPX += dirX * w;
  421. lenX = ffxSaturate(abs(dirX) * lenX);
  422. lenX *= lenX;
  423. lenP += lenX * w;
  424. FfxFloat16x2 ec = lE - lC;
  425. FfxFloat16x2 ca = lC - lA;
  426. FfxFloat16x2 lenY = max(abs(ec), abs(ca));
  427. lenY = ffxReciprocalHalf(lenY);
  428. FfxFloat16x2 dirY = lE - lA;
  429. dirPY += dirY * w;
  430. lenY = ffxSaturate(abs(dirY) * lenY);
  431. lenY *= lenY;
  432. lenP += lenY * w;
  433. }
  434. void FsrEasuH(
  435. FFX_PARAMETER_OUT FfxFloat16x3 pix,
  436. FFX_PARAMETER_IN FfxUInt32x2 ip,
  437. FFX_PARAMETER_IN FfxUInt32x4 con0,
  438. FFX_PARAMETER_IN FfxUInt32x4 con1,
  439. FFX_PARAMETER_IN FfxUInt32x4 con2,
  440. FFX_PARAMETER_IN FfxUInt32x4 con3)
  441. {
  442. FfxFloat32x2 pp = FfxFloat32x2(ip) * ffxAsFloat(con0.xy) + ffxAsFloat(con0.zw);
  443. FfxFloat32x2 fp = floor(pp);
  444. pp -= fp;
  445. FfxFloat16x2 ppp = FfxFloat16x2(pp);
  446. FfxFloat32x2 p0 = fp * ffxAsFloat(con1.xy) + ffxAsFloat(con1.zw);
  447. FfxFloat32x2 p1 = p0 + ffxAsFloat(con2.xy);
  448. FfxFloat32x2 p2 = p0 + ffxAsFloat(con2.zw);
  449. FfxFloat32x2 p3 = p0 + ffxAsFloat(con3.xy);
  450. FfxFloat16x4 bczzR = FsrEasuRH(p0);
  451. FfxFloat16x4 bczzG = FsrEasuGH(p0);
  452. FfxFloat16x4 bczzB = FsrEasuBH(p0);
  453. FfxFloat16x4 ijfeR = FsrEasuRH(p1);
  454. FfxFloat16x4 ijfeG = FsrEasuGH(p1);
  455. FfxFloat16x4 ijfeB = FsrEasuBH(p1);
  456. FfxFloat16x4 klhgR = FsrEasuRH(p2);
  457. FfxFloat16x4 klhgG = FsrEasuGH(p2);
  458. FfxFloat16x4 klhgB = FsrEasuBH(p2);
  459. FfxFloat16x4 zzonR = FsrEasuRH(p3);
  460. FfxFloat16x4 zzonG = FsrEasuGH(p3);
  461. FfxFloat16x4 zzonB = FsrEasuBH(p3);
  462. FfxFloat16x4 bczzL = bczzB * FFX_BROADCAST_FLOAT16X4(0.5) + (bczzR * FFX_BROADCAST_FLOAT16X4(0.5) + bczzG);
  463. FfxFloat16x4 ijfeL = ijfeB * FFX_BROADCAST_FLOAT16X4(0.5) + (ijfeR * FFX_BROADCAST_FLOAT16X4(0.5) + ijfeG);
  464. FfxFloat16x4 klhgL = klhgB * FFX_BROADCAST_FLOAT16X4(0.5) + (klhgR * FFX_BROADCAST_FLOAT16X4(0.5) + klhgG);
  465. FfxFloat16x4 zzonL = zzonB * FFX_BROADCAST_FLOAT16X4(0.5) + (zzonR * FFX_BROADCAST_FLOAT16X4(0.5) + zzonG);
  466. FfxFloat16 bL = bczzL.x;
  467. FfxFloat16 cL = bczzL.y;
  468. FfxFloat16 iL = ijfeL.x;
  469. FfxFloat16 jL = ijfeL.y;
  470. FfxFloat16 fL = ijfeL.z;
  471. FfxFloat16 eL = ijfeL.w;
  472. FfxFloat16 kL = klhgL.x;
  473. FfxFloat16 lL = klhgL.y;
  474. FfxFloat16 hL = klhgL.z;
  475. FfxFloat16 gL = klhgL.w;
  476. FfxFloat16 oL = zzonL.z;
  477. FfxFloat16 nL = zzonL.w;
  478. // This part is different, accumulating 2 taps in parallel.
  479. FfxFloat16x2 dirPX = FFX_BROADCAST_FLOAT16X2(0.0);
  480. FfxFloat16x2 dirPY = FFX_BROADCAST_FLOAT16X2(0.0);
  481. FfxFloat16x2 lenP = FFX_BROADCAST_FLOAT16X2(0.0);
  482. FsrEasuSetH(dirPX,
  483. dirPY,
  484. lenP,
  485. ppp,
  486. FfxUInt32(true),
  487. FfxUInt32(false),
  488. FfxFloat16x2(bL, cL),
  489. FfxFloat16x2(eL, fL),
  490. FfxFloat16x2(fL, gL),
  491. FfxFloat16x2(gL, hL),
  492. FfxFloat16x2(jL, kL));
  493. FsrEasuSetH(dirPX,
  494. dirPY,
  495. lenP,
  496. ppp,
  497. FfxUInt32(false),
  498. FfxUInt32(true),
  499. FfxFloat16x2(fL, gL),
  500. FfxFloat16x2(iL, jL),
  501. FfxFloat16x2(jL, kL),
  502. FfxFloat16x2(kL, lL),
  503. FfxFloat16x2(nL, oL));
  504. FfxFloat16x2 dir = FfxFloat16x2(dirPX.r + dirPX.g, dirPY.r + dirPY.g);
  505. FfxFloat16 len = lenP.r + lenP.g;
  506. FfxFloat16x2 dir2 = dir * dir;
  507. FfxFloat16 dirR = dir2.x + dir2.y;
  508. FfxBoolean zro = FfxBoolean(dirR < FFX_BROADCAST_FLOAT16(1.0 / 32768.0));
  509. dirR = ffxApproximateReciprocalSquareRootHalf(dirR);
  510. dirR = (zro > 0) ? FFX_BROADCAST_FLOAT16(1.0) : dirR;
  511. dir.x = (zro > 0) ? FFX_BROADCAST_FLOAT16(1.0) : dir.x;
  512. dir *= FFX_BROADCAST_FLOAT16X2(dirR);
  513. len = len * FFX_BROADCAST_FLOAT16(0.5);
  514. len *= len;
  515. FfxFloat16 stretch = (dir.x * dir.x + dir.y * dir.y) * ffxApproximateReciprocalHalf(max(abs(dir.x), abs(dir.y)));
  516. FfxFloat16x2 len2 =
  517. FfxFloat16x2(FFX_BROADCAST_FLOAT16(1.0) + (stretch - FFX_BROADCAST_FLOAT16(1.0)) * len, FFX_BROADCAST_FLOAT16(1.0) + FFX_BROADCAST_FLOAT16(-0.5) * len);
  518. FfxFloat16 lob = FFX_BROADCAST_FLOAT16(0.5) + FFX_BROADCAST_FLOAT16((1.0 / 4.0 - 0.04) - 0.5) * len;
  519. FfxFloat16 clp = ffxApproximateReciprocalHalf(lob);
  520. // FP16 is different, using packed trick to do min and max in same operation.
  521. FfxFloat16x2 bothR =
  522. max(max(FfxFloat16x2(-ijfeR.z, ijfeR.z), FfxFloat16x2(-klhgR.w, klhgR.w)), max(FfxFloat16x2(-ijfeR.y, ijfeR.y), FfxFloat16x2(-klhgR.x, klhgR.x)));
  523. FfxFloat16x2 bothG =
  524. max(max(FfxFloat16x2(-ijfeG.z, ijfeG.z), FfxFloat16x2(-klhgG.w, klhgG.w)), max(FfxFloat16x2(-ijfeG.y, ijfeG.y), FfxFloat16x2(-klhgG.x, klhgG.x)));
  525. FfxFloat16x2 bothB =
  526. max(max(FfxFloat16x2(-ijfeB.z, ijfeB.z), FfxFloat16x2(-klhgB.w, klhgB.w)), max(FfxFloat16x2(-ijfeB.y, ijfeB.y), FfxFloat16x2(-klhgB.x, klhgB.x)));
  527. // This part is different for FP16, working pairs of taps at a time.
  528. FfxFloat16x2 pR = FFX_BROADCAST_FLOAT16X2(0.0);
  529. FfxFloat16x2 pG = FFX_BROADCAST_FLOAT16X2(0.0);
  530. FfxFloat16x2 pB = FFX_BROADCAST_FLOAT16X2(0.0);
  531. FfxFloat16x2 pW = FFX_BROADCAST_FLOAT16X2(0.0);
  532. FsrEasuTapH(pR, pG, pB, pW, FfxFloat16x2(0.0, 1.0) - ppp.xx, FfxFloat16x2(-1.0, -1.0) - ppp.yy, dir, len2, lob, clp, bczzR.xy, bczzG.xy, bczzB.xy);
  533. FsrEasuTapH(pR, pG, pB, pW, FfxFloat16x2(-1.0, 0.0) - ppp.xx, FfxFloat16x2(1.0, 1.0) - ppp.yy, dir, len2, lob, clp, ijfeR.xy, ijfeG.xy, ijfeB.xy);
  534. FsrEasuTapH(pR, pG, pB, pW, FfxFloat16x2(0.0, -1.0) - ppp.xx, FfxFloat16x2(0.0, 0.0) - ppp.yy, dir, len2, lob, clp, ijfeR.zw, ijfeG.zw, ijfeB.zw);
  535. FsrEasuTapH(pR, pG, pB, pW, FfxFloat16x2(1.0, 2.0) - ppp.xx, FfxFloat16x2(1.0, 1.0) - ppp.yy, dir, len2, lob, clp, klhgR.xy, klhgG.xy, klhgB.xy);
  536. FsrEasuTapH(pR, pG, pB, pW, FfxFloat16x2(2.0, 1.0) - ppp.xx, FfxFloat16x2(0.0, 0.0) - ppp.yy, dir, len2, lob, clp, klhgR.zw, klhgG.zw, klhgB.zw);
  537. FsrEasuTapH(pR, pG, pB, pW, FfxFloat16x2(1.0, 0.0) - ppp.xx, FfxFloat16x2(2.0, 2.0) - ppp.yy, dir, len2, lob, clp, zzonR.zw, zzonG.zw, zzonB.zw);
  538. FfxFloat16x3 aC = FfxFloat16x3(pR.x + pR.y, pG.x + pG.y, pB.x + pB.y);
  539. FfxFloat16 aW = pW.x + pW.y;
  540. // Slightly different for FP16 version due to combined min and max.
  541. pix = min(FfxFloat16x3(bothR.y, bothG.y, bothB.y), max(-FfxFloat16x3(bothR.x, bothG.x, bothB.x), aC * FFX_BROADCAST_FLOAT16X3(ffxReciprocalHalf(aW))));
  542. }
  543. #endif // #if defined(FFX_GPU) && defined(FFX_HALF) && defined(FFX_FSR_EASU_HALF)
  544. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  545. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  546. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  547. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  548. //_____________________________________________________________/\_______________________________________________________________
  549. //==============================================================================================================================
  550. //
  551. // FSR - [RCAS] ROBUST CONTRAST ADAPTIVE SHARPENING
  552. //
  553. //------------------------------------------------------------------------------------------------------------------------------
  554. // CAS uses a simplified mechanism to convert local contrast into a variable amount of sharpness.
  555. // RCAS uses a more exact mechanism, solving for the maximum local sharpness possible before clipping.
  556. // RCAS also has a built in process to limit sharpening of what it detects as possible noise.
  557. // RCAS sharper does not support scaling, as it should be applied after EASU scaling.
  558. // Pass EASU output straight into RCAS, no color conversions necessary.
  559. //------------------------------------------------------------------------------------------------------------------------------
  560. // RCAS is based on the following logic.
  561. // RCAS uses a 5 tap filter in a cross pattern (same as CAS),
  562. // w n
  563. // w 1 w for taps w m e
  564. // w s
  565. // Where 'w' is the negative lobe weight.
  566. // output = (w*(n+e+w+s)+m)/(4*w+1)
  567. // RCAS solves for 'w' by seeing where the signal might clip out of the {0 to 1} input range,
  568. // 0 == (w*(n+e+w+s)+m)/(4*w+1) -> w = -m/(n+e+w+s)
  569. // 1 == (w*(n+e+w+s)+m)/(4*w+1) -> w = (1-m)/(n+e+w+s-4*1)
  570. // Then chooses the 'w' which results in no clipping, limits 'w', and multiplies by the 'sharp' amount.
  571. // This solution above has issues with MSAA input as the steps along the gradient cause edge detection issues.
  572. // So RCAS uses 4x the maximum and 4x the minimum (depending on equation)in place of the individual taps.
  573. // As well as switching from 'm' to either the minimum or maximum (depending on side), to help in energy conservation.
  574. // This stabilizes RCAS.
  575. // RCAS does a simple highpass which is normalized against the local contrast then shaped,
  576. // 0.25
  577. // 0.25 -1 0.25
  578. // 0.25
  579. // This is used as a noise detection filter, to reduce the effect of RCAS on grain, and focus on real edges.
  580. //
  581. // GLSL example for the required callbacks :
  582. //
  583. // FfxFloat16x4 FsrRcasLoadH(FfxInt16x2 p){return FfxFloat16x4(imageLoad(imgSrc,FfxInt32x2(p)));}
  584. // void FsrRcasInputH(inout FfxFloat16 r,inout FfxFloat16 g,inout FfxFloat16 b)
  585. // {
  586. // //do any simple input color conversions here or leave empty if none needed
  587. // }
  588. //
  589. // FsrRcasCon need to be called from the CPU or GPU to set up constants.
  590. // Including a GPU example here, the 'con' value would be stored out to a constant buffer.
  591. //
  592. // FfxUInt32x4 con;
  593. // FsrRcasCon(con,
  594. // 0.0); // The scale is {0.0 := maximum sharpness, to N>0, where N is the number of stops (halving) of the reduction of sharpness}.
  595. // ---------------
  596. // RCAS sharpening supports a CAS-like pass-through alpha via,
  597. // #define FSR_RCAS_PASSTHROUGH_ALPHA 1
  598. // RCAS also supports a define to enable a more expensive path to avoid some sharpening of noise.
  599. // Would suggest it is better to apply film grain after RCAS sharpening (and after scaling) instead of using this define,
  600. // #define FSR_RCAS_DENOISE 1
  601. //==============================================================================================================================
  602. // This is set at the limit of providing unnatural results for sharpening.
  603. #define FSR_RCAS_LIMIT (0.25-(1.0/16.0))
  604. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  605. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  606. //_____________________________________________________________/\_______________________________________________________________
  607. //==============================================================================================================================
  608. // CONSTANT SETUP
  609. //==============================================================================================================================
  610. // Call to setup required constant values (works on CPU or GPU).
  611. FFX_STATIC void FsrRcasCon(FfxUInt32x4 con,
  612. // The scale is {0.0 := maximum, to N>0, where N is the number of stops (halving) of the reduction of sharpness}.
  613. FfxFloat32 sharpness)
  614. {
  615. // Transform from stops to linear value.
  616. sharpness = exp2(-sharpness);
  617. FfxFloat32x2 hSharp = {sharpness, sharpness};
  618. con[0] = ffxAsUInt32(sharpness);
  619. con[1] = packHalf2x16(hSharp);
  620. con[2] = 0;
  621. con[3] = 0;
  622. }
  623. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  624. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  625. //_____________________________________________________________/\_______________________________________________________________
  626. //==============================================================================================================================
  627. // NON-PACKED 32-BIT VERSION
  628. //==============================================================================================================================
  629. #if defined(FFX_GPU)&&defined(FSR_RCAS_F)
  630. // Input callback prototypes that need to be implemented by calling shader
  631. FfxFloat32x4 FsrRcasLoadF(FfxInt32x2 p);
  632. void FsrRcasInputF(inout FfxFloat32 r,inout FfxFloat32 g,inout FfxFloat32 b);
  633. //------------------------------------------------------------------------------------------------------------------------------
  634. void FsrRcasF(out FfxFloat32 pixR, // Output values, non-vector so port between RcasFilter() and RcasFilterH() is easy.
  635. out FfxFloat32 pixG,
  636. out FfxFloat32 pixB,
  637. #ifdef FSR_RCAS_PASSTHROUGH_ALPHA
  638. out FfxFloat32 pixA,
  639. #endif
  640. FfxUInt32x2 ip, // Integer pixel position in output.
  641. FfxUInt32x4 con)
  642. { // Constant generated by RcasSetup().
  643. // Algorithm uses minimal 3x3 pixel neighborhood.
  644. // b
  645. // d e f
  646. // h
  647. FfxInt32x2 sp = FfxInt32x2(ip);
  648. FfxFloat32x3 b = FsrRcasLoadF(sp + FfxInt32x2(0, -1)).rgb;
  649. FfxFloat32x3 d = FsrRcasLoadF(sp + FfxInt32x2(-1, 0)).rgb;
  650. #ifdef FSR_RCAS_PASSTHROUGH_ALPHA
  651. FfxFloat32x4 ee = FsrRcasLoadF(sp);
  652. FfxFloat32x3 e = ee.rgb;
  653. pixA = ee.a;
  654. #else
  655. FfxFloat32x3 e = FsrRcasLoadF(sp).rgb;
  656. #endif
  657. FfxFloat32x3 f = FsrRcasLoadF(sp + FfxInt32x2(1, 0)).rgb;
  658. FfxFloat32x3 h = FsrRcasLoadF(sp + FfxInt32x2(0, 1)).rgb;
  659. // Rename (32-bit) or regroup (16-bit).
  660. FfxFloat32 bR = b.r;
  661. FfxFloat32 bG = b.g;
  662. FfxFloat32 bB = b.b;
  663. FfxFloat32 dR = d.r;
  664. FfxFloat32 dG = d.g;
  665. FfxFloat32 dB = d.b;
  666. FfxFloat32 eR = e.r;
  667. FfxFloat32 eG = e.g;
  668. FfxFloat32 eB = e.b;
  669. FfxFloat32 fR = f.r;
  670. FfxFloat32 fG = f.g;
  671. FfxFloat32 fB = f.b;
  672. FfxFloat32 hR = h.r;
  673. FfxFloat32 hG = h.g;
  674. FfxFloat32 hB = h.b;
  675. // Run optional input transform.
  676. FsrRcasInputF(bR, bG, bB);
  677. FsrRcasInputF(dR, dG, dB);
  678. FsrRcasInputF(eR, eG, eB);
  679. FsrRcasInputF(fR, fG, fB);
  680. FsrRcasInputF(hR, hG, hB);
  681. // Luma times 2.
  682. FfxFloat32 bL = bB * FfxFloat32(0.5) + (bR * FfxFloat32(0.5) + bG);
  683. FfxFloat32 dL = dB * FfxFloat32(0.5) + (dR * FfxFloat32(0.5) + dG);
  684. FfxFloat32 eL = eB * FfxFloat32(0.5) + (eR * FfxFloat32(0.5) + eG);
  685. FfxFloat32 fL = fB * FfxFloat32(0.5) + (fR * FfxFloat32(0.5) + fG);
  686. FfxFloat32 hL = hB * FfxFloat32(0.5) + (hR * FfxFloat32(0.5) + hG);
  687. // Noise detection.
  688. FfxFloat32 nz = FfxFloat32(0.25) * bL + FfxFloat32(0.25) * dL + FfxFloat32(0.25) * fL + FfxFloat32(0.25) * hL - eL;
  689. nz = ffxSaturate(abs(nz) * ffxApproximateReciprocalMedium(ffxMax3(ffxMax3(bL, dL, eL), fL, hL) - ffxMin3(ffxMin3(bL, dL, eL), fL, hL)));
  690. nz = FfxFloat32(-0.5) * nz + FfxFloat32(1.0);
  691. // Min and max of ring.
  692. FfxFloat32 mn4R = ffxMin(ffxMin3(bR, dR, fR), hR);
  693. FfxFloat32 mn4G = ffxMin(ffxMin3(bG, dG, fG), hG);
  694. FfxFloat32 mn4B = ffxMin(ffxMin3(bB, dB, fB), hB);
  695. FfxFloat32 mx4R = max(ffxMax3(bR, dR, fR), hR);
  696. FfxFloat32 mx4G = max(ffxMax3(bG, dG, fG), hG);
  697. FfxFloat32 mx4B = max(ffxMax3(bB, dB, fB), hB);
  698. // Immediate constants for peak range.
  699. FfxFloat32x2 peakC = FfxFloat32x2(1.0, -1.0 * 4.0);
  700. // Limiters, these need to be high precision RCPs.
  701. FfxFloat32 hitMinR = mn4R * rcp(FfxFloat32(4.0) * mx4R);
  702. FfxFloat32 hitMinG = mn4G * rcp(FfxFloat32(4.0) * mx4G);
  703. FfxFloat32 hitMinB = mn4B * rcp(FfxFloat32(4.0) * mx4B);
  704. FfxFloat32 hitMaxR = (peakC.x - mx4R) * rcp(FfxFloat32(4.0) * mn4R + peakC.y);
  705. FfxFloat32 hitMaxG = (peakC.x - mx4G) * rcp(FfxFloat32(4.0) * mn4G + peakC.y);
  706. FfxFloat32 hitMaxB = (peakC.x - mx4B) * rcp(FfxFloat32(4.0) * mn4B + peakC.y);
  707. FfxFloat32 lobeR = max(-hitMinR, hitMaxR);
  708. FfxFloat32 lobeG = max(-hitMinG, hitMaxG);
  709. FfxFloat32 lobeB = max(-hitMinB, hitMaxB);
  710. FfxFloat32 lobe = max(FfxFloat32(-FSR_RCAS_LIMIT), ffxMin(ffxMax3(lobeR, lobeG, lobeB), FfxFloat32(0.0))) * ffxAsFloat
  711. (con.x);
  712. // Apply noise removal.
  713. #ifdef FSR_RCAS_DENOISE
  714. lobe *= nz;
  715. #endif
  716. // Resolve, which needs the medium precision rcp approximation to avoid visible tonality changes.
  717. FfxFloat32 rcpL = ffxApproximateReciprocalMedium(FfxFloat32(4.0) * lobe + FfxFloat32(1.0));
  718. pixR = (lobe * bR + lobe * dR + lobe * hR + lobe * fR + eR) * rcpL;
  719. pixG = (lobe * bG + lobe * dG + lobe * hG + lobe * fG + eG) * rcpL;
  720. pixB = (lobe * bB + lobe * dB + lobe * hB + lobe * fB + eB) * rcpL;
  721. return;
  722. }
  723. #endif
  724. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  725. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  726. //_____________________________________________________________/\_______________________________________________________________
  727. //==============================================================================================================================
  728. // NON-PACKED 16-BIT VERSION
  729. //==============================================================================================================================
  730. #if defined(FFX_GPU) && FFX_HALF == 1 && defined(FSR_RCAS_H)
  731. // Input callback prototypes that need to be implemented by calling shader
  732. FfxFloat16x4 FsrRcasLoadH(FfxInt16x2 p);
  733. void FsrRcasInputH(inout FfxFloat16 r,inout FfxFloat16 g,inout FfxFloat16 b);
  734. //------------------------------------------------------------------------------------------------------------------------------
  735. void FsrRcasH(
  736. out FfxFloat16 pixR, // Output values, non-vector so port between RcasFilter() and RcasFilterH() is easy.
  737. out FfxFloat16 pixG,
  738. out FfxFloat16 pixB,
  739. #ifdef FSR_RCAS_PASSTHROUGH_ALPHA
  740. out FfxFloat16 pixA,
  741. #endif
  742. FfxUInt32x2 ip, // Integer pixel position in output.
  743. FfxUInt32x4 con){ // Constant generated by RcasSetup().
  744. // Sharpening algorithm uses minimal 3x3 pixel neighborhood.
  745. // b
  746. // d e f
  747. // h
  748. FfxInt16x2 sp=FfxInt16x2(ip);
  749. FfxFloat16x3 b=FsrRcasLoadH(sp+FfxInt16x2( 0,-1)).rgb;
  750. FfxFloat16x3 d=FsrRcasLoadH(sp+FfxInt16x2(-1, 0)).rgb;
  751. #ifdef FSR_RCAS_PASSTHROUGH_ALPHA
  752. FfxFloat16x4 ee=FsrRcasLoadH(sp);
  753. FfxFloat16x3 e=ee.rgb;pixA=ee.a;
  754. #else
  755. FfxFloat16x3 e=FsrRcasLoadH(sp).rgb;
  756. #endif
  757. FfxFloat16x3 f=FsrRcasLoadH(sp+FfxInt16x2( 1, 0)).rgb;
  758. FfxFloat16x3 h=FsrRcasLoadH(sp+FfxInt16x2( 0, 1)).rgb;
  759. // Rename (32-bit) or regroup (16-bit).
  760. FfxFloat16 bR=b.r;
  761. FfxFloat16 bG=b.g;
  762. FfxFloat16 bB=b.b;
  763. FfxFloat16 dR=d.r;
  764. FfxFloat16 dG=d.g;
  765. FfxFloat16 dB=d.b;
  766. FfxFloat16 eR=e.r;
  767. FfxFloat16 eG=e.g;
  768. FfxFloat16 eB=e.b;
  769. FfxFloat16 fR=f.r;
  770. FfxFloat16 fG=f.g;
  771. FfxFloat16 fB=f.b;
  772. FfxFloat16 hR=h.r;
  773. FfxFloat16 hG=h.g;
  774. FfxFloat16 hB=h.b;
  775. // Run optional input transform.
  776. FsrRcasInputH(bR,bG,bB);
  777. FsrRcasInputH(dR,dG,dB);
  778. FsrRcasInputH(eR,eG,eB);
  779. FsrRcasInputH(fR,fG,fB);
  780. FsrRcasInputH(hR,hG,hB);
  781. // Luma times 2.
  782. FfxFloat16 bL=bB*FFX_BROADCAST_FLOAT16(0.5)+(bR*FFX_BROADCAST_FLOAT16(0.5)+bG);
  783. FfxFloat16 dL=dB*FFX_BROADCAST_FLOAT16(0.5)+(dR*FFX_BROADCAST_FLOAT16(0.5)+dG);
  784. FfxFloat16 eL=eB*FFX_BROADCAST_FLOAT16(0.5)+(eR*FFX_BROADCAST_FLOAT16(0.5)+eG);
  785. FfxFloat16 fL=fB*FFX_BROADCAST_FLOAT16(0.5)+(fR*FFX_BROADCAST_FLOAT16(0.5)+fG);
  786. FfxFloat16 hL=hB*FFX_BROADCAST_FLOAT16(0.5)+(hR*FFX_BROADCAST_FLOAT16(0.5)+hG);
  787. // Noise detection.
  788. FfxFloat16 nz=FFX_BROADCAST_FLOAT16(0.25)*bL+FFX_BROADCAST_FLOAT16(0.25)*dL+FFX_BROADCAST_FLOAT16(0.25)*fL+FFX_BROADCAST_FLOAT16(0.25)*hL-eL;
  789. nz=ffxSaturate(abs(nz)*ffxApproximateReciprocalMediumHalf(ffxMax3Half(ffxMax3Half(bL,dL,eL),fL,hL)-ffxMin3Half(ffxMin3Half(bL,dL,eL),fL,hL)));
  790. nz=FFX_BROADCAST_FLOAT16(-0.5)*nz+FFX_BROADCAST_FLOAT16(1.0);
  791. // Min and max of ring.
  792. FfxFloat16 mn4R=min(ffxMin3Half(bR,dR,fR),hR);
  793. FfxFloat16 mn4G=min(ffxMin3Half(bG,dG,fG),hG);
  794. FfxFloat16 mn4B=min(ffxMin3Half(bB,dB,fB),hB);
  795. FfxFloat16 mx4R=max(ffxMax3Half(bR,dR,fR),hR);
  796. FfxFloat16 mx4G=max(ffxMax3Half(bG,dG,fG),hG);
  797. FfxFloat16 mx4B=max(ffxMax3Half(bB,dB,fB),hB);
  798. // Immediate constants for peak range.
  799. FfxFloat16x2 peakC=FfxFloat16x2(1.0,-1.0*4.0);
  800. // Limiters, these need to be high precision RCPs.
  801. FfxFloat16 hitMinR=mn4R*ffxReciprocalHalf(FFX_BROADCAST_FLOAT16(4.0)*mx4R);
  802. FfxFloat16 hitMinG=mn4G*ffxReciprocalHalf(FFX_BROADCAST_FLOAT16(4.0)*mx4G);
  803. FfxFloat16 hitMinB=mn4B*ffxReciprocalHalf(FFX_BROADCAST_FLOAT16(4.0)*mx4B);
  804. FfxFloat16 hitMaxR=(peakC.x-mx4R)*ffxReciprocalHalf(FFX_BROADCAST_FLOAT16(4.0)*mn4R+peakC.y);
  805. FfxFloat16 hitMaxG=(peakC.x-mx4G)*ffxReciprocalHalf(FFX_BROADCAST_FLOAT16(4.0)*mn4G+peakC.y);
  806. FfxFloat16 hitMaxB=(peakC.x-mx4B)*ffxReciprocalHalf(FFX_BROADCAST_FLOAT16(4.0)*mn4B+peakC.y);
  807. FfxFloat16 lobeR=max(-hitMinR,hitMaxR);
  808. FfxFloat16 lobeG=max(-hitMinG,hitMaxG);
  809. FfxFloat16 lobeB=max(-hitMinB,hitMaxB);
  810. FfxFloat16 lobe=max(FFX_BROADCAST_FLOAT16(-FSR_RCAS_LIMIT),min(ffxMax3Half(lobeR,lobeG,lobeB),FFX_BROADCAST_FLOAT16(0.0)))*FFX_UINT32_TO_FLOAT16X2(con.y).x;
  811. // Apply noise removal.
  812. #ifdef FSR_RCAS_DENOISE
  813. lobe*=nz;
  814. #endif
  815. // Resolve, which needs the medium precision rcp approximation to avoid visible tonality changes.
  816. FfxFloat16 rcpL=ffxApproximateReciprocalMediumHalf(FFX_BROADCAST_FLOAT16(4.0)*lobe+FFX_BROADCAST_FLOAT16(1.0));
  817. pixR=(lobe*bR+lobe*dR+lobe*hR+lobe*fR+eR)*rcpL;
  818. pixG=(lobe*bG+lobe*dG+lobe*hG+lobe*fG+eG)*rcpL;
  819. pixB=(lobe*bB+lobe*dB+lobe*hB+lobe*fB+eB)*rcpL;
  820. }
  821. #endif
  822. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  823. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  824. //_____________________________________________________________/\_______________________________________________________________
  825. //==============================================================================================================================
  826. // PACKED 16-BIT VERSION
  827. //==============================================================================================================================
  828. #if defined(FFX_GPU)&& FFX_HALF == 1 && defined(FSR_RCAS_HX2)
  829. // Input callback prototypes that need to be implemented by the calling shader
  830. FfxFloat16x4 FsrRcasLoadHx2(FfxInt16x2 p);
  831. void FsrRcasInputHx2(inout FfxFloat16x2 r,inout FfxFloat16x2 g,inout FfxFloat16x2 b);
  832. //------------------------------------------------------------------------------------------------------------------------------
  833. // Can be used to convert from packed Structures of Arrays to Arrays of Structures for store.
  834. void FsrRcasDepackHx2(out FfxFloat16x4 pix0,out FfxFloat16x4 pix1,FfxFloat16x2 pixR,FfxFloat16x2 pixG,FfxFloat16x2 pixB){
  835. #ifdef FFX_HLSL
  836. // Invoke a slower path for DX only, since it won't allow uninitialized values.
  837. pix0.a=pix1.a=0.0;
  838. #endif
  839. pix0.rgb=FfxFloat16x3(pixR.x,pixG.x,pixB.x);
  840. pix1.rgb=FfxFloat16x3(pixR.y,pixG.y,pixB.y);}
  841. //------------------------------------------------------------------------------------------------------------------------------
  842. void FsrRcasHx2(
  843. // Output values are for 2 8x8 tiles in a 16x8 region.
  844. // pix<R,G,B>.x = left 8x8 tile
  845. // pix<R,G,B>.y = right 8x8 tile
  846. // This enables later processing to easily be packed as well.
  847. out FfxFloat16x2 pixR,
  848. out FfxFloat16x2 pixG,
  849. out FfxFloat16x2 pixB,
  850. #ifdef FSR_RCAS_PASSTHROUGH_ALPHA
  851. out FfxFloat16x2 pixA,
  852. #endif
  853. FfxUInt32x2 ip, // Integer pixel position in output.
  854. FfxUInt32x4 con){ // Constant generated by RcasSetup().
  855. // No scaling algorithm uses minimal 3x3 pixel neighborhood.
  856. FfxInt16x2 sp0=FfxInt16x2(ip);
  857. FfxFloat16x3 b0=FsrRcasLoadHx2(sp0+FfxInt16x2( 0,-1)).rgb;
  858. FfxFloat16x3 d0=FsrRcasLoadHx2(sp0+FfxInt16x2(-1, 0)).rgb;
  859. #ifdef FSR_RCAS_PASSTHROUGH_ALPHA
  860. FfxFloat16x4 ee0=FsrRcasLoadHx2(sp0);
  861. FfxFloat16x3 e0=ee0.rgb;pixA.r=ee0.a;
  862. #else
  863. FfxFloat16x3 e0=FsrRcasLoadHx2(sp0).rgb;
  864. #endif
  865. FfxFloat16x3 f0=FsrRcasLoadHx2(sp0+FfxInt16x2( 1, 0)).rgb;
  866. FfxFloat16x3 h0=FsrRcasLoadHx2(sp0+FfxInt16x2( 0, 1)).rgb;
  867. FfxInt16x2 sp1=sp0+FfxInt16x2(8,0);
  868. FfxFloat16x3 b1=FsrRcasLoadHx2(sp1+FfxInt16x2( 0,-1)).rgb;
  869. FfxFloat16x3 d1=FsrRcasLoadHx2(sp1+FfxInt16x2(-1, 0)).rgb;
  870. #ifdef FSR_RCAS_PASSTHROUGH_ALPHA
  871. FfxFloat16x4 ee1=FsrRcasLoadHx2(sp1);
  872. FfxFloat16x3 e1=ee1.rgb;pixA.g=ee1.a;
  873. #else
  874. FfxFloat16x3 e1=FsrRcasLoadHx2(sp1).rgb;
  875. #endif
  876. FfxFloat16x3 f1=FsrRcasLoadHx2(sp1+FfxInt16x2( 1, 0)).rgb;
  877. FfxFloat16x3 h1=FsrRcasLoadHx2(sp1+FfxInt16x2( 0, 1)).rgb;
  878. // Arrays of Structures to Structures of Arrays conversion.
  879. FfxFloat16x2 bR=FfxFloat16x2(b0.r,b1.r);
  880. FfxFloat16x2 bG=FfxFloat16x2(b0.g,b1.g);
  881. FfxFloat16x2 bB=FfxFloat16x2(b0.b,b1.b);
  882. FfxFloat16x2 dR=FfxFloat16x2(d0.r,d1.r);
  883. FfxFloat16x2 dG=FfxFloat16x2(d0.g,d1.g);
  884. FfxFloat16x2 dB=FfxFloat16x2(d0.b,d1.b);
  885. FfxFloat16x2 eR=FfxFloat16x2(e0.r,e1.r);
  886. FfxFloat16x2 eG=FfxFloat16x2(e0.g,e1.g);
  887. FfxFloat16x2 eB=FfxFloat16x2(e0.b,e1.b);
  888. FfxFloat16x2 fR=FfxFloat16x2(f0.r,f1.r);
  889. FfxFloat16x2 fG=FfxFloat16x2(f0.g,f1.g);
  890. FfxFloat16x2 fB=FfxFloat16x2(f0.b,f1.b);
  891. FfxFloat16x2 hR=FfxFloat16x2(h0.r,h1.r);
  892. FfxFloat16x2 hG=FfxFloat16x2(h0.g,h1.g);
  893. FfxFloat16x2 hB=FfxFloat16x2(h0.b,h1.b);
  894. // Run optional input transform.
  895. FsrRcasInputHx2(bR,bG,bB);
  896. FsrRcasInputHx2(dR,dG,dB);
  897. FsrRcasInputHx2(eR,eG,eB);
  898. FsrRcasInputHx2(fR,fG,fB);
  899. FsrRcasInputHx2(hR,hG,hB);
  900. // Luma times 2.
  901. FfxFloat16x2 bL=bB*FFX_BROADCAST_FLOAT16X2(0.5)+(bR*FFX_BROADCAST_FLOAT16X2(0.5)+bG);
  902. FfxFloat16x2 dL=dB*FFX_BROADCAST_FLOAT16X2(0.5)+(dR*FFX_BROADCAST_FLOAT16X2(0.5)+dG);
  903. FfxFloat16x2 eL=eB*FFX_BROADCAST_FLOAT16X2(0.5)+(eR*FFX_BROADCAST_FLOAT16X2(0.5)+eG);
  904. FfxFloat16x2 fL=fB*FFX_BROADCAST_FLOAT16X2(0.5)+(fR*FFX_BROADCAST_FLOAT16X2(0.5)+fG);
  905. FfxFloat16x2 hL=hB*FFX_BROADCAST_FLOAT16X2(0.5)+(hR*FFX_BROADCAST_FLOAT16X2(0.5)+hG);
  906. // Noise detection.
  907. FfxFloat16x2 nz=FFX_BROADCAST_FLOAT16X2(0.25)*bL+FFX_BROADCAST_FLOAT16X2(0.25)*dL+FFX_BROADCAST_FLOAT16X2(0.25)*fL+FFX_BROADCAST_FLOAT16X2(0.25)*hL-eL;
  908. nz=ffxSaturate(abs(nz)*ffxApproximateReciprocalMediumHalf(ffxMax3Half(ffxMax3Half(bL,dL,eL),fL,hL)-ffxMin3Half(ffxMin3Half(bL,dL,eL),fL,hL)));
  909. nz=FFX_BROADCAST_FLOAT16X2(-0.5)*nz+FFX_BROADCAST_FLOAT16X2(1.0);
  910. // Min and max of ring.
  911. FfxFloat16x2 mn4R=min(ffxMin3Half(bR,dR,fR),hR);
  912. FfxFloat16x2 mn4G=min(ffxMin3Half(bG,dG,fG),hG);
  913. FfxFloat16x2 mn4B=min(ffxMin3Half(bB,dB,fB),hB);
  914. FfxFloat16x2 mx4R=max(ffxMax3Half(bR,dR,fR),hR);
  915. FfxFloat16x2 mx4G=max(ffxMax3Half(bG,dG,fG),hG);
  916. FfxFloat16x2 mx4B=max(ffxMax3Half(bB,dB,fB),hB);
  917. // Immediate constants for peak range.
  918. FfxFloat16x2 peakC=FfxFloat16x2(1.0,-1.0*4.0);
  919. // Limiters, these need to be high precision RCPs.
  920. FfxFloat16x2 hitMinR=mn4R*ffxReciprocalHalf(FFX_BROADCAST_FLOAT16X2(4.0)*mx4R);
  921. FfxFloat16x2 hitMinG=mn4G*ffxReciprocalHalf(FFX_BROADCAST_FLOAT16X2(4.0)*mx4G);
  922. FfxFloat16x2 hitMinB=mn4B*ffxReciprocalHalf(FFX_BROADCAST_FLOAT16X2(4.0)*mx4B);
  923. FfxFloat16x2 hitMaxR=(peakC.x-mx4R)*ffxReciprocalHalf(FFX_BROADCAST_FLOAT16X2(4.0)*mn4R+peakC.y);
  924. FfxFloat16x2 hitMaxG=(peakC.x-mx4G)*ffxReciprocalHalf(FFX_BROADCAST_FLOAT16X2(4.0)*mn4G+peakC.y);
  925. FfxFloat16x2 hitMaxB=(peakC.x-mx4B)*ffxReciprocalHalf(FFX_BROADCAST_FLOAT16X2(4.0)*mn4B+peakC.y);
  926. FfxFloat16x2 lobeR=max(-hitMinR,hitMaxR);
  927. FfxFloat16x2 lobeG=max(-hitMinG,hitMaxG);
  928. FfxFloat16x2 lobeB=max(-hitMinB,hitMaxB);
  929. FfxFloat16x2 lobe=max(FFX_BROADCAST_FLOAT16X2(-FSR_RCAS_LIMIT),min(ffxMax3Half(lobeR,lobeG,lobeB),FFX_BROADCAST_FLOAT16X2(0.0)))*FFX_BROADCAST_FLOAT16X2(FFX_UINT32_TO_FLOAT16X2(con.y).x);
  930. // Apply noise removal.
  931. #ifdef FSR_RCAS_DENOISE
  932. lobe*=nz;
  933. #endif
  934. // Resolve, which needs the medium precision rcp approximation to avoid visible tonality changes.
  935. FfxFloat16x2 rcpL=ffxApproximateReciprocalMediumHalf(FFX_BROADCAST_FLOAT16X2(4.0)*lobe+FFX_BROADCAST_FLOAT16X2(1.0));
  936. pixR=(lobe*bR+lobe*dR+lobe*hR+lobe*fR+eR)*rcpL;
  937. pixG=(lobe*bG+lobe*dG+lobe*hG+lobe*fG+eG)*rcpL;
  938. pixB=(lobe*bB+lobe*dB+lobe*hB+lobe*fB+eB)*rcpL;}
  939. #endif
  940. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  941. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  942. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  943. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  944. //_____________________________________________________________/\_______________________________________________________________
  945. //==============================================================================================================================
  946. //
  947. // FSR - [LFGA] LINEAR FILM GRAIN APPLICATOR
  948. //
  949. //------------------------------------------------------------------------------------------------------------------------------
  950. // Adding output-resolution film grain after scaling is a good way to mask both rendering and scaling artifacts.
  951. // Suggest using tiled blue noise as film grain input, with peak noise frequency set for a specific look and feel.
  952. // The 'Lfga*()' functions provide a convenient way to introduce grain.
  953. // These functions limit grain based on distance to signal limits.
  954. // This is done so that the grain is temporally energy preserving, and thus won't modify image tonality.
  955. // Grain application should be done in a linear colorspace.
  956. // The grain should be temporally changing, but have a temporal sum per pixel that adds to zero (non-biased).
  957. //------------------------------------------------------------------------------------------------------------------------------
  958. // Usage,
  959. // FsrLfga*(
  960. // color, // In/out linear colorspace color {0 to 1} ranged.
  961. // grain, // Per pixel grain texture value {-0.5 to 0.5} ranged, input is 3-channel to support colored grain.
  962. // amount); // Amount of grain (0 to 1} ranged.
  963. //------------------------------------------------------------------------------------------------------------------------------
  964. // Example if grain texture is monochrome: 'FsrLfgaF(color,ffxBroadcast3(grain),amount)'
  965. //==============================================================================================================================
  966. #if defined(FFX_GPU)
  967. // Maximum grain is the minimum distance to the signal limit.
  968. void FsrLfgaF(inout FfxFloat32x3 c, FfxFloat32x3 t, FfxFloat32 a)
  969. {
  970. c += (t * ffxBroadcast3(a)) * ffxMin(ffxBroadcast3(1.0) - c, c);
  971. }
  972. #endif
  973. //==============================================================================================================================
  974. #if defined(FFX_GPU)&& FFX_HALF == 1
  975. // Half precision version (slower).
  976. void FsrLfgaH(inout FfxFloat16x3 c, FfxFloat16x3 t, FfxFloat16 a)
  977. {
  978. c += (t * FFX_BROADCAST_FLOAT16X3(a)) * min(FFX_BROADCAST_FLOAT16X3(1.0) - c, c);
  979. }
  980. //------------------------------------------------------------------------------------------------------------------------------
  981. // Packed half precision version (faster).
  982. void FsrLfgaHx2(inout FfxFloat16x2 cR,inout FfxFloat16x2 cG,inout FfxFloat16x2 cB,FfxFloat16x2 tR,FfxFloat16x2 tG,FfxFloat16x2 tB,FfxFloat16 a){
  983. cR+=(tR*FFX_BROADCAST_FLOAT16X2(a))*min(FFX_BROADCAST_FLOAT16X2(1.0)-cR,cR);cG+=(tG*FFX_BROADCAST_FLOAT16X2(a))*min(FFX_BROADCAST_FLOAT16X2(1.0)-cG,cG);cB+=(tB*FFX_BROADCAST_FLOAT16X2(a))*min(FFX_BROADCAST_FLOAT16X2(1.0)-cB,cB);}
  984. #endif
  985. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  986. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  987. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  988. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  989. //_____________________________________________________________/\_______________________________________________________________
  990. //==============================================================================================================================
  991. //
  992. // FSR - [SRTM] SIMPLE REVERSIBLE TONE-MAPPER
  993. //
  994. //------------------------------------------------------------------------------------------------------------------------------
  995. // This provides a way to take linear HDR color {0 to FP16_MAX} and convert it into a temporary {0 to 1} ranged post-tonemapped linear.
  996. // The tonemapper preserves RGB ratio, which helps maintain HDR color bleed during filtering.
  997. //------------------------------------------------------------------------------------------------------------------------------
  998. // Reversible tonemapper usage,
  999. // FsrSrtm*(color); // {0 to FP16_MAX} converted to {0 to 1}.
  1000. // FsrSrtmInv*(color); // {0 to 1} converted into {0 to 32768, output peak safe for FP16}.
  1001. //==============================================================================================================================
  1002. #if defined(FFX_GPU)
  1003. void FsrSrtmF(inout FfxFloat32x3 c)
  1004. {
  1005. c *= ffxBroadcast3(rcp(ffxMax3(c.r, c.g, c.b) + FfxFloat32(1.0)));
  1006. }
  1007. // The extra max solves the c=1.0 case (which is a /0).
  1008. void FsrSrtmInvF(inout FfxFloat32x3 c){c*=ffxBroadcast3(rcp(max(FfxFloat32(1.0/32768.0),FfxFloat32(1.0)-ffxMax3(c.r,c.g,c.b))));}
  1009. #endif
  1010. //==============================================================================================================================
  1011. #if defined(FFX_GPU )&& FFX_HALF == 1
  1012. void FsrSrtmH(inout FfxFloat16x3 c)
  1013. {
  1014. c *= FFX_BROADCAST_FLOAT16X3(ffxReciprocalHalf(ffxMax3Half(c.r, c.g, c.b) + FFX_BROADCAST_FLOAT16(1.0)));
  1015. }
  1016. void FsrSrtmInvH(inout FfxFloat16x3 c)
  1017. {
  1018. c *= FFX_BROADCAST_FLOAT16X3(ffxReciprocalHalf(max(FFX_BROADCAST_FLOAT16(1.0 / 32768.0), FFX_BROADCAST_FLOAT16(1.0) - ffxMax3Half(c.r, c.g, c.b))));
  1019. }
  1020. //------------------------------------------------------------------------------------------------------------------------------
  1021. void FsrSrtmHx2(inout FfxFloat16x2 cR, inout FfxFloat16x2 cG, inout FfxFloat16x2 cB)
  1022. {
  1023. FfxFloat16x2 rcp = ffxReciprocalHalf(ffxMax3Half(cR, cG, cB) + FFX_BROADCAST_FLOAT16X2(1.0));
  1024. cR *= rcp;
  1025. cG *= rcp;
  1026. cB *= rcp;
  1027. }
  1028. void FsrSrtmInvHx2(inout FfxFloat16x2 cR,inout FfxFloat16x2 cG,inout FfxFloat16x2 cB)
  1029. {
  1030. FfxFloat16x2 rcp=ffxReciprocalHalf(max(FFX_BROADCAST_FLOAT16X2(1.0/32768.0),FFX_BROADCAST_FLOAT16X2(1.0)-ffxMax3Half(cR,cG,cB)));
  1031. cR*=rcp;
  1032. cG*=rcp;
  1033. cB*=rcp;
  1034. }
  1035. #endif
  1036. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1037. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1038. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1039. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1040. //_____________________________________________________________/\_______________________________________________________________
  1041. //==============================================================================================================================
  1042. //
  1043. // FSR - [TEPD] TEMPORAL ENERGY PRESERVING DITHER
  1044. //
  1045. //------------------------------------------------------------------------------------------------------------------------------
  1046. // Temporally energy preserving dithered {0 to 1} linear to gamma 2.0 conversion.
  1047. // Gamma 2.0 is used so that the conversion back to linear is just to square the color.
  1048. // The conversion comes in 8-bit and 10-bit modes, designed for output to 8-bit UNORM or 10:10:10:2 respectively.
  1049. // Given good non-biased temporal blue noise as dither input,
  1050. // the output dither will temporally conserve energy.
  1051. // This is done by choosing the linear nearest step point instead of perceptual nearest.
  1052. // See code below for details.
  1053. //------------------------------------------------------------------------------------------------------------------------------
  1054. // DX SPEC RULES FOR FLOAT->UNORM 8-BIT CONVERSION
  1055. // ===============================================
  1056. // - Output is 'FfxUInt32(floor(saturate(n)*255.0+0.5))'.
  1057. // - Thus rounding is to nearest.
  1058. // - NaN gets converted to zero.
  1059. // - INF is clamped to {0.0 to 1.0}.
  1060. //==============================================================================================================================
  1061. #if defined(FFX_GPU)
  1062. // Hand tuned integer position to dither value, with more values than simple checkerboard.
  1063. // Only 32-bit has enough precision for this compddation.
  1064. // Output is {0 to <1}.
  1065. FfxFloat32 FsrTepdDitF(FfxUInt32x2 p, FfxUInt32 f)
  1066. {
  1067. FfxFloat32 x = FfxFloat32(p.x + f);
  1068. FfxFloat32 y = FfxFloat32(p.y);
  1069. // The 1.61803 golden ratio.
  1070. FfxFloat32 a = FfxFloat32((1.0 + ffxSqrt(5.0f)) / 2.0);
  1071. // Number designed to provide a good visual pattern.
  1072. FfxFloat32 b = FfxFloat32(1.0 / 3.69);
  1073. x = x * a + (y * b);
  1074. return ffxFract(x);
  1075. }
  1076. //------------------------------------------------------------------------------------------------------------------------------
  1077. // This version is 8-bit gamma 2.0.
  1078. // The 'c' input is {0 to 1}.
  1079. // Output is {0 to 1} ready for image store.
  1080. void FsrTepdC8F(inout FfxFloat32x3 c, FfxFloat32 dit)
  1081. {
  1082. FfxFloat32x3 n = ffxSqrt(c);
  1083. n = floor(n * ffxBroadcast3(255.0)) * ffxBroadcast3(1.0 / 255.0);
  1084. FfxFloat32x3 a = n * n;
  1085. FfxFloat32x3 b = n + ffxBroadcast3(1.0 / 255.0);
  1086. b = b * b;
  1087. // Ratio of 'a' to 'b' required to produce 'c'.
  1088. // ffxApproximateReciprocal() won't work here (at least for very high dynamic ranges).
  1089. // ffxApproximateReciprocalMedium() is an IADD,FMA,MUL.
  1090. FfxFloat32x3 r = (c - b) * ffxApproximateReciprocalMedium(a - b);
  1091. // Use the ratio as a cutoff to choose 'a' or 'b'.
  1092. // ffxIsGreaterThanZero() is a MUL.
  1093. c = ffxSaturate(n + ffxIsGreaterThanZero(ffxBroadcast3(dit) - r) * ffxBroadcast3(1.0 / 255.0));
  1094. }
  1095. //------------------------------------------------------------------------------------------------------------------------------
  1096. // This version is 10-bit gamma 2.0.
  1097. // The 'c' input is {0 to 1}.
  1098. // Output is {0 to 1} ready for image store.
  1099. void FsrTepdC10F(inout FfxFloat32x3 c, FfxFloat32 dit)
  1100. {
  1101. FfxFloat32x3 n = ffxSqrt(c);
  1102. n = floor(n * ffxBroadcast3(1023.0)) * ffxBroadcast3(1.0 / 1023.0);
  1103. FfxFloat32x3 a = n * n;
  1104. FfxFloat32x3 b = n + ffxBroadcast3(1.0 / 1023.0);
  1105. b = b * b;
  1106. FfxFloat32x3 r = (c - b) * ffxApproximateReciprocalMedium(a - b);
  1107. c = ffxSaturate(n + ffxIsGreaterThanZero(ffxBroadcast3(dit) - r) * ffxBroadcast3(1.0 / 1023.0));
  1108. }
  1109. #endif
  1110. //==============================================================================================================================
  1111. #if defined(FFX_GPU)&& FFX_HALF == 1
  1112. FfxFloat16 FsrTepdDitH(FfxUInt32x2 p, FfxUInt32 f)
  1113. {
  1114. FfxFloat32 x = FfxFloat32(p.x + f);
  1115. FfxFloat32 y = FfxFloat32(p.y);
  1116. FfxFloat32 a = FfxFloat32((1.0 + ffxSqrt(5.0f)) / 2.0);
  1117. FfxFloat32 b = FfxFloat32(1.0 / 3.69);
  1118. x = x * a + (y * b);
  1119. return FfxFloat16(ffxFract(x));
  1120. }
  1121. //------------------------------------------------------------------------------------------------------------------------------
  1122. void FsrTepdC8H(inout FfxFloat16x3 c, FfxFloat16 dit)
  1123. {
  1124. FfxFloat16x3 n = sqrt(c);
  1125. n = floor(n * FFX_BROADCAST_FLOAT16X3(255.0)) * FFX_BROADCAST_FLOAT16X3(1.0 / 255.0);
  1126. FfxFloat16x3 a = n * n;
  1127. FfxFloat16x3 b = n + FFX_BROADCAST_FLOAT16X3(1.0 / 255.0);
  1128. b = b * b;
  1129. FfxFloat16x3 r = (c - b) * ffxApproximateReciprocalMediumHalf(a - b);
  1130. c = ffxSaturate(n + ffxIsGreaterThanZeroHalf(FFX_BROADCAST_FLOAT16X3(dit) - r) * FFX_BROADCAST_FLOAT16X3(1.0 / 255.0));
  1131. }
  1132. //------------------------------------------------------------------------------------------------------------------------------
  1133. void FsrTepdC10H(inout FfxFloat16x3 c, FfxFloat16 dit)
  1134. {
  1135. FfxFloat16x3 n = sqrt(c);
  1136. n = floor(n * FFX_BROADCAST_FLOAT16X3(1023.0)) * FFX_BROADCAST_FLOAT16X3(1.0 / 1023.0);
  1137. FfxFloat16x3 a = n * n;
  1138. FfxFloat16x3 b = n + FFX_BROADCAST_FLOAT16X3(1.0 / 1023.0);
  1139. b = b * b;
  1140. FfxFloat16x3 r = (c - b) * ffxApproximateReciprocalMediumHalf(a - b);
  1141. c = ffxSaturate(n + ffxIsGreaterThanZeroHalf(FFX_BROADCAST_FLOAT16X3(dit) - r) * FFX_BROADCAST_FLOAT16X3(1.0 / 1023.0));
  1142. }
  1143. //==============================================================================================================================
  1144. // This computes dither for positions 'p' and 'p+{8,0}'.
  1145. FfxFloat16x2 FsrTepdDitHx2(FfxUInt32x2 p, FfxUInt32 f)
  1146. {
  1147. FfxFloat32x2 x;
  1148. x.x = FfxFloat32(p.x + f);
  1149. x.y = x.x + FfxFloat32(8.0);
  1150. FfxFloat32 y = FfxFloat32(p.y);
  1151. FfxFloat32 a = FfxFloat32((1.0 + ffxSqrt(5.0f)) / 2.0);
  1152. FfxFloat32 b = FfxFloat32(1.0 / 3.69);
  1153. x = x * ffxBroadcast2(a) + ffxBroadcast2(y * b);
  1154. return FfxFloat16x2(ffxFract(x));
  1155. }
  1156. //------------------------------------------------------------------------------------------------------------------------------
  1157. void FsrTepdC8Hx2(inout FfxFloat16x2 cR, inout FfxFloat16x2 cG, inout FfxFloat16x2 cB, FfxFloat16x2 dit)
  1158. {
  1159. FfxFloat16x2 nR = sqrt(cR);
  1160. FfxFloat16x2 nG = sqrt(cG);
  1161. FfxFloat16x2 nB = sqrt(cB);
  1162. nR = floor(nR * FFX_BROADCAST_FLOAT16X2(255.0)) * FFX_BROADCAST_FLOAT16X2(1.0 / 255.0);
  1163. nG = floor(nG * FFX_BROADCAST_FLOAT16X2(255.0)) * FFX_BROADCAST_FLOAT16X2(1.0 / 255.0);
  1164. nB = floor(nB * FFX_BROADCAST_FLOAT16X2(255.0)) * FFX_BROADCAST_FLOAT16X2(1.0 / 255.0);
  1165. FfxFloat16x2 aR = nR * nR;
  1166. FfxFloat16x2 aG = nG * nG;
  1167. FfxFloat16x2 aB = nB * nB;
  1168. FfxFloat16x2 bR = nR + FFX_BROADCAST_FLOAT16X2(1.0 / 255.0);
  1169. bR = bR * bR;
  1170. FfxFloat16x2 bG = nG + FFX_BROADCAST_FLOAT16X2(1.0 / 255.0);
  1171. bG = bG * bG;
  1172. FfxFloat16x2 bB = nB + FFX_BROADCAST_FLOAT16X2(1.0 / 255.0);
  1173. bB = bB * bB;
  1174. FfxFloat16x2 rR = (cR - bR) * ffxApproximateReciprocalMediumHalf(aR - bR);
  1175. FfxFloat16x2 rG = (cG - bG) * ffxApproximateReciprocalMediumHalf(aG - bG);
  1176. FfxFloat16x2 rB = (cB - bB) * ffxApproximateReciprocalMediumHalf(aB - bB);
  1177. cR = ffxSaturate(nR + ffxIsGreaterThanZeroHalf(dit - rR) * FFX_BROADCAST_FLOAT16X2(1.0 / 255.0));
  1178. cG = ffxSaturate(nG + ffxIsGreaterThanZeroHalf(dit - rG) * FFX_BROADCAST_FLOAT16X2(1.0 / 255.0));
  1179. cB = ffxSaturate(nB + ffxIsGreaterThanZeroHalf(dit - rB) * FFX_BROADCAST_FLOAT16X2(1.0 / 255.0));
  1180. }
  1181. //------------------------------------------------------------------------------------------------------------------------------
  1182. void FsrTepdC10Hx2(inout FfxFloat16x2 cR,inout FfxFloat16x2 cG,inout FfxFloat16x2 cB,FfxFloat16x2 dit){
  1183. FfxFloat16x2 nR=sqrt(cR);
  1184. FfxFloat16x2 nG=sqrt(cG);
  1185. FfxFloat16x2 nB=sqrt(cB);
  1186. nR=floor(nR*FFX_BROADCAST_FLOAT16X2(1023.0))*FFX_BROADCAST_FLOAT16X2(1.0/1023.0);
  1187. nG=floor(nG*FFX_BROADCAST_FLOAT16X2(1023.0))*FFX_BROADCAST_FLOAT16X2(1.0/1023.0);
  1188. nB=floor(nB*FFX_BROADCAST_FLOAT16X2(1023.0))*FFX_BROADCAST_FLOAT16X2(1.0/1023.0);
  1189. FfxFloat16x2 aR=nR*nR;
  1190. FfxFloat16x2 aG=nG*nG;
  1191. FfxFloat16x2 aB=nB*nB;
  1192. FfxFloat16x2 bR=nR+FFX_BROADCAST_FLOAT16X2(1.0/1023.0);bR=bR*bR;
  1193. FfxFloat16x2 bG=nG+FFX_BROADCAST_FLOAT16X2(1.0/1023.0);bG=bG*bG;
  1194. FfxFloat16x2 bB=nB+FFX_BROADCAST_FLOAT16X2(1.0/1023.0);bB=bB*bB;
  1195. FfxFloat16x2 rR=(cR-bR)*ffxApproximateReciprocalMediumHalf(aR-bR);
  1196. FfxFloat16x2 rG=(cG-bG)*ffxApproximateReciprocalMediumHalf(aG-bG);
  1197. FfxFloat16x2 rB=(cB-bB)*ffxApproximateReciprocalMediumHalf(aB-bB);
  1198. cR=ffxSaturate(nR+ffxIsGreaterThanZeroHalf(dit-rR)*FFX_BROADCAST_FLOAT16X2(1.0/1023.0));
  1199. cG=ffxSaturate(nG+ffxIsGreaterThanZeroHalf(dit-rG)*FFX_BROADCAST_FLOAT16X2(1.0/1023.0));
  1200. cB = ffxSaturate(nB + ffxIsGreaterThanZeroHalf(dit - rB) * FFX_BROADCAST_FLOAT16X2(1.0 / 1023.0));
  1201. }
  1202. #endif