Revert "vulkan/slang: Allow a cache for spv."

This reverts commit db554aa8cd62f083b93d4b2e7147c5d2500366d9.
This commit is contained in:
BearOso 2023-02-21 16:33:35 -06:00
parent 7e41394eea
commit 130e1509da
9 changed files with 31 additions and 112 deletions

View File

@ -22,26 +22,6 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
std::string get_cache_dir()
{
char *env_home = getenv("HOME");
char *env_cache = getenv("XDG_CACHE_HOME");
std::string dir;
if (env_cache)
dir = std::string(env_cache) + "/snes9x";
else if (env_home)
dir = std::string(env_home) + "/.cache/snes9x";
if (dir.empty())
return dir;
fs::create_directories(dir);
return dir;
}
std::string get_config_dir() std::string get_config_dir()
{ {
// Find config directory // Find config directory

View File

@ -159,7 +159,6 @@ class Snes9xConfig
int joystick_threshold; int joystick_threshold;
}; };
std::string get_cache_dir();
std::string get_config_dir(); std::string get_config_dir();
std::string get_config_file_name(); std::string get_config_file_name();

View File

@ -84,7 +84,7 @@ int S9xVulkanDisplayDriver::init()
if (!gui_config->shader_filename.empty() && gui_config->use_shaders) if (!gui_config->shader_filename.empty() && gui_config->use_shaders)
{ {
shaderchain = std::make_unique<Vulkan::ShaderChain>(context.get()); shaderchain = std::make_unique<Vulkan::ShaderChain>(context.get());
if (!shaderchain->load_shader_preset(gui_config->shader_filename, get_cache_dir())) if (!shaderchain->load_shader_preset(gui_config->shader_filename))
{ {
fmt::print("Couldn't load shader preset file\n"); fmt::print("Couldn't load shader preset file\n");
shaderchain = nullptr; shaderchain = nullptr;
@ -94,8 +94,6 @@ int S9xVulkanDisplayDriver::init()
window->enable_widget("shader_parameters_item", true); window->enable_widget("shader_parameters_item", true);
return 0; return 0;
} }
} }
simple_output = std::make_unique<Vulkan::SimpleOutput>(context.get(), vk::Format::eR5G6B5UnormPack16); simple_output = std::make_unique<Vulkan::SimpleOutput>(context.get(), vk::Format::eR5G6B5UnormPack16);

View File

@ -107,10 +107,6 @@ bool SlangPreset::load_preset_file(string filename)
} }
} }
if (!cache_directory.empty())
for (auto &p : passes)
p.set_cache_directory(cache_directory);
std::vector<std::future<bool>> futures; std::vector<std::future<bool>> futures;
for (size_t i = 0; i < passes.size(); i++) for (size_t i = 0; i < passes.size(); i++)
{ {
@ -139,11 +135,6 @@ bool SlangPreset::load_preset_file(string filename)
return true; return true;
} }
void SlangPreset::set_cache_directory(std::string filename)
{
cache_directory = filename;
}
/* /*
Aggregates the parameters from individual stages and separate shader files, Aggregates the parameters from individual stages and separate shader files,
resolving duplicates. resolving duplicates.

View File

@ -18,7 +18,6 @@ struct SlangPreset
bool match_sampler_semantic(const std::string &name, int pass, SlangShader::Sampler::Type &type, int &specifier); bool match_sampler_semantic(const std::string &name, int pass, SlangShader::Sampler::Type &type, int &specifier);
void gather_parameters(); void gather_parameters();
bool save_to_file(std::string filename); bool save_to_file(std::string filename);
void set_cache_directory(std::string filename);
struct Texture struct Texture
{ {
@ -35,5 +34,4 @@ struct SlangPreset
int oldest_previous_frame; int oldest_previous_frame;
bool uses_feedback; bool uses_feedback;
bool last_pass_uses_feedback; bool last_pass_uses_feedback;
std::string cache_directory;
}; };

View File

@ -10,8 +10,6 @@
#include "../external/glslang/glslang/Public/ShaderLang.h" #include "../external/glslang/glslang/Public/ShaderLang.h"
#include "../external/glslang/SPIRV/GlslangToSpv.h" #include "../external/glslang/SPIRV/GlslangToSpv.h"
#include "../external/glslang/StandAlone/ResourceLimits.h" #include "../external/glslang/StandAlone/ResourceLimits.h"
#include "../external/xxh64/xxh64.hpp"
#include <filesystem>
using std::string; using std::string;
using std::vector; using std::vector;
@ -25,11 +23,6 @@ SlangShader::~SlangShader()
{ {
} }
void SlangShader::set_cache_directory(std::string dir)
{
cache_directory = dir;
}
/* /*
Recursively load shader file and included files into memory, applying Recursively load shader file and included files into memory, applying
#include and #pragma directives. Will strip all directives except #include and #pragma directives. Will strip all directives except
@ -238,76 +231,44 @@ std::vector<uint32_t> SlangShader::generate_spirv(std::string shader_string, std
} }
std::string SlangShader::get_hash_filename(const std::string &shader_string)
{
uint64_t hash = xxh64::hash(shader_string.c_str(), fragment_shader_string.size(), 56);
char hash_hex_string[32];
snprintf(hash_hex_string, 32, "%016lx.spv", hash);
auto cache_path = std::filesystem::path(cache_directory);
cache_path /= hash_hex_string;
return cache_path.string();
}
static const char *header = "SPVCACHE1";
static size_t header_size = 9;
bool SlangShader::load_cache(std::string hash_filename, std::vector<uint32_t> &output)
{
if (cache_directory.empty())
return false;
if (!std::filesystem::exists(hash_filename))
return false;
auto size = std::filesystem::file_size(hash_filename);
size -= header_size;
if ((size & 3) != 0)
return false;
std::ifstream file(hash_filename, std::ios::binary);
char headerblock[header_size];
file.read(headerblock, header_size);
if (memcmp(headerblock, header, header_size))
{
printf("Invalid cache file format %s.\n", headerblock);
return false;
}
output.resize(size / 4);
file.read((char *)output.data(), size);
return true;
}
void SlangShader::save_cache(std::string hash_filename, std::vector<uint32_t> &spv)
{
if (cache_directory.empty())
return;
std::ofstream file(hash_filename, std::ios::binary);
file.write(header, header_size);
file.write((char *)spv.data(), spv.size() * 4);
}
/* /*
Generate SPIRV from separate preprocessed fragment and vertex shaders. Generate SPIRV from separate preprocessed fragment and vertex shaders.
Must have called divide_into_stages beforehand. Returns true on success. Must have called divide_into_stages beforehand. Returns true on success.
*/ */
bool SlangShader::generate_spirv() bool SlangShader::generate_spirv()
{ {
fragment_shader_spirv.clear(); Initializeglslang();
vertex_shader_spirv.clear();
auto vertex_cache_filename = get_hash_filename(vertex_shader_string); const EShMessages messages = (EShMessages)(EShMsgDefault | EShMsgVulkanRules | EShMsgSpvRules | EShMsgDebugInfo | EShMsgAST | EShMsgEnhanced);
auto fragment_cache_filename = get_hash_filename(fragment_shader_string); auto forbid_includer = glslang::TShader::ForbidIncluder();
if (load_cache(vertex_cache_filename, vertex_shader_spirv)) glslang::TShader vertexTShader(EShLangVertex);
if (load_cache(fragment_cache_filename, fragment_shader_spirv)) glslang::TShader fragmentTShader(EShLangFragment);
return true;
vertex_shader_spirv = generate_spirv(vertex_shader_string, "vertex"); auto compile = [&](glslang::TShader &shader, string &shader_string, std::vector<uint32_t> &spirv) -> bool {
fragment_shader_spirv = generate_spirv(fragment_shader_string, "fragment"); const char *source = shader_string.c_str();
shader.setStrings(&source, 1);
if (!shader.parse(&glslang::DefaultTBuiltInResource, 450, false, messages, forbid_includer))
return false;
save_cache(vertex_cache_filename, vertex_shader_spirv); glslang::TProgram program;
save_cache(fragment_cache_filename, fragment_shader_spirv); program.addShader(&shader);
if (!program.link(messages))
return false;
glslang::GlslangToSpv(*program.getIntermediate(shader.getStage()), spirv);
return true;
};
if (!compile(vertexTShader, vertex_shader_string, vertex_shader_spirv))
{
printf("%s\n%s\n", vertexTShader.getInfoLog(), vertexTShader.getInfoDebugLog());
return false;
}
if (!compile(fragmentTShader, fragment_shader_string, fragment_shader_spirv))
{
printf("%s\n%s\n", fragmentTShader.getInfoLog(), fragmentTShader.getInfoDebugLog());
return false;
}
return true; return true;
} }

View File

@ -72,10 +72,6 @@ struct SlangShader
bool load_file(std::string new_filename = ""); bool load_file(std::string new_filename = "");
void divide_into_stages(const std::vector<std::string> &lines); void divide_into_stages(const std::vector<std::string> &lines);
bool generate_spirv(); bool generate_spirv();
std::string get_hash_filename(const std::string &shader_string);
bool load_cache(std::string hash_filename, std::vector<uint32_t> &output);
void save_cache(std::string hash_filename, std::vector<uint32_t> &spv);
void set_cache_directory(std::string dir);
static std::vector<uint32_t> generate_spirv(std::string shader_string, std::string stage); static std::vector<uint32_t> generate_spirv(std::string shader_string, std::string stage);
std::string filename; std::string filename;
@ -104,5 +100,4 @@ struct SlangShader
int ubo_binding; int ubo_binding;
std::vector<Uniform> uniforms; std::vector<Uniform> uniforms;
std::vector<Sampler> samplers; std::vector<Sampler> samplers;
std::string cache_directory;
}; };

View File

@ -187,16 +187,13 @@ void ShaderChain::update_and_propagate_sizes(int original_width_new, int origina
} }
} }
bool ShaderChain::load_shader_preset(std::string filename, std::string cache_dir) bool ShaderChain::load_shader_preset(std::string filename)
{ {
if (!ends_with(filename, ".slangp")) if (!ends_with(filename, ".slangp"))
printf("Warning: loading preset without .slangp extension\n"); printf("Warning: loading preset without .slangp extension\n");
preset = std::make_unique<SlangPreset>(); preset = std::make_unique<SlangPreset>();
if (!cache_dir.empty())
preset->set_cache_directory(cache_dir);
if (!preset->load_preset_file(filename)) if (!preset->load_preset_file(filename))
{ {
printf("Couldn't load preset file: %s\n", filename.c_str()); printf("Couldn't load preset file: %s\n", filename.c_str());

View File

@ -16,7 +16,7 @@ class ShaderChain
ShaderChain(Context *context_); ShaderChain(Context *context_);
~ShaderChain(); ~ShaderChain();
bool load_shader_preset(std::string filename, std::string cache_dir = ""); bool load_shader_preset(std::string filename);
void update_and_propagate_sizes(int original_width_, int original_height_, int viewport_width_, int viewport_height_); void update_and_propagate_sizes(int original_width_, int original_height_, int viewport_width_, int viewport_height_);
bool load_lookup_textures(); bool load_lookup_textures();
void do_frame(uint8_t *data, int width, int height, int stride, vk::Format format, int viewport_x, int viewport_y, int viewport_width, int viewport_height); void do_frame(uint8_t *data, int width, int height, int stride, vk::Format format, int viewport_x, int viewport_y, int viewport_width, int viewport_height);