Skip to content
0.2.0-dev Download ZIP
145 lines ·5.4 KB Raw
1// This file is part of the FidelityFX SDK.
2//
3// Copyright (c) 2022-2023 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#ifndef FFX_FSR2_RECONSTRUCT_DILATED_VELOCITY_AND_PREVIOUS_DEPTH_H
23#define FFX_FSR2_RECONSTRUCT_DILATED_VELOCITY_AND_PREVIOUS_DEPTH_H
24
25void ReconstructPrevDepth(FfxInt32x2 iPxPos, FfxFloat32 fDepth, FfxFloat32x2 fMotionVector, FfxInt32x2 iPxDepthSize)
26{
27 fMotionVector *= FfxFloat32(length(fMotionVector * DisplaySize()) > 0.1f);
28
29 FfxFloat32x2 fUv = (iPxPos + FfxFloat32(0.5)) / iPxDepthSize;
30 FfxFloat32x2 fReprojectedUv = fUv + fMotionVector;
31
32 BilinearSamplingData bilinearInfo = GetBilinearSamplingData(fReprojectedUv, RenderSize());
33
34 // Project current depth into previous frame locations.
35 // Push to all pixels having some contribution if reprojection is using bilinear logic.
36 for (FfxInt32 iSampleIndex = 0; iSampleIndex < 4; iSampleIndex++) {
37
38 const FfxInt32x2 iOffset = bilinearInfo.iOffsets[iSampleIndex];
39 FfxFloat32 fWeight = bilinearInfo.fWeights[iSampleIndex];
40
41 if (fWeight > fReconstructedDepthBilinearWeightThreshold) {
42
43 FfxInt32x2 iStorePos = bilinearInfo.iBasePos + iOffset;
44 if (IsOnScreen(iStorePos, iPxDepthSize)) {
45 StoreReconstructedDepth(iStorePos, fDepth);
46 }
47 }
48 }
49}
50
51void FindNearestDepth(FFX_PARAMETER_IN FfxInt32x2 iPxPos, FFX_PARAMETER_IN FfxInt32x2 iPxSize, FFX_PARAMETER_OUT FfxFloat32 fNearestDepth, FFX_PARAMETER_OUT FfxInt32x2 fNearestDepthCoord)
52{
53 const FfxInt32 iSampleCount = 9;
54 const FfxInt32x2 iSampleOffsets[iSampleCount] = {
55 FfxInt32x2(+0, +0),
56 FfxInt32x2(+1, +0),
57 FfxInt32x2(+0, +1),
58 FfxInt32x2(+0, -1),
59 FfxInt32x2(-1, +0),
60 FfxInt32x2(-1, +1),
61 FfxInt32x2(+1, +1),
62 FfxInt32x2(-1, -1),
63 FfxInt32x2(+1, -1),
64 };
65
66 // pull out the depth loads to allow SC to batch them
67 FfxFloat32 depth[9];
68 FfxInt32 iSampleIndex = 0;
69 FFX_UNROLL
70 for (iSampleIndex = 0; iSampleIndex < iSampleCount; ++iSampleIndex) {
71
72 FfxInt32x2 iPos = iPxPos + iSampleOffsets[iSampleIndex];
73 depth[iSampleIndex] = LoadInputDepth(iPos);
74 }
75
76 // find closest depth
77 fNearestDepthCoord = iPxPos;
78 fNearestDepth = depth[0];
79 FFX_UNROLL
80 for (iSampleIndex = 1; iSampleIndex < iSampleCount; ++iSampleIndex) {
81
82 FfxInt32x2 iPos = iPxPos + iSampleOffsets[iSampleIndex];
83 if (IsOnScreen(iPos, iPxSize)) {
84
85 FfxFloat32 fNdDepth = depth[iSampleIndex];
86#if FFX_FSR2_OPTION_INVERTED_DEPTH
87 if (fNdDepth > fNearestDepth) {
88#else
89 if (fNdDepth < fNearestDepth) {
90#endif
91 fNearestDepthCoord = iPos;
92 fNearestDepth = fNdDepth;
93 }
94 }
95 }
96}
97
98FfxFloat32 ComputeLockInputLuma(FfxInt32x2 iPxLrPos)
99{
100 //We assume linear data. if non-linear input (sRGB, ...),
101 //then we should convert to linear first and back to sRGB on output.
102 FfxFloat32x3 fRgb = ffxMax(FfxFloat32x3(0, 0, 0), LoadInputColor(iPxLrPos));
103
104 // Use internal auto exposure for locking logic
105 fRgb /= PreExposure();
106 fRgb *= Exposure();
107
108#if FFX_FSR2_OPTION_HDR_COLOR_INPUT
109 fRgb = Tonemap(fRgb);
110#endif
111
112 //compute luma used to lock pixels, if used elsewhere the ffxPow must be moved!
113 const FfxFloat32 fLockInputLuma = ffxPow(RGBToPerceivedLuma(fRgb), FfxFloat32(1.0 / 6.0));
114
115 return fLockInputLuma;
116}
117
118void ReconstructAndDilate(FfxInt32x2 iPxLrPos)
119{
120 FfxFloat32 fDilatedDepth;
121 FfxInt32x2 iNearestDepthCoord;
122
123 FindNearestDepth(iPxLrPos, RenderSize(), fDilatedDepth, iNearestDepthCoord);
124
125#if FFX_FSR2_OPTION_LOW_RESOLUTION_MOTION_VECTORS
126 FfxInt32x2 iSamplePos = iPxLrPos;
127 FfxInt32x2 iMotionVectorPos = iNearestDepthCoord;
128#else
129 FfxInt32x2 iSamplePos = ComputeHrPosFromLrPos(iPxLrPos);
130 FfxInt32x2 iMotionVectorPos = ComputeHrPosFromLrPos(iNearestDepthCoord);
131#endif
132
133 FfxFloat32x2 fDilatedMotionVector = LoadInputMotionVector(iMotionVectorPos);
134
135 StoreDilatedDepth(iPxLrPos, fDilatedDepth);
136 StoreDilatedMotionVector(iPxLrPos, fDilatedMotionVector);
137
138 ReconstructPrevDepth(iPxLrPos, fDilatedDepth, fDilatedMotionVector, RenderSize());
139
140 FfxFloat32 fLockInputLuma = ComputeLockInputLuma(iPxLrPos);
141 StoreLockInputLuma(iPxLrPos, fLockInputLuma);
142}
143
144
145#endif //!defined( FFX_FSR2_RECONSTRUCT_DILATED_VELOCITY_AND_PREVIOUS_DEPTH_H )