diff --git a/Assets/Resources/Shaders/ffx_fsr2_callbacks_hlsl.h b/Assets/Resources/Shaders/ffx_fsr2_callbacks_hlsl.h new file mode 100644 index 0000000..4a5fbca --- /dev/null +++ b/Assets/Resources/Shaders/ffx_fsr2_callbacks_hlsl.h @@ -0,0 +1,849 @@ +// This file is part of the FidelityFX SDK. +// +// Copyright (c) 2022 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 "ffx_fsr2_resources.h" + +#if defined(FFX_GPU) +#ifdef __hlsl_dx_compiler +#pragma dxc diagnostic push +#pragma dxc diagnostic ignored "-Wambig-lit-shift" +#endif //__hlsl_dx_compiler +#include "ffx_core.h" +#ifdef __hlsl_dx_compiler +#pragma dxc diagnostic pop +#endif //__hlsl_dx_compiler +#endif // #if defined(FFX_GPU) + +#if defined(FFX_GPU) +#ifndef FFX_FSR2_PREFER_WAVE64 +#define FFX_FSR2_PREFER_WAVE64 +#endif // #if defined(FFX_GPU) + +#if defined(FFX_GPU) +#pragma warning(disable: 3205) // conversion from larger type to smaller +#endif // #if defined(FFX_GPU) + +#define DECLARE_SRV_REGISTER(regIndex) t##regIndex +#define DECLARE_UAV_REGISTER(regIndex) u##regIndex +#define DECLARE_CB_REGISTER(regIndex) b##regIndex +#define FFX_FSR2_DECLARE_SRV(regIndex) register(DECLARE_SRV_REGISTER(regIndex)) +#define FFX_FSR2_DECLARE_UAV(regIndex) register(DECLARE_UAV_REGISTER(regIndex)) +#define FFX_FSR2_DECLARE_CB(regIndex) register(DECLARE_CB_REGISTER(regIndex)) + +#if defined(FSR2_BIND_CB_FSR2) + cbuffer cbFSR2 : FFX_FSR2_DECLARE_CB(FSR2_BIND_CB_FSR2) + { + FfxInt32x2 uRenderSize; + FfxInt32x2 uDisplaySize; + FfxInt32x2 uLumaMipDimensions; + FfxInt32 uLumaMipLevelToUse; + FfxUInt32 uFrameIndex; + FfxFloat32x2 fDisplaySizeRcp; + FfxFloat32x2 fJitter; + FfxFloat32x4 fDeviceToViewDepth; + FfxFloat32x2 depthclip_uv_scale; + FfxFloat32x2 postprocessed_lockstatus_uv_scale; + FfxFloat32x2 reactive_mask_dim_rcp; + FfxFloat32x2 MotionVectorScale; + FfxFloat32x2 fDownscaleFactor; + FfxFloat32 fPreExposure; + FfxFloat32 fTanHalfFOV; + FfxFloat32x2 fMotionVectorJitterCancellation; + FfxFloat32 fJitterSequenceLength; + FfxFloat32 fLockInitialLifetime; + FfxFloat32 fLockTickDelta; + FfxFloat32 fDeltaTime; + FfxFloat32 fDynamicResChangeFactor; + FfxFloat32 fLumaMipRcp; +#define FFX_FSR2_CONSTANT_BUFFER_1_SIZE 36 // Number of 32-bit values. This must be kept in sync with the cbFSR2 size. + }; +#else + #define iRenderSize 0 + #define iDisplaySize 0 + #define iLumaMipDimensions 0 + #define iLumaMipLevelToUse 0 + #define iFrameIndex 0 + #define fDisplaySizeRcp 0 + #define fJitter 0 + #define fDeviceToViewDepth FfxFloat32x4(0,0,0,0) + #define depthclip_uv_scale 0 + #define postprocessed_lockstatus_uv_scale 0 + #define reactive_mask_dim_rcp 0 + #define MotionVectorScale 0 + #define fDownscaleFactor 0 + #define fPreExposure 0 + #define fTanHalfFOV 0 + #define fMotionVectorJitterCancellation 0 + #define fJitterSequenceLength 0 + #define fLockInitialLifetime 0 + #define fLockTickDelta 0 + #define fDeltaTime 0 + #define fDynamicResChangeFactor 0 + #define fLumaMipRcp 0 +#endif + +#if defined(FFX_GPU) +#define FFX_FSR2_ROOTSIG_STRINGIFY(p) FFX_FSR2_ROOTSIG_STR(p) +#define FFX_FSR2_ROOTSIG_STR(p) #p +#define FFX_FSR2_ROOTSIG [RootSignature( "DescriptorTable(UAV(u0, numDescriptors = " FFX_FSR2_ROOTSIG_STRINGIFY(FFX_FSR2_RESOURCE_IDENTIFIER_COUNT) ")), " \ + "DescriptorTable(SRV(t0, numDescriptors = " FFX_FSR2_ROOTSIG_STRINGIFY(FFX_FSR2_RESOURCE_IDENTIFIER_COUNT) ")), " \ + "RootConstants(num32BitConstants=" FFX_FSR2_ROOTSIG_STRINGIFY(FFX_FSR2_CONSTANT_BUFFER_1_SIZE) ", b0), " \ + "StaticSampler(s0, filter = FILTER_MIN_MAG_MIP_POINT, " \ + "addressU = TEXTURE_ADDRESS_CLAMP, " \ + "addressV = TEXTURE_ADDRESS_CLAMP, " \ + "addressW = TEXTURE_ADDRESS_CLAMP, " \ + "comparisonFunc = COMPARISON_NEVER, " \ + "borderColor = STATIC_BORDER_COLOR_TRANSPARENT_BLACK), " \ + "StaticSampler(s1, filter = FILTER_MIN_MAG_MIP_LINEAR, " \ + "addressU = TEXTURE_ADDRESS_CLAMP, " \ + "addressV = TEXTURE_ADDRESS_CLAMP, " \ + "addressW = TEXTURE_ADDRESS_CLAMP, " \ + "comparisonFunc = COMPARISON_NEVER, " \ + "borderColor = STATIC_BORDER_COLOR_TRANSPARENT_BLACK)" )] + +#define FFX_FSR2_CONSTANT_BUFFER_2_SIZE 6 // Number of 32-bit values. This must be kept in sync with max( cbRCAS , cbSPD) size. + +#define FFX_FSR2_CB2_ROOTSIG [RootSignature( "DescriptorTable(UAV(u0, numDescriptors = " FFX_FSR2_ROOTSIG_STRINGIFY(FFX_FSR2_RESOURCE_IDENTIFIER_COUNT) ")), " \ + "DescriptorTable(SRV(t0, numDescriptors = " FFX_FSR2_ROOTSIG_STRINGIFY(FFX_FSR2_RESOURCE_IDENTIFIER_COUNT) ")), " \ + "RootConstants(num32BitConstants=" FFX_FSR2_ROOTSIG_STRINGIFY(FFX_FSR2_CONSTANT_BUFFER_1_SIZE) ", b0), " \ + "RootConstants(num32BitConstants=" FFX_FSR2_ROOTSIG_STRINGIFY(FFX_FSR2_CONSTANT_BUFFER_2_SIZE) ", b1), " \ + "StaticSampler(s0, filter = FILTER_MIN_MAG_MIP_POINT, " \ + "addressU = TEXTURE_ADDRESS_CLAMP, " \ + "addressV = TEXTURE_ADDRESS_CLAMP, " \ + "addressW = TEXTURE_ADDRESS_CLAMP, " \ + "comparisonFunc = COMPARISON_NEVER, " \ + "borderColor = STATIC_BORDER_COLOR_TRANSPARENT_BLACK), " \ + "StaticSampler(s1, filter = FILTER_MIN_MAG_MIP_LINEAR, " \ + "addressU = TEXTURE_ADDRESS_CLAMP, " \ + "addressV = TEXTURE_ADDRESS_CLAMP, " \ + "addressW = TEXTURE_ADDRESS_CLAMP, " \ + "comparisonFunc = COMPARISON_NEVER, " \ + "borderColor = STATIC_BORDER_COLOR_TRANSPARENT_BLACK)" )] +#if defined(FFX_FSR2_EMBED_ROOTSIG) +#define FFX_FSR2_EMBED_ROOTSIG_CONTENT FFX_FSR2_ROOTSIG +#define FFX_FSR2_EMBED_CB2_ROOTSIG_CONTENT FFX_FSR2_CB2_ROOTSIG +#else +#define FFX_FSR2_EMBED_ROOTSIG_CONTENT +#define FFX_FSR2_EMBED_CB2_ROOTSIG_CONTENT +#endif // #if FFX_FSR2_EMBED_ROOTSIG +#endif // #if defined(FFX_GPU) + + +FfxFloat32 LumaMipRcp() +{ + return fLumaMipRcp; +} + +FfxInt32x2 LumaMipDimensions() +{ + return uLumaMipDimensions; +} + +FfxInt32 LumaMipLevelToUse() +{ + return uLumaMipLevelToUse; +} + +FfxFloat32x2 DownscaleFactor() +{ + return fDownscaleFactor; +} + +FfxFloat32x2 Jitter() +{ + return fJitter; +} + +FfxFloat32x2 MotionVectorJitterCancellation() +{ + return fMotionVectorJitterCancellation; +} + +FfxInt32x2 RenderSize() +{ + return uRenderSize; +} + +FfxInt32x2 DisplaySize() +{ + return uDisplaySize; +} + +FfxFloat32x2 DisplaySizeRcp() +{ + return fDisplaySizeRcp; +} + +FfxFloat32 JitterSequenceLength() +{ + return fJitterSequenceLength; +} + +FfxFloat32 LockInitialLifetime() +{ + return fLockInitialLifetime; +} + +FfxFloat32 LockTickDelta() +{ + return fLockTickDelta; +} + +FfxFloat32 DeltaTime() +{ + return fDeltaTime; +} + +FfxFloat32 MaxAccumulationWeight() +{ + const FfxFloat32 averageLanczosWeightPerFrame = 0.74f; // Average lanczos weight for jitter accumulated samples + + return 12; //32.0f * averageLanczosWeightPerFrame; +} + +FfxFloat32 DynamicResChangeFactor() +{ + return fDynamicResChangeFactor; +} + +FfxUInt32 FrameIndex() +{ + return uFrameIndex; +} + +SamplerState s_PointClamp : register(s0); +SamplerState s_LinearClamp : register(s1); + +// SRVs +#if defined(FFX_INTERNAL) + Texture2D r_input_color_jittered : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_COLOR); + Texture2D r_motion_vectors : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_MOTION_VECTORS); + Texture2D r_depth : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_DEPTH); + Texture2D r_exposure : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_EXPOSURE); + Texture2D r_reactive_mask : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_REACTIVE_MASK); + Texture2D r_transparency_and_composition_mask : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_TRANSPARENCY_AND_COMPOSITION_MASK); + Texture2D r_reconstructed_previous_nearest_depth : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_RECONSTRUCTED_PREVIOUS_NEAREST_DEPTH); + Texture2D r_dilated_motion_vectors : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_MOTION_VECTORS); + Texture2D r_dilatedDepth : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_DEPTH); + Texture2D r_internal_upscaled_color : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_UPSCALED_COLOR); + Texture2D r_lock_status : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_STATUS); + Texture2D r_depth_clip : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_DEPTH_CLIP); + Texture2D r_prepared_input_color : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_PREPARED_INPUT_COLOR); + Texture2D r_luma_history : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_LUMA_HISTORY); + Texture2D r_rcas_input : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_RCAS_INPUT); + Texture2D r_lanczos_lut : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_LANCZOS_LUT); + Texture2D r_imgMips : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE); + Texture2D r_upsample_maximum_bias_lut : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTITIER_UPSAMPLE_MAXIMUM_BIAS_LUT); + Texture2D r_dilated_reactive_masks : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_REACTIVE_MASKS); + Texture2D r_debug_out : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_DEBUG_OUTPUT); + + // declarations not current form, no accessor functions + Texture2D r_transparency_mask : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_TRANSPARENCY_MASK); + Texture2D r_bias_current_color_mask : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_BIAS_CURRENT_COLOR_MASK); + Texture2D r_gbuffer_albedo : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_GBUFFER_ALBEDO); + Texture2D r_gbuffer_roughness : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_GBUFFER_ROUGHNESS); + Texture2D r_gbuffer_metallic : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_GBUFFER_METALLIC); + Texture2D r_gbuffer_specular : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_GBUFFER_SPECULAR); + Texture2D r_gbuffer_subsurface : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_GBUFFER_SUBSURFACE); + Texture2D r_gbuffer_normals : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_GBUFFER_NORMALS); + Texture2D r_gbuffer_shading_mode_id : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_GBUFFER_SHADING_MODE_ID); + Texture2D r_gbuffer_material_id : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_GBUFFER_MATERIAL_ID); + Texture2D r_motion_vectors_3d : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_VELOCITY_3D); + Texture2D r_is_particle_mask : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_IS_PARTICLE_MASK); + Texture2D r_animated_texture_mask : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_ANIMATED_TEXTURE_MASK); + Texture2D r_depth_high_res : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_DEPTH_HIGH_RES); + Texture2D r_position_view_space : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_POSITION_VIEW_SPACE); + Texture2D r_ray_tracing_hit_distance : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_RAY_TRACING_HIT_DISTANCE); + Texture2D r_motion_vectors_reflection : FFX_FSR2_DECLARE_SRV(FFX_FSR2_RESOURCE_IDENTIFIER_VELOCITY_REFLECTION); + + // UAV declarations + RWTexture2D rw_reconstructed_previous_nearest_depth : FFX_FSR2_DECLARE_UAV(FFX_FSR2_RESOURCE_IDENTIFIER_RECONSTRUCTED_PREVIOUS_NEAREST_DEPTH); + RWTexture2D rw_dilated_motion_vectors : FFX_FSR2_DECLARE_UAV(FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_MOTION_VECTORS); + RWTexture2D rw_dilatedDepth : FFX_FSR2_DECLARE_UAV(FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_DEPTH); + RWTexture2D rw_internal_upscaled_color : FFX_FSR2_DECLARE_UAV(FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_UPSCALED_COLOR); + RWTexture2D rw_lock_status : FFX_FSR2_DECLARE_UAV(FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_STATUS); + RWTexture2D rw_depth_clip : FFX_FSR2_DECLARE_UAV(FFX_FSR2_RESOURCE_IDENTIFIER_DEPTH_CLIP); + RWTexture2D rw_prepared_input_color : FFX_FSR2_DECLARE_UAV(FFX_FSR2_RESOURCE_IDENTIFIER_PREPARED_INPUT_COLOR); + RWTexture2D rw_luma_history : FFX_FSR2_DECLARE_UAV(FFX_FSR2_RESOURCE_IDENTIFIER_LUMA_HISTORY); + RWTexture2D rw_upscaled_output : FFX_FSR2_DECLARE_UAV(FFX_FSR2_RESOURCE_IDENTIFIER_UPSCALED_OUTPUT); + + globallycoherent RWTexture2D rw_img_mip_shading_change : FFX_FSR2_DECLARE_UAV(FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE_MIPMAP_SHADING_CHANGE); + globallycoherent RWTexture2D rw_img_mip_5 : FFX_FSR2_DECLARE_UAV(FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE_MIPMAP_5); + RWTexture2D rw_dilated_reactive_masks : FFX_FSR2_DECLARE_UAV(FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_REACTIVE_MASKS); + RWTexture2D rw_exposure : FFX_FSR2_DECLARE_UAV(FFX_FSR2_RESOURCE_IDENTIFIER_EXPOSURE); + globallycoherent RWTexture2D rw_spd_global_atomic : FFX_FSR2_DECLARE_UAV(FFX_FSR2_RESOURCE_IDENTIFIER_SPD_ATOMIC_COUNT); + RWTexture2D rw_debug_out : FFX_FSR2_DECLARE_UAV(FFX_FSR2_RESOURCE_IDENTIFIER_DEBUG_OUTPUT); + +#else // #if defined(FFX_INTERNAL) + #if defined FSR2_BIND_SRV_INPUT_COLOR + Texture2D r_input_color_jittered : FFX_FSR2_DECLARE_SRV(FSR2_BIND_SRV_INPUT_COLOR); + #endif + #if defined FSR2_BIND_SRV_MOTION_VECTORS + Texture2D r_motion_vectors : FFX_FSR2_DECLARE_SRV(FSR2_BIND_SRV_MOTION_VECTORS); + #endif + #if defined FSR2_BIND_SRV_DEPTH + Texture2D r_depth : FFX_FSR2_DECLARE_SRV(FSR2_BIND_SRV_DEPTH); + #endif + #if defined FSR2_BIND_SRV_EXPOSURE + Texture2D r_exposure : FFX_FSR2_DECLARE_SRV(FSR2_BIND_SRV_EXPOSURE); + #endif + #if defined FSR2_BIND_SRV_REACTIVE_MASK + Texture2D r_reactive_mask : FFX_FSR2_DECLARE_SRV(FSR2_BIND_SRV_REACTIVE_MASK); + #endif + #if defined FSR2_BIND_SRV_TRANSPARENCY_AND_COMPOSITION_MASK + Texture2D r_transparency_and_composition_mask : FFX_FSR2_DECLARE_SRV(FSR2_BIND_SRV_TRANSPARENCY_AND_COMPOSITION_MASK); + #endif + #if defined FSR2_BIND_SRV_RECONSTRUCTED_PREV_NEAREST_DEPTH + Texture2D r_reconstructed_previous_nearest_depth : FFX_FSR2_DECLARE_SRV(FSR2_BIND_SRV_RECONSTRUCTED_PREV_NEAREST_DEPTH); + #endif + #if defined FSR2_BIND_SRV_DILATED_MOTION_VECTORS + Texture2D r_dilated_motion_vectors : FFX_FSR2_DECLARE_SRV(FSR2_BIND_SRV_DILATED_MOTION_VECTORS); + #endif + #if defined FSR2_BIND_SRV_DILATED_DEPTH + Texture2D r_dilatedDepth : FFX_FSR2_DECLARE_SRV(FSR2_BIND_SRV_DILATED_DEPTH); + #endif + #if defined FSR2_BIND_SRV_INTERNAL_UPSCALED + Texture2D r_internal_upscaled_color : FFX_FSR2_DECLARE_SRV(FSR2_BIND_SRV_INTERNAL_UPSCALED); + #endif + #if defined FSR2_BIND_SRV_LOCK_STATUS + Texture2D r_lock_status : FFX_FSR2_DECLARE_SRV(FSR2_BIND_SRV_LOCK_STATUS); + #endif + #if defined FSR2_BIND_SRV_DEPTH_CLIP + Texture2D r_depth_clip : FFX_FSR2_DECLARE_SRV(FSR2_BIND_SRV_DEPTH_CLIP); + #endif + #if defined FSR2_BIND_SRV_PREPARED_INPUT_COLOR + Texture2D r_prepared_input_color : FFX_FSR2_DECLARE_SRV(FSR2_BIND_SRV_PREPARED_INPUT_COLOR); + #endif + #if defined FSR2_BIND_SRV_LUMA_HISTORY + Texture2D r_luma_history : FFX_FSR2_DECLARE_SRV(FSR2_BIND_SRV_LUMA_HISTORY); + #endif + #if defined FSR2_BIND_SRV_RCAS_INPUT + Texture2D r_rcas_input : FFX_FSR2_DECLARE_SRV(FSR2_BIND_SRV_RCAS_INPUT); + #endif + #if defined FSR2_BIND_SRV_LANCZOS_LUT + Texture2D r_lanczos_lut : FFX_FSR2_DECLARE_SRV(FSR2_BIND_SRV_LANCZOS_LUT); + #endif + #if defined FSR2_BIND_SRV_EXPOSURE_MIPS + Texture2D r_imgMips : FFX_FSR2_DECLARE_SRV(FSR2_BIND_SRV_EXPOSURE_MIPS); + #endif + #if defined FSR2_BIND_SRV_UPSCALE_MAXIMUM_BIAS_LUT + Texture2D r_upsample_maximum_bias_lut : FFX_FSR2_DECLARE_SRV(FSR2_BIND_SRV_UPSCALE_MAXIMUM_BIAS_LUT); + #endif + #if defined FSR2_BIND_SRV_DILATED_REACTIVE_MASKS + Texture2D r_dilated_reactive_masks : FFX_FSR2_DECLARE_SRV(FSR2_BIND_SRV_DILATED_REACTIVE_MASKS); + #endif + + // UAV declarations + #if defined FSR2_BIND_UAV_RECONSTRUCTED_PREV_NEAREST_DEPTH + RWTexture2D rw_reconstructed_previous_nearest_depth : FFX_FSR2_DECLARE_UAV(FSR2_BIND_UAV_RECONSTRUCTED_PREV_NEAREST_DEPTH); + #endif + #if defined FSR2_BIND_UAV_DILATED_MOTION_VECTORS + RWTexture2D rw_dilated_motion_vectors : FFX_FSR2_DECLARE_UAV(FSR2_BIND_UAV_DILATED_MOTION_VECTORS); + #endif + #if defined FSR2_BIND_UAV_DILATED_DEPTH + RWTexture2D rw_dilatedDepth : FFX_FSR2_DECLARE_UAV(FSR2_BIND_UAV_DILATED_DEPTH); + #endif + #if defined FSR2_BIND_UAV_INTERNAL_UPSCALED + RWTexture2D rw_internal_upscaled_color : FFX_FSR2_DECLARE_UAV(FSR2_BIND_UAV_INTERNAL_UPSCALED); + #endif + #if defined FSR2_BIND_UAV_LOCK_STATUS + RWTexture2D rw_lock_status : FFX_FSR2_DECLARE_UAV(FSR2_BIND_UAV_LOCK_STATUS); + #endif + #if defined FSR2_BIND_UAV_DEPTH_CLIP + RWTexture2D rw_depth_clip : FFX_FSR2_DECLARE_UAV(FSR2_BIND_UAV_DEPTH_CLIP); + #endif + #if defined FSR2_BIND_UAV_PREPARED_INPUT_COLOR + RWTexture2D rw_prepared_input_color : FFX_FSR2_DECLARE_UAV(FSR2_BIND_UAV_PREPARED_INPUT_COLOR); + #endif + #if defined FSR2_BIND_UAV_LUMA_HISTORY + RWTexture2D rw_luma_history : FFX_FSR2_DECLARE_UAV(FSR2_BIND_UAV_LUMA_HISTORY); + #endif + #if defined FSR2_BIND_UAV_UPSCALED_OUTPUT + RWTexture2D rw_upscaled_output : FFX_FSR2_DECLARE_UAV(FSR2_BIND_UAV_UPSCALED_OUTPUT); + #endif + #if defined FSR2_BIND_UAV_EXPOSURE_MIP_LUMA_CHANGE + globallycoherent RWTexture2D rw_img_mip_shading_change : FFX_FSR2_DECLARE_UAV(FSR2_BIND_UAV_EXPOSURE_MIP_LUMA_CHANGE); + #endif + #if defined FSR2_BIND_UAV_EXPOSURE_MIP_5 + globallycoherent RWTexture2D rw_img_mip_5 : FFX_FSR2_DECLARE_UAV(FSR2_BIND_UAV_EXPOSURE_MIP_5); + #endif + #if defined FSR2_BIND_UAV_DILATED_REACTIVE_MASKS + RWTexture2D rw_dilated_reactive_masks : FFX_FSR2_DECLARE_UAV(FSR2_BIND_UAV_DILATED_REACTIVE_MASKS); + #endif + #if defined FSR2_BIND_UAV_EXPOSURE + RWTexture2D rw_exposure : FFX_FSR2_DECLARE_UAV(FSR2_BIND_UAV_EXPOSURE); + #endif + #if defined FSR2_BIND_UAV_SPD_GLOBAL_ATOMIC + globallycoherent RWTexture2D rw_spd_global_atomic : FFX_FSR2_DECLARE_UAV(FSR2_BIND_UAV_SPD_GLOBAL_ATOMIC); + #endif +#endif // #if defined(FFX_INTERNAL) + +FfxFloat32 LoadMipLuma(FfxUInt32x2 iPxPos, FfxUInt32 mipLevel) +{ +#if defined(FSR2_BIND_SRV_EXPOSURE_MIPS) || defined(FFX_INTERNAL) + return r_imgMips.mips[mipLevel][iPxPos]; +#else + return 0.f; +#endif +} + +FfxFloat32 SampleMipLuma(FfxFloat32x2 fUV, FfxUInt32 mipLevel) +{ +#if defined(FSR2_BIND_SRV_EXPOSURE_MIPS) || defined(FFX_INTERNAL) + fUV *= depthclip_uv_scale; + return r_imgMips.SampleLevel(s_LinearClamp, fUV, mipLevel); +#else + return 0.f; +#endif + +} + +// +// a 0 0 0 x +// 0 b 0 0 y +// 0 0 c d z +// 0 0 e 0 1 +// +// z' = (z*c+d)/(z*e) +// z' = (c/e) + d/(z*e) +// z' - (c/e) = d/(z*e) +// (z'e - c)/e = d/(z*e) +// e / (z'e - c) = (z*e)/d +// (e * d) / (z'e - c) = z*e +// z = d / (z'e - c) +FfxFloat32 ConvertFromDeviceDepthToViewSpace(FfxFloat32 fDeviceDepth) +{ + return -fDeviceToViewDepth[2] / (fDeviceDepth * fDeviceToViewDepth[1] - fDeviceToViewDepth[0]); +} + +FfxFloat32 LoadInputDepth(FfxUInt32x2 iPxPos) +{ +#if defined(FSR2_BIND_SRV_DEPTH) || defined(FFX_INTERNAL) + return r_depth[iPxPos]; +#else + return 0.f; +#endif +} + +FfxFloat32 LoadReactiveMask(FfxUInt32x2 iPxPos) +{ +#if defined(FSR2_BIND_SRV_REACTIVE_MASK) || defined(FFX_INTERNAL) + return r_reactive_mask[iPxPos]; +#else + return 0.f; +#endif +} + +FfxFloat32x4 GatherReactiveMask(FfxUInt32x2 iPxPos) +{ +#if defined(FSR2_BIND_SRV_REACTIVE_MASK) || defined(FFX_INTERNAL) + return r_reactive_mask.GatherRed(s_LinearClamp, FfxFloat32x2(iPxPos) * reactive_mask_dim_rcp); +#else + return 0.f; +#endif +} + +FfxFloat32 LoadTransparencyAndCompositionMask(FfxUInt32x2 iPxPos) +{ +#if defined(FSR2_BIND_SRV_TRANSPARENCY_AND_COMPOSITION_MASK) || defined(FFX_INTERNAL) + return r_transparency_and_composition_mask[iPxPos]; +#else + return 0.f; +#endif +} + +FfxFloat32 SampleTransparencyAndCompositionMask(FfxFloat32x2 fUV) +{ +#if defined(FSR2_BIND_SRV_TRANSPARENCY_AND_COMPOSITION_MASK) || defined(FFX_INTERNAL) + fUV *= depthclip_uv_scale; + return r_transparency_and_composition_mask.SampleLevel(s_LinearClamp, fUV, 0); +#else + return 0.f; +#endif +} + +FfxFloat32 PreExposure() +{ + //return fPreExposure; + return 1.0f; +} + +FfxFloat32x3 LoadInputColor(FfxUInt32x2 iPxPos) +{ +#if defined(FSR2_BIND_SRV_INPUT_COLOR) || defined(FFX_INTERNAL) + return r_input_color_jittered[iPxPos].rgb / PreExposure(); +#else + return 0; +#endif +} + +FfxFloat32x3 LoadInputColorWithoutPreExposure(FfxUInt32x2 iPxPos) +{ +#if defined(FSR2_BIND_SRV_INPUT_COLOR) || defined(FFX_INTERNAL) + return r_input_color_jittered[iPxPos].rgb; +#else + return 0; +#endif +} + +FfxFloat32x3 LoadPreparedInputColor(FfxUInt32x2 iPxPos) +{ +#if defined(FSR2_BIND_SRV_PREPARED_INPUT_COLOR) || defined(FFX_INTERNAL) + return r_prepared_input_color[iPxPos].rgb; +#else + return 0.f; +#endif +} + +FfxFloat32 LoadPreparedInputColorLuma(FfxUInt32x2 iPxPos) +{ +#if defined(FSR2_BIND_SRV_PREPARED_INPUT_COLOR) || defined(FFX_INTERNAL) + return r_prepared_input_color[iPxPos].a; +#else + return 0.f; +#endif +} + +FfxFloat32x2 LoadInputMotionVector(FfxUInt32x2 iPxDilatedMotionVectorPos) +{ +#if defined(FSR2_BIND_SRV_MOTION_VECTORS) || defined(FFX_INTERNAL) + FfxFloat32x2 fSrcMotionVector = r_motion_vectors[iPxDilatedMotionVectorPos].xy; +#else + FfxFloat32x2 fSrcMotionVector = 0.f; +#endif + + FfxFloat32x2 fUvMotionVector = fSrcMotionVector * MotionVectorScale; + +#if FFX_FSR2_OPTION_JITTERED_MOTION_VECTORS + fUvMotionVector -= fMotionVectorJitterCancellation; +#endif + + return fUvMotionVector; +} + +FfxFloat32x4 LoadHistory(FfxUInt32x2 iPxHistory) +{ +#if defined(FSR2_BIND_SRV_INTERNAL_UPSCALED) || defined(FFX_INTERNAL) + return r_internal_upscaled_color[iPxHistory]; +#else + return 0.f; +#endif +} + +FfxFloat32x4 LoadRwInternalUpscaledColorAndWeight(FfxUInt32x2 iPxPos) +{ +#if defined(FSR2_BIND_UAV_INTERNAL_UPSCALED) || defined(FFX_INTERNAL) + return rw_internal_upscaled_color[iPxPos]; +#else + return 0.f; +#endif +} + +void StoreLumaHistory(FfxUInt32x2 iPxPos, FfxFloat32x4 fLumaHistory) +{ +#if defined(FSR2_BIND_UAV_LUMA_HISTORY) || defined(FFX_INTERNAL) + rw_luma_history[iPxPos] = fLumaHistory; +#endif +} + +FfxFloat32x4 LoadRwLumaHistory(FfxUInt32x2 iPxPos) +{ +#if defined(FSR2_BIND_UAV_LUMA_HISTORY) || defined(FFX_INTERNAL) + return rw_luma_history[iPxPos]; +#else + return 1.f; +#endif +} + +FfxFloat32 LoadLumaStabilityFactor(FfxUInt32x2 iPxPos) +{ +#if defined(FSR2_BIND_SRV_LUMA_HISTORY) || defined(FFX_INTERNAL) + return r_luma_history[iPxPos].w; +#else + return 0.f; +#endif +} + +FfxFloat32 SampleLumaStabilityFactor(FfxFloat32x2 fUV) +{ +#if defined(FSR2_BIND_SRV_LUMA_HISTORY) || defined(FFX_INTERNAL) + fUV *= depthclip_uv_scale; + return r_luma_history.SampleLevel(s_LinearClamp, fUV, 0).w; +#else + return 0.f; +#endif +} + +void StoreReprojectedHistory(FfxUInt32x2 iPxHistory, FfxFloat32x4 fHistory) +{ +#if defined(FSR2_BIND_UAV_INTERNAL_UPSCALED) || defined(FFX_INTERNAL) + rw_internal_upscaled_color[iPxHistory] = fHistory; +#endif +} + +void StoreInternalColorAndWeight(FfxUInt32x2 iPxPos, FfxFloat32x4 fColorAndWeight) +{ +#if defined(FSR2_BIND_UAV_INTERNAL_UPSCALED) || defined(FFX_INTERNAL) + rw_internal_upscaled_color[iPxPos] = fColorAndWeight; +#endif +} + +void StoreUpscaledOutput(FfxUInt32x2 iPxPos, FfxFloat32x3 fColor) +{ +#if defined(FSR2_BIND_UAV_UPSCALED_OUTPUT) || defined(FFX_INTERNAL) + rw_upscaled_output[iPxPos] = FfxFloat32x4(fColor * PreExposure(), 1.f); +#endif +} + +//LOCK_LIFETIME_REMAINING == 0 +//Should make LockInitialLifetime() return a const 1.0f later +FfxFloat32x3 LoadLockStatus(FfxUInt32x2 iPxPos) +{ +#if defined(FSR2_BIND_SRV_LOCK_STATUS) || defined(FFX_INTERNAL) + FfxFloat32x3 fLockStatus = r_lock_status[iPxPos]; + + fLockStatus[0] -= LockInitialLifetime() * 2.0f; + return fLockStatus; +#else + return 0.f; +#endif + + +} + +FfxFloat32x3 LoadRwLockStatus(FfxUInt32x2 iPxPos) +{ +#if defined(FSR2_BIND_UAV_LOCK_STATUS) || defined(FFX_INTERNAL) + FfxFloat32x3 fLockStatus = rw_lock_status[iPxPos]; + + fLockStatus[0] -= LockInitialLifetime() * 2.0f; + + return fLockStatus; +#else + return 0.f; +#endif +} + +void StoreLockStatus(FfxUInt32x2 iPxPos, FfxFloat32x3 fLockstatus) +{ +#if defined(FSR2_BIND_UAV_LOCK_STATUS) || defined(FFX_INTERNAL) + fLockstatus[0] += LockInitialLifetime() * 2.0f; + + rw_lock_status[iPxPos] = fLockstatus; +#endif +} + +void StorePreparedInputColor(FFX_PARAMETER_IN FfxUInt32x2 iPxPos, FFX_PARAMETER_IN FfxFloat32x4 fTonemapped) +{ +#if defined(FSR2_BIND_UAV_PREPARED_INPUT_COLOR) || defined(FFX_INTERNAL) + rw_prepared_input_color[iPxPos] = fTonemapped; +#endif +} + +FfxBoolean IsResponsivePixel(FfxUInt32x2 iPxPos) +{ + return FFX_FALSE; //not supported in prototype +} + +FfxFloat32 LoadDepthClip(FfxUInt32x2 iPxPos) +{ +#if defined(FSR2_BIND_SRV_DEPTH_CLIP) || defined(FFX_INTERNAL) + return r_depth_clip[iPxPos]; +#else + return 0.f; +#endif +} + +FfxFloat32 SampleDepthClip(FfxFloat32x2 fUV) +{ +#if defined(FSR2_BIND_SRV_DEPTH_CLIP) || defined(FFX_INTERNAL) + fUV *= depthclip_uv_scale; + return r_depth_clip.SampleLevel(s_LinearClamp, fUV, 0); +#else + return 0.f; +#endif +} + +FfxFloat32x3 SampleLockStatus(FfxFloat32x2 fUV) +{ +#if defined(FSR2_BIND_SRV_LOCK_STATUS) || defined(FFX_INTERNAL) + fUV *= postprocessed_lockstatus_uv_scale; + FfxFloat32x3 fLockStatus = r_lock_status.SampleLevel(s_LinearClamp, fUV, 0); + fLockStatus[0] -= LockInitialLifetime() * 2.0f; + return fLockStatus; +#else + return 0.f; +#endif +} + +void StoreDepthClip(FfxUInt32x2 iPxPos, FfxFloat32 fClip) +{ +#if defined(FSR2_BIND_UAV_DEPTH_CLIP) || defined(FFX_INTERNAL) + rw_depth_clip[iPxPos] = fClip; +#endif +} + +FfxFloat32 TanHalfFoV() +{ + return fTanHalfFOV; +} + +FfxFloat32 LoadSceneDepth(FfxUInt32x2 iPxInput) +{ +#if defined(FSR2_BIND_SRV_DEPTH) || defined(FFX_INTERNAL) + return r_depth[iPxInput]; +#else + return 0.f; +#endif +} + +FfxFloat32 LoadReconstructedPrevDepth(FfxUInt32x2 iPxPos) +{ +#if defined(FSR2_BIND_SRV_RECONSTRUCTED_PREV_NEAREST_DEPTH) || defined(FFX_INTERNAL) + return asfloat(r_reconstructed_previous_nearest_depth[iPxPos]); +#else + return 0; +#endif +} + +void StoreReconstructedDepth(FfxUInt32x2 iPxSample, FfxFloat32 fDepth) +{ + FfxUInt32 uDepth = asuint(fDepth); +#if defined(FSR2_BIND_UAV_RECONSTRUCTED_PREV_NEAREST_DEPTH) || defined(FFX_INTERNAL) + #if FFX_FSR2_OPTION_INVERTED_DEPTH + InterlockedMax(rw_reconstructed_previous_nearest_depth[iPxSample], uDepth); + #else + InterlockedMin(rw_reconstructed_previous_nearest_depth[iPxSample], uDepth); // min for standard, max for inverted depth + #endif +#endif +} + +void SetReconstructedDepth(FfxUInt32x2 iPxSample, const FfxUInt32 uValue) +{ +#if defined(FSR2_BIND_UAV_RECONSTRUCTED_PREV_NEAREST_DEPTH) || defined(FFX_INTERNAL) + rw_reconstructed_previous_nearest_depth[iPxSample] = uValue; +#endif +} + +void StoreDilatedDepth(FFX_PARAMETER_IN FfxUInt32x2 iPxPos, FFX_PARAMETER_IN FfxFloat32 fDepth) +{ +#if defined(FSR2_BIND_UAV_DILATED_DEPTH) || defined(FFX_INTERNAL) + rw_dilatedDepth[iPxPos] = fDepth; +#endif +} + +void StoreDilatedMotionVector(FFX_PARAMETER_IN FfxUInt32x2 iPxPos, FFX_PARAMETER_IN FfxFloat32x2 fMotionVector) +{ +#if defined(FSR2_BIND_UAV_DILATED_MOTION_VECTORS) || defined(FFX_INTERNAL) + rw_dilated_motion_vectors[iPxPos] = fMotionVector; +#endif +} + +FfxFloat32x2 LoadDilatedMotionVector(FfxUInt32x2 iPxInput) +{ +#if defined(FSR2_BIND_SRV_DILATED_MOTION_VECTORS) || defined(FFX_INTERNAL) + return r_dilated_motion_vectors[iPxInput].xy; +#else + return 0.f; +#endif +} + +FfxFloat32x2 SampleDilatedMotionVector(FfxFloat32x2 fUV) +{ +#if defined(FSR2_BIND_SRV_DILATED_MOTION_VECTORS) || defined(FFX_INTERNAL) + fUV *= depthclip_uv_scale; // TODO: assuming these are (RenderSize() / MaxRenderSize()) + return r_dilated_motion_vectors.SampleLevel(s_LinearClamp, fUV, 0); +#else + return 0.f; +#endif +} + +FfxFloat32 LoadDilatedDepth(FfxUInt32x2 iPxInput) +{ +#if defined(FSR2_BIND_SRV_DILATED_DEPTH) || defined(FFX_INTERNAL) + return r_dilatedDepth[iPxInput]; +#else + return 0.f; +#endif +} + +FfxFloat32 Exposure() +{ + // return 1.0f; + #if defined(FSR2_BIND_SRV_EXPOSURE) || defined(FFX_INTERNAL) + FfxFloat32 exposure = r_exposure[FfxUInt32x2(0, 0)].x; + #else + FfxFloat32 exposure = 1.f; + #endif + + if (exposure == 0.0f) { + exposure = 1.0f; + } + + return exposure; +} + +FfxFloat32 SampleLanczos2Weight(FfxFloat32 x) +{ +#if defined(FSR2_BIND_SRV_LANCZOS_LUT) || defined(FFX_INTERNAL) + return r_lanczos_lut.SampleLevel(s_LinearClamp, FfxFloat32x2(x / 2, 0.5f), 0); +#else + return 0.f; +#endif +} + +FfxFloat32 SampleUpsampleMaximumBias(FfxFloat32x2 uv) +{ +#if defined(FSR2_BIND_SRV_UPSCALE_MAXIMUM_BIAS_LUT) || defined(FFX_INTERNAL) + // Stored as a SNORM, so make sure to multiply by 2 to retrieve the actual expected range. + return FfxFloat32(2.0) * r_upsample_maximum_bias_lut.SampleLevel(s_LinearClamp, abs(uv) * 2.0, 0); +#else + return 0.f; +#endif +} + +FfxFloat32x2 SampleDilatedReactiveMasks(FfxFloat32x2 fUV) +{ +#if defined(FSR2_BIND_SRV_DILATED_REACTIVE_MASKS) || defined(FFX_INTERNAL) + fUV *= depthclip_uv_scale; + return r_dilated_reactive_masks.SampleLevel(s_LinearClamp, fUV, 0); +#else + return 0.f; +#endif +} + +FfxFloat32x2 LoadDilatedReactiveMasks(FFX_PARAMETER_IN FfxUInt32x2 iPxPos) +{ +#if defined(FSR2_BIND_SRV_DILATED_REACTIVE_MASKS) || defined(FFX_INTERNAL) + return r_dilated_reactive_masks[iPxPos]; +#else + return 0.f; +#endif +} + +void StoreDilatedReactiveMasks(FFX_PARAMETER_IN FfxUInt32x2 iPxPos, FFX_PARAMETER_IN FfxFloat32x2 fDilatedReactiveMasks) +{ +#if defined(FSR2_BIND_UAV_DILATED_REACTIVE_MASKS) || defined(FFX_INTERNAL) + rw_dilated_reactive_masks[iPxPos] = fDilatedReactiveMasks; +#endif +} + +#endif // #if defined(FFX_GPU) diff --git a/Assets/Resources/Shaders/ffx_fsr2_callbacks_hlsl.h.meta b/Assets/Resources/Shaders/ffx_fsr2_callbacks_hlsl.h.meta new file mode 100644 index 0000000..0e963b5 --- /dev/null +++ b/Assets/Resources/Shaders/ffx_fsr2_callbacks_hlsl.h.meta @@ -0,0 +1,27 @@ +fileFormatVersion: 2 +guid: 5c9c193ec7dc3064097ab1f74232c98d +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + defineConstraints: [] + isPreloaded: 0 + isOverridable: 0 + isExplicitlyReferenced: 0 + validateReferences: 1 + platformData: + - first: + Any: + second: + enabled: 1 + settings: {} + - first: + Editor: Editor + second: + enabled: 0 + settings: + DefaultValueInitialized: true + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Resources/Shaders/ffx_fsr2_rcas_pass.compute b/Assets/Resources/Shaders/ffx_fsr2_rcas_pass.compute index 38c5267..f925fd2 100644 --- a/Assets/Resources/Shaders/ffx_fsr2_rcas_pass.compute +++ b/Assets/Resources/Shaders/ffx_fsr2_rcas_pass.compute @@ -1,84 +1,10 @@ // Each #kernel tells which function to compile; you can have many kernels #pragma kernel CS -// Create a RenderTexture with enableRandomWrite flag and set it with cs.SetTexture -// NOTE: the .hlsl files conveniently contain a list of inputs and outputs in their header comments -//Texture2D r_exposure; // Expose is not used at the moment, so no need to bind it -Texture2D r_rcas_input; -RWTexture2D rw_upscaled_output; - // Some global defines are needed #define FFX_GPU // Compiling for GPU #define FFX_HLSL // Compile for plain HLSL //#define FFX_FSR2_OPTION_HDR_COLOR_INPUT // This ought to be supported in the long run -#define FSR_RCAS_F // Using 32-bit float mode (not half) -#define FSR2_BIND_UAV_UPSCALED_OUTPUT // Create an SRV binding for the output we need - -#include "ffx_core.h" // With the above defines this all compiles cleanly, yay! - -// This is from ffx_fsr2_callbacks_hlsl.h (bunch of macro use to declare SRVs, may need conversion) -FfxFloat32 Exposure() -{ - // return 1.0f; - #if defined(FSR2_BIND_SRV_EXPOSURE) || defined(FFX_INTERNAL) - FfxFloat32 exposure = r_exposure[FfxUInt32x2(0, 0)].x; - #else - FfxFloat32 exposure = 1.f; - #endif - - if (exposure == 0.0f) { - exposure = 1.0f; - } - - return exposure; -} - -FfxFloat32 PreExposure() -{ - //return fPreExposure; // TODO: requires constant buffer bindings, uhhh... (see FSR2_BIND_CB_FSR2) - return 1.0f; -} - -void StoreUpscaledOutput(FfxUInt32x2 iPxPos, FfxFloat32x3 fColor) -{ - #if defined(FSR2_BIND_UAV_UPSCALED_OUTPUT) || defined(FFX_INTERNAL) - rw_upscaled_output[iPxPos] = FfxFloat32x4(fColor * PreExposure(), 1.f); - #endif -} - -//#include "ffx_fsr2_common.h" // TODO: still depends on a bunch of CB's from callbacks_hlsl - -// Below is all converted from ffx_fsr2_rcas_pass.hlsl -uint4 RCASConfig() -{ - // These are not your typical config parameters... these are packed floats-as-ints, i.e. complete nonsense values - return uint4(1061290752, 974141968, 0, 0); // TODO: this needs to be a constant buffer value -} - -float4 LoadRCAS_Input(FfxInt32x2 iPxPos) -{ - return r_rcas_input[iPxPos]; -} - -#include "ffx_fsr2_rcas.h" - -#ifndef FFX_FSR2_THREAD_GROUP_WIDTH -#define FFX_FSR2_THREAD_GROUP_WIDTH 64 -#endif // #ifndef FFX_FSR2_THREAD_GROUP_WIDTH -#ifndef FFX_FSR2_THREAD_GROUP_HEIGHT -#define FFX_FSR2_THREAD_GROUP_HEIGHT 1 -#endif // #ifndef FFX_FSR2_THREAD_GROUP_HEIGHT -#ifndef FFX_FSR2_THREAD_GROUP_DEPTH -#define FFX_FSR2_THREAD_GROUP_DEPTH 1 -#endif // #ifndef FFX_FSR2_THREAD_GROUP_DEPTH -#ifndef FFX_FSR2_NUM_THREADS -#define FFX_FSR2_NUM_THREADS [numthreads(FFX_FSR2_THREAD_GROUP_WIDTH, FFX_FSR2_THREAD_GROUP_HEIGHT, FFX_FSR2_THREAD_GROUP_DEPTH)] -#endif // #ifndef FFX_FSR2_NUM_THREADS - -FFX_FSR2_NUM_THREADS -void CS(uint3 LocalThreadId : SV_GroupThreadID, uint3 WorkGroupId : SV_GroupID, uint3 Dtid : SV_DispatchThreadID) -{ - RCAS(LocalThreadId, WorkGroupId, Dtid); -} +#include "ffx_fsr2_rcas_pass.hlsl" diff --git a/Assets/Resources/Shaders/ffx_fsr2_rcas_pass.hlsl b/Assets/Resources/Shaders/ffx_fsr2_rcas_pass.hlsl new file mode 100644 index 0000000..7138281 --- /dev/null +++ b/Assets/Resources/Shaders/ffx_fsr2_rcas_pass.hlsl @@ -0,0 +1,89 @@ +// This file is part of the FidelityFX SDK. +// +// Copyright (c) 2022 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. + +// FSR2 pass 6 +// SRV 4 : m_Exposure : r_exposure +// SRV 19 : FSR2_InternalUpscaled1 : r_rcas_input +// UAV 18 : DisplayOutput : rw_upscaled_output +// CB 0 : cbFSR2 +// CB 1 : cbRCAS + +#define FSR2_BIND_SRV_EXPOSURE 0 +#define FSR2_BIND_SRV_RCAS_INPUT 1 +#define FSR2_BIND_UAV_UPSCALED_OUTPUT 0 +#define FSR2_BIND_CB_FSR2 0 +#define FSR2_BIND_CB_RCAS 1 + +#include "ffx_fsr2_callbacks_hlsl.h" +#include "ffx_fsr2_common.h" + +//Move to prototype shader! +#if defined(FSR2_BIND_CB_RCAS) + cbuffer cbRCAS : FFX_FSR2_DECLARE_CB(FSR2_BIND_CB_RCAS) + { + uint4 rcasConfig; + }; + + uint4 RCASConfig() + { + return rcasConfig; + } +#else + uint4 RCASConfig() + { + // These are not your typical config parameters... these are packed floats-as-ints, i.e. complete nonsense values + return uint4(1061290752, 974141968, 0, 0); // TODO: this needs to be a constant buffer value + } +#endif + +#if FFX_HALF +float4 LoadRCAS_Input(FfxInt16x2 iPxPos) +{ + return r_rcas_input[iPxPos]; +} +#else +float4 LoadRCAS_Input(FfxInt32x2 iPxPos) +{ + return r_rcas_input[iPxPos]; +} +#endif + +#include "ffx_fsr2_rcas.h" + +#ifndef FFX_FSR2_THREAD_GROUP_WIDTH +#define FFX_FSR2_THREAD_GROUP_WIDTH 64 +#endif // #ifndef FFX_FSR2_THREAD_GROUP_WIDTH +#ifndef FFX_FSR2_THREAD_GROUP_HEIGHT +#define FFX_FSR2_THREAD_GROUP_HEIGHT 1 +#endif // #ifndef FFX_FSR2_THREAD_GROUP_HEIGHT +#ifndef FFX_FSR2_THREAD_GROUP_DEPTH +#define FFX_FSR2_THREAD_GROUP_DEPTH 1 +#endif // #ifndef FFX_FSR2_THREAD_GROUP_DEPTH +#ifndef FFX_FSR2_NUM_THREADS +#define FFX_FSR2_NUM_THREADS [numthreads(FFX_FSR2_THREAD_GROUP_WIDTH, FFX_FSR2_THREAD_GROUP_HEIGHT, FFX_FSR2_THREAD_GROUP_DEPTH)] +#endif // #ifndef FFX_FSR2_NUM_THREADS + +FFX_FSR2_NUM_THREADS +FFX_FSR2_EMBED_CB2_ROOTSIG_CONTENT +void CS(uint3 LocalThreadId : SV_GroupThreadID, uint3 WorkGroupId : SV_GroupID, uint3 Dtid : SV_DispatchThreadID) +{ + RCAS(LocalThreadId, WorkGroupId, Dtid); +} diff --git a/Assets/Resources/Shaders/ffx_fsr2_rcas_pass.hlsl.meta b/Assets/Resources/Shaders/ffx_fsr2_rcas_pass.hlsl.meta new file mode 100644 index 0000000..f67bad0 --- /dev/null +++ b/Assets/Resources/Shaders/ffx_fsr2_rcas_pass.hlsl.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 77fd29b4fc373da40881622c212f2505 +ShaderIncludeImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Resources/Shaders/ffx_fsr2_resources.h b/Assets/Resources/Shaders/ffx_fsr2_resources.h new file mode 100644 index 0000000..89734f6 --- /dev/null +++ b/Assets/Resources/Shaders/ffx_fsr2_resources.h @@ -0,0 +1,88 @@ +// This file is part of the FidelityFX SDK. +// +// Copyright (c) 2022 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. + +#ifndef FFX_FSR2_RESOURCES_H +#define FFX_FSR2_RESOURCES_H + +#if defined(FFX_CPU) || defined(FFX_GPU) +#define FFX_FSR2_RESOURCE_IDENTIFIER_NULL 0 +#define FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_COLOR 1 +#define FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_MOTION_VECTORS 2 +#define FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_DEPTH 3 +#define FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_EXPOSURE 4 +#define FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_REACTIVE_MASK 5 +#define FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_TRANSPARENCY_AND_COMPOSITION_MASK 6 +#define FFX_FSR2_RESOURCE_IDENTIFIER_RECONSTRUCTED_PREVIOUS_NEAREST_DEPTH 7 +#define FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_MOTION_VECTORS 8 +#define FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_DEPTH 9 +#define FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_UPSCALED_COLOR 10 +#define FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_STATUS 11 +#define FFX_FSR2_RESOURCE_IDENTIFIER_DEPTH_CLIP 12 +#define FFX_FSR2_RESOURCE_IDENTIFIER_PREPARED_INPUT_COLOR 13 +#define FFX_FSR2_RESOURCE_IDENTIFIER_LUMA_HISTORY 14 +#define FFX_FSR2_RESOURCE_IDENTIFIER_DEBUG_OUTPUT 15 +#define FFX_FSR2_RESOURCE_IDENTIFIER_LANCZOS_LUT 16 +#define FFX_FSR2_RESOURCE_IDENTIFIER_SPD_ATOMIC_COUNT 17 +#define FFX_FSR2_RESOURCE_IDENTIFIER_UPSCALED_OUTPUT 18 +#define FFX_FSR2_RESOURCE_IDENTIFIER_RCAS_INPUT 19 +#define FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_STATUS_1 20 +#define FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_STATUS_2 21 +#define FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_UPSCALED_COLOR_1 22 +#define FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_UPSCALED_COLOR_2 23 +#define FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_DEFAULT_REACTIVITY 24 +#define FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_DEFAULT_TRANSPARENCY_AND_COMPOSITION 25 +#define FFX_FSR2_RESOURCE_IDENTITIER_UPSAMPLE_MAXIMUM_BIAS_LUT 26 +#define FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_REACTIVE_MASKS 27 +#define FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE 28 // same as FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE_MIPMAP_0 +#define FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE_MIPMAP_0 28 +#define FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE_MIPMAP_1 29 +#define FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE_MIPMAP_2 30 +#define FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE_MIPMAP_3 31 +#define FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE_MIPMAP_4 32 +#define FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE_MIPMAP_5 33 +#define FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE_MIPMAP_6 34 +#define FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE_MIPMAP_7 35 +#define FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE_MIPMAP_8 36 +#define FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE_MIPMAP_9 37 +#define FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE_MIPMAP_10 38 +#define FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE_MIPMAP_11 39 +#define FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE_MIPMAP_12 40 +#define FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_DEFAULT_EXPOSURE 41 +#define FFX_FSR2_RESOURCE_IDENTIFIER_EXPOSURE 42 + +// Shading change detection mip level setting, value must be in the range [FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE_MIPMAP_0, FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE_MIPMAP_12] +#define FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE_MIPMAP_SHADING_CHANGE FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE_MIPMAP_4 +#define FFX_FSR2_SHADING_CHANGE_MIP_LEVEL (FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE_MIPMAP_SHADING_CHANGE - FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE) + +#define FFX_FSR2_RESOURCE_IDENTIFIER_COUNT 43 + +#define FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_FSR2 0 +#define FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_SPD 1 +#define FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_RCAS 2 + +#define FFX_FSR2_AUTOREACTIVEFLAGS_APPLY_TONEMAP 1 +#define FFX_FSR2_AUTOREACTIVEFLAGS_APPLY_INVERSETONEMAP 2 +#define FFX_FSR2_AUTOREACTIVEFLAGS_APPLY_THRESHOLD 4 +#define FFX_FSR2_AUTOREACTIVEFLAGS_USE_COMPONENTS_MAX 8 + +#endif // #if defined(FFX_CPU) || defined(FFX_GPU) + +#endif //!defined( FFX_FSR2_RESOURCES_H ) diff --git a/Assets/Resources/Shaders/ffx_fsr2_resources.h.meta b/Assets/Resources/Shaders/ffx_fsr2_resources.h.meta new file mode 100644 index 0000000..27709d0 --- /dev/null +++ b/Assets/Resources/Shaders/ffx_fsr2_resources.h.meta @@ -0,0 +1,27 @@ +fileFormatVersion: 2 +guid: f54305f51c7d4314e8f18b6470ca188e +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + defineConstraints: [] + isPreloaded: 0 + isOverridable: 0 + isExplicitlyReferenced: 0 + validateReferences: 1 + platformData: + - first: + Any: + second: + enabled: 1 + settings: {} + - first: + Editor: Editor + second: + enabled: 0 + settings: + DefaultValueInitialized: true + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 8f32969..8a6dab0 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -315,7 +315,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 955cb66a9ecc20441a7e32934c9b4690, type: 3} m_Name: m_EditorClassIdentifier: - renderScale: 1 + renderScale: 2 enableJitter: 0 --- !u!114 &963194230 MonoBehaviour: diff --git a/Assets/Scripts/FSR2Thing.cs b/Assets/Scripts/FSR2Thing.cs index 86b77b3..eadd723 100644 --- a/Assets/Scripts/FSR2Thing.cs +++ b/Assets/Scripts/FSR2Thing.cs @@ -2,6 +2,7 @@ using System; using System.Collections; using System.Collections.Generic; using System.Linq; +using System.Runtime.InteropServices; using UnityEditor; using UnityEngine; using UnityEngine.Rendering; @@ -10,6 +11,9 @@ public class FSR2Thing : MonoBehaviour { [SerializeField] private bool performSharpenPass = true; + + [SerializeField, Range(0, 1)] + private float sharpness = 0.8f; [HideInInspector] public Camera gameCamera; @@ -22,6 +26,7 @@ public class FSR2Thing : MonoBehaviour private RenderTexture _upscaleRT; private RenderTexture _rcasOutput; + private Texture2D _exposure; private Material _testMaterial; private Material TestMaterial @@ -54,6 +59,9 @@ public class FSR2Thing : MonoBehaviour } } + private ComputeBuffer _rcasConstantBuffer; + private RcasConfig[] _rcasConfig = new RcasConfig[1]; + private void OnEnable() { RenderPipelineManager.endContextRendering += OnEndContextRendering; @@ -64,12 +72,40 @@ public class FSR2Thing : MonoBehaviour _rcasOutput = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGBHalf); _rcasOutput.enableRandomWrite = true; _rcasOutput.Create(); + + _exposure = new Texture2D(1, 1); + _exposure.name = "FSR2 Exposure"; + _exposure.SetPixel(0, 0, Color.white); + _exposure.Apply(); + + _rcasConstantBuffer = new ComputeBuffer(1, Marshal.SizeOf(), ComputeBufferType.Constant); } private void OnDisable() { - _rcasOutput.Release(); - _upscaleRT.Release(); + if (_rcasConstantBuffer != null) + { + _rcasConstantBuffer.Release(); + _rcasConstantBuffer = null; + } + + if (_exposure != null) + { + Destroy(_exposure); + _exposure = null; + } + + if (_rcasOutput != null) + { + _rcasOutput.Release(); + _rcasOutput = null; + } + + if (_upscaleRT != null) + { + _upscaleRT.Release(); + _upscaleRT = null; + } RenderPipelineManager.endContextRendering -= OnEndContextRendering; } @@ -88,10 +124,17 @@ public class FSR2Thing : MonoBehaviour if (performSharpenPass) { + int sharpnessIndex = Mathf.RoundToInt(Mathf.Clamp01(sharpness) * (RcasConfigs.Count - 1)); + _rcasConfig[0] = RcasConfigs[sharpnessIndex]; + _rcasConstantBuffer.SetData(_rcasConfig); + // Run the RCAS sharpening filter on the upscaled image int rcasKernel = RCASComputeShader.FindKernel("CS"); + // TODO: create constant buffer (cbFSR2) with fPreExposure set to 1.0 + RCASComputeShader.SetTexture(rcasKernel, "r_exposure", _exposure); RCASComputeShader.SetTexture(rcasKernel, "r_rcas_input", _upscaleRT); RCASComputeShader.SetTexture(rcasKernel, "rw_upscaled_output", _rcasOutput); + RCASComputeShader.SetConstantBuffer("cbRCAS", _rcasConstantBuffer, 0, Marshal.SizeOf()); RCASComputeShader.Dispatch(rcasKernel, Screen.width, Screen.height, 1); // TODO: not sure how these thread groups work... // Output sharpened image to screen @@ -102,4 +145,45 @@ public class FSR2Thing : MonoBehaviour Graphics.Blit(_upscaleRT, dest); } } + + [Serializable] + private struct RcasConfig + { + public RcasConfig(uint sharpness, uint halfSharp) + { + this.sharpness = sharpness; + this.halfSharp = halfSharp; + dummy0 = dummy1 = 0; + } + + public uint sharpness; + public uint halfSharp; + public uint dummy0; + public uint dummy1; + } + + private static readonly List RcasConfigs = new() + { + new(1048576000u, 872428544u), + new(1049178080u, 877212745u), + new(1049823372u, 882390168u), + new(1050514979u, 887895276u), + new(1051256227u, 893859143u), + new(1052050675u, 900216232u), + new(1052902144u, 907032080u), + new(1053814727u, 914306687u), + new(1054792807u, 922105590u), + new(1055841087u, 930494326u), + new(1056964608u, 939538432u), + new(1057566688u, 944322633u), + new(1058211980u, 949500056u), + new(1058903587u, 955005164u), + new(1059644835u, 960969031u), + new(1060439283u, 967326120u), + new(1061290752u, 974141968u), + new(1062203335u, 981416575u), + new(1063181415u, 989215478u), + new(1064229695u, 997604214u), + new(1065353216u, 1006648320), + }; }