shader_environment.cpp 24 KB

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