ffx_fsr2_reproject.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #ifndef FFX_FSR2_REPROJECT_H
  22. #define FFX_FSR2_REPROJECT_H
  23. #ifndef FFX_FSR2_OPTION_REPROJECT_USE_LANCZOS_TYPE
  24. #define FFX_FSR2_OPTION_REPROJECT_USE_LANCZOS_TYPE 0 // Reference
  25. #endif
  26. FfxFloat32x4 WrapHistory(FfxInt32x2 iPxSample)
  27. {
  28. return LoadHistory(iPxSample);
  29. }
  30. #if FFX_HALF
  31. FFX_MIN16_F4 WrapHistory(FFX_MIN16_I2 iPxSample)
  32. {
  33. return FFX_MIN16_F4(LoadHistory(iPxSample));
  34. }
  35. #endif
  36. #if FFX_FSR2_OPTION_REPROJECT_SAMPLERS_USE_DATA_HALF && FFX_HALF
  37. DeclareCustomFetchBicubicSamplesMin16(FetchHistorySamples, WrapHistory)
  38. DeclareCustomTextureSampleMin16(HistorySample, FFX_FSR2_GET_LANCZOS_SAMPLER1D(FFX_FSR2_OPTION_REPROJECT_USE_LANCZOS_TYPE), FetchHistorySamples)
  39. #else
  40. DeclareCustomFetchBicubicSamples(FetchHistorySamples, WrapHistory)
  41. DeclareCustomTextureSample(HistorySample, FFX_FSR2_GET_LANCZOS_SAMPLER1D(FFX_FSR2_OPTION_REPROJECT_USE_LANCZOS_TYPE), FetchHistorySamples)
  42. #endif
  43. FfxFloat32x4 WrapLockStatus(FfxInt32x2 iPxSample)
  44. {
  45. FfxFloat32x4 fSample = FfxFloat32x4(LoadLockStatus(iPxSample), 0.0f, 0.0f);
  46. return fSample;
  47. }
  48. #if FFX_HALF
  49. FFX_MIN16_F4 WrapLockStatus(FFX_MIN16_I2 iPxSample)
  50. {
  51. FFX_MIN16_F4 fSample = FFX_MIN16_F4(LoadLockStatus(iPxSample), 0.0, 0.0);
  52. return fSample;
  53. }
  54. #endif
  55. #if 1
  56. #if FFX_FSR2_OPTION_REPROJECT_SAMPLERS_USE_DATA_HALF && FFX_HALF
  57. DeclareCustomFetchBilinearSamplesMin16(FetchLockStatusSamples, WrapLockStatus)
  58. DeclareCustomTextureSampleMin16(LockStatusSample, Bilinear, FetchLockStatusSamples)
  59. #else
  60. DeclareCustomFetchBilinearSamples(FetchLockStatusSamples, WrapLockStatus)
  61. DeclareCustomTextureSample(LockStatusSample, Bilinear, FetchLockStatusSamples)
  62. #endif
  63. #else
  64. #if FFX_FSR2_OPTION_REPROJECT_SAMPLERS_USE_DATA_HALF && FFX_HALF
  65. DeclareCustomFetchBicubicSamplesMin16(FetchLockStatusSamples, WrapLockStatus)
  66. DeclareCustomTextureSampleMin16(LockStatusSample, FFX_FSR2_GET_LANCZOS_SAMPLER1D(FFX_FSR2_OPTION_REPROJECT_USE_LANCZOS_TYPE), FetchLockStatusSamples)
  67. #else
  68. DeclareCustomFetchBicubicSamples(FetchLockStatusSamples, WrapLockStatus)
  69. DeclareCustomTextureSample(LockStatusSample, FFX_FSR2_GET_LANCZOS_SAMPLER1D(FFX_FSR2_OPTION_REPROJECT_USE_LANCZOS_TYPE), FetchLockStatusSamples)
  70. #endif
  71. #endif
  72. FfxFloat32x2 GetMotionVector(FfxInt32x2 iPxHrPos, FfxFloat32x2 fHrUv)
  73. {
  74. #if FFX_FSR2_OPTION_LOW_RESOLUTION_MOTION_VECTORS
  75. FfxFloat32x2 fDilatedMotionVector = LoadDilatedMotionVector(FFX_MIN16_I2(fHrUv * RenderSize()));
  76. #else
  77. FfxFloat32x2 fDilatedMotionVector = LoadInputMotionVector(iPxHrPos);
  78. #endif
  79. return fDilatedMotionVector;
  80. }
  81. FfxBoolean IsUvInside(FfxFloat32x2 fUv)
  82. {
  83. return (fUv.x >= 0.0f && fUv.x <= 1.0f) && (fUv.y >= 0.0f && fUv.y <= 1.0f);
  84. }
  85. void ComputeReprojectedUVs(const AccumulationPassCommonParams params, FFX_PARAMETER_OUT FfxFloat32x2 fReprojectedHrUv, FFX_PARAMETER_OUT FfxBoolean bIsExistingSample)
  86. {
  87. fReprojectedHrUv = params.fHrUv + params.fMotionVector;
  88. bIsExistingSample = IsUvInside(fReprojectedHrUv);
  89. }
  90. void ReprojectHistoryColor(const AccumulationPassCommonParams params, FFX_PARAMETER_OUT FfxFloat32x3 fHistoryColor, FFX_PARAMETER_OUT FfxFloat32 fTemporalReactiveFactor, FFX_PARAMETER_OUT FfxBoolean bInMotionLastFrame)
  91. {
  92. FfxFloat32x4 fHistory = HistorySample(params.fReprojectedHrUv, DisplaySize());
  93. fHistoryColor = PrepareRgb(fHistory.rgb, Exposure(), PreviousFramePreExposure());
  94. fHistoryColor = RGBToYCoCg(fHistoryColor);
  95. //Compute temporal reactivity info
  96. fTemporalReactiveFactor = ffxSaturate(abs(fHistory.w));
  97. bInMotionLastFrame = (fHistory.w < 0.0f);
  98. }
  99. LockState ReprojectHistoryLockStatus(const AccumulationPassCommonParams params, FFX_PARAMETER_OUT FfxFloat32x2 fReprojectedLockStatus)
  100. {
  101. LockState state = { FFX_FALSE, FFX_FALSE };
  102. const FfxFloat32 fNewLockIntensity = LoadRwNewLocks(params.iPxHrPos);
  103. state.NewLock = fNewLockIntensity > (127.0f / 255.0f);
  104. FfxFloat32 fInPlaceLockLifetime = state.NewLock ? fNewLockIntensity : 0;
  105. fReprojectedLockStatus = SampleLockStatus(params.fReprojectedHrUv);
  106. if (fReprojectedLockStatus[LOCK_LIFETIME_REMAINING] != FfxFloat32(0.0f)) {
  107. state.WasLockedPrevFrame = true;
  108. }
  109. return state;
  110. }
  111. #endif //!defined( FFX_FSR2_REPROJECT_H )