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