Compare commits

...

17 Commits
master ... fsr2

Author SHA1 Message Date
Nico de Poel 167433ba31 Implemented base event ID the way Unity's plugin interface actually expects you to: by reserving a range during initialization and storing the base value returned by Unity. 10 months ago
Nico de Poel 3bf309fb1e Small changes to distinguish the FSR2 plugin from the FSR3 plugin 10 months ago
Nico de Poel e40f6b0705 Configure render event for DX12 and Vulkan, to ensure the upscaler gets called without any synchronization errors. 11 months ago
Nico de Poel fa85f22f79 Updated license with full names of the three TND authors 11 months ago
Nico de Poel 7cb0961776 Added some extra safety checks 11 months ago
Nico de Poel c2f3bb7958 Fall back to biasColorMask if reactiveMask is empty, because Unity can't seem to make up its mind what either of these textures mean. 11 months ago
Nico de Poel 3a23f8d874 Await end of frame on the CPU before destroying a context, if the GPU has a dispatch queued on the current frame. Fixes remaining crashes in D3D12 and Vulkan when enabling/disabling the plugin too rapidly. 11 months ago
Nico de Poel 2a67bef38b Added lock guards to all exported functions that interact with the features or the graphics contexts, as well as additional validity checks. Protects against most of Unity's weird unsafe behavior when using the plugin, fixing the majority of crashes. 11 months ago
Nico de Poel 639aac06e8 Implemented remainder of DX11 support 11 months ago
Nico de Poel 686c848760 Fixed clashing helper function names in DX11 and DX12, and fixed missing linker symbol in DX11 11 months ago
Nico de Poel 112beaa592 Implemented FSR2 for Vulkan, along with some initial bits for DX11 11 months ago
Nico de Poel 048ed98d12 Added DX11 and Vulkan headers and static libraries to the project 11 months ago
Nico de Poel e8bc0eeb9c Added Unity graphics API headers for DX11 and Vulkan 11 months ago
Nico de Poel f4af0819c7 Isolated DX12-specific code, so that it can easily be replaced with alternative DX11 and Vulkan code. 11 months ago
Nico de Poel 4ea19e9852 Renamed FSR3 => FSR2 11 months ago
Nico de Poel d822598778 Reimplemented upscaling using the older FSR2 API for DX12 11 months ago
Nico de Poel 1db11a5249 Added FSR 2.2 headers and static libraries for DX11, DX12 and Vulkan 11 months ago
  1. 630
      FSR2UnityPlugin.cpp
  2. 2
      FSR2UnityPlugin.sln
  3. 23
      FSR2UnityPlugin.vcxproj
  4. 4
      FSR2UnityPlugin.vcxproj.filters
  5. 41
      FSR2UnityTypes.h
  6. 360
      FSR3UnityPlugin.cpp
  7. 2
      LICENSE
  8. 27
      UnityPluginAPI/IUnityGraphicsD3D11.h
  9. 335
      UnityPluginAPI/IUnityGraphicsVulkan.h
  10. 99
      ffx-fsr2-api/dx11/ffx_fsr2_dx11.h
  11. 3444
      ffx-fsr2-api/dx12/d3dx12.h
  12. 114
      ffx-fsr2-api/dx12/ffx_fsr2_dx12.h
  13. 132
      ffx-fsr2-api/ffx_assert.h
  14. 59
      ffx-fsr2-api/ffx_error.h
  15. 454
      ffx-fsr2-api/ffx_fsr2.h
  16. 395
      ffx-fsr2-api/ffx_fsr2_interface.h
  17. 46
      ffx-fsr2-api/ffx_fsr2_maximum_bias.h
  18. 81
      ffx-fsr2-api/ffx_fsr2_private.h
  19. 364
      ffx-fsr2-api/ffx_types.h
  20. 78
      ffx-fsr2-api/ffx_util.h
  21. 565
      ffx-fsr2-api/shaders/ffx_fsr2_common.h
  22. 105
      ffx-fsr2-api/shaders/ffx_fsr2_resources.h
  23. 158
      ffx-fsr2-api/vk/ffx_fsr2_vk.h
  24. BIN
      lib/ffx_fsr2_api/ffx_fsr2_api_dx11_x64.lib
  25. BIN
      lib/ffx_fsr2_api/ffx_fsr2_api_dx11_x64d.lib
  26. BIN
      lib/ffx_fsr2_api/ffx_fsr2_api_dx12_x64.lib
  27. BIN
      lib/ffx_fsr2_api/ffx_fsr2_api_dx12_x64d.lib
  28. BIN
      lib/ffx_fsr2_api/ffx_fsr2_api_vk_x64.lib
  29. BIN
      lib/ffx_fsr2_api/ffx_fsr2_api_vk_x64d.lib
  30. BIN
      lib/ffx_fsr2_api/ffx_fsr2_api_x64.lib
  31. BIN
      lib/ffx_fsr2_api/ffx_fsr2_api_x64d.lib

630
FSR2UnityPlugin.cpp

@ -0,0 +1,630 @@
#include <stdint.h>
#include <vector>
#include <queue>
#include <mutex>
#include "UnityPluginAPI/IUnityInterface.h"
#include "UnityPluginAPI/IUnityLog.h"
#include "UnityPluginAPI/IUnityGraphics.h"
#include "UnityPluginAPI/IUnityRenderingExtensions.h"
#include <d3d11.h>
#include <d3d12.h>
#include <dxgi.h>
#include "UnityPluginAPI/IUnityGraphicsD3D11.h"
#include "UnityPluginAPI/IUnityGraphicsD3D12.h"
#include "UnityPluginAPI/IUnityGraphicsVulkan.h"
#include "ffx-fsr2-api/ffx_fsr2.h"
#include "ffx-fsr2-api/dx11/ffx_fsr2_dx11.h"
#include "ffx-fsr2-api/dx12/ffx_fsr2_dx12.h"
#include "ffx-fsr2-api/vk/ffx_fsr2_vk.h"
#include "FSR2UnityTypes.h"
static IUnityInterfaces* s_UnityInterfaces = nullptr;
static IUnityLog* s_Log = nullptr;
static IUnityGraphics* s_Graphics = nullptr;
static IUnityGraphicsD3D11* s_GraphicsD3D11 = nullptr;
static IUnityGraphicsD3D12v7* s_GraphicsD3D12 = nullptr;
static IUnityGraphicsVulkanV2* s_GraphicsVulkan = nullptr;
static UnityGfxRenderer s_RendererType = kUnityGfxRendererNull;
static int32_t s_BaseEventId = 0;
static FfxDevice s_Device = nullptr;
static FfxFsr2Interface s_Fsr2Interface;
static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType eventType);
static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data);
static void UNITY_INTERFACE_API OnSetTextureEvent(int eventID, void* data);
struct FSR2Feature
{
FfxFsr2Context upscalingContext;
uint32_t flags;
bool isValid;
uint64_t dispatchFrameValue;
FSR2TextureTable textureTable;
};
static std::vector<FSR2Feature> s_Features;
static std::queue<uint32_t> s_FeatureSlots;
static std::mutex s_FeatureMutex;
static HANDLE s_FrameFenceEventHandle = nullptr;
// Unity plugin load event
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces)
{
s_UnityInterfaces = unityInterfaces;
s_Log = unityInterfaces->Get<IUnityLog>();
s_Graphics = unityInterfaces->Get<IUnityGraphics>();
s_BaseEventId = s_Graphics->ReserveEventIDRange((int)ePluginEventCount);
s_Graphics->RegisterDeviceEventCallback(OnGraphicsDeviceEvent);
// Run OnGraphicsDeviceEvent(initialize) manually on plugin load
// to not miss the event in case the graphics device is already initialized
OnGraphicsDeviceEvent(kUnityGfxDeviceEventInitialize);
}
// Unity plugin unload event
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload()
{
s_Graphics->UnregisterDeviceEventCallback(OnGraphicsDeviceEvent);
}
static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType eventType)
{
std::lock_guard<std::mutex> lock(s_FeatureMutex);
switch (eventType)
{
case kUnityGfxDeviceEventInitialize:
{
s_RendererType = s_Graphics->GetRenderer();
switch (s_RendererType)
{
case kUnityGfxRendererD3D11:
{
s_GraphicsD3D11 = s_UnityInterfaces->Get<IUnityGraphicsD3D11>();
if (s_GraphicsD3D11 == nullptr)
{
UNITY_LOG_ERROR(s_Log, "Could not obtain D3D11 Graphics interface!");
return;
}
break;
}
case kUnityGfxRendererD3D12:
{
s_GraphicsD3D12 = s_UnityInterfaces->Get<IUnityGraphicsD3D12v7>();
if (s_GraphicsD3D12 == nullptr)
{
UNITY_LOG_ERROR(s_Log, "Could not obtain D3D12 Graphics interface!");
return;
}
UnityD3D12PluginEventConfig eventConfig;
eventConfig.graphicsQueueAccess = kUnityD3D12GraphicsQueueAccess_DontCare;
eventConfig.flags = kUnityD3D12EventConfigFlag_EnsurePreviousFrameSubmission | kUnityD3D12EventConfigFlag_FlushCommandBuffers | kUnityD3D12EventConfigFlag_SyncWorkerThreads | kUnityD3D12EventConfigFlag_ModifiesCommandBuffersState;
eventConfig.ensureActiveRenderTextureIsBound = false;
s_GraphicsD3D12->ConfigureEvent(s_BaseEventId + FSR2PluginEvent::eExecute, &eventConfig);
break;
}
case kUnityGfxRendererVulkan:
{
s_GraphicsVulkan = s_UnityInterfaces->Get<IUnityGraphicsVulkanV2>();
if (s_GraphicsVulkan == nullptr)
{
UNITY_LOG_ERROR(s_Log, "Could not obtain Vulkan Graphics interface!");
return;
}
UnityVulkanPluginEventConfig eventConfig;
eventConfig.graphicsQueueAccess = kUnityVulkanGraphicsQueueAccess_DontCare;
eventConfig.flags = kUnityVulkanEventConfigFlag_EnsurePreviousFrameSubmission | kUnityVulkanEventConfigFlag_FlushCommandBuffers | kUnityVulkanEventConfigFlag_SyncWorkerThreads | kUnityVulkanEventConfigFlag_ModifiesCommandBuffersState;
eventConfig.renderPassPrecondition = kUnityVulkanRenderPass_EnsureInside;
s_GraphicsVulkan->ConfigureEvent(s_BaseEventId + FSR2PluginEvent::eExecute, &eventConfig);
break;
}
};
break;
}
case kUnityGfxDeviceEventShutdown:
{
s_GraphicsVulkan = nullptr;
s_GraphicsD3D12 = nullptr;
s_GraphicsD3D11 = nullptr;
s_RendererType = kUnityGfxRendererNull;
break;
}
case kUnityGfxDeviceEventBeforeReset:
{
break;
}
case kUnityGfxDeviceEventAfterReset:
{
break;
}
};
}
static uint32_t AllocateFeatureSlot()
{
if (s_FeatureSlots.empty())
{
// Create a new feature if there are no free slots
uint32_t featureSlot = (uint32_t)s_Features.size();
s_Features.push_back(std::move(FSR2Feature()));
memset(&s_Features[featureSlot], 0, sizeof(FSR2Feature));
return featureSlot;
}
// Reallocate an existing free slot
uint32_t featureSlot = s_FeatureSlots.front();
s_FeatureSlots.pop();
return featureSlot;
}
static void FreeFeatureSlot(uint32_t featureSlot)
{
// Prevent duplicate free slots
auto& slots = s_FeatureSlots._Get_container();
if (std::find(slots.begin(), slots.end(), featureSlot) != slots.end())
return;
s_FeatureSlots.push(featureSlot);
memset(&s_Features[featureSlot], 0, sizeof(FSR2Feature));
}
static PFN_vkVoidFunction GetVulkanDeviceProcAddr(VkDevice device, const char* pName)
{
UnityVulkanInstance instance = s_GraphicsVulkan->Instance();
return instance.getInstanceProcAddr(instance.instance, pName);
}
extern "C" bool UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_InitApi()
{
std::lock_guard<std::mutex> lock(s_FeatureMutex);
if (s_GraphicsD3D11 != nullptr)
{
ID3D11Device* device = s_GraphicsD3D11->GetDevice();
if (device == nullptr)
return false;
s_Device = ffxGetDeviceDX11(device);
size_t scratchBufferSize = ffxFsr2GetScratchMemorySizeDX11();
void* scratchBuffer = malloc(scratchBufferSize);
ffxFsr2GetInterfaceDX11(&s_Fsr2Interface, device, scratchBuffer, scratchBufferSize);
return true;
}
else if (s_GraphicsD3D12 != nullptr)
{
ID3D12Device* device = s_GraphicsD3D12->GetDevice();
if (device == nullptr)
return false;
s_Device = ffxGetDeviceDX12(device);
s_FrameFenceEventHandle = CreateEventEx(nullptr, nullptr, 0, EVENT_ALL_ACCESS);
size_t scratchBufferSize = ffxFsr2GetScratchMemorySizeDX12();
void* scratchBuffer = malloc(scratchBufferSize);
ffxFsr2GetInterfaceDX12(&s_Fsr2Interface, device, scratchBuffer, scratchBufferSize);
return true;
}
else if (s_GraphicsVulkan != nullptr)
{
UnityVulkanInstance instance = s_GraphicsVulkan->Instance();
if (instance.device == nullptr)
return false;
s_Device = ffxGetDeviceVK(instance.device);
size_t scratchBufferSize = ffxFsr2GetScratchMemorySizeVK(instance.physicalDevice);
void* scratchBuffer = malloc(scratchBufferSize);
ffxFsr2GetInterfaceVK(&s_Fsr2Interface, scratchBuffer, scratchBufferSize, instance.physicalDevice, &GetVulkanDeviceProcAddr);
return true;
}
return false;
}
static void DestroyFeature(uint32_t featureSlot)
{
uint64_t dispatchFrameValue = 0;
{
// We need to lock the features list while we're accessing it, but we also can't hold the lock while blocking the main/render thread at the same time.
// Otherwise we're very likely to create a deadlock. So instead we only lock for as long as is necessary to grab the data we need from the feature.
std::lock_guard<std::mutex> lock(s_FeatureMutex);
auto& feature = s_Features[featureSlot];
if (!feature.isValid)
return;
dispatchFrameValue = feature.dispatchFrameValue;
}
if (s_GraphicsD3D12 != nullptr)
{
// If there's still an upscale dispatch executing on the current frame, wait until rendering is finished before destroying its context.
// Thanks to https://alextardif.com/D3D11To12P1.html for explaining how fences work and how to make the CPU wait for a command queue to complete.
ID3D12Fence* frameFence = s_GraphicsD3D12->GetFrameFence();
if (dispatchFrameValue > frameFence->GetCompletedValue())
{
frameFence->SetEventOnCompletion(dispatchFrameValue, s_FrameFenceEventHandle);
WaitForSingleObjectEx(s_FrameFenceEventHandle, INFINITE, false);
}
}
else if (s_GraphicsVulkan != nullptr)
{
UnityVulkanRecordingState state;
s_GraphicsVulkan->CommandRecordingState(&state, kUnityVulkanGraphicsQueueAccess_DontCare);
if (dispatchFrameValue > state.safeFrameNumber)
{
UnityVulkanInstance instance = s_GraphicsVulkan->Instance();
vkQueueWaitIdle(instance.graphicsQueue);
}
}
std::lock_guard<std::mutex> lock(s_FeatureMutex);
auto& feature = s_Features[featureSlot];
ffxFsr2ContextDestroy(&feature.upscalingContext);
FreeFeatureSlot(featureSlot);
}
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_ShutdownApi()
{
size_t numFeatures = 0;
{
std::lock_guard<std::mutex> lock(s_FeatureMutex);
numFeatures = s_Features.size();
}
for (uint32_t slot = 0; slot < numFeatures; ++slot)
{
DestroyFeature(slot);
}
std::lock_guard<std::mutex> lock(s_FeatureMutex);
if (s_Fsr2Interface.scratchBuffer != nullptr)
{
free(s_Fsr2Interface.scratchBuffer);
s_Fsr2Interface.scratchBuffer = nullptr;
}
if (s_FrameFenceEventHandle != nullptr)
{
CloseHandle(s_FrameFenceEventHandle);
s_FrameFenceEventHandle = nullptr;
}
s_Device = nullptr;
}
extern "C" uint32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetDeviceVersion()
{
return 0x0u;
}
extern "C" UnityRenderingEventAndData UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetRenderEventCallback()
{
return OnRenderEventAndData;
}
extern "C" UnityRenderingEventAndData UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetSetTextureEventCallback()
{
return OnSetTextureEvent;
}
extern "C" uint32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_CreateFeatureSlot()
{
std::lock_guard<std::mutex> lock(s_FeatureMutex);
return AllocateFeatureSlot();
}
extern "C" float UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetUpscaleRatioFromQualityMode(FSR2Quality qualityMode)
{
switch (qualityMode)
{
case FSR2Quality::qQuality:
return 1.5f;
case FSR2Quality::qBalanced:
return 1.7f;
case FSR2Quality::qPerformance:
return 2.0f;
case FSR2Quality::qUltraPerformance:
return 3.0f;
default:
return 1.0f;
}
}
extern "C" bool UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetRenderResolutionFromQualityMode(FSR2Quality qualityMode, uint32_t displayWidth, uint32_t displayHeight, uint32_t* renderWidth, uint32_t* renderHeight)
{
if (renderWidth == nullptr || renderHeight == nullptr)
return false;
float ratio = AMDUP_GetUpscaleRatioFromQualityMode(qualityMode);
*renderWidth = (uint32_t)roundf(displayWidth / ratio);
*renderHeight = (uint32_t)roundf(displayHeight / ratio);
return true;
}
extern "C" int32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetBaseEventId()
{
return s_BaseEventId;
}
static FfxResource GetVulkanTextureResource(UnityVulkanInstance& instance, FSR2Feature& feature, FSR2TextureDesc& texture, FfxResourceStates state = FFX_RESOURCE_STATE_COMPUTE_READ)
{
VkImageView imageView = nullptr;
if (texture.image != 0)
{
VkImageViewCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
createInfo.image = (VkImage)texture.image;
createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
createInfo.format = (VkFormat)texture.format;
createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
createInfo.subresourceRange.levelCount = 1;
createInfo.subresourceRange.layerCount = 1;
vkCreateImageView(instance.device, &createInfo, nullptr, &imageView);
}
texture.view = (intptr_t)imageView;
return ffxGetTextureResourceVK(&feature.upscalingContext, (VkImage)texture.image, imageView, texture.width, texture.height, (VkFormat)texture.format, nullptr, state);
}
static void DestroyVulkanImageView(UnityVulkanInstance& instance, FSR2TextureDesc& texture)
{
if (texture.view == 0)
return;
vkDestroyImageView(instance.device, (VkImageView)texture.view, nullptr);
texture.view = 0;
}
// Plugin function to handle a specific rendering event
static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data)
{
if (s_Device == nullptr)
return;
// User rendering code
switch (eventID - s_BaseEventId)
{
case FSR2PluginEvent::eDestroyFeature:
{
uint32_t featureSlot = (uint32_t)(int64_t)data;
if (featureSlot < 0 || featureSlot >= s_Features.size())
return;
DestroyFeature(featureSlot);
break;
}
case FSR2PluginEvent::eExecute:
{
std::lock_guard<std::mutex> lock(s_FeatureMutex);
auto* params = (FSR2CommandExecutionData*)data;
if (params->featureSlot < 0 || params->featureSlot >= s_Features.size())
return;
auto& feature = s_Features[params->featureSlot];
if (!feature.isValid)
return;
FfxFsr2DispatchDescription dispatchDescription{};
if (s_GraphicsD3D11 != nullptr)
{
ID3D11DeviceContext* ctx = nullptr;
s_GraphicsD3D11->GetDevice()->GetImmediateContext(&ctx);
dispatchDescription.commandList = ctx;
dispatchDescription.color = ffxGetResourceDX11(&feature.upscalingContext, (ID3D11Resource*)feature.textureTable.colorInput.image);
dispatchDescription.depth = ffxGetResourceDX11(&feature.upscalingContext, (ID3D11Resource*)feature.textureTable.depth.image);
dispatchDescription.motionVectors = ffxGetResourceDX11(&feature.upscalingContext, (ID3D11Resource*)feature.textureTable.motionVectors.image);
dispatchDescription.exposure = ffxGetResourceDX11(&feature.upscalingContext, (ID3D11Resource*)feature.textureTable.exposureTexture.image);
dispatchDescription.reactive = ffxGetResourceDX11(&feature.upscalingContext, (ID3D11Resource*)feature.textureTable.reactiveMask.image);
dispatchDescription.transparencyAndComposition = ffxGetResourceDX11(&feature.upscalingContext, (ID3D11Resource*)feature.textureTable.transparencyMask.image);
dispatchDescription.output = ffxGetResourceDX11(&feature.upscalingContext, (ID3D11Resource*)feature.textureTable.colorOutput.image, nullptr, FFX_RESOURCE_STATE_UNORDERED_ACCESS);
if (dispatchDescription.reactive.resource == nullptr)
dispatchDescription.reactive = ffxGetResourceDX11(&feature.upscalingContext, (ID3D11Resource*)feature.textureTable.biasColorMask.image);
}
else if (s_GraphicsD3D12 != nullptr)
{
UnityGraphicsD3D12RecordingState state;
s_GraphicsD3D12->CommandRecordingState(&state);
dispatchDescription.commandList = ffxGetCommandListDX12(state.commandList);
// Keep track of which frame this dispatch is happening on, so we can verify when it's finished
feature.dispatchFrameValue = s_GraphicsD3D12->GetNextFrameFenceValue();
dispatchDescription.color = ffxGetResourceDX12(&feature.upscalingContext, (ID3D12Resource*)feature.textureTable.colorInput.image);
dispatchDescription.depth = ffxGetResourceDX12(&feature.upscalingContext, (ID3D12Resource*)feature.textureTable.depth.image);
dispatchDescription.motionVectors = ffxGetResourceDX12(&feature.upscalingContext, (ID3D12Resource*)feature.textureTable.motionVectors.image);
dispatchDescription.exposure = ffxGetResourceDX12(&feature.upscalingContext, (ID3D12Resource*)feature.textureTable.exposureTexture.image);
dispatchDescription.reactive = ffxGetResourceDX12(&feature.upscalingContext, (ID3D12Resource*)feature.textureTable.reactiveMask.image);
dispatchDescription.transparencyAndComposition = ffxGetResourceDX12(&feature.upscalingContext, (ID3D12Resource*)feature.textureTable.transparencyMask.image);
dispatchDescription.output = ffxGetResourceDX12(&feature.upscalingContext, (ID3D12Resource*)feature.textureTable.colorOutput.image, nullptr, FFX_RESOURCE_STATE_UNORDERED_ACCESS);
if (dispatchDescription.reactive.resource == nullptr)
dispatchDescription.reactive = ffxGetResourceDX12(&feature.upscalingContext, (ID3D12Resource*)feature.textureTable.biasColorMask.image);
}
else if (s_GraphicsVulkan != nullptr)
{
UnityVulkanRecordingState state;
s_GraphicsVulkan->CommandRecordingState(&state, kUnityVulkanGraphicsQueueAccess_DontCare);
dispatchDescription.commandList = ffxGetCommandListVK(state.commandBuffer);
// Keep track of which frame this dispatch is happening on, so we can verify when it's finished
feature.dispatchFrameValue = state.currentFrameNumber;
UnityVulkanInstance instance = s_GraphicsVulkan->Instance();
dispatchDescription.color = GetVulkanTextureResource(instance, feature, feature.textureTable.colorInput);
dispatchDescription.depth = GetVulkanTextureResource(instance, feature, feature.textureTable.depth);
dispatchDescription.motionVectors = GetVulkanTextureResource(instance, feature, feature.textureTable.motionVectors);
dispatchDescription.exposure = GetVulkanTextureResource(instance, feature, feature.textureTable.exposureTexture);
dispatchDescription.reactive = GetVulkanTextureResource(instance, feature, feature.textureTable.reactiveMask);
dispatchDescription.transparencyAndComposition = GetVulkanTextureResource(instance, feature, feature.textureTable.transparencyMask);
dispatchDescription.output = GetVulkanTextureResource(instance, feature, feature.textureTable.colorOutput, FFX_RESOURCE_STATE_UNORDERED_ACCESS);
if (dispatchDescription.reactive.resource == nullptr)
dispatchDescription.reactive = GetVulkanTextureResource(instance, feature, feature.textureTable.biasColorMask);
}
dispatchDescription.jitterOffset.x = params->jitterOffsetX;
dispatchDescription.jitterOffset.y = params->jitterOffsetY;
dispatchDescription.motionVectorScale.x = params->MVScaleX;
dispatchDescription.motionVectorScale.y = params->MVScaleY;
dispatchDescription.reset = params->reset;
dispatchDescription.enableSharpening = params->enableSharpening;
dispatchDescription.sharpness = params->sharpness;
dispatchDescription.frameTimeDelta = params->frameTimeDelta;
dispatchDescription.preExposure = params->preExposure;
dispatchDescription.renderSize.width = params->renderSizeWidth;
dispatchDescription.renderSize.height = params->renderSizeHeight;
dispatchDescription.cameraFovAngleVertical = params->cameraFovAngleVertical;
if (feature.flags & FFX_FSR2_ENABLE_DEPTH_INVERTED)
{
dispatchDescription.cameraFar = params->cameraNear;
dispatchDescription.cameraNear = params->cameraFar;
}
else
{
dispatchDescription.cameraFar = params->cameraFar;
dispatchDescription.cameraNear = params->cameraNear;
}
ffxFsr2ContextDispatch(&feature.upscalingContext, &dispatchDescription);
break;
}
case FSR2PluginEvent::ePostExecute:
{
std::lock_guard<std::mutex> lock(s_FeatureMutex);
auto* params = (FSR2CommandExecutionData*)data;
if (params->featureSlot < 0 || params->featureSlot >= s_Features.size())
return;
auto& feature = s_Features[params->featureSlot];
if (s_GraphicsVulkan != nullptr)
{
UnityVulkanInstance instance = s_GraphicsVulkan->Instance();
DestroyVulkanImageView(instance, feature.textureTable.colorInput);
DestroyVulkanImageView(instance, feature.textureTable.depth);
DestroyVulkanImageView(instance, feature.textureTable.motionVectors);
DestroyVulkanImageView(instance, feature.textureTable.exposureTexture);
DestroyVulkanImageView(instance, feature.textureTable.reactiveMask);
DestroyVulkanImageView(instance, feature.textureTable.transparencyMask);
DestroyVulkanImageView(instance, feature.textureTable.colorOutput);
}
break;
}
case FSR2PluginEvent::eInit:
{
std::lock_guard<std::mutex> lock(s_FeatureMutex);
auto* params = (FSR2CommandInitializationData*)data;
if (params->featureSlot < 0 || params->featureSlot >= s_Features.size())
return;
auto& feature = s_Features[params->featureSlot];
feature.flags = params->flags;
FfxFsr2ContextDescription contextDescription{};
contextDescription.callbacks = s_Fsr2Interface;
contextDescription.device = s_Device;
contextDescription.displaySize = { params->displaySizeWidth, params->displaySizeHeight };
contextDescription.maxRenderSize = { params->maxRenderSizeWidth, params->maxRenderSizeHeight };
contextDescription.flags = params->flags;
feature.isValid = ffxFsr2ContextCreate(&feature.upscalingContext, &contextDescription) == FFX_OK;
break;
}
}
}
static void UNITY_INTERFACE_API OnSetTextureEvent(int eventID, void* data)
{
std::lock_guard<std::mutex> lock(s_FeatureMutex);
auto* params = (UnityRenderingExtTextureUpdateParamsV2*)data;
// userData = (featureId & (int) ushort.MaxValue) << 16 | (textureSlot & (int) short.MaxValue) << 1 | (clearTextureTable ? 1 : 0);
uint32_t featureSlot = (params->userData >> 16) & 0xFFFF;
uint32_t textureSlot = (params->userData >> 1) & 0x7FFF;
uint32_t clearTextureTable = params->userData & 0x1;
if (featureSlot < 0 || featureSlot >= s_Features.size())
return;
auto& feature = s_Features[featureSlot];
// User rendering code
switch (eventID)
{
case kUnityRenderingExtEventUpdateTextureBeginV2:
{
if (clearTextureTable)
{
memset(&feature.textureTable, 0, sizeof(FSR2TextureTable));
}
break;
}
case kUnityRenderingExtEventUpdateTextureEndV2:
{
// We organized the texture table struct to be ordered the same as the texture slot enum
// This way we can use the texture slot value simply as a pointer offset into the texture table
FSR2TextureDesc* textureDesc = ((FSR2TextureDesc*)&feature.textureTable) + textureSlot;
if (s_GraphicsD3D11 != nullptr)
{
textureDesc->image = (intptr_t)s_GraphicsD3D11->TextureFromNativeTexture((UnityTextureID)params->textureID);
}
else if (s_GraphicsD3D12 != nullptr)
{
textureDesc->image = (intptr_t)s_GraphicsD3D12->TextureFromNativeTexture((UnityTextureID)params->textureID);
}
else if (s_GraphicsVulkan != nullptr)
{
VkAccessFlags accessFlags = (textureSlot == FSR2Textures::tColorOutput) ? VK_ACCESS_SHADER_WRITE_BIT : VK_ACCESS_SHADER_READ_BIT;
UnityVulkanImage image;
if (s_GraphicsVulkan->AccessTextureByID((UnityTextureID)params->textureID, UnityVulkanWholeImage,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, accessFlags,
kUnityVulkanResourceAccess_PipelineBarrier, &image))
{
textureDesc->image = (intptr_t)image.image;
textureDesc->view = 0;
textureDesc->width = params->width;
textureDesc->height = params->height;
textureDesc->format = (uint32_t)image.format;
}
else
{
textureDesc->image = 0;
textureDesc->view = 0;
}
}
break;
}
}
}

2
FSR3UnityPlugin.sln → FSR2UnityPlugin.sln

@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.35312.102
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FSR3UnityPlugin", "FSR3UnityPlugin.vcxproj", "{505A5886-66BC-4210-B4E8-1301CFB75E74}"
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FSR2UnityPlugin", "FSR2UnityPlugin.vcxproj", "{505A5886-66BC-4210-B4E8-1301CFB75E74}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

23
FSR3UnityPlugin.vcxproj → FSR2UnityPlugin.vcxproj

@ -22,8 +22,9 @@
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{505a5886-66bc-4210-b4e8-1301cfb75e74}</ProjectGuid>
<RootNamespace>FSR3UnityPlugin</RootNamespace>
<RootNamespace>FSR2UnityPlugin</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<ProjectName>FSR2UnityPlugin</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
@ -80,7 +81,7 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;FSR3UNITYPLUGIN_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;_DEBUG;FSR2UNITYPLUGIN_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
@ -97,7 +98,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;FSR3UNITYPLUGIN_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;NDEBUG;FSR2UNITYPLUGIN_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
@ -114,16 +115,18 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;FSR3UNITYPLUGIN_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_DEBUG;FSR2UNITYPLUGIN_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>%VULKAN_SDK%\Include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<AdditionalDependencies>$(CoreLibraryDependencies);%(AdditionalDependencies);lib\amd_fidelityfx_dx12.lib</AdditionalDependencies>
<AdditionalDependencies>$(CoreLibraryDependencies);%(AdditionalDependencies);lib\ffx_fsr2_api\ffx_fsr2_api_x64d.lib;lib\ffx_fsr2_api\ffx_fsr2_api_dx11_x64d.lib;lib\ffx_fsr2_api\ffx_fsr2_api_dx12_x64d.lib;lib\ffx_fsr2_api\ffx_fsr2_api_vk_x64d.lib;vulkan-1.lib;dxguid.lib</AdditionalDependencies>
<AdditionalLibraryDirectories>%VULKAN_SDK%\Lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@ -132,10 +135,11 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;FSR3UNITYPLUGIN_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>NDEBUG;FSR2UNITYPLUGIN_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>%VULKAN_SDK%\Include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
@ -143,14 +147,15 @@
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<AdditionalDependencies>$(CoreLibraryDependencies);%(AdditionalDependencies);lib\amd_fidelityfx_dx12.lib</AdditionalDependencies>
<AdditionalDependencies>$(CoreLibraryDependencies);%(AdditionalDependencies);lib\ffx_fsr2_api\ffx_fsr2_api_x64.lib;lib\ffx_fsr2_api\ffx_fsr2_api_dx11_x64.lib;lib\ffx_fsr2_api\ffx_fsr2_api_dx12_x64.lib;lib\ffx_fsr2_api\ffx_fsr2_api_vk_x64.lib;vulkan-1.lib;dxguid.lib</AdditionalDependencies>
<AdditionalLibraryDirectories>%VULKAN_SDK%\Lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="FSR3UnityPlugin.cpp" />
<ClCompile Include="FSR2UnityPlugin.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="FSR3UnityTypes.h" />
<ClInclude Include="FSR2UnityTypes.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

4
FSR3UnityPlugin.vcxproj.filters → FSR2UnityPlugin.vcxproj.filters

@ -15,12 +15,12 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="FSR3UnityPlugin.cpp">
<ClCompile Include="FSR2UnityPlugin.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="FSR3UnityTypes.h">
<ClInclude Include="FSR2UnityTypes.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>

41
FSR3UnityTypes.h → FSR2UnityTypes.h

@ -1,15 +1,16 @@
#pragma once
enum FSR3PluginEvent : int32_t
enum FSR2PluginEvent : int32_t
{
eDestroyFeature,
eExecute,
ePostExecute,
eInit,
eSetTextures
ePluginEventCount
};
enum FSR3Quality: int32_t
enum FSR2Quality: int32_t
{
qQuality,
qBalanced,
@ -17,7 +18,7 @@ enum FSR3Quality: int32_t
qUltraPerformance
};
enum FSR3Textures: int32_t
enum FSR2Textures: int32_t
{
tColorInput,
tColorOutput,
@ -29,7 +30,7 @@ enum FSR3Textures: int32_t
tBiasColorMask,
};
struct FSR3CommandInitializationData
struct FSR2CommandInitializationData
{
uint32_t maxRenderSizeWidth;
uint32_t maxRenderSizeHeight;
@ -40,7 +41,7 @@ struct FSR3CommandInitializationData
uint32_t featureSlot;
};
struct FSR3CommandExecutionData
struct FSR2CommandExecutionData
{
float jitterOffsetX;
float jitterOffsetY;
@ -60,16 +61,24 @@ struct FSR3CommandExecutionData
uint32_t featureSlot;
};
struct FSR3TextureTable
struct FSR2TextureDesc
{
intptr_t colorInput;
intptr_t colorOutput;
intptr_t depth;
intptr_t motionVectors;
intptr_t transparencyMask;
intptr_t exposureTexture;
intptr_t reactiveMask;
intptr_t biasColorMask;
intptr_t image;
intptr_t view;
uint32_t featureSlot;
uint32_t width;
uint32_t height;
uint32_t format;
};
struct FSR2TextureTable
{
FSR2TextureDesc colorInput;
FSR2TextureDesc colorOutput;
FSR2TextureDesc depth;
FSR2TextureDesc motionVectors;
FSR2TextureDesc transparencyMask;
FSR2TextureDesc exposureTexture;
FSR2TextureDesc reactiveMask;
FSR2TextureDesc biasColorMask;
};

360
FSR3UnityPlugin.cpp

@ -1,360 +0,0 @@
#include <stdint.h>
#include <vector>
#include <queue>
#include <mutex>
#include "UnityPluginAPI/IUnityInterface.h"
#include "UnityPluginAPI/IUnityLog.h"
#include "UnityPluginAPI/IUnityGraphics.h"
#include "UnityPluginAPI/IUnityRenderingExtensions.h"
#include <d3d12.h>
#include <dxgi.h>
#include "UnityPluginAPI/IUnityGraphicsD3D12.h"
#include "ffx_api/ffx_upscale.hpp"
#include "ffx_api/dx12/ffx_api_dx12.hpp"
#include "FSR3UnityTypes.h"
static const int32_t BaseEventId = 0;
static IUnityInterfaces* s_UnityInterfaces = nullptr;
static IUnityLog* s_Log = nullptr;
static IUnityGraphics* s_Graphics = nullptr;
static IUnityGraphicsD3D12v7* s_GraphicsD3D12 = nullptr;
static UnityGfxRenderer s_RendererType = kUnityGfxRendererNull;
static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType eventType);
static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data);
static void UNITY_INTERFACE_API OnSetTextureEvent(int eventID, void* data);
struct FSR3Feature
{
ffx::Context upscalingContext;
uint32_t upscaleSizeWidth;
uint32_t upscaleSizeHeight;
uint32_t flags;
FSR3TextureTable textureTable;
};
static std::vector<FSR3Feature> s_Features;
static std::queue<uint32_t> s_FeatureSlots;
static std::mutex s_FeatureMutex;
static ffx::CreateBackendDX12Desc s_BackendDesc{};
// Unity plugin load event
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces)
{
s_UnityInterfaces = unityInterfaces;
s_Log = unityInterfaces->Get<IUnityLog>();
s_Graphics = unityInterfaces->Get<IUnityGraphics>();
s_Graphics->RegisterDeviceEventCallback(OnGraphicsDeviceEvent);
// Run OnGraphicsDeviceEvent(initialize) manually on plugin load
// to not miss the event in case the graphics device is already initialized
OnGraphicsDeviceEvent(kUnityGfxDeviceEventInitialize);
}
// Unity plugin unload event
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload()
{
s_Graphics->UnregisterDeviceEventCallback(OnGraphicsDeviceEvent);
}
static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType eventType)
{
switch (eventType)
{
case kUnityGfxDeviceEventInitialize:
{
s_RendererType = s_Graphics->GetRenderer();
if (s_RendererType != kUnityGfxRendererD3D12)
return;
s_GraphicsD3D12 = s_UnityInterfaces->Get<IUnityGraphicsD3D12v7>();
if (s_GraphicsD3D12 == nullptr)
{
UNITY_LOG_ERROR(s_Log, "Could not obtain D3D12 Graphics interface!");
return;
}
break;
}
case kUnityGfxDeviceEventShutdown:
{
s_GraphicsD3D12 = nullptr;
s_RendererType = kUnityGfxRendererNull;
break;
}
case kUnityGfxDeviceEventBeforeReset:
{
break;
}
case kUnityGfxDeviceEventAfterReset:
{
break;
}
};
}
// Thread-safe allocation of FSR3 feature slot
static uint32_t AllocateFeatureSlot()
{
std::lock_guard<std::mutex> lock(s_FeatureMutex);
if (s_FeatureSlots.empty())
{
// Create a new feature if there are no free slots
uint32_t featureSlot = (uint32_t)s_Features.size();
s_Features.push_back(std::move(FSR3Feature()));
return featureSlot;
}
// Reallocate an existing free slot
uint32_t featureSlot = s_FeatureSlots.front();
s_FeatureSlots.pop();
return featureSlot;
}
// Thread-safe freeing and clearing of FSR3 feature slot
static void FreeFeatureSlot(uint32_t featureSlot)
{
std::lock_guard<std::mutex> lock(s_FeatureMutex);
s_FeatureSlots.push(featureSlot);
memset(&s_Features[featureSlot], 0, sizeof(FSR3Feature));
}
extern "C" bool UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_InitApi()
{
if (s_GraphicsD3D12 == nullptr)
return false;
ID3D12Device* device = s_GraphicsD3D12->GetDevice();
if (device == nullptr)
return false;
s_BackendDesc.device = device;
return true;
}
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_ShutdownApi()
{
for (uint32_t slot = 0; slot < s_Features.size(); ++slot)
{
auto& feature = s_Features[slot];
if (feature.upscalingContext != nullptr)
{
ffx::DestroyContext(feature.upscalingContext);
FreeFeatureSlot(slot);
}
}
s_BackendDesc.device = nullptr;
}
extern "C" uint32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetDeviceVersion()
{
return 0x0u;
}
extern "C" UnityRenderingEventAndData UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetRenderEventCallback()
{
return OnRenderEventAndData;
}
extern "C" UnityRenderingEventAndData UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetSetTextureEventCallback()
{
return OnSetTextureEvent;
}
extern "C" uint32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_CreateFeatureSlot()
{
return AllocateFeatureSlot();
}
extern "C" float UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetUpscaleRatioFromQualityMode(FSR3Quality qualityMode)
{
switch (qualityMode)
{
case FSR3Quality::qQuality:
return 1.5f;
case FSR3Quality::qBalanced:
return 1.7f;
case FSR3Quality::qPerformance:
return 2.0f;
case FSR3Quality::qUltraPerformance:
return 3.0f;
default:
return 1.0f;
}
}
extern "C" bool UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetRenderResolutionFromQualityMode(FSR3Quality qualityMode, uint32_t displayWidth, uint32_t displayHeight, uint32_t* renderWidth, uint32_t* renderHeight)
{
if (renderWidth == nullptr || renderHeight == nullptr)
return false;
float ratio = AMDUP_GetUpscaleRatioFromQualityMode(qualityMode);
*renderWidth = (uint32_t)roundf(displayWidth / ratio);
*renderHeight = (uint32_t)roundf(displayHeight / ratio);
return true;
}
extern "C" int32_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API AMDUP_GetBaseEventId()
{
return BaseEventId;
}
// Plugin function to handle a specific rendering event
static void UNITY_INTERFACE_API OnRenderEventAndData(int eventID, void* data)
{
if (s_GraphicsD3D12 == nullptr || s_BackendDesc.device == nullptr)
return;
// User rendering code
switch (eventID)
{
case BaseEventId + FSR3PluginEvent::eDestroyFeature:
{
uint32_t featureSlot = (uint32_t)(int64_t)data;
if (featureSlot < 0 || featureSlot >= s_Features.size())
return;
auto& feature = s_Features[featureSlot];
if (feature.upscalingContext != nullptr)
{
ffx::DestroyContext(feature.upscalingContext);
FreeFeatureSlot(featureSlot);
}
break;
}
case BaseEventId + FSR3PluginEvent::eExecute:
{
auto* params = (FSR3CommandExecutionData*)data;
if (params->featureSlot < 0 || params->featureSlot >= s_Features.size())
return;
auto& feature = s_Features[params->featureSlot];
UnityGraphicsD3D12RecordingState state;
s_GraphicsD3D12->CommandRecordingState(&state);
ffx::DispatchDescUpscale dispatchUpscale{};
dispatchUpscale.commandList = state.commandList;
dispatchUpscale.color = ffxApiGetResourceDX12((ID3D12Resource*)feature.textureTable.colorInput, FFX_API_RESOURCE_STATE_PIXEL_COMPUTE_READ);
dispatchUpscale.depth = ffxApiGetResourceDX12((ID3D12Resource*)feature.textureTable.depth, FFX_API_RESOURCE_STATE_PIXEL_COMPUTE_READ);
dispatchUpscale.motionVectors = ffxApiGetResourceDX12((ID3D12Resource*)feature.textureTable.motionVectors, FFX_API_RESOURCE_STATE_PIXEL_COMPUTE_READ);
dispatchUpscale.exposure = ffxApiGetResourceDX12((ID3D12Resource*)feature.textureTable.exposureTexture, FFX_API_RESOURCE_STATE_PIXEL_COMPUTE_READ);
dispatchUpscale.reactive = ffxApiGetResourceDX12((ID3D12Resource*)feature.textureTable.reactiveMask, FFX_API_RESOURCE_STATE_PIXEL_COMPUTE_READ);
dispatchUpscale.transparencyAndComposition = ffxApiGetResourceDX12((ID3D12Resource*)feature.textureTable.transparencyMask, FFX_API_RESOURCE_STATE_PIXEL_COMPUTE_READ);
dispatchUpscale.output = ffxApiGetResourceDX12((ID3D12Resource*)feature.textureTable.colorOutput, FFX_API_RESOURCE_STATE_UNORDERED_ACCESS);
dispatchUpscale.jitterOffset.x = params->jitterOffsetX;
dispatchUpscale.jitterOffset.y = params->jitterOffsetY;
dispatchUpscale.motionVectorScale.x = params->MVScaleX;
dispatchUpscale.motionVectorScale.y = params->MVScaleY;
dispatchUpscale.reset = params->reset;
dispatchUpscale.enableSharpening = params->enableSharpening;
dispatchUpscale.sharpness = params->sharpness;
dispatchUpscale.frameTimeDelta = params->frameTimeDelta;
dispatchUpscale.preExposure = params->preExposure;
dispatchUpscale.renderSize.width = params->renderSizeWidth;
dispatchUpscale.renderSize.height = params->renderSizeHeight;
dispatchUpscale.upscaleSize.width = feature.upscaleSizeWidth;
dispatchUpscale.upscaleSize.height = feature.upscaleSizeHeight;
dispatchUpscale.cameraFovAngleVertical = params->cameraFovAngleVertical;
if (feature.flags & FFX_UPSCALE_ENABLE_DEPTH_INVERTED)
{
dispatchUpscale.cameraFar = params->cameraNear;
dispatchUpscale.cameraNear = params->cameraFar;
}
else
{
dispatchUpscale.cameraFar = params->cameraFar;
dispatchUpscale.cameraNear = params->cameraNear;
}
ffx::Dispatch(feature.upscalingContext, dispatchUpscale);
break;
}
case BaseEventId + FSR3PluginEvent::ePostExecute:
{
auto* params = (FSR3CommandExecutionData*)data;
break;
}
case BaseEventId + FSR3PluginEvent::eInit:
{
auto* params = (FSR3CommandInitializationData*)data;
if (params->featureSlot < 0 || params->featureSlot >= s_Features.size())
return;
auto& feature = s_Features[params->featureSlot];
feature.upscaleSizeWidth = params->displaySizeWidth;
feature.upscaleSizeHeight = params->displaySizeHeight;
feature.flags = params->flags;
ffx::CreateContextDescUpscale createUpscaling;
createUpscaling.maxUpscaleSize = { params->displaySizeWidth, params->displaySizeHeight };
createUpscaling.maxRenderSize = { params->maxRenderSizeWidth, params->maxRenderSizeHeight };
createUpscaling.flags = params->flags;
ffx::CreateContext(feature.upscalingContext, nullptr, createUpscaling, s_BackendDesc);
break;
}
case BaseEventId + FSR3PluginEvent::eSetTextures:
auto* params = (FSR3TextureTable*)data;
if (params->featureSlot < 0 || params->featureSlot >= s_Features.size())
return;
auto& feature = s_Features[params->featureSlot];
feature.textureTable = *params;
break;
}
}
static void UNITY_INTERFACE_API OnSetTextureEvent(int eventID, void* data)
{
if (s_GraphicsD3D12 == nullptr)
return;
auto* params = (UnityRenderingExtTextureUpdateParamsV2*)data;
// userData = (featureId & (int) ushort.MaxValue) << 16 | (textureSlot & (int) short.MaxValue) << 1 | (clearTextureTable ? 1 : 0);
uint32_t featureSlot = (params->userData >> 16) & 0xFFFF;
uint32_t textureSlot = (params->userData >> 1) & 0x7FFF;
uint32_t clearTextureTable = params->userData & 0x1;
if (featureSlot < 0 || featureSlot >= s_Features.size())
return;
auto& feature = s_Features[featureSlot];
// User rendering code
switch (eventID)
{
case kUnityRenderingExtEventUpdateTextureBeginV2:
{
if (clearTextureTable)
{
memset(&feature.textureTable, 0, sizeof(FSR3TextureTable));
}
break;
}
case kUnityRenderingExtEventUpdateTextureEndV2:
{
// We organized the texture table struct to be ordered the same as the texture slot enum
// This way we can use the texture slot value simply as a pointer offset into the texture table
*(((intptr_t*)&feature.textureTable) + textureSlot) = (intptr_t)s_GraphicsD3D12->TextureFromNativeTexture((UnityTextureID)params->textureID);
break;
}
}
}

2
LICENSE

@ -1,4 +1,4 @@
Copyright (c) 2025 Nico de Poel
Copyright (c) 2025 Nico de Poel, Dominic de Graaf, Mart van Saane
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

27
UnityPluginAPI/IUnityGraphicsD3D11.h

@ -0,0 +1,27 @@
// Unity Native Plugin API copyright © 2015 Unity Technologies ApS
//
// Licensed under the Unity Companion License for Unity - dependent projects--see[Unity Companion License](http://www.unity3d.com/legal/licenses/Unity_Companion_License).
//
// Unless expressly provided otherwise, the Software under this license is made available strictly on an AS IS BASIS WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED.Please review the license for details on these and other terms and conditions.
#pragma once
#include "IUnityInterface.h"
// Should only be used on the rendering thread unless noted otherwise.
UNITY_DECLARE_INTERFACE(IUnityGraphicsD3D11)
{
ID3D11Device* (UNITY_INTERFACE_API * GetDevice)();
ID3D11Resource* (UNITY_INTERFACE_API * TextureFromRenderBuffer)(UnityRenderBuffer buffer);
ID3D11Resource* (UNITY_INTERFACE_API * TextureFromNativeTexture)(UnityTextureID texture);
ID3D11RenderTargetView* (UNITY_INTERFACE_API * RTVFromRenderBuffer)(UnityRenderBuffer surface);
ID3D11ShaderResourceView* (UNITY_INTERFACE_API * SRVFromNativeTexture)(UnityTextureID texture);
IDXGISwapChain* (UNITY_INTERFACE_API * GetSwapChain)();
UINT32(UNITY_INTERFACE_API * GetSyncInterval)();
UINT(UNITY_INTERFACE_API * GetPresentFlags)();
};
UNITY_REGISTER_INTERFACE_GUID(0xAAB37EF87A87D748ULL, 0xBF76967F07EFB177ULL, IUnityGraphicsD3D11)

335
UnityPluginAPI/IUnityGraphicsVulkan.h

@ -0,0 +1,335 @@
// Unity Native Plugin API copyright © 2015 Unity Technologies ApS
//
// Licensed under the Unity Companion License for Unity - dependent projects--see[Unity Companion License](http://www.unity3d.com/legal/licenses/Unity_Companion_License).
//
// Unless expressly provided otherwise, the Software under this license is made available strictly on an AS IS BASIS WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED.Please review the license for details on these and other terms and conditions.
#pragma once
#include "IUnityInterface.h"
#ifndef UNITY_VULKAN_HEADER
#define UNITY_VULKAN_HEADER <vulkan/vulkan.h>
#endif
#include UNITY_VULKAN_HEADER
struct UnityVulkanInstance
{
VkPipelineCache pipelineCache; // Unity's pipeline cache is serialized to disk
VkInstance instance;
VkPhysicalDevice physicalDevice;
VkDevice device;
VkQueue graphicsQueue;
PFN_vkGetInstanceProcAddr getInstanceProcAddr; // vkGetInstanceProcAddr of the Vulkan loader, same as the one passed to UnityVulkanInitCallback
unsigned int queueFamilyIndex;
void* reserved[8];
};
struct UnityVulkanMemory
{
VkDeviceMemory memory; // Vulkan memory handle
VkDeviceSize offset; // offset within memory
VkDeviceSize size; // size in bytes, may be less than the total size of memory;
void* mapped; // pointer to mapped memory block, NULL if not mappable, offset is already applied, remaining block still has at least the given size.
VkMemoryPropertyFlags flags; // Vulkan memory properties
unsigned int memoryTypeIndex; // index into VkPhysicalDeviceMemoryProperties::memoryTypes
void* reserved[4];
};
enum UnityVulkanResourceAccessMode
{
// Does not imply any pipeline barriers, should only be used to query resource attributes
kUnityVulkanResourceAccess_ObserveOnly,
// Handles layout transition and barriers
kUnityVulkanResourceAccess_PipelineBarrier,
// Recreates the backing resource (VkBuffer/VkImage) but keeps the previous one alive if it's in use
kUnityVulkanResourceAccess_Recreate,
};
struct UnityVulkanImage
{
UnityVulkanMemory memory; // memory that backs the image
VkImage image; // Vulkan image handle
VkImageLayout layout; // current layout, may change resource access
VkImageAspectFlags aspect;
VkImageUsageFlags usage;
VkFormat format;
VkExtent3D extent;
VkImageTiling tiling;
VkImageType type;
VkSampleCountFlagBits samples;
int layers;
int mipCount;
void* reserved[4];
};
struct UnityVulkanBuffer
{
UnityVulkanMemory memory; // memory that backs the buffer
VkBuffer buffer; // Vulkan buffer handle
size_t sizeInBytes; // size of the buffer in bytes, may be less than memory size
VkBufferUsageFlags usage;
void* reserved[4];
};
struct UnityVulkanRecordingState
{
VkCommandBuffer commandBuffer; // Vulkan command buffer that is currently recorded by Unity
VkCommandBufferLevel commandBufferLevel;
VkRenderPass renderPass; // Current render pass, a compatible one or VK_NULL_HANDLE
VkFramebuffer framebuffer; // Current framebuffer or VK_NULL_HANDLE
int subPassIndex; // index of the current sub pass, -1 if not inside a render pass
// Resource life-time tracking counters, only relevant for resources allocated by the plugin
unsigned long long currentFrameNumber; // can be used to track lifetime of own resources
unsigned long long safeFrameNumber; // all resources that were used in this frame (or before) are safe to be released
void* reserved[4];
};
enum UnityVulkanEventRenderPassPreCondition
{
// Don't care about the state on Unity's current command buffer
// This is the default precondition
kUnityVulkanRenderPass_DontCare,
// Make sure that there is currently RenderPass in progress.
// There are no guarantees about the currently bound descriptor sets, vertex buffers, index buffers and pipeline objects
// Unity does however set dynamic pipeline set VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR based on the current settings
// If used in combination with the SRP RenderPass API the resuls is undefined
kUnityVulkanRenderPass_EnsureInside,
// Make sure that there is currently no RenderPass in progress.
// This allows e.g. resource uploads.
// Ends the current render pass (and resumes it afterwards if needed)
// If used in combination with the SRP RenderPass API the resuls is undefined.
kUnityVulkanRenderPass_EnsureOutside
};
enum UnityVulkanGraphicsQueueAccess
{
// No queue acccess, no work must be submitted to UnityVulkanInstance::graphicsQueue from the plugin event callback
kUnityVulkanGraphicsQueueAccess_DontCare,
// Make sure that Unity worker threads don't access the Vulkan graphics queue
// This disables access to the current Unity command buffer
kUnityVulkanGraphicsQueueAccess_Allow,
};
enum UnityVulkanEventConfigFlagBits
{
kUnityVulkanEventConfigFlag_EnsurePreviousFrameSubmission = (1 << 0), // default: set
kUnityVulkanEventConfigFlag_FlushCommandBuffers = (1 << 1), // submit existing command buffers, default: not set
kUnityVulkanEventConfigFlag_SyncWorkerThreads = (1 << 2), // wait for worker threads to finish, default: not set
kUnityVulkanEventConfigFlag_ModifiesCommandBuffersState = (1 << 3), // should be set when descriptor set bindings, vertex buffer bindings, etc are changed (default: set)
};
struct UnityVulkanPluginEventConfig
{
UnityVulkanEventRenderPassPreCondition renderPassPrecondition;
UnityVulkanGraphicsQueueAccess graphicsQueueAccess;
uint32_t flags;
};
// Constant that can be used to reference the whole image
const VkImageSubresource* const UnityVulkanWholeImage = NULL;
// callback function, see InterceptInitialization
typedef PFN_vkGetInstanceProcAddr(UNITY_INTERFACE_API * UnityVulkanInitCallback)(PFN_vkGetInstanceProcAddr getInstanceProcAddr, void* userdata);
enum UnityVulkanSwapchainMode
{
kUnityVulkanSwapchainMode_Default,
kUnityVulkanSwapchainMode_Offscreen
};
struct UnityVulkanSwapchainConfiguration
{
UnityVulkanSwapchainMode mode;
};
enum
{
kUnityVulkanInitCallbackMaxPriority = 0x7FFFFFFF
};
UNITY_DECLARE_INTERFACE(IUnityGraphicsVulkanV2)
{
// Vulkan API hooks
//
// Must be called before kUnityGfxDeviceEventInitialize (preload plugin)
// Unity will call 'func' when initializing the Vulkan API
// The 'getInstanceProcAddr' passed to the callback is the function pointer from the Vulkan Loader
// The function pointer returned from UnityVulkanInitCallback may be a different implementation
// This allows intercepting all Vulkan API calls
// This function is equivalent to calling `AddInterceptInitialization` with a priority of `kUnityVulkanInitCallbackMaxPriority`
//
// Most rules/restrictions for implementing a Vulkan layer apply
// Returns true on success, false on failure (typically because it is used too late)
bool(UNITY_INTERFACE_API * InterceptInitialization)(UnityVulkanInitCallback func, void* userdata);
// Intercept Vulkan API function of the given name with the given function
// In contrast to InterceptInitialization this interface can be used at any time
// The user must handle all synchronization
// Generally this cannot be used to wrap Vulkan object because there might because there may already be non-wrapped instances
// returns the previous function pointer
PFN_vkVoidFunction(UNITY_INTERFACE_API * InterceptVulkanAPI)(const char* name, PFN_vkVoidFunction func);
// Change the precondition for a specific user-defined event
// Should be called during initialization
void(UNITY_INTERFACE_API * ConfigureEvent)(int eventID, const UnityVulkanPluginEventConfig * pluginEventConfig);
// Access the Vulkan instance and render queue created by Unity
// UnityVulkanInstance does not change between kUnityGfxDeviceEventInitialize and kUnityGfxDeviceEventShutdown
UnityVulkanInstance(UNITY_INTERFACE_API * Instance)();
// Access the current command buffer
//
// outCommandRecordingState is invalidated by any resource access calls.
// queueAccess must be kUnityVulkanGraphicsQueueAccess_Allow when called from from a AccessQueue callback or from a event that is configured for queue access.
// Otherwise queueAccess must be kUnityVulkanGraphicsQueueAccess_DontCare.
bool(UNITY_INTERFACE_API * CommandRecordingState)(UnityVulkanRecordingState * outCommandRecordingState, UnityVulkanGraphicsQueueAccess queueAccess);
// Resource access
//
// Using the following resource query APIs will mark the resources as used for the current frame.
// Pipeline barriers will be inserted when needed.
//
// Resource access APIs may record commands, so the current UnityVulkanRecordingState is invalidated
// Must not be called from event callbacks configured for queue access (UnityVulkanGraphicsQueueAccess_Allow)
// or from a AccessQueue callback of an event
bool(UNITY_INTERFACE_API * AccessTexture)(void* nativeTexture, const VkImageSubresource * subResource, VkImageLayout layout,
VkPipelineStageFlags pipelineStageFlags, VkAccessFlags accessFlags, UnityVulkanResourceAccessMode accessMode, UnityVulkanImage * outImage);
bool(UNITY_INTERFACE_API * AccessRenderBufferTexture)(UnityRenderBuffer nativeRenderBuffer, const VkImageSubresource * subResource, VkImageLayout layout,
VkPipelineStageFlags pipelineStageFlags, VkAccessFlags accessFlags, UnityVulkanResourceAccessMode accessMode, UnityVulkanImage * outImage);
bool(UNITY_INTERFACE_API * AccessRenderBufferResolveTexture)(UnityRenderBuffer nativeRenderBuffer, const VkImageSubresource * subResource, VkImageLayout layout,
VkPipelineStageFlags pipelineStageFlags, VkAccessFlags accessFlags, UnityVulkanResourceAccessMode accessMode, UnityVulkanImage * outImage);
bool(UNITY_INTERFACE_API * AccessBuffer)(void* nativeBuffer, VkPipelineStageFlags pipelineStageFlags, VkAccessFlags accessFlags, UnityVulkanResourceAccessMode accessMode, UnityVulkanBuffer * outBuffer);
// Control current state of render pass
//
// Must not be called from event callbacks configured for queue access (UnityVulkanGraphicsQueueAccess_Allow, UnityVulkanGraphicsQueueAccess_FlushAndAllow)
// or from a AccessQueue callback of an event
// See kUnityVulkanRenderPass_EnsureInside, kUnityVulkanRenderPass_EnsureOutside
void(UNITY_INTERFACE_API * EnsureOutsideRenderPass)();
void(UNITY_INTERFACE_API * EnsureInsideRenderPass)();
// Allow command buffer submission to the the Vulkan graphics queue from the given UnityRenderingEventAndData callback.
// This is an alternative to using ConfigureEvent with kUnityVulkanGraphicsQueueAccess_Allow.
//
// eventId and userdata are passed to the callback
// This may or may not be called synchronously or from the submission thread.
// If flush is true then all Unity command buffers of this frame are submitted before UnityQueueAccessCallback
void(UNITY_INTERFACE_API * AccessQueue)(UnityRenderingEventAndData, int eventId, void* userData, bool flush);
// Configure swapchains that are created by Unity.
// Must be called before kUnityGfxDeviceEventInitialize (preload plugin)
bool(UNITY_INTERFACE_API * ConfigureSwapchain)(const UnityVulkanSwapchainConfiguration * swapChainConfig);
// see AccessTexture
// Accepts UnityTextureID (UnityRenderingExtTextureUpdateParamsV2::textureID, UnityRenderingExtCustomBlitParams::source)
bool(UNITY_INTERFACE_API * AccessTextureByID)(UnityTextureID textureID, const VkImageSubresource * subResource, VkImageLayout layout,
VkPipelineStageFlags pipelineStageFlags, VkAccessFlags accessFlags, UnityVulkanResourceAccessMode accessMode, UnityVulkanImage * outImage);
// Vulkan API hooks
//
// Must be called before kUnityGfxDeviceEventInitialize (preload plugin)
// Unity will call 'func' when initializing the Vulkan API
// The 'getInstanceProcAddr' passed to the callback is the function pointer from the Vulkan Loader
// The function pointer returned from UnityVulkanInitCallback may be a different implementation
// This allows intercepting all Vulkan API calls
// The priority is used to sort multiple callbacks such that the highest priority will be called last
// with the original Vulkan loader implementation of vkGetInstanceProcAddress passed in as 'getInstanceProcAddr'.
// A priority value of `kUnityVulkanInitCallbackMaxPriority` is used to force a callback to be called immediately before
// the original Vulkan loader implementation of `vkGetInstanceProcAddress`. Only one callback can be registered with a
// priority of `kUnityVulkanInitCallbackMaxPriority`, if one already exists it will be replaced.
// Passing a priority value of `kUnityVulkanInitCallbackMaxPriority` is equivalent to calling the `InterceptInitialization` method.
//
// Most rules/restrictions for implementing a Vulkan layer apply
// Returns true on success, false on failure (typically because it is used too late)
bool(UNITY_INTERFACE_API * AddInterceptInitialization)(UnityVulkanInitCallback func, void* userdata, int32_t priority);
// Remove vulkan intercept initialization callback.
// Removal will not take effect until the next time vulkan is initialized.
bool(UNITY_INTERFACE_API * RemoveInterceptInitialization)(UnityVulkanInitCallback func);
};
UNITY_REGISTER_INTERFACE_GUID(0x329334C09DCA4787ULL, 0xB347DD92A0097FFCULL, IUnityGraphicsVulkanV2)
UNITY_DECLARE_INTERFACE(IUnityGraphicsVulkan)
{
// Vulkan API hooks
//
// Must be called before kUnityGfxDeviceEventInitialize (preload plugin)
// Unity will call 'func' when initializing the Vulkan API
// The 'getInstanceProcAddr' passed to the callback is the function pointer from the Vulkan Loader
// The function pointer returned from UnityVulkanInitCallback may be a different implementation
// This allows intercepting all Vulkan API calls
//
// Most rules/restrictions for implementing a Vulkan layer apply
// Returns true on success, false on failure (typically because it is used too late)
bool(UNITY_INTERFACE_API * InterceptInitialization)(UnityVulkanInitCallback func, void* userdata);
// Intercept Vulkan API function of the given name with the given function
// In contrast to InterceptInitialization this interface can be used at any time
// The user must handle all synchronization
// Generally this cannot be used to wrap Vulkan object because there might because there may already be non-wrapped instances
// returns the previous function pointer
PFN_vkVoidFunction(UNITY_INTERFACE_API * InterceptVulkanAPI)(const char* name, PFN_vkVoidFunction func);
// Change the precondition for a specific user-defined event
// Should be called during initialization
void(UNITY_INTERFACE_API * ConfigureEvent)(int eventID, const UnityVulkanPluginEventConfig * pluginEventConfig);
// Access the Vulkan instance and render queue created by Unity
// UnityVulkanInstance does not change between kUnityGfxDeviceEventInitialize and kUnityGfxDeviceEventShutdown
UnityVulkanInstance(UNITY_INTERFACE_API * Instance)();
// Access the current command buffer
//
// outCommandRecordingState is invalidated by any resource access calls.
// queueAccess must be kUnityVulkanGraphicsQueueAccess_Allow when called from from a AccessQueue callback or from a event that is configured for queue access.
// Otherwise queueAccess must be kUnityVulkanGraphicsQueueAccess_DontCare.
bool(UNITY_INTERFACE_API * CommandRecordingState)(UnityVulkanRecordingState * outCommandRecordingState, UnityVulkanGraphicsQueueAccess queueAccess);
// Resource access
//
// Using the following resource query APIs will mark the resources as used for the current frame.
// Pipeline barriers will be inserted when needed.
//
// Resource access APIs may record commands, so the current UnityVulkanRecordingState is invalidated
// Must not be called from event callbacks configured for queue access (UnityVulkanGraphicsQueueAccess_Allow)
// or from a AccessQueue callback of an event
bool(UNITY_INTERFACE_API * AccessTexture)(void* nativeTexture, const VkImageSubresource * subResource, VkImageLayout layout,
VkPipelineStageFlags pipelineStageFlags, VkAccessFlags accessFlags, UnityVulkanResourceAccessMode accessMode, UnityVulkanImage * outImage);
bool(UNITY_INTERFACE_API * AccessRenderBufferTexture)(UnityRenderBuffer nativeRenderBuffer, const VkImageSubresource * subResource, VkImageLayout layout,
VkPipelineStageFlags pipelineStageFlags, VkAccessFlags accessFlags, UnityVulkanResourceAccessMode accessMode, UnityVulkanImage * outImage);
bool(UNITY_INTERFACE_API * AccessRenderBufferResolveTexture)(UnityRenderBuffer nativeRenderBuffer, const VkImageSubresource * subResource, VkImageLayout layout,
VkPipelineStageFlags pipelineStageFlags, VkAccessFlags accessFlags, UnityVulkanResourceAccessMode accessMode, UnityVulkanImage * outImage);
bool(UNITY_INTERFACE_API * AccessBuffer)(void* nativeBuffer, VkPipelineStageFlags pipelineStageFlags, VkAccessFlags accessFlags, UnityVulkanResourceAccessMode accessMode, UnityVulkanBuffer * outBuffer);
// Control current state of render pass
//
// Must not be called from event callbacks configured for queue access (UnityVulkanGraphicsQueueAccess_Allow, UnityVulkanGraphicsQueueAccess_FlushAndAllow)
// or from a AccessQueue callback of an event
// See kUnityVulkanRenderPass_EnsureInside, kUnityVulkanRenderPass_EnsureOutside
void(UNITY_INTERFACE_API * EnsureOutsideRenderPass)();
void(UNITY_INTERFACE_API * EnsureInsideRenderPass)();
// Allow command buffer submission to the the Vulkan graphics queue from the given UnityRenderingEventAndData callback.
// This is an alternative to using ConfigureEvent with kUnityVulkanGraphicsQueueAccess_Allow.
//
// eventId and userdata are passed to the callback
// This may or may not be called synchronously or from the submission thread.
// If flush is true then all Unity command buffers of this frame are submitted before UnityQueueAccessCallback
void(UNITY_INTERFACE_API * AccessQueue)(UnityRenderingEventAndData, int eventId, void* userData, bool flush);
// Configure swapchains that are created by Unity.
// Must be called before kUnityGfxDeviceEventInitialize (preload plugin)
bool(UNITY_INTERFACE_API * ConfigureSwapchain)(const UnityVulkanSwapchainConfiguration * swapChainConfig);
// see AccessTexture
// Accepts UnityTextureID (UnityRenderingExtTextureUpdateParamsV2::textureID, UnityRenderingExtCustomBlitParams::source)
bool(UNITY_INTERFACE_API * AccessTextureByID)(UnityTextureID textureID, const VkImageSubresource * subResource, VkImageLayout layout,
VkPipelineStageFlags pipelineStageFlags, VkAccessFlags accessFlags, UnityVulkanResourceAccessMode accessMode, UnityVulkanImage * outImage);
};
UNITY_REGISTER_INTERFACE_GUID(0x95355348d4ef4e11ULL, 0x9789313dfcffcc87ULL, IUnityGraphicsVulkan)

99
ffx-fsr2-api/dx11/ffx_fsr2_dx11.h

@ -0,0 +1,99 @@
// This file is part of the FidelityFX SDK.
//
// Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// @defgroup DX11
#pragma once
#include <d3d11.h>
#include "../ffx_fsr2_interface.h"
#if defined(__cplusplus)
extern "C" {
#endif // #if defined(__cplusplus)
/// Query how much memory is required for the DirectX 11 backend's scratch buffer.
///
/// @returns
/// The size (in bytes) of the required scratch memory buffer for the DX11 backend.
FFX_API size_t ffxFsr2GetScratchMemorySizeDX11();
/// Populate an interface with pointers for the DX11 backend.
///
/// @param [out] fsr2Interface A pointer to a <c><i>FfxFsr2Interface</i></c> structure to populate with pointers.
/// @param [in] device A pointer to the DirectX11 device.
/// @param [in] scratchBuffer A pointer to a buffer of memory which can be used by the DirectX(R)11 backend.
/// @param [in] scratchBufferSize The size (in bytes) of the buffer pointed to by <c><i>scratchBuffer</i></c>.
///
/// @retval
/// FFX_OK The operation completed successfully.
/// @retval
/// FFX_ERROR_CODE_INVALID_POINTER The <c><i>interface</i></c> pointer was <c><i>NULL</i></c>.
///
/// @ingroup FSR2 DX11
FFX_API FfxErrorCode ffxFsr2GetInterfaceDX11(
FfxFsr2Interface* fsr2Interface,
ID3D11Device* device,
void* scratchBuffer,
size_t scratchBufferSize);
/// Create a <c><i>FfxFsr2Device</i></c> from a <c><i>ID3D11Device</i></c>.
///
/// @param [in] device A pointer to the DirectX11 device.
///
/// @returns
/// An abstract FidelityFX device.
///
/// @ingroup FSR2 DX11
FFX_API FfxDevice ffxGetDeviceDX11(ID3D11Device* device);
/// Create a <c><i>FfxResource</i></c> from a <c><i>ID3D11Resource</i></c>.
///
/// @param [in] fsr2Interface A pointer to a <c><i>FfxFsr2Interface</i></c> structure.
/// @param [in] resDx11 A pointer to the DirectX11 resource.
/// @param [in] name (optional) A name string to identify the resource in debug mode.
/// @param [in] state The state the resource is currently in.
///
/// @returns
/// An abstract FidelityFX resources.
///
/// @ingroup FSR2 DX11
FFX_API FfxResource ffxGetResourceDX11(
FfxFsr2Context* context,
ID3D11Resource* resDx11,
const wchar_t* name = nullptr,
FfxResourceStates state = FFX_RESOURCE_STATE_COMPUTE_READ);
/// Retrieve a <c><i>ID3D11Resource</i></c> pointer associated with a RESOURCE_IDENTIFIER.
/// Used for debug purposes when blitting internal surfaces.
///
/// @param [in] context A pointer to a <c><i>FfxFsr2Context</i></c> structure.
/// @param [in] resId A resource.
///
/// @returns
/// A <c><i>ID3D11Resource</i> pointer</c>.
///
/// @ingroup FSR2 DX11
FFX_API ID3D11Resource* ffxGetDX11ResourcePtr(FfxFsr2Context* context, uint32_t resId);
#if defined(__cplusplus)
}
#endif // #if defined(__cplusplus)

3444
ffx-fsr2-api/dx12/d3dx12.h
File diff suppressed because it is too large
View File

114
ffx-fsr2-api/dx12/ffx_fsr2_dx12.h

@ -0,0 +1,114 @@
// This file is part of the FidelityFX SDK.
//
// Copyright (c) 2022-2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// This file contains function declarations to convert DX12 resources
// to and from API independent FFX resources.
// @defgroup DX12
#pragma once
#include <d3d12.h>
#include "../ffx_fsr2_interface.h"
#if defined(__cplusplus)
extern "C" {
#endif // #if defined(__cplusplus)
/// Query how much memory is required for the DirectX 12 backend's scratch buffer.
///
/// @returns
/// The size (in bytes) of the required scratch memory buffer for the DX12 backend.
FFX_API size_t ffxFsr2GetScratchMemorySizeDX12();
/// Populate an interface with pointers for the DX12 backend.
///
/// @param [out] fsr2Interface A pointer to a <c><i>FfxFsr2Interface</i></c> structure to populate with pointers.
/// @param [in] device A pointer to the DirectX12 device.
/// @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 <c><i>scratchBuffer</i></c>.
///
/// @retval
/// FFX_OK The operation completed successfully.
/// @retval
/// FFX_ERROR_CODE_INVALID_POINTER The <c><i>interface</i></c> pointer was <c><i>NULL</i></c>.
///
/// @ingroup FSR2 DX12
FFX_API FfxErrorCode ffxFsr2GetInterfaceDX12(
FfxFsr2Interface* fsr2Interface,
ID3D12Device* device,
void* scratchBuffer,
size_t scratchBufferSize);
/// Create a <c><i>FfxFsr2Device</i></c> from a <c><i>ID3D12Device</i></c>.
///
/// @param [in] device A pointer to the DirectX12 device.
///
/// @returns
/// An abstract FidelityFX device.
///
/// @ingroup FSR2 DX12
FFX_API FfxDevice ffxGetDeviceDX12(ID3D12Device* device);
/// Create a <c><i>FfxCommandList</i></c> from a <c><i>ID3D12CommandList</i></c>.
///
/// @param [in] cmdList A pointer to the DirectX12 command list.
///
/// @returns
/// An abstract FidelityFX command list.
///
/// @ingroup FSR2 DX12
FFX_API FfxCommandList ffxGetCommandListDX12(ID3D12CommandList* cmdList);
/// Create a <c><i>FfxResource</i></c> from a <c><i>ID3D12Resource</i></c>.
///
/// @param [in] fsr2Interface A pointer to a <c><i>FfxFsr2Interface</i></c> structure.
/// @param [in] resDx12 A pointer to the DirectX12 resource.
/// @param [in] name (optional) A name string to identify the resource in debug mode.
/// @param [in] state The state the resource is currently in.
/// @param [in] shaderComponentMapping The shader component mapping.
///
/// @returns
/// An abstract FidelityFX resources.
///
/// @ingroup FSR2 DX12
FFX_API FfxResource ffxGetResourceDX12(
FfxFsr2Context* context,
ID3D12Resource* resDx12,
const wchar_t* name = nullptr,
FfxResourceStates state = FFX_RESOURCE_STATE_COMPUTE_READ,
UINT shaderComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING);
/// Retrieve a <c><i>ID3D12Resource</i></c> pointer associated with a RESOURCE_IDENTIFIER.
/// Used for debug purposes when blitting internal surfaces.
///
/// @param [in] context A pointer to a <c><i>FfxFsr2Context</i></c> structure.
/// @param [in] resId A resourceID.
///
/// @returns
/// A <c><i>ID3D12Resource</i> pointer</c>.
///
/// @ingroup FSR2 DX12
FFX_API ID3D12Resource* ffxGetDX12ResourcePtr(FfxFsr2Context* context, uint32_t resId);
#if defined(__cplusplus)
}
#endif // #if defined(__cplusplus)

132
ffx-fsr2-api/ffx_assert.h

@ -0,0 +1,132 @@
// This file is part of the FidelityFX SDK.
//
// Copyright (c) 2022-2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
#include "ffx_types.h"
#include "ffx_util.h"
#ifdef __cplusplus
extern "C" {
#endif // #ifdef __cplusplus
#ifdef _DEBUG
#ifdef _WIN32
#ifdef DISABLE_FFX_DEBUG_BREAK
#define FFX_DEBUG_BREAK \
{ \
}
#else
/// Macro to force the debugger to break at this point in the code.
#define FFX_DEBUG_BREAK __debugbreak();
#endif
#else
#define FFX_DEBUG_BREAK \
{ \
}
#endif
#else
// don't allow debug break in release builds.
#define FFX_DEBUG_BREAK
#endif
/// A typedef for the callback function for assert printing.
///
/// This can be used to re-route printing of assert messages from the FFX backend
/// to another destination. For example instead of the default behaviour of printing
/// the assert messages to the debugger's TTY the message can be re-routed to a
/// MessageBox in a GUI application.
///
/// @param [in] message The message generated by the assert.
///
typedef void (*FfxAssertCallback)(const char* message);
/// Function to report an assert.
///
/// @param [in] file The name of the file as a string.
/// @param [in] line The index of the line in the file.
/// @param [in] condition The boolean condition that was tested.
/// @param [in] msg The optional message to print.
///
/// @returns
/// Always returns true.
///
FFX_API bool ffxAssertReport(const char* file, int32_t line, const char* condition, const char* msg);
/// Provides the ability to set a callback for assert messages.
///
/// @param [in] callback The callback function that will receive assert messages.
///
FFX_API void ffxAssertSetPrintingCallback(FfxAssertCallback callback);
#ifdef _DEBUG
/// Standard assert macro.
#define FFX_ASSERT(condition) \
do \
{ \
if (!(condition) && ffxAssertReport(__FILE__, __LINE__, #condition, NULL)) \
FFX_DEBUG_BREAK \
} while (0)
/// Assert macro with message.
#define FFX_ASSERT_MESSAGE(condition, msg) \
do \
{ \
if (!(condition) && ffxAssertReport(__FILE__, __LINE__, #condition, msg)) \
FFX_DEBUG_BREAK \
} while (0)
/// Assert macro that always fails.
#define FFX_ASSERT_FAIL(message) \
do \
{ \
ffxAssertReport(__FILE__, __LINE__, NULL, message); \
FFX_DEBUG_BREAK \
} while (0)
#else
// asserts disabled
#define FFX_ASSERT(condition) \
do \
{ \
FFX_UNUSED(condition); \
} while (0)
#define FFX_ASSERT_MESSAGE(condition, message) \
do \
{ \
FFX_UNUSED(condition); \
FFX_UNUSED(message); \
} while (0)
#define FFX_ASSERT_FAIL(message) \
do \
{ \
FFX_UNUSED(message); \
} while (0)
#endif // #if _DEBUG
/// Simple static assert.
#define FFX_STATIC_ASSERT(condition) static_assert(condition, #condition)
#ifdef __cplusplus
}
#endif // #ifdef __cplusplus

59
ffx-fsr2-api/ffx_error.h

@ -0,0 +1,59 @@
// This file is part of the FidelityFX SDK.
//
// Copyright (c) 2022-2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
#include "ffx_types.h"
/// Typedef for error codes returned from functions in the FidelityFX SDK.
typedef int32_t FfxErrorCode;
static const FfxErrorCode FFX_OK = 0; ///< The operation completed successfully.
static const FfxErrorCode FFX_ERROR_INVALID_POINTER = 0x80000000; ///< The operation failed due to an invalid pointer.
static const FfxErrorCode FFX_ERROR_INVALID_ALIGNMENT = 0x80000001; ///< The operation failed due to an invalid alignment.
static const FfxErrorCode FFX_ERROR_INVALID_SIZE = 0x80000002; ///< The operation failed due to an invalid size.
static const FfxErrorCode FFX_EOF = 0x80000003; ///< The end of the file was encountered.
static const FfxErrorCode FFX_ERROR_INVALID_PATH = 0x80000004; ///< The operation failed because the specified path was invalid.
static const FfxErrorCode FFX_ERROR_EOF = 0x80000005; ///< The operation failed because end of file was reached.
static const FfxErrorCode FFX_ERROR_MALFORMED_DATA = 0x80000006; ///< The operation failed because of some malformed data.
static const FfxErrorCode FFX_ERROR_OUT_OF_MEMORY = 0x80000007; ///< The operation failed because it ran out memory.
static const FfxErrorCode FFX_ERROR_INCOMPLETE_INTERFACE = 0x80000008; ///< The operation failed because the interface was not fully configured.
static const FfxErrorCode FFX_ERROR_INVALID_ENUM = 0x80000009; ///< The operation failed because of an invalid enumeration value.
static const FfxErrorCode FFX_ERROR_INVALID_ARGUMENT = 0x8000000a; ///< The operation failed because an argument was invalid.
static const FfxErrorCode FFX_ERROR_OUT_OF_RANGE = 0x8000000b; ///< The operation failed because a value was out of range.
static const FfxErrorCode FFX_ERROR_NULL_DEVICE = 0x8000000c; ///< The operation failed because a device was null.
static const FfxErrorCode FFX_ERROR_BACKEND_API_ERROR = 0x8000000d; ///< The operation failed because the backend API returned an error code.
static const FfxErrorCode FFX_ERROR_INSUFFICIENT_MEMORY = 0x8000000e; ///< The operation failed because there was not enough memory.
/// Helper macro to return error code y from a function when a specific condition, x, is not met.
#define FFX_RETURN_ON_ERROR(x, y) \
if (!(x)) \
{ \
return (y); \
}
/// Helper macro to return error code x from a function when it is not FFX_OK.
#define FFX_VALIDATE(x) \
{ \
FfxErrorCode ret = x; \
FFX_RETURN_ON_ERROR(ret == FFX_OK, ret); \
}

454
ffx-fsr2-api/ffx_fsr2.h

@ -0,0 +1,454 @@
// This file is part of the FidelityFX SDK.
//
// Copyright (c) 2022-2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// @defgroup FSR2
#pragma once
// Include the interface for the backend of the FSR2 API.
#include "ffx_fsr2_interface.h"
/// FidelityFX Super Resolution 2 major version.
///
/// @ingroup FSR2
#define FFX_FSR2_VERSION_MAJOR (2)
/// FidelityFX Super Resolution 2 minor version.
///
/// @ingroup FSR2
#define FFX_FSR2_VERSION_MINOR (2)
/// FidelityFX Super Resolution 2 patch version.
///
/// @ingroup FSR2
#define FFX_FSR2_VERSION_PATCH (1)
/// The size of the context specified in 32bit values.
///
/// @ingroup FSR2
#define FFX_FSR2_CONTEXT_SIZE (16536)
#if defined(__cplusplus)
extern "C" {
#endif // #if defined(__cplusplus)
/// An enumeration of all the quality modes supported by FidelityFX Super
/// Resolution 2 upscaling.
///
/// In order to provide a consistent user experience across multiple
/// applications which implement FSR2. It is strongly recommended that the
/// following preset scaling factors are made available through your
/// application's user interface.
///
/// If your application does not expose the notion of preset scaling factors
/// for upscaling algorithms (perhaps instead implementing a fixed ratio which
/// is immutable) or implementing a more dynamic scaling scheme (such as
/// dynamic resolution scaling), then there is no need to use these presets.
///
/// Please note that <c><i>FFX_FSR2_QUALITY_MODE_ULTRA_PERFORMANCE</i></c> is
/// an optional mode which may introduce significant quality degradation in the
/// final image. As such it is recommended that you evaluate the final results
/// of using this scaling mode before deciding if you should include it in your
/// application.
///
/// @ingroup FSR2
typedef enum FfxFsr2QualityMode {
FFX_FSR2_QUALITY_MODE_QUALITY = 1, ///< Perform upscaling with a per-dimension upscaling ratio of 1.5x.
FFX_FSR2_QUALITY_MODE_BALANCED = 2, ///< Perform upscaling with a per-dimension upscaling ratio of 1.7x.
FFX_FSR2_QUALITY_MODE_PERFORMANCE = 3, ///< Perform upscaling with a per-dimension upscaling ratio of 2.0x.
FFX_FSR2_QUALITY_MODE_ULTRA_PERFORMANCE = 4 ///< Perform upscaling with a per-dimension upscaling ratio of 3.0x.
} FfxFsr2QualityMode;
/// An enumeration of bit flags used when creating a
/// <c><i>FfxFsr2Context</i></c>. See <c><i>FfxFsr2ContextDescription</i></c>.
///
/// @ingroup FSR2
typedef enum FfxFsr2InitializationFlagBits {
FFX_FSR2_ENABLE_HIGH_DYNAMIC_RANGE = (1<<0), ///< A bit indicating if the input color data provided is using a high-dynamic range.
FFX_FSR2_ENABLE_DISPLAY_RESOLUTION_MOTION_VECTORS = (1<<1), ///< A bit indicating if the motion vectors are rendered at display resolution.
FFX_FSR2_ENABLE_MOTION_VECTORS_JITTER_CANCELLATION = (1<<2), ///< A bit indicating that the motion vectors have the jittering pattern applied to them.
FFX_FSR2_ENABLE_DEPTH_INVERTED = (1<<3), ///< A bit indicating that the input depth buffer data provided is inverted [1..0].
FFX_FSR2_ENABLE_DEPTH_INFINITE = (1<<4), ///< A bit indicating that the input depth buffer data provided is using an infinite far plane.
FFX_FSR2_ENABLE_AUTO_EXPOSURE = (1<<5), ///< A bit indicating if automatic exposure should be applied to input color data.
FFX_FSR2_ENABLE_DYNAMIC_RESOLUTION = (1<<6), ///< A bit indicating that the application uses dynamic resolution scaling.
FFX_FSR2_ENABLE_TEXTURE1D_USAGE = (1<<7), ///< A bit indicating that the backend should use 1D textures.
FFX_FSR2_ENABLE_DEBUG_CHECKING = (1<<8), ///< A bit indicating that the runtime should check some API values and report issues.
} FfxFsr2InitializationFlagBits;
/// A structure encapsulating the parameters required to initialize FidelityFX
/// Super Resolution 2 upscaling.
///
/// @ingroup FSR2
typedef struct FfxFsr2ContextDescription {
uint32_t flags; ///< A collection of <c><i>FfxFsr2InitializationFlagBits</i></c>.
FfxDimensions2D maxRenderSize; ///< The maximum size that rendering will be performed at.
FfxDimensions2D displaySize; ///< The size of the presentation resolution targeted by the upscaling process.
FfxFsr2Interface callbacks; ///< A set of pointers to the backend implementation for FSR 2.0.
FfxDevice device; ///< The abstracted device which is passed to some callback functions.
FfxFsr2Message fpMessage; ///< A pointer to a function that can recieve messages from the runtime.
} FfxFsr2ContextDescription;
/// A structure encapsulating the parameters for dispatching the various passes
/// of FidelityFX Super Resolution 2.
///
/// @ingroup FSR2
typedef struct FfxFsr2DispatchDescription {
FfxCommandList commandList; ///< The <c><i>FfxCommandList</i></c> to record FSR2 rendering commands into.
FfxResource color; ///< A <c><i>FfxResource</i></c> containing the color buffer for the current frame (at render resolution).
FfxResource depth; ///< A <c><i>FfxResource</i></c> containing 32bit depth values for the current frame (at render resolution).
FfxResource motionVectors; ///< A <c><i>FfxResource</i></c> containing 2-dimensional motion vectors (at render resolution if <c><i>FFX_FSR2_ENABLE_DISPLAY_RESOLUTION_MOTION_VECTORS</i></c> is not set).
FfxResource exposure; ///< A optional <c><i>FfxResource</i></c> containing a 1x1 exposure value.
FfxResource reactive; ///< A optional <c><i>FfxResource</i></c> containing alpha value of reactive objects in the scene.
FfxResource transparencyAndComposition; ///< A optional <c><i>FfxResource</i></c> containing alpha value of special objects in the scene.
FfxResource output; ///< A <c><i>FfxResource</i></c> containing the output color buffer for the current frame (at presentation resolution).
FfxFloatCoords2D jitterOffset; ///< The subpixel jitter offset applied to the camera.
FfxFloatCoords2D motionVectorScale; ///< The scale factor to apply to motion vectors.
FfxDimensions2D renderSize; ///< The resolution that was used for rendering the input resources.
bool enableSharpening; ///< Enable an additional sharpening pass.
float sharpness; ///< The sharpness value between 0 and 1, where 0 is no additional sharpness and 1 is maximum additional sharpness.
float frameTimeDelta; ///< The time elapsed since the last frame (expressed in milliseconds).
float preExposure; ///< The pre exposure value (must be > 0.0f)
bool reset; ///< A boolean value which when set to true, indicates the camera has moved discontinuously.
float cameraNear; ///< The distance to the near plane of the camera.
float cameraFar; ///< The distance to the far plane of the camera.
float cameraFovAngleVertical; ///< The camera angle field of view in the vertical direction (expressed in radians).
float viewSpaceToMetersFactor; ///< The scale factor to convert view space units to meters
// EXPERIMENTAL reactive mask generation parameters
bool enableAutoReactive; ///< A boolean value to indicate internal reactive autogeneration should be used
FfxResource colorOpaqueOnly; ///< A <c><i>FfxResource</i></c> containing the opaque only color buffer for the current frame (at render resolution).
float autoTcThreshold; ///< Cutoff value for TC
float autoTcScale; ///< A value to scale the transparency and composition mask
float autoReactiveScale; ///< A value to scale the reactive mask
float autoReactiveMax; ///< A value to clamp the reactive mask
} FfxFsr2DispatchDescription;
/// A structure encapsulating the parameters for automatic generation of a reactive mask
///
/// @ingroup FSR2
typedef struct FfxFsr2GenerateReactiveDescription {
FfxCommandList commandList; ///< The <c><i>FfxCommandList</i></c> to record FSR2 rendering commands into.
FfxResource colorOpaqueOnly; ///< A <c><i>FfxResource</i></c> containing the opaque only color buffer for the current frame (at render resolution).
FfxResource colorPreUpscale; ///< A <c><i>FfxResource</i></c> containing the opaque+translucent color buffer for the current frame (at render resolution).
FfxResource outReactive; ///< A <c><i>FfxResource</i></c> containing the surface to generate the reactive mask into.
FfxDimensions2D renderSize; ///< The resolution that was used for rendering the input resources.
float scale; ///< A value to scale the output
float cutoffThreshold; ///< A threshold value to generate a binary reactive mask
float binaryValue; ///< A value to set for the binary reactive mask
uint32_t flags; ///< Flags to determine how to generate the reactive mask
} FfxFsr2GenerateReactiveDescription;
/// A structure encapsulating the FidelityFX Super Resolution 2 context.
///
/// This sets up an object which contains all persistent internal data and
/// resources that are required by FSR2.
///
/// The <c><i>FfxFsr2Context</i></c> object should have a lifetime matching
/// your use of FSR2. Before destroying the FSR2 context care should be taken
/// to ensure the GPU is not accessing the resources created or used by FSR2.
/// It is therefore recommended that the GPU is idle before destroying the
/// FSR2 context.
///
/// @ingroup FSR2
typedef struct FfxFsr2Context {
uint32_t data[FFX_FSR2_CONTEXT_SIZE]; ///< An opaque set of <c>uint32_t</c> which contain the data for the context.
} FfxFsr2Context;
/// Create a FidelityFX Super Resolution 2 context from the parameters
/// programmed to the <c><i>FfxFsr2CreateParams</i></c> structure.
///
/// The context structure is the main object used to interact with the FSR2
/// API, and is responsible for the management of the internal resources used
/// by the FSR2 algorithm. When this API is called, multiple calls will be
/// made via the pointers contained in the <c><i>callbacks</i></c> structure.
/// These callbacks will attempt to retreive the device capabilities, and
/// create the internal resources, and pipelines required by FSR2's
/// frame-to-frame function. Depending on the precise configuration used when
/// creating the <c><i>FfxFsr2Context</i></c> a different set of resources and
/// pipelines might be requested via the callback functions.
///
/// The flags included in the <c><i>flags</i></c> field of
/// <c><i>FfxFsr2Context</i></c> how match the configuration of your
/// application as well as the intended use of FSR2. It is important that these
/// flags are set correctly (as well as a correct programmed
/// <c><i>FfxFsr2DispatchDescription</i></c>) to ensure correct operation. It is
/// recommended to consult the overview documentation for further details on
/// how FSR2 should be integerated into an application.
///
/// When the <c><i>FfxFsr2Context</i></c> is created, you should use the
/// <c><i>ffxFsr2ContextDispatch</i></c> function each frame where FSR2
/// upscaling should be applied. See the documentation of
/// <c><i>ffxFsr2ContextDispatch</i></c> for more details.
///
/// The <c><i>FfxFsr2Context</i></c> should be destroyed when use of it is
/// completed, typically when an application is unloaded or FSR2 upscaling is
/// disabled by a user. To destroy the FSR2 context you should call
/// <c><i>ffxFsr2ContextDestroy</i></c>.
///
/// @param [out] context A pointer to a <c><i>FfxFsr2Context</i></c> structure to populate.
/// @param [in] contextDescription A pointer to a <c><i>FfxFsr2ContextDescription</i></c> structure.
///
/// @retval
/// FFX_OK The operation completed successfully.
/// @retval
/// FFX_ERROR_CODE_NULL_POINTER The operation failed because either <c><i>context</i></c> or <c><i>contextDescription</i></c> was <c><i>NULL</i></c>.
/// @retval
/// FFX_ERROR_INCOMPLETE_INTERFACE The operation failed because the <c><i>FfxFsr2ContextDescription.callbacks</i></c> was not fully specified.
/// @retval
/// FFX_ERROR_BACKEND_API_ERROR The operation failed because of an error returned from the backend.
///
/// @ingroup FSR2
FFX_API FfxErrorCode ffxFsr2ContextCreate(FfxFsr2Context* context, const FfxFsr2ContextDescription* contextDescription);
/// Dispatch the various passes that constitute FidelityFX Super Resolution 2.
///
/// FSR2 is a composite effect, meaning that it is compromised of multiple
/// constituent passes (implemented as one or more clears, copies and compute
/// dispatches). The <c><i>ffxFsr2ContextDispatch</i></c> function is the
/// function which (via the use of the functions contained in the
/// <c><i>callbacks</i></c> field of the <c><i>FfxFsr2Context</i></c>
/// structure) utlimately generates the sequence of graphics API calls required
/// each frame.
///
/// As with the creation of the <c><i>FfxFsr2Context</i></c> correctly
/// programming the <c><i>FfxFsr2DispatchDescription</i></c> is key to ensuring
/// the correct operation of FSR2. It is particularly important to ensure that
/// camera jitter is correctly applied to your application's projection matrix
/// (or camera origin for raytraced applications). FSR2 provides the
/// <c><i>ffxFsr2GetJitterPhaseCount</i></c> and
/// <c><i>ffxFsr2GetJitterOffset</i></c> entry points to help applications
/// correctly compute the camera jitter. Whatever jitter pattern is used by the
/// application it should be correctly programmed to the
/// <c><i>jitterOffset</i></c> field of the <c><i>dispatchDescription</i></c>
/// structure. For more guidance on camera jitter please consult the
/// documentation for <c><i>ffxFsr2GetJitterOffset</i></c> as well as the
/// accompanying overview documentation for FSR2.
///
/// @param [in] context A pointer to a <c><i>FfxFsr2Context</i></c> structure.
/// @param [in] dispatchDescription A pointer to a <c><i>FfxFsr2DispatchDescription</i></c> structure.
///
/// @retval
/// FFX_OK The operation completed successfully.
/// @retval
/// FFX_ERROR_CODE_NULL_POINTER The operation failed because either <c><i>context</i></c> or <c><i>dispatchDescription</i></c> was <c><i>NULL</i></c>.
/// @retval
/// FFX_ERROR_OUT_OF_RANGE The operation failed because <c><i>dispatchDescription.renderSize</i></c> was larger than the maximum render resolution.
/// @retval
/// FFX_ERROR_NULL_DEVICE The operation failed because the device inside the context was <c><i>NULL</i></c>.
/// @retval
/// FFX_ERROR_BACKEND_API_ERROR The operation failed because of an error returned from the backend.
///
/// @ingroup FSR2
FFX_API FfxErrorCode ffxFsr2ContextDispatch(FfxFsr2Context* context, const FfxFsr2DispatchDescription* dispatchDescription);
/// A helper function generate a Reactive mask from an opaque only texure and one containing translucent objects.
///
/// @param [in] context A pointer to a <c><i>FfxFsr2Context</i></c> structure.
/// @param [in] params A pointer to a <c><i>FfxFsr2GenerateReactiveDescription</i></c> structure
///
/// @retval
/// FFX_OK The operation completed successfully.
///
/// @ingroup FSR2
FFX_API FfxErrorCode ffxFsr2ContextGenerateReactiveMask(FfxFsr2Context* context, const FfxFsr2GenerateReactiveDescription* params);
/// Destroy the FidelityFX Super Resolution context.
///
/// @param [out] context A pointer to a <c><i>FfxFsr2Context</i></c> structure to destroy.
///
/// @retval
/// FFX_OK The operation completed successfully.
/// @retval
/// FFX_ERROR_CODE_NULL_POINTER The operation failed because either <c><i>context</i></c> was <c><i>NULL</i></c>.
///
/// @ingroup FSR2
FFX_API FfxErrorCode ffxFsr2ContextDestroy(FfxFsr2Context* context);
/// Get the upscale ratio from the quality mode.
///
/// The following table enumerates the mapping of the quality modes to
/// per-dimension scaling ratios.
///
/// Quality preset | Scale factor
/// ----------------------------------------------------- | -------------
/// <c><i>FFX_FSR2_QUALITY_MODE_QUALITY</i></c> | 1.5x
/// <c><i>FFX_FSR2_QUALITY_MODE_BALANCED</i></c> | 1.7x
/// <c><i>FFX_FSR2_QUALITY_MODE_PERFORMANCE</i></c> | 2.0x
/// <c><i>FFX_FSR2_QUALITY_MODE_ULTRA_PERFORMANCE</i></c> | 3.0x
///
/// Passing an invalid <c><i>qualityMode</i></c> will return 0.0f.
///
/// @param [in] qualityMode The quality mode preset.
///
/// @returns
/// The upscaling the per-dimension upscaling ratio for
/// <c><i>qualityMode</i></c> according to the table above.
///
/// @ingroup FSR2
FFX_API float ffxFsr2GetUpscaleRatioFromQualityMode(FfxFsr2QualityMode qualityMode);
/// A helper function to calculate the rendering resolution from a target
/// resolution and desired quality level.
///
/// This function applies the scaling factor returned by
/// <c><i>ffxFsr2GetUpscaleRatioFromQualityMode</i></c> to each dimension.
///
/// @param [out] renderWidth A pointer to a <c>uint32_t</c> which will hold the calculated render resolution width.
/// @param [out] renderHeight A pointer to a <c>uint32_t</c> which will hold the calculated render resolution height.
/// @param [in] displayWidth The target display resolution width.
/// @param [in] displayHeight The target display resolution height.
/// @param [in] qualityMode The desired quality mode for FSR 2 upscaling.
///
/// @retval
/// FFX_OK The operation completed successfully.
/// @retval
/// FFX_ERROR_INVALID_POINTER Either <c><i>renderWidth</i></c> or <c><i>renderHeight</i></c> was <c>NULL</c>.
/// @retval
/// FFX_ERROR_INVALID_ENUM An invalid quality mode was specified.
///
/// @ingroup FSR2
FFX_API FfxErrorCode ffxFsr2GetRenderResolutionFromQualityMode(
uint32_t* renderWidth,
uint32_t* renderHeight,
uint32_t displayWidth,
uint32_t displayHeight,
FfxFsr2QualityMode qualityMode);
/// A helper function to calculate the jitter phase count from display
/// resolution.
///
/// For more detailed information about the application of camera jitter to
/// your application's rendering please refer to the
/// <c><i>ffxFsr2GetJitterOffset</i></c> function.
///
/// The table below shows the jitter phase count which this function
/// would return for each of the quality presets.
///
/// Quality preset | Scale factor | Phase count
/// ----------------------------------------------------- | ------------- | ---------------
/// <c><i>FFX_FSR2_QUALITY_MODE_QUALITY</i></c> | 1.5x | 18
/// <c><i>FFX_FSR2_QUALITY_MODE_BALANCED</i></c> | 1.7x | 23
/// <c><i>FFX_FSR2_QUALITY_MODE_PERFORMANCE</i></c> | 2.0x | 32
/// <c><i>FFX_FSR2_QUALITY_MODE_ULTRA_PERFORMANCE</i></c> | 3.0x | 72
/// Custom | [1..n]x | ceil(8*n^2)
///
/// @param [in] renderWidth The render resolution width.
/// @param [in] displayWidth The display resolution width.
///
/// @returns
/// The jitter phase count for the scaling factor between <c><i>renderWidth</i></c> and <c><i>displayWidth</i></c>.
///
/// @ingroup FSR2
FFX_API int32_t ffxFsr2GetJitterPhaseCount(int32_t renderWidth, int32_t displayWidth);
/// A helper function to calculate the subpixel jitter offset.
///
/// FSR2 relies on the application to apply sub-pixel jittering while rendering.
/// This is typically included in the projection matrix of the camera. To make
/// the application of camera jitter simple, the FSR2 API provides a small set
/// of utility function which computes the sub-pixel jitter offset for a
/// particular frame within a sequence of separate jitter offsets. To begin, the
/// index within the jitter phase must be computed. To calculate the
/// sequence's length, you can call the <c><i>ffxFsr2GetJitterPhaseCount</i></c>
/// function. The index should be a value which is incremented each frame modulo
/// the length of the sequence computed by <c><i>ffxFsr2GetJitterPhaseCount</i></c>.
/// The index within the jitter phase is passed to
/// <c><i>ffxFsr2GetJitterOffset</i></c> via the <c><i>index</i></c> parameter.
///
/// This function uses a Halton(2,3) sequence to compute the jitter offset.
/// The ultimate index used for the sequence is <c><i>index</i></c> %
/// <c><i>phaseCount</i></c>.
///
/// It is important to understand that the values returned from the
/// <c><i>ffxFsr2GetJitterOffset</i></c> function are in unit pixel space, and
/// in order to composite this correctly into a projection matrix we must
/// convert them into projection offsets. This is done as per the pseudo code
/// listing which is shown below.
///
/// const int32_t jitterPhaseCount = ffxFsr2GetJitterPhaseCount(renderWidth, displayWidth);
///
/// float jitterX = 0;
/// float jitterY = 0;
/// ffxFsr2GetJitterOffset(&jitterX, &jitterY, index, jitterPhaseCount);
///
/// const float jitterX = 2.0f * jitterX / (float)renderWidth;
/// const float jitterY = -2.0f * jitterY / (float)renderHeight;
/// const Matrix4 jitterTranslationMatrix = translateMatrix(Matrix3::identity, Vector3(jitterX, jitterY, 0));
/// const Matrix4 jitteredProjectionMatrix = jitterTranslationMatrix * projectionMatrix;
///
/// Jitter should be applied to all rendering. This includes opaque, alpha
/// transparent, and raytraced objects. For rasterized objects, the sub-pixel
/// jittering values calculated by the <c><i>iffxFsr2GetJitterOffset</i></c>
/// function can be applied to the camera projection matrix which is ultimately
/// used to perform transformations during vertex shading. For raytraced
/// rendering, the sub-pixel jitter should be applied to the ray's origin,
/// often the camera's position.
///
/// Whether you elect to use the <c><i>ffxFsr2GetJitterOffset</i></c> function
/// or your own sequence generator, you must program the
/// <c><i>jitterOffset</i></c> field of the
/// <c><i>FfxFsr2DispatchParameters</i></c> structure in order to inform FSR2
/// of the jitter offset that has been applied in order to render each frame.
///
/// If not using the recommended <c><i>ffxFsr2GetJitterOffset</i></c> function,
/// care should be taken that your jitter sequence never generates a null vector;
/// that is value of 0 in both the X and Y dimensions.
///
/// @param [out] outX A pointer to a <c>float</c> which will contain the subpixel jitter offset for the x dimension.
/// @param [out] outY A pointer to a <c>float</c> which will contain the subpixel jitter offset for the y dimension.
/// @param [in] index The index within the jitter sequence.
/// @param [in] phaseCount The length of jitter phase. See <c><i>ffxFsr2GetJitterPhaseCount</i></c>.
///
/// @retval
/// FFX_OK The operation completed successfully.
/// @retval
/// FFX_ERROR_INVALID_POINTER Either <c><i>outX</i></c> or <c><i>outY</i></c> was <c>NULL</c>.
/// @retval
/// FFX_ERROR_INVALID_ARGUMENT Argument <c><i>phaseCount</i></c> must be greater than 0.
///
/// @ingroup FSR2
FFX_API FfxErrorCode ffxFsr2GetJitterOffset(float* outX, float* outY, int32_t index, int32_t phaseCount);
/// A helper function to check if a resource is
/// <c><i>FFX_FSR2_RESOURCE_IDENTIFIER_NULL</i></c>.
///
/// @param [in] resource A <c><i>FfxResource</i></c>.
///
/// @returns
/// true The <c><i>resource</i></c> was not <c><i>FFX_FSR2_RESOURCE_IDENTIFIER_NULL</i></c>.
/// @returns
/// false The <c><i>resource</i></c> was <c><i>FFX_FSR2_RESOURCE_IDENTIFIER_NULL</i></c>.
///
/// @ingroup FSR2
FFX_API bool ffxFsr2ResourceIsNull(FfxResource resource);
#if defined(__cplusplus)
}
#endif // #if defined(__cplusplus)

395
ffx-fsr2-api/ffx_fsr2_interface.h

@ -0,0 +1,395 @@
// This file is part of the FidelityFX SDK.
//
// Copyright (c) 2022-2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
#include "ffx_assert.h"
#include "ffx_types.h"
#include "ffx_error.h"
// Include the FSR2 resources defined in the HLSL code. This shared here to avoid getting out of sync.
#define FFX_CPU
#include "shaders/ffx_fsr2_resources.h"
#include "shaders/ffx_fsr2_common.h"
#if defined(__cplusplus)
extern "C" {
#endif // #if defined(__cplusplus)
FFX_FORWARD_DECLARE(FfxFsr2Interface);
/// An enumeration of all the passes which constitute the FSR2 algorithm.
///
/// FSR2 is implemented as a composite of several compute passes each
/// computing a key part of the final result. Each call to the
/// <c><i>FfxFsr2ScheduleGpuJobFunc</i></c> callback function will
/// correspond to a single pass included in <c><i>FfxFsr2Pass</i></c>. For a
/// more comprehensive description of each pass, please refer to the FSR2
/// reference documentation.
///
/// Please note in some cases e.g.: <c><i>FFX_FSR2_PASS_ACCUMULATE</i></c>
/// and <c><i>FFX_FSR2_PASS_ACCUMULATE_SHARPEN</i></c> either one pass or the
/// other will be used (they are mutually exclusive). The choice of which will
/// depend on the way the <c><i>FfxFsr2Context</i></c> is created and the
/// precise contents of <c><i>FfxFsr2DispatchParamters</i></c> each time a call
/// is made to <c><i>ffxFsr2ContextDispatch</i></c>.
///
/// @ingroup FSR2
typedef enum FfxFsr2Pass {
FFX_FSR2_PASS_DEPTH_CLIP = 0, ///< A pass which performs depth clipping.
FFX_FSR2_PASS_RECONSTRUCT_PREVIOUS_DEPTH = 1, ///< A pass which performs reconstruction of previous frame's depth.
FFX_FSR2_PASS_LOCK = 2, ///< A pass which calculates pixel locks.
FFX_FSR2_PASS_ACCUMULATE = 3, ///< A pass which performs upscaling.
FFX_FSR2_PASS_ACCUMULATE_SHARPEN = 4, ///< A pass which performs upscaling when sharpening is used.
FFX_FSR2_PASS_RCAS = 5, ///< A pass which performs sharpening.
FFX_FSR2_PASS_COMPUTE_LUMINANCE_PYRAMID = 6, ///< A pass which generates the luminance mipmap chain for the current frame.
FFX_FSR2_PASS_GENERATE_REACTIVE = 7, ///< An optional pass to generate a reactive mask
FFX_FSR2_PASS_TCR_AUTOGENERATE = 8, ///< An optional pass to generate a texture-and-composition and reactive masks
FFX_FSR2_PASS_COUNT ///< The number of passes performed by FSR2.
} FfxFsr2Pass;
typedef enum FfxFsr2MsgType {
FFX_FSR2_MESSAGE_TYPE_ERROR = 0,
FFX_FSR2_MESSAGE_TYPE_WARNING = 1,
FFX_FSR2_MESSAGE_TYPE_COUNT
} FfxFsr2MsgType;
/// Create and initialize the backend context.
///
/// The callback function sets up the backend context for rendering.
/// It will create or reference the device and create required internal data structures.
///
/// @param [in] backendInterface A pointer to the backend interface.
/// @param [in] device The FfxDevice obtained by ffxGetDevice(DX12/VK/...).
///
/// @retval
/// FFX_OK The operation completed successfully.
/// @retval
/// Anything else The operation failed.
///
/// @ingroup FSR2
typedef FfxErrorCode (*FfxFsr2CreateBackendContextFunc)(
FfxFsr2Interface* backendInterface,
FfxDevice device);
/// Get a list of capabilities of the device.
///
/// When creating an <c><i>FfxFsr2Context</i></c> it is desirable for the FSR2
/// core implementation to be aware of certain characteristics of the platform
/// that is being targetted. This is because some optimizations which FSR2
/// attempts to perform are more effective on certain classes of hardware than
/// others, or are not supported by older hardware. In order to avoid cases
/// where optimizations actually have the effect of decreasing performance, or
/// reduce the breadth of support provided by FSR2, FSR2 queries the
/// capabilities of the device to make such decisions.
///
/// For target platforms with fixed hardware support you need not implement
/// this callback function by querying the device, but instead may hardcore
/// what features are available on the platform.
///
/// @param [in] backendInterface A pointer to the backend interface.
/// @param [out] outDeviceCapabilities The device capabilities structure to fill out.
/// @param [in] device The device to query for capabilities.
///
/// @retval
/// FFX_OK The operation completed successfully.
/// @retval
/// Anything else The operation failed.
///
/// @ingroup FSR2
typedef FfxErrorCode(*FfxFsr2GetDeviceCapabilitiesFunc)(
FfxFsr2Interface* backendInterface,
FfxDeviceCapabilities* outDeviceCapabilities,
FfxDevice device);
/// Destroy the backend context and dereference the device.
///
/// This function is called when the <c><i>FfxFsr2Context</i></c> is destroyed.
///
/// @param [in] backendInterface A pointer to the backend interface.
///
/// @retval
/// FFX_OK The operation completed successfully.
/// @retval
/// Anything else The operation failed.
///
/// @ingroup FSR2
typedef FfxErrorCode(*FfxFsr2DestroyBackendContextFunc)(
FfxFsr2Interface* backendInterface);
/// Create a resource.
///
/// This callback is intended for the backend to create internal resources.
///
/// Please note: It is also possible that the creation of resources might
/// itself cause additional resources to be created by simply calling the
/// <c><i>FfxFsr2CreateResourceFunc</i></c> function pointer again. This is
/// useful when handling the initial creation of resources which must be
/// initialized. The flow in such a case would be an initial call to create the
/// CPU-side resource, another to create the GPU-side resource, and then a call
/// to schedule a copy render job to move the data between the two. Typically
/// this type of function call flow is only seen during the creation of an
/// <c><i>FfxFsr2Context</i></c>.
///
/// @param [in] backendInterface A pointer to the backend interface.
/// @param [in] createResourceDescription A pointer to a <c><i>FfxCreateResourceDescription</i></c>.
/// @param [out] outResource A pointer to a <c><i>FfxResource</i></c> object.
///
/// @retval
/// FFX_OK The operation completed successfully.
/// @retval
/// Anything else The operation failed.
///
/// @ingroup FSR2
typedef FfxErrorCode (*FfxFsr2CreateResourceFunc)(
FfxFsr2Interface* backendInterface,
const FfxCreateResourceDescription* createResourceDescription,
FfxResourceInternal* outResource);
/// Register a resource in the backend for the current frame.
///
/// Since FSR2 and the backend are not aware how many different
/// resources will get passed to FSR2 over time, it's not safe
/// to register all resources simultaneously in the backend.
/// Also passed resources may not be valid after the dispatch call.
/// As a result it's safest to register them as FfxResourceInternal
/// and clear them at the end of the dispatch call.
///
/// @param [in] backendInterface A pointer to the backend interface.
/// @param [in] inResource A pointer to a <c><i>FfxResource</i></c>.
/// @param [out] outResource A pointer to a <c><i>FfxResourceInternal</i></c> object.
///
/// @retval
/// FFX_OK The operation completed successfully.
/// @retval
/// Anything else The operation failed.
///
/// @ingroup FSR2
typedef FfxErrorCode(*FfxFsr2RegisterResourceFunc)(
FfxFsr2Interface* backendInterface,
const FfxResource* inResource,
FfxResourceInternal* outResource);
/// Unregister all temporary FfxResourceInternal from the backend.
///
/// Unregister FfxResourceInternal referencing resources passed to
/// a function as a parameter.
///
/// @param [in] backendInterface A pointer to the backend interface.
///
/// @retval
/// FFX_OK The operation completed successfully.
/// @retval
/// Anything else The operation failed.
///
/// @ingroup FSR2
typedef FfxErrorCode(*FfxFsr2UnregisterResourcesFunc)(
FfxFsr2Interface* backendInterface);
/// Retrieve a <c><i>FfxResourceDescription</i></c> matching a
/// <c><i>FfxResource</i></c> structure.
///
/// @param [in] backendInterface A pointer to the backend interface.
/// @param [in] resource A pointer to a <c><i>FfxResource</i></c> object.
///
/// @returns
/// A description of the resource.
///
/// @ingroup FSR2
typedef FfxResourceDescription (*FfxFsr2GetResourceDescriptionFunc)(
FfxFsr2Interface* backendInterface,
FfxResourceInternal resource);
/// Destroy a resource
///
/// This callback is intended for the backend to release an internal resource.
///
/// @param [in] backendInterface A pointer to the backend interface.
/// @param [in] resource A pointer to a <c><i>FfxResource</i></c> object.
///
/// @retval
/// FFX_OK The operation completed successfully.
/// @retval
/// Anything else The operation failed.
///
/// @ingroup FSR2
typedef FfxErrorCode (*FfxFsr2DestroyResourceFunc)(
FfxFsr2Interface* backendInterface,
FfxResourceInternal resource);
/// Create a render pipeline.
///
/// A rendering pipeline contains the shader as well as resource bindpoints
/// and samplers.
///
/// @param [in] backendInterface A pointer to the backend interface.
/// @param [in] pass The identifier for the pass.
/// @param [in] pipelineDescription A pointer to a <c><i>FfxPipelineDescription</i></c> describing the pipeline to be created.
/// @param [out] outPipeline A pointer to a <c><i>FfxPipelineState</i></c> structure which should be populated.
///
/// @retval
/// FFX_OK The operation completed successfully.
/// @retval
/// Anything else The operation failed.
///
/// @ingroup FSR2
typedef FfxErrorCode (*FfxFsr2CreatePipelineFunc)(
FfxFsr2Interface* backendInterface,
FfxFsr2Pass pass,
const FfxPipelineDescription* pipelineDescription,
FfxPipelineState* outPipeline);
/// Destroy a render pipeline.
///
/// @param [in] backendInterface A pointer to the backend interface.
/// @param [out] pipeline A pointer to a <c><i>FfxPipelineState</i></c> structure which should be released.
///
/// @retval
/// FFX_OK The operation completed successfully.
/// @retval
/// Anything else The operation failed.
///
/// @ingroup FSR2
typedef FfxErrorCode (*FfxFsr2DestroyPipelineFunc)(
FfxFsr2Interface* backendInterface,
FfxPipelineState* pipeline);
/// Schedule a render job to be executed on the next call of
/// <c><i>FfxFsr2ExecuteGpuJobsFunc</i></c>.
///
/// Render jobs can perform one of three different tasks: clear, copy or
/// compute dispatches.
///
/// @param [in] backendInterface A pointer to the backend interface.
/// @param [in] job A pointer to a <c><i>FfxGpuJobDescription</i></c> structure.
///
/// @retval
/// FFX_OK The operation completed successfully.
/// @retval
/// Anything else The operation failed.
///
/// @ingroup FSR2
typedef FfxErrorCode (*FfxFsr2ScheduleGpuJobFunc)(
FfxFsr2Interface* backendInterface,
const FfxGpuJobDescription* job);
/// Execute scheduled render jobs on the <c><i>comandList</i></c> provided.
///
/// The recording of the graphics API commands should take place in this
/// callback function, the render jobs which were previously enqueued (via
/// callbacks made to <c><i>FfxFsr2ScheduleGpuJobFunc</i></c>) should be
/// processed in the order they were received. Advanced users might choose to
/// reorder the rendering jobs, but should do so with care to respect the
/// resource dependencies.
///
/// Depending on the precise contents of <c><i>FfxFsr2DispatchDescription</i></c> a
/// different number of render jobs might have previously been enqueued (for
/// example if sharpening is toggled on and off).
///
/// @param [in] backendInterface A pointer to the backend interface.
/// @param [in] commandList A pointer to a <c><i>FfxCommandList</i></c> structure.
///
/// @retval
/// FFX_OK The operation completed successfully.
/// @retval
/// Anything else The operation failed.
///
/// @ingroup FSR2
typedef FfxErrorCode (*FfxFsr2ExecuteGpuJobsFunc)(
FfxFsr2Interface* backendInterface,
FfxCommandList commandList);
/// Pass a string message
///
/// Used for debug messages.
///
/// @param [in] type The type of message.
/// @param [in] message A string message to pass.
///
///
/// @ingroup FSR2
typedef void(*FfxFsr2Message)(
FfxFsr2MsgType type,
const wchar_t* message);
/// A structure encapsulating the interface between the core implentation of
/// the FSR2 algorithm and any graphics API that it should ultimately call.
///
/// This set of functions serves as an abstraction layer between FSR2 and the
/// API used to implement it. While FSR2 ships with backends for DirectX12 and
/// Vulkan, it is possible to implement your own backend for other platforms or
/// which sits ontop of your engine's own abstraction layer. For details on the
/// expectations of what each function should do you should refer the
/// description of the following function pointer types:
///
/// <c><i>FfxFsr2CreateDeviceFunc</i></c>
/// <c><i>FfxFsr2GetDeviceCapabilitiesFunc</i></c>
/// <c><i>FfxFsr2DestroyDeviceFunc</i></c>
/// <c><i>FfxFsr2CreateResourceFunc</i></c>
/// <c><i>FfxFsr2GetResourceDescriptionFunc</i></c>
/// <c><i>FfxFsr2DestroyResourceFunc</i></c>
/// <c><i>FfxFsr2CreatePipelineFunc</i></c>
/// <c><i>FfxFsr2DestroyPipelineFunc</i></c>
/// <c><i>FfxFsr2ScheduleGpuJobFunc</i></c>
/// <c><i>FfxFsr2ExecuteGpuJobsFunc</i></c>
///
/// Depending on the graphics API that is abstracted by the backend, it may be
/// required that the backend is to some extent stateful. To ensure that
/// applications retain full control to manage the memory used by FSR2, the
/// <c><i>scratchBuffer</i></c> and <c><i>scratchBufferSize</i></c> fields are
/// provided. A backend should provide a means of specifying how much scratch
/// memory is required for its internal implementation (e.g: via a function
/// or constant value). The application is that responsible for allocating that
/// memory and providing it when setting up the FSR2 backend. Backends provided
/// with FSR2 do not perform dynamic memory allocations, and instead
/// suballocate all memory from the scratch buffers provided.
///
/// The <c><i>scratchBuffer</i></c> and <c><i>scratchBufferSize</i></c> fields
/// should be populated according to the requirements of each backend. For
/// example, if using the DirectX 12 backend you should call the
/// <c><i>ffxFsr2GetScratchMemorySizeDX12</i></c> function. It is not required
/// that custom backend implementations use a scratch buffer.
///
/// @ingroup FSR2
typedef struct FfxFsr2Interface {
FfxFsr2CreateBackendContextFunc fpCreateBackendContext; ///< A callback function to create and initialize the backend context.
FfxFsr2GetDeviceCapabilitiesFunc fpGetDeviceCapabilities; ///< A callback function to query device capabilites.
FfxFsr2DestroyBackendContextFunc fpDestroyBackendContext; ///< A callback function to destroy the backendcontext. This also dereferences the device.
FfxFsr2CreateResourceFunc fpCreateResource; ///< A callback function to create a resource.
FfxFsr2RegisterResourceFunc fpRegisterResource; ///< A callback function to register an external resource.
FfxFsr2UnregisterResourcesFunc fpUnregisterResources; ///< A callback function to unregister external resource.
FfxFsr2GetResourceDescriptionFunc fpGetResourceDescription; ///< A callback function to retrieve a resource description.
FfxFsr2DestroyResourceFunc fpDestroyResource; ///< A callback function to destroy a resource.
FfxFsr2CreatePipelineFunc fpCreatePipeline; ///< A callback function to create a render or compute pipeline.
FfxFsr2DestroyPipelineFunc fpDestroyPipeline; ///< A callback function to destroy a render or compute pipeline.
FfxFsr2ScheduleGpuJobFunc fpScheduleGpuJob; ///< A callback function to schedule a render job.
FfxFsr2ExecuteGpuJobsFunc fpExecuteGpuJobs; ///< A callback function to execute all queued render jobs.
void* scratchBuffer; ///< A preallocated buffer for memory utilized internally by the backend.
size_t scratchBufferSize; ///< Size of the buffer pointed to by <c><i>scratchBuffer</i></c>.
} FfxFsr2Interface;
#if defined(__cplusplus)
}
#endif // #if defined(__cplusplus)

46
ffx-fsr2-api/ffx_fsr2_maximum_bias.h

@ -0,0 +1,46 @@
// This file is part of the FidelityFX SDK.
//
// Copyright (c) 2022-2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// @internal
#pragma once
static const int FFX_FSR2_MAXIMUM_BIAS_TEXTURE_WIDTH = 16;
static const int FFX_FSR2_MAXIMUM_BIAS_TEXTURE_HEIGHT = 16;
static const float ffxFsr2MaximumBias[] = {
2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 1.876f, 1.809f, 1.772f, 1.753f, 1.748f,
2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 1.869f, 1.801f, 1.764f, 1.745f, 1.739f,
2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 1.976f, 1.841f, 1.774f, 1.737f, 1.716f, 1.71f,
2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 1.914f, 1.784f, 1.716f, 1.673f, 1.649f, 1.641f,
2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 1.793f, 1.676f, 1.604f, 1.562f, 1.54f, 1.533f,
2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 1.802f, 1.619f, 1.536f, 1.492f, 1.467f, 1.454f, 1.449f,
2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 1.812f, 1.575f, 1.496f, 1.456f, 1.432f, 1.416f, 1.408f, 1.405f,
2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 1.555f, 1.479f, 1.438f, 1.413f, 1.398f, 1.387f, 1.381f, 1.379f,
2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 1.812f, 1.555f, 1.474f, 1.43f, 1.404f, 1.387f, 1.376f, 1.368f, 1.363f, 1.362f,
2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 1.802f, 1.575f, 1.479f, 1.43f, 1.401f, 1.382f, 1.369f, 1.36f, 1.354f, 1.351f, 1.35f,
2.0f, 2.0f, 1.976f, 1.914f, 1.793f, 1.619f, 1.496f, 1.438f, 1.404f, 1.382f, 1.367f, 1.357f, 1.349f, 1.344f, 1.341f, 1.34f,
1.876f, 1.869f, 1.841f, 1.784f, 1.676f, 1.536f, 1.456f, 1.413f, 1.387f, 1.369f, 1.357f, 1.347f, 1.341f, 1.336f, 1.333f, 1.332f,
1.809f, 1.801f, 1.774f, 1.716f, 1.604f, 1.492f, 1.432f, 1.398f, 1.376f, 1.36f, 1.349f, 1.341f, 1.335f, 1.33f, 1.328f, 1.327f,
1.772f, 1.764f, 1.737f, 1.673f, 1.562f, 1.467f, 1.416f, 1.387f, 1.368f, 1.354f, 1.344f, 1.336f, 1.33f, 1.326f, 1.323f, 1.323f,
1.753f, 1.745f, 1.716f, 1.649f, 1.54f, 1.454f, 1.408f, 1.381f, 1.363f, 1.351f, 1.341f, 1.333f, 1.328f, 1.323f, 1.321f, 1.32f,
1.748f, 1.739f, 1.71f, 1.641f, 1.533f, 1.449f, 1.405f, 1.379f, 1.362f, 1.35f, 1.34f, 1.332f, 1.327f, 1.323f, 1.32f, 1.319f,
};

81
ffx-fsr2-api/ffx_fsr2_private.h

@ -0,0 +1,81 @@
// This file is part of the FidelityFX SDK.
//
// Copyright (c) 2022-2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
// Constants for FSR2 DX12 dispatches. Must be kept in sync with cbFSR2 in ffx_fsr2_callbacks_hlsl.h
typedef struct Fsr2Constants {
int32_t renderSize[2];
int32_t maxRenderSize[2];
int32_t displaySize[2];
int32_t inputColorResourceDimensions[2];
int32_t lumaMipDimensions[2];
int32_t lumaMipLevelToUse;
int32_t frameIndex;
float deviceToViewDepth[4];
float jitterOffset[2];
float motionVectorScale[2];
float downscaleFactor[2];
float motionVectorJitterCancellation[2];
float preExposure;
float previousFramePreExposure;
float tanHalfFOV;
float jitterPhaseCount;
float deltaTime;
float dynamicResChangeFactor;
float viewSpaceToMetersFactor;
} Fsr2Constants;
struct FfxFsr2ContextDescription;
struct FfxDeviceCapabilities;
struct FfxPipelineState;
struct FfxResource;
// FfxFsr2Context_Private
// The private implementation of the FSR2 context.
typedef struct FfxFsr2Context_Private {
FfxFsr2ContextDescription contextDescription;
Fsr2Constants constants;
FfxDevice device;
FfxDeviceCapabilities deviceCapabilities;
FfxPipelineState pipelineDepthClip;
FfxPipelineState pipelineReconstructPreviousDepth;
FfxPipelineState pipelineLock;
FfxPipelineState pipelineAccumulate;
FfxPipelineState pipelineAccumulateSharpen;
FfxPipelineState pipelineRCAS;
FfxPipelineState pipelineComputeLuminancePyramid;
FfxPipelineState pipelineGenerateReactive;
FfxPipelineState pipelineTcrAutogenerate;
// 2 arrays of resources, as e.g. FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_STATUS will use different resources when bound as SRV vs when bound as UAV
FfxResourceInternal srvResources[FFX_FSR2_RESOURCE_IDENTIFIER_COUNT];
FfxResourceInternal uavResources[FFX_FSR2_RESOURCE_IDENTIFIER_COUNT];
bool firstExecution;
bool refreshPipelineStates;
uint32_t resourceFrameIndex;
float previousJitterOffset[2];
int32_t jitterPhaseCountRemaining;
} FfxFsr2Context_Private;

364
ffx-fsr2-api/ffx_types.h

@ -0,0 +1,364 @@
// This file is part of the FidelityFX SDK.
//
// Copyright (c) 2022-2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
#include <stdint.h>
#if defined (FFX_GCC)
/// FidelityFX exported functions
#define FFX_API
#else
/// FidelityFX exported functions
#define FFX_API __declspec(dllexport)
#endif // #if defined (FFX_GCC)
/// Maximum supported number of simultaneously bound SRVs.
#define FFX_MAX_NUM_SRVS 16
/// Maximum supported number of simultaneously bound UAVs.
#define FFX_MAX_NUM_UAVS 8
/// Maximum number of constant buffers bound.
#define FFX_MAX_NUM_CONST_BUFFERS 2
/// Maximum size of bound constant buffers.
#define FFX_MAX_CONST_SIZE 64
/// Off by default warnings
#if defined(_MSC_VER)
#pragma warning(disable : 4365 4710 4820 5039)
#elif defined(__clang__)
#pragma clang diagnostic ignored "-Wunused-parameter"
#pragma clang diagnostic ignored "-Wmissing-field-initializers"
#pragma clang diagnostic ignored "-Wsign-compare"
#pragma clang diagnostic ignored "-Wunused-function"
#pragma clang diagnostic ignored "-Wignored-qualifiers"
#elif defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wunused-function"
#endif
#ifdef __cplusplus
extern "C" {
#endif // #ifdef __cplusplus
/// An enumeration of surface formats.
typedef enum FfxSurfaceFormat {
FFX_SURFACE_FORMAT_UNKNOWN, ///< Unknown format
FFX_SURFACE_FORMAT_R32G32B32A32_TYPELESS, ///< 32 bit per channel, 4 channel typeless format
FFX_SURFACE_FORMAT_R32G32B32A32_FLOAT, ///< 32 bit per channel, 4 channel float format
FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT, ///< 16 bit per channel, 4 channel float format
FFX_SURFACE_FORMAT_R16G16B16A16_UNORM, ///< 16 bit per channel, 4 channel unsigned normalized format
FFX_SURFACE_FORMAT_R32G32_FLOAT, ///< 32 bit per channel, 2 channel float format
FFX_SURFACE_FORMAT_R32_UINT, ///< 32 bit per channel, 1 channel float format
FFX_SURFACE_FORMAT_R8G8B8A8_TYPELESS, ///< 8 bit per channel, 4 channel float format
FFX_SURFACE_FORMAT_R8G8B8A8_UNORM, ///< 8 bit per channel, 4 channel unsigned normalized format
FFX_SURFACE_FORMAT_R11G11B10_FLOAT, ///< 32 bit 3 channel float format
FFX_SURFACE_FORMAT_R16G16_FLOAT, ///< 16 bit per channel, 2 channel float format
FFX_SURFACE_FORMAT_R16G16_UINT, ///< 16 bit per channel, 2 channel unsigned int format
FFX_SURFACE_FORMAT_R16_FLOAT, ///< 16 bit per channel, 1 channel float format
FFX_SURFACE_FORMAT_R16_UINT, ///< 16 bit per channel, 1 channel unsigned int format
FFX_SURFACE_FORMAT_R16_UNORM, ///< 16 bit per channel, 1 channel unsigned normalized format
FFX_SURFACE_FORMAT_R16_SNORM, ///< 16 bit per channel, 1 channel signed normalized format
FFX_SURFACE_FORMAT_R8_UNORM, ///< 8 bit per channel, 1 channel unsigned normalized format
FFX_SURFACE_FORMAT_R8_UINT, ///< 8 bit per channel, 1 channel unsigned int format
FFX_SURFACE_FORMAT_R8G8_UNORM, ///< 8 bit per channel, 2 channel unsigned normalized format
FFX_SURFACE_FORMAT_R32_FLOAT ///< 32 bit per channel, 1 channel float format
} FfxSurfaceFormat;
/// An enumeration of resource usage.
typedef enum FfxResourceUsage {
FFX_RESOURCE_USAGE_READ_ONLY = 0, ///< No usage flags indicate a resource is read only.
FFX_RESOURCE_USAGE_RENDERTARGET = (1<<0), ///< Indicates a resource will be used as render target.
FFX_RESOURCE_USAGE_UAV = (1<<1), ///< Indicates a resource will be used as UAV.
} FfxResourceUsage;
/// An enumeration of resource states.
typedef enum FfxResourceStates {
FFX_RESOURCE_STATE_UNORDERED_ACCESS = (1<<0), ///< Indicates a resource is in the state to be used as UAV.
FFX_RESOURCE_STATE_COMPUTE_READ = (1 << 1), ///< Indicates a resource is in the state to be read by compute shaders.
FFX_RESOURCE_STATE_COPY_SRC = (1 << 2), ///< Indicates a resource is in the state to be used as source in a copy command.
FFX_RESOURCE_STATE_COPY_DEST = (1 << 3), ///< Indicates a resource is in the state to be used as destination in a copy command.
FFX_RESOURCE_STATE_GENERIC_READ = (FFX_RESOURCE_STATE_COPY_SRC | FFX_RESOURCE_STATE_COMPUTE_READ), ///< Indicates a resource is in generic (slow) read state.
} FfxResourceStates;
/// An enumeration of surface dimensions.
typedef enum FfxResourceDimension {
FFX_RESOURCE_DIMENSION_TEXTURE_1D, ///< A resource with a single dimension.
FFX_RESOURCE_DIMENSION_TEXTURE_2D, ///< A resource with two dimensions.
} FfxResourceDimension;
/// An enumeration of surface dimensions.
typedef enum FfxResourceFlags {
FFX_RESOURCE_FLAGS_NONE = 0, ///< No flags.
FFX_RESOURCE_FLAGS_ALIASABLE = (1<<0), ///< A bit indicating a resource does not need to persist across frames.
} FfxResourceFlags;
/// An enumeration of all resource view types.
typedef enum FfxResourceViewType {
FFX_RESOURCE_VIEW_UNORDERED_ACCESS, ///< The resource view is an unordered access view (UAV).
FFX_RESOURCE_VIEW_SHADER_READ, ///< The resource view is a shader resource view (SRV).
} FfxResourceViewType;
/// The type of filtering to perform when reading a texture.
typedef enum FfxFilterType {
FFX_FILTER_TYPE_POINT, ///< Point sampling.
FFX_FILTER_TYPE_LINEAR ///< Sampling with interpolation.
} FfxFilterType;
/// An enumeration of all supported shader models.
typedef enum FfxShaderModel {
FFX_SHADER_MODEL_5_1, ///< Shader model 5.1.
FFX_SHADER_MODEL_6_0, ///< Shader model 6.0.
FFX_SHADER_MODEL_6_1, ///< Shader model 6.1.
FFX_SHADER_MODEL_6_2, ///< Shader model 6.2.
FFX_SHADER_MODEL_6_3, ///< Shader model 6.3.
FFX_SHADER_MODEL_6_4, ///< Shader model 6.4.
FFX_SHADER_MODEL_6_5, ///< Shader model 6.5.
FFX_SHADER_MODEL_6_6, ///< Shader model 6.6.
FFX_SHADER_MODEL_6_7, ///< Shader model 6.7.
} FfxShaderModel;
// An enumeration for different resource types
typedef enum FfxResourceType {
FFX_RESOURCE_TYPE_BUFFER, ///< The resource is a buffer.
FFX_RESOURCE_TYPE_TEXTURE1D, ///< The resource is a 1-dimensional texture.
FFX_RESOURCE_TYPE_TEXTURE2D, ///< The resource is a 2-dimensional texture.
FFX_RESOURCE_TYPE_TEXTURE3D, ///< The resource is a 3-dimensional texture.
} FfxResourceType;
/// An enumeration for different heap types
typedef enum FfxHeapType {
FFX_HEAP_TYPE_DEFAULT = 0, ///< Local memory.
FFX_HEAP_TYPE_UPLOAD ///< Heap used for uploading resources.
} FfxHeapType;
/// An enumberation for different render job types
typedef enum FfxGpuJobType {
FFX_GPU_JOB_CLEAR_FLOAT = 0, ///< The GPU job is performing a floating-point clear.
FFX_GPU_JOB_COPY = 1, ///< The GPU job is performing a copy.
FFX_GPU_JOB_COMPUTE = 2, ///< The GPU job is performing a compute dispatch.
} FfxGpuJobType;
/// A typedef representing the graphics device.
typedef void* FfxDevice;
/// A typedef representing a command list or command buffer.
typedef void* FfxCommandList;
/// A typedef for a root signature.
typedef void* FfxRootSignature;
/// A typedef for a pipeline state object.
typedef void* FfxPipeline;
/// A structure encapasulating a collection of device capabilities.
typedef struct FfxDeviceCapabilities {
FfxShaderModel minimumSupportedShaderModel; ///< The minimum shader model supported by the device.
uint32_t waveLaneCountMin; ///< The minimum supported wavefront width.
uint32_t waveLaneCountMax; ///< The maximum supported wavefront width.
bool fp16Supported; ///< The device supports FP16 in hardware.
bool raytracingSupported; ///< The device supports raytracing.
} FfxDeviceCapabilities;
/// A structure encapsulating a 2-dimensional point, using 32bit unsigned integers.
typedef struct FfxDimensions2D {
uint32_t width; ///< The width of a 2-dimensional range.
uint32_t height; ///< The height of a 2-dimensional range.
} FfxDimensions2D;
/// A structure encapsulating a 2-dimensional point,
typedef struct FfxIntCoords2D {
int32_t x; ///< The x coordinate of a 2-dimensional point.
int32_t y; ///< The y coordinate of a 2-dimensional point.
} FfxIntCoords2D;
/// A structure encapsulating a 2-dimensional set of floating point coordinates.
typedef struct FfxFloatCoords2D {
float x; ///< The x coordinate of a 2-dimensional point.
float y; ///< The y coordinate of a 2-dimensional point.
} FfxFloatCoords2D;
/// A structure describing a resource.
typedef struct FfxResourceDescription {
FfxResourceType type; ///< The type of the resource.
FfxSurfaceFormat format; ///< The surface format.
uint32_t width; ///< The width of the resource.
uint32_t height; ///< The height of the resource.
uint32_t depth; ///< The depth of the resource.
uint32_t mipCount; ///< Number of mips (or 0 for full mipchain).
FfxResourceFlags flags; ///< A set of <c><i>FfxResourceFlags</i></c> flags.
} FfxResourceDescription;
/// An outward facing structure containing a resource
typedef struct FfxResource {
void* resource; ///< pointer to the resource.
wchar_t name[64];
FfxResourceDescription description;
FfxResourceStates state;
bool isDepth;
uint64_t descriptorData;
} FfxResource;
/// An internal structure containing a handle to a resource and resource views
typedef struct FfxResourceInternal {
int32_t internalIndex; ///< The index of the resource.
} FfxResourceInternal;
/// A structure defining a resource bind point
typedef struct FfxResourceBinding
{
uint32_t slotIndex;
uint32_t resourceIdentifier;
wchar_t name[64];
}FfxResourceBinding;
/// A structure encapsulating a single pass of an algorithm.
typedef struct FfxPipelineState {
FfxRootSignature rootSignature; ///< The pipelines rootSignature
FfxPipeline pipeline; ///< The pipeline object
uint32_t uavCount; ///< Count of UAVs used in this pipeline
uint32_t srvCount; ///< Count of SRVs used in this pipeline
uint32_t constCount; ///< Count of constant buffers used in this pipeline
FfxResourceBinding uavResourceBindings[FFX_MAX_NUM_UAVS]; ///< Array of ResourceIdentifiers bound as UAVs
FfxResourceBinding srvResourceBindings[FFX_MAX_NUM_SRVS]; ///< Array of ResourceIdentifiers bound as SRVs
FfxResourceBinding cbResourceBindings[FFX_MAX_NUM_CONST_BUFFERS]; ///< Array of ResourceIdentifiers bound as CBs
} FfxPipelineState;
/// A structure containing the data required to create a resource.
typedef struct FfxCreateResourceDescription {
FfxHeapType heapType; ///< The heap type to hold the resource, typically <c><i>FFX_HEAP_TYPE_DEFAULT</i></c>.
FfxResourceDescription resourceDescription; ///< A resource description.
FfxResourceStates initalState; ///< The initial resource state.
uint32_t initDataSize; ///< Size of initial data buffer.
void* initData; ///< Buffer containing data to fill the resource.
const wchar_t* name; ///< Name of the resource.
FfxResourceUsage usage; ///< Resource usage flags.
uint32_t id; ///< Internal resource ID.
} FfxCreateResourceDescription;
/// A structure containing the description used to create a
/// <c><i>FfxPipeline</i></c> structure.
///
/// A pipeline is the name given to a shader and the collection of state that
/// is required to dispatch it. In the context of FSR2 and its architecture
/// this means that a <c><i>FfxPipelineDescription</i></c> will map to either a
/// monolithic object in an explicit API (such as a
/// <c><i>PipelineStateObject</i></c> in DirectX 12). Or a shader and some
/// ancillary API objects (in something like DirectX 11).
///
/// The <c><i>contextFlags</i></c> field contains a copy of the flags passed
/// to <c><i>ffxFsr2ContextCreate</i></c> via the <c><i>flags</i></c> field of
/// the <c><i>FfxFsr2InitializationParams</i></c> structure. These flags are
/// used to determine which permutation of a pipeline for a specific
/// <c><i>FfxFsr2Pass</i></c> should be used to implement the features required
/// by each application, as well as to acheive the best performance on specific
/// target hardware configurations.
///
/// When using one of the provided backends for FSR2 (such as DirectX 12 or
/// Vulkan) the data required to create a pipeline is compiled offline and
/// included into the backend library that you are using. For cases where the
/// backend interface is overriden by providing custom callback function
/// implementations care should be taken to respect the contents of the
/// <c><i>contextFlags</i></c> field in order to correctly support the options
/// provided by FSR2, and acheive best performance.
///
/// @ingroup FSR2
typedef struct FfxPipelineDescription {
uint32_t contextFlags; ///< A collection of <c><i>FfxFsr2InitializationFlagBits</i></c> which were passed to the context.
FfxFilterType* samplers; ///< Array of static samplers.
size_t samplerCount; ///< The number of samples contained inside <c><i>samplers</i></c>.
const uint32_t* rootConstantBufferSizes; ///< Array containing the sizes of the root constant buffers (count of 32 bit elements).
uint32_t rootConstantBufferCount; ///< The number of root constants contained within <c><i>rootConstantBufferSizes</i></c>.
} FfxPipelineDescription;
/// A structure containing a constant buffer.
typedef struct FfxConstantBuffer {
uint32_t uint32Size; ///< Size of 32 bit chunks used in the constant buffer
uint32_t data[FFX_MAX_CONST_SIZE]; ///< Constant buffer data
}FfxConstantBuffer;
/// A structure describing a clear render job.
typedef struct FfxClearFloatJobDescription {
float color[4]; ///< The clear color of the resource.
FfxResourceInternal target; ///< The resource to be cleared.
} FfxClearFloatJobDescription;
/// A structure describing a compute render job.
typedef struct FfxComputeJobDescription {
FfxPipelineState pipeline; ///< Compute pipeline for the render job.
uint32_t dimensions[3]; ///< Dispatch dimensions.
FfxResourceInternal srvs[FFX_MAX_NUM_SRVS]; ///< SRV resources to be bound in the compute job.
wchar_t srvNames[FFX_MAX_NUM_SRVS][64];
FfxResourceInternal uavs[FFX_MAX_NUM_UAVS]; ///< UAV resources to be bound in the compute job.
uint32_t uavMip[FFX_MAX_NUM_UAVS]; ///< Mip level of UAV resources to be bound in the compute job.
wchar_t uavNames[FFX_MAX_NUM_UAVS][64];
FfxConstantBuffer cbs[FFX_MAX_NUM_CONST_BUFFERS]; ///< Constant buffers to be bound in the compute job.
wchar_t cbNames[FFX_MAX_NUM_CONST_BUFFERS][64];
uint32_t cbSlotIndex[FFX_MAX_NUM_CONST_BUFFERS]; ///< Slot index in the descriptor table
} FfxComputeJobDescription;
/// A structure describing a copy render job.
typedef struct FfxCopyJobDescription
{
FfxResourceInternal src; ///< Source resource for the copy.
FfxResourceInternal dst; ///< Destination resource for the copy.
} FfxCopyJobDescription;
/// A structure describing a single render job.
typedef struct FfxGpuJobDescription{
FfxGpuJobType jobType; ///< Type of the job.
union {
FfxClearFloatJobDescription clearJobDescriptor; ///< Clear job descriptor. Valid when <c><i>jobType</i></c> is <c><i>FFX_RENDER_JOB_CLEAR_FLOAT</i></c>.
FfxCopyJobDescription copyJobDescriptor; ///< Copy job descriptor. Valid when <c><i>jobType</i></c> is <c><i>FFX_RENDER_JOB_COPY</i></c>.
FfxComputeJobDescription computeJobDescriptor; ///< Compute job descriptor. Valid when <c><i>jobType</i></c> is <c><i>FFX_RENDER_JOB_COMPUTE</i></c>.
};
} FfxGpuJobDescription;
#ifdef __cplusplus
}
#endif // #ifdef __cplusplus

78
ffx-fsr2-api/ffx_util.h

@ -0,0 +1,78 @@
// This file is part of the FidelityFX SDK.
//
// Copyright (c) 2022-2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
#include "ffx_types.h"
/// The value of Pi.
const float FFX_PI = 3.141592653589793f;
/// An epsilon value for floating point numbers.
const float FFX_EPSILON = 1e-06f;
/// Helper macro to create the version number.
#define FFX_MAKE_VERSION(major, minor, patch) ((major << 22) | (minor << 12) | patch)
///< Use this to specify no version.
#define FFX_UNSPECIFIED_VERSION 0xFFFFAD00
/// Helper macro to avoid warnings about unused variables.
#define FFX_UNUSED(x) ((void)(x))
/// Helper macro to align an integer to the specified power of 2 boundary
#define FFX_ALIGN_UP(x, y) (((x) + ((y)-1)) & ~((y)-1))
/// Helper macro to check if a value is aligned.
#define FFX_IS_ALIGNED(x) (((x) != 0) && ((x) & ((x)-1)))
/// Helper macro to stringify a value.
#define FFX_STR(s) FFX_XSTR(s)
#define FFX_XSTR(s) #s
/// Helper macro to forward declare a structure.
#define FFX_FORWARD_DECLARE(x) typedef struct x x
/// Helper macro to return the maximum of two values.
#define FFX_MAXIMUM(x, y) (((x) > (y)) ? (x) : (y))
/// Helper macro to return the minimum of two values.
#define FFX_MINIMUM(x, y) (((x) < (y)) ? (x) : (y))
/// Helper macro to do safe free on a pointer.
#define FFX_SAFE_FREE(x) \
if (x) \
free(x)
/// Helper macro to return the abs of an integer value.
#define FFX_ABSOLUTE(x) (((x) < 0) ? (-(x)) : (x))
/// Helper macro to return sign of a value.
#define FFX_SIGN(x) (((x) < 0) ? -1 : 1)
/// Helper macro to work out the number of elements in an array.
#define FFX_ARRAY_ELEMENTS(x) (int32_t)((sizeof(x) / sizeof(0 [x])) / ((size_t)(!(sizeof(x) % sizeof(0 [x])))))
/// The maximum length of a path that can be specified to the FidelityFX API.
#define FFX_MAXIMUM_PATH (260)
/// Helper macro to check if the specified key is set in a bitfield.
#define FFX_CONTAINS_FLAG(options, key) ((options & key) == key)

565
ffx-fsr2-api/shaders/ffx_fsr2_common.h

@ -0,0 +1,565 @@
// This file is part of the FidelityFX SDK.
//
// Copyright (c) 2022-2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#if !defined(FFX_FSR2_COMMON_H)
#define FFX_FSR2_COMMON_H
#if defined(FFX_CPU) || defined(FFX_GPU)
//Locks
#define LOCK_LIFETIME_REMAINING 0
#define LOCK_TEMPORAL_LUMA 1
#endif // #if defined(FFX_CPU) || defined(FFX_GPU)
#if defined(FFX_GPU)
FFX_STATIC const FfxFloat32 FSR2_FP16_MIN = 6.10e-05f;
FFX_STATIC const FfxFloat32 FSR2_FP16_MAX = 65504.0f;
FFX_STATIC const FfxFloat32 FSR2_EPSILON = 1e-03f;
FFX_STATIC const FfxFloat32 FSR2_TONEMAP_EPSILON = 1.0f / FSR2_FP16_MAX;
FFX_STATIC const FfxFloat32 FSR2_FLT_MAX = 3.402823466e+38f;
FFX_STATIC const FfxFloat32 FSR2_FLT_MIN = 1.175494351e-38f;
// treat vector truncation warnings as errors
#pragma warning(error: 3206)
// suppress warnings
#pragma warning(disable: 3205) // conversion from larger type to smaller
#pragma warning(disable: 3571) // in ffxPow(f, e), f could be negative
// Reconstructed depth usage
FFX_STATIC const FfxFloat32 fReconstructedDepthBilinearWeightThreshold = 0.01f;
// Accumulation
FFX_STATIC const FfxFloat32 fUpsampleLanczosWeightScale = 1.0f / 12.0f;
FFX_STATIC const FfxFloat32 fMaxAccumulationLanczosWeight = 1.0f;
FFX_STATIC const FfxFloat32 fAverageLanczosWeightPerFrame = 0.74f * fUpsampleLanczosWeightScale; // Average lanczos weight for jitter accumulated samples
FFX_STATIC const FfxFloat32 fAccumulationMaxOnMotion = 3.0f * fUpsampleLanczosWeightScale;
// Auto exposure
FFX_STATIC const FfxFloat32 resetAutoExposureAverageSmoothing = 1e8f;
struct AccumulationPassCommonParams
{
FfxInt32x2 iPxHrPos;
FfxFloat32x2 fHrUv;
FfxFloat32x2 fLrUv_HwSampler;
FfxFloat32x2 fMotionVector;
FfxFloat32x2 fReprojectedHrUv;
FfxFloat32 fHrVelocity;
FfxFloat32 fDepthClipFactor;
FfxFloat32 fDilatedReactiveFactor;
FfxFloat32 fAccumulationMask;
FfxBoolean bIsResetFrame;
FfxBoolean bIsExistingSample;
FfxBoolean bIsNewSample;
};
struct LockState
{
FfxBoolean NewLock; //Set for both unique new and re-locked new
FfxBoolean WasLockedPrevFrame; //Set to identify if the pixel was already locked (relock)
};
void InitializeNewLockSample(FFX_PARAMETER_OUT FfxFloat32x2 fLockStatus)
{
fLockStatus = FfxFloat32x2(0, 0);
}
#if FFX_HALF
void InitializeNewLockSample(FFX_PARAMETER_OUT FFX_MIN16_F2 fLockStatus)
{
fLockStatus = FFX_MIN16_F2(0, 0);
}
#endif
void KillLock(FFX_PARAMETER_INOUT FfxFloat32x2 fLockStatus)
{
fLockStatus[LOCK_LIFETIME_REMAINING] = 0;
}
#if FFX_HALF
void KillLock(FFX_PARAMETER_INOUT FFX_MIN16_F2 fLockStatus)
{
fLockStatus[LOCK_LIFETIME_REMAINING] = FFX_MIN16_F(0);
}
#endif
struct RectificationBox
{
FfxFloat32x3 boxCenter;
FfxFloat32x3 boxVec;
FfxFloat32x3 aabbMin;
FfxFloat32x3 aabbMax;
FfxFloat32 fBoxCenterWeight;
};
#if FFX_HALF
struct RectificationBoxMin16
{
FFX_MIN16_F3 boxCenter;
FFX_MIN16_F3 boxVec;
FFX_MIN16_F3 aabbMin;
FFX_MIN16_F3 aabbMax;
FFX_MIN16_F fBoxCenterWeight;
};
#endif
void RectificationBoxReset(FFX_PARAMETER_INOUT RectificationBox rectificationBox)
{
rectificationBox.fBoxCenterWeight = FfxFloat32(0);
rectificationBox.boxCenter = FfxFloat32x3(0, 0, 0);
rectificationBox.boxVec = FfxFloat32x3(0, 0, 0);
rectificationBox.aabbMin = FfxFloat32x3(FSR2_FLT_MAX, FSR2_FLT_MAX, FSR2_FLT_MAX);
rectificationBox.aabbMax = -FfxFloat32x3(FSR2_FLT_MAX, FSR2_FLT_MAX, FSR2_FLT_MAX);
}
#if FFX_HALF
void RectificationBoxReset(FFX_PARAMETER_INOUT RectificationBoxMin16 rectificationBox)
{
rectificationBox.fBoxCenterWeight = FFX_MIN16_F(0);
rectificationBox.boxCenter = FFX_MIN16_F3(0, 0, 0);
rectificationBox.boxVec = FFX_MIN16_F3(0, 0, 0);
rectificationBox.aabbMin = FFX_MIN16_F3(FSR2_FP16_MAX, FSR2_FP16_MAX, FSR2_FP16_MAX);
rectificationBox.aabbMax = -FFX_MIN16_F3(FSR2_FP16_MAX, FSR2_FP16_MAX, FSR2_FP16_MAX);
}
#endif
void RectificationBoxAddInitialSample(FFX_PARAMETER_INOUT RectificationBox rectificationBox, const FfxFloat32x3 colorSample, const FfxFloat32 fSampleWeight)
{
rectificationBox.aabbMin = colorSample;
rectificationBox.aabbMax = colorSample;
FfxFloat32x3 weightedSample = colorSample * fSampleWeight;
rectificationBox.boxCenter = weightedSample;
rectificationBox.boxVec = colorSample * weightedSample;
rectificationBox.fBoxCenterWeight = fSampleWeight;
}
void RectificationBoxAddSample(FfxBoolean bInitialSample, FFX_PARAMETER_INOUT RectificationBox rectificationBox, const FfxFloat32x3 colorSample, const FfxFloat32 fSampleWeight)
{
if (bInitialSample) {
RectificationBoxAddInitialSample(rectificationBox, colorSample, fSampleWeight);
} else {
rectificationBox.aabbMin = ffxMin(rectificationBox.aabbMin, colorSample);
rectificationBox.aabbMax = ffxMax(rectificationBox.aabbMax, colorSample);
FfxFloat32x3 weightedSample = colorSample * fSampleWeight;
rectificationBox.boxCenter += weightedSample;
rectificationBox.boxVec += colorSample * weightedSample;
rectificationBox.fBoxCenterWeight += fSampleWeight;
}
}
#if FFX_HALF
void RectificationBoxAddInitialSample(FFX_PARAMETER_INOUT RectificationBoxMin16 rectificationBox, const FFX_MIN16_F3 colorSample, const FFX_MIN16_F fSampleWeight)
{
rectificationBox.aabbMin = colorSample;
rectificationBox.aabbMax = colorSample;
FFX_MIN16_F3 weightedSample = colorSample * fSampleWeight;
rectificationBox.boxCenter = weightedSample;
rectificationBox.boxVec = colorSample * weightedSample;
rectificationBox.fBoxCenterWeight = fSampleWeight;
}
void RectificationBoxAddSample(FfxBoolean bInitialSample, FFX_PARAMETER_INOUT RectificationBoxMin16 rectificationBox, const FFX_MIN16_F3 colorSample, const FFX_MIN16_F fSampleWeight)
{
if (bInitialSample) {
RectificationBoxAddInitialSample(rectificationBox, colorSample, fSampleWeight);
} else {
rectificationBox.aabbMin = ffxMin(rectificationBox.aabbMin, colorSample);
rectificationBox.aabbMax = ffxMax(rectificationBox.aabbMax, colorSample);
FFX_MIN16_F3 weightedSample = colorSample * fSampleWeight;
rectificationBox.boxCenter += weightedSample;
rectificationBox.boxVec += colorSample * weightedSample;
rectificationBox.fBoxCenterWeight += fSampleWeight;
}
}
#endif
void RectificationBoxComputeVarianceBoxData(FFX_PARAMETER_INOUT RectificationBox rectificationBox)
{
rectificationBox.fBoxCenterWeight = (abs(rectificationBox.fBoxCenterWeight) > FfxFloat32(FSR2_EPSILON) ? rectificationBox.fBoxCenterWeight : FfxFloat32(1.f));
rectificationBox.boxCenter /= rectificationBox.fBoxCenterWeight;
rectificationBox.boxVec /= rectificationBox.fBoxCenterWeight;
FfxFloat32x3 stdDev = sqrt(abs(rectificationBox.boxVec - rectificationBox.boxCenter * rectificationBox.boxCenter));
rectificationBox.boxVec = stdDev;
}
#if FFX_HALF
void RectificationBoxComputeVarianceBoxData(FFX_PARAMETER_INOUT RectificationBoxMin16 rectificationBox)
{
rectificationBox.fBoxCenterWeight = (abs(rectificationBox.fBoxCenterWeight) > FFX_MIN16_F(FSR2_EPSILON) ? rectificationBox.fBoxCenterWeight : FFX_MIN16_F(1.f));
rectificationBox.boxCenter /= rectificationBox.fBoxCenterWeight;
rectificationBox.boxVec /= rectificationBox.fBoxCenterWeight;
FFX_MIN16_F3 stdDev = sqrt(abs(rectificationBox.boxVec - rectificationBox.boxCenter * rectificationBox.boxCenter));
rectificationBox.boxVec = stdDev;
}
#endif
FfxFloat32x3 SafeRcp3(FfxFloat32x3 v)
{
return (all(FFX_NOT_EQUAL(v, FfxFloat32x3(0, 0, 0)))) ? (FfxFloat32x3(1, 1, 1) / v) : FfxFloat32x3(0, 0, 0);
}
#if FFX_HALF
FFX_MIN16_F3 SafeRcp3(FFX_MIN16_F3 v)
{
return (all(FFX_NOT_EQUAL(v, FFX_MIN16_F3(0, 0, 0)))) ? (FFX_MIN16_F3(1, 1, 1) / v) : FFX_MIN16_F3(0, 0, 0);
}
#endif
FfxFloat32 MinDividedByMax(const FfxFloat32 v0, const FfxFloat32 v1)
{
const FfxFloat32 m = ffxMax(v0, v1);
return m != 0 ? ffxMin(v0, v1) / m : 0;
}
#if FFX_HALF
FFX_MIN16_F MinDividedByMax(const FFX_MIN16_F v0, const FFX_MIN16_F v1)
{
const FFX_MIN16_F m = ffxMax(v0, v1);
return m != FFX_MIN16_F(0) ? ffxMin(v0, v1) / m : FFX_MIN16_F(0);
}
#endif
FfxFloat32x3 YCoCgToRGB(FfxFloat32x3 fYCoCg)
{
FfxFloat32x3 fRgb;
fRgb = FfxFloat32x3(
fYCoCg.x + fYCoCg.y - fYCoCg.z,
fYCoCg.x + fYCoCg.z,
fYCoCg.x - fYCoCg.y - fYCoCg.z);
return fRgb;
}
#if FFX_HALF
FFX_MIN16_F3 YCoCgToRGB(FFX_MIN16_F3 fYCoCg)
{
FFX_MIN16_F3 fRgb;
fRgb = FFX_MIN16_F3(
fYCoCg.x + fYCoCg.y - fYCoCg.z,
fYCoCg.x + fYCoCg.z,
fYCoCg.x - fYCoCg.y - fYCoCg.z);
return fRgb;
}
#endif
FfxFloat32x3 RGBToYCoCg(FfxFloat32x3 fRgb)
{
FfxFloat32x3 fYCoCg;
fYCoCg = FfxFloat32x3(
0.25f * fRgb.r + 0.5f * fRgb.g + 0.25f * fRgb.b,
0.5f * fRgb.r - 0.5f * fRgb.b,
-0.25f * fRgb.r + 0.5f * fRgb.g - 0.25f * fRgb.b);
return fYCoCg;
}
#if FFX_HALF
FFX_MIN16_F3 RGBToYCoCg(FFX_MIN16_F3 fRgb)
{
FFX_MIN16_F3 fYCoCg;
fYCoCg = FFX_MIN16_F3(
0.25 * fRgb.r + 0.5 * fRgb.g + 0.25 * fRgb.b,
0.5 * fRgb.r - 0.5 * fRgb.b,
-0.25 * fRgb.r + 0.5 * fRgb.g - 0.25 * fRgb.b);
return fYCoCg;
}
#endif
FfxFloat32 RGBToLuma(FfxFloat32x3 fLinearRgb)
{
return dot(fLinearRgb, FfxFloat32x3(0.2126f, 0.7152f, 0.0722f));
}
#if FFX_HALF
FFX_MIN16_F RGBToLuma(FFX_MIN16_F3 fLinearRgb)
{
return dot(fLinearRgb, FFX_MIN16_F3(0.2126f, 0.7152f, 0.0722f));
}
#endif
FfxFloat32 RGBToPerceivedLuma(FfxFloat32x3 fLinearRgb)
{
FfxFloat32 fLuminance = RGBToLuma(fLinearRgb);
FfxFloat32 fPercievedLuminance = 0;
if (fLuminance <= 216.0f / 24389.0f) {
fPercievedLuminance = fLuminance * (24389.0f / 27.0f);
}
else {
fPercievedLuminance = ffxPow(fLuminance, 1.0f / 3.0f) * 116.0f - 16.0f;
}
return fPercievedLuminance * 0.01f;
}
#if FFX_HALF
FFX_MIN16_F RGBToPerceivedLuma(FFX_MIN16_F3 fLinearRgb)
{
FFX_MIN16_F fLuminance = RGBToLuma(fLinearRgb);
FFX_MIN16_F fPercievedLuminance = FFX_MIN16_F(0);
if (fLuminance <= FFX_MIN16_F(216.0f / 24389.0f)) {
fPercievedLuminance = fLuminance * FFX_MIN16_F(24389.0f / 27.0f);
}
else {
fPercievedLuminance = ffxPow(fLuminance, FFX_MIN16_F(1.0f / 3.0f)) * FFX_MIN16_F(116.0f) - FFX_MIN16_F(16.0f);
}
return fPercievedLuminance * FFX_MIN16_F(0.01f);
}
#endif
FfxFloat32x3 Tonemap(FfxFloat32x3 fRgb)
{
return fRgb / (ffxMax(ffxMax(0.f, fRgb.r), ffxMax(fRgb.g, fRgb.b)) + 1.f).xxx;
}
FfxFloat32x3 InverseTonemap(FfxFloat32x3 fRgb)
{
return fRgb / ffxMax(FSR2_TONEMAP_EPSILON, 1.f - ffxMax(fRgb.r, ffxMax(fRgb.g, fRgb.b))).xxx;
}
#if FFX_HALF
FFX_MIN16_F3 Tonemap(FFX_MIN16_F3 fRgb)
{
return fRgb / (ffxMax(ffxMax(FFX_MIN16_F(0.f), fRgb.r), ffxMax(fRgb.g, fRgb.b)) + FFX_MIN16_F(1.f)).xxx;
}
FFX_MIN16_F3 InverseTonemap(FFX_MIN16_F3 fRgb)
{
return fRgb / ffxMax(FFX_MIN16_F(FSR2_TONEMAP_EPSILON), FFX_MIN16_F(1.f) - ffxMax(fRgb.r, ffxMax(fRgb.g, fRgb.b))).xxx;
}
#endif
FfxInt32x2 ClampLoad(FfxInt32x2 iPxSample, FfxInt32x2 iPxOffset, FfxInt32x2 iTextureSize)
{
FfxInt32x2 result = iPxSample + iPxOffset;
result.x = (iPxOffset.x < 0) ? ffxMax(result.x, 0) : result.x;
result.x = (iPxOffset.x > 0) ? ffxMin(result.x, iTextureSize.x - 1) : result.x;
result.y = (iPxOffset.y < 0) ? ffxMax(result.y, 0) : result.y;
result.y = (iPxOffset.y > 0) ? ffxMin(result.y, iTextureSize.y - 1) : result.y;
return result;
// return ffxMed3(iPxSample + iPxOffset, FfxInt32x2(0, 0), iTextureSize - FfxInt32x2(1, 1));
}
#if FFX_HALF
FFX_MIN16_I2 ClampLoad(FFX_MIN16_I2 iPxSample, FFX_MIN16_I2 iPxOffset, FFX_MIN16_I2 iTextureSize)
{
FFX_MIN16_I2 result = iPxSample + iPxOffset;
result.x = (iPxOffset.x < 0) ? ffxMax(result.x, FFX_MIN16_I(0)) : result.x;
result.x = (iPxOffset.x > 0) ? ffxMin(result.x, iTextureSize.x - FFX_MIN16_I(1)) : result.x;
result.y = (iPxOffset.y < 0) ? ffxMax(result.y, FFX_MIN16_I(0)) : result.y;
result.y = (iPxOffset.y > 0) ? ffxMin(result.y, iTextureSize.y - FFX_MIN16_I(1)) : result.y;
return result;
// return ffxMed3Half(iPxSample + iPxOffset, FFX_MIN16_I2(0, 0), iTextureSize - FFX_MIN16_I2(1, 1));
}
#endif
FfxFloat32x2 ClampUv(FfxFloat32x2 fUv, FfxInt32x2 iTextureSize, FfxInt32x2 iResourceSize)
{
const FfxFloat32x2 fSampleLocation = fUv * iTextureSize;
const FfxFloat32x2 fClampedLocation = ffxMax(FfxFloat32x2(0.5f, 0.5f), ffxMin(fSampleLocation, FfxFloat32x2(iTextureSize) - FfxFloat32x2(0.5f, 0.5f)));
const FfxFloat32x2 fClampedUv = fClampedLocation / FfxFloat32x2(iResourceSize);
return fClampedUv;
}
FfxBoolean IsOnScreen(FfxInt32x2 pos, FfxInt32x2 size)
{
return all(FFX_LESS_THAN(FfxUInt32x2(pos), FfxUInt32x2(size)));
}
#if FFX_HALF
FfxBoolean IsOnScreen(FFX_MIN16_I2 pos, FFX_MIN16_I2 size)
{
return all(FFX_LESS_THAN(FFX_MIN16_U2(pos), FFX_MIN16_U2(size)));
}
#endif
FfxFloat32 ComputeAutoExposureFromLavg(FfxFloat32 Lavg)
{
Lavg = exp(Lavg);
const FfxFloat32 S = 100.0f; //ISO arithmetic speed
const FfxFloat32 K = 12.5f;
FfxFloat32 ExposureISO100 = log2((Lavg * S) / K);
const FfxFloat32 q = 0.65f;
FfxFloat32 Lmax = (78.0f / (q * S)) * ffxPow(2.0f, ExposureISO100);
return 1 / Lmax;
}
#if FFX_HALF
FFX_MIN16_F ComputeAutoExposureFromLavg(FFX_MIN16_F Lavg)
{
Lavg = exp(Lavg);
const FFX_MIN16_F S = FFX_MIN16_F(100.0f); //ISO arithmetic speed
const FFX_MIN16_F K = FFX_MIN16_F(12.5f);
const FFX_MIN16_F ExposureISO100 = log2((Lavg * S) / K);
const FFX_MIN16_F q = FFX_MIN16_F(0.65f);
const FFX_MIN16_F Lmax = (FFX_MIN16_F(78.0f) / (q * S)) * ffxPow(FFX_MIN16_F(2.0f), ExposureISO100);
return FFX_MIN16_F(1) / Lmax;
}
#endif
FfxInt32x2 ComputeHrPosFromLrPos(FfxInt32x2 iPxLrPos)
{
FfxFloat32x2 fSrcJitteredPos = FfxFloat32x2(iPxLrPos) + 0.5f - Jitter();
FfxFloat32x2 fLrPosInHr = (fSrcJitteredPos / RenderSize()) * DisplaySize();
FfxInt32x2 iPxHrPos = FfxInt32x2(floor(fLrPosInHr));
return iPxHrPos;
}
#if FFX_HALF
FFX_MIN16_I2 ComputeHrPosFromLrPos(FFX_MIN16_I2 iPxLrPos)
{
FFX_MIN16_F2 fSrcJitteredPos = FFX_MIN16_F2(iPxLrPos) + FFX_MIN16_F(0.5f) - FFX_MIN16_F2(Jitter());
FFX_MIN16_F2 fLrPosInHr = (fSrcJitteredPos / FFX_MIN16_F2(RenderSize())) * FFX_MIN16_F2(DisplaySize());
FFX_MIN16_I2 iPxHrPos = FFX_MIN16_I2(floor(fLrPosInHr));
return iPxHrPos;
}
#endif
FfxFloat32x2 ComputeNdc(FfxFloat32x2 fPxPos, FfxInt32x2 iSize)
{
return fPxPos / FfxFloat32x2(iSize) * FfxFloat32x2(2.0f, -2.0f) + FfxFloat32x2(-1.0f, 1.0f);
}
FfxFloat32 GetViewSpaceDepth(FfxFloat32 fDeviceDepth)
{
const FfxFloat32x4 fDeviceToViewDepth = DeviceToViewSpaceTransformFactors();
// fDeviceToViewDepth details found in ffx_fsr2.cpp
return (fDeviceToViewDepth[1] / (fDeviceDepth - fDeviceToViewDepth[0]));
}
FfxFloat32 GetViewSpaceDepthInMeters(FfxFloat32 fDeviceDepth)
{
return GetViewSpaceDepth(fDeviceDepth) * ViewSpaceToMetersFactor();
}
FfxFloat32x3 GetViewSpacePosition(FfxInt32x2 iViewportPos, FfxInt32x2 iViewportSize, FfxFloat32 fDeviceDepth)
{
const FfxFloat32x4 fDeviceToViewDepth = DeviceToViewSpaceTransformFactors();
const FfxFloat32 Z = GetViewSpaceDepth(fDeviceDepth);
const FfxFloat32x2 fNdcPos = ComputeNdc(iViewportPos, iViewportSize);
const FfxFloat32 X = fDeviceToViewDepth[2] * fNdcPos.x * Z;
const FfxFloat32 Y = fDeviceToViewDepth[3] * fNdcPos.y * Z;
return FfxFloat32x3(X, Y, Z);
}
FfxFloat32x3 GetViewSpacePositionInMeters(FfxInt32x2 iViewportPos, FfxInt32x2 iViewportSize, FfxFloat32 fDeviceDepth)
{
return GetViewSpacePosition(iViewportPos, iViewportSize, fDeviceDepth) * ViewSpaceToMetersFactor();
}
FfxFloat32 GetMaxDistanceInMeters()
{
#if FFX_FSR2_OPTION_INVERTED_DEPTH
return GetViewSpaceDepth(0.0f) * ViewSpaceToMetersFactor();
#else
return GetViewSpaceDepth(1.0f) * ViewSpaceToMetersFactor();
#endif
}
FfxFloat32x3 PrepareRgb(FfxFloat32x3 fRgb, FfxFloat32 fExposure, FfxFloat32 fPreExposure)
{
fRgb /= fPreExposure;
fRgb *= fExposure;
fRgb = clamp(fRgb, 0.0f, FSR2_FP16_MAX);
return fRgb;
}
FfxFloat32x3 UnprepareRgb(FfxFloat32x3 fRgb, FfxFloat32 fExposure)
{
fRgb /= fExposure;
fRgb *= PreExposure();
return fRgb;
}
struct BilinearSamplingData
{
FfxInt32x2 iOffsets[4];
FfxFloat32 fWeights[4];
FfxInt32x2 iBasePos;
};
BilinearSamplingData GetBilinearSamplingData(FfxFloat32x2 fUv, FfxInt32x2 iSize)
{
BilinearSamplingData data;
FfxFloat32x2 fPxSample = (fUv * iSize) - FfxFloat32x2(0.5f, 0.5f);
data.iBasePos = FfxInt32x2(floor(fPxSample));
FfxFloat32x2 fPxFrac = ffxFract(fPxSample);
data.iOffsets[0] = FfxInt32x2(0, 0);
data.iOffsets[1] = FfxInt32x2(1, 0);
data.iOffsets[2] = FfxInt32x2(0, 1);
data.iOffsets[3] = FfxInt32x2(1, 1);
data.fWeights[0] = (1 - fPxFrac.x) * (1 - fPxFrac.y);
data.fWeights[1] = (fPxFrac.x) * (1 - fPxFrac.y);
data.fWeights[2] = (1 - fPxFrac.x) * (fPxFrac.y);
data.fWeights[3] = (fPxFrac.x) * (fPxFrac.y);
return data;
}
struct PlaneData
{
FfxFloat32x3 fNormal;
FfxFloat32 fDistanceFromOrigin;
};
PlaneData GetPlaneFromPoints(FfxFloat32x3 fP0, FfxFloat32x3 fP1, FfxFloat32x3 fP2)
{
PlaneData plane;
FfxFloat32x3 v0 = fP0 - fP1;
FfxFloat32x3 v1 = fP0 - fP2;
plane.fNormal = normalize(cross(v0, v1));
plane.fDistanceFromOrigin = -dot(fP0, plane.fNormal);
return plane;
}
FfxFloat32 PointToPlaneDistance(PlaneData plane, FfxFloat32x3 fPoint)
{
return abs(dot(plane.fNormal, fPoint) + plane.fDistanceFromOrigin);
}
#endif // #if defined(FFX_GPU)
#endif //!defined(FFX_FSR2_COMMON_H)

105
ffx-fsr2-api/shaders/ffx_fsr2_resources.h

@ -0,0 +1,105 @@
// This file is part of the FidelityFX SDK.
//
// Copyright (c) 2022-2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#ifndef FFX_FSR2_RESOURCES_H
#define FFX_FSR2_RESOURCES_H
#if defined(FFX_CPU) || defined(FFX_GPU)
#define FFX_FSR2_RESOURCE_IDENTIFIER_NULL 0
#define FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_OPAQUE_ONLY 1
#define FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_COLOR 2
#define FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_MOTION_VECTORS 3
#define FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_DEPTH 4
#define FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_EXPOSURE 5
#define FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_REACTIVE_MASK 6
#define FFX_FSR2_RESOURCE_IDENTIFIER_INPUT_TRANSPARENCY_AND_COMPOSITION_MASK 7
#define FFX_FSR2_RESOURCE_IDENTIFIER_RECONSTRUCTED_PREVIOUS_NEAREST_DEPTH 8
#define FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_MOTION_VECTORS 9
#define FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_DEPTH 10
#define FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_UPSCALED_COLOR 11
#define FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_STATUS 12
#define FFX_FSR2_RESOURCE_IDENTIFIER_NEW_LOCKS 13
#define FFX_FSR2_RESOURCE_IDENTIFIER_PREPARED_INPUT_COLOR 14
#define FFX_FSR2_RESOURCE_IDENTIFIER_LUMA_HISTORY 15
#define FFX_FSR2_RESOURCE_IDENTIFIER_DEBUG_OUTPUT 16
#define FFX_FSR2_RESOURCE_IDENTIFIER_LANCZOS_LUT 17
#define FFX_FSR2_RESOURCE_IDENTIFIER_SPD_ATOMIC_COUNT 18
#define FFX_FSR2_RESOURCE_IDENTIFIER_UPSCALED_OUTPUT 19
#define FFX_FSR2_RESOURCE_IDENTIFIER_RCAS_INPUT 20
#define FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_STATUS_1 21
#define FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_STATUS_2 22
#define FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_UPSCALED_COLOR_1 23
#define FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_UPSCALED_COLOR_2 24
#define FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_DEFAULT_REACTIVITY 25
#define FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_DEFAULT_TRANSPARENCY_AND_COMPOSITION 26
#define FFX_FSR2_RESOURCE_IDENTITIER_UPSAMPLE_MAXIMUM_BIAS_LUT 27
#define FFX_FSR2_RESOURCE_IDENTIFIER_DILATED_REACTIVE_MASKS 28
#define FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE 29 // same as FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_0
#define FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_0 29
#define FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_1 30
#define FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_2 31
#define FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_3 32
#define FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_4 33
#define FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_5 34
#define FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_6 35
#define FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_7 36
#define FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_8 37
#define FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_9 38
#define FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_10 39
#define FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_11 40
#define FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_12 41
#define FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_DEFAULT_EXPOSURE 42
#define FFX_FSR2_RESOURCE_IDENTIFIER_AUTO_EXPOSURE 43
#define FFX_FSR2_RESOURCE_IDENTIFIER_AUTOREACTIVE 44
#define FFX_FSR2_RESOURCE_IDENTIFIER_AUTOCOMPOSITION 45
#define FFX_FSR2_RESOURCE_IDENTIFIER_PREV_PRE_ALPHA_COLOR 46
#define FFX_FSR2_RESOURCE_IDENTIFIER_PREV_POST_ALPHA_COLOR 47
#define FFX_FSR2_RESOURCE_IDENTIFIER_PREV_PRE_ALPHA_COLOR_1 48
#define FFX_FSR2_RESOURCE_IDENTIFIER_PREV_POST_ALPHA_COLOR_1 49
#define FFX_FSR2_RESOURCE_IDENTIFIER_PREV_PRE_ALPHA_COLOR_2 50
#define FFX_FSR2_RESOURCE_IDENTIFIER_PREV_POST_ALPHA_COLOR_2 51
#define FFX_FSR2_RESOURCE_IDENTIFIER_PREVIOUS_DILATED_MOTION_VECTORS 52
#define FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_DILATED_MOTION_VECTORS_1 53
#define FFX_FSR2_RESOURCE_IDENTIFIER_INTERNAL_DILATED_MOTION_VECTORS_2 54
#define FFX_FSR2_RESOURCE_IDENTIFIER_LUMA_HISTORY_1 55
#define FFX_FSR2_RESOURCE_IDENTIFIER_LUMA_HISTORY_2 56
#define FFX_FSR2_RESOURCE_IDENTIFIER_LOCK_INPUT_LUMA 57
// Shading change detection mip level setting, value must be in the range [FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_0, FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_12]
#define FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_SHADING_CHANGE FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_4
#define FFX_FSR2_SHADING_CHANGE_MIP_LEVEL (FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE_MIPMAP_SHADING_CHANGE - FFX_FSR2_RESOURCE_IDENTIFIER_SCENE_LUMINANCE)
#define FFX_FSR2_RESOURCE_IDENTIFIER_COUNT 58
#define FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_FSR2 0
#define FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_SPD 1
#define FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_RCAS 2
#define FFX_FSR2_CONSTANTBUFFER_IDENTIFIER_GENREACTIVE 3
#define FFX_FSR2_AUTOREACTIVEFLAGS_APPLY_TONEMAP 1
#define FFX_FSR2_AUTOREACTIVEFLAGS_APPLY_INVERSETONEMAP 2
#define FFX_FSR2_AUTOREACTIVEFLAGS_APPLY_THRESHOLD 4
#define FFX_FSR2_AUTOREACTIVEFLAGS_USE_COMPONENTS_MAX 8
#endif // #if defined(FFX_CPU) || defined(FFX_GPU)
#endif //!defined( FFX_FSR2_RESOURCES_H )

158
ffx-fsr2-api/vk/ffx_fsr2_vk.h

@ -0,0 +1,158 @@
// This file is part of the FidelityFX SDK.
//
// Copyright (c) 2022-2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// @defgroup VK
#pragma once
#include <vulkan/vulkan.h>
#include "../ffx_fsr2_interface.h"
#if defined(__cplusplus)
extern "C" {
#endif // #if defined(__cplusplus)
/// Query how much memory is required for the Vulkan backend's scratch buffer.
///
/// @returns
/// The size (in bytes) of the required scratch memory buffer for the VK backend.
FFX_API size_t ffxFsr2GetScratchMemorySizeVK(VkPhysicalDevice physicalDevice);
/// Populate an interface with pointers for the VK backend.
///
/// @param [out] fsr2Interface A pointer to a <c><i>FfxFsr2Interface</i></c> structure to populate with pointers.
/// @param [in] device A Vulkan device.
/// @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 <c><i>scratchBuffer</i></c>.
/// @param [in] physicalDevice The Vulkan physical device that FSR 2.0 will be executed on.
/// @param [in] getDeviceProcAddr A function pointer to vkGetDeviceProcAddr which is used to obtain all the other Vulkan functions.
///
/// @retval
/// FFX_OK The operation completed successfully.
/// @retval
/// FFX_ERROR_CODE_INVALID_POINTER The <c><i>interface</i></c> pointer was <c><i>NULL</i></c>.
///
/// @ingroup FSR2 VK
FFX_API FfxErrorCode ffxFsr2GetInterfaceVK(
FfxFsr2Interface* outInterface,
void* scratchBuffer,
size_t scratchBufferSize,
VkPhysicalDevice physicalDevice,
PFN_vkGetDeviceProcAddr getDeviceProcAddr);
/// Create a <c><i>FfxFsr2Device</i></c> from a <c><i>VkDevice</i></c>.
///
/// @param [in] device A pointer to the Vulkan logical device.
///
/// @returns
/// An abstract FidelityFX device.
///
/// @ingroup FSR2 VK
FFX_API FfxDevice ffxGetDeviceVK(VkDevice device);
/// Create a <c><i>FfxCommandList</i></c> from a <c><i>VkCommandBuffer</i></c>.
///
/// @param [in] cmdBuf A pointer to the Vulkan command buffer.
///
/// @returns
/// An abstract FidelityFX command list.
///
/// @ingroup FSR2 VK
FFX_API FfxCommandList ffxGetCommandListVK(VkCommandBuffer cmdBuf);
/// Create a <c><i>FfxResource</i></c> from a <c><i>VkImage</i></c>.
///
/// @param [in] context A pointer to a <c><i>FfxFsr2Context</i></c> structure.
/// @param [in] imgVk A Vulkan image resource.
/// @param [in] imageView An image view of the given image resource.
/// @param [in] width The width of the image resource.
/// @param [in] height The height of the image resource.
/// @param [in] imgFormat The format of the image resource.
/// @param [in] name (optional) A name string to identify the resource in debug mode.
/// @param [in] state The state the resource is currently in.
///
/// @returns
/// An abstract FidelityFX resources.
///
/// @ingroup FSR2 VK
FFX_API FfxResource ffxGetTextureResourceVK(FfxFsr2Context* context,
VkImage imgVk,
VkImageView imageView,
uint32_t width,
uint32_t height,
VkFormat imgFormat,
const wchar_t* name = nullptr,
FfxResourceStates state = FFX_RESOURCE_STATE_COMPUTE_READ);
/// Create a <c><i>FfxResource</i></c> from a <c><i>VkBuffer</i></c>.
///
/// @param [in] context A pointer to a <c><i>FfxFsr2Context</i></c> structure.
/// @param [in] bufVk A Vulkan buffer resource.
/// @param [in] size The size of the buffer resource.
/// @param [in] name (optional) A name string to identify the resource in debug mode.
/// @param [in] state The state the resource is currently in.
///
/// @returns
/// An abstract FidelityFX resources.
///
/// @ingroup FSR2 VK
FFX_API FfxResource ffxGetBufferResourceVK(FfxFsr2Context* context,
VkBuffer bufVk,
uint32_t size,
const wchar_t* name = nullptr,
FfxResourceStates state = FFX_RESOURCE_STATE_COMPUTE_READ);
/// Convert a <c><i>FfxResource</i></c> value to a <c><i>VkImage</i></c>.
///
/// @param [in] context A pointer to a <c><i>FfxFsr2Context</i></c> structure.
/// @param [in] resId A resourceID.
///
/// @returns
/// A <c><i>VkImage</i></c>.
///
/// @ingroup FSR2 VK
FFX_API VkImage ffxGetVkImage(FfxFsr2Context* context, uint32_t resId);
/// Convert a <c><i>FfxResource</i></c> value to a <c><i>VkImageView</i></c>.
///
/// @param [in] context A pointer to a <c><i>FfxFsr2Context</i></c> structure.
/// @param [in] resId A resourceID.
///
/// @returns
/// A <c><i>VkImage</i></c>.
///
/// @ingroup FSR2 VK
FFX_API VkImageView ffxGetVkImageView(FfxFsr2Context* context, uint32_t resId);
/// Convert a <c><i>FfxResource</i></c> value to a <c><i>VkImageLayout</i></c>.
///
/// @param [in] context A pointer to a <c><i>FfxFsr2Context</i></c> structure.
/// @param [in] resId A resourceID.
///
/// @returns
/// A <c><i>VkImage</i></c>.
///
/// @ingroup FSR2 VK
FFX_API VkImageLayout ffxGetVkImageLayout(FfxFsr2Context* context, uint32_t resId);
#if defined(__cplusplus)
}
#endif // #if defined(__cplusplus)

BIN
lib/ffx_fsr2_api/ffx_fsr2_api_dx11_x64.lib

BIN
lib/ffx_fsr2_api/ffx_fsr2_api_dx11_x64d.lib

BIN
lib/ffx_fsr2_api/ffx_fsr2_api_dx12_x64.lib

BIN
lib/ffx_fsr2_api/ffx_fsr2_api_dx12_x64d.lib

BIN
lib/ffx_fsr2_api/ffx_fsr2_api_vk_x64.lib

BIN
lib/ffx_fsr2_api/ffx_fsr2_api_vk_x64d.lib

BIN
lib/ffx_fsr2_api/ffx_fsr2_api_x64.lib

BIN
lib/ffx_fsr2_api/ffx_fsr2_api_x64d.lib

Loading…
Cancel
Save