Skip to content

Commit 947ec15

Browse files
committed
Simplify effect cache loading and saving code
1 parent 988c806 commit 947ec15

File tree

2 files changed

+42
-106
lines changed

2 files changed

+42
-106
lines changed

source/runtime.cpp

Lines changed: 40 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ bool reshade::runtime::load_effect(const std::filesystem::path &source_file, con
10111011
}
10121012

10131013
bool source_cached = false; std::string source;
1014-
if (!effect.preprocessed && (preprocess_required || (source_cached = load_effect_cache(source_file, source_hash, source)) == false))
1014+
if (!effect.preprocessed && (preprocess_required || (source_cached = load_effect_cache(source_file.stem().u8string() + '-' + std::to_string(_renderer_id) + '-' + std::to_string(source_hash), "i", source)) == false))
10151015
{
10161016
reshadefx::preprocessor pp;
10171017
pp.add_macro_definition("__RESHADE__", std::to_string(VERSION_MAJOR * 10000 + VERSION_MINOR * 100 + VERSION_REVISION));
@@ -1064,7 +1064,7 @@ bool reshade::runtime::load_effect(const std::filesystem::path &source_file, con
10641064
if (effect.preprocessed)
10651065
{
10661066
source = std::move(pp.output());
1067-
source_cached = save_effect_cache(source_file, source_hash, source);
1067+
source_cached = save_effect_cache(source_file.stem().u8string() + '-' + std::to_string(_renderer_id) + '-' + std::to_string(source_hash), "i", source);
10681068

10691069
// Keep track of used preprocessor definitions (so they can be displayed in the overlay)
10701070
effect.definitions.clear();
@@ -1387,7 +1387,7 @@ bool reshade::runtime::create_effect(size_t effect_index)
13871387
}
13881388

13891389
// Compile shader modules
1390-
std::unordered_map<std::string, std::vector<char>> entry_points;
1390+
std::unordered_map<std::string, std::string> entry_points;
13911391

13921392
for (const reshadefx::entry_point &entry_point : effect.module.entry_points)
13931393
{
@@ -1496,42 +1496,38 @@ bool reshade::runtime::create_effect(size_t effect_index)
14961496
}
14971497
else if (_renderer_id & 0x10000)
14981498
{
1499-
std::string source = "#version 430\n";
1500-
source += "#define ENTRY_POINT_" + entry_point.name + " 1\n";
1499+
cso = "#version 430\n#define ENTRY_POINT_" + entry_point.name + " 1\n";
15011500

15021501
if (type == api::shader_stage::vertex)
15031502
{
15041503
// OpenGL does not allow using 'discard' in the vertex shader profile
1505-
source += "#define discard\n";
1504+
cso += "#define discard\n";
15061505
// 'dFdx', 'dFdx' and 'fwidth' too are only available in fragment shaders
1507-
source += "#define dFdx(x) x\n";
1508-
source += "#define dFdy(y) y\n";
1509-
source += "#define fwidth(p) p\n";
1506+
cso += "#define dFdx(x) x\n";
1507+
cso += "#define dFdy(y) y\n";
1508+
cso += "#define fwidth(p) p\n";
15101509
}
15111510
if (type != api::shader_stage::compute)
15121511
{
15131512
// OpenGL does not allow using 'shared' in vertex/fragment shader profile
1514-
source += "#define shared\n";
1515-
source += "#define atomicAdd(a, b) a\n";
1516-
source += "#define atomicAnd(a, b) a\n";
1517-
source += "#define atomicOr(a, b) a\n";
1518-
source += "#define atomicXor(a, b) a\n";
1519-
source += "#define atomicMin(a, b) a\n";
1520-
source += "#define atomicMax(a, b) a\n";
1521-
source += "#define atomicExchange(a, b) a\n";
1522-
source += "#define atomicCompSwap(a, b, c) a\n";
1513+
cso += "#define shared\n";
1514+
cso += "#define atomicAdd(a, b) a\n";
1515+
cso += "#define atomicAnd(a, b) a\n";
1516+
cso += "#define atomicOr(a, b) a\n";
1517+
cso += "#define atomicXor(a, b) a\n";
1518+
cso += "#define atomicMin(a, b) a\n";
1519+
cso += "#define atomicMax(a, b) a\n";
1520+
cso += "#define atomicExchange(a, b) a\n";
1521+
cso += "#define atomicCompSwap(a, b, c) a\n";
15231522
// Barrier intrinsics are only available in compute shaders
1524-
source += "#define barrier()\n";
1525-
source += "#define memoryBarrier()\n";
1526-
source += "#define groupMemoryBarrier()\n";
1523+
cso += "#define barrier()\n";
1524+
cso += "#define memoryBarrier()\n";
1525+
cso += "#define groupMemoryBarrier()\n";
15271526
}
15281527

1529-
source += "#line 1 0\n"; // Reset line number, so it matches what is shown when viewing the generated code
1530-
source += effect.preamble;
1531-
source += effect.module.hlsl;
1532-
1533-
cso.resize(source.size());
1534-
std::memcpy(cso.data(), source.data(), cso.size());
1528+
cso += "#line 1 0\n"; // Reset line number, so it matches what is shown when viewing the generated code
1529+
cso += effect.preamble;
1530+
cso += effect.module.hlsl;
15351531
}
15361532
else
15371533
{
@@ -1617,8 +1613,11 @@ bool reshade::runtime::create_effect(size_t effect_index)
16171613
attributes += "profile=" + profile + ';';
16181614
attributes += "flags=" + std::to_string(compile_flags) + ';';
16191615

1620-
const size_t hash = std::hash<std::string_view>()(attributes) ^ std::hash<std::string_view>()(hlsl);
1621-
if (!load_effect_cache(effect.source_file, entry_point.name, hash, cso, effect.assembly[entry_point.name]))
1616+
const std::string cache_id =
1617+
effect.source_file.stem().u8string() + '-' + entry_point.name + '-' + std::to_string(_renderer_id) + '-' +
1618+
std::to_string(std::hash<std::string_view>()(attributes) ^ std::hash<std::string_view>()(hlsl));
1619+
1620+
if (load_effect_cache(cache_id, "cso", cso) == false)
16221621
{
16231622
com_ptr<ID3DBlob> d3d_compiled, d3d_errors;
16241623
const HRESULT hr = D3DCompile(
@@ -1676,10 +1675,16 @@ bool reshade::runtime::create_effect(size_t effect_index)
16761675
cso.resize(d3d_compiled->GetBufferSize());
16771676
std::memcpy(cso.data(), d3d_compiled->GetBufferPointer(), cso.size());
16781677

1678+
save_effect_cache(cache_id, "cso", cso);
1679+
}
1680+
1681+
std::string &assembly = effect.assembly[entry_point.name];
1682+
if (load_effect_cache(cache_id, "asm", assembly) == false)
1683+
{
16791684
if (com_ptr<ID3DBlob> d3d_disassembled; SUCCEEDED(D3DDisassemble(cso.data(), cso.size(), 0, nullptr, &d3d_disassembled)))
1680-
effect.assembly[entry_point.name].assign(static_cast<const char *>(d3d_disassembled->GetBufferPointer()), d3d_disassembled->GetBufferSize() - 1);
1685+
assembly.assign(static_cast<const char *>(d3d_disassembled->GetBufferPointer()), d3d_disassembled->GetBufferSize() - 1);
16811686

1682-
save_effect_cache(effect.source_file, entry_point.name, hash, cso, effect.assembly[entry_point.name]);
1687+
save_effect_cache(cache_id, "asm", assembly);
16831688
}
16841689
}
16851690
}
@@ -2707,13 +2712,13 @@ void reshade::runtime::destroy_effects()
27072712
_textures_loaded = false;
27082713
}
27092714

2710-
bool reshade::runtime::load_effect_cache(const std::filesystem::path &source_file, const size_t hash, std::string &source) const
2715+
bool reshade::runtime::load_effect_cache(const std::string &id, const std::string &type, std::string &source) const
27112716
{
27122717
if (_no_effect_cache)
27132718
return false;
27142719

27152720
std::filesystem::path path = g_reshade_base_path / _intermediate_cache_path;
2716-
path /= std::filesystem::u8path("reshade-" + source_file.stem().u8string() + '-' + std::to_string(_renderer_id) + '-' + std::to_string(hash) + ".i");
2721+
path /= std::filesystem::u8path("reshade-" + id + '.' + type);
27172722

27182723
{ const HANDLE file = CreateFileW(path.c_str(), FILE_GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, nullptr);
27192724
if (file == INVALID_HANDLE_VALUE)
@@ -2725,47 +2730,13 @@ bool reshade::runtime::load_effect_cache(const std::filesystem::path &source_fil
27252730
return result != FALSE;
27262731
}
27272732
}
2728-
bool reshade::runtime::load_effect_cache(const std::filesystem::path &source_file, const std::string &entry_point, const size_t hash, std::vector<char> &cso, std::string &dasm) const
2733+
bool reshade::runtime::save_effect_cache(const std::string &id, const std::string &type, const std::string &source) const
27292734
{
27302735
if (_no_effect_cache)
27312736
return false;
27322737

27332738
std::filesystem::path path = g_reshade_base_path / _intermediate_cache_path;
2734-
path /= std::filesystem::u8path("reshade-" + source_file.stem().u8string() + '-' + entry_point + '-' + std::to_string(_renderer_id) + '-' + std::to_string(hash) + ".cso");
2735-
2736-
{ const HANDLE file = CreateFileW(path.c_str(), FILE_GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, nullptr);
2737-
if (file == INVALID_HANDLE_VALUE)
2738-
return false;
2739-
DWORD size = GetFileSize(file, nullptr);
2740-
cso.resize(size);
2741-
const BOOL result = ReadFile(file, cso.data(), size, &size, nullptr);
2742-
CloseHandle(file);
2743-
if (result == FALSE)
2744-
return false;
2745-
}
2746-
2747-
path.replace_extension(L".asm");
2748-
2749-
{ const HANDLE file = CreateFileW(path.c_str(), FILE_GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, nullptr);
2750-
if (file == INVALID_HANDLE_VALUE)
2751-
return false;
2752-
DWORD size = GetFileSize(file, nullptr);
2753-
dasm.resize(size);
2754-
const BOOL result = ReadFile(file, dasm.data(), size, &size, nullptr);
2755-
CloseHandle(file);
2756-
if (result == FALSE)
2757-
return false;
2758-
}
2759-
2760-
return true;
2761-
}
2762-
bool reshade::runtime::save_effect_cache(const std::filesystem::path &source_file, const size_t hash, const std::string &source) const
2763-
{
2764-
if (_no_effect_cache)
2765-
return false;
2766-
2767-
std::filesystem::path path = g_reshade_base_path / _intermediate_cache_path;
2768-
path /= std::filesystem::u8path("reshade-" + source_file.stem().u8string() + '-' + std::to_string(_renderer_id) + '-' + std::to_string(hash) + ".i");
2739+
path /= std::filesystem::u8path("reshade-" + id + '.' + type);
27692740

27702741
{ const HANDLE file = CreateFileW(path.c_str(), FILE_GENERIC_WRITE, FILE_SHARE_READ, nullptr, CREATE_NEW, FILE_ATTRIBUTE_ARCHIVE | FILE_FLAG_SEQUENTIAL_SCAN, nullptr);
27712742
if (file == INVALID_HANDLE_VALUE)
@@ -2776,39 +2747,6 @@ bool reshade::runtime::save_effect_cache(const std::filesystem::path &source_fil
27762747
return result != FALSE;
27772748
}
27782749
}
2779-
bool reshade::runtime::save_effect_cache(const std::filesystem::path &source_file, const std::string &entry_point, const size_t hash, const std::vector<char> &cso, const std::string &dasm) const
2780-
{
2781-
if (_no_effect_cache)
2782-
return false;
2783-
2784-
std::filesystem::path path = g_reshade_base_path / _intermediate_cache_path;
2785-
path /= std::filesystem::u8path("reshade-" + source_file.stem().u8string() + '-' + entry_point + '-' + std::to_string(_renderer_id) + '-' + std::to_string(hash) + ".cso");
2786-
2787-
{ const HANDLE file = CreateFileW(path.c_str(), FILE_GENERIC_WRITE, FILE_SHARE_READ, nullptr, CREATE_NEW, FILE_FLAG_SEQUENTIAL_SCAN, nullptr);
2788-
if (file == INVALID_HANDLE_VALUE)
2789-
return false;
2790-
DWORD size = static_cast<DWORD>(cso.size());
2791-
const BOOL result = WriteFile(file, cso.data(), size, &size, nullptr);
2792-
CloseHandle(file);
2793-
if (result == FALSE)
2794-
return false;
2795-
}
2796-
2797-
path.replace_extension(L".asm");
2798-
2799-
{ const HANDLE file = CreateFileW(path.c_str(), FILE_GENERIC_WRITE, FILE_SHARE_READ, nullptr, CREATE_NEW, FILE_ATTRIBUTE_ARCHIVE | FILE_FLAG_SEQUENTIAL_SCAN, nullptr);
2800-
if (file == INVALID_HANDLE_VALUE)
2801-
return false;
2802-
DWORD size = static_cast<DWORD>(dasm.size());
2803-
const BOOL result = WriteFile(file, dasm.c_str(), size, &size, NULL);
2804-
CloseHandle(file);
2805-
if (result == FALSE)
2806-
return false;
2807-
}
2808-
2809-
return true;
2810-
}
2811-
28122750
void reshade::runtime::clear_effect_cache()
28132751
{
28142752
// Find all cached effect files and delete them

source/runtime.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,8 @@ namespace reshade
183183
void reload_effects();
184184
void destroy_effects();
185185

186-
bool load_effect_cache(const std::filesystem::path &source_file, const size_t hash, std::string &source) const;
187-
bool load_effect_cache(const std::filesystem::path &source_file, const std::string &entry_point, const size_t hash, std::vector<char> &cso, std::string &dasm) const;
188-
bool save_effect_cache(const std::filesystem::path &source_file, const size_t hash, const std::string &source) const;
189-
bool save_effect_cache(const std::filesystem::path &source_file, const std::string &entry_point, const size_t hash, const std::vector<char> &cso, const std::string &dasm) const;
186+
bool load_effect_cache(const std::string &id, const std::string &type, std::string &data) const;
187+
bool save_effect_cache(const std::string &id, const std::string &type, const std::string &source) const;
190188
void clear_effect_cache();
191189

192190
void update_and_render_effects();

0 commit comments

Comments
 (0)