Files
DroidDeck/tools/upscale/bridges/wl_ffx_bridge.c
T
The412Banner e5d7bddfdb upscale: DLSS frames reach the layer in Streamline/Unreal D3D12 games
Bright Memory: Infinite (Unreal Engine, D3D12, NVIDIA Streamline) now passed NGX's checks but no
frame reached the layer. Two causes, fixed here:

- The layer recognised the bridge's mailbox by size and the storage bit only, in a 128-entry table;
  the game's own 8 KB storage buffers filled it first, so the bridge's doorbells were ignored. It now
  matches the exact usage the bridges create them with (mailbox STORAGE|TRANSFER_DST, marker
  TRANSFER_DST), keeps 1024 entries and warns once if the table is ever full.
- Streamline (sl.interposer) wraps the devices and command lists it creates; vkd3d-proton and DXVK
  do not know the wrappers. wl_streamline.h asks a wrapper for StreamlineRetrieveBaseInterface
  (Streamline source/core/sl.interposer/d3d12/d3d12Device.cpp) and the interop calls retry on the
  wrapped object: device queries, BeginVkCommandBufferInterop/End, GetVulkanResourceInfo1.

DROIDDECK_UPSCALE_LOG=1 now also logs the NGX bridge's D3D12 attach and first evaluate (seam
ready, images found, command list accepted, dispatch result).
2026-12-05 02:40:10 -05:00

576 lines
23 KiB
C

// Winlator Ludashi Native AMD FidelityFX API 2.0 (FSR 4.1.1) Bridge DLL
// Works for BOTH Vulkan (amd_fidelityfx_vk.dll / amd_fidelityfx_upscaler_vk.dll)
// AND DirectX 12 via vkd3d-proton interop (amd_fidelityfx_dx12.dll / amd_fidelityfx_upscaler_dx12.dll).
//
// Exposes AMD FSR 4.1.1 (and 3.1.4 compat) via ffxQuery(GET_VERSIONS) and forwards every
// ffxDispatch(UPSCALE) zero-copy to libVkLayer_winlator_fsr4_npu.so on the Hexagon HTP v81 NPU.
#include <windows.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include "wl_seam_client.h"
#include "wl_vkd3d_interop.h"
#include "wl_xess3_direct.h"
// ---- Official AMD FidelityFX API 2.0 ABI (verified from GPUOpen FidelityFX-SDK) ----
typedef void *ffxContext;
typedef uint32_t ffxReturnCode_t;
typedef uint64_t ffxStructType_t;
enum {
FFX_API_RETURN_OK = 0,
FFX_API_RETURN_ERROR = 1,
FFX_API_RETURN_ERROR_UNKNOWN_DESCTYPE = 2,
FFX_API_RETURN_ERROR_RUNTIME_ERROR = 3,
FFX_API_RETURN_NO_PROVIDER = 4,
FFX_API_RETURN_ERROR_MEMORY = 5,
FFX_API_RETURN_ERROR_PARAMETER = 6,
};
typedef struct ffxApiHeader {
ffxStructType_t type;
struct ffxApiHeader *pNext;
} ffxApiHeader;
typedef void *(*ffxAlloc)(void *pUserData, uint64_t size);
typedef void (*ffxDealloc)(void *pUserData, void *pMem);
typedef struct ffxAllocationCallbacks {
void *pUserData;
ffxAlloc alloc;
ffxDealloc dealloc;
} ffxAllocationCallbacks;
typedef void (*ffxApiMessage)(uint32_t type, const wchar_t *message);
typedef struct FfxApiDimensions2D { uint32_t width, height; } FfxApiDimensions2D;
typedef struct FfxApiFloatCoords2D { float x, y; } FfxApiFloatCoords2D;
typedef struct FfxApiResourceDescription {
uint32_t type;
uint32_t format;
uint32_t width;
uint32_t height;
uint32_t depth;
uint32_t mipCount;
uint32_t flags;
uint32_t usage;
} FfxApiResourceDescription;
typedef struct FfxApiResource {
void *resource;
FfxApiResourceDescription description;
uint32_t state;
} FfxApiResource;
typedef struct FfxApiEffectMemoryUsage {
uint64_t totalUsageInBytes;
uint64_t aliasableUsageInBytes;
} FfxApiEffectMemoryUsage;
#define FFX_API_QUERY_DESC_TYPE_GET_VERSIONS 4u
#define FFX_API_DESC_TYPE_OVERRIDE_VERSION 5u
#define FFX_API_QUERY_DESC_TYPE_GET_PROVIDER_VERSION 6u
#define FFX_API_CONFIGURE_DESC_TYPE_GLOBALDEBUG 7u
#define FFX_API_CREATE_CONTEXT_DESC_TYPE_BACKEND_DX12 0x00000002u
#define FFX_API_CREATE_CONTEXT_DESC_TYPE_BACKEND_VK_DEPRECATED 0x00000003u
#define FFX_API_CREATE_CONTEXT_DESC_TYPE_BACKEND_VK 0x01000003u
#define FFX_API_CREATE_CONTEXT_DESC_TYPE_UPSCALE 0x00010000u
#define FFX_API_DISPATCH_DESC_TYPE_UPSCALE 0x00010001u
#define FFX_API_QUERY_DESC_TYPE_UPSCALE_GETUPSCALERATIO 0x00010002u
#define FFX_API_QUERY_DESC_TYPE_UPSCALE_GETRENDERRES 0x00010003u
#define FFX_API_QUERY_DESC_TYPE_UPSCALE_GETJITTERPHASECOUNT 0x00010004u
#define FFX_API_QUERY_DESC_TYPE_UPSCALE_GETJITTEROFFSET 0x00010005u
#define FFX_API_DISPATCH_DESC_TYPE_UPSCALE_GENREACTIVEMASK 0x00010006u
#define FFX_API_CONFIGURE_DESC_TYPE_UPSCALE_KEYVALUE 0x00010007u
#define FFX_API_QUERY_DESC_TYPE_UPSCALE_GPU_MEMORY_USAGE 0x00010008u
#define FFX_API_QUERY_DESC_TYPE_UPSCALE_GPU_MEMORY_USAGE_V2 0x00010009u
#define FFX_API_QUERY_DESC_TYPE_UPSCALE_GET_RESOURCE_REQS 0x0001000au
#define FFX_API_CREATE_CONTEXT_DESC_TYPE_UPSCALE_VERSION 0x0001000bu
enum {
FFX_UPSCALE_QUALITY_MODE_NATIVEAA = 0,
FFX_UPSCALE_QUALITY_MODE_QUALITY = 1,
FFX_UPSCALE_QUALITY_MODE_BALANCED = 2,
FFX_UPSCALE_QUALITY_MODE_PERFORMANCE = 3,
FFX_UPSCALE_QUALITY_MODE_ULTRA_PERFORMANCE = 4,
};
typedef struct {
ffxApiHeader header;
uint64_t createDescType;
void *device;
uint64_t *outputCount;
uint64_t *versionIds;
const char **versionNames;
} ffxQueryDescGetVersions;
typedef struct {
ffxApiHeader header;
uint64_t versionId;
const char *versionName;
} ffxQueryGetProviderVersion;
typedef struct {
ffxApiHeader header;
uint32_t flags;
FfxApiDimensions2D maxRenderSize;
FfxApiDimensions2D maxUpscaleSize;
ffxApiMessage fpMessage;
} ffxCreateContextDescUpscale;
typedef struct {
VkDevice vkDevice;
VkPhysicalDevice vkPhysicalDevice;
PFN_wlVkGetDeviceProcAddr vkDeviceProcAddr;
} FfxVkDeviceContext;
typedef struct {
ffxApiHeader header;
FfxVkDeviceContext deviceContext;
} ffxCreateBackendVKDesc;
typedef struct {
ffxApiHeader header;
void *device; // ID3D12Device*
} ffxCreateBackendDX12Desc;
typedef struct {
ffxApiHeader header;
void *commandList;
FfxApiResource color;
FfxApiResource depth;
FfxApiResource motionVectors;
FfxApiResource exposure;
FfxApiResource reactive;
FfxApiResource transparencyAndComposition;
FfxApiResource output;
FfxApiFloatCoords2D jitterOffset;
FfxApiFloatCoords2D motionVectorScale;
FfxApiDimensions2D renderSize;
FfxApiDimensions2D upscaleSize;
bool enableSharpening;
float sharpness;
float frameTimeDelta;
float preExposure;
bool reset;
float cameraNear;
float cameraFar;
float cameraFovAngleVertical;
float viewSpaceToMetersFactor;
uint32_t flags;
} ffxDispatchDescUpscale;
typedef struct {
ffxApiHeader header;
uint32_t qualityMode;
float *pOutUpscaleRatio;
} ffxQueryDescUpscaleGetUpscaleRatio;
typedef struct {
ffxApiHeader header;
uint32_t displayWidth, displayHeight, qualityMode;
uint32_t *pOutRenderWidth, *pOutRenderHeight;
} ffxQueryDescUpscaleGetRenderRes;
typedef struct {
ffxApiHeader header;
uint32_t renderWidth, displayWidth;
int32_t *pOutPhaseCount;
} ffxQueryDescUpscaleGetJitterPhaseCount;
typedef struct {
ffxApiHeader header;
int32_t index, phaseCount;
float *pOutX, *pOutY;
} ffxQueryDescUpscaleGetJitterOffset;
typedef struct {
ffxApiHeader header;
FfxApiEffectMemoryUsage *gpuMemoryUsageUpscaler;
} ffxQueryDescUpscaleGetGPUMem;
typedef struct {
ffxApiHeader header;
void *device;
FfxApiDimensions2D maxRenderSize, maxUpscaleSize;
uint32_t flags;
FfxApiEffectMemoryUsage *gpuMemoryUsageUpscaler;
} ffxQueryDescUpscaleGetGPUMemV2;
typedef struct {
ffxApiHeader header;
uint64_t required_resources;
uint64_t optional_resources;
} ffxQueryDescUpscaleGetResourceReqs;
// ---- Version IDs exposed to games ----
// Bit packing matches AMD FidelityFX SDK 2.0 (major<<22 | minor<<12 | patch) + provider tag
#define WL_FSR4_VER_ID_4_1_1 ((4ULL << 22) | (1ULL << 12) | 1ULL)
#define WL_FSR4_VER_ID_3_1_4 ((3ULL << 22) | (1ULL << 12) | 4ULL)
static const uint64_t k_ver_ids[2] = { WL_FSR4_VER_ID_4_1_1, WL_FSR4_VER_ID_3_1_4 };
static const char *k_ver_names[2] = { "4.1.1", "3.1.4" };
typedef struct WlFfxUpscaleCtx {
int is_dx12;
int needs_reset;
uint32_t create_flags;
uint32_t max_rw, max_rh, max_ow, max_oh;
void *d3d12_dev;
WlSeamClient seam;
WlXess3State xess3;
WlID3D12DXVKInteropDevice1 *dx12_interop;
} WlFfxUpscaleCtx;
static volatile uint32_t g_last_ffx_quality = WLFSR4_QUALITY_AUTO;
static uint32_t ffx_mode_to_wl_quality(uint32_t mode) {
switch (mode) {
case FFX_UPSCALE_QUALITY_MODE_NATIVEAA: return WLFSR4_QUALITY_NATIVE_AA;
case FFX_UPSCALE_QUALITY_MODE_QUALITY: return WLFSR4_QUALITY_QUALITY;
case FFX_UPSCALE_QUALITY_MODE_BALANCED: return WLFSR4_QUALITY_BALANCED;
case FFX_UPSCALE_QUALITY_MODE_PERFORMANCE: return WLFSR4_QUALITY_PERF;
case FFX_UPSCALE_QUALITY_MODE_ULTRA_PERFORMANCE: return WLFSR4_QUALITY_ULTRA_PERF;
default: return WLFSR4_QUALITY_AUTO;
}
}
static uint32_t ffx_infer_quality(uint32_t rw, uint32_t ow) {
if (rw == 0u || ow == 0u) return WLFSR4_QUALITY_AUTO;
float r = (float)ow / (float)rw;
if (r < 1.15f) return WLFSR4_QUALITY_NATIVE_AA;
if (r < 1.38f) return WLFSR4_QUALITY_ULTRA_QUAL;
if (r < 1.60f) return WLFSR4_QUALITY_QUALITY;
if (r < 1.85f) return WLFSR4_QUALITY_BALANCED;
if (r < 2.45f) return WLFSR4_QUALITY_PERF;
return WLFSR4_QUALITY_ULTRA_PERF;
}
static float halton(int32_t index, int32_t base) {
float f = 1.0f, r = 0.0f;
int32_t cur = index;
while (cur > 0) {
f /= (float)base;
r += f * (float)(cur % base);
cur /= base;
}
return r;
}
static uint32_t ffx_fmt_to_vk(uint32_t f) {
switch (f) {
case 3: return 109; /* R32G32B32A32_SFLOAT */
case 4: return 97; /* R16G16B16A16_SFLOAT */
case 6: return 103; /* R32G32_SFLOAT */
case 10: return 37; /* R8G8B8A8_UNORM */
case 12: return 43; /* R8G8B8A8_SRGB */
case 14: return 44; /* B8G8R8A8_UNORM */
case 15: return 50; /* B8G8R8A8_SRGB */
case 16: return 122; /* B10G11R11_UFLOAT_PACK32 */
case 17: return 64; /* A2B10G10R10_UNORM_PACK32 */
case 18: return 83; /* R16G16_SFLOAT */
case 28: return 100; /* R32_SFLOAT */
default: return 0; /* let Vulkan layer look up from vkCreateImage table */
}
}
static uint32_t ffx_state_to_vk_layout(uint32_t st, int is_depth) {
if (st & (1u << 1)) return 1; /* UNORDERED_ACCESS -> GENERAL */
if (st & ((1u << 2) | (1u << 3))) return is_depth ? 4 : 5; /* DEPTH_STENCIL_READ_ONLY / SHADER_READ_ONLY */
if (st & (1u << 4)) return 6; /* TRANSFER_SRC */
if (st & (1u << 5)) return 7; /* TRANSFER_DST */
if (st & (1u << 8)) return 2; /* COLOR_ATTACHMENT_OPTIMAL */
if (st & (1u << 9)) return 3; /* DEPTH_STENCIL_ATTACHMENT_OPTIMAL */
return 1; /* GENERAL */
}
static float quality_ratio(uint32_t mode) {
switch (mode) {
case FFX_UPSCALE_QUALITY_MODE_NATIVEAA: return 1.0f;
case FFX_UPSCALE_QUALITY_MODE_QUALITY: return 1.5f;
case FFX_UPSCALE_QUALITY_MODE_BALANCED: return 1.7f;
case FFX_UPSCALE_QUALITY_MODE_PERFORMANCE: return 2.0f;
case FFX_UPSCALE_QUALITY_MODE_ULTRA_PERFORMANCE: return 3.0f;
default: return 2.0f;
}
}
__declspec(dllexport) ffxReturnCode_t ffxCreateContext(ffxContext *context, ffxApiHeader *desc, const ffxAllocationCallbacks *memCb) {
(void)memCb;
if (!context || !desc) return FFX_API_RETURN_ERROR_PARAMETER;
const ffxCreateContextDescUpscale *up = NULL;
const ffxCreateBackendVKDesc *bvk = NULL;
const ffxCreateBackendDX12Desc *bdx = NULL;
for (const ffxApiHeader *h = desc; h; h = h->pNext) {
if (h->type == FFX_API_CREATE_CONTEXT_DESC_TYPE_UPSCALE) up = (const ffxCreateContextDescUpscale *)h;
else if (h->type == FFX_API_CREATE_CONTEXT_DESC_TYPE_BACKEND_VK ||
h->type == FFX_API_CREATE_CONTEXT_DESC_TYPE_BACKEND_VK_DEPRECATED) bvk = (const ffxCreateBackendVKDesc *)h;
else if (h->type == FFX_API_CREATE_CONTEXT_DESC_TYPE_BACKEND_DX12) bdx = (const ffxCreateBackendDX12Desc *)h;
}
if (!up) return FFX_API_RETURN_NO_PROVIDER;
WlFfxUpscaleCtx *ctx = (WlFfxUpscaleCtx *)calloc(1, sizeof *ctx);
if (!ctx) return FFX_API_RETURN_ERROR_MEMORY;
ctx->create_flags = up->flags;
ctx->needs_reset = 1;
ctx->max_rw = up->maxRenderSize.width;
ctx->max_rh = up->maxRenderSize.height;
ctx->max_ow = up->maxUpscaleSize.width;
ctx->max_oh = up->maxUpscaleSize.height;
if (bvk && bvk->deviceContext.vkDevice) {
ctx->is_dx12 = 0;
wl_seam_init(&ctx->seam, NULL, bvk->deviceContext.vkPhysicalDevice,
bvk->deviceContext.vkDevice, bvk->deviceContext.vkDeviceProcAddr);
} else if (bdx && bdx->device) {
ctx->is_dx12 = 1;
ctx->d3d12_dev = bdx->device;
ctx->dx12_interop = wl_query_vkd3d_interop(bdx->device);
if (ctx->dx12_interop) {
void *vk_inst = NULL, *vk_phys = NULL, *vk_dev = NULL;
if (ctx->dx12_interop->lpVtbl->GetVulkanHandles(ctx->dx12_interop, &vk_inst, &vk_phys, &vk_dev) == S_OK && vk_dev) {
wl_seam_init(&ctx->seam, vk_inst, vk_phys, vk_dev, NULL);
}
}
}
*context = ctx;
return FFX_API_RETURN_OK;
}
__declspec(dllexport) ffxReturnCode_t ffxDestroyContext(ffxContext *context, const ffxAllocationCallbacks *memCb) {
(void)memCb;
if (!context || !*context) return FFX_API_RETURN_OK;
WlFfxUpscaleCtx *ctx = (WlFfxUpscaleCtx *)(*context);
wl_xess3_destroy(&ctx->xess3);
wl_seam_destroy(&ctx->seam);
if (ctx->dx12_interop) {
ctx->dx12_interop->lpVtbl->Release(ctx->dx12_interop);
ctx->dx12_interop = NULL;
}
free(ctx);
*context = NULL;
return FFX_API_RETURN_OK;
}
__declspec(dllexport) ffxReturnCode_t ffxConfigure(ffxContext *context, const ffxApiHeader *desc) {
(void)context; (void)desc;
return FFX_API_RETURN_OK;
}
__declspec(dllexport) ffxReturnCode_t ffxQuery(ffxContext *context, ffxApiHeader *desc) {
(void)context;
if (!desc) return FFX_API_RETURN_ERROR_PARAMETER;
for (ffxApiHeader *h = desc; h; h = h->pNext) {
switch (h->type) {
case FFX_API_QUERY_DESC_TYPE_GET_VERSIONS: {
ffxQueryDescGetVersions *q = (ffxQueryDescGetVersions *)h;
if (!q->outputCount) return FFX_API_RETURN_ERROR_PARAMETER;
uint64_t cap = *q->outputCount;
if (cap == 0 || (!q->versionIds && !q->versionNames)) {
*q->outputCount = 2;
break;
}
uint64_t n = cap < 2 ? cap : 2;
for (uint64_t i = 0; i < n; i++) {
if (q->versionIds) q->versionIds[i] = k_ver_ids[i];
if (q->versionNames) q->versionNames[i] = k_ver_names[i];
}
*q->outputCount = n;
break;
}
case FFX_API_QUERY_DESC_TYPE_GET_PROVIDER_VERSION: {
ffxQueryGetProviderVersion *q = (ffxQueryGetProviderVersion *)h;
q->versionId = WL_FSR4_VER_ID_4_1_1;
q->versionName = "4.1.1";
break;
}
case FFX_API_QUERY_DESC_TYPE_UPSCALE_GETUPSCALERATIO: {
ffxQueryDescUpscaleGetUpscaleRatio *q = (ffxQueryDescUpscaleGetUpscaleRatio *)h;
g_last_ffx_quality = ffx_mode_to_wl_quality(q->qualityMode);
if (q->pOutUpscaleRatio) *q->pOutUpscaleRatio = quality_ratio(q->qualityMode);
break;
}
case FFX_API_QUERY_DESC_TYPE_UPSCALE_GETRENDERRES: {
ffxQueryDescUpscaleGetRenderRes *q = (ffxQueryDescUpscaleGetRenderRes *)h;
g_last_ffx_quality = ffx_mode_to_wl_quality(q->qualityMode);
float r = quality_ratio(q->qualityMode);
if (q->pOutRenderWidth) *q->pOutRenderWidth = (uint32_t)((float)q->displayWidth / r);
if (q->pOutRenderHeight) *q->pOutRenderHeight = (uint32_t)((float)q->displayHeight / r);
break;
}
case FFX_API_QUERY_DESC_TYPE_UPSCALE_GETJITTERPHASECOUNT: {
ffxQueryDescUpscaleGetJitterPhaseCount *q = (ffxQueryDescUpscaleGetJitterPhaseCount *)h;
if (q->pOutPhaseCount && q->renderWidth > 0) {
float ratio = (float)q->displayWidth / (float)q->renderWidth;
int32_t pc = (int32_t)ceilf(8.0f * ratio * ratio);
if (pc < 8) pc = 8;
*q->pOutPhaseCount = pc;
}
break;
}
case FFX_API_QUERY_DESC_TYPE_UPSCALE_GETJITTEROFFSET: {
ffxQueryDescUpscaleGetJitterOffset *q = (ffxQueryDescUpscaleGetJitterOffset *)h;
int32_t pc = q->phaseCount > 0 ? q->phaseCount : 8;
int32_t idx = (q->index % pc) + 1;
if (idx <= 0) idx += pc;
if (q->pOutX) *q->pOutX = halton(idx, 2) - 0.5f;
if (q->pOutY) *q->pOutY = halton(idx, 3) - 0.5f;
break;
}
case FFX_API_QUERY_DESC_TYPE_UPSCALE_GPU_MEMORY_USAGE: {
ffxQueryDescUpscaleGetGPUMem *q = (ffxQueryDescUpscaleGetGPUMem *)h;
if (q->gpuMemoryUsageUpscaler) {
q->gpuMemoryUsageUpscaler->totalUsageInBytes = 64ULL * 1024ULL * 1024ULL;
q->gpuMemoryUsageUpscaler->aliasableUsageInBytes = 0;
}
break;
}
case FFX_API_QUERY_DESC_TYPE_UPSCALE_GPU_MEMORY_USAGE_V2: {
ffxQueryDescUpscaleGetGPUMemV2 *q = (ffxQueryDescUpscaleGetGPUMemV2 *)h;
if (q->gpuMemoryUsageUpscaler) {
q->gpuMemoryUsageUpscaler->totalUsageInBytes = 64ULL * 1024ULL * 1024ULL;
q->gpuMemoryUsageUpscaler->aliasableUsageInBytes = 0;
}
break;
}
case FFX_API_QUERY_DESC_TYPE_UPSCALE_GET_RESOURCE_REQS: {
ffxQueryDescUpscaleGetResourceReqs *q = (ffxQueryDescUpscaleGetResourceReqs *)h;
q->required_resources = (1ULL << 0) | (1ULL << 1) | (1ULL << 2); /* COLOR | DEPTH | MV */
q->optional_resources = (1ULL << 3) | (1ULL << 4) | (1ULL << 5);
break;
}
default:
break;
}
}
return FFX_API_RETURN_OK;
}
static void fill_vk_img_ref(WlFsr4ImageRef *dst, const FfxApiResource *src, int is_depth) {
memset(dst, 0, sizeof *dst);
if (!src || !src->resource) return;
dst->vk_image = (uint64_t)(uintptr_t)src->resource;
dst->vk_format = ffx_fmt_to_vk(src->description.format);
dst->vk_layout = ffx_state_to_vk_layout(src->state, is_depth);
dst->width = src->description.width;
dst->height = src->description.height;
dst->aspect = is_depth ? 2u : 1u;
}
static void fill_dx12_img_ref(WlID3D12DXVKInteropDevice1 *interop, WlFsr4ImageRef *dst, const FfxApiResource *src, int is_depth) {
memset(dst, 0, sizeof *dst);
if (!interop || !src || !src->resource) return;
UINT64 vk_img = 0, off = 0;
UINT32 vk_fmt = 0;
if (wl_vkd3d_resource_info(interop, src->resource, &vk_img, &off, &vk_fmt) == S_OK && vk_img) {
dst->vk_image = vk_img;
dst->vk_format = vk_fmt ? vk_fmt : ffx_fmt_to_vk(src->description.format);
dst->vk_layout = ffx_state_to_vk_layout(src->state, is_depth);
dst->width = src->description.width;
dst->height = src->description.height;
dst->aspect = is_depth ? 2u : 1u;
}
}
__declspec(dllexport) ffxReturnCode_t ffxDispatch(ffxContext *context, const ffxApiHeader *desc) {
if (!context || !*context || !desc) return FFX_API_RETURN_ERROR_PARAMETER;
WlFfxUpscaleCtx *ctx = (WlFfxUpscaleCtx *)(*context);
for (const ffxApiHeader *h = desc; h; h = h->pNext) {
if (h->type != FFX_API_DISPATCH_DESC_TYPE_UPSCALE) continue;
const ffxDispatchDescUpscale *d = (const ffxDispatchDescUpscale *)h;
if (!d->commandList || !ctx->seam.ready) return FFX_API_RETURN_ERROR_RUNTIME_ERROR;
WlFsr4FrameDesc f;
memset(&f, 0, sizeof f);
f.render_w = d->renderSize.width ? d->renderSize.width : ctx->max_rw;
f.render_h = d->renderSize.height ? d->renderSize.height : ctx->max_rh;
f.output_w = d->upscaleSize.width ? d->upscaleSize.width : ctx->max_ow;
f.output_h = d->upscaleSize.height ? d->upscaleSize.height : ctx->max_oh;
f.jitter_x = d->jitterOffset.x;
f.jitter_y = d->jitterOffset.y;
f.mv_scale_x = d->motionVectorScale.x != 0.f ? d->motionVectorScale.x : 1.f;
f.mv_scale_y = d->motionVectorScale.y != 0.f ? d->motionVectorScale.y : 1.f;
f.pre_exposure = d->preExposure > 0.f ? d->preExposure : 1.f;
f.sharpness = d->sharpness;
f.frame_time_ms = d->frameTimeDelta;
f.camera_near = d->cameraNear;
f.camera_far = d->cameraFar;
f.camera_fov_y = d->cameraFovAngleVertical;
if (d->reset || ctx->needs_reset) {
f.flags |= WLFSR4_FLAG_RESET;
ctx->needs_reset = 0;
}
if (ctx->create_flags & (1u << 0)) f.flags |= WLFSR4_FLAG_HDR;
if (ctx->create_flags & (1u << 1)) f.flags |= WLFSR4_FLAG_DISPLAY_RES_MV;
if (ctx->create_flags & (1u << 2)) f.flags |= WLFSR4_FLAG_MV_JITTERED;
if (ctx->create_flags & (1u << 3)) f.flags |= WLFSR4_FLAG_DEPTH_INVERTED;
if (ctx->create_flags & (1u << 5)) f.flags |= WLFSR4_FLAG_AUTO_EXPOSURE;
if (d->enableSharpening) f.flags |= WLFSR4_FLAG_SHARPEN;
f.quality_mode = (g_last_ffx_quality != WLFSR4_QUALITY_AUTO)
? g_last_ffx_quality
: ffx_infer_quality(f.render_w, f.output_w);
if (f.quality_mode == WLFSR4_QUALITY_ULTRA_PERF) f.preset_hint = WLFSR4_PRESET_ULTRAPERF;
if (!ctx->is_dx12) {
f.api_source = WLFSR4_API_FFX_VK;
fill_vk_img_ref(&f.color, &d->color, 0);
fill_vk_img_ref(&f.depth, &d->depth, 1);
fill_vk_img_ref(&f.motion, &d->motionVectors, 0);
fill_vk_img_ref(&f.output, &d->output, 0);
fill_vk_img_ref(&f.reactive, &d->reactive, 0);
fill_vk_img_ref(&f.exposure, &d->exposure, 0);
if (!wl_seam_dispatch_or_blit(&ctx->seam, (VkCommandBuffer)d->commandList, &f))
return FFX_API_RETURN_ERROR_RUNTIME_ERROR;
} else if (ctx->dx12_interop) {
f.api_source = WLFSR4_API_FFX_DX12;
fill_dx12_img_ref(ctx->dx12_interop, &f.color, &d->color, 0);
fill_dx12_img_ref(ctx->dx12_interop, &f.depth, &d->depth, 1);
fill_dx12_img_ref(ctx->dx12_interop, &f.motion, &d->motionVectors, 0);
fill_dx12_img_ref(ctx->dx12_interop, &f.output, &d->output, 0);
fill_dx12_img_ref(ctx->dx12_interop, &f.reactive, &d->reactive, 0);
fill_dx12_img_ref(ctx->dx12_interop, &f.exposure, &d->exposure, 0);
int live_en = 1;
int live_backend = wl_seam_live_backend(&ctx->seam, &live_en);
if (live_en && live_backend == WL_FSR_BACKEND_XESS3_GPU && ctx->d3d12_dev) {
if (wl_xess3_eval_d3d12(&ctx->xess3, &ctx->seam, ctx->dx12_interop,
ctx->d3d12_dev, d->commandList, &f,
d->color.resource, d->depth.resource,
d->motionVectors.resource, d->output.resource)) {
continue;
}
wl_xess3_tag_fallback(&ctx->xess3, &f);
} else {
wl_xess3_on_inactive(&ctx->xess3);
}
void *vk_cb = NULL;
int ok = 0;
void *cl_used = NULL;
if (wl_vkd3d_begin_cb(ctx->dx12_interop, d->commandList, &vk_cb, &cl_used)) {
ok = wl_seam_dispatch_or_blit(&ctx->seam, (VkCommandBuffer)vk_cb, &f);
wl_vkd3d_end_cb(ctx->dx12_interop, d->commandList, cl_used);
}
if (!ok) return FFX_API_RETURN_ERROR_RUNTIME_ERROR;
}
}
return FFX_API_RETURN_OK;
}
BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved) {
(void)hinst; (void)reason; (void)reserved;
return TRUE;
}