#pragma once #include "../hash_combine.h" #include "../types.h" #include "../windows_headers.h" #include "shader_compiler.h" #include #include #include #include #include #include namespace D3D11 { class ShaderCache { public: template using ComPtr = Microsoft::WRL::ComPtr; ShaderCache(); ~ShaderCache(); void Open(std::string_view base_path, D3D_FEATURE_LEVEL feature_level, u32 version, bool debug); ComPtr GetShaderBlob(ShaderCompiler::Type type, std::string_view shader_code); ComPtr GetVertexShader(ID3D11Device* device, std::string_view shader_code); ComPtr GetGeometryShader(ID3D11Device* device, std::string_view shader_code); ComPtr GetPixelShader(ID3D11Device* device, std::string_view shader_code); ComPtr GetComputeShader(ID3D11Device* device, std::string_view shader_code); private: static constexpr u32 FILE_VERSION = 2; struct CacheIndexKey { u64 source_hash_low; u64 source_hash_high; u32 source_length; ShaderCompiler::Type shader_type; bool operator==(const CacheIndexKey& key) const; bool operator!=(const CacheIndexKey& key) const; }; struct CacheIndexEntryHasher { std::size_t operator()(const CacheIndexKey& e) const noexcept { std::size_t h = 0; hash_combine(h, e.source_hash_low, e.source_hash_high, e.source_length, e.shader_type); return h; } }; struct CacheIndexData { u32 file_offset; u32 blob_size; }; using CacheIndex = std::unordered_map; static std::string GetCacheBaseFileName(const std::string_view& base_path, D3D_FEATURE_LEVEL feature_level, bool debug); static CacheIndexKey GetCacheKey(ShaderCompiler::Type type, const std::string_view& shader_code); bool CreateNew(const std::string& index_filename, const std::string& blob_filename); bool ReadExisting(const std::string& index_filename, const std::string& blob_filename); void Close(); ComPtr CompileAndAddShaderBlob(const CacheIndexKey& key, std::string_view shader_code); std::FILE* m_index_file = nullptr; std::FILE* m_blob_file = nullptr; CacheIndex m_index; D3D_FEATURE_LEVEL m_feature_level = D3D_FEATURE_LEVEL_11_0; u32 m_version = 0; bool m_debug = false; }; } // namespace D3D11