From 99d593a27c1adb463176688c1fea93a6bebbd268 Mon Sep 17 00:00:00 2001 From: 187J3X1-114514 Date: Tue, 1 Oct 2024 17:39:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- CMakeLists.txt | 11 +- ...erresolution_fsr2_nativelib_ffx_fsr2_api.h | 124 +- include/pch.h | 15 - src/ffx-fsr2-api/CMakeLists.txt | 12 +- src/ffx-fsr2-api/ffx_fsr2.cpp | 700 +++--- src/ffx-fsr2-api/gl/CMakeLists.txt | 8 +- src/ffx-fsr2-api/gl/ffx_fsr2_gl.cpp | 2137 ++++++++--------- src/ffx-fsr2-api/gl/ffx_fsr2_gl.h | 45 +- src/ffxFsr2.cpp | 123 +- src/pch.cpp | 1 - src/utils.cpp | 75 +- 12 files changed, 1618 insertions(+), 1636 deletions(-) delete mode 100644 include/pch.h delete mode 100644 src/pch.cpp diff --git a/.gitignore b/.gitignore index 7544ee5..3fac731 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,5 @@ CMakeUserPresets.json build/ bin/ tools/test.ipynb -.vscode/ \ No newline at end of file +.vscode/ +.vs/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index b90ea65..9563225 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,14 @@ +cmake_minimum_required(VERSION 3.15) project (fsr2_java_lib) -set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -O3 -Wall") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -Wall") - +set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -O3 -Wall -g") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -Wall -g") +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -g") +add_definitions(-D_DEBUG) if (CMAKE_SYSTEM_NAME MATCHES "Linux") add_definitions(-DON_LINUX) add_definitions(-DFFX_GCC) + endif() if (CMAKE_SYSTEM_NAME MATCHES "Linux") @@ -39,8 +42,6 @@ set_target_properties(lib_shared PROPERTIES OUTPUT_NAME "fsr2javalib") target_link_libraries(lib_shared ${LIB_NVML} ${LIB_FSR2_GL} ${LIB_FSR2_API}) - - set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../bin) #mingw32-make \ No newline at end of file diff --git a/include/io_homo_superresolution_fsr2_nativelib_ffx_fsr2_api.h b/include/io_homo_superresolution_fsr2_nativelib_ffx_fsr2_api.h index bf3c59c..f914d5a 100644 --- a/include/io_homo_superresolution_fsr2_nativelib_ffx_fsr2_api.h +++ b/include/io_homo_superresolution_fsr2_nativelib_ffx_fsr2_api.h @@ -1,89 +1,71 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ - - #ifdef ON_LINUX #include "jni_linux64.h" #else #include "jni.h" #endif +/* Header for class io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper */ -/* Header for class io_homo_superresolution_fsr2_nativelib_ffx_fsr2_api */ - -#ifndef _Included_io_homo_superresolution_fsr2_nativelib_ffx_fsr2_api -#define _Included_io_homo_superresolution_fsr2_nativelib_ffx_fsr2_api +#ifndef _Included_io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper +#define _Included_io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif - /* - * Class: io_homo_superresolution_fsr2_nativelib_ffx_fsr2_api - * Method: init - * Signature: ()V - */ - JNIEXPORT void JNICALL Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1api_init(JNIEnv *, jobject); - - /* - * Class: io_homo_superresolution_fsr2_nativelib_ffx_fsr2_api - * Method: ffxFsr2GetScratchMemorySizeGL - * Signature: ()I - */ - JNIEXPORT jint JNICALL Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1api_ffxFsr2GetScratchMemorySizeGL(JNIEnv *, jobject); - - /* - * Class: io_homo_superresolution_fsr2_nativelib_ffx_fsr2_api - * Method: ffxFsr2CreateContext - * Signature: ()I - */ - JNIEXPORT jint JNICALL Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1api_ffxFsr2CreateContext(JNIEnv *, jobject); +/* + * Class: io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper + * Method: ffxFsr2GetScratchMemorySizeGL + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper_ffxFsr2GetScratchMemorySizeGL + (JNIEnv *, jobject); - /* - * Class: io_homo_superresolution_fsr2_nativelib_ffx_fsr2_api - * Method: ffxFsr2GetInterfaceGL - * Signature: (IFIII)I - */ - JNIEXPORT jint JNICALL Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1api_ffxFsr2GetInterfaceGL(JNIEnv *, jobject, jint, jfloat, jint, jint, jint); +/* + * Class: io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper + * Method: ffxFsr2CreateGL + * Signature: (IFIII)I + */ +JNIEXPORT jint JNICALL Java_io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper_ffxFsr2CreateGL + (JNIEnv *, jobject, jint, jfloat, jint, jint, jint); - /* - * Class: io_homo_superresolution_fsr2_nativelib_ffx_fsr2_api - * Method: ffxFsr2Test - * Signature: ()I - */ - JNIEXPORT jint JNICALL Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1api_ffxFsr2Test(JNIEnv *, jobject); +/* + * Class: io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper + * Method: ffxFsr2Test + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper_ffxFsr2Test + (JNIEnv *, jobject); - /* - * Class: io_homo_superresolution_fsr2_nativelib_ffx_fsr2_api - * Method: ffxFsr2ContextDestroy - * Signature: ()I - */ - JNIEXPORT jint JNICALL Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1api_ffxFsr2ContextDestroy(JNIEnv *, jobject); +/* + * Class: io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper + * Method: getGPUInfoNV + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper_getGPUInfoNV + (JNIEnv *, jobject); - /* - * Class: io_homo_superresolution_fsr2_nativelib_ffx_fsr2_api - * Method: getGPUInfoNV - * Signature: ()Ljava/lang/String; - */ - JNIEXPORT jstring JNICALL Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1api_getGPUInfoNV(JNIEnv *, jobject); +/* + * Class: io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper + * Method: getGPUInfoAMD + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper_getGPUInfoAMD + (JNIEnv *, jobject); - /* - * Class: io_homo_superresolution_fsr2_nativelib_ffx_fsr2_api - * Method: getGPUInfoAMD - * Signature: ()Ljava/lang/String; - */ - JNIEXPORT jstring JNICALL Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1api_getGPUInfoAMD(JNIEnv *, jobject); +/* + * Class: io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper + * Method: ffxFsr2ContextDispatch + * Signature: (Lio/homo/superresolution/fsr2/types/FfxResource;Lio/homo/superresolution/fsr2/types/FfxResource;Lio/homo/superresolution/fsr2/types/FfxResource;Lio/homo/superresolution/fsr2/types/FfxResource;Lio/homo/superresolution/fsr2/types/FfxResource;Lio/homo/superresolution/fsr2/types/FfxResource;FFFFIIZFFFZFFFFZII)I + */ +JNIEXPORT jint JNICALL Java_io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper_ffxFsr2ContextDispatch + (JNIEnv *, jobject, jobject, jobject, jobject, jobject, jobject, jobject, jfloat, jfloat, jfloat, jfloat, jint, jint, jboolean, jfloat, jfloat, jfloat, jboolean, jfloat, jfloat, jfloat, jfloat, jboolean, jint, jint); - /* - * Class: io_homo_superresolution_fsr2_nativelib_ffx_fsr2_api - * Method: ffxFsr2ContextDispatch - * Signature: (IIIIIIFFFFIIZFFFZFFFFZII)I - */ - JNIEXPORT jint JNICALL Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1api_ffxFsr2ContextDispatch(JNIEnv *, jobject, jint, jint, jint, jint, jint, jint, jfloat, jfloat, jfloat, jfloat, jint, jint, jboolean, jfloat, jfloat, jfloat, jboolean, jfloat, jfloat, jfloat, jfloat, jboolean, jint, jint); +/* + * Class: io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper + * Method: ffxGetTextureResourceGL + * Signature: (JIII)Lio/homo/superresolution/fsr2/types/FfxResource; + */ +JNIEXPORT jobject JNICALL Java_io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper_ffxGetTextureResourceGL + (JNIEnv *, jobject, jlong, jint, jint, jint); - /* - * Class: io_homo_superresolution_fsr2_nativelib_ffx_fsr2_api - * Method: ffxGetTextureResourceGL - * Signature: (JIII)Lio/homo/superresolution/fsr2/types/FfxResource; - */ - JNIEXPORT jobject JNICALL Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1api_ffxGetTextureResourceGL(JNIEnv *, jobject, jlong, jint, jint, jint); #ifdef __cplusplus } #endif diff --git a/include/pch.h b/include/pch.h deleted file mode 100644 index 6769bed..0000000 --- a/include/pch.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#ifndef PCH_H -#define PCH_H - -#include -#include -#include -#include -#include -#include "json.hpp" -#include -#include "nvml.h" - -#endif diff --git a/src/ffx-fsr2-api/CMakeLists.txt b/src/ffx-fsr2-api/CMakeLists.txt index 086b240..a233ce3 100644 --- a/src/ffx-fsr2-api/CMakeLists.txt +++ b/src/ffx-fsr2-api/CMakeLists.txt @@ -23,8 +23,8 @@ cmake_minimum_required(VERSION 3.15) set(CMAKE_DEBUG_POSTFIX d) option (FFX_FSR2_API_GL "Build FSR 2.0 OpenGL backend" ON) -set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -O3 -Wall") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -Wall") +set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -O3 -Wall -g") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -Wall -g") set(FSR2_AUTO_COMPILE_SHADERS ON CACHE BOOL "Compile shaders automatically as a prebuild step.") @@ -38,10 +38,10 @@ set(FSR2_PLATFORM_NAME x64) if ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU")) add_compile_definitions(FFX_GCC) # Should work with gcc and others endif() -# Embed PDBs in the debug versions of the libs -set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Z7") -set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CMAKE_HOME_DIRECTORY}/bin/ffx_fsr2_api/) -set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${CMAKE_HOME_DIRECTORY}/bin/ffx_fsr2_api/) +## Embed PDBs in the debug versions of the libs +#set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Z7") +#set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CMAKE_HOME_DIRECTORY}/bin) +#set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${CMAKE_HOME_DIRECTORY}/bin) add_compile_definitions(_UNICODE) add_compile_definitions(UNICODE) diff --git a/src/ffx-fsr2-api/ffx_fsr2.cpp b/src/ffx-fsr2-api/ffx_fsr2.cpp index 08a7ff0..58a0ea0 100644 --- a/src/ffx-fsr2-api/ffx_fsr2.cpp +++ b/src/ffx-fsr2-api/ffx_fsr2.cpp @@ -19,10 +19,10 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#include // for max used inside SPD CPU code. -#include // for fabs, abs, sinf, sqrt, etc. -#include // for memset -#include // for FLT_EPSILON +#include // for max used inside SPD CPU code. +#include // for fabs, abs, sinf, sqrt, etc. +#include // for memset +#include // for FLT_EPSILON #include #include "ffx_fsr2.h" #define FFX_CPU @@ -49,77 +49,78 @@ static const uint32_t FSR2_MAX_QUEUED_FRAMES = 16; // lists to map shader resource bindpoint name to resource identifier typedef struct ResourceBinding { - uint32_t index; - wchar_t name[64]; -}ResourceBinding; + uint32_t index; + wchar_t name[64]; +} ResourceBinding; static const ResourceBinding srvResourceBindingTable[] = -{ - {FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_COLOR, L"r_input_color_jittered"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_OPAQUE_ONLY, L"r_input_opaque_only"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_MOTION_VECTORS, L"r_input_motion_vectors"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_DEPTH, L"r_input_depth" }, - {FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_EXPOSURE, L"r_input_exposure"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE, L"r_auto_exposure"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_REACTIVE_MASK, L"r_reactive_mask"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_TRANSPARENCY_AND_COMPOSITION_MASK, L"r_transparency_and_composition_mask"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_RECONSTRUCTED_PREVIOUS_NEAREST_DEPTH, L"r_reconstructed_previous_nearest_depth"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_MOTION_VECTORS, L"r_dilated_motion_vectors"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_PREVIOUS_DILATED_MOTION_VECTORS, L"r_previous_dilated_motion_vectors"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_DEPTH, L"r_dilatedDepth"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_UPSCALED_COLOR, L"r_internal_upscaled_color"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_STATUS, L"r_lock_status"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_PREPARED_INPUT_COLOR, L"r_prepared_input_color"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_LUMA_HISTORY, L"r_luma_history" }, - {FFX_FSR2_RESOURCE_IDENTIFIER_RCAS_INPUT, L"r_rcas_input"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_LANCZOS_LUT, L"r_lanczos_lut"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE, L"r_imgMips"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_SHADING_CHANGE, L"r_img_mip_shading_change"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_5, L"r_img_mip_5"}, - {FFX_FSR2_RESOURCE_IDENTITIER_UPSAMPLE_MAXIMUM_BIAS_LUT, L"r_upsample_maximum_bias_lut"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_REACTIVE_MASKS, L"r_dilated_reactive_masks"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_NEW_LOCKS, L"r_new_locks"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_INPUT_LUMA, L"r_lock_input_luma"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_PREV_PRE_ALPHA_COLOR, L"r_input_prev_color_pre_alpha"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_PREV_POST_ALPHA_COLOR, L"r_input_prev_color_post_alpha"}, + { + {FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_COLOR, L"r_input_color_jittered"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_OPAQUE_ONLY, L"r_input_opaque_only"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_MOTION_VECTORS, L"r_input_motion_vectors"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_DEPTH, L"r_input_depth"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_EXPOSURE, L"r_input_exposure"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE, L"r_auto_exposure"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_REACTIVE_MASK, L"r_reactive_mask"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_TRANSPARENCY_AND_COMPOSITION_MASK, L"r_transparency_and_composition_mask"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_RECONSTRUCTED_PREVIOUS_NEAREST_DEPTH, L"r_reconstructed_previous_nearest_depth"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_MOTION_VECTORS, L"r_dilated_motion_vectors"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_PREVIOUS_DILATED_MOTION_VECTORS, L"r_previous_dilated_motion_vectors"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_DEPTH, L"r_dilatedDepth"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_UPSCALED_COLOR, L"r_internal_upscaled_color"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_STATUS, L"r_lock_status"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_PREPARED_INPUT_COLOR, L"r_prepared_input_color"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_LUMA_HISTORY, L"r_luma_history"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_RCAS_INPUT, L"r_rcas_input"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_LANCZOS_LUT, L"r_lanczos_lut"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE, L"r_imgMips"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_SHADING_CHANGE, L"r_img_mip_shading_change"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_5, L"r_img_mip_5"}, + {FFX_FSR2_RESOURCE_IDENTITIER_UPSAMPLE_MAXIMUM_BIAS_LUT, L"r_upsample_maximum_bias_lut"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_REACTIVE_MASKS, L"r_dilated_reactive_masks"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_NEW_LOCKS, L"r_new_locks"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_INPUT_LUMA, L"r_lock_input_luma"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_PREV_PRE_ALPHA_COLOR, L"r_input_prev_color_pre_alpha"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_PREV_POST_ALPHA_COLOR, L"r_input_prev_color_post_alpha"}, }; static const ResourceBinding uavResourceBindingTable[] = -{ - {FFX_FSR2_RESOURCE_IDENTIFIER_RECONSTRUCTED_PREVIOUS_NEAREST_DEPTH, L"rw_reconstructed_previous_nearest_depth"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_MOTION_VECTORS, L"rw_dilated_motion_vectors"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_DEPTH, L"rw_dilatedDepth"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_UPSCALED_COLOR, L"rw_internal_upscaled_color"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_STATUS, L"rw_lock_status"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_PREPARED_INPUT_COLOR, L"rw_prepared_input_color"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_LUMA_HISTORY, L"rw_luma_history"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_UPSCALED_OUTPUT, L"rw_upscaled_output"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_SHADING_CHANGE, L"rw_img_mip_shading_change"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_5, L"rw_img_mip_5"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_REACTIVE_MASKS, L"rw_dilated_reactive_masks"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE, L"rw_auto_exposure"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_SPD_ATOMIC_COUNT, L"rw_spd_global_atomic"}, - #if defined(FFX_INTERNAL) - {FFX_FSR2_RESOURCE_IDENTIFIER_DEBUG_OUTPUT, L"rw_debug_out"}, - #endif - {FFX_FSR2_RESOURCE_IDENTIFIER_NEW_LOCKS, L"rw_new_locks"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_INPUT_LUMA, L"rw_lock_input_luma"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_AUTOREACTIVE, L"rw_output_autoreactive"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_AUTOCOMPOSITION, L"rw_output_autocomposition"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_PREV_PRE_ALPHA_COLOR, L"rw_output_prev_color_pre_alpha"}, - {FFX_FSR2_RESOURCE_IDENTIFIER_PREV_POST_ALPHA_COLOR, L"rw_output_prev_color_post_alpha"}, + { + {FFX_FSR2_RESOURCE_IDENTIFIER_RECONSTRUCTED_PREVIOUS_NEAREST_DEPTH, L"rw_reconstructed_previous_nearest_depth"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_MOTION_VECTORS, L"rw_dilated_motion_vectors"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_DEPTH, L"rw_dilatedDepth"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_UPSCALED_COLOR, L"rw_internal_upscaled_color"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_STATUS, L"rw_lock_status"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_PREPARED_INPUT_COLOR, L"rw_prepared_input_color"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_LUMA_HISTORY, L"rw_luma_history"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_UPSCALED_OUTPUT, L"rw_upscaled_output"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_SHADING_CHANGE, L"rw_img_mip_shading_change"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_5, L"rw_img_mip_5"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_REACTIVE_MASKS, L"rw_dilated_reactive_masks"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE, L"rw_auto_exposure"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_SPD_ATOMIC_COUNT, L"rw_spd_global_atomic"}, +#if defined(FFX_INTERNAL) + {FFX_FSR2_RESOURCE_IDENTIFIER_DEBUG_OUTPUT, L"rw_debug_out"}, +#endif + {FFX_FSR2_RESOURCE_IDENTIFIER_NEW_LOCKS, L"rw_new_locks"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_INPUT_LUMA, L"rw_lock_input_luma"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_AUTOREACTIVE, L"rw_output_autoreactive"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_AUTOCOMPOSITION, L"rw_output_autocomposition"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_PREV_PRE_ALPHA_COLOR, L"rw_output_prev_color_pre_alpha"}, + {FFX_FSR2_RESOURCE_IDENTIFIER_PREV_POST_ALPHA_COLOR, L"rw_output_prev_color_post_alpha"}, }; static const ResourceBinding cbResourceBindingTable[] = -{ - {FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_FSR2, L"cbFSR2"}, - {FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_SPD, L"cbSPD"}, - {FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_RCAS, L"cbRCAS"}, - {FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_GENREACTIVE, L"cbGenerateReactive"}, + { + {FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_FSR2, L"cbFSR2"}, + {FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_SPD, L"cbSPD"}, + {FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_RCAS, L"cbRCAS"}, + {FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_GENREACTIVE, L"cbGenerateReactive"}, }; // Broad structure of the root signature. -typedef enum Fsr2RootSignatureLayout { +typedef enum Fsr2RootSignatureLayout +{ FSR2_ROOT_SIGNATURE_LAYOUT_UAVS, FSR2_ROOT_SIGNATURE_LAYOUT_SRVS, @@ -128,74 +129,76 @@ typedef enum Fsr2RootSignatureLayout { FSR2_ROOT_SIGNATURE_LAYOUT_PARAMETER_COUNT } Fsr2RootSignatureLayout; -typedef struct Fsr2RcasConstants { +typedef struct Fsr2RcasConstants +{ - uint32_t rcasConfig[4]; + uint32_t rcasConfig[4]; } FfxRcasConstants; -typedef struct Fsr2SpdConstants { +typedef struct Fsr2SpdConstants +{ - uint32_t mips; - uint32_t numworkGroups; - uint32_t workGroupOffset[2]; - uint32_t renderSize[2]; + uint32_t mips; + uint32_t numworkGroups; + uint32_t workGroupOffset[2]; + uint32_t renderSize[2]; } Fsr2SpdConstants; typedef struct Fsr2GenerateReactiveConstants { - float scale; - float threshold; - float binaryValue; - uint32_t flags; + float scale; + float threshold; + float binaryValue; + uint32_t flags; } Fsr2GenerateReactiveConstants; typedef struct Fsr2GenerateReactiveConstants2 { - float autoTcThreshold; - float autoTcScale; - float autoReactiveScale; - float autoReactiveMax; + float autoTcThreshold; + float autoTcScale; + float autoReactiveScale; + float autoReactiveMax; } Fsr2GenerateReactiveConstants2; -typedef union Fsr2SecondaryUnion { +typedef union Fsr2SecondaryUnion +{ - Fsr2RcasConstants rcas; - Fsr2SpdConstants spd; - Fsr2GenerateReactiveConstants2 autogenReactive; + Fsr2RcasConstants rcas; + Fsr2SpdConstants spd; + Fsr2GenerateReactiveConstants2 autogenReactive; } Fsr2SecondaryUnion; -typedef struct Fsr2ResourceDescription { - - uint32_t id; - const wchar_t* name; - FfxResourceUsage usage; - FfxSurfaceFormat format; - uint32_t width; - uint32_t height; - uint32_t mipCount; - FfxResourceFlags flags; - uint32_t initDataSize; - void* initData; +typedef struct Fsr2ResourceDescription +{ + + uint32_t id; + const wchar_t *name; + FfxResourceUsage usage; + FfxSurfaceFormat format; + uint32_t width; + uint32_t height; + uint32_t mipCount; + FfxResourceFlags flags; + uint32_t initDataSize; + void *initData; } Fsr2ResourceDescription; FfxConstantBuffer globalFsr2ConstantBuffers[4] = { - { sizeof(Fsr2Constants) / sizeof(uint32_t) }, - { sizeof(Fsr2SpdConstants) / sizeof(uint32_t) }, - { sizeof(Fsr2RcasConstants) / sizeof(uint32_t) }, - { sizeof(Fsr2GenerateReactiveConstants) / sizeof(uint32_t) } -}; + {sizeof(Fsr2Constants) / sizeof(uint32_t)}, + {sizeof(Fsr2SpdConstants) / sizeof(uint32_t)}, + {sizeof(Fsr2RcasConstants) / sizeof(uint32_t)}, + {sizeof(Fsr2GenerateReactiveConstants) / sizeof(uint32_t)}}; // Lanczos static float lanczos2(float value) { - #ifdef ON_LINUX - return std::abs(value) < FFX_EPSILON ? 1.f : (sinf(FFX_PI * value) / (FFX_PI * value)) * (sinf(0.5f * FFX_PI * value) / (0.5f * FFX_PI * value)); - #else - return abs(value) < FFX_EPSILON ? 1.f : (sinf(FFX_PI * value) / (FFX_PI * value)) * (sinf(0.5f * FFX_PI * value) / (0.5f * FFX_PI * value)); - #endif - +#ifdef ON_LINUX + return std::abs(value) < FFX_EPSILON ? 1.f : (sinf(FFX_PI * value) / (FFX_PI * value)) * (sinf(0.5f * FFX_PI * value) / (0.5f * FFX_PI * value)); +#else + return abs(value) < FFX_EPSILON ? 1.f : (sinf(FFX_PI * value) / (FFX_PI * value)) * (sinf(0.5f * FFX_PI * value) / (0.5f * FFX_PI * value)); +#endif } // Calculate halton number for index and base. @@ -203,7 +206,8 @@ static float halton(int32_t index, int32_t base) { float f = 1.0f, result = 0.0f; - for (int32_t currentIndex = index; currentIndex > 0;) { + for (int32_t currentIndex = index; currentIndex > 0;) + { f /= (float)base; result = result + f * (float)(currentIndex % base); @@ -213,7 +217,7 @@ static float halton(int32_t index, int32_t base) return result; } -static void fsr2DebugCheckDispatch(FfxFsr2Context_Private* context, const FfxFsr2DispatchDescription* params) +static void fsr2DebugCheckDispatch(FfxFsr2Context_Private *context, const FfxFsr2DispatchDescription *params) { if ((params->commandList == nullptr) && !(context->contextDescription.flags & FFX_FSR2_ALLOW_NULL_DEVICE_AND_COMMAND_LIST)) { @@ -298,20 +302,20 @@ static void fsr2DebugCheckDispatch(FfxFsr2Context_Private* context, const FfxFsr if (params->cameraNear < params->cameraFar) { context->contextDescription.fpMessage(FFX_FSR2_MESSAGE_TYPE_WARNING, - L"FFX_FSR2_ENABLE_DEPTH_INVERTED flag is present yet cameraNear is less than cameraFar"); + L"FFX_FSR2_ENABLE_DEPTH_INVERTED flag is present yet cameraNear is less than cameraFar"); } if (infiniteDepth) { if (params->cameraNear != FLT_MAX) { context->contextDescription.fpMessage(FFX_FSR2_MESSAGE_TYPE_WARNING, - L"FFX_FSR2_ENABLE_DEPTH_INFINITE and FFX_FSR2_ENABLE_DEPTH_INVERTED present, yet cameraNear != FLT_MAX"); + L"FFX_FSR2_ENABLE_DEPTH_INFINITE and FFX_FSR2_ENABLE_DEPTH_INVERTED present, yet cameraNear != FLT_MAX"); } } if (params->cameraFar < 0.075f) { context->contextDescription.fpMessage(FFX_FSR2_MESSAGE_TYPE_WARNING, - L"FFX_FSR2_ENABLE_DEPTH_INFINITE and FFX_FSR2_ENABLE_DEPTH_INVERTED present, cameraFar value is very low which may result in depth separation artefacting"); + L"FFX_FSR2_ENABLE_DEPTH_INFINITE and FFX_FSR2_ENABLE_DEPTH_INVERTED present, cameraFar value is very low which may result in depth separation artefacting"); } } else @@ -319,20 +323,20 @@ static void fsr2DebugCheckDispatch(FfxFsr2Context_Private* context, const FfxFsr if (params->cameraNear > params->cameraFar) { context->contextDescription.fpMessage(FFX_FSR2_MESSAGE_TYPE_WARNING, - L"cameraNear is greater than cameraFar in non-inverted-depth context"); + L"cameraNear is greater than cameraFar in non-inverted-depth context"); } if (infiniteDepth) { if (params->cameraFar != FLT_MAX) { context->contextDescription.fpMessage(FFX_FSR2_MESSAGE_TYPE_WARNING, - L"FFX_FSR2_ENABLE_DEPTH_INFINITE and FFX_FSR2_ENABLE_DEPTH_INVERTED present, yet cameraFar != FLT_MAX"); + L"FFX_FSR2_ENABLE_DEPTH_INFINITE and FFX_FSR2_ENABLE_DEPTH_INVERTED present, yet cameraFar != FLT_MAX"); } } if (params->cameraNear < 0.075f) { context->contextDescription.fpMessage(FFX_FSR2_MESSAGE_TYPE_WARNING, - L"FFX_FSR2_ENABLE_DEPTH_INFINITE and FFX_FSR2_ENABLE_DEPTH_INVERTED present, cameraNear value is very low which may result in depth separation artefacting"); + L"FFX_FSR2_ENABLE_DEPTH_INFINITE and FFX_FSR2_ENABLE_DEPTH_INVERTED present, cameraNear value is very low which may result in depth separation artefacting"); } } @@ -346,7 +350,7 @@ static void fsr2DebugCheckDispatch(FfxFsr2Context_Private* context, const FfxFsr } } -static FfxErrorCode patchResourceBindings(FfxPipelineState* inoutPipeline) +static FfxErrorCode patchResourceBindings(FfxPipelineState *inoutPipeline) { for (uint32_t srvIndex = 0; srvIndex < inoutPipeline->srvCount; ++srvIndex) { @@ -393,8 +397,7 @@ static FfxErrorCode patchResourceBindings(FfxPipelineState* inoutPipeline) return FFX_OK; } - -static FfxErrorCode createPipelineStates(FfxFsr2Context_Private* context) +static FfxErrorCode createPipelineStates(FfxFsr2Context_Private *context) { FFX_ASSERT(context); @@ -428,7 +431,7 @@ static FfxErrorCode createPipelineStates(FfxFsr2Context_Private* context) FFX_VALIDATE(context->contextDescription.callbacks.fpCreatePipeline(&context->contextDescription.callbacks, FFX_FSR2_PASS_LOCK, &pipelineDescription, &context->pipelineLock)); FFX_VALIDATE(context->contextDescription.callbacks.fpCreatePipeline(&context->contextDescription.callbacks, FFX_FSR2_PASS_ACCUMULATE, &pipelineDescription, &context->pipelineAccumulate)); FFX_VALIDATE(context->contextDescription.callbacks.fpCreatePipeline(&context->contextDescription.callbacks, FFX_FSR2_PASS_ACCUMULATE_SHARPEN, &pipelineDescription, &context->pipelineAccumulateSharpen)); - + // for each pipeline: re-route/fix-up IDs based on names patchResourceBindings(&context->pipelineDepthClip); patchResourceBindings(&context->pipelineReconstructPreviousDepth); @@ -443,9 +446,9 @@ static FfxErrorCode createPipelineStates(FfxFsr2Context_Private* context) return FFX_OK; } -static FfxErrorCode generateReactiveMaskInternal(FfxFsr2Context_Private* contextPrivate, const FfxFsr2DispatchDescription* params); +static FfxErrorCode generateReactiveMaskInternal(FfxFsr2Context_Private *contextPrivate, const FfxFsr2DispatchDescription *params); -static FfxErrorCode fsr2Create(FfxFsr2Context_Private* context, const FfxFsr2ContextDescription* contextDescription) +static FfxErrorCode fsr2Create(FfxFsr2Context_Private *context, const FfxFsr2ContextDescription *contextDescription) { FFX_ASSERT(context); FFX_ASSERT(contextDescription); @@ -483,9 +486,10 @@ static FfxErrorCode fsr2Create(FfxFsr2Context_Private* context, const FfxFsr2Con // generate the data for the LUT. const uint32_t lanczos2LutWidth = 128; - int16_t lanczos2Weights[lanczos2LutWidth] = { }; + int16_t lanczos2Weights[lanczos2LutWidth] = {}; - for (uint32_t currentLanczosWidthIndex = 0; currentLanczosWidthIndex < lanczos2LutWidth; currentLanczosWidthIndex++) { + for (uint32_t currentLanczosWidthIndex = 0; currentLanczosWidthIndex < lanczos2LutWidth; currentLanczosWidthIndex++) + { const float x = 2.0f * currentLanczosWidthIndex / float(lanczos2LutWidth - 1); const float y = lanczos2(x); @@ -494,109 +498,110 @@ static FfxErrorCode fsr2Create(FfxFsr2Context_Private* context, const FfxFsr2Con // upload path only supports R16_SNORM, let's go and convert int16_t maximumBias[FFX_FSR2_MAXIMUM_BIAS_TEXTURE_WIDTH * FFX_FSR2_MAXIMUM_BIAS_TEXTURE_HEIGHT]; - for (uint32_t i = 0; i < FFX_FSR2_MAXIMUM_BIAS_TEXTURE_WIDTH * FFX_FSR2_MAXIMUM_BIAS_TEXTURE_HEIGHT; ++i) { + for (uint32_t i = 0; i < FFX_FSR2_MAXIMUM_BIAS_TEXTURE_WIDTH * FFX_FSR2_MAXIMUM_BIAS_TEXTURE_HEIGHT; ++i) + { maximumBias[i] = int16_t(roundf(ffxFsr2MaximumBias[i] / 2.0f * 32767.0f)); } uint8_t defaultReactiveMaskData = 0U; uint32_t atomicInitData = 0U; - float defaultExposure[] = { 0.0f, 0.0f }; + float defaultExposure[] = {0.0f, 0.0f}; const FfxResourceType texture1dResourceType = (context->contextDescription.flags & FFX_FSR2_ENABLE_TEXTURE1D_USAGE) ? FFX_RESOURCE_TYPE_TEXTURE1D : FFX_RESOURCE_TYPE_TEXTURE2D; // declare internal resources needed const Fsr2ResourceDescription internalSurfaceDesc[] = { - { FFX_FSR2_RESOURCE_IDENTIFIER_PREPARED_INPUT_COLOR, L"FSR2_PreparedInputColor", FFX_RESOURCE_USAGE_UAV, - FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_ALIASABLE }, + {FFX_FSR2_RESOURCE_IDENTIFIER_PREPARED_INPUT_COLOR, L"FSR2_PreparedInputColor", FFX_RESOURCE_USAGE_UAV, + FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_ALIASABLE}, - { FFX_FSR2_RESOURCE_IDENTIFIER_RECONSTRUCTED_PREVIOUS_NEAREST_DEPTH, L"FSR2_ReconstructedPrevNearestDepth", FFX_RESOURCE_USAGE_UAV, - FFX_SURFACE_FORMAT_R32_UINT, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_ALIASABLE }, + {FFX_FSR2_RESOURCE_IDENTIFIER_RECONSTRUCTED_PREVIOUS_NEAREST_DEPTH, L"FSR2_ReconstructedPrevNearestDepth", FFX_RESOURCE_USAGE_UAV, + FFX_SURFACE_FORMAT_R32_UINT, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_ALIASABLE}, - { FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_DILATED_MOTION_VECTORS_1, L"FSR2_InternalDilatedVelocity1", (FfxResourceUsage)(FFX_RESOURCE_USAGE_RENDERTARGET | FFX_RESOURCE_USAGE_UAV), - FFX_SURFACE_FORMAT_R16G16_FLOAT, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_NONE }, + {FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_DILATED_MOTION_VECTORS_1, L"FSR2_InternalDilatedVelocity1", (FfxResourceUsage)(FFX_RESOURCE_USAGE_RENDERTARGET | FFX_RESOURCE_USAGE_UAV), + FFX_SURFACE_FORMAT_R16G16_FLOAT, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_NONE}, - { FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_DILATED_MOTION_VECTORS_2, L"FSR2_InternalDilatedVelocity2", (FfxResourceUsage)(FFX_RESOURCE_USAGE_RENDERTARGET | FFX_RESOURCE_USAGE_UAV), - FFX_SURFACE_FORMAT_R16G16_FLOAT, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_NONE }, + {FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_DILATED_MOTION_VECTORS_2, L"FSR2_InternalDilatedVelocity2", (FfxResourceUsage)(FFX_RESOURCE_USAGE_RENDERTARGET | FFX_RESOURCE_USAGE_UAV), + FFX_SURFACE_FORMAT_R16G16_FLOAT, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_NONE}, - { FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_DEPTH, L"FSR2_DilatedDepth", (FfxResourceUsage)(FFX_RESOURCE_USAGE_RENDERTARGET | FFX_RESOURCE_USAGE_UAV), - FFX_SURFACE_FORMAT_R32_FLOAT, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_ALIASABLE }, - - { FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_STATUS_1, L"FSR2_LockStatus1", (FfxResourceUsage)(FFX_RESOURCE_USAGE_RENDERTARGET | FFX_RESOURCE_USAGE_UAV), - FFX_SURFACE_FORMAT_R16G16_FLOAT, contextDescription->displaySize.width, contextDescription->displaySize.height, 1, FFX_RESOURCE_FLAGS_NONE }, + {FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_DEPTH, L"FSR2_DilatedDepth", (FfxResourceUsage)(FFX_RESOURCE_USAGE_RENDERTARGET | FFX_RESOURCE_USAGE_UAV), + FFX_SURFACE_FORMAT_R32_FLOAT, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_ALIASABLE}, - { FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_STATUS_2, L"FSR2_LockStatus2", (FfxResourceUsage)(FFX_RESOURCE_USAGE_RENDERTARGET | FFX_RESOURCE_USAGE_UAV), - FFX_SURFACE_FORMAT_R16G16_FLOAT, contextDescription->displaySize.width, contextDescription->displaySize.height, 1, FFX_RESOURCE_FLAGS_NONE }, + {FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_STATUS_1, L"FSR2_LockStatus1", (FfxResourceUsage)(FFX_RESOURCE_USAGE_RENDERTARGET | FFX_RESOURCE_USAGE_UAV), + FFX_SURFACE_FORMAT_R16G16_FLOAT, contextDescription->displaySize.width, contextDescription->displaySize.height, 1, FFX_RESOURCE_FLAGS_NONE}, - { FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_INPUT_LUMA, L"FSR2_LockInputLuma", (FfxResourceUsage)(FFX_RESOURCE_USAGE_UAV), - FFX_SURFACE_FORMAT_R16_FLOAT, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_ALIASABLE }, + {FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_STATUS_2, L"FSR2_LockStatus2", (FfxResourceUsage)(FFX_RESOURCE_USAGE_RENDERTARGET | FFX_RESOURCE_USAGE_UAV), + FFX_SURFACE_FORMAT_R16G16_FLOAT, contextDescription->displaySize.width, contextDescription->displaySize.height, 1, FFX_RESOURCE_FLAGS_NONE}, - { FFX_FSR2_RESOURCE_IDENTIFIER_NEW_LOCKS, L"FSR2_NewLocks", (FfxResourceUsage)(FFX_RESOURCE_USAGE_UAV), - FFX_SURFACE_FORMAT_R8_UNORM, contextDescription->displaySize.width, contextDescription->displaySize.height, 1, FFX_RESOURCE_FLAGS_ALIASABLE }, + {FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_INPUT_LUMA, L"FSR2_LockInputLuma", (FfxResourceUsage)(FFX_RESOURCE_USAGE_UAV), + FFX_SURFACE_FORMAT_R16_FLOAT, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_ALIASABLE}, - { FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_UPSCALED_COLOR_1, L"FSR2_InternalUpscaled1", (FfxResourceUsage)(FFX_RESOURCE_USAGE_RENDERTARGET | FFX_RESOURCE_USAGE_UAV), - FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT, contextDescription->displaySize.width, contextDescription->displaySize.height, 1, FFX_RESOURCE_FLAGS_NONE }, + {FFX_FSR2_RESOURCE_IDENTIFIER_NEW_LOCKS, L"FSR2_NewLocks", (FfxResourceUsage)(FFX_RESOURCE_USAGE_UAV), + FFX_SURFACE_FORMAT_R8_UNORM, contextDescription->displaySize.width, contextDescription->displaySize.height, 1, FFX_RESOURCE_FLAGS_ALIASABLE}, - { FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_UPSCALED_COLOR_2, L"FSR2_InternalUpscaled2", (FfxResourceUsage)(FFX_RESOURCE_USAGE_RENDERTARGET | FFX_RESOURCE_USAGE_UAV), - FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT, contextDescription->displaySize.width, contextDescription->displaySize.height, 1, FFX_RESOURCE_FLAGS_NONE }, + {FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_UPSCALED_COLOR_1, L"FSR2_InternalUpscaled1", (FfxResourceUsage)(FFX_RESOURCE_USAGE_RENDERTARGET | FFX_RESOURCE_USAGE_UAV), + FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT, contextDescription->displaySize.width, contextDescription->displaySize.height, 1, FFX_RESOURCE_FLAGS_NONE}, - { FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE, L"FSR2_ExposureMips", FFX_RESOURCE_USAGE_UAV, - FFX_SURFACE_FORMAT_R16_FLOAT, contextDescription->maxRenderSize.width / 2, contextDescription->maxRenderSize.height / 2, 0, FFX_RESOURCE_FLAGS_ALIASABLE }, + {FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_UPSCALED_COLOR_2, L"FSR2_InternalUpscaled2", (FfxResourceUsage)(FFX_RESOURCE_USAGE_RENDERTARGET | FFX_RESOURCE_USAGE_UAV), + FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT, contextDescription->displaySize.width, contextDescription->displaySize.height, 1, FFX_RESOURCE_FLAGS_NONE}, - { FFX_FSR2_RESOURCE_IDENTIFIER_LUMA_HISTORY_1, L"FSR2_LumaHistory1", (FfxResourceUsage)(FFX_RESOURCE_USAGE_RENDERTARGET | FFX_RESOURCE_USAGE_UAV), - FFX_SURFACE_FORMAT_R8G8B8A8_UNORM, contextDescription->displaySize.width, contextDescription->displaySize.height, 1, FFX_RESOURCE_FLAGS_NONE }, + {FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE, L"FSR2_ExposureMips", FFX_RESOURCE_USAGE_UAV, + FFX_SURFACE_FORMAT_R16_FLOAT, contextDescription->maxRenderSize.width / 2, contextDescription->maxRenderSize.height / 2, 0, FFX_RESOURCE_FLAGS_ALIASABLE}, - { FFX_FSR2_RESOURCE_IDENTIFIER_LUMA_HISTORY_2, L"FSR2_LumaHistory2", (FfxResourceUsage)(FFX_RESOURCE_USAGE_RENDERTARGET | FFX_RESOURCE_USAGE_UAV), - FFX_SURFACE_FORMAT_R8G8B8A8_UNORM, contextDescription->displaySize.width, contextDescription->displaySize.height, 1, FFX_RESOURCE_FLAGS_NONE }, + {FFX_FSR2_RESOURCE_IDENTIFIER_LUMA_HISTORY_1, L"FSR2_LumaHistory1", (FfxResourceUsage)(FFX_RESOURCE_USAGE_RENDERTARGET | FFX_RESOURCE_USAGE_UAV), + FFX_SURFACE_FORMAT_R8G8B8A8_UNORM, contextDescription->displaySize.width, contextDescription->displaySize.height, 1, FFX_RESOURCE_FLAGS_NONE}, - { FFX_FSR2_RESOURCE_IDENTIFIER_SPD_ATOMIC_COUNT, L"FSR2_SpdAtomicCounter", (FfxResourceUsage)(FFX_RESOURCE_USAGE_UAV), - FFX_SURFACE_FORMAT_R32_UINT, 1, 1, 1, FFX_RESOURCE_FLAGS_ALIASABLE, sizeof(atomicInitData), &atomicInitData }, + {FFX_FSR2_RESOURCE_IDENTIFIER_LUMA_HISTORY_2, L"FSR2_LumaHistory2", (FfxResourceUsage)(FFX_RESOURCE_USAGE_RENDERTARGET | FFX_RESOURCE_USAGE_UAV), + FFX_SURFACE_FORMAT_R8G8B8A8_UNORM, contextDescription->displaySize.width, contextDescription->displaySize.height, 1, FFX_RESOURCE_FLAGS_NONE}, - { FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_REACTIVE_MASKS, L"FSR2_DilatedReactiveMasks", FFX_RESOURCE_USAGE_UAV, - FFX_SURFACE_FORMAT_R8G8_UNORM, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_ALIASABLE }, + {FFX_FSR2_RESOURCE_IDENTIFIER_SPD_ATOMIC_COUNT, L"FSR2_SpdAtomicCounter", (FfxResourceUsage)(FFX_RESOURCE_USAGE_UAV), + FFX_SURFACE_FORMAT_R32_UINT, 1, 1, 1, FFX_RESOURCE_FLAGS_ALIASABLE, sizeof(atomicInitData), &atomicInitData}, - { FFX_FSR2_RESOURCE_IDENTIFIER_LANCZOS_LUT, L"FSR2_LanczosLutData", FFX_RESOURCE_USAGE_READ_ONLY, - FFX_SURFACE_FORMAT_R16_SNORM, lanczos2LutWidth, 1, 1, FFX_RESOURCE_FLAGS_NONE, sizeof(lanczos2Weights), lanczos2Weights }, + {FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_REACTIVE_MASKS, L"FSR2_DilatedReactiveMasks", FFX_RESOURCE_USAGE_UAV, + FFX_SURFACE_FORMAT_R8G8_UNORM, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_ALIASABLE}, - { FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_DEFAULT_REACTIVITY, L"FSR2_DefaultReactiviyMask", FFX_RESOURCE_USAGE_READ_ONLY, - FFX_SURFACE_FORMAT_R8_UNORM, 1, 1, 1, FFX_RESOURCE_FLAGS_NONE, sizeof(defaultReactiveMaskData), &defaultReactiveMaskData }, + {FFX_FSR2_RESOURCE_IDENTIFIER_LANCZOS_LUT, L"FSR2_LanczosLutData", FFX_RESOURCE_USAGE_READ_ONLY, + FFX_SURFACE_FORMAT_R16_SNORM, lanczos2LutWidth, 1, 1, FFX_RESOURCE_FLAGS_NONE, sizeof(lanczos2Weights), lanczos2Weights}, - { FFX_FSR2_RESOURCE_IDENTITIER_UPSAMPLE_MAXIMUM_BIAS_LUT, L"FSR2_MaximumUpsampleBias", FFX_RESOURCE_USAGE_READ_ONLY, - FFX_SURFACE_FORMAT_R16_SNORM, FFX_FSR2_MAXIMUM_BIAS_TEXTURE_WIDTH, FFX_FSR2_MAXIMUM_BIAS_TEXTURE_HEIGHT, 1, FFX_RESOURCE_FLAGS_NONE, sizeof(maximumBias), maximumBias }, + {FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_DEFAULT_REACTIVITY, L"FSR2_DefaultReactiviyMask", FFX_RESOURCE_USAGE_READ_ONLY, + FFX_SURFACE_FORMAT_R8_UNORM, 1, 1, 1, FFX_RESOURCE_FLAGS_NONE, sizeof(defaultReactiveMaskData), &defaultReactiveMaskData}, - { FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_DEFAULT_EXPOSURE, L"FSR2_DefaultExposure", FFX_RESOURCE_USAGE_READ_ONLY, - FFX_SURFACE_FORMAT_R32G32_FLOAT, 1, 1, 1, FFX_RESOURCE_FLAGS_NONE, sizeof(defaultExposure), defaultExposure }, + {FFX_FSR2_RESOURCE_IDENTITIER_UPSAMPLE_MAXIMUM_BIAS_LUT, L"FSR2_MaximumUpsampleBias", FFX_RESOURCE_USAGE_READ_ONLY, + FFX_SURFACE_FORMAT_R16_SNORM, FFX_FSR2_MAXIMUM_BIAS_TEXTURE_WIDTH, FFX_FSR2_MAXIMUM_BIAS_TEXTURE_HEIGHT, 1, FFX_RESOURCE_FLAGS_NONE, sizeof(maximumBias), maximumBias}, - { FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE, L"FSR2_AutoExposure", FFX_RESOURCE_USAGE_UAV, - FFX_SURFACE_FORMAT_R32G32_FLOAT, 1, 1, 1, FFX_RESOURCE_FLAGS_NONE }, + {FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_DEFAULT_EXPOSURE, L"FSR2_DefaultExposure", FFX_RESOURCE_USAGE_READ_ONLY, + FFX_SURFACE_FORMAT_R32G32_FLOAT, 1, 1, 1, FFX_RESOURCE_FLAGS_NONE, sizeof(defaultExposure), defaultExposure}, + {FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE, L"FSR2_AutoExposure", FFX_RESOURCE_USAGE_UAV, + FFX_SURFACE_FORMAT_R32G32_FLOAT, 1, 1, 1, FFX_RESOURCE_FLAGS_NONE}, // only one for now, will need pingpont to respect the motion vectors - { FFX_FSR2_RESOURCE_IDENTIFIER_AUTOREACTIVE, L"FSR2_AutoReactive", FFX_RESOURCE_USAGE_UAV, - FFX_SURFACE_FORMAT_R8_UNORM, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_NONE }, - { FFX_FSR2_RESOURCE_IDENTIFIER_AUTOCOMPOSITION, L"FSR2_AutoComposition", FFX_RESOURCE_USAGE_UAV, - FFX_SURFACE_FORMAT_R8_UNORM, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_NONE }, - { FFX_FSR2_RESOURCE_IDENTIFIER_PREV_PRE_ALPHA_COLOR_1, L"FSR2_PrevPreAlpha0", FFX_RESOURCE_USAGE_UAV, - FFX_SURFACE_FORMAT_R11G11B10_FLOAT, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_NONE }, - { FFX_FSR2_RESOURCE_IDENTIFIER_PREV_POST_ALPHA_COLOR_1, L"FSR2_PrevPostAlpha0", FFX_RESOURCE_USAGE_UAV, - FFX_SURFACE_FORMAT_R11G11B10_FLOAT, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_NONE }, - { FFX_FSR2_RESOURCE_IDENTIFIER_PREV_PRE_ALPHA_COLOR_2, L"FSR2_PrevPreAlpha1", FFX_RESOURCE_USAGE_UAV, - FFX_SURFACE_FORMAT_R11G11B10_FLOAT, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_NONE }, - { FFX_FSR2_RESOURCE_IDENTIFIER_PREV_POST_ALPHA_COLOR_2, L"FSR2_PrevPostAlpha1", FFX_RESOURCE_USAGE_UAV, - FFX_SURFACE_FORMAT_R11G11B10_FLOAT, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_NONE }, + {FFX_FSR2_RESOURCE_IDENTIFIER_AUTOREACTIVE, L"FSR2_AutoReactive", FFX_RESOURCE_USAGE_UAV, + FFX_SURFACE_FORMAT_R8_UNORM, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_NONE}, + {FFX_FSR2_RESOURCE_IDENTIFIER_AUTOCOMPOSITION, L"FSR2_AutoComposition", FFX_RESOURCE_USAGE_UAV, + FFX_SURFACE_FORMAT_R8_UNORM, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_NONE}, + {FFX_FSR2_RESOURCE_IDENTIFIER_PREV_PRE_ALPHA_COLOR_1, L"FSR2_PrevPreAlpha0", FFX_RESOURCE_USAGE_UAV, + FFX_SURFACE_FORMAT_R11G11B10_FLOAT, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_NONE}, + {FFX_FSR2_RESOURCE_IDENTIFIER_PREV_POST_ALPHA_COLOR_1, L"FSR2_PrevPostAlpha0", FFX_RESOURCE_USAGE_UAV, + FFX_SURFACE_FORMAT_R11G11B10_FLOAT, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_NONE}, + {FFX_FSR2_RESOURCE_IDENTIFIER_PREV_PRE_ALPHA_COLOR_2, L"FSR2_PrevPreAlpha1", FFX_RESOURCE_USAGE_UAV, + FFX_SURFACE_FORMAT_R11G11B10_FLOAT, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_NONE}, + {FFX_FSR2_RESOURCE_IDENTIFIER_PREV_POST_ALPHA_COLOR_2, L"FSR2_PrevPostAlpha1", FFX_RESOURCE_USAGE_UAV, + FFX_SURFACE_FORMAT_R11G11B10_FLOAT, contextDescription->maxRenderSize.width, contextDescription->maxRenderSize.height, 1, FFX_RESOURCE_FLAGS_NONE}, }; // clear the SRV resources to NULL. memset(context->srvResources, 0, sizeof(context->srvResources)); - for (int32_t currentSurfaceIndex = 0; currentSurfaceIndex < FFX_ARRAY_ELEMENTS(internalSurfaceDesc); ++currentSurfaceIndex) { + for (int32_t currentSurfaceIndex = 0; currentSurfaceIndex < FFX_ARRAY_ELEMENTS(internalSurfaceDesc); ++currentSurfaceIndex) + { - const Fsr2ResourceDescription* currentSurfaceDescription = &internalSurfaceDesc[currentSurfaceIndex]; + const Fsr2ResourceDescription *currentSurfaceDescription = &internalSurfaceDesc[currentSurfaceIndex]; const FfxResourceType resourceType = currentSurfaceDescription->height > 1 ? FFX_RESOURCE_TYPE_TEXTURE2D : texture1dResourceType; - const FfxResourceDescription resourceDescription = { resourceType, currentSurfaceDescription->format, currentSurfaceDescription->width, currentSurfaceDescription->height, 1, currentSurfaceDescription->mipCount }; + const FfxResourceDescription resourceDescription = {resourceType, currentSurfaceDescription->format, currentSurfaceDescription->width, currentSurfaceDescription->height, 1, currentSurfaceDescription->mipCount}; const FfxResourceStates initialState = (currentSurfaceDescription->usage == FFX_RESOURCE_USAGE_READ_ONLY) ? FFX_RESOURCE_STATE_COMPUTE_READ : FFX_RESOURCE_STATE_UNORDERED_ACCESS; - const FfxCreateResourceDescription createResourceDescription = { FFX_HEAP_TYPE_DEFAULT, resourceDescription, initialState, currentSurfaceDescription->initDataSize, currentSurfaceDescription->initData, currentSurfaceDescription->name, currentSurfaceDescription->usage, currentSurfaceDescription->id }; + const FfxCreateResourceDescription createResourceDescription = {FFX_HEAP_TYPE_DEFAULT, resourceDescription, initialState, currentSurfaceDescription->initDataSize, currentSurfaceDescription->initData, currentSurfaceDescription->name, currentSurfaceDescription->usage, currentSurfaceDescription->id}; FFX_VALIDATE(context->contextDescription.callbacks.fpCreateResource(&context->contextDescription.callbacks, &createResourceDescription, &context->srvResources[currentSurfaceDescription->id])); } @@ -613,21 +618,22 @@ static FfxErrorCode fsr2Create(FfxFsr2Context_Private* context, const FfxFsr2Con return FFX_OK; } -static void fsr2SafeReleasePipeline(FfxFsr2Context_Private* context, FfxPipelineState* pipeline) +static void fsr2SafeReleasePipeline(FfxFsr2Context_Private *context, FfxPipelineState *pipeline) { FFX_ASSERT(pipeline); context->contextDescription.callbacks.fpDestroyPipeline(&context->contextDescription.callbacks, pipeline); } -static void fsr2SafeReleaseResource(FfxFsr2Context_Private* context, FfxResourceInternal resource) +static void fsr2SafeReleaseResource(FfxFsr2Context_Private *context, FfxResourceInternal resource) { context->contextDescription.callbacks.fpDestroyResource(&context->contextDescription.callbacks, resource); } -static void fsr2SafeReleaseDevice(FfxFsr2Context_Private* context, FfxDevice* device) +static void fsr2SafeReleaseDevice(FfxFsr2Context_Private *context, FfxDevice *device) { - if (*device == nullptr && !(context->contextDescription.flags & FFX_FSR2_ALLOW_NULL_DEVICE_AND_COMMAND_LIST)) { + if (*device == nullptr && !(context->contextDescription.flags & FFX_FSR2_ALLOW_NULL_DEVICE_AND_COMMAND_LIST)) + { return; } @@ -635,7 +641,7 @@ static void fsr2SafeReleaseDevice(FfxFsr2Context_Private* context, FfxDevice* de *device = nullptr; } -static FfxErrorCode fsr2Release(FfxFsr2Context_Private* context) +static FfxErrorCode fsr2Release(FfxFsr2Context_Private *context) { FFX_ASSERT(context); @@ -650,20 +656,21 @@ static FfxErrorCode fsr2Release(FfxFsr2Context_Private* context) fsr2SafeReleasePipeline(context, &context->pipelineTcrAutogenerate); // unregister resources not created internally - context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_OPAQUE_ONLY] = { FFX_FSR2_RESOURCE_IDENTIFIER_NULL }; - context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_COLOR] = { FFX_FSR2_RESOURCE_IDENTIFIER_NULL }; - context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_DEPTH] = { FFX_FSR2_RESOURCE_IDENTIFIER_NULL }; - context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_MOTION_VECTORS] = { FFX_FSR2_RESOURCE_IDENTIFIER_NULL }; - context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_EXPOSURE] = { FFX_FSR2_RESOURCE_IDENTIFIER_NULL }; - context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_REACTIVE_MASK] = { FFX_FSR2_RESOURCE_IDENTIFIER_NULL }; - context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_TRANSPARENCY_AND_COMPOSITION_MASK] = { FFX_FSR2_RESOURCE_IDENTIFIER_NULL }; - context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_STATUS] = { FFX_FSR2_RESOURCE_IDENTIFIER_NULL }; - context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_UPSCALED_COLOR] = { FFX_FSR2_RESOURCE_IDENTIFIER_NULL }; - context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_RCAS_INPUT] = { FFX_FSR2_RESOURCE_IDENTIFIER_NULL }; - context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_UPSCALED_OUTPUT] = { FFX_FSR2_RESOURCE_IDENTIFIER_NULL }; + context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_OPAQUE_ONLY] = {FFX_FSR2_RESOURCE_IDENTIFIER_NULL}; + context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_COLOR] = {FFX_FSR2_RESOURCE_IDENTIFIER_NULL}; + context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_DEPTH] = {FFX_FSR2_RESOURCE_IDENTIFIER_NULL}; + context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_MOTION_VECTORS] = {FFX_FSR2_RESOURCE_IDENTIFIER_NULL}; + context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_EXPOSURE] = {FFX_FSR2_RESOURCE_IDENTIFIER_NULL}; + context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_REACTIVE_MASK] = {FFX_FSR2_RESOURCE_IDENTIFIER_NULL}; + context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_TRANSPARENCY_AND_COMPOSITION_MASK] = {FFX_FSR2_RESOURCE_IDENTIFIER_NULL}; + context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_STATUS] = {FFX_FSR2_RESOURCE_IDENTIFIER_NULL}; + context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_UPSCALED_COLOR] = {FFX_FSR2_RESOURCE_IDENTIFIER_NULL}; + context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_RCAS_INPUT] = {FFX_FSR2_RESOURCE_IDENTIFIER_NULL}; + context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_UPSCALED_OUTPUT] = {FFX_FSR2_RESOURCE_IDENTIFIER_NULL}; // release internal resources - for (int32_t currentResourceIndex = 0; currentResourceIndex < FFX_FSR2_RESOURCE_IDENTIFIER_COUNT; ++currentResourceIndex) { + for (int32_t currentResourceIndex = 0; currentResourceIndex < FFX_FSR2_RESOURCE_IDENTIFIER_COUNT; ++currentResourceIndex) + { fsr2SafeReleaseResource(context, context->srvResources[currentResourceIndex]); } @@ -673,7 +680,7 @@ static FfxErrorCode fsr2Release(FfxFsr2Context_Private* context) return FFX_OK; } -static void setupDeviceDepthToViewSpaceDepthParams(FfxFsr2Context_Private* context, const FfxFsr2DispatchDescription* params) +static void setupDeviceDepthToViewSpaceDepthParams(FfxFsr2Context_Private *context, const FfxFsr2DispatchDescription *params) { FFX_ASSERT_MESSAGE(params->deviceDepthNegativeOneToOne == false, "OpenGL depth convention not yet supported"); const bool bInverted = (context->contextDescription.flags & FFX_FSR2_ENABLE_DEPTH_INVERTED) == FFX_FSR2_ENABLE_DEPTH_INVERTED; @@ -684,7 +691,8 @@ static void setupDeviceDepthToViewSpaceDepthParams(FfxFsr2Context_Private* conte float fMin = FFX_MINIMUM(params->cameraNear, params->cameraFar); float fMax = FFX_MAXIMUM(params->cameraNear, params->cameraFar); - if (bInverted) { + if (bInverted) + { float tmp = fMin; fMin = fMax; fMax = tmp; @@ -699,17 +707,17 @@ static void setupDeviceDepthToViewSpaceDepthParams(FfxFsr2Context_Private* conte const float d = -1.0f; // for clarity const float matrix_elem_c[2][2] = { - fQ, // non reversed, non infinite - -1.0f - FLT_EPSILON, // non reversed, infinite - fQ, // reversed, non infinite - 0.0f + FLT_EPSILON // reversed, infinite + fQ, // non reversed, non infinite + -1.0f - FLT_EPSILON, // non reversed, infinite + fQ, // reversed, non infinite + 0.0f + FLT_EPSILON // reversed, infinite }; const float matrix_elem_e[2][2] = { - fQ * fMin, // non reversed, non infinite - -fMin - FLT_EPSILON, // non reversed, infinite - fQ * fMin, // reversed, non infinite - fMax, // reversed, infinite + fQ * fMin, // non reversed, non infinite + -fMin - FLT_EPSILON, // non reversed, infinite + fQ * fMin, // reversed, non infinite + fMax, // reversed, infinite }; context->constants.deviceToViewDepth[0] = d * matrix_elem_c[bInverted][bInfinite]; @@ -725,33 +733,34 @@ static void setupDeviceDepthToViewSpaceDepthParams(FfxFsr2Context_Private* conte context->constants.deviceToViewDepth[3] = (1.0f / b); } -static void scheduleDispatch(FfxFsr2Context_Private* context, const FfxFsr2DispatchDescription* params, const FfxPipelineState* pipeline, uint32_t dispatchX, uint32_t dispatchY) +static void scheduleDispatch(FfxFsr2Context_Private *context, const FfxFsr2DispatchDescription *params, const FfxPipelineState *pipeline, uint32_t dispatchX, uint32_t dispatchY) { FfxComputeJobDescription jobDescriptor = {}; - for (uint32_t currentShaderResourceViewIndex = 0; currentShaderResourceViewIndex < pipeline->srvCount; ++currentShaderResourceViewIndex) { + for (uint32_t currentShaderResourceViewIndex = 0; currentShaderResourceViewIndex < pipeline->srvCount; ++currentShaderResourceViewIndex) + { const uint32_t currentResourceId = pipeline->srvResourceBindings[currentShaderResourceViewIndex].resourceIdentifier; const FfxResourceInternal currentResource = context->srvResources[currentResourceId]; jobDescriptor.srvs[currentShaderResourceViewIndex] = currentResource; - #ifdef ON_LINUX - wcscpy(jobDescriptor.srvNames[currentShaderResourceViewIndex], pipeline->srvResourceBindings[currentShaderResourceViewIndex].name); - #else - wcscpy_s(jobDescriptor.srvNames[currentShaderResourceViewIndex], pipeline->srvResourceBindings[currentShaderResourceViewIndex].name); - #endif - +#ifdef ON_LINUX + wcscpy(jobDescriptor.srvNames[currentShaderResourceViewIndex], pipeline->srvResourceBindings[currentShaderResourceViewIndex].name); +#else + wcscpy_s(jobDescriptor.srvNames[currentShaderResourceViewIndex], pipeline->srvResourceBindings[currentShaderResourceViewIndex].name); +#endif } - for (uint32_t currentUnorderedAccessViewIndex = 0; currentUnorderedAccessViewIndex < pipeline->uavCount; ++currentUnorderedAccessViewIndex) { + for (uint32_t currentUnorderedAccessViewIndex = 0; currentUnorderedAccessViewIndex < pipeline->uavCount; ++currentUnorderedAccessViewIndex) + { const uint32_t currentResourceId = pipeline->uavResourceBindings[currentUnorderedAccessViewIndex].resourceIdentifier; - #ifdef ON_LINUX +#ifdef ON_LINUX wcscpy(jobDescriptor.uavNames[currentUnorderedAccessViewIndex], pipeline->uavResourceBindings[currentUnorderedAccessViewIndex].name); - #else +#else wcscpy_s(jobDescriptor.uavNames[currentUnorderedAccessViewIndex], pipeline->uavResourceBindings[currentUnorderedAccessViewIndex].name); - #endif +#endif if (currentResourceId >= FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_0 && currentResourceId <= FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_12) { @@ -766,41 +775,43 @@ static void scheduleDispatch(FfxFsr2Context_Private* context, const FfxFsr2Dispa jobDescriptor.uavMip[currentUnorderedAccessViewIndex] = 0; } } - + jobDescriptor.dimensions[0] = dispatchX; jobDescriptor.dimensions[1] = dispatchY; jobDescriptor.dimensions[2] = 1; jobDescriptor.pipeline = *pipeline; - for (uint32_t currentRootConstantIndex = 0; currentRootConstantIndex < pipeline->constCount; ++currentRootConstantIndex) { - #ifdef ON_LINUX - wcscpy( jobDescriptor.cbNames[currentRootConstantIndex], pipeline->cbResourceBindings[currentRootConstantIndex].name); + for (uint32_t currentRootConstantIndex = 0; currentRootConstantIndex < pipeline->constCount; ++currentRootConstantIndex) + { +#ifdef ON_LINUX + wcscpy(jobDescriptor.cbNames[currentRootConstantIndex], pipeline->cbResourceBindings[currentRootConstantIndex].name); - #else - wcscpy_s( jobDescriptor.cbNames[currentRootConstantIndex], pipeline->cbResourceBindings[currentRootConstantIndex].name); +#else + wcscpy_s(jobDescriptor.cbNames[currentRootConstantIndex], pipeline->cbResourceBindings[currentRootConstantIndex].name); - #endif +#endif jobDescriptor.cbs[currentRootConstantIndex] = globalFsr2ConstantBuffers[pipeline->cbResourceBindings[currentRootConstantIndex].resourceIdentifier]; jobDescriptor.cbSlotIndex[currentRootConstantIndex] = pipeline->cbResourceBindings[currentRootConstantIndex].slotIndex; } - FfxGpuJobDescription dispatchJob = { FFX_GPU_JOB_COMPUTE }; + FfxGpuJobDescription dispatchJob = {FFX_GPU_JOB_COMPUTE}; dispatchJob.computeJobDescriptor = jobDescriptor; context->contextDescription.callbacks.fpScheduleGpuJob(&context->contextDescription.callbacks, &dispatchJob); } -static FfxErrorCode fsr2Dispatch(FfxFsr2Context_Private* context, const FfxFsr2DispatchDescription* params) +static FfxErrorCode fsr2Dispatch(FfxFsr2Context_Private *context, const FfxFsr2DispatchDescription *params) { - if ((context->contextDescription.flags & FFX_FSR2_ENABLE_DEBUG_CHECKING) == FFX_FSR2_ENABLE_DEBUG_CHECKING) - { + //if ((context->contextDescription.flags & FFX_FSR2_ENABLE_DEBUG_CHECKING) == FFX_FSR2_ENABLE_DEBUG_CHECKING) + //{ fsr2DebugCheckDispatch(context, params); - } + //} // take a short cut to the command list FfxCommandList commandList = params->commandList; // try and refresh shaders first. Early exit in case of error. - if (context->refreshPipelineStates) { + if (context->refreshPipelineStates) + { context->refreshPipelineStates = false; @@ -810,9 +821,9 @@ static FfxErrorCode fsr2Dispatch(FfxFsr2Context_Private* context, const FfxFsr2D if (context->firstExecution) { - FfxGpuJobDescription clearJob = { FFX_GPU_JOB_CLEAR_FLOAT }; + FfxGpuJobDescription clearJob = {FFX_GPU_JOB_CLEAR_FLOAT}; - const float clearValuesToZeroFloat[]{ 0.f, 0.f, 0.f, 0.f }; + const float clearValuesToZeroFloat[]{0.f, 0.f, 0.f, 0.f}; memcpy(clearJob.clearJobDescriptor.color, clearValuesToZeroFloat, 4 * sizeof(float)); clearJob.clearJobDescriptor.target = context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_STATUS_1]; @@ -849,31 +860,42 @@ static FfxErrorCode fsr2Dispatch(FfxFsr2Context_Private* context, const FfxFsr2D context->contextDescription.callbacks.fpRegisterResource(&context->contextDescription.callbacks, ¶ms->motionVectors, &context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_MOTION_VECTORS]); // if auto exposure is enabled use the auto exposure SRV, otherwise what the app sends. - if (context->contextDescription.flags & FFX_FSR2_ENABLE_AUTO_EXPOSURE) { + if (context->contextDescription.flags & FFX_FSR2_ENABLE_AUTO_EXPOSURE) + { context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_EXPOSURE] = context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE]; - } else { - if (ffxFsr2ResourceIsNull(params->exposure)) { + } + else + { + if (ffxFsr2ResourceIsNull(params->exposure)) + { context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_EXPOSURE] = context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_DEFAULT_EXPOSURE]; - } else { + } + else + { context->contextDescription.callbacks.fpRegisterResource(&context->contextDescription.callbacks, ¶ms->exposure, &context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_EXPOSURE]); } } - + if (params->enableAutoReactive) { context->contextDescription.callbacks.fpRegisterResource(&context->contextDescription.callbacks, ¶ms->colorOpaqueOnly, &context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_PREV_PRE_ALPHA_COLOR]); } - - if (ffxFsr2ResourceIsNull(params->reactive)) { + + if (ffxFsr2ResourceIsNull(params->reactive)) + { context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_REACTIVE_MASK] = context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_DEFAULT_REACTIVITY]; } - else { + else + { context->contextDescription.callbacks.fpRegisterResource(&context->contextDescription.callbacks, ¶ms->reactive, &context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_REACTIVE_MASK]); } - - if (ffxFsr2ResourceIsNull(params->transparencyAndComposition)) { + + if (ffxFsr2ResourceIsNull(params->transparencyAndComposition)) + { context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_TRANSPARENCY_AND_COMPOSITION_MASK] = context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_DEFAULT_REACTIVITY]; - } else { + } + else + { context->contextDescription.callbacks.fpRegisterResource(&context->contextDescription.callbacks, ¶ms->transparencyAndComposition, &context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_TRANSPARENCY_AND_COMPOSITION_MASK]); } @@ -891,8 +913,8 @@ static FfxErrorCode fsr2Dispatch(FfxFsr2Context_Private* context, const FfxFsr2D context->uavResources[FFX_FSR2_RESOURCE_IDENTIFIER_LUMA_HISTORY] = context->uavResources[lumaHistoryUavResourceIndex]; context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_LUMA_HISTORY] = context->srvResources[lumaHistorySrvResourceIndex]; - context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_PREV_PRE_ALPHA_COLOR] = context->srvResources[prevPreAlphaColorSrvResourceIndex]; - context->uavResources[FFX_FSR2_RESOURCE_IDENTIFIER_PREV_PRE_ALPHA_COLOR] = context->uavResources[prevPreAlphaColorUavResourceIndex]; + context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_PREV_PRE_ALPHA_COLOR] = context->srvResources[prevPreAlphaColorSrvResourceIndex]; + context->uavResources[FFX_FSR2_RESOURCE_IDENTIFIER_PREV_PRE_ALPHA_COLOR] = context->uavResources[prevPreAlphaColorUavResourceIndex]; context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_PREV_POST_ALPHA_COLOR] = context->srvResources[prevPostAlphaColorSrvResourceIndex]; context->uavResources[FFX_FSR2_RESOURCE_IDENTIFIER_PREV_POST_ALPHA_COLOR] = context->uavResources[prevPostAlphaColorUavResourceIndex]; @@ -905,7 +927,7 @@ static FfxErrorCode fsr2Dispatch(FfxFsr2Context_Private* context, const FfxFsr2D context->constants.jitterOffset[0] = params->jitterOffset.x; context->constants.jitterOffset[1] = params->jitterOffset.y; - context->constants.renderSize[0] = int32_t(params->renderSize.width ? params->renderSize.width : resourceDescInputColor.width); + context->constants.renderSize[0] = int32_t(params->renderSize.width ? params->renderSize.width : resourceDescInputColor.width); context->constants.renderSize[1] = int32_t(params->renderSize.height ? params->renderSize.height : resourceDescInputColor.height); context->constants.maxRenderSize[0] = int32_t(context->contextDescription.maxRenderSize.width); context->constants.maxRenderSize[1] = int32_t(context->contextDescription.maxRenderSize.height); @@ -928,13 +950,14 @@ static FfxErrorCode fsr2Dispatch(FfxFsr2Context_Private* context, const FfxFsr2D context->constants.preExposure = (params->preExposure != 0) ? params->preExposure : 1.0f; // motion vector data - const int32_t* motionVectorsTargetSize = (context->contextDescription.flags & FFX_FSR2_ENABLE_DISPLAY_RESOLUTION_MOTION_VECTORS) ? context->constants.displaySize : context->constants.renderSize; + const int32_t *motionVectorsTargetSize = (context->contextDescription.flags & FFX_FSR2_ENABLE_DISPLAY_RESOLUTION_MOTION_VECTORS) ? context->constants.displaySize : context->constants.renderSize; context->constants.motionVectorScale[0] = (params->motionVectorScale.x / motionVectorsTargetSize[0]); context->constants.motionVectorScale[1] = (params->motionVectorScale.y / motionVectorsTargetSize[1]); // compute jitter cancellation - if (context->contextDescription.flags & FFX_FSR2_ENABLE_MOTION_VECTORS_JITTER_CANCELLATION) { + if (context->contextDescription.flags & FFX_FSR2_ENABLE_MOTION_VECTORS_JITTER_CANCELLATION) + { context->constants.motionVectorJitterCancellation[0] = (context->previousJitterOffset[0] - context->constants.jitterOffset[0]) / motionVectorsTargetSize[0]; context->constants.motionVectorJitterCancellation[1] = (context->previousJitterOffset[1] - context->constants.jitterOffset[1]) / motionVectorsTargetSize[1]; @@ -947,13 +970,19 @@ static FfxErrorCode fsr2Dispatch(FfxFsr2Context_Private* context, const FfxFsr2D const int32_t jitterPhaseCount = ffxFsr2GetJitterPhaseCount(params->renderSize.width, context->contextDescription.displaySize.width); // init on first frame - if (resetAccumulation || context->constants.jitterPhaseCount == 0) { + if (resetAccumulation || context->constants.jitterPhaseCount == 0) + { context->constants.jitterPhaseCount = (float)jitterPhaseCount; - } else { + } + else + { const int32_t jitterPhaseCountDelta = (int32_t)(jitterPhaseCount - context->constants.jitterPhaseCount); - if (jitterPhaseCountDelta > 0) { + if (jitterPhaseCountDelta > 0) + { context->constants.jitterPhaseCount++; - } else if (jitterPhaseCountDelta < 0) { + } + else if (jitterPhaseCountDelta < 0) + { context->constants.jitterPhaseCount--; } } @@ -961,9 +990,12 @@ static FfxErrorCode fsr2Dispatch(FfxFsr2Context_Private* context, const FfxFsr2D // convert delta time to seconds and clamp to [0, 1]. context->constants.deltaTime = FFX_MAXIMUM(0.0f, FFX_MINIMUM(1.0f, params->frameTimeDelta / 1000.0f)); - if (resetAccumulation) { + if (resetAccumulation) + { context->constants.frameIndex = 0; - } else { + } + else + { context->constants.frameIndex++; } @@ -982,9 +1014,10 @@ static FfxErrorCode fsr2Dispatch(FfxFsr2Context_Private* context, const FfxFsr2D const int32_t dispatchDstY = (context->contextDescription.displaySize.height + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim; // Clear reconstructed depth for max depth store. - if (resetAccumulation) { + if (resetAccumulation) + { - FfxGpuJobDescription clearJob = { FFX_GPU_JOB_CLEAR_FLOAT }; + FfxGpuJobDescription clearJob = {FFX_GPU_JOB_CLEAR_FLOAT}; // LockStatus resource has no sign bit, callback functions are compensating for this. // Clearing the resource must follow the same logic. @@ -996,7 +1029,7 @@ static FfxErrorCode fsr2Dispatch(FfxFsr2Context_Private* context, const FfxFsr2D clearJob.clearJobDescriptor.target = context->srvResources[lockStatusSrvResourceIndex]; context->contextDescription.callbacks.fpScheduleGpuJob(&context->contextDescription.callbacks, &clearJob); - const float clearValuesToZeroFloat[]{ 0.f, 0.f, 0.f, 0.f }; + const float clearValuesToZeroFloat[]{0.f, 0.f, 0.f, 0.f}; memcpy(clearJob.clearJobDescriptor.color, clearValuesToZeroFloat, 4 * sizeof(float)); clearJob.clearJobDescriptor.target = context->srvResources[upscaledColorSrvResourceIndex]; context->contextDescription.callbacks.fpScheduleGpuJob(&context->contextDescription.callbacks, &clearJob); @@ -1004,10 +1037,10 @@ static FfxErrorCode fsr2Dispatch(FfxFsr2Context_Private* context, const FfxFsr2D clearJob.clearJobDescriptor.target = context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE]; context->contextDescription.callbacks.fpScheduleGpuJob(&context->contextDescription.callbacks, &clearJob); - //if (context->contextDescription.flags & FFX_FSR2_ENABLE_AUTO_EXPOSURE) - // Auto exposure always used to track luma changes in locking logic + // if (context->contextDescription.flags & FFX_FSR2_ENABLE_AUTO_EXPOSURE) + // Auto exposure always used to track luma changes in locking logic { - const float clearValuesExposure[]{ -1.f, 1e8f, 0.f, 0.f }; + const float clearValuesExposure[]{-1.f, 1e8f, 0.f, 0.f}; memcpy(clearJob.clearJobDescriptor.color, clearValuesExposure, 4 * sizeof(float)); clearJob.clearJobDescriptor.target = context->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE]; context->contextDescription.callbacks.fpScheduleGpuJob(&context->contextDescription.callbacks, &clearJob); @@ -1018,7 +1051,7 @@ static FfxErrorCode fsr2Dispatch(FfxFsr2Context_Private* context, const FfxFsr2D uint32_t dispatchThreadGroupCountXY[2]; uint32_t workGroupOffset[2]; uint32_t numWorkGroupsAndMips[2]; - uint32_t rectInfo[4] = { 0, 0, params->renderSize.width, params->renderSize.height }; + uint32_t rectInfo[4] = {0, 0, params->renderSize.width, params->renderSize.height}; SpdSetup(dispatchThreadGroupCountXY, workGroupOffset, numWorkGroupsAndMips, rectInfo); // downsample @@ -1042,10 +1075,10 @@ static FfxErrorCode fsr2Dispatch(FfxFsr2Context_Private* context, const FfxFsr2D genReactiveConsts.autoReactiveMax = params->autoReactiveMax; // initialize constantBuffers data - memcpy(&globalFsr2ConstantBuffers[FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_FSR2].data, &context->constants, globalFsr2ConstantBuffers[FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_FSR2].uint32Size * sizeof(uint32_t)); - memcpy(&globalFsr2ConstantBuffers[FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_SPD].data, &luminancePyramidConstants, globalFsr2ConstantBuffers[FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_SPD].uint32Size * sizeof(uint32_t)); - memcpy(&globalFsr2ConstantBuffers[FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_RCAS].data, &rcasConsts, globalFsr2ConstantBuffers[FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_RCAS].uint32Size * sizeof(uint32_t)); - memcpy(&globalFsr2ConstantBuffers[FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_GENREACTIVE].data, &genReactiveConsts, globalFsr2ConstantBuffers[FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_GENREACTIVE].uint32Size * sizeof(uint32_t)); + memcpy(&globalFsr2ConstantBuffers[FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_FSR2].data, &context->constants, globalFsr2ConstantBuffers[FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_FSR2].uint32Size * sizeof(uint32_t)); + memcpy(&globalFsr2ConstantBuffers[FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_SPD].data, &luminancePyramidConstants, globalFsr2ConstantBuffers[FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_SPD].uint32Size * sizeof(uint32_t)); + memcpy(&globalFsr2ConstantBuffers[FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_RCAS].data, &rcasConsts, globalFsr2ConstantBuffers[FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_RCAS].uint32Size * sizeof(uint32_t)); + memcpy(&globalFsr2ConstantBuffers[FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_GENREACTIVE].data, &genReactiveConsts, globalFsr2ConstantBuffers[FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_GENREACTIVE].uint32Size * sizeof(uint32_t)); // Auto reactive if (params->enableAutoReactive) @@ -1064,7 +1097,8 @@ static FfxErrorCode fsr2Dispatch(FfxFsr2Context_Private* context, const FfxFsr2D scheduleDispatch(context, params, sharpenEnabled ? &context->pipelineAccumulateSharpen : &context->pipelineAccumulate, dispatchDstX, dispatchDstY); // RCAS - if (sharpenEnabled) { + if (sharpenEnabled) + { // dispatch RCAS const int32_t threadGroupWorkRegionDimRCAS = 16; @@ -1086,7 +1120,7 @@ static FfxErrorCode fsr2Dispatch(FfxFsr2Context_Private* context, const FfxFsr2D return FFX_OK; } -FfxErrorCode ffxFsr2ContextCreate(FfxFsr2Context* context, const FfxFsr2ContextDescription* contextDescription) +FfxErrorCode ffxFsr2ContextCreate(FfxFsr2Context *context, const FfxFsr2ContextDescription *contextDescription) { // zero context memory memset(context, 0, sizeof(FfxFsr2Context)); @@ -1105,7 +1139,8 @@ FfxErrorCode ffxFsr2ContextCreate(FfxFsr2Context* context, const FfxFsr2ContextD FFX_RETURN_ON_ERROR(contextDescription->callbacks.fpDestroyBackendContext, FFX_ERROR_INCOMPLETE_INTERFACE); // if a scratch buffer is declared, then we must have a size - if (contextDescription->callbacks.scratchBuffer) { + if (contextDescription->callbacks.scratchBuffer) + { FFX_RETURN_ON_ERROR(contextDescription->callbacks.scratchBufferSize, FFX_ERROR_INCOMPLETE_INTERFACE); } @@ -1114,25 +1149,25 @@ FfxErrorCode ffxFsr2ContextCreate(FfxFsr2Context* context, const FfxFsr2ContextD FFX_STATIC_ASSERT(sizeof(FfxFsr2Context) >= sizeof(FfxFsr2Context_Private)); // create the context. - FfxFsr2Context_Private* contextPrivate = (FfxFsr2Context_Private*)(context); + FfxFsr2Context_Private *contextPrivate = (FfxFsr2Context_Private *)(context); const FfxErrorCode errorCode = fsr2Create(contextPrivate, contextDescription); return errorCode; } -FfxErrorCode ffxFsr2ContextDestroy(FfxFsr2Context* context) +FfxErrorCode ffxFsr2ContextDestroy(FfxFsr2Context *context) { FFX_RETURN_ON_ERROR( context, FFX_ERROR_INVALID_POINTER); // destroy the context. - FfxFsr2Context_Private* contextPrivate = (FfxFsr2Context_Private*)(context); + FfxFsr2Context_Private *contextPrivate = (FfxFsr2Context_Private *)(context); const FfxErrorCode errorCode = fsr2Release(contextPrivate); return errorCode; } -FfxErrorCode ffxFsr2ContextDispatch(FfxFsr2Context* context, const FfxFsr2DispatchDescription* dispatchParams) +FfxErrorCode ffxFsr2ContextDispatch(FfxFsr2Context *context, const FfxFsr2DispatchDescription *dispatchParams) { FFX_RETURN_ON_ERROR( context, @@ -1141,7 +1176,7 @@ FfxErrorCode ffxFsr2ContextDispatch(FfxFsr2Context* context, const FfxFsr2Dispat dispatchParams, FFX_ERROR_INVALID_POINTER); - FfxFsr2Context_Private* contextPrivate = (FfxFsr2Context_Private*)(context); + FfxFsr2Context_Private *contextPrivate = (FfxFsr2Context_Private *)(context); // validate that renderSize is within the maximum. FFX_RETURN_ON_ERROR( @@ -1152,9 +1187,9 @@ FfxErrorCode ffxFsr2ContextDispatch(FfxFsr2Context* context, const FfxFsr2Dispat FFX_ERROR_OUT_OF_RANGE); if (!(contextPrivate->contextDescription.flags & FFX_FSR2_ALLOW_NULL_DEVICE_AND_COMMAND_LIST)) { - FFX_RETURN_ON_ERROR( - contextPrivate->device, - FFX_ERROR_NULL_DEVICE); + FFX_RETURN_ON_ERROR( + contextPrivate->device, + FFX_ERROR_NULL_DEVICE); } // dispatch the FSR2 passes. @@ -1164,7 +1199,8 @@ FfxErrorCode ffxFsr2ContextDispatch(FfxFsr2Context* context, const FfxFsr2Dispat float ffxFsr2GetUpscaleRatioFromQualityMode(FfxFsr2QualityMode qualityMode) { - switch (qualityMode) { + switch (qualityMode) + { case FFX_FSR2_QUALITY_MODE_QUALITY: return 1.5f; @@ -1180,8 +1216,8 @@ float ffxFsr2GetUpscaleRatioFromQualityMode(FfxFsr2QualityMode qualityMode) } FfxErrorCode ffxFsr2GetRenderResolutionFromQualityMode( - uint32_t* renderWidth, - uint32_t* renderHeight, + uint32_t *renderWidth, + uint32_t *renderHeight, uint32_t displayWidth, uint32_t displayHeight, FfxFsr2QualityMode qualityMode) @@ -1206,13 +1242,13 @@ FfxErrorCode ffxFsr2GetRenderResolutionFromQualityMode( return FFX_OK; } -FfxErrorCode ffxFsr2ContextEnqueueRefreshPipelineRequest(FfxFsr2Context* context) +FfxErrorCode ffxFsr2ContextEnqueueRefreshPipelineRequest(FfxFsr2Context *context) { FFX_RETURN_ON_ERROR( context, FFX_ERROR_INVALID_POINTER); - FfxFsr2Context_Private* contextPrivate = (FfxFsr2Context_Private*)context; + FfxFsr2Context_Private *contextPrivate = (FfxFsr2Context_Private *)context; contextPrivate->refreshPipelineStates = true; return FFX_OK; @@ -1225,7 +1261,7 @@ int32_t ffxFsr2GetJitterPhaseCount(int32_t renderWidth, int32_t displayWidth) return jitterPhaseCount; } -FfxErrorCode ffxFsr2GetJitterOffset(float* outX, float* outY, int32_t index, int32_t phaseCount) +FfxErrorCode ffxFsr2GetJitterOffset(float *outX, float *outY, int32_t index, int32_t phaseCount) { FFX_RETURN_ON_ERROR( outX, @@ -1250,7 +1286,7 @@ FFX_API bool ffxFsr2ResourceIsNull(FfxResource resource) return resource.resource == NULL; } -FfxErrorCode ffxFsr2ContextGenerateReactiveMask(FfxFsr2Context* context, const FfxFsr2GenerateReactiveDescription* params) +FfxErrorCode ffxFsr2ContextGenerateReactiveMask(FfxFsr2Context *context, const FfxFsr2GenerateReactiveDescription *params) { FFX_RETURN_ON_ERROR( context, @@ -1262,16 +1298,17 @@ FfxErrorCode ffxFsr2ContextGenerateReactiveMask(FfxFsr2Context* context, const F params->commandList, FFX_ERROR_INVALID_POINTER); - FfxFsr2Context_Private* contextPrivate = (FfxFsr2Context_Private*)(context); + FfxFsr2Context_Private *contextPrivate = (FfxFsr2Context_Private *)(context); if (!(contextPrivate->contextDescription.flags & FFX_FSR2_ALLOW_NULL_DEVICE_AND_COMMAND_LIST)) { - FFX_RETURN_ON_ERROR( - contextPrivate->device, - FFX_ERROR_NULL_DEVICE); + FFX_RETURN_ON_ERROR( + contextPrivate->device, + FFX_ERROR_NULL_DEVICE); } - if (contextPrivate->refreshPipelineStates) { + if (contextPrivate->refreshPipelineStates) + { createPipelineStates(contextPrivate); contextPrivate->refreshPipelineStates = false; @@ -1280,10 +1317,10 @@ FfxErrorCode ffxFsr2ContextGenerateReactiveMask(FfxFsr2Context* context, const F // take a short cut to the command list FfxCommandList commandList = params->commandList; - FfxPipelineState* pipeline = &contextPrivate->pipelineGenerateReactive; + FfxPipelineState *pipeline = &contextPrivate->pipelineGenerateReactive; const int32_t threadGroupWorkRegionDim = 8; - const int32_t dispatchSrcX = (params->renderSize.width + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim; + const int32_t dispatchSrcX = (params->renderSize.width + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim; const int32_t dispatchSrcY = (params->renderSize.height + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim; // save internal reactive resource @@ -1293,36 +1330,36 @@ FfxErrorCode ffxFsr2ContextGenerateReactiveMask(FfxFsr2Context* context, const F contextPrivate->contextDescription.callbacks.fpRegisterResource(&contextPrivate->contextDescription.callbacks, ¶ms->colorOpaqueOnly, &contextPrivate->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_OPAQUE_ONLY]); contextPrivate->contextDescription.callbacks.fpRegisterResource(&contextPrivate->contextDescription.callbacks, ¶ms->colorPreUpscale, &contextPrivate->srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_COLOR]); contextPrivate->contextDescription.callbacks.fpRegisterResource(&contextPrivate->contextDescription.callbacks, ¶ms->outReactive, &contextPrivate->uavResources[FFX_FSR2_RESOURCE_IDENTIFIER_AUTOREACTIVE]); - + jobDescriptor.uavs[0] = contextPrivate->uavResources[FFX_FSR2_RESOURCE_IDENTIFIER_AUTOREACTIVE]; - #ifdef ON_LINUX - wcscpy(jobDescriptor.srvNames[0], pipeline->srvResourceBindings[0].name); +#ifdef ON_LINUX + wcscpy(jobDescriptor.srvNames[0], pipeline->srvResourceBindings[0].name); wcscpy(jobDescriptor.srvNames[1], pipeline->srvResourceBindings[1].name); wcscpy(jobDescriptor.uavNames[0], pipeline->uavResourceBindings[0].name); - #else - wcscpy_s(jobDescriptor.srvNames[0], pipeline->srvResourceBindings[0].name); +#else + wcscpy_s(jobDescriptor.srvNames[0], pipeline->srvResourceBindings[0].name); wcscpy_s(jobDescriptor.srvNames[1], pipeline->srvResourceBindings[1].name); wcscpy_s(jobDescriptor.uavNames[0], pipeline->uavResourceBindings[0].name); - #endif - +#endif jobDescriptor.dimensions[0] = dispatchSrcX; jobDescriptor.dimensions[1] = dispatchSrcY; jobDescriptor.dimensions[2] = 1; jobDescriptor.pipeline = *pipeline; - for (uint32_t currentShaderResourceViewIndex = 0; currentShaderResourceViewIndex < pipeline->srvCount; ++currentShaderResourceViewIndex) { + for (uint32_t currentShaderResourceViewIndex = 0; currentShaderResourceViewIndex < pipeline->srvCount; ++currentShaderResourceViewIndex) + { const uint32_t currentResourceId = pipeline->srvResourceBindings[currentShaderResourceViewIndex].resourceIdentifier; const FfxResourceInternal currentResource = contextPrivate->srvResources[currentResourceId]; jobDescriptor.srvs[currentShaderResourceViewIndex] = currentResource; - #ifdef ON_LINUX +#ifdef ON_LINUX wcscpy(jobDescriptor.srvNames[currentShaderResourceViewIndex], pipeline->srvResourceBindings[currentShaderResourceViewIndex].name); - #else +#else wcscpy_s(jobDescriptor.srvNames[currentShaderResourceViewIndex], pipeline->srvResourceBindings[currentShaderResourceViewIndex].name); - #endif +#endif } Fsr2GenerateReactiveConstants constants = {}; @@ -1333,14 +1370,13 @@ FfxErrorCode ffxFsr2ContextGenerateReactiveMask(FfxFsr2Context* context, const F jobDescriptor.cbs[0].uint32Size = sizeof(constants); memcpy(&jobDescriptor.cbs[0].data, &constants, sizeof(constants)); - #ifdef ON_LINUX - wcscpy(jobDescriptor.cbNames[0], pipeline->cbResourceBindings[0].name); - #else - wcscpy_s(jobDescriptor.cbNames[0], pipeline->cbResourceBindings[0].name); - #endif - +#ifdef ON_LINUX + wcscpy(jobDescriptor.cbNames[0], pipeline->cbResourceBindings[0].name); +#else + wcscpy_s(jobDescriptor.cbNames[0], pipeline->cbResourceBindings[0].name); +#endif - FfxGpuJobDescription dispatchJob = { FFX_GPU_JOB_COMPUTE }; + FfxGpuJobDescription dispatchJob = {FFX_GPU_JOB_COMPUTE}; dispatchJob.computeJobDescriptor = jobDescriptor; contextPrivate->contextDescription.callbacks.fpScheduleGpuJob(&contextPrivate->contextDescription.callbacks, &dispatchJob); @@ -1353,9 +1389,10 @@ FfxErrorCode ffxFsr2ContextGenerateReactiveMask(FfxFsr2Context* context, const F return FFX_OK; } -static FfxErrorCode generateReactiveMaskInternal(FfxFsr2Context_Private* contextPrivate, const FfxFsr2DispatchDescription* params) +static FfxErrorCode generateReactiveMaskInternal(FfxFsr2Context_Private *contextPrivate, const FfxFsr2DispatchDescription *params) { - if (contextPrivate->refreshPipelineStates) { + if (contextPrivate->refreshPipelineStates) + { createPipelineStates(contextPrivate); contextPrivate->refreshPipelineStates = false; @@ -1364,7 +1401,7 @@ static FfxErrorCode generateReactiveMaskInternal(FfxFsr2Context_Private* context // take a short cut to the command list FfxCommandList commandList = params->commandList; - FfxPipelineState* pipeline = &contextPrivate->pipelineTcrAutogenerate; + FfxPipelineState *pipeline = &contextPrivate->pipelineTcrAutogenerate; const int32_t threadGroupWorkRegionDim = 8; const int32_t dispatchSrcX = (params->renderSize.width + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim; @@ -1378,51 +1415,52 @@ static FfxErrorCode generateReactiveMaskInternal(FfxFsr2Context_Private* context jobDescriptor.uavs[1] = contextPrivate->uavResources[FFX_FSR2_RESOURCE_IDENTIFIER_AUTOCOMPOSITION]; jobDescriptor.uavs[2] = contextPrivate->uavResources[FFX_FSR2_RESOURCE_IDENTIFIER_PREV_PRE_ALPHA_COLOR]; jobDescriptor.uavs[3] = contextPrivate->uavResources[FFX_FSR2_RESOURCE_IDENTIFIER_PREV_POST_ALPHA_COLOR]; - #ifdef ON_LINUX - wcscpy(jobDescriptor.uavNames[0], pipeline->uavResourceBindings[0].name); +#ifdef ON_LINUX + wcscpy(jobDescriptor.uavNames[0], pipeline->uavResourceBindings[0].name); wcscpy(jobDescriptor.uavNames[1], pipeline->uavResourceBindings[1].name); wcscpy(jobDescriptor.uavNames[2], pipeline->uavResourceBindings[2].name); wcscpy(jobDescriptor.uavNames[3], pipeline->uavResourceBindings[3].name); - #else - wcscpy_s(jobDescriptor.uavNames[0], pipeline->uavResourceBindings[0].name); +#else + wcscpy_s(jobDescriptor.uavNames[0], pipeline->uavResourceBindings[0].name); wcscpy_s(jobDescriptor.uavNames[1], pipeline->uavResourceBindings[1].name); wcscpy_s(jobDescriptor.uavNames[2], pipeline->uavResourceBindings[2].name); wcscpy_s(jobDescriptor.uavNames[3], pipeline->uavResourceBindings[3].name); - #endif - +#endif jobDescriptor.dimensions[0] = dispatchSrcX; jobDescriptor.dimensions[1] = dispatchSrcY; jobDescriptor.dimensions[2] = 1; jobDescriptor.pipeline = *pipeline; - for (uint32_t currentShaderResourceViewIndex = 0; currentShaderResourceViewIndex < pipeline->srvCount; ++currentShaderResourceViewIndex) { + for (uint32_t currentShaderResourceViewIndex = 0; currentShaderResourceViewIndex < pipeline->srvCount; ++currentShaderResourceViewIndex) + { const uint32_t currentResourceId = pipeline->srvResourceBindings[currentShaderResourceViewIndex].resourceIdentifier; const FfxResourceInternal currentResource = contextPrivate->srvResources[currentResourceId]; jobDescriptor.srvs[currentShaderResourceViewIndex] = currentResource; - #ifdef ON_LINUX +#ifdef ON_LINUX wcscpy(jobDescriptor.srvNames[currentShaderResourceViewIndex], pipeline->srvResourceBindings[currentShaderResourceViewIndex].name); - #else +#else wcscpy_s(jobDescriptor.srvNames[currentShaderResourceViewIndex], pipeline->srvResourceBindings[currentShaderResourceViewIndex].name); - #endif +#endif } - for (uint32_t currentRootConstantIndex = 0; currentRootConstantIndex < pipeline->constCount; ++currentRootConstantIndex) { - #ifdef ON_LINUX + for (uint32_t currentRootConstantIndex = 0; currentRootConstantIndex < pipeline->constCount; ++currentRootConstantIndex) + { +#ifdef ON_LINUX wcscpy(jobDescriptor.cbNames[currentRootConstantIndex], pipeline->cbResourceBindings[currentRootConstantIndex].name); - #else +#else wcscpy_s(jobDescriptor.cbNames[currentRootConstantIndex], pipeline->cbResourceBindings[currentRootConstantIndex].name); - #endif +#endif jobDescriptor.cbs[currentRootConstantIndex] = globalFsr2ConstantBuffers[pipeline->cbResourceBindings[currentRootConstantIndex].resourceIdentifier]; jobDescriptor.cbSlotIndex[currentRootConstantIndex] = pipeline->cbResourceBindings[currentRootConstantIndex].slotIndex; } - FfxGpuJobDescription dispatchJob = { FFX_GPU_JOB_COMPUTE }; + FfxGpuJobDescription dispatchJob = {FFX_GPU_JOB_COMPUTE}; dispatchJob.computeJobDescriptor = jobDescriptor; contextPrivate->contextDescription.callbacks.fpScheduleGpuJob(&contextPrivate->contextDescription.callbacks, &dispatchJob); diff --git a/src/ffx-fsr2-api/gl/CMakeLists.txt b/src/ffx-fsr2-api/gl/CMakeLists.txt index 32abb93..4399ccb 100644 --- a/src/ffx-fsr2-api/gl/CMakeLists.txt +++ b/src/ffx-fsr2-api/gl/CMakeLists.txt @@ -23,8 +23,8 @@ set(FFX_SC_GL_BASE_ARGS -compiler=glslang -e main --target-env opengl --target-env spirv1.3 --amb --stb comp 8 --ssb comp 8 --sib comp 0 --suavb comp 0 -Os -S comp -DFFX_GLSL=1) -set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -O3 -Wall") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -Wall") +set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -O3 -g -Wall") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -g -Wall") file(GLOB SHADERS "${CMAKE_CURRENT_SOURCE_DIR}/../shaders/*.h" @@ -47,7 +47,9 @@ file(GLOB_RECURSE GL add_library(ffx_fsr2_api_gl_${FSR2_PLATFORM_NAME} SHARED ${GL}) -find_package(OpenGL REQUIRED) +find_library(LOG_LIBRARY LOG) +find_library(OPENGL_ES3_LIBRARY OpenGL ES3) +#find_library(OpenGL REQUIRED) target_include_directories(ffx_fsr2_api_gl_${FSR2_PLATFORM_NAME} PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/../shaders/gl) diff --git a/src/ffx-fsr2-api/gl/ffx_fsr2_gl.cpp b/src/ffx-fsr2-api/gl/ffx_fsr2_gl.cpp index 5800098..af854bc 100644 --- a/src/ffx-fsr2-api/gl/ffx_fsr2_gl.cpp +++ b/src/ffx-fsr2-api/gl/ffx_fsr2_gl.cpp @@ -35,10 +35,9 @@ #ifdef ON_LINUX #include #else - #include // GetModuleHandleA +#include // GetModuleHandleA #endif - // prototypes for functions in the interface FfxErrorCode GetDeviceCapabilitiesGL(FfxFsr2Interface *backendInterface, FfxDeviceCapabilities *deviceCapabilities, FfxDevice); FfxErrorCode CreateBackendContextGL(FfxFsr2Interface *backendInterface, FfxDevice); @@ -55,1275 +54,1275 @@ FfxErrorCode ExecuteGpuJobsGL(FfxFsr2Interface *backendInterface, FfxCommandList namespace { - constexpr uint32_t FSR2_MAX_QUEUED_FRAMES = 4; - constexpr uint32_t FSR2_MAX_RESOURCE_COUNT = 64; - constexpr uint32_t FSR2_MAX_STAGING_RESOURCE_COUNT = 8; - constexpr uint32_t FSR2_MAX_GPU_JOBS = 32; - constexpr uint32_t FSR2_MAX_UNIFORM_BUFFERS = 4; - constexpr uint32_t FSR2_MAX_IMAGE_VIEWS = 32; - constexpr uint32_t FSR2_MAX_BUFFERED_DESCRIPTORS = FFX_FSR2_PASS_COUNT * FSR2_MAX_QUEUED_FRAMES; - constexpr uint32_t FSR2_UBO_RING_BUFFER_SIZE = FSR2_MAX_BUFFERED_DESCRIPTORS * FSR2_MAX_UNIFORM_BUFFERS; - constexpr uint32_t FSR2_UBO_SIZE = 256; - constexpr uint32_t FSR2_DEFAULT_SUBGROUP_SIZE = 32; - - namespace GL - { - struct Texture - { - GLuint id = {}; - }; - struct Buffer - { - GLuint id = {}; - }; - struct Sampler - { - GLuint id = {}; - }; - } + constexpr uint32_t FSR2_MAX_QUEUED_FRAMES = 4; + constexpr uint32_t FSR2_MAX_RESOURCE_COUNT = 64; + constexpr uint32_t FSR2_MAX_STAGING_RESOURCE_COUNT = 8; + constexpr uint32_t FSR2_MAX_GPU_JOBS = 32; + constexpr uint32_t FSR2_MAX_UNIFORM_BUFFERS = 4; + constexpr uint32_t FSR2_MAX_IMAGE_VIEWS = 32; + constexpr uint32_t FSR2_MAX_BUFFERED_DESCRIPTORS = FFX_FSR2_PASS_COUNT * FSR2_MAX_QUEUED_FRAMES; + constexpr uint32_t FSR2_UBO_RING_BUFFER_SIZE = FSR2_MAX_BUFFERED_DESCRIPTORS * FSR2_MAX_UNIFORM_BUFFERS; + constexpr uint32_t FSR2_UBO_SIZE = 256; + constexpr uint32_t FSR2_DEFAULT_SUBGROUP_SIZE = 32; + + namespace GL + { + struct Texture + { + GLuint id = {}; + }; + struct Buffer + { + GLuint id = {}; + }; + struct Sampler + { + GLuint id = {}; + }; + } } struct BackendContext_GL { - enum class Aspect - { - UNDEFINED, - COLOR, - DEPTH - }; + enum class Aspect + { + UNDEFINED, + COLOR, + DEPTH + }; - // store for resources and resourceViews - struct Resource - { + // store for resources and resourceViews + struct Resource + { #ifdef _DEBUG - char resourceName[64] = {}; + char resourceName[64] = {}; #endif - FfxResourceDescription resourceDescription; - - GL::Buffer buffer = {}; - - GL::Texture textureAllMipsView = {}; - GL::Texture textureSingleMipViews[FSR2_MAX_IMAGE_VIEWS] = {}; - Aspect textureAspect = {}; - }; - - struct UniformBuffer - { - GL::Buffer bufferResource = {}; - uint8_t *pData = {}; - }; - - struct GLFunctionTable - { - ffx_glGetProcAddress glGetProcAddress = nullptr; - PFNGLGETINTEGERVPROC glGetIntegerv = nullptr; - PFNGLGETSTRINGIPROC glGetStringi = nullptr; - PFNGLGETSTRINGPROC glGetString = nullptr; - PFNGLGETSHADERIVPROC glGetShaderiv = nullptr; - PFNGLGETPROGRAMIVPROC glGetProgramiv = nullptr; - PFNGLOBJECTLABELPROC glObjectLabel = nullptr; - PFNGLCREATESAMPLERSPROC glCreateSamplers = nullptr; - PFNGLSAMPLERPARAMETERIPROC glSamplerParameteri = nullptr; - PFNGLSAMPLERPARAMETERFPROC glSamplerParameterf = nullptr; - PFNGLCREATEBUFFERSPROC glCreateBuffers = nullptr; - PFNGLNAMEDBUFFERSTORAGEPROC glNamedBufferStorage = nullptr; - PFNGLCREATETEXTURESPROC glCreateTextures = nullptr; - PFNGLGENTEXTURESPROC glGenTextures = nullptr; - PFNGLTEXTUREVIEWPROC glTextureView = nullptr; - PFNGLTEXTURESTORAGE1DPROC glTextureStorage1D = nullptr; - PFNGLTEXTURESTORAGE2DPROC glTextureStorage2D = nullptr; - PFNGLTEXTURESTORAGE3DPROC glTextureStorage3D = nullptr; - PFNGLCREATESHADERPROC glCreateShader = nullptr; - PFNGLSHADERBINARYPROC glShaderBinary = nullptr; - PFNGLSPECIALIZESHADERPROC glSpecializeShader = nullptr; - PFNGLCOMPILESHADERPROC glCompileShader = nullptr; - PFNGLCREATEPROGRAMPROC glCreateProgram = nullptr; - PFNGLATTACHSHADERPROC glAttachShader = nullptr; - PFNGLLINKPROGRAMPROC glLinkProgram = nullptr; - PFNGLDELETEPROGRAMPROC glDeleteProgram = nullptr; - PFNGLDELETETEXTURESPROC glDeleteTextures = nullptr; - PFNGLDELETEBUFFERSPROC glDeleteBuffers = nullptr; - PFNGLDELETESAMPLERSPROC glDeleteSamplers = nullptr; - PFNGLDELETESHADERPROC glDeleteShader = nullptr; - PFNGLMAPNAMEDBUFFERRANGEPROC glMapNamedBufferRange = nullptr; - PFNGLUNMAPNAMEDBUFFERPROC glUnmapNamedBuffer = nullptr; - PFNGLMEMORYBARRIERPROC glMemoryBarrier = nullptr; - PFNGLUSEPROGRAMPROC glUseProgram = nullptr; - PFNGLPROGRAMUNIFORM1IPROC glProgramUniform1i = nullptr; - PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation = nullptr; - PFNGLBINDTEXTUREUNITPROC glBindTextureUnit = nullptr; - PFNGLBINDSAMPLERPROC glBindSampler = nullptr; - PFNGLBINDBUFFERRANGEPROC glBindBufferRange = nullptr; - PFNGLBINDIMAGETEXTUREPROC glBindImageTexture = nullptr; - PFNGLDISPATCHCOMPUTEPROC glDispatchCompute = nullptr; - PFNGLCOPYNAMEDBUFFERSUBDATAPROC glCopyNamedBufferSubData = nullptr; - PFNGLCOPYIMAGESUBDATAPROC glCopyImageSubData = nullptr; - PFNGLTEXTURESUBIMAGE1DPROC glTextureSubImage1D = nullptr; - PFNGLTEXTURESUBIMAGE2DPROC glTextureSubImage2D = nullptr; - PFNGLTEXTURESUBIMAGE3DPROC glTextureSubImage3D = nullptr; - PFNGLCLEARTEXIMAGEPROC glClearTexImage = nullptr; - }; - - GLFunctionTable glFunctionTable = {}; - FfxDeviceCapabilities capabilities = {}; - - uint32_t gpuJobCount = 0; - FfxGpuJobDescription gpuJobs[FSR2_MAX_GPU_JOBS] = {}; - - uint32_t nextStaticResource = 0; - uint32_t nextDynamicResource = 0; - uint32_t stagingResourceCount = 0; - Resource resources[FSR2_MAX_RESOURCE_COUNT] = {}; - FfxResourceInternal stagingResources[FSR2_MAX_STAGING_RESOURCE_COUNT] = {}; - - GL::Sampler pointSampler = {}; - GL::Sampler linearSampler = {}; - - UniformBuffer uboRingBuffer[FSR2_UBO_RING_BUFFER_SIZE] = {}; - uint32_t uboRingBufferIndex = 0; + FfxResourceDescription resourceDescription; + + GL::Buffer buffer = {}; + + GL::Texture textureAllMipsView = {}; + GL::Texture textureSingleMipViews[FSR2_MAX_IMAGE_VIEWS] = {}; + Aspect textureAspect = {}; + }; + + struct UniformBuffer + { + GL::Buffer bufferResource = {}; + uint8_t *pData = {}; + }; + + struct GLFunctionTable + { + ffx_glGetProcAddress glGetProcAddress = nullptr; + PFNGLGETINTEGERVPROC glGetIntegerv = nullptr; + PFNGLGETSTRINGIPROC glGetStringi = nullptr; + PFNGLGETSTRINGPROC glGetString = nullptr; + PFNGLGETSHADERIVPROC glGetShaderiv = nullptr; + PFNGLGETPROGRAMIVPROC glGetProgramiv = nullptr; + PFNGLOBJECTLABELPROC glObjectLabel = nullptr; + PFNGLCREATESAMPLERSPROC glCreateSamplers = nullptr; + PFNGLSAMPLERPARAMETERIPROC glSamplerParameteri = nullptr; + PFNGLSAMPLERPARAMETERFPROC glSamplerParameterf = nullptr; + PFNGLCREATEBUFFERSPROC glCreateBuffers = nullptr; + PFNGLNAMEDBUFFERSTORAGEPROC glNamedBufferStorage = nullptr; + PFNGLCREATETEXTURESPROC glCreateTextures = nullptr; + PFNGLGENTEXTURESPROC glGenTextures = nullptr; + PFNGLTEXTUREVIEWPROC glTextureView = nullptr; + PFNGLTEXTURESTORAGE1DPROC glTextureStorage1D = nullptr; + PFNGLTEXTURESTORAGE2DPROC glTextureStorage2D = nullptr; + PFNGLTEXTURESTORAGE3DPROC glTextureStorage3D = nullptr; + PFNGLCREATESHADERPROC glCreateShader = nullptr; + PFNGLSHADERBINARYPROC glShaderBinary = nullptr; + PFNGLSPECIALIZESHADERPROC glSpecializeShader = nullptr; + PFNGLCOMPILESHADERPROC glCompileShader = nullptr; + PFNGLCREATEPROGRAMPROC glCreateProgram = nullptr; + PFNGLATTACHSHADERPROC glAttachShader = nullptr; + PFNGLLINKPROGRAMPROC glLinkProgram = nullptr; + PFNGLDELETEPROGRAMPROC glDeleteProgram = nullptr; + PFNGLDELETETEXTURESPROC glDeleteTextures = nullptr; + PFNGLDELETEBUFFERSPROC glDeleteBuffers = nullptr; + PFNGLDELETESAMPLERSPROC glDeleteSamplers = nullptr; + PFNGLDELETESHADERPROC glDeleteShader = nullptr; + PFNGLMAPNAMEDBUFFERRANGEPROC glMapNamedBufferRange = nullptr; + PFNGLUNMAPNAMEDBUFFERPROC glUnmapNamedBuffer = nullptr; + PFNGLMEMORYBARRIERPROC glMemoryBarrier = nullptr; + PFNGLUSEPROGRAMPROC glUseProgram = nullptr; + PFNGLPROGRAMUNIFORM1IPROC glProgramUniform1i = nullptr; + PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation = nullptr; + PFNGLBINDTEXTUREUNITPROC glBindTextureUnit = nullptr; + PFNGLBINDSAMPLERPROC glBindSampler = nullptr; + PFNGLBINDBUFFERRANGEPROC glBindBufferRange = nullptr; + PFNGLBINDIMAGETEXTUREPROC glBindImageTexture = nullptr; + PFNGLDISPATCHCOMPUTEPROC glDispatchCompute = nullptr; + PFNGLCOPYNAMEDBUFFERSUBDATAPROC glCopyNamedBufferSubData = nullptr; + PFNGLCOPYIMAGESUBDATAPROC glCopyImageSubData = nullptr; + PFNGLTEXTURESUBIMAGE1DPROC glTextureSubImage1D = nullptr; + PFNGLTEXTURESUBIMAGE2DPROC glTextureSubImage2D = nullptr; + PFNGLTEXTURESUBIMAGE3DPROC glTextureSubImage3D = nullptr; + PFNGLCLEARTEXIMAGEPROC glClearTexImage = nullptr; + }; + + GLFunctionTable glFunctionTable = {}; + FfxDeviceCapabilities capabilities = {}; + + uint32_t gpuJobCount = 0; + FfxGpuJobDescription gpuJobs[FSR2_MAX_GPU_JOBS] = {}; + + uint32_t nextStaticResource = 0; + uint32_t nextDynamicResource = 0; + uint32_t stagingResourceCount = 0; + Resource resources[FSR2_MAX_RESOURCE_COUNT] = {}; + FfxResourceInternal stagingResources[FSR2_MAX_STAGING_RESOURCE_COUNT] = {}; + + GL::Sampler pointSampler = {}; + GL::Sampler linearSampler = {}; + + UniformBuffer uboRingBuffer[FSR2_UBO_RING_BUFFER_SIZE] = {}; + uint32_t uboRingBufferIndex = 0; }; FFX_API size_t ffxFsr2GetScratchMemorySizeGL() { - return sizeof(BackendContext_GL); + return sizeof(BackendContext_GL); } FfxErrorCode ffxFsr2GetInterfaceGL( - FfxFsr2Interface *outInterface, - void *scratchBuffer, - size_t scratchBufferSize, - ffx_glGetProcAddress getProcAddress) + FfxFsr2Interface *outInterface, + void *scratchBuffer, + size_t scratchBufferSize, + ffx_glGetProcAddress getProcAddress) { - FFX_RETURN_ON_ERROR( - outInterface, - FFX_ERROR_INVALID_POINTER); - FFX_RETURN_ON_ERROR( - scratchBuffer, - FFX_ERROR_INVALID_POINTER); - FFX_RETURN_ON_ERROR( - scratchBufferSize >= ffxFsr2GetScratchMemorySizeGL(), - FFX_ERROR_INSUFFICIENT_MEMORY); - - outInterface->fpGetDeviceCapabilities = GetDeviceCapabilitiesGL; - outInterface->fpCreateBackendContext = CreateBackendContextGL; - outInterface->fpDestroyBackendContext = DestroyBackendContextGL; - outInterface->fpCreateResource = CreateResourceGL; - outInterface->fpRegisterResource = RegisterResourceGL; - outInterface->fpUnregisterResources = UnregisterResourcesGL; - outInterface->fpGetResourceDescription = GetResourceDescriptorGL; - outInterface->fpDestroyResource = DestroyResourceGL; - outInterface->fpCreatePipeline = CreatePipelineGL; - outInterface->fpDestroyPipeline = DestroyPipelineGL; - outInterface->fpScheduleGpuJob = ScheduleGpuJobGL; - outInterface->fpExecuteGpuJobs = ExecuteGpuJobsGL; - outInterface->scratchBuffer = scratchBuffer; - outInterface->scratchBufferSize = scratchBufferSize; - - BackendContext_GL *context = (BackendContext_GL *)scratchBuffer; - - context->glFunctionTable.glGetProcAddress = getProcAddress; - - return FFX_OK; + FFX_RETURN_ON_ERROR( + outInterface, + FFX_ERROR_INVALID_POINTER); + FFX_RETURN_ON_ERROR( + scratchBuffer, + FFX_ERROR_INVALID_POINTER); + FFX_RETURN_ON_ERROR( + scratchBufferSize >= ffxFsr2GetScratchMemorySizeGL(), + FFX_ERROR_INSUFFICIENT_MEMORY); + + outInterface->fpGetDeviceCapabilities = GetDeviceCapabilitiesGL; + outInterface->fpCreateBackendContext = CreateBackendContextGL; + outInterface->fpDestroyBackendContext = DestroyBackendContextGL; + outInterface->fpCreateResource = CreateResourceGL; + outInterface->fpRegisterResource = RegisterResourceGL; + outInterface->fpUnregisterResources = UnregisterResourcesGL; + outInterface->fpGetResourceDescription = GetResourceDescriptorGL; + outInterface->fpDestroyResource = DestroyResourceGL; + outInterface->fpCreatePipeline = CreatePipelineGL; + outInterface->fpDestroyPipeline = DestroyPipelineGL; + outInterface->fpScheduleGpuJob = ScheduleGpuJobGL; + outInterface->fpExecuteGpuJobs = ExecuteGpuJobsGL; + outInterface->scratchBuffer = scratchBuffer; + outInterface->scratchBufferSize = scratchBufferSize; + + BackendContext_GL *context = (BackendContext_GL *)scratchBuffer; + + context->glFunctionTable.glGetProcAddress = getProcAddress; + + return FFX_OK; } static void loadGLFunctions(BackendContext_GL *backendContext, ffx_glGetProcAddress getProcAddress) { - FFX_ASSERT(NULL != backendContext); - - backendContext->glFunctionTable.glObjectLabel = (PFNGLOBJECTLABELPROC)getProcAddress("glObjectLabel"); - backendContext->glFunctionTable.glGetIntegerv = (PFNGLGETINTEGERVPROC)getProcAddress("glGetIntegerv"); - backendContext->glFunctionTable.glGetString = (PFNGLGETSTRINGPROC)getProcAddress("glGetString"); - backendContext->glFunctionTable.glGetStringi = (PFNGLGETSTRINGIPROC)getProcAddress("glGetStringi"); - backendContext->glFunctionTable.glGetShaderiv = (PFNGLGETSHADERIVPROC)getProcAddress("glGetShaderiv"); - backendContext->glFunctionTable.glGetProgramiv = (PFNGLGETPROGRAMIVPROC)getProcAddress("glGetProgramiv"); - backendContext->glFunctionTable.glCreateSamplers = (PFNGLCREATESAMPLERSPROC)getProcAddress("glCreateSamplers"); - backendContext->glFunctionTable.glSamplerParameteri = (PFNGLSAMPLERPARAMETERIPROC)getProcAddress("glSamplerParameteri"); - backendContext->glFunctionTable.glSamplerParameterf = (PFNGLSAMPLERPARAMETERFPROC)getProcAddress("glSamplerParameterf"); - backendContext->glFunctionTable.glCreateBuffers = (PFNGLCREATEBUFFERSPROC)getProcAddress("glCreateBuffers"); - backendContext->glFunctionTable.glNamedBufferStorage = (PFNGLNAMEDBUFFERSTORAGEPROC)getProcAddress("glNamedBufferStorage"); - backendContext->glFunctionTable.glCreateTextures = (PFNGLCREATETEXTURESPROC)getProcAddress("glCreateTextures"); - backendContext->glFunctionTable.glGenTextures = (PFNGLGENTEXTURESPROC)getProcAddress("glGenTextures"); - backendContext->glFunctionTable.glTextureView = (PFNGLTEXTUREVIEWPROC)getProcAddress("glTextureView"); - backendContext->glFunctionTable.glTextureStorage1D = (PFNGLTEXTURESTORAGE1DPROC)getProcAddress("glTextureStorage1D"); - backendContext->glFunctionTable.glTextureStorage2D = (PFNGLTEXTURESTORAGE2DPROC)getProcAddress("glTextureStorage2D"); - backendContext->glFunctionTable.glTextureStorage3D = (PFNGLTEXTURESTORAGE3DPROC)getProcAddress("glTextureStorage3D"); - backendContext->glFunctionTable.glCreateShader = (PFNGLCREATESHADERPROC)getProcAddress("glCreateShader"); - backendContext->glFunctionTable.glShaderBinary = (PFNGLSHADERBINARYPROC)getProcAddress("glShaderBinary"); - backendContext->glFunctionTable.glSpecializeShader = (PFNGLSPECIALIZESHADERPROC)getProcAddress("glSpecializeShader"); - backendContext->glFunctionTable.glCompileShader = (PFNGLCOMPILESHADERPROC)getProcAddress("glCompileShader"); - backendContext->glFunctionTable.glCreateProgram = (PFNGLCREATEPROGRAMPROC)getProcAddress("glCreateProgram"); - backendContext->glFunctionTable.glAttachShader = (PFNGLATTACHSHADERPROC)getProcAddress("glAttachShader"); - backendContext->glFunctionTable.glLinkProgram = (PFNGLLINKPROGRAMPROC)getProcAddress("glLinkProgram"); - backendContext->glFunctionTable.glDeleteProgram = (PFNGLDELETEPROGRAMPROC)getProcAddress("glDeleteProgram"); - backendContext->glFunctionTable.glDeleteTextures = (PFNGLDELETETEXTURESPROC)getProcAddress("glDeleteTextures"); - backendContext->glFunctionTable.glDeleteBuffers = (PFNGLDELETEBUFFERSPROC)getProcAddress("glDeleteBuffers"); - backendContext->glFunctionTable.glDeleteSamplers = (PFNGLDELETESAMPLERSPROC)getProcAddress("glDeleteSamplers"); - backendContext->glFunctionTable.glDeleteShader = (PFNGLDELETESHADERPROC)getProcAddress("glDeleteShader"); - backendContext->glFunctionTable.glMapNamedBufferRange = (PFNGLMAPNAMEDBUFFERRANGEPROC)getProcAddress("glMapNamedBufferRange"); - backendContext->glFunctionTable.glUnmapNamedBuffer = (PFNGLUNMAPNAMEDBUFFERPROC)getProcAddress("glUnmapNamedBuffer"); - backendContext->glFunctionTable.glMemoryBarrier = (PFNGLMEMORYBARRIERPROC)getProcAddress("glMemoryBarrier"); - backendContext->glFunctionTable.glUseProgram = (PFNGLUSEPROGRAMPROC)getProcAddress("glUseProgram"); - backendContext->glFunctionTable.glProgramUniform1i = (PFNGLPROGRAMUNIFORM1IPROC)getProcAddress("glProgramUniform1i"); - backendContext->glFunctionTable.glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)getProcAddress("glGetUniformLocation"); - backendContext->glFunctionTable.glBindTextureUnit = (PFNGLBINDTEXTUREUNITPROC)getProcAddress("glBindTextureUnit"); - backendContext->glFunctionTable.glBindSampler = (PFNGLBINDSAMPLERPROC)getProcAddress("glBindSampler"); - backendContext->glFunctionTable.glBindBufferRange = (PFNGLBINDBUFFERRANGEPROC)getProcAddress("glBindBufferRange"); - backendContext->glFunctionTable.glBindImageTexture = (PFNGLBINDIMAGETEXTUREPROC)getProcAddress("glBindImageTexture"); - backendContext->glFunctionTable.glDispatchCompute = (PFNGLDISPATCHCOMPUTEPROC)getProcAddress("glDispatchCompute"); - backendContext->glFunctionTable.glCopyNamedBufferSubData = (PFNGLCOPYNAMEDBUFFERSUBDATAPROC)getProcAddress("glCopyNamedBufferSubData"); - backendContext->glFunctionTable.glCopyImageSubData = (PFNGLCOPYIMAGESUBDATAPROC)getProcAddress("glCopyImageSubData"); - backendContext->glFunctionTable.glTextureSubImage1D = (PFNGLTEXTURESUBIMAGE1DPROC)getProcAddress("glTextureSubImage1D"); - backendContext->glFunctionTable.glTextureSubImage2D = (PFNGLTEXTURESUBIMAGE2DPROC)getProcAddress("glTextureSubImage2D"); - backendContext->glFunctionTable.glTextureSubImage3D = (PFNGLTEXTURESUBIMAGE3DPROC)getProcAddress("glTextureSubImage3D"); - backendContext->glFunctionTable.glClearTexImage = (PFNGLCLEARTEXIMAGEPROC)getProcAddress("glClearTexImage"); + FFX_ASSERT(NULL != backendContext); + + backendContext->glFunctionTable.glObjectLabel = (PFNGLOBJECTLABELPROC)getProcAddress("glObjectLabel"); + backendContext->glFunctionTable.glGetIntegerv = (PFNGLGETINTEGERVPROC)getProcAddress("glGetIntegerv"); + backendContext->glFunctionTable.glGetString = (PFNGLGETSTRINGPROC)getProcAddress("glGetString"); + backendContext->glFunctionTable.glGetStringi = (PFNGLGETSTRINGIPROC)getProcAddress("glGetStringi"); + backendContext->glFunctionTable.glGetShaderiv = (PFNGLGETSHADERIVPROC)getProcAddress("glGetShaderiv"); + backendContext->glFunctionTable.glGetProgramiv = (PFNGLGETPROGRAMIVPROC)getProcAddress("glGetProgramiv"); + backendContext->glFunctionTable.glCreateSamplers = (PFNGLCREATESAMPLERSPROC)getProcAddress("glCreateSamplers"); + backendContext->glFunctionTable.glSamplerParameteri = (PFNGLSAMPLERPARAMETERIPROC)getProcAddress("glSamplerParameteri"); + backendContext->glFunctionTable.glSamplerParameterf = (PFNGLSAMPLERPARAMETERFPROC)getProcAddress("glSamplerParameterf"); + backendContext->glFunctionTable.glCreateBuffers = (PFNGLCREATEBUFFERSPROC)getProcAddress("glCreateBuffers"); + backendContext->glFunctionTable.glNamedBufferStorage = (PFNGLNAMEDBUFFERSTORAGEPROC)getProcAddress("glNamedBufferStorage"); + backendContext->glFunctionTable.glCreateTextures = (PFNGLCREATETEXTURESPROC)getProcAddress("glCreateTextures"); + backendContext->glFunctionTable.glGenTextures = (PFNGLGENTEXTURESPROC)getProcAddress("glGenTextures"); + backendContext->glFunctionTable.glTextureView = (PFNGLTEXTUREVIEWPROC)getProcAddress("glTextureView"); + backendContext->glFunctionTable.glTextureStorage1D = (PFNGLTEXTURESTORAGE1DPROC)getProcAddress("glTextureStorage1D"); + backendContext->glFunctionTable.glTextureStorage2D = (PFNGLTEXTURESTORAGE2DPROC)getProcAddress("glTextureStorage2D"); + backendContext->glFunctionTable.glTextureStorage3D = (PFNGLTEXTURESTORAGE3DPROC)getProcAddress("glTextureStorage3D"); + backendContext->glFunctionTable.glCreateShader = (PFNGLCREATESHADERPROC)getProcAddress("glCreateShader"); + backendContext->glFunctionTable.glShaderBinary = (PFNGLSHADERBINARYPROC)getProcAddress("glShaderBinary"); + backendContext->glFunctionTable.glSpecializeShader = (PFNGLSPECIALIZESHADERPROC)getProcAddress("glSpecializeShader"); + backendContext->glFunctionTable.glCompileShader = (PFNGLCOMPILESHADERPROC)getProcAddress("glCompileShader"); + backendContext->glFunctionTable.glCreateProgram = (PFNGLCREATEPROGRAMPROC)getProcAddress("glCreateProgram"); + backendContext->glFunctionTable.glAttachShader = (PFNGLATTACHSHADERPROC)getProcAddress("glAttachShader"); + backendContext->glFunctionTable.glLinkProgram = (PFNGLLINKPROGRAMPROC)getProcAddress("glLinkProgram"); + backendContext->glFunctionTable.glDeleteProgram = (PFNGLDELETEPROGRAMPROC)getProcAddress("glDeleteProgram"); + backendContext->glFunctionTable.glDeleteTextures = (PFNGLDELETETEXTURESPROC)getProcAddress("glDeleteTextures"); + backendContext->glFunctionTable.glDeleteBuffers = (PFNGLDELETEBUFFERSPROC)getProcAddress("glDeleteBuffers"); + backendContext->glFunctionTable.glDeleteSamplers = (PFNGLDELETESAMPLERSPROC)getProcAddress("glDeleteSamplers"); + backendContext->glFunctionTable.glDeleteShader = (PFNGLDELETESHADERPROC)getProcAddress("glDeleteShader"); + backendContext->glFunctionTable.glMapNamedBufferRange = (PFNGLMAPNAMEDBUFFERRANGEPROC)getProcAddress("glMapNamedBufferRange"); + backendContext->glFunctionTable.glUnmapNamedBuffer = (PFNGLUNMAPNAMEDBUFFERPROC)getProcAddress("glUnmapNamedBuffer"); + backendContext->glFunctionTable.glMemoryBarrier = (PFNGLMEMORYBARRIERPROC)getProcAddress("glMemoryBarrier"); + backendContext->glFunctionTable.glUseProgram = (PFNGLUSEPROGRAMPROC)getProcAddress("glUseProgram"); + backendContext->glFunctionTable.glProgramUniform1i = (PFNGLPROGRAMUNIFORM1IPROC)getProcAddress("glProgramUniform1i"); + backendContext->glFunctionTable.glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)getProcAddress("glGetUniformLocation"); + backendContext->glFunctionTable.glBindTextureUnit = (PFNGLBINDTEXTUREUNITPROC)getProcAddress("glBindTextureUnit"); + backendContext->glFunctionTable.glBindSampler = (PFNGLBINDSAMPLERPROC)getProcAddress("glBindSampler"); + backendContext->glFunctionTable.glBindBufferRange = (PFNGLBINDBUFFERRANGEPROC)getProcAddress("glBindBufferRange"); + backendContext->glFunctionTable.glBindImageTexture = (PFNGLBINDIMAGETEXTUREPROC)getProcAddress("glBindImageTexture"); + backendContext->glFunctionTable.glDispatchCompute = (PFNGLDISPATCHCOMPUTEPROC)getProcAddress("glDispatchCompute"); + backendContext->glFunctionTable.glCopyNamedBufferSubData = (PFNGLCOPYNAMEDBUFFERSUBDATAPROC)getProcAddress("glCopyNamedBufferSubData"); + backendContext->glFunctionTable.glCopyImageSubData = (PFNGLCOPYIMAGESUBDATAPROC)getProcAddress("glCopyImageSubData"); + backendContext->glFunctionTable.glTextureSubImage1D = (PFNGLTEXTURESUBIMAGE1DPROC)getProcAddress("glTextureSubImage1D"); + backendContext->glFunctionTable.glTextureSubImage2D = (PFNGLTEXTURESUBIMAGE2DPROC)getProcAddress("glTextureSubImage2D"); + backendContext->glFunctionTable.glTextureSubImage3D = (PFNGLTEXTURESUBIMAGE3DPROC)getProcAddress("glTextureSubImage3D"); + backendContext->glFunctionTable.glClearTexImage = (PFNGLCLEARTEXIMAGEPROC)getProcAddress("glClearTexImage"); } static GLenum getGLFormatFromSurfaceFormat(FfxSurfaceFormat fmt) { - switch (fmt) - { - case FFX_SURFACE_FORMAT_R32G32B32A32_TYPELESS: - return GL_RGBA32F; - case FFX_SURFACE_FORMAT_R32G32B32A32_FLOAT: - return GL_RGBA32F; - case FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT: - return GL_RGBA16F; - case FFX_SURFACE_FORMAT_R16G16B16A16_UNORM: - return GL_RGBA16; - case FFX_SURFACE_FORMAT_R32G32_FLOAT: - return GL_RG32F; - case FFX_SURFACE_FORMAT_R32_UINT: - return GL_R32UI; - case FFX_SURFACE_FORMAT_R8G8B8A8_TYPELESS: - return GL_RGBA8; - case FFX_SURFACE_FORMAT_R8G8B8A8_UNORM: - return GL_RGBA8; - case FFX_SURFACE_FORMAT_R11G11B10_FLOAT: - return GL_R11F_G11F_B10F; - case FFX_SURFACE_FORMAT_R16G16_FLOAT: - return GL_RG16F; - case FFX_SURFACE_FORMAT_R16G16_UINT: - return GL_RG16UI; - case FFX_SURFACE_FORMAT_R16_FLOAT: - return GL_R16F; - case FFX_SURFACE_FORMAT_R16_UINT: - return GL_R16UI; - case FFX_SURFACE_FORMAT_R16_UNORM: - return GL_R16; - case FFX_SURFACE_FORMAT_R16_SNORM: - return GL_R16_SNORM; - case FFX_SURFACE_FORMAT_R8_UNORM: - return GL_R8; - case FFX_SURFACE_FORMAT_R8G8_UNORM: - return GL_RG8; - case FFX_SURFACE_FORMAT_R32_FLOAT: - return GL_R32F; - case FFX_SURFACE_FORMAT_R8_UINT: - return GL_R8UI; - default: - FFX_ASSERT_FAIL(""); - return 0; - } + switch (fmt) + { + case FFX_SURFACE_FORMAT_R32G32B32A32_TYPELESS: + return GL_RGBA32F; + case FFX_SURFACE_FORMAT_R32G32B32A32_FLOAT: + return GL_RGBA32F; + case FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT: + return GL_RGBA16F; + case FFX_SURFACE_FORMAT_R16G16B16A16_UNORM: + return GL_RGBA16; + case FFX_SURFACE_FORMAT_R32G32_FLOAT: + return GL_RG32F; + case FFX_SURFACE_FORMAT_R32_UINT: + return GL_R32UI; + case FFX_SURFACE_FORMAT_R8G8B8A8_TYPELESS: + return GL_RGBA8; + case FFX_SURFACE_FORMAT_R8G8B8A8_UNORM: + return GL_RGBA8; + case FFX_SURFACE_FORMAT_R11G11B10_FLOAT: + return GL_R11F_G11F_B10F; + case FFX_SURFACE_FORMAT_R16G16_FLOAT: + return GL_RG16F; + case FFX_SURFACE_FORMAT_R16G16_UINT: + return GL_RG16UI; + case FFX_SURFACE_FORMAT_R16_FLOAT: + return GL_R16F; + case FFX_SURFACE_FORMAT_R16_UINT: + return GL_R16UI; + case FFX_SURFACE_FORMAT_R16_UNORM: + return GL_R16; + case FFX_SURFACE_FORMAT_R16_SNORM: + return GL_R16_SNORM; + case FFX_SURFACE_FORMAT_R8_UNORM: + return GL_R8; + case FFX_SURFACE_FORMAT_R8G8_UNORM: + return GL_RG8; + case FFX_SURFACE_FORMAT_R32_FLOAT: + return GL_R32F; + case FFX_SURFACE_FORMAT_R8_UINT: + return GL_R8UI; + default: + FFX_ASSERT_FAIL(""); + return 0; + } } static GLenum getGLUploadFormatFromSurfaceFormat(FfxSurfaceFormat fmt) { - switch (fmt) - { - case FFX_SURFACE_FORMAT_R32G32B32A32_TYPELESS: - case FFX_SURFACE_FORMAT_R32G32B32A32_FLOAT: - case FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT: - case FFX_SURFACE_FORMAT_R16G16B16A16_UNORM: - case FFX_SURFACE_FORMAT_R8G8B8A8_TYPELESS: - case FFX_SURFACE_FORMAT_R8G8B8A8_UNORM: - return GL_RGBA; - case FFX_SURFACE_FORMAT_R11G11B10_FLOAT: - return GL_RGB; - case FFX_SURFACE_FORMAT_R32G32_FLOAT: - case FFX_SURFACE_FORMAT_R16G16_FLOAT: - case FFX_SURFACE_FORMAT_R16G16_UINT: - case FFX_SURFACE_FORMAT_R8G8_UNORM: - return GL_RG; - case FFX_SURFACE_FORMAT_R16_FLOAT: - case FFX_SURFACE_FORMAT_R16_UNORM: - case FFX_SURFACE_FORMAT_R16_SNORM: - case FFX_SURFACE_FORMAT_R8_UNORM: - case FFX_SURFACE_FORMAT_R32_FLOAT: - return GL_RED; - case FFX_SURFACE_FORMAT_R8_UINT: - case FFX_SURFACE_FORMAT_R16_UINT: - case FFX_SURFACE_FORMAT_R32_UINT: - return GL_RED_INTEGER; - default: - FFX_ASSERT_FAIL(""); - return 0; - } + switch (fmt) + { + case FFX_SURFACE_FORMAT_R32G32B32A32_TYPELESS: + case FFX_SURFACE_FORMAT_R32G32B32A32_FLOAT: + case FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT: + case FFX_SURFACE_FORMAT_R16G16B16A16_UNORM: + case FFX_SURFACE_FORMAT_R8G8B8A8_TYPELESS: + case FFX_SURFACE_FORMAT_R8G8B8A8_UNORM: + return GL_RGBA; + case FFX_SURFACE_FORMAT_R11G11B10_FLOAT: + return GL_RGB; + case FFX_SURFACE_FORMAT_R32G32_FLOAT: + case FFX_SURFACE_FORMAT_R16G16_FLOAT: + case FFX_SURFACE_FORMAT_R16G16_UINT: + case FFX_SURFACE_FORMAT_R8G8_UNORM: + return GL_RG; + case FFX_SURFACE_FORMAT_R16_FLOAT: + case FFX_SURFACE_FORMAT_R16_UNORM: + case FFX_SURFACE_FORMAT_R16_SNORM: + case FFX_SURFACE_FORMAT_R8_UNORM: + case FFX_SURFACE_FORMAT_R32_FLOAT: + return GL_RED; + case FFX_SURFACE_FORMAT_R8_UINT: + case FFX_SURFACE_FORMAT_R16_UINT: + case FFX_SURFACE_FORMAT_R32_UINT: + return GL_RED_INTEGER; + default: + FFX_ASSERT_FAIL(""); + return 0; + } } static GLenum getGLUploadTypeFromSurfaceFormat(FfxSurfaceFormat fmt) { - switch (fmt) - { - case FFX_SURFACE_FORMAT_R32G32B32A32_TYPELESS: - case FFX_SURFACE_FORMAT_R32G32B32A32_FLOAT: - case FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT: - case FFX_SURFACE_FORMAT_R32G32_FLOAT: - case FFX_SURFACE_FORMAT_R11G11B10_FLOAT: - case FFX_SURFACE_FORMAT_R16G16_FLOAT: - case FFX_SURFACE_FORMAT_R16_FLOAT: - case FFX_SURFACE_FORMAT_R32_FLOAT: - return GL_FLOAT; - case FFX_SURFACE_FORMAT_R8G8B8A8_UNORM: - case FFX_SURFACE_FORMAT_R8G8B8A8_TYPELESS: - case FFX_SURFACE_FORMAT_R8G8_UNORM: - case FFX_SURFACE_FORMAT_R8_UNORM: - return GL_UNSIGNED_BYTE; - case FFX_SURFACE_FORMAT_R32_UINT: - return GL_UNSIGNED_INT; - case FFX_SURFACE_FORMAT_R16G16B16A16_UNORM: - case FFX_SURFACE_FORMAT_R16_UNORM: - case FFX_SURFACE_FORMAT_R16G16_UINT: - case FFX_SURFACE_FORMAT_R16_UINT: - case FFX_SURFACE_FORMAT_R8_UINT: - return GL_UNSIGNED_SHORT; - case FFX_SURFACE_FORMAT_R16_SNORM: - return GL_SHORT; - default: - FFX_ASSERT_FAIL(""); - return 0; - } + switch (fmt) + { + case FFX_SURFACE_FORMAT_R32G32B32A32_TYPELESS: + case FFX_SURFACE_FORMAT_R32G32B32A32_FLOAT: + case FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT: + case FFX_SURFACE_FORMAT_R32G32_FLOAT: + case FFX_SURFACE_FORMAT_R11G11B10_FLOAT: + case FFX_SURFACE_FORMAT_R16G16_FLOAT: + case FFX_SURFACE_FORMAT_R16_FLOAT: + case FFX_SURFACE_FORMAT_R32_FLOAT: + return GL_FLOAT; + case FFX_SURFACE_FORMAT_R8G8B8A8_UNORM: + case FFX_SURFACE_FORMAT_R8G8B8A8_TYPELESS: + case FFX_SURFACE_FORMAT_R8G8_UNORM: + case FFX_SURFACE_FORMAT_R8_UNORM: + return GL_UNSIGNED_BYTE; + case FFX_SURFACE_FORMAT_R32_UINT: + return GL_UNSIGNED_INT; + case FFX_SURFACE_FORMAT_R16G16B16A16_UNORM: + case FFX_SURFACE_FORMAT_R16_UNORM: + case FFX_SURFACE_FORMAT_R16G16_UINT: + case FFX_SURFACE_FORMAT_R16_UINT: + case FFX_SURFACE_FORMAT_R8_UINT: + return GL_UNSIGNED_SHORT; + case FFX_SURFACE_FORMAT_R16_SNORM: + return GL_SHORT; + default: + FFX_ASSERT_FAIL(""); + return 0; + } } FfxSurfaceFormat ffxGetSurfaceFormatGL(GLenum fmt) { - switch (fmt) - { - case GL_RGBA32F: - return FFX_SURFACE_FORMAT_R32G32B32A32_FLOAT; - case GL_RGBA16F: - return FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT; - case GL_RGBA16: - return FFX_SURFACE_FORMAT_R16G16B16A16_UNORM; - case GL_RG32F: - return FFX_SURFACE_FORMAT_R32G32_FLOAT; - case GL_R32UI: - return FFX_SURFACE_FORMAT_R32_UINT; - case GL_RGBA8: - return FFX_SURFACE_FORMAT_R8G8B8A8_UNORM; - case GL_R11F_G11F_B10F: - return FFX_SURFACE_FORMAT_R11G11B10_FLOAT; - case GL_RG16F: - return FFX_SURFACE_FORMAT_R16G16_FLOAT; - case GL_RG16UI: - return FFX_SURFACE_FORMAT_R16G16_UINT; - case GL_R16F: - return FFX_SURFACE_FORMAT_R16_FLOAT; - case GL_R16UI: - return FFX_SURFACE_FORMAT_R16_UINT; - case GL_R16: - return FFX_SURFACE_FORMAT_R16_UNORM; - case GL_R16_SNORM: - return FFX_SURFACE_FORMAT_R16_SNORM; - case GL_R8: - return FFX_SURFACE_FORMAT_R8_UNORM; - case GL_R32F: - return FFX_SURFACE_FORMAT_R32_FLOAT; - case GL_R8UI: - return FFX_SURFACE_FORMAT_R8_UINT; - default: - return FFX_SURFACE_FORMAT_UNKNOWN; - } + switch (fmt) + { + case GL_RGBA32F: + return FFX_SURFACE_FORMAT_R32G32B32A32_FLOAT; + case GL_RGBA16F: + return FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT; + case GL_RGBA16: + return FFX_SURFACE_FORMAT_R16G16B16A16_UNORM; + case GL_RG32F: + return FFX_SURFACE_FORMAT_R32G32_FLOAT; + case GL_R32UI: + return FFX_SURFACE_FORMAT_R32_UINT; + case GL_RGBA8: + return FFX_SURFACE_FORMAT_R8G8B8A8_UNORM; + case GL_R11F_G11F_B10F: + return FFX_SURFACE_FORMAT_R11G11B10_FLOAT; + case GL_RG16F: + return FFX_SURFACE_FORMAT_R16G16_FLOAT; + case GL_RG16UI: + return FFX_SURFACE_FORMAT_R16G16_UINT; + case GL_R16F: + return FFX_SURFACE_FORMAT_R16_FLOAT; + case GL_R16UI: + return FFX_SURFACE_FORMAT_R16_UINT; + case GL_R16: + return FFX_SURFACE_FORMAT_R16_UNORM; + case GL_R16_SNORM: + return FFX_SURFACE_FORMAT_R16_SNORM; + case GL_R8: + return FFX_SURFACE_FORMAT_R8_UNORM; + case GL_R32F: + return FFX_SURFACE_FORMAT_R32_FLOAT; + case GL_R8UI: + return FFX_SURFACE_FORMAT_R8_UINT; + default: + return FFX_SURFACE_FORMAT_UNKNOWN; + } } static BackendContext_GL::UniformBuffer accquireDynamicUBO(BackendContext_GL *backendContext, uint32_t size, const void *pData) { - // the ubo ring buffer is pre-populated with VkBuffer objects of 256-bytes to prevent creating buffers at runtime - FFX_ASSERT(size <= 256); + // the ubo ring buffer is pre-populated with VkBuffer objects of 256-bytes to prevent creating buffers at runtime + FFX_ASSERT(size <= 256); - BackendContext_GL::UniformBuffer &ubo = backendContext->uboRingBuffer[backendContext->uboRingBufferIndex]; + BackendContext_GL::UniformBuffer &ubo = backendContext->uboRingBuffer[backendContext->uboRingBufferIndex]; - if (pData) - { - memcpy(ubo.pData, pData, size); - } + if (pData) + { + memcpy(ubo.pData, pData, size); + } - backendContext->uboRingBufferIndex++; + backendContext->uboRingBufferIndex++; - if (backendContext->uboRingBufferIndex >= FSR2_UBO_RING_BUFFER_SIZE) - { - backendContext->uboRingBufferIndex = 0; - } + if (backendContext->uboRingBufferIndex >= FSR2_UBO_RING_BUFFER_SIZE) + { + backendContext->uboRingBufferIndex = 0; + } - return ubo; + return ubo; } FfxResource ffxGetTextureResourceGL(GLuint textureGL, uint32_t width, uint32_t height, GLenum imgFormat, const wchar_t *name) { - FfxResource resource = {}; - resource.resource = reinterpret_cast(static_cast(textureGL)); - resource.descriptorData = 0; - resource.description.flags = FFX_RESOURCE_FLAGS_NONE; - resource.description.type = FFX_RESOURCE_TYPE_TEXTURE2D; - resource.description.width = width; - resource.description.height = height; - resource.description.depth = 1; - resource.description.mipCount = 1; - resource.description.format = ffxGetSurfaceFormatGL(imgFormat); - - switch (imgFormat) - { - case GL_DEPTH_COMPONENT16: - case GL_DEPTH_COMPONENT24: - case GL_DEPTH_COMPONENT32F: - case GL_DEPTH24_STENCIL8: - case GL_DEPTH32F_STENCIL8: - { - resource.isDepth = true; - break; - } - default: - { - resource.isDepth = false; - break; - } - } + FfxResource resource = {}; + resource.resource = reinterpret_cast(static_cast(textureGL)); + resource.descriptorData = 0; + resource.description.flags = FFX_RESOURCE_FLAGS_NONE; + resource.description.type = FFX_RESOURCE_TYPE_TEXTURE2D; + resource.description.width = width; + resource.description.height = height; + resource.description.depth = 1; + resource.description.mipCount = 1; + resource.description.format = ffxGetSurfaceFormatGL(imgFormat); + + switch (imgFormat) + { + case GL_DEPTH_COMPONENT16: + case GL_DEPTH_COMPONENT24: + case GL_DEPTH_COMPONENT32F: + case GL_DEPTH24_STENCIL8: + case GL_DEPTH32F_STENCIL8: + { + resource.isDepth = true; + break; + } + default: + { + resource.isDepth = false; + break; + } + } #ifdef _DEBUG - if (name) - { + if (name) + { #ifdef ON_LINUX - wcscpy(resource.name, name); + wcscpy(resource.name, name); #else - wcscpy_s(resource.name, name); + wcscpy_s(resource.name, name); #endif - } + } #endif - return resource; + return resource; } FfxResource ffxGetBufferResourceGL(GLuint bufferGL, uint32_t size, const wchar_t *name) { - FfxResource resource = {}; - resource.resource = reinterpret_cast(static_cast(bufferGL)); - resource.descriptorData = 0; - resource.description.flags = FFX_RESOURCE_FLAGS_NONE; - resource.description.type = FFX_RESOURCE_TYPE_BUFFER; - resource.description.width = size; - resource.description.height = 1; - resource.description.depth = 1; - resource.description.mipCount = 1; - resource.description.format = FFX_SURFACE_FORMAT_UNKNOWN; - resource.isDepth = false; + FfxResource resource = {}; + resource.resource = reinterpret_cast(static_cast(bufferGL)); + resource.descriptorData = 0; + resource.description.flags = FFX_RESOURCE_FLAGS_NONE; + resource.description.type = FFX_RESOURCE_TYPE_BUFFER; + resource.description.width = size; + resource.description.height = 1; + resource.description.depth = 1; + resource.description.mipCount = 1; + resource.description.format = FFX_SURFACE_FORMAT_UNKNOWN; + resource.isDepth = false; #ifdef _DEBUG - if (name) - { + if (name) + { #ifdef ON_LINUX - wcscpy(resource.name, name); + wcscpy(resource.name, name); #else - wcscpy_s(resource.name, name); + wcscpy_s(resource.name, name); #endif - } + } #endif - return resource; + return resource; } GLuint ffxGetGLImage(FfxFsr2Context *context, uint32_t resId) { - FFX_ASSERT(context); + FFX_ASSERT(context); - FfxFsr2Context_Private *contextPrivate = (FfxFsr2Context_Private *)(context); - BackendContext_GL *backendContext = (BackendContext_GL *)(contextPrivate->contextDescription.callbacks.scratchBuffer); + FfxFsr2Context_Private *contextPrivate = (FfxFsr2Context_Private *)(context); + BackendContext_GL *backendContext = (BackendContext_GL *)(contextPrivate->contextDescription.callbacks.scratchBuffer); - int32_t internalIndex = contextPrivate->uavResources[resId].internalIndex; + int32_t internalIndex = contextPrivate->uavResources[resId].internalIndex; - return (internalIndex == -1) ? 0 : backendContext->resources[internalIndex].textureAllMipsView.id; + return (internalIndex == -1) ? 0 : backendContext->resources[internalIndex].textureAllMipsView.id; } FfxErrorCode RegisterResourceGL( - FfxFsr2Interface *backendInterface, - const FfxResource *inFfxResource, - FfxResourceInternal *outFfxResourceInternal) + FfxFsr2Interface *backendInterface, + const FfxResource *inFfxResource, + FfxResourceInternal *outFfxResourceInternal) { - FFX_ASSERT(backendInterface); + FFX_ASSERT(backendInterface); - BackendContext_GL *backendContext = (BackendContext_GL *)(backendInterface->scratchBuffer); + BackendContext_GL *backendContext = (BackendContext_GL *)(backendInterface->scratchBuffer); - if (inFfxResource->resource == nullptr) - { + if (inFfxResource->resource == nullptr) + { - outFfxResourceInternal->internalIndex = FFX_FSR2_RESOURCE_IDENTIFIER_NULL; - return FFX_OK; - } + outFfxResourceInternal->internalIndex = FFX_FSR2_RESOURCE_IDENTIFIER_NULL; + return FFX_OK; + } - FFX_ASSERT(backendContext->nextDynamicResource > backendContext->nextStaticResource); - outFfxResourceInternal->internalIndex = backendContext->nextDynamicResource--; + FFX_ASSERT(backendContext->nextDynamicResource > backendContext->nextStaticResource); + outFfxResourceInternal->internalIndex = backendContext->nextDynamicResource--; - BackendContext_GL::Resource *backendResource = &backendContext->resources[outFfxResourceInternal->internalIndex]; + BackendContext_GL::Resource *backendResource = &backendContext->resources[outFfxResourceInternal->internalIndex]; - backendResource->resourceDescription = inFfxResource->description; + backendResource->resourceDescription = inFfxResource->description; #ifdef _DEBUG - size_t retval = 0; - wcstombs_s(&retval, backendResource->resourceName, sizeof(backendResource->resourceName), inFfxResource->name, sizeof(backendResource->resourceName)); - if (retval >= 64) - backendResource->resourceName[63] = '\0'; + size_t retval = 0; + wcstombs_s(&retval, backendResource->resourceName, sizeof(backendResource->resourceName), inFfxResource->name, sizeof(backendResource->resourceName)); + if (retval >= 64) + backendResource->resourceName[63] = '\0'; #endif - if (inFfxResource->description.type == FFX_RESOURCE_TYPE_BUFFER) - { - const auto buffer = static_cast(reinterpret_cast(inFfxResource->resource)); - - backendResource->buffer = {buffer}; - } - else - { - const auto texture = static_cast(reinterpret_cast(inFfxResource->resource)); - - backendResource->textureAllMipsView = {texture}; - backendResource->textureSingleMipViews[0] = {texture}; - - if (texture) - { - if (inFfxResource->isDepth) - { - backendResource->textureAspect = BackendContext_GL::Aspect::DEPTH; - } - else - { - backendResource->textureAspect = BackendContext_GL::Aspect::COLOR; - } - } - } - - return FFX_OK; + if (inFfxResource->description.type == FFX_RESOURCE_TYPE_BUFFER) + { + const auto buffer = static_cast(reinterpret_cast(inFfxResource->resource)); + + backendResource->buffer = {buffer}; + } + else + { + const auto texture = static_cast(reinterpret_cast(inFfxResource->resource)); + + backendResource->textureAllMipsView = {texture}; + backendResource->textureSingleMipViews[0] = {texture}; + + if (texture) + { + if (inFfxResource->isDepth) + { + backendResource->textureAspect = BackendContext_GL::Aspect::DEPTH; + } + else + { + backendResource->textureAspect = BackendContext_GL::Aspect::COLOR; + } + } + } + + return FFX_OK; } // dispose dynamic resources: This should be called at the end of the frame FfxErrorCode UnregisterResourcesGL(FfxFsr2Interface *backendInterface) { - FFX_ASSERT(backendInterface); + FFX_ASSERT(backendInterface); - BackendContext_GL *backendContext = (BackendContext_GL *)(backendInterface->scratchBuffer); + BackendContext_GL *backendContext = (BackendContext_GL *)(backendInterface->scratchBuffer); - backendContext->nextDynamicResource = FSR2_MAX_RESOURCE_COUNT - 1; + backendContext->nextDynamicResource = FSR2_MAX_RESOURCE_COUNT - 1; - return FFX_OK; + return FFX_OK; } FfxErrorCode GetDeviceCapabilitiesGL(FfxFsr2Interface *backendInterface, FfxDeviceCapabilities *deviceCapabilities, FfxDevice) { - FFX_ASSERT(backendInterface); - FFX_ASSERT(deviceCapabilities); - - BackendContext_GL *backendContext = (BackendContext_GL *)backendInterface->scratchBuffer; - - // no shader model in vulkan so assume the minimum - deviceCapabilities->minimumSupportedShaderModel = FFX_SHADER_MODEL_5_1; - deviceCapabilities->waveLaneCountMin = 0; - deviceCapabilities->waveLaneCountMax = 0; - deviceCapabilities->fp16Supported = false; - deviceCapabilities->raytracingSupported = false; - - // check if extensions are supported - - // Workaround: latest AMD driver does not report GL_KHR_shader_subgroup, despite the extension being supported - bool vendorIsAmd = false; - const auto *vendor = reinterpret_cast(backendContext->glFunctionTable.glGetString(GL_VENDOR)); - if (strstr(vendor, "ATI")) - { - vendorIsAmd = true; - } - - bool subgroupSupported = false; - GLint numExtensions{}; - backendContext->glFunctionTable.glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions); - for (GLint i = 0; i < numExtensions; i++) - { - const auto *extensionString = reinterpret_cast(backendContext->glFunctionTable.glGetStringi(GL_EXTENSIONS, i)); - if (vendorIsAmd || (strcmp(extensionString, "GL_KHR_shader_subgroup") == 0)) - { - GLint supportedStages{}; - backendContext->glFunctionTable.glGetIntegerv(GL_SUBGROUP_SUPPORTED_STAGES_KHR, &supportedStages); - if (supportedStages & GL_COMPUTE_SHADER_BIT) - { - subgroupSupported = true; - } - } - - if (strcmp(extensionString, "GL_NV_gpu_shader5") == 0 || strcmp(extensionString, "GL_AMD_gpu_shader_half_float") == 0) - { - deviceCapabilities->fp16Supported = true; - } - } - bool renderDocIsAttached; + FFX_ASSERT(backendInterface); + FFX_ASSERT(deviceCapabilities); + + BackendContext_GL *backendContext = (BackendContext_GL *)backendInterface->scratchBuffer; + + // no shader model in vulkan so assume the minimum + deviceCapabilities->minimumSupportedShaderModel = FFX_SHADER_MODEL_5_1; + deviceCapabilities->waveLaneCountMin = 0; + deviceCapabilities->waveLaneCountMax = 0; + deviceCapabilities->fp16Supported = false; + deviceCapabilities->raytracingSupported = false; + + // check if extensions are supported + + // Workaround: latest AMD driver does not report GL_KHR_shader_subgroup, despite the extension being supported + bool vendorIsAmd = false; + const auto *vendor = reinterpret_cast(backendContext->glFunctionTable.glGetString(GL_VENDOR)); + if (strstr(vendor, "ATI")) + { + vendorIsAmd = true; + } + + bool subgroupSupported = false; + GLint numExtensions{}; + backendContext->glFunctionTable.glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions); + for (GLint i = 0; i < numExtensions; i++) + { + const auto *extensionString = reinterpret_cast(backendContext->glFunctionTable.glGetStringi(GL_EXTENSIONS, i)); + if (vendorIsAmd || (strcmp(extensionString, "GL_KHR_shader_subgroup") == 0)) + { + GLint supportedStages{}; + backendContext->glFunctionTable.glGetIntegerv(GL_SUBGROUP_SUPPORTED_STAGES_KHR, &supportedStages); + if (supportedStages & GL_COMPUTE_SHADER_BIT) + { + subgroupSupported = true; + } + } + + if (strcmp(extensionString, "GL_NV_gpu_shader5") == 0 || strcmp(extensionString, "GL_AMD_gpu_shader_half_float") == 0) + { + deviceCapabilities->fp16Supported = true; + } + } + bool renderDocIsAttached; #ifdef ON_LINUX -renderDocIsAttached = true; + renderDocIsAttached = true; #else - // Workaround: RenderDoc prevents many extensions from being reported - // In this case, shaders that use GL_KHR_shader_subgroup will still work, but API calls using constants from it will not - renderDocIsAttached = GetModuleHandleA("renderdoc.dll"); + // Workaround: RenderDoc prevents many extensions from being reported + // In this case, shaders that use GL_KHR_shader_subgroup will still work, but API calls using constants from it will not + renderDocIsAttached = GetModuleHandleA("renderdoc.dll"); #endif - if (renderDocIsAttached) - { - subgroupSupported = true; - } - - if (!subgroupSupported) - { - return FFX_ERROR_BACKEND_API_ERROR; // GL_KHR_shader_subgroup is required - } - - GLint subgroupSize = FSR2_DEFAULT_SUBGROUP_SIZE; - if (!renderDocIsAttached) - { - backendContext->glFunctionTable.glGetIntegerv(GL_SUBGROUP_SIZE_KHR, &subgroupSize); - } - deviceCapabilities->waveLaneCountMin = static_cast(subgroupSize); - deviceCapabilities->waveLaneCountMax = static_cast(subgroupSize); - - return FFX_OK; + if (renderDocIsAttached) + { + subgroupSupported = true; + } + + if (!subgroupSupported) + { + return FFX_ERROR_BACKEND_API_ERROR; // GL_KHR_shader_subgroup is required + } + + GLint subgroupSize = FSR2_DEFAULT_SUBGROUP_SIZE; + if (!renderDocIsAttached) + { + backendContext->glFunctionTable.glGetIntegerv(GL_SUBGROUP_SIZE_KHR, &subgroupSize); + } + deviceCapabilities->waveLaneCountMin = static_cast(subgroupSize); + deviceCapabilities->waveLaneCountMax = static_cast(subgroupSize); + + return FFX_OK; } FfxErrorCode CreateBackendContextGL(FfxFsr2Interface *backendInterface, FfxDevice) { - FFX_ASSERT(backendInterface); - - // set up some internal resources we need (space for resource views and constant buffers) - BackendContext_GL *backendContext = (BackendContext_GL *)backendInterface->scratchBuffer; - - backendContext->nextStaticResource = 0; - backendContext->nextDynamicResource = FSR2_MAX_RESOURCE_COUNT - 1; - - // load OpenGL functions - loadGLFunctions(backendContext, backendContext->glFunctionTable.glGetProcAddress); - - FFX_VALIDATE(GetDeviceCapabilitiesGL(backendInterface, &backendContext->capabilities, nullptr)); - - // create samplers - backendContext->glFunctionTable.glCreateSamplers(1, &backendContext->pointSampler.id); - backendContext->glFunctionTable.glSamplerParameteri(backendContext->pointSampler.id, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); - backendContext->glFunctionTable.glSamplerParameteri(backendContext->pointSampler.id, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - backendContext->glFunctionTable.glSamplerParameteri(backendContext->pointSampler.id, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - backendContext->glFunctionTable.glSamplerParameteri(backendContext->pointSampler.id, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - backendContext->glFunctionTable.glSamplerParameteri(backendContext->pointSampler.id, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); - backendContext->glFunctionTable.glSamplerParameterf(backendContext->pointSampler.id, GL_TEXTURE_MIN_LOD, -1000); - backendContext->glFunctionTable.glSamplerParameterf(backendContext->pointSampler.id, GL_TEXTURE_MAX_LOD, 1000); - backendContext->glFunctionTable.glSamplerParameterf(backendContext->pointSampler.id, GL_TEXTURE_MAX_ANISOTROPY, 1); - - backendContext->glFunctionTable.glCreateSamplers(1, &backendContext->linearSampler.id); - backendContext->glFunctionTable.glSamplerParameteri(backendContext->linearSampler.id, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); - backendContext->glFunctionTable.glSamplerParameteri(backendContext->linearSampler.id, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - backendContext->glFunctionTable.glSamplerParameteri(backendContext->linearSampler.id, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - backendContext->glFunctionTable.glSamplerParameteri(backendContext->linearSampler.id, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - backendContext->glFunctionTable.glSamplerParameteri(backendContext->linearSampler.id, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); - backendContext->glFunctionTable.glSamplerParameterf(backendContext->linearSampler.id, GL_TEXTURE_MIN_LOD, -1000); - backendContext->glFunctionTable.glSamplerParameterf(backendContext->linearSampler.id, GL_TEXTURE_MAX_LOD, 1000); - backendContext->glFunctionTable.glSamplerParameterf(backendContext->linearSampler.id, GL_TEXTURE_MAX_ANISOTROPY, 1); - - // allocate ring buffer of uniform buffers - for (uint32_t i = 0; i < FSR2_UBO_RING_BUFFER_SIZE; i++) - { - BackendContext_GL::UniformBuffer &ubo = backendContext->uboRingBuffer[i]; - backendContext->glFunctionTable.glCreateBuffers(1, &ubo.bufferResource.id); - constexpr GLbitfield mapFlags = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT; - backendContext->glFunctionTable.glNamedBufferStorage(ubo.bufferResource.id, FSR2_UBO_SIZE, nullptr, mapFlags); - - // map the memory block - ubo.pData = (uint8_t *)backendContext->glFunctionTable.glMapNamedBufferRange(ubo.bufferResource.id, 0, FSR2_UBO_SIZE, mapFlags); - - if (!ubo.pData) - { - return FFX_ERROR_BACKEND_API_ERROR; - } - } - - backendContext->gpuJobCount = 0; - backendContext->stagingResourceCount = 0; - backendContext->uboRingBufferIndex = 0; - - return FFX_OK; + FFX_ASSERT(backendInterface); + + // set up some internal resources we need (space for resource views and constant buffers) + BackendContext_GL *backendContext = (BackendContext_GL *)backendInterface->scratchBuffer; + + backendContext->nextStaticResource = 0; + backendContext->nextDynamicResource = FSR2_MAX_RESOURCE_COUNT - 1; + + // load OpenGL functions + loadGLFunctions(backendContext, backendContext->glFunctionTable.glGetProcAddress); + + FFX_VALIDATE(GetDeviceCapabilitiesGL(backendInterface, &backendContext->capabilities, nullptr)); + + // create samplers + backendContext->glFunctionTable.glCreateSamplers(1, &backendContext->pointSampler.id); + backendContext->glFunctionTable.glSamplerParameteri(backendContext->pointSampler.id, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); + backendContext->glFunctionTable.glSamplerParameteri(backendContext->pointSampler.id, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + backendContext->glFunctionTable.glSamplerParameteri(backendContext->pointSampler.id, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + backendContext->glFunctionTable.glSamplerParameteri(backendContext->pointSampler.id, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + backendContext->glFunctionTable.glSamplerParameteri(backendContext->pointSampler.id, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); + backendContext->glFunctionTable.glSamplerParameterf(backendContext->pointSampler.id, GL_TEXTURE_MIN_LOD, -1000); + backendContext->glFunctionTable.glSamplerParameterf(backendContext->pointSampler.id, GL_TEXTURE_MAX_LOD, 1000); + backendContext->glFunctionTable.glSamplerParameterf(backendContext->pointSampler.id, GL_TEXTURE_MAX_ANISOTROPY, 1); + + backendContext->glFunctionTable.glCreateSamplers(1, &backendContext->linearSampler.id); + backendContext->glFunctionTable.glSamplerParameteri(backendContext->linearSampler.id, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); + backendContext->glFunctionTable.glSamplerParameteri(backendContext->linearSampler.id, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + backendContext->glFunctionTable.glSamplerParameteri(backendContext->linearSampler.id, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + backendContext->glFunctionTable.glSamplerParameteri(backendContext->linearSampler.id, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + backendContext->glFunctionTable.glSamplerParameteri(backendContext->linearSampler.id, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); + backendContext->glFunctionTable.glSamplerParameterf(backendContext->linearSampler.id, GL_TEXTURE_MIN_LOD, -1000); + backendContext->glFunctionTable.glSamplerParameterf(backendContext->linearSampler.id, GL_TEXTURE_MAX_LOD, 1000); + backendContext->glFunctionTable.glSamplerParameterf(backendContext->linearSampler.id, GL_TEXTURE_MAX_ANISOTROPY, 1); + + // allocate ring buffer of uniform buffers + for (uint32_t i = 0; i < FSR2_UBO_RING_BUFFER_SIZE; i++) + { + BackendContext_GL::UniformBuffer &ubo = backendContext->uboRingBuffer[i]; + backendContext->glFunctionTable.glCreateBuffers(1, &ubo.bufferResource.id); + constexpr GLbitfield mapFlags = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT; + backendContext->glFunctionTable.glNamedBufferStorage(ubo.bufferResource.id, FSR2_UBO_SIZE, nullptr, mapFlags); + + // map the memory block + ubo.pData = (uint8_t *)backendContext->glFunctionTable.glMapNamedBufferRange(ubo.bufferResource.id, 0, FSR2_UBO_SIZE, mapFlags); + + if (!ubo.pData) + { + return FFX_ERROR_BACKEND_API_ERROR; + } + } + + backendContext->gpuJobCount = 0; + backendContext->stagingResourceCount = 0; + backendContext->uboRingBufferIndex = 0; + + return FFX_OK; } FfxErrorCode DestroyBackendContextGL(FfxFsr2Interface *backendInterface) { - FFX_ASSERT(backendInterface); + FFX_ASSERT(backendInterface); - BackendContext_GL *backendContext = (BackendContext_GL *)backendInterface->scratchBuffer; + BackendContext_GL *backendContext = (BackendContext_GL *)backendInterface->scratchBuffer; - for (uint32_t i = 0; i < backendContext->stagingResourceCount; i++) - { - DestroyResourceGL(backendInterface, backendContext->stagingResources[i]); - } + for (uint32_t i = 0; i < backendContext->stagingResourceCount; i++) + { + DestroyResourceGL(backendInterface, backendContext->stagingResources[i]); + } - for (uint32_t i = 0; i < FSR2_UBO_RING_BUFFER_SIZE; i++) - { - BackendContext_GL::UniformBuffer &ubo = backendContext->uboRingBuffer[i]; + for (uint32_t i = 0; i < FSR2_UBO_RING_BUFFER_SIZE; i++) + { + BackendContext_GL::UniformBuffer &ubo = backendContext->uboRingBuffer[i]; - // buffer is implicitly unmapped by deleting it - backendContext->glFunctionTable.glDeleteBuffers(1, &ubo.bufferResource.id); - } + // buffer is implicitly unmapped by deleting it + backendContext->glFunctionTable.glDeleteBuffers(1, &ubo.bufferResource.id); + } - backendContext->glFunctionTable.glDeleteSamplers(1, &backendContext->pointSampler.id); - backendContext->glFunctionTable.glDeleteSamplers(1, &backendContext->linearSampler.id); + backendContext->glFunctionTable.glDeleteSamplers(1, &backendContext->pointSampler.id); + backendContext->glFunctionTable.glDeleteSamplers(1, &backendContext->linearSampler.id); - // clear all the fields of the context - *backendContext = {}; + // clear all the fields of the context + *backendContext = {}; - return FFX_OK; + return FFX_OK; } // create a internal resource that will stay alive until effect gets shut down FfxErrorCode CreateResourceGL( - FfxFsr2Interface *backendInterface, - const FfxCreateResourceDescription *createResourceDescription, - FfxResourceInternal *outResource) + FfxFsr2Interface *backendInterface, + const FfxCreateResourceDescription *createResourceDescription, + FfxResourceInternal *outResource) { - FFX_ASSERT(backendInterface); - FFX_ASSERT(createResourceDescription); - FFX_ASSERT(outResource); + FFX_ASSERT(backendInterface); + FFX_ASSERT(createResourceDescription); + FFX_ASSERT(outResource); - BackendContext_GL *backendContext = (BackendContext_GL *)backendInterface->scratchBuffer; + BackendContext_GL *backendContext = (BackendContext_GL *)backendInterface->scratchBuffer; - FFX_ASSERT(backendContext->nextStaticResource + 1 < backendContext->nextDynamicResource); - outResource->internalIndex = backendContext->nextStaticResource++; - BackendContext_GL::Resource *res = &backendContext->resources[outResource->internalIndex]; - res->resourceDescription = createResourceDescription->resourceDescription; - res->resourceDescription.mipCount = createResourceDescription->resourceDescription.mipCount; + FFX_ASSERT(backendContext->nextStaticResource + 1 < backendContext->nextDynamicResource); + outResource->internalIndex = backendContext->nextStaticResource++; + BackendContext_GL::Resource *res = &backendContext->resources[outResource->internalIndex]; + res->resourceDescription = createResourceDescription->resourceDescription; + res->resourceDescription.mipCount = createResourceDescription->resourceDescription.mipCount; - if (res->resourceDescription.mipCount == 0) - { - res->resourceDescription.mipCount = (uint32_t)(1 + floor(log2(FFX_MAXIMUM(FFX_MAXIMUM(createResourceDescription->resourceDescription.width, createResourceDescription->resourceDescription.height), createResourceDescription->resourceDescription.depth)))); - } + if (res->resourceDescription.mipCount == 0) + { + res->resourceDescription.mipCount = (uint32_t)(1 + floor(log2(FFX_MAXIMUM(FFX_MAXIMUM(createResourceDescription->resourceDescription.width, createResourceDescription->resourceDescription.height), createResourceDescription->resourceDescription.depth)))); + } #ifdef _DEBUG - size_t retval = 0; - wcstombs_s(&retval, res->resourceName, sizeof(res->resourceName), createResourceDescription->name, sizeof(res->resourceName)); - if (retval >= 64) - res->resourceName[63] = '\0'; + size_t retval = 0; + wcstombs_s(&retval, res->resourceName, sizeof(res->resourceName), createResourceDescription->name, sizeof(res->resourceName)); + if (retval >= 64) + res->resourceName[63] = '\0'; #endif - switch (createResourceDescription->resourceDescription.type) - { - case FFX_RESOURCE_TYPE_BUFFER: - { - if (createResourceDescription->initData) - { - FFX_ASSERT(createResourceDescription->resourceDescription.width == createResourceDescription->initDataSize); - } - - backendContext->glFunctionTable.glCreateBuffers(1, &res->buffer.id); - backendContext->glFunctionTable.glNamedBufferStorage( - res->buffer.id, - createResourceDescription->resourceDescription.width, - createResourceDescription->initData, - 0); + switch (createResourceDescription->resourceDescription.type) + { + case FFX_RESOURCE_TYPE_BUFFER: + { + if (createResourceDescription->initData) + { + FFX_ASSERT(createResourceDescription->resourceDescription.width == createResourceDescription->initDataSize); + } + + backendContext->glFunctionTable.glCreateBuffers(1, &res->buffer.id); + backendContext->glFunctionTable.glNamedBufferStorage( + res->buffer.id, + createResourceDescription->resourceDescription.width, + createResourceDescription->initData, + 0); #ifdef _DEBUG - backendContext->glFunctionTable.glObjectLabel(GL_BUFFER, res->buffer.id, -1, res->resourceName); + backendContext->glFunctionTable.glObjectLabel(GL_BUFFER, res->buffer.id, -1, res->resourceName); #endif - break; - } - case FFX_RESOURCE_TYPE_TEXTURE1D: - { - backendContext->glFunctionTable.glCreateTextures(GL_TEXTURE_1D, 1, &res->textureAllMipsView.id); - backendContext->glFunctionTable.glTextureStorage1D( - res->textureAllMipsView.id, - res->resourceDescription.mipCount, - getGLFormatFromSurfaceFormat(createResourceDescription->resourceDescription.format), - createResourceDescription->resourceDescription.width); - - if (createResourceDescription->initData) - { - backendContext->glFunctionTable.glad_glTextureSubImage1D( - res->textureAllMipsView.id, - 0, - 0, - createResourceDescription->resourceDescription.width, - getGLUploadFormatFromSurfaceFormat(createResourceDescription->resourceDescription.format), - getGLUploadTypeFromSurfaceFormat(createResourceDescription->resourceDescription.format), - createResourceDescription->initData); - } - - break; - } - case FFX_RESOURCE_TYPE_TEXTURE2D: - { - backendContext->glFunctionTable.glCreateTextures(GL_TEXTURE_2D, 1, &res->textureAllMipsView.id); - backendContext->glFunctionTable.glTextureStorage2D( - res->textureAllMipsView.id, - res->resourceDescription.mipCount, - getGLFormatFromSurfaceFormat(createResourceDescription->resourceDescription.format), - createResourceDescription->resourceDescription.width, - createResourceDescription->resourceDescription.height); - - if (createResourceDescription->initData) - { - backendContext->glFunctionTable.glad_glTextureSubImage2D( - res->textureAllMipsView.id, - 0, - 0, - 0, - createResourceDescription->resourceDescription.width, - createResourceDescription->resourceDescription.height, - getGLUploadFormatFromSurfaceFormat(createResourceDescription->resourceDescription.format), - getGLUploadTypeFromSurfaceFormat(createResourceDescription->resourceDescription.format), - createResourceDescription->initData); - } - - break; - } - case FFX_RESOURCE_TYPE_TEXTURE3D: - { - backendContext->glFunctionTable.glCreateTextures(GL_TEXTURE_3D, 1, &res->textureAllMipsView.id); - backendContext->glFunctionTable.glTextureStorage3D( - res->textureAllMipsView.id, - res->resourceDescription.mipCount, - getGLFormatFromSurfaceFormat(createResourceDescription->resourceDescription.format), - createResourceDescription->resourceDescription.width, - createResourceDescription->resourceDescription.height, - createResourceDescription->resourceDescription.depth); - - if (createResourceDescription->initData) - { - backendContext->glFunctionTable.glad_glTextureSubImage3D( - res->textureAllMipsView.id, - 0, - 0, - 0, - 0, - createResourceDescription->resourceDescription.width, - createResourceDescription->resourceDescription.height, - createResourceDescription->resourceDescription.depth, - getGLUploadFormatFromSurfaceFormat(createResourceDescription->resourceDescription.format), - getGLUploadTypeFromSurfaceFormat(createResourceDescription->resourceDescription.format), - createResourceDescription->initData); - } - break; - } - default:; - } - - if (createResourceDescription->resourceDescription.type != FFX_RESOURCE_TYPE_BUFFER) - { - GLenum type = 0; - switch (createResourceDescription->resourceDescription.type) - { - case FFX_RESOURCE_TYPE_TEXTURE1D: - type = GL_TEXTURE_1D; - break; - case FFX_RESOURCE_TYPE_TEXTURE2D: - type = GL_TEXTURE_2D; - break; - case FFX_RESOURCE_TYPE_TEXTURE3D: - type = GL_TEXTURE_3D; - break; - } - - res->textureAspect = BackendContext_GL::Aspect::COLOR; - - for (uint32_t i = 0; i < res->resourceDescription.mipCount; i++) - { - backendContext->glFunctionTable.glGenTextures(1, &res->textureSingleMipViews[i].id); - backendContext->glFunctionTable.glTextureView( - res->textureSingleMipViews[i].id, - type, - res->textureAllMipsView.id, - getGLFormatFromSurfaceFormat(createResourceDescription->resourceDescription.format), - i, - 1, - 0, - 1); - - // texture view name + break; + } + case FFX_RESOURCE_TYPE_TEXTURE1D: + { + backendContext->glFunctionTable.glCreateTextures(GL_TEXTURE_1D, 1, &res->textureAllMipsView.id); + backendContext->glFunctionTable.glTextureStorage1D( + res->textureAllMipsView.id, + res->resourceDescription.mipCount, + getGLFormatFromSurfaceFormat(createResourceDescription->resourceDescription.format), + createResourceDescription->resourceDescription.width); + + if (createResourceDescription->initData) + { + backendContext->glFunctionTable.glad_glTextureSubImage1D( + res->textureAllMipsView.id, + 0, + 0, + createResourceDescription->resourceDescription.width, + getGLUploadFormatFromSurfaceFormat(createResourceDescription->resourceDescription.format), + getGLUploadTypeFromSurfaceFormat(createResourceDescription->resourceDescription.format), + createResourceDescription->initData); + } + + break; + } + case FFX_RESOURCE_TYPE_TEXTURE2D: + { + backendContext->glFunctionTable.glCreateTextures(GL_TEXTURE_2D, 1, &res->textureAllMipsView.id); + backendContext->glFunctionTable.glTextureStorage2D( + res->textureAllMipsView.id, + res->resourceDescription.mipCount, + getGLFormatFromSurfaceFormat(createResourceDescription->resourceDescription.format), + createResourceDescription->resourceDescription.width, + createResourceDescription->resourceDescription.height); + + if (createResourceDescription->initData) + { + backendContext->glFunctionTable.glad_glTextureSubImage2D( + res->textureAllMipsView.id, + 0, + 0, + 0, + createResourceDescription->resourceDescription.width, + createResourceDescription->resourceDescription.height, + getGLUploadFormatFromSurfaceFormat(createResourceDescription->resourceDescription.format), + getGLUploadTypeFromSurfaceFormat(createResourceDescription->resourceDescription.format), + createResourceDescription->initData); + } + + break; + } + case FFX_RESOURCE_TYPE_TEXTURE3D: + { + backendContext->glFunctionTable.glCreateTextures(GL_TEXTURE_3D, 1, &res->textureAllMipsView.id); + backendContext->glFunctionTable.glTextureStorage3D( + res->textureAllMipsView.id, + res->resourceDescription.mipCount, + getGLFormatFromSurfaceFormat(createResourceDescription->resourceDescription.format), + createResourceDescription->resourceDescription.width, + createResourceDescription->resourceDescription.height, + createResourceDescription->resourceDescription.depth); + + if (createResourceDescription->initData) + { + backendContext->glFunctionTable.glad_glTextureSubImage3D( + res->textureAllMipsView.id, + 0, + 0, + 0, + 0, + createResourceDescription->resourceDescription.width, + createResourceDescription->resourceDescription.height, + createResourceDescription->resourceDescription.depth, + getGLUploadFormatFromSurfaceFormat(createResourceDescription->resourceDescription.format), + getGLUploadTypeFromSurfaceFormat(createResourceDescription->resourceDescription.format), + createResourceDescription->initData); + } + break; + } + default:; + } + + if (createResourceDescription->resourceDescription.type != FFX_RESOURCE_TYPE_BUFFER) + { + GLenum type = 0; + switch (createResourceDescription->resourceDescription.type) + { + case FFX_RESOURCE_TYPE_TEXTURE1D: + type = GL_TEXTURE_1D; + break; + case FFX_RESOURCE_TYPE_TEXTURE2D: + type = GL_TEXTURE_2D; + break; + case FFX_RESOURCE_TYPE_TEXTURE3D: + type = GL_TEXTURE_3D; + break; + } + + res->textureAspect = BackendContext_GL::Aspect::COLOR; + + for (uint32_t i = 0; i < res->resourceDescription.mipCount; i++) + { + backendContext->glFunctionTable.glGenTextures(1, &res->textureSingleMipViews[i].id); + backendContext->glFunctionTable.glTextureView( + res->textureSingleMipViews[i].id, + type, + res->textureAllMipsView.id, + getGLFormatFromSurfaceFormat(createResourceDescription->resourceDescription.format), + i, + 1, + 0, + 1); + + // texture view name #ifdef _DEBUG - backendContext->glFunctionTable.glObjectLabel(GL_TEXTURE, res->textureSingleMipViews[i].id, -1, res->resourceName); + backendContext->glFunctionTable.glObjectLabel(GL_TEXTURE, res->textureSingleMipViews[i].id, -1, res->resourceName); #endif - } + } - // texture name + // texture name #ifdef _DEBUG - backendContext->glFunctionTable.glObjectLabel(GL_TEXTURE, res->textureAllMipsView.id, -1, res->resourceName); + backendContext->glFunctionTable.glObjectLabel(GL_TEXTURE, res->textureAllMipsView.id, -1, res->resourceName); #endif - } + } - return FFX_OK; + return FFX_OK; } FfxResourceDescription GetResourceDescriptorGL(FfxFsr2Interface *backendInterface, FfxResourceInternal resource) { - FFX_ASSERT(backendInterface); + FFX_ASSERT(backendInterface); - BackendContext_GL *backendContext = (BackendContext_GL *)backendInterface->scratchBuffer; + BackendContext_GL *backendContext = (BackendContext_GL *)backendInterface->scratchBuffer; - if (resource.internalIndex == -1) - { - return {}; - } + if (resource.internalIndex == -1) + { + return {}; + } - return backendContext->resources[resource.internalIndex].resourceDescription; + return backendContext->resources[resource.internalIndex].resourceDescription; } FfxErrorCode CreatePipelineGL(FfxFsr2Interface *backendInterface, FfxFsr2Pass pass, const FfxPipelineDescription *pipelineDescription, FfxPipelineState *outPipeline) { - FFX_ASSERT(backendInterface); - FFX_ASSERT(pipelineDescription); - - BackendContext_GL *backendContext = (BackendContext_GL *)backendInterface->scratchBuffer; - - // query device capabilities - FfxDeviceCapabilities deviceCapabilities; - - GetDeviceCapabilitiesGL(backendInterface, &deviceCapabilities, nullptr); - - bool useLut = false; - - if (deviceCapabilities.waveLaneCountMax == 64) - { - useLut = true; - } - - // check if we have 16bit floating point. - bool supportedFP16 = deviceCapabilities.fp16Supported; - - if (pass == FFX_FSR2_PASS_ACCUMULATE || pass == FFX_FSR2_PASS_ACCUMULATE_SHARPEN) - { - // Workaround: Disable FP16 path for the accumulate pass on NVIDIA due to reduced occupancy and high VRAM throughput. - const auto *vendor = reinterpret_cast(backendContext->glFunctionTable.glGetString(GL_VENDOR)); - if (strstr(vendor, "NVIDIA")) - { - supportedFP16 = false; - } - } - - // work out what permutation to load. - uint32_t flags = 0; - flags |= (pipelineDescription->contextFlags & FFX_FSR2_ENABLE_HIGH_DYNAMIC_RANGE) ? FSR2_SHADER_PERMUTATION_HDR_COLOR_INPUT : 0; - flags |= (pipelineDescription->contextFlags & FFX_FSR2_ENABLE_DISPLAY_RESOLUTION_MOTION_VECTORS) ? 0 : FSR2_SHADER_PERMUTATION_LOW_RES_MOTION_VECTORS; - flags |= (pipelineDescription->contextFlags & FFX_FSR2_ENABLE_MOTION_VECTORS_JITTER_CANCELLATION) ? FSR2_SHADER_PERMUTATION_JITTER_MOTION_VECTORS : 0; - flags |= (pipelineDescription->contextFlags & FFX_FSR2_ENABLE_DEPTH_INVERTED) ? FSR2_SHADER_PERMUTATION_DEPTH_INVERTED : 0; - flags |= (pass == FFX_FSR2_PASS_ACCUMULATE_SHARPEN) ? FSR2_SHADER_PERMUTATION_ENABLE_SHARPENING : 0; - flags |= (useLut) ? FSR2_SHADER_PERMUTATION_REPROJECT_USE_LANCZOS_TYPE : 0; - // flags |= (canForceWave64) ? FSR2_SHADER_PERMUTATION_FORCE_WAVE64 : 0; // cannot force wave64 in OpenGL - flags |= (supportedFP16 && (pass != FFX_FSR2_PASS_RCAS)) ? FSR2_SHADER_PERMUTATION_ALLOW_FP16 : 0; - - const Fsr2ShaderBlobGL shaderBlob = fsr2GetPermutationBlobByIndexGL(pass, flags); - FFX_ASSERT(shaderBlob.data && shaderBlob.size); - - // populate the pass. - outPipeline->srvCount = shaderBlob.combinedSamplerCount; - outPipeline->uavCount = shaderBlob.storageImageCount; - outPipeline->constCount = shaderBlob.uniformBufferCount; - - FFX_ASSERT(shaderBlob.storageImageCount < FFX_MAX_NUM_UAVS); - FFX_ASSERT(shaderBlob.combinedSamplerCount < FFX_MAX_NUM_SRVS); - std::wstring_convert> converter; - - for (uint32_t srvIndex = 0; srvIndex < outPipeline->srvCount; ++srvIndex) - { - outPipeline->srvResourceBindings[srvIndex].slotIndex = shaderBlob.boundCombinedSamplerBindings[srvIndex]; + FFX_ASSERT(backendInterface); + FFX_ASSERT(pipelineDescription); + + BackendContext_GL *backendContext = (BackendContext_GL *)backendInterface->scratchBuffer; + + // query device capabilities + FfxDeviceCapabilities deviceCapabilities; + + GetDeviceCapabilitiesGL(backendInterface, &deviceCapabilities, nullptr); + + bool useLut = false; + + if (deviceCapabilities.waveLaneCountMax == 64) + { + useLut = true; + } + + // check if we have 16bit floating point. + bool supportedFP16 = deviceCapabilities.fp16Supported; + + if (pass == FFX_FSR2_PASS_ACCUMULATE || pass == FFX_FSR2_PASS_ACCUMULATE_SHARPEN) + { + // Workaround: Disable FP16 path for the accumulate pass on NVIDIA due to reduced occupancy and high VRAM throughput. + const auto *vendor = reinterpret_cast(backendContext->glFunctionTable.glGetString(GL_VENDOR)); + if (strstr(vendor, "NVIDIA")) + { + supportedFP16 = false; + } + } + + // work out what permutation to load. + uint32_t flags = 0; + flags |= (pipelineDescription->contextFlags & FFX_FSR2_ENABLE_HIGH_DYNAMIC_RANGE) ? FSR2_SHADER_PERMUTATION_HDR_COLOR_INPUT : 0; + flags |= (pipelineDescription->contextFlags & FFX_FSR2_ENABLE_DISPLAY_RESOLUTION_MOTION_VECTORS) ? 0 : FSR2_SHADER_PERMUTATION_LOW_RES_MOTION_VECTORS; + flags |= (pipelineDescription->contextFlags & FFX_FSR2_ENABLE_MOTION_VECTORS_JITTER_CANCELLATION) ? FSR2_SHADER_PERMUTATION_JITTER_MOTION_VECTORS : 0; + flags |= (pipelineDescription->contextFlags & FFX_FSR2_ENABLE_DEPTH_INVERTED) ? FSR2_SHADER_PERMUTATION_DEPTH_INVERTED : 0; + flags |= (pass == FFX_FSR2_PASS_ACCUMULATE_SHARPEN) ? FSR2_SHADER_PERMUTATION_ENABLE_SHARPENING : 0; + flags |= (useLut) ? FSR2_SHADER_PERMUTATION_REPROJECT_USE_LANCZOS_TYPE : 0; + // flags |= (canForceWave64) ? FSR2_SHADER_PERMUTATION_FORCE_WAVE64 : 0; // cannot force wave64 in OpenGL + flags |= (supportedFP16 && (pass != FFX_FSR2_PASS_RCAS)) ? FSR2_SHADER_PERMUTATION_ALLOW_FP16 : 0; + + const Fsr2ShaderBlobGL shaderBlob = fsr2GetPermutationBlobByIndexGL(pass, flags); + FFX_ASSERT(shaderBlob.data && shaderBlob.size); + + // populate the pass. + outPipeline->srvCount = shaderBlob.combinedSamplerCount; + outPipeline->uavCount = shaderBlob.storageImageCount; + outPipeline->constCount = shaderBlob.uniformBufferCount; + + FFX_ASSERT(shaderBlob.storageImageCount < FFX_MAX_NUM_UAVS); + FFX_ASSERT(shaderBlob.combinedSamplerCount < FFX_MAX_NUM_SRVS); + std::wstring_convert> converter; + + for (uint32_t srvIndex = 0; srvIndex < outPipeline->srvCount; ++srvIndex) + { + outPipeline->srvResourceBindings[srvIndex].slotIndex = shaderBlob.boundCombinedSamplerBindings[srvIndex]; #ifdef ON_LINUX - wcscpy(outPipeline->srvResourceBindings[srvIndex].name, converter.from_bytes(shaderBlob.boundCombinedSamplerNames[srvIndex]).c_str()); + wcscpy(outPipeline->srvResourceBindings[srvIndex].name, converter.from_bytes(shaderBlob.boundCombinedSamplerNames[srvIndex]).c_str()); #else - wcscpy_s(outPipeline->srvResourceBindings[srvIndex].name, converter.from_bytes(shaderBlob.boundCombinedSamplerNames[srvIndex]).c_str()); + wcscpy_s(outPipeline->srvResourceBindings[srvIndex].name, converter.from_bytes(shaderBlob.boundCombinedSamplerNames[srvIndex]).c_str()); #endif - } - for (uint32_t uavIndex = 0; uavIndex < outPipeline->uavCount; ++uavIndex) - { - outPipeline->uavResourceBindings[uavIndex].slotIndex = shaderBlob.boundStorageImageBindings[uavIndex]; + } + for (uint32_t uavIndex = 0; uavIndex < outPipeline->uavCount; ++uavIndex) + { + outPipeline->uavResourceBindings[uavIndex].slotIndex = shaderBlob.boundStorageImageBindings[uavIndex]; #ifdef ON_LINUX - wcscpy(outPipeline->uavResourceBindings[uavIndex].name, converter.from_bytes(shaderBlob.boundStorageImageNames[uavIndex]).c_str()); + wcscpy(outPipeline->uavResourceBindings[uavIndex].name, converter.from_bytes(shaderBlob.boundStorageImageNames[uavIndex]).c_str()); #else - wcscpy_s(outPipeline->uavResourceBindings[uavIndex].name, converter.from_bytes(shaderBlob.boundStorageImageNames[uavIndex]).c_str()); + wcscpy_s(outPipeline->uavResourceBindings[uavIndex].name, converter.from_bytes(shaderBlob.boundStorageImageNames[uavIndex]).c_str()); #endif - } - for (uint32_t cbIndex = 0; cbIndex < outPipeline->constCount; ++cbIndex) - { - outPipeline->cbResourceBindings[cbIndex].slotIndex = shaderBlob.boundUniformBufferBindings[cbIndex]; + } + for (uint32_t cbIndex = 0; cbIndex < outPipeline->constCount; ++cbIndex) + { + outPipeline->cbResourceBindings[cbIndex].slotIndex = shaderBlob.boundUniformBufferBindings[cbIndex]; #ifdef ON_LINUX - wcscpy(outPipeline->cbResourceBindings[cbIndex].name, converter.from_bytes(shaderBlob.boundUniformBufferNames[cbIndex]).c_str()); + wcscpy(outPipeline->cbResourceBindings[cbIndex].name, converter.from_bytes(shaderBlob.boundUniformBufferNames[cbIndex]).c_str()); #else - wcscpy_s(outPipeline->cbResourceBindings[cbIndex].name, converter.from_bytes(shaderBlob.boundUniformBufferNames[cbIndex]).c_str()); + wcscpy_s(outPipeline->cbResourceBindings[cbIndex].name, converter.from_bytes(shaderBlob.boundUniformBufferNames[cbIndex]).c_str()); #endif - } - - // create the shader module - GLuint shader = backendContext->glFunctionTable.glCreateShader(GL_COMPUTE_SHADER); - backendContext->glFunctionTable.glShaderBinary(1, &shader, GL_SHADER_BINARY_FORMAT_SPIR_V, shaderBlob.data, shaderBlob.size); - backendContext->glFunctionTable.glSpecializeShader(shader, "main", 0, nullptr, nullptr); - - GLint compileStatus{}; - backendContext->glFunctionTable.glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus); - if (compileStatus == GL_FALSE) - { - return FFX_ERROR_BACKEND_API_ERROR; - } - - // create the compute pipeline - GLuint program = backendContext->glFunctionTable.glCreateProgram(); - backendContext->glFunctionTable.glAttachShader(program, shader); - backendContext->glFunctionTable.glLinkProgram(program); - - GLint linkStatus{}; - backendContext->glFunctionTable.glGetProgramiv(program, GL_LINK_STATUS, &linkStatus); - if (linkStatus == GL_FALSE) - { - backendContext->glFunctionTable.glDeleteShader(shader); - return FFX_ERROR_BACKEND_API_ERROR; - } - - backendContext->glFunctionTable.glDeleteShader(shader); - - outPipeline->pipeline = reinterpret_cast(static_cast(program)); - outPipeline->rootSignature = nullptr; - - return FFX_OK; + } + + // create the shader module + GLuint shader = backendContext->glFunctionTable.glCreateShader(GL_COMPUTE_SHADER); + backendContext->glFunctionTable.glShaderBinary(1, &shader, GL_SHADER_BINARY_FORMAT_SPIR_V, shaderBlob.data, shaderBlob.size); + backendContext->glFunctionTable.glSpecializeShader(shader, "main", 0, nullptr, nullptr); + + GLint compileStatus{}; + backendContext->glFunctionTable.glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus); + if (compileStatus == GL_FALSE) + { + return FFX_ERROR_BACKEND_API_ERROR; + } + + // create the compute pipeline + GLuint program = backendContext->glFunctionTable.glCreateProgram(); + backendContext->glFunctionTable.glAttachShader(program, shader); + backendContext->glFunctionTable.glLinkProgram(program); + + GLint linkStatus{}; + backendContext->glFunctionTable.glGetProgramiv(program, GL_LINK_STATUS, &linkStatus); + if (linkStatus == GL_FALSE) + { + backendContext->glFunctionTable.glDeleteShader(shader); + return FFX_ERROR_BACKEND_API_ERROR; + } + + backendContext->glFunctionTable.glDeleteShader(shader); + + outPipeline->pipeline = reinterpret_cast(static_cast(program)); + outPipeline->rootSignature = nullptr; + + return FFX_OK; } FfxErrorCode ScheduleGpuJobGL(FfxFsr2Interface *backendInterface, const FfxGpuJobDescription *job) { - FFX_ASSERT(backendInterface); - FFX_ASSERT(job); + FFX_ASSERT(backendInterface); + FFX_ASSERT(job); - BackendContext_GL *backendContext = (BackendContext_GL *)backendInterface->scratchBuffer; + BackendContext_GL *backendContext = (BackendContext_GL *)backendInterface->scratchBuffer; - FFX_ASSERT(backendContext->gpuJobCount < FSR2_MAX_GPU_JOBS); + FFX_ASSERT(backendContext->gpuJobCount < FSR2_MAX_GPU_JOBS); - backendContext->gpuJobs[backendContext->gpuJobCount] = *job; + backendContext->gpuJobs[backendContext->gpuJobCount] = *job; - if (job->jobType == FFX_GPU_JOB_COMPUTE) - { - // needs to copy SRVs and UAVs in case they are on the stack only - FfxComputeJobDescription *computeJob = &backendContext->gpuJobs[backendContext->gpuJobCount].computeJobDescriptor; - const uint32_t numConstBuffers = job->computeJobDescriptor.pipeline.constCount; - for (uint32_t currentRootConstantIndex = 0; currentRootConstantIndex < numConstBuffers; ++currentRootConstantIndex) - { - computeJob->cbs[currentRootConstantIndex].uint32Size = job->computeJobDescriptor.cbs[currentRootConstantIndex].uint32Size; - memcpy(computeJob->cbs[currentRootConstantIndex].data, job->computeJobDescriptor.cbs[currentRootConstantIndex].data, computeJob->cbs[currentRootConstantIndex].uint32Size * sizeof(uint32_t)); - } - } + if (job->jobType == FFX_GPU_JOB_COMPUTE) + { + // needs to copy SRVs and UAVs in case they are on the stack only + FfxComputeJobDescription *computeJob = &backendContext->gpuJobs[backendContext->gpuJobCount].computeJobDescriptor; + const uint32_t numConstBuffers = job->computeJobDescriptor.pipeline.constCount; + for (uint32_t currentRootConstantIndex = 0; currentRootConstantIndex < numConstBuffers; ++currentRootConstantIndex) + { + computeJob->cbs[currentRootConstantIndex].uint32Size = job->computeJobDescriptor.cbs[currentRootConstantIndex].uint32Size; + memcpy(computeJob->cbs[currentRootConstantIndex].data, job->computeJobDescriptor.cbs[currentRootConstantIndex].data, computeJob->cbs[currentRootConstantIndex].uint32Size * sizeof(uint32_t)); + } + } - backendContext->gpuJobCount++; + backendContext->gpuJobCount++; - return FFX_OK; + return FFX_OK; } static void addBarrier(const BackendContext_GL *backendContext, bool isBufferBarrier, FfxResourceStates newState) { - FFX_ASSERT(backendContext); - - if (isBufferBarrier) - { - GLbitfield barriers = 0; - barriers |= (newState & FFX_RESOURCE_STATE_UNORDERED_ACCESS) ? GL_SHADER_STORAGE_BARRIER_BIT : 0; - barriers |= (newState & FFX_RESOURCE_STATE_COMPUTE_READ) ? GL_UNIFORM_BARRIER_BIT : 0; - barriers |= (newState & FFX_RESOURCE_STATE_COPY_SRC) ? (GL_BUFFER_UPDATE_BARRIER_BIT | GL_PIXEL_BUFFER_BARRIER_BIT) : 0; - barriers |= (newState & FFX_RESOURCE_STATE_COPY_DEST) ? (GL_BUFFER_UPDATE_BARRIER_BIT | GL_PIXEL_BUFFER_BARRIER_BIT) : 0; - backendContext->glFunctionTable.glMemoryBarrier(barriers); - } - else - { - GLbitfield barriers = 0; - barriers |= (newState & FFX_RESOURCE_STATE_UNORDERED_ACCESS) ? GL_SHADER_IMAGE_ACCESS_BARRIER_BIT : 0; - barriers |= (newState & FFX_RESOURCE_STATE_COMPUTE_READ) ? (GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT) : 0; - barriers |= (newState & FFX_RESOURCE_STATE_COPY_SRC) ? GL_TEXTURE_UPDATE_BARRIER_BIT : 0; - barriers |= (newState & FFX_RESOURCE_STATE_COPY_DEST) ? GL_TEXTURE_UPDATE_BARRIER_BIT : 0; - backendContext->glFunctionTable.glMemoryBarrier(barriers); - } + FFX_ASSERT(backendContext); + + if (isBufferBarrier) + { + GLbitfield barriers = 0; + barriers |= (newState & FFX_RESOURCE_STATE_UNORDERED_ACCESS) ? GL_SHADER_STORAGE_BARRIER_BIT : 0; + barriers |= (newState & FFX_RESOURCE_STATE_COMPUTE_READ) ? GL_UNIFORM_BARRIER_BIT : 0; + barriers |= (newState & FFX_RESOURCE_STATE_COPY_SRC) ? (GL_BUFFER_UPDATE_BARRIER_BIT | GL_PIXEL_BUFFER_BARRIER_BIT) : 0; + barriers |= (newState & FFX_RESOURCE_STATE_COPY_DEST) ? (GL_BUFFER_UPDATE_BARRIER_BIT | GL_PIXEL_BUFFER_BARRIER_BIT) : 0; + backendContext->glFunctionTable.glMemoryBarrier(barriers); + } + else + { + GLbitfield barriers = 0; + barriers |= (newState & FFX_RESOURCE_STATE_UNORDERED_ACCESS) ? GL_SHADER_IMAGE_ACCESS_BARRIER_BIT : 0; + barriers |= (newState & FFX_RESOURCE_STATE_COMPUTE_READ) ? (GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT) : 0; + barriers |= (newState & FFX_RESOURCE_STATE_COPY_SRC) ? GL_TEXTURE_UPDATE_BARRIER_BIT : 0; + barriers |= (newState & FFX_RESOURCE_STATE_COPY_DEST) ? GL_TEXTURE_UPDATE_BARRIER_BIT : 0; + backendContext->glFunctionTable.glMemoryBarrier(barriers); + } } static FfxErrorCode executeGpuJobCompute(BackendContext_GL *backendContext, FfxGpuJobDescription *job) { - FFX_ASSERT(backendContext); - - const auto program = static_cast(reinterpret_cast(job->computeJobDescriptor.pipeline.pipeline)); - - // bind uavs (storage images) - if (job->computeJobDescriptor.pipeline.uavCount > 0) - { - addBarrier(backendContext, false, FFX_RESOURCE_STATE_UNORDERED_ACCESS); - } - - for (uint32_t uav = 0; uav < job->computeJobDescriptor.pipeline.uavCount; ++uav) - { - BackendContext_GL::Resource ffxResource = backendContext->resources[job->computeJobDescriptor.uavs[uav].internalIndex]; - - backendContext->glFunctionTable.glBindImageTexture( - job->computeJobDescriptor.pipeline.uavResourceBindings[uav].slotIndex, - ffxResource.textureSingleMipViews[job->computeJobDescriptor.uavMip[uav]].id, - 0, - true, - 0, - GL_READ_WRITE, - getGLFormatFromSurfaceFormat(ffxResource.resourceDescription.format)); - } - - // bind srvs (sampled textures) - if (job->computeJobDescriptor.pipeline.srvCount > 0) - { - addBarrier(backendContext, false, FFX_RESOURCE_STATE_COMPUTE_READ); - } - - for (uint32_t srv = 0; srv < job->computeJobDescriptor.pipeline.srvCount; ++srv) - { - BackendContext_GL::Resource ffxResource = backendContext->resources[job->computeJobDescriptor.srvs[srv].internalIndex]; - - backendContext->glFunctionTable.glBindTextureUnit(job->computeJobDescriptor.pipeline.srvResourceBindings[srv].slotIndex, ffxResource.textureAllMipsView.id); - backendContext->glFunctionTable.glBindSampler(job->computeJobDescriptor.pipeline.srvResourceBindings[srv].slotIndex, backendContext->linearSampler.id); - } - - // update ubos (uniform buffers) - for (uint32_t i = 0; i < job->computeJobDescriptor.pipeline.constCount; ++i) - { - auto ubo = accquireDynamicUBO(backendContext, job->computeJobDescriptor.cbs[i].uint32Size * sizeof(uint32_t), job->computeJobDescriptor.cbs[i].data); - backendContext->glFunctionTable.glBindBufferRange( - GL_UNIFORM_BUFFER, - job->computeJobDescriptor.pipeline.cbResourceBindings[i].slotIndex, - ubo.bufferResource.id, - 0, - FSR2_UBO_SIZE); - } - - backendContext->glFunctionTable.glUseProgram(program); - backendContext->glFunctionTable.glDispatchCompute(job->computeJobDescriptor.dimensions[0], job->computeJobDescriptor.dimensions[1], job->computeJobDescriptor.dimensions[2]); - - return FFX_OK; + FFX_ASSERT(backendContext); + + const auto program = static_cast(reinterpret_cast(job->computeJobDescriptor.pipeline.pipeline)); + + // bind uavs (storage images) + if (job->computeJobDescriptor.pipeline.uavCount > 0) + { + addBarrier(backendContext, false, FFX_RESOURCE_STATE_UNORDERED_ACCESS); + } + + for (uint32_t uav = 0; uav < job->computeJobDescriptor.pipeline.uavCount; ++uav) + { + BackendContext_GL::Resource ffxResource = backendContext->resources[job->computeJobDescriptor.uavs[uav].internalIndex]; + + backendContext->glFunctionTable.glBindImageTexture( + job->computeJobDescriptor.pipeline.uavResourceBindings[uav].slotIndex, + ffxResource.textureSingleMipViews[job->computeJobDescriptor.uavMip[uav]].id, + 0, + true, + 0, + GL_READ_WRITE, + getGLFormatFromSurfaceFormat(ffxResource.resourceDescription.format)); + } + + // bind srvs (sampled textures) + if (job->computeJobDescriptor.pipeline.srvCount > 0) + { + addBarrier(backendContext, false, FFX_RESOURCE_STATE_COMPUTE_READ); + } + + for (uint32_t srv = 0; srv < job->computeJobDescriptor.pipeline.srvCount; ++srv) + { + BackendContext_GL::Resource ffxResource = backendContext->resources[job->computeJobDescriptor.srvs[srv].internalIndex]; + + backendContext->glFunctionTable.glBindTextureUnit(job->computeJobDescriptor.pipeline.srvResourceBindings[srv].slotIndex, ffxResource.textureAllMipsView.id); + backendContext->glFunctionTable.glBindSampler(job->computeJobDescriptor.pipeline.srvResourceBindings[srv].slotIndex, backendContext->linearSampler.id); + } + + // update ubos (uniform buffers) + for (uint32_t i = 0; i < job->computeJobDescriptor.pipeline.constCount; ++i) + { + auto ubo = accquireDynamicUBO(backendContext, job->computeJobDescriptor.cbs[i].uint32Size * sizeof(uint32_t), job->computeJobDescriptor.cbs[i].data); + backendContext->glFunctionTable.glBindBufferRange( + GL_UNIFORM_BUFFER, + job->computeJobDescriptor.pipeline.cbResourceBindings[i].slotIndex, + ubo.bufferResource.id, + 0, + FSR2_UBO_SIZE); + } + + backendContext->glFunctionTable.glUseProgram(program); + backendContext->glFunctionTable.glDispatchCompute(job->computeJobDescriptor.dimensions[0], job->computeJobDescriptor.dimensions[1], job->computeJobDescriptor.dimensions[2]); + + return FFX_OK; } static FfxErrorCode executeGpuJobClearFloat(BackendContext_GL *backendContext, FfxGpuJobDescription *job) { - FFX_ASSERT(backendContext); + FFX_ASSERT(backendContext); - uint32_t idx = job->clearJobDescriptor.target.internalIndex; - BackendContext_GL::Resource ffxResource = backendContext->resources[idx]; + uint32_t idx = job->clearJobDescriptor.target.internalIndex; + BackendContext_GL::Resource ffxResource = backendContext->resources[idx]; - if (ffxResource.resourceDescription.type != FFX_RESOURCE_TYPE_BUFFER) - { - addBarrier(backendContext, false, FFX_RESOURCE_STATE_COPY_DEST); + if (ffxResource.resourceDescription.type != FFX_RESOURCE_TYPE_BUFFER) + { + addBarrier(backendContext, false, FFX_RESOURCE_STATE_COPY_DEST); - auto texture = ffxResource.textureAllMipsView; + auto texture = ffxResource.textureAllMipsView; - float clearColorValue[4] = {}; - clearColorValue[0] = job->clearJobDescriptor.color[0]; - clearColorValue[1] = job->clearJobDescriptor.color[1]; - clearColorValue[2] = job->clearJobDescriptor.color[2]; - clearColorValue[3] = job->clearJobDescriptor.color[3]; + float clearColorValue[4] = {}; + clearColorValue[0] = job->clearJobDescriptor.color[0]; + clearColorValue[1] = job->clearJobDescriptor.color[1]; + clearColorValue[2] = job->clearJobDescriptor.color[2]; + clearColorValue[3] = job->clearJobDescriptor.color[3]; - for (uint32_t i = 0; i < ffxResource.resourceDescription.mipCount; i++) - { - backendContext->glFunctionTable.glClearTexImage(texture.id, i, GL_RGBA, GL_FLOAT, clearColorValue); - } - } + for (uint32_t i = 0; i < ffxResource.resourceDescription.mipCount; i++) + { + backendContext->glFunctionTable.glClearTexImage(texture.id, i, GL_RGBA, GL_FLOAT, clearColorValue); + } + } - return FFX_OK; + return FFX_OK; } FfxErrorCode ExecuteGpuJobsGL(FfxFsr2Interface *backendInterface, FfxCommandList) { - FFX_ASSERT(backendInterface); - - BackendContext_GL *backendContext = (BackendContext_GL *)backendInterface->scratchBuffer; - - FfxErrorCode errorCode = FFX_OK; - - // execute all renderjobs - for (uint32_t i = 0; i < backendContext->gpuJobCount; ++i) - { - FfxGpuJobDescription *gpuJob = &backendContext->gpuJobs[i]; - - switch (gpuJob->jobType) - { - case FFX_GPU_JOB_CLEAR_FLOAT: - { - errorCode = executeGpuJobClearFloat(backendContext, gpuJob); - break; - } - case FFX_GPU_JOB_COPY: - { - FFX_ASSERT_FAIL("Copy job is not implemented in OpenGL backend"); - break; - } - case FFX_GPU_JOB_COMPUTE: - { - errorCode = executeGpuJobCompute(backendContext, gpuJob); - break; - } - default:; - } - } - - // check the execute function returned cleanly. - FFX_RETURN_ON_ERROR( - errorCode == FFX_OK, - FFX_ERROR_BACKEND_API_ERROR); - - backendContext->gpuJobCount = 0; - - return FFX_OK; + FFX_ASSERT(backendInterface); + + BackendContext_GL *backendContext = (BackendContext_GL *)backendInterface->scratchBuffer; + + FfxErrorCode errorCode = FFX_OK; + + // execute all renderjobs + for (uint32_t i = 0; i < backendContext->gpuJobCount; ++i) + { + FfxGpuJobDescription *gpuJob = &backendContext->gpuJobs[i]; + + switch (gpuJob->jobType) + { + case FFX_GPU_JOB_CLEAR_FLOAT: + { + errorCode = executeGpuJobClearFloat(backendContext, gpuJob); + break; + } + case FFX_GPU_JOB_COPY: + { + FFX_ASSERT_FAIL("Copy job is not implemented in OpenGL backend"); + break; + } + case FFX_GPU_JOB_COMPUTE: + { + errorCode = executeGpuJobCompute(backendContext, gpuJob); + break; + } + default:; + } + } + + // check the execute function returned cleanly. + FFX_RETURN_ON_ERROR( + errorCode == FFX_OK, + FFX_ERROR_BACKEND_API_ERROR); + + backendContext->gpuJobCount = 0; + + return FFX_OK; } FfxErrorCode DestroyResourceGL(FfxFsr2Interface *backendInterface, FfxResourceInternal resource) { - FFX_ASSERT(backendInterface); - - BackendContext_GL *backendContext = (BackendContext_GL *)backendInterface->scratchBuffer; - - if (resource.internalIndex != -1) - { - BackendContext_GL::Resource &res = backendContext->resources[resource.internalIndex]; - - if (res.resourceDescription.type == FFX_RESOURCE_TYPE_BUFFER) - { - if (res.buffer.id) - { - backendContext->glFunctionTable.glDeleteBuffers(1, &res.buffer.id); - res.buffer = {}; - } - } - else - { - if (res.textureAllMipsView.id) - { - backendContext->glFunctionTable.glDeleteTextures(1, &res.textureAllMipsView.id); - res.textureAllMipsView = {}; - } - - for (uint32_t i = 0; i < res.resourceDescription.mipCount; i++) - { - if (res.textureSingleMipViews[i].id) - { - backendContext->glFunctionTable.glDeleteTextures(1, &res.textureSingleMipViews[i].id); - res.textureSingleMipViews[i] = {}; - } - } - } - } - - return FFX_OK; + FFX_ASSERT(backendInterface); + + BackendContext_GL *backendContext = (BackendContext_GL *)backendInterface->scratchBuffer; + + if (resource.internalIndex != -1) + { + BackendContext_GL::Resource &res = backendContext->resources[resource.internalIndex]; + + if (res.resourceDescription.type == FFX_RESOURCE_TYPE_BUFFER) + { + if (res.buffer.id) + { + backendContext->glFunctionTable.glDeleteBuffers(1, &res.buffer.id); + res.buffer = {}; + } + } + else + { + if (res.textureAllMipsView.id) + { + backendContext->glFunctionTable.glDeleteTextures(1, &res.textureAllMipsView.id); + res.textureAllMipsView = {}; + } + + for (uint32_t i = 0; i < res.resourceDescription.mipCount; i++) + { + if (res.textureSingleMipViews[i].id) + { + backendContext->glFunctionTable.glDeleteTextures(1, &res.textureSingleMipViews[i].id); + res.textureSingleMipViews[i] = {}; + } + } + } + } + + return FFX_OK; } FfxErrorCode DestroyPipelineGL(FfxFsr2Interface *backendInterface, FfxPipelineState *pipeline) { - FFX_ASSERT(backendInterface); + FFX_ASSERT(backendInterface); - if (!pipeline) - { - return FFX_OK; - } + if (!pipeline) + { + return FFX_OK; + } - BackendContext_GL *backendContext = (BackendContext_GL *)backendInterface->scratchBuffer; + BackendContext_GL *backendContext = (BackendContext_GL *)backendInterface->scratchBuffer; - // destroy pipeline - const auto program = static_cast(reinterpret_cast(pipeline->pipeline)); - if (program) - { - backendContext->glFunctionTable.glDeleteProgram(program); - pipeline->pipeline = nullptr; - } + // destroy pipeline + const auto program = static_cast(reinterpret_cast(pipeline->pipeline)); + if (program) + { + backendContext->glFunctionTable.glDeleteProgram(program); + pipeline->pipeline = nullptr; + } - return FFX_OK; + return FFX_OK; } diff --git a/src/ffx-fsr2-api/gl/ffx_fsr2_gl.h b/src/ffx-fsr2-api/gl/ffx_fsr2_gl.h index 071eeca..4383a4f 100644 --- a/src/ffx-fsr2-api/gl/ffx_fsr2_gl.h +++ b/src/ffx-fsr2-api/gl/ffx_fsr2_gl.h @@ -27,11 +27,12 @@ #include "../ffx_fsr2_interface.h" #if defined(__cplusplus) -extern "C" { +extern "C" +{ #endif // #if defined(__cplusplus) - typedef void(*ffx_glProc)(); - typedef ffx_glProc(*ffx_glGetProcAddress)(const char*); + typedef void (*ffx_glProc)(); + typedef ffx_glProc (*ffx_glGetProcAddress)(const char *); /// Query how much memory is required for the Vulkan backend's scratch buffer. /// @@ -45,16 +46,16 @@ extern "C" { /// @param [in] scratchBuffer A pointer to a buffer of memory which can be used by the DirectX(R)12 backend. /// @param [in] scratchBufferSize The size (in bytes) of the buffer pointed to by scratchBuffer. /// @param [in] getProcAddress A pointer to a function which can be used to load OpenGL functions. - /// + /// /// @retval /// FFX_OK The operation completed successfully. /// @retval /// FFX_ERROR_CODE_INVALID_POINTER The interface pointer was NULL. - /// + /// /// @ingroup FSR2 GL FFX_API FfxErrorCode ffxFsr2GetInterfaceGL( - FfxFsr2Interface* outInterface, - void* scratchBuffer, + FfxFsr2Interface *outInterface, + void *scratchBuffer, size_t scratchBufferSize, ffx_glGetProcAddress getProcAddress); @@ -65,43 +66,43 @@ extern "C" { /// @param [in] height The height of the texture object. /// @param [in] imgFormat The format of the texture object. /// @param [in] name (optional) A name string to identify the resource in debug mode. - /// + /// /// @returns /// An abstract FidelityFX resources. - /// + /// /// @ingroup FSR2 GL FFX_API FfxResource ffxGetTextureResourceGL( - GLuint textureGL, - uint32_t width, - uint32_t height, - GLenum imgFormat, - const wchar_t* name = nullptr); + GLuint textureGL, + uint32_t width, + uint32_t height, + GLenum imgFormat, + const wchar_t *name = nullptr); /// Create a FfxResource from a VkBuffer. /// /// @param [in] bufferGL An OpenGL buffer object. /// @param [in] size The size of the buffer object. /// @param [in] name (optional) A name string to identify the resource in debug mode. - /// + /// /// @returns /// An abstract FidelityFX resources. - /// + /// /// @ingroup FSR2 GL FFX_API FfxResource ffxGetBufferResourceGL( - GLuint bufferGL, - uint32_t size, - const wchar_t* name = nullptr); + GLuint bufferGL, + uint32_t size, + const wchar_t *name = nullptr); /// Convert a FfxResource value to a VkImage. /// /// @param [in] context A pointer to a FfxFsr2Context structure. /// @param [in] resId A resourceID. - /// + /// /// @returns /// A GLuint. - /// + /// /// @ingroup FSR2 GL - FFX_API GLuint ffxGetGLImage(FfxFsr2Context* context, uint32_t resId); + FFX_API GLuint ffxGetGLImage(FfxFsr2Context *context, uint32_t resId); #if defined(__cplusplus) } diff --git a/src/ffxFsr2.cpp b/src/ffxFsr2.cpp index 2e27a6f..3444cac 100644 --- a/src/ffxFsr2.cpp +++ b/src/ffxFsr2.cpp @@ -1,4 +1,3 @@ -#include "pch.h" #include "io_homo_superresolution_fsr2_nativelib_ffx_fsr2_api.h" #include "ffx-fsr2-api/ffx_fsr2.h" #include "ffx-fsr2-api/gl/ffx_fsr2_gl.h" @@ -11,8 +10,6 @@ bool fsr2FirstInit = true; FfxFsr2Context fsr2Context; std::unique_ptr fsr2ScratchMemory; -FfxFsr2ContextDescription contextDesc; - static void up_env(JNIEnv *env) { @@ -23,12 +20,16 @@ static void check_env(JNIEnv *env) set_env(env); } -JNIEXPORT jint JNICALL Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1api_ffxFsr2GetInterfaceGL(JNIEnv *env, jobject, jint scratchMemorySize, jfloat fsr2Ratio, jint width, jint height, jint flags) +JNIEXPORT jint JNICALL Java_io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper_ffxFsr2CreateGL(JNIEnv *env, jobject, jint scratchMemorySize, jfloat fsr2Ratio, jint width, jint height, jint flags) { - + if (!fsr2FirstInit) + { + ffxFsr2ContextDestroy(&fsr2Context); + } check_env(env); unsigned int renderWidth = static_cast(width / fsr2Ratio); unsigned int renderHeight = static_cast(height / fsr2Ratio); + FfxFsr2ContextDescription contextDesc = {}; contextDesc.flags = static_cast(flags); contextDesc.maxRenderSize = {renderWidth, renderHeight}; contextDesc.displaySize = {static_cast(width), static_cast(height)}; @@ -36,7 +37,7 @@ JNIEXPORT jint JNICALL Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1ap contextDesc.fpMessage = [](FfxFsr2MsgType type, const wchar_t *message) { char cstr[256] = {}; - #ifdef ON_LINUX +#ifdef ON_LINUX wcstombs(cstr, message, sizeof(cstr)); #else @@ -44,68 +45,40 @@ JNIEXPORT jint JNICALL Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1ap #endif cstr[255] = '\0'; - if (type == FFX_FSR2_MESSAGE_TYPE_WARNING) - { - java_log(cstr, 1); - } - else - { - if (type == FFX_FSR2_MESSAGE_TYPE_ERROR) - { - java_log(cstr, 2); - } - else - { - java_log(cstr, 0); - } - } + java_log(cstr, 0); }; - fsr2ScratchMemory = std::make_unique(scratchMemorySize); - FfxErrorCode code = ffxFsr2GetInterfaceGL(&contextDesc.callbacks, fsr2ScratchMemory.get(), scratchMemorySize, java_glfwGetProcAddress); + fsr2ScratchMemory = std::make_unique(ffxFsr2GetScratchMemorySizeGL()); + ffxFsr2GetInterfaceGL(&contextDesc.callbacks, fsr2ScratchMemory.get(), ffxFsr2GetScratchMemorySizeGL(), java_glfwGetProcAddress); + FfxErrorCode code = ffxFsr2ContextCreate(&fsr2Context, &contextDesc); + java_log("FSR_CPP ffxFsr2CreateGL", 0); return static_cast(code); } -JNIEXPORT jint JNICALL Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1api_ffxFsr2GetScratchMemorySizeGL(JNIEnv *env, jobject) +JNIEXPORT jint JNICALL Java_io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper_ffxFsr2GetScratchMemorySizeGL(JNIEnv *env, jobject) { check_env(env); return static_cast(ffxFsr2GetScratchMemorySizeGL()); } -JNIEXPORT void JNICALL Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1api_init(JNIEnv *env, jobject) -{ - up_env(env); -} - -JNIEXPORT jint JNICALL Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1api_ffxFsr2CreateContext(JNIEnv *env, jobject) -{ - check_env(env); - return static_cast(ffxFsr2ContextCreate(&fsr2Context, &contextDesc)); -}; - -JNIEXPORT jint JNICALL Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1api_ffxFsr2ContextDestroy(JNIEnv *env, jobject) -{ - check_env(env); - return static_cast(ffxFsr2ContextDestroy(&fsr2Context)); -} -JNIEXPORT jstring JNICALL Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1api_getGPUInfoNV(JNIEnv *env, jobject) +JNIEXPORT jstring JNICALL Java_io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper_getGPUInfoNV(JNIEnv *env, jobject) { check_env(env); return env->NewStringUTF(get_gpu_info_nv()); }; -JNIEXPORT jstring JNICALL Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1api_getGPUInfoAMD(JNIEnv *env, jobject) +JNIEXPORT jstring JNICALL Java_io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper_getGPUInfoAMD(JNIEnv *env, jobject) { check_env(env); return env->NewStringUTF(""); }; -JNIEXPORT jint JNICALL Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1api_ffxFsr2ContextDispatch( +JNIEXPORT jint JNICALL Java_io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper_ffxFsr2ContextDispatch( JNIEnv *env, jobject, - jint color, - jint depth, - jint motionVectors, - jint exposure, - jint reactive, - jint output, + jobject color, + jobject depth, + jobject motionVectors, + jobject exposure, + jobject reactive, + jobject output, jfloat jitterX, jfloat jitterY, jfloat motionVectorScaleWidth, @@ -133,47 +106,45 @@ JNIEXPORT jint JNICALL Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1ap FfxResource exposure_tex = {}; // if (exposure != -1) exposure_tex = ffxGetTextureResourceGL(exposure, 1, 1, GL_R32F); FfxResource transparencyAndComposition_tex = {}; - FfxFsr2DispatchDescription dispatchDesc{ - .color = ffxGetTextureResourceGL(color, renderWidth, renderHeight, GL_R11F_G11F_B10F), - .depth = ffxGetTextureResourceGL(depth, renderWidth, renderHeight, GL_DEPTH_COMPONENT32F), - .motionVectors = ffxGetTextureResourceGL(motionVectors, renderWidth, renderHeight, GL_RG16F), - .exposure = exposure_tex, - .reactive = reactive_tex, - .transparencyAndComposition = transparencyAndComposition_tex, - .output = - ffxGetTextureResourceGL(output, width, height, GL_R11F_G11F_B10F), - .jitterOffset = {jitterX, jitterY}, - .motionVectorScale = {float(static_cast(renderWidth)), float(static_cast(renderHeight))}, - .renderSize = {static_cast(renderWidth), static_cast(renderHeight)}, - .enableSharpening = ToCppBool(enableSharpening), - .sharpness = sharpness, - .frameTimeDelta = static_cast(frameTimeDelta), // ms - .preExposure = preExposure, - .reset = ToCppBool(reset), - .cameraNear = cameraNear, - .cameraFar = cameraFar, - .cameraFovAngleVertical = cameraFovY, - .viewSpaceToMetersFactor = viewSpaceToMetersFactor, - .deviceDepthNegativeOneToOne = ToCppBool(deviceDepthNegativeOneToOne), - }; + FfxFsr2DispatchDescription dispatchDesc = {}; + dispatchDesc.color = ffxResourceJavaToCpp(env, color); + dispatchDesc.depth = ffxResourceJavaToCpp(env, depth); + dispatchDesc.motionVectors = ffxResourceJavaToCpp(env, motionVectors); + dispatchDesc.exposure = exposure_tex; + dispatchDesc.reactive = reactive_tex; + dispatchDesc.transparencyAndComposition = transparencyAndComposition_tex; + dispatchDesc.output = ffxResourceJavaToCpp(env, output); + dispatchDesc.jitterOffset = {jitterX, jitterY}; + dispatchDesc.motionVectorScale = {float(static_cast(renderWidth)), float(static_cast(renderHeight))}; + dispatchDesc.renderSize = {static_cast(renderWidth), static_cast(renderHeight)}; + dispatchDesc.enableSharpening = ToCppBool(enableSharpening); + dispatchDesc.sharpness = sharpness; + dispatchDesc.frameTimeDelta = static_cast(frameTimeDelta); // ms + dispatchDesc.preExposure = preExposure; + dispatchDesc.reset = ToCppBool(reset); + dispatchDesc.cameraNear = cameraNear; + dispatchDesc.cameraFar = cameraFar; + dispatchDesc.cameraFovAngleVertical = cameraFovY; + dispatchDesc.viewSpaceToMetersFactor = viewSpaceToMetersFactor; + dispatchDesc.deviceDepthNegativeOneToOne = ToCppBool(deviceDepthNegativeOneToOne); FfxErrorCode err = ffxFsr2ContextDispatch(&fsr2Context, &dispatchDesc); return static_cast(err); } -JNIEXPORT jobject JNICALL Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1api_ffxGetTextureResourceGL(JNIEnv *env, jobject, jlong texGL, jint width, jint height, jint type) +JNIEXPORT jobject JNICALL Java_io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper_ffxGetTextureResourceGL(JNIEnv *env, jobject, jlong texGL, jint width, jint height, jint type) { FfxResource resource = ffxGetTextureResourceGL(texGL, width, height, type); jclass javaffxrescls = env->FindClass("io/homo/superresolution/fsr2/types/FfxResource"); jmethodID constrocMID = env->GetMethodID(javaffxrescls, "", "(IZJIIIIIIII)V"); - jobject javaffxres_ojb = env->NewObject(javaffxrescls, constrocMID,(jint)(uintptr_t)(resource.resource),resource.isDepth,(jlong)resource.descriptorData,(jint)resource.description.type,(jint)resource.description.format,resource.description.width,resource.description.height,resource.description.depth,resource.description.mipCount,(jint)resource.description.flags,(jint)resource.state); + jobject javaffxres_ojb = env->NewObject(javaffxrescls, constrocMID, (jint)texGL, resource.isDepth, (jlong)resource.descriptorData, (jint)resource.description.type, (jint)resource.description.format, resource.description.width, resource.description.height, resource.description.depth, resource.description.mipCount, (jint)resource.description.flags, (jint)resource.state); return javaffxres_ojb; }; -JNIEXPORT jint JNICALL Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1api_ffxFsr2Test(JNIEnv *env, jobject o) +JNIEXPORT jint JNICALL Java_io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper_ffxFsr2Test(JNIEnv *env, jobject o) { check_env(env); - jobject j = Java_io_homo_superresolution_fsr2_nativelib_ffx_1fsr2_1api_ffxGetTextureResourceGL(env, o, 0, 1, 1, 0); + jobject j = Java_io_homo_superresolution_fsr2_nativelib_FSR2ApiHelper_ffxGetTextureResourceGL(env, o, 0, 1, 1, 0); ffxResourceJavaToCpp(env, j); return 0; }; \ No newline at end of file diff --git a/src/pch.cpp b/src/pch.cpp deleted file mode 100644 index 1730571..0000000 --- a/src/pch.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "pch.h" \ No newline at end of file diff --git a/src/utils.cpp b/src/utils.cpp index 1af7ffe..12be7bc 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,38 +1,37 @@ -#include "pch.h" #include "utils.h" +#include "nvml.h" #include "json.hpp" -#include -#include +#include +#include #include "ffx-fsr2-api/ffx_types.h" using json = nlohmann::json; -JNIEnv* cur_env; -void set_env(JNIEnv* env) { +JNIEnv *cur_env; +void set_env(JNIEnv *env) +{ cur_env = env; } -JNIEnv* get_env() +JNIEnv *get_env() { return cur_env; } -void java_log(const char* msg, int level) { +void java_log(const char *msg, int level) +{ jclass cpp_helper = cur_env->FindClass("io/homo/superresolution/fsr2/CPPHelper"); jmethodID methodID = cur_env->GetStaticMethodID(cpp_helper, "CPP_Log", "(Ljava/lang/String;I)V"); - if (methodID) { - jstring jmsg = cur_env->NewStringUTF(msg); - if (jmsg) { - cur_env->CallStaticVoidMethod(cpp_helper, methodID, jmsg,jint(level)); - cur_env->DeleteLocalRef(jmsg); - } - } + jstring jmsg = cur_env->NewStringUTF(msg); + cur_env->CallStaticVoidMethod(cpp_helper, methodID, jmsg, jint(level)); + cur_env->DeleteLocalRef(jmsg); } -GLFWglproc java_glfwGetProcAddress(const char* name) +GLFWglproc java_glfwGetProcAddress(const char *name) { jclass cpp_helper = cur_env->FindClass("io/homo/superresolution/fsr2/CPPHelper"); jmethodID methodID = cur_env->GetStaticMethodID(cpp_helper, "CPP_glfwGetProcAddress", "(Ljava/lang/String;)J"); - if (methodID) { + if (methodID) + { jstring jmsg = cur_env->NewStringUTF(name); jlong jlongValue = cur_env->CallStaticLongMethod(cpp_helper, methodID, jmsg); GLFWglproc glfwProc = reinterpret_cast(jlongValue); @@ -42,38 +41,43 @@ GLFWglproc java_glfwGetProcAddress(const char* name) return 0; } - using namespace std; -char* get_gpu_info_nv() { - char* out = (char*)""; +char *get_gpu_info_nv() +{ + char *out = (char *)""; json root_json; nvmlReturn_t result = nvmlInit(); - if (result != NVML_SUCCESS) { - return (char*)"Failed to initialize NVML: "; + if (result != NVML_SUCCESS) + { + return (char *)"Failed to initialize NVML: "; } unsigned int deviceCount; result = nvmlDeviceGetCount(&deviceCount); - if (result != NVML_SUCCESS) { - return (char*)"Failed to get device count: "; + if (result != NVML_SUCCESS) + { + return (char *)"Failed to get device count: "; } root_json["device_count"] = deviceCount; root_json["gpus"] = {}; - for (unsigned int i = 0; i < deviceCount; i++) { + for (unsigned int i = 0; i < deviceCount; i++) + { json gpuinfo; gpuinfo["id"] = i; gpuinfo["msg"] = "OK"; nvmlDevice_t device; result = nvmlDeviceGetHandleByIndex(i, &device); - if (result != NVML_SUCCESS) { + if (result != NVML_SUCCESS) + { gpuinfo["msg"] = "Failed to get handle for device :" + std::string(nvmlErrorString(result)); root_json["gpus"].push_back(gpuinfo); continue; } char name[NVML_DEVICE_NAME_BUFFER_SIZE]; result = nvmlDeviceGetName(device, name, NVML_DEVICE_NAME_BUFFER_SIZE); - if (result != NVML_SUCCESS) { + if (result != NVML_SUCCESS) + { gpuinfo["msg"] = "Failed to get name for device :" + std::string(nvmlErrorString(result)); root_json["gpus"].push_back(gpuinfo); continue; @@ -81,7 +85,8 @@ char* get_gpu_info_nv() { gpuinfo["name"] = name; nvmlMemory_t memoryInfo; result = nvmlDeviceGetMemoryInfo(device, &memoryInfo); - if (result != NVML_SUCCESS) { + if (result != NVML_SUCCESS) + { gpuinfo["msg"] = "Failed to get memory info for device :" + std::string(nvmlErrorString(result)); root_json["gpus"].push_back(gpuinfo); continue; @@ -90,19 +95,18 @@ char* get_gpu_info_nv() { gpuinfo["memory_total"] = memoryInfo.total; gpuinfo["memory_used"] = memoryInfo.used; root_json["gpus"].push_back(gpuinfo); - } std::string jsonString = root_json.dump(4); nvmlShutdown(); - return (char*)jsonString.c_str(); - + return (char *)jsonString.c_str(); } -bool ToCppBool(jboolean value) { +bool ToCppBool(jboolean value) +{ return value == JNI_TRUE; } -FfxResource ffxResourceJavaToCpp(JNIEnv* env,jobject javaffxres) +FfxResource ffxResourceJavaToCpp(JNIEnv *env, jobject javaffxres) { jclass cls = env->GetObjectClass(javaffxres); jfieldID resourceFieldId = env->GetFieldID(cls, "resource", "J"); @@ -128,7 +132,7 @@ FfxResource ffxResourceJavaToCpp(JNIEnv* env,jobject javaffxres) int flags = env->GetIntField(javaffxres, flagsFieldId); int state = env->GetIntField(javaffxres, stateFieldId); - FfxResource res = __ffxResourceJavaToCpp(resourceField, isDepth, descriptorDataField, type,format,width,height,depth,mipCount,flags,state); + FfxResource res = __ffxResourceJavaToCpp(resourceField, isDepth, descriptorDataField, type, format, width, height, depth, mipCount, flags, state); return res; } @@ -136,11 +140,10 @@ FfxResource ffxResourceJavaToCpp(JNIEnv* env,jobject javaffxres) FfxResource __ffxResourceJavaToCpp( int resource, bool isDepth, uint64_t descriptorData, int type, int format, int width, int height, int depth, - int mipCount, int flags, int state -) + int mipCount, int flags, int state) { FfxResource ffxresource = {}; - ffxresource.resource = reinterpret_cast(static_cast(resource)); + ffxresource.resource = reinterpret_cast(static_cast(resource)); ffxresource.descriptorData = descriptorData; ffxresource.state = (FfxResourceStates)state; ffxresource.isDepth = isDepth;