Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/amd-fsr2/shaders/ffx_fsr2_reproject.h
9899 views
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_REPROJECT_H
23
#define FFX_FSR2_REPROJECT_H
24
25
#ifndef FFX_FSR2_OPTION_REPROJECT_USE_LANCZOS_TYPE
26
#define FFX_FSR2_OPTION_REPROJECT_USE_LANCZOS_TYPE 0 // Reference
27
#endif
28
29
FfxFloat32x4 WrapHistory(FfxInt32x2 iPxSample)
30
{
31
return LoadHistory(iPxSample);
32
}
33
34
#if FFX_HALF
35
FFX_MIN16_F4 WrapHistory(FFX_MIN16_I2 iPxSample)
36
{
37
return FFX_MIN16_F4(LoadHistory(iPxSample));
38
}
39
#endif
40
41
42
#if FFX_FSR2_OPTION_REPROJECT_SAMPLERS_USE_DATA_HALF && FFX_HALF
43
DeclareCustomFetchBicubicSamplesMin16(FetchHistorySamples, WrapHistory)
44
DeclareCustomTextureSampleMin16(HistorySample, FFX_FSR2_GET_LANCZOS_SAMPLER1D(FFX_FSR2_OPTION_REPROJECT_USE_LANCZOS_TYPE), FetchHistorySamples)
45
#else
46
DeclareCustomFetchBicubicSamples(FetchHistorySamples, WrapHistory)
47
DeclareCustomTextureSample(HistorySample, FFX_FSR2_GET_LANCZOS_SAMPLER1D(FFX_FSR2_OPTION_REPROJECT_USE_LANCZOS_TYPE), FetchHistorySamples)
48
#endif
49
50
FfxFloat32x4 WrapLockStatus(FfxInt32x2 iPxSample)
51
{
52
FfxFloat32x4 fSample = FfxFloat32x4(LoadLockStatus(iPxSample), 0.0f, 0.0f);
53
return fSample;
54
}
55
56
#if FFX_HALF
57
FFX_MIN16_F4 WrapLockStatus(FFX_MIN16_I2 iPxSample)
58
{
59
FFX_MIN16_F4 fSample = FFX_MIN16_F4(LoadLockStatus(iPxSample), 0.0, 0.0);
60
61
return fSample;
62
}
63
#endif
64
65
#if 1
66
#if FFX_FSR2_OPTION_REPROJECT_SAMPLERS_USE_DATA_HALF && FFX_HALF
67
DeclareCustomFetchBilinearSamplesMin16(FetchLockStatusSamples, WrapLockStatus)
68
DeclareCustomTextureSampleMin16(LockStatusSample, Bilinear, FetchLockStatusSamples)
69
#else
70
DeclareCustomFetchBilinearSamples(FetchLockStatusSamples, WrapLockStatus)
71
DeclareCustomTextureSample(LockStatusSample, Bilinear, FetchLockStatusSamples)
72
#endif
73
#else
74
#if FFX_FSR2_OPTION_REPROJECT_SAMPLERS_USE_DATA_HALF && FFX_HALF
75
DeclareCustomFetchBicubicSamplesMin16(FetchLockStatusSamples, WrapLockStatus)
76
DeclareCustomTextureSampleMin16(LockStatusSample, FFX_FSR2_GET_LANCZOS_SAMPLER1D(FFX_FSR2_OPTION_REPROJECT_USE_LANCZOS_TYPE), FetchLockStatusSamples)
77
#else
78
DeclareCustomFetchBicubicSamples(FetchLockStatusSamples, WrapLockStatus)
79
DeclareCustomTextureSample(LockStatusSample, FFX_FSR2_GET_LANCZOS_SAMPLER1D(FFX_FSR2_OPTION_REPROJECT_USE_LANCZOS_TYPE), FetchLockStatusSamples)
80
#endif
81
#endif
82
83
FfxFloat32x2 GetMotionVector(FfxInt32x2 iPxHrPos, FfxFloat32x2 fHrUv)
84
{
85
#if FFX_FSR2_OPTION_LOW_RESOLUTION_MOTION_VECTORS
86
FfxFloat32x2 fDilatedMotionVector = LoadDilatedMotionVector(FFX_MIN16_I2(fHrUv * RenderSize()));
87
#else
88
FfxFloat32x2 fDilatedMotionVector = LoadInputMotionVector(iPxHrPos);
89
#endif
90
91
return fDilatedMotionVector;
92
}
93
94
FfxBoolean IsUvInside(FfxFloat32x2 fUv)
95
{
96
return (fUv.x >= 0.0f && fUv.x <= 1.0f) && (fUv.y >= 0.0f && fUv.y <= 1.0f);
97
}
98
99
void ComputeReprojectedUVs(const AccumulationPassCommonParams params, FFX_PARAMETER_OUT FfxFloat32x2 fReprojectedHrUv, FFX_PARAMETER_OUT FfxBoolean bIsExistingSample)
100
{
101
fReprojectedHrUv = params.fHrUv + params.fMotionVector;
102
103
bIsExistingSample = IsUvInside(fReprojectedHrUv);
104
}
105
106
void ReprojectHistoryColor(const AccumulationPassCommonParams params, FFX_PARAMETER_OUT FfxFloat32x3 fHistoryColor, FFX_PARAMETER_OUT FfxFloat32 fTemporalReactiveFactor, FFX_PARAMETER_OUT FfxBoolean bInMotionLastFrame)
107
{
108
FfxFloat32x4 fHistory = HistorySample(params.fReprojectedHrUv, DisplaySize());
109
110
fHistoryColor = PrepareRgb(fHistory.rgb, Exposure(), PreviousFramePreExposure());
111
112
fHistoryColor = RGBToYCoCg(fHistoryColor);
113
114
//Compute temporal reactivity info
115
fTemporalReactiveFactor = ffxSaturate(abs(fHistory.w));
116
bInMotionLastFrame = (fHistory.w < 0.0f);
117
}
118
119
LockState ReprojectHistoryLockStatus(const AccumulationPassCommonParams params, FFX_PARAMETER_OUT FfxFloat32x2 fReprojectedLockStatus)
120
{
121
LockState state = { FFX_FALSE, FFX_FALSE };
122
const FfxFloat32 fNewLockIntensity = LoadRwNewLocks(params.iPxHrPos);
123
state.NewLock = fNewLockIntensity > (127.0f / 255.0f);
124
125
FfxFloat32 fInPlaceLockLifetime = state.NewLock ? fNewLockIntensity : 0;
126
127
fReprojectedLockStatus = SampleLockStatus(params.fReprojectedHrUv);
128
129
if (fReprojectedLockStatus[LOCK_LIFETIME_REMAINING] != FfxFloat32(0.0f)) {
130
state.WasLockedPrevFrame = true;
131
}
132
133
return state;
134
}
135
136
#endif //!defined( FFX_FSR2_REPROJECT_H )
137
138