shader_environment.cpp 24 KB

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