Path: blob/master/thirdparty/amd-fsr2/shaders/ffx_fsr2_postprocess_lock_status.h
9903 views
// This file is part of the FidelityFX SDK.1//2// Copyright (c) 2022-2023 Advanced Micro Devices, Inc. All rights reserved.3//4// Permission is hereby granted, free of charge, to any person obtaining a copy5// of this software and associated documentation files (the "Software"), to deal6// in the Software without restriction, including without limitation the rights7// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell8// copies of the Software, and to permit persons to whom the Software is9// furnished to do so, subject to the following conditions:10// The above copyright notice and this permission notice shall be included in11// all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN19// THE SOFTWARE.2021#ifndef FFX_FSR2_POSTPROCESS_LOCK_STATUS_H22#define FFX_FSR2_POSTPROCESS_LOCK_STATUS_H2324FfxFloat32x4 WrapShadingChangeLuma(FfxInt32x2 iPxSample)25{26return FfxFloat32x4(LoadMipLuma(iPxSample, LumaMipLevelToUse()), 0, 0, 0);27}2829#if FFX_HALF30FFX_MIN16_F4 WrapShadingChangeLuma(FFX_MIN16_I2 iPxSample)31{32return FFX_MIN16_F4(LoadMipLuma(iPxSample, LumaMipLevelToUse()), 0, 0, 0);33}34#endif3536#if FFX_FSR2_OPTION_POSTPROCESSLOCKSTATUS_SAMPLERS_USE_DATA_HALF && FFX_HALF37DeclareCustomFetchBilinearSamplesMin16(FetchShadingChangeLumaSamples, WrapShadingChangeLuma)38#else39DeclareCustomFetchBicubicSamples(FetchShadingChangeLumaSamples, WrapShadingChangeLuma)40#endif41DeclareCustomTextureSample(ShadingChangeLumaSample, Lanczos2, FetchShadingChangeLumaSamples)4243FfxFloat32 GetShadingChangeLuma(FfxInt32x2 iPxHrPos, FfxFloat32x2 fUvCoord)44{45FfxFloat32 fShadingChangeLuma = 0;4647#if 048fShadingChangeLuma = Exposure() * exp(ShadingChangeLumaSample(fUvCoord, LumaMipDimensions()).x);49#else5051const FfxFloat32 fDiv = FfxFloat32(2 << LumaMipLevelToUse());52FfxInt32x2 iMipRenderSize = FfxInt32x2(RenderSize() / fDiv);5354fUvCoord = ClampUv(fUvCoord, iMipRenderSize, LumaMipDimensions());55fShadingChangeLuma = Exposure() * exp(FfxFloat32(SampleMipLuma(fUvCoord, LumaMipLevelToUse())));56#endif5758fShadingChangeLuma = ffxPow(fShadingChangeLuma, 1.0f / 6.0f);5960return fShadingChangeLuma;61}6263void UpdateLockStatus(AccumulationPassCommonParams params,64FFX_PARAMETER_INOUT FfxFloat32 fReactiveFactor, LockState state,65FFX_PARAMETER_INOUT FfxFloat32x2 fLockStatus,66FFX_PARAMETER_OUT FfxFloat32 fLockContributionThisFrame,67FFX_PARAMETER_OUT FfxFloat32 fLuminanceDiff) {6869const FfxFloat32 fShadingChangeLuma = GetShadingChangeLuma(params.iPxHrPos, params.fHrUv);7071//init temporal shading change factor, init to -1 or so in reproject to know if "true new"?72fLockStatus[LOCK_TEMPORAL_LUMA] = (fLockStatus[LOCK_TEMPORAL_LUMA] == FfxFloat32(0.0f)) ? fShadingChangeLuma : fLockStatus[LOCK_TEMPORAL_LUMA];7374FfxFloat32 fPreviousShadingChangeLuma = fLockStatus[LOCK_TEMPORAL_LUMA];7576fLuminanceDiff = 1.0f - MinDividedByMax(fPreviousShadingChangeLuma, fShadingChangeLuma);7778if (state.NewLock) {79fLockStatus[LOCK_TEMPORAL_LUMA] = fShadingChangeLuma;8081fLockStatus[LOCK_LIFETIME_REMAINING] = (fLockStatus[LOCK_LIFETIME_REMAINING] != 0.0f) ? 2.0f : 1.0f;82}83else if(fLockStatus[LOCK_LIFETIME_REMAINING] <= 1.0f) {84fLockStatus[LOCK_TEMPORAL_LUMA] = ffxLerp(fLockStatus[LOCK_TEMPORAL_LUMA], FfxFloat32(fShadingChangeLuma), 0.5f);85}86else {87if (fLuminanceDiff > 0.1f) {88KillLock(fLockStatus);89}90}9192fReactiveFactor = ffxMax(fReactiveFactor, ffxSaturate((fLuminanceDiff - 0.1f) * 10.0f));93fLockStatus[LOCK_LIFETIME_REMAINING] *= (1.0f - fReactiveFactor);9495fLockStatus[LOCK_LIFETIME_REMAINING] *= ffxSaturate(1.0f - params.fAccumulationMask);96fLockStatus[LOCK_LIFETIME_REMAINING] *= FfxFloat32(params.fDepthClipFactor < 0.1f);9798// Compute this frame lock contribution99const FfxFloat32 fLifetimeContribution = ffxSaturate(fLockStatus[LOCK_LIFETIME_REMAINING] - 1.0f);100const FfxFloat32 fShadingChangeContribution = ffxSaturate(MinDividedByMax(fLockStatus[LOCK_TEMPORAL_LUMA], fShadingChangeLuma));101102fLockContributionThisFrame = ffxSaturate(ffxSaturate(fLifetimeContribution * 4.0f) * fShadingChangeContribution);103}104105#endif //!defined( FFX_FSR2_POSTPROCESS_LOCK_STATUS_H )106107108