shader_environment.cpp 21 KB

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