//_____________________________________________________________/\_______________________________________________________________1//==============================================================================================================================2//3//4// AMD FidelityFX SUPER RESOLUTION [FSR 1] ::: SPATIAL SCALING & EXTRAS - v1.202106295//6//7//------------------------------------------------------------------------------------------------------------------------------8////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////9////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////10//------------------------------------------------------------------------------------------------------------------------------11// FidelityFX Super Resolution Sample12//13// Copyright (c) 2021 Advanced Micro Devices, Inc. All rights reserved.14// Permission is hereby granted, free of charge, to any person obtaining a copy15// of this software and associated documentation files(the "Software"), to deal16// in the Software without restriction, including without limitation the rights17// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell18// copies of the Software, and to permit persons to whom the Software is19// furnished to do so, subject to the following conditions :20// The above copyright notice and this permission notice shall be included in21// all copies or substantial portions of the Software.22// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR23// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,24// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE25// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER26// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,27// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN28// THE SOFTWARE.29//------------------------------------------------------------------------------------------------------------------------------30////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////31////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////32//------------------------------------------------------------------------------------------------------------------------------33// ABOUT34// =====35// FSR is a collection of algorithms relating to generating a higher resolution image.36// This specific header focuses on single-image non-temporal image scaling, and related tools.37//38// The core functions are EASU and RCAS:39// [EASU] Edge Adaptive Spatial Upsampling ....... 1x to 4x area range spatial scaling, clamped adaptive elliptical filter.40// [RCAS] Robust Contrast Adaptive Sharpening .... A non-scaling variation on CAS.41// RCAS needs to be applied after EASU as a separate pass.42//43// Optional utility functions are:44// [LFGA] Linear Film Grain Applicator ........... Tool to apply film grain after scaling.45// [SRTM] Simple Reversible Tone-Mapper .......... Linear HDR {0 to FP16_MAX} to {0 to 1} and back.46// [TEPD] Temporal Energy Preserving Dither ...... Temporally energy preserving dithered {0 to 1} linear to gamma 2.0 conversion.47// See each individual sub-section for inline documentation.48//------------------------------------------------------------------------------------------------------------------------------49////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////50////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////51//------------------------------------------------------------------------------------------------------------------------------52// FUNCTION PERMUTATIONS53// =====================54// *F() ..... Single item computation with 32-bit.55// *H() ..... Single item computation with 16-bit, with packing (aka two 16-bit ops in parallel) when possible.56// *Hx2() ... Processing two items in parallel with 16-bit, easier packing.57// Not all interfaces in this file have a *Hx2() form.58//==============================================================================================================================59////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////60////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////61////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////62////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////63//_____________________________________________________________/\_______________________________________________________________64//==============================================================================================================================65//66// FSR - [EASU] EDGE ADAPTIVE SPATIAL UPSAMPLING67//68//------------------------------------------------------------------------------------------------------------------------------69// EASU provides a high quality spatial-only scaling at relatively low cost.70// Meaning EASU is appropiate for laptops and other low-end GPUs.71// Quality from 1x to 4x area scaling is good.72//------------------------------------------------------------------------------------------------------------------------------73// The scalar uses a modified fast approximation to the standard lanczos(size=2) kernel.74// EASU runs in a single pass, so it applies a directionally and anisotropically adaptive radial lanczos.75// This is also kept as simple as possible to have minimum runtime.76//------------------------------------------------------------------------------------------------------------------------------77// The lanzcos filter has negative lobes, so by itself it will introduce ringing.78// To remove all ringing, the algorithm uses the nearest 2x2 input texels as a neighborhood,79// and limits output to the minimum and maximum of that neighborhood.80//------------------------------------------------------------------------------------------------------------------------------81// Input image requirements:82//83// Color needs to be encoded as 3 channel[red, green, blue](e.g.XYZ not supported)84// Each channel needs to be in the range[0, 1]85// Any color primaries are supported86// Display / tonemapping curve needs to be as if presenting to sRGB display or similar(e.g.Gamma 2.0)87// There should be no banding in the input88// There should be no high amplitude noise in the input89// There should be no noise in the input that is not at input pixel granularity90// For performance purposes, use 32bpp formats91//------------------------------------------------------------------------------------------------------------------------------92// Best to apply EASU at the end of the frame after tonemapping93// but before film grain or composite of the UI.94//------------------------------------------------------------------------------------------------------------------------------95// Example of including this header for D3D HLSL :96//97// #define A_GPU 198// #define A_HLSL 199// #define A_HALF 1100// #include "ffx_a.h"101// #define FSR_EASU_H 1102// #define FSR_RCAS_H 1103// //declare input callbacks104// #include "ffx_fsr1.h"105//106// Example of including this header for Vulkan GLSL :107//108// #define A_GPU 1109// #define A_GLSL 1110// #define A_HALF 1111// #include "ffx_a.h"112// #define FSR_EASU_H 1113// #define FSR_RCAS_H 1114// //declare input callbacks115// #include "ffx_fsr1.h"116//117// Example of including this header for Vulkan HLSL :118//119// #define A_GPU 1120// #define A_HLSL 1121// #define A_HLSL_6_2 1122// #define A_NO_16_BIT_CAST 1123// #define A_HALF 1124// #include "ffx_a.h"125// #define FSR_EASU_H 1126// #define FSR_RCAS_H 1127// //declare input callbacks128// #include "ffx_fsr1.h"129//130// Example of declaring the required input callbacks for GLSL :131// The callbacks need to gather4 for each color channel using the specified texture coordinate 'p'.132// EASU uses gather4 to reduce position computation logic and for free Arrays of Structures to Structures of Arrays conversion.133//134// AH4 FsrEasuRH(AF2 p){return AH4(textureGather(sampler2D(tex,sam),p,0));}135// AH4 FsrEasuGH(AF2 p){return AH4(textureGather(sampler2D(tex,sam),p,1));}136// AH4 FsrEasuBH(AF2 p){return AH4(textureGather(sampler2D(tex,sam),p,2));}137// ...138// The FsrEasuCon function needs to be called from the CPU or GPU to set up constants.139// The difference in viewport and input image size is there to support Dynamic Resolution Scaling.140// To use FsrEasuCon() on the CPU, define A_CPU before including ffx_a and ffx_fsr1.141// Including a GPU example here, the 'con0' through 'con3' values would be stored out to a constant buffer.142// AU4 con0,con1,con2,con3;143// FsrEasuCon(con0,con1,con2,con3,144// 1920.0,1080.0, // Viewport size (top left aligned) in the input image which is to be scaled.145// 3840.0,2160.0, // The size of the input image.146// 2560.0,1440.0); // The output resolution.147//==============================================================================================================================148////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////149////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////150//_____________________________________________________________/\_______________________________________________________________151//==============================================================================================================================152// CONSTANT SETUP153//==============================================================================================================================154// Call to setup required constant values (works on CPU or GPU).155A_STATIC void FsrEasuCon(156outAU4 con0,157outAU4 con1,158outAU4 con2,159outAU4 con3,160// This the rendered image resolution being upscaled161AF1 inputViewportInPixelsX,162AF1 inputViewportInPixelsY,163// This is the resolution of the resource containing the input image (useful for dynamic resolution)164AF1 inputSizeInPixelsX,165AF1 inputSizeInPixelsY,166// This is the display resolution which the input image gets upscaled to167AF1 outputSizeInPixelsX,168AF1 outputSizeInPixelsY){169// Output integer position to a pixel position in viewport.170con0[0]=AU1_AF1(inputViewportInPixelsX*ARcpF1(outputSizeInPixelsX));171con0[1]=AU1_AF1(inputViewportInPixelsY*ARcpF1(outputSizeInPixelsY));172con0[2]=AU1_AF1(AF1_(0.5)*inputViewportInPixelsX*ARcpF1(outputSizeInPixelsX)-AF1_(0.5));173con0[3]=AU1_AF1(AF1_(0.5)*inputViewportInPixelsY*ARcpF1(outputSizeInPixelsY)-AF1_(0.5));174// Viewport pixel position to normalized image space.175// This is used to get upper-left of 'F' tap.176con1[0]=AU1_AF1(ARcpF1(inputSizeInPixelsX));177con1[1]=AU1_AF1(ARcpF1(inputSizeInPixelsY));178// Centers of gather4, first offset from upper-left of 'F'.179// +---+---+180// | | |181// +--(0)--+182// | b | c |183// +---F---+---+---+184// | e | f | g | h |185// +--(1)--+--(2)--+186// | i | j | k | l |187// +---+---+---+---+188// | n | o |189// +--(3)--+190// | | |191// +---+---+192con1[2]=AU1_AF1(AF1_( 1.0)*ARcpF1(inputSizeInPixelsX));193con1[3]=AU1_AF1(AF1_(-1.0)*ARcpF1(inputSizeInPixelsY));194// These are from (0) instead of 'F'.195con2[0]=AU1_AF1(AF1_(-1.0)*ARcpF1(inputSizeInPixelsX));196con2[1]=AU1_AF1(AF1_( 2.0)*ARcpF1(inputSizeInPixelsY));197con2[2]=AU1_AF1(AF1_( 1.0)*ARcpF1(inputSizeInPixelsX));198con2[3]=AU1_AF1(AF1_( 2.0)*ARcpF1(inputSizeInPixelsY));199con3[0]=AU1_AF1(AF1_( 0.0)*ARcpF1(inputSizeInPixelsX));200con3[1]=AU1_AF1(AF1_( 4.0)*ARcpF1(inputSizeInPixelsY));201con3[2]=con3[3]=0;}202203//If the an offset into the input image resource204A_STATIC void FsrEasuConOffset(205outAU4 con0,206outAU4 con1,207outAU4 con2,208outAU4 con3,209// This the rendered image resolution being upscaled210AF1 inputViewportInPixelsX,211AF1 inputViewportInPixelsY,212// This is the resolution of the resource containing the input image (useful for dynamic resolution)213AF1 inputSizeInPixelsX,214AF1 inputSizeInPixelsY,215// This is the display resolution which the input image gets upscaled to216AF1 outputSizeInPixelsX,217AF1 outputSizeInPixelsY,218// This is the input image offset into the resource containing it (useful for dynamic resolution)219AF1 inputOffsetInPixelsX,220AF1 inputOffsetInPixelsY) {221FsrEasuCon(con0, con1, con2, con3, inputViewportInPixelsX, inputViewportInPixelsY, inputSizeInPixelsX, inputSizeInPixelsY, outputSizeInPixelsX, outputSizeInPixelsY);222con0[2] = AU1_AF1(AF1_(0.5) * inputViewportInPixelsX * ARcpF1(outputSizeInPixelsX) - AF1_(0.5) + inputOffsetInPixelsX);223con0[3] = AU1_AF1(AF1_(0.5) * inputViewportInPixelsY * ARcpF1(outputSizeInPixelsY) - AF1_(0.5) + inputOffsetInPixelsY);224}225////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////226////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////227//_____________________________________________________________/\_______________________________________________________________228//==============================================================================================================================229// NON-PACKED 32-BIT VERSION230//==============================================================================================================================231#if defined(A_GPU)&&defined(FSR_EASU_F)232// Input callback prototypes, need to be implemented by calling shader233AF4 FsrEasuRF(AF2 p);234AF4 FsrEasuGF(AF2 p);235AF4 FsrEasuBF(AF2 p);236//------------------------------------------------------------------------------------------------------------------------------237// Filtering for a given tap for the scalar.238void FsrEasuTapF(239inout AF3 aC, // Accumulated color, with negative lobe.240inout AF1 aW, // Accumulated weight.241AF2 off, // Pixel offset from resolve position to tap.242AF2 dir, // Gradient direction.243AF2 len, // Length.244AF1 lob, // Negative lobe strength.245AF1 clp, // Clipping point.246AF3 c){ // Tap color.247// Rotate offset by direction.248AF2 v;249v.x=(off.x*( dir.x))+(off.y*dir.y);250v.y=(off.x*(-dir.y))+(off.y*dir.x);251// Anisotropy.252v*=len;253// Compute distance^2.254AF1 d2=v.x*v.x+v.y*v.y;255// Limit to the window as at corner, 2 taps can easily be outside.256d2=min(d2,clp);257// Approximation of lancos2 without sin() or rcp(), or sqrt() to get x.258// (25/16 * (2/5 * x^2 - 1)^2 - (25/16 - 1)) * (1/4 * x^2 - 1)^2259// |_______________________________________| |_______________|260// base window261// The general form of the 'base' is,262// (a*(b*x^2-1)^2-(a-1))263// Where 'a=1/(2*b-b^2)' and 'b' moves around the negative lobe.264AF1 wB=AF1_(2.0/5.0)*d2+AF1_(-1.0);265AF1 wA=lob*d2+AF1_(-1.0);266wB*=wB;267wA*=wA;268wB=AF1_(25.0/16.0)*wB+AF1_(-(25.0/16.0-1.0));269AF1 w=wB*wA;270// Do weighted average.271aC+=c*w;aW+=w;}272//------------------------------------------------------------------------------------------------------------------------------273// Accumulate direction and length.274void FsrEasuSetF(275inout AF2 dir,276inout AF1 len,277AF2 pp,278AP1 biS,AP1 biT,AP1 biU,AP1 biV,279AF1 lA,AF1 lB,AF1 lC,AF1 lD,AF1 lE){280// Compute bilinear weight, branches factor out as predicates are compiler time immediates.281// s t282// u v283AF1 w = AF1_(0.0);284if(biS)w=(AF1_(1.0)-pp.x)*(AF1_(1.0)-pp.y);285if(biT)w= pp.x *(AF1_(1.0)-pp.y);286if(biU)w=(AF1_(1.0)-pp.x)* pp.y ;287if(biV)w= pp.x * pp.y ;288// Direction is the '+' diff.289// a290// b c d291// e292// Then takes magnitude from abs average of both sides of 'c'.293// Length converts gradient reversal to 0, smoothly to non-reversal at 1, shaped, then adding horz and vert terms.294AF1 dc=lD-lC;295AF1 cb=lC-lB;296AF1 lenX=max(abs(dc),abs(cb));297lenX=APrxLoRcpF1(lenX);298AF1 dirX=lD-lB;299dir.x+=dirX*w;300lenX=ASatF1(abs(dirX)*lenX);301lenX*=lenX;302len+=lenX*w;303// Repeat for the y axis.304AF1 ec=lE-lC;305AF1 ca=lC-lA;306AF1 lenY=max(abs(ec),abs(ca));307lenY=APrxLoRcpF1(lenY);308AF1 dirY=lE-lA;309dir.y+=dirY*w;310lenY=ASatF1(abs(dirY)*lenY);311lenY*=lenY;312len+=lenY*w;}313//------------------------------------------------------------------------------------------------------------------------------314void FsrEasuF(315out AF3 pix,316AU2 ip, // Integer pixel position in output.317AU4 con0, // Constants generated by FsrEasuCon().318AU4 con1,319AU4 con2,320AU4 con3){321//------------------------------------------------------------------------------------------------------------------------------322// Get position of 'f'.323AF2 pp=AF2(ip)*AF2_AU2(con0.xy)+AF2_AU2(con0.zw);324AF2 fp=floor(pp);325pp-=fp;326//------------------------------------------------------------------------------------------------------------------------------327// 12-tap kernel.328// b c329// e f g h330// i j k l331// n o332// Gather 4 ordering.333// a b334// r g335// For packed FP16, need either {rg} or {ab} so using the following setup for gather in all versions,336// a b <- unused (z)337// r g338// a b a b339// r g r g340// a b341// r g <- unused (z)342// Allowing dead-code removal to remove the 'z's.343AF2 p0=fp*AF2_AU2(con1.xy)+AF2_AU2(con1.zw);344// These are from p0 to avoid pulling two constants on pre-Navi hardware.345AF2 p1=p0+AF2_AU2(con2.xy);346AF2 p2=p0+AF2_AU2(con2.zw);347AF2 p3=p0+AF2_AU2(con3.xy);348AF4 bczzR=FsrEasuRF(p0);349AF4 bczzG=FsrEasuGF(p0);350AF4 bczzB=FsrEasuBF(p0);351AF4 ijfeR=FsrEasuRF(p1);352AF4 ijfeG=FsrEasuGF(p1);353AF4 ijfeB=FsrEasuBF(p1);354AF4 klhgR=FsrEasuRF(p2);355AF4 klhgG=FsrEasuGF(p2);356AF4 klhgB=FsrEasuBF(p2);357AF4 zzonR=FsrEasuRF(p3);358AF4 zzonG=FsrEasuGF(p3);359AF4 zzonB=FsrEasuBF(p3);360//------------------------------------------------------------------------------------------------------------------------------361// Simplest multi-channel approximate luma possible (luma times 2, in 2 FMA/MAD).362AF4 bczzL=bczzB*AF4_(0.5)+(bczzR*AF4_(0.5)+bczzG);363AF4 ijfeL=ijfeB*AF4_(0.5)+(ijfeR*AF4_(0.5)+ijfeG);364AF4 klhgL=klhgB*AF4_(0.5)+(klhgR*AF4_(0.5)+klhgG);365AF4 zzonL=zzonB*AF4_(0.5)+(zzonR*AF4_(0.5)+zzonG);366// Rename.367AF1 bL=bczzL.x;368AF1 cL=bczzL.y;369AF1 iL=ijfeL.x;370AF1 jL=ijfeL.y;371AF1 fL=ijfeL.z;372AF1 eL=ijfeL.w;373AF1 kL=klhgL.x;374AF1 lL=klhgL.y;375AF1 hL=klhgL.z;376AF1 gL=klhgL.w;377AF1 oL=zzonL.z;378AF1 nL=zzonL.w;379// Accumulate for bilinear interpolation.380AF2 dir=AF2_(0.0);381AF1 len=AF1_(0.0);382FsrEasuSetF(dir,len,pp,true, false,false,false,bL,eL,fL,gL,jL);383FsrEasuSetF(dir,len,pp,false,true ,false,false,cL,fL,gL,hL,kL);384FsrEasuSetF(dir,len,pp,false,false,true ,false,fL,iL,jL,kL,nL);385FsrEasuSetF(dir,len,pp,false,false,false,true ,gL,jL,kL,lL,oL);386//------------------------------------------------------------------------------------------------------------------------------387// Normalize with approximation, and cleanup close to zero.388AF2 dir2=dir*dir;389AF1 dirR=dir2.x+dir2.y;390AP1 zro=dirR<AF1_(1.0/32768.0);391dirR=APrxLoRsqF1(dirR);392dirR=zro?AF1_(1.0):dirR;393dir.x=zro?AF1_(1.0):dir.x;394dir*=AF2_(dirR);395// Transform from {0 to 2} to {0 to 1} range, and shape with square.396len=len*AF1_(0.5);397len*=len;398// Stretch kernel {1.0 vert|horz, to sqrt(2.0) on diagonal}.399AF1 stretch=(dir.x*dir.x+dir.y*dir.y)*APrxLoRcpF1(max(abs(dir.x),abs(dir.y)));400// Anisotropic length after rotation,401// x := 1.0 lerp to 'stretch' on edges402// y := 1.0 lerp to 2x on edges403AF2 len2=AF2(AF1_(1.0)+(stretch-AF1_(1.0))*len,AF1_(1.0)+AF1_(-0.5)*len);404// Based on the amount of 'edge',405// the window shifts from +/-{sqrt(2.0) to slightly beyond 2.0}.406AF1 lob=AF1_(0.5)+AF1_((1.0/4.0-0.04)-0.5)*len;407// Set distance^2 clipping point to the end of the adjustable window.408AF1 clp=APrxLoRcpF1(lob);409//------------------------------------------------------------------------------------------------------------------------------410// Accumulation mixed with min/max of 4 nearest.411// b c412// e f g h413// i j k l414// n o415AF3 min4=min(AMin3F3(AF3(ijfeR.z,ijfeG.z,ijfeB.z),AF3(klhgR.w,klhgG.w,klhgB.w),AF3(ijfeR.y,ijfeG.y,ijfeB.y)),416AF3(klhgR.x,klhgG.x,klhgB.x));417AF3 max4=max(AMax3F3(AF3(ijfeR.z,ijfeG.z,ijfeB.z),AF3(klhgR.w,klhgG.w,klhgB.w),AF3(ijfeR.y,ijfeG.y,ijfeB.y)),418AF3(klhgR.x,klhgG.x,klhgB.x));419// Accumulation.420AF3 aC=AF3_(0.0);421AF1 aW=AF1_(0.0);422FsrEasuTapF(aC,aW,AF2( 0.0,-1.0)-pp,dir,len2,lob,clp,AF3(bczzR.x,bczzG.x,bczzB.x)); // b423FsrEasuTapF(aC,aW,AF2( 1.0,-1.0)-pp,dir,len2,lob,clp,AF3(bczzR.y,bczzG.y,bczzB.y)); // c424FsrEasuTapF(aC,aW,AF2(-1.0, 1.0)-pp,dir,len2,lob,clp,AF3(ijfeR.x,ijfeG.x,ijfeB.x)); // i425FsrEasuTapF(aC,aW,AF2( 0.0, 1.0)-pp,dir,len2,lob,clp,AF3(ijfeR.y,ijfeG.y,ijfeB.y)); // j426FsrEasuTapF(aC,aW,AF2( 0.0, 0.0)-pp,dir,len2,lob,clp,AF3(ijfeR.z,ijfeG.z,ijfeB.z)); // f427FsrEasuTapF(aC,aW,AF2(-1.0, 0.0)-pp,dir,len2,lob,clp,AF3(ijfeR.w,ijfeG.w,ijfeB.w)); // e428FsrEasuTapF(aC,aW,AF2( 1.0, 1.0)-pp,dir,len2,lob,clp,AF3(klhgR.x,klhgG.x,klhgB.x)); // k429FsrEasuTapF(aC,aW,AF2( 2.0, 1.0)-pp,dir,len2,lob,clp,AF3(klhgR.y,klhgG.y,klhgB.y)); // l430FsrEasuTapF(aC,aW,AF2( 2.0, 0.0)-pp,dir,len2,lob,clp,AF3(klhgR.z,klhgG.z,klhgB.z)); // h431FsrEasuTapF(aC,aW,AF2( 1.0, 0.0)-pp,dir,len2,lob,clp,AF3(klhgR.w,klhgG.w,klhgB.w)); // g432FsrEasuTapF(aC,aW,AF2( 1.0, 2.0)-pp,dir,len2,lob,clp,AF3(zzonR.z,zzonG.z,zzonB.z)); // o433FsrEasuTapF(aC,aW,AF2( 0.0, 2.0)-pp,dir,len2,lob,clp,AF3(zzonR.w,zzonG.w,zzonB.w)); // n434//------------------------------------------------------------------------------------------------------------------------------435// Normalize and dering.436pix=min(max4,max(min4,aC*AF3_(ARcpF1(aW))));}437#endif438////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////439////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////440//_____________________________________________________________/\_______________________________________________________________441//==============================================================================================================================442// PACKED 16-BIT VERSION443//==============================================================================================================================444#if defined(A_GPU)&&defined(A_HALF)&&defined(FSR_EASU_H)445// Input callback prototypes, need to be implemented by calling shader446AH4 FsrEasuRH(AF2 p);447AH4 FsrEasuGH(AF2 p);448AH4 FsrEasuBH(AF2 p);449//------------------------------------------------------------------------------------------------------------------------------450// This runs 2 taps in parallel.451void FsrEasuTapH(452inout AH2 aCR,inout AH2 aCG,inout AH2 aCB,453inout AH2 aW,454AH2 offX,AH2 offY,455AH2 dir,456AH2 len,457AH1 lob,458AH1 clp,459AH2 cR,AH2 cG,AH2 cB){460AH2 vX,vY;461vX=offX* dir.xx +offY*dir.yy;462vY=offX*(-dir.yy)+offY*dir.xx;463vX*=len.x;vY*=len.y;464AH2 d2=vX*vX+vY*vY;465d2=min(d2,AH2_(clp));466AH2 wB=AH2_(2.0/5.0)*d2+AH2_(-1.0);467AH2 wA=AH2_(lob)*d2+AH2_(-1.0);468wB*=wB;469wA*=wA;470wB=AH2_(25.0/16.0)*wB+AH2_(-(25.0/16.0-1.0));471AH2 w=wB*wA;472aCR+=cR*w;aCG+=cG*w;aCB+=cB*w;aW+=w;}473//------------------------------------------------------------------------------------------------------------------------------474// This runs 2 taps in parallel.475void FsrEasuSetH(476inout AH2 dirPX,inout AH2 dirPY,477inout AH2 lenP,478AH2 pp,479AP1 biST,AP1 biUV,480AH2 lA,AH2 lB,AH2 lC,AH2 lD,AH2 lE){481AH2 w = AH2_(0.0);482if(biST)w=(AH2(1.0,0.0)+AH2(-pp.x,pp.x))*AH2_(AH1_(1.0)-pp.y);483if(biUV)w=(AH2(1.0,0.0)+AH2(-pp.x,pp.x))*AH2_( pp.y);484// ABS is not free in the packed FP16 path.485AH2 dc=lD-lC;486AH2 cb=lC-lB;487AH2 lenX=max(abs(dc),abs(cb));488lenX=ARcpH2(lenX);489AH2 dirX=lD-lB;490dirPX+=dirX*w;491lenX=ASatH2(abs(dirX)*lenX);492lenX*=lenX;493lenP+=lenX*w;494AH2 ec=lE-lC;495AH2 ca=lC-lA;496AH2 lenY=max(abs(ec),abs(ca));497lenY=ARcpH2(lenY);498AH2 dirY=lE-lA;499dirPY+=dirY*w;500lenY=ASatH2(abs(dirY)*lenY);501lenY*=lenY;502lenP+=lenY*w;}503//------------------------------------------------------------------------------------------------------------------------------504void FsrEasuH(505out AH3 pix,506AU2 ip,507AU4 con0,508AU4 con1,509AU4 con2,510AU4 con3){511//------------------------------------------------------------------------------------------------------------------------------512AF2 pp=AF2(ip)*AF2_AU2(con0.xy)+AF2_AU2(con0.zw);513AF2 fp=floor(pp);514pp-=fp;515AH2 ppp=AH2(pp);516//------------------------------------------------------------------------------------------------------------------------------517AF2 p0=fp*AF2_AU2(con1.xy)+AF2_AU2(con1.zw);518AF2 p1=p0+AF2_AU2(con2.xy);519AF2 p2=p0+AF2_AU2(con2.zw);520AF2 p3=p0+AF2_AU2(con3.xy);521AH4 bczzR=FsrEasuRH(p0);522AH4 bczzG=FsrEasuGH(p0);523AH4 bczzB=FsrEasuBH(p0);524AH4 ijfeR=FsrEasuRH(p1);525AH4 ijfeG=FsrEasuGH(p1);526AH4 ijfeB=FsrEasuBH(p1);527AH4 klhgR=FsrEasuRH(p2);528AH4 klhgG=FsrEasuGH(p2);529AH4 klhgB=FsrEasuBH(p2);530AH4 zzonR=FsrEasuRH(p3);531AH4 zzonG=FsrEasuGH(p3);532AH4 zzonB=FsrEasuBH(p3);533//------------------------------------------------------------------------------------------------------------------------------534AH4 bczzL=bczzB*AH4_(0.5)+(bczzR*AH4_(0.5)+bczzG);535AH4 ijfeL=ijfeB*AH4_(0.5)+(ijfeR*AH4_(0.5)+ijfeG);536AH4 klhgL=klhgB*AH4_(0.5)+(klhgR*AH4_(0.5)+klhgG);537AH4 zzonL=zzonB*AH4_(0.5)+(zzonR*AH4_(0.5)+zzonG);538AH1 bL=bczzL.x;539AH1 cL=bczzL.y;540AH1 iL=ijfeL.x;541AH1 jL=ijfeL.y;542AH1 fL=ijfeL.z;543AH1 eL=ijfeL.w;544AH1 kL=klhgL.x;545AH1 lL=klhgL.y;546AH1 hL=klhgL.z;547AH1 gL=klhgL.w;548AH1 oL=zzonL.z;549AH1 nL=zzonL.w;550// This part is different, accumulating 2 taps in parallel.551AH2 dirPX=AH2_(0.0);552AH2 dirPY=AH2_(0.0);553AH2 lenP=AH2_(0.0);554FsrEasuSetH(dirPX,dirPY,lenP,ppp,true, false,AH2(bL,cL),AH2(eL,fL),AH2(fL,gL),AH2(gL,hL),AH2(jL,kL));555FsrEasuSetH(dirPX,dirPY,lenP,ppp,false,true ,AH2(fL,gL),AH2(iL,jL),AH2(jL,kL),AH2(kL,lL),AH2(nL,oL));556AH2 dir=AH2(dirPX.r+dirPX.g,dirPY.r+dirPY.g);557AH1 len=lenP.r+lenP.g;558//------------------------------------------------------------------------------------------------------------------------------559AH2 dir2=dir*dir;560AH1 dirR=dir2.x+dir2.y;561AP1 zro=dirR<AH1_(1.0/32768.0);562dirR=APrxLoRsqH1(dirR);563dirR=zro?AH1_(1.0):dirR;564dir.x=zro?AH1_(1.0):dir.x;565dir*=AH2_(dirR);566len=len*AH1_(0.5);567len*=len;568AH1 stretch=(dir.x*dir.x+dir.y*dir.y)*APrxLoRcpH1(max(abs(dir.x),abs(dir.y)));569AH2 len2=AH2(AH1_(1.0)+(stretch-AH1_(1.0))*len,AH1_(1.0)+AH1_(-0.5)*len);570AH1 lob=AH1_(0.5)+AH1_((1.0/4.0-0.04)-0.5)*len;571AH1 clp=APrxLoRcpH1(lob);572//------------------------------------------------------------------------------------------------------------------------------573// FP16 is different, using packed trick to do min and max in same operation.574AH2 bothR=max(max(AH2(-ijfeR.z,ijfeR.z),AH2(-klhgR.w,klhgR.w)),max(AH2(-ijfeR.y,ijfeR.y),AH2(-klhgR.x,klhgR.x)));575AH2 bothG=max(max(AH2(-ijfeG.z,ijfeG.z),AH2(-klhgG.w,klhgG.w)),max(AH2(-ijfeG.y,ijfeG.y),AH2(-klhgG.x,klhgG.x)));576AH2 bothB=max(max(AH2(-ijfeB.z,ijfeB.z),AH2(-klhgB.w,klhgB.w)),max(AH2(-ijfeB.y,ijfeB.y),AH2(-klhgB.x,klhgB.x)));577// This part is different for FP16, working pairs of taps at a time.578AH2 pR=AH2_(0.0);579AH2 pG=AH2_(0.0);580AH2 pB=AH2_(0.0);581AH2 pW=AH2_(0.0);582FsrEasuTapH(pR,pG,pB,pW,AH2( 0.0, 1.0)-ppp.xx,AH2(-1.0,-1.0)-ppp.yy,dir,len2,lob,clp,bczzR.xy,bczzG.xy,bczzB.xy);583FsrEasuTapH(pR,pG,pB,pW,AH2(-1.0, 0.0)-ppp.xx,AH2( 1.0, 1.0)-ppp.yy,dir,len2,lob,clp,ijfeR.xy,ijfeG.xy,ijfeB.xy);584FsrEasuTapH(pR,pG,pB,pW,AH2( 0.0,-1.0)-ppp.xx,AH2( 0.0, 0.0)-ppp.yy,dir,len2,lob,clp,ijfeR.zw,ijfeG.zw,ijfeB.zw);585FsrEasuTapH(pR,pG,pB,pW,AH2( 1.0, 2.0)-ppp.xx,AH2( 1.0, 1.0)-ppp.yy,dir,len2,lob,clp,klhgR.xy,klhgG.xy,klhgB.xy);586FsrEasuTapH(pR,pG,pB,pW,AH2( 2.0, 1.0)-ppp.xx,AH2( 0.0, 0.0)-ppp.yy,dir,len2,lob,clp,klhgR.zw,klhgG.zw,klhgB.zw);587FsrEasuTapH(pR,pG,pB,pW,AH2( 1.0, 0.0)-ppp.xx,AH2( 2.0, 2.0)-ppp.yy,dir,len2,lob,clp,zzonR.zw,zzonG.zw,zzonB.zw);588AH3 aC=AH3(pR.x+pR.y,pG.x+pG.y,pB.x+pB.y);589AH1 aW=pW.x+pW.y;590//------------------------------------------------------------------------------------------------------------------------------591// Slightly different for FP16 version due to combined min and max.592pix=min(AH3(bothR.y,bothG.y,bothB.y),max(-AH3(bothR.x,bothG.x,bothB.x),aC*AH3_(ARcpH1(aW))));}593#endif594////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////595////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////596////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////597////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////598//_____________________________________________________________/\_______________________________________________________________599//==============================================================================================================================600//601// FSR - [RCAS] ROBUST CONTRAST ADAPTIVE SHARPENING602//603//------------------------------------------------------------------------------------------------------------------------------604// CAS uses a simplified mechanism to convert local contrast into a variable amount of sharpness.605// RCAS uses a more exact mechanism, solving for the maximum local sharpness possible before clipping.606// RCAS also has a built in process to limit sharpening of what it detects as possible noise.607// RCAS sharper does not support scaling, as it should be applied after EASU scaling.608// Pass EASU output straight into RCAS, no color conversions necessary.609//------------------------------------------------------------------------------------------------------------------------------610// RCAS is based on the following logic.611// RCAS uses a 5 tap filter in a cross pattern (same as CAS),612// w n613// w 1 w for taps w m e614// w s615// Where 'w' is the negative lobe weight.616// output = (w*(n+e+w+s)+m)/(4*w+1)617// RCAS solves for 'w' by seeing where the signal might clip out of the {0 to 1} input range,618// 0 == (w*(n+e+w+s)+m)/(4*w+1) -> w = -m/(n+e+w+s)619// 1 == (w*(n+e+w+s)+m)/(4*w+1) -> w = (1-m)/(n+e+w+s-4*1)620// Then chooses the 'w' which results in no clipping, limits 'w', and multiplies by the 'sharp' amount.621// This solution above has issues with MSAA input as the steps along the gradient cause edge detection issues.622// So RCAS uses 4x the maximum and 4x the minimum (depending on equation)in place of the individual taps.623// As well as switching from 'm' to either the minimum or maximum (depending on side), to help in energy conservation.624// This stabilizes RCAS.625// RCAS does a simple highpass which is normalized against the local contrast then shaped,626// 0.25627// 0.25 -1 0.25628// 0.25629// This is used as a noise detection filter, to reduce the effect of RCAS on grain, and focus on real edges.630//631// GLSL example for the required callbacks :632//633// AH4 FsrRcasLoadH(ASW2 p){return AH4(imageLoad(imgSrc,ASU2(p)));}634// void FsrRcasInputH(inout AH1 r,inout AH1 g,inout AH1 b)635// {636// //do any simple input color conversions here or leave empty if none needed637// }638//639// FsrRcasCon need to be called from the CPU or GPU to set up constants.640// Including a GPU example here, the 'con' value would be stored out to a constant buffer.641//642// AU4 con;643// FsrRcasCon(con,644// 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}.645// ---------------646// RCAS sharpening supports a CAS-like pass-through alpha via,647// #define FSR_RCAS_PASSTHROUGH_ALPHA 1648// RCAS also supports a define to enable a more expensive path to avoid some sharpening of noise.649// Would suggest it is better to apply film grain after RCAS sharpening (and after scaling) instead of using this define,650// #define FSR_RCAS_DENOISE 1651//==============================================================================================================================652// This is set at the limit of providing unnatural results for sharpening.653#define FSR_RCAS_LIMIT (0.25-(1.0/16.0))654////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////655////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////656//_____________________________________________________________/\_______________________________________________________________657//==============================================================================================================================658// CONSTANT SETUP659//==============================================================================================================================660// Call to setup required constant values (works on CPU or GPU).661A_STATIC void FsrRcasCon(662outAU4 con,663// The scale is {0.0 := maximum, to N>0, where N is the number of stops (halving) of the reduction of sharpness}.664AF1 sharpness){665// Transform from stops to linear value.666sharpness=AExp2F1(-sharpness);667varAF2(hSharp)=initAF2(sharpness,sharpness);668con[0]=AU1_AF1(sharpness);669con[1]=AU1_AH2_AF2(hSharp);670con[2]=0;671con[3]=0;}672////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////673////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////674//_____________________________________________________________/\_______________________________________________________________675//==============================================================================================================================676// NON-PACKED 32-BIT VERSION677//==============================================================================================================================678#if defined(A_GPU)&&defined(FSR_RCAS_F)679// Input callback prototypes that need to be implemented by calling shader680AF4 FsrRcasLoadF(ASU2 p);681void FsrRcasInputF(inout AF1 r,inout AF1 g,inout AF1 b);682//------------------------------------------------------------------------------------------------------------------------------683void FsrRcasF(684out AF1 pixR, // Output values, non-vector so port between RcasFilter() and RcasFilterH() is easy.685out AF1 pixG,686out AF1 pixB,687#ifdef FSR_RCAS_PASSTHROUGH_ALPHA688out AF1 pixA,689#endif690AU2 ip, // Integer pixel position in output.691AU4 con){ // Constant generated by RcasSetup().692// Algorithm uses minimal 3x3 pixel neighborhood.693// b694// d e f695// h696ASU2 sp=ASU2(ip);697AF3 b=FsrRcasLoadF(sp+ASU2( 0,-1)).rgb;698AF3 d=FsrRcasLoadF(sp+ASU2(-1, 0)).rgb;699#ifdef FSR_RCAS_PASSTHROUGH_ALPHA700AF4 ee=FsrRcasLoadF(sp);701AF3 e=ee.rgb;pixA=ee.a;702#else703AF3 e=FsrRcasLoadF(sp).rgb;704#endif705AF3 f=FsrRcasLoadF(sp+ASU2( 1, 0)).rgb;706AF3 h=FsrRcasLoadF(sp+ASU2( 0, 1)).rgb;707// Rename (32-bit) or regroup (16-bit).708AF1 bR=b.r;709AF1 bG=b.g;710AF1 bB=b.b;711AF1 dR=d.r;712AF1 dG=d.g;713AF1 dB=d.b;714AF1 eR=e.r;715AF1 eG=e.g;716AF1 eB=e.b;717AF1 fR=f.r;718AF1 fG=f.g;719AF1 fB=f.b;720AF1 hR=h.r;721AF1 hG=h.g;722AF1 hB=h.b;723// Run optional input transform.724FsrRcasInputF(bR,bG,bB);725FsrRcasInputF(dR,dG,dB);726FsrRcasInputF(eR,eG,eB);727FsrRcasInputF(fR,fG,fB);728FsrRcasInputF(hR,hG,hB);729// Luma times 2.730AF1 bL=bB*AF1_(0.5)+(bR*AF1_(0.5)+bG);731AF1 dL=dB*AF1_(0.5)+(dR*AF1_(0.5)+dG);732AF1 eL=eB*AF1_(0.5)+(eR*AF1_(0.5)+eG);733AF1 fL=fB*AF1_(0.5)+(fR*AF1_(0.5)+fG);734AF1 hL=hB*AF1_(0.5)+(hR*AF1_(0.5)+hG);735// Noise detection.736AF1 nz=AF1_(0.25)*bL+AF1_(0.25)*dL+AF1_(0.25)*fL+AF1_(0.25)*hL-eL;737nz=ASatF1(abs(nz)*APrxMedRcpF1(AMax3F1(AMax3F1(bL,dL,eL),fL,hL)-AMin3F1(AMin3F1(bL,dL,eL),fL,hL)));738nz=AF1_(-0.5)*nz+AF1_(1.0);739// Min and max of ring.740AF1 mn4R=min(AMin3F1(bR,dR,fR),hR);741AF1 mn4G=min(AMin3F1(bG,dG,fG),hG);742AF1 mn4B=min(AMin3F1(bB,dB,fB),hB);743AF1 mx4R=max(AMax3F1(bR,dR,fR),hR);744AF1 mx4G=max(AMax3F1(bG,dG,fG),hG);745AF1 mx4B=max(AMax3F1(bB,dB,fB),hB);746// Immediate constants for peak range.747AF2 peakC=AF2(1.0,-1.0*4.0);748// Limiters, these need to be high precision RCPs.749AF1 hitMinR=min(mn4R,eR)*ARcpF1(AF1_(4.0)*mx4R);750AF1 hitMinG=min(mn4G,eG)*ARcpF1(AF1_(4.0)*mx4G);751AF1 hitMinB=min(mn4B,eB)*ARcpF1(AF1_(4.0)*mx4B);752AF1 hitMaxR=(peakC.x-max(mx4R,eR))*ARcpF1(AF1_(4.0)*mn4R+peakC.y);753AF1 hitMaxG=(peakC.x-max(mx4G,eG))*ARcpF1(AF1_(4.0)*mn4G+peakC.y);754AF1 hitMaxB=(peakC.x-max(mx4B,eB))*ARcpF1(AF1_(4.0)*mn4B+peakC.y);755AF1 lobeR=max(-hitMinR,hitMaxR);756AF1 lobeG=max(-hitMinG,hitMaxG);757AF1 lobeB=max(-hitMinB,hitMaxB);758AF1 lobe=max(AF1_(-FSR_RCAS_LIMIT),min(AMax3F1(lobeR,lobeG,lobeB),AF1_(0.0)))*AF1_AU1(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.764AF1 rcpL=APrxMedRcpF1(AF1_(4.0)*lobe+AF1_(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#endif770////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////771////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////772//_____________________________________________________________/\_______________________________________________________________773//==============================================================================================================================774// NON-PACKED 16-BIT VERSION775//==============================================================================================================================776#if defined(A_GPU)&&defined(A_HALF)&&defined(FSR_RCAS_H)777// Input callback prototypes that need to be implemented by calling shader778AH4 FsrRcasLoadH(ASW2 p);779void FsrRcasInputH(inout AH1 r,inout AH1 g,inout AH1 b);780//------------------------------------------------------------------------------------------------------------------------------781void FsrRcasH(782out AH1 pixR, // Output values, non-vector so port between RcasFilter() and RcasFilterH() is easy.783out AH1 pixG,784out AH1 pixB,785#ifdef FSR_RCAS_PASSTHROUGH_ALPHA786out AH1 pixA,787#endif788AU2 ip, // Integer pixel position in output.789AU4 con){ // Constant generated by RcasSetup().790// Sharpening algorithm uses minimal 3x3 pixel neighborhood.791// b792// d e f793// h794ASW2 sp=ASW2(ip);795AH3 b=FsrRcasLoadH(sp+ASW2( 0,-1)).rgb;796AH3 d=FsrRcasLoadH(sp+ASW2(-1, 0)).rgb;797#ifdef FSR_RCAS_PASSTHROUGH_ALPHA798AH4 ee=FsrRcasLoadH(sp);799AH3 e=ee.rgb;pixA=ee.a;800#else801AH3 e=FsrRcasLoadH(sp).rgb;802#endif803AH3 f=FsrRcasLoadH(sp+ASW2( 1, 0)).rgb;804AH3 h=FsrRcasLoadH(sp+ASW2( 0, 1)).rgb;805// Rename (32-bit) or regroup (16-bit).806AH1 bR=b.r;807AH1 bG=b.g;808AH1 bB=b.b;809AH1 dR=d.r;810AH1 dG=d.g;811AH1 dB=d.b;812AH1 eR=e.r;813AH1 eG=e.g;814AH1 eB=e.b;815AH1 fR=f.r;816AH1 fG=f.g;817AH1 fB=f.b;818AH1 hR=h.r;819AH1 hG=h.g;820AH1 hB=h.b;821// Run optional input transform.822FsrRcasInputH(bR,bG,bB);823FsrRcasInputH(dR,dG,dB);824FsrRcasInputH(eR,eG,eB);825FsrRcasInputH(fR,fG,fB);826FsrRcasInputH(hR,hG,hB);827// Luma times 2.828AH1 bL=bB*AH1_(0.5)+(bR*AH1_(0.5)+bG);829AH1 dL=dB*AH1_(0.5)+(dR*AH1_(0.5)+dG);830AH1 eL=eB*AH1_(0.5)+(eR*AH1_(0.5)+eG);831AH1 fL=fB*AH1_(0.5)+(fR*AH1_(0.5)+fG);832AH1 hL=hB*AH1_(0.5)+(hR*AH1_(0.5)+hG);833// Noise detection.834AH1 nz=AH1_(0.25)*bL+AH1_(0.25)*dL+AH1_(0.25)*fL+AH1_(0.25)*hL-eL;835nz=ASatH1(abs(nz)*APrxMedRcpH1(AMax3H1(AMax3H1(bL,dL,eL),fL,hL)-AMin3H1(AMin3H1(bL,dL,eL),fL,hL)));836nz=AH1_(-0.5)*nz+AH1_(1.0);837// Min and max of ring.838AH1 mn4R=min(AMin3H1(bR,dR,fR),hR);839AH1 mn4G=min(AMin3H1(bG,dG,fG),hG);840AH1 mn4B=min(AMin3H1(bB,dB,fB),hB);841AH1 mx4R=max(AMax3H1(bR,dR,fR),hR);842AH1 mx4G=max(AMax3H1(bG,dG,fG),hG);843AH1 mx4B=max(AMax3H1(bB,dB,fB),hB);844// Immediate constants for peak range.845AH2 peakC=AH2(1.0,-1.0*4.0);846// Limiters, these need to be high precision RCPs.847AH1 hitMinR=min(mn4R,eR)*ARcpH1(AH1_(4.0)*mx4R);848AH1 hitMinG=min(mn4G,eG)*ARcpH1(AH1_(4.0)*mx4G);849AH1 hitMinB=min(mn4B,eB)*ARcpH1(AH1_(4.0)*mx4B);850AH1 hitMaxR=(peakC.x-max(mx4R,eR))*ARcpH1(AH1_(4.0)*mn4R+peakC.y);851AH1 hitMaxG=(peakC.x-max(mx4G,eG))*ARcpH1(AH1_(4.0)*mn4G+peakC.y);852AH1 hitMaxB=(peakC.x-max(mx4B,eB))*ARcpH1(AH1_(4.0)*mn4B+peakC.y);853AH1 lobeR=max(-hitMinR,hitMaxR);854AH1 lobeG=max(-hitMinG,hitMaxG);855AH1 lobeB=max(-hitMinB,hitMaxB);856AH1 lobe=max(AH1_(-FSR_RCAS_LIMIT),min(AMax3H1(lobeR,lobeG,lobeB),AH1_(0.0)))*AH2_AU1(con.y).x;857// Apply noise removal.858#ifdef FSR_RCAS_DENOISE859lobe*=nz;860#endif861// Resolve, which needs the medium precision rcp approximation to avoid visible tonality changes.862AH1 rcpL=APrxMedRcpH1(AH1_(4.0)*lobe+AH1_(1.0));863pixR=(lobe*bR+lobe*dR+lobe*hR+lobe*fR+eR)*rcpL;864pixG=(lobe*bG+lobe*dG+lobe*hG+lobe*fG+eG)*rcpL;865pixB=(lobe*bB+lobe*dB+lobe*hB+lobe*fB+eB)*rcpL;}866#endif867////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////868////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////869//_____________________________________________________________/\_______________________________________________________________870//==============================================================================================================================871// PACKED 16-BIT VERSION872//==============================================================================================================================873#if defined(A_GPU)&&defined(A_HALF)&&defined(FSR_RCAS_HX2)874// Input callback prototypes that need to be implemented by the calling shader875AH4 FsrRcasLoadHx2(ASW2 p);876void FsrRcasInputHx2(inout AH2 r,inout AH2 g,inout AH2 b);877//------------------------------------------------------------------------------------------------------------------------------878// Can be used to convert from packed Structures of Arrays to Arrays of Structures for store.879void FsrRcasDepackHx2(out AH4 pix0,out AH4 pix1,AH2 pixR,AH2 pixG,AH2 pixB){880#ifdef A_HLSL881// Invoke a slower path for DX only, since it won't allow uninitialized values.882pix0.a=pix1.a=0.0;883#endif884pix0.rgb=AH3(pixR.x,pixG.x,pixB.x);885pix1.rgb=AH3(pixR.y,pixG.y,pixB.y);}886//------------------------------------------------------------------------------------------------------------------------------887void FsrRcasHx2(888// Output values are for 2 8x8 tiles in a 16x8 region.889// pix<R,G,B>.x = left 8x8 tile890// pix<R,G,B>.y = right 8x8 tile891// This enables later processing to easily be packed as well.892out AH2 pixR,893out AH2 pixG,894out AH2 pixB,895#ifdef FSR_RCAS_PASSTHROUGH_ALPHA896out AH2 pixA,897#endif898AU2 ip, // Integer pixel position in output.899AU4 con){ // Constant generated by RcasSetup().900// No scaling algorithm uses minimal 3x3 pixel neighborhood.901ASW2 sp0=ASW2(ip);902AH3 b0=FsrRcasLoadHx2(sp0+ASW2( 0,-1)).rgb;903AH3 d0=FsrRcasLoadHx2(sp0+ASW2(-1, 0)).rgb;904#ifdef FSR_RCAS_PASSTHROUGH_ALPHA905AH4 ee0=FsrRcasLoadHx2(sp0);906AH3 e0=ee0.rgb;pixA.r=ee0.a;907#else908AH3 e0=FsrRcasLoadHx2(sp0).rgb;909#endif910AH3 f0=FsrRcasLoadHx2(sp0+ASW2( 1, 0)).rgb;911AH3 h0=FsrRcasLoadHx2(sp0+ASW2( 0, 1)).rgb;912ASW2 sp1=sp0+ASW2(8,0);913AH3 b1=FsrRcasLoadHx2(sp1+ASW2( 0,-1)).rgb;914AH3 d1=FsrRcasLoadHx2(sp1+ASW2(-1, 0)).rgb;915#ifdef FSR_RCAS_PASSTHROUGH_ALPHA916AH4 ee1=FsrRcasLoadHx2(sp1);917AH3 e1=ee1.rgb;pixA.g=ee1.a;918#else919AH3 e1=FsrRcasLoadHx2(sp1).rgb;920#endif921AH3 f1=FsrRcasLoadHx2(sp1+ASW2( 1, 0)).rgb;922AH3 h1=FsrRcasLoadHx2(sp1+ASW2( 0, 1)).rgb;923// Arrays of Structures to Structures of Arrays conversion.924AH2 bR=AH2(b0.r,b1.r);925AH2 bG=AH2(b0.g,b1.g);926AH2 bB=AH2(b0.b,b1.b);927AH2 dR=AH2(d0.r,d1.r);928AH2 dG=AH2(d0.g,d1.g);929AH2 dB=AH2(d0.b,d1.b);930AH2 eR=AH2(e0.r,e1.r);931AH2 eG=AH2(e0.g,e1.g);932AH2 eB=AH2(e0.b,e1.b);933AH2 fR=AH2(f0.r,f1.r);934AH2 fG=AH2(f0.g,f1.g);935AH2 fB=AH2(f0.b,f1.b);936AH2 hR=AH2(h0.r,h1.r);937AH2 hG=AH2(h0.g,h1.g);938AH2 hB=AH2(h0.b,h1.b);939// Run optional input transform.940FsrRcasInputHx2(bR,bG,bB);941FsrRcasInputHx2(dR,dG,dB);942FsrRcasInputHx2(eR,eG,eB);943FsrRcasInputHx2(fR,fG,fB);944FsrRcasInputHx2(hR,hG,hB);945// Luma times 2.946AH2 bL=bB*AH2_(0.5)+(bR*AH2_(0.5)+bG);947AH2 dL=dB*AH2_(0.5)+(dR*AH2_(0.5)+dG);948AH2 eL=eB*AH2_(0.5)+(eR*AH2_(0.5)+eG);949AH2 fL=fB*AH2_(0.5)+(fR*AH2_(0.5)+fG);950AH2 hL=hB*AH2_(0.5)+(hR*AH2_(0.5)+hG);951// Noise detection.952AH2 nz=AH2_(0.25)*bL+AH2_(0.25)*dL+AH2_(0.25)*fL+AH2_(0.25)*hL-eL;953nz=ASatH2(abs(nz)*APrxMedRcpH2(AMax3H2(AMax3H2(bL,dL,eL),fL,hL)-AMin3H2(AMin3H2(bL,dL,eL),fL,hL)));954nz=AH2_(-0.5)*nz+AH2_(1.0);955// Min and max of ring.956AH2 mn4R=min(AMin3H2(bR,dR,fR),hR);957AH2 mn4G=min(AMin3H2(bG,dG,fG),hG);958AH2 mn4B=min(AMin3H2(bB,dB,fB),hB);959AH2 mx4R=max(AMax3H2(bR,dR,fR),hR);960AH2 mx4G=max(AMax3H2(bG,dG,fG),hG);961AH2 mx4B=max(AMax3H2(bB,dB,fB),hB);962// Immediate constants for peak range.963AH2 peakC=AH2(1.0,-1.0*4.0);964// Limiters, these need to be high precision RCPs.965AH2 hitMinR=min(mn4R,eR)*ARcpH2(AH2_(4.0)*mx4R);966AH2 hitMinG=min(mn4G,eG)*ARcpH2(AH2_(4.0)*mx4G);967AH2 hitMinB=min(mn4B,eB)*ARcpH2(AH2_(4.0)*mx4B);968AH2 hitMaxR=(peakC.x-max(mx4R,eR))*ARcpH2(AH2_(4.0)*mn4R+peakC.y);969AH2 hitMaxG=(peakC.x-max(mx4G,eG))*ARcpH2(AH2_(4.0)*mn4G+peakC.y);970AH2 hitMaxB=(peakC.x-max(mx4B,eB))*ARcpH2(AH2_(4.0)*mn4B+peakC.y);971AH2 lobeR=max(-hitMinR,hitMaxR);972AH2 lobeG=max(-hitMinG,hitMaxG);973AH2 lobeB=max(-hitMinB,hitMaxB);974AH2 lobe=max(AH2_(-FSR_RCAS_LIMIT),min(AMax3H2(lobeR,lobeG,lobeB),AH2_(0.0)))*AH2_(AH2_AU1(con.y).x);975// Apply noise removal.976#ifdef FSR_RCAS_DENOISE977lobe*=nz;978#endif979// Resolve, which needs the medium precision rcp approximation to avoid visible tonality changes.980AH2 rcpL=APrxMedRcpH2(AH2_(4.0)*lobe+AH2_(1.0));981pixR=(lobe*bR+lobe*dR+lobe*hR+lobe*fR+eR)*rcpL;982pixG=(lobe*bG+lobe*dG+lobe*hG+lobe*fG+eG)*rcpL;983pixB=(lobe*bB+lobe*dB+lobe*hB+lobe*fB+eB)*rcpL;}984#endif985////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////986////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////987////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////988////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////989//_____________________________________________________________/\_______________________________________________________________990//==============================================================================================================================991//992// FSR - [LFGA] LINEAR FILM GRAIN APPLICATOR993//994//------------------------------------------------------------------------------------------------------------------------------995// Adding output-resolution film grain after scaling is a good way to mask both rendering and scaling artifacts.996// Suggest using tiled blue noise as film grain input, with peak noise frequency set for a specific look and feel.997// The 'Lfga*()' functions provide a convenient way to introduce grain.998// These functions limit grain based on distance to signal limits.999// This is done so that the grain is temporally energy preserving, and thus won't modify image tonality.1000// Grain application should be done in a linear colorspace.1001// The grain should be temporally changing, but have a temporal sum per pixel that adds to zero (non-biased).1002//------------------------------------------------------------------------------------------------------------------------------1003// Usage,1004// FsrLfga*(1005// color, // In/out linear colorspace color {0 to 1} ranged.1006// grain, // Per pixel grain texture value {-0.5 to 0.5} ranged, input is 3-channel to support colored grain.1007// amount); // Amount of grain (0 to 1} ranged.1008//------------------------------------------------------------------------------------------------------------------------------1009// Example if grain texture is monochrome: 'FsrLfgaF(color,AF3_(grain),amount)'1010//==============================================================================================================================1011#if defined(A_GPU)1012// Maximum grain is the minimum distance to the signal limit.1013void FsrLfgaF(inout AF3 c,AF3 t,AF1 a){c+=(t*AF3_(a))*min(AF3_(1.0)-c,c);}1014#endif1015//==============================================================================================================================1016#if defined(A_GPU)&&defined(A_HALF)1017// Half precision version (slower).1018void FsrLfgaH(inout AH3 c,AH3 t,AH1 a){c+=(t*AH3_(a))*min(AH3_(1.0)-c,c);}1019//------------------------------------------------------------------------------------------------------------------------------1020// Packed half precision version (faster).1021void FsrLfgaHx2(inout AH2 cR,inout AH2 cG,inout AH2 cB,AH2 tR,AH2 tG,AH2 tB,AH1 a){1022cR+=(tR*AH2_(a))*min(AH2_(1.0)-cR,cR);cG+=(tG*AH2_(a))*min(AH2_(1.0)-cG,cG);cB+=(tB*AH2_(a))*min(AH2_(1.0)-cB,cB);}1023#endif1024////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////1025////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////1026////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////1027////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////1028//_____________________________________________________________/\_______________________________________________________________1029//==============================================================================================================================1030//1031// FSR - [SRTM] SIMPLE REVERSIBLE TONE-MAPPER1032//1033//------------------------------------------------------------------------------------------------------------------------------1034// 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.1035// The tonemapper preserves RGB ratio, which helps maintain HDR color bleed during filtering.1036//------------------------------------------------------------------------------------------------------------------------------1037// Reversible tonemapper usage,1038// FsrSrtm*(color); // {0 to FP16_MAX} converted to {0 to 1}.1039// FsrSrtmInv*(color); // {0 to 1} converted into {0 to 32768, output peak safe for FP16}.1040//==============================================================================================================================1041#if defined(A_GPU)1042void FsrSrtmF(inout AF3 c){c*=AF3_(ARcpF1(AMax3F1(c.r,c.g,c.b)+AF1_(1.0)));}1043// The extra max solves the c=1.0 case (which is a /0).1044void FsrSrtmInvF(inout AF3 c){c*=AF3_(ARcpF1(max(AF1_(1.0/32768.0),AF1_(1.0)-AMax3F1(c.r,c.g,c.b))));}1045#endif1046//==============================================================================================================================1047#if defined(A_GPU)&&defined(A_HALF)1048void FsrSrtmH(inout AH3 c){c*=AH3_(ARcpH1(AMax3H1(c.r,c.g,c.b)+AH1_(1.0)));}1049void FsrSrtmInvH(inout AH3 c){c*=AH3_(ARcpH1(max(AH1_(1.0/32768.0),AH1_(1.0)-AMax3H1(c.r,c.g,c.b))));}1050//------------------------------------------------------------------------------------------------------------------------------1051void FsrSrtmHx2(inout AH2 cR,inout AH2 cG,inout AH2 cB){1052AH2 rcp=ARcpH2(AMax3H2(cR,cG,cB)+AH2_(1.0));cR*=rcp;cG*=rcp;cB*=rcp;}1053void FsrSrtmInvHx2(inout AH2 cR,inout AH2 cG,inout AH2 cB){1054AH2 rcp=ARcpH2(max(AH2_(1.0/32768.0),AH2_(1.0)-AMax3H2(cR,cG,cB)));cR*=rcp;cG*=rcp;cB*=rcp;}1055#endif1056////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////1057////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////1058////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////1059////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////1060//_____________________________________________________________/\_______________________________________________________________1061//==============================================================================================================================1062//1063// FSR - [TEPD] TEMPORAL ENERGY PRESERVING DITHER1064//1065//------------------------------------------------------------------------------------------------------------------------------1066// Temporally energy preserving dithered {0 to 1} linear to gamma 2.0 conversion.1067// Gamma 2.0 is used so that the conversion back to linear is just to square the color.1068// The conversion comes in 8-bit and 10-bit modes, designed for output to 8-bit UNORM or 10:10:10:2 respectively.1069// Given good non-biased temporal blue noise as dither input,1070// the output dither will temporally conserve energy.1071// This is done by choosing the linear nearest step point instead of perceptual nearest.1072// See code below for details.1073//------------------------------------------------------------------------------------------------------------------------------1074// DX SPEC RULES FOR FLOAT->UNORM 8-BIT CONVERSION1075// ===============================================1076// - Output is 'uint(floor(saturate(n)*255.0+0.5))'.1077// - Thus rounding is to nearest.1078// - NaN gets converted to zero.1079// - INF is clamped to {0.0 to 1.0}.1080//==============================================================================================================================1081#if defined(A_GPU)1082// Hand tuned integer position to dither value, with more values than simple checkerboard.1083// Only 32-bit has enough precision for this compddation.1084// Output is {0 to <1}.1085AF1 FsrTepdDitF(AU2 p,AU1 f){1086AF1 x=AF1_(p.x+f);1087AF1 y=AF1_(p.y);1088// The 1.61803 golden ratio.1089AF1 a=AF1_((1.0+sqrt(5.0))/2.0);1090// Number designed to provide a good visual pattern.1091AF1 b=AF1_(1.0/3.69);1092x=x*a+(y*b);1093return AFractF1(x);}1094//------------------------------------------------------------------------------------------------------------------------------1095// This version is 8-bit gamma 2.0.1096// The 'c' input is {0 to 1}.1097// Output is {0 to 1} ready for image store.1098void FsrTepdC8F(inout AF3 c,AF1 dit){1099AF3 n=sqrt(c);1100n=floor(n*AF3_(255.0))*AF3_(1.0/255.0);1101AF3 a=n*n;1102AF3 b=n+AF3_(1.0/255.0);b=b*b;1103// Ratio of 'a' to 'b' required to produce 'c'.1104// APrxLoRcpF1() won't work here (at least for very high dynamic ranges).1105// APrxMedRcpF1() is an IADD,FMA,MUL.1106AF3 r=(c-b)*APrxMedRcpF3(a-b);1107// Use the ratio as a cutoff to choose 'a' or 'b'.1108// AGtZeroF1() is a MUL.1109c=ASatF3(n+AGtZeroF3(AF3_(dit)-r)*AF3_(1.0/255.0));}1110//------------------------------------------------------------------------------------------------------------------------------1111// This version is 10-bit gamma 2.0.1112// The 'c' input is {0 to 1}.1113// Output is {0 to 1} ready for image store.1114void FsrTepdC10F(inout AF3 c,AF1 dit){1115AF3 n=sqrt(c);1116n=floor(n*AF3_(1023.0))*AF3_(1.0/1023.0);1117AF3 a=n*n;1118AF3 b=n+AF3_(1.0/1023.0);b=b*b;1119AF3 r=(c-b)*APrxMedRcpF3(a-b);1120c=ASatF3(n+AGtZeroF3(AF3_(dit)-r)*AF3_(1.0/1023.0));}1121#endif1122//==============================================================================================================================1123#if defined(A_GPU)&&defined(A_HALF)1124AH1 FsrTepdDitH(AU2 p,AU1 f){1125AF1 x=AF1_(p.x+f);1126AF1 y=AF1_(p.y);1127AF1 a=AF1_((1.0+sqrt(5.0))/2.0);1128AF1 b=AF1_(1.0/3.69);1129x=x*a+(y*b);1130return AH1(AFractF1(x));}1131//------------------------------------------------------------------------------------------------------------------------------1132void FsrTepdC8H(inout AH3 c,AH1 dit){1133AH3 n=sqrt(c);1134n=floor(n*AH3_(255.0))*AH3_(1.0/255.0);1135AH3 a=n*n;1136AH3 b=n+AH3_(1.0/255.0);b=b*b;1137AH3 r=(c-b)*APrxMedRcpH3(a-b);1138c=ASatH3(n+AGtZeroH3(AH3_(dit)-r)*AH3_(1.0/255.0));}1139//------------------------------------------------------------------------------------------------------------------------------1140void FsrTepdC10H(inout AH3 c,AH1 dit){1141AH3 n=sqrt(c);1142n=floor(n*AH3_(1023.0))*AH3_(1.0/1023.0);1143AH3 a=n*n;1144AH3 b=n+AH3_(1.0/1023.0);b=b*b;1145AH3 r=(c-b)*APrxMedRcpH3(a-b);1146c=ASatH3(n+AGtZeroH3(AH3_(dit)-r)*AH3_(1.0/1023.0));}1147//==============================================================================================================================1148// This computes dither for positions 'p' and 'p+{8,0}'.1149AH2 FsrTepdDitHx2(AU2 p,AU1 f){1150AF2 x;1151x.x=AF1_(p.x+f);1152x.y=x.x+AF1_(8.0);1153AF1 y=AF1_(p.y);1154AF1 a=AF1_((1.0+sqrt(5.0))/2.0);1155AF1 b=AF1_(1.0/3.69);1156x=x*AF2_(a)+AF2_(y*b);1157return AH2(AFractF2(x));}1158//------------------------------------------------------------------------------------------------------------------------------1159void FsrTepdC8Hx2(inout AH2 cR,inout AH2 cG,inout AH2 cB,AH2 dit){1160AH2 nR=sqrt(cR);1161AH2 nG=sqrt(cG);1162AH2 nB=sqrt(cB);1163nR=floor(nR*AH2_(255.0))*AH2_(1.0/255.0);1164nG=floor(nG*AH2_(255.0))*AH2_(1.0/255.0);1165nB=floor(nB*AH2_(255.0))*AH2_(1.0/255.0);1166AH2 aR=nR*nR;1167AH2 aG=nG*nG;1168AH2 aB=nB*nB;1169AH2 bR=nR+AH2_(1.0/255.0);bR=bR*bR;1170AH2 bG=nG+AH2_(1.0/255.0);bG=bG*bG;1171AH2 bB=nB+AH2_(1.0/255.0);bB=bB*bB;1172AH2 rR=(cR-bR)*APrxMedRcpH2(aR-bR);1173AH2 rG=(cG-bG)*APrxMedRcpH2(aG-bG);1174AH2 rB=(cB-bB)*APrxMedRcpH2(aB-bB);1175cR=ASatH2(nR+AGtZeroH2(dit-rR)*AH2_(1.0/255.0));1176cG=ASatH2(nG+AGtZeroH2(dit-rG)*AH2_(1.0/255.0));1177cB=ASatH2(nB+AGtZeroH2(dit-rB)*AH2_(1.0/255.0));}1178//------------------------------------------------------------------------------------------------------------------------------1179void FsrTepdC10Hx2(inout AH2 cR,inout AH2 cG,inout AH2 cB,AH2 dit){1180AH2 nR=sqrt(cR);1181AH2 nG=sqrt(cG);1182AH2 nB=sqrt(cB);1183nR=floor(nR*AH2_(1023.0))*AH2_(1.0/1023.0);1184nG=floor(nG*AH2_(1023.0))*AH2_(1.0/1023.0);1185nB=floor(nB*AH2_(1023.0))*AH2_(1.0/1023.0);1186AH2 aR=nR*nR;1187AH2 aG=nG*nG;1188AH2 aB=nB*nB;1189AH2 bR=nR+AH2_(1.0/1023.0);bR=bR*bR;1190AH2 bG=nG+AH2_(1.0/1023.0);bG=bG*bG;1191AH2 bB=nB+AH2_(1.0/1023.0);bB=bB*bB;1192AH2 rR=(cR-bR)*APrxMedRcpH2(aR-bR);1193AH2 rG=(cG-bG)*APrxMedRcpH2(aG-bG);1194AH2 rB=(cB-bB)*APrxMedRcpH2(aB-bB);1195cR=ASatH2(nR+AGtZeroH2(dit-rR)*AH2_(1.0/1023.0));1196cG=ASatH2(nG+AGtZeroH2(dit-rG)*AH2_(1.0/1023.0));1197cB=ASatH2(nB+AGtZeroH2(dit-rB)*AH2_(1.0/1023.0));}1198#endif119912001201