shader_environment.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <filesystem>
  5. #include <fstream>
  6. #include <memory>
  7. #include <optional>
  8. #include <utility>
  9. #include "common/assert.h"
  10. #include "common/cityhash.h"
  11. #include "common/common_types.h"
  12. #include "common/div_ceil.h"
  13. #include "common/fs/fs.h"
  14. #include "common/fs/path_util.h"
  15. #include "common/logging/log.h"
  16. #include "shader_recompiler/environment.h"
  17. #include "video_core/engines/kepler_compute.h"
  18. #include "video_core/memory_manager.h"
  19. #include "video_core/shader_environment.h"
  20. #include "video_core/textures/texture.h"
  21. namespace VideoCommon {
  22. constexpr std::array<char, 8> MAGIC_NUMBER{'y', 'u', 'z', 'u', 'c', 'a', 'c', 'h'};
  23. constexpr size_t INST_SIZE = sizeof(u64);
  24. using Maxwell = Tegra::Engines::Maxwell3D::Regs;
  25. static u64 MakeCbufKey(u32 index, u32 offset) {
  26. return (static_cast<u64>(index) << 32) | offset;
  27. }
  28. static Shader::TextureType ConvertType(const Tegra::Texture::TICEntry& entry) {
  29. switch (entry.texture_type) {
  30. case Tegra::Texture::TextureType::Texture1D:
  31. return Shader::TextureType::Color1D;
  32. case Tegra::Texture::TextureType::Texture2D:
  33. case Tegra::Texture::TextureType::Texture2DNoMipmap:
  34. return entry.normalized_coords ? Shader::TextureType::Color2D
  35. : Shader::TextureType::Color2DRect;
  36. case Tegra::Texture::TextureType::Texture3D:
  37. return Shader::TextureType::Color3D;
  38. case Tegra::Texture::TextureType::TextureCubemap:
  39. return Shader::TextureType::ColorCube;
  40. case Tegra::Texture::TextureType::Texture1DArray:
  41. return Shader::TextureType::ColorArray1D;
  42. case Tegra::Texture::TextureType::Texture2DArray:
  43. return Shader::TextureType::ColorArray2D;
  44. case Tegra::Texture::TextureType::Texture1DBuffer:
  45. return Shader::TextureType::Buffer;
  46. case Tegra::Texture::TextureType::TextureCubeArray:
  47. return Shader::TextureType::ColorArrayCube;
  48. default:
  49. UNIMPLEMENTED();
  50. return Shader::TextureType::Color2D;
  51. }
  52. }
  53. static std::string_view StageToPrefix(Shader::Stage stage) {
  54. switch (stage) {
  55. case Shader::Stage::VertexB:
  56. return "VB";
  57. case Shader::Stage::TessellationControl:
  58. return "TC";
  59. case Shader::Stage::TessellationEval:
  60. return "TE";
  61. case Shader::Stage::Geometry:
  62. return "GS";
  63. case Shader::Stage::Fragment:
  64. return "FS";
  65. case Shader::Stage::Compute:
  66. return "CS";
  67. case Shader::Stage::VertexA:
  68. return "VA";
  69. default:
  70. return "UK";
  71. }
  72. }
  73. static void DumpImpl(u64 hash, const u64* code, u32 read_highest, u32 read_lowest,
  74. u32 initial_offset, Shader::Stage stage) {
  75. const auto shader_dir{Common::FS::GetYuzuPath(Common::FS::YuzuPath::DumpDir)};
  76. const auto base_dir{shader_dir / "shaders"};
  77. if (!Common::FS::CreateDir(shader_dir) || !Common::FS::CreateDir(base_dir)) {
  78. LOG_ERROR(Common_Filesystem, "Failed to create shader dump directories");
  79. return;
  80. }
  81. const auto prefix = StageToPrefix(stage);
  82. const auto name{base_dir / fmt::format("{}{:016x}.ash", prefix, hash)};
  83. const size_t real_size = read_highest - read_lowest + initial_offset;
  84. const size_t padding_needed = ((32 - (real_size % 32)) % 32);
  85. std::fstream shader_file(name, std::ios::out | std::ios::binary);
  86. const size_t jump_index = initial_offset / sizeof(u64);
  87. shader_file.write(reinterpret_cast<const char*>(code + jump_index), real_size);
  88. for (size_t i = 0; i < padding_needed; i++) {
  89. shader_file.put(0);
  90. }
  91. }
  92. GenericEnvironment::GenericEnvironment(Tegra::MemoryManager& gpu_memory_, GPUVAddr program_base_,
  93. u32 start_address_)
  94. : gpu_memory{&gpu_memory_}, program_base{program_base_} {
  95. start_address = start_address_;
  96. }
  97. GenericEnvironment::~GenericEnvironment() = default;
  98. u32 GenericEnvironment::TextureBoundBuffer() const {
  99. return texture_bound;
  100. }
  101. u32 GenericEnvironment::LocalMemorySize() const {
  102. return local_memory_size;
  103. }
  104. u32 GenericEnvironment::SharedMemorySize() const {
  105. return shared_memory_size;
  106. }
  107. std::array<u32, 3> GenericEnvironment::WorkgroupSize() const {
  108. return workgroup_size;
  109. }
  110. u64 GenericEnvironment::ReadInstruction(u32 address) {
  111. read_lowest = std::min(read_lowest, address);
  112. read_highest = std::max(read_highest, address);
  113. if (address >= cached_lowest && address < cached_highest) {
  114. return code[(address - cached_lowest) / INST_SIZE];
  115. }
  116. has_unbound_instructions = true;
  117. return gpu_memory->Read<u64>(program_base + address);
  118. }
  119. std::optional<u64> GenericEnvironment::Analyze() {
  120. const std::optional<u64> size{TryFindSize()};
  121. if (!size) {
  122. return std::nullopt;
  123. }
  124. cached_lowest = start_address;
  125. cached_highest = start_address + static_cast<u32>(*size);
  126. return Common::CityHash64(reinterpret_cast<const char*>(code.data()), *size);
  127. }
  128. void GenericEnvironment::SetCachedSize(size_t size_bytes) {
  129. cached_lowest = start_address;
  130. cached_highest = start_address + static_cast<u32>(size_bytes);
  131. code.resize(CachedSize());
  132. gpu_memory->ReadBlock(program_base + cached_lowest, code.data(), code.size() * sizeof(u64));
  133. }
  134. size_t GenericEnvironment::CachedSize() const noexcept {
  135. return cached_highest - cached_lowest + INST_SIZE;
  136. }
  137. size_t GenericEnvironment::ReadSize() const noexcept {
  138. return read_highest - read_lowest + INST_SIZE;
  139. }
  140. bool GenericEnvironment::CanBeSerialized() const noexcept {
  141. return !has_unbound_instructions;
  142. }
  143. u64 GenericEnvironment::CalculateHash() const {
  144. const size_t size{ReadSize()};
  145. const auto data{std::make_unique<char[]>(size)};
  146. gpu_memory->ReadBlock(program_base + read_lowest, data.get(), size);
  147. return Common::CityHash64(data.get(), size);
  148. }
  149. void GenericEnvironment::Dump(u64 hash) {
  150. DumpImpl(hash, code.data(), read_highest, read_lowest, initial_offset, stage);
  151. }
  152. void GenericEnvironment::Serialize(std::ofstream& file) const {
  153. const u64 code_size{static_cast<u64>(CachedSize())};
  154. const u64 num_texture_types{static_cast<u64>(texture_types.size())};
  155. const u64 num_cbuf_values{static_cast<u64>(cbuf_values.size())};
  156. file.write(reinterpret_cast<const char*>(&code_size), sizeof(code_size))
  157. .write(reinterpret_cast<const char*>(&num_texture_types), sizeof(num_texture_types))
  158. .write(reinterpret_cast<const char*>(&num_cbuf_values), sizeof(num_cbuf_values))
  159. .write(reinterpret_cast<const char*>(&local_memory_size), sizeof(local_memory_size))
  160. .write(reinterpret_cast<const char*>(&texture_bound), sizeof(texture_bound))
  161. .write(reinterpret_cast<const char*>(&start_address), sizeof(start_address))
  162. .write(reinterpret_cast<const char*>(&cached_lowest), sizeof(cached_lowest))
  163. .write(reinterpret_cast<const char*>(&cached_highest), sizeof(cached_highest))
  164. .write(reinterpret_cast<const char*>(&stage), sizeof(stage))
  165. .write(reinterpret_cast<const char*>(code.data()), code_size);
  166. for (const auto& [key, type] : texture_types) {
  167. file.write(reinterpret_cast<const char*>(&key), sizeof(key))
  168. .write(reinterpret_cast<const char*>(&type), sizeof(type));
  169. }
  170. for (const auto& [key, type] : cbuf_values) {
  171. file.write(reinterpret_cast<const char*>(&key), sizeof(key))
  172. .write(reinterpret_cast<const char*>(&type), sizeof(type));
  173. }
  174. if (stage == Shader::Stage::Compute) {
  175. file.write(reinterpret_cast<const char*>(&workgroup_size), sizeof(workgroup_size))
  176. .write(reinterpret_cast<const char*>(&shared_memory_size), sizeof(shared_memory_size));
  177. } else {
  178. file.write(reinterpret_cast<const char*>(&sph), sizeof(sph));
  179. if (stage == Shader::Stage::Geometry) {
  180. file.write(reinterpret_cast<const char*>(&gp_passthrough_mask),
  181. sizeof(gp_passthrough_mask));
  182. }
  183. }
  184. }
  185. std::optional<u64> GenericEnvironment::TryFindSize() {
  186. static constexpr size_t BLOCK_SIZE = 0x1000;
  187. static constexpr size_t MAXIMUM_SIZE = 0x100000;
  188. static constexpr u64 SELF_BRANCH_A = 0xE2400FFFFF87000FULL;
  189. static constexpr u64 SELF_BRANCH_B = 0xE2400FFFFF07000FULL;
  190. GPUVAddr guest_addr{program_base + start_address};
  191. size_t offset{0};
  192. size_t size{BLOCK_SIZE};
  193. while (size <= MAXIMUM_SIZE) {
  194. code.resize(size / INST_SIZE);
  195. u64* const data = code.data() + offset / INST_SIZE;
  196. gpu_memory->ReadBlock(guest_addr, data, BLOCK_SIZE);
  197. for (size_t index = 0; index < BLOCK_SIZE; index += INST_SIZE) {
  198. const u64 inst = data[index / INST_SIZE];
  199. if (inst == SELF_BRANCH_A || inst == SELF_BRANCH_B) {
  200. return offset + index;
  201. }
  202. }
  203. guest_addr += BLOCK_SIZE;
  204. size += BLOCK_SIZE;
  205. offset += BLOCK_SIZE;
  206. }
  207. return std::nullopt;
  208. }
  209. Shader::TextureType GenericEnvironment::ReadTextureTypeImpl(GPUVAddr tic_addr, u32 tic_limit,
  210. bool via_header_index, u32 raw) {
  211. const auto handle{Tegra::Texture::TexturePair(raw, via_header_index)};
  212. const GPUVAddr descriptor_addr{tic_addr + handle.first * sizeof(Tegra::Texture::TICEntry)};
  213. Tegra::Texture::TICEntry entry;
  214. gpu_memory->ReadBlock(descriptor_addr, &entry, sizeof(entry));
  215. const Shader::TextureType result{ConvertType(entry)};
  216. texture_types.emplace(raw, result);
  217. return result;
  218. }
  219. GraphicsEnvironment::GraphicsEnvironment(Tegra::Engines::Maxwell3D& maxwell3d_,
  220. Tegra::MemoryManager& gpu_memory_,
  221. Maxwell::ShaderProgram program, GPUVAddr program_base_,
  222. u32 start_address_)
  223. : GenericEnvironment{gpu_memory_, program_base_, start_address_}, maxwell3d{&maxwell3d_} {
  224. gpu_memory->ReadBlock(program_base + start_address, &sph, sizeof(sph));
  225. initial_offset = sizeof(sph);
  226. gp_passthrough_mask = maxwell3d->regs.gp_passthrough_mask;
  227. switch (program) {
  228. case Maxwell::ShaderProgram::VertexA:
  229. stage = Shader::Stage::VertexA;
  230. stage_index = 0;
  231. break;
  232. case Maxwell::ShaderProgram::VertexB:
  233. stage = Shader::Stage::VertexB;
  234. stage_index = 0;
  235. break;
  236. case Maxwell::ShaderProgram::TesselationControl:
  237. stage = Shader::Stage::TessellationControl;
  238. stage_index = 1;
  239. break;
  240. case Maxwell::ShaderProgram::TesselationEval:
  241. stage = Shader::Stage::TessellationEval;
  242. stage_index = 2;
  243. break;
  244. case Maxwell::ShaderProgram::Geometry:
  245. stage = Shader::Stage::Geometry;
  246. stage_index = 3;
  247. break;
  248. case Maxwell::ShaderProgram::Fragment:
  249. stage = Shader::Stage::Fragment;
  250. stage_index = 4;
  251. break;
  252. default:
  253. ASSERT_MSG(false, "Invalid program={}", program);
  254. break;
  255. }
  256. const u64 local_size{sph.LocalMemorySize()};
  257. ASSERT(local_size <= std::numeric_limits<u32>::max());
  258. local_memory_size = static_cast<u32>(local_size) + sph.common3.shader_local_memory_crs_size;
  259. texture_bound = maxwell3d->regs.tex_cb_index;
  260. }
  261. u32 GraphicsEnvironment::ReadCbufValue(u32 cbuf_index, u32 cbuf_offset) {
  262. const auto& cbuf{maxwell3d->state.shader_stages[stage_index].const_buffers[cbuf_index]};
  263. ASSERT(cbuf.enabled);
  264. u32 value{};
  265. if (cbuf_offset < cbuf.size) {
  266. value = gpu_memory->Read<u32>(cbuf.address + cbuf_offset);
  267. }
  268. cbuf_values.emplace(MakeCbufKey(cbuf_index, cbuf_offset), value);
  269. return value;
  270. }
  271. Shader::TextureType GraphicsEnvironment::ReadTextureType(u32 handle) {
  272. const auto& regs{maxwell3d->regs};
  273. const bool via_header_index{regs.sampler_index == Maxwell::SamplerIndex::ViaHeaderIndex};
  274. return ReadTextureTypeImpl(regs.tic.Address(), regs.tic.limit, via_header_index, handle);
  275. }
  276. ComputeEnvironment::ComputeEnvironment(Tegra::Engines::KeplerCompute& kepler_compute_,
  277. Tegra::MemoryManager& gpu_memory_, GPUVAddr program_base_,
  278. u32 start_address_)
  279. : GenericEnvironment{gpu_memory_, program_base_, start_address_}, kepler_compute{
  280. &kepler_compute_} {
  281. const auto& qmd{kepler_compute->launch_description};
  282. stage = Shader::Stage::Compute;
  283. local_memory_size = qmd.local_pos_alloc + qmd.local_crs_alloc;
  284. texture_bound = kepler_compute->regs.tex_cb_index;
  285. shared_memory_size = qmd.shared_alloc;
  286. workgroup_size = {qmd.block_dim_x, qmd.block_dim_y, qmd.block_dim_z};
  287. }
  288. u32 ComputeEnvironment::ReadCbufValue(u32 cbuf_index, u32 cbuf_offset) {
  289. const auto& qmd{kepler_compute->launch_description};
  290. ASSERT(((qmd.const_buffer_enable_mask.Value() >> cbuf_index) & 1) != 0);
  291. const auto& cbuf{qmd.const_buffer_config[cbuf_index]};
  292. u32 value{};
  293. if (cbuf_offset < cbuf.size) {
  294. value = gpu_memory->Read<u32>(cbuf.Address() + cbuf_offset);
  295. }
  296. cbuf_values.emplace(MakeCbufKey(cbuf_index, cbuf_offset), value);
  297. return value;
  298. }
  299. Shader::TextureType ComputeEnvironment::ReadTextureType(u32 handle) {
  300. const auto& regs{kepler_compute->regs};
  301. const auto& qmd{kepler_compute->launch_description};
  302. return ReadTextureTypeImpl(regs.tic.Address(), regs.tic.limit, qmd.linked_tsc != 0, handle);
  303. }
  304. void FileEnvironment::Deserialize(std::ifstream& file) {
  305. u64 code_size{};
  306. u64 num_texture_types{};
  307. u64 num_cbuf_values{};
  308. file.read(reinterpret_cast<char*>(&code_size), sizeof(code_size))
  309. .read(reinterpret_cast<char*>(&num_texture_types), sizeof(num_texture_types))
  310. .read(reinterpret_cast<char*>(&num_cbuf_values), sizeof(num_cbuf_values))
  311. .read(reinterpret_cast<char*>(&local_memory_size), sizeof(local_memory_size))
  312. .read(reinterpret_cast<char*>(&texture_bound), sizeof(texture_bound))
  313. .read(reinterpret_cast<char*>(&start_address), sizeof(start_address))
  314. .read(reinterpret_cast<char*>(&read_lowest), sizeof(read_lowest))
  315. .read(reinterpret_cast<char*>(&read_highest), sizeof(read_highest))
  316. .read(reinterpret_cast<char*>(&stage), sizeof(stage));
  317. code = std::make_unique<u64[]>(Common::DivCeil(code_size, sizeof(u64)));
  318. file.read(reinterpret_cast<char*>(code.get()), code_size);
  319. for (size_t i = 0; i < num_texture_types; ++i) {
  320. u32 key;
  321. Shader::TextureType type;
  322. file.read(reinterpret_cast<char*>(&key), sizeof(key))
  323. .read(reinterpret_cast<char*>(&type), sizeof(type));
  324. texture_types.emplace(key, type);
  325. }
  326. for (size_t i = 0; i < num_cbuf_values; ++i) {
  327. u64 key;
  328. u32 value;
  329. file.read(reinterpret_cast<char*>(&key), sizeof(key))
  330. .read(reinterpret_cast<char*>(&value), sizeof(value));
  331. cbuf_values.emplace(key, value);
  332. }
  333. if (stage == Shader::Stage::Compute) {
  334. file.read(reinterpret_cast<char*>(&workgroup_size), sizeof(workgroup_size))
  335. .read(reinterpret_cast<char*>(&shared_memory_size), sizeof(shared_memory_size));
  336. initial_offset = 0;
  337. } else {
  338. file.read(reinterpret_cast<char*>(&sph), sizeof(sph));
  339. initial_offset = sizeof(sph);
  340. if (stage == Shader::Stage::Geometry) {
  341. file.read(reinterpret_cast<char*>(&gp_passthrough_mask), sizeof(gp_passthrough_mask));
  342. }
  343. }
  344. }
  345. void FileEnvironment::Dump(u64 hash) {
  346. DumpImpl(hash, code.get(), read_highest, read_lowest, initial_offset, stage);
  347. }
  348. u64 FileEnvironment::ReadInstruction(u32 address) {
  349. if (address < read_lowest || address > read_highest) {
  350. throw Shader::LogicError("Out of bounds address {}", address);
  351. }
  352. return code[(address - read_lowest) / sizeof(u64)];
  353. }
  354. u32 FileEnvironment::ReadCbufValue(u32 cbuf_index, u32 cbuf_offset) {
  355. const auto it{cbuf_values.find(MakeCbufKey(cbuf_index, cbuf_offset))};
  356. if (it == cbuf_values.end()) {
  357. throw Shader::LogicError("Uncached read texture type");
  358. }
  359. return it->second;
  360. }
  361. Shader::TextureType FileEnvironment::ReadTextureType(u32 handle) {
  362. const auto it{texture_types.find(handle)};
  363. if (it == texture_types.end()) {
  364. throw Shader::LogicError("Uncached read texture type");
  365. }
  366. return it->second;
  367. }
  368. u32 FileEnvironment::LocalMemorySize() const {
  369. return local_memory_size;
  370. }
  371. u32 FileEnvironment::SharedMemorySize() const {
  372. return shared_memory_size;
  373. }
  374. u32 FileEnvironment::TextureBoundBuffer() const {
  375. return texture_bound;
  376. }
  377. std::array<u32, 3> FileEnvironment::WorkgroupSize() const {
  378. return workgroup_size;
  379. }
  380. void SerializePipeline(std::span<const char> key, std::span<const GenericEnvironment* const> envs,
  381. const std::filesystem::path& filename, u32 cache_version) try {
  382. std::ofstream file(filename, std::ios::binary | std::ios::ate | std::ios::app);
  383. file.exceptions(std::ifstream::failbit);
  384. if (!file.is_open()) {
  385. LOG_ERROR(Common_Filesystem, "Failed to open pipeline cache file {}",
  386. Common::FS::PathToUTF8String(filename));
  387. return;
  388. }
  389. if (file.tellp() == 0) {
  390. // Write header
  391. file.write(MAGIC_NUMBER.data(), MAGIC_NUMBER.size())
  392. .write(reinterpret_cast<const char*>(&cache_version), sizeof(cache_version));
  393. }
  394. if (!std::ranges::all_of(envs, &GenericEnvironment::CanBeSerialized)) {
  395. return;
  396. }
  397. const u32 num_envs{static_cast<u32>(envs.size())};
  398. file.write(reinterpret_cast<const char*>(&num_envs), sizeof(num_envs));
  399. for (const GenericEnvironment* const env : envs) {
  400. env->Serialize(file);
  401. }
  402. file.write(key.data(), key.size_bytes());
  403. } catch (const std::ios_base::failure& e) {
  404. LOG_ERROR(Common_Filesystem, "{}", e.what());
  405. if (!Common::FS::RemoveFile(filename)) {
  406. LOG_ERROR(Common_Filesystem, "Failed to delete pipeline cache file {}",
  407. Common::FS::PathToUTF8String(filename));
  408. }
  409. }
  410. void LoadPipelines(
  411. std::stop_token stop_loading, const std::filesystem::path& filename, u32 expected_cache_version,
  412. Common::UniqueFunction<void, std::ifstream&, FileEnvironment> load_compute,
  413. Common::UniqueFunction<void, std::ifstream&, std::vector<FileEnvironment>> load_graphics) try {
  414. std::ifstream file(filename, std::ios::binary | std::ios::ate);
  415. if (!file.is_open()) {
  416. return;
  417. }
  418. file.exceptions(std::ifstream::failbit);
  419. const auto end{file.tellg()};
  420. file.seekg(0, std::ios::beg);
  421. std::array<char, 8> magic_number;
  422. u32 cache_version;
  423. file.read(magic_number.data(), magic_number.size())
  424. .read(reinterpret_cast<char*>(&cache_version), sizeof(cache_version));
  425. if (magic_number != MAGIC_NUMBER || cache_version != expected_cache_version) {
  426. file.close();
  427. if (Common::FS::RemoveFile(filename)) {
  428. if (magic_number != MAGIC_NUMBER) {
  429. LOG_ERROR(Common_Filesystem, "Invalid pipeline cache file");
  430. }
  431. if (cache_version != expected_cache_version) {
  432. LOG_INFO(Common_Filesystem, "Deleting old pipeline cache");
  433. }
  434. } else {
  435. LOG_ERROR(Common_Filesystem,
  436. "Invalid pipeline cache file and failed to delete it in \"{}\"",
  437. Common::FS::PathToUTF8String(filename));
  438. }
  439. return;
  440. }
  441. while (file.tellg() != end) {
  442. if (stop_loading.stop_requested()) {
  443. return;
  444. }
  445. u32 num_envs{};
  446. file.read(reinterpret_cast<char*>(&num_envs), sizeof(num_envs));
  447. std::vector<FileEnvironment> envs(num_envs);
  448. for (FileEnvironment& env : envs) {
  449. env.Deserialize(file);
  450. }
  451. if (envs.front().ShaderStage() == Shader::Stage::Compute) {
  452. load_compute(file, std::move(envs.front()));
  453. } else {
  454. load_graphics(file, std::move(envs));
  455. }
  456. }
  457. } catch (const std::ios_base::failure& e) {
  458. LOG_ERROR(Common_Filesystem, "{}", e.what());
  459. if (!Common::FS::RemoveFile(filename)) {
  460. LOG_ERROR(Common_Filesystem, "Failed to delete pipeline cache file {}",
  461. Common::FS::PathToUTF8String(filename));
  462. }
  463. }
  464. } // namespace VideoCommon