Skip to content

Commit 29bcb36

Browse files
Integrate FSR spatial upscaler
1 parent 00f1740 commit 29bcb36

16 files changed

+12052
-1
lines changed

Graphics/SuperResolution/CMakeLists.txt

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ project(Diligent-SuperResolution CXX)
66

77
set(DILIGENT_DLSS_SUPPORTED FALSE CACHE INTERNAL "DLSS is not supported")
88
set(DILIGENT_DSR_SUPPORTED FALSE CACHE INTERNAL "DirectSR is not supported")
9+
set(DILIGENT_FSR_SUPPORTED FALSE CACHE INTERNAL "FSR is not supported")
10+
11+
if(NOT FILE2STRING_PATH STREQUAL "")
12+
set(DILIGENT_FSR_SUPPORTED TRUE CACHE INTERNAL "FSR is supported")
13+
endif()
14+
15+
if(${DILIGENT_NO_FSR})
16+
set(DILIGENT_FSR_SUPPORTED FALSE CACHE INTERNAL "FSR is forcibly disabled")
17+
endif()
918

1019
if(PLATFORM_WIN32 AND NOT MINGW_BUILD AND CMAKE_SIZEOF_VOID_P EQUAL 8)
1120
if (D3D11_SUPPORTED OR D3D12_SUPPORTED OR VULKAN_SUPPORTED)
@@ -61,6 +70,67 @@ set(SOURCE
6170
src/SuperResolutionFactory.cpp
6271
)
6372

73+
set(FSR_SHADERS
74+
shaders/FSR_FullQuad.fx
75+
shaders/FSRStructures.fxh
76+
shaders/FSR_EdgeAdaptiveUpsampling.fx
77+
shaders/FSR_ContrastAdaptiveSharpening.fx
78+
shaders/fsr1/ffx_common_types.h
79+
shaders/fsr1/ffx_core.h
80+
shaders/fsr1/ffx_core_glsl.h
81+
shaders/fsr1/ffx_core_gpu_common.h
82+
shaders/fsr1/ffx_core_gpu_common_half.h
83+
shaders/fsr1/ffx_core_hlsl.h
84+
shaders/fsr1/ffx_core_portability.h
85+
shaders/fsr1/ffx_fsr1.h
86+
)
87+
set_source_files_properties(${FSR_SHADERS} PROPERTIES VS_TOOL_OVERRIDE "None")
88+
89+
set(FSR_SHADER_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/shaders_inc/FSR)
90+
file(MAKE_DIRECTORY ${FSR_SHADER_OUTPUT_DIR})
91+
92+
set(FSR_SHADERS_LIST_FILE ${FSR_SHADER_OUTPUT_DIR}/FSRShaderList.h)
93+
94+
if(DILIGENT_FSR_SUPPORTED)
95+
find_package(Python3 REQUIRED)
96+
97+
file(WRITE ${FSR_SHADERS_LIST_FILE}
98+
"static const MemoryShaderSourceFileInfo g_FSRShaders[] =\n"
99+
"{"
100+
)
101+
102+
foreach(FILE ${FSR_SHADERS})
103+
get_filename_component(FILE_NAME ${FILE} NAME)
104+
set(CONVERTED_FILE ${FSR_SHADER_OUTPUT_DIR}/${FILE_NAME}.h)
105+
add_custom_command(OUTPUT ${CONVERTED_FILE}
106+
COMMAND ${Python3_EXECUTABLE} ${FILE2STRING_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/${FILE} ${CONVERTED_FILE}
107+
MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}
108+
COMMENT "Processing FSR shader ${FILE}"
109+
VERBATIM)
110+
111+
file(APPEND ${FSR_SHADERS_LIST_FILE}
112+
"\n {"
113+
"\n \"${FILE_NAME}\","
114+
"\n #include \"${FILE_NAME}.h\""
115+
"\n },"
116+
)
117+
118+
list(APPEND FSR_SHADERS_INC_LIST ${CONVERTED_FILE})
119+
endforeach()
120+
121+
file(APPEND ${FSR_SHADERS_LIST_FILE}
122+
"\n};\n"
123+
)
124+
125+
set_source_files_properties(${FSR_SHADERS_INC_LIST} PROPERTIES GENERATED TRUE)
126+
else()
127+
message(WARNING "File2String utility is not found. FSR is disabled.")
128+
endif()
129+
130+
if(DILIGENT_FSR_SUPPORTED)
131+
list(APPEND SOURCE src/FSRProvider.cpp)
132+
endif()
133+
64134
if(DILIGENT_DLSS_SUPPORTED)
65135
list(APPEND INCLUDE include/SuperResolutionDLSS.hpp)
66136
list(APPEND SOURCE src/SuperResolutionDLSS.cpp)
@@ -92,9 +162,17 @@ target_include_directories(Diligent-SuperResolutionInterface INTERFACE interface
92162
target_compile_definitions(Diligent-SuperResolutionInterface INTERFACE SUPER_RESOLUTION_SUPPORTED=1)
93163

94164
add_library(Diligent-SuperResolution-static STATIC
95-
${SOURCE} ${INTERFACE} ${INCLUDE}
165+
${SOURCE}
166+
${INTERFACE}
167+
${INCLUDE}
168+
${FSR_SHADERS}
169+
${FSR_SHADERS_INC_LIST}
170+
${FSR_SHADERS_LIST_FILE}
96171
readme.md
97172
)
173+
source_group("shaders/FSR" FILES ${FSR_SHADERS})
174+
source_group("generated/FSR" FILES ${FSR_SHADERS_INC_LIST} ${FSR_SHADERS_LIST_FILE})
175+
98176
add_library(Diligent-SuperResolution-shared SHARED
99177
readme.md
100178
)
@@ -109,6 +187,7 @@ PRIVATE
109187
../GraphicsEngine/include
110188
../GraphicsEngineD3DBase/include
111189
../GraphicsEngineNextGenBase/include
190+
${CMAKE_CURRENT_BINARY_DIR}/shaders_inc/FSR
112191
)
113192

114193
if(DILIGENT_DLSS_SUPPORTED)
@@ -129,6 +208,10 @@ if(DILIGENT_DSR_SUPPORTED)
129208
endif()
130209
endif()
131210

211+
if(DILIGENT_FSR_SUPPORTED)
212+
target_compile_definitions(Diligent-SuperResolution-static PRIVATE DILIGENT_FSR_SUPPORTED=1)
213+
endif()
214+
132215
target_compile_definitions(Diligent-SuperResolution-shared PUBLIC DILIGENT_SUPER_RESOLUTION_SHARED=1)
133216

134217
target_link_libraries(Diligent-SuperResolution-static
@@ -139,6 +222,7 @@ PRIVATE
139222
Diligent-Common
140223
Diligent-GraphicsAccessories
141224
Diligent-ShaderTools
225+
Diligent-GraphicsTools
142226
)
143227

144228
if(DILIGENT_DSR_SUPPORTED)

Graphics/SuperResolution/include/SuperResolutionVariants.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,8 @@ static constexpr INTERFACE_ID VariantId_MetalFXSpatial =
4646
static constexpr INTERFACE_ID VariantId_MetalFXTemporal =
4747
{0xc4d70002, 0xa1b2, 0x4c3d, {0x8e, 0x9f, 0x0a, 0x1b, 0x2c, 0x3d, 0x4e, 0x5f}};
4848

49+
// {F5A10001-B2C3-4D5E-9F01-2A3B4C5D6E7F}
50+
static constexpr INTERFACE_ID VariantId_FSR_Spatial =
51+
{0xf5a10001, 0xb2c3, 0x4d5e, {0x9f, 0x01, 0x2a, 0x3b, 0x4c, 0x5d, 0x6e, 0x7f}};
52+
4953
} // namespace Diligent
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef _FSR_STRUCTURES_FXH_
2+
#define _FSR_STRUCTURES_FXH_
3+
4+
#ifndef __cplusplus
5+
# ifndef DEFAULT_VALUE
6+
# define DEFAULT_VALUE(x)
7+
# endif
8+
#elif !defined(DEFAULT_VALUE)
9+
# define DEFAULT_VALUE(x) = x
10+
#endif
11+
12+
#ifndef __cplusplus
13+
# ifndef CHECK_STRUCT_ALIGNMENT
14+
# define CHECK_STRUCT_ALIGNMENT(s)
15+
# endif
16+
#endif
17+
18+
struct FSRAttribs
19+
{
20+
uint4 EASUConstants0;
21+
uint4 EASUConstants1;
22+
uint4 EASUConstants2;
23+
uint4 EASUConstants3;
24+
uint4 RCASConstants;
25+
float4 SourceSize;
26+
};
27+
28+
#ifdef CHECK_STRUCT_ALIGNMENT
29+
CHECK_STRUCT_ALIGNMENT(FSRAttribs);
30+
#endif
31+
32+
33+
#endif //_FSR_STRUCTURES_FXH_
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "FSRStructures.fxh"
2+
3+
cbuffer cbFSRAttribs
4+
{
5+
FSRAttribs g_FSRAttribs;
6+
}
7+
8+
Texture2D<float4> g_TextureSource;
9+
10+
#define FFX_GPU
11+
#define FFX_HLSL
12+
#define FFX_HALF 0
13+
#define FFX_HLSL_SM 50
14+
#include "ffx_core.h"
15+
16+
#define FSR_RCAS_F 1
17+
#define FSR_RCAS_DENOISE 1
18+
19+
FfxFloat32x4 FsrRcasLoadF(FfxInt32x2 Position)
20+
{
21+
return g_TextureSource.Load(FfxInt32x3(Position, 0));
22+
}
23+
24+
void FsrRcasInputF(FFX_PARAMETER_INOUT FfxFloat32 R, FFX_PARAMETER_INOUT FfxFloat32 G, FFX_PARAMETER_INOUT FfxFloat32 B)
25+
{
26+
27+
}
28+
29+
#include "ffx_fsr1.h"
30+
31+
FfxFloat32x4 ComputeContrastAdaptiveSharpeningPS(in float4 Position : SV_Position) : SV_Target0
32+
{
33+
FfxFloat32x3 ResultColor = FfxFloat32x3(0.0, 0.0, 0.0);
34+
FsrRcasF(ResultColor.r, ResultColor.g, ResultColor.b, FfxUInt32x2(Position.xy), g_FSRAttribs.RCASConstants);
35+
return FfxFloat32x4(ResultColor, 1.0);
36+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include "FSRStructures.fxh"
2+
3+
cbuffer cbFSRAttribs
4+
{
5+
FSRAttribs g_FSRAttribs;
6+
}
7+
8+
Texture2D<float4> g_TextureSource;
9+
SamplerState g_TextureSource_sampler;
10+
11+
#define FFX_GPU
12+
#define FFX_HLSL
13+
#define FFX_HALF 0
14+
#define FFX_HLSL_SM 50
15+
#include "ffx_core.h"
16+
17+
#define FFX_FSR_EASU_FLOAT 1
18+
19+
FfxFloat32x4 FsrEasuRF(FfxFloat32x2 Texcoord)
20+
{
21+
#ifdef FSR_FEATURE_TEXTURE_GATHER
22+
return g_TextureSource.GatherRed(g_TextureSource_sampler, Texcoord);
23+
#else
24+
float2 Position = g_FSRAttribs.SourceSize.xy * Texcoord - float2(0.5, 0.5);
25+
FfxFloat32x4 Gather;
26+
Gather.x = g_TextureSource.Load(int3(int2(Position) + int2(0, 1), 0)).r;
27+
Gather.y = g_TextureSource.Load(int3(int2(Position) + int2(1, 1), 0)).r;
28+
Gather.z = g_TextureSource.Load(int3(int2(Position) + int2(1, 0), 0)).r;
29+
Gather.w = g_TextureSource.Load(int3(int2(Position) + int2(0, 0), 0)).r;
30+
return Gather;
31+
#endif
32+
}
33+
34+
FfxFloat32x4 FsrEasuGF(FfxFloat32x2 Texcoord)
35+
{
36+
#ifdef FSR_FEATURE_TEXTURE_GATHER
37+
return g_TextureSource.GatherGreen(g_TextureSource_sampler, Texcoord);
38+
#else
39+
float2 Position = g_FSRAttribs.SourceSize.xy * Texcoord - float2(0.5, 0.5);
40+
FfxFloat32x4 Gather;
41+
Gather.x = g_TextureSource.Load(int3(int2(Position) + int2(0, 1), 0)).g;
42+
Gather.y = g_TextureSource.Load(int3(int2(Position) + int2(1, 1), 0)).g;
43+
Gather.z = g_TextureSource.Load(int3(int2(Position) + int2(1, 0), 0)).g;
44+
Gather.w = g_TextureSource.Load(int3(int2(Position) + int2(0, 0), 0)).g;
45+
return Gather;
46+
#endif
47+
}
48+
49+
FfxFloat32x4 FsrEasuBF(FfxFloat32x2 Texcoord)
50+
{
51+
#ifdef FSR_FEATURE_TEXTURE_GATHER
52+
return g_TextureSource.GatherBlue(g_TextureSource_sampler, Texcoord);
53+
#else
54+
float2 Position = g_FSRAttribs.SourceSize.xy * Texcoord - float2(0.5, 0.5);
55+
FfxFloat32x4 Gather;
56+
Gather.x = g_TextureSource.Load(int3(int2(Position) + int2(0, 1), 0)).b;
57+
Gather.y = g_TextureSource.Load(int3(int2(Position) + int2(1, 1), 0)).b;
58+
Gather.z = g_TextureSource.Load(int3(int2(Position) + int2(1, 0), 0)).b;
59+
Gather.w = g_TextureSource.Load(int3(int2(Position) + int2(0, 0), 0)).b;
60+
return Gather;
61+
#endif
62+
}
63+
64+
#include "ffx_fsr1.h"
65+
66+
FfxFloat32x4 ComputeEdgeAdaptiveUpsamplingPS(in float4 Position : SV_Position) : SV_Target0
67+
{
68+
FfxFloat32x3 ResultColor = FfxFloat32x3(0.0, 0.0, 0.0);
69+
ffxFsrEasuFloat(ResultColor, FfxUInt32x2(Position.xy),
70+
g_FSRAttribs.EASUConstants0, g_FSRAttribs.EASUConstants1,
71+
g_FSRAttribs.EASUConstants2, g_FSRAttribs.EASUConstants3);
72+
return FfxFloat32x4(ResultColor, 1.0);
73+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
struct FSR_VSOutput
2+
{
3+
float4 Position : SV_Position;
4+
};
5+
6+
void FSR_FullQuadVS(in uint VertexId : SV_VertexID, out FSR_VSOutput VSOut)
7+
{
8+
float2 PosXY[3];
9+
PosXY[0] = float2(-1.0, -1.0);
10+
PosXY[1] = float2(-1.0, +3.0);
11+
PosXY[2] = float2(+3.0, -1.0);
12+
13+
VSOut.Position = float4(PosXY[VertexId % 3u], 0.0, 1.0);
14+
}

0 commit comments

Comments
 (0)