The last of the Winlator lineage rewritten out; rumble on the phone

The five files the audit measured as Winlator's are replaced: a one-method callback interface by
java.util.function.Consumer, the component base by SessionPart (attach an application context,
start, stop), the environment list by HostEnvironment, the process starter by HostProcess, and
the pad's state holder by PadState with named sticks, triggers, directions and buttons. The
frame-generation tree is framegen/ (its contents were never Winlator's: lsfg-vk's port and
Bannerlator's Win-FG), the log tags are the app's, and the fake pad's rumble socket is now one
the app actually serves: RumbleComponent turns a game's force-feedback effects into the phone's
vibrator, which used to go nowhere. The two design notes describing Bannerlator's X11 path are
gone, and the README and credits say what is whose.
This commit is contained in:
The412Banner committed 2026-09-23 09:12:14 -04:00
1 parent 35dbef0536
commit a604dce6cb
84 files changed
+493 -1771

No files matched your search

@@ -0,0 +1,846 @@
#pragma once
#include <vulkan/vulkan.h>
#include <list>
#include <vulkan/vulkan_android.h>
struct VkTable {
PFN_vkCreateInstance CreateInstance;
PFN_vkDestroyInstance DestroyInstance;
PFN_vkEnumeratePhysicalDevices EnumeratePhysicalDevices;
PFN_vkGetPhysicalDeviceProperties GetPhysicalDeviceProperties;
PFN_vkGetPhysicalDeviceMemoryProperties GetPhysicalDeviceMemoryProperties;
PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR GetPhysicalDeviceSurfaceCapabilitiesKHR;
PFN_vkGetPhysicalDeviceSurfaceFormatsKHR GetPhysicalDeviceSurfaceFormatsKHR;
PFN_vkGetPhysicalDeviceSurfacePresentModesKHR GetPhysicalDeviceSurfacePresentModesKHR;
PFN_vkGetPhysicalDeviceQueueFamilyProperties GetPhysicalDeviceQueueFamilyProperties;
PFN_vkGetPhysicalDeviceSurfaceSupportKHR GetPhysicalDeviceSurfaceSupportKHR;
PFN_vkGetPhysicalDeviceFormatProperties GetPhysicalDeviceFormatProperties;
PFN_vkGetPhysicalDeviceFeatures2 GetPhysicalDeviceFeatures2;
PFN_vkCreateDevice CreateDevice;
PFN_vkDestroySurfaceKHR DestroySurfaceKHR;
PFN_vkCreateAndroidSurfaceKHR CreateAndroidSurfaceKHR;
PFN_vkGetDeviceProcAddr GetDeviceProcAddr;
PFN_vkDestroyDevice DestroyDevice;
PFN_vkGetDeviceQueue GetDeviceQueue;
PFN_vkDeviceWaitIdle DeviceWaitIdle;
PFN_vkCreateSwapchainKHR CreateSwapchainKHR;
PFN_vkDestroySwapchainKHR DestroySwapchainKHR;
PFN_vkGetSwapchainImagesKHR GetSwapchainImagesKHR;
PFN_vkAcquireNextImageKHR AcquireNextImageKHR;
PFN_vkQueuePresentKHR QueuePresentKHR;
PFN_vkQueueSubmit QueueSubmit;
PFN_vkCreateQueryPool CreateQueryPool;
PFN_vkDestroyQueryPool DestroyQueryPool;
PFN_vkCmdResetQueryPool CmdResetQueryPool;
PFN_vkCmdWriteTimestamp CmdWriteTimestamp;
PFN_vkGetQueryPoolResults GetQueryPoolResults;
PFN_vkCreateRenderPass CreateRenderPass;
PFN_vkDestroyRenderPass DestroyRenderPass;
PFN_vkCreateFramebuffer CreateFramebuffer;
PFN_vkDestroyFramebuffer DestroyFramebuffer;
PFN_vkCreateImageView CreateImageView;
PFN_vkDestroyImageView DestroyImageView;
PFN_vkCreateImage CreateImage;
PFN_vkDestroyImage DestroyImage;
PFN_vkCreateBuffer CreateBuffer;
PFN_vkDestroyBuffer DestroyBuffer;
PFN_vkAllocateMemory AllocateMemory;
PFN_vkFreeMemory FreeMemory;
PFN_vkMapMemory MapMemory;
PFN_vkFlushMappedMemoryRanges FlushMappedMemoryRanges;
PFN_vkBindBufferMemory BindBufferMemory;
PFN_vkBindImageMemory BindImageMemory;
PFN_vkGetBufferMemoryRequirements GetBufferMemoryRequirements;
PFN_vkGetImageMemoryRequirements GetImageMemoryRequirements;
PFN_vkCreateDescriptorSetLayout CreateDescriptorSetLayout;
PFN_vkDestroyDescriptorSetLayout DestroyDescriptorSetLayout;
PFN_vkCreateDescriptorPool CreateDescriptorPool;
PFN_vkDestroyDescriptorPool DestroyDescriptorPool;
PFN_vkAllocateDescriptorSets AllocateDescriptorSets;
PFN_vkFreeDescriptorSets FreeDescriptorSets;
PFN_vkUpdateDescriptorSets UpdateDescriptorSets;
PFN_vkCreatePipelineLayout CreatePipelineLayout;
PFN_vkDestroyPipelineLayout DestroyPipelineLayout;
PFN_vkCreateShaderModule CreateShaderModule;
PFN_vkDestroyShaderModule DestroyShaderModule;
PFN_vkCreateGraphicsPipelines CreateGraphicsPipelines;
PFN_vkDestroyPipeline DestroyPipeline;
PFN_vkCreateCommandPool CreateCommandPool;
PFN_vkDestroyCommandPool DestroyCommandPool;
PFN_vkAllocateCommandBuffers AllocateCommandBuffers;
PFN_vkFreeCommandBuffers FreeCommandBuffers;
PFN_vkBeginCommandBuffer BeginCommandBuffer;
PFN_vkEndCommandBuffer EndCommandBuffer;
PFN_vkResetCommandBuffer ResetCommandBuffer;
PFN_vkCmdBeginRenderPass CmdBeginRenderPass;
PFN_vkCmdEndRenderPass CmdEndRenderPass;
PFN_vkCmdBindPipeline CmdBindPipeline;
PFN_vkCmdBindDescriptorSets CmdBindDescriptorSets;
PFN_vkCmdDraw CmdDraw;
PFN_vkCmdPushConstants CmdPushConstants;
PFN_vkCmdSetViewport CmdSetViewport;
PFN_vkCmdSetScissor CmdSetScissor;
PFN_vkCmdPipelineBarrier CmdPipelineBarrier;
PFN_vkCmdCopyImage CmdCopyImage;
PFN_vkCmdBlitImage CmdBlitImage; // frame-gen capture resolution: composite -> swapchain upscale
// Compute: the native LSFG chain is 25 compute dispatches.
PFN_vkCmdDispatch CmdDispatch;
PFN_vkCreateComputePipelines CreateComputePipelines;
PFN_vkUnmapMemory UnmapMemory;
// win-fg native: clears its flow scratch, resets its scratch pool on resize.
PFN_vkCmdClearColorImage CmdClearColorImage;
PFN_vkResetDescriptorPool ResetDescriptorPool;
PFN_vkCmdCopyBufferToImage CmdCopyBufferToImage;
PFN_vkCreateSampler CreateSampler;
PFN_vkDestroySampler DestroySampler;
PFN_vkCreateSemaphore CreateSemaphore;
PFN_vkDestroySemaphore DestroySemaphore;
PFN_vkCreateFence CreateFence;
PFN_vkDestroyFence DestroyFence;
PFN_vkWaitForFences WaitForFences;
PFN_vkResetFences ResetFences;
PFN_vkGetFenceStatus GetFenceStatus;
PFN_vkGetAndroidHardwareBufferPropertiesANDROID GetAndroidHardwareBufferPropertiesANDROID;
};
#include <android/log.h>
#include <string>
#define WLOG_TAG "SteamDeck_Renderer"
#define RLOG(...) if(verboseLog) __android_log_print(ANDROID_LOG_DEBUG,WLOG_TAG,__VA_ARGS__)
#define RLOG_E(...) __android_log_print(ANDROID_LOG_ERROR,WLOG_TAG,__VA_ARGS__)
#define SCANOUT_LOG(...) __android_log_print(ANDROID_LOG_DEBUG,"SteamDeck_Scanout",__VA_ARGS__)
#include <vulkan/vulkan_android.h>
#include <android/hardware_buffer.h>
#include <android/native_window.h>
#include <vector>
#include <unordered_map>
#include <thread>
#include <atomic>
#include <mutex>
#include <shared_mutex>
#include <condition_variable>
#include <chrono>
// Renderer-neutral direct-scanout impl (owns the SurfaceControl/AHB state).
// Included after SCANOUT_LOG is defined above; its own definition is guarded.
#include "../scanout/ScanoutContext.h"
// Native (compositor-side) LSFG frame generation - capability gate.
#include "lsfg/lsfg_probe.h"
#include <memory>
#include <string>
namespace lsfg { class Engine; }
namespace winfg { class Engine; }
static constexpr uint32_t MAX_FRAMES_IN_FLIGHT = 2;
struct WindowPushConstants { float ndcX0, ndcY0, ndcX1, ndcY1; int useTexAlpha; };
// Push-constant layouts for the spatial-upscaler post passes. The leading vec4
// `ndc` matches upscale.vert; the remaining members are read only by the
// fragment stage. std430 offsets line up with these C structs.
struct SgsrPushConstants { // 36 bytes
float ndc[4];
float viewportInfo[4]; // xy = 1/inputSize, zw = inputSize px
float edgeSharpness; // SGSR EdgeSharpness, from the slider
};
struct NisPushConstants { // 36 bytes
float ndc[4];
float viewportInfo[4]; // xy = 1/inputSize, zw = inputSize px
float sharpness; // NIS sharpness [0..1], from the slider
};
struct EasuPushConstants { // 88 bytes
float ndc[4];
uint32_t con0[4], con1[4], con2[4], con3[4];
float outW, outH;
};
struct RcasPushConstants { // 40 bytes
float ndc[4];
uint32_t con[4]; // con.x = bit-packed sharpness
float outW, outH;
};
struct DownscalePushConstants { // 32 bytes
float ndc[4];
float srcW, srcH; // render (source) resolution
float dstW, dstH; // display (output) resolution
};
// Composable post effects (AMD CAS sharpen + fake-HDR). Both lead with vec4 ndc
// (offset 0) like the upscaler PCs and stay well under the 88-byte PC range.
struct CasPushConstants { // 32 bytes
float ndc[4];
float resolution[2]; // input texture size in px
float sharpness; // CAS SHARPNESS term [0..1]
float _pad;
};
struct HdrPushConstants { // 24 bytes
float ndc[4];
float resolution[2]; // input texture size in px
};
// Phase 2 screen effects ported from the GL EffectComposer. Each leads with vec4
// ndc (offset 0) like the upscaler PCs and stays well under the 88-byte PC range.
struct FxaaPushConstants { // 24 bytes
float ndc[4];
float resolution[2]; // input texture size in px
};
struct ToonPushConstants { // 24 bytes
float ndc[4];
float resolution[2]; // input texture size in px
};
struct ColorPushConstants { // 32 bytes
float ndc[4];
float brightness; // additive [-1,1]
float contrast; // [0,2]
float gamma; // [0.1,5]
float saturation; // [0,2]; 1 = unchanged, 0 = greyscale.
// APPENDED last so the three offsets above
// stay where color.frag already had them.
};
struct NtscPushConstants { // 28 bytes
float ndc[4];
float resolution[2]; // screen size in px (TextureSize/resolution)
float frameCount; // animated chroma-phase counter
};
struct CrtPushConstants { // 24 bytes
float ndc[4];
float resolution[2]; // input texture size in px (unused by shader)
};
struct DebandPushConstants { // 28 bytes
float ndc[4];
float resolution[2]; // input texture size in px (layout parity)
float strength; // dither amplitude in LSBs (1.0 = +/-1/255)
};
class VulkanRendererContext {
public:
// `lsfgVk11Compat` (experimental) lets the LSFG capability probe accept a
// Vulkan 1.1/1.2 device that offers the extensions the chain needs; it has
// to be known before vkCreateDevice, hence a constructor argument.
VulkanRendererContext(ANativeWindow* window, int cWidth, int cHeight, void* adrenotoolsHandle = nullptr,
bool lsfgVk11Compat = false);
~VulkanRendererContext();
void onSurfaceResized(int width, int height);
void setTransform(float ox, float oy, float sx, float sy);
// #413: game-region clip rect (surface px). Stored and applied as the swapchain scissor in
// recordCmdBuf so a FILL/STRETCH game confined to a half (TOP/BOTTOM alignment) can't bleed into
// the on-screen-controls half. A full-surface (or w<=0) rect means "no clip" (CENTER).
void setClipRegion(int x, int y, int w, int h);
void updatePointerPosition(short x, short y);
void updateWindowContent(int64_t id, void* pixels, short w, short h, short stride, int x, int y);
void updateWindowContentAHB(int64_t id, AHardwareBuffer* ahb, short w, short h, int x, int y);
void updateCursorImage(void* pixels, short w, short h, short hotX, short hotY);
void setCursorVisible(bool visible);
void setRenderList(const int64_t* ids, const int* xs, const int* ys, int count);
void removeWindow(int64_t id);
void clearBackbuffer() {}
void beginBatch() {}
void endBatch() {}
void initScanout();
void destroyScanout();
void applyScanoutBuffer();
void initScanoutFromWindows(ANativeWindow* gameWin, ANativeWindow* cursorWin);
void scanoutSetDst(int x, int y, int w, int h);
void scanoutSetBuffer(AHardwareBuffer* ahb, int x, int y, int w, int h, int fenceFd = -1);
void scanoutSetCursorImage(void* pixels, short w, short h, short stride);
void scanoutSetCursorPos(short x, short y, short hotX, short hotY);
// The renderer-neutral scanout implementation. The scanout methods above are
// thin forwarders to this (see VulkanRendererScanout.cpp); the cursor render-
// thread signalling stays in the forwarders, not in ScanoutContext.
ScanoutContext scanout;
// Bound to scanout's atomics so the existing JNI ABI (r->scanoutActive /
// r->gameFrameDelivered direct member access) keeps compiling against a
// single source of truth. Must be declared after `scanout`.
std::atomic<bool>& scanoutActive = scanout.scanoutActive;
std::atomic<bool>& gameFrameDelivered = scanout.gameFrameDelivered;
std::atomic<bool> surfaceDetached{false};
void detachSurface();
bool reattachSurface(ANativeWindow* newWindow);
bool verboseLog = true;
void setVerboseLog(bool v) { verboseLog = v; scanout.setVerboseLog(v); }
void dumpRendererInfo();
std::string adrenoDriverPath;
std::string adrenoDriverName;
std::string adrenoNativeLibDir;
void* vulkanHandle = nullptr;
std::atomic<bool> scanoutBlackFrameDone{false};
PFN_vkGetInstanceProcAddr gipa = nullptr;
VkTable vk_ = {};
void loadCustomDriver();
void loadInstanceDispatch();
void loadDeviceDispatch();
void setFilterMode(int mode);
void setUpscaler(int mode);
void setHqDownscale(bool enabled);
void setCas(bool enabled, int sharpness);
void setHdr(bool enabled);
void setDeband(bool enabled, int strength);
void setUpscaleSharpness(int sharpness);
void setFxaa(bool enabled);
void setToon(bool enabled);
void setCrt(bool enabled);
void setNtsc(bool enabled);
void setColorGrade(float brightness, float contrast, float gamma, float saturation);
void setSwapRB(bool enabled);
void setPresentMode(VkPresentModeKHR mode);
std::vector<int> getSupportedPresentModes() const;
// --- Native LSFG frame generation -------------------------------------
// Capability verdict for the compositor-side LSFG engine. Filled at device
// creation and completed once the swapchain format is known; read by the
// UI (through JNI) to grey the engine out with a reason.
const lsfg::Caps& lsfgCaps() const { return lsfgCaps_; }
// Arm/disarm native LSFG frame generation for this session. Changing the
// armed state recreates the swapchain, because the composite path needs
// TRANSFER_DST usage and a deeper image queue that a normal session does
// not pay for.
void setFrameGenArmed(bool armed, int multiplier);
bool frameGenArmed() const { return fgArmed_.load(std::memory_order_relaxed); }
// Live frame-gen telemetry for the in-game readout:
// [0] generations the governor currently trusts
// [1] generations actually planned for the last source frame
// [2] measured source (real) frames per second
// [3] measured presented frames per second
// [4] thermal status, -1 when the device gives no signal
// [5] GPU milliseconds the chain spends per generated frame, -1 unknown
void frameGenStats(float out[6]) const;
// Why the selected native frame-gen engine cannot run here, for the UI:
// -1 not known yet (caps not probed), 0 fine, 1 this driver lacks what the
// engine needs (lsfgCaps().reason says which gate), 2 the engine failed to start.
int frameGenProblem() const;
// Path to the SPIR-V cache built from the user's Lossless.dll. Setting it
// drops any existing engine so the next armed frame rebuilds from it.
void setLsfgCachePath(const char* path);
// Flow scale (0.25-1.0) and the panel's real refresh rate. The pacer never
// generates above the refresh rate.
// Experimental: `captureHeight` sizes the composite ring the chain runs on,
// the width following the swapchain's aspect, so a phone GPU generates at
// game-like resolution and the result is blitted up. 0 = panel;
// kFgCaptureGame = the X screen's height (containerHeight), which already
// carries the per-game override and render scale; otherwise a pixel height.
static constexpr int32_t kFgCaptureGame = -1;
void setFrameGenTuning(float flowScale, float refreshHz, int32_t captureHeight = 0);
// Which native engine generates: 0 = LSFG (needs the cache built from the
// user's Lossless.dll), 1 = win-fg (our own chain, embedded, needs nothing).
// Switching drops the other engine so only one ever holds GPU resources.
void setFrameGenEngine(int kind);
// win-fg only: interpolation model (3/4) and performance preset (0..2).
void setWinFgTuning(int model, int perfPreset);
private:
struct WinTex {
VkImage img = VK_NULL_HANDLE;
VkDeviceMemory mem = VK_NULL_HANDLE;
VkImageView view = VK_NULL_HANDLE;
VkDescriptorSet ds = VK_NULL_HANDLE;
VkBuffer stg = VK_NULL_HANDLE;
VkDeviceMemory stgMem = VK_NULL_HANDLE;
void* mapped = nullptr;
VkDeviceSize cap = 0;
int w = 0;
int h = 0;
bool dirty = false;
bool isAHB = false;
bool needsTransition = false;
AHardwareBuffer* ahb = nullptr;
};
struct RenderEntry { int64_t id; int x, y; };
struct DrawEntry {
VkImage img = VK_NULL_HANDLE;
VkDescriptorSet ds = VK_NULL_HANDLE;
VkBuffer upload = VK_NULL_HANDLE;
int x=0, y=0, w=0, h=0;
bool needsTransition = false;
bool isAHB = false;
};
ANativeWindow* window;
int surfaceWidth, surfaceHeight, containerWidth, containerHeight;
// #413 compositor clip rect (surface px). clipRegionW<=0 => disabled (whole swapchain). Written from
// the renderer thread (setClipRegion), read from the render thread (recordCmdBuf) -> atomic.
std::atomic<int> clipRegionX{0}, clipRegionY{0}, clipRegionW{0}, clipRegionH{0};
void* adrenotoolsHandle = nullptr;
bool lsfgVk11Compat_ = false; // see the constructor
int filterMode = 0;
bool swapRB = false;
float maxAnisotropy = 1.0f;
bool cubicSupported = false;
VkPhysicalDeviceMemoryProperties memProperties{};
VkPresentModeKHR requestedPresentMode = VK_PRESENT_MODE_FIFO_KHR;
uint32_t graphicsQueueFamilyIndex = 0;
std::vector<VkPresentModeKHR> availablePresentModes;
std::unordered_map<int64_t, WinTex> texMap;
std::unordered_map<AHardwareBuffer*, WinTex> ahbImportCache;
std::unordered_map<int64_t, std::vector<AHardwareBuffer*>> windowAhbs;
std::vector<WinTex> deleteQueue;
std::vector<RenderEntry> renderList;
std::vector<DrawEntry> frameDraws;
std::vector<VkImageMemoryBarrier> frameAhbTransitions;
std::vector<VkImageMemoryBarrier> framePreUpload;
std::vector<VkImageMemoryBarrier> framePostUpload;
// [P0] All SurfaceControl / AHardwareBuffer scanout state moved to
// ScanoutContext (member `scanout` above). Dead members (lastDst*,
// ScanoutPending/scanoutPending*, fnSTSetBackPressure) were dropped.
std::atomic<int> pointerX{0}, pointerY{0};
float sceneOffsetX=0.f, sceneOffsetY=0.f, sceneScaleX=1.f, sceneScaleY=1.f;
std::atomic<bool> cursorVisible{false};
short cursorHotX=0, cursorHotY=0, cursorTexW=0, cursorTexH=0;
std::vector<uint32_t> cursorPixels;
std::atomic<bool> isCursorImageDirty{false};
std::atomic<bool> cursorMoved{false};
VkImage cursorImg = VK_NULL_HANDLE;
VkDeviceMemory cursorMem = VK_NULL_HANDLE;
VkImageView cursorView = VK_NULL_HANDLE;
VkDescriptorSet cursorDS = VK_NULL_HANDLE;
VkBuffer cursorStg = VK_NULL_HANDLE;
VkDeviceMemory cursorStgM = VK_NULL_HANDLE;
void* cursorStgP = nullptr;
VkDeviceSize cursorStgC = 0;
VkDeviceSize cursorUploadSize = 0;
VkInstance instance;
VkSurfaceKHR surface;
VkPhysicalDevice physicalDevice;
VkDevice device;
VkQueue graphicsQueue;
VkSwapchainKHR swapchain = VK_NULL_HANDLE;
VkFormat swapchainFmt;
VkExtent2D swapchainExt;
std::vector<VkImage> swapchainImages;
std::vector<VkImageView> swapchainViews;
std::vector<VkFramebuffer> swapchainFBs;
VkRenderPass renderPass = VK_NULL_HANDLE;
VkDescriptorSetLayout dsLayout = VK_NULL_HANDLE;
VkPipelineLayout pipeLayout = VK_NULL_HANDLE;
VkPipeline pipeline = VK_NULL_HANDLE;
// ---- Spatial upscaler (SGSR / FSR1) ----
// upscalerMode enum (mirrored by JNI nativeSetUpscaler):
// 0 = none (passthrough; current sampler governs scaling)
// 1 = linear (rebuild base sampler LINEAR; no shader pass)
// 2 = nearest (rebuild base sampler NEAREST; no shader pass)
// 3 = sgsr (Snapdragon GSR 1.0; single pass; aspect-fit / letterbox)
// 4 = fsr (AMD FSR1 EASU+RCAS; two passes; fill / stretch)
// 5 = fsr_fit (AMD FSR1 EASU+RCAS; two passes; aspect-fit / letterbox)
// 6 = sharpen (RCAS-only; any resolution; aspect-fit / letterbox)
// 7 = nis (NVIDIA Image Scaling NVScaler; single pass; aspect-fit)
// 8 = sgsr_quality (SGSR 1 edge-direction variant; single pass; aspect-fit)
// Shader upscaling only engages for modes 3-8 AND when the game render
// resolution (container) is smaller than the swapchain. Otherwise the
// existing direct-to-swapchain path is used unchanged.
int upscalerMode = 0;
// Independent of upscalerMode: high-quality Lanczos downscale for the
// supersampling case (game render res > display res). When enabled and the
// render res exceeds the swapchain, replaces the bilinear minify with a
// proper downscale pass. Supersampling and the upscale modes are mutually
// exclusive in practice (one is render>display, the other render<display).
bool hqDownscale = false;
// Composable post effects, independent of the scaling mode. CAS layers a sharpen
// on top of any mode (incl. native res); HDR is a binary fake-HDR. The upscaler
// sharpness (RCAS lobe scale / SGSR EdgeSharpness) is driven by its own slider as a
// linear 0..1 value: 0 = neutral (no sharpening; the upscale still runs), 1 = max.
bool casEnabled = false;
int casSharpness = 60; // slider 0..100 -> CAS SHARPNESS
bool hdrEnabled = false;
float upscaleSharpness01 = 0.75f; // slider/100; RCAS lobe scale, SGSR edge derived
// Debanding: terminal TPDF/IGN dither pass before the 8-bit swapchain quantize.
bool debandEnabled = false;
int debandStrength = 100; // slider 0..200 -> strength/100 LSBs
// Phase 2 screen effects (GL EffectComposer parity). FXAA/Toon/CRT/NTSC are
// binary; Color is the brightness/contrast/gamma grade (always-applied via the
// sliders, no toggle) and is "enabled" only when not at neutral (0,0,1).
bool fxaaEnabled = false;
bool toonEnabled = false;
bool crtEnabled = false;
bool ntscEnabled = false;
bool colorEnabled = false; // derived: !neutral grade
float colorBrightness = 0.0f; // [-1,1] (slider/100, clamped)
float colorContrast = 0.0f; // [0,2] (slider/100, clamped)
float colorGamma = 1.0f; // [0.1,5]
float colorSaturation = 1.0f; // [0,2] (slider/100, clamped)
uint32_t ntscFrameCounter = 0; // animates NTSC chroma phase
VkSampler upscaleSampler = VK_NULL_HANDLE; // linear clamp; offscreen/mid input
// === Native LSFG: composite target ring ===================================
// Frame generation cannot composite straight into a swapchain image: the
// finished frame has to be READABLE (it becomes the next frame's LSFG
// input) and generated frames have to be STORAGE-WRITABLE by a compute
// dispatch. Android swapchain images are COLOR_ATTACHMENT only, and
// storage support on a swapchain format is not something a driver owes us.
//
// So when frame gen is armed the whole existing recording is redirected at
// a composite image we own - format-identical to the swapchain, so every
// existing pipeline stays render-pass compatible - and a copy moves it into
// the acquired swapchain image at the end. With frame gen off, not one of
// these objects is created and the direct-to-swapchain path is untouched.
struct CompositeTarget {
VkImage img = VK_NULL_HANDLE;
VkDeviceMemory mem = VK_NULL_HANDLE;
VkImageView view = VK_NULL_HANDLE; // colour attachment + sampled
VkImageView storageView = VK_NULL_HANDLE; // compute writes (generate)
VkFramebuffer fb = VK_NULL_HANDLE;
VkDescriptorSet ds = VK_NULL_HANDLE; // sampled, for later passes
};
// Hard ceiling: (max generations + 1) presentable frames per source frame,
// times a queue depth of 2, capped so the footprint stays bounded.
static constexpr uint32_t kMaxCompositeTargets = 7;
std::vector<CompositeTarget> compositeTargets;
VkRenderPass compositeRenderPass = VK_NULL_HANDLE; // CLEAR -> GENERAL
uint32_t compositeW = 0, compositeH = 0;
uint32_t compositeIndex = 0; // rotates per composite; gives history for free
bool compositeArmed = false; // targets exist AND this frame uses them
bool swapchainTransferDst = false; // swapchain was created with TRANSFER_DST
// Set from the app when the native LSFG engine is selected for this
// session. Read on the render thread; false keeps every path as it was.
std::atomic<bool> fgArmed_{false};
std::atomic<int> fgMultiplier_{0};
// Tuning is written by the UI thread and consumed by the render thread, so
// it is published through atomics and applied to the engine at the top of
// the frame-gen block. The UI thread never touches the engine itself.
std::atomic<float> fgFlowScale_{1.0f};
std::atomic<float> fgRefreshHz_{0.0f};
std::atomic<bool> fgConfigDirty_{true};
// Experimental (see setFrameGenTuning): capture height of the composite
// ring. 0 = panel resolution, kFgCaptureGame = the X screen's height.
std::atomic<int32_t> fgCaptureHeight_{0};
// Extent the composite ring should have for the current swapchain and
// capture height (width follows the swapchain aspect, both even).
void compositeExtentFor(uint32_t& w, uint32_t& h) const;
// The extent the scene is rendered at this frame: the composite ring while
// frame gen runs on a ring smaller than the swapchain (experimental capture
// resolution), otherwise the swapchain. Every full-target pass (the effect
// chain, the spatial upscalers, the render-scale downscale) sizes itself by
// this, so those keep working below panel resolution and their
// intermediates shrink with the ring.
VkExtent2D renderExtent() const {
if (compositeActive() && (compositeW != swapchainExt.width || compositeH != swapchainExt.height))
return VkExtent2D{compositeW, compositeH};
return swapchainExt;
}
// Copy (same extent) or blit (composite smaller than the swapchain) a
// composite-ring image, already in TRANSFER_SRC, into a swapchain image
// already in TRANSFER_DST.
void recordCompositeToSwapchainTransfer(VkCommandBuffer cb, VkImage src, uint32_t imgIdx);
bool createCompositeRenderPass();
bool ensureCompositeTargets(uint32_t w, uint32_t h, uint32_t count);
void destroyCompositeTargets();
// True when this frame should composite off-swapchain. Call on the render
// thread; every gate must hold or we fall back to the untouched path.
bool compositeActive() const;
// Render pass / framebuffer this frame draws its FINAL pass into.
VkRenderPass targetRenderPass() const;
VkFramebuffer targetFramebuffer(uint32_t imgIdx) const;
// Copy the finished composite into the acquired swapchain image and leave
// it in PRESENT_SRC. No-op when the composite path is not active.
void copyCompositeToSwapchain(VkCommandBuffer cb, uint32_t imgIdx);
// === Native LSFG: the software cursor ====================================
// LSFG interpolates whatever it is given, so a cursor composited into the
// frame gets warped along the flow field and smears. It is therefore
// excluded from the composite while frame gen is armed and drawn once into
// EVERY presented image instead - real and generated alike - through a
// load-op render pass that leaves the image ready to present.
VkRenderPass cursorOverlayRenderPass = VK_NULL_HANDLE;
struct CursorOverlay {
bool draw = false;
float ox = 0, oy = 0, sx = 0, sy = 0, cw = 0, ch = 0;
short ptrX = 0, ptrY = 0, hotX = 0, hotY = 0, w = 0, h = 0;
};
CursorOverlay cursorOverlay_{};
bool createCursorOverlayRenderPass();
// Draw the cursor into an already-copied swapchain image and leave it in
// PRESENT_SRC. Takes over the final transition from the copy helpers.
void recordCursorOverlay(VkCommandBuffer cb, uint32_t imgIdx);
// True when the cursor is being drawn per present rather than composited.
bool cursorDrawnPerPresent() const;
// === Native LSFG: the per-source-frame present plan =======================
// Interpolation produces frames that belong BETWEEN N-1 and N, so the
// generated frames are presented FIRST and real frame N is held back one
// slot. All presents for one source frame are queued together: with FIFO
// the driver then shows them on consecutive vblanks, so the pacing falls
// out of the present mode for free - no sleeps, no render-mode change.
static constexpr uint32_t kMaxPresentsPerFrame = 4; // 1 real + up to 3 generated
struct FrameGenPlan {
uint32_t generations = 0;
uint32_t presents = 1;
uint32_t imgIdx[kMaxPresentsPerFrame] = {};
};
FrameGenPlan fgPlan_{};
std::unique_ptr<lsfg::Engine> lsfgEngine_;
uint64_t fgSourceFrames_ = 0;
std::string lsfgCachePath_;
bool lsfgEngineTried_ = false;
std::unique_ptr<winfg::Engine> winfgEngine_;
bool winfgEngineTried_ = false;
std::atomic<int> fgEngineKind_{0}; // 0 = lsfg, 1 = win-fg
std::atomic<int> fgModel_{4};
std::atomic<int> fgPerfPreset_{1};
bool ensureWinFgEngine();
// Capability gate for the SELECTED engine. win-fg's shaders need only a
// storage-capable swapchain format; the LSFG chain also needs the fp16 /
// memory-model feature set, which some otherwise capable GPUs lack.
bool fgCapsOk() const;
// Sync objects are indexed per PRESENT, not per composite: each pending
// present needs its own image-available and render-finished semaphore.
uint32_t syncSlot(uint32_t k) const { return currentFrame * kMaxPresentsPerFrame + k; }
// Destroy and rebuild every acquire/present semaphore. Called with the
// swapchain, so no stale pending signal can survive into the new one.
void recreateSyncObjects();
uint32_t fgAcquireFailLog_ = 0;
// Measured PRESENTS per second - the rate that actually reaches the panel,
// counting generated frames. The pacer's own "loop rate" cannot serve this
// purpose: it is sampled once per SOURCE frame, so it always equals the
// guest rate and contains no evidence that generation happened at all.
float fgPresentedRate_ = 0.0f;
// Measured SOURCE frames per second over the same window, for engines that
// do not measure it themselves (win-fg).
float fgSourceRate_ = 0.0f;
uint32_t fgSourceAccum_ = 0;
uint32_t fgPresentAccum_ = 0;
std::chrono::steady_clock::time_point fgRateWindowStart_{};
bool fgRateWindowOpen_ = false;
void trackPresentedRate(uint32_t presents);
bool ensureLsfgEngine();
// One command buffer per pending present: slot 0 carries the composite,
// the chain's shared passes and generated frame 0; each later generated
// frame and the real frame are recorded and submitted on their own, so a
// finished frame reaches the presentation engine without waiting for the
// rest of the chain.
uint32_t cmdSlot(uint32_t k) const { return currentFrame * kMaxPresentsPerFrame + k; }
// Shared chain passes (the 24 shaders every generated frame depends on).
void recordFrameGenProcess(VkCommandBuffer cb);
// Generated frame g: synthesise into a spare composite target and copy it
// into the swapchain image reserved for it.
void recordFrameGenGeneration(VkCommandBuffer cb, uint32_t g);
// Chain cost: a timestamp pair per frame slot, start before the shared
// passes, end after the last generation's compute (before its copy, so a
// vblank wait on the swapchain image is not counted). Read back after the
// slot's fence wait, one frame later.
VkQueryPool fgQueryPool_ = VK_NULL_HANDLE;
bool fgTimestampsOk_ = false;
float fgTimestampPeriodNs_ = 0.0f;
bool fgQueryPending_[MAX_FRAMES_IN_FLIGHT] = {};
uint32_t fgQueryGens_[MAX_FRAMES_IN_FLIGHT] = {};
float fgChainMsPerGen_ = -1.0f;
uint32_t fgChainLogCount_ = 0;
void ensureFgQueryPool();
void destroyFgQueryPool();
void readFgQueryResult();
VkFormat offscreenFmt = VK_FORMAT_R8G8B8A8_UNORM;
VkRenderPass offscreenRenderPass = VK_NULL_HANDLE; // CLEAR -> SHADER_READ_ONLY
VkPipelineLayout postPipeLayout = VK_NULL_HANDLE;
VkPipeline sgsrPipeline = VK_NULL_HANDLE;
VkPipeline nisPipeline = VK_NULL_HANDLE; // NVIDIA Image Scaling (mode 7)
VkPipeline sgsrQualityPipeline = VK_NULL_HANDLE; // SGSR 1 edge-direction (mode 8)
VkPipeline easuPipeline = VK_NULL_HANDLE;
VkPipeline rcasPipeline = VK_NULL_HANDLE;
VkPipeline downscalePipeline = VK_NULL_HANDLE;
// CAS/HDR can be either a non-final pass (writes an fx target via
// offscreenRenderPass) or the final pass (writes the swapchain via renderPass);
// a pipeline is render-pass-specific, so keep one variant of each.
VkPipeline casPipelineOff = VK_NULL_HANDLE; // -> fx target (offscreenRenderPass)
VkPipeline casPipelineSwap = VK_NULL_HANDLE; // -> swapchain (renderPass)
VkPipeline hdrPipelineOff = VK_NULL_HANDLE;
VkPipeline hdrPipelineSwap = VK_NULL_HANDLE;
// Phase 2 effects: same Off (-> fx target) / Swap (-> swapchain) variant pair.
VkPipeline fxaaPipelineOff = VK_NULL_HANDLE;
VkPipeline fxaaPipelineSwap = VK_NULL_HANDLE;
VkPipeline toonPipelineOff = VK_NULL_HANDLE;
VkPipeline toonPipelineSwap = VK_NULL_HANDLE;
VkPipeline colorPipelineOff = VK_NULL_HANDLE;
VkPipeline colorPipelineSwap = VK_NULL_HANDLE;
VkPipeline ntscPipelineOff = VK_NULL_HANDLE;
VkPipeline ntscPipelineSwap = VK_NULL_HANDLE;
VkPipeline crtPipelineOff = VK_NULL_HANDLE;
VkPipeline crtPipelineSwap = VK_NULL_HANDLE;
// Debanding is always the LAST effect (terminal) -> only a swapchain variant.
VkPipeline debandPipelineSwap = VK_NULL_HANDLE;
// offscreen composite target @ game/container resolution
VkImage offscreenImg = VK_NULL_HANDLE;
VkDeviceMemory offscreenMem = VK_NULL_HANDLE;
VkImageView offscreenView = VK_NULL_HANDLE;
VkFramebuffer offscreenFB = VK_NULL_HANDLE;
VkDescriptorSet offscreenDS = VK_NULL_HANDLE;
int offscreenW = 0, offscreenH = 0;
// FSR intermediate (EASU output) @ upscale output resolution
VkImage midImg = VK_NULL_HANDLE;
VkDeviceMemory midMem = VK_NULL_HANDLE;
VkImageView midView = VK_NULL_HANDLE;
VkFramebuffer midFB = VK_NULL_HANDLE;
VkDescriptorSet midDS = VK_NULL_HANDLE;
int midW = 0, midH = 0;
// Effect-chain intermediates @ swapchain resolution. fx1 holds the scaled (or
// composited) image fed into CAS/HDR; fx2 ping-pongs when both effects run.
VkImage fx1Img = VK_NULL_HANDLE;
VkDeviceMemory fx1Mem = VK_NULL_HANDLE;
VkImageView fx1View = VK_NULL_HANDLE;
VkFramebuffer fx1FB = VK_NULL_HANDLE;
VkDescriptorSet fx1DS = VK_NULL_HANDLE;
int fx1W = 0, fx1H = 0;
VkImage fx2Img = VK_NULL_HANDLE;
VkDeviceMemory fx2Mem = VK_NULL_HANDLE;
VkImageView fx2View = VK_NULL_HANDLE;
VkFramebuffer fx2FB = VK_NULL_HANDLE;
VkDescriptorSet fx2DS = VK_NULL_HANDLE;
int fx2W = 0, fx2H = 0;
// Per-frame upscale plan, computed in renderFrame, consumed by recordCmdBuf.
// upFrame.mode reuses the upscalerMode enum (3=sgsr,4=fsr,5=fsr_fit,6=sharpen,7=nis,8=sgsr_quality)
// plus an internal sentinel (UPMODE_DOWNSCALE) for the supersampling path.
struct UpscaleFrame {
bool active = false;
int mode = 0;
int outX = 0, outY = 0, outW = 0, outH = 0;
bool cas = false; // run CAS this frame (snapshot of casEnabled)
bool hdr = false; // run HDR this frame (snapshot of hdrEnabled)
bool fxaa = false; // snapshot of fxaaEnabled
bool toon = false; // snapshot of toonEnabled
bool color = false; // snapshot of colorEnabled (non-neutral grade)
bool ntsc = false; // snapshot of ntscEnabled
bool crt = false; // snapshot of crtEnabled
bool deband = false;// snapshot of debandEnabled (terminal dither)
SgsrPushConstants sgsrPC{};
NisPushConstants nisPC{};
EasuPushConstants easuPC{};
RcasPushConstants rcasPC{};
DownscalePushConstants dsPC{};
CasPushConstants casPC{};
HdrPushConstants hdrPC{};
FxaaPushConstants fxaaPC{};
ToonPushConstants toonPC{};
ColorPushConstants colorPC{};
NtscPushConstants ntscPC{};
CrtPushConstants crtPC{};
DebandPushConstants debandPC{};
} upFrame;
VkCommandPool cmdPool = VK_NULL_HANDLE;
std::vector<VkCommandBuffer> cmdBufs;
std::vector<VkSemaphore> imgAvailSems;
std::vector<VkSemaphore> renderDoneSems;
std::vector<VkFence> inFlightFences;
std::vector<VkFence> imgInFlight;
uint32_t currentFrame = 0;
VkSampler sampler = VK_NULL_HANDLE;
VkDescriptorPool winTexPool = VK_NULL_HANDLE;
std::atomic<bool> needsRender{false};
std::thread renderThread;
std::atomic<bool> isRunning{false};
std::atomic<bool> fbResized{false};
std::mutex renderMutex;
std::mutex dirtyMutex;
std::condition_variable dirtyCV;
std::shared_mutex frameMutex;
void createInstance();
void createSurface();
void pickPhysicalDevice();
lsfg::Caps lsfgCaps_{};
void createLogicalDevice();
void createSwapchain();
void createRenderPass();
void createDSLayout();
void createPipeline(bool blend, VkPipeline& out);
void createFramebuffers();
void createCmdPool();
void createSampler();
void createUpscaleSampler();
void createOffscreenRenderPass();
void createPostPipelines();
VkPipeline createPostPipeline(const uint32_t* fragCode, size_t fragSz, VkRenderPass rp);
bool createColorTarget(int w, int h, VkImage& img, VkDeviceMemory& mem,
VkImageView& view, VkFramebuffer& fb, VkDescriptorSet& ds);
void destroyColorTarget(VkImage& img, VkDeviceMemory& mem, VkImageView& view,
VkFramebuffer& fb, VkDescriptorSet& ds);
bool ensureOffscreen(int w, int h);
bool ensureMid(int w, int h);
bool ensureFx1(int w, int h);
bool ensureFx2(int w, int h);
void recordUpscalePasses(VkCommandBuffer cb, uint32_t imgIdx,
const std::vector<DrawEntry>& draws, bool cursorDrawn,
short ptrX, short ptrY, short curHotX, short curHotY,
short curW, short curH,
float ox, float oy, float sx, float sy,
float cw, float ch);
void planUpscaleFrame();
void createWinTexPool();
void createCursorPipeline();
void createCursorDS();
void createCmdBufs();
void createSyncObjects();
void cleanupSwapchain();
bool createWinTexResources(WinTex& wt, int w, int h);
bool importAHBToWinTex(WinTex& wt, AHardwareBuffer* ahb);
void cleanupAllAHBCache();
void flushDeleteQueue();
void destroyWinTex(WinTex& wt);
void ensureCursorTex(short w, short h);
void cleanupCursorTex();
void ensureCursorStaging(VkDeviceSize sz);
void recordCmdBuf(VkCommandBuffer cb, uint32_t imgIdx,
const std::vector<DrawEntry>& draws,
std::vector<VkImageMemoryBarrier>& ahbTransitions,
std::vector<VkImageMemoryBarrier>& preUpload,
std::vector<VkImageMemoryBarrier>& postUpload,
VkBuffer cursorUpload, bool hasCursorUpload,
float ox, float oy, float sx, float sy, float cw, float ch,
short ptrX, short ptrY, short curHotX, short curHotY,
short curW, short curH, bool curVis);
void renderLoop();
void renderFrame();
uint32_t findMemType(uint32_t filter, VkMemoryPropertyFlags props);
void createBuffer(VkDeviceSize sz, VkBufferUsageFlags usage,
VkMemoryPropertyFlags props, VkBuffer& buf, VkDeviceMemory& mem);
VkCommandBuffer beginOneTime();
void endOneTime(VkCommandBuffer cmd);
void transition(VkCommandBuffer cmd, VkImage img,
VkImageLayout oldL, VkImageLayout newL,
VkAccessFlags srcA, VkAccessFlags dstA,
VkPipelineStageFlags srcS, VkPipelineStageFlags dstS);
VkShaderModule makeShader(const uint32_t* code, size_t sz);
};
@@ -0,0 +1,157 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2025 lsfg-vk
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Ported into Bannerlator's compositor from WinNative (GPL-3.0-or-later),
// whose LSFG port is credited to Camille LaVey / the Eden Emulator Project and
// follows upstream lsfg-vk. Only the Vulkan dispatch differs: Bannerlator
// resolves entry points through the renderer's own table (see lsfg_vkd.h).
#include "lsfg_alpha.hpp"
#include "lsfg_shaders.hpp"
#include <vector>
namespace lsfg {
namespace {
constexpr uint32_t DISPATCH_TILE_SHIFT = 3;
[[nodiscard]] uint32_t GroupCount(uint32_t size) {
return (size + (1u << DISPATCH_TILE_SHIFT) - 1) >> DISPATCH_TILE_SHIFT;
}
[[nodiscard]] VkExtent2D HalveExtent(VkExtent2D extent) {
return VkExtent2D{
(extent.width + 1) >> 1,
(extent.height + 1) >> 1,
};
}
}
LsfgAlphaPasses::LsfgAlphaPasses(const Device& device, const LsfgShaders& shaders) {
passes[0] = LsfgPass(device, shaders, LSFG_ALPHA_SHADERS[0],
{{1, VK_DESCRIPTOR_TYPE_SAMPLER},
{1, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE},
{1, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE}});
passes[1] = LsfgPass(device, shaders, LSFG_ALPHA_SHADERS[1],
{{1, VK_DESCRIPTOR_TYPE_SAMPLER},
{1, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE},
{1, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE}});
passes[2] = LsfgPass(device, shaders, LSFG_ALPHA_SHADERS[2],
{{1, VK_DESCRIPTOR_TYPE_SAMPLER},
{1, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE},
{2, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE}});
passes[3] = LsfgPass(device, shaders, LSFG_ALPHA_SHADERS[3],
{{1, VK_DESCRIPTOR_TYPE_SAMPLER},
{2, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE},
{2, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE}});
}
bool LsfgAlphaPasses::Valid() const {
for (const auto& pass : passes) {
if (!pass.Valid()) return false;
}
return true;
}
LsfgAlpha::LsfgAlpha(const Device& device, const LsfgAlphaPasses& passes_,
LsfgResources& resources, VkDescriptorPool descriptor_pool, LsfgImage& input_)
: passes{&passes_}, input{&input_} {
if (!passes->Valid()) return;
const VkExtent2D half_extent = HalveExtent(input->Extent());
const VkExtent2D quarter_extent = HalveExtent(half_extent);
temp1 = LsfgImage(device, half_extent);
temp2 = LsfgImage(device, half_extent);
if (!temp1.Valid() || !temp2.Valid()) return;
for (size_t i = 0; i < temp3.size(); ++i) {
temp3[i] = LsfgImage(device, quarter_extent);
if (!temp3[i].Valid()) return;
for (size_t j = 0; j < LSFG_HISTORY_SLOTS; ++j) {
out_images[j][i] = LsfgImage(device, quarter_extent);
if (!out_images[j][i].Valid()) return;
}
}
std::vector<VkDescriptorSetLayout> layouts;
for (size_t i = 0; i < LSFG_ALPHA_STAGES - 1; ++i) {
layouts.push_back(passes->Get(i).SetLayout());
}
for (size_t i = 0; i < LSFG_HISTORY_SLOTS; ++i) {
layouts.push_back(passes->Get(3).SetLayout());
}
const std::vector<VkDescriptorSet> sets =
AllocateLsfgDescriptorSets(device, descriptor_pool, layouts);
if (sets.size() != layouts.size()) return;
for (size_t i = 0; i < LSFG_ALPHA_STAGES - 1; ++i) {
descriptor_sets[i] = sets[i];
}
for (size_t i = 0; i < LSFG_HISTORY_SLOTS; ++i) {
last_descriptor_sets[i] = sets[LSFG_ALPHA_STAGES - 1 + i];
}
const VkSampler sampler = resources.GetSampler();
LsfgDescriptorWriter(descriptor_sets[0])
.AddSampler(sampler)
.AddSampledImage(*input)
.AddStorageImage(temp1)
.Build(device);
LsfgDescriptorWriter(descriptor_sets[1])
.AddSampler(sampler)
.AddSampledImage(temp1)
.AddStorageImage(temp2)
.Build(device);
LsfgDescriptorWriter(descriptor_sets[2])
.AddSampler(sampler)
.AddSampledImage(temp2)
.AddStorageImages(temp3)
.Build(device);
for (size_t i = 0; i < LSFG_HISTORY_SLOTS; ++i) {
LsfgDescriptorWriter(last_descriptor_sets[i])
.AddSampler(sampler)
.AddSampledImages(temp3)
.AddStorageImages(out_images[i])
.Build(device);
}
allocated = true;
}
void LsfgAlpha::PushBarriers(LsfgBarriers& barriers, uint64_t frame_count, size_t stage) {
switch (stage) {
case 0:
barriers.WriteToRead(*input).ReadToWrite(temp1);
break;
case 1:
barriers.WriteToRead(temp1).ReadToWrite(temp2);
break;
case 2:
barriers.WriteToRead(temp2).ReadToWriteAll(temp3);
break;
default:
barriers.WriteToReadAll(temp3).ReadToWriteAll(out_images[frame_count % LSFG_HISTORY_SLOTS]);
break;
}
}
void LsfgAlpha::DispatchStage(VkCommandBuffer cmdbuf, uint64_t frame_count, size_t stage) {
const VkExtent2D extent = stage < 2 ? temp1.Extent() : temp3[0].Extent();
const VkDescriptorSet set = stage < LSFG_ALPHA_STAGES - 1
? descriptor_sets[stage]
: last_descriptor_sets[frame_count % LSFG_HISTORY_SLOTS];
passes->Get(stage).BindSet(cmdbuf, set);
vkd.CmdDispatch(cmdbuf, GroupCount(extent.width), GroupCount(extent.height), 1);
}
}
@@ -0,0 +1,70 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2025 lsfg-vk
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Ported into Bannerlator's compositor from WinNative (GPL-3.0-or-later),
// whose LSFG port is credited to Camille LaVey / the Eden Emulator Project and
// follows upstream lsfg-vk. Only the Vulkan dispatch differs: Bannerlator
// resolves entry points through the renderer's own table (see lsfg_vkd.h).
#pragma once
#include <array>
#include "lsfg_common.hpp"
namespace lsfg {
class LsfgShaders;
constexpr size_t LSFG_ALPHA_STAGES = 4;
class LsfgAlphaPasses {
public:
LsfgAlphaPasses() = default;
LsfgAlphaPasses(const Device& device, const LsfgShaders& shaders);
[[nodiscard]] const LsfgPass& Get(size_t stage) const {
return passes[stage];
}
[[nodiscard]] bool Valid() const;
private:
std::array<LsfgPass, LSFG_ALPHA_STAGES> passes;
};
class LsfgAlpha {
public:
LsfgAlpha() = default;
LsfgAlpha(const Device& device, const LsfgAlphaPasses& passes, LsfgResources& resources,
VkDescriptorPool descriptor_pool, LsfgImage& input);
void PushBarriers(LsfgBarriers& barriers, uint64_t frame_count, size_t stage);
void DispatchStage(VkCommandBuffer cmdbuf, uint64_t frame_count, size_t stage);
[[nodiscard]] LsfgImageHistory& Outputs() {
return out_images;
}
[[nodiscard]] bool Valid() const {
return allocated;
}
private:
const LsfgAlphaPasses* passes{};
LsfgImage* input{};
std::array<VkDescriptorSet, LSFG_ALPHA_STAGES - 1> descriptor_sets{};
std::array<VkDescriptorSet, LSFG_HISTORY_SLOTS> last_descriptor_sets{};
LsfgImage temp1;
LsfgImage temp2;
LsfgImagePair temp3;
LsfgImageHistory out_images;
bool allocated{};
};
}
@@ -0,0 +1,155 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2025 lsfg-vk
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Ported into Bannerlator's compositor from WinNative (GPL-3.0-or-later),
// whose LSFG port is credited to Camille LaVey / the Eden Emulator Project and
// follows upstream lsfg-vk. Only the Vulkan dispatch differs: Bannerlator
// resolves entry points through the renderer's own table (see lsfg_vkd.h).
#include "lsfg_beta.hpp"
#include "lsfg_shaders.hpp"
#include <vector>
namespace lsfg {
namespace {
constexpr uint32_t DISPATCH_TILE_SHIFT = 3;
constexpr uint32_t OUTPUT_TILE_SHIFT = 5;
[[nodiscard]] uint32_t GroupCount(uint32_t size, uint32_t shift) {
return (size + (1u << shift) - 1) >> shift;
}
}
LsfgBeta::LsfgBeta(const Device& device, const LsfgShaders& shaders, LsfgResources& resources,
VkDescriptorPool descriptor_pool, LsfgImageHistory& inputs_)
: inputs{&inputs_} {
passes[0] = LsfgPass(device, shaders, LSFG_BETA_SHADERS[0],
{{1, VK_DESCRIPTOR_TYPE_SAMPLER},
{6, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE},
{2, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE}});
for (size_t i = 1; i < LSFG_BETA_STAGES - 1; ++i) {
passes[i] = LsfgPass(device, shaders, LSFG_BETA_SHADERS[i],
{{1, VK_DESCRIPTOR_TYPE_SAMPLER},
{2, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE},
{2, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE}});
}
passes[4] = LsfgPass(device, shaders, LSFG_BETA_SHADERS[4],
{{1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER},
{1, VK_DESCRIPTOR_TYPE_SAMPLER},
{2, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE},
{6, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE}});
for (const auto& pass : passes) {
if (!pass.Valid()) return;
}
const VkExtent2D extent = (*inputs)[0][0].Extent();
for (size_t i = 0; i < temp1.size(); ++i) {
temp1[i] = LsfgImage(device, extent);
temp2[i] = LsfgImage(device, extent);
if (!temp1[i].Valid() || !temp2[i].Valid()) return;
}
for (size_t i = 0; i < LSFG_BETA_OUTPUTS; ++i) {
const VkExtent2D level_extent{
extent.width >> i,
extent.height >> i,
};
out_images[i] = LsfgImage(device, level_extent, LSFG_FLOW_FORMAT);
if (!out_images[i].Valid()) return;
}
std::vector<VkDescriptorSetLayout> layouts;
for (size_t i = 0; i < LSFG_HISTORY_SLOTS; ++i) {
layouts.push_back(passes[0].SetLayout());
}
for (size_t i = 1; i < LSFG_BETA_STAGES; ++i) {
layouts.push_back(passes[i].SetLayout());
}
const std::vector<VkDescriptorSet> sets =
AllocateLsfgDescriptorSets(device, descriptor_pool, layouts);
if (sets.size() != layouts.size()) return;
for (size_t i = 0; i < LSFG_HISTORY_SLOTS; ++i) {
first_descriptor_sets[i] = sets[i];
}
for (size_t i = 0; i < LSFG_BETA_STAGES - 1; ++i) {
descriptor_sets[i] = sets[LSFG_HISTORY_SLOTS + i];
}
const VkSampler sampler = resources.GetSampler();
const VkSampler border_sampler =
resources.GetSampler(VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER, VK_COMPARE_OP_NEVER, true);
for (size_t i = 0; i < LSFG_HISTORY_SLOTS; ++i) {
LsfgDescriptorWriter(first_descriptor_sets[i])
.AddSampler(border_sampler)
.AddSampledImages((*inputs)[(i + 1) % LSFG_HISTORY_SLOTS])
.AddSampledImages((*inputs)[(i + 2) % LSFG_HISTORY_SLOTS])
.AddSampledImages((*inputs)[i % LSFG_HISTORY_SLOTS])
.AddStorageImages(temp1)
.Build(device);
}
LsfgDescriptorWriter(descriptor_sets[0])
.AddSampler(sampler)
.AddSampledImages(temp1)
.AddStorageImages(temp2)
.Build(device);
LsfgDescriptorWriter(descriptor_sets[1])
.AddSampler(sampler)
.AddSampledImages(temp2)
.AddStorageImages(temp1)
.Build(device);
LsfgDescriptorWriter(descriptor_sets[2])
.AddSampler(sampler)
.AddSampledImages(temp1)
.AddStorageImages(temp2)
.Build(device);
LsfgDescriptorWriter(descriptor_sets[3])
.AddUniformBuffer(resources.GetBuffer(0.5f), LsfgResources::BufferSize())
.AddSampler(sampler)
.AddSampledImages(temp2)
.AddStorageImages(out_images)
.Build(device);
allocated = true;
}
void LsfgBeta::Dispatch(VkCommandBuffer cmdbuf, uint64_t frame_count) {
const VkExtent2D extent = temp1[0].Extent();
const uint32_t groups_x = GroupCount(extent.width, DISPATCH_TILE_SHIFT);
const uint32_t groups_y = GroupCount(extent.height, DISPATCH_TILE_SHIFT);
LsfgBarriers barriers(cmdbuf);
for (auto& slot : *inputs) {
barriers.WriteToReadAll(slot);
}
barriers.ReadToWriteAll(temp1).Build();
passes[0].Bind(cmdbuf, first_descriptor_sets[frame_count % LSFG_HISTORY_SLOTS]);
vkd.CmdDispatch(cmdbuf, groups_x, groups_y, 1);
LsfgBarriers(cmdbuf).WriteToReadAll(temp1).ReadToWriteAll(temp2).Build();
passes[1].Bind(cmdbuf, descriptor_sets[0]);
vkd.CmdDispatch(cmdbuf, groups_x, groups_y, 1);
LsfgBarriers(cmdbuf).WriteToReadAll(temp2).ReadToWriteAll(temp1).Build();
passes[2].Bind(cmdbuf, descriptor_sets[1]);
vkd.CmdDispatch(cmdbuf, groups_x, groups_y, 1);
LsfgBarriers(cmdbuf).WriteToReadAll(temp1).ReadToWriteAll(temp2).Build();
passes[3].Bind(cmdbuf, descriptor_sets[2]);
vkd.CmdDispatch(cmdbuf, groups_x, groups_y, 1);
LsfgBarriers(cmdbuf).WriteToReadAll(temp2).ReadToWriteAll(out_images).Build();
passes[4].Bind(cmdbuf, descriptor_sets[3]);
vkd.CmdDispatch(cmdbuf, GroupCount(extent.width, OUTPUT_TILE_SHIFT),
GroupCount(extent.height, OUTPUT_TILE_SHIFT), 1);
}
}
@@ -0,0 +1,54 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2025 lsfg-vk
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Ported into Bannerlator's compositor from WinNative (GPL-3.0-or-later),
// whose LSFG port is credited to Camille LaVey / the Eden Emulator Project and
// follows upstream lsfg-vk. Only the Vulkan dispatch differs: Bannerlator
// resolves entry points through the renderer's own table (see lsfg_vkd.h).
#pragma once
#include <array>
#include "lsfg_common.hpp"
namespace lsfg {
class LsfgShaders;
constexpr size_t LSFG_BETA_STAGES = 5;
constexpr size_t LSFG_BETA_OUTPUTS = 6;
class LsfgBeta {
public:
LsfgBeta() = default;
LsfgBeta(const Device& device, const LsfgShaders& shaders, LsfgResources& resources,
VkDescriptorPool descriptor_pool, LsfgImageHistory& inputs);
void Dispatch(VkCommandBuffer cmdbuf, uint64_t frame_count);
[[nodiscard]] LsfgImage& Output(size_t level) {
return out_images[level];
}
[[nodiscard]] bool Valid() const {
return allocated;
}
private:
LsfgImageHistory* inputs{};
std::array<LsfgPass, LSFG_BETA_STAGES> passes;
std::array<VkDescriptorSet, LSFG_HISTORY_SLOTS> first_descriptor_sets{};
std::array<VkDescriptorSet, LSFG_BETA_STAGES - 1> descriptor_sets{};
LsfgImagePair temp1;
LsfgImagePair temp2;
std::array<LsfgImage, LSFG_BETA_OUTPUTS> out_images;
bool allocated{};
};
}
@@ -0,0 +1,149 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2025 lsfg-vk
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Ported into Bannerlator's compositor from WinNative (GPL-3.0-or-later),
// whose LSFG port is credited to Camille LaVey / the Eden Emulator Project and
// follows upstream lsfg-vk. Only the Vulkan dispatch differs: Bannerlator
// resolves entry points through the renderer's own table (see lsfg_vkd.h).
#include "lsfg_chain.hpp"
#include "lsfg_shaders.hpp"
#include <algorithm>
namespace lsfg {
namespace {
constexpr uint32_t FIXED_DESCRIPTOR_SETS = 64;
constexpr uint32_t DESCRIPTOR_SETS_PER_SLOT = 112;
[[nodiscard]] constexpr bool HasDelta(size_t i) {
return i >= LSFG_FIRST_DELTA_LEVEL && i <= LSFG_LAST_DELTA_LEVEL;
}
}
LsfgChain::LsfgChain(const Device& device, const LsfgShaders& shaders, VkExtent2D extent,
VkFormat format, float flow_scale)
: resources{device, flow_scale}, owner{device.Handle()} {
descriptor_pool = CreateLsfgDescriptorPool(
device, FIXED_DESCRIPTOR_SETS +
DESCRIPTOR_SETS_PER_SLOT * static_cast<uint32_t>(LSFG_GENERATION_SLOTS));
if (descriptor_pool == VK_NULL_HANDLE) return;
for (auto& image : frames) {
image = LsfgImage(device, extent, format);
if (!image.Valid()) return;
}
mipmaps = LsfgMipmaps(device, shaders, resources, descriptor_pool, frames, flow_scale);
if (!mipmaps.Valid()) return;
alpha_passes = LsfgAlphaPasses(device, shaders);
if (!alpha_passes.Valid()) return;
for (size_t i = 0; i < LSFG_MIP_LEVELS; ++i) {
alpha[i] = LsfgAlpha(device, alpha_passes, resources, descriptor_pool, mipmaps.Output(i));
if (!alpha[i].Valid()) return;
}
beta = LsfgBeta(device, shaders, resources, descriptor_pool, alpha[0].Outputs());
if (!beta.Valid()) return;
for (size_t i = 0; i < LSFG_MIP_LEVELS; ++i) {
const size_t level = LSFG_MIP_LEVELS - 1 - i;
gamma[i] = LsfgGamma(device, shaders, resources, descriptor_pool, alpha[level].Outputs(),
beta.Output(std::min(level, LSFG_BETA_OUTPUTS - 1)),
i == 0 ? nullptr : &gamma[i - 1].Output());
if (!gamma[i].Valid()) return;
if (!HasDelta(i)) {
continue;
}
const size_t index = i - LSFG_FIRST_DELTA_LEVEL;
const bool first = i == LSFG_FIRST_DELTA_LEVEL;
delta[index] = LsfgDelta(device, shaders, resources, descriptor_pool,
alpha[level].Outputs(), beta.Output(level),
first ? nullptr : &gamma[i - 1].Output(),
first ? nullptr : &delta[index - 1].Output1(),
first ? nullptr : &delta[index - 1].Output2());
if (!delta[index].Valid()) return;
}
generate = LsfgGenerate(device, shaders, resources, descriptor_pool, frames,
gamma[LSFG_MIP_LEVELS - 1].Output(),
delta[LSFG_DELTA_INSTANCES - 1].Output1(),
delta[LSFG_DELTA_INSTANCES - 1].Output2());
if (!generate.Valid()) return;
valid = true;
}
LsfgChain::~LsfgChain() {
if (descriptor_pool != VK_NULL_HANDLE) {
vkd.DestroyDescriptorPool(owner, descriptor_pool, nullptr);
descriptor_pool = VK_NULL_HANDLE;
}
}
void LsfgChain::DispatchShared(VkCommandBuffer cmdbuf, uint64_t frame_count) {
resources.PrepareDummies(cmdbuf);
mipmaps.Dispatch(cmdbuf, frame_count);
for (size_t stage = 0; stage < LSFG_ALPHA_STAGES; ++stage) {
LsfgBarriers barriers(cmdbuf);
for (auto& level : alpha) {
level.PushBarriers(barriers, frame_count, stage);
}
barriers.Build();
alpha_passes.Get(stage).BindPipeline(cmdbuf);
for (auto& level : alpha) {
level.DispatchStage(cmdbuf, frame_count, stage);
}
}
beta.Dispatch(cmdbuf, frame_count);
}
void LsfgChain::DispatchGeneration(VkCommandBuffer cmdbuf, uint64_t frame_count,
size_t generation_count, size_t generation, uint32_t target,
VkImage image, VkExtent2D extent) {
const size_t slot = LsfgGenerationSlot(generation_count, generation);
constexpr size_t PAIRED_STEPS =
LSFG_GAMMA_STAGES > LSFG_DELTA_STAGES ? LSFG_GAMMA_STAGES : LSFG_DELTA_STAGES;
for (size_t i = 0; i < LSFG_MIP_LEVELS; ++i) {
if (!HasDelta(i)) {
gamma[i].Dispatch(cmdbuf, frame_count, slot);
continue;
}
LsfgDelta& paired = delta[i - LSFG_FIRST_DELTA_LEVEL];
for (size_t step = 0; step < PAIRED_STEPS; ++step) {
LsfgBarriers barriers(cmdbuf);
if (step < LSFG_GAMMA_STAGES) {
gamma[i].PushStepBarriers(barriers, frame_count, step);
}
if (step < LSFG_DELTA_STAGES) {
paired.PushStepBarriers(barriers, frame_count, step);
}
barriers.Build();
if (step < LSFG_GAMMA_STAGES) {
gamma[i].DispatchStep(cmdbuf, frame_count, slot, step);
}
if (step < LSFG_DELTA_STAGES) {
paired.DispatchStep(cmdbuf, frame_count, slot, step);
}
}
}
generate.Dispatch(cmdbuf, frame_count, slot, target, image, extent);
}
}
@@ -0,0 +1,83 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2025 lsfg-vk
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Ported into Bannerlator's compositor from WinNative (GPL-3.0-or-later),
// whose LSFG port is credited to Camille LaVey / the Eden Emulator Project and
// follows upstream lsfg-vk. Only the Vulkan dispatch differs: Bannerlator
// resolves entry points through the renderer's own table (see lsfg_vkd.h).
#pragma once
#include <array>
#include "lsfg_alpha.hpp"
#include "lsfg_beta.hpp"
#include "lsfg_common.hpp"
#include "lsfg_delta.hpp"
#include "lsfg_gamma.hpp"
#include "lsfg_generate.hpp"
#include "lsfg_mipmaps.hpp"
namespace lsfg {
class LsfgShaders;
constexpr size_t LSFG_FIRST_DELTA_LEVEL = 4;
constexpr size_t LSFG_LAST_DELTA_LEVEL = LSFG_MIP_LEVELS - 1;
constexpr size_t LSFG_DELTA_INSTANCES = LSFG_LAST_DELTA_LEVEL + 1 - LSFG_FIRST_DELTA_LEVEL;
class LsfgChain {
public:
LsfgChain(const Device& device, const LsfgShaders& shaders, VkExtent2D extent, VkFormat format,
float flow_scale);
~LsfgChain();
LsfgChain(const LsfgChain&) = delete;
LsfgChain& operator=(const LsfgChain&) = delete;
void DispatchShared(VkCommandBuffer cmdbuf, uint64_t frame_count);
void DispatchGeneration(VkCommandBuffer cmdbuf, uint64_t frame_count, size_t generation_count,
size_t generation, uint32_t target, VkImage image, VkExtent2D extent);
void SetTarget(const Device& device, size_t generation_count, size_t generation,
uint32_t target, VkImageView view) {
generate.SetTarget(device, LsfgGenerationSlot(generation_count, generation), target, view);
}
void ForgetTargets() {
generate.ForgetTargets();
}
[[nodiscard]] LsfgImage& Input(uint64_t frame_count) {
return frames[frame_count % frames.size()];
}
[[nodiscard]] LsfgImage& FlowLevel(size_t level) {
return mipmaps.Output(level);
}
[[nodiscard]] bool Valid() const {
return valid;
}
private:
LsfgResources resources;
VkDevice owner{VK_NULL_HANDLE};
VkDescriptorPool descriptor_pool{VK_NULL_HANDLE};
LsfgImagePair frames;
LsfgMipmaps mipmaps;
LsfgAlphaPasses alpha_passes;
std::array<LsfgAlpha, LSFG_MIP_LEVELS> alpha;
LsfgBeta beta;
std::array<LsfgGamma, LSFG_MIP_LEVELS> gamma;
std::array<LsfgDelta, LSFG_DELTA_INSTANCES> delta;
LsfgGenerate generate;
bool valid{};
};
}
@@ -0,0 +1,653 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2025 lsfg-vk
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Ported into Bannerlator's compositor from WinNative (GPL-3.0-or-later),
// whose LSFG port is credited to Camille LaVey / the Eden Emulator Project and
// follows upstream lsfg-vk. Only the Vulkan dispatch differs: Bannerlator
// resolves entry points through the renderer's own table (see lsfg_vkd.h).
#include "lsfg_common.hpp"
#include "lsfg_shaders.hpp"
#include <algorithm>
#include <cstring>
namespace lsfg {
namespace {
constexpr uint32_t DESCRIPTORS_PER_TYPE = 4096;
struct LsfgConstants {
std::array<uint32_t, 2> input_offset;
uint32_t first_iter;
uint32_t first_iter_s;
uint32_t advanced_color_kind;
uint32_t hdr_support;
float resolution_inv_scale;
float timestamp;
float ui_threshold;
std::array<uint32_t, 3> padding;
};
static_assert(sizeof(LsfgConstants) == 48);
VkImageMemoryBarrier MakeBarrier(const LsfgImage& image, VkAccessFlags src_access,
VkAccessFlags dst_access) {
VkImageMemoryBarrier barrier{};
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
barrier.srcAccessMask = src_access;
barrier.dstAccessMask = dst_access;
barrier.oldLayout = image.Layout();
barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.image = image.Handle();
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
barrier.subresourceRange.levelCount = 1;
barrier.subresourceRange.layerCount = 1;
return barrier;
}
}
Device::Device(VkDevice device_, VkPhysicalDevice physical_device_)
: device{device_}, physical_device{physical_device_} {
vkd.GetPhysicalDeviceMemoryProperties(physical_device, &memory_properties);
}
uint32_t Device::FindMemoryType(uint32_t bits, VkMemoryPropertyFlags properties) const {
for (uint32_t i = 0; i < memory_properties.memoryTypeCount; i++) {
if ((bits & (1u << i)) == 0) continue;
if ((memory_properties.memoryTypes[i].propertyFlags & properties) == properties) return i;
}
return UINT32_MAX;
}
Buffer::Buffer(const Device& device_, VkDeviceSize size) : device{device_.Handle()} {
VkBufferCreateInfo buffer_ci{};
buffer_ci.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
buffer_ci.size = size;
buffer_ci.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
buffer_ci.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
if (vkd.CreateBuffer(device, &buffer_ci, nullptr, &buffer) != VK_SUCCESS) {
buffer = VK_NULL_HANDLE;
return;
}
VkMemoryRequirements requirements;
vkd.GetBufferMemoryRequirements(device, buffer, &requirements);
VkMemoryAllocateInfo allocate_info{};
allocate_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocate_info.allocationSize = requirements.size;
allocate_info.memoryTypeIndex = device_.FindMemoryType(
requirements.memoryTypeBits,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
if (allocate_info.memoryTypeIndex == UINT32_MAX ||
vkd.AllocateMemory(device, &allocate_info, nullptr, &memory) != VK_SUCCESS) {
Release();
return;
}
vkd.BindBufferMemory(device, buffer, memory, 0);
if (vkd.MapMemory(device, memory, 0, VK_WHOLE_SIZE, 0, &mapped) != VK_SUCCESS) {
mapped = nullptr;
Release();
}
}
Buffer::~Buffer() {
Release();
}
Buffer::Buffer(Buffer&& other) noexcept
: device{other.device}, buffer{other.buffer}, memory{other.memory}, mapped{other.mapped} {
other.device = VK_NULL_HANDLE;
other.buffer = VK_NULL_HANDLE;
other.memory = VK_NULL_HANDLE;
other.mapped = nullptr;
}
Buffer& Buffer::operator=(Buffer&& other) noexcept {
if (this != &other) {
Release();
device = other.device;
buffer = other.buffer;
memory = other.memory;
mapped = other.mapped;
other.device = VK_NULL_HANDLE;
other.buffer = VK_NULL_HANDLE;
other.memory = VK_NULL_HANDLE;
other.mapped = nullptr;
}
return *this;
}
void Buffer::Upload(const void* data, size_t size) {
if (mapped) std::memcpy(mapped, data, size);
}
void Buffer::Release() {
if (device == VK_NULL_HANDLE) return;
if (mapped) {
vkd.UnmapMemory(device, memory);
mapped = nullptr;
}
if (buffer) vkd.DestroyBuffer(device, buffer, nullptr);
if (memory) vkd.FreeMemory(device, memory, nullptr);
buffer = VK_NULL_HANDLE;
memory = VK_NULL_HANDLE;
}
LsfgImage::LsfgImage(const Device& device_, VkExtent2D extent_, VkFormat format_)
: device{device_.Handle()},
extent{std::max(1u, extent_.width), std::max(1u, extent_.height)}, format{format_} {
VkImageCreateInfo image_ci{};
image_ci.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
image_ci.imageType = VK_IMAGE_TYPE_2D;
image_ci.format = format;
image_ci.extent = {extent.width, extent.height, 1};
image_ci.mipLevels = 1;
image_ci.arrayLayers = 1;
image_ci.samples = VK_SAMPLE_COUNT_1_BIT;
image_ci.tiling = VK_IMAGE_TILING_OPTIMAL;
image_ci.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
image_ci.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
image_ci.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
if (vkd.CreateImage(device, &image_ci, nullptr, &image) != VK_SUCCESS) {
image = VK_NULL_HANDLE;
return;
}
VkMemoryRequirements requirements;
vkd.GetImageMemoryRequirements(device, image, &requirements);
VkMemoryAllocateInfo allocate_info{};
allocate_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocate_info.allocationSize = requirements.size;
allocate_info.memoryTypeIndex =
device_.FindMemoryType(requirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
if (allocate_info.memoryTypeIndex == UINT32_MAX ||
vkd.AllocateMemory(device, &allocate_info, nullptr, &memory) != VK_SUCCESS) {
Release();
return;
}
vkd.BindImageMemory(device, image, memory, 0);
VkImageViewCreateInfo view_ci{};
view_ci.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
view_ci.image = image;
view_ci.viewType = VK_IMAGE_VIEW_TYPE_2D;
view_ci.format = format;
view_ci.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
view_ci.subresourceRange.levelCount = 1;
view_ci.subresourceRange.layerCount = 1;
if (vkd.CreateImageView(device, &view_ci, nullptr, &view) != VK_SUCCESS) {
view = VK_NULL_HANDLE;
Release();
}
}
LsfgImage::~LsfgImage() {
Release();
}
LsfgImage::LsfgImage(LsfgImage&& other) noexcept
: device{other.device}, image{other.image}, view{other.view}, memory{other.memory},
extent{other.extent}, format{other.format}, layout{other.layout} {
other.device = VK_NULL_HANDLE;
other.image = VK_NULL_HANDLE;
other.view = VK_NULL_HANDLE;
other.memory = VK_NULL_HANDLE;
}
LsfgImage& LsfgImage::operator=(LsfgImage&& other) noexcept {
if (this != &other) {
Release();
device = other.device;
image = other.image;
view = other.view;
memory = other.memory;
extent = other.extent;
format = other.format;
layout = other.layout;
other.device = VK_NULL_HANDLE;
other.image = VK_NULL_HANDLE;
other.view = VK_NULL_HANDLE;
other.memory = VK_NULL_HANDLE;
}
return *this;
}
void LsfgImage::Release() {
if (device == VK_NULL_HANDLE) return;
if (view) vkd.DestroyImageView(device, view, nullptr);
if (image) vkd.DestroyImage(device, image, nullptr);
if (memory) vkd.FreeMemory(device, memory, nullptr);
view = VK_NULL_HANDLE;
image = VK_NULL_HANDLE;
memory = VK_NULL_HANDLE;
}
LsfgResources::~LsfgResources() {
if (!device) return;
for (auto& [key, sampler] : samplers) {
vkd.DestroySampler(device->Handle(), sampler, nullptr);
}
samplers.clear();
}
VkDeviceSize LsfgResources::BufferSize() {
return sizeof(LsfgConstants);
}
VkSampler LsfgResources::GetSampler(VkSamplerAddressMode address_mode, VkCompareOp compare_op,
bool white_border) {
const uint64_t key = static_cast<uint64_t>(address_mode) |
(static_cast<uint64_t>(compare_op) << 8) |
(static_cast<uint64_t>(white_border) << 16);
const auto it = samplers.find(key);
if (it != samplers.end()) return it->second;
VkSampler sampler = CreateLsfgSampler(*device, address_mode, compare_op, white_border);
samplers.emplace(key, sampler);
return sampler;
}
VkBuffer LsfgResources::GetBuffer(float timestamp, bool first_iter, bool first_iter_s) {
uint32_t timestamp_bits{};
std::memcpy(&timestamp_bits, &timestamp, sizeof(timestamp_bits));
const uint64_t key = static_cast<uint64_t>(timestamp_bits) |
(static_cast<uint64_t>(first_iter) << 32) |
(static_cast<uint64_t>(first_iter_s) << 33);
const auto it = buffers.find(key);
if (it != buffers.end()) return it->second.Handle();
Buffer buffer{*device, sizeof(LsfgConstants)};
if (!buffer.Valid()) return VK_NULL_HANDLE;
LsfgConstants constants{};
constants.first_iter = first_iter ? 1u : 0u;
constants.first_iter_s = first_iter_s ? 1u : 0u;
constants.resolution_inv_scale = 1.0f / flow_scale;
constants.timestamp = timestamp;
constants.ui_threshold = 0.5f;
buffer.Upload(&constants, sizeof(constants));
const auto [entry, inserted] = buffers.emplace(key, std::move(buffer));
return entry->second.Handle();
}
const LsfgImage& LsfgResources::GetDummy(VkFormat format) {
const uint32_t key = static_cast<uint32_t>(format);
const auto it = dummies.find(key);
if (it != dummies.end()) return it->second;
LsfgImage image{*device, VkExtent2D{1, 1}, format};
const auto [entry, inserted] = dummies.emplace(key, std::move(image));
dummies_ready = false;
return entry->second;
}
void LsfgResources::PrepareDummies(VkCommandBuffer cmdbuf) {
if (dummies_ready || dummies.empty()) return;
std::vector<VkImageMemoryBarrier> barriers;
for (auto& [key, image] : dummies) {
if (!image.Valid()) continue;
VkImageMemoryBarrier barrier{};
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
barrier.srcAccessMask = 0;
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.image = image.Handle();
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
barrier.subresourceRange.levelCount = 1;
barrier.subresourceRange.layerCount = 1;
barriers.push_back(barrier);
image.SetLayout(VK_IMAGE_LAYOUT_GENERAL);
}
if (!barriers.empty()) {
vkd.CmdPipelineBarrier(cmdbuf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 0, nullptr,
static_cast<uint32_t>(barriers.size()), barriers.data());
}
dummies_ready = true;
}
LsfgBarriers& LsfgBarriers::Push(LsfgImage& image, VkAccessFlags src_access,
VkAccessFlags dst_access) {
barriers.push_back(MakeBarrier(image, src_access, dst_access));
image.SetLayout(VK_IMAGE_LAYOUT_GENERAL);
return *this;
}
LsfgBarriers& LsfgBarriers::WriteToRead(LsfgImage& image) {
return Push(image, VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT);
}
LsfgBarriers& LsfgBarriers::ReadToWrite(LsfgImage& image) {
return Push(image, VK_ACCESS_SHADER_READ_BIT, VK_ACCESS_SHADER_WRITE_BIT);
}
LsfgBarriers& LsfgBarriers::WriteToRead(LsfgImage* image) {
return image == nullptr ? *this : WriteToRead(*image);
}
LsfgBarriers& LsfgBarriers::ReadToWrite(LsfgImage* image) {
return image == nullptr ? *this : ReadToWrite(*image);
}
LsfgBarriers& LsfgBarriers::DiscardToWrite(VkImage image) {
VkImageMemoryBarrier barrier{};
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
barrier.srcAccessMask = 0;
barrier.dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.image = image;
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
barrier.subresourceRange.levelCount = 1;
barrier.subresourceRange.layerCount = 1;
barriers.push_back(barrier);
return *this;
}
void LsfgBarriers::Build() {
if (barriers.empty()) return;
vkd.CmdPipelineBarrier(cmdbuf, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 0, nullptr,
static_cast<uint32_t>(barriers.size()), barriers.data());
barriers.clear();
}
LsfgDescriptorWriter& LsfgDescriptorWriter::PushImage(VkDescriptorType type, VkSampler sampler,
VkImageView view) {
VkDescriptorImageInfo info{};
info.sampler = sampler;
info.imageView = view;
info.imageLayout =
view == VK_NULL_HANDLE ? VK_IMAGE_LAYOUT_UNDEFINED : VK_IMAGE_LAYOUT_GENERAL;
image_infos.push_back(info);
VkWriteDescriptorSet write{};
write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
write.dstSet = set;
write.dstBinding = binding++;
write.descriptorCount = 1;
write.descriptorType = type;
write.pImageInfo = &image_infos.back();
writes.push_back(write);
return *this;
}
LsfgDescriptorWriter& LsfgDescriptorWriter::AddSampler(VkSampler sampler) {
return PushImage(VK_DESCRIPTOR_TYPE_SAMPLER, sampler, VK_NULL_HANDLE);
}
LsfgDescriptorWriter& LsfgDescriptorWriter::AddSampledImage(const LsfgImage& image) {
return PushImage(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, VK_NULL_HANDLE, image.View());
}
LsfgDescriptorWriter& LsfgDescriptorWriter::AddSampledImage(const LsfgImage* image) {
return PushImage(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, VK_NULL_HANDLE,
image == nullptr ? VK_NULL_HANDLE : image->View());
}
LsfgDescriptorWriter& LsfgDescriptorWriter::AddStorageImage(const LsfgImage& image) {
return PushImage(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, VK_NULL_HANDLE, image.View());
}
LsfgDescriptorWriter& LsfgDescriptorWriter::AddStorageView(VkImageView view) {
return PushImage(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, VK_NULL_HANDLE, view);
}
LsfgDescriptorWriter& LsfgDescriptorWriter::AddUniformBuffer(VkBuffer buffer, VkDeviceSize size) {
VkDescriptorBufferInfo info{};
info.buffer = buffer;
info.offset = 0;
info.range = size;
buffer_infos.push_back(info);
VkWriteDescriptorSet write{};
write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
write.dstSet = set;
write.dstBinding = binding++;
write.descriptorCount = 1;
write.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
write.pBufferInfo = &buffer_infos.back();
writes.push_back(write);
return *this;
}
void LsfgDescriptorWriter::Build(const Device& device) {
if (writes.empty()) return;
vkd.UpdateDescriptorSets(device.Handle(), static_cast<uint32_t>(writes.size()), writes.data(), 0,
nullptr);
writes.clear();
}
LsfgPass::LsfgPass(const Device& device_, const LsfgShaders& shaders, uint32_t shader_id,
LsfgBindings bindings)
: device{device_.Handle()} {
std::vector<VkDescriptorSetLayoutBinding> layout_bindings;
uint32_t index = 0;
for (const auto& [count, type] : bindings) {
for (uint32_t i = 0; i < count; i++) {
VkDescriptorSetLayoutBinding entry{};
entry.binding = index++;
entry.descriptorType = type;
entry.descriptorCount = 1;
entry.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
layout_bindings.push_back(entry);
}
}
descriptor_count = index;
VkDescriptorSetLayoutCreateInfo layout_ci{};
layout_ci.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
layout_ci.bindingCount = static_cast<uint32_t>(layout_bindings.size());
layout_ci.pBindings = layout_bindings.data();
if (vkd.CreateDescriptorSetLayout(device, &layout_ci, nullptr, &descriptor_set_layout) !=
VK_SUCCESS) {
Release();
return;
}
VkPipelineLayoutCreateInfo pipeline_layout_ci{};
pipeline_layout_ci.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipeline_layout_ci.setLayoutCount = 1;
pipeline_layout_ci.pSetLayouts = &descriptor_set_layout;
if (vkd.CreatePipelineLayout(device, &pipeline_layout_ci, nullptr, &pipeline_layout) !=
VK_SUCCESS) {
Release();
return;
}
const VkShaderModule module = shaders.Get(shader_id);
if (module == VK_NULL_HANDLE) {
Release();
return;
}
VkPipelineShaderStageCreateInfo stage{};
stage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
stage.stage = VK_SHADER_STAGE_COMPUTE_BIT;
stage.module = module;
stage.pName = "main";
VkComputePipelineCreateInfo pipeline_ci{};
pipeline_ci.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO;
pipeline_ci.stage = stage;
pipeline_ci.layout = pipeline_layout;
if (vkd.CreateComputePipelines(device, VK_NULL_HANDLE, 1, &pipeline_ci, nullptr, &pipeline) !=
VK_SUCCESS) {
pipeline = VK_NULL_HANDLE;
Release();
}
}
LsfgPass::~LsfgPass() {
Release();
}
LsfgPass::LsfgPass(LsfgPass&& other) noexcept
: device{other.device}, descriptor_set_layout{other.descriptor_set_layout},
pipeline_layout{other.pipeline_layout}, pipeline{other.pipeline},
descriptor_count{other.descriptor_count} {
other.device = VK_NULL_HANDLE;
other.descriptor_set_layout = VK_NULL_HANDLE;
other.pipeline_layout = VK_NULL_HANDLE;
other.pipeline = VK_NULL_HANDLE;
}
LsfgPass& LsfgPass::operator=(LsfgPass&& other) noexcept {
if (this != &other) {
Release();
device = other.device;
descriptor_set_layout = other.descriptor_set_layout;
pipeline_layout = other.pipeline_layout;
pipeline = other.pipeline;
descriptor_count = other.descriptor_count;
other.device = VK_NULL_HANDLE;
other.descriptor_set_layout = VK_NULL_HANDLE;
other.pipeline_layout = VK_NULL_HANDLE;
other.pipeline = VK_NULL_HANDLE;
}
return *this;
}
void LsfgPass::Release() {
if (device == VK_NULL_HANDLE) return;
if (pipeline) vkd.DestroyPipeline(device, pipeline, nullptr);
if (pipeline_layout) vkd.DestroyPipelineLayout(device, pipeline_layout, nullptr);
if (descriptor_set_layout) vkd.DestroyDescriptorSetLayout(device, descriptor_set_layout, nullptr);
pipeline = VK_NULL_HANDLE;
pipeline_layout = VK_NULL_HANDLE;
descriptor_set_layout = VK_NULL_HANDLE;
}
void LsfgPass::Bind(VkCommandBuffer cmdbuf, VkDescriptorSet set) const {
BindPipeline(cmdbuf);
BindSet(cmdbuf, set);
}
void LsfgPass::BindPipeline(VkCommandBuffer cmdbuf) const {
vkd.CmdBindPipeline(cmdbuf, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline);
}
void LsfgPass::BindSet(VkCommandBuffer cmdbuf, VkDescriptorSet set) const {
vkd.CmdBindDescriptorSets(cmdbuf, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline_layout, 0, 1, &set, 0,
nullptr);
}
VkDescriptorPool CreateLsfgDescriptorPool(const Device& device, uint32_t max_sets) {
const VkDescriptorType types[] = {
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
VK_DESCRIPTOR_TYPE_SAMPLER,
VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
};
std::vector<VkDescriptorPoolSize> sizes;
for (const VkDescriptorType type : types) {
VkDescriptorPoolSize size{};
size.type = type;
size.descriptorCount = DESCRIPTORS_PER_TYPE;
sizes.push_back(size);
}
VkDescriptorPoolCreateInfo pool_ci{};
pool_ci.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
pool_ci.maxSets = max_sets;
pool_ci.poolSizeCount = static_cast<uint32_t>(sizes.size());
pool_ci.pPoolSizes = sizes.data();
VkDescriptorPool pool = VK_NULL_HANDLE;
if (vkd.CreateDescriptorPool(device.Handle(), &pool_ci, nullptr, &pool) != VK_SUCCESS) {
return VK_NULL_HANDLE;
}
return pool;
}
std::vector<VkDescriptorSet> AllocateLsfgDescriptorSets(const Device& device,
VkDescriptorPool pool,
VkDescriptorSetLayout layout,
uint32_t count) {
if (pool == VK_NULL_HANDLE || layout == VK_NULL_HANDLE || count == 0) return {};
const std::vector<VkDescriptorSetLayout> layouts(count, layout);
VkDescriptorSetAllocateInfo allocate_info{};
allocate_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocate_info.descriptorPool = pool;
allocate_info.descriptorSetCount = count;
allocate_info.pSetLayouts = layouts.data();
std::vector<VkDescriptorSet> sets(count, VK_NULL_HANDLE);
if (vkd.AllocateDescriptorSets(device.Handle(), &allocate_info, sets.data()) != VK_SUCCESS) {
return {};
}
return sets;
}
std::vector<VkDescriptorSet> AllocateLsfgDescriptorSets(
const Device& device, VkDescriptorPool pool,
const std::vector<VkDescriptorSetLayout>& layouts) {
if (pool == VK_NULL_HANDLE || layouts.empty()) return {};
for (const VkDescriptorSetLayout layout : layouts) {
if (layout == VK_NULL_HANDLE) return {};
}
VkDescriptorSetAllocateInfo allocate_info{};
allocate_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocate_info.descriptorPool = pool;
allocate_info.descriptorSetCount = static_cast<uint32_t>(layouts.size());
allocate_info.pSetLayouts = layouts.data();
std::vector<VkDescriptorSet> sets(layouts.size(), VK_NULL_HANDLE);
if (vkd.AllocateDescriptorSets(device.Handle(), &allocate_info, sets.data()) != VK_SUCCESS) {
return {};
}
return sets;
}
VkSampler CreateLsfgSampler(const Device& device, VkSamplerAddressMode address_mode,
VkCompareOp compare_op, bool white_border) {
VkSamplerCreateInfo sampler_ci{};
sampler_ci.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
sampler_ci.magFilter = VK_FILTER_LINEAR;
sampler_ci.minFilter = VK_FILTER_LINEAR;
sampler_ci.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
sampler_ci.addressModeU = address_mode;
sampler_ci.addressModeV = address_mode;
sampler_ci.addressModeW = address_mode;
sampler_ci.anisotropyEnable = VK_FALSE;
sampler_ci.compareEnable = VK_FALSE;
sampler_ci.compareOp = compare_op;
sampler_ci.maxLod = VK_LOD_CLAMP_NONE;
sampler_ci.borderColor = white_border ? VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
: VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;
sampler_ci.unnormalizedCoordinates = VK_FALSE;
VkSampler sampler = VK_NULL_HANDLE;
if (vkd.CreateSampler(device.Handle(), &sampler_ci, nullptr, &sampler) != VK_SUCCESS) {
return VK_NULL_HANDLE;
}
return sampler;
}
}
@@ -0,0 +1,331 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2025 lsfg-vk
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Ported into Bannerlator's compositor from WinNative (GPL-3.0-or-later),
// whose LSFG port is credited to Camille LaVey / the Eden Emulator Project and
// follows upstream lsfg-vk. Only the Vulkan dispatch differs: Bannerlator
// resolves entry points through the renderer's own table (see lsfg_vkd.h).
#pragma once
#include <array>
#include <cstdint>
#include <deque>
#include <initializer_list>
#include <map>
#include <utility>
#include <vector>
#include "lsfg_vkd.h"
namespace lsfg {
constexpr VkFormat LSFG_DEFAULT_FORMAT = VK_FORMAT_R8G8B8A8_UNORM;
constexpr VkFormat LSFG_FLOW_FORMAT = VK_FORMAT_R8_UNORM;
constexpr VkFormat LSFG_MOTION_FORMAT = VK_FORMAT_R16G16B16A16_SFLOAT;
constexpr size_t LSFG_HISTORY_SLOTS = 3;
constexpr size_t LSFG_MAX_TARGETS = 7;
constexpr size_t LSFG_MAX_GENERATIONS = 3;
constexpr size_t LSFG_MIP_LEVELS = 7;
constexpr size_t LSFG_GENERATION_SLOTS = LSFG_MAX_GENERATIONS * (LSFG_MAX_GENERATIONS + 1) / 2;
constexpr size_t LSFG_BARRIER_CAPACITY = 32;
constexpr std::array<uint32_t, 4> LSFG_ALPHA_SHADERS{290, 291, 292, 293};
constexpr std::array<uint32_t, 5> LSFG_BETA_SHADERS{298, 299, 300, 301, 302};
constexpr std::array<uint32_t, 5> LSFG_GAMMA_SHADERS{280, 282, 283, 284, 285};
constexpr std::array<uint32_t, 10> LSFG_DELTA_SHADERS{280, 286, 287, 288, 289,
281, 294, 295, 296, 297};
[[nodiscard]] constexpr size_t LsfgGenerationSlot(size_t generation_count, size_t generation) {
return (generation_count - 1) * generation_count / 2 + generation;
}
[[nodiscard]] constexpr float LsfgTimestamp(size_t generation, size_t generation_count) {
return static_cast<float>(generation + 1) /
static_cast<float>(generation_count + 1);
}
[[nodiscard]] constexpr size_t LsfgSlotCount(size_t slot) {
size_t count = 1;
while (LsfgGenerationSlot(count + 1, 0) <= slot) {
++count;
}
return count;
}
[[nodiscard]] constexpr float LsfgSlotTimestamp(size_t slot) {
const size_t count = LsfgSlotCount(slot);
return LsfgTimestamp(slot - LsfgGenerationSlot(count, 0), count);
}
class Device {
public:
Device() = default;
Device(VkDevice device_, VkPhysicalDevice physical_device_);
[[nodiscard]] VkDevice Handle() const {
return device;
}
[[nodiscard]] uint32_t FindMemoryType(uint32_t bits, VkMemoryPropertyFlags properties) const;
private:
VkDevice device{VK_NULL_HANDLE};
VkPhysicalDevice physical_device{VK_NULL_HANDLE};
VkPhysicalDeviceMemoryProperties memory_properties{};
};
class Buffer {
public:
Buffer() = default;
Buffer(const Device& device, VkDeviceSize size);
~Buffer();
Buffer(const Buffer&) = delete;
Buffer& operator=(const Buffer&) = delete;
Buffer(Buffer&& other) noexcept;
Buffer& operator=(Buffer&& other) noexcept;
[[nodiscard]] VkBuffer Handle() const {
return buffer;
}
[[nodiscard]] bool Valid() const {
return buffer != VK_NULL_HANDLE;
}
void Upload(const void* data, size_t size);
private:
void Release();
VkDevice device{VK_NULL_HANDLE};
VkBuffer buffer{VK_NULL_HANDLE};
VkDeviceMemory memory{VK_NULL_HANDLE};
void* mapped{nullptr};
};
class LsfgImage {
public:
LsfgImage() = default;
LsfgImage(const Device& device, VkExtent2D extent, VkFormat format = LSFG_DEFAULT_FORMAT);
~LsfgImage();
LsfgImage(const LsfgImage&) = delete;
LsfgImage& operator=(const LsfgImage&) = delete;
LsfgImage(LsfgImage&& other) noexcept;
LsfgImage& operator=(LsfgImage&& other) noexcept;
[[nodiscard]] VkImage Handle() const {
return image;
}
[[nodiscard]] VkImageView View() const {
return view;
}
[[nodiscard]] VkExtent2D Extent() const {
return extent;
}
[[nodiscard]] VkFormat Format() const {
return format;
}
[[nodiscard]] VkImageLayout Layout() const {
return layout;
}
[[nodiscard]] bool Valid() const {
return image != VK_NULL_HANDLE;
}
void SetLayout(VkImageLayout new_layout) {
layout = new_layout;
}
private:
void Release();
VkDevice device{VK_NULL_HANDLE};
VkImage image{VK_NULL_HANDLE};
VkImageView view{VK_NULL_HANDLE};
VkDeviceMemory memory{VK_NULL_HANDLE};
VkExtent2D extent{};
VkFormat format{VK_FORMAT_UNDEFINED};
VkImageLayout layout{VK_IMAGE_LAYOUT_UNDEFINED};
};
using LsfgImagePair = std::array<LsfgImage, 2>;
using LsfgImageHistory = std::array<LsfgImagePair, LSFG_HISTORY_SLOTS>;
class LsfgResources {
public:
LsfgResources() = default;
LsfgResources(const Device& device_, float flow_scale_)
: device{&device_}, flow_scale{flow_scale_} {}
~LsfgResources();
LsfgResources(const LsfgResources&) = delete;
LsfgResources& operator=(const LsfgResources&) = delete;
[[nodiscard]] VkSampler GetSampler(
VkSamplerAddressMode address_mode = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER,
VkCompareOp compare_op = VK_COMPARE_OP_NEVER, bool white_border = false);
[[nodiscard]] VkBuffer GetBuffer(float timestamp = 0.0f, bool first_iter = false,
bool first_iter_s = false);
[[nodiscard]] const LsfgImage& GetDummy(VkFormat format = LSFG_MOTION_FORMAT);
void PrepareDummies(VkCommandBuffer cmdbuf);
[[nodiscard]] static VkDeviceSize BufferSize();
private:
const Device* device{};
float flow_scale{1.0f};
std::map<uint64_t, VkSampler> samplers;
std::map<uint64_t, Buffer> buffers;
std::map<uint32_t, LsfgImage> dummies;
bool dummies_ready{};
};
class LsfgBarriers {
public:
explicit LsfgBarriers(VkCommandBuffer cmdbuf_) : cmdbuf{cmdbuf_} {
barriers.reserve(LSFG_BARRIER_CAPACITY);
}
LsfgBarriers& WriteToRead(LsfgImage& image);
LsfgBarriers& ReadToWrite(LsfgImage& image);
LsfgBarriers& WriteToRead(LsfgImage* image);
LsfgBarriers& ReadToWrite(LsfgImage* image);
LsfgBarriers& DiscardToWrite(VkImage image);
template <typename Range>
LsfgBarriers& WriteToReadAll(Range& images) {
for (auto& image : images) {
WriteToRead(image);
}
return *this;
}
template <typename Range>
LsfgBarriers& ReadToWriteAll(Range& images) {
for (auto& image : images) {
ReadToWrite(image);
}
return *this;
}
void Build();
private:
LsfgBarriers& Push(LsfgImage& image, VkAccessFlags src_access, VkAccessFlags dst_access);
VkCommandBuffer cmdbuf;
std::vector<VkImageMemoryBarrier> barriers;
};
class LsfgDescriptorWriter {
public:
explicit LsfgDescriptorWriter(VkDescriptorSet set_) : set{set_} {}
LsfgDescriptorWriter& AddSampler(VkSampler sampler);
LsfgDescriptorWriter& AddSampledImage(const LsfgImage& image);
LsfgDescriptorWriter& AddSampledImage(const LsfgImage* image);
LsfgDescriptorWriter& AddStorageImage(const LsfgImage& image);
LsfgDescriptorWriter& AddStorageView(VkImageView view);
LsfgDescriptorWriter& AddUniformBuffer(VkBuffer buffer, VkDeviceSize size);
template <typename Range>
LsfgDescriptorWriter& AddSampledImages(const Range& images) {
for (const auto& image : images) {
AddSampledImage(image);
}
return *this;
}
template <typename Range>
LsfgDescriptorWriter& AddStorageImages(const Range& images) {
for (const auto& image : images) {
AddStorageImage(image);
}
return *this;
}
void Build(const Device& device);
private:
LsfgDescriptorWriter& PushImage(VkDescriptorType type, VkSampler sampler, VkImageView view);
VkDescriptorSet set;
uint32_t binding{};
std::deque<VkDescriptorImageInfo> image_infos;
std::deque<VkDescriptorBufferInfo> buffer_infos;
std::vector<VkWriteDescriptorSet> writes;
};
using LsfgBindings = std::initializer_list<std::pair<uint32_t, VkDescriptorType>>;
class LsfgShaders;
class LsfgPass {
public:
LsfgPass() = default;
LsfgPass(const Device& device, const LsfgShaders& shaders, uint32_t shader_id,
LsfgBindings bindings);
~LsfgPass();
LsfgPass(const LsfgPass&) = delete;
LsfgPass& operator=(const LsfgPass&) = delete;
LsfgPass(LsfgPass&& other) noexcept;
LsfgPass& operator=(LsfgPass&& other) noexcept;
[[nodiscard]] VkDescriptorSetLayout SetLayout() const {
return descriptor_set_layout;
}
[[nodiscard]] uint32_t DescriptorCount() const {
return descriptor_count;
}
[[nodiscard]] bool Valid() const {
return pipeline != VK_NULL_HANDLE;
}
void Bind(VkCommandBuffer cmdbuf, VkDescriptorSet set) const;
void BindPipeline(VkCommandBuffer cmdbuf) const;
void BindSet(VkCommandBuffer cmdbuf, VkDescriptorSet set) const;
private:
void Release();
VkDevice device{VK_NULL_HANDLE};
VkDescriptorSetLayout descriptor_set_layout{VK_NULL_HANDLE};
VkPipelineLayout pipeline_layout{VK_NULL_HANDLE};
VkPipeline pipeline{VK_NULL_HANDLE};
uint32_t descriptor_count{};
};
[[nodiscard]] VkDescriptorPool CreateLsfgDescriptorPool(const Device& device, uint32_t max_sets);
[[nodiscard]] std::vector<VkDescriptorSet> AllocateLsfgDescriptorSets(
const Device& device, VkDescriptorPool pool, VkDescriptorSetLayout layout, uint32_t count);
[[nodiscard]] std::vector<VkDescriptorSet> AllocateLsfgDescriptorSets(
const Device& device, VkDescriptorPool pool,
const std::vector<VkDescriptorSetLayout>& layouts);
[[nodiscard]] VkSampler CreateLsfgSampler(const Device& device, VkSamplerAddressMode address_mode,
VkCompareOp compare_op, bool white_border);
}
@@ -0,0 +1,293 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2025 lsfg-vk
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Ported into Bannerlator's compositor from WinNative (GPL-3.0-or-later),
// whose LSFG port is credited to Camille LaVey / the Eden Emulator Project and
// follows upstream lsfg-vk. Only the Vulkan dispatch differs: Bannerlator
// resolves entry points through the renderer's own table (see lsfg_vkd.h).
#include "lsfg_delta.hpp"
#include "lsfg_shaders.hpp"
#include <vector>
namespace lsfg {
namespace {
constexpr uint32_t DISPATCH_TILE_SHIFT = 3;
[[nodiscard]] uint32_t GroupCount(uint32_t size) {
return (size + (1u << DISPATCH_TILE_SHIFT) - 1) >> DISPATCH_TILE_SHIFT;
}
}
LsfgDelta::LsfgDelta(const Device& device, const LsfgShaders& shaders, LsfgResources& resources,
VkDescriptorPool descriptor_pool, LsfgImageHistory& inputs_,
LsfgImage& flow_input_, LsfgImage* previous_gamma_, LsfgImage* previous1_,
LsfgImage* previous2_)
: inputs{&inputs_}, flow_input{&flow_input_}, previous_gamma{previous_gamma_},
previous1{previous1_}, previous2{previous2_} {
passes[0] = LsfgPass(device, shaders, LSFG_DELTA_SHADERS[0],
{{1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER},
{2, VK_DESCRIPTOR_TYPE_SAMPLER},
{5, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE},
{3, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE}});
passes[1] = LsfgPass(device, shaders, LSFG_DELTA_SHADERS[1],
{{1, VK_DESCRIPTOR_TYPE_SAMPLER},
{3, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE},
{2, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE}});
passes[2] = LsfgPass(device, shaders, LSFG_DELTA_SHADERS[2],
{{1, VK_DESCRIPTOR_TYPE_SAMPLER},
{2, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE},
{2, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE}});
passes[3] = LsfgPass(device, shaders, LSFG_DELTA_SHADERS[3],
{{1, VK_DESCRIPTOR_TYPE_SAMPLER},
{2, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE},
{2, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE}});
passes[4] = LsfgPass(device, shaders, LSFG_DELTA_SHADERS[4],
{{1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER},
{2, VK_DESCRIPTOR_TYPE_SAMPLER},
{4, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE},
{1, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE}});
passes[5] = LsfgPass(device, shaders, LSFG_DELTA_SHADERS[5],
{{1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER},
{2, VK_DESCRIPTOR_TYPE_SAMPLER},
{6, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE},
{1, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE}});
for (size_t i = 6; i < LSFG_DELTA_STAGES - 1; ++i) {
passes[i] = LsfgPass(device, shaders, LSFG_DELTA_SHADERS[i],
{{1, VK_DESCRIPTOR_TYPE_SAMPLER},
{1, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE},
{1, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE}});
}
passes[9] = LsfgPass(device, shaders, LSFG_DELTA_SHADERS[9],
{{1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER},
{2, VK_DESCRIPTOR_TYPE_SAMPLER},
{2, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE},
{1, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE}});
for (const auto& pass : passes) {
if (!pass.Valid()) return;
}
const VkExtent2D extent = (*inputs)[0][0].Extent();
for (auto& image : temp1) {
image = LsfgImage(device, extent);
if (!image.Valid()) return;
}
for (auto& image : temp2) {
image = LsfgImage(device, extent);
if (!image.Valid()) return;
}
out_image1 = LsfgImage(device, extent, LSFG_MOTION_FORMAT);
out_image2 = LsfgImage(device, extent, LSFG_MOTION_FORMAT);
if (!out_image1.Valid() || !out_image2.Valid()) return;
std::vector<VkDescriptorSetLayout> layouts;
for (size_t slot = 0; slot < LSFG_GENERATION_SLOTS; ++slot) {
for (size_t i = 0; i < LSFG_HISTORY_SLOTS; ++i) {
layouts.push_back(passes[0].SetLayout());
}
for (size_t i = 1; i <= 4; ++i) {
layouts.push_back(passes[i].SetLayout());
}
for (size_t i = 0; i < LSFG_HISTORY_SLOTS; ++i) {
layouts.push_back(passes[5].SetLayout());
}
for (size_t i = 6; i < LSFG_DELTA_STAGES; ++i) {
layouts.push_back(passes[i].SetLayout());
}
}
const std::vector<VkDescriptorSet> sets =
AllocateLsfgDescriptorSets(device, descriptor_pool, layouts);
if (sets.size() != layouts.size()) return;
const LsfgImage& dummy = resources.GetDummy(LSFG_MOTION_FORMAT);
const LsfgImage& previous_gamma_image = previous_gamma != nullptr ? *previous_gamma : dummy;
const LsfgImage& previous1_image = previous1 != nullptr ? *previous1 : dummy;
const LsfgImage& previous2_image = previous2 != nullptr ? *previous2 : dummy;
if (!dummy.Valid()) return;
const VkSampler sampler = resources.GetSampler();
const VkSampler border_sampler =
resources.GetSampler(VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER, VK_COMPARE_OP_NEVER, true);
const VkSampler edge_sampler =
resources.GetSampler(VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, VK_COMPARE_OP_ALWAYS, false);
size_t next = 0;
for (size_t slot = 0; slot < LSFG_GENERATION_SLOTS; ++slot) {
Generation& pass = generations[slot];
const VkBuffer buffer =
resources.GetBuffer(LsfgSlotTimestamp(slot), false, previous_gamma == nullptr);
for (size_t i = 0; i < LSFG_HISTORY_SLOTS; ++i) {
pass.first_descriptor_sets[i] = sets[next++];
}
for (size_t i = 0; i < 4; ++i) {
pass.descriptor_sets[i] = sets[next++];
}
for (size_t i = 0; i < LSFG_HISTORY_SLOTS; ++i) {
pass.sixth_descriptor_sets[i] = sets[next++];
}
for (size_t i = 4; i < LSFG_DELTA_STAGES - 2; ++i) {
pass.descriptor_sets[i] = sets[next++];
}
for (size_t i = 0; i < LSFG_HISTORY_SLOTS; ++i) {
LsfgDescriptorWriter(pass.first_descriptor_sets[i])
.AddUniformBuffer(buffer, LsfgResources::BufferSize())
.AddSampler(border_sampler)
.AddSampler(edge_sampler)
.AddSampledImages((*inputs)[(i + 2) % LSFG_HISTORY_SLOTS])
.AddSampledImages((*inputs)[i % LSFG_HISTORY_SLOTS])
.AddSampledImage(previous_gamma_image)
.AddStorageImages(temp1)
.Build(device);
LsfgDescriptorWriter(pass.sixth_descriptor_sets[i])
.AddUniformBuffer(buffer, LsfgResources::BufferSize())
.AddSampler(border_sampler)
.AddSampler(edge_sampler)
.AddSampledImages((*inputs)[(i + 2) % LSFG_HISTORY_SLOTS])
.AddSampledImages((*inputs)[i % LSFG_HISTORY_SLOTS])
.AddSampledImage(previous_gamma_image)
.AddSampledImage(previous1_image)
.AddStorageImage(temp2[0])
.Build(device);
}
LsfgDescriptorWriter(pass.descriptor_sets[0])
.AddSampler(sampler)
.AddSampledImages(temp1)
.AddStorageImages(temp2)
.Build(device);
LsfgDescriptorWriter(pass.descriptor_sets[1])
.AddSampler(sampler)
.AddSampledImages(temp2)
.AddStorageImage(temp1[0])
.AddStorageImage(temp1[1])
.Build(device);
LsfgDescriptorWriter(pass.descriptor_sets[2])
.AddSampler(sampler)
.AddSampledImage(temp1[0])
.AddSampledImage(temp1[1])
.AddStorageImages(temp2)
.Build(device);
LsfgDescriptorWriter(pass.descriptor_sets[3])
.AddUniformBuffer(buffer, LsfgResources::BufferSize())
.AddSampler(sampler)
.AddSampler(edge_sampler)
.AddSampledImages(temp2)
.AddSampledImage(previous_gamma_image)
.AddSampledImage(*flow_input)
.AddStorageImage(out_image1)
.Build(device);
LsfgDescriptorWriter(pass.descriptor_sets[4])
.AddSampler(sampler)
.AddSampledImage(temp2[0])
.AddStorageImage(temp1[0])
.Build(device);
LsfgDescriptorWriter(pass.descriptor_sets[5])
.AddSampler(sampler)
.AddSampledImage(temp1[0])
.AddStorageImage(temp2[0])
.Build(device);
LsfgDescriptorWriter(pass.descriptor_sets[6])
.AddSampler(sampler)
.AddSampledImage(temp2[0])
.AddStorageImage(temp1[0])
.Build(device);
LsfgDescriptorWriter(pass.descriptor_sets[7])
.AddUniformBuffer(buffer, LsfgResources::BufferSize())
.AddSampler(sampler)
.AddSampler(edge_sampler)
.AddSampledImage(temp1[0])
.AddSampledImage(previous2_image)
.AddStorageImage(out_image2)
.Build(device);
}
allocated = true;
}
void LsfgDelta::PushStepBarriers(LsfgBarriers& barriers, uint64_t frame_count, size_t step) {
const size_t history = frame_count % LSFG_HISTORY_SLOTS;
const size_t previous_history = (frame_count + 2) % LSFG_HISTORY_SLOTS;
switch (step) {
case 0:
barriers.WriteToReadAll((*inputs)[previous_history])
.WriteToReadAll((*inputs)[history])
.WriteToRead(previous_gamma)
.ReadToWriteAll(temp1);
break;
case 1:
barriers.WriteToReadAll(temp1).ReadToWriteAll(temp2);
break;
case 2:
barriers.WriteToReadAll(temp2).ReadToWriteAll(temp1);
break;
case 3:
barriers.WriteToReadAll(temp1).ReadToWriteAll(temp2);
break;
case 4:
barriers.WriteToReadAll(temp2)
.WriteToRead(previous_gamma)
.WriteToRead(*flow_input)
.ReadToWrite(out_image1);
break;
case 5:
barriers.WriteToReadAll((*inputs)[previous_history])
.WriteToReadAll((*inputs)[history])
.WriteToRead(previous_gamma)
.WriteToRead(previous1)
.ReadToWriteAll(temp2);
break;
case 6:
barriers.WriteToReadAll(temp2).ReadToWrite(temp1[0]).ReadToWrite(temp1[1]);
break;
case 7:
barriers.WriteToRead(temp1[0]).WriteToRead(temp1[1]).ReadToWriteAll(temp2);
break;
case 8:
barriers.WriteToReadAll(temp2).ReadToWrite(temp1[0]).ReadToWrite(temp1[1]);
break;
default:
barriers.WriteToRead(temp1[0])
.WriteToRead(temp1[1])
.WriteToRead(previous2)
.ReadToWrite(out_image2);
break;
}
}
void LsfgDelta::DispatchStep(VkCommandBuffer cmdbuf, uint64_t frame_count, size_t slot,
size_t step) {
const Generation& pass = generations[slot];
const VkExtent2D extent = temp1[0].Extent();
const size_t history = frame_count % LSFG_HISTORY_SLOTS;
if (step == 0) {
passes[0].Bind(cmdbuf, pass.first_descriptor_sets[history]);
} else if (step == 5) {
passes[5].Bind(cmdbuf, pass.sixth_descriptor_sets[history]);
} else if (step < 5) {
passes[step].Bind(cmdbuf, pass.descriptor_sets[step - 1]);
} else {
passes[step].Bind(cmdbuf, pass.descriptor_sets[step - 2]);
}
vkd.CmdDispatch(cmdbuf, GroupCount(extent.width), GroupCount(extent.height), 1);
}
void LsfgDelta::Dispatch(VkCommandBuffer cmdbuf, uint64_t frame_count, size_t slot) {
for (size_t step = 0; step < LSFG_DELTA_STAGES; ++step) {
LsfgBarriers barriers(cmdbuf);
PushStepBarriers(barriers, frame_count, step);
barriers.Build();
DispatchStep(cmdbuf, frame_count, slot, step);
}
}
}
@@ -0,0 +1,74 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2025 lsfg-vk
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Ported into Bannerlator's compositor from WinNative (GPL-3.0-or-later),
// whose LSFG port is credited to Camille LaVey / the Eden Emulator Project and
// follows upstream lsfg-vk. Only the Vulkan dispatch differs: Bannerlator
// resolves entry points through the renderer's own table (see lsfg_vkd.h).
#pragma once
#include <array>
#include "lsfg_common.hpp"
namespace lsfg {
class LsfgShaders;
constexpr size_t LSFG_DELTA_STAGES = 10;
constexpr size_t LSFG_DELTA_TEMPS = 3;
class LsfgDelta {
public:
LsfgDelta() = default;
LsfgDelta(const Device& device, const LsfgShaders& shaders, LsfgResources& resources,
VkDescriptorPool descriptor_pool, LsfgImageHistory& inputs, LsfgImage& flow_input,
LsfgImage* previous_gamma, LsfgImage* previous1, LsfgImage* previous2);
void Dispatch(VkCommandBuffer cmdbuf, uint64_t frame_count, size_t slot);
void PushStepBarriers(LsfgBarriers& barriers, uint64_t frame_count, size_t step);
void DispatchStep(VkCommandBuffer cmdbuf, uint64_t frame_count, size_t slot,
size_t step);
[[nodiscard]] LsfgImage& Output1() {
return out_image1;
}
[[nodiscard]] LsfgImage& Output2() {
return out_image2;
}
[[nodiscard]] bool Valid() const {
return allocated;
}
private:
struct Generation {
std::array<VkDescriptorSet, LSFG_HISTORY_SLOTS> first_descriptor_sets{};
std::array<VkDescriptorSet, LSFG_HISTORY_SLOTS> sixth_descriptor_sets{};
std::array<VkDescriptorSet, LSFG_DELTA_STAGES - 2> descriptor_sets{};
};
LsfgImageHistory* inputs{};
LsfgImage* flow_input{};
LsfgImage* previous_gamma{};
LsfgImage* previous1{};
LsfgImage* previous2{};
std::array<LsfgPass, LSFG_DELTA_STAGES> passes;
std::array<Generation, LSFG_GENERATION_SLOTS> generations{};
std::array<LsfgImage, LSFG_DELTA_TEMPS> temp1;
LsfgImagePair temp2;
LsfgImage out_image1;
LsfgImage out_image2;
bool allocated{};
};
}
+702
View File
@@ -0,0 +1,702 @@
// See lsfg_dll.h. PE resource walk + on-device SPIR-V cache for the Lossless
// Scaling frame-generation chain.
//
// Derived from WinNative's lsfg_dll.c (GPL-3.0-or-later), LSFG port credited to
// Camille LaVey / the Eden Emulator Project, following upstream lsfg-vk
// (PancakeTAS). Bannerlator is GPL-3.0.
#include "lsfg_dll.h"
#include "lsfg_dxbc.h"
#include <android/log.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define LOG_TAG "LsfgDll"
#define LSFG_LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LSFG_LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
namespace lsfg {
namespace {
// ---- PE constants ---------------------------------------------------------
constexpr uint16_t kDosMagic = 0x5A4Du; // "MZ"
constexpr uint32_t kPeSignature = 0x00004550u; // "PE\0\0"
constexpr uint16_t kPe32Magic = 0x010Bu;
constexpr uint16_t kPe32PlusMagic = 0x020Bu;
constexpr size_t kDosLfanewOffset = 0x3Cu;
constexpr size_t kCoffHeaderSize = 20u;
constexpr size_t kOptionalHeaderSizeOff = 16u;
constexpr size_t kDataDirectoryEntrySize = 8u;
constexpr size_t kDataDirectoryOffPe32 = 96u;
constexpr size_t kDataDirectoryOffPe32P = 112u;
constexpr size_t kResourceDirectoryIndex = 2u;
constexpr size_t kSectionHeaderSize = 40u;
constexpr size_t kResourceDirectorySize = 16u;
constexpr size_t kResourceNamedCountOff = 12u;
constexpr size_t kResourceIdCountOff = 14u;
constexpr size_t kResourceEntrySize = 8u;
constexpr uint32_t kResourceSubdirectoryBit = 0x80000000u;
constexpr uint32_t kResourceTypeRcData = 10u;
// ---- SPIR-V constants -----------------------------------------------------
constexpr uint32_t kSpirvMagic = 0x07230203u;
constexpr size_t kSpirvHeaderWords = 5u;
constexpr uint32_t kSpirvWordCountShift = 16u;
constexpr uint32_t kSpirvOpcodeMask = 0xFFFFu;
constexpr uint32_t kSpirvOpFunction = 54u;
constexpr uint32_t kSpirvOpDecorate = 71u;
constexpr uint32_t kDecorationBinding = 33u;
constexpr uint32_t kDecorationDescriptorSet = 34u;
constexpr size_t kDecorationLiteralWord = 3u;
constexpr uint32_t kVariantFp16Offset = 49u;
constexpr uint32_t kVariantFp32Offset = 98u;
constexpr uint32_t kMaxResourceId = 512u;
constexpr size_t kMaxSpirvWords = 16u * 1024u * 1024u;
constexpr uint32_t kCacheMagic = 0x4C534642u; // "BFSL" little-endian
constexpr uint32_t kCacheVersion = 1u;
struct CacheHeader {
uint32_t magic;
uint32_t version;
uint64_t sourceSize;
uint64_t sourceHash;
uint32_t moduleCount;
uint32_t variant;
};
struct PeSection {
uint32_t virtualAddress = 0;
uint32_t virtualSize = 0;
uint32_t rawAddress = 0;
uint32_t rawSize = 0;
};
// Read-only mapping of the DLL. Closes itself; the DLL is never executed.
class PeImage {
public:
~PeImage() { close(); }
bool open(const std::string& path) {
fd_ = ::open(path.c_str(), O_RDONLY | O_CLOEXEC);
if (fd_ < 0) return false;
struct stat info{};
if (fstat(fd_, &info) != 0 || info.st_size <= 0) { close(); return false; }
size_ = (size_t)info.st_size;
void* m = mmap(nullptr, size_, PROT_READ, MAP_PRIVATE, fd_, 0);
if (m == MAP_FAILED) { close(); return false; }
data_ = (const uint8_t*)m;
return true;
}
void close() {
if (data_) { munmap((void*)data_, size_); data_ = nullptr; }
if (fd_ >= 0) { ::close(fd_); fd_ = -1; }
size_ = 0;
sections_.clear();
}
const uint8_t* data() const { return data_; }
size_t size() const { return size_; }
bool readU16(size_t off, uint16_t& out) const {
if (off > size_ || size_ - off < sizeof(uint16_t)) return false;
memcpy(&out, data_ + off, sizeof(uint16_t));
return true;
}
bool readU32(size_t off, uint32_t& out) const {
if (off > size_ || size_ - off < sizeof(uint32_t)) return false;
memcpy(&out, data_ + off, sizeof(uint32_t));
return true;
}
bool findPeHeader(size_t& outOffset) const {
uint16_t dos = 0;
if (!readU16(0, dos) || dos != kDosMagic) return false;
uint32_t peOff = 0;
if (!readU32(kDosLfanewOffset, peOff)) return false;
uint32_t sig = 0;
if (!readU32(peOff, sig) || sig != kPeSignature) return false;
outOffset = (size_t)peOff;
return true;
}
bool readSections(size_t peOffset) {
uint16_t count = 0, optSize = 0;
if (!readU16(peOffset + 4 + 2, count) ||
!readU16(peOffset + 4 + kOptionalHeaderSizeOff, optSize)) return false;
if (count == 0) return false;
sections_.assign(count, PeSection{});
const size_t table = peOffset + 4 + kCoffHeaderSize + optSize;
for (uint16_t i = 0; i < count; i++) {
const size_t off = table + (size_t)i * kSectionHeaderSize;
PeSection& s = sections_[i];
if (!readU32(off + 8, s.virtualSize) ||
!readU32(off + 12, s.virtualAddress) ||
!readU32(off + 16, s.rawSize) ||
!readU32(off + 20, s.rawAddress)) { sections_.clear(); return false; }
}
return true;
}
bool rvaToOffset(uint32_t rva, size_t& outOffset) const {
for (const PeSection& s : sections_) {
const uint32_t span = s.virtualSize > s.rawSize ? s.virtualSize : s.rawSize;
if (span == 0 || rva < s.virtualAddress) continue;
const uint32_t rel = rva - s.virtualAddress;
if (rel < span) { outOffset = (size_t)s.rawAddress + rel; return true; }
}
return false;
}
private:
int fd_ = -1;
const uint8_t* data_ = nullptr;
size_t size_ = 0;
std::vector<PeSection> sections_;
};
// RCDATA id -> blob, for the ids we care about.
struct ResourceTable {
const uint8_t* data[kMaxResourceId] = {};
uint32_t size[kMaxResourceId] = {};
};
uint64_t fnv1a64(const uint8_t* data, size_t size) {
uint64_t hash = 1469598103934665603ULL;
for (size_t i = 0; i < size; i++) {
hash ^= (uint64_t)data[i];
hash *= 1099511628211ULL;
}
return hash;
}
bool resourceEntryCount(const PeImage& img, size_t dirOffset, size_t& outTotal) {
uint16_t named = 0, ids = 0;
if (!img.readU16(dirOffset + kResourceNamedCountOff, named) ||
!img.readU16(dirOffset + kResourceIdCountOff, ids)) return false;
outTotal = (size_t)named + (size_t)ids;
return true;
}
bool resourceEntryAt(const PeImage& img, size_t dirOffset, size_t index,
uint32_t& outId, uint32_t& outOffset,
bool& outIsDirectory, bool& outIsNamed) {
const size_t off = dirOffset + kResourceDirectorySize + index * kResourceEntrySize;
uint32_t name = 0, data = 0;
if (!img.readU32(off, name) || !img.readU32(off + 4, data)) return false;
outId = name & ~kResourceSubdirectoryBit;
outOffset = data & ~kResourceSubdirectoryBit;
outIsDirectory = (data & kResourceSubdirectoryBit) != 0;
outIsNamed = (name & kResourceSubdirectoryBit) != 0;
return true;
}
bool resourceReadLeaf(const PeImage& img, size_t leafOffset,
const uint8_t*& outData, uint32_t& outSize) {
uint32_t rva = 0, size = 0;
if (!img.readU32(leafOffset, rva) || !img.readU32(leafOffset + 4, size) || size == 0)
return false;
size_t dataOffset = 0;
if (!img.rvaToOffset(rva, dataOffset)) return false;
if (dataOffset > img.size() || img.size() - dataOffset < size) return false;
outData = img.data() + dataOffset;
outSize = size;
return true;
}
// Walk type -> name -> language and keep the first language of every RCDATA id.
bool collectRcData(const PeImage& img, size_t resourceBase, ResourceTable& out) {
size_t typeTotal = 0;
if (!resourceEntryCount(img, resourceBase, typeTotal)) return false;
for (size_t t = 0; t < typeTotal; t++) {
uint32_t typeId = 0, typeOffset = 0;
bool typeIsDir = false, typeIsNamed = false;
if (!resourceEntryAt(img, resourceBase, t, typeId, typeOffset, typeIsDir, typeIsNamed))
return false;
if (typeIsNamed || typeId != kResourceTypeRcData || !typeIsDir) continue;
const size_t nameBase = resourceBase + typeOffset;
size_t nameTotal = 0;
if (!resourceEntryCount(img, nameBase, nameTotal)) return false;
for (size_t n = 0; n < nameTotal; n++) {
uint32_t nameId = 0, nameOffset = 0;
bool nameIsDir = false, nameIsNamed = false;
if (!resourceEntryAt(img, nameBase, n, nameId, nameOffset, nameIsDir, nameIsNamed))
return false;
if (nameIsNamed || !nameIsDir || nameId >= kMaxResourceId) continue;
const size_t langBase = resourceBase + nameOffset;
size_t langTotal = 0;
if (!resourceEntryCount(img, langBase, langTotal)) return false;
for (size_t l = 0; l < langTotal; l++) {
uint32_t langId = 0, langOffset = 0;
bool langIsDir = false, langIsNamed = false;
if (!resourceEntryAt(img, langBase, l, langId, langOffset, langIsDir, langIsNamed))
return false;
if (langIsDir) continue;
const uint8_t* blob = nullptr;
uint32_t blobSize = 0;
if (!resourceReadLeaf(img, resourceBase + langOffset, blob, blobSize)) continue;
out.data[nameId] = blob;
out.size[nameId] = blobSize;
break; // first language wins
}
}
}
return true;
}
DllStatus parseResources(PeImage& img, ResourceTable& table) {
size_t peOffset = 0;
if (!img.findPeHeader(peOffset)) return DllStatus::NotPortableExecutable;
if (!img.readSections(peOffset)) return DllStatus::NotPortableExecutable;
uint16_t optionalMagic = 0;
const size_t optionalHeaderOffset = peOffset + 4 + kCoffHeaderSize;
if (!img.readU16(optionalHeaderOffset, optionalMagic)) return DllStatus::NotPortableExecutable;
size_t dataDirectory = 0;
if (optionalMagic == kPe32Magic) dataDirectory = optionalHeaderOffset + kDataDirectoryOffPe32;
else if (optionalMagic == kPe32PlusMagic) dataDirectory = optionalHeaderOffset + kDataDirectoryOffPe32P;
else return DllStatus::NotPortableExecutable;
uint32_t resourceRva = 0;
if (!img.readU32(dataDirectory + kResourceDirectoryIndex * kDataDirectoryEntrySize, resourceRva)
|| resourceRva == 0) {
return DllStatus::MissingShaders;
}
size_t resourceBase = 0;
if (!img.rvaToOffset(resourceRva, resourceBase)) return DllStatus::MissingShaders;
if (!collectRcData(img, resourceBase, table)) return DllStatus::MissingShaders;
return DllStatus::Ok;
}
// ---- SPIR-V adoption (precompiled-variant path) ---------------------------
bool isSpirvModule(const uint8_t* blob, uint32_t size) {
if (!blob || size < kSpirvHeaderWords * sizeof(uint32_t)) return false;
if (size % sizeof(uint32_t) != 0) return false;
uint32_t magic = 0;
memcpy(&magic, blob, sizeof(magic));
return magic == kSpirvMagic;
}
uint32_t lookupDescriptorSet(const std::vector<uint32_t>& w, uint32_t targetId) {
size_t off = kSpirvHeaderWords;
while (off < w.size()) {
const uint32_t length = w[off] >> kSpirvWordCountShift;
const uint32_t opcode = w[off] & kSpirvOpcodeMask;
if (length == 0 || off + length > w.size()) break;
if (opcode == kSpirvOpFunction) break;
if (opcode == kSpirvOpDecorate && length >= 4 &&
w[off + 2] == kDecorationDescriptorSet && w[off + 1] == targetId) {
return w[off + 3];
}
off += length;
}
return 0;
}
// Precompiled blobs are renumbered into set/binding order - that is the
// convention they were built with. The DXBC path uses encounter order instead
// (see lsfg_dxbc), which is what DXVK's output pairs with. Do not merge them.
bool renumberBindingsSetOrder(std::vector<uint32_t>& w) {
struct Slot { uint32_t set; uint32_t binding; size_t literalOffset; };
std::vector<Slot> slots;
size_t off = kSpirvHeaderWords;
while (off < w.size()) {
const uint32_t length = w[off] >> kSpirvWordCountShift;
const uint32_t opcode = w[off] & kSpirvOpcodeMask;
if (length == 0 || off + length > w.size()) return false;
if (opcode == kSpirvOpFunction) break;
if (opcode == kSpirvOpDecorate && length >= 4 && w[off + 2] == kDecorationBinding) {
slots.push_back(Slot{ lookupDescriptorSet(w, w[off + 1]),
w[off + 3],
off + kDecorationLiteralWord });
}
off += length;
}
if (slots.empty()) return true;
std::sort(slots.begin(), slots.end(), [](const Slot& a, const Slot& b) {
return a.set != b.set ? a.set < b.set : a.binding < b.binding;
});
for (size_t i = 0; i < slots.size(); i++) w[slots[i].literalOffset] = (uint32_t)i;
return true;
}
bool adoptSpirv(const uint8_t* blob, uint32_t size, std::vector<uint32_t>& outWords) {
if (!isSpirvModule(blob, size)) return false;
const size_t wordCount = size / sizeof(uint32_t);
if (wordCount > kMaxSpirvWords) return false;
outWords.resize(wordCount);
memcpy(outWords.data(), blob, wordCount * sizeof(uint32_t));
if (!renumberBindingsSetOrder(outWords)) { outWords.clear(); return false; }
return true;
}
bool hasNativeVariant(const ResourceTable& t, uint32_t variantOffset) {
for (uint32_t id : shaderIds()) {
const uint32_t vid = id + variantOffset;
if (vid >= kMaxResourceId) return false;
if (!isSpirvModule(t.data[vid], t.size[vid])) return false;
}
return true;
}
bool hasBaseChain(const ResourceTable& t) {
for (uint32_t id : shaderIds()) {
if (id >= kMaxResourceId || t.data[id] == nullptr) return false;
}
return true;
}
Variant selectVariant(const ResourceTable& t, bool preferFp16) {
if (preferFp16 && hasNativeVariant(t, kVariantFp16Offset)) return Variant::SpirvFp16;
if (hasNativeVariant(t, kVariantFp32Offset)) return Variant::SpirvFp32;
if (hasNativeVariant(t, kVariantFp16Offset)) return Variant::SpirvFp16;
return Variant::None;
}
uint32_t variantOffset(Variant v) {
return v == Variant::SpirvFp16 ? kVariantFp16Offset : kVariantFp32Offset;
}
bool writeCache(const std::string& cachePath, const CacheHeader& header, const ModuleSet& set) {
const std::string tempPath = cachePath + ".tmp";
FILE* f = fopen(tempPath.c_str(), "wb");
if (!f) return false;
bool ok = fwrite(&header, sizeof(header), 1, f) == 1;
for (size_t i = 0; ok && i < set.modules.size(); i++) {
const Module& m = set.modules[i];
const uint32_t wordCount = (uint32_t)m.words.size();
ok = fwrite(&m.id, sizeof(m.id), 1, f) == 1
&& fwrite(&wordCount, sizeof(wordCount), 1, f) == 1
&& fwrite(m.words.data(), sizeof(uint32_t), wordCount, f) == wordCount;
}
if (ok) ok = fflush(f) == 0;
if (ok) ok = fsync(fileno(f)) == 0;
fclose(f);
// Temp file + rename, so a failed or interrupted build can never leave a
// half-written cache that would later be loaded as if it were complete.
if (!ok || rename(tempPath.c_str(), cachePath.c_str()) != 0) {
unlink(tempPath.c_str());
return false;
}
return true;
}
bool readCacheHeader(const std::string& cachePath, CacheHeader& out) {
FILE* f = fopen(cachePath.c_str(), "rb");
if (!f) return false;
const bool ok = fread(&out, sizeof(out), 1, f) == 1;
fclose(f);
return ok && out.magic == kCacheMagic && out.version == kCacheVersion;
}
} // namespace
// ---- public API -----------------------------------------------------------
const std::vector<uint32_t>& shaderIds() {
static const std::vector<uint32_t> ids = [] {
std::vector<uint32_t> v;
v.reserve(kShaderCount);
v.push_back(kShaderMipmaps);
v.push_back(kShaderGenerate);
for (uint32_t id = kShaderChainFirst; id <= kShaderChainLast; id++) v.push_back(id);
return v;
}();
return ids;
}
const std::vector<uint32_t>* ModuleSet::find(uint32_t id) const {
for (const Module& m : modules) if (m.id == id) return &m.words;
return nullptr;
}
bool ModuleSet::complete() const {
if (modules.size() != kShaderCount) return false;
for (uint32_t id : shaderIds()) if (!find(id)) return false;
return true;
}
const char* statusName(DllStatus s) {
switch (s) {
case DllStatus::Ok: return "ok";
case DllStatus::NotInstalled: return "not installed";
case DllStatus::UnreadableFile: return "unreadable file";
case DllStatus::NotPortableExecutable: return "not a PE file";
case DllStatus::MissingShaders: return "missing shaders";
case DllStatus::TranslationFailed: return "shader translation failed";
case DllStatus::CacheUnusable: return "cache unusable";
}
return "unknown";
}
const char* variantName(Variant v) {
switch (v) {
case Variant::SpirvFp16: return "spirv-fp16";
case Variant::SpirvFp32: return "spirv-fp32";
case Variant::DxbcTranslated: return "dxbc-translated";
case Variant::None: return "none";
}
return "none";
}
DllStatus validateDll(const std::string& dllPath) {
if (dllPath.empty()) return DllStatus::NotInstalled;
PeImage img;
if (!img.open(dllPath)) return DllStatus::NotInstalled;
ResourceTable table;
DllStatus status = parseResources(img, table);
if (status == DllStatus::Ok && !hasBaseChain(table)) status = DllStatus::MissingShaders;
return status;
}
Variant dllVariant(const std::string& dllPath, bool preferFp16) {
if (dllPath.empty()) return Variant::None;
PeImage img;
if (!img.open(dllPath)) return Variant::None;
ResourceTable table;
if (parseResources(img, table) != DllStatus::Ok) return Variant::None;
Variant v = selectVariant(table, preferFp16);
if (v == Variant::None && hasBaseChain(table)) v = Variant::DxbcTranslated;
return v;
}
DllStatus buildCache(const std::string& dllPath, const std::string& cachePath, bool preferFp16) {
if (dllPath.empty() || cachePath.empty()) return DllStatus::NotInstalled;
PeImage img;
if (!img.open(dllPath)) return DllStatus::NotInstalled;
ResourceTable table;
DllStatus status = parseResources(img, table);
if (status != DllStatus::Ok) return status;
const Variant precompiled = selectVariant(table, preferFp16);
const bool translate = precompiled == Variant::None;
if (translate && !hasBaseChain(table)) return DllStatus::MissingShaders;
ModuleSet set;
set.variant = translate ? Variant::DxbcTranslated : precompiled;
set.modules.reserve(kShaderCount);
const uint32_t offset = translate ? 0u : variantOffset(precompiled);
for (uint32_t id : shaderIds()) {
const uint32_t resourceId = id + offset;
if (resourceId >= kMaxResourceId) return DllStatus::MissingShaders;
Module m;
m.id = id;
const bool ok = translate
? translateDxbc(table.data[resourceId], table.size[resourceId], m.words)
: adoptSpirv(table.data[resourceId], table.size[resourceId], m.words);
if (!ok || m.words.empty()) {
LSFG_LOGE("shader %u (%s) failed", id, translate ? "dxbc" : "spirv");
return DllStatus::TranslationFailed;
}
set.modules.push_back(std::move(m));
}
CacheHeader header{};
header.magic = kCacheMagic;
header.version = kCacheVersion;
header.sourceSize = (uint64_t)img.size();
header.sourceHash = fnv1a64(img.data(), img.size());
header.moduleCount = (uint32_t)set.modules.size();
header.variant = (uint32_t)set.variant;
if (!writeCache(cachePath, header, set)) {
LSFG_LOGE("cache write failed: %s", cachePath.c_str());
return DllStatus::CacheUnusable;
}
size_t totalWords = 0;
for (const Module& m : set.modules) totalWords += m.words.size();
LSFG_LOGI("cached %zu LSFG modules, %zu SPIR-V words (%s)",
set.modules.size(), totalWords, variantName(set.variant));
return DllStatus::Ok;
}
DllStatus cacheMatchesSource(const std::string& cachePath, const std::string& dllPath,
bool& outMatches) {
outMatches = false;
if (cachePath.empty() || dllPath.empty()) return DllStatus::CacheUnusable;
CacheHeader header{};
if (!readCacheHeader(cachePath, header)) return DllStatus::CacheUnusable;
PeImage img;
if (!img.open(dllPath)) return DllStatus::NotInstalled;
outMatches = header.sourceSize == (uint64_t)img.size()
&& header.sourceHash == fnv1a64(img.data(), img.size());
return DllStatus::Ok;
}
DllStatus cacheVariant(const std::string& cachePath, Variant& outVariant) {
outVariant = Variant::None;
CacheHeader header{};
if (!readCacheHeader(cachePath, header)) return DllStatus::CacheUnusable;
if (header.variant > (uint32_t)Variant::DxbcTranslated) return DllStatus::CacheUnusable;
outVariant = (Variant)header.variant;
return DllStatus::Ok;
}
DllStatus loadModules(const std::string& cachePath, ModuleSet& outSet) {
outSet.modules.clear();
outSet.variant = Variant::None;
if (cachePath.empty()) return DllStatus::CacheUnusable;
FILE* f = fopen(cachePath.c_str(), "rb");
if (!f) return DllStatus::NotInstalled;
CacheHeader header{};
if (fread(&header, sizeof(header), 1, f) != 1 || header.magic != kCacheMagic ||
header.version != kCacheVersion || header.moduleCount != kShaderCount) {
fclose(f);
return DllStatus::CacheUnusable;
}
outSet.variant = (Variant)header.variant;
outSet.modules.reserve(header.moduleCount);
DllStatus status = DllStatus::Ok;
for (uint32_t i = 0; i < header.moduleCount; i++) {
uint32_t id = 0, wordCount = 0;
if (fread(&id, sizeof(id), 1, f) != 1 ||
fread(&wordCount, sizeof(wordCount), 1, f) != 1 ||
wordCount == 0 || wordCount > kMaxSpirvWords) {
status = DllStatus::CacheUnusable;
break;
}
Module m;
m.id = id;
m.words.resize(wordCount);
if (fread(m.words.data(), sizeof(uint32_t), wordCount, f) != wordCount) {
status = DllStatus::CacheUnusable;
break;
}
outSet.modules.push_back(std::move(m));
}
fclose(f);
if (status == DllStatus::Ok && !outSet.complete()) status = DllStatus::MissingShaders;
if (status != DllStatus::Ok) { outSet.modules.clear(); outSet.variant = Variant::None; }
return status;
}
// ---- SPIR-V version lowering (Vulkan 1.1/1.2 compat) ----------------------
namespace {
constexpr uint32_t kSpirvOpExtension = 10u;
constexpr uint32_t kSpirvOpExtInstImport= 11u;
constexpr uint32_t kSpirvOpMemoryModel = 14u;
constexpr uint32_t kSpirvOpCapability = 17u;
// Capabilities that are core in the version the translator emits but not in
// the versions we lower to.
constexpr uint32_t kCapVulkanMemoryModel = 5345u; // core from 1.5
constexpr uint32_t kCapVulkanMemoryModelDeviceScope = 5346u; // core from 1.5
constexpr uint32_t kCapDemoteToHelperInvocation = 5379u; // core from 1.6
std::vector<uint32_t> encodeOpExtension(const char* name) {
const size_t len = strlen(name) + 1; // incl. NUL
const size_t strWords = (len + 3) / 4;
std::vector<uint32_t> ins(1 + strWords, 0u);
ins[0] = ((uint32_t)(1 + strWords) << kSpirvWordCountShift) | kSpirvOpExtension;
memcpy(&ins[1], name, len);
return ins;
}
bool hasOpExtension(const std::vector<uint32_t>& w, const char* name) {
size_t off = kSpirvHeaderWords;
while (off < w.size()) {
const uint32_t length = w[off] >> kSpirvWordCountShift;
const uint32_t opcode = w[off] & kSpirvOpcodeMask;
if (length == 0 || off + length > w.size()) return false;
if (opcode == kSpirvOpFunction || opcode == kSpirvOpMemoryModel) break;
if (opcode == kSpirvOpExtension && length >= 2) {
const char* s = reinterpret_cast<const char*>(&w[off + 1]);
if (strncmp(s, name, (length - 1) * 4) == 0) return true;
}
off += length;
}
return false;
}
} // namespace
bool downgradeSpirv(std::vector<uint32_t>& w, uint32_t targetVersion) {
if (w.size() < kSpirvHeaderWords || w[0] != kSpirvMagic) return false;
if (targetVersion < kSpirv14) return false;
if (w[1] <= targetVersion) return true; // already low enough
// Walk the preamble first and only touch the module once it is known to
// be lowerable: note which extension-gated capabilities are declared and
// where the capability block ends (OpExtension must follow every
// OpCapability and precede OpExtInstImport / OpMemoryModel).
bool needMemoryModel = false;
size_t insertAt = kSpirvHeaderWords;
size_t off = kSpirvHeaderWords;
while (off < w.size()) {
const uint32_t length = w[off] >> kSpirvWordCountShift;
const uint32_t opcode = w[off] & kSpirvOpcodeMask;
if (length == 0 || off + length > w.size()) return false;
if (opcode == kSpirvOpCapability && length >= 2) {
const uint32_t cap = w[off + 1];
if (cap == kCapVulkanMemoryModel || cap == kCapVulkanMemoryModelDeviceScope) needMemoryModel = true;
// Core only in 1.6, and VK_EXT_shader_demote_to_helper_invocation
// is never enabled on the compat path: refuse rather than hand the
// driver a module that is not legal at the lowered version.
if (cap == kCapDemoteToHelperInvocation) return false;
insertAt = off + length;
} else if (opcode == kSpirvOpExtension) {
insertAt = off + length;
} else if (opcode == kSpirvOpExtInstImport || opcode == kSpirvOpMemoryModel
|| opcode == kSpirvOpFunction) {
break;
}
off += length;
}
// 1.5 has the memory model as core; only 1.4 needs it declared.
if (targetVersion < kSpirv15 && needMemoryModel
&& !hasOpExtension(w, "SPV_KHR_vulkan_memory_model")) {
const auto e = encodeOpExtension("SPV_KHR_vulkan_memory_model");
w.insert(w.begin() + (std::ptrdiff_t)insertAt, e.begin(), e.end());
}
w[1] = targetVersion;
return true;
}
} // namespace lsfg
+134
View File
@@ -0,0 +1,134 @@
#pragma once
// ============================================================================
// lsfg_dll - extract the Lossless Scaling frame-generation shader chain from
// the user's own Lossless.dll, and cache it on device as SPIR-V.
//
// The 25 compute shaders that make up LSFG 3.1 are NOT redistributable. They
// ship as RCDATA resources inside Lossless.dll from the user's own paid copy
// of Lossless Scaling (Steam app 993090). Every implementation - upstream
// lsfg-vk, LSFG-Android, eden, WinNative - requires the user to supply their
// own DLL and extracts the resources on device. Bannerlator does the same, and
// bundles nothing.
//
// The DLL is mmap'd READ-ONLY and parsed as data. It is never loaded as a
// library and never executed.
//
// Resource layout, measured against real installs:
// * The base chain lives at RCDATA ids 255, 256 and 280-302 (25 modules),
// and is DXBC. It has always been DXBC.
// * Lossless Scaling MAY additionally carry precompiled SPIR-V copies at
// base+49 (native fp16) and base+98 (native fp32). No build currently
// downloadable from Steam carries them - public buildId 19655272 was
// measured with zero SPIR-V anywhere in the 311 MB install - so the DXBC
// path is the normal path and the translator is not optional. The SPIR-V
// path is kept because it costs nothing and a future build may ship them.
//
// Both producers fill the same shader-id -> SPIR-V words map, so everything
// downstream of this file is source-agnostic.
//
// Derived from WinNative's lsfg_dll.c (GPL-3.0-or-later), whose LSFG port is
// credited to Camille LaVey / the Eden Emulator Project, and which follows
// upstream lsfg-vk (PancakeTAS). Bannerlator is GPL-3.0; see THIRD-PARTY-LICENSES.md.
// ============================================================================
#include <cstdint>
#include <string>
#include <vector>
namespace lsfg {
enum class DllStatus {
Ok = 0,
NotInstalled, // no such file / cannot open
UnreadableFile, // opens but cannot be mapped or is empty
NotPortableExecutable,
MissingShaders, // parses, but the base chain is not all there
TranslationFailed, // a shader failed DXBC->SPIR-V or SPIR-V adoption
CacheUnusable // cache could not be written or read back
};
// SPIR-V version words. The translator emits 1.6; a Vulkan 1.1 device takes
// 1.4 (via VK_KHR_spirv_1_4) and a 1.2 device 1.5. One definition for the
// probe, the engine and the shader loader.
constexpr uint32_t kSpirv14 = 0x00010400u;
constexpr uint32_t kSpirv15 = 0x00010500u;
constexpr uint32_t kSpirv16 = 0x00010600u;
enum class Variant {
None = 0,
SpirvFp16, // precompiled fp16 blobs at base+49
SpirvFp32, // precompiled fp32 blobs at base+98
DxbcTranslated // base chain, translated on device (the normal case)
};
// The 25 shader ids that make up the chain.
constexpr uint32_t kShaderMipmaps = 255u;
constexpr uint32_t kShaderGenerate = 256u;
constexpr uint32_t kShaderChainFirst= 280u;
constexpr uint32_t kShaderChainLast = 302u;
constexpr uint32_t kShaderCount = 25u;
// Upstream spelling for the two ids the ported chain files refer to by name.
// Kept so those files stay byte-close to their upstream form; these alias the
// constants above rather than being a second source of truth.
constexpr uint32_t LSFG_SHADER_MIPMAPS = kShaderMipmaps;
constexpr uint32_t LSFG_SHADER_GENERATE = kShaderGenerate;
struct Module {
uint32_t id = 0;
std::vector<uint32_t> words; // SPIR-V
};
struct ModuleSet {
std::vector<Module> modules;
Variant variant = Variant::None;
const std::vector<uint32_t>* find(uint32_t id) const;
bool complete() const;
};
// The canonical shader-id list, in dispatch-independent order.
const std::vector<uint32_t>& shaderIds();
const char* statusName(DllStatus s);
const char* variantName(Variant v);
// Does this file look like a usable Lossless.dll? Checks the base chain only,
// so a DLL without the SPIR-V variants (i.e. every build on Steam today) is
// reported as valid - the translator handles it.
DllStatus validateDll(const std::string& dllPath);
// Which producer would be used for this DLL.
Variant dllVariant(const std::string& dllPath, bool preferFp16);
// Parse the DLL, produce all 25 SPIR-V modules, and write them to cachePath
// (via temp file + rename, so a failed build cannot leave a half-written
// cache behind). Records which producer ran in the cache header.
DllStatus buildCache(const std::string& dllPath, const std::string& cachePath, bool preferFp16);
// Is the cache current for this DLL? Compares source size + content hash.
DllStatus cacheMatchesSource(const std::string& cachePath, const std::string& dllPath,
bool& outMatches);
// Load a previously built cache.
DllStatus loadModules(const std::string& cachePath, ModuleSet& outSet);
// Which producer built an existing cache, without loading the modules.
DllStatus cacheVariant(const std::string& cachePath, Variant& outVariant);
// Experimental Vulkan 1.1 / 1.2 compat: lower a DXBC-translated module's
// declared SPIR-V version to `targetVersion` (kSpirv14 or kSpirv15). Below 1.5
// the Vulkan memory model is not core, so SPV_KHR_vulkan_memory_model is
// declared for it (the device side enables VK_KHR_vulkan_memory_model). The
// cache is built without a device in hand, always at the translator's native
// 1.6, so this runs at load time against whatever the device can take. A
// module already at or below the target is left untouched. Returns false,
// leaving the module untouched, for a malformed module, a target below 1.4, or
// a module declaring DemoteToHelperInvocation: that capability is core only in
// 1.6 and its extension is never enabled on the compat path, so such a module
// cannot be made legal here (DXVK emits it for pixel-shader discard only, so
// the 25 compute shaders never carry it). Whether the device then accepts the
// result is vkCreateShaderModule's / vkCreateComputePipelines' verdict.
bool downgradeSpirv(std::vector<uint32_t>& words, uint32_t targetVersion);
} // namespace lsfg
@@ -0,0 +1,84 @@
// See lsfg_dxbc.h. DXBC -> SPIR-V via DXVK's vendored translator.
//
// Follows upstream lsfg-vk's src/extract/trans.cpp, including its
// encounter-order binding renumber. Derived from WinNative's lsfg_dxbc.cpp
// (GPL-3.0-or-later); the translator itself is DXVK's, zlib licensed, vendored
// under cpp/thirdparty/dxbc.
#include "lsfg_dxbc.h"
#include <algorithm>
#include <cstring>
#include <dxbc_modinfo.h>
#include <dxbc_module.h>
#include <dxbc_reader.h>
#include <thirdparty/spirv.hpp>
namespace lsfg {
namespace {
struct BindingSlot {
uint32_t bindingIndex = 0;
uint32_t bindingOffset = 0; // word offset of the Binding literal
uint32_t setIndex = 0;
uint32_t setOffset = 0;
};
} // namespace
bool translateDxbc(const uint8_t* bytecode, uint32_t size, std::vector<uint32_t>& outWords) {
outWords.clear();
if (!bytecode || size < 4) return false;
try {
dxvk::DxbcReader reader(reinterpret_cast<const char*>(bytecode), size);
dxvk::DxbcModule module(reader);
const dxvk::DxbcModuleInfo info{};
auto code = module.compile(info, "CS");
// Collect the descriptor decorations in the order the Binding
// decorations appear. Everything before the first OpFunction is the
// decoration block, so the scan stops there.
std::vector<BindingSlot> slotsByVarId;
std::vector<uint32_t> varIdsInOrder;
for (auto ins : code) {
if (ins.opCode() == spv::OpDecorate) {
const uint32_t varId = ins.arg(1);
if (ins.arg(2) == spv::DecorationBinding) {
slotsByVarId.resize(std::max(slotsByVarId.size(), (size_t)varId + 1));
slotsByVarId[varId].bindingIndex = ins.arg(3);
slotsByVarId[varId].bindingOffset = ins.offset() + 3;
varIdsInOrder.push_back(varId);
} else if (ins.arg(2) == spv::DecorationDescriptorSet) {
slotsByVarId.resize(std::max(slotsByVarId.size(), (size_t)varId + 1));
slotsByVarId[varId].setIndex = ins.arg(3);
slotsByVarId[varId].setOffset = ins.offset() + 3;
}
}
if (ins.opCode() == spv::OpFunction) break;
}
// Renumber to a dense 0..n range, in encounter order.
uint32_t next = 0;
for (const uint32_t varId : varIdsInOrder) {
const BindingSlot& slot = slotsByVarId[varId];
if (slot.bindingOffset == 0) continue;
code.data()[slot.bindingOffset] = next++;
}
const size_t byteSize = code.size();
if (byteSize == 0 || byteSize % sizeof(uint32_t) != 0) return false;
outWords.resize(byteSize / sizeof(uint32_t));
std::memcpy(outWords.data(), code.data(), byteSize);
return true;
} catch (...) {
// DXVK's compiler signals malformed bytecode by throwing. A bad
// resource must degrade to "this DLL is unusable", never to a crash.
outWords.clear();
return false;
}
}
} // namespace lsfg
@@ -0,0 +1,39 @@
#pragma once
// ============================================================================
// lsfg_dxbc - DXBC -> SPIR-V for the Lossless Scaling shader chain.
//
// Why this exists at all: the base chain inside Lossless.dll is DXBC, and has
// always been DXBC. Lossless Scaling 3.2.2's release note ("Added shaders
// intended for use by the lsfg-vk project") describes precompiled SPIR-V
// copies at RCDATA base+49 / base+98 - but no build downloadable from Steam
// carries them. Measured against the current public build (buildId 19655272,
// 2025-08-19; linux_testing byte-identical on depot 993091): the whole 311 MB
// install contains zero occurrences of the SPIR-V magic word, and the DLL's
// 202 RCDATA entries are all DXBC.
//
// So a translator is required, not optional. Upstream lsfg-vk reaches the same
// conclusion and links DXVK's `dxbc` in src/extract/trans.cpp; we vendor the
// same subset (zlib licence) under cpp/thirdparty/dxbc and follow its
// trans.cpp step for step.
//
// The binding renumber here is ENCOUNTER ORDER - the order in which Binding
// decorations appear in DXVK's output - which is what DXVK's own layout pairs
// with. The precompiled-SPIR-V path in lsfg_dll.cpp uses a set/binding sort
// instead, because those blobs were built with that convention. The two must
// not be merged.
// ============================================================================
#include <cstdint>
#include <vector>
namespace lsfg {
// Translate one DXBC compute shader to SPIR-V, renumbering descriptor bindings
// into a dense 0..n range in encounter order. Returns false (leaving outWords
// empty) on any malformed input - DXVK's compiler throws, and everything is
// caught here.
// Emits DXVK's native SPIR-V 1.6; lowering for a Vulkan 1.1/1.2 device happens
// at load time (lsfg_dll.h downgradeSpirv).
bool translateDxbc(const uint8_t* bytecode, uint32_t size, std::vector<uint32_t>& outWords);
} // namespace lsfg
@@ -0,0 +1,320 @@
// See lsfg_engine.h.
//
// Ported from WinNative's vkr_lsfg.cpp (GPL-3.0-or-later), LSFG port credited
// to Camille LaVey / the Eden Emulator Project, following upstream lsfg-vk.
#include "lsfg_engine.h"
#include "lsfg_chain.hpp"
#include "lsfg_shaders.hpp"
#include "lsfg_vkd.h"
#include <algorithm>
#include <cmath>
#include <android/log.h>
#define LSFG_LOGI(...) __android_log_print(ANDROID_LOG_INFO, "LsfgEngine", __VA_ARGS__)
#define LSFG_LOGW(...) __android_log_print(ANDROID_LOG_WARN, "LsfgEngine", __VA_ARGS__)
namespace lsfg {
namespace {
// The chain needs two real frames in its history before anything it produces
// is meaningful, and we require the plan to stay warm for a couple of frames
// so a single settled sample cannot start generation from garbage.
constexpr uint64_t kRequiredFrames = 2;
constexpr uint32_t kRecurrenceFrames = 2;
constexpr uint64_t kTelemetryInterval = 120;
constexpr float kFlowScaleMin = 0.25f;
constexpr float kFlowScaleMax = 1.0f;
constexpr float kFlowScaleSteps = 20.0f;
VkImageMemoryBarrier makeTransition(VkImage image, VkAccessFlags srcAccess,
VkAccessFlags dstAccess, VkImageLayout oldLayout,
VkImageLayout newLayout) {
VkImageMemoryBarrier b{};
b.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
b.srcAccessMask = srcAccess;
b.dstAccessMask = dstAccess;
b.oldLayout = oldLayout;
b.newLayout = newLayout;
b.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
b.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
b.image = image;
b.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
b.subresourceRange.levelCount = 1;
b.subresourceRange.layerCount = 1;
return b;
}
// Copy the just-composited frame into the chain's input ring. `source` is the
// composite target, which the composite render pass leaves in GENERAL.
void copyPresentedFrame(VkCommandBuffer cmd, VkImage source, LsfgImage& destination,
VkExtent2D extent) {
const VkImageMemoryBarrier before[] = {
makeTransition(source, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT,
VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL),
makeTransition(destination.Handle(), VK_ACCESS_SHADER_READ_BIT,
VK_ACCESS_TRANSFER_WRITE_BIT, destination.Layout(),
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL),
};
vkd.CmdPipelineBarrier(cmd,
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 2, before);
VkImageCopy region{};
region.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
region.srcSubresource.layerCount = 1;
region.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
region.dstSubresource.layerCount = 1;
region.extent = {extent.width, extent.height, 1};
vkd.CmdCopyImage(cmd, source, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, destination.Handle(),
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
const VkImageMemoryBarrier after[] = {
makeTransition(source, VK_ACCESS_TRANSFER_READ_BIT, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL),
makeTransition(destination.Handle(), VK_ACCESS_TRANSFER_WRITE_BIT,
VK_ACCESS_SHADER_READ_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
VK_IMAGE_LAYOUT_GENERAL),
};
vkd.CmdPipelineBarrier(cmd, VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
0, 0, nullptr, 0, nullptr, 2, after);
destination.SetLayout(VK_IMAGE_LAYOUT_GENERAL);
}
} // namespace
Engine::Engine() = default;
Engine::~Engine() {
chain_.reset();
shaders_.reset();
}
bool Engine::init(VkDevice device, VkPhysicalDevice physicalDevice, const std::string& cachePath,
uint32_t spirvTarget) {
if (device == VK_NULL_HANDLE || physicalDevice == VK_NULL_HANDLE || cachePath.empty())
return false;
if (!lsfgVkdReady()) {
LSFG_LOGW("dispatch not initialised; frame generation unavailable");
return false;
}
device_ = Device(device, physicalDevice);
cachePath_ = cachePath;
shaders_ = std::make_unique<LsfgShaders>(device_, cachePath_, spirvTarget);
if (!shaders_->IsValid()) {
LSFG_LOGW("shader cache at %s did not yield all modules", cachePath.c_str());
shaders_.reset();
return false;
}
LSFG_LOGI("frame generation shaders ready");
return true;
}
void Engine::configure(uint32_t multiplier, uint32_t targetRate, float flowScale,
float refreshRate) {
LsfgPacerConfig config = pacer_.Config();
config.multiplier = multiplier;
config.target_rate = targetRate;
config.refresh_rate = refreshRate;
pacer_.SetConfig(config);
flowScale_ = std::clamp(flowScale, kFlowScaleMin, kFlowScaleMax);
}
void Engine::setRefreshRate(float refreshRate) {
LsfgPacerConfig config = pacer_.Config();
if (config.refresh_rate == refreshRate) return;
config.refresh_rate = refreshRate;
pacer_.SetConfig(config);
}
void Engine::setGuestExtent(uint32_t width, uint32_t height) {
if (width == 0 || height == 0) return;
peakGuestExtent_.width = std::max(peakGuestExtent_.width, width);
peakGuestExtent_.height = std::max(peakGuestExtent_.height, height);
}
// The flow pyramid is the expensive part, so its resolution is scaled relative
// to the largest guest surface seen: a game running well below panel
// resolution does not need a panel-resolution flow field.
float Engine::effectiveFlowScale(uint32_t width) const {
if (width == 0 || peakGuestExtent_.width == 0) return flowScale_;
const float ratio = (float)peakGuestExtent_.width / (float)width;
const float stepped = std::ceil(ratio * kFlowScaleSteps) / kFlowScaleSteps;
return std::clamp(std::min(stepped, flowScale_), kFlowScaleMin, kFlowScaleMax);
}
bool Engine::needsRebuild(uint32_t width, uint32_t height, VkFormat format) const {
if (unavailable_) return false;
return !chain_
|| builtExtent_.width != width || builtExtent_.height != height
|| builtFormat_ != format
|| builtFlowScale_ != effectiveFlowScale(width);
}
bool Engine::prepare(uint32_t width, uint32_t height, VkFormat format) {
if (unavailable_ || !shaders_) return false;
if (width == 0 || height == 0 || format == VK_FORMAT_UNDEFINED) return false;
if (!needsRebuild(width, height, format)) return chain_ && chain_->Valid();
const float scale = effectiveFlowScale(width);
chain_.reset();
chain_ = std::make_unique<LsfgChain>(device_, *shaders_, VkExtent2D{width, height}, format,
scale);
if (!chain_->Valid()) {
// A device that cannot build the chain reports unsupported once and
// stops trying, rather than thrashing allocations every frame.
LSFG_LOGW("chain build failed at %ux%u; frame generation unavailable", width, height);
chain_.reset();
unavailable_ = true;
return false;
}
builtExtent_ = VkExtent2D{width, height};
builtFormat_ = format;
builtFlowScale_ = scale;
frameCount_ = 0;
lastCopiedCount_ = 0;
haveCopied_ = false;
primeHistory_ = false;
planCalls_ = 0;
warmStreak_ = 0;
warm_ = false;
generating_ = false;
pacer_.Reset();
governor_.reset();
LSFG_LOGI("chain built at %ux%u, flow %ux%u scale %.2f (preset %.2f, guest %ux%u)",
width, height, (unsigned)(width * scale), (unsigned)(height * scale),
(double)scale, (double)flowScale_,
peakGuestExtent_.width, peakGuestExtent_.height);
return true;
}
uint32_t Engine::plan(uint32_t capacity, uint64_t sourceFrames) {
if (unavailable_ || !chain_) return 0;
plan_ = pacer_.Plan(std::min<size_t>(capacity, kMaxGenerations), sourceFrames);
// The pacer says how many frames FIT in the panel's budget. The governor
// says how many this device can actually afford right now - on a handheld
// the chain competes with the game for one GPU, so an extra generation has
// to prove it improves total output without collapsing the real frame rate.
if (governorEnabled_) {
const LsfgPacerStats s = pacer_.Stats();
governor_.configure((uint32_t)std::min<size_t>(pacer_.MaxGenerations(), kMaxGenerations));
// presentedRate_, NOT the pacer's loop rate. The pacer samples its loop
// once per SOURCE frame, so its "loop rate" is the guest rate by
// construction and can never show that generation added anything - the
// governor could not accept a probe on any hardware.
plan_.generations = governor_.cap((uint32_t)plan_.generations, s.source_rate,
presentedRate_);
}
warm_ = plan_.warm && frameCount_ + 1 >= kRequiredFrames;
warmStreak_ = warm_ ? warmStreak_ + 1 : 0;
generating_ = warm_ && warmStreak_ >= kRecurrenceFrames && plan_.generations > 0;
// The ring is only seeded while generating, so the first frame after any
// gap - arming, or a pacer discontinuity after a hitch - would interpolate
// between this frame and whatever the other slot held when generation last
// stopped. That is a visible warp, and it happens every time the engine
// starts. Spend this frame filling the ring instead and generate from the
// next one, when both slots are genuinely consecutive.
primeHistory_ = false;
if (generating_ && !historyFresh()) {
primeHistory_ = true;
generating_ = false;
if ((primeLogCount_++ % 60) == 0)
LSFG_LOGI("priming the input ring (last copied frame %llu, now at %llu)",
(unsigned long long)(haveCopied_ ? lastCopiedCount_ : 0),
(unsigned long long)frameCount_);
}
if ((planCalls_++ % kTelemetryInterval) == 0) {
const LsfgPacerStats stats = pacer_.Stats();
const float wanted = stats.source_rate * (float)(plan_.generations + 1);
LSFG_LOGI("presented=%.1f fps (measured at the swapchain)", (double)presentedRate_);
LSFG_LOGI("pace gen=%zu max=%zu cap=%u guest=%.1f loop=%.1f refresh=%.1f target=%.0f "
"slots=%.2f drawn=%llu needs=%.1fHz%s%s",
plan_.generations, pacer_.MaxGenerations(), capacity,
(double)stats.source_rate, (double)stats.loop_rate, (double)stats.refresh_rate,
(double)stats.target_rate, (double)stats.slots,
(unsigned long long)stats.last_drawn, (double)wanted,
(stats.refresh_rate > 0.0f && wanted > stats.refresh_rate + 1.0f)
? " PANEL-BOUND" : "",
stats.rates_settled ? (warm_ ? "" : " cold") : " sampling");
}
return generating_ ? (uint32_t)plan_.generations : 0;
}
void Engine::process(VkCommandBuffer cmd, VkImage source, uint32_t width, uint32_t height,
uint32_t generations) {
if (!chain_ || !chain_->Valid()) return;
const uint64_t count = frameCount_++;
lastCount_ = count;
lastGenerations_ = generations;
// Seeding the input ring is a full-resolution image copy every frame, so it
// is NOT free at 100 fps - it was costing real frame rate while producing
// nothing. Copy only when generating, on the one priming frame that
// precedes generation, or while the governor is warming up towards a
// probe - so the ring is populated by the time it is needed.
const bool needHistory = generations > 0 || primeHistory_
|| (governorEnabled_ && governor_.wantsHistory());
if (needHistory) {
copyPresentedFrame(cmd, source, chain_->Input(count), VkExtent2D{width, height});
lastCopiedCount_ = count;
haveCopied_ = true;
}
// The SHARED chain is 24 of the 25 shaders - the whole flow pyramid - and
// only `generate` runs per generated frame. Running it while producing
// nothing spends almost the entire cost of frame generation for no frames
// at all, every frame. That is what pinned the GPU at 100% on device
// regardless of the game's own settings, and it fed straight back into the
// governor: the chain made the source rate collapse, the governor saw the
// collapse and refused to generate, and refusing did not stop the chain.
if (warm_ && generations > 0) chain_->DispatchShared(cmd, count);
}
void Engine::generateInto(VkCommandBuffer cmd, uint32_t generation, uint32_t targetIndex,
VkImage targetImage, VkImageView targetView,
uint32_t width, uint32_t height) {
if (!chain_ || !chain_->Valid()) return;
if (targetIndex >= LSFG_MAX_TARGETS) return;
chain_->SetTarget(device_, lastGenerations_, generation, targetIndex, targetView);
chain_->DispatchGeneration(cmd, lastCount_, lastGenerations_, generation, targetIndex,
targetImage, VkExtent2D{width, height});
}
float Engine::sourceRate() const { return pacer_.Stats().source_rate; }
void Engine::forgetTargets() {
if (chain_) chain_->ForgetTargets();
}
void Engine::reset() {
pacer_.Reset();
governor_.reset();
peakGuestExtent_ = VkExtent2D{};
lastCopiedCount_ = 0;
haveCopied_ = false;
primeHistory_ = false;
warmStreak_ = 0;
warm_ = false;
generating_ = false;
plan_ = {};
}
} // namespace lsfg
@@ -0,0 +1,149 @@
#pragma once
// ============================================================================
// lsfg_engine - the compositor's handle on native LSFG frame generation.
//
// Owns the shader modules, the interpolation chain and the pacer, and exposes
// the small contract the render loop needs:
//
// prepare(w, h, format) build/rebuild the chain for this size
// plan(capacity, sourceFrames) how many frames to generate this time
// process(cmd, source, w, h, gens) take frame N in, run the shared chain
// generateInto(cmd, g, i, img, view) synthesise generated frame g
//
// Ordering matters and is not obvious: interpolation produces frames that
// belong BETWEEN N-1 and N, so the generated frames are presented FIRST and
// the real frame N is held back one slot. That one output interval is the
// unavoidable cost of interpolating rather than extrapolating; no placement of
// this code changes it.
//
// Ported from WinNative's vkr_lsfg.cpp (GPL-3.0-or-later), LSFG port credited
// to Camille LaVey / the Eden Emulator Project, following upstream lsfg-vk.
// ============================================================================
#include <cstdint>
#include <memory>
#include <string>
#include <vulkan/vulkan.h>
#include "lsfg_common.hpp"
#include "lsfg_dll.h" // kSpirv16
#include "lsfg_governor.h"
#include "lsfg_pacer.hpp"
namespace lsfg {
class LsfgShaders;
class LsfgChain;
// Hard ceiling on generated frames per source frame (2x..4x -> 1..3).
constexpr uint32_t kMaxGenerations = 3;
class Engine {
public:
// Both the constructor and destructor are out-of-line ON PURPOSE. The
// shader set and the chain are held by unique_ptr to forward-declared
// types, and an inline constructor would make the compiler emit the
// exception-cleanup path for those members at every construction site -
// which needs the complete types there. Keeping both here confines that to
// lsfg_engine.cpp, where the types are complete.
Engine();
~Engine();
Engine(const Engine&) = delete;
Engine& operator=(const Engine&) = delete;
// Build the shader modules from a cache produced by lsfg_dll::buildCache.
// Returns false if the cache does not yield all 25 modules. `spirvTarget`
// is the highest SPIR-V version the device accepts (lsfg_probe.h); the
// cached 1.6 modules are lowered to it when it is below that.
bool init(VkDevice device, VkPhysicalDevice physicalDevice, const std::string& cachePath,
uint32_t spirvTarget = kSpirv16);
bool valid() const { return shaders_ != nullptr && !unavailable_; }
bool unavailable() const { return unavailable_; }
void configure(uint32_t multiplier, uint32_t targetRate, float flowScale, float refreshRate);
void setRefreshRate(float refreshRate);
void setGuestExtent(uint32_t width, uint32_t height);
bool needsRebuild(uint32_t width, uint32_t height, VkFormat format) const;
bool prepare(uint32_t width, uint32_t height, VkFormat format);
// How many frames to generate for this source frame. Returns 0 until the
// history slots are valid and the rates have settled, so the first frames
// after a start or a resize are never interpolated from garbage.
uint32_t plan(uint32_t capacity, uint64_t sourceFrames);
// Take the just-composited frame as LSFG input and run the shared part of
// the chain (everything except `generate`). `source` must be in GENERAL.
void process(VkCommandBuffer cmd, VkImage source, uint32_t width, uint32_t height,
uint32_t generations);
// Synthesise generated frame `generation` into the given storage image.
void generateInto(VkCommandBuffer cmd, uint32_t generation, uint32_t targetIndex,
VkImage targetImage, VkImageView targetView,
uint32_t width, uint32_t height);
// What the probe governor currently trusts, and the latest thermal
// reading (-1 = no signal). Surfaced for the HUD and diagnostics.
uint32_t acceptedGenerations() const { return governor_.accepted(); }
int thermalStatus() const { return governor_.thermalStatus(); }
float sourceRate() const;
// The rate that actually reaches the panel, generated frames included.
// Supplied by the renderer, which is the only thing that counts presents.
float loopRate() const { return presentedRate_; }
void setPresentedRate(float fps) { presentedRate_ = fps; }
void forgetTargets();
void reset();
private:
float effectiveFlowScale(uint32_t width) const;
// A generated frame is a blend of the last two REAL frames, so the input
// ring - which is two images deep - only holds usable history when the
// other slot was filled by the frame immediately before this one. It is
// not, every time generation resumes after a pause: the ring is seeded
// only while generating, so the slot still carries whatever was there when
// generation last stopped.
bool historyFresh() const { return haveCopied_ && lastCopiedCount_ + 1 == frameCount_; }
Device device_{};
std::string cachePath_;
std::unique_ptr<LsfgShaders> shaders_;
std::unique_ptr<LsfgChain> chain_;
LsfgPacer pacer_;
ProbeGovernor governor_;
// The governor is OFF by default: the device's own thermal management is
// the authority, and a probe loop that can veto the user's setting is not
// wanted here. The multiplier is then what was asked for, still clamped by
// what the panel can show. Kept behind a flag rather than deleted because
// the probe/backoff logic is the right answer for a device WITHOUT decent
// native throttling, and this is the only thing that would have to change.
bool governorEnabled_ = false;
LsfgPlan plan_{};
VkExtent2D builtExtent_{};
VkExtent2D peakGuestExtent_{};
VkFormat builtFormat_{VK_FORMAT_UNDEFINED};
float builtFlowScale_{};
float flowScale_{1.0f};
float presentedRate_{};
uint64_t frameCount_{};
uint64_t lastCopiedCount_{};
bool haveCopied_{};
bool primeHistory_{};
uint64_t primeLogCount_{};
uint64_t lastCount_{};
size_t lastGenerations_{};
uint64_t planCalls_{};
uint32_t warmStreak_{};
bool warm_{};
bool generating_{};
bool unavailable_{};
};
} // namespace lsfg
@@ -0,0 +1,200 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2025 lsfg-vk
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Ported into Bannerlator's compositor from WinNative (GPL-3.0-or-later),
// whose LSFG port is credited to Camille LaVey / the Eden Emulator Project and
// follows upstream lsfg-vk. Only the Vulkan dispatch differs: Bannerlator
// resolves entry points through the renderer's own table (see lsfg_vkd.h).
#include "lsfg_gamma.hpp"
#include "lsfg_shaders.hpp"
#include <vector>
namespace lsfg {
namespace {
constexpr uint32_t DISPATCH_TILE_SHIFT = 3;
[[nodiscard]] uint32_t GroupCount(uint32_t size) {
return (size + (1u << DISPATCH_TILE_SHIFT) - 1) >> DISPATCH_TILE_SHIFT;
}
}
LsfgGamma::LsfgGamma(const Device& device, const LsfgShaders& shaders, LsfgResources& resources,
VkDescriptorPool descriptor_pool, LsfgImageHistory& inputs_,
LsfgImage& flow_input_, LsfgImage* previous_)
: inputs{&inputs_}, flow_input{&flow_input_}, previous{previous_} {
passes[0] = LsfgPass(device, shaders, LSFG_GAMMA_SHADERS[0],
{{1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER},
{2, VK_DESCRIPTOR_TYPE_SAMPLER},
{5, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE},
{3, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE}});
passes[1] = LsfgPass(device, shaders, LSFG_GAMMA_SHADERS[1],
{{1, VK_DESCRIPTOR_TYPE_SAMPLER},
{3, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE},
{2, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE}});
passes[2] = LsfgPass(device, shaders, LSFG_GAMMA_SHADERS[2],
{{1, VK_DESCRIPTOR_TYPE_SAMPLER},
{2, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE},
{2, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE}});
passes[3] = LsfgPass(device, shaders, LSFG_GAMMA_SHADERS[3],
{{1, VK_DESCRIPTOR_TYPE_SAMPLER},
{2, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE},
{2, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE}});
passes[4] = LsfgPass(device, shaders, LSFG_GAMMA_SHADERS[4],
{{1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER},
{2, VK_DESCRIPTOR_TYPE_SAMPLER},
{4, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE},
{1, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE}});
for (const auto& pass : passes) {
if (!pass.Valid()) return;
}
const VkExtent2D extent = (*inputs)[0][0].Extent();
for (auto& image : temp1) {
image = LsfgImage(device, extent);
if (!image.Valid()) return;
}
for (auto& image : temp2) {
image = LsfgImage(device, extent);
if (!image.Valid()) return;
}
out_image = LsfgImage(device, extent, LSFG_MOTION_FORMAT);
if (!out_image.Valid()) return;
std::vector<VkDescriptorSetLayout> layouts;
for (size_t slot = 0; slot < LSFG_GENERATION_SLOTS; ++slot) {
for (size_t i = 0; i < LSFG_HISTORY_SLOTS; ++i) {
layouts.push_back(passes[0].SetLayout());
}
for (size_t i = 1; i < LSFG_GAMMA_STAGES; ++i) {
layouts.push_back(passes[i].SetLayout());
}
}
const std::vector<VkDescriptorSet> sets =
AllocateLsfgDescriptorSets(device, descriptor_pool, layouts);
if (sets.size() != layouts.size()) return;
const LsfgImage& previous_image =
previous != nullptr ? *previous : resources.GetDummy(LSFG_MOTION_FORMAT);
if (!previous_image.Valid()) return;
const VkSampler sampler = resources.GetSampler();
const VkSampler border_sampler =
resources.GetSampler(VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER, VK_COMPARE_OP_NEVER, true);
const VkSampler edge_sampler =
resources.GetSampler(VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, VK_COMPARE_OP_ALWAYS, false);
size_t next = 0;
for (size_t slot = 0; slot < LSFG_GENERATION_SLOTS; ++slot) {
Generation& pass = generations[slot];
const VkBuffer buffer = resources.GetBuffer(LsfgSlotTimestamp(slot), previous == nullptr);
for (size_t i = 0; i < LSFG_HISTORY_SLOTS; ++i) {
pass.first_descriptor_sets[i] = sets[next++];
}
for (size_t i = 0; i < LSFG_GAMMA_STAGES - 1; ++i) {
pass.descriptor_sets[i] = sets[next++];
}
for (size_t i = 0; i < LSFG_HISTORY_SLOTS; ++i) {
LsfgDescriptorWriter(pass.first_descriptor_sets[i])
.AddUniformBuffer(buffer, LsfgResources::BufferSize())
.AddSampler(border_sampler)
.AddSampler(edge_sampler)
.AddSampledImages((*inputs)[(i + 2) % LSFG_HISTORY_SLOTS])
.AddSampledImages((*inputs)[i % LSFG_HISTORY_SLOTS])
.AddSampledImage(previous_image)
.AddStorageImages(temp1)
.Build(device);
}
LsfgDescriptorWriter(pass.descriptor_sets[0])
.AddSampler(sampler)
.AddSampledImages(temp1)
.AddStorageImages(temp2)
.Build(device);
LsfgDescriptorWriter(pass.descriptor_sets[1])
.AddSampler(sampler)
.AddSampledImages(temp2)
.AddStorageImage(temp1[0])
.AddStorageImage(temp1[1])
.Build(device);
LsfgDescriptorWriter(pass.descriptor_sets[2])
.AddSampler(sampler)
.AddSampledImage(temp1[0])
.AddSampledImage(temp1[1])
.AddStorageImages(temp2)
.Build(device);
LsfgDescriptorWriter(pass.descriptor_sets[3])
.AddUniformBuffer(buffer, LsfgResources::BufferSize())
.AddSampler(sampler)
.AddSampler(edge_sampler)
.AddSampledImages(temp2)
.AddSampledImage(previous_image)
.AddSampledImage(*flow_input)
.AddStorageImage(out_image)
.Build(device);
}
allocated = true;
}
void LsfgGamma::PushStepBarriers(LsfgBarriers& barriers, uint64_t frame_count, size_t step) {
const size_t history = frame_count % LSFG_HISTORY_SLOTS;
const size_t previous_history = (frame_count + 2) % LSFG_HISTORY_SLOTS;
switch (step) {
case 0:
barriers.WriteToReadAll((*inputs)[previous_history])
.WriteToReadAll((*inputs)[history])
.WriteToRead(previous)
.ReadToWriteAll(temp1);
break;
case 1:
barriers.WriteToReadAll(temp1).ReadToWriteAll(temp2);
break;
case 2:
barriers.WriteToReadAll(temp2).ReadToWrite(temp1[0]).ReadToWrite(temp1[1]);
break;
case 3:
barriers.WriteToRead(temp1[0]).WriteToRead(temp1[1]).ReadToWriteAll(temp2);
break;
default:
barriers.WriteToReadAll(temp2)
.WriteToRead(previous)
.WriteToRead(*flow_input)
.ReadToWrite(out_image);
break;
}
}
void LsfgGamma::DispatchStep(VkCommandBuffer cmdbuf, uint64_t frame_count, size_t slot,
size_t step) {
const Generation& pass = generations[slot];
const VkExtent2D extent = temp1[0].Extent();
const size_t history = frame_count % LSFG_HISTORY_SLOTS;
if (step == 0) {
passes[0].Bind(cmdbuf, pass.first_descriptor_sets[history]);
} else {
passes[step].Bind(cmdbuf, pass.descriptor_sets[step - 1]);
}
vkd.CmdDispatch(cmdbuf, GroupCount(extent.width), GroupCount(extent.height), 1);
}
void LsfgGamma::Dispatch(VkCommandBuffer cmdbuf, uint64_t frame_count, size_t slot) {
for (size_t step = 0; step < LSFG_GAMMA_STAGES; ++step) {
LsfgBarriers barriers(cmdbuf);
PushStepBarriers(barriers, frame_count, step);
barriers.Build();
DispatchStep(cmdbuf, frame_count, slot, step);
}
}
}
@@ -0,0 +1,66 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2025 lsfg-vk
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Ported into Bannerlator's compositor from WinNative (GPL-3.0-or-later),
// whose LSFG port is credited to Camille LaVey / the Eden Emulator Project and
// follows upstream lsfg-vk. Only the Vulkan dispatch differs: Bannerlator
// resolves entry points through the renderer's own table (see lsfg_vkd.h).
#pragma once
#include <array>
#include "lsfg_common.hpp"
namespace lsfg {
class LsfgShaders;
constexpr size_t LSFG_GAMMA_STAGES = 5;
constexpr size_t LSFG_GAMMA_TEMPS = 3;
class LsfgGamma {
public:
LsfgGamma() = default;
LsfgGamma(const Device& device, const LsfgShaders& shaders, LsfgResources& resources,
VkDescriptorPool descriptor_pool, LsfgImageHistory& inputs, LsfgImage& flow_input,
LsfgImage* previous);
void Dispatch(VkCommandBuffer cmdbuf, uint64_t frame_count, size_t slot);
void PushStepBarriers(LsfgBarriers& barriers, uint64_t frame_count, size_t step);
void DispatchStep(VkCommandBuffer cmdbuf, uint64_t frame_count, size_t slot,
size_t step);
[[nodiscard]] LsfgImage& Output() {
return out_image;
}
[[nodiscard]] bool Valid() const {
return allocated;
}
private:
struct Generation {
std::array<VkDescriptorSet, LSFG_HISTORY_SLOTS> first_descriptor_sets{};
std::array<VkDescriptorSet, LSFG_GAMMA_STAGES - 1> descriptor_sets{};
};
LsfgImageHistory* inputs{};
LsfgImage* flow_input{};
LsfgImage* previous{};
std::array<LsfgPass, LSFG_GAMMA_STAGES> passes;
std::array<Generation, LSFG_GENERATION_SLOTS> generations{};
std::array<LsfgImage, LSFG_GAMMA_TEMPS> temp1;
LsfgImagePair temp2;
LsfgImage out_image;
bool allocated{};
};
}
@@ -0,0 +1,137 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2025 lsfg-vk
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Ported into Bannerlator's compositor from WinNative (GPL-3.0-or-later),
// whose LSFG port is credited to Camille LaVey / the Eden Emulator Project and
// follows upstream lsfg-vk. Only the Vulkan dispatch differs: Bannerlator
// resolves entry points through the renderer's own table (see lsfg_vkd.h).
#include "lsfg_generate.hpp"
#include "lsfg_dll.h"
#include "lsfg_shaders.hpp"
#include <vector>
namespace lsfg {
namespace {
constexpr uint32_t DISPATCH_TILE_SHIFT = 4;
[[nodiscard]] uint32_t GroupCount(uint32_t size) {
return (size + (1u << DISPATCH_TILE_SHIFT) - 1) >> DISPATCH_TILE_SHIFT;
}
VkImageMemoryBarrier MakeTargetBarrier(VkImage image, VkAccessFlags src_access,
VkAccessFlags dst_access, VkImageLayout old_layout) {
VkImageMemoryBarrier barrier{};
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
barrier.srcAccessMask = src_access;
barrier.dstAccessMask = dst_access;
barrier.oldLayout = old_layout;
barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.image = image;
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
barrier.subresourceRange.levelCount = 1;
barrier.subresourceRange.layerCount = 1;
return barrier;
}
}
LsfgGenerate::LsfgGenerate(const Device& device, const LsfgShaders& shaders,
LsfgResources& resources, VkDescriptorPool descriptor_pool,
LsfgImagePair& frames_, LsfgImage& motion_, LsfgImage& detail1_,
LsfgImage& detail2_)
: frames{&frames_}, motion{&motion_}, detail1{&detail1_}, detail2{&detail2_} {
pass = LsfgPass(device, shaders, LSFG_SHADER_GENERATE,
{{1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER},
{2, VK_DESCRIPTOR_TYPE_SAMPLER},
{5, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE},
{1, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE}});
if (!pass.Valid()) return;
sampler = resources.GetSampler();
edge_sampler =
resources.GetSampler(VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, VK_COMPARE_OP_ALWAYS, false);
const uint32_t total =
static_cast<uint32_t>(LSFG_GENERATION_SLOTS * LSFG_MAX_TARGETS * 2);
const std::vector<VkDescriptorSet> sets =
AllocateLsfgDescriptorSets(device, descriptor_pool, pass.SetLayout(), total);
if (sets.size() != total) return;
size_t next = 0;
for (size_t slot = 0; slot < LSFG_GENERATION_SLOTS; ++slot) {
Generation& target = generations[slot];
target.buffer = resources.GetBuffer(LsfgSlotTimestamp(slot));
for (auto& entry : target.targets) {
for (auto& set : entry.descriptor_sets) {
set = sets[next++];
}
}
}
allocated = true;
}
void LsfgGenerate::SetTarget(const Device& device, size_t slot, uint32_t target,
VkImageView view) {
Target& entry = generations[slot].targets[target];
if (entry.view == view) return;
entry.view = view;
for (size_t i = 0; i < entry.descriptor_sets.size(); ++i) {
LsfgDescriptorWriter(entry.descriptor_sets[i])
.AddUniformBuffer(generations[slot].buffer, LsfgResources::BufferSize())
.AddSampler(sampler)
.AddSampler(edge_sampler)
.AddSampledImage((*frames)[1 - i])
.AddSampledImage((*frames)[i])
.AddSampledImage(*motion)
.AddSampledImage(*detail1)
.AddSampledImage(*detail2)
.AddStorageView(view)
.Build(device);
}
}
void LsfgGenerate::ForgetTargets() {
for (auto& generation : generations) {
for (auto& entry : generation.targets) {
entry.view = VK_NULL_HANDLE;
}
}
}
void LsfgGenerate::Dispatch(VkCommandBuffer cmdbuf, uint64_t frame_count, size_t slot,
uint32_t target, VkImage image, VkExtent2D extent) {
const Target& entry = generations[slot].targets[target];
LsfgBarriers(cmdbuf)
.WriteToReadAll(*frames)
.WriteToRead(*motion)
.WriteToRead(*detail1)
.WriteToRead(*detail2)
.DiscardToWrite(image)
.Build();
pass.Bind(cmdbuf, entry.descriptor_sets[frame_count % entry.descriptor_sets.size()]);
vkd.CmdDispatch(cmdbuf, GroupCount(extent.width), GroupCount(extent.height), 1);
const VkImageMemoryBarrier after = MakeTargetBarrier(
image, VK_ACCESS_SHADER_WRITE_BIT,
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_TRANSFER_READ_BIT,
VK_IMAGE_LAYOUT_GENERAL);
vkd.CmdPipelineBarrier(cmdbuf, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT |
VK_PIPELINE_STAGE_TRANSFER_BIT,
0, 0, nullptr, 0, nullptr, 1, &after);
}
}
@@ -0,0 +1,63 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2025 lsfg-vk
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Ported into Bannerlator's compositor from WinNative (GPL-3.0-or-later),
// whose LSFG port is credited to Camille LaVey / the Eden Emulator Project and
// follows upstream lsfg-vk. Only the Vulkan dispatch differs: Bannerlator
// resolves entry points through the renderer's own table (see lsfg_vkd.h).
#pragma once
#include <array>
#include "lsfg_common.hpp"
namespace lsfg {
class LsfgShaders;
class LsfgGenerate {
public:
LsfgGenerate() = default;
LsfgGenerate(const Device& device, const LsfgShaders& shaders, LsfgResources& resources,
VkDescriptorPool descriptor_pool, LsfgImagePair& frames, LsfgImage& motion,
LsfgImage& detail1, LsfgImage& detail2);
void SetTarget(const Device& device, size_t slot, uint32_t target, VkImageView view);
void ForgetTargets();
void Dispatch(VkCommandBuffer cmdbuf, uint64_t frame_count, size_t slot, uint32_t target,
VkImage image, VkExtent2D extent);
[[nodiscard]] bool Valid() const {
return pass.Valid() && allocated;
}
private:
struct Target {
std::array<VkDescriptorSet, 2> descriptor_sets{};
VkImageView view{VK_NULL_HANDLE};
};
struct Generation {
std::array<Target, LSFG_MAX_TARGETS> targets{};
VkBuffer buffer{VK_NULL_HANDLE};
};
LsfgImagePair* frames{};
LsfgImage* motion{};
LsfgImage* detail1{};
LsfgImage* detail2{};
VkSampler sampler{VK_NULL_HANDLE};
VkSampler edge_sampler{VK_NULL_HANDLE};
LsfgPass pass;
std::array<Generation, LSFG_GENERATION_SLOTS> generations{};
bool allocated{};
};
}
@@ -0,0 +1,237 @@
// See lsfg_governor.h.
#include "lsfg_governor.h"
#include <algorithm>
#include <android/log.h>
#include <dlfcn.h>
#define GOV_LOGI(...) __android_log_print(ANDROID_LOG_INFO, "LsfgGovernor", __VA_ARGS__)
namespace lsfg {
namespace {
// A probe has to run long enough to survive a single unlucky frame, and the
// baseline long enough to be a fair comparison.
constexpr float kBaselineSeconds = 2.0f;
constexpr float kProbeSeconds = 2.0f;
constexpr uint32_t kMinSamples = 30;
// Keep the extra generation only if it actually paid off.
constexpr float kOutputGainRequired = 1.15f;
constexpr float kSourceFloor = 0.70f;
constexpr float kBackoffSeconds[] = {5.0f, 15.0f, 30.0f, 60.0f};
constexpr uint32_t kBackoffSteps = 4;
constexpr float kThermalPollSeconds = 2.0f;
constexpr float kThermalDecaySeconds = 1.0f;
// ADEVICE_THERMAL_STATUS_* from <android/thermal.h>, which is API 30. Resolved
// by dlsym so the build keeps working below that and on devices that do not
// ship the service; -1 simply means "no thermal signal", never "hot".
// Android reports a STATUS, not a temperature - NONE 0, LIGHT 1, MODERATE 2,
// SEVERE 3, CRITICAL 4, EMERGENCY 5, SHUTDOWN 6 - so there is no degree value
// here to raise. The equivalent of "let it run hotter" is to act on a higher
// status, which is what these now do.
//
// A handheld under a real 3D load sits at SEVERE routinely; blocking there
// made the governor refuse on a device that was working exactly as intended.
// It now only stops GROWING at SEVERE and only gives frames back at CRITICAL,
// leaving the device's own thermal management as the authority.
constexpr int kThermalBlock = 4; // CRITICAL: give generations back
constexpr int kThermalNoGrowth = 3; // SEVERE: keep what we have, stop probing
using PFN_AThermal_acquireManager = void* (*)();
using PFN_AThermal_getCurrentThermalStatus = int (*)(void*);
void* thermalManager() {
static void* manager = [] () -> void* {
void* lib = dlopen("libandroid.so", RTLD_NOW | RTLD_LOCAL);
if (!lib) return nullptr;
auto acquire = (PFN_AThermal_acquireManager)dlsym(lib, "AThermal_acquireManager");
return acquire ? acquire() : nullptr;
}();
return manager;
}
int readThermalStatus() {
void* manager = thermalManager();
if (!manager) return -1;
static auto get = [] () -> PFN_AThermal_getCurrentThermalStatus {
void* lib = dlopen("libandroid.so", RTLD_NOW | RTLD_LOCAL);
return lib ? (PFN_AThermal_getCurrentThermalStatus)
dlsym(lib, "AThermal_getCurrentThermalStatus") : nullptr;
}();
return get ? get(manager) : -1;
}
float phaseSeconds(std::chrono::steady_clock::time_point a,
std::chrono::steady_clock::time_point b) {
return std::chrono::duration<float>(b - a).count();
}
} // namespace
void ProbeGovernor::configure(uint32_t maxGenerations) {
if (maxGenerations_ == maxGenerations) return;
// Raising the ceiling must NOT throw away what we already know works -
// going 2x -> 4x used to reset the accepted level to zero and restart the
// whole baseline/probe cycle, so a user raising the multiplier got FEWER
// frames. Only clamp down to a ceiling that has actually been lowered.
maxGenerations_ = maxGenerations;
if (accepted_ > maxGenerations_) accepted_ = maxGenerations_;
// Re-measure against the new ceiling, but keep the accepted level.
phase_ = Phase::Baseline;
havePhaseStart_ = false;
sourceAccum_ = loopAccum_ = 0.0f;
samples_ = 0;
haveBaseline_ = false;
}
void ProbeGovernor::reset() {
accepted_ = 0;
phase_ = Phase::Baseline;
havePhaseStart_ = false;
sourceAccum_ = loopAccum_ = 0.0f;
samples_ = 0;
baselineSource_ = baselineLoop_ = 0.0f;
haveBaseline_ = false;
backoffStep_ = 0;
}
void ProbeGovernor::enterBaseline(Clock::time_point now) {
phase_ = Phase::Baseline;
phaseStart_ = now;
havePhaseStart_ = true;
sourceAccum_ = loopAccum_ = 0.0f;
samples_ = 0;
}
void ProbeGovernor::enterBackoff(Clock::time_point now) {
phase_ = Phase::Backoff;
phaseStart_ = now;
havePhaseStart_ = true;
sourceAccum_ = loopAccum_ = 0.0f;
samples_ = 0;
if (backoffStep_ < kBackoffSteps - 1) backoffStep_++;
}
void ProbeGovernor::pollThermal(Clock::time_point now) {
if (haveThermalPoll_ && phaseSeconds(lastThermalPoll_, now) < kThermalPollSeconds) return;
lastThermalPoll_ = now;
haveThermalPoll_ = true;
thermal_ = readThermalStatus();
}
uint32_t ProbeGovernor::cap(uint32_t requested, float sourceRate, float loopRate) {
if (maxGenerations_ == 0 || requested == 0) return 0;
const Clock::time_point now = Clock::now();
if (!havePhaseStart_) enterBaseline(now);
pollThermal(now);
// Thermal override: at SEVERE and above we stop asking for more and give
// generations back, but SLOWLY.
//
// This runs once per source frame - about forty times a second - so the
// previous version's unconditional accepted_-- was not "give one back", it
// was "slam to zero in three frames", and its unconditional enterBackoff()
// reset the backoff clock every single call, so the backoff could never
// expire and the governor could never measure or recover. One decay per
// second, and no churn at all once there is nothing left to give back.
thermalBlocked_ = thermal_ >= kThermalBlock;
if (thermal_ >= kThermalBlock) {
// Logged, sparingly. This is a REFUSAL TO EVEN MEASURE, and it was
// silent: on device it looked identical to "measured and declined".
if ((refusalLog_++ % 600u) == 0u)
GOV_LOGI("thermal status %d - not probing (accepted=%u)", thermal_, accepted_);
const bool decayDue = !haveThermalDecay_
|| phaseSeconds(lastThermalDecay_, now) >= kThermalDecaySeconds;
if (accepted_ > 0 && decayDue) {
GOV_LOGI("thermal status %d - dropping to %u generations", thermal_, accepted_ - 1);
accepted_--;
lastThermalDecay_ = now;
haveThermalDecay_ = true;
enterBackoff(now);
}
return std::min(requested, accepted_);
}
haveThermalDecay_ = false;
if (sourceRate > 0.0f && loopRate > 0.0f) {
sourceAccum_ += sourceRate;
loopAccum_ += loopRate;
samples_++;
}
const float elapsed = phaseSeconds(phaseStart_, now);
switch (phase_) {
case Phase::Baseline:
if (elapsed >= kBaselineSeconds && samples_ >= kMinSamples) {
baselineSource_ = sourceAccum_ / (float)samples_;
baselineLoop_ = loopAccum_ / (float)samples_;
haveBaseline_ = true;
const bool roomToGrow = accepted_ < maxGenerations_ && requested > accepted_;
const bool thermallyFree = thermal_ < kThermalNoGrowth || thermal_ < 0;
if (roomToGrow && thermallyFree) {
phase_ = Phase::Probing;
phaseStart_ = now;
sourceAccum_ = loopAccum_ = 0.0f;
samples_ = 0;
} else {
// Also previously silent. "Baseline completed but we did not
// probe" is a decision worth seeing in a log.
if ((refusalLog_++ % 600u) == 0u)
GOV_LOGI("not probing: accepted=%u max=%u requested=%u thermal=%d",
accepted_, maxGenerations_, requested, thermal_);
enterBaseline(now); // nothing to try; measure again later
}
}
break;
case Phase::Probing:
if (elapsed >= kProbeSeconds && samples_ >= kMinSamples && haveBaseline_) {
const float probeSource = sourceAccum_ / (float)samples_;
const float probeLoop = loopAccum_ / (float)samples_;
const bool outputImproved =
baselineLoop_ > 0.0f && probeLoop >= baselineLoop_ * kOutputGainRequired;
const bool sourceHeld =
baselineSource_ <= 0.0f || probeSource >= baselineSource_ * kSourceFloor;
if (outputImproved && sourceHeld) {
accepted_ = std::min(accepted_ + 1, maxGenerations_);
backoffStep_ = 0;
GOV_LOGI("probe kept: %u generations (output %.1f -> %.1f, source %.1f -> %.1f)",
accepted_, (double)baselineLoop_, (double)probeLoop,
(double)baselineSource_, (double)probeSource);
enterBaseline(now);
} else {
GOV_LOGI("probe rejected at %u+1 (output %.1f -> %.1f, source %.1f -> %.1f) - "
"backing off %.0fs", accepted_,
(double)baselineLoop_, (double)probeLoop,
(double)baselineSource_, (double)probeSource,
(double)kBackoffSeconds[backoffStep_]);
enterBackoff(now);
}
}
break;
case Phase::Backoff:
if (elapsed >= kBackoffSeconds[backoffStep_]) enterBaseline(now);
break;
}
// While probing we allow one more than is currently accepted; otherwise the
// accepted level is the ceiling, and the pacer's own answer still applies.
const uint32_t allowed = phase_ == Phase::Probing
? std::min(accepted_ + 1, maxGenerations_)
: accepted_;
return std::min(requested, allowed);
}
} // namespace lsfg
@@ -0,0 +1,90 @@
#pragma once
// ============================================================================
// lsfg_governor - decides whether an extra generated frame is actually WORTH
// it on this device, right now.
//
// The ported pacer answers "how many frames fit in the panel's budget". That
// is necessary but not sufficient on a handheld: the LSFG chain competes with
// the game for the same GPU, so a naive fixed 3x on a GPU-bound title lowers
// the real frame rate by more than the generated frames add. The user sees a
// higher HUD number and a worse-looking game.
//
// So the multiplier is treated as a ceiling to earn, not a setting to obey.
// The governor starts at 0 generations, periodically PROBES one more, and
// keeps it only if the probe measurably paid off:
//
// * total output rate must improve by at least 1.15x, and
// * the real (source) frame rate must not collapse below 0.70x baseline.
//
// A failed probe reverts immediately and backs off 5s, 15s, 30s, then 60s, so
// a device that cannot afford another generation stops being asked.
//
// Thermals are the second input a phone needs and a desktop does not.
// Sustained frame generation is a thermal decision as much as a performance
// one, so the governor gives back generations as the device heats and refuses
// to probe while it is throttling.
// ============================================================================
#include <chrono>
#include <cstdint>
namespace lsfg {
class ProbeGovernor {
public:
void configure(uint32_t maxGenerations);
// Cap the pacer's answer. Called once per source frame with the measured
// rates; returns the number of generations actually permitted.
uint32_t cap(uint32_t requested, float sourceRate, float loopRate);
void reset();
uint32_t accepted() const { return accepted_; }
bool probing() const { return phase_ == Phase::Probing; }
// Whether the chain's input ring needs to be kept fed. Seeding it is a
// full-resolution copy every frame, so it is skipped during the long idle
// stretches - a backoff, or a thermal block - and resumed during Baseline,
// which gives it a couple of seconds to fill before a probe can start.
bool wantsHistory() const {
return !thermalBlocked_ && phase_ != Phase::Backoff;
}
// Latest thermal reading, 0 (none) .. 6 (shutdown); -1 when unavailable.
int thermalStatus() const { return thermal_; }
private:
using Clock = std::chrono::steady_clock;
enum class Phase { Baseline, Probing, Backoff };
void enterBaseline(Clock::time_point now);
void enterBackoff(Clock::time_point now);
void pollThermal(Clock::time_point now);
uint32_t maxGenerations_ = 0;
uint32_t accepted_ = 0;
Phase phase_ = Phase::Baseline;
Clock::time_point phaseStart_{};
bool havePhaseStart_ = false;
// Rolling means over the current phase window.
float sourceAccum_ = 0.0f, loopAccum_ = 0.0f;
uint32_t samples_ = 0;
// What the accepted level achieves, measured during Baseline.
float baselineSource_ = 0.0f, baselineLoop_ = 0.0f;
bool haveBaseline_ = false;
uint32_t backoffStep_ = 0;
int thermal_ = -1;
Clock::time_point lastThermalDecay_{};
bool haveThermalDecay_ = false;
bool thermalBlocked_ = false;
uint32_t refusalLog_ = 0;
Clock::time_point lastThermalPoll_{};
bool haveThermalPoll_ = false;
};
} // namespace lsfg
@@ -0,0 +1,69 @@
// JNI surface for native LSFG shader-cache management.
//
// Deliberately free of any renderer handle: extracting the shader chain from
// Lossless.dll and caching it is a slow, once-per-DLL operation that the app
// runs off the render thread, long before a game launches.
#include "lsfg_dll.h"
#include <jni.h>
#include <string>
namespace {
std::string toStdString(JNIEnv* env, jstring s) {
if (!s) return {};
const char* chars = env->GetStringUTFChars(s, nullptr);
if (!chars) return {};
std::string out(chars);
env->ReleaseStringUTFChars(s, chars);
return out;
}
} // namespace
extern "C" JNIEXPORT jint JNICALL
Java_com_steamdeck_launcher_gpu_LsfgNative_nativeValidateDll(JNIEnv* env, jclass, jstring dllPath) {
return (jint)lsfg::validateDll(toStdString(env, dllPath));
}
extern "C" JNIEXPORT jint JNICALL
Java_com_steamdeck_launcher_gpu_LsfgNative_nativeDllVariant(JNIEnv* env, jclass, jstring dllPath,
jboolean preferFp16) {
return (jint)lsfg::dllVariant(toStdString(env, dllPath), preferFp16 == JNI_TRUE);
}
extern "C" JNIEXPORT jint JNICALL
Java_com_steamdeck_launcher_gpu_LsfgNative_nativeBuildCache(JNIEnv* env, jclass, jstring dllPath,
jstring cachePath, jboolean preferFp16) {
return (jint)lsfg::buildCache(toStdString(env, dllPath), toStdString(env, cachePath),
preferFp16 == JNI_TRUE);
}
extern "C" JNIEXPORT jboolean JNICALL
Java_com_steamdeck_launcher_gpu_LsfgNative_nativeCacheMatchesSource(JNIEnv* env, jclass,
jstring cachePath,
jstring dllPath) {
bool matches = false;
const lsfg::DllStatus status = lsfg::cacheMatchesSource(toStdString(env, cachePath),
toStdString(env, dllPath), matches);
return (jboolean)(status == lsfg::DllStatus::Ok && matches);
}
extern "C" JNIEXPORT jint JNICALL
Java_com_steamdeck_launcher_gpu_LsfgNative_nativeCacheVariant(JNIEnv* env, jclass, jstring cachePath) {
lsfg::Variant variant = lsfg::Variant::None;
if (lsfg::cacheVariant(toStdString(env, cachePath), variant) != lsfg::DllStatus::Ok)
return (jint)lsfg::Variant::None;
return (jint)variant;
}
extern "C" JNIEXPORT jstring JNICALL
Java_com_steamdeck_launcher_gpu_LsfgNative_nativeStatusName(JNIEnv* env, jclass, jint status) {
return env->NewStringUTF(lsfg::statusName((lsfg::DllStatus)status));
}
extern "C" JNIEXPORT jstring JNICALL
Java_com_steamdeck_launcher_gpu_LsfgNative_nativeVariantName(JNIEnv* env, jclass, jint variant) {
return env->NewStringUTF(lsfg::variantName((lsfg::Variant)variant));
}
@@ -0,0 +1,85 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2025 lsfg-vk
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Ported into Bannerlator's compositor from WinNative (GPL-3.0-or-later),
// whose LSFG port is credited to Camille LaVey / the Eden Emulator Project and
// follows upstream lsfg-vk. Only the Vulkan dispatch differs: Bannerlator
// resolves entry points through the renderer's own table (see lsfg_vkd.h).
#include "lsfg_mipmaps.hpp"
#include "lsfg_dll.h"
#include "lsfg_shaders.hpp"
#include <algorithm>
#include <vector>
namespace lsfg {
namespace {
constexpr uint32_t DISPATCH_TILE_SHIFT = 6;
[[nodiscard]] uint32_t GroupCount(uint32_t size) {
return (size + (1u << DISPATCH_TILE_SHIFT) - 1) >> DISPATCH_TILE_SHIFT;
}
}
LsfgMipmaps::LsfgMipmaps(const Device& device, const LsfgShaders& shaders,
LsfgResources& resources, VkDescriptorPool descriptor_pool,
LsfgImagePair& frames_, float flow_scale)
: frames{&frames_} {
pass = LsfgPass(device, shaders, LSFG_SHADER_MIPMAPS,
{{1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER},
{1, VK_DESCRIPTOR_TYPE_SAMPLER},
{1, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE},
{LSFG_MIP_LEVELS, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE}});
if (!pass.Valid()) return;
const VkExtent2D input_extent = (*frames)[0].Extent();
flow_extent = VkExtent2D{
std::max(1u, static_cast<uint32_t>(static_cast<float>(input_extent.width) * flow_scale)),
std::max(1u, static_cast<uint32_t>(static_cast<float>(input_extent.height) * flow_scale)),
};
for (size_t i = 0; i < LSFG_MIP_LEVELS; ++i) {
const VkExtent2D level_extent{
std::max(1u, flow_extent.width >> i),
std::max(1u, flow_extent.height >> i),
};
out_images[i] = LsfgImage(device, level_extent, LSFG_FLOW_FORMAT);
if (!out_images[i].Valid()) return;
}
const std::vector<VkDescriptorSet> sets = AllocateLsfgDescriptorSets(
device, descriptor_pool, pass.SetLayout(),
static_cast<uint32_t>(descriptor_sets.size()));
if (sets.size() != descriptor_sets.size()) return;
const VkSampler sampler = resources.GetSampler();
const VkBuffer buffer = resources.GetBuffer();
for (size_t i = 0; i < descriptor_sets.size(); ++i) {
descriptor_sets[i] = sets[i];
LsfgDescriptorWriter(descriptor_sets[i])
.AddUniformBuffer(buffer, LsfgResources::BufferSize())
.AddSampler(sampler)
.AddSampledImage((*frames)[i])
.AddStorageImages(out_images)
.Build(device);
}
}
void LsfgMipmaps::Dispatch(VkCommandBuffer cmdbuf, uint64_t frame_count) {
const size_t slot = frame_count % descriptor_sets.size();
LsfgBarriers(cmdbuf).WriteToRead((*frames)[slot]).ReadToWriteAll(out_images).Build();
pass.Bind(cmdbuf, descriptor_sets[slot]);
vkd.CmdDispatch(cmdbuf, GroupCount(flow_extent.width), GroupCount(flow_extent.height), 1);
}
}
@@ -0,0 +1,52 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2025 lsfg-vk
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Ported into Bannerlator's compositor from WinNative (GPL-3.0-or-later),
// whose LSFG port is credited to Camille LaVey / the Eden Emulator Project and
// follows upstream lsfg-vk. Only the Vulkan dispatch differs: Bannerlator
// resolves entry points through the renderer's own table (see lsfg_vkd.h).
#pragma once
#include <array>
#include "lsfg_common.hpp"
namespace lsfg {
class LsfgShaders;
class LsfgMipmaps {
public:
LsfgMipmaps() = default;
LsfgMipmaps(const Device& device, const LsfgShaders& shaders, LsfgResources& resources,
VkDescriptorPool descriptor_pool, LsfgImagePair& frames, float flow_scale);
void Dispatch(VkCommandBuffer cmdbuf, uint64_t frame_count);
[[nodiscard]] LsfgImage& Output(size_t level) {
return out_images[level];
}
[[nodiscard]] VkExtent2D FlowExtent() const {
return flow_extent;
}
[[nodiscard]] bool Valid() const {
return pass.Valid() && descriptor_sets[0] != VK_NULL_HANDLE;
}
private:
LsfgImagePair* frames{};
LsfgPass pass;
std::array<VkDescriptorSet, 2> descriptor_sets{};
VkExtent2D flow_extent{};
std::array<LsfgImage, LSFG_MIP_LEVELS> out_images;
};
}
@@ -0,0 +1,193 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Ported into Bannerlator's compositor from WinNative (GPL-3.0-or-later),
// whose LSFG port is credited to Camille LaVey / the Eden Emulator Project and
// follows upstream lsfg-vk. Only the Vulkan dispatch differs: Bannerlator
// resolves entry points through the renderer's own table (see lsfg_vkd.h).
#include "lsfg_pacer.hpp"
#include <algorithm>
#include <cmath>
namespace lsfg {
namespace {
using Clock = std::chrono::steady_clock;
constexpr float INTERVAL_SMOOTHING = 0.25f;
constexpr float SOURCE_SMOOTHING = 0.15f;
constexpr float SOURCE_STALE_SECONDS = 0.5f;
constexpr float DISCONTINUITY_SECONDS = 0.25f;
constexpr float HEADROOM_EPSILON = 0.02f;
constexpr float CREDIT_EPSILON = 1.0e-4f;
constexpr float SOURCE_ACCUM_FLOOR = 0.01f;
constexpr uint32_t MIN_RATE_SAMPLES = 12;
}
size_t LsfgPacer::MaxGenerations() const {
if (config.multiplier < 2) return 0;
if (config.target_rate != 0) return LSFG_MAX_MULTIPLIER - 1;
return std::min<size_t>(config.multiplier, LSFG_MAX_MULTIPLIER) - 1;
}
void LsfgPacer::TrackSourceRate(Clock::time_point now, uint64_t source_frames) {
if (!last_source_sample) {
last_source_sample = now;
last_source_frames = source_frames;
return;
}
const float elapsed = std::chrono::duration<float>(now - *last_source_sample).count();
if (elapsed <= 0.0f) {
return;
}
last_source_sample = now;
const uint64_t drawn =
source_frames > last_source_frames ? source_frames - last_source_frames : 0;
last_source_frames = source_frames;
if (elapsed > SOURCE_STALE_SECONDS) {
source_frame_accum = 0.0f;
source_time_accum = 0.0f;
source_interval = 0.0f;
source_samples = 0;
return;
}
last_drawn = drawn;
last_elapsed = elapsed;
source_frame_accum += (static_cast<float>(drawn) - source_frame_accum) * SOURCE_SMOOTHING;
source_time_accum += (elapsed - source_time_accum) * SOURCE_SMOOTHING;
source_interval =
source_frame_accum > SOURCE_ACCUM_FLOOR ? source_time_accum / source_frame_accum : 0.0f;
if (source_samples < MIN_RATE_SAMPLES) ++source_samples;
}
void LsfgPacer::TrackLoopRate(float interval_seconds) {
loop_interval = loop_interval > 0.0f
? loop_interval + (interval_seconds - loop_interval) * INTERVAL_SMOOTHING
: interval_seconds;
if (loop_samples < MIN_RATE_SAMPLES) ++loop_samples;
}
bool LsfgPacer::RatesSettled() const {
return source_samples >= MIN_RATE_SAMPLES && loop_samples >= MIN_RATE_SAMPLES;
}
size_t LsfgPacer::HeadroomLimit() const {
if (config.refresh_rate <= 0.0f || source_interval <= 0.0f ||
source_samples < MIN_RATE_SAMPLES) {
return LSFG_MAX_MULTIPLIER - 1;
}
const float budget = std::ceil(config.refresh_rate * source_interval - HEADROOM_EPSILON);
return budget < 2.0f ? 0 : static_cast<size_t>(budget) - 1;
}
LsfgPlan LsfgPacer::Plan(size_t capacity, uint64_t source_frames) {
const size_t ceiling = std::min(capacity, MaxGenerations());
if (ceiling == 0) {
Reset();
return {};
}
const Clock::time_point now = Clock::now();
TrackSourceRate(now, source_frames);
if (!last_frame) {
last_frame = now;
return {};
}
const float interval_seconds = std::chrono::duration<float>(now - *last_frame).count();
last_frame = now;
if (interval_seconds <= 0.0f || interval_seconds > DISCONTINUITY_SECONDS) {
output_credit = 0.0f;
return LsfgPlan{0, true};
}
TrackLoopRate(interval_seconds);
float target_rate = static_cast<float>(config.target_rate);
if (target_rate > 0.0f && config.refresh_rate > 0.0f) {
target_rate = std::min(target_rate, config.refresh_rate);
}
if (target_rate == 0.0f) {
// No explicit target rate: the multiplier is the multiplier, full stop.
// The panel-headroom clamp was here briefly and was removed at the
// user's decision; this is also upstream's behaviour on this branch.
//
// What that means above the panel's refresh rate, for the record: the
// display cannot show more frames than it refreshes, so under FIFO the
// surplus presents queue and the render thread waits on acquire. Nothing
// is displayed faster, and the compositor falls behind on guest
// deliveries instead. Below the refresh rate - every source at or under
// panel/4 - this branch behaves identically with or without the clamp.
// To restore it: limit = std::min(ceiling, HeadroomLimit()).
output_credit = 0.0f;
limit = ceiling;
return LsfgPlan{limit, true};
}
const size_t allowed = std::min(ceiling, HeadroomLimit());
const float desired_outputs = loop_interval * target_rate;
if (allowed == 0 || desired_outputs <= 1.0f) {
output_credit = 0.0f;
limit = 0;
return {};
}
output_credit += desired_outputs;
const size_t outputs =
std::max<size_t>(1, static_cast<size_t>(std::floor(output_credit + CREDIT_EPSILON)));
const size_t generations = std::min(outputs - 1, allowed);
output_credit -= static_cast<float>(generations + 1);
if (output_credit < 0.0f) {
output_credit = 0.0f;
} else if (generations == allowed && output_credit >= 1.0f) {
output_credit = std::fmod(output_credit, 1.0f);
}
limit = generations;
return LsfgPlan{generations, true};
}
LsfgPacerStats LsfgPacer::Stats() const {
LsfgPacerStats stats;
stats.source_rate = source_interval > 0.0f ? 1.0f / source_interval : 0.0f;
stats.loop_rate = loop_interval > 0.0f ? 1.0f / loop_interval : 0.0f;
stats.refresh_rate = config.refresh_rate;
stats.target_rate = static_cast<float>(config.target_rate);
stats.slots = config.refresh_rate * source_interval;
stats.limit = limit;
stats.rates_settled = RatesSettled();
stats.last_drawn = last_drawn;
stats.last_elapsed = last_elapsed;
stats.source_frames = last_source_frames;
return stats;
}
void LsfgPacer::Reset() {
last_frame.reset();
last_source_sample.reset();
last_source_frames = 0;
source_interval = 0.0f;
source_frame_accum = 0.0f;
source_time_accum = 0.0f;
loop_interval = 0.0f;
source_samples = 0;
loop_samples = 0;
last_drawn = 0;
last_elapsed = 0.0f;
output_credit = 0.0f;
limit = 0;
}
}
@@ -0,0 +1,87 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Ported into Bannerlator's compositor from WinNative (GPL-3.0-or-later),
// whose LSFG port is credited to Camille LaVey / the Eden Emulator Project and
// follows upstream lsfg-vk. Only the Vulkan dispatch differs: Bannerlator
// resolves entry points through the renderer's own table (see lsfg_vkd.h).
#pragma once
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <optional>
namespace lsfg {
constexpr size_t LSFG_MAX_MULTIPLIER = 4;
struct LsfgPacerConfig {
uint32_t multiplier{2};
uint32_t target_rate{};
float refresh_rate{};
};
struct LsfgPlan {
size_t generations{};
bool warm{};
};
struct LsfgPacerStats {
float source_rate{};
float loop_rate{};
float refresh_rate{};
float target_rate{};
float slots{};
size_t limit{};
bool rates_settled{};
uint64_t last_drawn{};
float last_elapsed{};
uint64_t source_frames{};
};
class LsfgPacer {
public:
void SetConfig(const LsfgPacerConfig& config_) {
config = config_;
}
[[nodiscard]] const LsfgPacerConfig& Config() const {
return config;
}
[[nodiscard]] size_t MaxGenerations() const;
[[nodiscard]] LsfgPlan Plan(size_t capacity, uint64_t source_frames);
[[nodiscard]] LsfgPacerStats Stats() const;
void Reset();
private:
using Clock = std::chrono::steady_clock;
void TrackSourceRate(Clock::time_point now, uint64_t source_frames);
void TrackLoopRate(float interval_seconds);
[[nodiscard]] bool RatesSettled() const;
[[nodiscard]] size_t HeadroomLimit() const;
LsfgPacerConfig config;
std::optional<Clock::time_point> last_frame;
std::optional<Clock::time_point> last_source_sample;
uint64_t last_source_frames{};
float source_interval{};
float source_frame_accum{};
float source_time_accum{};
float loop_interval{};
uint32_t source_samples{};
uint32_t loop_samples{};
uint64_t last_drawn{};
float last_elapsed{};
float output_credit{};
size_t limit{};
};
}
@@ -0,0 +1,166 @@
// See lsfg_probe.h. Capability gate for native compositor-side LSFG.
#include "lsfg_probe.h"
#include "../VulkanRendererContext.h"
#include <cstdio>
#include <cstring>
namespace lsfg {
namespace {
bool hasExtension(const std::vector<VkExtensionProperties>& exts, const char* name) {
for (const auto& e : exts)
if (strcmp(e.extensionName, name) == 0) return true;
return false;
}
} // namespace
FeatureSupport queryFeatures(const VkTable& vk, VkPhysicalDevice pd,
const std::vector<VkExtensionProperties>& deviceExtensions,
bool allowVk11) {
FeatureSupport fs{};
if (pd == VK_NULL_HANDLE || !vk.GetPhysicalDeviceProperties) return fs;
VkPhysicalDeviceProperties props{};
vk.GetPhysicalDeviceProperties(pd, &props);
fs.deviceApiVersion = props.apiVersion;
fs.apiAtLeast13 = props.apiVersion >= VK_API_VERSION_1_3;
if (!vk.GetPhysicalDeviceFeatures2) return fs;
const bool below12 = props.apiVersion < VK_API_VERSION_1_2;
if (below12) {
// Below 1.2 there is no VkPhysicalDeviceVulkan12Features to chain, and
// the SPIR-V 1.6 modules will not load anyway. Unless the experimental
// compat path is on AND the driver offers the extensions that make the
// chain loadable as SPIR-V 1.4, don't touch the device further:
// device creation stays exactly as it is today.
if (!allowVk11) return fs;
fs.hasSpirv14 = hasExtension(deviceExtensions, VK_KHR_SPIRV_1_4_EXTENSION_NAME);
fs.hasFloatControls = hasExtension(deviceExtensions, VK_KHR_SHADER_FLOAT_CONTROLS_EXTENSION_NAME);
fs.hasMemoryModelExt = hasExtension(deviceExtensions, VK_KHR_VULKAN_MEMORY_MODEL_EXTENSION_NAME);
if (!fs.hasSpirv14 || !fs.hasFloatControls || !fs.hasMemoryModelExt) {
// Report what was looked at so explain() can name the gap.
fs.queried = true;
return fs;
}
fs.extensionPath = true;
fs.spirvTarget = kSpirv14;
} else if (!fs.apiAtLeast13 && allowVk11) {
// 1.2: SPIR-V 1.5 is core, VulkanMemoryModel is core. Nothing to
// enable beyond the features; the modules only need their header
// lowered from 1.6 to 1.5. Same experimental gate as the 1.1 path so
// a production build keeps rejecting 1.2 exactly as before.
fs.extensionPath = true;
fs.spirvTarget = kSpirv15;
}
// On 1.2+ the Vulkan12Features struct carries the memory model; below
// that the KHR struct does (same sType value, same layout).
VkPhysicalDeviceVulkan12Features v12{};
v12.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
VkPhysicalDeviceVulkanMemoryModelFeaturesKHR mm{};
mm.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_MEMORY_MODEL_FEATURES_KHR;
VkPhysicalDeviceFeatures2 f2{};
f2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
f2.pNext = below12 ? (void*)&mm : (void*)&v12;
vk.GetPhysicalDeviceFeatures2(pd, &f2);
fs.queried = true;
if (below12) {
fs.vulkanMemoryModel = mm.vulkanMemoryModel == VK_TRUE;
fs.vulkanMemoryModelDeviceScope = mm.vulkanMemoryModelDeviceScope == VK_TRUE;
} else {
fs.vulkanMemoryModel = v12.vulkanMemoryModel == VK_TRUE;
fs.vulkanMemoryModelDeviceScope = v12.vulkanMemoryModelDeviceScope == VK_TRUE;
}
fs.storageImageWriteWithoutFormat = f2.features.shaderStorageImageWriteWithoutFormat == VK_TRUE;
fs.storageImageExtendedFormats = f2.features.shaderStorageImageExtendedFormats == VK_TRUE;
return fs;
}
std::vector<const char*> extensionNames(const FeatureSupport& f) {
std::vector<const char*> out;
if (!f.extensionPath || f.deviceApiVersion >= VK_API_VERSION_1_2) return out;
out.push_back(VK_KHR_SHADER_FLOAT_CONTROLS_EXTENSION_NAME); // dependency of spirv_1_4
out.push_back(VK_KHR_SPIRV_1_4_EXTENSION_NAME);
out.push_back(VK_KHR_VULKAN_MEMORY_MODEL_EXTENSION_NAME);
return out;
}
bool probeLinearBlit(const VkTable& vk, VkPhysicalDevice pd, VkFormat fmt) {
if (pd == VK_NULL_HANDLE || fmt == VK_FORMAT_UNDEFINED
|| !vk.GetPhysicalDeviceFormatProperties) return false;
VkFormatProperties fp{};
vk.GetPhysicalDeviceFormatProperties(pd, fmt, &fp);
return (fp.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT) != 0;
}
bool probeStorageFormat(const VkTable& vk, VkPhysicalDevice pd, VkFormat fmt) {
if (pd == VK_NULL_HANDLE || fmt == VK_FORMAT_UNDEFINED
|| !vk.GetPhysicalDeviceFormatProperties) return false;
VkFormatProperties fp{};
vk.GetPhysicalDeviceFormatProperties(pd, fmt, &fp);
// The composite target is COLOR_ATTACHMENT (effect chain writes it),
// SAMPLED (next frame's LSFG input reads it), STORAGE (generate writes it)
// and both blit ends (it is copied - or, below panel resolution, blitted -
// into the swapchain image).
const VkFormatFeatureFlags need =
VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT
| VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT
| VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT
| VK_FORMAT_FEATURE_BLIT_SRC_BIT
| VK_FORMAT_FEATURE_BLIT_DST_BIT;
return (fp.optimalTilingFeatures & need) == need;
}
void explain(Caps& caps) {
const FeatureSupport& f = caps.features;
const char* why = nullptr;
if (!f.queried) {
why = f.deviceApiVersion < VK_API_VERSION_1_2
? "device Vulkan version below 1.2"
: "vkGetPhysicalDeviceFeatures2 unavailable";
} else if (!f.apiAtLeast13 && !f.extensionPath) {
if (f.deviceApiVersion < VK_API_VERSION_1_2) {
why = !f.hasSpirv14 ? "Vulkan 1.1 driver lacks VK_KHR_spirv_1_4"
: !f.hasFloatControls ? "Vulkan 1.1 driver lacks VK_KHR_shader_float_controls"
: "Vulkan 1.1 driver lacks VK_KHR_vulkan_memory_model";
} else {
why = "device Vulkan version below 1.3 (SPIR-V 1.6 will not load)";
}
} else if (!f.vulkanMemoryModel) {
why = "driver lacks vulkanMemoryModel";
} else if (!f.storageImageWriteWithoutFormat) {
why = "driver lacks shaderStorageImageWriteWithoutFormat";
} else if (!f.storageImageExtendedFormats) {
why = "driver lacks shaderStorageImageExtendedFormats";
} else if (!caps.featuresEnabled) {
why = "required features not enabled at device creation";
} else if (!caps.storageOnSwapchainFormat) {
why = "swapchain format is not storage-image capable";
}
if (why) {
snprintf(caps.reason, sizeof(caps.reason), "unsupported: %s", why);
} else {
snprintf(caps.reason, sizeof(caps.reason),
"supported (device Vulkan %u.%u.%u%s, fmt %d)",
VK_VERSION_MAJOR(f.deviceApiVersion),
VK_VERSION_MINOR(f.deviceApiVersion),
VK_VERSION_PATCH(f.deviceApiVersion),
f.extensionPath ? " via extensions, SPIR-V lowered" : "",
(int)caps.probedFormat);
}
}
} // namespace lsfg
+111
View File
@@ -0,0 +1,111 @@
#pragma once
// ============================================================================
// lsfg_probe - capability gate for native (compositor-side) LSFG frame
// generation.
//
// The Lossless Scaling chain is 25 compute shaders that DXVK's DXBC translator
// emits as SPIR-V 1.6 with OpCapability VulkanMemoryModel and
// StorageImageWriteWithoutFormat. Three consequences, all checked here:
//
// * SPIR-V 1.6 will not load on a Vulkan 1.1 device, so the DEVICE (not just
// the instance) must report 1.3+ ... OR, on the experimental Vulkan 1.1
// compat path, offer VK_KHR_spirv_1_4 + VK_KHR_vulkan_memory_model so the
// same modules can be handed over as SPIR-V 1.4 (see downgradeSpirv in
// lsfg_dll.h). The stock Qualcomm driver on Adreno 7xx phones reports
// 1.1.128 yet carries every one of those extensions (measured on an
// Adreno 710 / HyperOS device with `cmd gpu vkjson`).
// * vulkanMemoryModel, shaderStorageImageWriteWithoutFormat and
// shaderStorageImageExtendedFormats must be ENABLED at device creation.
// Today the renderer enables no features at all, so all three are off.
// * `generate` writes into a storage image, and Android swapchain formats
// are frequently not storage-capable - so the format is probed separately
// once the swapchain has picked one.
//
// A device failing any gate reports unsupported UP FRONT, with a reason, so
// the UI can grey the engine out instead of failing later inside
// vkCreateShaderModule or vkCreateComputePipelines.
// ============================================================================
#include <vulkan/vulkan.h>
#include <cstdint>
#include <vector>
#include "lsfg_dll.h" // kSpirv14/15/16
struct VkTable;
namespace lsfg {
// Which of the three required features the physical device OFFERS. Queried
// before vkCreateDevice; what we actually enable is recorded in Caps below.
struct FeatureSupport {
bool queried = false; // vkGetPhysicalDeviceFeatures2 resolved
bool apiAtLeast13 = false;
bool vulkanMemoryModel = false;
bool vulkanMemoryModelDeviceScope= false;
bool storageImageWriteWithoutFormat = false;
bool storageImageExtendedFormats = false;
uint32_t deviceApiVersion = 0;
// Experimental Vulkan 1.1 compat: the device is below 1.3 but offers the
// extensions that make the chain loadable. When set, the renderer must
// enable those extensions at device creation (see extensionNames) and the
// modules are downgraded to `spirvTarget` at load.
bool extensionPath = false;
bool hasSpirv14 = false; // VK_KHR_spirv_1_4
bool hasFloatControls = false; // VK_KHR_shader_float_controls (spirv_1_4 depends on it)
bool hasMemoryModelExt = false; // VK_KHR_vulkan_memory_model
uint32_t spirvTarget = kSpirv16;
// Every hard device-level gate passes (format is checked separately).
bool deviceGatesPass() const {
return queried && (apiAtLeast13 || extensionPath) && vulkanMemoryModel
&& storageImageWriteWithoutFormat && storageImageExtendedFormats;
}
};
// What was actually enabled + the running verdict. Owned by the renderer.
struct Caps {
FeatureSupport features;
bool featuresEnabled = false; // the chain was passed to vkCreateDevice
bool storageOnSwapchainFormat = false;
// The swapchain format blits with VK_FILTER_LINEAR (needs
// SAMPLED_IMAGE_FILTER_LINEAR on the source). Not a gate: without it the
// capture-resolution upscale falls back to NEAREST.
bool linearBlitOnSwapchainFormat = false;
VkFormat probedFormat = VK_FORMAT_UNDEFINED;
char reason[160] = "not probed";
// The single question the UI and the render path ask.
bool supported() const {
return featuresEnabled && features.deviceGatesPass() && storageOnSwapchainFormat;
}
};
// Ask the physical device which of the required features it offers.
// Safe on any driver: if vkGetPhysicalDeviceFeatures2 cannot be resolved, or
// the device reports below Vulkan 1.2 and `allowVk11` is false, nothing is
// chained and `queried` is left false - the caller then creates the device
// exactly as it always has. With `allowVk11`, a 1.1 device is accepted when
// `deviceExtensions` lists VK_KHR_spirv_1_4, VK_KHR_shader_float_controls and
// VK_KHR_vulkan_memory_model, and the memory-model features are queried
// through the KHR struct instead of VkPhysicalDeviceVulkan12Features.
FeatureSupport queryFeatures(const VkTable& vk, VkPhysicalDevice pd,
const std::vector<VkExtensionProperties>& deviceExtensions,
bool allowVk11);
// The device extensions the extension path needs enabled, in the order they
// should be pushed onto VkDeviceCreateInfo. Empty unless extensionPath.
std::vector<const char*> extensionNames(const FeatureSupport& f);
// Probe VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT on the live swapchain format.
// `generate` writes into an image of this format via a compute dispatch.
bool probeStorageFormat(const VkTable& vk, VkPhysicalDevice pd, VkFormat fmt);
// Whether the format may be the source of a VK_FILTER_LINEAR blit.
bool probeLinearBlit(const VkTable& vk, VkPhysicalDevice pd, VkFormat fmt);
// Fill caps.reason with the FIRST gate that failed (or "supported").
void explain(Caps& caps);
} // namespace lsfg
@@ -0,0 +1,100 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Ported into Bannerlator's compositor from WinNative (GPL-3.0-or-later),
// whose LSFG port is credited to Camille LaVey / the Eden Emulator Project and
// follows upstream lsfg-vk. Only the Vulkan dispatch differs: Bannerlator
// resolves entry points through the renderer's own table (see lsfg_vkd.h).
#include "lsfg_shaders.hpp"
#include "lsfg_common.hpp"
#include "lsfg_dll.h"
#include <android/log.h>
#define LOG_TAG "LsfgShaders"
#define SHADER_LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define SHADER_LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
namespace lsfg {
LsfgShaders::LsfgShaders(const Device& device_, const std::string& cache_path,
uint32_t spirv_target)
: device{device_.Handle()} {
ModuleSet set;
const DllStatus status = loadModules(cache_path, set);
if (status != DllStatus::Ok) {
SHADER_LOGE("Shader cache unusable (%s)", statusName(status));
return;
}
// Vulkan 1.1/1.2 compat: the cache is always built at the translator's
// native SPIR-V 1.6 (no device exists at import time), so lower each module
// to what THIS device accepts. A no-op on 1.3+ devices. Only the
// DXBC-translated chain is lowered: downgradeSpirv knows the capabilities
// DXVK emits, not whatever a precompiled vendor SPIR-V variant may carry,
// and relabelling one of those could hand the driver invalid SPIR-V.
uint32_t lowered = 0;
for (Module& module : set.modules) {
if (module.words.size() > 1 && module.words[1] > spirv_target) {
if (set.variant != Variant::DxbcTranslated) {
SHADER_LOGE("shader %u: precompiled SPIR-V 0x%x is above this device's 0x%x; not lowering",
module.id, module.words[1], spirv_target);
return;
}
if (!downgradeSpirv(module.words, spirv_target)) {
SHADER_LOGE("shader %u: could not lower SPIR-V 0x%x to 0x%x",
module.id, module.words[1], spirv_target);
return;
}
lowered++;
}
}
if (lowered)
SHADER_LOGI("Lowered %u modules to SPIR-V 0x%x for this device", lowered, spirv_target);
for (const Module& module : set.modules) {
VkShaderModuleCreateInfo module_ci{};
module_ci.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
module_ci.codeSize = module.words.size() * sizeof(uint32_t);
module_ci.pCode = module.words.data();
VkShaderModule handle = VK_NULL_HANDLE;
if (vkd.CreateShaderModule(device, &module_ci, nullptr, &handle) != VK_SUCCESS) {
SHADER_LOGE("vkCreateShaderModule failed for shader %u", module.id);
Release();
return;
}
modules.emplace(module.id, handle);
}
valid = modules.size() == kShaderCount;
if (valid) {
SHADER_LOGI("Created %zu LSFG shader modules, variant=%s", modules.size(),
variantName(set.variant));
} else {
SHADER_LOGE("Expected %u shader modules, got %zu", kShaderCount, modules.size());
Release();
}
}
LsfgShaders::~LsfgShaders() {
Release();
}
void LsfgShaders::Release() {
if (device != VK_NULL_HANDLE) {
for (auto& [id, module] : modules) {
vkd.DestroyShaderModule(device, module, nullptr);
}
}
modules.clear();
valid = false;
}
VkShaderModule LsfgShaders::Get(uint32_t shader_id) const {
const auto it = modules.find(shader_id);
return it == modules.end() ? VK_NULL_HANDLE : it->second;
}
}
@@ -0,0 +1,47 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Ported into Bannerlator's compositor from WinNative (GPL-3.0-or-later),
// whose LSFG port is credited to Camille LaVey / the Eden Emulator Project and
// follows upstream lsfg-vk. Only the Vulkan dispatch differs: Bannerlator
// resolves entry points through the renderer's own table (see lsfg_vkd.h).
#pragma once
#include <cstdint>
#include <map>
#include <string>
#include "lsfg_dll.h"
#include "lsfg_vkd.h"
namespace lsfg {
class Device;
class LsfgShaders {
public:
LsfgShaders() = default;
// `spirv_target` is the highest SPIR-V version the device accepts (see
// lsfg_probe.h); cached modules above it are lowered before creation.
LsfgShaders(const Device& device, const std::string& cache_path,
uint32_t spirv_target = kSpirv16);
~LsfgShaders();
LsfgShaders(const LsfgShaders&) = delete;
LsfgShaders& operator=(const LsfgShaders&) = delete;
[[nodiscard]] bool IsValid() const {
return valid;
}
[[nodiscard]] VkShaderModule Get(uint32_t shader_id) const;
private:
void Release();
VkDevice device{VK_NULL_HANDLE};
std::map<uint32_t, VkShaderModule> modules;
bool valid{};
};
}
@@ -0,0 +1,66 @@
// See lsfg_vkd.h. Fills the chain's dispatch from the renderer's own table.
#include "lsfg_vkd.h"
#include "../VulkanRendererContext.h"
#include <android/log.h>
LsfgVkDispatch vkd;
namespace {
bool g_ready = false;
}
bool lsfgVkdInit(const VkTable& table) {
bool ok = true;
#define COPY(fn) \
do { \
vkd.fn = table.fn; \
if (!vkd.fn) { \
__android_log_print(ANDROID_LOG_ERROR, "LsfgVkd", \
"entry point vk" #fn " did not resolve"); \
ok = false; \
} \
} while (0)
COPY(AllocateDescriptorSets);
COPY(AllocateMemory);
COPY(BindBufferMemory);
COPY(BindImageMemory);
COPY(CmdBindDescriptorSets);
COPY(CmdBindPipeline);
COPY(CmdCopyImage);
COPY(CmdDispatch);
COPY(CmdPipelineBarrier);
COPY(CreateBuffer);
COPY(CreateComputePipelines);
COPY(CreateDescriptorPool);
COPY(CreateDescriptorSetLayout);
COPY(CreateImage);
COPY(CreateImageView);
COPY(CreatePipelineLayout);
COPY(CreateSampler);
COPY(CreateShaderModule);
COPY(DestroyBuffer);
COPY(DestroyDescriptorPool);
COPY(DestroyDescriptorSetLayout);
COPY(DestroyImage);
COPY(DestroyImageView);
COPY(DestroyPipeline);
COPY(DestroyPipelineLayout);
COPY(DestroySampler);
COPY(DestroyShaderModule);
COPY(FreeMemory);
COPY(GetBufferMemoryRequirements);
COPY(GetImageMemoryRequirements);
COPY(GetPhysicalDeviceMemoryProperties);
COPY(MapMemory);
COPY(UnmapMemory);
COPY(UpdateDescriptorSets);
#undef COPY
g_ready = ok;
return ok;
}
bool lsfgVkdReady() { return g_ready; }
+70
View File
@@ -0,0 +1,70 @@
#pragma once
// ============================================================================
// lsfg_vkd - the Vulkan entry points the LSFG chain calls, as a single global.
//
// Two dispatch styles meet here. Bannerlator's compositor resolves Vulkan
// through a per-context VkTable (`vk_`), because an adrenotools-loaded driver
// lives in an isolated linker namespace and shares no global symbols with the
// system loader. The LSFG chain, ported from eden by way of WinNative, calls
// through a file-scope `vkd`. Rather than rewrite ~2,300 lines of proven chain
// code to thread a table through every call, this shim exposes exactly the 34
// entry points the chain uses and is filled from the renderer's own table when
// the chain is created.
//
// That keeps the ported files close to their upstream form, which matters:
// they are the part of this feature that is hardest to review by eye and
// easiest to break with a well-meaning edit.
//
// Single-renderer assumption: there is one Vulkan compositor per process, so
// one global table is correct. lsfgVkdInit() is idempotent and simply
// overwrites; lsfgVkdReady() reports whether every pointer resolved.
// ============================================================================
#include <vulkan/vulkan.h>
struct VkTable;
struct LsfgVkDispatch {
PFN_vkAllocateDescriptorSets AllocateDescriptorSets = nullptr;
PFN_vkAllocateMemory AllocateMemory = nullptr;
PFN_vkBindBufferMemory BindBufferMemory = nullptr;
PFN_vkBindImageMemory BindImageMemory = nullptr;
PFN_vkCmdBindDescriptorSets CmdBindDescriptorSets = nullptr;
PFN_vkCmdBindPipeline CmdBindPipeline = nullptr;
PFN_vkCmdCopyImage CmdCopyImage = nullptr;
PFN_vkCmdDispatch CmdDispatch = nullptr;
PFN_vkCmdPipelineBarrier CmdPipelineBarrier = nullptr;
PFN_vkCreateBuffer CreateBuffer = nullptr;
PFN_vkCreateComputePipelines CreateComputePipelines = nullptr;
PFN_vkCreateDescriptorPool CreateDescriptorPool = nullptr;
PFN_vkCreateDescriptorSetLayout CreateDescriptorSetLayout = nullptr;
PFN_vkCreateImage CreateImage = nullptr;
PFN_vkCreateImageView CreateImageView = nullptr;
PFN_vkCreatePipelineLayout CreatePipelineLayout = nullptr;
PFN_vkCreateSampler CreateSampler = nullptr;
PFN_vkCreateShaderModule CreateShaderModule = nullptr;
PFN_vkDestroyBuffer DestroyBuffer = nullptr;
PFN_vkDestroyDescriptorPool DestroyDescriptorPool = nullptr;
PFN_vkDestroyDescriptorSetLayout DestroyDescriptorSetLayout = nullptr;
PFN_vkDestroyImage DestroyImage = nullptr;
PFN_vkDestroyImageView DestroyImageView = nullptr;
PFN_vkDestroyPipeline DestroyPipeline = nullptr;
PFN_vkDestroyPipelineLayout DestroyPipelineLayout = nullptr;
PFN_vkDestroySampler DestroySampler = nullptr;
PFN_vkDestroyShaderModule DestroyShaderModule = nullptr;
PFN_vkFreeMemory FreeMemory = nullptr;
PFN_vkGetBufferMemoryRequirements GetBufferMemoryRequirements = nullptr;
PFN_vkGetImageMemoryRequirements GetImageMemoryRequirements = nullptr;
PFN_vkGetPhysicalDeviceMemoryProperties GetPhysicalDeviceMemoryProperties = nullptr;
PFN_vkMapMemory MapMemory = nullptr;
PFN_vkUnmapMemory UnmapMemory = nullptr;
PFN_vkUpdateDescriptorSets UpdateDescriptorSets = nullptr;
};
extern LsfgVkDispatch vkd;
// Fill `vkd` from the renderer's table. Returns false (and leaves the chain
// unusable) if any entry point failed to resolve - better to report the engine
// unsupported than to call through a null pointer mid-frame.
bool lsfgVkdInit(const VkTable& table);
bool lsfgVkdReady();
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2026 The412Banner
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.
@@ -0,0 +1,47 @@
# NOTICE - FidelityFX Optical Flow (model 3)
win-fg "model 3" (FSR3 Optical Flow) embeds compute shaders that are an
adaptation of the **AMD FidelityFX SDK FSR3 Optical Flow** algorithm.
Source passes adapted (FidelityFX-SDK, branch `main`):
- `Kits/FidelityFX/framegeneration/fsr3/include/gpu/opticalflow/ffx_opticalflow_prepare_luma.h`
→ `of3_luma.comp` (Rec.709 LDR luminance extraction)
- `Kits/FidelityFX/framegeneration/fsr3/include/gpu/opticalflow/ffx_opticalflow_compute_luminance_pyramid.h`
→ `of3_downsample.comp` (4-tap box / SPD-equivalent mip reduce)
- `Kits/FidelityFX/framegeneration/fsr3/include/gpu/opticalflow/ffx_opticalflow_compute_optical_flow_v5.h`
→ `of3_flow.comp` (SAD block-match search with coarse-level prediction)
- `Kits/FidelityFX/framegeneration/fsr3/include/gpu/opticalflow/ffx_opticalflow_scale_optical_flow_advanced_v5.h`
→ `of3_expand.comp` (bilinear upscale of coarse flow to display resolution)
The GLSL adaptations are subgroup-free / SPD-library-free re-implementations so
they compile with `glslangValidator` for Vulkan 1.1 (Adreno/Turnip-safe). They
intentionally drop the SDK's HDR transfer-function handling, wave intrinsics
(`ffxWaveSum`), `msad4`, packed-uint luma and groupshared SAD maps, none of
which are portable to bare GLSL on this stack. See per-shader DEVIATION comments.
FidelityFX SDK is distributed under the MIT License:
```
This file is part of the FidelityFX SDK.
Copyright (C) Advanced Micro Devices, Inc.
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.
```
+357
View File
@@ -0,0 +1,357 @@
// win-fg - runtime config: enable gate, model, multiplier, and the synthesis
// tuning parameters. Sourced from environment variables and an optional
// conf.toml (a config file, when present, wins over the env defaults).
#pragma once
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <string>
#include <fstream>
#include <sstream>
namespace winfg {
struct Config {
bool enabled = false; // gate; loader also honours the manifest enable var
// GRANULAR PRESENT-PATH DEBUG TRACE. Default OFF. When ON, winfg_QueuePresentKHR
// emits a per-frame, step-by-step trail (logcat tag "win-fg") so a freeze/
// crash-on-enable leaves an obvious LAST line at the exact stage that hung or
// faulted (acquire-spare, compute-submit, present-generated/real, fence wait,
// fallback). Zero behaviour change and effectively zero cost when off - one
// predicted-not-taken bool test per step, no logcat writes.
// WIN_FG_DEBUG=1|0 / conf.toml debug=on|off.
bool debug = false;
// ── EVEN-CADENCE FRAME PACING ─────────────────────────────────────────────
// Default ON. The 2x insert path presents the GENERATED frame then the REAL
// frame back-to-back in one vkQueuePresentKHR hook call, so on Mailbox/
// Immediate both land in ~one instant followed by a full frame-interval gap
// until the next pair - uneven in time → judder ("slows down to catch up").
// With pacing on, the layer measures the real-frame interval (EMA of a
// monotonic clock) and holds the REAL present to its scheduled beat so the
// generated frame lands near the temporal MIDPOINT between consecutive real
// frames (even gen,real,gen,real cadence). Pure bounded CPU sleep - no GPU
// wait added; if a frame is already behind schedule the wait is SKIPPED
// (present immediately) so worst case is one un-paced pair, never a hitch.
// FIFO self-paces so the waits collapse to ~0 there. Off ⇒ byte-identical
// back-to-back behaviour. WIN_FG_PACING=on|off / conf.toml pacing=on|off.
// ISOLATION BUILD: default flipped to OFF to A/B the AYANEO-750 post-setting-change
// flicker regression against the pacing feature (everything else byte-identical).
bool pacing = false;
int model = 4; // 3 = symmetric flow, 4 = bidir + occlusion gate
int multiplier = 2; // generated presents per real present + 1
// EXTRA SWAPCHAIN IMAGE HEADROOM (device-freeze fix). On top of the +1 spare the
// insert path needs, request this many ADDITIONAL images at swapchain creation:
// requested minImageCount = app_min + 1 (spare) + extra_images,
// clamped to surfaceCaps.maxImageCount (0 = unlimited -> no clamp), never below
// app_min+1. The extra headroom stops the guest's NEXT vkAcquireNextImageKHR from
// starving while win-fg holds a spare + a real frame in flight to the AHB-backed
// host compositor. Some SoCs (Adreno 840 / Turnip) recycle displayed AHBs too
// slowly to tolerate the extra present with only the +1 spare and freeze on the
// first inserted frame; the AYANEO (Adreno 750) recycles fast enough to tolerate
// it. Default 2 (a few MB VRAM), applied on all devices. On a device that already
// works this only grows the pool - no FPS/quality change; the pool-headroom guard
// in QueuePresentKHR stays a no-op when headroom is adequate.
// WIN_FG_EXTRA_IMAGES / conf.toml extra_images.
int extraImages = 2;
// ── FLOW-RESOLUTION PERFORMANCE PRESET ────────────────────────────────────
// Trades base-FPS-drop against optical-flow quality by selecting the FINEST
// pyramid level the dense flow is solved at (FrameGen::kFlowFinest):
// 0 = Quality -> kFlowFinest = 1 (~1/2-res flow; best motion, biggest FG cost)
// 1 = Balanced -> kFlowFinest = 2 (~1/4-res; TODAY's behaviour) [DEFAULT]
// 2 = Performance -> kFlowFinest = 3 (~1/8-res; cheapest -> smallest base-FPS drop)
// Out-of-range clamps to Balanced (1), so an unset/older conf.toml is byte-
// identical to today. Changing this LIVE rebuilds the affected per-size flow
// images (like a resize) inside the layer and resets the flow predictor, so a
// preset change is fully hot - no FG toggle needed. See FrameGen::configure /
// FrameGen::flowFinestForPreset. WIN_FG_PERF_PRESET / conf.toml perf_preset.
int perfPreset = 1; // 0 quality, 1 balanced (default), 2 performance
float flowScale = 1.0f; // scales solved flow magnitude
// C1 GLOBAL-MOTION PRE-WARP. Estimate the per-frame camera affine (LK) and
// remove it before the dense SAD flow search so the search only sees coherent
// object-only residual motion (kills fast-pan "melt"). 0 = auto (engage only
// when the LK solve is stable), 1 = on (force-engage once a solve exists),
// 2 = off (never pre-warp; identity affine → pipeline == pre-C1 exactly).
// WIN_FG_GM=auto|on|off / conf.toml global_motion=auto|on|off.
int gmMode = 0; // 0 auto, 1 on, 2 off
// C2 FLOW REGULARIZATION (TV-L1 smoothness prior). After C1 removes the camera
// motion, the dense flow (flowLvl_ at kFlowFinest, 1/4-res) is the OBJECT-only
// residual; it still carries spurious per-block vectors. C2 runs N semi-implicit
// TV-L1 denoising iterations on that field - an L1 data term + edge-aware Total-
// Variation regularizer - to kill incoherent vectors while KEEPING true motion
// discontinuities (object edges) sharp. Solved in-place; expand's median + C1
// add-back consume the cleaned field unchanged. 0 iterations ⇒ byte-identical to
// pre-C2. See shaders/of3_flowreg.comp.
// WIN_FG_FLOWREG=auto|on|off / conf.toml flow_reg=auto|on|off.
int frMode = 0; // 0 auto (engaged), 1 on (engaged), 2 off
int frIters = 4; // TV-L1 iterations at kFlowFinest (0 ⇒ off)
float frLambda = 2.0f; // L1 data-fidelity weight (higher ⇒ trust SAD flow)
float frDt = 0.25f; // semi-implicit smoothing step (higher ⇒ smoother)
float frEdge = 8.0f; // luma-gradient edge sensitivity for the TV weight g
float frEps = 0.05f; // Charbonnier epsilon (px) for the TV diffusivity
// synthesis (wfg_synth) tuning
float beta = 8.0f; // softmax sharpness on importance Z
float lambda = 0.6f; // FB-consistency vs photometric weight
// Tightened 2026-08-17 to prioritise "no ghost" over max sharpness - the
// remaining single-frame ghosts came from pixels that the previous defaults
// still trusted with warp when they should have cross-faded. Raising both
// pushes borderline pixels toward the safe cross-fade path (softer, but no
// smear). Prewarp will lift the ceiling further; this is the "safety net"
// knob change until then.
// Walked back from (0.10, 9.0) tighten after user reported softening. The
// real fix for the residual ghosts is the 3a-fallback path change in
// layer.cpp (present real curr on fallback instead of blitting synth over);
// the gate can stay closer to the original values now.
float epsilon = 0.07f; // disocclusion floor (0.05 -> 0.10 -> 0.07)
float photoScale = 7.5f; // photometric residual scale into Z (6.0 -> 9.0 -> 7.5)
// HUD exclusion rect in pixel coords (x0,y0,x1,y1). Fragments inside are
// passed through as the real current frame (no warp, no synth) so text /
// overlays don't ghost. Disabled when x0 >= x1 or y0 >= y1 (the default).
// Sourced from WIN_FG_HUD_RECT="x0,y0,x1,y1" env var or conf.toml
// (hudRect="x0,y0,x1,y1"). Pattern is Isygold's Vegas DXVK framegen -
// host knows where the HUD is, tell the shader to skip it.
float hudX0 = 0.0f;
float hudY0 = 0.0f;
float hudX1 = 0.0f;
float hudY1 = 0.0f;
// ── TRAINING-DATA CAPTURE MODE (Route-B VFI dataset collection) ───────────
// A dev-only gate. When OFF (default) the present path is byte-identical to a
// build without this feature - a single bool test and nothing else. When ON,
// every real present is downscaled on the GPU, read back async, and written to
// disk as lossless QOI (motion-rich 256² triplet patches by default, or full
// downscaled frames) plus a JSONL manifest, for offline (i-1,i+1)->i training.
// NEVER captures win-fg's generated frames - capture reads the real swapchain
// image at the TOP of QueuePresentKHR, before any interpolation.
// WIN_FG_CAPTURE=on|off / conf.toml capture=on|off.
bool capture = false;
// Downscale target box (aspect-preserving, never upscales). WIN_FG_CAPTURE_W/H,
// conf capture_width/capture_height.
int captureW = 1280;
int captureH = 720;
// 0 = patch (self-contained aligned triplets, DEFAULT), 1 = frame (full
// downscaled frames in order). WIN_FG_CAPTURE_MODE=patch|frame.
int capMode = 0;
int capPatches = 3; // patches per triplet in patch mode (WIN_FG_CAPTURE_PATCHES)
int capPatchSize = 256; // patch edge in downscaled px (WIN_FG_CAPTURE_PATCH)
// Skip a unit whose inter-frame motion (mean per-pixel luma abs-diff, 0..255)
// is below this. WIN_FG_CAPTURE_MOTION / conf capture_motion.
float capMotion = 2.0f;
// Output dir; empty => $HOME/.cache/winfg-capture. WIN_FG_CAPTURE_DIR /
// conf capture_dir. A per-run session subdir is created under it.
std::string capDir;
// Rolling-shard size cap (MiB). Output is a small number of large packed
// .wfgcap containers, not thousands of loose files; a new shard opens when the
// current one passes this. WIN_FG_CAPTURE_SHARD_MB / conf capture_shard_mb.
int capShardMB = 1024;
void sanitize() {
if (model < 3) model = 3; if (model > 4) model = 4;
if (multiplier < 2) multiplier = 2; if (multiplier > 4) multiplier = 4;
if (extraImages < 0) extraImages = 0; if (extraImages > 8) extraImages = 8;
if (perfPreset < 0 || perfPreset > 2) perfPreset = 1; // clamp out-of-range -> Balanced
if (gmMode < 0) gmMode = 0; if (gmMode > 2) gmMode = 2;
if (frMode < 0) frMode = 0; if (frMode > 2) frMode = 2;
if (frIters < 0) frIters = 0; if (frIters > 16) frIters = 16;
if (frLambda < 0.0f) frLambda = 0.0f;
if (frDt < 0.01f) frDt = 0.01f; if (frDt > 4.0f) frDt = 4.0f;
if (frEdge < 0.0f) frEdge = 0.0f;
if (frEps < 1e-3f) frEps = 1e-3f;
if (flowScale < 0.05f) flowScale = 0.05f; if (flowScale > 4.0f) flowScale = 4.0f;
if (beta < 0.0f) beta = 0.0f; if (lambda < 0.0f) lambda = 0.0f;
// capture knobs
if (captureW < 16) captureW = 16; if (captureW > 7680) captureW = 7680;
if (captureH < 16) captureH = 16; if (captureH > 4320) captureH = 4320;
if (capMode < 0) capMode = 0; if (capMode > 1) capMode = 1;
if (capPatches < 1) capPatches = 1; if (capPatches > 16) capPatches = 16;
if (capPatchSize < 16) capPatchSize = 16; if (capPatchSize > 1024) capPatchSize = 1024;
if (capMotion < 0.0f) capMotion = 0.0f;
if (capShardMB < 16) capShardMB = 16; if (capShardMB > 65536) capShardMB = 65536;
}
};
static inline float envf(const char* k, float d) {
const char* v = std::getenv(k); return v ? std::strtof(v, nullptr) : d;
}
static inline int envi(const char* k, int d) {
const char* v = std::getenv(k); return v ? std::atoi(v) : d;
}
// "auto"/"on"/"off" (also true/false/on/off numerics) -> {0 auto, 1 on, 2 off}.
static inline int parse_tristate(const std::string& v, int dflt) {
if (v.empty()) return dflt;
if (v == "auto") return 0;
if (v == "on" || v == "1" || v == "force" || v == "true" || v == "yes") return 1;
if (v == "off" || v == "0" || v == "false" || v == "no") return 2;
return dflt;
}
// "on"/"off"/"1"/"true"/... -> bool.
static inline bool parse_bool(const std::string& v, bool dflt) {
int t = parse_tristate(v, dflt ? 1 : 2);
return t == 1;
}
// "patch"|"frame" -> {0 patch, 1 frame}.
static inline int parse_capmode(const std::string& v, int dflt) {
if (v == "patch" || v == "patches" || v == "0") return 0;
if (v == "frame" || v == "frames" || v == "1") return 1;
return dflt;
}
// key=value TOML-lite reader (flat keys, '#' comments) - enough for our knobs.
static inline void apply_toml(Config& c, const std::string& path) {
std::ifstream f(path);
if (!f) return;
std::string line;
while (std::getline(f, line)) {
auto h = line.find('#'); if (h != std::string::npos) line = line.substr(0, h);
auto eq = line.find('='); if (eq == std::string::npos) continue;
auto trim = [](std::string s){
size_t a = s.find_first_not_of(" \t\r\n"); size_t b = s.find_last_not_of(" \t\r\n");
return (a == std::string::npos) ? std::string() : s.substr(a, b - a + 1); };
std::string k = trim(line.substr(0, eq)), v = trim(line.substr(eq + 1));
if (k.empty() || v.empty()) continue;
if (k == "enabled") c.enabled = (v == "1" || v == "true");
else if (k == "debug") c.debug = parse_bool(v, c.debug);
else if (k == "pacing") c.pacing = parse_bool(v, c.pacing);
else if (k == "model") c.model = std::atoi(v.c_str());
else if (k == "multiplier") c.multiplier = std::atoi(v.c_str());
else if (k == "extra_images") c.extraImages = std::atoi(v.c_str());
else if (k == "perf_preset") c.perfPreset = std::atoi(v.c_str());
else if (k == "global_motion") c.gmMode = parse_tristate(v, c.gmMode);
else if (k == "flow_reg") c.frMode = parse_tristate(v, c.frMode);
else if (k == "fr_iters") c.frIters = std::atoi(v.c_str());
else if (k == "fr_lambda") c.frLambda = std::strtof(v.c_str(), nullptr);
else if (k == "fr_dt") c.frDt = std::strtof(v.c_str(), nullptr);
else if (k == "fr_edge") c.frEdge = std::strtof(v.c_str(), nullptr);
else if (k == "fr_eps") c.frEps = std::strtof(v.c_str(), nullptr);
else if (k == "flowScale") c.flowScale = std::strtof(v.c_str(), nullptr);
else if (k == "beta") c.beta = std::strtof(v.c_str(), nullptr);
else if (k == "lambda") c.lambda = std::strtof(v.c_str(), nullptr);
else if (k == "epsilon") c.epsilon = std::strtof(v.c_str(), nullptr);
else if (k == "photoScale") c.photoScale = std::strtof(v.c_str(), nullptr);
// ── capture-mode knobs ────────────────────────────────────────────────
else if (k == "capture") c.capture = parse_bool(v, c.capture);
else if (k == "capture_dir") c.capDir = v;
else if (k == "capture_width") c.captureW = std::atoi(v.c_str());
else if (k == "capture_height") c.captureH = std::atoi(v.c_str());
else if (k == "capture_mode") c.capMode = parse_capmode(v, c.capMode);
else if (k == "capture_patches") c.capPatches = std::atoi(v.c_str());
else if (k == "capture_patch_size") c.capPatchSize = std::atoi(v.c_str());
else if (k == "capture_motion") c.capMotion = std::strtof(v.c_str(), nullptr);
else if (k == "capture_shard_mb") c.capShardMB = std::atoi(v.c_str());
else if (k == "hudRect") {
// "x0,y0,x1,y1" in pixel coords
float r[4] = {0,0,0,0}; int n = 0;
size_t start = 0; std::string s = v;
for (; n < 4; ++n) {
size_t comma = s.find(',', start);
std::string tok = s.substr(start, comma == std::string::npos ? std::string::npos : comma - start);
r[n] = std::strtof(tok.c_str(), nullptr);
if (comma == std::string::npos) break;
start = comma + 1;
}
c.hudX0 = r[0]; c.hudY0 = r[1]; c.hudX1 = r[2]; c.hudY1 = r[3];
}
}
}
// Path of the conf.toml the app writes (used for hot-reload mtime checks).
static inline std::string conf_path() {
if (const char* p = std::getenv("WIN_FG_CONF")) return p;
const char* home = std::getenv("HOME");
return home ? std::string(home) + "/.config/win-fg/conf.toml" : std::string();
}
// Resolve the capture output root: explicit override, else $HOME/.cache/winfg-capture,
// else a /tmp fallback so a HOME-less prefix still writes somewhere pullable.
static inline std::string capture_root(const Config& c) {
if (!c.capDir.empty()) return c.capDir;
if (const char* home = std::getenv("HOME")) return std::string(home) + "/.cache/winfg-capture";
return std::string("/tmp/winfg-capture");
}
// env defaults first, then conf.toml overrides (file wins when present).
static inline Config load_config() {
Config c;
c.enabled = envi("WIN_FG_ENABLE", 0) != 0;
if (const char* d = std::getenv("WIN_FG_DEBUG")) c.debug = parse_bool(d, c.debug);
if (const char* pc = std::getenv("WIN_FG_PACING")) c.pacing = parse_bool(pc, c.pacing);
c.model = envi("WIN_FG_MODEL", c.model);
c.multiplier = envi("WIN_FG_MULT", c.multiplier);
c.extraImages = envi("WIN_FG_EXTRA_IMAGES", c.extraImages);
c.perfPreset = envi("WIN_FG_PERF_PRESET", c.perfPreset);
if (const char* g = std::getenv("WIN_FG_GM")) c.gmMode = parse_tristate(g, c.gmMode);
if (const char* r = std::getenv("WIN_FG_FLOWREG")) c.frMode = parse_tristate(r, c.frMode);
c.frIters = envi("WIN_FG_FR_ITERS", c.frIters);
c.frLambda = envf("WIN_FG_FR_LAMBDA", c.frLambda);
c.frDt = envf("WIN_FG_FR_DT", c.frDt);
c.frEdge = envf("WIN_FG_FR_EDGE", c.frEdge);
c.frEps = envf("WIN_FG_FR_EPS", c.frEps);
c.flowScale = envf("WIN_FG_FLOWSCALE", c.flowScale);
c.beta = envf("WIN_FG_BETA", c.beta);
c.lambda = envf("WIN_FG_LAMBDA", c.lambda);
c.epsilon = envf("WIN_FG_EPSILON", c.epsilon);
c.photoScale = envf("WIN_FG_PHOTOSCALE", c.photoScale);
// ── capture-mode env (conf.toml still wins below) ─────────────────────────
if (const char* cc = std::getenv("WIN_FG_CAPTURE")) c.capture = parse_bool(cc, c.capture);
c.captureW = envi("WIN_FG_CAPTURE_W", c.captureW);
c.captureH = envi("WIN_FG_CAPTURE_H", c.captureH);
if (const char* cm = std::getenv("WIN_FG_CAPTURE_MODE")) c.capMode = parse_capmode(cm, c.capMode);
c.capPatches = envi("WIN_FG_CAPTURE_PATCHES", c.capPatches);
c.capPatchSize = envi("WIN_FG_CAPTURE_PATCH", c.capPatchSize);
c.capMotion = envf("WIN_FG_CAPTURE_MOTION", c.capMotion);
c.capShardMB = envi("WIN_FG_CAPTURE_SHARD_MB", c.capShardMB);
if (const char* cd = std::getenv("WIN_FG_CAPTURE_DIR")) c.capDir = cd;
if (const char* h = std::getenv("WIN_FG_HUD_RECT")) {
// "x0,y0,x1,y1" - pixel coords; disabled when x0>=x1 or y0>=y1
float r[4] = {0,0,0,0}; int n = 0;
std::string s = h; size_t start = 0;
for (; n < 4; ++n) {
size_t comma = s.find(',', start);
r[n] = std::strtof(s.substr(start, comma == std::string::npos ? std::string::npos : comma - start).c_str(), nullptr);
if (comma == std::string::npos) break;
start = comma + 1;
}
c.hudX0 = r[0]; c.hudY0 = r[1]; c.hudX1 = r[2]; c.hudY1 = r[3];
}
const char* home = std::getenv("HOME");
if (home) apply_toml(c, std::string(home) + "/.config/win-fg/conf.toml");
if (const char* p = std::getenv("WIN_FG_CONF")) apply_toml(c, p);
c.sanitize();
return c;
}
// UBO layout - MUST match wfg_synth.comp binding 0.
// The vec4 hudRect is 16-byte aligned in std140, sits at offset 32 (after the
// 8 leading floats which naturally occupy 32B). Disabled when hudX0>=hudX1.
struct SynthUBO {
float flowScale, alpha, beta, lambda, epsilon, photoScale, pad0, pad1;
float hudX0, hudY0, hudX1, hudY1;
};
// UBO layout - MUST match of3_flow / of3_expand_m4 binding 0 (Model3UBO).
struct FlowUBO { float flowScale; uint32_t level; float occlLo; float occlHi; };
// C1 global-motion UBO - MUST match binding 1 in of3_gm_reduce / of3_gm_prewarp /
// of3_expand(_m4). std140: four vec4 = 64 bytes. The affine is in centered-UV
// space: P = [[p1,p3],[p2,p4]], translation (tx,ty); warp uvSrc = uv + P*(uv-0.5)
// + t maps a prev(template) coord to the curr(image) coord it samples (prev→curr).
// lin[] = running LK estimate (linearization point fed to of3_gm_reduce)
// app[] = APPLIED affine (of3_gm_prewarp + expand add-back); identity when
// disengaged so the pipeline is byte-identical to pre-C1.
struct GMUBO {
float lin[4]; // p1,p2,p3,p4
float linT[4]; // tx,ty, engaged, pad
float app[4]; // p1,p2,p3,p4
float appT[4]; // tx,ty, pad, pad
};
// C2 flow-regularization UBO - MUST match binding 0 (vec4 params) in
// of3_flowreg.comp. std140: one vec4 = 16 bytes. See Config::fr* fields.
struct FlowRegUBO {
float dt; // params.x - semi-implicit smoothing step
float lambda; // params.y - L1 data-fidelity weight (θ = dt·lambda)
float edgeAlpha; // params.z - luma-gradient edge sensitivity for g
float epsTV; // params.w - Charbonnier epsilon (px) for the TV diffusivity
};
} // namespace winfg
@@ -0,0 +1,44 @@
// GENERATED by tools/embed_spv.py - do not edit by hand.
// win-fg embedded SPIR-V: our optical-flow front end + synthesis only.
#pragma once
#include <cstdint>
#include <cstddef>
namespace winfg { namespace embedded {
static const uint32_t OF3_LUMA_SPV[] = {0x07230203u,0x00010300u,0x0008000bu,0x00000054u,0x00000000u,0x00020011u,0x00000001u,0x00020011u,0x00000032u,0x0006000bu,0x00000001u,0x4c534c47u,0x6474732eu,0x3035342eu,0x00000000u,0x0003000eu,0x00000000u,0x00000001u,0x0006000fu,0x00000005u,0x00000004u,0x6e69616du,0x00000000u,0x0000000du,0x00060010u,0x00000004u,0x00000011u,0x00000010u,0x00000010u,0x00000001u,0x00030003u,0x00000002u,0x000001c2u,0x00040005u,0x00000004u,0x6e69616du,0x00000000u,0x00030005u,0x00000009u,0x00646967u,0x00080005u,0x0000000du,0x475f6c67u,0x61626f6cu,0x766e496cu,0x7461636fu,0x496e6f69u,0x00000044u,0x00030005u,0x00000012u,0x006d6964u,0x00040005u,0x00000016u,0x74734475u,0x00000000u,0x00030005u,0x00000030u,0x00007675u,0x00030005u,0x0000003bu,0x00626772u,0x00040005u,0x0000003fu,0x63725375u,0x00000000u,0x00030005u,0x00000047u,0x00005966u,0x00040047u,0x0000000du,0x0000000bu,0x0000001cu,0x00030047u,0x00000016u,0x00000019u,0x00040047u,0x00000016u,0x00000021u,0x00000030u,0x00040047u,0x00000016u,0x00000022u,0x00000000u,0x00040047u,0x0000003fu,0x00000021u,0x00000020u,0x00040047u,0x0000003fu,0x00000022u,0x00000000u,0x00040047u,0x00000053u,0x0000000bu,0x00000019u,0x00020013u,0x00000002u,0x00030021u,0x00000003u,0x00000002u,0x00040015u,0x00000006u,0x00000020u,0x00000001u,0x00040017u,0x00000007u,0x00000006u,0x00000002u,0x00040020u,0x00000008u,0x00000007u,0x00000007u,0x00040015u,0x0000000au,0x00000020u,0x00000000u,0x00040017u,0x0000000bu,0x0000000au,0x00000003u,0x00040020u,0x0000000cu,0x00000001u,0x0000000bu,0x0004003bu,0x0000000cu,0x0000000du,0x00000001u,0x00040017u,0x0000000eu,0x0000000au,0x00000002u,0x00030016u,0x00000013u,0x00000020u,0x00090019u,0x00000014u,0x00000013u,0x00000001u,0x00000000u,0x00000000u,0x00000000u,0x00000002u,0x00000003u,0x00040020u,0x00000015u,0x00000000u,0x00000014u,0x0004003bu,0x00000015u,0x00000016u,0x00000000u,0x00020014u,0x00000019u,0x0004002bu,0x0000000au,0x0000001au,0x00000000u,0x00040020u,0x0000001bu,0x00000007u,0x00000006u,0x0004002bu,0x0000000au,0x00000024u,0x00000001u,0x00040017u,0x0000002eu,0x00000013u,0x00000002u,0x00040020u,0x0000002fu,0x00000007u,0x0000002eu,0x0004002bu,0x00000013u,0x00000033u,0x3f000000u,0x00040017u,0x00000039u,0x00000013u,0x00000003u,0x00040020u,0x0000003au,0x00000007u,0x00000039u,0x00090019u,0x0000003cu,0x00000013u,0x00000001u,0x00000000u,0x00000000u,0x00000000u,0x00000001u,0x00000000u,0x0003001bu,0x0000003du,0x0000003cu,0x00040020u,0x0000003eu,0x00000000u,0x0000003du,0x0004003bu,0x0000003eu,0x0000003fu,0x00000000u,0x0004002bu,0x00000013u,0x00000042u,0x00000000u,0x00040017u,0x00000043u,0x00000013u,0x00000004u,0x00040020u,0x00000046u,0x00000007u,0x00000013u,0x0004002bu,0x00000013u,0x00000049u,0x3e59b3d0u,0x0004002bu,0x00000013u,0x0000004au,0x3f371759u,0x0004002bu,0x00000013u,0x0000004bu,0x3d93dd98u,0x0006002cu,0x00000039u,0x0000004cu,0x00000049u,0x0000004au,0x0000004bu,0x0004002bu,0x0000000au,0x00000052u,0x00000010u,0x0006002cu,0x0000000bu,0x00000053u,0x00000052u,0x00000052u,0x00000024u,0x00050036u,0x00000002u,0x00000004u,0x00000000u,0x00000003u,0x000200f8u,0x00000005u,0x0004003bu,0x00000008u,0x00000009u,0x00000007u,0x0004003bu,0x00000008u,0x00000012u,0x00000007u,0x0004003bu,0x0000002fu,0x00000030u,0x00000007u,0x0004003bu,0x0000003au,0x0000003bu,0x00000007u,0x0004003bu,0x00000046u,0x00000047u,0x00000007u,0x0004003du,0x0000000bu,0x0000000fu,0x0000000du,0x0007004fu,0x0000000eu,0x00000010u,0x0000000fu,0x0000000fu,0x00000000u,0x00000001u,0x0004007cu,0x00000007u,0x00000011u,0x00000010u,0x0003003eu,0x00000009u,0x00000011u,0x0004003du,0x00000014u,0x00000017u,0x00000016u,0x00040068u,0x00000007u,0x00000018u,0x00000017u,0x0003003eu,0x00000012u,0x00000018u,0x00050041u,0x0000001bu,0x0000001cu,0x00000009u,0x0000001au,0x0004003du,0x00000006u,0x0000001du,0x0000001cu,0x00050041u,0x0000001bu,0x0000001eu,0x00000012u,0x0000001au,0x0004003du,0x00000006u,0x0000001fu,0x0000001eu,0x000500afu,0x00000019u,0x00000020u,0x0000001du,0x0000001fu,0x000400a8u,0x00000019u,0x00000021u,0x00000020u,0x000300f7u,0x00000023u,0x00000000u,0x000400fau,0x00000021u,0x00000022u,0x00000023u,0x000200f8u,0x00000022u,0x00050041u,0x0000001bu,0x00000025u,0x00000009u,0x00000024u,0x0004003du,0x00000006u,0x00000026u,0x00000025u,0x00050041u,0x0000001bu,0x00000027u,0x00000012u,0x00000024u,0x0004003du,0x00000006u,0x00000028u,0x00000027u,0x000500afu,0x00000019u,0x00000029u,0x00000026u,0x00000028u,0x000200f9u,0x00000023u,0x000200f8u,0x00000023u,0x000700f5u,0x00000019u,0x0000002au,0x00000020u,0x00000005u,0x00000029u,0x00000022u,0x000300f7u,0x0000002cu,0x00000000u,0x000400fau,0x0000002au,0x0000002bu,0x0000002cu,0x000200f8u,0x0000002bu,0x000100fdu,0x000200f8u,0x0000002cu,0x0004003du,0x00000007u,0x00000031u,0x00000009u,0x0004006fu,0x0000002eu,0x00000032u,0x00000031u,0x00050050u,0x0000002eu,0x00000034u,0x00000033u,0x00000033u,0x00050081u,0x0000002eu,0x00000035u,0x00000032u,0x00000034u,0x0004003du,0x00000007u,0x00000036u,0x00000012u,0x0004006fu,0x0000002eu,0x00000037u,0x00000036u,0x00050088u,0x0000002eu,0x00000038u,0x0Line truncated
static const size_t OF3_LUMA_LEN = 481;
static const uint32_t OF3_DOWNSAMPLE_SPV[] = {0x07230203u,0x00010300u,0x0008000bu,0x0000008au,0x00000000u,0x00020011u,0x00000001u,0x00020011u,0x00000032u,0x0006000bu,0x00000001u,0x4c534c47u,0x6474732eu,0x3035342eu,0x00000000u,0x0003000eu,0x00000000u,0x00000001u,0x0006000fu,0x00000005u,0x00000004u,0x6e69616du,0x00000000u,0x0000000du,0x00060010u,0x00000004u,0x00000011u,0x00000010u,0x00000010u,0x00000001u,0x00030003u,0x00000002u,0x000001c2u,0x00040005u,0x00000004u,0x6e69616du,0x00000000u,0x00030005u,0x00000009u,0x00646967u,0x00080005u,0x0000000du,0x475f6c67u,0x61626f6cu,0x766e496cu,0x7461636fu,0x496e6f69u,0x00000044u,0x00030005u,0x00000012u,0x006d6964u,0x00040005u,0x00000016u,0x74734475u,0x00000000u,0x00040005u,0x00000030u,0x44637273u,0x00006d69u,0x00040005u,0x00000034u,0x63725375u,0x00000000u,0x00040005u,0x0000003au,0x65736162u,0x00000000u,0x00030005u,0x00000044u,0x00000068u,0x00030005u,0x0000004au,0x00000061u,0x00030005u,0x00000059u,0x00000062u,0x00030005u,0x00000065u,0x00000063u,0x00030005u,0x00000071u,0x00000064u,0x00040047u,0x0000000du,0x0000000bu,0x0000001cu,0x00030047u,0x00000016u,0x00000019u,0x00040047u,0x00000016u,0x00000021u,0x00000030u,0x00040047u,0x00000016u,0x00000022u,0x00000000u,0x00040047u,0x00000034u,0x00000021u,0x00000020u,0x00040047u,0x00000034u,0x00000022u,0x00000000u,0x00040047u,0x00000089u,0x0000000bu,0x00000019u,0x00020013u,0x00000002u,0x00030021u,0x00000003u,0x00000002u,0x00040015u,0x00000006u,0x00000020u,0x00000001u,0x00040017u,0x00000007u,0x00000006u,0x00000002u,0x00040020u,0x00000008u,0x00000007u,0x00000007u,0x00040015u,0x0000000au,0x00000020u,0x00000000u,0x00040017u,0x0000000bu,0x0000000au,0x00000003u,0x00040020u,0x0000000cu,0x00000001u,0x0000000bu,0x0004003bu,0x0000000cu,0x0000000du,0x00000001u,0x00040017u,0x0000000eu,0x0000000au,0x00000002u,0x00030016u,0x00000013u,0x00000020u,0x00090019u,0x00000014u,0x00000013u,0x00000001u,0x00000000u,0x00000000u,0x00000000u,0x00000002u,0x00000003u,0x00040020u,0x00000015u,0x00000000u,0x00000014u,0x0004003bu,0x00000015u,0x00000016u,0x00000000u,0x00020014u,0x00000019u,0x0004002bu,0x0000000au,0x0000001au,0x00000000u,0x00040020u,0x0000001bu,0x00000007u,0x00000006u,0x0004002bu,0x0000000au,0x00000024u,0x00000001u,0x00040017u,0x0000002eu,0x00000013u,0x00000002u,0x00040020u,0x0000002fu,0x00000007u,0x0000002eu,0x00090019u,0x00000031u,0x00000013u,0x00000001u,0x00000000u,0x00000000u,0x00000000u,0x00000001u,0x00000000u,0x0003001bu,0x00000032u,0x00000031u,0x00040020u,0x00000033u,0x00000000u,0x00000032u,0x0004003bu,0x00000033u,0x00000034u,0x00000000u,0x0004002bu,0x00000006u,0x00000036u,0x00000000u,0x0004002bu,0x00000013u,0x0000003du,0x40000000u,0x0004002bu,0x00000013u,0x0000003fu,0x3f800000u,0x0004002bu,0x00000013u,0x00000045u,0x3f000000u,0x00040020u,0x00000049u,0x00000007u,0x00000013u,0x0004002bu,0x00000013u,0x00000055u,0x00000000u,0x00040017u,0x00000056u,0x00000013u,0x00000004u,0x0004002bu,0x00000013u,0x00000085u,0x3e800000u,0x0004002bu,0x0000000au,0x00000088u,0x00000010u,0x0006002cu,0x0000000bu,0x00000089u,0x00000088u,0x00000088u,0x00000024u,0x00050036u,0x00000002u,0x00000004u,0x00000000u,0x00000003u,0x000200f8u,0x00000005u,0x0004003bu,0x00000008u,0x00000009u,0x00000007u,0x0004003bu,0x00000008u,0x00000012u,0x00000007u,0x0004003bu,0x0000002fu,0x00000030u,0x00000007u,0x0004003bu,0x0000002fu,0x0000003au,0x00000007u,0x0004003bu,0x0000002fu,0x00000044u,0x00000007u,0x0004003bu,0x00000049u,0x0000004au,0x00000007u,0x0004003bu,0x00000049u,0x00000059u,0x00000007u,0x0004003bu,0x00000049u,0x00000065u,0x00000007u,0x0004003bu,0x00000049u,0x00000071u,0x00000007u,0x0004003du,0x0000000bu,0x0000000fu,0x0000000du,0x0007004fu,0x0000000eu,0x00000010u,0x0000000fu,0x0000000fu,0x00000000u,0x00000001u,0x0004007cu,0x00000007u,0x00000011u,0x00000010u,0x0003003eu,0x00000009u,0x00000011u,0x0004003du,0x00000014u,0x00000017u,0x00000016u,0x00040068u,0x00000007u,0x00000018u,0x00000017u,0x0003003eu,0x00000012u,0x00000018u,0x00050041u,0x0000001bu,0x0000001cu,0x00000009u,0x0000001au,0x0004003du,0x00000006u,0x0000001du,0x0000001cu,0x00050041u,0x0000001bu,0x0000001eu,0x00000012u,0x0000001au,0x0004003du,0x00000006u,0x0000001fu,0x0000001eu,0x000500afu,0x00000019u,0x00000020u,0x0000001du,0x0000001fu,0x000400a8u,0x00000019u,0x00000021u,0x00000020u,0x000300f7u,0x00000023u,0x00000000u,0x000400fau,0x00000021u,0x00000022u,0x00000023u,0x000200f8u,0x00000022u,0x00050041u,0x0000001bu,0x00000025u,0x00000009u,0x00000024u,0x0004003du,0x00000006u,0x00000026u,0x00000025u,0x00050041u,0x0000001bu,0x00000027u,0x00000012u,0x00000024u,0x0004003du,0x00000006u,0x00000028u,0x00000027u,0x000500afu,0x00000019u,0x00000029u,0x00000026u,0x00000028u,0x000200f9u,0x00000023u,0x000200f8u,0x00000023u,0x000700f5u,0x00000019u,0x0000002au,0x00000020u,0x00000005u,0x00000029u,0x00000022u,0x000300f7u,0x0000002cu,0x00000000u,0x000400fau,0x0000002au,0x0000002bu,0x0000002cu,0x000200f8u,0x0000002bu,0x000100fdu,0x000200f8u,0x0000002cu,0x0004003du,0x00000032u,0x00000035u,0x00000034u,0x00040064u,0x00000031u,0x00000037u,0x00000035u,0x0005006Line truncated
static const size_t OF3_DOWNSAMPLE_LEN = 753;
static const uint32_t OF3_FLOW_SPV[] = {0x07230203u,0x00010300u,0x0008000bu,0x00000185u,0x00000000u,0x00020011u,0x00000001u,0x00020011u,0x00000032u,0x0006000bu,0x00000001u,0x4c534c47u,0x6474732eu,0x3035342eu,0x00000000u,0x0003000eu,0x00000000u,0x00000001u,0x0006000fu,0x00000005u,0x00000004u,0x6e69616du,0x00000000u,0x0000000du,0x00060010u,0x00000004u,0x00000011u,0x00000010u,0x00000010u,0x00000001u,0x00030003u,0x00000002u,0x000001c2u,0x00040005u,0x00000004u,0x6e69616du,0x00000000u,0x00030005u,0x00000009u,0x00646967u,0x00080005u,0x0000000du,0x475f6c67u,0x61626f6cu,0x766e496cu,0x7461636fu,0x496e6f69u,0x00000044u,0x00030005u,0x00000012u,0x006d6964u,0x00040005u,0x00000016u,0x6f6c4675u,0x00000077u,0x00040005u,0x00000030u,0x44766e69u,0x00006d69u,0x00030005u,0x00000036u,0x00007675u,0x00040005u,0x0000003fu,0x654c6f74u,0x006c6576u,0x00030005u,0x00000040u,0x00000055u,0x00060006u,0x00000040u,0x00000000u,0x776f6c66u,0x6c616353u,0x00000065u,0x00050006u,0x00000040u,0x00000001u,0x6576656cu,0x0000006cu,0x00050006u,0x00000040u,0x00000002u,0x30646170u,0x00000000u,0x00050006u,0x00000040u,0x00000003u,0x31646170u,0x00000000u,0x00030005u,0x00000042u,0x00000000u,0x00050005u,0x0000004au,0x64657270u,0x6c6c7546u,0x00000000u,0x00050005u,0x0000004eu,0x65725075u,0x74636964u,0x0000726fu,0x00050005u,0x00000055u,0x64657270u,0x6576654cu,0x0000006cu,0x00040005u,0x00000059u,0x74736562u,0x00646153u,0x00050005u,0x0000005bu,0x74736562u,0x70736944u,0x00000000u,0x00030005u,0x0000005du,0x00007973u,0x00030005u,0x00000067u,0x00007873u,0x00040005u,0x0000006fu,0x646e6163u,0x00000000u,0x00030005u,0x00000077u,0x00646173u,0x00030005u,0x00000078u,0x00007962u,0x00030005u,0x00000081u,0x00007862u,0x00030005u,0x00000089u,0x00006f62u,0x00030005u,0x0000008fu,0x00000070u,0x00050005u,0x00000090u,0x6d754c75u,0x65725061u,0x00000076u,0x00030005u,0x00000099u,0x00000063u,0x00050005u,0x0000009au,0x6d754c75u,0x72754361u,0x00000072u,0x00040005u,0x000000cau,0x43646173u,0x00000000u,0x00040005u,0x000000ccu,0x58646173u,0x0000006du,0x00040005u,0x000000cdu,0x58646173u,0x00000070u,0x00040005u,0x000000ceu,0x59646173u,0x0000006du,0x00040005u,0x000000cfu,0x59646173u,0x00000070u,0x00030005u,0x000000d0u,0x00007962u,0x00030005u,0x000000d8u,0x00007862u,0x00030005u,0x000000e0u,0x00006f62u,0x00030005u,0x000000e6u,0x00000070u,0x00040005u,0x000000efu,0x646e6163u,0x00006d58u,0x00040005u,0x000000f4u,0x646e6163u,0x00007058u,0x00040005u,0x000000f8u,0x646e6163u,0x00006d59u,0x00040005u,0x000000fcu,0x646e6163u,0x00007059u,0x00030005u,0x00000148u,0x00007864u,0x00030005u,0x00000149u,0x00007964u,0x00040005u,0x0000014au,0x6f6e6564u,0x0000586du,0x00040005u,0x00000152u,0x6f6e6564u,0x0000596du,0x00050005u,0x00000176u,0x776f6c66u,0x6c6c7546u,0x00000000u,0x00040047u,0x0000000du,0x0000000bu,0x0000001cu,0x00030047u,0x00000016u,0x00000019u,0x00040047u,0x00000016u,0x00000021u,0x00000030u,0x00040047u,0x00000016u,0x00000022u,0x00000000u,0x00030047u,0x00000040u,0x00000002u,0x00050048u,0x00000040u,0x00000000u,0x00000023u,0x00000000u,0x00050048u,0x00000040u,0x00000001u,0x00000023u,0x00000004u,0x00050048u,0x00000040u,0x00000002u,0x00000023u,0x00000008u,0x00050048u,0x00000040u,0x00000003u,0x00000023u,0x0000000cu,0x00040047u,0x00000042u,0x00000021u,0x00000000u,0x00040047u,0x00000042u,0x00000022u,0x00000000u,0x00040047u,0x0000004eu,0x00000021u,0x00000022u,0x00040047u,0x0000004eu,0x00000022u,0x00000000u,0x00040047u,0x00000090u,0x00000021u,0x00000020u,0x00040047u,0x00000090u,0x00000022u,0x00000000u,0x00040047u,0x0000009au,0x00000021u,0x00000021u,0x00040047u,0x0000009au,0x00000022u,0x00000000u,0x00040047u,0x00000184u,0x0000000bu,0x00000019u,0x00020013u,0x00000002u,0x00030021u,0x00000003u,0x00000002u,0x00040015u,0x00000006u,0x00000020u,0x00000001u,0x00040017u,0x00000007u,0x00000006u,0x00000002u,0x00040020u,0x00000008u,0x00000007u,0x00000007u,0x00040015u,0x0000000au,0x00000020u,0x00000000u,0x00040017u,0x0000000bu,0x0000000au,0x00000003u,0x00040020u,0x0000000cu,0x00000001u,0x0000000bu,0x0004003bu,0x0000000cu,0x0000000du,0x00000001u,0x00040017u,0x0000000eu,0x0000000au,0x00000002u,0x00030016u,0x00000013u,0x00000020u,0x00090019u,0x00000014u,0x00000013u,0x00000001u,0x00000000u,0x00000000u,0x00000000u,0x00000002u,0x00000002u,0x00040020u,0x00000015u,0x00000000u,0x00000014u,0x0004003bu,0x00000015u,0x00000016u,0x00000000u,0x00020014u,0x00000019u,0x0004002bu,0x0000000au,0x0000001au,0x00000000u,0x00040020u,0x0000001bu,0x00000007u,0x00000006u,0x0004002bu,0x0000000au,0x00000024u,0x00000001u,0x00040017u,0x0000002eu,0x00000013u,0x00000002u,0x00040020u,0x0000002fu,0x00000007u,0x0000002eu,0x0004002bu,0x00000013u,0x00000031u,0x3f800000u,0x0004002bu,0x00000013u,0x00000039u,0x3f000000u,0x00040020u,0x0000003eu,0x00000007u,0x00000013u,0x0006001eu,0x00000040u,0x00000013u,0x0000000au,0x0000000au,0x0000000au,0x00040020u,0x00000041u,0x00000002u,0x00000040u,0x0004003bu,0x00000041u,0x00000042u,0x00000002u,0x0004002bu,0x00000006u,0x00000043u,0x00000001u,0x00040020u,0x00000044u,0x00000002u,0x0000000au,0x00090019u,0x0000004bu,0x00000013u,0x00000001u,0x0Line truncated
static const size_t OF3_FLOW_LEN = 2253;
static const uint32_t OF3_EXPAND_SPV[] = {0x07230203u,0x00010300u,0x0008000bu,0x00000124u,0x00000000u,0x00020011u,0x00000001u,0x00020011u,0x00000032u,0x0006000bu,0x00000001u,0x4c534c47u,0x6474732eu,0x3035342eu,0x00000000u,0x0003000eu,0x00000000u,0x00000001u,0x0006000fu,0x00000005u,0x00000004u,0x6e69616du,0x00000000u,0x00000049u,0x00060010u,0x00000004u,0x00000011u,0x00000010u,0x00000010u,0x00000001u,0x00030003u,0x00000002u,0x000001c2u,0x00040005u,0x00000004u,0x6e69616du,0x00000000u,0x00080005u,0x0000000eu,0x3564656du,0x3b316628u,0x663b3166u,0x31663b31u,0x3b31663bu,0x00000000u,0x00030005u,0x00000009u,0x00000061u,0x00030005u,0x0000000au,0x00000062u,0x00030005u,0x0000000bu,0x00000063u,0x00030005u,0x0000000cu,0x00000064u,0x00030005u,0x0000000du,0x00000065u,0x00030005u,0x00000010u,0x00000078u,0x00030005u,0x00000034u,0x00000079u,0x00030005u,0x00000045u,0x00646967u,0x00080005u,0x00000049u,0x475f6c67u,0x61626f6cu,0x766e496cu,0x7461636fu,0x496e6f69u,0x00000044u,0x00030005u,0x0000004eu,0x006d6964u,0x00040005u,0x00000051u,0x64774675u,0x00000000u,0x00030005u,0x0000006bu,0x00007675u,0x00030005u,0x00000074u,0x00007366u,0x00050005u,0x00000079u,0x6f6c4675u,0x6e694677u,0x00000065u,0x00030005u,0x00000081u,0x00000063u,0x00030005u,0x00000088u,0x00006d78u,0x00030005u,0x00000091u,0x00007078u,0x00030005u,0x0000009au,0x00006d79u,0x00030005u,0x000000a3u,0x00007079u,0x00030005u,0x000000acu,0x00000064u,0x00040005u,0x000000adu,0x61726170u,0x0000006du,0x00040005u,0x000000b0u,0x61726170u,0x0000006du,0x00040005u,0x000000b3u,0x61726170u,0x0000006du,0x00040005u,0x000000b6u,0x61726170u,0x0000006du,0x00040005u,0x000000b9u,0x61726170u,0x0000006du,0x00040005u,0x000000bdu,0x61726170u,0x0000006du,0x00040005u,0x000000c0u,0x61726170u,0x0000006du,0x00040005u,0x000000c3u,0x61726170u,0x0000006du,0x00040005u,0x000000c6u,0x61726170u,0x0000006du,0x00040005u,0x000000c9u,0x61726170u,0x0000006du,0x00030005u,0x000000ceu,0x00000071u,0x00030005u,0x000000d2u,0x00787067u,0x00030005u,0x000000d3u,0x00004d47u,0x00050006u,0x000000d3u,0x00000000u,0x694c6d67u,0x0000006eu,0x00050006u,0x000000d3u,0x00000001u,0x694c6d67u,0x0000546eu,0x00050006u,0x000000d3u,0x00000002u,0x70416d67u,0x00000070u,0x00050006u,0x000000d3u,0x00000003u,0x70416d67u,0x00005470u,0x00030005u,0x000000d5u,0x00000000u,0x00030005u,0x000000fdu,0x00003241u,0x00040005u,0x0000010bu,0x6c6c7566u,0x00000000u,0x00040005u,0x00000117u,0x64774275u,0x00000000u,0x00030005u,0x00000121u,0x00000055u,0x00060006u,0x00000121u,0x00000000u,0x776f6c66u,0x6c616353u,0x00000065u,0x00050006u,0x00000121u,0x00000001u,0x6576656cu,0x0000006cu,0x00050006u,0x00000121u,0x00000002u,0x30646170u,0x00000000u,0x00050006u,0x00000121u,0x00000003u,0x31646170u,0x00000000u,0x00030005u,0x00000123u,0x00000000u,0x00040047u,0x00000049u,0x0000000bu,0x0000001cu,0x00030047u,0x00000051u,0x00000019u,0x00040047u,0x00000051u,0x00000021u,0x00000030u,0x00040047u,0x00000051u,0x00000022u,0x00000000u,0x00040047u,0x00000079u,0x00000021u,0x00000020u,0x00040047u,0x00000079u,0x00000022u,0x00000000u,0x00030047u,0x000000d3u,0x00000002u,0x00050048u,0x000000d3u,0x00000000u,0x00000023u,0x00000000u,0x00050048u,0x000000d3u,0x00000001u,0x00000023u,0x00000010u,0x00050048u,0x000000d3u,0x00000002u,0x00000023u,0x00000020u,0x00050048u,0x000000d3u,0x00000003u,0x00000023u,0x00000030u,0x00040047u,0x000000d5u,0x00000021u,0x00000001u,0x00040047u,0x000000d5u,0x00000022u,0x00000000u,0x00030047u,0x00000117u,0x00000019u,0x00040047u,0x00000117u,0x00000021u,0x00000031u,0x00040047u,0x00000117u,0x00000022u,0x00000000u,0x00040047u,0x00000120u,0x0000000bu,0x00000019u,0x00030047u,0x00000121u,0x00000002u,0x00050048u,0x00000121u,0x00000000u,0x00000023u,0x00000000u,0x00050048u,0x00000121u,0x00000001u,0x00000023u,0x00000004u,0x00050048u,0x00000121u,0x00000002u,0x00000023u,0x00000008u,0x00050048u,0x00000121u,0x00000003u,0x00000023u,0x0000000cu,0x00040047u,0x00000123u,0x00000021u,0x00000000u,0x00040047u,0x00000123u,0x00000022u,0x00000000u,0x00020013u,0x00000002u,0x00030021u,0x00000003u,0x00000002u,0x00030016u,0x00000006u,0x00000020u,0x00040020u,0x00000007u,0x00000007u,0x00000006u,0x00080021u,0x00000008u,0x00000006u,0x00000007u,0x00000007u,0x00000007u,0x00000007u,0x00000007u,0x00040015u,0x00000042u,0x00000020u,0x00000001u,0x00040017u,0x00000043u,0x00000042u,0x00000002u,0x00040020u,0x00000044u,0x00000007u,0x00000043u,0x00040015u,0x00000046u,0x00000020u,0x00000000u,0x00040017u,0x00000047u,0x00000046u,0x00000003u,0x00040020u,0x00000048u,0x00000001u,0x00000047u,0x0004003bu,0x00000048u,0x00000049u,0x00000001u,0x00040017u,0x0000004au,0x00000046u,0x00000002u,0x00090019u,0x0000004fu,0x00000006u,0x00000001u,0x00000000u,0x00000000u,0x00000000u,0x00000002u,0x00000002u,0x00040020u,0x00000050u,0x00000000u,0x0000004fu,0x0004003bu,0x00000050u,0x00000051u,0x00000000u,0x00020014u,0x00000054u,0x0004002bu,0x00000046u,0x00000055u,0x00000000u,0x00040020u,0x00000056u,0x00000007u,0x00000042u,0x0004002bu,0x00000046u,0x0000005fu,0x00000001u,0x00040017u,0x00000069u,0x00000006u,0x00000002u,0x00040020u,0x0000006au,0x00000007u,0Line truncated
static const size_t OF3_EXPAND_LEN = 1798;
static const uint32_t OF3_FLOW_M4_SPV[] = {0x07230203u,0x00010300u,0x0008000bu,0x00000151u,0x00000000u,0x00020011u,0x00000001u,0x00020011u,0x00000032u,0x0006000bu,0x00000001u,0x4c534c47u,0x6474732eu,0x3035342eu,0x00000000u,0x0003000eu,0x00000000u,0x00000001u,0x0006000fu,0x00000005u,0x00000004u,0x6e69616du,0x00000000u,0x000000f6u,0x00060010u,0x00000004u,0x00000011u,0x00000010u,0x00000010u,0x00000001u,0x00030003u,0x00000002u,0x000001c2u,0x00040005u,0x00000004u,0x6e69616du,0x00000000u,0x00080005u,0x00000010u,0x636f6c62u,0x6461536bu,0x31327328u,0x3132733bu,0x3266763bu,0x0000003bu,0x00030005u,0x0000000du,0x00000061u,0x00030005u,0x0000000eu,0x00000062u,0x00040005u,0x0000000fu,0x646e6163u,0x00000000u,0x00070005u,0x00000016u,0x766c6f73u,0x32732865u,0x32733b31u,0x66763b31u,0x00003b32u,0x00030005u,0x00000013u,0x00000061u,0x00030005u,0x00000014u,0x00000062u,0x00040005u,0x00000015u,0x64657270u,0x00000000u,0x00030005u,0x00000019u,0x00646173u,0x00030005u,0x0000001du,0x00007962u,0x00030005u,0x00000028u,0x00007862u,0x00030005u,0x00000030u,0x00006f62u,0x00030005u,0x00000036u,0x00006170u,0x00030005u,0x00000039u,0x00765567u,0x00050005u,0x0000003cu,0x766e4967u,0x616d754cu,0x00000000u,0x00030005u,0x00000045u,0x00006270u,0x00040005u,0x0000005du,0x74736562u,0x00646153u,0x00050005u,0x0000005fu,0x74736562u,0x70736944u,0x00000000u,0x00030005u,0x00000061u,0x00007973u,0x00030005u,0x0000006bu,0x00007873u,0x00040005u,0x00000073u,0x646e6163u,0x00000000u,0x00030005u,0x0000007bu,0x00646173u,0x00040005u,0x0000007cu,0x61726170u,0x0000006du,0x00030005u,0x00000095u,0x00000055u,0x00060006u,0x00000095u,0x00000000u,0x776f6c66u,0x6c616353u,0x00000065u,0x00050006u,0x00000095u,0x00000001u,0x6576656cu,0x0000006cu,0x00050006u,0x00000095u,0x00000002u,0x6c63636fu,0x00006f4cu,0x00050006u,0x00000095u,0x00000003u,0x6c63636fu,0x00006948u,0x00030005u,0x00000097u,0x00000000u,0x00040005u,0x0000009eu,0x43646173u,0x00000000u,0x00040005u,0x000000a1u,0x58646173u,0x0000006du,0x00040005u,0x000000a6u,0x61726170u,0x0000006du,0x00040005u,0x000000a9u,0x58646173u,0x00000070u,0x00040005u,0x000000aeu,0x61726170u,0x0000006du,0x00040005u,0x000000b1u,0x59646173u,0x0000006du,0x00040005u,0x000000b5u,0x61726170u,0x0000006du,0x00040005u,0x000000b8u,0x59646173u,0x00000070u,0x00040005u,0x000000bcu,0x61726170u,0x0000006du,0x00030005u,0x000000bfu,0x00007864u,0x00030005u,0x000000c0u,0x00007964u,0x00040005u,0x000000c1u,0x6f6e6564u,0x0000586du,0x00040005u,0x000000c9u,0x6f6e6564u,0x0000596du,0x00030005u,0x000000f3u,0x00646967u,0x00080005u,0x000000f6u,0x475f6c67u,0x61626f6cu,0x766e496cu,0x7461636fu,0x496e6f69u,0x00000044u,0x00030005u,0x000000fbu,0x006d6964u,0x00040005u,0x000000feu,0x6f6c4675u,0x00000077u,0x00050005u,0x0000011au,0x6d754c75u,0x72754361u,0x00000072u,0x00040005u,0x00000122u,0x654c6f74u,0x006c6576u,0x00040005u,0x00000129u,0x64657270u,0x00000000u,0x00050005u,0x0000012au,0x65725075u,0x74636964u,0x0000726fu,0x00030005u,0x0000012eu,0x00647766u,0x00050005u,0x0000012fu,0x6d754c75u,0x65725061u,0x00000076u,0x00040005u,0x00000134u,0x61726170u,0x0000006du,0x00030005u,0x00000136u,0x00647762u,0x00040005u,0x0000013bu,0x61726170u,0x0000006du,0x00040005u,0x0000013du,0x75466f74u,0x00006c6cu,0x00030047u,0x00000095u,0x00000002u,0x00050048u,0x00000095u,0x00000000u,0x00000023u,0x00000000u,0x00050048u,0x00000095u,0x00000001u,0x00000023u,0x00000004u,0x00050048u,0x00000095u,0x00000002u,0x00000023u,0x00000008u,0x00050048u,0x00000095u,0x00000003u,0x00000023u,0x0000000cu,0x00040047u,0x00000097u,0x00000021u,0x00000000u,0x00040047u,0x00000097u,0x00000022u,0x00000000u,0x00040047u,0x000000f6u,0x0000000bu,0x0000001cu,0x00030047u,0x000000feu,0x00000019u,0x00040047u,0x000000feu,0x00000021u,0x00000030u,0x00040047u,0x000000feu,0x00000022u,0x00000000u,0x00040047u,0x0000011au,0x00000021u,0x00000021u,0x00040047u,0x0000011au,0x00000022u,0x00000000u,0x00040047u,0x0000012au,0x00000021u,0x00000022u,0x00040047u,0x0000012au,0x00000022u,0x00000000u,0x00040047u,0x0000012fu,0x00000021u,0x00000020u,0x00040047u,0x0000012fu,0x00000022u,0x00000000u,0x00040047u,0x00000150u,0x0000000bu,0x00000019u,0x00020013u,0x00000002u,0x00030021u,0x00000003u,0x00000002u,0x00030016u,0x00000006u,0x00000020u,0x00090019u,0x00000007u,0x00000006u,0x00000001u,0x00000000u,0x00000000u,0x00000000u,0x00000001u,0x00000000u,0x0003001bu,0x00000008u,0x00000007u,0x00040020u,0x00000009u,0x00000000u,0x00000008u,0x00040017u,0x0000000au,0x00000006u,0x00000002u,0x00040020u,0x0000000bu,0x00000007u,0x0000000au,0x00060021u,0x0000000cu,0x00000006u,0x00000009u,0x00000009u,0x0000000bu,0x00060021u,0x00000012u,0x0000000au,0x00000009u,0x00000009u,0x0000000bu,0x00040020u,0x00000018u,0x00000007u,0x00000006u,0x0004002bu,0x00000006u,0x0000001au,0x00000000u,0x00040015u,0x0000001bu,0x00000020u,0x00000001u,0x00040020u,0x0000001cu,0x00000007u,0x0000001bu,0x0004002bu,0x0000001bu,0x0000001eu,0xffffffffu,0x0004002bu,0x0000001bu,0x00000025u,0x00000001u,0x00020014u,0x00000026u,0x00040020u,0x00000038u,0x00000006u,0x0000000au,0x0004003bu,0x00000038u,0x00000039u,0x00000006u,0x0004003bu,Line truncated
static const size_t OF3_FLOW_M4_LEN = 2005;
static const uint32_t OF3_EXPAND_M4_SPV[] = {0x07230203u,0x00010300u,0x0008000bu,0x00000169u,0x00000000u,0x00020011u,0x00000001u,0x00020011u,0x00000032u,0x0006000bu,0x00000001u,0x4c534c47u,0x6474732eu,0x3035342eu,0x00000000u,0x0003000eu,0x00000000u,0x00000001u,0x0006000fu,0x00000005u,0x00000004u,0x6e69616du,0x00000000u,0x00000049u,0x00060010u,0x00000004u,0x00000011u,0x00000010u,0x00000010u,0x00000001u,0x00030003u,0x00000002u,0x000001c2u,0x00040005u,0x00000004u,0x6e69616du,0x00000000u,0x00080005u,0x0000000eu,0x3564656du,0x3b316628u,0x663b3166u,0x31663b31u,0x3b31663bu,0x00000000u,0x00030005u,0x00000009u,0x00000061u,0x00030005u,0x0000000au,0x00000062u,0x00030005u,0x0000000bu,0x00000063u,0x00030005u,0x0000000cu,0x00000064u,0x00030005u,0x0000000du,0x00000065u,0x00030005u,0x00000010u,0x00000078u,0x00030005u,0x00000034u,0x00000079u,0x00030005u,0x00000045u,0x00646967u,0x00080005u,0x00000049u,0x475f6c67u,0x61626f6cu,0x766e496cu,0x7461636fu,0x496e6f69u,0x00000044u,0x00030005u,0x0000004eu,0x006d6964u,0x00040005u,0x00000051u,0x64774675u,0x00000000u,0x00030005u,0x0000006bu,0x00007675u,0x00030005u,0x00000074u,0x00007366u,0x00050005u,0x00000079u,0x6f6c4675u,0x6e694677u,0x00000065u,0x00030005u,0x00000083u,0x00003463u,0x00030005u,0x00000088u,0x00006d78u,0x00030005u,0x00000090u,0x00007078u,0x00030005u,0x00000098u,0x00006d79u,0x00030005u,0x000000a0u,0x00007079u,0x00030005u,0x000000a8u,0x00000066u,0x00040005u,0x000000a9u,0x61726170u,0x0000006du,0x00040005u,0x000000acu,0x61726170u,0x0000006du,0x00040005u,0x000000afu,0x61726170u,0x0000006du,0x00040005u,0x000000b2u,0x61726170u,0x0000006du,0x00040005u,0x000000b5u,0x61726170u,0x0000006du,0x00040005u,0x000000b9u,0x61726170u,0x0000006du,0x00040005u,0x000000bcu,0x61726170u,0x0000006du,0x00040005u,0x000000bfu,0x61726170u,0x0000006du,0x00040005u,0x000000c2u,0x61726170u,0x0000006du,0x00040005u,0x000000c5u,0x61726170u,0x0000006du,0x00040005u,0x000000c9u,0x61726170u,0x0000006du,0x00040005u,0x000000cdu,0x61726170u,0x0000006du,0x00040005u,0x000000d0u,0x61726170u,0x0000006du,0x00040005u,0x000000d3u,0x61726170u,0x0000006du,0x00040005u,0x000000d6u,0x61726170u,0x0000006du,0x00040005u,0x000000dau,0x61726170u,0x0000006du,0x00040005u,0x000000deu,0x61726170u,0x0000006du,0x00040005u,0x000000e1u,0x61726170u,0x0000006du,0x00040005u,0x000000e4u,0x61726170u,0x0000006du,0x00040005u,0x000000e7u,0x61726170u,0x0000006du,0x00030005u,0x000000ecu,0x00647766u,0x00030005u,0x000000efu,0x00647762u,0x00030005u,0x000000f2u,0x0067616du,0x00030005u,0x000000fau,0x006c6572u,0x00040005u,0x00000101u,0x666e6f63u,0x00000000u,0x00030005u,0x00000102u,0x00000055u,0x00060006u,0x00000102u,0x00000000u,0x776f6c66u,0x6c616353u,0x00000065u,0x00050006u,0x00000102u,0x00000001u,0x6576656cu,0x0000006cu,0x00050006u,0x00000102u,0x00000002u,0x6c63636fu,0x00006f4cu,0x00050006u,0x00000102u,0x00000003u,0x6c63636fu,0x00006948u,0x00030005u,0x00000104u,0x00000000u,0x00030005u,0x0000010fu,0x00000071u,0x00030005u,0x00000113u,0x00787067u,0x00030005u,0x00000114u,0x00004d47u,0x00050006u,0x00000114u,0x00000000u,0x694c6d67u,0x0000006eu,0x00050006u,0x00000114u,0x00000001u,0x694c6d67u,0x0000546eu,0x00050006u,0x00000114u,0x00000002u,0x70416d67u,0x00000070u,0x00050006u,0x00000114u,0x00000003u,0x70416d67u,0x00005470u,0x00030005u,0x00000116u,0x00000000u,0x00030005u,0x00000139u,0x00003241u,0x00040005u,0x00000147u,0x6c6c7566u,0x00647746u,0x00040005u,0x0000014fu,0x6c6c7566u,0x00647742u,0x00040005u,0x0000015fu,0x64774275u,0x00000000u,0x00040047u,0x00000049u,0x0000000bu,0x0000001cu,0x00030047u,0x00000051u,0x00000019u,0x00040047u,0x00000051u,0x00000021u,0x00000030u,0x00040047u,0x00000051u,0x00000022u,0x00000000u,0x00040047u,0x00000079u,0x00000021u,0x00000020u,0x00040047u,0x00000079u,0x00000022u,0x00000000u,0x00030047u,0x00000102u,0x00000002u,0x00050048u,0x00000102u,0x00000000u,0x00000023u,0x00000000u,0x00050048u,0x00000102u,0x00000001u,0x00000023u,0x00000004u,0x00050048u,0x00000102u,0x00000002u,0x00000023u,0x00000008u,0x00050048u,0x00000102u,0x00000003u,0x00000023u,0x0000000cu,0x00040047u,0x00000104u,0x00000021u,0x00000000u,0x00040047u,0x00000104u,0x00000022u,0x00000000u,0x00030047u,0x00000114u,0x00000002u,0x00050048u,0x00000114u,0x00000000u,0x00000023u,0x00000000u,0x00050048u,0x00000114u,0x00000001u,0x00000023u,0x00000010u,0x00050048u,0x00000114u,0x00000002u,0x00000023u,0x00000020u,0x00050048u,0x00000114u,0x00000003u,0x00000023u,0x00000030u,0x00040047u,0x00000116u,0x00000021u,0x00000001u,0x00040047u,0x00000116u,0x00000022u,0x00000000u,0x00030047u,0x0000015fu,0x00000019u,0x00040047u,0x0000015fu,0x00000021u,0x00000031u,0x00040047u,0x0000015fu,0x00000022u,0x00000000u,0x00040047u,0x00000168u,0x0000000bu,0x00000019u,0x00020013u,0x00000002u,0x00030021u,0x00000003u,0x00000002u,0x00030016u,0x00000006u,0x00000020u,0x00040020u,0x00000007u,0x00000007u,0x00000006u,0x00080021u,0x00000008u,0x00000006u,0x00000007u,0x00000007u,0x00000007u,0x00000007u,0x00000007u,0x00040015u,0x00000042u,0x00000020u,0x00000001u,0x00040017u,0x00000043u,0x00000042u,0x00000002u,0x00040020u,0x00000044Line truncated
static const size_t OF3_EXPAND_M4_LEN = 2217;
static const uint32_t OF3_GM_REDUCE_SPV[] = {0x07230203u,0x00010300u,0x0008000bu,0x000001d7u,0x00000000u,0x00020011u,0x00000001u,0x00020011u,0x00000032u,0x0006000bu,0x00000001u,0x4c534c47u,0x6474732eu,0x3035342eu,0x00000000u,0x0003000eu,0x00000000u,0x00000001u,0x0006000fu,0x00000005u,0x00000004u,0x6e69616du,0x00000000u,0x0000000bu,0x00060010u,0x00000004u,0x00000011u,0x00000040u,0x00000001u,0x00000001u,0x00030003u,0x00000002u,0x000001c2u,0x00040005u,0x00000004u,0x6e69616du,0x00000000u,0x00040005u,0x00000008u,0x64697467u,0x00000000u,0x00080005u,0x0000000bu,0x475f6c67u,0x61626f6cu,0x766e496cu,0x7461636fu,0x496e6f69u,0x00000044u,0x00030005u,0x0000001au,0x006d6964u,0x00040005u,0x0000001fu,0x65725075u,0x00000076u,0x00040005u,0x00000024u,0x61746f74u,0x0000006cu,0x00040005u,0x00000030u,0x44766e69u,0x00006d69u,0x00040005u,0x00000036u,0x6c6f6370u,0x00000030u,0x00030005u,0x00000038u,0x00004d47u,0x00050006u,0x00000038u,0x00000000u,0x694c6d67u,0x0000006eu,0x00050006u,0x00000038u,0x00000001u,0x694c6d67u,0x0000546eu,0x00050006u,0x00000038u,0x00000002u,0x70416d67u,0x00000070u,0x00050006u,0x00000038u,0x00000003u,0x70416d67u,0x00005470u,0x00030005u,0x0000003au,0x00000000u,0x00040005u,0x00000041u,0x6c6f6370u,0x00000031u,0x00030005u,0x00000049u,0x00000074u,0x00030005u,0x0000004fu,0x0000006bu,0x00030005u,0x0000005au,0x00636361u,0x00030005u,0x00000061u,0x00000069u,0x00030005u,0x0000006bu,0x00007870u,0x00030005u,0x0000009au,0x00007675u,0x00030005u,0x000000a2u,0x00000071u,0x00040005u,0x000000a6u,0x72537675u,0x00000063u,0x00030005u,0x000000cfu,0x00000054u,0x00030005u,0x000000d4u,0x00000049u,0x00040005u,0x000000d5u,0x72754375u,0x00000072u,0x00030005u,0x000000dau,0x00000065u,0x00030005u,0x000000deu,0x006d7854u,0x00030005u,0x000000e7u,0x00707854u,0x00030005u,0x000000f0u,0x006d7954u,0x00030005u,0x000000f9u,0x00707954u,0x00030005u,0x00000102u,0x00007854u,0x00030005u,0x0000010au,0x00007954u,0x00030005u,0x00000112u,0x00006561u,0x00030005u,0x00000115u,0x00000077u,0x00030005u,0x00000125u,0x00006473u,0x00030005u,0x00000142u,0x00786469u,0x00030005u,0x00000143u,0x00000072u,0x00030005u,0x0000014bu,0x00000063u,0x00040005u,0x000001bcu,0x65736162u,0x00000000u,0x00030005u,0x000001bfu,0x0000006bu,0x00050005u,0x000001c8u,0x74726150u,0x736c6169u,0x00000000u,0x00050006u,0x000001c8u,0x00000000u,0x74726170u,0x00000000u,0x00030005u,0x000001cau,0x00000000u,0x00040047u,0x0000000bu,0x0000000bu,0x0000001cu,0x00040047u,0x0000001fu,0x00000021u,0x00000020u,0x00040047u,0x0000001fu,0x00000022u,0x00000000u,0x00030047u,0x00000038u,0x00000002u,0x00050048u,0x00000038u,0x00000000u,0x00000023u,0x00000000u,0x00050048u,0x00000038u,0x00000001u,0x00000023u,0x00000010u,0x00050048u,0x00000038u,0x00000002u,0x00000023u,0x00000020u,0x00050048u,0x00000038u,0x00000003u,0x00000023u,0x00000030u,0x00040047u,0x0000003au,0x00000021u,0x00000001u,0x00040047u,0x0000003au,0x00000022u,0x00000000u,0x00040047u,0x000000d5u,0x00000021u,0x00000021u,0x00040047u,0x000000d5u,0x00000022u,0x00000000u,0x00040047u,0x000001c7u,0x00000006u,0x00000004u,0x00030047u,0x000001c8u,0x00000002u,0x00050048u,0x000001c8u,0x00000000u,0x00000023u,0x00000000u,0x00040047u,0x000001cau,0x00000021u,0x00000010u,0x00040047u,0x000001cau,0x00000022u,0x00000000u,0x00040047u,0x000001d6u,0x0000000bu,0x00000019u,0x00020013u,0x00000002u,0x00030021u,0x00000003u,0x00000002u,0x00040015u,0x00000006u,0x00000020u,0x00000000u,0x00040020u,0x00000007u,0x00000007u,0x00000006u,0x00040017u,0x00000009u,0x00000006u,0x00000003u,0x00040020u,0x0000000au,0x00000001u,0x00000009u,0x0004003bu,0x0000000au,0x0000000bu,0x00000001u,0x0004002bu,0x00000006u,0x0000000cu,0x00000000u,0x00040020u,0x0000000du,0x00000001u,0x00000006u,0x0004002bu,0x00000006u,0x00000011u,0x00000200u,0x00020014u,0x00000012u,0x00040015u,0x00000017u,0x00000020u,0x00000001u,0x00040017u,0x00000018u,0x00000017u,0x00000002u,0x00040020u,0x00000019u,0x00000007u,0x00000018u,0x00030016u,0x0000001bu,0x00000020u,0x00090019u,0x0000001cu,0x0000001bu,0x00000001u,0x00000000u,0x00000000u,0x00000000u,0x00000001u,0x00000000u,0x0003001bu,0x0000001du,0x0000001cu,0x00040020u,0x0000001eu,0x00000000u,0x0000001du,0x0004003bu,0x0000001eu,0x0000001fu,0x00000000u,0x0004002bu,0x00000017u,0x00000021u,0x00000000u,0x00040020u,0x00000025u,0x00000007u,0x00000017u,0x0004002bu,0x00000006u,0x00000029u,0x00000001u,0x00040017u,0x0000002eu,0x0000001bu,0x00000002u,0x00040020u,0x0000002fu,0x00000007u,0x0000002eu,0x0004002bu,0x0000001bu,0x00000031u,0x3f800000u,0x00040017u,0x00000037u,0x0000001bu,0x00000004u,0x0006001eu,0x00000038u,0x00000037u,0x00000037u,0x00000037u,0x00000037u,0x00040020u,0x00000039u,0x00000002u,0x00000038u,0x0004003bu,0x00000039u,0x0000003au,0x00000002u,0x00040020u,0x0000003bu,0x00000002u,0x0000001bu,0x0004002bu,0x00000006u,0x00000042u,0x00000002u,0x0004002bu,0x00000006u,0x00000045u,0x00000003u,0x0004002bu,0x00000017u,0x0000004au,0x00000001u,0x00040020u,0x0000004bu,0x00000002u,0x00000037u,0x0004002bu,0x00000006u,0x00000056u,0x00000020u,0x0004001cu,0x00000058u,0x0000001bu,0x00000056u,0x00040020u,0x00000059Line truncated
static const size_t OF3_GM_REDUCE_LEN = 2584;
static const uint32_t OF3_GM_PREWARP_SPV[] = {0x07230203u,0x00010300u,0x0008000bu,0x00000074u,0x00000000u,0x00020011u,0x00000001u,0x00020011u,0x00000032u,0x0006000bu,0x00000001u,0x4c534c47u,0x6474732eu,0x3035342eu,0x00000000u,0x0003000eu,0x00000000u,0x00000001u,0x0006000fu,0x00000005u,0x00000004u,0x6e69616du,0x00000000u,0x0000000du,0x00060010u,0x00000004u,0x00000011u,0x00000010u,0x00000010u,0x00000001u,0x00030003u,0x00000002u,0x000001c2u,0x00040005u,0x00000004u,0x6e69616du,0x00000000u,0x00030005u,0x00000009u,0x00646967u,0x00080005u,0x0000000du,0x475f6c67u,0x61626f6cu,0x766e496cu,0x7461636fu,0x496e6f69u,0x00000044u,0x00030005u,0x00000012u,0x006d6964u,0x00040005u,0x00000016u,0x74734475u,0x00000000u,0x00030005u,0x00000030u,0x00007675u,0x00030005u,0x00000039u,0x00000071u,0x00040005u,0x0000003du,0x6c6f6370u,0x00000030u,0x00030005u,0x0000003fu,0x00004d47u,0x00050006u,0x0000003fu,0x00000000u,0x694c6d67u,0x0000006eu,0x00050006u,0x0000003fu,0x00000001u,0x694c6d67u,0x0000546eu,0x00050006u,0x0000003fu,0x00000002u,0x70416d67u,0x00000070u,0x00050006u,0x0000003fu,0x00000003u,0x70416d67u,0x00005470u,0x00030005u,0x00000041u,0x00000000u,0x00040005u,0x00000049u,0x6c6f6370u,0x00000031u,0x00040005u,0x00000051u,0x72537675u,0x00000063u,0x00030005u,0x00000064u,0x00000076u,0x00040005u,0x00000068u,0x72754375u,0x00000072u,0x00040047u,0x0000000du,0x0000000bu,0x0000001cu,0x00030047u,0x00000016u,0x00000019u,0x00040047u,0x00000016u,0x00000021u,0x00000030u,0x00040047u,0x00000016u,0x00000022u,0x00000000u,0x00030047u,0x0000003fu,0x00000002u,0x00050048u,0x0000003fu,0x00000000u,0x00000023u,0x00000000u,0x00050048u,0x0000003fu,0x00000001u,0x00000023u,0x00000010u,0x00050048u,0x0000003fu,0x00000002u,0x00000023u,0x00000020u,0x00050048u,0x0000003fu,0x00000003u,0x00000023u,0x00000030u,0x00040047u,0x00000041u,0x00000021u,0x00000001u,0x00040047u,0x00000041u,0x00000022u,0x00000000u,0x00040047u,0x00000068u,0x00000021u,0x00000020u,0x00040047u,0x00000068u,0x00000022u,0x00000000u,0x00040047u,0x00000073u,0x0000000bu,0x00000019u,0x00020013u,0x00000002u,0x00030021u,0x00000003u,0x00000002u,0x00040015u,0x00000006u,0x00000020u,0x00000001u,0x00040017u,0x00000007u,0x00000006u,0x00000002u,0x00040020u,0x00000008u,0x00000007u,0x00000007u,0x00040015u,0x0000000au,0x00000020u,0x00000000u,0x00040017u,0x0000000bu,0x0000000au,0x00000003u,0x00040020u,0x0000000cu,0x00000001u,0x0000000bu,0x0004003bu,0x0000000cu,0x0000000du,0x00000001u,0x00040017u,0x0000000eu,0x0000000au,0x00000002u,0x00030016u,0x00000013u,0x00000020u,0x00090019u,0x00000014u,0x00000013u,0x00000001u,0x00000000u,0x00000000u,0x00000000u,0x00000002u,0x00000003u,0x00040020u,0x00000015u,0x00000000u,0x00000014u,0x0004003bu,0x00000015u,0x00000016u,0x00000000u,0x00020014u,0x00000019u,0x0004002bu,0x0000000au,0x0000001au,0x00000000u,0x00040020u,0x0000001bu,0x00000007u,0x00000006u,0x0004002bu,0x0000000au,0x00000024u,0x00000001u,0x00040017u,0x0000002eu,0x00000013u,0x00000002u,0x00040020u,0x0000002fu,0x00000007u,0x0000002eu,0x0004002bu,0x00000013u,0x00000033u,0x3f000000u,0x00040017u,0x0000003eu,0x00000013u,0x00000004u,0x0006001eu,0x0000003fu,0x0000003eu,0x0000003eu,0x0000003eu,0x0000003eu,0x00040020u,0x00000040u,0x00000002u,0x0000003fu,0x0004003bu,0x00000040u,0x00000041u,0x00000002u,0x0004002bu,0x00000006u,0x00000042u,0x00000002u,0x00040020u,0x00000043u,0x00000002u,0x00000013u,0x0004002bu,0x0000000au,0x0000004au,0x00000002u,0x0004002bu,0x0000000au,0x0000004du,0x00000003u,0x00040020u,0x00000054u,0x00000007u,0x00000013u,0x0004002bu,0x00000006u,0x0000005eu,0x00000003u,0x00040020u,0x0000005fu,0x00000002u,0x0000003eu,0x00090019u,0x00000065u,0x00000013u,0x00000001u,0x00000000u,0x00000000u,0x00000000u,0x00000001u,0x00000000u,0x0003001bu,0x00000066u,0x00000065u,0x00040020u,0x00000067u,0x00000000u,0x00000066u,0x0004003bu,0x00000067u,0x00000068u,0x00000000u,0x0004002bu,0x00000013u,0x0000006bu,0x00000000u,0x0004002bu,0x0000000au,0x00000072u,0x00000010u,0x0006002cu,0x0000000bu,0x00000073u,0x00000072u,0x00000072u,0x00000024u,0x00050036u,0x00000002u,0x00000004u,0x00000000u,0x00000003u,0x000200f8u,0x00000005u,0x0004003bu,0x00000008u,0x00000009u,0x00000007u,0x0004003bu,0x00000008u,0x00000012u,0x00000007u,0x0004003bu,0x0000002fu,0x00000030u,0x00000007u,0x0004003bu,0x0000002fu,0x00000039u,0x00000007u,0x0004003bu,0x0000002fu,0x0000003du,0x00000007u,0x0004003bu,0x0000002fu,0x00000049u,0x00000007u,0x0004003bu,0x0000002fu,0x00000051u,0x00000007u,0x0004003bu,0x00000054u,0x00000064u,0x00000007u,0x0004003du,0x0000000bu,0x0000000fu,0x0000000du,0x0007004fu,0x0000000eu,0x00000010u,0x0000000fu,0x0000000fu,0x00000000u,0x00000001u,0x0004007cu,0x00000007u,0x00000011u,0x00000010u,0x0003003eu,0x00000009u,0x00000011u,0x0004003du,0x00000014u,0x00000017u,0x00000016u,0x00040068u,0x00000007u,0x00000018u,0x00000017u,0x0003003eu,0x00000012u,0x00000018u,0x00050041u,0x0000001bu,0x0000001cu,0x00000009u,0x0000001au,0x0004003du,0x00000006u,0x0000001du,0x0000001cu,0x00050041u,0x0000001bu,0x0000001eu,0x00000012u,0x0000001au,0x0004003du,0x00000006u,0x0000001fu,0x0000001Line truncated
static const size_t OF3_GM_PREWARP_LEN = 706;
static const uint32_t OF3_FLOWREG_SPV[] = {0x07230203u,0x00010300u,0x0008000bu,0x00000144u,0x00000000u,0x00020011u,0x00000001u,0x00020011u,0x00000032u,0x0006000bu,0x00000001u,0x4c534c47u,0x6474732eu,0x3035342eu,0x00000000u,0x0003000eu,0x00000000u,0x00000001u,0x0006000fu,0x00000005u,0x00000004u,0x6e69616du,0x00000000u,0x00000037u,0x00060010u,0x00000004u,0x00000011u,0x00000010u,0x00000010u,0x00000001u,0x00030003u,0x00000002u,0x000001c2u,0x00040005u,0x00000004u,0x6e69616du,0x00000000u,0x00060005u,0x0000000eu,0x63746566u,0x76285568u,0x763b3269u,0x003b3269u,0x00030005u,0x0000000cu,0x00000070u,0x00030005u,0x0000000du,0x006d6964u,0x00060005u,0x00000013u,0x63746566u,0x76284c68u,0x763b3269u,0x003b3269u,0x00030005u,0x00000011u,0x00000070u,0x00030005u,0x00000012u,0x006d6964u,0x00040005u,0x00000018u,0x6f6c4675u,0x006e4977u,0x00040005u,0x00000026u,0x6d754c75u,0x00000061u,0x00030005u,0x00000034u,0x00646967u,0x00080005u,0x00000037u,0x475f6c67u,0x61626f6cu,0x766e496cu,0x7461636fu,0x496e6f69u,0x00000044u,0x00030005u,0x0000003cu,0x006d6964u,0x00030005u,0x00000055u,0x00007464u,0x00030005u,0x00000056u,0x00000055u,0x00050006u,0x00000056u,0x00000000u,0x61726170u,0x0000736du,0x00030005u,0x00000058u,0x00000000u,0x00040005u,0x0000005cu,0x626d616cu,0x00006164u,0x00050005u,0x0000005fu,0x65676465u,0x68706c41u,0x00000061u,0x00040005u,0x00000063u,0x54737065u,0x00000056u,0x00030005u,0x00000068u,0x00004375u,0x00040005u,0x00000069u,0x61726170u,0x0000006du,0x00040005u,0x0000006bu,0x61726170u,0x0000006du,0x00030005u,0x0000006eu,0x00004c75u,0x00040005u,0x00000073u,0x61726170u,0x0000006du,0x00040005u,0x00000074u,0x61726170u,0x0000006du,0x00030005u,0x00000077u,0x00005275u,0x00040005u,0x0000007bu,0x61726170u,0x0000006du,0x00040005u,0x0000007cu,0x61726170u,0x0000006du,0x00030005u,0x0000007fu,0x00005575u,0x00040005u,0x00000083u,0x61726170u,0x0000006du,0x00040005u,0x00000084u,0x61726170u,0x0000006du,0x00030005u,0x00000087u,0x00004475u,0x00040005u,0x0000008bu,0x61726170u,0x0000006du,0x00040005u,0x0000008cu,0x61726170u,0x0000006du,0x00040005u,0x0000008fu,0x6d69646cu,0x00000000u,0x00030005u,0x00000093u,0x00004c6cu,0x00040005u,0x00000096u,0x61726170u,0x0000006du,0x00040005u,0x00000097u,0x61726170u,0x0000006du,0x00030005u,0x0000009au,0x0000526cu,0x00040005u,0x0000009du,0x61726170u,0x0000006du,0x00040005u,0x0000009eu,0x61726170u,0x0000006du,0x00030005u,0x000000a1u,0x0000556cu,0x00040005u,0x000000a4u,0x61726170u,0x0000006du,0x00040005u,0x000000a5u,0x61726170u,0x0000006du,0x00030005u,0x000000a8u,0x0000446cu,0x00040005u,0x000000abu,0x61726170u,0x0000006du,0x00040005u,0x000000acu,0x61726170u,0x0000006du,0x00040005u,0x000000afu,0x67616d67u,0x00000000u,0x00030005u,0x000000bbu,0x00000067u,0x00030005u,0x000000c4u,0x00004c64u,0x00030005u,0x000000c8u,0x00005264u,0x00030005u,0x000000ccu,0x00005564u,0x00030005u,0x000000d0u,0x00004464u,0x00030005u,0x000000d4u,0x00003265u,0x00030005u,0x000000d8u,0x00004c77u,0x00030005u,0x000000e1u,0x00005277u,0x00030005u,0x000000eau,0x00005577u,0x00030005u,0x000000f3u,0x00004477u,0x00040005u,0x000000fcu,0x576d7573u,0x00000000u,0x00040005u,0x00000104u,0x576d7573u,0x00000055u,0x00040005u,0x00000114u,0x66694475u,0x00000066u,0x00030005u,0x00000120u,0x00003066u,0x00050005u,0x00000121u,0x6f6c4675u,0x73624f77u,0x00000000u,0x00040005u,0x0000012au,0x74656874u,0x00000061u,0x00030005u,0x0000012eu,0x00000072u,0x00040005u,0x00000132u,0x77654e75u,0x00000000u,0x00050005u,0x0000013eu,0x6f6c4675u,0x74754f77u,0x00000000u,0x00040047u,0x00000018u,0x00000021u,0x00000020u,0x00040047u,0x00000018u,0x00000022u,0x00000000u,0x00040047u,0x00000026u,0x00000021u,0x00000022u,0x00040047u,0x00000026u,0x00000022u,0x00000000u,0x00040047u,0x00000037u,0x0000000bu,0x0000001cu,0x00030047u,0x00000056u,0x00000002u,0x00050048u,0x00000056u,0x00000000u,0x00000023u,0x00000000u,0x00040047u,0x00000058u,0x00000021u,0x00000000u,0x00040047u,0x00000058u,0x00000022u,0x00000000u,0x00040047u,0x00000121u,0x00000021u,0x00000021u,0x00040047u,0x00000121u,0x00000022u,0x00000000u,0x00030047u,0x0000013eu,0x00000019u,0x00040047u,0x0000013eu,0x00000021u,0x00000030u,0x00040047u,0x0000013eu,0x00000022u,0x00000000u,0x00040047u,0x00000143u,0x0000000bu,0x00000019u,0x00020013u,0x00000002u,0x00030021u,0x00000003u,0x00000002u,0x00040015u,0x00000006u,0x00000020u,0x00000001u,0x00040017u,0x00000007u,0x00000006u,0x00000002u,0x00040020u,0x00000008u,0x00000007u,0x00000007u,0x00030016u,0x00000009u,0x00000020u,0x00040017u,0x0000000au,0x00000009u,0x00000004u,0x00050021u,0x0000000bu,0x0000000au,0x00000008u,0x00000008u,0x00050021u,0x00000010u,0x00000009u,0x00000008u,0x00000008u,0x00090019u,0x00000015u,0x00000009u,0x00000001u,0x00000000u,0x00000000u,0x00000000u,0x00000001u,0x00000000u,0x0003001bu,0x00000016u,0x00000015u,0x00040020u,0x00000017u,0x00000000u,0x00000016u,0x0004003bu,0x00000017u,0x00000018u,0x00000000u,0x0004002bu,0x00000006u,0x0000001bu,0x00000000u,0x0005002cu,0x00000007u,0x0000001cu,0x0000001bu,0x0000001bu,0x0004002bu,0x00000006u,0x0000001eu,0x00000001u,0x0004003bu,0x00000017u,0x00000026u,0x00000000u,Line truncated
static const size_t OF3_FLOWREG_LEN = 1936;
static const uint32_t WFG_SYNTH_SPV[] = {0x07230203u,0x00010300u,0x0008000bu,0x00000208u,0x00000000u,0x00020011u,0x00000001u,0x00020011u,0x00000032u,0x0006000bu,0x00000001u,0x4c534c47u,0x6474732eu,0x3035342eu,0x00000000u,0x0003000eu,0x00000000u,0x00000001u,0x0006000fu,0x00000005u,0x00000004u,0x6e69616du,0x00000000u,0x0000001cu,0x00060010u,0x00000004u,0x00000011u,0x00000010u,0x00000010u,0x00000001u,0x00030003u,0x00000002u,0x000001c2u,0x00040005u,0x00000004u,0x6e69616du,0x00000000u,0x00050005u,0x0000000bu,0x616d756cu,0x33667628u,0x0000003bu,0x00030005u,0x0000000au,0x00000063u,0x00030005u,0x00000018u,0x00646967u,0x00080005u,0x0000001cu,0x475f6c67u,0x61626f6cu,0x766e496cu,0x7461636fu,0x496e6f69u,0x00000044u,0x00030005u,0x00000021u,0x006d6964u,0x00040005u,0x00000024u,0x74754f75u,0x00000000u,0x00040005u,0x0000003eu,0x44766e69u,0x00006d69u,0x00030005u,0x00000044u,0x00007675u,0x00030005u,0x0000004du,0x00000055u,0x00060006u,0x0000004du,0x00000000u,0x776f6c66u,0x6c616353u,0x00000065u,0x00050006u,0x0000004du,0x00000001u,0x68706c61u,0x00000061u,0x00050006u,0x0000004du,0x00000002u,0x61746562u,0x00000000u,0x00050006u,0x0000004du,0x00000003u,0x626d616cu,0x00006164u,0x00050006u,0x0000004du,0x00000004u,0x69737065u,0x006e6f6cu,0x00060006u,0x0000004du,0x00000005u,0x746f6870u,0x6163536fu,0x0000656cu,0x00050006u,0x0000004du,0x00000006u,0x30646170u,0x00000000u,0x00050006u,0x0000004du,0x00000007u,0x31646170u,0x00000000u,0x00050006u,0x0000004du,0x00000008u,0x52647568u,0x00746365u,0x00030005u,0x0000004fu,0x00000000u,0x00040005u,0x0000008cu,0x72754375u,0x00000072u,0x00030005u,0x00000098u,0x00676264u,0x00030005u,0x0000009fu,0x00007366u,0x00040005u,0x000000a9u,0x64774273u,0x00000000u,0x00050005u,0x000000aau,0x6f6c4675u,0x64774277u,0x00000000u,0x00040005u,0x000000aeu,0x64774673u,0x00000000u,0x00050005u,0x000000afu,0x6f6c4675u,0x64774677u,0x00000000u,0x00030005u,0x000000b3u,0x00313046u,0x00030005u,0x000000bau,0x00303146u,0x00040005u,0x000000c1u,0x31306663u,0x00000000u,0x00040005u,0x000000c5u,0x30316663u,0x00000000u,0x00030005u,0x000000c9u,0x0031306du,0x00030005u,0x000000d5u,0x0030316du,0x00030005u,0x000000e0u,0x00004170u,0x00030005u,0x000000e8u,0x00004270u,0x00030005u,0x000000f0u,0x00003043u,0x00040005u,0x000000f1u,0x65725075u,0x00000076u,0x00030005u,0x000000f6u,0x00003143u,0x00030005u,0x000000fbu,0x00004165u,0x00040005u,0x000000fcu,0x61726170u,0x0000006du,0x00040005u,0x0000010cu,0x61726170u,0x0000006du,0x00030005u,0x00000111u,0x00004265u,0x00040005u,0x00000112u,0x61726170u,0x0000006du,0x00040005u,0x00000122u,0x61726170u,0x0000006du,0x00040005u,0x00000127u,0x76556266u,0x00000000u,0x00040005u,0x0000012bu,0x65526266u,0x00646973u,0x00040005u,0x00000138u,0x736e6f63u,0x00747369u,0x00030005u,0x00000149u,0x0000305au,0x00030005u,0x00000157u,0x0000315au,0x00040005u,0x00000163u,0x30756174u,0x00000000u,0x00040005u,0x00000167u,0x31756174u,0x00000000u,0x00040005u,0x0000016au,0x78616d7au,0x00000000u,0x00030005u,0x0000016eu,0x00003057u,0x00030005u,0x00000179u,0x00003157u,0x00040005u,0x00000183u,0x6d757357u,0x00000000u,0x00040005u,0x00000187u,0x70726177u,0x00006465u,0x00050005u,0x000001a0u,0x736f7263u,0x64616673u,0x00000065u,0x00050005u,0x000001adu,0x746f6870u,0x7272456fu,0x00000000u,0x00050005u,0x000001b1u,0x746f6870u,0x7572546fu,0x00007473u,0x00040005u,0x000001b7u,0x73757274u,0x00000074u,0x00040005u,0x000001c3u,0x76657270u,0x00000043u,0x00040005u,0x000001c8u,0x72727563u,0x00000043u,0x00050005u,0x000001cdu,0x746e6f63u,0x44746e65u,0x00666669u,0x00050005u,0x000001d2u,0x72746e75u,0x65747375u,0x00000064u,0x00040005u,0x000001d7u,0x44676962u,0x00666669u,0x00050005u,0x000001dcu,0x70616e73u,0x75436f54u,0x00007272u,0x00040005u,0x000001e5u,0x6374756fu,0x00000000u,0x00030005u,0x000001eeu,0x0067616du,0x00030005u,0x000001f2u,0x0000006eu,0x00030005u,0x000001f6u,0x00746173u,0x00040047u,0x0000001cu,0x0000000bu,0x0000001cu,0x00030047u,0x00000024u,0x00000019u,0x00040047u,0x00000024u,0x00000021u,0x00000030u,0x00040047u,0x00000024u,0x00000022u,0x00000000u,0x00030047u,0x0000004du,0x00000002u,0x00050048u,0x0000004du,0x00000000u,0x00000023u,0x00000000u,0x00050048u,0x0000004du,0x00000001u,0x00000023u,0x00000004u,0x00050048u,0x0000004du,0x00000002u,0x00000023u,0x00000008u,0x00050048u,0x0000004du,0x00000003u,0x00000023u,0x0000000cu,0x00050048u,0x0000004du,0x00000004u,0x00000023u,0x00000010u,0x00050048u,0x0000004du,0x00000005u,0x00000023u,0x00000014u,0x00050048u,0x0000004du,0x00000006u,0x00000023u,0x00000018u,0x00050048u,0x0000004du,0x00000007u,0x00000023u,0x0000001cu,0x00050048u,0x0000004du,0x00000008u,0x00000023u,0x00000020u,0x00040047u,0x0000004fu,0x00000021u,0x00000000u,0x00040047u,0x0000004fu,0x00000022u,0x00000000u,0x00040047u,0x0000008cu,0x00000021u,0x00000021u,0x00040047u,0x0000008cu,0x00000022u,0x00000000u,0x00040047u,0x000000aau,0x00000021u,0x00000023u,0x00040047u,0x000000aau,0x00000022u,0x00000000u,0x00040047u,0x000000afu,0x00000021u,0x00000022u,0x00040047u,0x000000afu,0x00000022u,0x00000000u,0x00040047u,0x000000f1u,0x00000021u,0x00000020u,0x00040047u,0x000000f1u,0xLine truncated
static const size_t WFG_SYNTH_LEN = 3082;
struct Blob { const uint32_t* code; size_t words; };
enum Shader { OF3_LUMA, OF3_DOWNSAMPLE, OF3_FLOW, OF3_EXPAND, OF3_FLOW_M4, OF3_EXPAND_M4, OF3_GM_REDUCE, OF3_GM_PREWARP, OF3_FLOWREG, WFG_SYNTH, SHADER_COUNT };
static inline Blob blob(Shader s) {
switch (s) {
case OF3_LUMA: return { OF3_LUMA_SPV, OF3_LUMA_LEN };
case OF3_DOWNSAMPLE: return { OF3_DOWNSAMPLE_SPV, OF3_DOWNSAMPLE_LEN };
case OF3_FLOW: return { OF3_FLOW_SPV, OF3_FLOW_LEN };
case OF3_EXPAND: return { OF3_EXPAND_SPV, OF3_EXPAND_LEN };
case OF3_FLOW_M4: return { OF3_FLOW_M4_SPV, OF3_FLOW_M4_LEN };
case OF3_EXPAND_M4: return { OF3_EXPAND_M4_SPV, OF3_EXPAND_M4_LEN };
case OF3_GM_REDUCE: return { OF3_GM_REDUCE_SPV, OF3_GM_REDUCE_LEN };
case OF3_GM_PREWARP: return { OF3_GM_PREWARP_SPV, OF3_GM_PREWARP_LEN };
case OF3_FLOWREG: return { OF3_FLOWREG_SPV, OF3_FLOWREG_LEN };
case WFG_SYNTH: return { WFG_SYNTH_SPV, WFG_SYNTH_LEN };
default: return { nullptr, 0 };
}
}
} } // namespace winfg::embedded
@@ -0,0 +1,269 @@
// win-fg - compute frame-generation engine implementation.
#include "framegen.hpp"
#include "embedded_shaders.hpp"
#include "log.hpp"
#include <cmath>
#include <cstdio>
namespace winfg {
#define VKOK(x) do { if ((x) != VK_SUCCESS) { WFG_LOGE("vk fail: %s", #x); return false; } } while (0)
uint32_t FrameGen::findMemType(uint32_t bits, VkMemoryPropertyFlags props) const {
for (uint32_t i = 0; i < memProps_.memoryTypeCount; ++i)
if ((bits & (1u << i)) && (memProps_.memoryTypes[i].propertyFlags & props) == props)
return i;
return 0;
}
bool FrameGen::makeImage(Img& out, VkExtent2D ext, VkFormat fmt, VkImageUsageFlags usage) {
out.extent = ext; out.fmt = fmt;
VkImageCreateInfo ci{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
ci.imageType = VK_IMAGE_TYPE_2D;
ci.format = fmt;
ci.extent = {ext.width, ext.height, 1};
ci.mipLevels = 1; ci.arrayLayers = 1;
ci.samples = VK_SAMPLE_COUNT_1_BIT;
ci.tiling = VK_IMAGE_TILING_OPTIMAL;
ci.usage = usage;
ci.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
ci.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
VKOK(dd_->CreateImage(device_, &ci, nullptr, &out.image));
VkMemoryRequirements mr; dd_->GetImageMemoryRequirements(device_, out.image, &mr);
VkMemoryAllocateInfo ai{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
ai.allocationSize = mr.size;
ai.memoryTypeIndex = findMemType(mr.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
VKOK(dd_->AllocateMemory(device_, &ai, nullptr, &out.mem));
VKOK(dd_->BindImageMemory(device_, out.image, out.mem, 0));
VkImageViewCreateInfo vi{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO};
vi.image = out.image; vi.viewType = VK_IMAGE_VIEW_TYPE_2D; vi.format = fmt;
vi.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
VKOK(dd_->CreateImageView(device_, &vi, nullptr, &out.view));
return true;
}
void FrameGen::destroyImage(Img& i) {
if (i.view) dd_->DestroyImageView(device_, i.view, nullptr);
if (i.image) dd_->DestroyImage(device_, i.image, nullptr);
if (i.mem) dd_->FreeMemory(device_, i.mem, nullptr);
i = Img{};
}
// C1: allocate the per-slot host-visible SSBOs the LK reduce writes partials into.
// Host-visible + coherent so the CPU can read them back after the owning frame's
// fence signals (no staging copy needed; the buffers are tiny - kGmThreads*kGmStride
// floats each, ~64 KB). Persistently mapped.
bool FrameGen::makeGmBuffers() {
const VkDeviceSize sz = (VkDeviceSize)kGmThreads * kGmStride * sizeof(float);
const VkMemoryPropertyFlags want = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
for (int s = 0; s < kGmSlots; ++s) {
VkBufferCreateInfo bi{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
bi.size = sz; bi.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; bi.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
VKOK(dd_->CreateBuffer(device_, &bi, nullptr, &gmSsbo_[s]));
VkMemoryRequirements mr; dd_->GetBufferMemoryRequirements(device_, gmSsbo_[s], &mr);
VkMemoryAllocateInfo ai{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
ai.allocationSize = mr.size; ai.memoryTypeIndex = findMemType(mr.memoryTypeBits, want);
VKOK(dd_->AllocateMemory(device_, &ai, nullptr, &gmSsboMem_[s]));
VKOK(dd_->BindBufferMemory(device_, gmSsbo_[s], gmSsboMem_[s], 0));
VKOK(dd_->MapMemory(device_, gmSsboMem_[s], 0, sz, 0, &gmSsboPtr_[s]));
std::memset(gmSsboPtr_[s], 0, (size_t)sz);
gmSlotValid_[s] = false;
}
gmBuffersReady_ = true;
return true;
}
bool FrameGen::makePipe(Pipe& p, int shaderId,
const std::vector<VkDescriptorType>&) {
// binding layout is derived per shader below in init(); here we only build
// the module + a generic layout supplied by caller via setLayout already set.
embedded::Blob b = embedded::blob((embedded::Shader)shaderId);
VkShaderModuleCreateInfo mi{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO};
mi.codeSize = b.words * 4; mi.pCode = b.code;
VKOK(dd_->CreateShaderModule(device_, &mi, nullptr, &p.module));
VkPipelineLayoutCreateInfo li{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO};
li.setLayoutCount = 1; li.pSetLayouts = &p.setLayout;
VKOK(dd_->CreatePipelineLayout(device_, &li, nullptr, &p.layout));
VkComputePipelineCreateInfo pi{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO};
pi.stage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
pi.stage.stage = VK_SHADER_STAGE_COMPUTE_BIT;
pi.stage.module = p.module; pi.stage.pName = "main";
pi.layout = p.layout;
VKOK(dd_->CreateComputePipelines(device_, VK_NULL_HANDLE, 1, &pi, nullptr, &p.pipeline));
return true;
}
void FrameGen::destroyPipe(Pipe& p) {
if (p.pipeline) dd_->DestroyPipeline(device_, p.pipeline, nullptr);
if (p.layout) dd_->DestroyPipelineLayout(device_, p.layout, nullptr);
if (p.setLayout) dd_->DestroyDescriptorSetLayout(device_, p.setLayout, nullptr);
if (p.module) dd_->DestroyShaderModule(device_, p.module, nullptr);
p = Pipe{};
}
// Build a descriptor-set layout from an explicit (binding, type) list.
static bool buildSetLayout(const DeviceDispatch* dd, VkDevice dev,
const std::vector<std::pair<uint32_t, VkDescriptorType>>& binds,
VkDescriptorSetLayout& out) {
std::vector<VkDescriptorSetLayoutBinding> b;
for (auto& kv : binds)
b.push_back({kv.first, kv.second, 1, VK_SHADER_STAGE_COMPUTE_BIT, nullptr});
VkDescriptorSetLayoutCreateInfo ci{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
ci.bindingCount = (uint32_t)b.size(); ci.pBindings = b.data();
return dd->CreateDescriptorSetLayout(dev, &ci, nullptr, &out) == VK_SUCCESS;
}
bool FrameGen::init(const DeviceDispatch* dd, const InstanceDispatch* id,
VkPhysicalDevice phys, VkDevice dev, uint32_t queueFamily, VkQueue queue) {
dd_ = dd; id_ = id; phys_ = phys; device_ = dev; queueFamily_ = queueFamily; queue_ = queue;
id_->GetPhysicalDeviceMemoryProperties(phys_, &memProps_);
VkSamplerCreateInfo si{VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO};
si.magFilter = si.minFilter = VK_FILTER_LINEAR;
si.addressModeU = si.addressModeV = si.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
VKOK(dd_->CreateSampler(device_, &si, nullptr, &linear_));
// descriptor pool sized generously for our per-frame sets.
VkDescriptorPoolSize sizes[] = {
{VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 256},
{VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256},
{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 64},
{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 16}, // C1 LK-reduce SSBOs
};
VkDescriptorPoolCreateInfo pci{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO};
pci.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
pci.maxSets = 256; pci.poolSizeCount = 4; pci.pPoolSizes = sizes;
VKOK(dd_->CreateDescriptorPool(device_, &pci, nullptr, &descPool_));
using B = std::pair<uint32_t, VkDescriptorType>;
const auto S = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
const auto W = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
const auto U = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
const auto SB = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
// per-shader binding sets (match the GLSL layout(binding=...) declarations)
if (!buildSetLayout(dd_, dev, {B{32,S},B{48,W}}, pLuma_.setLayout)) return false;
if (!buildSetLayout(dd_, dev, {B{32,S},B{48,W}}, pDown_.setLayout)) return false;
if (!buildSetLayout(dd_, dev, {B{0,U},B{32,S},B{33,S},B{34,S},B{48,W}}, pFlow_.setLayout)) return false;
if (!buildSetLayout(dd_, dev, {B{0,U},B{32,S},B{33,S},B{34,S},B{48,W}}, pFlowM4_.setLayout)) return false;
// C1: expand sets gain a second UBO (binding 1 = GMUBO) for the global-motion add-back.
if (!buildSetLayout(dd_, dev, {B{0,U},B{1,U},B{32,S},B{48,W},B{49,W}}, pExpand_.setLayout)) return false;
if (!buildSetLayout(dd_, dev, {B{0,U},B{1,U},B{32,S},B{48,W},B{49,W}}, pExpandM4_.setLayout)) return false;
if (!buildSetLayout(dd_, dev, {B{0,U},B{32,S},B{33,S},B{34,S},B{35,S},B{48,W}}, pSynth_.setLayout)) return false;
// C1: LK-reduce (UBO, SSBO, prev, curr) and curr-luma stabilization prewarp (UBO, src, dst).
if (!buildSetLayout(dd_, dev, {B{1,U},B{16,SB},B{32,S},B{33,S}}, pGmReduce_.setLayout)) return false;
if (!buildSetLayout(dd_, dev, {B{1,U},B{32,S},B{48,W}}, pGmPrewarp_.setLayout)) return false;
// C2: flow-reg (UBO, u_in, f0_obs, luma, out).
if (!buildSetLayout(dd_, dev, {B{0,U},B{32,S},B{33,S},B{34,S},B{48,W}}, pFlowReg_.setLayout)) return false;
std::vector<VkDescriptorType> unused;
if (!makePipe(pLuma_, embedded::OF3_LUMA, unused)) return false;
if (!makePipe(pDown_, embedded::OF3_DOWNSAMPLE, unused)) return false;
if (!makePipe(pFlow_, embedded::OF3_FLOW, unused)) return false;
if (!makePipe(pFlowM4_, embedded::OF3_FLOW_M4, unused)) return false;
if (!makePipe(pExpand_, embedded::OF3_EXPAND, unused)) return false;
if (!makePipe(pExpandM4_, embedded::OF3_EXPAND_M4, unused)) return false;
if (!makePipe(pGmReduce_, embedded::OF3_GM_REDUCE, unused)) return false;
if (!makePipe(pGmPrewarp_, embedded::OF3_GM_PREWARP, unused)) return false;
if (!makePipe(pFlowReg_, embedded::OF3_FLOWREG, unused)) return false;
if (!makePipe(pSynth_, embedded::WFG_SYNTH, unused)) return false;
if (!makeGmBuffers()) { WFG_LOGE("framegen: GM SSBO alloc failed - C1 disabled"); }
WFG_LOGI("framegen init ok (queueFamily=%u, 10 pipelines, model=%d, C1 gm=%d, C2 flow_reg=%d iters=%d)",
queueFamily_, cfg_.model, cfg_.gmMode, cfg_.frMode, cfg_.frIters);
return true;
}
static VkExtent2D levelExtent(VkExtent2D e, int l) {
return { e.width >> l ? e.width >> l : 1u, e.height >> l ? e.height >> l : 1u };
}
bool FrameGen::onResize(VkExtent2D extent, VkFormat /*colorFormat*/, bool force) {
// force skips the same-extent early-out so a perf_preset change (which alters
// flowFinest_ and therefore the per-size flow-image resolutions) rebuilds them.
if (!force && ready_ && extent.width == extent_.width && extent.height == extent_.height) return true;
// tear down previous size
for (auto& i : pyrA_) destroyImage(i); pyrA_.clear();
for (auto& i : pyrB_) destroyImage(i); pyrB_.clear();
for (auto& i : pyrAs_) destroyImage(i); pyrAs_.clear();
for (auto& i : flowLvl_) destroyImage(i); flowLvl_.clear();
destroyImage(flowExpA_); destroyImage(flowExpB_);
destroyImage(flowRegA_); destroyImage(flowRegB_); // C2
extent_ = extent; ready_ = false;
const VkImageUsageFlags lumaUsage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT;
// flowLvl_ is cleared every frame (per-frame reset) and C2 copies the cleaned
// finest level back into it - both need TRANSFER_DST. The C2 ping-pong images
// are the copy SOURCE (TRANSFER_SRC).
const VkImageUsageFlags flowUsage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
const VkImageUsageFlags regUsage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
pyrA_.resize(kLevels); pyrB_.resize(kLevels); flowLvl_.resize(kLevels);
for (int l = 0; l < kLevels; ++l) {
VkExtent2D e = levelExtent(extent, l);
if (!makeImage(pyrA_[l], e, VK_FORMAT_R32_SFLOAT, lumaUsage)) return false;
if (!makeImage(pyrB_[l], e, VK_FORMAT_R32_SFLOAT, lumaUsage)) return false;
if (!makeImage(flowLvl_[l], e, VK_FORMAT_R16G16B16A16_SFLOAT, flowUsage)) return false;
}
// C1: stabilized curr luma - only the levels the dense flow reads (finest..coarsest).
pyrAs_.resize(kLevels);
for (int l = flowFinest_; l < kLevels; ++l)
if (!makeImage(pyrAs_[l], levelExtent(extent, l), VK_FORMAT_R32_SFLOAT, lumaUsage)) return false;
if (!makeImage(flowExpA_, extent, VK_FORMAT_R16G16B16A16_SFLOAT, flowUsage)) return false;
if (!makeImage(flowExpB_, extent, VK_FORMAT_R16G16B16A16_SFLOAT, flowUsage)) return false;
// C2: TV-L1 ping-pong scratch at the finest solved flow level (perf_preset-driven res).
if (!makeImage(flowRegA_, levelExtent(extent, flowFinest_), VK_FORMAT_R16G16B16A16_SFLOAT, regUsage)) return false;
if (!makeImage(flowRegB_, levelExtent(extent, flowFinest_), VK_FORMAT_R16G16B16A16_SFLOAT, regUsage)) return false;
ready_ = true;
WFG_LOGI("framegen resized to %ux%u (%d pyramid levels, kFlowFinest=%d perf_preset=%d)",
extent.width, extent.height, kLevels, flowFinest_, cfg_.perfPreset);
return true;
}
void FrameGen::barrier(VkCommandBuffer cmd, VkImage img, VkImageLayout from, VkImageLayout to) {
VkImageMemoryBarrier b{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER};
b.oldLayout = from; b.newLayout = to;
b.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
b.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT;
b.image = img; b.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
b.srcQueueFamilyIndex = b.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
dd_->CmdPipelineBarrier(cmd, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &b);
}
// NOTE: descriptor-set allocation/update per record() and the exact per-level
// flow wiring are implemented in record_impl.inc for readability; the graph is:
// luma(curr)->pyrA[0], luma(prev)->pyrB[0]
// downsample pyrA/pyrB to kLevels
// coarse->fine flow (per level) into flowLvl
// expand flowLvl[0] -> flowExpA/flowExpB
// synth(prev,curr,flowExpA,flowExpB, alpha) -> out
void FrameGen::destroy() {
if (!device_) return;
dd_->DeviceWaitIdle(device_);
for (auto& i : pyrA_) destroyImage(i); pyrA_.clear();
for (auto& i : pyrB_) destroyImage(i); pyrB_.clear();
for (auto& i : pyrAs_) destroyImage(i); pyrAs_.clear();
for (auto& i : flowLvl_) destroyImage(i); flowLvl_.clear();
destroyImage(flowExpA_); destroyImage(flowExpB_);
destroyImage(flowRegA_); destroyImage(flowRegB_); // C2
destroyPipe(pLuma_); destroyPipe(pDown_); destroyPipe(pFlow_); destroyPipe(pFlowM4_);
destroyPipe(pExpand_); destroyPipe(pExpandM4_); destroyPipe(pSynth_);
destroyPipe(pGmReduce_); destroyPipe(pGmPrewarp_);
destroyPipe(pFlowReg_); // C2
for (int s = 0; s < kGmSlots; ++s) {
if (gmSsboMem_[s]) dd_->UnmapMemory(device_, gmSsboMem_[s]);
if (gmSsbo_[s]) dd_->DestroyBuffer(device_, gmSsbo_[s], nullptr);
if (gmSsboMem_[s]) dd_->FreeMemory(device_, gmSsboMem_[s], nullptr);
gmSsbo_[s] = VK_NULL_HANDLE; gmSsboMem_[s] = VK_NULL_HANDLE; gmSsboPtr_[s] = nullptr;
}
gmBuffersReady_ = false;
if (descPool_) dd_->DestroyDescriptorPool(device_, descPool_, nullptr);
if (linear_) dd_->DestroySampler(device_, linear_, nullptr);
descPool_ = VK_NULL_HANDLE; linear_ = VK_NULL_HANDLE; device_ = VK_NULL_HANDLE; ready_ = false;
}
} // namespace winfg
// record() + its scratch helpers reopen namespace winfg themselves, so include
// this OUTSIDE the namespace block above (keeps FrameGen:: members attached).
#include "record_impl.inc"
@@ -0,0 +1,151 @@
// win-fg - compute frame-generation engine.
// Owns the compute pipelines (our of3_* optical flow + wfg_synth) and the
// per-resolution intermediate images, and records the flow+synth graph that
// turns (prev, curr) into an interpolated frame. Written for win-fg; no code
// from bionic-fg / lsfg / GameScope.
#pragma once
#include "vk_dispatch.hpp"
#include "config.hpp"
#include <vector>
namespace winfg {
class FrameGen {
public:
bool init(const DeviceDispatch* dd, const InstanceDispatch* id,
VkPhysicalDevice phys, VkDevice dev, uint32_t queueFamily, VkQueue queue);
// Applies a new config. Recomputes the active finest flow level from
// cfg_.perfPreset; if it changed while resources are live, safely rebuilds the
// affected per-size flow images (like a resize) and resets the flow predictor,
// so a perf_preset change is FULLY LIVE - no FG toggle needed. Defined in
// record_impl.inc (needs the scratch-state teardown). See flowFinestForPreset.
void configure(const Config& c);
// (Re)build per-resolution resources for a swapchain of the given size/format.
// force=true rebuilds even when the extent is unchanged (used by configure()
// when a perf_preset change alters the finest solved flow level).
bool onResize(VkExtent2D extent, VkFormat colorFormat, bool force = false);
// Record the whole flow+synth graph into cmd. prevView/currView are SAMPLED
// views of the two real frames; outView is a STORAGE view of the target
// image the generated frame is written into. alpha in (0,1).
// gmSlot selects which C1 global-motion SSBO to reduce into / read back; the
// caller MUST pass the ring-slot index of the FrameCtx it just fence-waited
// (that guarantees this slot's PREVIOUS reduce has completed and is safe to
// read on the host - see the decoupled-LK note in record_impl.inc).
void record(VkCommandBuffer cmd, VkImageView prevView, VkImageView currView,
VkImageView outView, float alpha, uint32_t gmSlot = 0);
void destroy();
bool valid() const { return device_ != VK_NULL_HANDLE && ready_; }
const Config& config() const { return cfg_; }
// public so the record_impl.inc free helpers can reference them
struct Img { VkImage image = VK_NULL_HANDLE; VkDeviceMemory mem = VK_NULL_HANDLE;
VkImageView view = VK_NULL_HANDLE; VkExtent2D extent{}; VkFormat fmt{}; };
struct Pipe { VkPipeline pipeline = VK_NULL_HANDLE; VkPipelineLayout layout = VK_NULL_HANDLE;
VkDescriptorSetLayout setLayout = VK_NULL_HANDLE; VkShaderModule module = VK_NULL_HANDLE; };
// ── C1 GLOBAL-MOTION PRE-WARP constants (public so record_impl.inc's free
// helpers can size/index the SSBO pool). See of3_gm_reduce/of3_gm_prewarp. ──
static constexpr int kGmSlots = 3; // >= present ring size; one SSBO per in-flight ctx
static constexpr int kGmLevel = 3; // 1/8-res luma pyramid level for the LK estimate
static constexpr int kGmThreads = 512; // fixed reduce threads (8 groups * local_size_x 64)
static constexpr int kGmStride = 32; // floats/thread: 21 H + 6 b + coverage + resid + pad
// Finest pyramid level the dense flow is solved at (0 = full res). This is now
// RUNTIME-SELECTABLE via conf.toml perf_preset (0 Quality=1 / 1 Balanced=2 /
// 2 Performance=3). kFlowFinestDefault is the Balanced value (today's behaviour);
// the ACTIVE level lives in flowFinest_ and is read through flowFinest(). onResize
// sizes the stabilized-curr pyramid + C2 scratch from it, and record_impl.inc
// wires the flow/expand inputs to it - both must agree, so they all read
// flowFinest()/flowFinest_ (never a hard-coded 2). (Was a static constexpr = 2.)
static constexpr int kFlowFinestDefault = 2; // Balanced (perf_preset=1)
// Map perf_preset -> finest solved level. Out-of-range -> Balanced. A higher
// level = coarser (cheaper) flow = smaller base-FPS drop. configure() clamps the
// result against kLevels so at least two solved levels always remain.
static int flowFinestForPreset(int preset) {
switch (preset) {
case 0: return 1; // Quality ~1/2-res
case 2: return 3; // Performance ~1/8-res
case 1: default: return kFlowFinestDefault; // Balanced ~1/4-res (today)
}
}
// The ACTIVE finest solved flow level (set by configure() from perf_preset).
int flowFinest() const { return flowFinest_; }
private:
uint32_t findMemType(uint32_t bits, VkMemoryPropertyFlags props) const;
bool makeImage(Img& out, VkExtent2D ext, VkFormat fmt, VkImageUsageFlags usage);
void destroyImage(Img& i);
bool makeGmBuffers(); // allocate the kGmSlots host-visible LK-reduce SSBOs
// Read back one reduce SSBO, solve the 6x6 inverse-compositional LK step, and
// update the running/applied global affine. Returns true if it engaged this
// frame. Implemented in record_impl.inc (has the ScratchState/UBO context).
bool updateGlobalMotion(uint32_t gmSlot, VkExtent2D fullExtent);
bool makePipe(Pipe& p, int shaderId,
const std::vector<VkDescriptorType>& bindings);
void destroyPipe(Pipe& p);
void barrier(VkCommandBuffer cmd, VkImage img, VkImageLayout from, VkImageLayout to);
const DeviceDispatch* dd_ = nullptr;
const InstanceDispatch* id_ = nullptr;
VkPhysicalDevice phys_ = VK_NULL_HANDLE;
VkDevice device_ = VK_NULL_HANDLE;
VkQueue queue_ = VK_NULL_HANDLE;
uint32_t queueFamily_ = 0;
VkPhysicalDeviceMemoryProperties memProps_{};
VkSampler linear_ = VK_NULL_HANDLE;
VkDescriptorPool descPool_ = VK_NULL_HANDLE;
Config cfg_;
VkExtent2D extent_{};
bool ready_ = false;
// ACTIVE finest solved flow level (perf_preset). Default = Balanced (today's 2)
// so an unconfigured engine and an unset conf.toml are byte-identical to before.
int flowFinest_ = kFlowFinestDefault;
// pipelines
Pipe pLuma_, pDown_, pFlow_, pFlowM4_, pExpand_, pExpandM4_, pSynth_;
// C1: LK normal-equation reduce + curr-luma stabilization pre-warp
Pipe pGmReduce_, pGmPrewarp_;
// C2: TV-L1 flow-regularization iteration (one dispatch per iteration)
Pipe pFlowReg_;
// per-resolution resources
// 7 levels (was 5): the coarsest levels set the max motion the solver can track.
// At 5 levels the reach was only ~±84px/frame at 720p, so fast camera motion
// (150-300px/frame at the periphery) saturated the search -> incoherent vectors
// -> oil-paint melt. Two more (tiny) coarse levels push the reach to ~±300px so
// large global motion is captured coherently instead of smeared. Near-zero cost:
// the extra levels are 40x22 and 20x11 images.
static const int kLevels = 7;
std::vector<Img> pyrA_, pyrB_; // luma pyramids for curr / prev
std::vector<Img> pyrAs_; // C1: STABILIZED curr luma (flow levels only)
std::vector<Img> flowLvl_; // per-level flow scratch
Img flowExpA_, flowExpB_; // fwd / bwd expanded flow (+conf)
// C2: TV-L1 ping-pong scratch at kFlowFinest res. flowLvl_[kFlowFinest] holds
// the fixed observation f0 (never written during the iterations); u ping-pongs
// between these two; the final iterate is copied back into flowLvl_[kFlowFinest]
// so of3_expand consumes the cleaned field with no binding change (⇒ skipping
// C2 leaves flowLvl_ byte-identical to pre-C2).
Img flowRegA_, flowRegB_;
// ── C1 global-motion state ────────────────────────────────────────────────
// Host-visible SSBOs the reduce shader writes per-thread partials into; the
// CPU sums + solves them. One per ring slot so a readback never races a live
// submission (the caller fence-waited its slot before calling record()).
VkBuffer gmSsbo_[kGmSlots] {};
VkDeviceMemory gmSsboMem_[kGmSlots]{};
void* gmSsboPtr_[kGmSlots]{};
bool gmBuffersReady_ = false;
// running affine params p1..p6 in centered-UV space (see GMUBO in config.hpp);
// identity = all zero. gmLinAt_ records the linearization params in effect when
// each slot's reduce was dispatched (the correct IC-update base for that slot).
double gmRun_[6] {};
double gmLinAt_[kGmSlots][6] {};
bool gmSlotValid_[kGmSlots]{};
bool gmHaveEst_ = false; // at least one good solve so far
int gmStableCount_ = 0; // consecutive good solves (auto-engage threshold)
bool gmEngaged_ = false; // is the affine being APPLIED this frame
float gmLastTransPx_ = 0.0f; // estimated global translation magnitude (full-res px), for logging
unsigned long long gmFrames_ = 0;
};
} // namespace winfg
+20
View File
@@ -0,0 +1,20 @@
// win-fg - logging. On Android goes to logcat (tag "win-fg") so device bring-up
// can trace the layer; elsewhere falls back to stderr.
#pragma once
#if defined(__ANDROID__)
#include <android/log.h>
#define WFG_LOGI(...) __android_log_print(ANDROID_LOG_INFO, "win-fg", __VA_ARGS__)
#define WFG_LOGE(...) __android_log_print(ANDROID_LOG_ERROR, "win-fg", __VA_ARGS__)
#else
#include <cstdio>
#define WFG_LOGI(...) do { std::fprintf(stderr, "[win-fg] " __VA_ARGS__); std::fprintf(stderr, "\n"); } while(0)
#define WFG_LOGE(...) do { std::fprintf(stderr, "[win-fg][E] " __VA_ARGS__); std::fprintf(stderr, "\n"); } while(0)
#endif
// Runtime-gated granular debug trace. The gate is a RUNTIME bool (Config::debug),
// not a compile-time switch - a shipping .so carries the full trail but pays only
// a predicted-not-taken bool test per step when debug is off (no logcat write, no
// arg evaluation of the message). Same "win-fg" tag so the app captures it under
// its own UID. Used to step the present path so a freeze leaves an obvious last
// line at the stage that hung. Usage: WFG_LOGD(dbg, "P#%llu step ...", n);
#define WFG_LOGD(cond, ...) do { if (cond) WFG_LOGI(__VA_ARGS__); } while(0)
@@ -0,0 +1,649 @@
// win-fg - FrameGen::record() and its lazy per-size descriptor/UBO setup.
// Included from framegen.cpp (has access to FrameGen privates). Written for
// win-fg; standard Vulkan compute wiring, no third-party code.
//
// ── C1 GLOBAL-MOTION PRE-WARP (this file wires it end to end) ─────────────────
// The dense SAD flow search melts on fast camera pans because the global motion
// saturates its window. C1 estimates the per-frame camera affine with a cheap
// inverse-compositional Lucas-Kanade solve (of3_gm_reduce accumulates the 6x6
// normal equations at 1/8 res → CPU 6x6 Cholesky here), then of3_gm_prewarp
// removes it from the curr luma pyramid BEFORE the search, so the search only
// sees small, coherent, object-only residual motion. of3_expand(_m4) composes
// the global affine back so wfg_synth is unchanged and the final warp is correct.
//
// DECOUPLED (stall-free) SCHEDULING: a true in-frame CPU solve would need a
// mid-frame GPU submit+readback stall - unacceptable on a GPU-bound title. So we
// read back the reduce SSBO the caller ALREADY fence-waited (its ring slot from a
// couple of presents ago) and run ONE Gauss-Newton step per frame, warm-started
// from the running estimate. Camera motion is temporally smooth, so a ~1-2 frame
// old affine is an excellent predictor; and because the SAME affine is removed
// (prewarp) and added back (expand), the result is EXACT for any affine we pick -
// an approximate/stale one only changes how much residual the search must absorb,
// never correctness. Identity affine ⇒ byte-identical to the pre-C1 pipeline, so
// the bail-to-identity safety path can never be worse than "no pre-warp".
namespace {
struct ScratchState {
// host-visible UBO buffers
VkBuffer synthUbo = VK_NULL_HANDLE; VkDeviceMemory synthUboMem = VK_NULL_HANDLE; void* synthUboPtr = nullptr;
std::vector<VkBuffer> flowUbo; std::vector<VkDeviceMemory> flowUboMem; std::vector<void*> flowUboPtr;
VkBuffer expandUbo = VK_NULL_HANDLE; VkDeviceMemory expandUboMem = VK_NULL_HANDLE; void* expandUboPtr = nullptr;
// C2: flow-regularization UBO (shared by all iterations; refreshed per frame).
VkBuffer flowRegUbo = VK_NULL_HANDLE; VkDeviceMemory flowRegUboMem = VK_NULL_HANDLE; void* flowRegUboPtr = nullptr;
// C1: per-slot global-motion UBO (double-buffered by ring slot so a live GPU
// read is never overwritten by the next frame - see the header note).
VkBuffer gmUbo[winfg::FrameGen::kGmSlots]{};
VkDeviceMemory gmUboMem[winfg::FrameGen::kGmSlots]{};
void* gmUboPtr[winfg::FrameGen::kGmSlots]{};
// persistent descriptor sets
VkDescriptorSet sLumaCurr = VK_NULL_HANDLE, sLumaPrev = VK_NULL_HANDLE;
std::vector<VkDescriptorSet> sDownA, sDownB, sFlow;
VkDescriptorSet sExpand[winfg::FrameGen::kGmSlots]{}; // per-slot (binds gmUbo[slot])
// C2: three flow-reg sets covering every (u_in → u_out) pairing of the
// ping-pong. sFlowReg0: in=flowLvl_[F] (=f0), out=A. sFlowRegAB: in=A, out=B.
// sFlowRegBA: in=B, out=A. f0 (binding 33) = flowLvl_[F] in all three.
VkDescriptorSet sFlowReg0 = VK_NULL_HANDLE, sFlowRegAB = VK_NULL_HANDLE, sFlowRegBA = VK_NULL_HANDLE;
VkDescriptorSet sSynth = VK_NULL_HANDLE;
// C1: per-slot reduce set + per-slot per-level prewarp sets
VkDescriptorSet sGmReduce[winfg::FrameGen::kGmSlots]{};
std::vector<VkDescriptorSet> sGmPrewarp[winfg::FrameGen::kGmSlots]; // sized kLevels; [flowFinest()..) used
VkExtent2D builtFor{0,0};
bool built = false;
bool flowInited = false; // flow images laid out once, then persist (temporal predictor)
};
ScratchState g_scratch; // one swapchain in flight in this first-draft engine
}
static bool makeUbo(const winfg::DeviceDispatch* dd, VkDevice dev,
const VkPhysicalDeviceMemoryProperties& mp,
VkDeviceSize size, VkBuffer& buf, VkDeviceMemory& mem, void*& ptr) {
VkBufferCreateInfo bi{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
bi.size = size; bi.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; bi.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
if (dd->CreateBuffer(dev, &bi, nullptr, &buf) != VK_SUCCESS) return false;
VkMemoryRequirements mr; dd->GetBufferMemoryRequirements(dev, buf, &mr);
uint32_t idx = 0; VkMemoryPropertyFlags want = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
for (uint32_t i = 0; i < mp.memoryTypeCount; ++i)
if ((mr.memoryTypeBits & (1u<<i)) && (mp.memoryTypes[i].propertyFlags & want) == want) { idx = i; break; }
VkMemoryAllocateInfo ai{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO}; ai.allocationSize = mr.size; ai.memoryTypeIndex = idx;
if (dd->AllocateMemory(dev, &ai, nullptr, &mem) != VK_SUCCESS) return false;
if (dd->BindBufferMemory(dev, buf, mem, 0) != VK_SUCCESS) return false;
return dd->MapMemory(dev, mem, 0, size, 0, &ptr) == VK_SUCCESS;
}
static VkDescriptorSet allocSet(const winfg::DeviceDispatch* dd, VkDevice dev,
VkDescriptorPool pool, VkDescriptorSetLayout layout) {
VkDescriptorSetAllocateInfo ai{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO};
ai.descriptorPool = pool; ai.descriptorSetCount = 1; ai.pSetLayouts = &layout;
VkDescriptorSet s = VK_NULL_HANDLE; dd->AllocateDescriptorSets(dev, &ai, &s); return s;
}
// write helpers
static void wImg(const winfg::DeviceDispatch* dd, VkDevice dev, VkDescriptorSet set, uint32_t binding,
VkDescriptorType type, VkImageView view, VkSampler samp, VkImageLayout layout) {
VkDescriptorImageInfo ii{}; ii.imageView = view; ii.sampler = samp; ii.imageLayout = layout;
VkWriteDescriptorSet w{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
w.dstSet = set; w.dstBinding = binding; w.descriptorCount = 1; w.descriptorType = type; w.pImageInfo = &ii;
dd->UpdateDescriptorSets(dev, 1, &w, 0, nullptr);
}
static void wUbo(const winfg::DeviceDispatch* dd, VkDevice dev, VkDescriptorSet set, uint32_t binding,
VkBuffer buf, VkDeviceSize range) {
VkDescriptorBufferInfo bi{}; bi.buffer = buf; bi.range = range;
VkWriteDescriptorSet w{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
w.dstSet = set; w.dstBinding = binding; w.descriptorCount = 1;
w.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; w.pBufferInfo = &bi;
dd->UpdateDescriptorSets(dev, 1, &w, 0, nullptr);
}
static void wSsbo(const winfg::DeviceDispatch* dd, VkDevice dev, VkDescriptorSet set, uint32_t binding,
VkBuffer buf, VkDeviceSize range) {
VkDescriptorBufferInfo bi{}; bi.buffer = buf; bi.range = range;
VkWriteDescriptorSet w{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
w.dstSet = set; w.dstBinding = binding; w.descriptorCount = 1;
w.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; w.pBufferInfo = &bi;
dd->UpdateDescriptorSets(dev, 1, &w, 0, nullptr);
}
// ── C1 CPU-side linear algebra (clean-room, textbook) ────────────────────────
// 6x6 symmetric positive-definite solve via Cholesky. Returns false if not PD
// (a bad/ill-conditioned frame → caller bails to identity). Damped by the caller.
static bool cholSolve6(double A[6][6], const double b[6], double x[6]) {
double L[6][6] = {{0}};
for (int i = 0; i < 6; ++i) {
for (int j = 0; j <= i; ++j) {
double s = A[i][j];
for (int k = 0; k < j; ++k) s -= L[i][k] * L[j][k];
if (i == j) {
if (s <= 1e-20) return false; // not positive-definite
L[i][j] = std::sqrt(s);
} else {
L[i][j] = s / L[j][j];
}
}
}
double y[6];
for (int i = 0; i < 6; ++i) { double s = b[i]; for (int k = 0; k < i; ++k) s -= L[i][k]*y[k]; y[i] = s / L[i][i]; }
for (int i = 5; i >= 0; --i) { double s = y[i]; for (int k = i+1; k < 6; ++k) s -= L[k][i]*x[k]; x[i] = s / L[i][i]; }
return true;
}
// affine <-> params. params p=(p1,p2,p3,p4,p5,p6); M = I + [[p1,p3,p5],[p2,p4,p6]].
static void affFromParams(const double p[6], double M[3][3]) {
M[0][0]=1.0+p[0]; M[0][1]=p[2]; M[0][2]=p[4];
M[1][0]=p[1]; M[1][1]=1.0+p[3]; M[1][2]=p[5];
M[2][0]=0.0; M[2][1]=0.0; M[2][2]=1.0;
}
static void paramsFromAff(const double M[3][3], double p[6]) {
p[0]=M[0][0]-1.0; p[2]=M[0][1]; p[4]=M[0][2];
p[1]=M[1][0]; p[3]=M[1][1]-1.0; p[5]=M[1][2];
}
static bool invAff(const double M[3][3], double out[3][3]) {
double a=M[0][0], b=M[0][1], tx=M[0][2], c=M[1][0], d=M[1][1], ty=M[1][2];
double det = a*d - b*c;
if (std::fabs(det) < 1e-12) return false;
double id = 1.0/det;
out[0][0]= d*id; out[0][1]= -b*id; out[0][2]= (b*ty - d*tx)*id;
out[1][0]= -c*id; out[1][1]= a*id; out[1][2]= (c*tx - a*ty)*id;
out[2][0]=0.0; out[2][1]=0.0; out[2][2]=1.0;
return true;
}
static void mulAff(const double A[3][3], const double B[3][3], double out[3][3]) {
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 3; ++j)
out[i][j] = A[i][0]*B[0][j] + A[i][1]*B[1][j] + A[i][2]*B[2][j];
out[2][0]=0.0; out[2][1]=0.0; out[2][2]=1.0;
}
namespace winfg {
// Lazily allocate per-size UBOs + descriptor sets and wire the fixed (internal-
// resource) bindings once. External views (prev/curr/out) are wired per frame.
static bool ensureScratch(FrameGen* fg, const DeviceDispatch* dd, VkDevice dev,
const VkPhysicalDeviceMemoryProperties& mp, VkDescriptorPool pool,
VkSampler samp, VkExtent2D ext,
std::vector<FrameGen::Img>& pyrA, std::vector<FrameGen::Img>& pyrB,
std::vector<FrameGen::Img>& pyrAs,
std::vector<FrameGen::Img>& flowLvl, FrameGen::Img& expA, FrameGen::Img& expB,
const FrameGen::Pipe& pLuma, const FrameGen::Pipe& pDown,
const FrameGen::Pipe& pFlow, const FrameGen::Pipe& pExpand,
const FrameGen::Pipe& pSynth, const FrameGen::Pipe& pGmReduce,
const FrameGen::Pipe& pGmPrewarp, const FrameGen::Pipe& pFlowReg,
FrameGen::Img& flowRegA, FrameGen::Img& flowRegB, VkBuffer* gmSsbo,
int kLevels, const Config& cfg);
// Tear down the lazily-built scratch (all UBO buffers + every descriptor set) so a
// live perf_preset rebuild neither leaks them nor exhausts the descriptor pool.
// The CALLER must have made the device idle first - no in-flight command buffer may
// still reference these. Resetting the pool frees every set FrameGen allocated (all
// sets come from this pool); the mapped UBO memory is unmapped + freed explicitly.
static void destroyScratch(const DeviceDispatch* dd, VkDevice dev, VkDescriptorPool pool) {
auto freeUbo = [&](VkBuffer& b, VkDeviceMemory& m, void*& p) {
if (m) dd->UnmapMemory(dev, m);
if (b) dd->DestroyBuffer(dev, b, nullptr);
if (m) dd->FreeMemory(dev, m, nullptr);
b = VK_NULL_HANDLE; m = VK_NULL_HANDLE; p = nullptr;
};
auto& S = g_scratch;
freeUbo(S.synthUbo, S.synthUboMem, S.synthUboPtr);
freeUbo(S.expandUbo, S.expandUboMem, S.expandUboPtr);
freeUbo(S.flowRegUbo, S.flowRegUboMem, S.flowRegUboPtr);
for (size_t i = 0; i < S.flowUbo.size(); ++i) freeUbo(S.flowUbo[i], S.flowUboMem[i], S.flowUboPtr[i]);
for (int s = 0; s < FrameGen::kGmSlots; ++s) freeUbo(S.gmUbo[s], S.gmUboMem[s], S.gmUboPtr[s]);
if (pool && dd->ResetDescriptorPool) dd->ResetDescriptorPool(dev, pool, 0); // reclaim all sets at once
S = ScratchState{}; // built=false -> next record() rebuilds via ensureScratch
}
// ── perf_preset apply / live rebuild ─────────────────────────────────────────
// Sets the config, resolves the perf_preset -> finest-solved-level, and - when
// that level changed while resources are already live - safely rebuilds the
// affected per-size flow images (a resize) + scratch descriptor sets and resets
// the flow predictor. This makes a perf_preset change FULLY LIVE per-present (the
// conf.toml hot-reload path calls this), so no FG toggle / bg-fg cycle is needed.
void FrameGen::configure(const Config& c) {
cfg_ = c; cfg_.sanitize();
// Map preset -> finest level, clamped so at least TWO solved levels always
// remain (coarse->fine needs a range). With kLevels=7 the presets (1/2/3) never
// hit the clamp; the guard is defensive for future level/preset tuning.
int want = flowFinestForPreset(cfg_.perfPreset);
const int maxFinest = kLevels - 2; // keep >= 2 solved levels
if (want > maxFinest) {
WFG_LOGE("perf-preset=%d wants kFlowFinest=%d but only %d pyramid levels exist "
"(need >=2 solved) - clamping to %d", cfg_.perfPreset, want, kLevels, maxFinest);
want = maxFinest;
}
if (want < 1) want = 1; // never solve full-res (level 0)
const int prevFinest = flowFinest_;
flowFinest_ = want;
const bool live = (flowFinest_ != prevFinest) && ready_;
WFG_LOGI("perf-preset=%d -> kFlowFinest=%d (was %d) live-rebuild=%d ready=%d",
cfg_.perfPreset, flowFinest_, prevFinest, live ? 1 : 0, ready_ ? 1 : 0);
if (!live) return; // unchanged, or not yet built (initial configure before onResize)
// The per-size flow images (pyrAs_/flowReg*/flowLvl_ wiring) and the scratch
// descriptor sets were built for the OLD finest level. Rebuild them so record()
// never reads/writes mismatched-size images.
dd_->DeviceWaitIdle(device_); // no in-flight cmd buf may touch these
destroyScratch(dd_, device_, descPool_); // free old scratch (UBOs + all sets)
if (!onResize(extent_, VK_FORMAT_UNDEFINED, /*force=*/true))
WFG_LOGE("perf-preset live rebuild: onResize FAILED at %ux%u - FG disabled until next resize",
extent_.width, extent_.height);
// Reset the persistent flow predictor so stale, differently-scaled state never
// feeds the new resolution (avoids a melt/garbage frame on switch). The per-frame
// flow field (flowLvl_) is already cleared every frame; the cross-frame state is
// the C1 global-motion estimate + its per-slot reduce partials (dispatched against
// the old pyramid) - drop both back to identity.
for (int i = 0; i < 6; ++i) gmRun_[i] = 0.0;
for (int s = 0; s < kGmSlots; ++s) { gmSlotValid_[s] = false; for (int i = 0; i < 6; ++i) gmLinAt_[s][i] = 0.0; }
gmHaveEst_ = false; gmEngaged_ = false; gmStableCount_ = 0; gmLastTransPx_ = 0.0f;
WFG_LOGI("perf-preset live rebuild DONE (kFlowFinest=%d, flow predictor reset)", flowFinest_);
}
// ── C1: read back one reduce SSBO, run one inverse-compositional LK step ──────
// Updates the running/applied global affine. Returns whether GM engages this frame.
bool FrameGen::updateGlobalMotion(uint32_t gmSlot, VkExtent2D fullExtent) {
++gmFrames_;
if (!gmBuffersReady_ || cfg_.gmMode == 2 /*off*/) {
// off: keep identity applied; keep estimating cheaply is pointless when off.
gmEngaged_ = false; gmStableCount_ = 0;
// still refresh the linearization record so a live switch to auto starts clean
for (int i = 0; i < 6; ++i) gmRun_[i] = 0.0;
for (int i = 0; i < 6; ++i) gmLinAt_[gmSlot][i] = gmRun_[i];
gmLastTransPx_ = 0.0f;
return false;
}
bool good = false;
if (gmSlotValid_[gmSlot]) {
const float* pt = (const float*)gmSsboPtr_[gmSlot];
double sums[32]; for (int k = 0; k < 32; ++k) sums[k] = 0.0;
for (int t = 0; t < kGmThreads; ++t) {
const float* row = pt + (size_t)t * kGmStride;
for (int k = 0; k < kGmStride; ++k) sums[k] += (double)row[k];
}
double coverage = sums[27];
// require a few percent of the level's pixels as inliers
double lvlpx = double((fullExtent.width >> kGmLevel) * (fullExtent.height >> kGmLevel));
double minCov = 0.03 * (lvlpx > 0 ? lvlpx : 1.0);
if (coverage > minCov && coverage > 64.0) {
double H[6][6]; int idx = 0;
for (int r = 0; r < 6; ++r) for (int c = r; c < 6; ++c) { H[r][c] = H[c][r] = sums[idx]; ++idx; }
double b[6]; for (int i = 0; i < 6; ++i) b[i] = sums[21 + i];
// Levenberg damping for conditioning (relative + tiny absolute).
for (int i = 0; i < 6; ++i) H[i][i] += 1e-3 * H[i][i] + 1e-9;
double dp[6];
if (cholSolve6(H, b, dp)) {
// reject wild steps (something non-affine / a scene cut)
bool bounded = std::fabs(dp[0]) < 0.5 && std::fabs(dp[1]) < 0.5 &&
std::fabs(dp[2]) < 0.5 && std::fabs(dp[3]) < 0.5 &&
std::fabs(dp[4]) < 0.25 && std::fabs(dp[5]) < 0.25;
if (bounded) {
// IC update: M_new = M_lin ∘ W(dp)^-1 (compose onto the SLOT's
// linearization base, which is what this reduce linearized around).
double Mlin[3][3], Md[3][3], Mdi[3][3], Mnew[3][3];
affFromParams(gmLinAt_[gmSlot], Mlin);
affFromParams(dp, Md);
if (invAff(Md, Mdi)) {
mulAff(Mlin, Mdi, Mnew);
double pnew[6]; paramsFromAff(Mnew, pnew);
// final sanity clamp on the composed estimate
bool sane = std::fabs(pnew[0]) < 0.6 && std::fabs(pnew[1]) < 0.6 &&
std::fabs(pnew[2]) < 0.6 && std::fabs(pnew[3]) < 0.6 &&
std::fabs(pnew[4]) < 0.5 && std::fabs(pnew[5]) < 0.5;
if (sane) { for (int i = 0; i < 6; ++i) gmRun_[i] = pnew[i]; good = true; gmHaveEst_ = true; }
}
}
}
}
}
if (good) ++gmStableCount_; else gmStableCount_ = 0;
// engage policy
switch (cfg_.gmMode) {
case 1: gmEngaged_ = gmHaveEst_; break; // on
case 2: gmEngaged_ = false; break; // off (unreachable; handled above)
default: gmEngaged_ = (gmStableCount_ >= 3); break; // auto: 3 consecutive good solves
}
// record the linearization base for the reduce we are about to dispatch into
// this slot this frame (i.e. the current running estimate).
for (int i = 0; i < 6; ++i) gmLinAt_[gmSlot][i] = gmRun_[i];
gmSlotValid_[gmSlot] = true; // this frame's reduce will (re)write it
gmLastTransPx_ = (float)std::sqrt(
(gmRun_[4]*fullExtent.width) * (gmRun_[4]*fullExtent.width) +
(gmRun_[5]*fullExtent.height) * (gmRun_[5]*fullExtent.height));
return gmEngaged_;
}
void FrameGen::record(VkCommandBuffer cmd, VkImageView prevView, VkImageView currView,
VkImageView outView, float alpha, uint32_t gmSlot) {
if (!valid()) return;
if (gmSlot >= (uint32_t)kGmSlots) gmSlot = 0;
const bool m4 = (cfg_.model == 4);
const Pipe& flowP = m4 ? pFlowM4_ : pFlow_;
const Pipe& expandP = m4 ? pExpandM4_ : pExpand_;
if (!g_scratch.built || g_scratch.builtFor.width != extent_.width || g_scratch.builtFor.height != extent_.height) {
ensureScratch(this, dd_, device_, memProps_, descPool_, linear_, extent_,
pyrA_, pyrB_, pyrAs_, flowLvl_, flowExpA_, flowExpB_,
pLuma_, pDown_, flowP, expandP, pSynth_, pGmReduce_, pGmPrewarp_,
pFlowReg_, flowRegA_, flowRegB_, gmSsbo_, kLevels, cfg_);
}
// ── C1: solve the global-motion affine (reads the SSBO the caller fence-waited) ──
bool gmEngaged = updateGlobalMotion(gmSlot, extent_);
// Build this slot's GMUBO from the running / applied affine.
if (g_scratch.gmUboPtr[gmSlot]) {
GMUBO g{};
g.lin[0]=(float)gmRun_[0]; g.lin[1]=(float)gmRun_[1]; g.lin[2]=(float)gmRun_[2]; g.lin[3]=(float)gmRun_[3];
g.linT[0]=(float)gmRun_[4]; g.linT[1]=(float)gmRun_[5]; g.linT[2]=gmEngaged?1.0f:0.0f; g.linT[3]=0.0f;
if (gmEngaged) {
g.app[0]=(float)gmRun_[0]; g.app[1]=(float)gmRun_[1]; g.app[2]=(float)gmRun_[2]; g.app[3]=(float)gmRun_[3];
g.appT[0]=(float)gmRun_[4]; g.appT[1]=(float)gmRun_[5];
} // else app/appT stay 0 -> identity -> prewarp/expand are pre-C1 no-ops
std::memcpy(g_scratch.gmUboPtr[gmSlot], &g, sizeof(g));
}
// Log engage state + estimated global translation periodically (tag win-fg).
{
static int lastEng = -1;
if (gmEngaged != (lastEng == 1) || (gmFrames_ % 300ull) == 0) {
WFG_LOGI("C1 global-motion: engaged=%d mode=%d stable=%d transPx=%.1f (model=%d)",
gmEngaged ? 1 : 0, cfg_.gmMode, gmStableCount_, gmLastTransPx_, cfg_.model);
lastEng = gmEngaged ? 1 : 0;
}
}
// update UBOs for this frame
SynthUBO su{cfg_.flowScale, alpha, cfg_.beta, cfg_.lambda, cfg_.epsilon, cfg_.photoScale, 0, 0,
cfg_.hudX0, cfg_.hudY0, cfg_.hudX1, cfg_.hudY1};
if (g_scratch.synthUboPtr) std::memcpy(g_scratch.synthUboPtr, &su, sizeof(su));
// wire per-frame external views
wImg(dd_, device_, g_scratch.sLumaCurr, 32, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, currView, linear_, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
wImg(dd_, device_, g_scratch.sLumaPrev, 32, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, prevView, linear_, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
wImg(dd_, device_, g_scratch.sSynth, 32, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, prevView, linear_, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
wImg(dd_, device_, g_scratch.sSynth, 33, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, currView, linear_, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
wImg(dd_, device_, g_scratch.sSynth, 48, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, outView, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL);
auto disp = [&](const Pipe& p, VkDescriptorSet set, VkExtent2D e) {
dd_->CmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, p.pipeline);
dd_->CmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, p.layout, 0, 1, &set, 0, nullptr);
dd_->CmdDispatch(cmd, (e.width + 15) / 16, (e.height + 15) / 16, 1);
};
auto bar = [&](FrameGen::Img& i){ barrier(cmd, i.image, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_GENERAL); };
// Internal images are created UNDEFINED and fully recomputed every frame, so
// discard-transition them all to GENERAL up front (all descriptors use GENERAL
// for these). This also fixes the first-frame layout (storage writes require
// GENERAL, not UNDEFINED).
for (auto& i : pyrA_) barrier(cmd, i.image, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL);
for (auto& i : pyrB_) barrier(cmd, i.image, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL);
for (int l = flowFinest_; l < kLevels; ++l) barrier(cmd, pyrAs_[l].image, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL);
barrier(cmd, flowExpA_.image, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL);
barrier(cmd, flowExpB_.image, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL);
// C2: ping-pong scratch to GENERAL (storage writes require it; contents discarded).
barrier(cmd, flowRegA_.image, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL);
barrier(cmd, flowRegB_.image, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL);
// Reset the flow field to zero every frame. The coarsest level uses it as its
// search predictor; carrying it across frames lets a spurious vector feed
// forward until it diverges and melts the image (the bg/fg pause was resetting
// it - that's why artifacts vanished on resume). Fresh zero every frame =
// stable, standard pyramid flow, no feedback loop.
{
VkClearColorValue zero{};
VkImageSubresourceRange rng{VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
for (int l = flowFinest_; l < kLevels; ++l) {
VkImageMemoryBarrier b{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER};
b.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; b.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
b.srcAccessMask = 0; b.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
b.image = flowLvl_[l].image; b.subresourceRange = rng;
b.srcQueueFamilyIndex = b.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
dd_->CmdPipelineBarrier(cmd, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &b);
dd_->CmdClearColorImage(cmd, flowLvl_[l].image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &zero, 1, &rng);
b.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; b.newLayout = VK_IMAGE_LAYOUT_GENERAL;
b.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; b.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT;
dd_->CmdPipelineBarrier(cmd, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &b);
}
}
// 1) luma of curr/prev at full res
disp(pLuma_, g_scratch.sLumaCurr, levelExtent(extent_, 0));
disp(pLuma_, g_scratch.sLumaPrev, levelExtent(extent_, 0));
bar(pyrA_[0]); bar(pyrB_[0]);
// 2) downsample pyramids
for (int l = 1; l < kLevels; ++l) {
disp(pDown_, g_scratch.sDownA[l - 1], levelExtent(extent_, l));
disp(pDown_, g_scratch.sDownB[l - 1], levelExtent(extent_, l));
bar(pyrA_[l]); bar(pyrB_[l]);
}
// 2b) C1: LK normal-equation reduce (fixed grid, 1/8-res) → this slot's SSBO.
// Reads raw prev/curr luma at kGmLevel; writes per-thread partials. A
// compute→host barrier makes the write visible to the CPU readback that
// happens when this slot comes round again (~1-2 frames later).
if (gmBuffersReady_ && g_scratch.sGmReduce[gmSlot]) {
dd_->CmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, pGmReduce_.pipeline);
dd_->CmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, pGmReduce_.layout, 0, 1, &g_scratch.sGmReduce[gmSlot], 0, nullptr);
dd_->CmdDispatch(cmd, (uint32_t)(kGmThreads / 64), 1, 1);
VkMemoryBarrier mb{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
mb.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT; mb.dstAccessMask = VK_ACCESS_HOST_READ_BIT;
dd_->CmdPipelineBarrier(cmd, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_HOST_BIT,
0, 1, &mb, 0, nullptr, 0, nullptr);
}
// 2c) C1: pre-warp curr luma pyramid by the applied affine → stabilized pyramid
// (identity affine ⇒ exact copy, so the flow below is unchanged when
// disengaged). The dense flow reads pyrAs from here on.
for (int l = flowFinest_; l < kLevels; ++l) {
disp(pGmPrewarp_, g_scratch.sGmPrewarp[gmSlot][l], levelExtent(extent_, l));
bar(pyrAs_[l]);
}
// 3) coarse->fine flow (only down to flowFinest_ - reduced res for perf; the
// finest level is perf_preset-selectable). Curr input is now the STABILIZED
// pyramid (pyrAs), wired in ensureScratch.
for (int l = kLevels - 1; l >= flowFinest_; --l) {
disp(flowP, g_scratch.sFlow[l], levelExtent(extent_, l));
bar(flowLvl_[l]);
}
// 3b) ── C2: TV-L1 flow regularization on the finest residual field ──────────
// Run frIters semi-implicit TV-L1 denoising iterations on flowLvl_[flowFinest_]
// (the object-only residual left by C1). flowLvl_[F] is the fixed data term f0
// (read, never written here); u ping-pongs A<->B; the final iterate is copied
// back into flowLvl_[F] so of3_expand consumes the cleaned field unchanged.
// Disengaged (frMode==off or frIters==0) ⇒ flowLvl_[F] untouched ⇒ byte-identical
// to pre-C2. Identity/zero iterations is therefore never worse than "no reg".
const bool frEngaged = (cfg_.frMode != 2) && (cfg_.frIters > 0);
if (frEngaged) {
const int F = flowFinest_;
const VkExtent2D fe = levelExtent(extent_, F);
if (g_scratch.flowRegUboPtr) {
FlowRegUBO fu{cfg_.frDt, cfg_.frLambda, cfg_.frEdge, cfg_.frEps};
std::memcpy(g_scratch.flowRegUboPtr, &fu, sizeof(fu));
}
FrameGen::Img* last = nullptr;
for (int it = 0; it < cfg_.frIters; ++it) {
VkDescriptorSet set; FrameGen::Img* outImg;
if (it == 0) { set = g_scratch.sFlowReg0; outImg = &flowRegA_; }
else if (it % 2 == 1) { set = g_scratch.sFlowRegAB; outImg = &flowRegB_; }
else { set = g_scratch.sFlowRegBA; outImg = &flowRegA_; }
dd_->CmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, pFlowReg_.pipeline);
dd_->CmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, pFlowReg_.layout, 0, 1, &set, 0, nullptr);
dd_->CmdDispatch(cmd, (fe.width + 15) / 16, (fe.height + 15) / 16, 1);
bar(*outImg);
last = outImg;
}
// Copy the cleaned finest flow back into flowLvl_[F] (GENERAL->GENERAL).
// Barrier: C2's f0 reads (SHADER_READ of flowLvl_[F]) and the last iterate
// write must complete before the transfer overwrites/reads.
VkMemoryBarrier mb{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
mb.srcAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT;
mb.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT;
dd_->CmdPipelineBarrier(cmd, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
0, 1, &mb, 0, nullptr, 0, nullptr);
VkImageCopy region{};
region.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
region.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
region.extent = {fe.width, fe.height, 1};
dd_->CmdCopyImage(cmd, last->image, VK_IMAGE_LAYOUT_GENERAL,
flowLvl_[F].image, VK_IMAGE_LAYOUT_GENERAL, 1, &region);
VkMemoryBarrier mb2{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
mb2.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
mb2.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
dd_->CmdPipelineBarrier(cmd, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
0, 1, &mb2, 0, nullptr, 0, nullptr);
}
{ // periodic C2 status (tag win-fg), mirrors the C1 log cadence
static int lastFr = -1;
int fe = frEngaged ? 1 : 0;
if (fe != lastFr || (gmFrames_ % 300ull) == 0) {
WFG_LOGI("C2 flow-reg: engaged=%d mode=%d iters=%d lambda=%.2f dt=%.2f edge=%.1f eps=%.3f (model=%d)",
fe, cfg_.frMode, cfg_.frIters, cfg_.frLambda, cfg_.frDt, cfg_.frEdge, cfg_.frEps, cfg_.model);
lastFr = fe;
}
}
// 4) expand finest solved flow -> fwd/bwd (+conf), + C1 global-motion add-back.
disp(expandP, g_scratch.sExpand[gmSlot], levelExtent(extent_, 0));
bar(flowExpA_); bar(flowExpB_);
// 5) synthesise interpolated frame
disp(pSynth_, g_scratch.sSynth, levelExtent(extent_, 0));
}
// ---- lazy scratch builder ----
static bool ensureScratch(FrameGen* fg, const DeviceDispatch* dd, VkDevice dev,
const VkPhysicalDeviceMemoryProperties& mp, VkDescriptorPool pool,
VkSampler samp, VkExtent2D ext,
std::vector<FrameGen::Img>& pyrA, std::vector<FrameGen::Img>& pyrB,
std::vector<FrameGen::Img>& pyrAs,
std::vector<FrameGen::Img>& flowLvl, FrameGen::Img& expA, FrameGen::Img& expB,
const FrameGen::Pipe& pLuma, const FrameGen::Pipe& pDown,
const FrameGen::Pipe& pFlow, const FrameGen::Pipe& pExpand,
const FrameGen::Pipe& pSynth, const FrameGen::Pipe& pGmReduce,
const FrameGen::Pipe& pGmPrewarp, const FrameGen::Pipe& pFlowReg,
FrameGen::Img& flowRegA, FrameGen::Img& flowRegB, VkBuffer* gmSsbo,
int kLevels, const Config& cfg) {
auto& S = g_scratch;
S = ScratchState{};
const auto GEN = VK_IMAGE_LAYOUT_GENERAL;
makeUbo(dd, dev, mp, sizeof(SynthUBO), S.synthUbo, S.synthUboMem, S.synthUboPtr);
makeUbo(dd, dev, mp, sizeof(FlowUBO), S.expandUbo, S.expandUboMem, S.expandUboPtr);
if (S.expandUboPtr) { FlowUBO u{cfg.flowScale, 0, 0.6f, 1.6f}; std::memcpy(S.expandUboPtr, &u, sizeof(u)); }
// C2: flow-reg UBO (params refreshed each frame in record() for hot-reload).
makeUbo(dd, dev, mp, sizeof(FlowRegUBO), S.flowRegUbo, S.flowRegUboMem, S.flowRegUboPtr);
if (S.flowRegUboPtr) { FlowRegUBO fu{cfg.frDt, cfg.frLambda, cfg.frEdge, cfg.frEps}; std::memcpy(S.flowRegUboPtr, &fu, sizeof(fu)); }
S.flowUbo.resize(kLevels); S.flowUboMem.resize(kLevels); S.flowUboPtr.resize(kLevels);
for (int l = 0; l < kLevels; ++l) {
makeUbo(dd, dev, mp, sizeof(FlowUBO), S.flowUbo[l], S.flowUboMem[l], S.flowUboPtr[l]);
if (S.flowUboPtr[l]) { FlowUBO u{cfg.flowScale, (uint32_t)l, 0.6f, 1.6f}; std::memcpy(S.flowUboPtr[l], &u, sizeof(u)); }
}
// C1: per-slot GMUBO (initialised to identity)
for (int s = 0; s < FrameGen::kGmSlots; ++s) {
makeUbo(dd, dev, mp, sizeof(GMUBO), S.gmUbo[s], S.gmUboMem[s], S.gmUboPtr[s]);
if (S.gmUboPtr[s]) { GMUBO g{}; std::memcpy(S.gmUboPtr[s], &g, sizeof(g)); }
}
// luma sets (external src wired per frame; dst is pyramid[0])
S.sLumaCurr = allocSet(dd, dev, pool, pLuma.setLayout);
S.sLumaPrev = allocSet(dd, dev, pool, pLuma.setLayout);
wImg(dd, dev, S.sLumaCurr, 48, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, pyrA[0].view, VK_NULL_HANDLE, GEN);
wImg(dd, dev, S.sLumaPrev, 48, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, pyrB[0].view, VK_NULL_HANDLE, GEN);
// downsample sets (fixed)
S.sDownA.resize(kLevels - 1); S.sDownB.resize(kLevels - 1);
for (int l = 1; l < kLevels; ++l) {
S.sDownA[l-1] = allocSet(dd, dev, pool, pDown.setLayout);
S.sDownB[l-1] = allocSet(dd, dev, pool, pDown.setLayout);
wImg(dd, dev, S.sDownA[l-1], 32, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, pyrA[l-1].view, samp, GEN);
wImg(dd, dev, S.sDownA[l-1], 48, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, pyrA[l].view, VK_NULL_HANDLE, GEN);
wImg(dd, dev, S.sDownB[l-1], 32, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, pyrB[l-1].view, samp, GEN);
wImg(dd, dev, S.sDownB[l-1], 48, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, pyrB[l].view, VK_NULL_HANDLE, GEN);
}
// flow sets (fixed): predictor = coarser flow level, or self at coarsest.
// C1: the CURR input (binding 33) is the STABILIZED pyramid pyrAs for the
// solved flow levels; prev stays raw. Below kFlowFinest the level is never
// dispatched, so leaving those at raw curr is harmless.
S.sFlow.resize(kLevels);
for (int l = 0; l < kLevels; ++l) {
S.sFlow[l] = allocSet(dd, dev, pool, pFlow.setLayout);
int pred = (l + 1 < kLevels) ? l + 1 : l;
VkImageView currLvl = (l >= fg->flowFinest()) ? pyrAs[l].view : pyrA[l].view;
wUbo(dd, dev, S.sFlow[l], 0, S.flowUbo[l], sizeof(FlowUBO));
wImg(dd, dev, S.sFlow[l], 32, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, pyrB[l].view, samp, GEN);
wImg(dd, dev, S.sFlow[l], 33, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, currLvl, samp, GEN);
wImg(dd, dev, S.sFlow[l], 34, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, flowLvl[pred].view, samp, GEN);
wImg(dd, dev, S.sFlow[l], 48, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, flowLvl[l].view, VK_NULL_HANDLE, GEN);
}
// C1: reduce sets (one per slot) - prev/curr raw luma at kGmLevel + SSBO + GMUBO.
if (gmSsbo) {
for (int s = 0; s < FrameGen::kGmSlots; ++s) {
if (!gmSsbo[s]) continue;
S.sGmReduce[s] = allocSet(dd, dev, pool, pGmReduce.setLayout);
wUbo (dd, dev, S.sGmReduce[s], 1, S.gmUbo[s], sizeof(GMUBO));
wSsbo(dd, dev, S.sGmReduce[s], 16, gmSsbo[s], VK_WHOLE_SIZE);
wImg (dd, dev, S.sGmReduce[s], 32, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, pyrB[FrameGen::kGmLevel].view, samp, GEN); // prev = template
wImg (dd, dev, S.sGmReduce[s], 33, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, pyrA[FrameGen::kGmLevel].view, samp, GEN); // curr = image
}
}
// C1: prewarp sets (one per slot, per solved flow level) - raw curr in, stabilized out.
for (int s = 0; s < FrameGen::kGmSlots; ++s) {
S.sGmPrewarp[s].assign(kLevels, VK_NULL_HANDLE);
for (int l = fg->flowFinest(); l < kLevels; ++l) {
S.sGmPrewarp[s][l] = allocSet(dd, dev, pool, pGmPrewarp.setLayout);
wUbo(dd, dev, S.sGmPrewarp[s][l], 1, S.gmUbo[s], sizeof(GMUBO));
wImg(dd, dev, S.sGmPrewarp[s][l], 32, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, pyrA[l].view, samp, GEN);
wImg(dd, dev, S.sGmPrewarp[s][l], 48, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, pyrAs[l].view, VK_NULL_HANDLE, GEN);
}
}
// expand sets (one per slot): flow inputs fixed; binding 1 = that slot's GMUBO.
for (int s = 0; s < FrameGen::kGmSlots; ++s) {
S.sExpand[s] = allocSet(dd, dev, pool, pExpand.setLayout);
wUbo(dd, dev, S.sExpand[s], 0, S.expandUbo, sizeof(FlowUBO));
wUbo(dd, dev, S.sExpand[s], 1, S.gmUbo[s], sizeof(GMUBO));
wImg(dd, dev, S.sExpand[s], 32, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, flowLvl[fg->flowFinest()].view, samp, GEN);
wImg(dd, dev, S.sExpand[s], 48, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, expA.view, VK_NULL_HANDLE, GEN);
wImg(dd, dev, S.sExpand[s], 49, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, expB.view, VK_NULL_HANDLE, GEN);
}
// C2: flow-reg sets. F = kFlowFinest. f0 (binding 33) = flowLvl_[F] in all; luma
// (binding 34) = prev luma at F (pyrB) for the edge-aware weight. u_in (32) and
// u_out (48) alternate to realize the ping-pong (see ScratchState comment).
{
const int F = fg->flowFinest();
S.sFlowReg0 = allocSet(dd, dev, pool, pFlowReg.setLayout);
S.sFlowRegAB = allocSet(dd, dev, pool, pFlowReg.setLayout);
S.sFlowRegBA = allocSet(dd, dev, pool, pFlowReg.setLayout);
VkDescriptorSet all3[3] = {S.sFlowReg0, S.sFlowRegAB, S.sFlowRegBA};
for (VkDescriptorSet s : all3) {
wUbo(dd, dev, s, 0, S.flowRegUbo, sizeof(FlowRegUBO));
wImg(dd, dev, s, 33, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, flowLvl[F].view, samp, GEN); // f0
wImg(dd, dev, s, 34, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, pyrB[F].view, samp, GEN); // luma
}
// iter0: in=flowLvl_[F] (=f0), out=A
wImg(dd, dev, S.sFlowReg0, 32, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, flowLvl[F].view, samp, GEN);
wImg(dd, dev, S.sFlowReg0, 48, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, flowRegA.view, VK_NULL_HANDLE, GEN);
// odd iters: in=A, out=B
wImg(dd, dev, S.sFlowRegAB, 32, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, flowRegA.view, samp, GEN);
wImg(dd, dev, S.sFlowRegAB, 48, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, flowRegB.view, VK_NULL_HANDLE, GEN);
// even iters (>0): in=B, out=A
wImg(dd, dev, S.sFlowRegBA, 32, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, flowRegB.view, samp, GEN);
wImg(dd, dev, S.sFlowRegBA, 48, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, flowRegA.view, VK_NULL_HANDLE, GEN);
}
// synth set: external prev/curr/out wired per frame; flow inputs fixed
S.sSynth = allocSet(dd, dev, pool, pSynth.setLayout);
wUbo(dd, dev, S.sSynth, 0, S.synthUbo, sizeof(SynthUBO));
// GENERAL (not SR): these internal flow images stay in GENERAL across the whole
// record() (written as storage by expand, sampled here) - see the discard
// transition at the top of record().
wImg(dd, dev, S.sSynth, 34, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, expA.view, samp, GEN);
wImg(dd, dev, S.sSynth, 35, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, expB.view, samp, GEN);
S.builtFor = ext; S.built = true;
return true;
}
} // namespace winfg
@@ -0,0 +1,123 @@
// win-fg - minimal Vulkan layer dispatch tables.
// Structure follows the public Khronos loader/layer interface and the
// renderdoc "Vulkan layer guide" skeleton (learning references, Apache-2.0 /
// CC-BY). No code from bionic-fg / lsfg / GameScope.
#pragma once
#include <vulkan/vulkan.h>
#include <vulkan/vk_layer.h>
#include <cstring>
namespace winfg {
// The loader sets the first pointer-sized word of a dispatchable handle to the
// dispatch table pointer; layers key their own maps on that value so that all
// handles sharing a chain resolve to the same table.
static inline void* dispatch_key(void* handle) { return *reinterpret_cast<void**>(handle); }
// Instance-level functions we call from the layer.
struct InstanceDispatch {
PFN_vkGetInstanceProcAddr GetInstanceProcAddr = nullptr;
PFN_vkDestroyInstance DestroyInstance = nullptr;
PFN_vkGetPhysicalDeviceMemoryProperties GetPhysicalDeviceMemoryProperties = nullptr;
PFN_vkGetPhysicalDeviceQueueFamilyProperties GetPhysicalDeviceQueueFamilyProperties = nullptr;
// Device-context logging (affected-device fingerprint). Properties2 is core 1.1;
// may be null on a 1.0 instance - always null-check before calling.
PFN_vkGetPhysicalDeviceProperties GetPhysicalDeviceProperties = nullptr;
PFN_vkGetPhysicalDeviceProperties2 GetPhysicalDeviceProperties2 = nullptr;
// Surface caps - used to clamp the extra-image-headroom request to the surface's
// maxImageCount so the bump never exceeds what the driver can grant.
PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR GetPhysicalDeviceSurfaceCapabilitiesKHR = nullptr;
};
// Device-level functions we call. Populated by walking vkGetDeviceProcAddr on
// the next layer down at device-create time.
struct DeviceDispatch {
PFN_vkGetDeviceProcAddr GetDeviceProcAddr = nullptr;
PFN_vkDestroyDevice DestroyDevice = nullptr;
PFN_vkGetDeviceQueue GetDeviceQueue = nullptr;
PFN_vkQueueSubmit QueueSubmit = nullptr;
// Synchronization2 submit path (core 1.3 + KHR alias). DXVK may use either the
// legacy QueueSubmit or QueueSubmit2/2KHR; both are hooked for the guest-submit
// diagnostic. May be null if the device did not enable them - always null-check.
PFN_vkQueueSubmit2 QueueSubmit2 = nullptr;
PFN_vkQueueSubmit2KHR QueueSubmit2KHR = nullptr;
PFN_vkQueueWaitIdle QueueWaitIdle = nullptr;
PFN_vkDeviceWaitIdle DeviceWaitIdle = nullptr;
// swapchain / present
PFN_vkCreateSwapchainKHR CreateSwapchainKHR = nullptr;
PFN_vkDestroySwapchainKHR DestroySwapchainKHR = nullptr;
PFN_vkGetSwapchainImagesKHR GetSwapchainImagesKHR = nullptr;
PFN_vkAcquireNextImageKHR AcquireNextImageKHR = nullptr;
// Alternate acquire entry (VK_KHR_swapchain 1.1+ / device-group). DXVK may use
// either AcquireNextImageKHR or AcquireNextImage2KHR - both hooked for the
// guest-acquire diagnostic. May be null - always null-check.
PFN_vkAcquireNextImage2KHR AcquireNextImage2KHR = nullptr;
PFN_vkQueuePresentKHR QueuePresentKHR = nullptr;
// resources
PFN_vkCreateImage CreateImage = nullptr;
PFN_vkDestroyImage DestroyImage = nullptr;
PFN_vkCreateImageView CreateImageView = nullptr;
PFN_vkDestroyImageView DestroyImageView = nullptr;
PFN_vkAllocateMemory AllocateMemory = nullptr;
PFN_vkFreeMemory FreeMemory = nullptr;
PFN_vkBindImageMemory BindImageMemory = nullptr;
PFN_vkGetImageMemoryRequirements GetImageMemoryRequirements = nullptr;
PFN_vkCreateBuffer CreateBuffer = nullptr;
PFN_vkDestroyBuffer DestroyBuffer = nullptr;
PFN_vkGetBufferMemoryRequirements GetBufferMemoryRequirements = nullptr;
PFN_vkBindBufferMemory BindBufferMemory = nullptr;
PFN_vkMapMemory MapMemory = nullptr;
PFN_vkUnmapMemory UnmapMemory = nullptr;
PFN_vkCreateSampler CreateSampler = nullptr;
PFN_vkDestroySampler DestroySampler = nullptr;
// pipeline / descriptors
PFN_vkCreateShaderModule CreateShaderModule = nullptr;
PFN_vkDestroyShaderModule DestroyShaderModule = nullptr;
PFN_vkCreateDescriptorSetLayout CreateDescriptorSetLayout = nullptr;
PFN_vkDestroyDescriptorSetLayout DestroyDescriptorSetLayout = nullptr;
PFN_vkCreatePipelineLayout CreatePipelineLayout = nullptr;
PFN_vkDestroyPipelineLayout DestroyPipelineLayout = nullptr;
PFN_vkCreateComputePipelines CreateComputePipelines = nullptr;
PFN_vkDestroyPipeline DestroyPipeline = nullptr;
PFN_vkCreateDescriptorPool CreateDescriptorPool = nullptr;
PFN_vkDestroyDescriptorPool DestroyDescriptorPool = nullptr;
// Frees every set in the pool at once - used by FrameGen::configure() to reclaim
// the old scratch descriptor sets when a perf_preset change forces a live rebuild
// (otherwise repeated preset switches would exhaust the pool). May be null in
// theory; the rebuild path null-checks before calling.
PFN_vkResetDescriptorPool ResetDescriptorPool = nullptr;
PFN_vkAllocateDescriptorSets AllocateDescriptorSets = nullptr;
PFN_vkUpdateDescriptorSets UpdateDescriptorSets = nullptr;
// command recording
PFN_vkCreateCommandPool CreateCommandPool = nullptr;
PFN_vkDestroyCommandPool DestroyCommandPool = nullptr;
PFN_vkAllocateCommandBuffers AllocateCommandBuffers = nullptr;
PFN_vkFreeCommandBuffers FreeCommandBuffers = nullptr;
PFN_vkBeginCommandBuffer BeginCommandBuffer = nullptr;
PFN_vkEndCommandBuffer EndCommandBuffer = nullptr;
PFN_vkCmdBindPipeline CmdBindPipeline = nullptr;
PFN_vkCmdBindDescriptorSets CmdBindDescriptorSets = nullptr;
PFN_vkCmdDispatch CmdDispatch = nullptr;
PFN_vkCmdPipelineBarrier CmdPipelineBarrier = nullptr;
PFN_vkCmdCopyImage CmdCopyImage = nullptr;
PFN_vkCmdCopyImageToBuffer CmdCopyImageToBuffer = nullptr; // capture readback
PFN_vkCmdBlitImage CmdBlitImage = nullptr;
PFN_vkCmdClearColorImage CmdClearColorImage = nullptr;
PFN_vkCreateFence CreateFence = nullptr;
PFN_vkDestroyFence DestroyFence = nullptr;
PFN_vkWaitForFences WaitForFences = nullptr;
PFN_vkResetFences ResetFences = nullptr;
PFN_vkCreateSemaphore CreateSemaphore = nullptr;
PFN_vkDestroySemaphore DestroySemaphore = nullptr;
};
template <typename PFN>
static inline void load(PFN& slot, PFN_vkGetDeviceProcAddr gdpa, VkDevice dev, const char* name) {
slot = reinterpret_cast<PFN>(gdpa(dev, name));
}
} // namespace winfg
@@ -0,0 +1,204 @@
// See winfg_engine.h.
#include "winfg_engine.h"
#include "winfg_vkd.h"
#include "log.hpp"
#include <algorithm>
#include <cstring>
namespace winfg {
namespace {
// 2x: the guest layer synthesises at 0.35, not 0.5 - biased towards prev to
// cut the double-exposure ghost on HUD/text ("iterative bring-up knob" in
// layer.cpp). Kept for parity, since that is the tuning that was proven.
// 3x/4x: evenly spaced; never exercised by the guest layer, so this is the
// new ground native opens up and the first thing to judge by eye.
float alphaFor(uint32_t k, uint32_t n) {
if (n <= 1) return 0.35f;
return (float)(k + 1) / (float)(n + 1);
}
VkImageMemoryBarrier bar(VkImage img, VkImageLayout from, VkImageLayout to,
VkAccessFlags src, VkAccessFlags dst) {
VkImageMemoryBarrier b{};
b.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
b.oldLayout = from; b.newLayout = to;
b.srcAccessMask = src; b.dstAccessMask = dst;
b.srcQueueFamilyIndex = b.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
b.image = img;
b.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
return b;
}
} // namespace
Engine::Engine() = default;
Engine::~Engine() { destroyRing(); fg_.destroy(); }
bool Engine::init(const VkTable& table, VkPhysicalDevice phys, VkDevice dev,
uint32_t queueFamily, VkQueue queue) {
if (!winfgVkdInit(table, dd_, id_)) {
WFG_LOGE("native: dispatch incomplete; frame generation unavailable");
return false;
}
phys_ = phys; dev_ = dev; qf_ = queueFamily; queue_ = queue;
id_.GetPhysicalDeviceMemoryProperties(phys_, &memProps_);
if (!fg_.init(&dd_, &id_, phys_, dev_, qf_, queue_)) {
WFG_LOGE("native: FrameGen::init failed");
unavailable_ = true;
return false;
}
WFG_LOGI("native: chain ready (10 pipelines, embedded shaders)");
return true;
}
void Engine::configure(uint32_t multiplier, int model, int perfPreset, float flowScale) {
cfg_.enabled = true;
cfg_.multiplier = (int)std::max<uint32_t>(2, multiplier);
cfg_.model = (model == 3) ? 3 : 4;
cfg_.perfPreset = std::clamp(perfPreset, 0, 2);
cfg_.flowScale = std::clamp(flowScale, 0.25f, 1.0f);
fg_.configure(cfg_);
}
bool Engine::prepare(uint32_t w, uint32_t h, VkFormat fmt) {
if (unavailable_ || w == 0 || h == 0 || fmt == VK_FORMAT_UNDEFINED) return false;
if (builtW_ == w && builtH_ == h && builtFmt_ == fmt && fg_.valid()) return true;
// onResize rebuilds the pyramids/scratch; our own input ring follows it.
if (!fg_.onResize(VkExtent2D{w, h}, fmt, /*force=*/false)) {
WFG_LOGE("native: onResize %ux%u failed; frame generation unavailable", w, h);
unavailable_ = true;
return false;
}
destroyRing();
for (Slot& s : ring_) {
if (!makeSlot(s, w, h, fmt)) { destroyRing(); unavailable_ = true; return false; }
}
builtW_ = w; builtH_ = h; builtFmt_ = fmt;
history_ = 0; curr_ = 0; frames_ = 0;
WFG_LOGI("native: built %ux%u fmt=%d model=%d preset=%d flow=%.2f",
w, h, (int)fmt, cfg_.model, cfg_.perfPreset, (double)cfg_.flowScale);
return true;
}
uint32_t Engine::plan(uint32_t capacity) const {
if (!valid() || history_ < 2) return 0; // need prev AND curr
const uint32_t want = (uint32_t)std::max(cfg_.multiplier - 1, 0);
return std::min({want, capacity, kMaxGenerations});
}
void Engine::process(VkCommandBuffer cmd, VkImage source, uint32_t w, uint32_t h,
uint32_t /*generations*/) {
if (!valid()) return;
// Rotate: the slot we are about to overwrite is the OLD prev; after the
// copy it is the new curr and the other slot is prev.
curr_ = 1 - curr_;
Slot& dst = ring_[curr_];
// source is GENERAL (composite render pass leaves it there); the slot may
// be in any layout from last use - UNDEFINED is legal as "discard".
VkImageMemoryBarrier pre[2] = {
bar(source, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT),
bar(dst.img, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
0, VK_ACCESS_TRANSFER_WRITE_BIT),
};
dd_.CmdPipelineBarrier(cmd,
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 2, pre);
VkImageCopy region{};
region.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
region.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
region.extent = {w, h, 1};
dd_.CmdCopyImage(cmd, source, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
dst.img, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
// source back to GENERAL for the real-frame copy that follows; the new
// curr becomes SHADER_READ_ONLY, which is what the chain samples it as.
VkImageMemoryBarrier post[2] = {
bar(source, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL,
VK_ACCESS_TRANSFER_READ_BIT, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_TRANSFER_READ_BIT),
bar(dst.img, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT),
};
dd_.CmdPipelineBarrier(cmd, VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT | VK_PIPELINE_STAGE_TRANSFER_BIT,
0, 0, nullptr, 0, nullptr, 2, post);
dst.valid = true;
if (history_ < 2) history_++;
frames_++;
}
void Engine::generateInto(VkCommandBuffer cmd, uint32_t generation, uint32_t count,
VkImage targetImage, VkImageView targetView, uint32_t w, uint32_t h) {
if (!valid() || history_ < 2) return;
const Slot& prev = ring_[1 - curr_];
const Slot& curr = ring_[curr_];
if (!prev.valid || !curr.valid) return;
// The chain writes the output through a STORAGE descriptor in GENERAL.
// The composite ring already keeps its targets in GENERAL; make the
// prior contents' writes visible and take ownership for the compute write.
VkImageMemoryBarrier toWrite = bar(targetImage, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_GENERAL,
VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_SHADER_READ_BIT, VK_ACCESS_SHADER_WRITE_BIT);
dd_.CmdPipelineBarrier(cmd, VK_PIPELINE_STAGE_TRANSFER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &toWrite);
// gmSlot indexes the C1 global-motion SSBO read back on the CPU next
// frame; kGmSlots (3) >= our frames in flight (2), so rotating per source
// frame keeps a slot from being read while its reduce is still in flight.
fg_.record(cmd, prev.view, curr.view, targetView, alphaFor(generation, count), gmSlot_);
if (generation + 1 == count) gmSlot_ = (gmSlot_ + 1) % (uint32_t)FrameGen::kGmSlots;
(void)w; (void)h;
}
void Engine::reset() { history_ = 0; frames_ = 0; }
// ---- input ring --------------------------------------------------------------
uint32_t Engine::memType(uint32_t bits, VkMemoryPropertyFlags props) const {
for (uint32_t i = 0; i < memProps_.memoryTypeCount; i++)
if ((bits & (1u << i)) && (memProps_.memoryTypes[i].propertyFlags & props) == props) return i;
return 0;
}
bool Engine::makeSlot(Slot& s, uint32_t w, uint32_t h, VkFormat fmt) {
VkImageCreateInfo ii{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
ii.imageType = VK_IMAGE_TYPE_2D; ii.extent = {w, h, 1};
ii.mipLevels = 1; ii.arrayLayers = 1; ii.format = fmt;
ii.tiling = VK_IMAGE_TILING_OPTIMAL; ii.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
ii.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
ii.samples = VK_SAMPLE_COUNT_1_BIT; ii.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
if (dd_.CreateImage(dev_, &ii, nullptr, &s.img) != VK_SUCCESS) return false;
VkMemoryRequirements req; dd_.GetImageMemoryRequirements(dev_, s.img, &req);
VkMemoryAllocateInfo ai{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
ai.allocationSize = req.size;
ai.memoryTypeIndex = memType(req.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
if (dd_.AllocateMemory(dev_, &ai, nullptr, &s.mem) != VK_SUCCESS) { destroySlot(s); return false; }
dd_.BindImageMemory(dev_, s.img, s.mem, 0);
VkImageViewCreateInfo vi{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO};
vi.image = s.img; vi.viewType = VK_IMAGE_VIEW_TYPE_2D; vi.format = fmt;
vi.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
if (dd_.CreateImageView(dev_, &vi, nullptr, &s.view) != VK_SUCCESS) { destroySlot(s); return false; }
s.valid = false;
return true;
}
void Engine::destroySlot(Slot& s) {
if (s.view) dd_.DestroyImageView(dev_, s.view, nullptr);
if (s.img) dd_.DestroyImage(dev_, s.img, nullptr);
if (s.mem) dd_.FreeMemory(dev_, s.mem, nullptr);
s = Slot{};
}
void Engine::destroyRing() {
if (dev_ == VK_NULL_HANDLE || !dd_.DeviceWaitIdle) return;
if (ring_[0].img || ring_[1].img) dd_.DeviceWaitIdle(dev_);
destroySlot(ring_[0]); destroySlot(ring_[1]);
history_ = 0;
}
} // namespace winfg
@@ -0,0 +1,102 @@
#pragma once
// ============================================================================
// winfg_engine - win-fg (Bannerlator's own frame generation) running inside
// the compositor, behind the same contract the LSFG engine uses:
//
// prepare(w, h, format) build/rebuild the chain for this size
// plan(capacity) how many frames to generate this time
// process(cmd, source, w, h, gens) take frame N in (becomes "curr")
// generateInto(cmd, k, n, ...) synthesise generated frame k of n
//
// Why this exists: as a guest-side Vulkan layer, win-fg's generated frames
// had to cross Wine -> DRI3 -> our X server -> the compositor, where the
// per-window latest-wins overwrite dropped them before they were ever
// presented - the HUD counted 2x, the panel got 1x. Here the chain runs in the
// compositor's own command stream and its frames are presented directly, on
// the same multi-present path that is device-proven for LSFG Native.
//
// Unlike LSFG this needs nothing from the user: the ten shaders are ours
// (MIT, see LICENSE.win-fg / NOTICE_FIDELITYFX_OPTICALFLOW.md) and embedded.
//
// The chain (framegen.cpp) is used unmodified. Its inputs are two sampled
// rgba8 views (prev, curr) and its output a GENERAL storage view; the guest
// layer transitions curr to SHADER_READ_ONLY before record() and the output
// UNDEFINED -> GENERAL, so we do exactly the same around the same call.
// ============================================================================
#include <cstdint>
#include <vulkan/vulkan.h>
#include "framegen.hpp"
#include "vk_dispatch.hpp"
struct VkTable;
namespace winfg {
// Hard ceiling on generated frames per source frame (2x..4x -> 1..3), matching
// the LSFG engine and the drawer's buttons.
constexpr uint32_t kMaxGenerations = 3;
class Engine {
public:
Engine();
~Engine();
Engine(const Engine&) = delete;
Engine& operator=(const Engine&) = delete;
bool init(const VkTable& table, VkPhysicalDevice phys, VkDevice dev,
uint32_t queueFamily, VkQueue queue);
bool valid() const { return fg_.valid() && !unavailable_; }
bool unavailable() const { return unavailable_; }
// model 3/4, perf preset 0..2, flow scale - the same knobs the drawer
// already exposes for win-fg; they map straight onto winfg::Config.
void configure(uint32_t multiplier, int model, int perfPreset, float flowScale);
bool prepare(uint32_t width, uint32_t height, VkFormat format);
// 0 until there is a previous frame to interpolate from.
uint32_t plan(uint32_t capacity) const;
// Copy the composited frame into the input ring as "curr" (the previous
// one becomes "prev"). `source` must be in GENERAL.
void process(VkCommandBuffer cmd, VkImage source, uint32_t width, uint32_t height,
uint32_t generations);
// Synthesise generated frame `generation` of `count` into the storage view.
// The target must be in GENERAL (the composite ring keeps it there).
void generateInto(VkCommandBuffer cmd, uint32_t generation, uint32_t count,
VkImage targetImage, VkImageView targetView,
uint32_t width, uint32_t height);
void reset();
private:
struct Slot { VkImage img = VK_NULL_HANDLE; VkDeviceMemory mem = VK_NULL_HANDLE;
VkImageView view = VK_NULL_HANDLE; bool valid = false; };
bool makeSlot(Slot& s, uint32_t w, uint32_t h, VkFormat fmt);
void destroySlot(Slot& s);
void destroyRing();
uint32_t memType(uint32_t bits, VkMemoryPropertyFlags props) const;
DeviceDispatch dd_{};
InstanceDispatch id_{};
VkPhysicalDevice phys_ = VK_NULL_HANDLE;
VkDevice dev_ = VK_NULL_HANDLE;
uint32_t qf_ = 0;
VkQueue queue_ = VK_NULL_HANDLE;
VkPhysicalDeviceMemoryProperties memProps_{};
FrameGen fg_;
Config cfg_;
bool unavailable_ = false;
Slot ring_[2];
int curr_ = 0; // index of "curr" in ring_; prev is 1 - curr_
uint32_t history_ = 0; // frames copied so far (need 2)
uint32_t gmSlot_ = 0; // rotates through FrameGen::kGmSlots
uint32_t builtW_ = 0, builtH_ = 0;
VkFormat builtFmt_ = VK_FORMAT_UNDEFINED;
uint64_t frames_ = 0;
};
} // namespace winfg
@@ -0,0 +1,56 @@
// See winfg_vkd.h.
#include "winfg_vkd.h"
#include "../VulkanRendererContext.h"
#include <android/log.h>
bool winfgVkdInit(const VkTable& table, winfg::DeviceDispatch& dd, winfg::InstanceDispatch& id) {
bool ok = true;
#define COPY(dst, fn) \
do { \
dst.fn = table.fn; \
if (!dst.fn) { \
__android_log_print(ANDROID_LOG_ERROR, "WinFgVkd", \
"entry point vk" #fn " did not resolve"); \
ok = false; \
} \
} while (0)
COPY(dd, AllocateDescriptorSets);
COPY(dd, AllocateMemory);
COPY(dd, BindBufferMemory);
COPY(dd, BindImageMemory);
COPY(dd, CmdBindDescriptorSets);
COPY(dd, CmdBindPipeline);
COPY(dd, CmdClearColorImage);
COPY(dd, CmdCopyImage);
COPY(dd, CmdDispatch);
COPY(dd, CmdPipelineBarrier);
COPY(dd, CreateBuffer);
COPY(dd, CreateComputePipelines);
COPY(dd, CreateDescriptorPool);
COPY(dd, CreateDescriptorSetLayout);
COPY(dd, CreateImage);
COPY(dd, CreateImageView);
COPY(dd, CreatePipelineLayout);
COPY(dd, CreateSampler);
COPY(dd, CreateShaderModule);
COPY(dd, DestroyBuffer);
COPY(dd, DestroyDescriptorPool);
COPY(dd, DestroyDescriptorSetLayout);
COPY(dd, DestroyImage);
COPY(dd, DestroyImageView);
COPY(dd, DestroyPipeline);
COPY(dd, DestroyPipelineLayout);
COPY(dd, DestroySampler);
COPY(dd, DestroyShaderModule);
COPY(dd, DeviceWaitIdle);
COPY(dd, FreeMemory);
COPY(dd, GetBufferMemoryRequirements);
COPY(dd, GetImageMemoryRequirements);
COPY(dd, MapMemory);
COPY(dd, ResetDescriptorPool);
COPY(dd, UnmapMemory);
COPY(dd, UpdateDescriptorSets);
COPY(id, GetPhysicalDeviceMemoryProperties);
#undef COPY
return ok;
}
@@ -0,0 +1,19 @@
#pragma once
// ============================================================================
// winfg_vkd - fill win-fg's own dispatch tables from the renderer's VkTable.
//
// win-fg's chain (framegen.cpp / record_impl.inc) was written as a Vulkan
// LAYER and calls through winfg::DeviceDispatch / InstanceDispatch. Inside the
// compositor there is no layer chain to resolve those from, so they are filled
// from the renderer's per-context table instead - the same bridge lsfg_vkd
// provides for the LSFG chain, for the same reason: an adrenotools driver
// shares no global symbols with the system loader.
//
// Only the entry points the chain actually calls are filled (36 device-level
// plus GetPhysicalDeviceMemoryProperties); everything else in the structs
// stays null and is never reached. winfgVkdInit reports a false if any of
// those failed to resolve, so a null is never called mid-frame.
// ============================================================================
#include "vk_dispatch.hpp"
struct VkTable;
bool winfgVkdInit(const VkTable& table, winfg::DeviceDispatch& dd, winfg::InstanceDispatch& id);