Triforce v0.8.0

Evil HD (SMAA Ultra, 16x filtering, full textures, ambient occlusion with bounce light, sunlight shafts, foliage drawn 4x as far, the remastered texture pack's loader and the launcher's download of it), FSR 3.1 frame generation for every GPU, Intel XeSS, FSR 4 model choice, ReShade presets, Auto (this PC), HUD placement, the options menu in 13 languages, a second input for every control, hold to toggle, DualSense speaker and haptics, NFC from the player's own dumps, the above-60 fps game fixes, the Debug tab's flight and Inventory fixes, the launcher's full settings, and over 250 fixes.
This commit is contained in:
ruckus7382sfgsgdf
2026-11-25 10:21:32 -06:00
parent 861702476a
commit 4fa4f9188b
881 changed files with 179428 additions and 15581 deletions

152
gpu/vk/reshade/fx_builtin.h Normal file
View File

@@ -0,0 +1,152 @@
#pragma once
#include "fx_clarity.h"
#include <string>
namespace mkwvk::reshade {
// The numbers are reshade_preset's in triforce_options.ini, so a number once given never changes. The Clarity looks are
// the last block: a look appended to clarity_presets takes the next number and moves none
inline constexpr int user_preset = 3;
inline constexpr int hdr_preset = 4;
inline constexpr int depth_view_preset = 5;
inline constexpr int depth_haze_preset = 6;
inline constexpr int first_clarity_preset = 7;
inline constexpr int last_builtin_preset = first_clarity_preset + static_cast<int>(clarity_presets.size()) - 1;
static_assert(depth_haze_preset + 1 == first_clarity_preset, "the Clarity looks are the last block");
// Work in the same sRGB range as the SDR versions. Normalize by the pixel's peak
// before grading, then restore the peak in linear light so highlights can exceed 1.
inline constexpr const char* builtin_color_space = R"FX(
#if BUFFER_COLOR_SPACE == 2
float GradePeak(float3 rgb) { return max(1.0, max(rgb.r, max(rgb.g, rgb.b))); }
float3 GradeInput(float3 rgb)
{
float3 v = max(rgb, 0.0) / GradePeak(rgb);
return lerp(v * 12.92, 1.055 * pow(v, 1.0 / 2.4) - 0.055, step(0.0031308, v));
}
float3 GradeOutput(float3 graded, float3 original)
{
float3 decoded = lerp(graded / 12.92, pow((graded + 0.055) / 1.055, 2.4), step(0.04045, graded));
return decoded * GradePeak(original) + min(original, 0.0);
}
#else
float3 GradeInput(float3 rgb) { return rgb; }
float3 GradeOutput(float3 graded, float3 original) { return graded; }
#endif
)FX";
inline constexpr const char* color_fx = R"FX(
texture BackBuffer : COLOR;
sampler BackBufferSampler { Texture = BackBuffer; };
uniform float3 Tint = float3(1.0, 1.0, 1.0);
uniform float Saturation = 1.0;
void Fullscreen(uint id : SV_VertexID, out float4 position : SV_Position, out float2 uv : TEXCOORD0)
{
uv = float2((id << 1) & 2, id & 2);
position = float4(uv * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
}
float4 Grade(float4 position : SV_Position, float2 uv : TEXCOORD0) : SV_Target
{
float4 color = tex2D(BackBufferSampler, uv);
float3 rgb = GradeInput(color.rgb);
float luma = dot(rgb, float3(0.2126, 0.7152, 0.0722));
color.rgb = GradeOutput(saturate(lerp(luma.xxx, rgb, Saturation) * Tint), color.rgb);
return color;
}
technique TriforceColor
{
pass { VertexShader = Fullscreen; PixelShader = Grade; }
}
)FX";
inline constexpr const char* color_presets[] = {
"Techniques=TriforceColor@TriforceColor.fx\n[TriforceColor.fx]\nTint=1.04,1.0,0.94\nSaturation=1.12\n",
"Techniques=TriforceColor@TriforceColor.fx\n[TriforceColor.fx]\nTint=0.95,1.0,1.04\nSaturation=0.88\n",
};
inline constexpr const char* hdr_fx = R"FX(
texture Scene : COLOR;
sampler SceneSampler { Texture = Scene; };
void Fullscreen(uint id : SV_VertexID, out float4 position : SV_Position, out float2 uv : TEXCOORD0)
{
uv = float2((id << 1) & 2, id & 2);
position = float4(uv * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
}
float4 Grade(float4 position : SV_Position, float2 uv : TEXCOORD0) : SV_Target
{
float4 color = tex2D(SceneSampler, uv);
float luma = dot(color.rgb, float3(0.2126, 0.7152, 0.0722));
float lowest = min(color.r, min(color.g, color.b));
float amount = min(0.12, max(0.0, lowest) / max(luma - lowest, 0.00001));
color.rgb += (color.rgb - luma.xxx) * amount;
return color;
}
technique TriforceHDRVibrance
{
pass { VertexShader = Fullscreen; PixelShader = Grade; }
}
)FX";
inline constexpr const char* depth_fx = R"FX(
texture Scene : COLOR;
texture SceneDepth : DEPTH;
sampler SceneSampler { Texture = Scene; };
sampler DepthSampler { Texture = SceneDepth; MinFilter = POINT; MagFilter = POINT; };
void Fullscreen(uint id : SV_VertexID, out float4 position : SV_Position, out float2 uv : TEXCOORD0)
{
uv = float2((id << 1) & 2, id & 2);
position = float4(uv * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
}
float4 DepthView(float4 position : SV_Position, float2 uv : TEXCOORD0) : SV_Target
{
float d = tex2D(DepthSampler, uv).r;
float shade = pow(saturate(d), 20.0);
return float4(shade, shade, shade, 1.0);
}
float4 DepthHaze(float4 position : SV_Position, float2 uv : TEXCOORD0) : SV_Target
{
float d = tex2D(DepthSampler, uv).r;
float4 scene = tex2D(SceneSampler, uv);
float3 grade = lerp(GradeInput(scene.rgb), float3(0.57, 0.68, 0.77), 0.19 * smoothstep(0.985, 1.0, d));
scene.rgb = GradeOutput(grade, scene.rgb);
return scene;
}
technique TriforceDepthView { pass { VertexShader = Fullscreen; PixelShader = DepthView; } }
technique TriforceDepthHaze { pass { VertexShader = Fullscreen; PixelShader = DepthHaze; } }
)FX";
struct BuiltinPreset {
const char* shader;
std::string ini;
std::string source() const { return std::string(builtin_color_space) + shader; }
};
inline constexpr bool supports_color_space(bool native_hdr, bool linear_hdr, bool builtin) {
return native_hdr == linear_hdr || (native_hdr && builtin);
}
inline BuiltinPreset builtin_preset(int choice) {
if (choice == 1 || choice == 2) return {color_fx, color_presets[choice - 1]};
if (choice == hdr_preset) return {hdr_fx, "TriforceColorSpace=LinearHDR\nTechniques=TriforceHDRVibrance@TriforceHDR.fx\n"};
if (choice == depth_view_preset) return {depth_fx, "Techniques=TriforceDepthView@TriforceDepth.fx\n"};
if (choice == depth_haze_preset) return {depth_fx, "Techniques=TriforceDepthHaze@TriforceDepth.fx\n"};
if (choice < first_clarity_preset || choice > last_builtin_preset) return {nullptr, {}};
const auto& preset = clarity_presets[choice - first_clarity_preset];
return {clarity_fx, std::string("Techniques=TriforceClarity@TriforceClarity.fx\n[TriforceClarity.fx]\n") + preset.settings};
}
} // namespace mkwvk::reshade

105
gpu/vk/reshade/fx_clarity.h Normal file
View File

@@ -0,0 +1,105 @@
#pragma once
#include <array>
namespace mkwvk::reshade {
struct ClarityPreset {
const char* name;
const char* settings;
};
// SDR adaptations of the Clarity GFX presets by Jamie, Kiri, Crementif and the preset authors: their names and settings from
// the Cemu graphic packs (CC0 1.0, https://github.com/cemu-project/cemu_graphic_packs at 496df20311147cfd22523445eea71961115602ab,
// its LICENSE.md); the shader below is this game's own.
// The order is part of reshade_preset in triforce_options.ini (fx_builtin.h first_clarity_preset): append new entries.
inline constexpr auto clarity_presets = std::to_array<ClarityPreset>({
{"Serfrost (Recommended)", "Exposure=1.06\nLevels=5,232\nGamma=0.91,0.91,0.91\nGain=1.06,1.06,1.06\n"
"Vibrance=0.45\nSaturation=0.95\nFilm=0.22\nSharpness=0.45\n"},
{"Serfrost (Dim Display)", "Exposure=1.13\nLevels=1,235\nGamma=0.99,0.99,0.99\nGain=1.06,1.06,1.06\n"
"Vibrance=0.45\nSaturation=1.15\nFilm=0.15\n"},
{"Serfrost (Bright Display)", "Levels=2,300\nGamma=0.88,0.88,0.88\nGain=1.1,1.1,1.1\n"
"Vibrance=0.15\nSaturation=1.10\nFilm=0.15\n"},
{"Bruz", "Exposure=1.08\nGamma=0.9,0.9,0.9\nVibrance=0.15\nSaturation=1.12\nFilm=0.30\n"},
{"t3nka Neutral+", "Exposure=1.08\nVibrance=0.15\nFilm=0.06\n"},
{"Jamie", "Exposure=1.08\nLift=0.98,0.98,0.98\nVibrance=0.15\nSaturation=1.10\nFilm=0.14\nSharpness=0.35\n"},
{"Contrasty", "Exposure=1.10\nGamma=0.88,0.88,0.88\nContrast=0.65\nVibrance=0.15\n"},
{"The Complaining Gamer", "Gamma=0.78,0.78,0.78\nSaturation=0.72\nVibrance=0.15\nContrast=0.40\nSharpness=0.25\n"},
{"Filmic", "Exposure=1.08\nContrast=0.42\nSaturation=0.90\nVibrance=0.15\nFilm=0.36\nSharpness=0.35\n"},
{"CellHunter", "Gamma=1.00,0.965,1.01\nGain=1.00,0.975,1.01\nSaturation=0.80\n"
"Vibrance=0.335\nContrast=0.115\nFilm=0.17\n"},
{"ViskClarity", "Exposure=0.92\nLevels=2,285\nGamma=1.0,0.99,1.0\nGain=1.0,0.99,1.0\n"
"Vibrance=0.50\nContrast=-0.35\nFilm=0.22\n"},
{"Survival of the Wild (Warmer)", "Gamma=0.95,0.95,0.95\nGain=1.025,1.00,0.96\nVibrance=0.90\nSaturation=1.07\nSharpness=0.08\n"},
{"Survival of the Wild (Colder)", "Gamma=0.93,0.93,0.93\nGain=0.96,1.00,1.04\nVibrance=0.65\n"
"Contrast=0.10\nSaturation=1.10\nSharpness=0.05\n"},
{"Godot's Mexican", "Exposure=0.87\nLevels=5,205\nGain=1.04,1.00,0.96\nContrast=0.72\nFilm=0.22\nSharpness=0.23\n"},
{"Hexaae's Clear", "Levels=0,277\nGamma=0.96,0.96,0.96\nSaturation=0.994\n"},
{"MaranBr (Little More Intense)", "Exposure=1.10\nVibrance=0.20\nContrast=0.32\nFilm=0.12\n"},
{"YeMaoWuXin (Bright Colors)", "Gamma=0.95,0.95,0.95\nGain=1.075,1.075,1.125\nVibrance=0.90\n"},
{"YeMaoWuXin (Vivid and Clear)", "Gamma=0.97,0.96,0.98\nGain=1.05,1.00,1.05\nVibrance=0.80\n"},
{"Cobalt (Saturated & Bright)", "Exposure=1.10\nSaturation=1.17\nContrast=0.74\n"},
{"Clarity Base", "Saturation=1.18\n"},
});
inline constexpr const char* clarity_fx = R"FX(
texture BackBuffer : COLOR;
sampler BackBufferSampler { Texture = BackBuffer; };
uniform float Exposure = 1.0;
uniform float2 Levels = float2(0.0, 255.0);
uniform float3 Lift = float3(1.0, 1.0, 1.0);
uniform float3 Gamma = float3(1.0, 1.0, 1.0);
uniform float3 Gain = float3(1.0, 1.0, 1.0);
uniform float Contrast = 0.0;
uniform float Saturation = 1.0;
uniform float Vibrance = 0.0;
uniform float Film = 0.0;
uniform float Sharpness = 0.0;
void Fullscreen(uint id : SV_VertexID, out float4 position : SV_Position, out float2 uv : TEXCOORD0)
{
uv = float2((id << 1) & 2, id & 2);
position = float4(uv * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
}
float4 Grade(float4 position : SV_Position, float2 uv : TEXCOORD0) : SV_Target
{
const float3 weights = float3(0.2126, 0.7152, 0.0722);
float4 scene = tex2D(BackBufferSampler, uv);
float3 rgb = GradeInput(scene.rgb);
if (Sharpness > 0.0) {
const float2 texel = float2(BUFFER_RCP_WIDTH, BUFFER_RCP_HEIGHT);
float3 average = GradeInput(tex2D(BackBufferSampler, uv + float2(texel.x, 0.0)).rgb);
average += GradeInput(tex2D(BackBufferSampler, uv - float2(texel.x, 0.0)).rgb);
average += GradeInput(tex2D(BackBufferSampler, uv + float2(0.0, texel.y)).rgb);
average += GradeInput(tex2D(BackBufferSampler, uv - float2(0.0, texel.y)).rgb);
rgb += clamp(dot(rgb - 0.25 * average, weights) * Sharpness, -0.045, 0.045);
}
// Exposure in display space with a shoulder that keeps white at white.
rgb = saturate(rgb);
rgb = rgb * Exposure / (1.0 + (Exposure - 1.0) * rgb);
rgb = saturate((255.0 * rgb - Levels.x) / (Levels.y - Levels.x));
rgb = saturate((rgb + 0.5 * (Lift - 1.0) * (1.0 - rgb)) * Gain);
rgb = pow(rgb, 1.0 / Gamma);
float luma = dot(rgb, weights);
float centered = luma - 0.5;
float contrastLuma = 0.5 + centered / (0.5 + abs(centered));
rgb += (contrastLuma - luma) * Contrast;
float filmLuma = luma * luma * (3.0 - 2.0 * luma);
rgb += (filmLuma - luma) * Film;
luma = dot(rgb, weights);
float spread = max(rgb.r, max(rgb.g, rgb.b)) - min(rgb.r, min(rgb.g, rgb.b));
float3 grade = saturate(lerp(luma.xxx, rgb, Saturation * (1.0 + Vibrance * (1.0 - saturate(spread)))));
scene.rgb = GradeOutput(grade, scene.rgb);
return scene;
}
technique TriforceClarity
{
pass { VertexShader = Fullscreen; PixelShader = Grade; }
}
)FX";
} // namespace mkwvk::reshade

View File

@@ -0,0 +1,129 @@
#include "fx_compile.h"
#include "effect_codegen.hpp"
#include "effect_parser.hpp"
#include "effect_preprocessor.hpp"
#include "fx_files.h"
#include <cstring>
#include <list>
#include <memory>
#include <mutex>
#include <utility>
namespace tf::reshade {
namespace {
struct CachedEffect {
std::string source;
FxEffect effect;
std::string diagnostics;
};
struct EffectCache {
std::mutex mutex;
std::list<CachedEffect> entries;
};
constexpr size_t cache_entries = 8;
constexpr size_t max_cached_source = 256 * 1024;
static std::optional<FxEffect> compile(const std::filesystem::path& path, std::optional<std::string> source,
std::span<const std::filesystem::path> include_paths,
const FxCompileOptions& options, std::string& diagnostics,
const FxDefinitions& definitions) {
diagnostics.clear();
if (!options.width || !options.height) {
diagnostics = "ReShade FX requires a nonzero buffer size\n";
return std::nullopt;
}
const FxFileScope file_scope(include_paths);
reshadefx::preprocessor preprocessor;
preprocessor.add_macro_definition("__RESHADE__", std::to_string(options.reshade_version));
preprocessor.add_macro_definition("__RENDERER__", "0x20000"); // Vulkan
preprocessor.add_macro_definition("__RESHADE_PERMUTATION__", "0");
preprocessor.add_macro_definition("__RESHADE_PERFORMANCE_MODE__", "0");
preprocessor.add_macro_definition("BUFFER_WIDTH", std::to_string(options.width));
preprocessor.add_macro_definition("BUFFER_HEIGHT", std::to_string(options.height));
preprocessor.add_macro_definition("BUFFER_RCP_WIDTH", "(1.0 / BUFFER_WIDTH)");
preprocessor.add_macro_definition("BUFFER_RCP_HEIGHT", "(1.0 / BUFFER_HEIGHT)");
preprocessor.add_macro_definition("BUFFER_COLOR_SPACE", std::to_string(options.color_space));
preprocessor.add_macro_definition("BUFFER_COLOR_FORMAT", std::to_string(options.color_format));
preprocessor.add_macro_definition("BUFFER_COLOR_BIT_DEPTH", std::to_string(options.color_bit_depth));
for (const auto& [name, value] : definitions) {
if (!preprocessor.add_macro_definition(name, value)) {
diagnostics = "Cannot redefine ReShade FX macro " + name + '\n';
return std::nullopt;
}
}
// The game's depth runs from near 0 to far 1. A preset may still supply its own interpretation.
preprocessor.add_macro_definition("RESHADE_DEPTH_INPUT_IS_REVERSED", "0");
for (const auto& path : include_paths) preprocessor.add_include_path(path);
const bool preprocessed = source ? preprocessor.append_string(std::move(*source), path) : preprocessor.append_file(path);
diagnostics = preprocessor.errors();
if (!preprocessed) {
if (diagnostics.empty()) diagnostics = "Could not open the ReShade FX source\n";
return std::nullopt;
}
const auto& expanded = preprocessor.output();
static EffectCache cache;
if (expanded.size() <= max_cached_source) {
std::lock_guard lock(cache.mutex);
for (auto entry = cache.entries.begin(); entry != cache.entries.end(); ++entry) {
if (entry->source != expanded) continue;
diagnostics += entry->diagnostics;
cache.entries.splice(cache.entries.begin(), cache.entries, entry);
return cache.entries.front().effect;
}
}
std::unique_ptr<reshadefx::codegen> codegen(reshadefx::create_codegen_spirv(
/*vulkan_semantics=*/true, /*debug_info=*/false, /*uniforms_to_spec_constants=*/false));
reshadefx::parser parser;
const auto compile_diagnostics = diagnostics.size();
const bool parsed = parser.parse(expanded, codegen.get());
diagnostics += parser.errors();
if (!parsed) return std::nullopt;
FxEffect effect;
for (const auto& [name, stage] : codegen->module().entry_points) {
std::string binary, assembly, errors;
const bool assembled = codegen->assemble_code_for_entry_point(name, binary, assembly, errors);
diagnostics += errors;
if (!assembled || binary.empty() || binary.size() % sizeof(uint32_t) != 0) {
diagnostics += "Could not assemble SPIR-V for " + name + '\n';
return std::nullopt;
}
FxShader shader{name, stage, std::vector<uint32_t>(binary.size() / sizeof(uint32_t))};
std::memcpy(shader.spirv.data(), binary.data(), binary.size());
effect.shaders.push_back(std::move(shader));
}
effect.module = std::move(codegen->module());
if (expanded.size() <= max_cached_source) {
size_t shader_bytes = 0;
for (const auto& shader : effect.shaders) shader_bytes += shader.spirv.size() * sizeof(uint32_t);
if (shader_bytes <= max_cached_source) {
std::lock_guard lock(cache.mutex);
cache.entries.push_front({expanded, effect, diagnostics.substr(compile_diagnostics)});
if (cache.entries.size() > cache_entries) cache.entries.pop_back();
}
}
return effect;
}
} // namespace
std::optional<FxEffect> compile_fx(const std::filesystem::path& source,
std::span<const std::filesystem::path> include_paths,
const FxCompileOptions& options, std::string& diagnostics,
const FxDefinitions& definitions) {
return compile(source, std::nullopt, include_paths, options, diagnostics, definitions);
}
std::optional<FxEffect> compile_builtin_fx(std::string source, const FxCompileOptions& options,
std::string& diagnostics, const std::filesystem::path& name) {
return compile(name, std::move(source), {}, options, diagnostics, {});
}
} // namespace tf::reshade

View File

@@ -0,0 +1,42 @@
#pragma once
#include "effect_module.hpp"
#include <cstdint>
#include <filesystem>
#include <optional>
#include <span>
#include <string>
#include <utility>
#include <vector>
namespace tf::reshade {
struct FxCompileOptions {
unsigned width, height;
unsigned reshade_version;
unsigned color_space, color_format, color_bit_depth; // ReShade buffer macro values
};
struct FxShader {
std::string name;
reshadefx::shader_type stage;
std::vector<uint32_t> spirv;
};
struct FxEffect {
reshadefx::effect_module module;
std::vector<FxShader> shaders;
};
using FxDefinitions = std::vector<std::pair<std::string, std::string>>;
// The caller supplies the shader roots. Preprocessing confines source and includes to these roots.
std::optional<FxEffect> compile_fx(const std::filesystem::path& source,
std::span<const std::filesystem::path> include_paths,
const FxCompileOptions& options, std::string& diagnostics,
const FxDefinitions& definitions = {});
std::optional<FxEffect> compile_builtin_fx(std::string source, const FxCompileOptions& options,
std::string& diagnostics, const std::filesystem::path& name);
} // namespace tf::reshade

View File

@@ -0,0 +1,92 @@
#include "fx_files.h"
#include <algorithm>
#include <fstream>
namespace tf::reshade {
namespace fs = std::filesystem;
namespace {
thread_local std::span<const fs::path> asset_roots;
bool asset_name(const fs::path& name) {
if (name.empty() || name.has_root_path() || name.native().find(':') != fs::path::string_type::npos) return false;
return std::none_of(name.begin(), name.end(), [](const auto& part) { return part == ".."; });
}
bool within(const fs::path& root, const fs::path& file) {
auto [root_end, file_end] = std::mismatch(root.begin(), root.end(), file.begin(), file.end());
return root_end == root.end() && file_end != file.end();
}
fs::path canonical_file(const fs::path& file) {
std::error_code error;
const fs::path real = fs::canonical(file, error);
return !error && fs::is_regular_file(real, error) && !error ? real : fs::path{};
}
fs::path file_in_scope(const fs::path& file) {
if (asset_roots.empty()) return {};
const fs::path real = canonical_file(file);
for (const auto& root : asset_roots) {
std::error_code error;
const auto base = fs::canonical(root, error);
if (!error && !real.empty() && within(base, real)) return real;
}
return {};
}
} // namespace
FxFileScope::FxFileScope(std::span<const fs::path> roots) : previous_(asset_roots) { asset_roots = roots; }
FxFileScope::~FxFileScope() { asset_roots = previous_; }
fs::path fx_asset(const fs::path& root, const fs::path& name) {
if (!asset_name(name)) return {};
const FxFileScope scope(std::span(&root, 1));
return file_in_scope(root / name);
}
fs::path fx_shader(const fs::path& root, const fs::path& name, bool& ambiguous) {
ambiguous = false;
if (!asset_name(name) || name.extension() != ".fx") return {};
if (const auto direct = fx_asset(root, name); !direct.empty() || name.has_parent_path()) return direct;
fs::path found;
std::error_code error;
size_t entries = 0;
for (fs::recursive_directory_iterator it(root, fs::directory_options::skip_permission_denied, error), end;
!error && it != end && entries < 4096; it.increment(error), ++entries) {
if (it.depth() >= 7) it.disable_recursion_pending();
if (it->path().filename() != name) continue;
const auto candidate = fx_asset(root, it->path().lexically_relative(root));
if (candidate.empty() || candidate == found) continue;
if (!found.empty()) {
ambiguous = true;
return {};
}
found = candidate;
}
return found;
}
fs::path resolve_fx_include(const fs::path& source, const fs::path& name, std::span<const fs::path> includes) {
if (!asset_name(name)) return {};
if (auto path = file_in_scope(source.parent_path() / name); !path.empty()) return path;
for (const auto& dir : includes) {
if (auto path = file_in_scope(dir / name); !path.empty()) return path;
}
return {};
}
bool read_fx_file(const fs::path& path, std::string& data, size_t limit) {
const auto real = file_in_scope(path);
if (real.empty()) return false;
std::ifstream file(real, std::ios::binary | std::ios::ate);
const auto size = file.tellg();
if (size < 0 || static_cast<std::streamoff>(size) > static_cast<std::streamoff>(limit)) return false;
data.resize(static_cast<size_t>(size));
return static_cast<bool>(file.seekg(0).read(data.data(), size));
}
} // namespace tf::reshade

28
gpu/vk/reshade/fx_files.h Normal file
View File

@@ -0,0 +1,28 @@
#pragma once
#include <filesystem>
#include <span>
#include <string>
namespace tf::reshade {
// Canonicalize before opening any asset, including includes produced by a preprocessor macro.
std::filesystem::path fx_asset(const std::filesystem::path& root, const std::filesystem::path& name);
std::filesystem::path fx_shader(const std::filesystem::path& root, const std::filesystem::path& name,
bool& ambiguous);
std::filesystem::path resolve_fx_include(const std::filesystem::path& source, const std::filesystem::path& name,
std::span<const std::filesystem::path> includes);
bool read_fx_file(const std::filesystem::path& path, std::string& data, size_t limit = 2 * 1024 * 1024);
class FxFileScope {
public:
explicit FxFileScope(std::span<const std::filesystem::path> roots);
~FxFileScope();
FxFileScope(const FxFileScope&) = delete;
FxFileScope& operator=(const FxFileScope&) = delete;
private:
std::span<const std::filesystem::path> previous_;
};
} // namespace tf::reshade

View File

@@ -0,0 +1,77 @@
#include "fx_pipeline_cache.h"
#include "vk_paths.h"
#include <cstring>
#include <fstream>
#include <vector>
namespace mkwvk::reshade {
namespace {
constexpr size_t file_limit = 16 * 1024 * 1024;
bool matches_device(const std::vector<char>& bytes) {
VkPipelineCacheHeaderVersionOne header{};
if (bytes.size() < sizeof(header)) return false;
std::memcpy(&header, bytes.data(), sizeof(header));
return header.headerSize >= sizeof(header) && header.headerSize <= bytes.size() &&
header.headerVersion == VK_PIPELINE_CACHE_HEADER_VERSION_ONE && header.vendorID == dev.props.vendorID &&
header.deviceID == dev.props.deviceID && std::memcmp(header.pipelineCacheUUID, dev.props.pipelineCacheUUID, VK_UUID_SIZE) == 0;
}
} // namespace
std::filesystem::path game_dir() {
char exe[MAX_PATH];
const DWORD n = game_module_path(exe, MAX_PATH);
if (!n || n >= MAX_PATH) return {};
return std::filesystem::u8path(std::string(exe, n)).parent_path();
}
PipelineCache::PipelineCache() {
// ((Triforce) beside the game's own pipeline caches, triforce_pipelines.bin and the rest: the game folder)
const auto root = game_dir();
if (!root.empty()) path_ = root / "triforce_reshade_pipelines.bin";
std::vector<char> bytes;
if (!path_.empty()) {
std::ifstream file(path_, std::ios::binary | std::ios::ate);
const auto size = file.tellg();
if (size > 0 && static_cast<std::streamoff>(size) <= file_limit) {
bytes.resize(static_cast<size_t>(size));
if (!file.seekg(0).read(bytes.data(), bytes.size()) || !matches_device(bytes)) bytes.clear();
}
}
VkPipelineCacheCreateInfo ci{VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO};
ci.initialDataSize = bytes.size();
ci.pInitialData = bytes.data();
if (vkCreatePipelineCache(dev.device, &ci, nullptr, &cache_) != VK_SUCCESS && !bytes.empty()) {
ci.initialDataSize = 0;
ci.pInitialData = nullptr;
(void)vkCreatePipelineCache(dev.device, &ci, nullptr, &cache_);
}
}
PipelineCache::~PipelineCache() { if (cache_) vkDestroyPipelineCache(dev.device, cache_, nullptr); }
void PipelineCache::save() const {
if (!cache_ || path_.empty()) return;
size_t size = 0;
if (vkGetPipelineCacheData(dev.device, cache_, &size, nullptr) != VK_SUCCESS || !size || size > file_limit) return;
std::vector<char> bytes(size);
if (vkGetPipelineCacheData(dev.device, cache_, &size, bytes.data()) != VK_SUCCESS) return;
std::error_code error;
std::filesystem::create_directories(path_.parent_path(), error);
if (error) return;
auto temporary = path_;
temporary += "." + std::to_string(GetCurrentProcessId()) + ".tmp";
std::ofstream out(temporary, std::ios::binary | std::ios::trunc);
out.write(bytes.data(), size);
out.close();
if (out) {
std::filesystem::rename(temporary, path_, error);
if (!error) return;
}
std::filesystem::remove(temporary, error);
}
} // namespace mkwvk::reshade

View File

@@ -0,0 +1,28 @@
#pragma once
#include "vk_internal.h"
#include <filesystem>
namespace mkwvk::reshade {
/// (Triforce) The game folder (vk_paths.h game_module_path's rule: above bin when the exe is in bin beside image.bin), as a
/// path from its UTF-8 name: the process's code page is UTF-8 (the exe's manifest)
std::filesystem::path game_dir();
class PipelineCache {
public:
PipelineCache();
~PipelineCache();
PipelineCache(const PipelineCache&) = delete;
PipelineCache& operator=(const PipelineCache&) = delete;
VkPipelineCache handle() const { return cache_; }
void save() const;
private:
std::filesystem::path path_;
VkPipelineCache cache_{};
};
} // namespace mkwvk::reshade

View File

@@ -0,0 +1,130 @@
#include "fx_preset.h"
#include "fx_files.h"
#include <algorithm>
#include <ranges>
namespace tf::reshade {
namespace {
std::string_view trim(std::string_view text) {
constexpr std::string_view whitespace = " \r\t";
const auto first = text.find_first_not_of(whitespace);
if (first == text.npos) return {};
return text.substr(first, text.find_last_not_of(whitespace) - first + 1);
}
std::vector<std::string> split_list(std::string_view text) {
// ReShade INI escapes a literal comma by doubling it.
std::vector<std::string> list;
std::string part;
for (size_t i = 0; i < text.size(); ++i) {
if (text[i] != ',') {
part += text[i];
} else if (i + 1 < text.size() && text[i + 1] == ',') {
part += ',';
++i;
} else {
list.emplace_back(trim(part));
part.clear();
}
}
if (!part.empty()) list.emplace_back(trim(part));
return list;
}
FxTechnique technique(std::string_view text) {
const auto at = text.rfind('@');
if (at == text.npos) return {std::string(text), {}};
return {std::string(text.substr(0, at)), std::string(text.substr(at + 1))};
}
void add_definitions(FxDefinitions& out, std::string_view text) {
for (const auto& definition : split_list(text)) {
const auto equals = definition.find('=');
const auto key = trim(std::string_view(definition).substr(0, equals));
if (key.empty()) continue;
const auto value = (equals == definition.npos || equals + 1 == definition.size()) ? "1" : definition.substr(equals + 1);
auto previous = std::ranges::find(out, key, &FxDefinitions::value_type::first);
if (previous != out.end()) previous->second = value;
else out.emplace_back(key, value);
}
}
} // namespace
std::optional<FxPreset> parse_preset(std::string_view text, std::string& diagnostics) {
diagnostics.clear();
if (text.starts_with("\xef\xbb\xbf")) text.remove_prefix(3);
std::map<std::string, FxPresetSection, std::less<>> sections;
std::string name;
for (const auto line_chars : std::views::split(text, '\n')) {
const auto line = trim(std::string_view(line_chars.begin(), line_chars.end()));
if (line.empty() || line[0] == ';' || line[0] == '#') continue;
if (line.starts_with('[') && line.ends_with(']')) {
name = trim(line.substr(1, line.size() - 2));
continue;
}
const auto equals = line.find('=');
if (equals != line.npos) sections[name][std::string(trim(line.substr(0, equals)))] = trim(line.substr(equals + 1));
}
auto root = sections.extract("");
if (root.empty() || !root.mapped().contains("Techniques")) {
diagnostics = "The preset is missing Techniques";
return std::nullopt;
}
FxPreset preset;
const auto& color_space = root.mapped()["TriforceColorSpace"];
if (color_space == "LinearHDR") preset.linear_hdr = true;
else if (!color_space.empty() && color_space != "SDR") {
diagnostics = "Unsupported TriforceColorSpace (use SDR or LinearHDR)";
return std::nullopt;
}
add_definitions(preset.definitions, root.mapped()["PreprocessorDefinitions"]);
std::vector<FxTechnique> enabled;
for (const auto& id : split_list(root.mapped()["Techniques"])) {
auto item = technique(id);
if (item.name.empty() || (id.find('@') != id.npos && item.shader.empty())) {
diagnostics = "Invalid ReShade technique identifier: " + id;
return std::nullopt;
}
enabled.push_back(std::move(item));
}
for (const auto& id : split_list(root.mapped()["TechniqueSorting"])) {
const auto order = technique(id);
auto it = std::ranges::find(enabled, order);
if (it == enabled.end() && order.shader.empty() && std::ranges::count(enabled, order.name, &FxTechnique::name) == 1)
it = std::ranges::find(enabled, order.name, &FxTechnique::name);
if (it != enabled.end()) {
preset.techniques.push_back(std::move(*it));
enabled.erase(it);
}
}
for (auto& item : enabled) preset.techniques.push_back(std::move(item));
preset.shader_options = std::move(sections);
return preset;
}
std::optional<FxPreset> load_preset(const std::filesystem::path& root, const std::filesystem::path& name,
std::string& diagnostics) {
const FxFileScope scope(std::span(&root, 1));
const auto file = fx_asset(root, name);
std::string text;
if (file.empty() || !read_fx_file(file, text)) {
diagnostics = "Could not read the ReShade preset";
return std::nullopt;
}
return parse_preset(text, diagnostics);
}
FxDefinitions preset_definitions(const FxPreset& preset, std::string_view shader) {
FxDefinitions result = preset.definitions;
if (auto section = preset.shader_options.find(shader); section != preset.shader_options.end()) {
if (auto item = section->second.find("PreprocessorDefinitions"); item != section->second.end())
add_definitions(result, item->second);
}
return result;
}
} // namespace tf::reshade

View File

@@ -0,0 +1,28 @@
#pragma once
#include "fx_compile.h"
#include <map>
#include <string_view>
namespace tf::reshade {
struct FxTechnique {
std::string name, shader;
bool operator==(const FxTechnique&) const = default;
};
using FxPresetSection = std::map<std::string, std::string, std::less<>>;
struct FxPreset {
bool linear_hdr = false;
std::vector<FxTechnique> techniques;
FxDefinitions definitions;
std::map<std::string, FxPresetSection, std::less<>> shader_options;
};
std::optional<FxPreset> parse_preset(std::string_view text, std::string& diagnostics);
std::optional<FxPreset> load_preset(const std::filesystem::path& root, const std::filesystem::path& name,
std::string& diagnostics);
FxDefinitions preset_definitions(const FxPreset& preset, std::string_view shader);
} // namespace tf::reshade

View File

@@ -0,0 +1,893 @@
#include "fx_runtime.h"
#include "fx_builtin.h"
#include "fx_files.h"
#include "fx_pipeline_cache.h"
#include "fx_preset.h"
#include "fx_texture.h"
#include "vk_shader.h" // (extern_guarded_shader_module, extern_guarded_graphics)
#include "vk_gprof.h"
#include "vk_temporal.h"
#include "vk_texture.h"
#include <algorithm>
#include <array>
#include <charconv>
#include <cmath>
#include <cstring>
#include <fstream>
#include <future>
#include <memory>
#include <mutex>
#include "vk_work.h" // Order the effect with the game's recorder and barrier deduplication.
namespace mkwvk::reshade {
std::atomic<bool> enabled{false};
std::atomic<bool> needs_tick{false};
std::atomic<bool> depth_requested{false};
static std::atomic<bool> preview_bypassed{false};
namespace {
namespace fs = std::filesystem;
using namespace tf::reshade;
constexpr VkFormat sdr_format = VK_FORMAT_R8G8B8A8_SRGB;
constexpr VkFormat code_format = VK_FORMAT_R8G8B8A8_UNORM;
constexpr VkFormat hdr_format = VK_FORMAT_R16G16B16A16_SFLOAT;
constexpr VkShaderStageFlags graphics = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
constexpr VkImageSubresourceLayers color_layers{VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
struct Scene {
uint32_t width, height;
VkFormat format;
bool hdr;
bool operator==(const Scene&) const = default;
};
struct Image {
VkImage image{};
VmaAllocation allocation{};
VkImageView code{}, srgb{};
VkFormat code_view_format = code_format;
uint32_t width = 0, height = 0;
~Image() {
if (code) vkDestroyImageView(dev.device, code, nullptr);
if (srgb) vkDestroyImageView(dev.device, srgb, nullptr);
if (image) destroy_image(image, allocation);
}
Image() = default;
Image(const Image&) = delete;
Image& operator=(const Image&) = delete;
bool make(uint32_t width, uint32_t height, VkImageUsageFlags usage, bool fp16 = false) {
VkImageCreateInfo ci{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
if (!fp16) ci.flags = VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
ci.imageType = VK_IMAGE_TYPE_2D;
ci.format = fp16 ? hdr_format : sdr_format;
ci.extent = {width, height, 1};
this->width = width;
this->height = height;
ci.mipLevels = ci.arrayLayers = 1;
ci.samples = VK_SAMPLE_COUNT_1_BIT;
ci.tiling = VK_IMAGE_TILING_OPTIMAL;
ci.usage = usage;
if (!create_image(ci, image, allocation)) return false;
VkImageViewCreateInfo vi{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO};
vi.image = image;
vi.viewType = VK_IMAGE_VIEW_TYPE_2D;
vi.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
code_view_format = vi.format = fp16 ? hdr_format : code_format;
if (vkCreateImageView(dev.device, &vi, nullptr, &code) != VK_SUCCESS) return false;
if (fp16) return true;
vi.format = sdr_format;
return vkCreateImageView(dev.device, &vi, nullptr, &srgb) == VK_SUCCESS;
}
};
struct Buffer {
VkBuffer buffer{};
VmaAllocation allocation{};
~Buffer() { if (buffer) destroy_buffer(buffer, allocation); }
Buffer() = default;
Buffer(const Buffer&) = delete;
Buffer& operator=(const Buffer&) = delete;
bool make(std::span<const uint8_t> bytes, VkBufferUsageFlags usage) {
void* map = nullptr;
if (!create_buffer_cached(bytes.size(), usage, buffer, allocation, &map) || !map) return false;
std::memcpy(map, bytes.data(), bytes.size());
flush_mapped(allocation, bytes.size());
return true;
}
};
struct Binding {
uint32_t binding;
VkSampler sampler;
bool srgb;
bool depth;
const Image* image; // null for this pass's COLOR input
};
struct Pass {
std::vector<Binding> bindings;
VkDescriptorSetLayout samples{};
VkPipelineLayout layout{};
VkPipeline pipeline{};
VkDescriptorSet uniforms{};
bool srgb_write = false;
bool copy_scene = false;
VkAttachmentLoadOp load = VK_ATTACHMENT_LOAD_OP_CLEAR;
Image* target = nullptr; // null for the ping-pong scene buffer
~Pass() {
if (pipeline) vkDestroyPipeline(dev.device, pipeline, nullptr);
if (layout) vkDestroyPipelineLayout(dev.device, layout, nullptr);
if (samples) vkDestroyDescriptorSetLayout(dev.device, samples, nullptr);
for (const auto& b : bindings) vkDestroySampler(dev.device, b.sampler, nullptr);
}
};
struct Resource {
Image image;
std::unique_ptr<Buffer> upload;
bool ready = false; // a file or a target written by an earlier pass of this effect
};
struct Program {
Scene scene{};
Image scene_ping[2];
bool initialized = false;
bool depth = false;
uint64_t uploaded_at = 0;
size_t resource_bytes = 0;
VkDescriptorSetLayout uniforms{};
VkDescriptorPool pool{};
std::vector<std::unique_ptr<Buffer>> buffers;
std::vector<std::unique_ptr<Resource>> resources;
std::vector<std::unique_ptr<Pass>> passes;
~Program() {
passes.clear();
if (pool) vkDestroyDescriptorPool(dev.device, pool, nullptr);
if (uniforms) vkDestroyDescriptorSetLayout(dev.device, uniforms, nullptr);
}
};
struct Request {
int preset = 0;
std::string name;
uint64_t revision = 0;
};
struct Prepared {
uint64_t revision;
Scene scene;
std::unique_ptr<Program> program;
std::string message;
};
struct State {
std::mutex mutex;
Request request;
std::string message;
std::future<Prepared> work;
std::optional<std::pair<uint64_t, Scene>> attempted;
std::unique_ptr<Program> active;
uint64_t active_revision = 0;
std::optional<uint64_t> preview_start;
bool show_requested_only = false;
std::vector<std::pair<uint64_t, std::unique_ptr<Program>>> retired;
};
State& state() {
// As for the renderer's other long-lived Vulkan state, the process owns the device and this state.
static auto* const instance = new State;
return *instance;
}
// (Triforce) the game folder's reshade folder, beside triforce_options.ini: presets/, shaders/, textures/, selected.txt
fs::path user_root() { return game_dir() / "reshade"; }
std::string utf8(const fs::path& path) {
const auto text = path.u8string();
return {reinterpret_cast<const char*>(text.data()), text.size()};
}
std::string read_selection() {
const auto root = user_root();
const auto path = fx_asset(root, "selected.txt");
if (path.empty()) return {};
const FxFileScope scope(std::span(&root, 1));
std::string name;
if (!read_fx_file(path, name) || name.size() > 255 || name.find_first_of("/\\\r\n:") != name.npos) return {};
return name;
}
bool blend_or_mask(const reshadefx::pass& pass) {
return pass.blend_enable[0] || pass.render_target_write_mask[0] != 0xf;
}
bool graphics_pass(const reshadefx::pass& pass, const Scene& scene) {
const bool scene_target = pass.render_target_names[0].empty();
return pass.cs_entry_point.empty() && !pass.vs_entry_point.empty() && !pass.ps_entry_point.empty() &&
pass.num_vertices == 3 && pass.topology == reshadefx::primitive_topology::triangle_list &&
pass.storage_bindings.empty() && !pass.stencil_enable &&
std::all_of(std::begin(pass.render_target_names) + 1, std::end(pass.render_target_names),
[](const auto& name) { return name.empty(); }) &&
(!scene_target || ((!pass.viewport_width || pass.viewport_width == scene.width) &&
(!pass.viewport_height || pass.viewport_height == scene.height)));
}
VkBlendFactor blend_factor(reshadefx::blend_factor factor) {
constexpr VkBlendFactor factors[] = {
VK_BLEND_FACTOR_ZERO, VK_BLEND_FACTOR_ONE,
VK_BLEND_FACTOR_SRC_COLOR, VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR,
VK_BLEND_FACTOR_DST_COLOR, VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR,
VK_BLEND_FACTOR_SRC_ALPHA, VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
VK_BLEND_FACTOR_DST_ALPHA, VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA,
};
const auto index = static_cast<unsigned>(factor);
return index < std::size(factors) ? factors[index] : VK_BLEND_FACTOR_MAX_ENUM;
}
VkBlendOp blend_op(reshadefx::blend_op op) {
constexpr VkBlendOp ops[] = {VK_BLEND_OP_ADD, VK_BLEND_OP_SUBTRACT, VK_BLEND_OP_REVERSE_SUBTRACT,
VK_BLEND_OP_MIN, VK_BLEND_OP_MAX};
const auto index = static_cast<unsigned>(op) - 1;
return index < std::size(ops) ? ops[index] : VK_BLEND_OP_MAX_ENUM;
}
template<class T>
bool number(std::string_view value, uint8_t* to) {
T x{};
while (value.starts_with(' ') || value.starts_with('\t')) value.remove_prefix(1);
while (value.ends_with(' ') || value.ends_with('\t')) value.remove_suffix(1);
if (value.empty()) return false;
const auto [end, error] = std::from_chars(value.data(), value.data() + value.size(), x);
if (error != std::errc{} || end != value.data() + value.size()) return false;
if constexpr (std::is_same_v<T, float>) if (!std::isfinite(x)) return false;
std::memcpy(to, &x, sizeof(x));
return true;
}
bool uniform_bytes(const reshadefx::uniform& u, const FxPresetSection* values, std::span<uint8_t> bytes) {
const auto count = u.type.rows;
if (!u.type.is_numeric() || u.type.is_array() || u.type.is_matrix() || count < 1 || count > 4 ||
u.offset > bytes.size() || count * 4 > bytes.size() - u.offset ||
std::ranges::any_of(u.annotations, [](const auto& a) { return a.name == "source"; })) return false;
auto* dst = bytes.data() + u.offset;
if (u.has_initializer_value) std::memcpy(dst, u.initializer_value.as_uint, count * 4);
if (!values) return true;
const auto preset = values->find(u.name);
if (preset == values->end()) return true;
std::string_view value = preset->second;
for (size_t i = 0; i < count; ++i) {
const auto comma = value.find(',');
if ((i + 1 == count) != (comma == value.npos)) return false;
const auto item = value.substr(0, comma);
const bool written = u.type.is_floating_point() ? number<float>(item, dst) :
u.type.is_signed() ? number<int32_t>(item, dst) : number<uint32_t>(item, dst);
if (!written) return false;
dst += 4;
if (comma != value.npos) value.remove_prefix(comma + 1);
}
return true;
}
bool make_uniforms(Program& p, const FxEffect& effect, const FxPresetSection* values, VkDescriptorSet& set,
std::string& error) {
if (!effect.module.total_uniform_size) return true;
if (effect.module.total_uniform_size > 16384) return false;
std::vector<uint8_t> bytes(effect.module.total_uniform_size);
for (const auto& u : effect.module.uniforms) {
if (!uniform_bytes(u, values, bytes)) {
error = "Unsupported or invalid uniform " + u.name;
return false;
}
}
auto buffer = std::make_unique<Buffer>();
if (!buffer->make(bytes, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) return false;
VkDescriptorSetAllocateInfo ai{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO};
ai.descriptorPool = p.pool;
ai.descriptorSetCount = 1;
ai.pSetLayouts = &p.uniforms;
if (vkAllocateDescriptorSets(dev.device, &ai, &set) != VK_SUCCESS) return false;
const VkDescriptorBufferInfo info{buffer->buffer, 0, bytes.size()};
VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
write.dstSet = set;
write.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
write.descriptorCount = 1;
write.pBufferInfo = &info;
vkUpdateDescriptorSets(dev.device, 1, &write, 0, nullptr);
p.buffers.push_back(std::move(buffer));
return true;
}
VkSamplerAddressMode address(reshadefx::texture_address_mode mode) {
switch (mode) {
case reshadefx::texture_address_mode::wrap: return VK_SAMPLER_ADDRESS_MODE_REPEAT;
case reshadefx::texture_address_mode::mirror: return VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT;
case reshadefx::texture_address_mode::border: return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
default: return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
}
}
using Resources = std::map<std::string, Resource*, std::less<>>;
Resource* make_resource(Program& p, Resources& resources, const reshadefx::texture& tex,
const fs::path& root, std::string& error) {
if (auto it = resources.find(tex.unique_name); it != resources.end()) return it->second;
const bool fp16 = tex.format == reshadefx::texture_format::rgba16f;
if (!tex.semantic.empty() || tex.type != reshadefx::texture_type::texture_2d || tex.levels != 1 || tex.storage_access ||
(!fp16 && tex.format != reshadefx::texture_format::rgba8) ||
!tex.width || !tex.height || tex.width > 4096 || tex.height > 4096) {
error = "Unsupported texture format or dimensions for " + tex.name + " (requires 2D RGBA8 or RGBA16F)";
return nullptr;
}
const auto source = std::ranges::find(tex.annotations, "source", &reshadefx::annotation::name);
const auto bytes = size_t(tex.width) * tex.height * (fp16 ? 8 : 4);
if (p.resources.size() >= 24 || p.resource_bytes + bytes > 128 * 1024 * 1024) {
error = "ReShade resources exceed the preset limit";
return nullptr;
}
auto res = std::make_unique<Resource>();
VkImageUsageFlags usage = VK_IMAGE_USAGE_SAMPLED_BIT;
if (source != tex.annotations.end()) {
if (tex.render_target || fp16) {
error = "PNG sources require a read-only RGBA8 texture";
return nullptr;
}
const auto pixels = load_png(root, fs::u8path(source->value.string_data), error);
if (!pixels) return nullptr;
if (pixels->width != tex.width || pixels->height != tex.height) {
error = "Texture dimensions do not match " + tex.name;
return nullptr;
}
res->upload = std::make_unique<Buffer>();
if (!res->upload->make(pixels->rgba, VK_BUFFER_USAGE_TRANSFER_SRC_BIT)) return nullptr;
res->ready = true;
usage |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
} else if (tex.render_target) {
usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
} else {
error = "Texture " + tex.name + " requires a PNG source or a render pass";
return nullptr;
}
if (fp16) {
VkFormatProperties properties{};
vkGetPhysicalDeviceFormatProperties(dev.physical, hdr_format, &properties);
constexpr auto required = VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT;
if ((properties.optimalTilingFeatures & required) != required) return nullptr;
}
if (!res->image.make(tex.width, tex.height, usage, fp16)) return nullptr;
auto* resource = res.get();
p.resource_bytes += bytes;
p.resources.push_back(std::move(res));
resources.emplace(tex.unique_name, resource);
return resource;
}
bool make_bindings(Program& program, Pass& out, const FxEffect& effect, const reshadefx::pass& in,
Resources& resources, const fs::path& root, std::string& error) {
if (in.sampler_bindings.size() > dev.max_push_descriptors || in.sampler_bindings.size() > 16) return false;
std::vector<VkDescriptorSetLayoutBinding> bindings;
for (const auto& binding : in.sampler_bindings) {
const auto& s = effect.module.samplers.at(binding.index);
const auto tex = std::ranges::find(effect.module.textures, s.texture_name, &reshadefx::texture::unique_name);
if (tex == effect.module.textures.end() || (!tex->semantic.empty() && tex->semantic != "COLOR" && tex->semantic != "DEPTH")) {
error = "Unsupported texture input " + (tex == effect.module.textures.end() ? s.texture_name : tex->semantic);
return false;
}
const bool depth = tex->semantic == "DEPTH";
if (depth && s.srgb) {
error = "Depth is a linear scalar texture";
return false;
}
program.depth |= depth;
if (program.scene.hdr && tex->semantic == "COLOR" && s.srgb) {
error = "Linear HDR COLOR cannot be sampled as sRGB";
return false;
}
const Image* image = nullptr;
if (tex->semantic.empty()) {
auto* resource = make_resource(program, resources, *tex, root, error);
if (!resource) return false;
image = &resource->image;
if (s.srgb && !image->srgb) {
error = "RGBA16F texture " + tex->name + " cannot be sampled as sRGB";
return false;
}
if (!resource->ready || image == out.target) {
error = "Pass reads " + tex->name + " before it has a separate output to sample";
return false;
}
}
if (s.filter == reshadefx::filter_mode::anisotropic) return false;
const auto filter = static_cast<unsigned>(s.filter);
VkSamplerCreateInfo si{VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO};
si.minFilter = (!depth && (filter & 0x10)) ? VK_FILTER_LINEAR : VK_FILTER_NEAREST;
si.magFilter = (!depth && (filter & 0x4)) ? VK_FILTER_LINEAR : VK_FILTER_NEAREST;
si.mipmapMode = (!depth && (filter & 1)) ? VK_SAMPLER_MIPMAP_MODE_LINEAR : VK_SAMPLER_MIPMAP_MODE_NEAREST;
si.addressModeU = address(s.address_u);
si.addressModeV = address(s.address_v);
si.addressModeW = address(s.address_w);
VkSampler sampler{};
if (vkCreateSampler(dev.device, &si, nullptr, &sampler) != VK_SUCCESS) return false;
out.bindings.push_back({binding.entry_point_binding, sampler, s.srgb, depth, image});
bindings.push_back({binding.entry_point_binding, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, graphics, nullptr});
}
VkDescriptorSetLayoutCreateInfo li{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
li.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR;
li.bindingCount = static_cast<uint32_t>(bindings.size());
li.pBindings = bindings.data();
return vkCreateDescriptorSetLayout(dev.device, &li, nullptr, &out.samples) == VK_SUCCESS;
}
bool make_pipeline(Program& p, Pass& pass, const FxEffect& effect, const reshadefx::pass& in,
VkPipelineCache cache, unsigned& cache_hits) {
const VkDescriptorSetLayout sets[] = {p.uniforms, pass.samples};
VkPipelineLayoutCreateInfo li{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO};
li.setLayoutCount = 2;
li.pSetLayouts = sets;
if (vkCreatePipelineLayout(dev.device, &li, nullptr, &pass.layout) != VK_SUCCESS) return false;
struct Modules {
VkShaderModule modules[2]{};
~Modules() { for (auto module : modules) if (module) vkDestroyShaderModule(dev.device, module, nullptr); }
} modules;
const std::string* names[] = {&in.vs_entry_point, &in.ps_entry_point};
VkPipelineShaderStageCreateInfo stages[2]{};
for (int i = 0; i < 2; i++) {
const auto s = std::ranges::find(effect.shaders, *names[i], &FxShader::name);
if (s == effect.shaders.end()) return false;
VkShaderModuleCreateInfo sm{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO};
sm.codeSize = s->spirv.size() * sizeof(uint32_t);
sm.pCode = s->spirv.data();
if (extern_guarded_shader_module(&sm, &modules.modules[i], 0) != VK_SUCCESS) return false;
stages[i] = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO};
stages[i].stage = i ? VK_SHADER_STAGE_FRAGMENT_BIT : VK_SHADER_STAGE_VERTEX_BIT;
stages[i].module = modules.modules[i];
stages[i].pName = s->name.c_str();
}
VkPipelineVertexInputStateCreateInfo vi{VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO};
VkPipelineInputAssemblyStateCreateInfo ia{VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO};
ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
VkPipelineViewportStateCreateInfo vp{VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO};
vp.viewportCount = vp.scissorCount = 1;
VkPipelineRasterizationStateCreateInfo rs{VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO};
rs.polygonMode = VK_POLYGON_MODE_FILL;
rs.lineWidth = 1.0f;
VkPipelineMultisampleStateCreateInfo ms{VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO};
ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
VkPipelineColorBlendAttachmentState blend{};
blend.colorWriteMask = in.render_target_write_mask[0] & 0xf;
blend.blendEnable = in.blend_enable[0];
blend.srcColorBlendFactor = blend_factor(in.source_color_blend_factor[0]);
blend.dstColorBlendFactor = blend_factor(in.dest_color_blend_factor[0]);
blend.colorBlendOp = blend_op(in.color_blend_op[0]);
blend.srcAlphaBlendFactor = blend_factor(in.source_alpha_blend_factor[0]);
blend.dstAlphaBlendFactor = blend_factor(in.dest_alpha_blend_factor[0]);
blend.alphaBlendOp = blend_op(in.alpha_blend_op[0]);
if (blend.srcColorBlendFactor == VK_BLEND_FACTOR_MAX_ENUM || blend.dstColorBlendFactor == VK_BLEND_FACTOR_MAX_ENUM ||
blend.srcAlphaBlendFactor == VK_BLEND_FACTOR_MAX_ENUM || blend.dstAlphaBlendFactor == VK_BLEND_FACTOR_MAX_ENUM ||
blend.colorBlendOp == VK_BLEND_OP_MAX_ENUM || blend.alphaBlendOp == VK_BLEND_OP_MAX_ENUM) return false;
VkPipelineColorBlendStateCreateInfo cb{VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO};
cb.attachmentCount = 1;
cb.pAttachments = &blend;
const VkDynamicState dynamics[] = {VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR};
VkPipelineDynamicStateCreateInfo ds{VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO};
ds.dynamicStateCount = 2;
ds.pDynamicStates = dynamics;
const auto& output = pass.target ? *pass.target : p.scene_ping[0];
const VkFormat format = pass.srgb_write ? sdr_format : output.code_view_format;
if (blend.blendEnable) {
VkFormatProperties properties{};
vkGetPhysicalDeviceFormatProperties(dev.physical, format, &properties);
if (!(properties.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT)) return false;
}
VkPipelineRenderingCreateInfo ri{VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO};
ri.colorAttachmentCount = 1;
ri.pColorAttachmentFormats = &format;
VkGraphicsPipelineCreateInfo gi{VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO};
VkPipelineCreationFeedback feedback{};
VkPipelineCreationFeedbackCreateInfo feedback_info{VK_STRUCTURE_TYPE_PIPELINE_CREATION_FEEDBACK_CREATE_INFO};
feedback_info.pNext = &ri;
feedback_info.pPipelineCreationFeedback = &feedback;
gi.pNext = &feedback_info;
gi.stageCount = 2;
gi.pStages = stages;
gi.pVertexInputState = &vi;
gi.pInputAssemblyState = &ia;
gi.pViewportState = &vp;
gi.pRasterizationState = &rs;
gi.pMultisampleState = &ms;
gi.pColorBlendState = &cb;
gi.pDynamicState = &ds;
gi.layout = pass.layout;
const bool made = extern_guarded_graphics(cache, &gi, &pass.pipeline) == VK_SUCCESS;
if (made && (feedback.flags & VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT)) ++cache_hits;
return made;
}
bool make_targets(Program& p, size_t effects) {
VkFormatProperties properties{};
vkGetPhysicalDeviceFormatProperties(dev.physical, p.scene.format, &properties);
constexpr auto blit = VK_FORMAT_FEATURE_BLIT_SRC_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT;
if ((properties.optimalTilingFeatures & blit) != blit || !dev.push_descriptor) return false;
vkGetPhysicalDeviceFormatProperties(dev.physical, p.scene.hdr ? hdr_format : sdr_format, &properties);
constexpr auto color = VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT;
if ((properties.optimalTilingFeatures & (blit | color)) != (blit | color)) return false;
vkGetPhysicalDeviceFormatProperties(dev.physical, p.scene.hdr ? hdr_format : code_format, &properties);
if ((properties.optimalTilingFeatures & color) != color) return false;
for (auto& image : p.scene_ping) {
if (!image.make(p.scene.width, p.scene.height, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT |
VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT,
p.scene.hdr)) return false;
}
VkDescriptorSetLayoutBinding binding{0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, graphics, nullptr};
VkDescriptorSetLayoutCreateInfo li{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
li.bindingCount = 1;
li.pBindings = &binding;
if (vkCreateDescriptorSetLayout(dev.device, &li, nullptr, &p.uniforms) != VK_SUCCESS) return false;
const VkDescriptorPoolSize count{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, static_cast<uint32_t>(effects)};
VkDescriptorPoolCreateInfo pi{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO};
pi.maxSets = count.descriptorCount;
pi.poolSizeCount = 1;
pi.pPoolSizes = &count;
return vkCreateDescriptorPool(dev.device, &pi, nullptr, &p.pool) == VK_SUCCESS;
}
Prepared prepare(Request request, Scene scene) {
const auto start = std::chrono::steady_clock::now();
Prepared result{request.revision, scene, {}, {}};
try {
const auto included = builtin_preset(request.preset);
const bool builtin = included.shader != nullptr;
const auto root = builtin ? fs::path{} : user_root();
const auto shaders = root / "shaders";
const auto preset = builtin ? parse_preset(included.ini, result.message) :
load_preset(root / "presets", fs::u8path(request.name), result.message);
if (!preset) return result;
if (!supports_color_space(scene.hdr, preset->linear_hdr, builtin)) {
result.message = scene.hdr ? "Use Expanded HDR for imported SDR presets." :
"This preset requires active Native HDR";
return result;
}
if (preset->techniques.empty() || preset->techniques.size() > 32) {
result.message = "Select a preset with 1 to 32 enabled techniques";
return result;
}
auto program = std::make_unique<Program>();
program->scene = scene;
if (!make_targets(*program, preset->techniques.size())) {
result.message = "Cannot make the ReShade targets on this device";
return result;
}
const FxCompileOptions options{scene.width, scene.height, 60000, scene.hdr ? 2u : 1u,
scene.hdr ? 10u : 28u, scene.hdr ? 16u : 8u};
struct Compiled { FxEffect effect; VkDescriptorSet uniforms; Resources resources; };
std::map<std::string, Compiled, std::less<>> compiled;
const PipelineCache cache;
unsigned cache_hits = 0;
for (auto id : preset->techniques) {
const bool unnamed_file = id.shader.empty();
if (unnamed_file) id.shader = id.name + ".fx";
auto it = compiled.find(id.shader);
if (it == compiled.end()) {
bool ambiguous = false;
const auto path = builtin ? fs::path{} : fx_shader(shaders, fs::u8path(id.shader), ambiguous);
if (!builtin && path.empty()) {
if (ambiguous) result.message = "More than one " + id.shader + "; give the shader's relative path";
else if (unnamed_file) result.message = "Name the shader explicitly as " + id.name + "@shader.fx";
else result.message = "Missing shader " + id.shader + " in reshade/shaders";
return result;
}
const auto filename = builtin ? id.shader : utf8(path.filename());
auto effect = builtin ? compile_builtin_fx(included.source(), options, result.message, fs::u8path(id.shader)) :
compile_fx(path, std::span(&shaders, 1), options, result.message,
preset_definitions(*preset, filename));
if (!effect) return result;
VkDescriptorSet uniforms{};
const auto settings = preset->shader_options.find(filename);
result.message = "Cannot prepare uniforms for " + id.shader;
if (!make_uniforms(*program, *effect, settings == preset->shader_options.end() ? nullptr : &settings->second,
uniforms, result.message)) return result;
it = compiled.emplace(id.shader, Compiled{std::move(*effect), uniforms, {}}).first;
}
auto& info = it->second;
const auto tech = std::ranges::find(info.effect.module.techniques, id.name, &reshadefx::technique::name);
if (tech == info.effect.module.techniques.end() || tech->passes.empty()) {
result.message = "Missing technique " + id.name + " in " + id.shader;
return result;
}
for (const auto& source : tech->passes) {
if (!graphics_pass(source, scene) || program->passes.size() >= 64) {
result.message = "Unsupported pass in " + id.name + " (requires a fullscreen graphics pass)";
return result;
}
if (scene.hdr && source.render_target_names[0].empty() && source.srgb_write_enable) {
result.message = "Linear HDR output cannot use SRGBWriteEnable";
return result;
}
auto pass = std::make_unique<Pass>();
pass->srgb_write = source.srgb_write_enable;
pass->uniforms = info.uniforms;
result.message = "Cannot create Vulkan pipeline for " + id.name;
Resource* target = nullptr;
if (!source.render_target_names[0].empty()) {
const auto tex = std::ranges::find(info.effect.module.textures, source.render_target_names[0],
&reshadefx::texture::unique_name);
if (tex == info.effect.module.textures.end()) return result;
target = make_resource(*program, info.resources, *tex, root / "textures", result.message);
if (!target || target->upload || (source.viewport_width && source.viewport_width != target->image.width) ||
(source.viewport_height && source.viewport_height != target->image.height)) return result;
pass->target = &target->image;
if (pass->srgb_write && !target->image.srgb) {
result.message = "RGBA16F target " + tex->name + " cannot use SRGBWriteEnable";
return result;
}
}
if (!source.clear_render_targets && blend_or_mask(source)) {
pass->copy_scene = !target;
if (!target || target->ready) pass->load = VK_ATTACHMENT_LOAD_OP_LOAD;
}
if (!make_bindings(*program, *pass, info.effect, source, info.resources, root / "textures", result.message) ||
!make_pipeline(*program, *pass, info.effect, source, cache.handle(), cache_hits)) return result;
if (target) target->ready = true;
program->passes.push_back(std::move(pass));
}
}
if (std::ranges::none_of(program->passes, [](const auto& pass) { return pass->target == nullptr; })) {
result.message = "The preset does not write to the gameplay scene";
return result;
}
cache.save();
const auto elapsed = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - start);
logf("vk: ReShade: prepared in %.1f ms, Vulkan pipeline cache %u/%zu hits", elapsed.count(),
cache_hits, program->passes.size());
result.program = std::move(program);
result.message = scene.hdr ? "Ready for linear HDR gameplay" : "Ready for SDR gameplay";
} catch (const std::exception& e) {
result.message = e.what();
}
return result;
}
void retire(State& s) {
if (s.active) s.retired.emplace_back(vk_retirement_value(), std::move(s.active));
}
void blit(VkCommandBuffer cmd, VkImage from, VkImage to, const Scene& scene) {
const VkOffset3D end{static_cast<int32_t>(scene.width), static_cast<int32_t>(scene.height), 1};
const VkImageBlit region{color_layers, {{0, 0, 0}, end}, color_layers, {{0, 0, 0}, end}};
vkCmdBlitImage(cmd, from, VK_IMAGE_LAYOUT_GENERAL, to, VK_IMAGE_LAYOUT_GENERAL, 1, &region, VK_FILTER_NEAREST);
}
void render(VkCommandBuffer cmd, Program& p, VkImage image, VkImageView depth) {
GprofSpan profile(cmd, "ReShade FX");
if (!p.initialized) {
std::vector<VkImageMemoryBarrier2> barriers;
auto transition = [&](VkImage handle) {
VkImageMemoryBarrier2 barrier{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2};
barrier.dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT;
barrier.dstAccessMask = VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT;
barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
barrier.srcQueueFamilyIndex = barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.image = handle;
barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
barriers.push_back(barrier);
};
for (const auto& ping : p.scene_ping) transition(ping.image);
for (const auto& resource : p.resources) transition(resource->image.image);
VkDependencyInfo dependency{VK_STRUCTURE_TYPE_DEPENDENCY_INFO};
dependency.imageMemoryBarrierCount = static_cast<uint32_t>(barriers.size());
dependency.pImageMemoryBarriers = barriers.data();
vkCmdPipelineBarrier2(cmd, &dependency);
for (const auto& resource : p.resources) {
if (!resource->upload) continue;
VkBufferImageCopy copy{};
copy.imageSubresource = color_layers;
copy.imageExtent = {resource->image.width, resource->image.height, 1};
vkCmdCopyBufferToImage(cmd, resource->upload->buffer, resource->image.image, VK_IMAGE_LAYOUT_GENERAL, 1, &copy);
p.uploaded_at = vk_retirement_value();
}
if (p.uploaded_at) needs_tick.store(true, std::memory_order_release);
p.initialized = true;
logf("vk: ReShade: recording %zu pass(es) at %ux%u before the HUD (%s%s)", p.passes.size(),
p.scene.width, p.scene.height, p.scene.hdr ? "linear HDR" : "SDR", p.depth ? ", gameplay depth" : "");
}
full_barrier(cmd, "ReShade input");
blit(cmd, image, p.scene_ping[0].image, p.scene);
unsigned read = 0;
for (const auto& pass : p.passes) {
full_barrier(cmd, "ReShade pass");
const auto& input = p.scene_ping[read];
const auto& output = pass->target ? *pass->target : p.scene_ping[read ^ 1];
const auto width = output.width, height = output.height;
if (pass->copy_scene) {
const VkImageCopy copy{color_layers, {}, color_layers, {}, {width, height, 1}};
vkCmdCopyImage(cmd, input.image, VK_IMAGE_LAYOUT_GENERAL, output.image, VK_IMAGE_LAYOUT_GENERAL, 1, &copy);
full_barrier(cmd, "ReShade blend destination");
}
VkRenderingAttachmentInfo attachment{VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO};
attachment.imageView = pass->srgb_write ? output.srgb : output.code;
attachment.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
attachment.loadOp = pass->load;
attachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
VkRenderingInfo ri{VK_STRUCTURE_TYPE_RENDERING_INFO};
ri.renderArea.extent = {width, height};
ri.layerCount = ri.colorAttachmentCount = 1;
ri.pColorAttachments = &attachment;
vkCmdBeginRendering(cmd, &ri);
const VkViewport viewport{0.0f, float(height), float(width), -float(height), 0.0f, 1.0f};
const VkRect2D scissor{{0, 0}, {width, height}};
vkCmdSetViewport(cmd, 0, 1, &viewport);
vkCmdSetScissor(cmd, 0, 1, &scissor);
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pass->pipeline);
if (pass->uniforms) vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pass->layout, 0, 1,
&pass->uniforms, 0, nullptr);
std::array<VkDescriptorImageInfo, 16> images{};
std::array<VkWriteDescriptorSet, 16> writes{};
for (size_t i = 0; i < pass->bindings.size(); ++i) {
const auto& b = pass->bindings[i];
const auto& texture = b.image ? *b.image : input;
images[i] = {b.sampler, b.depth ? depth : b.srgb ? texture.srgb : texture.code, VK_IMAGE_LAYOUT_GENERAL};
auto& w = writes[i];
w = {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
w.dstBinding = b.binding;
w.descriptorCount = 1;
w.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
w.pImageInfo = &images[i];
}
if (!pass->bindings.empty()) vkCmdPushDescriptorSetKHR(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pass->layout, 1,
static_cast<uint32_t>(pass->bindings.size()), writes.data());
vkCmdDraw(cmd, 3, 1, 0, 0);
vkCmdEndRendering(cmd);
if (!pass->target) read ^= 1;
}
full_barrier(cmd, "ReShade output");
blit(cmd, p.scene_ping[read].image, image, p.scene);
full_barrier(cmd, "ReShade to game HUD");
}
} // namespace
void preview_bypass(bool bypass) { preview_bypassed.store(bypass, std::memory_order_relaxed); }
static void request_preset(State& s, int preset, std::string name, bool reload, bool exact) {
s.show_requested_only = exact;
if (!reload && s.request.preset == preset && s.request.name == name) return;
s.request = {preset, std::move(name), s.request.revision + 1};
s.message = preset ? "Waiting for the gameplay scene" : "Off";
enabled.store(preset != 0, std::memory_order_release);
if (!preset) depth_requested.store(false, std::memory_order_release);
needs_tick.store(true, std::memory_order_release);
}
void select(int preset) {
if (preset < 0 || preset > last_builtin_preset) preset = 0;
if (!preset && !enabled.load(std::memory_order_acquire)) return;
auto name = preset == user_preset ? read_selection() : std::string{};
auto& s = state();
std::lock_guard lock(s.mutex);
request_preset(s, preset, std::move(name), true, false);
}
void preview_select(int preset, std::string user_file) {
auto& s = state();
std::lock_guard lock(s.mutex);
if (!s.preview_start) s.preview_start = s.request.revision;
request_preset(s, preset, std::move(user_file), false, true);
}
void preview_end(int saved_preset) {
auto name = saved_preset == user_preset ? read_selection() : std::string{};
auto& s = state();
std::lock_guard lock(s.mutex);
const bool changed = s.preview_start && *s.preview_start != s.request.revision;
s.preview_start.reset();
request_preset(s, saved_preset, std::move(name), false, changed);
preview_bypass(false);
}
void select_user(std::string name) {
const auto root = user_root();
if (fx_asset(root / "presets", fs::u8path(name)).empty()) return;
const auto file = root / "selected.txt";
const auto temporary = root / "selected.tmp";
std::error_code error;
if (fs::exists(fs::symlink_status(temporary, error))) fs::remove(temporary, error);
std::ofstream out(temporary, std::ios::binary | std::ios::trunc);
out << name;
out.close();
if (out) fs::rename(temporary, file, error);
}
std::vector<std::string> user_presets() {
std::vector<std::string> names;
const auto dir = user_root() / "presets";
std::error_code error;
fs::create_directories(dir, error);
fs::create_directories(dir.parent_path() / "shaders", error);
fs::create_directories(dir.parent_path() / "textures", error);
for (fs::directory_iterator it(dir, error), end; !error && it != end; it.increment(error)) {
const auto filename = it->path().filename();
if (filename.extension() == ".ini" && !fx_asset(dir, filename).empty()) names.push_back(utf8(filename));
}
std::ranges::sort(names);
return names;
}
std::string user_selection() { return read_selection(); }
std::string status() {
if (!enabled.load(std::memory_order_acquire)) return {};
auto& s = state();
std::lock_guard lock(s.mutex);
return s.message;
}
void tick() {
auto& s = state();
std::lock_guard lock(s.mutex);
if (!s.request.preset) retire(s);
if (s.work.valid() && s.work.wait_for(std::chrono::seconds(0)) == std::future_status::ready) {
auto done = s.work.get();
if (s.request.preset && done.revision == s.request.revision) {
s.message = done.message;
if (done.program) {
retire(s);
s.active = std::move(done.program);
s.active_revision = done.revision;
depth_requested.store(s.active->depth, std::memory_order_release);
}
logf("vk: ReShade: %s", s.message.c_str());
}
}
if (!s.retired.empty() || (s.active && s.active->uploaded_at)) {
const auto completed = vk_timeline_completed();
std::erase_if(s.retired, [completed](const auto& retired) { return retired.first && retired.first <= completed; });
if (s.active && s.active->uploaded_at && s.active->uploaded_at <= completed) {
for (const auto& resource : s.active->resources) resource->upload.reset();
s.active->uploaded_at = 0;
}
}
needs_tick.store(s.work.valid() || !s.retired.empty() || (s.active && (s.active->uploaded_at || !s.request.preset)),
std::memory_order_release);
}
bool record(VkCommandBuffer cmd, Texture* picture, uint32_t level, bool native_hdr) {
constexpr auto transfer = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
if (!picture || !picture->image || level || picture->layers != 1 || picture->type != VK_IMAGE_TYPE_2D ||
picture->samples != VK_SAMPLE_COUNT_1_BIT || (picture->usage & transfer) != transfer ||
(picture->format != VK_FORMAT_B10G11R11_UFLOAT_PACK32 && !(native_hdr && picture->format == hdr_format))) return false;
const Scene scene{picture->width, picture->height, picture->format, native_hdr};
auto& s = state();
std::lock_guard lock(s.mutex);
if (!s.request.preset) return false;
const auto key = std::pair{s.request.revision, scene};
if (!s.work.valid() && (!s.attempted || *s.attempted != key)) {
s.attempted = key;
try {
s.work = std::async(std::launch::async, prepare, s.request, scene);
s.message = "Preparing ReShade preset";
needs_tick.store(true, std::memory_order_release);
} catch (const std::exception& e) {
s.message = e.what();
}
}
if (!s.active || s.active->scene != scene || preview_bypassed.load(std::memory_order_relaxed) ||
(s.show_requested_only && s.active_revision != s.request.revision)) return false;
const auto depth = s.active->depth ? temporal_reshade_depth(scene.width, scene.height) : VK_NULL_HANDLE;
if (s.active->depth && !depth) return false;
render(cmd, *s.active, picture->image, depth);
return true;
}
} // namespace mkwvk::reshade

View File

@@ -0,0 +1,32 @@
#pragma once
#include "vk_internal.h"
#include <atomic>
#include <string>
#include <vector>
namespace mkwvk {
struct Texture;
namespace reshade {
// Tested only at the gameplay scene boundary. No state is allocated with Off.
extern std::atomic<bool> enabled;
extern std::atomic<bool> needs_tick;
extern std::atomic<bool> depth_requested;
void select(int preset);
void preview_bypass(bool bypass);
void preview_select(int preset, std::string user_file);
void preview_end(int saved_preset);
void select_user(std::string name);
std::vector<std::string> user_presets();
std::string user_selection();
std::string status();
void tick();
bool record(VkCommandBuffer cmd, Texture* picture, uint32_t level, bool native_hdr);
} // namespace reshade
} // namespace mkwvk

View File

@@ -0,0 +1,46 @@
#include "fx_texture.h"
#include "fx_files.h"
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_STATIC
#define STBI_NO_STDIO
#define STBI_NO_HDR
#define STBI_NO_LINEAR
#define STBI_ONLY_PNG
#define STBI_MAX_DIMENSIONS 4096
#include "stb_image.h"
#include <memory>
#include <span>
namespace tf::reshade {
std::optional<TexturePixels> load_png(const std::filesystem::path& root, const std::filesystem::path& name,
std::string& diagnostics) {
const auto real = fx_asset(root, name);
if (real.empty()) {
const auto text = name.u8string();
diagnostics = "Missing texture " + std::string(reinterpret_cast<const char*>(text.data()), text.size()) +
" in reshade/textures";
return std::nullopt;
}
const FxFileScope scope(std::span(&root, 1));
std::string bytes;
if (!read_fx_file(real, bytes, 16 * 1024 * 1024)) {
diagnostics = "Cannot read ReShade PNG (file limit 16 MiB)";
return std::nullopt;
}
int width = 0, height = 0, components = 0;
const auto input = reinterpret_cast<const stbi_uc*>(bytes.data());
const auto length = static_cast<int>(bytes.size());
const std::unique_ptr<stbi_uc, decltype(&stbi_image_free)> pixels(
stbi_load_from_memory(input, length, &width, &height, &components, 4), stbi_image_free);
if (!pixels) {
diagnostics = "ReShade textures must be valid PNG images up to 4096 x 4096";
return std::nullopt;
}
const auto size = static_cast<size_t>(width) * height * 4;
return TexturePixels{static_cast<uint32_t>(width), static_cast<uint32_t>(height), {pixels.get(), pixels.get() + size}};
}
} // namespace tf::reshade

View File

@@ -0,0 +1,19 @@
#pragma once
#include <cstdint>
#include <filesystem>
#include <optional>
#include <string>
#include <vector>
namespace tf::reshade {
struct TexturePixels {
uint32_t width, height;
std::vector<uint8_t> rgba;
};
std::optional<TexturePixels> load_png(const std::filesystem::path& root, const std::filesystem::path& name,
std::string& diagnostics);
} // namespace tf::reshade