Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ffx-fsr2-api build on Linux #60

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 47 additions & 14 deletions src/ffx-fsr2-api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,20 @@ if(CMAKE_GENERATOR_PLATFORM STREQUAL "x64" OR CMAKE_EXE_LINKER_FLAGS STREQUAL "/
set(FSR2_PLATFORM_NAME x64)
elseif(CMAKE_GENERATOR_PLATFORM STREQUAL "Win32" OR CMAKE_EXE_LINKER_FLAGS STREQUAL "/machine:X86")
set(FSR2_PLATFORM_NAME x86)
elseif(CMAKE_SYSTEM_PROCESSOR)
set(FSR2_PLATFORM_NAME ${CMAKE_SYSTEM_PROCESSOR})

if ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR (NOT MSVC))
add_compile_definitions(FFX_GCC) # Should work with gcc and others
endif()
else()
message(FATAL_ERROR "Unsupported target platform - only supporting x64 and Win32 currently")
endif()

# Embed PDBs in the debug versions of the libs
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Z7")
if (MSVC)
# Embed PDBs in the debug versions of the libs
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Z7")
endif()

# Write both debug and release versions of the static libs to the /lib folder as they are uniquely named
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CMAKE_HOME_DIRECTORY}/bin/ffx_fsr2_api/)
Expand All @@ -63,8 +71,13 @@ if(FSR2_VS_VERSION STREQUAL 2015)
set(CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION "10.0.18362.0")
endif()

set(FFX_SC_EXECUTABLE
${CMAKE_CURRENT_SOURCE_DIR}/../../tools/sc/FidelityFX_SC.exe)
if (WIN32)
set(FFX_SC_EXECUTABLE
${CMAKE_CURRENT_SOURCE_DIR}/../../tools/sc/FidelityFX_SC.exe)
else()
set(FFX_SC_EXECUTABLE
${CMAKE_CURRENT_SOURCE_DIR}/../../tools/sc/FidelityFX_SC.sh)
endif()

set(FFX_SC_BASE_ARGS
-reflection -deps=gcc -DFFX_GPU=1
Expand All @@ -75,16 +88,30 @@ set(FFX_SC_BASE_ARGS
-DFFX_FSR2_OPTION_POSTPROCESSLOCKSTATUS_SAMPLERS_USE_DATA_HALF=0
# Upsample uses lanczos approximation
-DFFX_FSR2_OPTION_UPSAMPLE_USE_LANCZOS_TYPE=2
)

set(FFX_SC_PERMUTATION_ARGS
# Reproject can use either reference lanczos or LUT
-DFFX_FSR2_OPTION_REPROJECT_USE_LANCZOS_TYPE={0,1}
-DFFX_FSR2_OPTION_HDR_COLOR_INPUT={0,1}
-DFFX_FSR2_OPTION_LOW_RESOLUTION_MOTION_VECTORS={0,1}
-DFFX_FSR2_OPTION_JITTERED_MOTION_VECTORS={0,1}
-DFFX_FSR2_OPTION_INVERTED_DEPTH={0,1}
-DFFX_FSR2_OPTION_APPLY_SHARPENING={0,1})
)

if (WIN32)
set(FFX_SC_PERMUTATION_ARGS
# Reproject can use either reference lanczos or LUT
-DFFX_FSR2_OPTION_REPROJECT_USE_LANCZOS_TYPE={0,1}
-DFFX_FSR2_OPTION_HDR_COLOR_INPUT={0,1}
-DFFX_FSR2_OPTION_LOW_RESOLUTION_MOTION_VECTORS={0,1}
-DFFX_FSR2_OPTION_JITTERED_MOTION_VECTORS={0,1}
-DFFX_FSR2_OPTION_INVERTED_DEPTH={0,1}
-DFFX_FSR2_OPTION_APPLY_SHARPENING={0,1}
)
else()
# Fix bash curly braces replacement
set(FFX_SC_PERMUTATION_ARGS
# Reproject can use either reference lanczos or LUT
'-DFFX_FSR2_OPTION_REPROJECT_USE_LANCZOS_TYPE={0,1}'
'-DFFX_FSR2_OPTION_HDR_COLOR_INPUT={0,1}'
'-DFFX_FSR2_OPTION_LOW_RESOLUTION_MOTION_VECTORS={0,1}'
'-DFFX_FSR2_OPTION_JITTERED_MOTION_VECTORS={0,1}'
'-DFFX_FSR2_OPTION_INVERTED_DEPTH={0,1}'
'-DFFX_FSR2_OPTION_APPLY_SHARPENING={0,1}'
)
endif()

file(GLOB SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/*.cpp"
Expand All @@ -96,14 +123,20 @@ else()
add_library(ffx_fsr2_api_${FSR2_PLATFORM_NAME} STATIC ${SOURCES})
endif()

set(FFX_FSR2_API ffx_fsr2_api_${FSR2_PLATFORM_NAME} PARENT_SCOPE)

# graphics api backends
if(FFX_FSR2_API_DX12)
message("Will build FSR2 library: DX12 backend")
add_subdirectory(dx12)

set(FFX_FSR2_API_DX12 ffx_fsr2_api_dx12_${FSR2_PLATFORM_NAME} PARENT_SCOPE)
endif()
if(FFX_FSR2_API_VK)
message("Will build FSR2 library: Vulkan backend")
add_subdirectory(vk)

set(FFX_FSR2_API_VK ffx_fsr2_api_vk_${FSR2_PLATFORM_NAME} PARENT_SCOPE)
endif()

# api
Expand Down
44 changes: 26 additions & 18 deletions src/ffx-fsr2-api/ffx_fsr2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <algorithm> // for max used inside SPD CPU code.
#include <cmath> // for fabs, abs, sinf, sqrt, etc.
#include <string.h> // for memset
#include <wchar.h> // for wcscmp, wcscpy
#include <cfloat> // for FLT_EPSILON
#include "ffx_fsr2.h"
#define FFX_CPU
Expand All @@ -36,6 +37,10 @@
#pragma clang diagnostic ignored "-Wunused-variable"
#endif

#ifndef _countof
#define _countof(array) (sizeof(array) / sizeof(array[0]))
#endif

// max queued frames for descriptor management
static const uint32_t FSR2_MAX_QUEUED_FRAMES = 16;

Expand Down Expand Up @@ -94,6 +99,9 @@ static const ResourceBinding uavResourceBindingTable[] =
{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"},
Expand Down Expand Up @@ -182,7 +190,7 @@ FfxConstantBuffer globalFsr2ConstantBuffers[4] = {
// Lanczos
static float lanczos2(float value)
{
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));
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));
}

// Calculate halton number for index and base.
Expand Down Expand Up @@ -377,7 +385,7 @@ static FfxErrorCode patchResourceBindings(FfxPipelineState* inoutPipeline)
inoutPipeline->cbResourceBindings[cbIndex].resourceIdentifier = cbResourceBindingTable[mapIndex].index;
}

return FFX_OK;
return FFX_OK;
}


Expand Down Expand Up @@ -415,7 +423,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);
Expand Down Expand Up @@ -720,13 +728,13 @@ static void scheduleDispatch(FfxFsr2Context_Private* context, const FfxFsr2Dispa
const uint32_t currentResourceId = pipeline->srvResourceBindings[currentShaderResourceViewIndex].resourceIdentifier;
const FfxResourceInternal currentResource = context->srvResources[currentResourceId];
jobDescriptor.srvs[currentShaderResourceViewIndex] = currentResource;
wcscpy_s(jobDescriptor.srvNames[currentShaderResourceViewIndex], pipeline->srvResourceBindings[currentShaderResourceViewIndex].name);
wcscpy(jobDescriptor.srvNames[currentShaderResourceViewIndex], pipeline->srvResourceBindings[currentShaderResourceViewIndex].name);
}

for (uint32_t currentUnorderedAccessViewIndex = 0; currentUnorderedAccessViewIndex < pipeline->uavCount; ++currentUnorderedAccessViewIndex) {

const uint32_t currentResourceId = pipeline->uavResourceBindings[currentUnorderedAccessViewIndex].resourceIdentifier;
wcscpy_s(jobDescriptor.uavNames[currentUnorderedAccessViewIndex], pipeline->uavResourceBindings[currentUnorderedAccessViewIndex].name);
wcscpy(jobDescriptor.uavNames[currentUnorderedAccessViewIndex], pipeline->uavResourceBindings[currentUnorderedAccessViewIndex].name);

if (currentResourceId >= FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_0 && currentResourceId <= FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_12)
{
Expand All @@ -741,14 +749,14 @@ 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) {
wcscpy_s( jobDescriptor.cbNames[currentRootConstantIndex], pipeline->cbResourceBindings[currentRootConstantIndex].name);
wcscpy( jobDescriptor.cbNames[currentRootConstantIndex], pipeline->cbResourceBindings[currentRootConstantIndex].name);
jobDescriptor.cbs[currentRootConstantIndex] = globalFsr2ConstantBuffers[pipeline->cbResourceBindings[currentRootConstantIndex].resourceIdentifier];
jobDescriptor.cbSlotIndex[currentRootConstantIndex] = pipeline->cbResourceBindings[currentRootConstantIndex].slotIndex;
}
Expand Down Expand Up @@ -1257,9 +1265,9 @@ FfxErrorCode ffxFsr2ContextGenerateReactiveMask(FfxFsr2Context* context, const F

jobDescriptor.uavs[0] = contextPrivate->uavResources[FFX_FSR2_RESOURCE_IDENTIFIER_AUTOREACTIVE];

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);
wcscpy(jobDescriptor.srvNames[0], pipeline->srvResourceBindings[0].name);
wcscpy(jobDescriptor.srvNames[1], pipeline->srvResourceBindings[1].name);
wcscpy(jobDescriptor.uavNames[0], pipeline->uavResourceBindings[0].name);

jobDescriptor.dimensions[0] = dispatchSrcX;
jobDescriptor.dimensions[1] = dispatchSrcY;
Expand All @@ -1271,7 +1279,7 @@ FfxErrorCode ffxFsr2ContextGenerateReactiveMask(FfxFsr2Context* context, const F
const uint32_t currentResourceId = pipeline->srvResourceBindings[currentShaderResourceViewIndex].resourceIdentifier;
const FfxResourceInternal currentResource = contextPrivate->srvResources[currentResourceId];
jobDescriptor.srvs[currentShaderResourceViewIndex] = currentResource;
wcscpy_s(jobDescriptor.srvNames[currentShaderResourceViewIndex], pipeline->srvResourceBindings[currentShaderResourceViewIndex].name);
wcscpy(jobDescriptor.srvNames[currentShaderResourceViewIndex], pipeline->srvResourceBindings[currentShaderResourceViewIndex].name);
}

Fsr2GenerateReactiveConstants constants = {};
Expand All @@ -1282,7 +1290,7 @@ FfxErrorCode ffxFsr2ContextGenerateReactiveMask(FfxFsr2Context* context, const F

jobDescriptor.cbs[0].uint32Size = sizeof(constants);
memcpy(&jobDescriptor.cbs[0].data, &constants, sizeof(constants));
wcscpy_s(jobDescriptor.cbNames[0], pipeline->cbResourceBindings[0].name);
wcscpy(jobDescriptor.cbNames[0], pipeline->cbResourceBindings[0].name);

FfxGpuJobDescription dispatchJob = { FFX_GPU_JOB_COMPUTE };
dispatchJob.computeJobDescriptor = jobDescriptor;
Expand Down Expand Up @@ -1323,10 +1331,10 @@ static FfxErrorCode generateReactiveMaskInternal(FfxFsr2Context_Private* context
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];

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);
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);

jobDescriptor.dimensions[0] = dispatchSrcX;
jobDescriptor.dimensions[1] = dispatchSrcY;
Expand All @@ -1338,11 +1346,11 @@ static FfxErrorCode generateReactiveMaskInternal(FfxFsr2Context_Private* context
const uint32_t currentResourceId = pipeline->srvResourceBindings[currentShaderResourceViewIndex].resourceIdentifier;
const FfxResourceInternal currentResource = contextPrivate->srvResources[currentResourceId];
jobDescriptor.srvs[currentShaderResourceViewIndex] = currentResource;
wcscpy_s(jobDescriptor.srvNames[currentShaderResourceViewIndex], pipeline->srvResourceBindings[currentShaderResourceViewIndex].name);
wcscpy(jobDescriptor.srvNames[currentShaderResourceViewIndex], pipeline->srvResourceBindings[currentShaderResourceViewIndex].name);
}

for (uint32_t currentRootConstantIndex = 0; currentRootConstantIndex < pipeline->constCount; ++currentRootConstantIndex) {
wcscpy_s(jobDescriptor.cbNames[currentRootConstantIndex], pipeline->cbResourceBindings[currentRootConstantIndex].name);
wcscpy(jobDescriptor.cbNames[currentRootConstantIndex], pipeline->cbResourceBindings[currentRootConstantIndex].name);
jobDescriptor.cbs[currentRootConstantIndex] = globalFsr2ConstantBuffers[pipeline->cbResourceBindings[currentRootConstantIndex].resourceIdentifier];
jobDescriptor.cbSlotIndex[currentRootConstantIndex] = pipeline->cbResourceBindings[currentRootConstantIndex].slotIndex;
}
Expand Down
1 change: 1 addition & 0 deletions src/ffx-fsr2-api/ffx_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#pragma once

#include <stdint.h>
#include <stdlib.h>

#if defined (FFX_GCC)
/// FidelityFX exported functions
Expand Down
11 changes: 8 additions & 3 deletions src/ffx-fsr2-api/vk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@ foreach(PASS_SHADER ${PASS_SHADERS})
# skip 16-bit permutations for the compute luminance pyramid pass
set(FFX_SC_ARGS ${FFX_SC_BASE_ARGS} ${FFX_SC_VK_BASE_ARGS} ${FFX_SC_PERMUTATION_ARGS} -DFFX_HALF=0)
else()
set(FFX_SC_ARGS ${FFX_SC_BASE_ARGS} ${FFX_SC_VK_BASE_ARGS} ${FFX_SC_PERMUTATION_ARGS} -DFFX_HALF={0,1})
endif()
if (WIN32)
set(FFX_SC_ARGS ${FFX_SC_BASE_ARGS} ${FFX_SC_VK_BASE_ARGS} ${FFX_SC_PERMUTATION_ARGS} -DFFX_HALF={0,1})
else()
# Fix bash curly braces replacement
set(FFX_SC_ARGS ${FFX_SC_BASE_ARGS} ${FFX_SC_VK_BASE_ARGS} ${FFX_SC_PERMUTATION_ARGS} '-DFFX_HALF={0,1}')
endif()
endif()

if(USE_DEPFILE)
add_custom_command(
Expand All @@ -107,4 +112,4 @@ add_custom_target(shader_permutations_vk DEPENDS ${PERMUTATION_OUTPUTS})
add_dependencies(${FFX_SC_DEPENDENT_TARGET} shader_permutations_vk)

source_group("source" FILES ${VK})
source_group("shaders" FILES ${SHADERS})
source_group("shaders" FILES ${SHADERS})
11 changes: 6 additions & 5 deletions src/ffx-fsr2-api/vk/ffx_fsr2_vk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <math.h>
#include <stdlib.h>
#include <codecvt>
#include <locale>

// prototypes for functions in the interface
FfxErrorCode GetDeviceCapabilitiesVK(FfxFsr2Interface* backendInterface, FfxDeviceCapabilities* deviceCapabilities, FfxDevice device);
Expand Down Expand Up @@ -573,7 +574,7 @@ FfxResource ffxGetTextureResourceVK(FfxFsr2Context* context, VkImage imgVk, VkIm

#ifdef _DEBUG
if (name) {
wcscpy_s(resource.name, name);
wcscpy(resource.name, name);
}
#endif

Expand All @@ -597,7 +598,7 @@ FfxResource ffxGetBufferResourceVK(FfxFsr2Context* context, VkBuffer bufVk, uint

#ifdef _DEBUG
if (name) {
wcscpy_s(resource.name, name);
wcscpy(resource.name, name);
}
#endif

Expand Down Expand Up @@ -1331,17 +1332,17 @@ FfxErrorCode CreatePipelineVK(FfxFsr2Interface* backendInterface, FfxFsr2Pass pa
for (uint32_t srvIndex = 0; srvIndex < outPipeline->srvCount; ++srvIndex)
{
outPipeline->srvResourceBindings[srvIndex].slotIndex = shaderBlob.boundSampledImageBindings[srvIndex];
wcscpy_s(outPipeline->srvResourceBindings[srvIndex].name, converter.from_bytes(shaderBlob.boundSampledImageNames[srvIndex]).c_str());
wcscpy(outPipeline->srvResourceBindings[srvIndex].name, converter.from_bytes(shaderBlob.boundSampledImageNames[srvIndex]).c_str());
}
for (uint32_t uavIndex = 0; uavIndex < outPipeline->uavCount; ++uavIndex)
{
outPipeline->uavResourceBindings[uavIndex].slotIndex = shaderBlob.boundStorageImageBindings[uavIndex];
wcscpy_s(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());
}
for (uint32_t cbIndex = 0; cbIndex < outPipeline->constCount; ++cbIndex)
{
outPipeline->cbResourceBindings[cbIndex].slotIndex = shaderBlob.boundUniformBufferBindings[cbIndex];
wcscpy_s(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());
}

// create descriptor set layout
Expand Down
Loading