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