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