// This file is part of the FidelityFX Super Resolution 3.1 Unreal Engine Plugin. // // Copyright (c) 2023-2025 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. #include "FFXRHIBackendFSRShaders.h" #include "FFXRHIBackendSubPass.h" #include "FFXRHIBackend.h" #include "ShaderParameterStruct.h" #include "ShaderCompilerCore.h" #if UE_VERSION_AT_LEAST(5, 2, 0) #include "DataDrivenShaderPlatformInfo.h" #else #include "RHIDefinitions.h" #endif #include "FFXFSR3.h" #include "ffx_fsr3upscaler_private.h" #if UE_VERSION_AT_LEAST(5, 2, 0) #include "DataDrivenShaderPlatformInfo.h" #else #include "RHIDefinitions.h" #endif class FFXRHILumaPyramidCS : public FFXFSRGlobalShader { public: DECLARE_GLOBAL_SHADER(FFXRHILumaPyramidCS); SHADER_USE_PARAMETER_STRUCT(FFXRHILumaPyramidCS, FFXFSRGlobalShader); using FParameters = FFXFSRGlobalShader::FParameters; static void BindParameters(FRDGBuilder& GraphBuilder, FFXBackendState* Context, const FfxGpuJobDescription* job, FParameters* Parameters) { FFXFSRGlobalShader::BindParameters(GraphBuilder, Context, job, Parameters); } using FPermutationDomain = FFXFSRGlobalShader::FPermutationDomain; static bool ShouldCompilePermutation(const FGlobalShaderPermutationParameters& Parameters) { return FFXFSRGlobalShader::ShouldCompilePermutation(Parameters); } static void ModifyCompilationEnvironment(const FGlobalShaderPermutationParameters& Parameters, FShaderCompilerEnvironment& OutEnvironment) { FFXFSRGlobalShader::ModifyCompilationEnvironment(Parameters, OutEnvironment); OutEnvironment.CompilerFlags.Add(CFLAG_PreferFlowControl); } static const wchar_t** GetBoundSRVNames() { static const wchar_t* SRVs[] = { L"r_current_luma", L"r_farthest_depth", }; return SRVs; } static const wchar_t** GetBoundUAVNames() { static const wchar_t* UAVs[] = { L"rw_spd_global_atomic", L"rw_frame_info", L"rw_spd_mip0", L"rw_spd_mip1", L"rw_spd_mip2", L"rw_spd_mip3", L"rw_spd_mip4", L"rw_spd_mip5", L"rw_farthest_depth_mip1", }; return UAVs; } static const wchar_t** GetBoundCBNames() { static const wchar_t* CBs[] = { L"cbFSR3Upscaler", L"cbSPD", }; return CBs; } static uint32* GetBoundSRVs() { static uint32 SRVs[] = { FFX_FSR3UPSCALER_RESOURCE_IDENTIFIER_CURRENT_LUMA, FFX_FSR3UPSCALER_RESOURCE_IDENTIFIER_FARTHEST_DEPTH, }; return SRVs; } static uint32 GetNumBoundSRVs() { return 2; } static uint32* GetBoundUAVs() { static uint32 UAVs[] = { FFX_FSR3UPSCALER_RESOURCE_IDENTIFIER_SPD_ATOMIC_COUNT, FFX_FSR3UPSCALER_RESOURCE_IDENTIFIER_FRAME_INFO, FFX_FSR3UPSCALER_RESOURCE_IDENTIFIER_SPD_MIPS_LEVEL_0, FFX_FSR3UPSCALER_RESOURCE_IDENTIFIER_SPD_MIPS_LEVEL_1, FFX_FSR3UPSCALER_RESOURCE_IDENTIFIER_SPD_MIPS_LEVEL_2, FFX_FSR3UPSCALER_RESOURCE_IDENTIFIER_SPD_MIPS_LEVEL_3, FFX_FSR3UPSCALER_RESOURCE_IDENTIFIER_SPD_MIPS_LEVEL_4, FFX_FSR3UPSCALER_RESOURCE_IDENTIFIER_SPD_MIPS_LEVEL_5, FFX_FSR3UPSCALER_RESOURCE_IDENTIFIER_FARTHEST_DEPTH_MIP1, }; return UAVs; } static uint32 GetNumBoundUAVs() { return 9; } static uint32* GetBoundCBs() { static uint32 CBs[] = { FFX_FSR3UPSCALER_CONSTANTBUFFER_IDENTIFIER_FSR3UPSCALER, FFX_FSR3UPSCALER_CONSTANTBUFFER_IDENTIFIER_SPD, }; return CBs; } static uint32 GetNumConstants() { return 2; } static uint32 GetConstantSizeInDWords(uint32 Index) { static uint32 Sizes[] = { sizeof(FFXFSRPassParameters) / sizeof(uint32), sizeof(FFXLumaPyramidParameters) / sizeof(uint32) }; return Sizes[Index]; } }; IMPLEMENT_GLOBAL_SHADER(FFXRHILumaPyramidCS, "/Plugin/FFX/Private/ffx_fsr3upscaler_luma_pyramid_pass.usf", "CS", SF_Compute); IFFXRHIBackendSubPass* GetLumaPyramidPass(FfxPass pass, uint32_t permutationOptions, const FfxPipelineDescription* desc, FfxPipelineState* outPipeline, bool bSupportHalf, bool bPreferWave64) { auto* Pipeline = new TFFXRHIBackendSubPass(TEXT("FidelityFX-FSR3/LumaPyramid (CS)"), desc, outPipeline, bSupportHalf); Pipeline->Permutation.template Set(desc->contextFlags & FFX_FSR3UPSCALER_ENABLE_HIGH_DYNAMIC_RANGE); Pipeline->Permutation.template Set(!(desc->contextFlags & FFX_FSR3UPSCALER_ENABLE_DISPLAY_RESOLUTION_MOTION_VECTORS)); Pipeline->Permutation.template Set(desc->contextFlags & FFX_FSR3UPSCALER_ENABLE_MOTION_VECTORS_JITTER_CANCELLATION); Pipeline->Permutation.template Set(desc->contextFlags & FFX_FSR3UPSCALER_ENABLE_DEPTH_INVERTED); Pipeline->Permutation.template Set(pass == FFX_FSR3UPSCALER_PASS_ACCUMULATE_SHARPEN); Pipeline->Permutation.template Set(permutationOptions & FSR3UPSCALER_SHADER_PERMUTATION_USE_LANCZOS_TYPE); Pipeline->Permutation.template Set(bSupportHalf); Pipeline->Permutation.template Set(bPreferWave64); return Pipeline; }