Frame generation in the compositor: Win-FG Native and LSFG Native

The compositor was built with its frame-generation leg stubbed out. Bannerlator's
two engines and the DXBC translator they need now ride along, so extra frames
are generated on gamescope's final output — any game, nothing changed in the
runtime. Win-FG needs nothing; LSFG takes its shader chain from the user's own
Lossless Scaling, which in this app is installed from the Steam client and read
straight out of the runtime's library. One picker on the main screen: Off, or an
engine at 2x/3x/4x, applied live on the next return to the session.
This commit is contained in:
The412Banner committed 2026-09-19 15:14:45 -04:00
1 parent d46bd4a0c3
commit 0425023063
114 files changed
+36459

No files matched your search

+27
View File
@@ -6,6 +6,33 @@ project(steamdeck C CXX)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2 -Wno-unused-function -Wimplicit-function-declaration")
add_subdirectory(adrenotools)
# DXVK's DXBC->SPIR-V translator (zlib licence), vendored under thirdparty/dxbc from the same
# subset upstream lsfg-vk uses. Native LSFG frame generation needs it: the Lossless Scaling
# shader chain is DXBC in every build downloadable from Steam.
add_subdirectory(thirdparty/dxbc)
# The Win-FG chain self-identifies by two halves: the upstream tag it was copied from (hand-set,
# UPDATE IT when re-copying) and a hash over the sources themselves, computed here, which cannot
# rot. The compositor's frame-generation leg (waylandcomp/CMakeLists.txt) stamps both in.
set(WINFG_UPSTREAM "v0.3.0")
set(WINFG_CHAIN_SRC
winlator/winfg/framegen.cpp
winlator/winfg/framegen.hpp
winlator/winfg/record_impl.inc
winlator/winfg/config.hpp
winlator/winfg/embedded_shaders.hpp
winlator/winfg/winfg_engine.cpp
)
set(WINFG_HASH_INPUT "")
foreach(f ${WINFG_CHAIN_SRC})
file(SHA256 "${CMAKE_CURRENT_SOURCE_DIR}/${f}" _h)
string(APPEND WINFG_HASH_INPUT "${_h}")
endforeach()
string(SHA256 WINFG_CHAIN_HASH_FULL "${WINFG_HASH_INPUT}")
string(SUBSTRING "${WINFG_CHAIN_HASH_FULL}" 0 12 WINFG_CHAIN_HASH)
message(STATUS "win-fg chain: ${WINFG_UPSTREAM} (src ${WINFG_CHAIN_HASH})")
add_subdirectory(waylandcomp)
add_library(sdnative SHARED sdnative/sdnative.c)
target_link_libraries(sdnative log)
@@ -0,0 +1,3 @@
# cmake files
.cache/
build/
+29
View File
@@ -0,0 +1,29 @@
cmake_minimum_required(VERSION 3.10)
project(dxbc
VERSION 2.6.2
DESCRIPTION "DXVK's DXBC"
LANGUAGES CXX)
file(GLOB SOURCES
"src/dxbc/*.cpp"
"src/spirv/*.cpp"
"src/util/*.cpp"
)
add_library(dxbc STATIC ${SOURCES})
# target
set_target_properties(dxbc PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON)
target_include_directories(dxbc SYSTEM
PUBLIC include/dxbc
PUBLIC include/spirv include/util include/dxvk)
target_compile_options(dxbc PRIVATE
-fPIC)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set_target_properties(dxbc PROPERTIES
EXPORT_COMPILE_COMMANDS ON)
endif()
+24
View File
@@ -0,0 +1,24 @@
Copyright (c) 2017 Philip Rebohle
Copyright (c) 2019 Joshua Ashton
Copyright (c) 2019 Robin Kertels
Copyright (c) 2023 Jeffrey Ellison
zlib/libpng license
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
– The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
– Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
– This notice may not be removed or altered from any source distribution.
+4
View File
@@ -0,0 +1,4 @@
This GitHub repo hosts the files for running DXVK's shader translation independently of DXVK.
Please check out the original project:
https://github.com/doitsujin/dxvk
@@ -0,0 +1,110 @@
#pragma once
#include "dxbc_chunk_isgn.h"
#include "dxbc_decoder.h"
#include "dxbc_defs.h"
#include "dxbc_names.h"
#include "dxbc_modinfo.h"
#include "dxbc_util.h"
namespace dxvk {
/**
* \brief Info about unordered access views
*
* Stores whether an UAV is accessed with typed
* read or atomic instructions. This information
* will be used to generate image types.
*/
struct DxbcUavInfo {
bool accessTypedLoad = false;
bool accessAtomicOp = false;
bool sparseFeedback = false;
bool nonInvariantAccess = false;
DxvkAccessOp accessOp = DxvkAccessOp::None;
VkAccessFlags accessFlags = 0;
};
/**
* \brief Info about shader resource views
*
* Stores whether an SRV is accessed with
* sparse feedback. Useful for buffers.
*/
struct DxbcSrvInfo {
bool sparseFeedback = false;
};
/**
* \brief Counts cull and clip distances
*/
struct DxbcClipCullInfo {
uint32_t numClipPlanes = 0;
uint32_t numCullPlanes = 0;
};
/**
* \brief Shader analysis info
*/
struct DxbcAnalysisInfo {
std::array<DxbcUavInfo, 64> uavInfos;
std::array<DxbcSrvInfo, 128> srvInfos;
std::array<DxbcRegMask, 4096> xRegMasks;
DxbcClipCullInfo clipCullIn;
DxbcClipCullInfo clipCullOut;
DxbcBindingMask bindings = { };
bool usesDerivatives = false;
bool usesKill = false;
};
/**
* \brief DXBC shader analysis pass
*
* Collects information about the shader itself
* and the resources used by the shader, which
* will later be used by the actual compiler.
*/
class DxbcAnalyzer {
public:
DxbcAnalyzer(
const DxbcModuleInfo& moduleInfo,
const DxbcProgramInfo& programInfo,
const Rc<DxbcIsgn>& isgn,
const Rc<DxbcIsgn>& osgn,
const Rc<DxbcIsgn>& psgn,
DxbcAnalysisInfo& analysis);
~DxbcAnalyzer();
/**
* \brief Processes a single instruction
* \param [in] ins The instruction
*/
void processInstruction(
const DxbcShaderInstruction& ins);
private:
Rc<DxbcIsgn> m_isgn;
Rc<DxbcIsgn> m_osgn;
Rc<DxbcIsgn> m_psgn;
DxbcAnalysisInfo* m_analysis = nullptr;
DxbcClipCullInfo getClipCullInfo(
const Rc<DxbcIsgn>& sgn) const;
void setUavAccessOp(uint32_t uav, DxvkAccessOp op);
static DxvkAccessOp getStoreAccessOp(DxbcRegMask writeMask, const DxbcRegister& src);
static DxvkAccessOp getConstantStoreOp(uint32_t value);
};
}
@@ -0,0 +1,68 @@
#pragma once
#include <cctype>
#include "dxbc_common.h"
#include "dxbc_decoder.h"
#include "dxbc_enums.h"
#include "dxbc_reader.h"
namespace dxvk {
/**
* \brief Signature entry
*
* Stores the semantic name of an input or
* output and the corresponding register.
*/
struct DxbcSgnEntry {
std::string semanticName;
uint32_t semanticIndex;
uint32_t registerId;
DxbcRegMask componentMask;
DxbcRegMask componentUsed;
DxbcScalarType componentType;
DxbcSystemValue systemValue;
uint32_t streamId;
};
/**
* \brief Input/Output signature chunk
*
* Stores information about the input and
* output registers used by the shader stage.
*/
class DxbcIsgn : public RcObject {
public:
DxbcIsgn(DxbcReader reader, DxbcTag tag);
~DxbcIsgn();
auto begin() const { return m_entries.cbegin(); }
auto end () const { return m_entries.cend(); }
const DxbcSgnEntry* findByRegister(
uint32_t registerId) const;
const DxbcSgnEntry* find(
const std::string& semanticName,
uint32_t semanticIndex,
uint32_t streamIndex) const;
DxbcRegMask regMask(
uint32_t registerId) const;
uint32_t maxRegisterCount() const;
static bool compareSemanticNames(
const std::string& a,
const std::string& b);
private:
std::vector<DxbcSgnEntry> m_entries;
};
}
@@ -0,0 +1,39 @@
#pragma once
#include "dxbc_common.h"
#include "dxbc_decoder.h"
#include "dxbc_reader.h"
namespace dxvk {
/**
* \brief Shader code chunk
*
* Stores the DXBC shader code itself, as well
* as some meta info about the shader, i.e. what
* type of shader this is.
*/
class DxbcShex : public RcObject {
public:
DxbcShex(DxbcReader reader);
~DxbcShex();
DxbcProgramInfo programInfo() const {
return m_programInfo;
}
DxbcCodeSlice slice() const {
return DxbcCodeSlice(m_code.data(),
m_code.data() + m_code.size());
}
private:
DxbcProgramInfo m_programInfo;
std::vector<uint32_t> m_code;
};
}
@@ -0,0 +1,72 @@
#pragma once
#include "dxbc_include.h"
namespace dxvk {
/**
* \brief DXBC Program type
*
* Defines the shader stage that a DXBC
* module has been compiled form.
*/
enum class DxbcProgramType : uint16_t {
PixelShader = 0,
VertexShader = 1,
GeometryShader = 2,
HullShader = 3,
DomainShader = 4,
ComputeShader = 5,
Count
};
using DxbcProgramTypeFlags = Flags<DxbcProgramType>;
/**
* \brief DXBC shader info
*
* Stores the shader program type.
*/
class DxbcProgramInfo {
public:
DxbcProgramInfo() { }
DxbcProgramInfo(DxbcProgramType type)
: m_type(type) { }
/**
* \brief Program type
* \returns Program type
*/
DxbcProgramType type() const {
return m_type;
}
/**
* \brief Vulkan shader stage
*
* The \c VkShaderStageFlagBits constant
* that corresponds to the program type.
* \returns Vulkan shaer stage
*/
VkShaderStageFlagBits shaderStage() const;
/**
* \brief SPIR-V execution model
*
* The execution model that corresponds
* to the Vulkan shader stage.
* \returns SPIR-V execution model
*/
spv::ExecutionModel executionModel() const;
private:
DxbcProgramType m_type = DxbcProgramType::PixelShader;
};
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,509 @@
#pragma once
#include <array>
#include "dxbc_common.h"
#include "dxbc_decoder.h"
#include "dxbc_defs.h"
#include "dxbc_enums.h"
#include "dxbc_names.h"
namespace dxvk {
constexpr size_t DxbcMaxRegIndexDim = 3;
struct DxbcRegister;
/**
* \brief Source operand modifiers
*
* These are applied after loading
* an operand register.
*/
enum class DxbcRegModifier : uint32_t {
Neg = 0,
Abs = 1,
};
using DxbcRegModifiers = Flags<DxbcRegModifier>;
/**
* \brief Constant buffer binding
*
* Stores information required to
* access a constant buffer.
*/
struct DxbcConstantBuffer {
uint32_t varId = 0;
uint32_t size = 0;
};
/**
* \brief Sampler binding
*
* Stores a sampler variable that can be
* used together with a texture resource.
*/
struct DxbcSampler {
uint32_t varId = 0;
uint32_t typeId = 0;
};
/**
* \brief Image type information
*/
struct DxbcImageInfo {
spv::Dim dim = spv::Dim1D;
uint32_t array = 0;
uint32_t ms = 0;
uint32_t sampled = 0;
VkImageViewType vtype = VK_IMAGE_VIEW_TYPE_MAX_ENUM;
};
/**
* \brief Shader resource binding
*
* Stores a resource variable
* and associated type IDs.
*/
struct DxbcShaderResource {
DxbcResourceType type = DxbcResourceType::Typed;
DxbcImageInfo imageInfo;
uint32_t varId = 0;
uint32_t specId = 0;
DxbcScalarType sampledType = DxbcScalarType::Float32;
uint32_t sampledTypeId = 0;
uint32_t imageTypeId = 0;
uint32_t colorTypeId = 0;
uint32_t depthTypeId = 0;
uint32_t structStride = 0;
bool isRawSsbo = false;
};
/**
* \brief Unordered access binding
*
* Stores a resource variable that is provided
* by a UAV, as well as associated type IDs.
*/
struct DxbcUav {
DxbcResourceType type = DxbcResourceType::Typed;
DxbcImageInfo imageInfo;
uint32_t varId = 0;
uint32_t ctrId = 0;
uint32_t specId = 0;
DxbcScalarType sampledType = DxbcScalarType::Float32;
uint32_t sampledTypeId = 0;
uint32_t imageTypeId = 0;
uint32_t structStride = 0;
uint32_t coherence = 0;
bool isRawSsbo = false;
};
/**
* \brief Component swizzle
*
* Maps vector components to
* other vector components.
*/
class DxbcRegSwizzle {
public:
DxbcRegSwizzle() { }
DxbcRegSwizzle(uint32_t x, uint32_t y, uint32_t z, uint32_t w)
: m_mask((x << 0) | (y << 2) | (z << 4) | (w << 6)) { }
uint32_t operator [] (uint32_t id) const {
return (m_mask >> (id + id)) & 0x3;
}
bool operator == (const DxbcRegSwizzle& other) const { return m_mask == other.m_mask; }
bool operator != (const DxbcRegSwizzle& other) const { return m_mask != other.m_mask; }
private:
uint8_t m_mask = 0;
};
/**
* \brief Component mask
*
* Enables access to certain
* subset of vector components.
*/
class DxbcRegMask {
public:
DxbcRegMask() { }
DxbcRegMask(uint32_t mask) : m_mask(mask) { }
DxbcRegMask(bool x, bool y, bool z, bool w)
: m_mask((x ? 0x1 : 0) | (y ? 0x2 : 0)
| (z ? 0x4 : 0) | (w ? 0x8 : 0)) { }
uint32_t raw() const {
return m_mask;
}
bool operator [] (uint32_t id) const {
return (m_mask >> id) & 1;
}
uint32_t popCount() const {
const uint8_t n[16] = { 0, 1, 1, 2, 1, 2, 2, 3,
1, 2, 2, 3, 2, 3, 3, 4 };
return n[m_mask & 0xF];
}
uint32_t firstSet() const {
const uint8_t n[16] = { 4, 0, 1, 0, 2, 0, 1, 0,
3, 0, 1, 0, 2, 0, 1, 0 };
return n[m_mask & 0xF];
}
uint32_t minComponents() const {
const uint8_t n[16] = { 0, 1, 2, 2, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4 };
return n[m_mask & 0xF];
}
bool operator == (const DxbcRegMask& other) const { return m_mask == other.m_mask; }
bool operator != (const DxbcRegMask& other) const { return m_mask != other.m_mask; }
DxbcRegMask& operator |= (const DxbcRegMask& other) {
m_mask |= other.m_mask;
return *this;
}
static DxbcRegMask firstN(uint32_t n) {
return DxbcRegMask(n >= 1, n >= 2, n >= 3, n >= 4);
}
static DxbcRegMask select(uint32_t n) {
return DxbcRegMask(n == 0, n == 1, n == 2, n == 3);
}
std::string maskString() const {
std::string out = "";
out += (m_mask & 0x1) ? "x" : "";
out += (m_mask & 0x2) ? "y" : "";
out += (m_mask & 0x4) ? "z" : "";
out += (m_mask & 0x8) ? "w" : "";
return out;
}
explicit operator bool () const {
return m_mask != 0;
}
private:
uint8_t m_mask = 0;
};
/**
* \brief System value mapping
*
* Maps a system value to a given set of
* components of an input or output register.
*/
struct DxbcSvMapping {
uint32_t regId;
DxbcRegMask regMask;
DxbcSystemValue sv;
};
struct DxbcRegIndex {
DxbcRegister* relReg;
int32_t offset;
};
/**
* \brief Instruction operand
*/
struct DxbcRegister {
DxbcOperandType type;
DxbcScalarType dataType;
DxbcComponentCount componentCount;
uint32_t idxDim;
DxbcRegIndex idx[DxbcMaxRegIndexDim];
DxbcRegMask mask;
DxbcRegSwizzle swizzle;
DxbcRegModifiers modifiers;
union {
uint32_t u32_4[4];
uint32_t u32_1;
} imm;
};
/**
* \brief Instruction result modifiers
*
* Modifiers that are applied
* to all destination operands.
*/
struct DxbcOpModifiers {
bool saturate;
bool precise;
};
/**
* \brief Opcode controls
*
* Instruction-specific controls. Usually,
* only one of the members will be valid.
*/
class DxbcShaderOpcodeControls {
public:
DxbcShaderOpcodeControls()
: m_bits(0) { }
DxbcShaderOpcodeControls(uint32_t bits)
: m_bits(bits) { }
DxbcInstructionReturnType returnType() const {
return DxbcInstructionReturnType(bit::extract(m_bits, 11, 11));
}
DxbcGlobalFlags globalFlags() const {
return DxbcGlobalFlags(bit::extract(m_bits, 11, 14));
}
DxbcZeroTest zeroTest() const {
return DxbcZeroTest(bit::extract(m_bits, 18, 18));
}
DxbcSyncFlags syncFlags() const {
return DxbcSyncFlags(bit::extract(m_bits, 11, 14));
}
DxbcResourceDim resourceDim() const {
return DxbcResourceDim(bit::extract(m_bits, 11, 15));
}
DxbcResinfoType resinfoType() const {
return DxbcResinfoType(bit::extract(m_bits, 11, 12));
}
DxbcInterpolationMode interpolation() const {
return DxbcInterpolationMode(bit::extract(m_bits, 11, 14));
}
DxbcSamplerMode samplerMode() const {
return DxbcSamplerMode(bit::extract(m_bits, 11, 14));
}
DxbcPrimitiveTopology primitiveTopology() const {
return DxbcPrimitiveTopology(bit::extract(m_bits, 11, 17));
}
DxbcPrimitive primitive() const {
return DxbcPrimitive(bit::extract(m_bits, 11, 16));
}
DxbcTessDomain tessDomain() const {
return DxbcTessDomain(bit::extract(m_bits, 11, 12));
}
DxbcTessOutputPrimitive tessOutputPrimitive() const {
return DxbcTessOutputPrimitive(bit::extract(m_bits, 11, 13));
}
DxbcTessPartitioning tessPartitioning() const {
return DxbcTessPartitioning(bit::extract(m_bits, 11, 13));
}
DxbcUavFlags uavFlags() const {
return DxbcUavFlags(bit::extract(m_bits, 16, 17));
}
DxbcConstantBufferAccessType accessType() const {
return DxbcConstantBufferAccessType(bit::extract(m_bits, 11, 11));
}
uint32_t controlPointCount() const {
return bit::extract(m_bits, 11, 16);
}
bool precise() const {
return bit::extract(m_bits, 19, 22) != 0;
}
private:
uint32_t m_bits;
};
/**
* \brief Sample controls
*
* Constant texel offset with
* values raning from -8 to 7.
*/
struct DxbcShaderSampleControls {
int u, v, w;
};
/**
* \brief Immediate value
*
* Immediate argument represented either
* as a 32-bit or 64-bit unsigned integer,
* or a 32-bit or 32-bit floating point number.
*/
union DxbcImmediate {
float f32;
double f64;
uint32_t u32;
uint64_t u64;
};
/**
* \brief Shader instruction
*
* Note that this structure may store pointer to
* external structures, such as the original code
* buffer. This is safe to use if and only if:
* - The \ref DxbcDecodeContext that created it
* still exists and was not moved
* - The code buffer that was being decoded
* still exists and was not moved.
*/
struct DxbcShaderInstruction {
DxbcOpcode op;
DxbcInstClass opClass;
DxbcOpModifiers modifiers;
DxbcShaderOpcodeControls controls;
DxbcShaderSampleControls sampleControls;
uint32_t dstCount;
uint32_t srcCount;
uint32_t immCount;
const DxbcRegister* dst;
const DxbcRegister* src;
const DxbcImmediate* imm;
DxbcCustomDataClass customDataType;
uint32_t customDataSize;
const uint32_t* customData;
};
/**
* \brief DXBC code slice
*
* Convenient pointer pair that allows
* reading the code word stream safely.
*/
class DxbcCodeSlice {
public:
DxbcCodeSlice(
const uint32_t* ptr,
const uint32_t* end)
: m_ptr(ptr), m_end(end) { }
const uint32_t* ptrAt(uint32_t id) const;
uint32_t at(uint32_t id) const;
uint32_t read();
DxbcCodeSlice take(uint32_t n) const;
DxbcCodeSlice skip(uint32_t n) const;
bool atEnd() const {
return m_ptr == m_end;
}
private:
const uint32_t* m_ptr = nullptr;
const uint32_t* m_end = nullptr;
};
/**
* \brief Decode context
*
* Stores data that is required to decode a single
* instruction. This data is not persistent, so it
* should be forwarded to the compiler right away.
*/
class DxbcDecodeContext {
public:
/**
* \brief Retrieves current instruction
*
* This is only valid after a call to \ref decode.
* \returns Reference to last decoded instruction
*/
const DxbcShaderInstruction& getInstruction() const {
return m_instruction;
}
/**
* \brief Decodes an instruction
*
* This also advances the given code slice by the
* number of dwords consumed by the instruction.
* \param [in] code Code slice
*/
void decodeInstruction(DxbcCodeSlice& code);
private:
DxbcShaderInstruction m_instruction;
std::array<DxbcRegister, 8> m_dstOperands;
std::array<DxbcRegister, 8> m_srcOperands;
std::array<DxbcImmediate, 4> m_immOperands;
std::array<DxbcRegister, 12> m_indices;
// Index into the indices array. Used when decoding
// instruction operands with relative indexing.
uint32_t m_indexId = 0;
void decodeCustomData(DxbcCodeSlice code);
void decodeOperation(DxbcCodeSlice code);
void decodeComponentSelection(DxbcRegister& reg, uint32_t token);
void decodeOperandExtensions(DxbcCodeSlice& code, DxbcRegister& reg, uint32_t token);
void decodeOperandImmediates(DxbcCodeSlice& code, DxbcRegister& reg);
void decodeOperandIndex(DxbcCodeSlice& code, DxbcRegister& reg, uint32_t token);
void decodeRegister(DxbcCodeSlice& code, DxbcRegister& reg, DxbcScalarType type);
void decodeImm32(DxbcCodeSlice& code, DxbcImmediate& imm, DxbcScalarType type);
void decodeOperand(DxbcCodeSlice& code, const DxbcInstOperandFormat& format);
};
}
@@ -0,0 +1,104 @@
#pragma once
#include "dxbc_enums.h"
namespace dxvk {
constexpr size_t DxbcMaxInterfaceRegs = 32;
constexpr size_t DxbcMaxOperandCount = 8;
/**
* \brief Operand kind
*
* In the instruction format definition, this specified
* whether an operand uses an actual operand token, or
* whether it is stored as an immediate value.
*/
enum class DxbcOperandKind {
DstReg, ///< Destination register
SrcReg, ///< Source register
Imm32, ///< Constant number
};
/**
* \brief Instruction class
*
* Instructions with a similar format are grouped into
* instruction classes in order to make implementing
* new instructions easier.
*/
enum class DxbcInstClass {
Declaration, ///< Interface or resource declaration
CustomData, ///< Immediate constant buffer
ControlFlow, ///< Control flow instructions
GeometryEmit, ///< Special geometry shader instructions
Atomic, ///< Atomic operations
AtomicCounter, ///< Atomic counter operations
Barrier, ///< Execution or memory barrier
BitExtract, ///< Bit field extract operations
BitInsert, ///< Bit field insert operations
BitScan, ///< Bit scan operations
BufferQuery, ///< Buffer query instruction
BufferLoad, ///< Structured or raw buffer load
BufferStore, ///< Structured or raw buffer store
ConvertFloat16, ///< 16-bit float packing/unpacking
ConvertFloat64, ///< 64-bit float conversion
HullShaderPhase, ///< Hull shader phase declaration
HullShaderInstCnt, ///< Hull shader phase instance count
Interpolate, ///< Input attribute interpolation
NoOperation, ///< The most useful instruction class
SparseCheckAccess, ///< Verifies sparse resource access
TextureQuery, ///< Texture query instruction
TextureQueryLod, ///< Texture LOD query instruction
TextureQueryMs, ///< Multisample texture query
TextureQueryMsPos, ///< Sample position query
TextureFetch, ///< Texture fetch instruction
TextureGather, ///< Texture gather instruction
TextureSample, ///< Texture sampling instruction
TypedUavLoad, ///< Typed UAV load
TypedUavStore, ///< Typed UAV store
VectorAlu, ///< Component-wise vector instructions
VectorCmov, ///< Component-wise conditional move
VectorCmp, ///< Component-wise vector comparison
VectorDeriv, ///< Vector derivatives
VectorDot, ///< Dot product instruction
VectorIdiv, ///< Component-wise integer division
VectorImul, ///< Component-wise integer multiplication
VectorMsad, ///< Component-wise sum of absolute difference
VectorShift, ///< Bit shift operations on vectors
VectorSinCos, ///< Sine and Cosine instruction
Undefined, ///< Instruction code not defined
};
/**
* \brief Instruction operand format
*
* Stores the kind and the expected data type
* of an operand. Used when parsing instructions.
*/
struct DxbcInstOperandFormat {
DxbcOperandKind kind;
DxbcScalarType type;
};
/**
* \brief Instruction format
*
* Defines the instruction class as well as
* the format of the insttruction operands.
*/
struct DxbcInstFormat {
uint32_t operandCount = 0;
DxbcInstClass instructionClass = DxbcInstClass::Undefined;
DxbcInstOperandFormat operands[DxbcMaxOperandCount];
};
/**
* \brief Retrieves instruction format info
*
* \param [in] opcode The opcode to retrieve
* \returns Instruction format info
*/
DxbcInstFormat dxbcInstructionFormat(DxbcOpcode opcode);
}
@@ -0,0 +1,654 @@
#pragma once
#include "dxbc_include.h"
namespace dxvk {
/**
* \brief Instruction code listing
*/
enum class DxbcOpcode : uint32_t {
Add = 0,
And = 1,
Break = 2,
Breakc = 3,
Call = 4,
Callc = 5,
Case = 6,
Continue = 7,
Continuec = 8,
Cut = 9,
Default = 10,
DerivRtx = 11,
DerivRty = 12,
Discard = 13,
Div = 14,
Dp2 = 15,
Dp3 = 16,
Dp4 = 17,
Else = 18,
Emit = 19,
EmitThenCut = 20,
EndIf = 21,
EndLoop = 22,
EndSwitch = 23,
Eq = 24,
Exp = 25,
Frc = 26,
FtoI = 27,
FtoU = 28,
Ge = 29,
IAdd = 30,
If = 31,
IEq = 32,
IGe = 33,
ILt = 34,
IMad = 35,
IMax = 36,
IMin = 37,
IMul = 38,
INe = 39,
INeg = 40,
IShl = 41,
IShr = 42,
ItoF = 43,
Label = 44,
Ld = 45,
LdMs = 46,
Log = 47,
Loop = 48,
Lt = 49,
Mad = 50,
Min = 51,
Max = 52,
CustomData = 53,
Mov = 54,
Movc = 55,
Mul = 56,
Ne = 57,
Nop = 58,
Not = 59,
Or = 60,
ResInfo = 61,
Ret = 62,
Retc = 63,
RoundNe = 64,
RoundNi = 65,
RoundPi = 66,
RoundZ = 67,
Rsq = 68,
Sample = 69,
SampleC = 70,
SampleClz = 71,
SampleL = 72,
SampleD = 73,
SampleB = 74,
Sqrt = 75,
Switch = 76,
SinCos = 77,
UDiv = 78,
ULt = 79,
UGe = 80,
UMul = 81,
UMad = 82,
UMax = 83,
UMin = 84,
UShr = 85,
UtoF = 86,
Xor = 87,
DclResource = 88,
DclConstantBuffer = 89,
DclSampler = 90,
DclIndexRange = 91,
DclGsOutputPrimitiveTopology = 92,
DclGsInputPrimitive = 93,
DclMaxOutputVertexCount = 94,
DclInput = 95,
DclInputSgv = 96,
DclInputSiv = 97,
DclInputPs = 98,
DclInputPsSgv = 99,
DclInputPsSiv = 100,
DclOutput = 101,
DclOutputSgv = 102,
DclOutputSiv = 103,
DclTemps = 104,
DclIndexableTemp = 105,
DclGlobalFlags = 106,
Reserved0 = 107,
Lod = 108,
Gather4 = 109,
SamplePos = 110,
SampleInfo = 111,
Reserved1 = 112,
HsDecls = 113,
HsControlPointPhase = 114,
HsForkPhase = 115,
HsJoinPhase = 116,
EmitStream = 117,
CutStream = 118,
EmitThenCutStream = 119,
InterfaceCall = 120,
BufInfo = 121,
DerivRtxCoarse = 122,
DerivRtxFine = 123,
DerivRtyCoarse = 124,
DerivRtyFine = 125,
Gather4C = 126,
Gather4Po = 127,
Gather4PoC = 128,
Rcp = 129,
F32toF16 = 130,
F16toF32 = 131,
UAddc = 132,
USubb = 133,
CountBits = 134,
FirstBitHi = 135,
FirstBitLo = 136,
FirstBitShi = 137,
UBfe = 138,
IBfe = 139,
Bfi = 140,
BfRev = 141,
Swapc = 142,
DclStream = 143,
DclFunctionBody = 144,
DclFunctionTable = 145,
DclInterface = 146,
DclInputControlPointCount = 147,
DclOutputControlPointCount = 148,
DclTessDomain = 149,
DclTessPartitioning = 150,
DclTessOutputPrimitive = 151,
DclHsMaxTessFactor = 152,
DclHsForkPhaseInstanceCount = 153,
DclHsJoinPhaseInstanceCount = 154,
DclThreadGroup = 155,
DclUavTyped = 156,
DclUavRaw = 157,
DclUavStructured = 158,
DclThreadGroupSharedMemoryRaw = 159,
DclThreadGroupSharedMemoryStructured = 160,
DclResourceRaw = 161,
DclResourceStructured = 162,
LdUavTyped = 163,
StoreUavTyped = 164,
LdRaw = 165,
StoreRaw = 166,
LdStructured = 167,
StoreStructured = 168,
AtomicAnd = 169,
AtomicOr = 170,
AtomicXor = 171,
AtomicCmpStore = 172,
AtomicIAdd = 173,
AtomicIMax = 174,
AtomicIMin = 175,
AtomicUMax = 176,
AtomicUMin = 177,
ImmAtomicAlloc = 178,
ImmAtomicConsume = 179,
ImmAtomicIAdd = 180,
ImmAtomicAnd = 181,
ImmAtomicOr = 182,
ImmAtomicXor = 183,
ImmAtomicExch = 184,
ImmAtomicCmpExch = 185,
ImmAtomicIMax = 186,
ImmAtomicIMin = 187,
ImmAtomicUMax = 188,
ImmAtomicUMin = 189,
Sync = 190,
DAdd = 191,
DMax = 192,
DMin = 193,
DMul = 194,
DEq = 195,
DGe = 196,
DLt = 197,
DNe = 198,
DMov = 199,
DMovc = 200,
DtoF = 201,
FtoD = 202,
EvalSnapped = 203,
EvalSampleIndex = 204,
EvalCentroid = 205,
DclGsInstanceCount = 206,
Abort = 207,
DebugBreak = 208,
ReservedBegin11_1 = 209,
DDiv = 210,
DFma = 211,
DRcp = 212,
Msad = 213,
DtoI = 214,
DtoU = 215,
ItoD = 216,
UtoD = 217,
ReservedBegin11_2 = 218,
Gather4S = 219,
Gather4CS = 220,
Gather4PoS = 221,
Gather4PoCS = 222,
LdS = 223,
LdMsS = 224,
LdUavTypedS = 225,
LdRawS = 226,
LdStructuredS = 227,
SampleLS = 228,
SampleClzS = 229,
SampleClampS = 230,
SampleBClampS = 231,
SampleDClampS = 232,
SampleCClampS = 233,
CheckAccessFullyMapped = 234,
};
/**
* \brief Extended opcode
*/
enum class DxbcExtOpcode : uint32_t {
Empty = 0,
SampleControls = 1,
ResourceDim = 2,
ResourceReturnType = 3,
};
/**
* \brief Operand type
*
* Selects the 'register file' from which
* to retrieve an operand's value.
*/
enum class DxbcOperandType : uint32_t {
Temp = 0,
Input = 1,
Output = 2,
IndexableTemp = 3,
Imm32 = 4,
Imm64 = 5,
Sampler = 6,
Resource = 7,
ConstantBuffer = 8,
ImmediateConstantBuffer = 9,
Label = 10,
InputPrimitiveId = 11,
OutputDepth = 12,
Null = 13,
Rasterizer = 14,
OutputCoverageMask = 15,
Stream = 16,
FunctionBody = 17,
FunctionTable = 18,
Interface = 19,
FunctionInput = 20,
FunctionOutput = 21,
OutputControlPointId = 22,
InputForkInstanceId = 23,
InputJoinInstanceId = 24,
InputControlPoint = 25,
OutputControlPoint = 26,
InputPatchConstant = 27,
InputDomainPoint = 28,
ThisPointer = 29,
UnorderedAccessView = 30,
ThreadGroupSharedMemory = 31,
InputThreadId = 32,
InputThreadGroupId = 33,
InputThreadIdInGroup = 34,
InputCoverageMask = 35,
InputThreadIndexInGroup = 36,
InputGsInstanceId = 37,
OutputDepthGe = 38,
OutputDepthLe = 39,
CycleCounter = 40,
OutputStencilRef = 41,
InputInnerCoverage = 42,
};
/**
* \brief Number of components
*
* Used by operands to determine whether the
* operand has one, four or zero components.
*/
enum class DxbcComponentCount : uint32_t {
Component0 = 0,
Component1 = 1,
Component4 = 2,
};
/**
* \brief Component selection mode
*
* When an operand has four components, the
* component selection mode deterines which
* components are used for the operation.
*/
enum class DxbcRegMode : uint32_t {
Mask = 0,
Swizzle = 1,
Select1 = 2,
};
/**
* \brief Index representation
*
* Determines how an operand
* register index is stored.
*/
enum class DxbcOperandIndexRepresentation : uint32_t {
Imm32 = 0,
Imm64 = 1,
Relative = 2,
Imm32Relative = 3,
Imm64Relative = 4,
};
/**
* \brief Extended operand type
*/
enum class DxbcOperandExt : uint32_t {
OperandModifier = 1,
};
/**
* \brief Resource dimension
* The type of a resource.
*/
enum class DxbcResourceDim : uint32_t {
Unknown = 0,
Buffer = 1,
Texture1D = 2,
Texture2D = 3,
Texture2DMs = 4,
Texture3D = 5,
TextureCube = 6,
Texture1DArr = 7,
Texture2DArr = 8,
Texture2DMsArr = 9,
TextureCubeArr = 10,
RawBuffer = 11,
StructuredBuffer = 12,
};
/**
* \brief Resource return type
* Data type for resource read ops.
*/
enum class DxbcResourceReturnType : uint32_t {
Unorm = 1,
Snorm = 2,
Sint = 3,
Uint = 4,
Float = 5,
Mixed = 6, /// ?
Double = 7,
Continued = 8, /// ?
Unused = 9, /// ?
};
/**
* \brief Register component type
* Data type of a register component.
*/
enum class DxbcRegisterComponentType : uint32_t {
Unknown = 0,
Uint32 = 1,
Sint32 = 2,
Float32 = 3,
};
/**
* \brief Instruction return type
*/
enum class DxbcInstructionReturnType : uint32_t {
Float = 0,
Uint = 1,
};
enum class DxbcSystemValue : uint32_t {
None = 0,
Position = 1,
ClipDistance = 2,
CullDistance = 3,
RenderTargetId = 4,
ViewportId = 5,
VertexId = 6,
PrimitiveId = 7,
InstanceId = 8,
IsFrontFace = 9,
SampleIndex = 10,
FinalQuadUeq0EdgeTessFactor = 11,
FinalQuadVeq0EdgeTessFactor = 12,
FinalQuadUeq1EdgeTessFactor = 13,
FinalQuadVeq1EdgeTessFactor = 14,
FinalQuadUInsideTessFactor = 15,
FinalQuadVInsideTessFactor = 16,
FinalTriUeq0EdgeTessFactor = 17,
FinalTriVeq0EdgeTessFactor = 18,
FinalTriWeq0EdgeTessFactor = 19,
FinalTriInsideTessFactor = 20,
FinalLineDetailTessFactor = 21,
FinalLineDensityTessFactor = 22,
Target = 64,
Depth = 65,
Coverage = 66,
DepthGe = 67,
DepthLe = 68
};
enum class DxbcInterpolationMode : uint32_t {
Undefined = 0,
Constant = 1,
Linear = 2,
LinearCentroid = 3,
LinearNoPerspective = 4,
LinearNoPerspectiveCentroid = 5,
LinearSample = 6,
LinearNoPerspectiveSample = 7,
};
enum class DxbcGlobalFlag : uint32_t {
RefactoringAllowed = 0,
DoublePrecision = 1,
EarlyFragmentTests = 2,
RawStructuredBuffers = 3,
};
using DxbcGlobalFlags = Flags<DxbcGlobalFlag>;
enum class DxbcZeroTest : uint32_t {
TestZ = 0,
TestNz = 1,
};
enum class DxbcResinfoType : uint32_t {
Float = 0,
RcpFloat = 1,
Uint = 2,
};
enum class DxbcSyncFlag : uint32_t {
ThreadsInGroup = 0,
ThreadGroupSharedMemory = 1,
UavMemoryGroup = 2,
UavMemoryGlobal = 3,
};
using DxbcSyncFlags = Flags<DxbcSyncFlag>;
/**
* \brief Geometry shader input primitive
*/
enum class DxbcPrimitive : uint32_t {
Undefined = 0,
Point = 1,
Line = 2,
Triangle = 3,
LineAdj = 6,
TriangleAdj = 7,
Patch1 = 8,
Patch2 = 9,
Patch3 = 10,
Patch4 = 11,
Patch5 = 12,
Patch6 = 13,
Patch7 = 14,
Patch8 = 15,
Patch9 = 16,
Patch10 = 17,
Patch11 = 18,
Patch12 = 19,
Patch13 = 20,
Patch14 = 21,
Patch15 = 22,
Patch16 = 23,
Patch17 = 24,
Patch18 = 25,
Patch19 = 26,
Patch20 = 27,
Patch21 = 28,
Patch22 = 29,
Patch23 = 30,
Patch24 = 31,
Patch25 = 32,
Patch26 = 33,
Patch27 = 34,
Patch28 = 35,
Patch29 = 36,
Patch30 = 37,
Patch31 = 38,
Patch32 = 39,
};
/**
* \brief Geometry shader output topology
*/
enum class DxbcPrimitiveTopology : uint32_t {
Undefined = 0,
PointList = 1,
LineList = 2,
LineStrip = 3,
TriangleList = 4,
TriangleStrip = 5,
LineListAdj = 10,
LineStripAdj = 11,
TriangleListAdj = 12,
TriangleStripAdj = 13,
};
/**
* \brief Sampler operation mode
*/
enum class DxbcSamplerMode : uint32_t {
Default = 0,
Comparison = 1,
Mono = 2,
};
/**
* \brief Scalar value type
*
* Enumerates possible register component
* types. Scalar types are represented as
* a one-component vector type.
*/
enum class DxbcScalarType : uint32_t {
Uint32 = 0,
Uint64 = 1,
Sint32 = 2,
Sint64 = 3,
Float32 = 4,
Float64 = 5,
Bool = 6,
};
/**
* \brief Tessellator domain
*/
enum class DxbcTessDomain : uint32_t {
Undefined = 0,
Isolines = 1,
Triangles = 2,
Quads = 3,
};
/**
* \brief Tessellator partitioning
*/
enum class DxbcTessPartitioning : uint32_t {
Undefined = 0,
Integer = 1,
Pow2 = 2,
FractOdd = 3,
FractEven = 4,
};
/**
* \brief UAV definition flags
*/
enum class DxbcUavFlag : uint32_t {
GloballyCoherent = 0,
RasterizerOrdered = 1,
};
using DxbcUavFlags = Flags<DxbcUavFlag>;
/**
* \brief Tessellator output primitive
*/
enum class DxbcTessOutputPrimitive : uint32_t {
Undefined = 0,
Point = 1,
Line = 2,
TriangleCw = 3,
TriangleCcw = 4,
};
/**
* \brief Custom data class
*
* Stores which type of custom data is
* referenced by the instruction.
*/
enum class DxbcCustomDataClass : uint32_t {
Comment = 0,
DebugInfo = 1,
Opaque = 2,
ImmConstBuf = 3,
};
enum class DxbcResourceType : uint32_t {
Typed = 0,
Raw = 1,
Structured = 2,
};
enum class DxbcConstantBufferAccessType : uint32_t {
StaticallyIndexed = 0,
DynamicallyIndexed = 1,
};
}
@@ -0,0 +1,48 @@
#pragma once
#include <vector>
#include "dxbc_reader.h"
namespace dxvk {
/**
* \brief DXBC header
*
* Stores information about the shader file itself
* and the data chunks stored inside the file.
*/
class DxbcHeader {
public:
DxbcHeader(DxbcReader& reader);
~DxbcHeader();
/**
* \brief Number of chunks
* \returns Chunk count
*/
uint32_t numChunks() const {
return m_chunkOffsets.size();
}
/**
* \brief Chunk offset
*
* Retrieves the offset of a chunk, in
* bytes, from the start of the file.
* \param [in] chunkId Chunk index
* \returns Byte offset of that chunk
*/
uint32_t chunkOffset(uint32_t chunkId) const {
return m_chunkOffsets.at(chunkId);
}
private:
std::vector<uint32_t> m_chunkOffsets;
};
}
@@ -0,0 +1,20 @@
#pragma once
#include <vulkan/vulkan.h>
#include <thirdparty/spirv.hpp>
#include "dxvk_limits.h"
#include "dxvk_pipelayout.h"
#include "log/log.h"
#include "log/log_debug.h"
#include "rc/util_rc.h"
#include "rc/util_rc_ptr.h"
#include "util_bit.h"
#include "util_enum.h"
#include "util_error.h"
#include "util_string.h"
#include "util_flags.h"
#include "util_small_vector.h"
@@ -0,0 +1,59 @@
#pragma once
#include "dxbc_options.h"
namespace dxvk {
/**
* \brief Tessellation info
*
* Stores the maximum tessellation factor
* to export from tessellation shaders.
*/
struct DxbcTessInfo {
float maxTessFactor;
};
/**
* \brief Xfb capture entry
*
* Stores an output variable to capture,
* as well as the buffer to write it to.
*/
struct DxbcXfbEntry {
const char* semanticName;
uint32_t semanticIndex;
uint32_t componentIndex;
uint32_t componentCount;
uint32_t streamId;
uint32_t bufferId;
uint32_t offset;
};
/**
* \brief Xfb info
*
* Stores capture entries and output buffer
* strides. This structure must only be
* defined if \c entryCount is non-zero.
*/
struct DxbcXfbInfo {
uint32_t entryCount;
DxbcXfbEntry entries[128];
uint32_t strides[4];
int32_t rasterizedStream;
};
/**
* \brief Shader module info
*
* Stores information which may affect shader compilation.
* This data can be supplied by the client API implementation.
*/
struct DxbcModuleInfo {
DxbcOptions options;
DxbcTessInfo* tess;
DxbcXfbInfo* xfb;
};
}
@@ -0,0 +1,137 @@
#pragma once
#include "dxbc_chunk_isgn.h"
#include "dxbc_chunk_shex.h"
#include "dxbc_header.h"
#include "dxbc_modinfo.h"
#include "dxbc_reader.h"
#include "dxbc_util.h"
#include "spirv_code_buffer.h"
#include <optional>
// References used for figuring out DXBC:
// - https://github.com/tgjones/slimshader-cpp
// - Wine
namespace dxvk {
class DxbcAnalyzer;
class DxbcCompiler;
/**
* \brief Immediate constant buffer properties
*/
struct DxbcIcbInfo {
size_t size = 0u;
const void* data = nullptr;
};
/**
* \brief DXBC shader module
*
* Reads the DXBC byte code and extracts information
* about the resource bindings and the instruction
* stream. A module can then be compiled to SPIR-V.
*/
class DxbcModule {
public:
DxbcModule(DxbcReader& reader);
~DxbcModule();
/**
* \brief Shader type
* \returns Shader type
*/
std::optional<DxbcProgramInfo> programInfo() const {
if (m_shexChunk == nullptr)
return std::nullopt;
return m_shexChunk->programInfo();
}
/**
* \brief Queries shader binding mask
*
* Only valid after successfully compiling the shader.
*/
std::optional<DxbcBindingMask> bindings() const {
return m_bindings;
}
/**
* \brief Retrieves immediate constant buffer info
*
* Only valid after successfully compiling the shader.
* \returns Immediate constant buffer data
*/
DxbcIcbInfo icbInfo() const {
DxbcIcbInfo result = { };
result.size = m_icb.size() * sizeof(uint32_t);
result.data = m_icb.data();
return result;
}
/**
* \brief Input and output signature chunks
*
* Parts of the D3D11 API need access to the
* input or output signature of the shader.
*/
Rc<DxbcIsgn> isgn() const { return m_isgnChunk; }
Rc<DxbcIsgn> osgn() const { return m_osgnChunk; }
/**
* \brief Compiles DXBC shader to SPIR-V module
*
* \param [in] moduleInfo DXBC module info
* \param [in] fileName File name, will be added to
* the compiled SPIR-V for debugging purposes.
* \returns The compiled shader object
*/
SpirvCodeBuffer compile(
const DxbcModuleInfo& moduleInfo,
const std::string& fileName);
/**
* \brief Compiles a pass-through geometry shader
*
* Applications can pass a vertex shader to create
* a geometry shader with stream output. In this
* case, we have to create a passthrough geometry
* shader, which operates in point to point mode.
* \param [in] moduleInfo DXBC module info
* \param [in] fileName SPIR-V shader name
*/
SpirvCodeBuffer compilePassthroughShader(
const DxbcModuleInfo& moduleInfo,
const std::string& fileName) const;
private:
DxbcHeader m_header;
Rc<DxbcIsgn> m_isgnChunk;
Rc<DxbcIsgn> m_osgnChunk;
Rc<DxbcIsgn> m_psgnChunk;
Rc<DxbcShex> m_shexChunk;
std::vector<uint32_t> m_icb;
std::optional<DxbcBindingMask> m_bindings;
void runAnalyzer(
DxbcAnalyzer& analyzer,
DxbcCodeSlice slice) const;
void runCompiler(
DxbcCompiler& compiler,
DxbcCodeSlice slice) const;
};
}
@@ -0,0 +1,26 @@
#pragma once
#include <ostream>
#include "dxbc_common.h"
#include "dxbc_enums.h"
namespace dxvk {
std::ostream& operator << (std::ostream& os, DxbcOpcode e);
std::ostream& operator << (std::ostream& os, DxbcExtOpcode e);
std::ostream& operator << (std::ostream& os, DxbcOperandType e);
std::ostream& operator << (std::ostream& os, DxbcOperandExt e);
std::ostream& operator << (std::ostream& os, DxbcComponentCount e);
std::ostream& operator << (std::ostream& os, DxbcRegMode e);
std::ostream& operator << (std::ostream& os, DxbcOperandIndexRepresentation e);
std::ostream& operator << (std::ostream& os, DxbcResourceDim e);
std::ostream& operator << (std::ostream& os, DxbcResourceReturnType e);
std::ostream& operator << (std::ostream& os, DxbcRegisterComponentType e);
std::ostream& operator << (std::ostream& os, DxbcInstructionReturnType e);
std::ostream& operator << (std::ostream& os, DxbcSystemValue e);
std::ostream& operator << (std::ostream& os, DxbcProgramType e);
std::ostream& operator << (std::ostream& os, DxbcCustomDataClass e);
std::ostream& operator << (std::ostream& os, DxbcScalarType e);
} // namespace dxvk
@@ -0,0 +1,74 @@
#pragma once
#include <vulkan/vulkan.h>
#include "util_flags.h"
namespace dxvk {
struct D3D11Options;
enum class DxbcFloatControlFlag : uint32_t {
DenormFlushToZero32,
DenormPreserve64,
PreserveNan32,
PreserveNan64,
};
using DxbcFloatControlFlags = Flags<DxbcFloatControlFlag>;
struct DxbcOptions {
DxbcOptions() {}
// Clamp oDepth in fragment shaders if the depth
// clip device feature is not supported
bool useDepthClipWorkaround = false;
/// Determines whether format qualifiers
/// on typed UAV loads are required
bool supportsTypedUavLoadR32 = false;
/// Determines whether raw access chains are supported
bool supportsRawAccessChains = false;
/// Clear thread-group shared memory to zero
bool zeroInitWorkgroupMemory = false;
/// Declare vertex positions as invariant
bool invariantPosition = false;
/// Insert memory barriers after TGSM stoes
bool forceVolatileTgsmAccess = false;
/// Try to detect hazards in UAV access and insert
/// barriers when we know control flow is uniform.
bool forceComputeUavBarriers = false;
/// Replace ld_ms with ld
bool disableMsaa = false;
/// Force sample rate shading by using sample
/// interpolation for fragment shader inputs
bool forceSampleRateShading = false;
// Enable per-sample interlock if supported
bool enableSampleShadingInterlock = false;
/// Use tightly packed arrays for immediate
/// constant buffers if possible
bool supportsTightIcbPacking = false;
/// Whether exporting point size is required
bool needsPointSizeExport = true;
/// Whether to enable sincos emulation
bool sincosEmulation = false;
/// Float control flags
DxbcFloatControlFlags floatControl;
/// Minimum storage buffer alignment
VkDeviceSize minSsboAlignment = 0;
};
}
@@ -0,0 +1,78 @@
#pragma once
#include <cstddef>
#include <string>
#include "dxbc_tag.h"
namespace dxvk {
/**
* \brief DXBC bytecode reader
*
* Holds references to the shader byte code and
* provides methods to read
*/
class DxbcReader {
public:
DxbcReader(const char* data, size_t size)
: DxbcReader(data, size, 0) { }
auto readu8 () { return this->readNum<uint8_t> (); }
auto readu16() { return this->readNum<uint16_t>(); }
auto readu32() { return this->readNum<uint32_t>(); }
auto readu64() { return this->readNum<uint64_t>(); }
auto readi8 () { return this->readNum<int8_t> (); }
auto readi16() { return this->readNum<int16_t> (); }
auto readi32() { return this->readNum<int32_t> (); }
auto readi64() { return this->readNum<int64_t> (); }
auto readf32() { return this->readNum<float> (); }
auto readf64() { return this->readNum<double> (); }
template<typename T>
auto readEnum() {
using Tx = std::underlying_type_t<T>;
return static_cast<T>(this->readNum<Tx>());
}
DxbcTag readTag();
std::string readString();
void read(void* dst, size_t n);
void skip(size_t n);
DxbcReader clone(size_t pos) const;
DxbcReader resize(size_t size) const;
bool eof() const {
return m_pos >= m_size;
}
void store(std::ostream&& stream) const;
private:
DxbcReader(const char* data, size_t size, size_t pos)
: m_data(data), m_size(size), m_pos(pos) { }
const char* m_data = nullptr;
size_t m_size = 0;
size_t m_pos = 0;
template<typename T>
T readNum() {
T result;
this->read(&result, sizeof(result));
return result;
}
};
}
@@ -0,0 +1,47 @@
#pragma once
#include "dxbc_include.h"
namespace dxvk {
/**
* \brief Four-character tag
*
* Used to identify chunks in the
* compiled DXBC file by name.
*/
class DxbcTag {
public:
DxbcTag() {
for (size_t i = 0; i < 4; i++)
m_chars[i] = '\0';
}
DxbcTag(const char* tag) {
for (size_t i = 0; i < 4; i++)
m_chars[i] = tag[i];
}
bool operator == (const DxbcTag& other) const {
bool result = true;
for (size_t i = 0; i < 4; i++)
result &= m_chars[i] == other.m_chars[i];
return result;
}
bool operator != (const DxbcTag& other) const {
return !this->operator == (other);
}
const char* operator & () const { return m_chars; }
char* operator & () { return m_chars; }
private:
char m_chars[4];
};
}
@@ -0,0 +1,164 @@
#pragma once
#include "dxbc_common.h"
#include "dxbc_enums.h"
namespace dxvk {
/**
* \brief Push constant struct
*/
struct DxbcPushConstants {
uint32_t rasterizerSampleCount;
};
/**
* \brief Binding numbers and properties
*/
enum DxbcBindingProperties : uint32_t {
DxbcConstBufBindingIndex = 0,
DxbcConstBufBindingCount = 16,
DxbcSamplerBindingIndex = DxbcConstBufBindingIndex
+ DxbcConstBufBindingCount,
DxbcSamplerBindingCount = 16,
DxbcResourceBindingIndex = DxbcSamplerBindingIndex
+ DxbcSamplerBindingCount,
DxbcResourceBindingCount = 128,
DxbcStageBindingCount = DxbcConstBufBindingCount
+ DxbcSamplerBindingCount
+ DxbcResourceBindingCount,
DxbcUavBindingIndex = DxbcStageBindingCount * 6,
DxbcUavBindingCount = 64,
};
/**
* \brief Shader binding mask
*
* Stores a bit masks of resource bindings
* that are accessed by any given shader.
*/
struct DxbcBindingMask {
uint32_t cbvMask = 0u;
uint32_t samplerMask = 0u;
uint64_t uavMask = 0u;
std::array<uint64_t, 2> srvMask = { };
void reset() {
cbvMask = 0u;
samplerMask = 0u;
uavMask = 0u;
srvMask = { };
}
bool empty() const {
uint64_t mask = (uint64_t(cbvMask) | uint64_t(samplerMask) << 32u)
| (uavMask | srvMask[0] | srvMask[1]);
return !mask;
}
DxbcBindingMask operator & (const DxbcBindingMask& other) const {
DxbcBindingMask result = *this;
result.cbvMask &= other.cbvMask;
result.samplerMask &= other.samplerMask;
result.uavMask &= other.uavMask;
result.srvMask[0] &= other.srvMask[0];
result.srvMask[1] &= other.srvMask[1];
return result;
}
};
/**
* \brief Computes first binding index for a given stage
*
* \param [in] stage The shader stage
* \returns Index of first binding
*/
inline uint32_t computeStageBindingOffset(DxbcProgramType stage) {
return DxbcStageBindingCount * uint32_t(stage);
}
/**
* \brief Computes first UAV binding index offset for a given stage
*
* \param [in] stage The shader stage
* \returns Index of first UAV binding
*/
inline uint32_t computeStageUavBindingOffset(DxbcProgramType stage) {
return DxbcUavBindingIndex
+ DxbcUavBindingCount * (stage == DxbcProgramType::ComputeShader ? 2 : 0);
}
/**
* \brief Computes constant buffer binding index
*
* \param [in] stage Shader stage
* \param [in] index Constant buffer index
* \returns Binding index
*/
inline uint32_t computeConstantBufferBinding(DxbcProgramType stage, uint32_t index) {
return computeStageBindingOffset(stage) + DxbcConstBufBindingIndex + index;
}
/**
* \brief Computes sampler binding index
*
* \param [in] stage Shader stage
* \param [in] index Sampler index
* \returns Binding index
*/
inline uint32_t computeSamplerBinding(DxbcProgramType stage, uint32_t index) {
return computeStageBindingOffset(stage) + DxbcSamplerBindingIndex + index;
}
/**
* \brief Computes resource binding index
*
* \param [in] stage Shader stage
* \param [in] index Resource index
* \returns Binding index
*/
inline uint32_t computeSrvBinding(DxbcProgramType stage, uint32_t index) {
return computeStageBindingOffset(stage) + DxbcResourceBindingIndex + index;
}
/**
* \brief Computes UAV binding offset
*
* \param [in] stage Shader stage
* \param [in] index UAV index
* \returns Binding index
*/
inline uint32_t computeUavBinding(DxbcProgramType stage, uint32_t index) {
return computeStageUavBindingOffset(stage) + index;
}
/**
* \brief Computes UAV counter binding offset
*
* \param [in] stage Shader stage
* \param [in] index UAV index
* \returns Binding index
*/
inline uint32_t computeUavCounterBinding(DxbcProgramType stage, uint32_t index) {
return computeStageUavBindingOffset(stage) + DxbcUavBindingCount + index;
}
/**
* \brief Primitive vertex count
*
* Calculates the number of vertices
* for a given primitive type.
*/
uint32_t primitiveVertexCount(
DxbcPrimitive primitive);
}
@@ -0,0 +1,41 @@
#pragma once
#include <cstddef>
namespace dxvk {
struct DxvkEq {
template<typename T>
size_t operator () (const T& a, const T& b) const {
return a.eq(b);
}
};
struct DxvkHash {
template<typename T>
size_t operator () (const T& object) const {
return object.hash();
}
};
class DxvkHashState {
public:
void add(size_t hash) {
m_value ^= hash + 0x9e3779b9
+ (m_value << 6)
+ (m_value >> 2);
}
operator size_t () const {
return m_value;
}
private:
size_t m_value = 0;
};
}
@@ -0,0 +1,23 @@
#pragma once
#include <cstddef>
namespace dxvk {
enum DxvkLimits : size_t {
MaxNumRenderTargets = 8,
MaxNumVertexAttributes = 32,
MaxNumVertexBindings = 32,
MaxNumXfbBuffers = 4,
MaxNumXfbStreams = 4,
MaxNumViewports = 16,
MaxNumResourceSlots = 1216,
MaxNumQueuedCommandBuffers = 32,
MaxNumQueryCountPerPool = 128,
MaxNumSpecConstants = 12,
MaxUniformBufferSize = 65536,
MaxVertexBindingStride = 2048,
MaxPushConstantSize = 128,
};
}
@@ -0,0 +1,107 @@
#pragma once
#include <vulkan/vulkan.h>
#include <cstdint>
#include "dxvk_hash.h"
#include "util_math.h"
#include "util_bit.h"
#include "util_flags.h"
namespace dxvk {
class DxvkDevice;
class DxvkPipelineManager;
/**
* \brief Order-invariant atomic access operation
*
* Information used to optimize barriers when a resource
* is accessed exlusively via order-invariant stores.
*/
struct DxvkAccessOp {
enum OpType : uint16_t {
None = 0x0u,
Or = 0x1u,
And = 0x2u,
Xor = 0x3u,
Add = 0x4u,
IMin = 0x5u,
IMax = 0x6u,
UMin = 0x7u,
UMax = 0x8u,
StoreF = 0xdu,
StoreUi = 0xeu,
StoreSi = 0xfu,
};
DxvkAccessOp() = default;
DxvkAccessOp(OpType t)
: op(uint16_t(t)) { }
DxvkAccessOp(OpType t, uint16_t constant)
: op(uint16_t(t) | (constant << 4u)) { }
uint16_t op = 0u;
bool operator == (const DxvkAccessOp& t) const { return op == t.op; }
bool operator != (const DxvkAccessOp& t) const { return op != t.op; }
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
explicit operator T() const { return op; }
};
static_assert(sizeof(DxvkAccessOp) == sizeof(uint16_t));
/**
* \brief Binding info
*
* Stores metadata for a single binding in
* a given shader, or for the whole pipeline.
*/
struct DxvkBindingInfo {
VkDescriptorType descriptorType = VK_DESCRIPTOR_TYPE_MAX_ENUM; ///< Vulkan descriptor type
uint32_t resourceBinding = 0u; ///< API binding slot for the resource
VkImageViewType viewType = VK_IMAGE_VIEW_TYPE_MAX_ENUM; ///< Image view type
VkShaderStageFlagBits stage = VK_SHADER_STAGE_FLAG_BITS_MAX_ENUM; ///< Shader stage
VkAccessFlags access = 0u; ///< Access mask for the resource
DxvkAccessOp accessOp = DxvkAccessOp::None; ///< Order-invariant store type, if any
bool uboSet = false; ///< Whether to include this in the UBO set
bool isMultisampled = false; ///< Multisampled binding
/**
* \brief Computes descriptor set index for the given binding
*
* This is determines based on the shader stages that use the binding.
* \returns Descriptor set index
*/
uint32_t computeSetIndex() const;
/**
* \brief Numeric value of the binding
*
* Used when sorting bindings.
* \returns Numeric value
*/
uint32_t value() const;
/**
* \brief Checks for equality
*
* \param [in] other Binding to compare to
* \returns \c true if both bindings are equal
*/
bool eq(const DxvkBindingInfo& other) const;
/**
* \brief Hashes binding info
* \returns Binding hash
*/
size_t hash() const;
};
}
@@ -0,0 +1,236 @@
#pragma once
#include <iostream>
#include <utility>
#include <vector>
#include "spirv_instruction.h"
namespace dxvk {
/**
* \brief SPIR-V code buffer
*
* Helper class for generating SPIR-V shaders.
* Stores arbitrary SPIR-V instructions in a
* format that can be read by Vulkan drivers.
*/
class SpirvCodeBuffer {
public:
SpirvCodeBuffer();
explicit SpirvCodeBuffer(uint32_t size);
SpirvCodeBuffer(const SpirvCodeBuffer &) = default;
SpirvCodeBuffer(SpirvCodeBuffer &&) = default;
SpirvCodeBuffer(uint32_t size, const uint32_t* data);
SpirvCodeBuffer(std::istream& stream);
template<size_t N>
SpirvCodeBuffer(const uint32_t (&data)[N])
: SpirvCodeBuffer(N, data) { }
~SpirvCodeBuffer();
SpirvCodeBuffer &operator=(const SpirvCodeBuffer &) = default;
SpirvCodeBuffer &operator=(SpirvCodeBuffer &&) = default;
/**
* \brief Code data
* \returns Code data
*/
const uint32_t* data() const { return m_code.data(); }
uint32_t* data() { return m_code.data(); }
/**
* \brief Code size, in dwords
* \returns Code size, in dwords
*/
uint32_t dwords() const {
return m_code.size();
}
/**
* \brief Code size, in bytes
* \returns Code size, in bytes
*/
size_t size() const {
return m_code.size() * sizeof(uint32_t);
}
/**
* \brief Begin instruction iterator
*
* Points to the first instruction in the instruction
* block. The header, if any, will be skipped over.
* \returns Instruction iterator
*/
SpirvInstructionIterator begin() {
return SpirvInstructionIterator(
m_code.data(), 0, m_code.size());
}
/**
* \brief End instruction iterator
*
* Points to the end of the instruction block.
* \returns Instruction iterator
*/
SpirvInstructionIterator end() {
return SpirvInstructionIterator(nullptr, 0, 0);
}
/**
* \brief Allocates a new ID
*
* Returns a new valid ID and increments the
* maximum ID count stored in the header.
* \returns The new SPIR-V ID
*/
uint32_t allocId();
/**
* \brief Appends an instruction
*
* Slightly faster than individually adding words.
* \param [in] ins Instruction
*/
void append(const SpirvInstruction& ins);
/**
* \brief Merges two code buffers
*
* This is useful to generate declarations or
* the SPIR-V header at the same time as the
* code when doing so in advance is impossible.
* \param [in] other Code buffer to append
*/
void append(const SpirvCodeBuffer& other);
/**
* \brief Appends an 32-bit word to the buffer
* \param [in] word The word to append
*/
void putWord(uint32_t word);
/**
* \brief Appends an instruction word to the buffer
*
* Adds a single word containing both the word count
* and the op code number for a single instruction.
* \param [in] opCode Operand code
* \param [in] wordCount Number of words
*/
void putIns(spv::Op opCode, uint16_t wordCount);
/**
* \brief Appends a 32-bit integer to the buffer
* \param [in] value The number to add
*/
void putInt32(uint32_t word);
/**
* \brief Appends a 64-bit integer to the buffer
*
* A 64-bit integer will take up two 32-bit words.
* \param [in] value 64-bit value to add
*/
void putInt64(uint64_t value);
/**
* \brief Appends a 32-bit float to the buffer
* \param [in] value The number to add
*/
void putFloat32(float value);
/**
* \brief Appends a 64-bit float to the buffer
* \param [in] value The number to add
*/
void putFloat64(double value);
/**
* \brief Appends a literal string to the buffer
* \param [in] str String to append to the buffer
*/
void putStr(const char* str);
/**
* \brief Adds the header to the buffer
*
* \param [in] version SPIR-V version
* \param [in] boundIds Number of bound IDs
*/
void putHeader(uint32_t version, uint32_t boundIds);
/**
* \brief Erases given number of dwords
*
* Removes data from the code buffer, starting
* at the current insertion offset.
* \param [in] size Number of words to remove
*/
void erase(size_t size);
/**
* \brief Computes length of a literal string
*
* \param [in] str The string to check
* \returns Number of words consumed by a string
*/
uint32_t strLen(const char* str);
/**
* \brief Stores the SPIR-V module to a stream
*
* The ability to save modules to a file
* exists mostly for debugging purposes.
* \param [in] stream Output stream
*/
void store(std::ostream& stream) const;
/**
* \brief Retrieves current insertion pointer
*
* Sometimes it may be necessay to insert code into the
* middle of the stream rather than appending it. This
* retrieves the current function pointer. Note that the
* pointer will become invalid if any code is inserted
* before the current pointer location.
* \returns Current instruction pointr
*/
size_t getInsertionPtr() const {
return m_ptr;
}
/**
* \brief Sets insertion pointer to a specific value
*
* Sets the insertion pointer to a value that was
* previously retrieved by \ref getInsertionPtr.
* \returns Current instruction pointr
*/
void beginInsertion(size_t ptr) {
m_ptr = ptr;
}
/**
* \brief Sets insertion pointer to the end
*
* After this call, new instructions will be
* appended to the stream. In other words,
* this will restore default behaviour.
* \returns Previous instruction pointer
*/
size_t endInsertion() {
return std::exchange(m_ptr, m_code.size());
}
private:
std::vector<uint32_t> m_code;
size_t m_ptr = 0;
};
}
@@ -0,0 +1,15 @@
#pragma once
#include <thirdparty/spirv.hpp>
#include <thirdparty/GLSL.std.450.h>
#include "log/log.h"
#include "log/log_debug.h"
#include "util_error.h"
#include "util_flags.h"
#include "util_likely.h"
#include "util_string.h"
#include "rc/util_rc.h"
#include "rc/util_rc_ptr.h"
@@ -0,0 +1,158 @@
#pragma once
#include "spirv_include.h"
namespace dxvk {
/**
* \brief SPIR-V instruction
*
* Helps parsing a single instruction, providing
* access to the op code, instruction length and
* instruction arguments.
*/
class SpirvInstruction {
public:
SpirvInstruction() { }
SpirvInstruction(uint32_t* code, uint32_t offset, uint32_t length)
: m_code(code), m_offset(offset), m_length(length) { }
/**
* \brief SPIR-V Op code
* \returns The op code
*/
spv::Op opCode() const {
return static_cast<spv::Op>(
this->arg(0) & spv::OpCodeMask);
}
/**
* \brief Instruction length
* \returns Number of DWORDs
*/
uint32_t length() const {
return this->arg(0) >> spv::WordCountShift;
}
/**
* \brief Instruction offset
* \returns Offset in DWORDs
*/
uint32_t offset() const {
return m_offset;
}
/**
* \brief Argument value
*
* Retrieves an argument DWORD. Note that some instructions
* take 64-bit arguments which require more than one DWORD.
* Arguments start at index 1. Calling this method with an
* argument ID of 0 will return the opcode token.
* \param [in] idx Argument index, starting at 1
* \returns The argument value
*/
uint32_t arg(uint32_t idx) const {
const uint32_t index = m_offset + idx;
return index < m_length ? m_code[index] : 0;
}
/**
* \brief Argument string
*
* Retrieves a pointer to a UTF-8-encoded string.
* \param [in] idx Argument index, starting at 1
* \returns Pointer to the literal string
*/
const char* chr(uint32_t idx) const {
const uint32_t index = m_offset + idx;
return index < m_length ? reinterpret_cast<const char*>(&m_code[index]) : nullptr;
}
/**
* \brief Changes the value of an argument
*
* \param [in] idx Argument index, starting at 1
* \param [in] word New argument word
*/
void setArg(uint32_t idx, uint32_t word) const {
if (m_offset + idx < m_length)
m_code[m_offset + idx] = word;
}
private:
uint32_t* m_code = nullptr;
uint32_t m_offset = 0;
uint32_t m_length = 0;
};
/**
* \brief SPIR-V instruction iterator
*
* Convenient iterator that can be used
* to process raw SPIR-V shader code.
*/
class SpirvInstructionIterator {
public:
SpirvInstructionIterator() { }
SpirvInstructionIterator(uint32_t* code, uint32_t offset, uint32_t length)
: m_code (length != 0 ? code : nullptr),
m_offset(length != 0 ? offset : 0),
m_length(length) {
if ((length >= 5) && (offset == 0) && (m_code[0] == spv::MagicNumber))
this->advance(5);
}
SpirvInstructionIterator& operator ++ () {
this->advance(SpirvInstruction(m_code, m_offset, m_length).length());
return *this;
}
SpirvInstructionIterator operator ++ (int) {
SpirvInstructionIterator result = *this;
this->advance(SpirvInstruction(m_code, m_offset, m_length).length());
return result;
}
SpirvInstruction operator * () const {
return SpirvInstruction(m_code, m_offset, m_length);
}
bool operator == (const SpirvInstructionIterator& other) const {
return this->m_code == other.m_code
&& this->m_offset == other.m_offset
&& this->m_length == other.m_length;
}
bool operator != (const SpirvInstructionIterator& other) const {
return this->m_code != other.m_code
|| this->m_offset != other.m_offset
|| this->m_length != other.m_length;
}
private:
uint32_t* m_code = nullptr;
uint32_t m_offset = 0;
uint32_t m_length = 0;
void advance(uint32_t n) {
if (m_offset + n < m_length) {
m_offset += n;
} else {
m_code = nullptr;
m_offset = 0;
m_length = 0;
}
}
};
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,131 @@
/*
** Copyright (c) 2014-2024 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and/or associated documentation files (the "Materials"),
** to deal in the Materials without restriction, including without limitation
** the rights to use, copy, modify, merge, publish, distribute, sublicense,
** and/or sell copies of the Materials, and to permit persons to whom the
** Materials are 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 Materials.
**
** MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS KHRONOS
** STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS SPECIFICATIONS AND
** HEADER INFORMATION ARE LOCATED AT https://www.khronos.org/registry/
**
** THE MATERIALS ARE 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 MATERIALS OR THE USE OR OTHER DEALINGS
** IN THE MATERIALS.
*/
#ifndef GLSLstd450_H
#define GLSLstd450_H
static const int GLSLstd450Version = 100;
static const int GLSLstd450Revision = 3;
enum GLSLstd450 {
GLSLstd450Bad = 0, // Don't use
GLSLstd450Round = 1,
GLSLstd450RoundEven = 2,
GLSLstd450Trunc = 3,
GLSLstd450FAbs = 4,
GLSLstd450SAbs = 5,
GLSLstd450FSign = 6,
GLSLstd450SSign = 7,
GLSLstd450Floor = 8,
GLSLstd450Ceil = 9,
GLSLstd450Fract = 10,
GLSLstd450Radians = 11,
GLSLstd450Degrees = 12,
GLSLstd450Sin = 13,
GLSLstd450Cos = 14,
GLSLstd450Tan = 15,
GLSLstd450Asin = 16,
GLSLstd450Acos = 17,
GLSLstd450Atan = 18,
GLSLstd450Sinh = 19,
GLSLstd450Cosh = 20,
GLSLstd450Tanh = 21,
GLSLstd450Asinh = 22,
GLSLstd450Acosh = 23,
GLSLstd450Atanh = 24,
GLSLstd450Atan2 = 25,
GLSLstd450Pow = 26,
GLSLstd450Exp = 27,
GLSLstd450Log = 28,
GLSLstd450Exp2 = 29,
GLSLstd450Log2 = 30,
GLSLstd450Sqrt = 31,
GLSLstd450InverseSqrt = 32,
GLSLstd450Determinant = 33,
GLSLstd450MatrixInverse = 34,
GLSLstd450Modf = 35, // second operand needs an OpVariable to write to
GLSLstd450ModfStruct = 36, // no OpVariable operand
GLSLstd450FMin = 37,
GLSLstd450UMin = 38,
GLSLstd450SMin = 39,
GLSLstd450FMax = 40,
GLSLstd450UMax = 41,
GLSLstd450SMax = 42,
GLSLstd450FClamp = 43,
GLSLstd450UClamp = 44,
GLSLstd450SClamp = 45,
GLSLstd450FMix = 46,
GLSLstd450IMix = 47, // Reserved
GLSLstd450Step = 48,
GLSLstd450SmoothStep = 49,
GLSLstd450Fma = 50,
GLSLstd450Frexp = 51, // second operand needs an OpVariable to write to
GLSLstd450FrexpStruct = 52, // no OpVariable operand
GLSLstd450Ldexp = 53,
GLSLstd450PackSnorm4x8 = 54,
GLSLstd450PackUnorm4x8 = 55,
GLSLstd450PackSnorm2x16 = 56,
GLSLstd450PackUnorm2x16 = 57,
GLSLstd450PackHalf2x16 = 58,
GLSLstd450PackDouble2x32 = 59,
GLSLstd450UnpackSnorm2x16 = 60,
GLSLstd450UnpackUnorm2x16 = 61,
GLSLstd450UnpackHalf2x16 = 62,
GLSLstd450UnpackSnorm4x8 = 63,
GLSLstd450UnpackUnorm4x8 = 64,
GLSLstd450UnpackDouble2x32 = 65,
GLSLstd450Length = 66,
GLSLstd450Distance = 67,
GLSLstd450Cross = 68,
GLSLstd450Normalize = 69,
GLSLstd450FaceForward = 70,
GLSLstd450Reflect = 71,
GLSLstd450Refract = 72,
GLSLstd450FindILsb = 73,
GLSLstd450FindSMsb = 74,
GLSLstd450FindUMsb = 75,
GLSLstd450InterpolateAtCentroid = 76,
GLSLstd450InterpolateAtSample = 77,
GLSLstd450InterpolateAtOffset = 78,
GLSLstd450NMin = 79,
GLSLstd450NMax = 80,
GLSLstd450NClamp = 81,
GLSLstd450Count
};
#endif // #ifndef GLSLstd450_H
File diff suppressed because it is too large. Load diff
+44
View File
@@ -0,0 +1,44 @@
#pragma once
#include <cstdint>
#include <string>
namespace dxvk {
enum class LogLevel : uint32_t {
Trace = 0,
Debug = 1,
Info = 2,
Warn = 3,
Error = 4,
None = 5,
};
/**
* \brief Logger
*
* Logger for one DLL. Creates a text file and
* writes all log messages to that file.
*/
class Logger {
public:
Logger() {}
Logger(const std::string& file_name) {}
~Logger() {}
static void trace(const std::string& message) {}
static void debug(const std::string& message) {}
static void info (const std::string& message) {}
static void warn (const std::string& message) {}
static void err (const std::string& message) {}
static void log (LogLevel level, const std::string& message) {}
static LogLevel logLevel() {
return LogLevel::Warn;
}
};
}
@@ -0,0 +1,49 @@
#pragma once
#include <sstream>
#include "log/log.h"
#ifdef _MSC_VER
#define METHOD_NAME __FUNCSIG__
#else
#define METHOD_NAME __PRETTY_FUNCTION__
#endif
#define TRACE_ENABLED
#ifdef TRACE_ENABLED
#define TRACE(...) \
do { dxvk::debug::trace(METHOD_NAME, ##__VA_ARGS__); } while (0)
#else
#define TRACE(...) \
do { } while (0)
#endif
namespace dxvk::debug {
std::string methodName(const std::string& prettyName);
inline void traceArgs(std::stringstream& stream) { }
template<typename Arg1>
void traceArgs(std::stringstream& stream, const Arg1& arg1) {
stream << arg1;
}
template<typename Arg1, typename Arg2, typename... Args>
void traceArgs(std::stringstream& stream, const Arg1& arg1, const Arg2& arg2, const Args&... args) {
stream << arg1 << ",";
traceArgs(stream, arg2, args...);
}
template<typename... Args>
void trace(const std::string& funcName, const Args&... args) {
std::stringstream stream;
stream << methodName(funcName) << "(";
traceArgs(stream, args...);
stream << ")";
Logger::trace(stream.str());
}
}
@@ -0,0 +1,38 @@
#pragma once
#include <atomic>
#include "../util_likely.h"
namespace dxvk {
/**
* \brief Reference-counted object
*/
class RcObject {
public:
/**
* \brief Increments reference count
* \returns New reference count
*/
force_inline uint32_t incRef() {
return ++m_refCount;
}
/**
* \brief Decrements reference count
* \returns New reference count
*/
force_inline uint32_t decRef() {
return --m_refCount;
}
private:
std::atomic<uint32_t> m_refCount = { 0u };
};
}
@@ -0,0 +1,189 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <functional>
#include <ostream>
#include <utility>
namespace dxvk {
/**
* \brief Pointer for reference-counted objects
*
* This only requires the given type to implement \c incRef
* and \c decRef methods that adjust the reference count.
* \tparam T Object type
*/
template<typename T>
class Rc {
template<typename Tx>
friend class Rc;
public:
Rc() = default;
Rc(std::nullptr_t) { }
Rc(T* object)
: m_object(object) {
this->incRef();
}
Rc(const Rc& other)
: m_object(other.m_object) {
this->incRef();
}
template<typename Tx>
Rc(const Rc<Tx>& other)
: m_object(other.m_object) {
this->incRef();
}
Rc(Rc&& other)
: m_object(other.m_object) {
other.m_object = nullptr;
}
template<typename Tx>
Rc(Rc<Tx>&& other)
: m_object(other.m_object) {
other.m_object = nullptr;
}
Rc& operator = (std::nullptr_t) {
this->decRef();
m_object = nullptr;
return *this;
}
Rc& operator = (const Rc& other) {
other.incRef();
this->decRef();
m_object = other.m_object;
return *this;
}
template<typename Tx>
Rc& operator = (const Rc<Tx>& other) {
other.incRef();
this->decRef();
m_object = other.m_object;
return *this;
}
Rc& operator = (Rc&& other) {
this->decRef();
this->m_object = other.m_object;
other.m_object = nullptr;
return *this;
}
template<typename Tx>
Rc& operator = (Rc<Tx>&& other) {
this->decRef();
this->m_object = other.m_object;
other.m_object = nullptr;
return *this;
}
~Rc() {
this->decRef();
}
T& operator * () const { return *m_object; }
T* operator -> () const { return m_object; }
T* ptr() const { return m_object; }
template<typename Tx> bool operator == (const Rc<Tx>& other) const { return m_object == other.m_object; }
template<typename Tx> bool operator != (const Rc<Tx>& other) const { return m_object != other.m_object; }
template<typename Tx> bool operator == (Tx* other) const { return m_object == other; }
template<typename Tx> bool operator != (Tx* other) const { return m_object != other; }
bool operator == (std::nullptr_t) const { return m_object == nullptr; }
bool operator != (std::nullptr_t) const { return m_object != nullptr; }
explicit operator bool () const {
return m_object != nullptr;
}
/**
* \brief Sets pointer without acquiring a reference
*
* Must only be use when a reference has been taken via
* other means.
* \param [in] object Object pointer
*/
void unsafeInsert(T* object) {
this->decRef();
m_object = object;
}
/**
* \brief Extracts raw pointer
*
* Sets the smart pointer to null without decrementing the
* reference count. Must only be used when the reference
* count is decremented in some other way.
* \returns Pointer to owned object
*/
T* unsafeExtract() {
return std::exchange(m_object, nullptr);
}
/**
* \brief Creates smart pointer without taking reference
*
* Must only be used when a refernece has been obtained via other means.
* \param [in] object Pointer to object to take ownership of
*/
static Rc<T> unsafeCreate(T* object) {
return Rc<T>(object, false);
}
private:
T* m_object = nullptr;
explicit Rc(T* object, bool)
: m_object(object) { }
force_inline void incRef() const {
if (m_object != nullptr)
m_object->incRef();
}
force_inline void decRef() const {
if (m_object != nullptr) {
if constexpr (std::is_void_v<decltype(m_object->decRef())>) {
m_object->decRef();
} else {
// Deprecated, objects should manage themselves now.
if (!m_object->decRef())
delete m_object;
}
}
}
};
template<typename Tx, typename Ty>
bool operator == (Tx* a, const Rc<Ty>& b) { return b == a; }
template<typename Tx, typename Ty>
bool operator != (Tx* a, const Rc<Ty>& b) { return b != a; }
struct RcHash {
template<typename T>
size_t operator () (const Rc<T>& rc) const {
return reinterpret_cast<uintptr_t>(rc.ptr()) / sizeof(T);
}
};
}
template<typename T>
std::ostream& operator << (std::ostream& os, const dxvk::Rc<T>& rc) {
return os << rc.ptr();
}
+719
View File
@@ -0,0 +1,719 @@
#pragma once
#if (defined(__x86_64__) && !defined(__arm64ec__)) || (defined(_M_X64) && !defined(_M_ARM64EC)) \
|| defined(__i386__) || defined(_M_IX86) || defined(__e2k__)
#define DXVK_ARCH_X86
#if defined(__x86_64__) || defined(_M_X64) || defined(__e2k__)
#define DXVK_ARCH_X86_64
#endif
#elif defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
#define DXVK_ARCH_ARM64
#endif
#ifdef DXVK_ARCH_X86
#ifndef _MSC_VER
#if defined(_WIN32) && (defined(__AVX__) || defined(__AVX2__))
#error "AVX-enabled builds not supported due to stack alignment issues."
#endif
#if defined(__WINE__) && defined(__clang__)
#pragma push_macro("_WIN32")
#undef _WIN32
#endif
#include <x86intrin.h>
#if defined(__WINE__) && defined(__clang__)
#pragma pop_macro("_WIN32")
#endif
#else
#include <intrin.h>
#endif
#endif
#include "util_likely.h"
#include "util_math.h"
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <iterator>
#include <type_traits>
#include <vector>
namespace dxvk::bit {
template<typename T, typename J>
T cast(const J& src) {
static_assert(sizeof(T) == sizeof(J));
static_assert(std::is_trivially_copyable<J>::value && std::is_trivial<T>::value);
T dst;
std::memcpy(&dst, &src, sizeof(T));
return dst;
}
template<typename T>
T extract(T value, uint32_t fst, uint32_t lst) {
return (value >> fst) & ~(~T(0) << (lst - fst + 1));
}
template<typename T>
T popcnt(T n) {
n -= ((n >> 1u) & T(0x5555555555555555ull));
n = (n & T(0x3333333333333333ull)) + ((n >> 2u) & T(0x3333333333333333ull));
n = (n + (n >> 4u)) & T(0x0f0f0f0f0f0f0f0full);
n *= T(0x0101010101010101ull);
return n >> (8u * (sizeof(T) - 1u));
}
inline uint32_t tzcnt(uint32_t n) {
#if defined(_MSC_VER) && !defined(__clang__)
if(n == 0)
return 32;
return _tzcnt_u32(n);
#elif defined(__BMI__)
return __tzcnt_u32(n);
#elif defined(DXVK_ARCH_X86) && (defined(__GNUC__) || defined(__clang__))
// tzcnt is encoded as rep bsf, so we can use it on all
// processors, but the behaviour of zero inputs differs:
// - bsf: zf = 1, cf = ?, result = ?
// - tzcnt: zf = 0, cf = 1, result = 32
// We'll have to handle this case manually.
uint32_t res;
uint32_t tmp;
asm (
"tzcnt %2, %0;"
"mov $32, %1;"
"test %2, %2;"
"cmovz %1, %0;"
: "=&r" (res), "=&r" (tmp)
: "r" (n)
: "cc");
return res;
#elif defined(__GNUC__) || defined(__clang__)
return n != 0 ? __builtin_ctz(n) : 32;
#else
uint32_t r = 31;
n &= -n;
r -= (n & 0x0000FFFF) ? 16 : 0;
r -= (n & 0x00FF00FF) ? 8 : 0;
r -= (n & 0x0F0F0F0F) ? 4 : 0;
r -= (n & 0x33333333) ? 2 : 0;
r -= (n & 0x55555555) ? 1 : 0;
return n != 0 ? r : 32;
#endif
}
inline uint32_t tzcnt(uint64_t n) {
#if defined(DXVK_ARCH_X86_64) && defined(_MSC_VER) && !defined(__clang__)
if(n == 0)
return 64;
return (uint32_t)_tzcnt_u64(n);
#elif defined(DXVK_ARCH_X86_64) && defined(__BMI__)
return __tzcnt_u64(n);
#elif defined(DXVK_ARCH_X86_64) && (defined(__GNUC__) || defined(__clang__))
uint64_t res;
uint64_t tmp;
asm (
"tzcnt %2, %0;"
"mov $64, %1;"
"test %2, %2;"
"cmovz %1, %0;"
: "=&r" (res), "=&r" (tmp)
: "r" (n)
: "cc");
return res;
#elif defined(__GNUC__) || defined(__clang__)
return n != 0 ? __builtin_ctzll(n) : 64;
#else
uint32_t lo = uint32_t(n);
if (lo) {
return tzcnt(lo);
} else {
uint32_t hi = uint32_t(n >> 32);
return tzcnt(hi) + 32;
}
#endif
}
inline uint32_t bsf(uint32_t n) {
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__BMI__) && defined(DXVK_ARCH_X86)
uint32_t res;
asm ("tzcnt %1,%0"
: "=r" (res)
: "r" (n)
: "cc");
return res;
#else
return tzcnt(n);
#endif
}
inline uint32_t bsf(uint64_t n) {
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__BMI__) && defined(DXVK_ARCH_X86_64)
uint64_t res;
asm ("tzcnt %1,%0"
: "=r" (res)
: "r" (n)
: "cc");
return res;
#else
return tzcnt(n);
#endif
}
inline uint32_t lzcnt(uint32_t n) {
#if defined(_MSC_VER) && !defined(__clang__) && !defined(__LZCNT__)
unsigned long bsr;
if(n == 0)
return 32;
_BitScanReverse(&bsr, n);
return 31-bsr;
#elif (defined(_MSC_VER) && !defined(__clang__)) || defined(__LZCNT__)
return _lzcnt_u32(n);
#elif defined(__GNUC__) || defined(__clang__)
return n != 0 ? __builtin_clz(n) : 32;
#else
uint32_t r = 0;
if (n == 0) return 32;
if (n <= 0x0000FFFF) { r += 16; n <<= 16; }
if (n <= 0x00FFFFFF) { r += 8; n <<= 8; }
if (n <= 0x0FFFFFFF) { r += 4; n <<= 4; }
if (n <= 0x3FFFFFFF) { r += 2; n <<= 2; }
if (n <= 0x7FFFFFFF) { r += 1; n <<= 1; }
return r;
#endif
}
inline uint32_t lzcnt(uint64_t n) {
#if defined(_MSC_VER) && !defined(__clang__) && !defined(__LZCNT__) && defined(DXVK_ARCH_X86_64)
unsigned long bsr;
if(n == 0)
return 64;
_BitScanReverse64(&bsr, n);
return 63-bsr;
#elif defined(DXVK_ARCH_X86_64) && ((defined(_MSC_VER) && !defined(__clang__)) && defined(__LZCNT__))
return _lzcnt_u64(n);
#elif defined(DXVK_ARCH_X86_64) && (defined(__GNUC__) || defined(__clang__))
return n != 0 ? __builtin_clzll(n) : 64;
#else
uint32_t lo = uint32_t(n);
uint32_t hi = uint32_t(n >> 32u);
return hi ? lzcnt(hi) : lzcnt(lo) + 32u;
#endif
}
template<typename T>
uint32_t pack(T& dst, uint32_t& shift, T src, uint32_t count) {
constexpr uint32_t Bits = 8 * sizeof(T);
if (likely(shift < Bits))
dst |= src << shift;
shift += count;
return shift > Bits ? shift - Bits : 0;
}
template<typename T>
uint32_t unpack(T& dst, T src, uint32_t& shift, uint32_t count) {
constexpr uint32_t Bits = 8 * sizeof(T);
if (likely(shift < Bits))
dst = (src >> shift) & ((T(1) << count) - 1);
shift += count;
return shift > Bits ? shift - Bits : 0;
}
/**
* \brief Clears cache lines of memory
*
* Uses non-temporal stores. The memory region offset
* and size are assumed to be aligned to 64 bytes.
* \param [in] mem Memory region to clear
* \param [in] size Number of bytes to clear
*/
inline void bclear(void* mem, size_t size) {
#if defined(DXVK_ARCH_X86) && (defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER))
auto zero = _mm_setzero_si128();
#if defined(__clang__)
#pragma nounroll
#elif defined(__GNUC__)
#pragma GCC unroll 0
#endif
for (size_t i = 0; i < size; i += 64u) {
auto* ptr = reinterpret_cast<__m128i*>(mem) + i / sizeof(zero);
_mm_stream_si128(ptr + 0u, zero);
_mm_stream_si128(ptr + 1u, zero);
_mm_stream_si128(ptr + 2u, zero);
_mm_stream_si128(ptr + 3u, zero);
}
#else
std::memset(mem, 0, size);
#endif
}
/**
* \brief Compares two aligned structs bit by bit
*
* \param [in] a First struct
* \param [in] b Second struct
* \returns \c true if the structs are equal
*/
template<typename T>
bool bcmpeq(const T* a, const T* b) {
static_assert(alignof(T) >= 16);
#if defined(DXVK_ARCH_X86) && (defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER))
auto ai = reinterpret_cast<const __m128i*>(a);
auto bi = reinterpret_cast<const __m128i*>(b);
size_t i = 0;
#if defined(__clang__)
#pragma nounroll
#elif defined(__GNUC__)
#pragma GCC unroll 0
#endif
for ( ; i < 2 * (sizeof(T) / 32); i += 2) {
__m128i eq0 = _mm_cmpeq_epi8(
_mm_load_si128(ai + i),
_mm_load_si128(bi + i));
__m128i eq1 = _mm_cmpeq_epi8(
_mm_load_si128(ai + i + 1),
_mm_load_si128(bi + i + 1));
__m128i eq = _mm_and_si128(eq0, eq1);
int mask = _mm_movemask_epi8(eq);
if (mask != 0xFFFF)
return false;
}
for ( ; i < sizeof(T) / 16; i++) {
__m128i eq = _mm_cmpeq_epi8(
_mm_load_si128(ai + i),
_mm_load_si128(bi + i));
int mask = _mm_movemask_epi8(eq);
if (mask != 0xFFFF)
return false;
}
return true;
#else
return !std::memcmp(a, b, sizeof(T));
#endif
}
template <size_t Bits>
class bitset {
static constexpr size_t Dwords = align(Bits, 32) / 32;
public:
constexpr bitset()
: m_dwords() {
}
constexpr bool get(uint32_t idx) const {
uint32_t dword = 0;
uint32_t bit = idx;
// Compiler doesn't remove this otherwise.
if constexpr (Dwords > 1) {
dword = idx / 32;
bit = idx % 32;
}
return m_dwords[dword] & (1u << bit);
}
constexpr void set(uint32_t idx, bool value) {
uint32_t dword = 0;
uint32_t bit = idx;
// Compiler doesn't remove this otherwise.
if constexpr (Dwords > 1) {
dword = idx / 32;
bit = idx % 32;
}
if (value)
m_dwords[dword] |= 1u << bit;
else
m_dwords[dword] &= ~(1u << bit);
}
constexpr bool exchange(uint32_t idx, bool value) {
bool oldValue = get(idx);
set(idx, value);
return oldValue;
}
constexpr void flip(uint32_t idx) {
uint32_t dword = 0;
uint32_t bit = idx;
// Compiler doesn't remove this otherwise.
if constexpr (Dwords > 1) {
dword = idx / 32;
bit = idx % 32;
}
m_dwords[dword] ^= 1u << bit;
}
constexpr void setAll() {
if constexpr (Bits % 32 == 0) {
for (size_t i = 0; i < Dwords; i++)
m_dwords[i] = std::numeric_limits<uint32_t>::max();
}
else {
for (size_t i = 0; i < Dwords - 1; i++)
m_dwords[i] = std::numeric_limits<uint32_t>::max();
m_dwords[Dwords - 1] = (1u << (Bits % 32)) - 1;
}
}
constexpr void clearAll() {
for (size_t i = 0; i < Dwords; i++)
m_dwords[i] = 0;
}
constexpr bool any() const {
for (size_t i = 0; i < Dwords; i++) {
if (m_dwords[i] != 0)
return true;
}
return false;
}
constexpr uint32_t& dword(uint32_t idx) {
return m_dwords[idx];
}
constexpr size_t bitCount() {
return Bits;
}
constexpr size_t dwordCount() {
return Dwords;
}
constexpr bool operator [] (uint32_t idx) const {
return get(idx);
}
constexpr void setN(uint32_t bits) {
uint32_t fullDwords = bits / 32;
uint32_t offset = bits % 32;
for (size_t i = 0; i < fullDwords; i++)
m_dwords[i] = std::numeric_limits<uint32_t>::max();
if (offset > 0)
m_dwords[fullDwords] = (1u << offset) - 1;
}
private:
uint32_t m_dwords[Dwords];
};
class bitvector {
public:
bool get(uint32_t idx) const {
uint32_t dword = idx / 32;
uint32_t bit = idx % 32;
return m_dwords[dword] & (1u << bit);
}
void ensureSize(uint32_t bitCount) {
uint32_t dword = bitCount / 32;
if (unlikely(dword >= m_dwords.size())) {
m_dwords.resize(dword + 1);
}
m_bitCount = std::max(m_bitCount, bitCount);
}
void set(uint32_t idx, bool value) {
ensureSize(idx + 1);
uint32_t dword = 0;
uint32_t bit = idx;
if (value)
m_dwords[dword] |= 1u << bit;
else
m_dwords[dword] &= ~(1u << bit);
}
bool exchange(uint32_t idx, bool value) {
ensureSize(idx + 1);
bool oldValue = get(idx);
set(idx, value);
return oldValue;
}
void flip(uint32_t idx) {
ensureSize(idx + 1);
uint32_t dword = idx / 32;
uint32_t bit = idx % 32;
m_dwords[dword] ^= 1u << bit;
}
void setAll() {
if (m_bitCount % 32 == 0) {
for (size_t i = 0; i < m_dwords.size(); i++)
m_dwords[i] = std::numeric_limits<uint32_t>::max();
}
else {
for (size_t i = 0; i < m_dwords.size() - 1; i++)
m_dwords[i] = std::numeric_limits<uint32_t>::max();
m_dwords[m_dwords.size() - 1] = (1u << (m_bitCount % 32)) - 1;
}
}
void clearAll() {
for (size_t i = 0; i < m_dwords.size(); i++)
m_dwords[i] = 0;
}
bool any() const {
for (size_t i = 0; i < m_dwords.size(); i++) {
if (m_dwords[i] != 0)
return true;
}
return false;
}
uint32_t& dword(uint32_t idx) {
return m_dwords[idx];
}
size_t bitCount() const {
return m_bitCount;
}
size_t dwordCount() const {
return m_dwords.size();
}
bool operator [] (uint32_t idx) const {
return get(idx);
}
void setN(uint32_t bits) {
ensureSize(bits);
uint32_t fullDwords = bits / 32;
uint32_t offset = bits % 32;
for (size_t i = 0; i < fullDwords; i++)
m_dwords[i] = std::numeric_limits<uint32_t>::max();
if (offset > 0)
m_dwords[fullDwords] = (1u << offset) - 1;
}
private:
std::vector<uint32_t> m_dwords;
uint32_t m_bitCount = 0;
};
template<typename T>
class BitMask {
public:
class iterator {
public:
using iterator_category = std::input_iterator_tag;
using value_type = T;
using difference_type = T;
using pointer = const T*;
using reference = T;
explicit iterator(T flags)
: m_mask(flags) { }
iterator& operator ++ () {
m_mask &= m_mask - 1;
return *this;
}
iterator operator ++ (int) {
iterator retval = *this;
m_mask &= m_mask - 1;
return retval;
}
T operator * () const {
return bsf(m_mask);
}
bool operator == (iterator other) const { return m_mask == other.m_mask; }
bool operator != (iterator other) const { return m_mask != other.m_mask; }
private:
T m_mask;
};
BitMask()
: m_mask(0) { }
explicit BitMask(T n)
: m_mask(n) { }
iterator begin() {
return iterator(m_mask);
}
iterator end() {
return iterator(0);
}
private:
T m_mask;
};
/**
* \brief Encodes float as fixed point
*
* Rounds away from zero. If this is not suitable for
* certain use cases, implement round to nearest even.
* \tparam T Integer type, may be signed
* \tparam I Integer bits
* \tparam F Fractional bits
* \param n Float to encode
* \returns Encoded fixed-point value
*/
template<typename T, int32_t I, int32_t F>
T encodeFixed(float n) {
if (n != n)
return 0u;
n *= float(1u << F);
if constexpr (std::is_signed_v<T>) {
n = std::max(n, -float(1u << (I + F - 1u)));
n = std::min(n, float(1u << (I + F - 1u)) - 1.0f);
n += n < 0.0f ? -0.5f : 0.5f;
} else {
n = std::max(n, 0.0f);
n = std::min(n, float(1u << (I + F)) - 1.0f);
n += 0.5f;
}
T result = T(n);
if constexpr (std::is_signed_v<T>)
result &= ((T(1u) << (I + F)) - 1u);
return result;
}
/**
* \brief Decodes fixed-point integer to float
*
* \tparam T Integer type, may be signed
* \tparam I Integer bits
* \tparam F Fractional bits
* \param n Number to decode
* \returns Decoded number
*/
template<typename T, int32_t I, int32_t F>
float decodeFixed(T n) {
// Sign-extend as necessary
if constexpr (std::is_signed_v<T>)
n -= (n & (T(1u) << (I + F - 1u))) << 1u;
return float(n) / float(1u << F);
}
/**
* \brief Inserts one null bit after each bit
*/
inline uint32_t split2(uint32_t c) {
c = (c ^ (c << 8u)) & 0x00ff00ffu;
c = (c ^ (c << 4u)) & 0x0f0f0f0fu;
c = (c ^ (c << 2u)) & 0x33333333u;
c = (c ^ (c << 1u)) & 0x55555555u;
return c;
}
/**
* \brief Inserts two null bits after each bit
*/
inline uint64_t split3(uint64_t c) {
c = (c | c << 32u) & 0x001f00000000ffffull;
c = (c | c << 16u) & 0x001f0000ff0000ffull;
c = (c | c << 8u) & 0x100f00f00f00f00full;
c = (c | c << 4u) & 0x10c30c30c30c30c3ull;
c = (c | c << 2u) & 0x1249249249249249ull;
return c;
}
/**
* \brief Interleaves bits from two integers
*
* Both numbers must fit into 16 bits.
* \param [in] x X coordinate
* \param [in] y Y coordinate
* \returns Morton code of x and y
*/
inline uint32_t interleave(uint16_t x, uint16_t y) {
return split2(x) | (split2(y) << 1u);
}
/**
* \brief Interleaves bits from three integers
*
* All three numbers must fit into 16 bits.
*/
inline uint64_t interleave(uint16_t x, uint16_t y, uint16_t z) {
return split3(x) | (split3(y) << 1u) | (split3(z) << 2u);
}
/**
* \brief 48-bit integer storage type
*/
struct uint48_t {
explicit uint48_t(uint64_t n)
: a(uint16_t(n)), b(uint16_t(n >> 16)), c(uint16_t(n >> 32)) { }
uint16_t a;
uint16_t b;
uint16_t c;
explicit operator uint64_t () const {
// GCC generates worse code if we promote to uint64 directly
uint32_t lo = uint32_t(a) | (uint32_t(b) << 16);
return uint64_t(lo) | (uint64_t(c) << 32);
}
};
}
@@ -0,0 +1,7 @@
#pragma once
#define ENUM_NAME(name) \
case name: return os << #name
#define ENUM_DEFAULT(name) \
default: return os << static_cast<int32_t>(e)
@@ -0,0 +1,31 @@
#pragma once
#include <string>
namespace dxvk {
/**
* \brief DXVK error
*
* A generic exception class that stores a
* message. Exceptions should be logged.
*/
class DxvkError {
public:
DxvkError() { }
DxvkError(std::string&& message)
: m_message(std::move(message)) { }
const std::string& message() const {
return m_message;
}
private:
std::string m_message;
};
}
@@ -0,0 +1,110 @@
#pragma once
#include <type_traits>
#include "util_bit.h"
namespace dxvk {
template<typename T>
class Flags {
public:
using IntType = std::underlying_type_t<T>;
Flags() { }
Flags(IntType t)
: m_bits(t) { }
template<typename... Tx>
Flags(T f, Tx... fx) {
this->set(f, fx...);
}
template<typename... Tx>
void set(Tx... fx) {
m_bits |= bits(fx...);
}
void set(Flags flags) {
m_bits |= flags.m_bits;
}
template<typename... Tx>
void clr(Tx... fx) {
m_bits &= ~bits(fx...);
}
void clr(Flags flags) {
m_bits &= ~flags.m_bits;
}
template<typename... Tx>
bool any(Tx... fx) const {
return (m_bits & bits(fx...)) != 0;
}
template<typename... Tx>
bool all(Tx... fx) const {
const IntType mask = bits(fx...);
return (m_bits & mask) == mask;
}
bool test(T f) const {
return this->any(f);
}
bool isClear() const {
return m_bits == 0;
}
void clrAll() {
m_bits = 0;
}
IntType raw() const {
return m_bits;
}
Flags operator & (const Flags& other) const {
return Flags(m_bits & other.m_bits);
}
Flags operator | (const Flags& other) const {
return Flags(m_bits | other.m_bits);
}
Flags operator ^ (const Flags& other) const {
return Flags(m_bits ^ other.m_bits);
}
bool operator == (const Flags& other) const {
return m_bits == other.m_bits;
}
bool operator != (const Flags& other) const {
return m_bits != other.m_bits;
}
private:
IntType m_bits = 0;
static IntType bit(T f) {
return IntType(1) << static_cast<IntType>(f);
}
template<typename... Tx>
static IntType bits(T f, Tx... fx) {
return bit(f) | bits(fx...);
}
static IntType bits() {
return 0;
}
};
}
@@ -0,0 +1,11 @@
#pragma once
#ifdef __GNUC__
#define likely(x) __builtin_expect(bool(x),1)
#define unlikely(x) __builtin_expect(bool(x),0)
#define force_inline inline __attribute__((always_inline))
#else
#define likely(x) (x)
#define unlikely(x) (x)
#define force_inline inline
#endif
@@ -0,0 +1,40 @@
#pragma once
#include <cmath>
namespace dxvk {
constexpr size_t CACHE_LINE_SIZE = 64;
constexpr double pi = 3.14159265359;
template<typename T>
constexpr T clamp(T n, T lo, T hi) {
if (n < lo) return lo;
if (n > hi) return hi;
return n;
}
template<typename T, typename U = T>
constexpr T align(T what, U to) {
return (what + to - 1) & ~(to - 1);
}
template<typename T, typename U = T>
constexpr T alignDown(T what, U to) {
return (what / to) * to;
}
// Equivalent of std::clamp for use with floating point numbers
// Handles (-){INFINITY,NAN} cases.
// Will return min in cases of NAN, etc.
inline float fclamp(float value, float min, float max) {
return std::fmin(
std::fmax(value, min), max);
}
template<typename T>
inline T divCeil(T dividend, T divisor) {
return (dividend + divisor - 1) / divisor;
}
}
@@ -0,0 +1,214 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <type_traits>
#include <utility>
#include "util_bit.h"
#include "util_likely.h"
namespace dxvk {
template<typename T, size_t N>
class small_vector {
using storage = std::aligned_storage_t<sizeof(T), alignof(T)>;
public:
constexpr static size_t MinCapacity = N;
small_vector() { }
small_vector(size_t size) {
resize(size);
}
small_vector(const small_vector& other) {
reserve(other.m_size);
for (size_t i = 0; i < other.m_size; i++)
*ptr(i) = *other.ptr(i);
m_size = other.m_size;
}
small_vector& operator = (const small_vector& other) {
for (size_t i = 0; i < m_size; i++)
ptr(i)->~T();
reserve(other.m_size);
for (size_t i = 0; i < other.m_size; i++)
*ptr(i) = *other.ptr(i);
m_size = other.m_size;
return *this;
}
small_vector(small_vector&& other) {
if (other.m_size <= N) {
for (size_t i = 0; i < other.m_size; i++)
new (&u.m_data[i]) T(std::move(*other.ptr(i)));
} else {
u.m_ptr = other.u.m_ptr;
m_capacity = other.m_capacity;
other.u.m_ptr = nullptr;
other.m_capacity = N;
}
m_size = other.m_size;
other.m_size = 0;
}
small_vector& operator = (small_vector&& other) {
for (size_t i = 0; i < m_size; i++)
ptr(i)->~T();
if (m_capacity > N)
delete[] u.m_ptr;
if (other.m_size <= N) {
m_capacity = N;
for (size_t i = 0; i < other.m_size; i++)
new (&u.m_data[i]) T(std::move(*other.ptr(i)));
} else {
u.m_ptr = other.u.m_ptr;
m_capacity = other.m_capacity;
other.u.m_ptr = nullptr;
other.m_capacity = N;
}
m_size = other.m_size;
other.m_size = 0;
return *this;
}
~small_vector() {
for (size_t i = 0; i < m_size; i++)
ptr(i)->~T();
if (m_capacity > N)
delete[] u.m_ptr;
}
size_t size() const {
return m_size;
}
void reserve(size_t n) {
if (likely(n <= m_capacity))
return;
n = pick_capacity(n);
storage* data = new storage[n];
for (size_t i = 0; i < m_size; i++) {
new (&data[i]) T(std::move(*ptr(i)));
ptr(i)->~T();
}
if (m_capacity > N)
delete[] u.m_ptr;
m_capacity = n;
u.m_ptr = data;
}
const T* data() const { return ptr(0); }
T* data() { return ptr(0); }
void resize(size_t n) {
reserve(n);
for (size_t i = n; i < m_size; i++)
ptr(i)->~T();
for (size_t i = m_size; i < n; i++)
new (ptr(i)) T();
m_size = n;
}
void push_back(const T& object) {
reserve(m_size + 1);
new (ptr(m_size++)) T(object);
}
void push_back(T&& object) {
reserve(m_size + 1);
new (ptr(m_size++)) T(std::move(object));
}
template<typename... Args>
T& emplace_back(Args... args) {
reserve(m_size + 1);
return *(new (ptr(m_size++)) T(std::forward<Args>(args)...));
}
void erase(size_t idx) {
ptr(idx)->~T();
for (size_t i = idx; i < m_size - 1; i++) {
new (ptr(i)) T(std::move(*ptr(i + 1)));
ptr(i + 1)->~T();
}
}
void pop_back() {
ptr(--m_size)->~T();
}
void clear() {
for (size_t i = 0; i < m_size; i++)
ptr(i)->~T();
m_size = 0;
}
bool empty() const {
return m_size == 0;
}
T& operator [] (size_t idx) { return *ptr(idx); }
const T& operator [] (size_t idx) const { return *ptr(idx); }
T& front() { return *ptr(0); }
const T& front() const { return *ptr(0); }
T& back() { return *ptr(m_size - 1); }
const T& back() const { return *ptr(m_size - 1); }
private:
size_t m_capacity = N;
size_t m_size = 0;
union {
storage* m_ptr;
storage m_data[N];
} u;
size_t pick_capacity(size_t n) {
// Pick next largest power of two for the new capacity
return size_t(1u) << ((sizeof(n) * 8u) - bit::lzcnt(n - 1));
}
T* ptr(size_t idx) {
return m_capacity == N
? reinterpret_cast<T*>(&u.m_data[idx])
: reinterpret_cast<T*>(&u.m_ptr[idx]);
}
const T* ptr(size_t idx) const {
return m_capacity == N
? reinterpret_cast<const T*>(&u.m_data[idx])
: reinterpret_cast<const T*>(&u.m_ptr[idx]);
}
};
}
@@ -0,0 +1,240 @@
#pragma once
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include "util_bit.h"
#include "util_likely.h"
namespace dxvk::str {
template<size_t S> struct UnicodeChar { };
template<> struct UnicodeChar<1> { using type = uint8_t; };
template<> struct UnicodeChar<2> { using type = uint16_t; };
template<> struct UnicodeChar<4> { using type = uint32_t; };
template<typename T>
using UnicodeCharType = typename UnicodeChar<sizeof(T)>::type;
const uint8_t* decodeTypedChar(
const uint8_t* begin,
const uint8_t* end,
uint32_t& ch);
const uint16_t* decodeTypedChar(
const uint16_t* begin,
const uint16_t* end,
uint32_t& ch);
const uint32_t* decodeTypedChar(
const uint32_t* begin,
const uint32_t* end,
uint32_t& ch);
size_t encodeTypedChar(
uint8_t* begin,
uint8_t* end,
uint32_t ch);
size_t encodeTypedChar(
uint16_t* begin,
uint16_t* end,
uint32_t ch);
size_t encodeTypedChar(
uint32_t* begin,
uint32_t* end,
uint32_t ch);
/**
* \brief Decodes a single character
*
* Note that \c begin and \c end must not be equal.
* \param [in] begin Pointer to current position within the input string
* \param [in] end Pointer to the end of the input string
* \param [out] ch Pointer to the decoded character code
* \returns Pointer to next character in the input string
*/
template<typename T>
const T* decodeChar(
const T* begin,
const T* end,
uint32_t& ch) {
using CharType = UnicodeCharType<T>;
const CharType* result = decodeTypedChar(
reinterpret_cast<const CharType*>(begin),
reinterpret_cast<const CharType*>(end),
ch);
return reinterpret_cast<const T*>(result);
}
/**
* \brief Encodes a character
*
* Note that \c begin and \c end may be both be \c nullptr or equal, in
* which case only the length of the encoded character will be returned.
* \param [in] begin Pointer to current position within the output string
* \param [in] end Pointer to the end of the output string
* \param [in] ch Character to encode
* \returns If begin is \c nullptr , the number of units required to encode
* the character. Otherwise, the number of units written to the output.
* This may return \c 0 for characters that cannot be written or encoded.
*/
template<typename T>
size_t encodeChar(
T* begin,
T* end,
uint32_t ch) {
using CharType = UnicodeCharType<T>;
return encodeTypedChar(
reinterpret_cast<CharType*>(begin),
reinterpret_cast<CharType*>(end),
ch);
}
/**
* \brief Computes length of a null-terminated string
*
* \param [in] begin Start of input string
* \returns Number of characters in input string,
* excluding the terminating null character
*/
template<typename S>
size_t length(const S* string) {
size_t result = 0;
while (string[result])
result += 1;
return result;
}
/**
* \brief Converts string from one encoding to another
*
* The output string arguments may be \c nullptr. In that case, the
* total length of the transcoded string will be returned, in units
* of the output character type. The output string will only be
* null-terminated if the input string is also null-terminated.
* \tparam D Output character type
* \tparam S Input character type
* \param [in] dstBegin Start of output string
* \param [in] dstLength Length of output string
* \param [in] srcBegin Start of input string
* \param [in] srcLength Length of input string
* \returns If \c dstBegin is \c nullptr , the total number of output
* characters required to store the output string. Otherwise, the
* total number of characters written to the output string.
*/
template<typename D, typename S>
size_t transcodeString(
D* dstBegin,
size_t dstLength,
const S* srcBegin,
size_t srcLength) {
size_t totalLength = 0;
auto dstEnd = dstBegin + dstLength;
auto srcEnd = srcBegin + srcLength;
while (srcBegin < srcEnd) {
uint32_t ch;
srcBegin = decodeChar<S>(srcBegin, srcEnd, ch);
if (dstBegin)
totalLength += encodeChar<D>(dstBegin + totalLength, dstEnd, ch);
else
totalLength += encodeChar<D>(nullptr, nullptr, ch);
if (!ch)
break;
}
return totalLength;
}
/**
* \brief Creates string object from wide char array
*
* \param [in] ws Null-terminated wide string
* \returns Regular string object
*/
std::string fromws(const wchar_t* ws);
/**
* \brief Creates wide string object from char array
*
* \param [in] mbs Null-terminated string
* \returns Wide string object
*/
std::wstring tows(const char* mbs);
#ifdef _WIN32
using path_string = std::wstring;
inline path_string topath(const char* mbs) { return tows(mbs); }
#else
using path_string = std::string;
inline path_string topath(const char* mbs) { return std::string(mbs); }
#endif
inline void format1(std::stringstream&) { }
template<typename... Tx>
void format1(std::stringstream& str, const wchar_t *arg, const Tx&... args) {
str << fromws(arg);
format1(str, args...);
}
template<typename T, typename... Tx>
void format1(std::stringstream& str, const T& arg, const Tx&... args) {
str << arg;
format1(str, args...);
}
template<typename... Args>
std::string format(const Args&... args) {
std::stringstream stream;
format1(stream, args...);
return stream.str();
}
inline void strlcpy(char* dst, const char* src, size_t count) {
if (count > 0) {
std::strncpy(dst, src, count - 1);
dst[count - 1] = '\0';
}
}
/**
* \brief Split string at one or more delimiters characters
*
* \param [in] string String to split
* \param [in] delims Delimiter characters
* \returns Vector of substring views
*/
inline std::vector<std::string_view> split(std::string_view string, std::string_view delims = " ") {
std::vector<std::string_view> tokens;
for (size_t start = 0; start < string.size(); ) {
// Find first delimiter
const auto end = string.find_first_of(delims, start);
// Add non-empty tokens
if (start != end)
tokens.emplace_back(string.substr(start, end-start));
// Break at the end of string
if (end == std::string_view::npos)
break;
start = end + 1;
}
return tokens;
}
}
@@ -0,0 +1,254 @@
#include "dxbc_analysis.h"
namespace dxvk {
DxbcAnalyzer::DxbcAnalyzer(
const DxbcModuleInfo& moduleInfo,
const DxbcProgramInfo& programInfo,
const Rc<DxbcIsgn>& isgn,
const Rc<DxbcIsgn>& osgn,
const Rc<DxbcIsgn>& psgn,
DxbcAnalysisInfo& analysis)
: m_isgn (isgn),
m_osgn (osgn),
m_psgn (psgn),
m_analysis(&analysis) {
// Get number of clipping and culling planes from the
// input and output signatures. We will need this to
// declare the shader input and output interfaces.
m_analysis->clipCullIn = getClipCullInfo(m_isgn);
m_analysis->clipCullOut = getClipCullInfo(m_osgn);
}
DxbcAnalyzer::~DxbcAnalyzer() {
}
void DxbcAnalyzer::processInstruction(const DxbcShaderInstruction& ins) {
switch (ins.opClass) {
case DxbcInstClass::Atomic: {
const uint32_t operandId = ins.dstCount - 1;
if (ins.dst[operandId].type == DxbcOperandType::UnorderedAccessView) {
const uint32_t registerId = ins.dst[operandId].idx[0].offset;
m_analysis->uavInfos[registerId].accessAtomicOp = true;
m_analysis->uavInfos[registerId].accessFlags |= VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT;
// Check whether the atomic operation is order-invariant
DxvkAccessOp op = DxvkAccessOp::None;
switch (ins.op) {
case DxbcOpcode::AtomicAnd: op = DxvkAccessOp::And; break;
case DxbcOpcode::AtomicOr: op = DxvkAccessOp::Or; break;
case DxbcOpcode::AtomicXor: op = DxvkAccessOp::Xor; break;
case DxbcOpcode::AtomicIAdd: op = DxvkAccessOp::Add; break;
case DxbcOpcode::AtomicIMax: op = DxvkAccessOp::IMax; break;
case DxbcOpcode::AtomicIMin: op = DxvkAccessOp::IMin; break;
case DxbcOpcode::AtomicUMax: op = DxvkAccessOp::UMax; break;
case DxbcOpcode::AtomicUMin: op = DxvkAccessOp::UMin; break;
default: break;
}
setUavAccessOp(registerId, op);
}
} break;
case DxbcInstClass::TextureSample:
case DxbcInstClass::TextureGather:
case DxbcInstClass::TextureQueryLod:
case DxbcInstClass::VectorDeriv: {
m_analysis->usesDerivatives = true;
} break;
case DxbcInstClass::ControlFlow: {
if (ins.op == DxbcOpcode::Discard)
m_analysis->usesKill = true;
} break;
case DxbcInstClass::BufferLoad: {
uint32_t operandId = ins.op == DxbcOpcode::LdStructured ? 2 : 1;
bool sparseFeedback = ins.dstCount == 2;
if (ins.src[operandId].type == DxbcOperandType::UnorderedAccessView) {
const uint32_t registerId = ins.src[operandId].idx[0].offset;
m_analysis->uavInfos[registerId].accessFlags |= VK_ACCESS_SHADER_READ_BIT;
m_analysis->uavInfos[registerId].sparseFeedback |= sparseFeedback;
setUavAccessOp(registerId, DxvkAccessOp::None);
} else if (ins.src[operandId].type == DxbcOperandType::Resource) {
const uint32_t registerId = ins.src[operandId].idx[0].offset;
m_analysis->srvInfos[registerId].sparseFeedback |= sparseFeedback;
}
} break;
case DxbcInstClass::BufferStore: {
if (ins.dst[0].type == DxbcOperandType::UnorderedAccessView) {
const uint32_t registerId = ins.dst[0].idx[0].offset;
m_analysis->uavInfos[registerId].accessFlags |= VK_ACCESS_SHADER_WRITE_BIT;
setUavAccessOp(registerId, getStoreAccessOp(ins.dst[0].mask, ins.src[ins.srcCount - 1u]));
}
} break;
case DxbcInstClass::TypedUavLoad: {
const uint32_t registerId = ins.src[1].idx[0].offset;
m_analysis->uavInfos[registerId].accessTypedLoad = true;
m_analysis->uavInfos[registerId].accessFlags |= VK_ACCESS_SHADER_READ_BIT;
setUavAccessOp(registerId, DxvkAccessOp::None);
} break;
case DxbcInstClass::TypedUavStore: {
const uint32_t registerId = ins.dst[0].idx[0].offset;
m_analysis->uavInfos[registerId].accessFlags |= VK_ACCESS_SHADER_WRITE_BIT;
// The UAV format may change between dispatches, so be conservative here
// and only allow this optimization when the app is writing zeroes.
DxvkAccessOp storeOp = getStoreAccessOp(DxbcRegMask(0xf), ins.src[1u]);
if (storeOp != DxvkAccessOp(DxvkAccessOp::OpType::StoreUi, 0u))
storeOp = DxvkAccessOp::None;
setUavAccessOp(registerId, storeOp);
} break;
case DxbcInstClass::Declaration: {
switch (ins.op) {
case DxbcOpcode::DclConstantBuffer: {
uint32_t registerId = ins.dst[0].idx[0].offset;
if (registerId < DxbcConstBufBindingCount)
m_analysis->bindings.cbvMask |= 1u << registerId;
} break;
case DxbcOpcode::DclSampler: {
uint32_t registerId = ins.dst[0].idx[0].offset;
if (registerId < DxbcSamplerBindingCount)
m_analysis->bindings.samplerMask |= 1u << registerId;
} break;
case DxbcOpcode::DclResource:
case DxbcOpcode::DclResourceRaw:
case DxbcOpcode::DclResourceStructured: {
uint32_t registerId = ins.dst[0].idx[0].offset;
uint32_t idx = registerId / 64u;
uint32_t bit = registerId % 64u;
if (registerId < DxbcResourceBindingCount)
m_analysis->bindings.srvMask[idx] |= uint64_t(1u) << bit;
} break;
case DxbcOpcode::DclUavTyped:
case DxbcOpcode::DclUavRaw:
case DxbcOpcode::DclUavStructured: {
uint32_t registerId = ins.dst[0].idx[0].offset;
if (registerId < DxbcUavBindingCount)
m_analysis->bindings.uavMask |= uint64_t(1u) << registerId;
} break;
default: ;
}
} break;
default:
break;
}
for (uint32_t i = 0; i < ins.dstCount; i++) {
if (ins.dst[i].type == DxbcOperandType::IndexableTemp) {
uint32_t index = ins.dst[i].idx[0].offset;
m_analysis->xRegMasks[index] |= ins.dst[i].mask;
}
}
}
DxbcClipCullInfo DxbcAnalyzer::getClipCullInfo(const Rc<DxbcIsgn>& sgn) const {
DxbcClipCullInfo result;
if (sgn != nullptr) {
for (auto e = sgn->begin(); e != sgn->end(); e++) {
const uint32_t componentCount = e->componentMask.popCount();
if (e->systemValue == DxbcSystemValue::ClipDistance)
result.numClipPlanes += componentCount;
if (e->systemValue == DxbcSystemValue::CullDistance)
result.numCullPlanes += componentCount;
}
}
return result;
}
void DxbcAnalyzer::setUavAccessOp(uint32_t uav, DxvkAccessOp op) {
if (m_analysis->uavInfos[uav].accessOp == DxvkAccessOp::None)
m_analysis->uavInfos[uav].accessOp = op;
// Maintain ordering if the UAV is accessed via other operations as well
if (op == DxvkAccessOp::None || m_analysis->uavInfos[uav].accessOp != op)
m_analysis->uavInfos[uav].nonInvariantAccess = true;
}
DxvkAccessOp DxbcAnalyzer::getStoreAccessOp(DxbcRegMask writeMask, const DxbcRegister& src) {
if (src.type != DxbcOperandType::Imm32)
return DxvkAccessOp::None;
// Trivial case, same value is written to all components
if (src.componentCount == DxbcComponentCount::Component1)
return getConstantStoreOp(src.imm.u32_1);
if (src.componentCount != DxbcComponentCount::Component4)
return DxvkAccessOp::None;
// Otherwise, make sure that all written components are equal
DxvkAccessOp op = DxvkAccessOp::None;
for (uint32_t i = 0u; i < 4u; i++) {
if (!writeMask[i])
continue;
// If the written value can't be represented, skip
DxvkAccessOp scalarOp = getConstantStoreOp(src.imm.u32_4[i]);
if (scalarOp == DxvkAccessOp::None)
return DxvkAccessOp::None;
// First component written
if (op == DxvkAccessOp::None)
op = scalarOp;
// Conflicting store ops
if (op != scalarOp)
return DxvkAccessOp::None;
}
return op;
}
DxvkAccessOp DxbcAnalyzer::getConstantStoreOp(uint32_t value) {
constexpr uint32_t mask = 0xfffu;
uint32_t ubits = value & mask;
uint32_t fbits = (value >> 20u);
if (value == ubits)
return DxvkAccessOp(DxvkAccessOp::OpType::StoreUi, ubits);
if (value == (ubits | ~mask))
return DxvkAccessOp(DxvkAccessOp::OpType::StoreSi, ubits);
if (value == (fbits << 20u))
return DxvkAccessOp(DxvkAccessOp::OpType::StoreF, fbits);
return DxvkAccessOp::None;
}
}
@@ -0,0 +1,112 @@
#include "dxbc_chunk_isgn.h"
namespace dxvk {
DxbcIsgn::DxbcIsgn(DxbcReader reader, DxbcTag tag) {
uint32_t elementCount = reader.readu32();
reader.skip(sizeof(uint32_t));
std::array<DxbcScalarType, 4> componentTypes = {
DxbcScalarType::Uint32, DxbcScalarType::Uint32,
DxbcScalarType::Sint32, DxbcScalarType::Float32,
};
// https://github.com/DarkStarSword/3d-fixes/blob/master/dx11shaderanalyse.py#L101
bool hasStream = (tag == "ISG1") || (tag == "OSG1") || (tag == "PSG1") || (tag == "OSG5");
bool hasPrecision = (tag == "ISG1") || (tag == "OSG1") || (tag == "PSG1");
for (uint32_t i = 0; i < elementCount; i++) {
DxbcSgnEntry entry;
entry.streamId = hasStream ? reader.readu32() : 0;
entry.semanticName = reader.clone(reader.readu32()).readString();
entry.semanticIndex = reader.readu32();
entry.systemValue = static_cast<DxbcSystemValue>(reader.readu32());
entry.componentType = componentTypes.at(reader.readu32());
entry.registerId = reader.readu32();
uint32_t mask = reader.readu32();
entry.componentMask = bit::extract(mask, 0, 3);
entry.componentUsed = bit::extract(mask, 8, 11);
if (hasPrecision)
reader.readu32();
m_entries.push_back(entry);
}
}
DxbcIsgn::~DxbcIsgn() {
}
const DxbcSgnEntry* DxbcIsgn::findByRegister(uint32_t registerId) const {
for (auto e = this->begin(); e != this->end(); e++) {
if (e->registerId == registerId)
return &(*e);
}
return nullptr;
}
const DxbcSgnEntry* DxbcIsgn::find(
const std::string& semanticName,
uint32_t semanticIndex,
uint32_t streamId) const {
for (auto e = this->begin(); e != this->end(); e++) {
if (e->semanticIndex == semanticIndex
&& e->streamId == streamId
&& compareSemanticNames(semanticName, e->semanticName))
return &(*e);
}
return nullptr;
}
DxbcRegMask DxbcIsgn::regMask(
uint32_t registerId) const {
DxbcRegMask mask;
for (auto e = this->begin(); e != this->end(); e++) {
if (e->registerId == registerId)
mask |= e->componentMask;
}
return mask;
}
uint32_t DxbcIsgn::maxRegisterCount() const {
uint32_t result = 0;
for (auto e = this->begin(); e != this->end(); e++)
result = std::max(result, e->registerId + 1);
return result;
}
bool DxbcIsgn::compareSemanticNames(
const std::string& a, const std::string& b) {
if (a.size() != b.size())
return false;
for (size_t i = 0; i < a.size(); i++) {
char ac = a[i];
char bc = b[i];
if (ac != bc) {
if (ac >= 'A' && ac <= 'Z') ac += 'a' - 'A';
if (bc >= 'A' && bc <= 'Z') bc += 'a' - 'A';
if (ac != bc)
return false;
}
}
return true;
}
}
@@ -0,0 +1,24 @@
#include "dxbc_chunk_shex.h"
namespace dxvk {
DxbcShex::DxbcShex(DxbcReader reader) {
// The shader version and type are stored in a 32-bit unit,
// where the first byte contains the major and minor version
// numbers, and the high word contains the program type.
reader.skip(2);
auto pType = reader.readEnum<DxbcProgramType>();
m_programInfo = DxbcProgramInfo(pType);
// Read the actual shader code as an array of DWORDs.
auto codeLength = reader.readu32() - 2;
m_code.resize(codeLength);
reader.read(m_code.data(), codeLength * sizeof(uint32_t));
}
DxbcShex::~DxbcShex() {
}
}
@@ -0,0 +1,30 @@
#include "dxbc_common.h"
namespace dxvk {
VkShaderStageFlagBits DxbcProgramInfo::shaderStage() const {
switch (m_type) {
case DxbcProgramType::PixelShader : return VK_SHADER_STAGE_FRAGMENT_BIT;
case DxbcProgramType::VertexShader : return VK_SHADER_STAGE_VERTEX_BIT;
case DxbcProgramType::GeometryShader : return VK_SHADER_STAGE_GEOMETRY_BIT;
case DxbcProgramType::HullShader : return VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT;
case DxbcProgramType::DomainShader : return VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
case DxbcProgramType::ComputeShader : return VK_SHADER_STAGE_COMPUTE_BIT;
default: throw DxvkError("DxbcProgramInfo::shaderStage: Unsupported program type");
}
}
spv::ExecutionModel DxbcProgramInfo::executionModel() const {
switch (m_type) {
case DxbcProgramType::PixelShader : return spv::ExecutionModelFragment;
case DxbcProgramType::VertexShader : return spv::ExecutionModelVertex;
case DxbcProgramType::GeometryShader : return spv::ExecutionModelGeometry;
case DxbcProgramType::HullShader : return spv::ExecutionModelTessellationControl;
case DxbcProgramType::DomainShader : return spv::ExecutionModelTessellationEvaluation;
case DxbcProgramType::ComputeShader : return spv::ExecutionModelGLCompute;
default: throw DxvkError("DxbcProgramInfo::executionModel: Unsupported program type");
}
}
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,360 @@
#include "dxbc_decoder.h"
namespace dxvk {
const uint32_t* DxbcCodeSlice::ptrAt(uint32_t id) const {
if (m_ptr + id >= m_end)
throw DxvkError("DxbcCodeSlice: End of stream");
return m_ptr + id;
}
uint32_t DxbcCodeSlice::at(uint32_t id) const {
if (m_ptr + id >= m_end)
throw DxvkError("DxbcCodeSlice: End of stream");
return m_ptr[id];
}
uint32_t DxbcCodeSlice::read() {
if (m_ptr >= m_end)
throw DxvkError("DxbcCodeSlice: End of stream");
return *(m_ptr++);
}
DxbcCodeSlice DxbcCodeSlice::take(uint32_t n) const {
if (m_ptr + n > m_end)
throw DxvkError("DxbcCodeSlice: End of stream");
return DxbcCodeSlice(m_ptr, m_ptr + n);
}
DxbcCodeSlice DxbcCodeSlice::skip(uint32_t n) const {
if (m_ptr + n > m_end)
throw DxvkError("DxbcCodeSlice: End of stream");
return DxbcCodeSlice(m_ptr + n, m_end);
}
void DxbcDecodeContext::decodeInstruction(DxbcCodeSlice& code) {
const uint32_t token0 = code.at(0);
// Initialize the instruction structure. Some of these values
// may not get written otherwise while decoding the instruction.
m_instruction.op = static_cast<DxbcOpcode>(bit::extract(token0, 0, 10));
m_instruction.opClass = DxbcInstClass::Undefined;
m_instruction.sampleControls = { 0, 0, 0 };
m_instruction.dstCount = 0;
m_instruction.srcCount = 0;
m_instruction.immCount = 0;
m_instruction.dst = m_dstOperands.data();
m_instruction.src = m_srcOperands.data();
m_instruction.imm = m_immOperands.data();
m_instruction.customDataType = DxbcCustomDataClass::Comment;
m_instruction.customDataSize = 0;
m_instruction.customData = nullptr;
// Reset the index pointer, which may still contain
// a non-zero value from the previous iteration
m_indexId = 0;
// Instruction length, in DWORDs. This includes the token
// itself and any other prefix that an instruction may have.
uint32_t length = 0;
if (m_instruction.op == DxbcOpcode::CustomData) {
length = code.at(1);
this->decodeCustomData(code.take(length));
} else {
length = bit::extract(token0, 24, 30);
this->decodeOperation(code.take(length));
}
// Advance the caller's slice to the next token so that
// they can make consecutive calls to decodeInstruction()
code = code.skip(length);
}
void DxbcDecodeContext::decodeCustomData(DxbcCodeSlice code) {
const uint32_t blockLength = code.at(1);
if (blockLength < 2) {
Logger::err("DxbcDecodeContext: Invalid custom data block");
return;
}
// Custom data blocks have their own instruction class
m_instruction.op = DxbcOpcode::CustomData;
m_instruction.opClass = DxbcInstClass::CustomData;
// We'll point into the code buffer rather than making a copy
m_instruction.customDataType = static_cast<DxbcCustomDataClass>(
bit::extract(code.at(0), 11, 31));
m_instruction.customDataSize = blockLength - 2;
m_instruction.customData = code.ptrAt(2);
}
void DxbcDecodeContext::decodeOperation(DxbcCodeSlice code) {
uint32_t token = code.read();
// Result modifiers, which are applied to common ALU ops
m_instruction.modifiers.saturate = !!bit::extract(token, 13, 13);
m_instruction.modifiers.precise = !!bit::extract(token, 19, 22);
// Opcode controls. It will depend on the
// opcode itself which ones are valid.
m_instruction.controls = DxbcShaderOpcodeControls(token);
// Process extended opcode tokens
while (bit::extract(token, 31, 31)) {
token = code.read();
const DxbcExtOpcode extOpcode
= static_cast<DxbcExtOpcode>(bit::extract(token, 0, 5));
switch (extOpcode) {
case DxbcExtOpcode::SampleControls: {
struct {
int u : 4;
int v : 4;
int w : 4;
} aoffimmi;
aoffimmi.u = bit::extract(token, 9, 12);
aoffimmi.v = bit::extract(token, 13, 16);
aoffimmi.w = bit::extract(token, 17, 20);
// Four-bit signed numbers, sign-extend them
m_instruction.sampleControls.u = aoffimmi.u;
m_instruction.sampleControls.v = aoffimmi.v;
m_instruction.sampleControls.w = aoffimmi.w;
} break;
case DxbcExtOpcode::ResourceDim:
case DxbcExtOpcode::ResourceReturnType:
break; // part of resource description
default:
Logger::warn(str::format(
"DxbcDecodeContext: Unhandled extended opcode: ",
extOpcode));
}
}
// Retrieve the instruction format in order to parse the
// operands. Doing this mostly automatically means that
// the compiler can rely on the operands being valid.
const DxbcInstFormat format = dxbcInstructionFormat(m_instruction.op);
m_instruction.opClass = format.instructionClass;
for (uint32_t i = 0; i < format.operandCount; i++)
this->decodeOperand(code, format.operands[i]);
}
void DxbcDecodeContext::decodeComponentSelection(DxbcRegister& reg, uint32_t token) {
// Pick the correct component selection mode based on the
// component count. We'll simplify this here so that the
// compiler can assume that everything is a 4D vector.
reg.componentCount = static_cast<DxbcComponentCount>(bit::extract(token, 0, 1));
switch (reg.componentCount) {
// No components - used for samplers etc.
case DxbcComponentCount::Component0:
reg.mask = DxbcRegMask(false, false, false, false);
reg.swizzle = DxbcRegSwizzle(0, 0, 0, 0);
break;
// One component - used for immediates
// and a few built-in registers.
case DxbcComponentCount::Component1:
reg.mask = DxbcRegMask(true, false, false, false);
reg.swizzle = DxbcRegSwizzle(0, 0, 0, 0);
break;
// Four components - everything else. This requires us
// to actually parse the component selection mode.
case DxbcComponentCount::Component4: {
const DxbcRegMode componentMode =
static_cast<DxbcRegMode>(bit::extract(token, 2, 3));
switch (componentMode) {
// Write mask for destination operands
case DxbcRegMode::Mask:
reg.mask = bit::extract(token, 4, 7);
reg.swizzle = DxbcRegSwizzle(0, 1, 2, 3);
break;
// Swizzle for source operands (including resources)
case DxbcRegMode::Swizzle:
reg.mask = DxbcRegMask(true, true, true, true);
reg.swizzle = DxbcRegSwizzle(
bit::extract(token, 4, 5),
bit::extract(token, 6, 7),
bit::extract(token, 8, 9),
bit::extract(token, 10, 11));
break;
// Selection of one component. We can generate both a
// mask and a swizzle for this so that the compiler
// won't have to deal with this case specifically.
case DxbcRegMode::Select1: {
const uint32_t n = bit::extract(token, 4, 5);
reg.mask = DxbcRegMask(n == 0, n == 1, n == 2, n == 3);
reg.swizzle = DxbcRegSwizzle(n, n, n, n);
} break;
default:
Logger::warn("DxbcDecodeContext: Invalid component selection mode");
}
} break;
default:
Logger::warn("DxbcDecodeContext: Invalid component count");
}
}
void DxbcDecodeContext::decodeOperandExtensions(DxbcCodeSlice& code, DxbcRegister& reg, uint32_t token) {
while (bit::extract(token, 31, 31)) {
token = code.read();
// Type of the extended operand token
const DxbcOperandExt extTokenType =
static_cast<DxbcOperandExt>(bit::extract(token, 0, 5));
switch (extTokenType) {
// Operand modifiers, which are used to manipulate the
// value of a source operand during the load operation
case DxbcOperandExt::OperandModifier:
reg.modifiers = bit::extract(token, 6, 13);
break;
default:
Logger::warn(str::format(
"DxbcDecodeContext: Unhandled extended operand token: ",
extTokenType));
}
}
}
void DxbcDecodeContext::decodeOperandImmediates(DxbcCodeSlice& code, DxbcRegister& reg) {
if (reg.type == DxbcOperandType::Imm32
|| reg.type == DxbcOperandType::Imm64) {
switch (reg.componentCount) {
// This is commonly used if only one vector
// component is involved in an operation
case DxbcComponentCount::Component1: {
reg.imm.u32_1 = code.read();
} break;
// Typical four-component vector
case DxbcComponentCount::Component4: {
reg.imm.u32_4[0] = code.read();
reg.imm.u32_4[1] = code.read();
reg.imm.u32_4[2] = code.read();
reg.imm.u32_4[3] = code.read();
} break;
default:
Logger::warn("DxbcDecodeContext: Invalid component count for immediate operand");
}
}
}
void DxbcDecodeContext::decodeOperandIndex(DxbcCodeSlice& code, DxbcRegister& reg, uint32_t token) {
reg.idxDim = bit::extract(token, 20, 21);
for (uint32_t i = 0; i < reg.idxDim; i++) {
// An index can be encoded in various different ways
const DxbcOperandIndexRepresentation repr =
static_cast<DxbcOperandIndexRepresentation>(
bit::extract(token, 22 + 3 * i, 24 + 3 * i));
switch (repr) {
case DxbcOperandIndexRepresentation::Imm32:
reg.idx[i].offset = static_cast<int32_t>(code.read());
reg.idx[i].relReg = nullptr;
break;
case DxbcOperandIndexRepresentation::Relative:
reg.idx[i].offset = 0;
reg.idx[i].relReg = &m_indices.at(m_indexId);
this->decodeRegister(code,
m_indices.at(m_indexId++),
DxbcScalarType::Sint32);
break;
case DxbcOperandIndexRepresentation::Imm32Relative:
reg.idx[i].offset = static_cast<int32_t>(code.read());
reg.idx[i].relReg = &m_indices.at(m_indexId);
this->decodeRegister(code,
m_indices.at(m_indexId++),
DxbcScalarType::Sint32);
break;
default:
Logger::warn(str::format(
"DxbcDecodeContext: Unhandled index representation: ",
repr));
}
}
}
void DxbcDecodeContext::decodeRegister(DxbcCodeSlice& code, DxbcRegister& reg, DxbcScalarType type) {
const uint32_t token = code.read();
reg.type = static_cast<DxbcOperandType>(bit::extract(token, 12, 19));
reg.dataType = type;
reg.modifiers = 0;
reg.idxDim = 0;
for (uint32_t i = 0; i < DxbcMaxRegIndexDim; i++) {
reg.idx[i].relReg = nullptr;
reg.idx[i].offset = 0;
}
this->decodeComponentSelection(reg, token);
this->decodeOperandExtensions(code, reg, token);
this->decodeOperandImmediates(code, reg);
this->decodeOperandIndex(code, reg, token);
}
void DxbcDecodeContext::decodeImm32(DxbcCodeSlice& code, DxbcImmediate& imm, DxbcScalarType type) {
imm.u32 = code.read();
}
void DxbcDecodeContext::decodeOperand(DxbcCodeSlice& code, const DxbcInstOperandFormat& format) {
switch (format.kind) {
case DxbcOperandKind::DstReg: {
const uint32_t operandId = m_instruction.dstCount++;
this->decodeRegister(code, m_dstOperands.at(operandId), format.type);
} break;
case DxbcOperandKind::SrcReg: {
const uint32_t operandId = m_instruction.srcCount++;
this->decodeRegister(code, m_srcOperands.at(operandId), format.type);
} break;
case DxbcOperandKind::Imm32: {
const uint32_t operandId = m_instruction.immCount++;
this->decodeImm32(code, m_immOperands.at(operandId), format.type);
} break;
default:
throw DxvkError("DxbcDecodeContext: Invalid operand format");
}
}
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,30 @@
#include "dxbc_header.h"
namespace dxvk {
DxbcHeader::DxbcHeader(DxbcReader& reader) {
// FourCC at the start of the file, must be 'DXBC'
DxbcTag fourcc = reader.readTag();
if (fourcc != "DXBC")
throw DxvkError("DxbcHeader::DxbcHeader: Invalid fourcc, expected 'DXBC'");
// Stuff we don't actually need to store
reader.skip(4 * sizeof(uint32_t)); // Check sum
reader.skip(1 * sizeof(uint32_t)); // Constant 1
reader.skip(1 * sizeof(uint32_t)); // Bytecode length
// Number of chunks in the file
uint32_t chunkCount = reader.readu32();
// Chunk offsets are stored immediately after
for (uint32_t i = 0; i < chunkCount; i++)
m_chunkOffsets.push_back(reader.readu32());
}
DxbcHeader::~DxbcHeader() {
}
}
@@ -0,0 +1,120 @@
#include "dxbc_analysis.h"
#include "dxbc_compiler.h"
#include "dxbc_module.h"
namespace dxvk {
DxbcModule::DxbcModule(DxbcReader& reader)
: m_header(reader) {
for (uint32_t i = 0; i < m_header.numChunks(); i++) {
// The chunk tag is stored at the beginning of each chunk
auto chunkReader = reader.clone(m_header.chunkOffset(i));
auto tag = chunkReader.readTag();
// The chunk size follows right after the four-character
// code. This does not include the eight bytes that are
// consumed by the FourCC and chunk length entry.
auto chunkLength = chunkReader.readu32();
chunkReader = chunkReader.clone(8);
chunkReader = chunkReader.resize(chunkLength);
if ((tag == "SHDR") || (tag == "SHEX"))
m_shexChunk = new DxbcShex(chunkReader);
if ((tag == "ISGN") || (tag == "ISG1"))
m_isgnChunk = new DxbcIsgn(chunkReader, tag);
if ((tag == "OSGN") || (tag == "OSG5") || (tag == "OSG1"))
m_osgnChunk = new DxbcIsgn(chunkReader, tag);
if ((tag == "PCSG") || (tag == "PSG1"))
m_psgnChunk = new DxbcIsgn(chunkReader, tag);
}
}
DxbcModule::~DxbcModule() {
}
SpirvCodeBuffer DxbcModule::compile(
const DxbcModuleInfo& moduleInfo,
const std::string& fileName) {
if (m_shexChunk == nullptr)
throw DxvkError("DxbcModule::compile: No SHDR/SHEX chunk");
DxbcAnalysisInfo analysisInfo;
DxbcAnalyzer analyzer(moduleInfo,
m_shexChunk->programInfo(),
m_isgnChunk, m_osgnChunk,
m_psgnChunk, analysisInfo);
this->runAnalyzer(analyzer, m_shexChunk->slice());
m_bindings = std::make_optional(analysisInfo.bindings);
DxbcCompiler compiler(
fileName, moduleInfo,
m_shexChunk->programInfo(),
m_isgnChunk, m_osgnChunk,
m_psgnChunk, analysisInfo);
this->runCompiler(compiler, m_shexChunk->slice());
m_icb = compiler.getIcbData();
return compiler.finalize();
}
SpirvCodeBuffer DxbcModule::compilePassthroughShader(
const DxbcModuleInfo& moduleInfo,
const std::string& fileName) const {
if (m_shexChunk == nullptr)
throw DxvkError("DxbcModule::compile: No SHDR/SHEX chunk");
DxbcAnalysisInfo analysisInfo;
DxbcCompiler compiler(
fileName, moduleInfo,
DxbcProgramType::GeometryShader,
m_osgnChunk, m_osgnChunk,
m_psgnChunk, analysisInfo);
compiler.processXfbPassthrough();
return compiler.finalize();
}
void DxbcModule::runAnalyzer(
DxbcAnalyzer& analyzer,
DxbcCodeSlice slice) const {
DxbcDecodeContext decoder;
while (!slice.atEnd()) {
decoder.decodeInstruction(slice);
analyzer.processInstruction(
decoder.getInstruction());
}
}
void DxbcModule::runCompiler(
DxbcCompiler& compiler,
DxbcCodeSlice slice) const {
DxbcDecodeContext decoder;
while (!slice.atEnd()) {
decoder.decodeInstruction(slice);
compiler.processInstruction(
decoder.getInstruction());
}
}
}
+445
View File
@@ -0,0 +1,445 @@
#include "dxbc_names.h"
namespace dxvk {
std::ostream& operator << (std::ostream& os, DxbcOpcode e) {
switch (e) {
ENUM_NAME(DxbcOpcode::Add);
ENUM_NAME(DxbcOpcode::And);
ENUM_NAME(DxbcOpcode::Break);
ENUM_NAME(DxbcOpcode::Breakc);
ENUM_NAME(DxbcOpcode::Call);
ENUM_NAME(DxbcOpcode::Callc);
ENUM_NAME(DxbcOpcode::Case);
ENUM_NAME(DxbcOpcode::Continue);
ENUM_NAME(DxbcOpcode::Continuec);
ENUM_NAME(DxbcOpcode::Cut);
ENUM_NAME(DxbcOpcode::Default);
ENUM_NAME(DxbcOpcode::DerivRtx);
ENUM_NAME(DxbcOpcode::DerivRty);
ENUM_NAME(DxbcOpcode::Discard);
ENUM_NAME(DxbcOpcode::Div);
ENUM_NAME(DxbcOpcode::Dp2);
ENUM_NAME(DxbcOpcode::Dp3);
ENUM_NAME(DxbcOpcode::Dp4);
ENUM_NAME(DxbcOpcode::Else);
ENUM_NAME(DxbcOpcode::Emit);
ENUM_NAME(DxbcOpcode::EmitThenCut);
ENUM_NAME(DxbcOpcode::EndIf);
ENUM_NAME(DxbcOpcode::EndLoop);
ENUM_NAME(DxbcOpcode::EndSwitch);
ENUM_NAME(DxbcOpcode::Eq);
ENUM_NAME(DxbcOpcode::Exp);
ENUM_NAME(DxbcOpcode::Frc);
ENUM_NAME(DxbcOpcode::FtoI);
ENUM_NAME(DxbcOpcode::FtoU);
ENUM_NAME(DxbcOpcode::Ge);
ENUM_NAME(DxbcOpcode::IAdd);
ENUM_NAME(DxbcOpcode::If);
ENUM_NAME(DxbcOpcode::IEq);
ENUM_NAME(DxbcOpcode::IGe);
ENUM_NAME(DxbcOpcode::ILt);
ENUM_NAME(DxbcOpcode::IMad);
ENUM_NAME(DxbcOpcode::IMax);
ENUM_NAME(DxbcOpcode::IMin);
ENUM_NAME(DxbcOpcode::IMul);
ENUM_NAME(DxbcOpcode::INe);
ENUM_NAME(DxbcOpcode::INeg);
ENUM_NAME(DxbcOpcode::IShl);
ENUM_NAME(DxbcOpcode::IShr);
ENUM_NAME(DxbcOpcode::ItoF);
ENUM_NAME(DxbcOpcode::Label);
ENUM_NAME(DxbcOpcode::Ld);
ENUM_NAME(DxbcOpcode::LdMs);
ENUM_NAME(DxbcOpcode::Log);
ENUM_NAME(DxbcOpcode::Loop);
ENUM_NAME(DxbcOpcode::Lt);
ENUM_NAME(DxbcOpcode::Mad);
ENUM_NAME(DxbcOpcode::Min);
ENUM_NAME(DxbcOpcode::Max);
ENUM_NAME(DxbcOpcode::CustomData);
ENUM_NAME(DxbcOpcode::Mov);
ENUM_NAME(DxbcOpcode::Movc);
ENUM_NAME(DxbcOpcode::Mul);
ENUM_NAME(DxbcOpcode::Ne);
ENUM_NAME(DxbcOpcode::Nop);
ENUM_NAME(DxbcOpcode::Not);
ENUM_NAME(DxbcOpcode::Or);
ENUM_NAME(DxbcOpcode::ResInfo);
ENUM_NAME(DxbcOpcode::Ret);
ENUM_NAME(DxbcOpcode::Retc);
ENUM_NAME(DxbcOpcode::RoundNe);
ENUM_NAME(DxbcOpcode::RoundNi);
ENUM_NAME(DxbcOpcode::RoundPi);
ENUM_NAME(DxbcOpcode::RoundZ);
ENUM_NAME(DxbcOpcode::Rsq);
ENUM_NAME(DxbcOpcode::Sample);
ENUM_NAME(DxbcOpcode::SampleC);
ENUM_NAME(DxbcOpcode::SampleClz);
ENUM_NAME(DxbcOpcode::SampleL);
ENUM_NAME(DxbcOpcode::SampleD);
ENUM_NAME(DxbcOpcode::SampleB);
ENUM_NAME(DxbcOpcode::Sqrt);
ENUM_NAME(DxbcOpcode::Switch);
ENUM_NAME(DxbcOpcode::SinCos);
ENUM_NAME(DxbcOpcode::UDiv);
ENUM_NAME(DxbcOpcode::ULt);
ENUM_NAME(DxbcOpcode::UGe);
ENUM_NAME(DxbcOpcode::UMul);
ENUM_NAME(DxbcOpcode::UMad);
ENUM_NAME(DxbcOpcode::UMax);
ENUM_NAME(DxbcOpcode::UMin);
ENUM_NAME(DxbcOpcode::UShr);
ENUM_NAME(DxbcOpcode::UtoF);
ENUM_NAME(DxbcOpcode::Xor);
ENUM_NAME(DxbcOpcode::DclResource);
ENUM_NAME(DxbcOpcode::DclConstantBuffer);
ENUM_NAME(DxbcOpcode::DclSampler);
ENUM_NAME(DxbcOpcode::DclIndexRange);
ENUM_NAME(DxbcOpcode::DclGsOutputPrimitiveTopology);
ENUM_NAME(DxbcOpcode::DclGsInputPrimitive);
ENUM_NAME(DxbcOpcode::DclMaxOutputVertexCount);
ENUM_NAME(DxbcOpcode::DclInput);
ENUM_NAME(DxbcOpcode::DclInputSgv);
ENUM_NAME(DxbcOpcode::DclInputSiv);
ENUM_NAME(DxbcOpcode::DclInputPs);
ENUM_NAME(DxbcOpcode::DclInputPsSgv);
ENUM_NAME(DxbcOpcode::DclInputPsSiv);
ENUM_NAME(DxbcOpcode::DclOutput);
ENUM_NAME(DxbcOpcode::DclOutputSgv);
ENUM_NAME(DxbcOpcode::DclOutputSiv);
ENUM_NAME(DxbcOpcode::DclTemps);
ENUM_NAME(DxbcOpcode::DclIndexableTemp);
ENUM_NAME(DxbcOpcode::DclGlobalFlags);
ENUM_NAME(DxbcOpcode::Reserved0);
ENUM_NAME(DxbcOpcode::Lod);
ENUM_NAME(DxbcOpcode::Gather4);
ENUM_NAME(DxbcOpcode::SamplePos);
ENUM_NAME(DxbcOpcode::SampleInfo);
ENUM_NAME(DxbcOpcode::Reserved1);
ENUM_NAME(DxbcOpcode::HsDecls);
ENUM_NAME(DxbcOpcode::HsControlPointPhase);
ENUM_NAME(DxbcOpcode::HsForkPhase);
ENUM_NAME(DxbcOpcode::HsJoinPhase);
ENUM_NAME(DxbcOpcode::EmitStream);
ENUM_NAME(DxbcOpcode::CutStream);
ENUM_NAME(DxbcOpcode::EmitThenCutStream);
ENUM_NAME(DxbcOpcode::InterfaceCall);
ENUM_NAME(DxbcOpcode::BufInfo);
ENUM_NAME(DxbcOpcode::DerivRtxCoarse);
ENUM_NAME(DxbcOpcode::DerivRtxFine);
ENUM_NAME(DxbcOpcode::DerivRtyCoarse);
ENUM_NAME(DxbcOpcode::DerivRtyFine);
ENUM_NAME(DxbcOpcode::Gather4C);
ENUM_NAME(DxbcOpcode::Gather4Po);
ENUM_NAME(DxbcOpcode::Gather4PoC);
ENUM_NAME(DxbcOpcode::Rcp);
ENUM_NAME(DxbcOpcode::F32toF16);
ENUM_NAME(DxbcOpcode::F16toF32);
ENUM_NAME(DxbcOpcode::UAddc);
ENUM_NAME(DxbcOpcode::USubb);
ENUM_NAME(DxbcOpcode::CountBits);
ENUM_NAME(DxbcOpcode::FirstBitHi);
ENUM_NAME(DxbcOpcode::FirstBitLo);
ENUM_NAME(DxbcOpcode::FirstBitShi);
ENUM_NAME(DxbcOpcode::UBfe);
ENUM_NAME(DxbcOpcode::IBfe);
ENUM_NAME(DxbcOpcode::Bfi);
ENUM_NAME(DxbcOpcode::BfRev);
ENUM_NAME(DxbcOpcode::Swapc);
ENUM_NAME(DxbcOpcode::DclStream);
ENUM_NAME(DxbcOpcode::DclFunctionBody);
ENUM_NAME(DxbcOpcode::DclFunctionTable);
ENUM_NAME(DxbcOpcode::DclInterface);
ENUM_NAME(DxbcOpcode::DclInputControlPointCount);
ENUM_NAME(DxbcOpcode::DclOutputControlPointCount);
ENUM_NAME(DxbcOpcode::DclTessDomain);
ENUM_NAME(DxbcOpcode::DclTessPartitioning);
ENUM_NAME(DxbcOpcode::DclTessOutputPrimitive);
ENUM_NAME(DxbcOpcode::DclHsMaxTessFactor);
ENUM_NAME(DxbcOpcode::DclHsForkPhaseInstanceCount);
ENUM_NAME(DxbcOpcode::DclHsJoinPhaseInstanceCount);
ENUM_NAME(DxbcOpcode::DclThreadGroup);
ENUM_NAME(DxbcOpcode::DclUavTyped);
ENUM_NAME(DxbcOpcode::DclUavRaw);
ENUM_NAME(DxbcOpcode::DclUavStructured);
ENUM_NAME(DxbcOpcode::DclThreadGroupSharedMemoryRaw);
ENUM_NAME(DxbcOpcode::DclThreadGroupSharedMemoryStructured);
ENUM_NAME(DxbcOpcode::DclResourceRaw);
ENUM_NAME(DxbcOpcode::DclResourceStructured);
ENUM_NAME(DxbcOpcode::LdUavTyped);
ENUM_NAME(DxbcOpcode::StoreUavTyped);
ENUM_NAME(DxbcOpcode::LdRaw);
ENUM_NAME(DxbcOpcode::StoreRaw);
ENUM_NAME(DxbcOpcode::LdStructured);
ENUM_NAME(DxbcOpcode::StoreStructured);
ENUM_NAME(DxbcOpcode::AtomicAnd);
ENUM_NAME(DxbcOpcode::AtomicOr);
ENUM_NAME(DxbcOpcode::AtomicXor);
ENUM_NAME(DxbcOpcode::AtomicCmpStore);
ENUM_NAME(DxbcOpcode::AtomicIAdd);
ENUM_NAME(DxbcOpcode::AtomicIMax);
ENUM_NAME(DxbcOpcode::AtomicIMin);
ENUM_NAME(DxbcOpcode::AtomicUMax);
ENUM_NAME(DxbcOpcode::AtomicUMin);
ENUM_NAME(DxbcOpcode::ImmAtomicAlloc);
ENUM_NAME(DxbcOpcode::ImmAtomicConsume);
ENUM_NAME(DxbcOpcode::ImmAtomicIAdd);
ENUM_NAME(DxbcOpcode::ImmAtomicAnd);
ENUM_NAME(DxbcOpcode::ImmAtomicOr);
ENUM_NAME(DxbcOpcode::ImmAtomicXor);
ENUM_NAME(DxbcOpcode::ImmAtomicExch);
ENUM_NAME(DxbcOpcode::ImmAtomicCmpExch);
ENUM_NAME(DxbcOpcode::ImmAtomicIMax);
ENUM_NAME(DxbcOpcode::ImmAtomicIMin);
ENUM_NAME(DxbcOpcode::ImmAtomicUMax);
ENUM_NAME(DxbcOpcode::ImmAtomicUMin);
ENUM_NAME(DxbcOpcode::Sync);
ENUM_NAME(DxbcOpcode::DAdd);
ENUM_NAME(DxbcOpcode::DMax);
ENUM_NAME(DxbcOpcode::DMin);
ENUM_NAME(DxbcOpcode::DMul);
ENUM_NAME(DxbcOpcode::DEq);
ENUM_NAME(DxbcOpcode::DGe);
ENUM_NAME(DxbcOpcode::DLt);
ENUM_NAME(DxbcOpcode::DNe);
ENUM_NAME(DxbcOpcode::DMov);
ENUM_NAME(DxbcOpcode::DMovc);
ENUM_NAME(DxbcOpcode::DtoF);
ENUM_NAME(DxbcOpcode::FtoD);
ENUM_NAME(DxbcOpcode::EvalSnapped);
ENUM_NAME(DxbcOpcode::EvalSampleIndex);
ENUM_NAME(DxbcOpcode::EvalCentroid);
ENUM_NAME(DxbcOpcode::DclGsInstanceCount);
ENUM_DEFAULT(e);
}
}
std::ostream& operator << (std::ostream& os, DxbcExtOpcode e) {
switch (e) {
ENUM_NAME(DxbcExtOpcode::Empty);
ENUM_NAME(DxbcExtOpcode::SampleControls);
ENUM_NAME(DxbcExtOpcode::ResourceDim);
ENUM_NAME(DxbcExtOpcode::ResourceReturnType);
ENUM_DEFAULT(e);
}
}
std::ostream& operator << (std::ostream& os, DxbcOperandType e) {
switch (e) {
ENUM_NAME(DxbcOperandType::Temp);
ENUM_NAME(DxbcOperandType::Input);
ENUM_NAME(DxbcOperandType::Output);
ENUM_NAME(DxbcOperandType::IndexableTemp);
ENUM_NAME(DxbcOperandType::Imm32);
ENUM_NAME(DxbcOperandType::Imm64);
ENUM_NAME(DxbcOperandType::Sampler);
ENUM_NAME(DxbcOperandType::Resource);
ENUM_NAME(DxbcOperandType::ConstantBuffer);
ENUM_NAME(DxbcOperandType::ImmediateConstantBuffer);
ENUM_NAME(DxbcOperandType::Label);
ENUM_NAME(DxbcOperandType::InputPrimitiveId);
ENUM_NAME(DxbcOperandType::OutputDepth);
ENUM_NAME(DxbcOperandType::Null);
ENUM_NAME(DxbcOperandType::Rasterizer);
ENUM_NAME(DxbcOperandType::OutputCoverageMask);
ENUM_NAME(DxbcOperandType::Stream);
ENUM_NAME(DxbcOperandType::FunctionBody);
ENUM_NAME(DxbcOperandType::FunctionTable);
ENUM_NAME(DxbcOperandType::Interface);
ENUM_NAME(DxbcOperandType::FunctionInput);
ENUM_NAME(DxbcOperandType::FunctionOutput);
ENUM_NAME(DxbcOperandType::OutputControlPointId);
ENUM_NAME(DxbcOperandType::InputForkInstanceId);
ENUM_NAME(DxbcOperandType::InputJoinInstanceId);
ENUM_NAME(DxbcOperandType::InputControlPoint);
ENUM_NAME(DxbcOperandType::OutputControlPoint);
ENUM_NAME(DxbcOperandType::InputPatchConstant);
ENUM_NAME(DxbcOperandType::InputDomainPoint);
ENUM_NAME(DxbcOperandType::ThisPointer);
ENUM_NAME(DxbcOperandType::UnorderedAccessView);
ENUM_NAME(DxbcOperandType::ThreadGroupSharedMemory);
ENUM_NAME(DxbcOperandType::InputThreadId);
ENUM_NAME(DxbcOperandType::InputThreadGroupId);
ENUM_NAME(DxbcOperandType::InputThreadIdInGroup);
ENUM_NAME(DxbcOperandType::InputCoverageMask);
ENUM_NAME(DxbcOperandType::InputThreadIndexInGroup);
ENUM_NAME(DxbcOperandType::InputGsInstanceId);
ENUM_NAME(DxbcOperandType::OutputDepthGe);
ENUM_NAME(DxbcOperandType::OutputDepthLe);
ENUM_NAME(DxbcOperandType::CycleCounter);
ENUM_DEFAULT(e);
}
}
std::ostream& operator << (std::ostream& os, dxvk::DxbcOperandExt e) {
switch (e) {
ENUM_NAME(DxbcOperandExt::OperandModifier);
ENUM_DEFAULT(e);
}
}
std::ostream& operator << (std::ostream& os, DxbcComponentCount e) {
switch (e) {
ENUM_NAME(DxbcComponentCount::Component0);
ENUM_NAME(DxbcComponentCount::Component1);
ENUM_NAME(DxbcComponentCount::Component4);
ENUM_DEFAULT(e);
}
}
std::ostream& operator << (std::ostream& os, DxbcRegMode e) {
switch (e) {
ENUM_NAME(DxbcRegMode::Mask);
ENUM_NAME(DxbcRegMode::Swizzle);
ENUM_NAME(DxbcRegMode::Select1);
ENUM_DEFAULT(e);
}
}
std::ostream& operator << (std::ostream& os, DxbcOperandIndexRepresentation e) {
switch (e) {
ENUM_NAME(DxbcOperandIndexRepresentation::Imm32);
ENUM_NAME(DxbcOperandIndexRepresentation::Imm64);
ENUM_NAME(DxbcOperandIndexRepresentation::Relative);
ENUM_NAME(DxbcOperandIndexRepresentation::Imm32Relative);
ENUM_NAME(DxbcOperandIndexRepresentation::Imm64Relative);
ENUM_DEFAULT(e);
}
}
std::ostream& operator << (std::ostream& os, DxbcResourceDim e) {
switch (e) {
ENUM_NAME(DxbcResourceDim::Unknown);
ENUM_NAME(DxbcResourceDim::Buffer);
ENUM_NAME(DxbcResourceDim::Texture1D);
ENUM_NAME(DxbcResourceDim::Texture2D);
ENUM_NAME(DxbcResourceDim::Texture2DMs);
ENUM_NAME(DxbcResourceDim::Texture3D);
ENUM_NAME(DxbcResourceDim::TextureCube);
ENUM_NAME(DxbcResourceDim::Texture1DArr);
ENUM_NAME(DxbcResourceDim::Texture2DArr);
ENUM_NAME(DxbcResourceDim::Texture2DMsArr);
ENUM_NAME(DxbcResourceDim::TextureCubeArr);
ENUM_NAME(DxbcResourceDim::RawBuffer);
ENUM_NAME(DxbcResourceDim::StructuredBuffer);
ENUM_DEFAULT(e);
}
}
std::ostream& operator << (std::ostream& os, DxbcResourceReturnType e) {
switch (e) {
ENUM_NAME(DxbcResourceReturnType::Unorm);
ENUM_NAME(DxbcResourceReturnType::Snorm);
ENUM_NAME(DxbcResourceReturnType::Sint);
ENUM_NAME(DxbcResourceReturnType::Uint);
ENUM_NAME(DxbcResourceReturnType::Float);
ENUM_NAME(DxbcResourceReturnType::Mixed);
ENUM_NAME(DxbcResourceReturnType::Double);
ENUM_NAME(DxbcResourceReturnType::Continued);
ENUM_NAME(DxbcResourceReturnType::Unused);
ENUM_DEFAULT(e);
}
}
std::ostream& operator << (std::ostream& os, DxbcRegisterComponentType e) {
switch (e) {
ENUM_NAME(DxbcRegisterComponentType::Unknown);
ENUM_NAME(DxbcRegisterComponentType::Uint32);
ENUM_NAME(DxbcRegisterComponentType::Sint32);
ENUM_NAME(DxbcRegisterComponentType::Float32);
ENUM_DEFAULT(e);
}
}
std::ostream& operator << (std::ostream& os, DxbcInstructionReturnType e) {
switch (e) {
ENUM_NAME(DxbcInstructionReturnType::Float);
ENUM_NAME(DxbcInstructionReturnType::Uint);
ENUM_DEFAULT(e);
}
}
std::ostream& operator << (std::ostream& os, DxbcSystemValue e) {
switch (e) {
ENUM_NAME(DxbcSystemValue::None);
ENUM_NAME(DxbcSystemValue::Position);
ENUM_NAME(DxbcSystemValue::ClipDistance);
ENUM_NAME(DxbcSystemValue::CullDistance);
ENUM_NAME(DxbcSystemValue::RenderTargetId);
ENUM_NAME(DxbcSystemValue::ViewportId);
ENUM_NAME(DxbcSystemValue::VertexId);
ENUM_NAME(DxbcSystemValue::PrimitiveId);
ENUM_NAME(DxbcSystemValue::InstanceId);
ENUM_NAME(DxbcSystemValue::IsFrontFace);
ENUM_NAME(DxbcSystemValue::SampleIndex);
ENUM_NAME(DxbcSystemValue::FinalQuadUeq0EdgeTessFactor);
ENUM_NAME(DxbcSystemValue::FinalQuadVeq0EdgeTessFactor);
ENUM_NAME(DxbcSystemValue::FinalQuadUeq1EdgeTessFactor);
ENUM_NAME(DxbcSystemValue::FinalQuadVeq1EdgeTessFactor);
ENUM_NAME(DxbcSystemValue::FinalQuadUInsideTessFactor);
ENUM_NAME(DxbcSystemValue::FinalQuadVInsideTessFactor);
ENUM_NAME(DxbcSystemValue::FinalTriUeq0EdgeTessFactor);
ENUM_NAME(DxbcSystemValue::FinalTriVeq0EdgeTessFactor);
ENUM_NAME(DxbcSystemValue::FinalTriWeq0EdgeTessFactor);
ENUM_NAME(DxbcSystemValue::FinalTriInsideTessFactor);
ENUM_NAME(DxbcSystemValue::FinalLineDetailTessFactor);
ENUM_NAME(DxbcSystemValue::FinalLineDensityTessFactor);
ENUM_NAME(DxbcSystemValue::Target);
ENUM_NAME(DxbcSystemValue::Depth);
ENUM_NAME(DxbcSystemValue::Coverage);
ENUM_NAME(DxbcSystemValue::DepthGe);
ENUM_NAME(DxbcSystemValue::DepthLe);
ENUM_DEFAULT(e);
}
}
std::ostream& operator << (std::ostream& os, dxvk::DxbcProgramType e) {
switch (e) {
ENUM_NAME(DxbcProgramType::PixelShader);
ENUM_NAME(DxbcProgramType::VertexShader);
ENUM_NAME(DxbcProgramType::GeometryShader);
ENUM_NAME(DxbcProgramType::HullShader);
ENUM_NAME(DxbcProgramType::DomainShader);
ENUM_NAME(DxbcProgramType::ComputeShader);
ENUM_DEFAULT(e);
}
}
std::ostream& operator << (std::ostream& os, dxvk::DxbcCustomDataClass e) {
switch (e) {
ENUM_NAME(DxbcCustomDataClass::Comment);
ENUM_NAME(DxbcCustomDataClass::DebugInfo);
ENUM_NAME(DxbcCustomDataClass::Opaque);
ENUM_NAME(DxbcCustomDataClass::ImmConstBuf);
ENUM_DEFAULT(e);
}
}
std::ostream& operator << (std::ostream& os, dxvk::DxbcScalarType e) {
switch (e) {
ENUM_NAME(DxbcScalarType::Uint32);
ENUM_NAME(DxbcScalarType::Uint64);
ENUM_NAME(DxbcScalarType::Sint32);
ENUM_NAME(DxbcScalarType::Sint64);
ENUM_NAME(DxbcScalarType::Float32);
ENUM_NAME(DxbcScalarType::Float64);
ENUM_NAME(DxbcScalarType::Bool);
ENUM_DEFAULT(e);
}
}
} //namespace dxvk
@@ -0,0 +1,58 @@
#include <cstring>
#include "dxbc_reader.h"
namespace dxvk {
DxbcTag DxbcReader::readTag() {
DxbcTag tag;
this->read(&tag, 4);
return tag;
}
std::string DxbcReader::readString() {
std::string result;
while (m_data[m_pos] != '\0')
result.push_back(m_data[m_pos++]);
m_pos++;
return result;
}
void DxbcReader::read(void* dst, size_t n) {
if (m_pos + n > m_size)
throw DxvkError("DxbcReader::read: Unexpected end of file");
std::memcpy(dst, m_data + m_pos, n);
m_pos += n;
}
void DxbcReader::skip(size_t n) {
if (m_pos + n > m_size)
throw DxvkError("DxbcReader::skip: Unexpected end of file");
m_pos += n;
}
DxbcReader DxbcReader::clone(size_t pos) const {
if (pos > m_size)
throw DxvkError("DxbcReader::clone: Invalid offset");
return DxbcReader(m_data + pos, m_size - pos);
}
DxbcReader DxbcReader::resize(size_t size) const {
if (size > m_size)
throw DxvkError("DxbcReader::resize: Invalid size");
return DxbcReader(m_data, size, m_pos);
}
void DxbcReader::store(std::ostream&& stream) const {
stream.write(m_data, m_size);
}
}
+26
View File
@@ -0,0 +1,26 @@
#include "dxbc_util.h"
namespace dxvk {
uint32_t primitiveVertexCount(DxbcPrimitive primitive) {
static const std::array<uint32_t, 8> s_vertexCounts = {
0, // Undefined
1, // Point
2, // Line
3, // Triangle
0, // Undefined
0, // Undefined
4, // Line with adjacency
6, // Triangle with adjacency
};
if (primitive >= DxbcPrimitive::Patch1) {
return uint32_t(primitive)
- uint32_t(DxbcPrimitive::Patch1)
+ 1u;
} else {
return s_vertexCounts.at(uint32_t(primitive));
}
}
}
@@ -0,0 +1,166 @@
#include <array>
#include <cstring>
#include "spirv_code_buffer.h"
namespace dxvk {
SpirvCodeBuffer:: SpirvCodeBuffer() { }
SpirvCodeBuffer::~SpirvCodeBuffer() { }
SpirvCodeBuffer::SpirvCodeBuffer(uint32_t size)
: m_ptr(size) {
m_code.resize(size);
}
SpirvCodeBuffer::SpirvCodeBuffer(uint32_t size, const uint32_t* data)
: m_ptr(size) {
m_code.resize(size);
std::memcpy(m_code.data(), data, size * sizeof(uint32_t));
}
SpirvCodeBuffer::SpirvCodeBuffer(std::istream& stream) {
stream.ignore(std::numeric_limits<std::streamsize>::max());
std::streamsize length = stream.gcount();
stream.clear();
stream.seekg(0, std::ios_base::beg);
std::vector<char> buffer(length);
stream.read(buffer.data(), length);
buffer.resize(stream.gcount());
m_code.resize(buffer.size() / sizeof(uint32_t));
std::memcpy(reinterpret_cast<char*>(m_code.data()),
buffer.data(), m_code.size() * sizeof(uint32_t));
m_ptr = m_code.size();
}
uint32_t SpirvCodeBuffer::allocId() {
constexpr size_t BoundIdsOffset = 3;
if (m_code.size() <= BoundIdsOffset)
return 0;
return m_code[BoundIdsOffset]++;
}
void SpirvCodeBuffer::append(const SpirvInstruction& ins) {
const size_t size = m_code.size();
m_code.resize(size + ins.length());
for (uint32_t i = 0; i < ins.length(); i++)
m_code[size + i] = ins.arg(i);
m_ptr += ins.length();
}
void SpirvCodeBuffer::append(const SpirvCodeBuffer& other) {
if (other.size() != 0) {
const size_t size = m_code.size();
m_code.resize(size + other.m_code.size());
uint32_t* dst = this->m_code.data();
const uint32_t* src = other.m_code.data();
std::memcpy(dst + size, src, other.size());
m_ptr += other.m_code.size();
}
}
void SpirvCodeBuffer::putWord(uint32_t word) {
m_code.insert(m_code.begin() + m_ptr, word);
m_ptr += 1;
}
void SpirvCodeBuffer::putIns(spv::Op opCode, uint16_t wordCount) {
this->putWord(
(static_cast<uint32_t>(opCode) << 0)
| (static_cast<uint32_t>(wordCount) << 16));
}
void SpirvCodeBuffer::putInt32(uint32_t word) {
this->putWord(word);
}
void SpirvCodeBuffer::putInt64(uint64_t value) {
this->putWord(value >> 0);
this->putWord(value >> 32);
}
void SpirvCodeBuffer::putFloat32(float value) {
uint32_t tmp;
static_assert(sizeof(tmp) == sizeof(value));
std::memcpy(&tmp, &value, sizeof(value));
this->putInt32(tmp);
}
void SpirvCodeBuffer::putFloat64(double value) {
uint64_t tmp;
static_assert(sizeof(tmp) == sizeof(value));
std::memcpy(&tmp, &value, sizeof(value));
this->putInt64(tmp);
}
void SpirvCodeBuffer::putStr(const char* str) {
uint32_t word = 0;
uint32_t nbit = 0;
for (uint32_t i = 0; str[i] != '\0'; str++) {
word |= (static_cast<uint32_t>(str[i]) & 0xFF) << nbit;
if ((nbit += 8) == 32) {
this->putWord(word);
word = 0;
nbit = 0;
}
}
// Commit current word
this->putWord(word);
}
void SpirvCodeBuffer::putHeader(uint32_t version, uint32_t boundIds) {
this->putWord(spv::MagicNumber);
this->putWord(version);
this->putWord(0); // Generator
this->putWord(boundIds);
this->putWord(0); // Schema
}
void SpirvCodeBuffer::erase(size_t size) {
m_code.erase(
m_code.begin() + m_ptr,
m_code.begin() + m_ptr + size);
}
uint32_t SpirvCodeBuffer::strLen(const char* str) {
// Null-termination plus padding
return (std::strlen(str) + 4) / 4;
}
void SpirvCodeBuffer::store(std::ostream& stream) const {
stream.write(
reinterpret_cast<const char*>(m_code.data()),
sizeof(uint32_t) * m_code.size());
}
}
File diff suppressed because it is too large. Load diff
+11
View File
@@ -0,0 +1,11 @@
#include "log/log_debug.h"
namespace dxvk::debug {
std::string methodName(const std::string& prettyName) {
size_t end = prettyName.find("(");
size_t begin = prettyName.substr(0, end).rfind(" ") + 1;
return prettyName.substr(begin,end - begin);
}
}
@@ -0,0 +1,234 @@
#include "util_string.h"
namespace dxvk::str {
const uint8_t* decodeTypedChar(
const uint8_t* begin,
const uint8_t* end,
uint32_t& ch) {
uint32_t first = begin[0];
if (likely(first < 0x80)) {
// Basic ASCII character
ch = uint32_t(first);
return begin + 1;
} else if (unlikely(first < 0xC0)) {
// Character starts with a continuation byte,
// just skip until we find the next valid prefix
while ((begin < end) && (((*begin) & 0xC0) == 0x80))
begin += 1;
ch = uint32_t('?');
return begin;
} else {
// The number of leading 1 bits in the first byte
// determines the length of this character
size_t length = bit::lzcnt((~first) << 24);
if (unlikely(begin + length > end)) {
ch = uint32_t('?');
return end;
}
if (first < 0xE0) {
ch = ((uint32_t(begin[0]) & 0x1F) << 6)
| ((uint32_t(begin[1]) & 0x3F));
} else if (first < 0xF0) {
ch = ((uint32_t(begin[0]) & 0x0F) << 12)
| ((uint32_t(begin[1]) & 0x3F) << 6)
| ((uint32_t(begin[2]) & 0x3F));
} else if (first < 0xF8) {
ch = ((uint32_t(begin[0]) & 0x07) << 18)
| ((uint32_t(begin[1]) & 0x3F) << 12)
| ((uint32_t(begin[2]) & 0x3F) << 6)
| ((uint32_t(begin[3]) & 0x3F));
} else {
// Invalid prefix
ch = uint32_t('?');
}
return begin + length;
}
}
const uint16_t* decodeTypedChar(
const uint16_t* begin,
const uint16_t* end,
uint32_t& ch) {
uint32_t first = begin[0];
if (likely(first < 0xD800)) {
ch = first;
return begin + 1;
} else if (first < 0xDC00) {
if (unlikely(begin + 2 > end)) {
ch = uint32_t('?');
return end;
}
ch = 0x10000
+ ((uint32_t(begin[0]) & 0x3FF) << 10)
+ ((uint32_t(begin[1]) & 0x3FF));
return begin + 2;
} else if (unlikely(first < 0xE000)) {
// Stray low surrogate
ch = uint32_t('?');
return begin + 1;
} else {
ch = first;
return begin + 1;
}
}
const uint32_t* decodeTypedChar(
const uint32_t* begin,
const uint32_t* end,
uint32_t& ch) {
ch = begin[0];
return begin + 1;
}
size_t encodeTypedChar(
uint8_t* begin,
uint8_t* end,
uint32_t ch) {
if (likely(ch < 0x80)) {
if (begin) {
if (unlikely(begin + 1 > end))
return 0;
begin[0] = uint8_t(ch);
}
return 1;
} else if (ch < 0x800) {
if (begin) {
if (unlikely(begin + 2 > end))
return 0;
begin[0] = uint8_t(0xC0 | (ch >> 6));
begin[1] = uint8_t(0x80 | (ch & 0x3F));
}
return 2;
} else if (ch < 0x10000) {
if (begin) {
if (unlikely(begin + 3 > end))
return 0;
begin[0] = uint8_t(0xE0 | ((ch >> 12)));
begin[1] = uint8_t(0x80 | ((ch >> 6) & 0x3F));
begin[2] = uint8_t(0x80 | ((ch >> 0) & 0x3F));
}
return 3;
} else if (ch < 0x200000) {
if (begin) {
if (unlikely(begin + 4 > end))
return 0;
begin[0] = uint8_t(0xF0 | ((ch >> 18)));
begin[1] = uint8_t(0x80 | ((ch >> 12) & 0x3F));
begin[2] = uint8_t(0x80 | ((ch >> 6) & 0x3F));
begin[3] = uint8_t(0x80 | ((ch >> 0) & 0x3F));
}
return 4;
} else {
// Invalid code point for UTF-8
return 0;
}
}
size_t encodeTypedChar(
uint16_t* begin,
uint16_t* end,
uint32_t ch) {
if (likely(ch < 0xD800)) {
if (begin) {
if (unlikely(begin + 1 > end))
return 0;
begin[0] = ch;
}
return 1;
} else if (ch < 0xE000) {
// Private use code points,
// we can't encode these
return 0;
} else if (ch < 0x10000) {
if (begin) {
if (unlikely(begin + 1 > end))
return 0;
begin[0] = ch;
}
return 1;
} else if (ch < 0x110000) {
if (begin) {
if (unlikely(begin + 2 > end))
return 0;
ch -= 0x10000;
begin[0] = uint16_t(0xD800 + (ch >> 10));
begin[1] = uint16_t(0xDC00 + (ch & 0x3FF));
}
return 2;
} else {
// Invalid code point
return 0;
}
}
size_t encodeTypedChar(
uint32_t* begin,
uint32_t* end,
uint32_t ch) {
if (begin) {
if (unlikely(begin + 1 > end))
return 0;
begin[0] = ch;
}
return 1;
}
std::string fromws(const wchar_t* ws) {
size_t srcLen = length(ws);
size_t dstLen = transcodeString<char>(
nullptr, 0, ws, srcLen);
std::string result;
result.resize(dstLen);
transcodeString(result.data(),
dstLen, ws, srcLen);
return result;
}
std::wstring tows(const char* mbs) {
size_t srcLen = length(mbs);
size_t dstLen = transcodeString<wchar_t>(
nullptr, 0, mbs, srcLen);
std::wstring result;
result.resize(dstLen);
transcodeString(result.data(),
dstLen, mbs, srcLen);
return result;
}
}
@@ -72,6 +72,8 @@ if (TARGET dxbc AND EXISTS ${BANNER_FRAMEGEN_LSFG_DIR}/lsfg_engine.cpp
${BANNER_FRAMEGEN_LSFG_DIR}/lsfg_pacer.cpp
${BANNER_FRAMEGEN_LSFG_DIR}/lsfg_engine.cpp
${BANNER_FRAMEGEN_LSFG_DIR}/lsfg_governor.cpp
# The Lossless.dll import (validate / extract / cache) for the app's LsfgNative.
${BANNER_FRAMEGEN_LSFG_DIR}/lsfg_jni.cpp
${BANNER_FRAMEGEN_WINFG_DIR}/framegen.cpp
${BANNER_FRAMEGEN_WINFG_DIR}/winfg_vkd.cpp
${BANNER_FRAMEGEN_WINFG_DIR}/winfg_engine.cpp
@@ -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 "Winlator_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,"Winlator_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
Loaded 100 of 114 files, more files were not shown because too many files have changed in this diff. Show more