shader_environment.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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(CachedSizeWords());
  153. gpu_memory->ReadBlock(program_base + cached_lowest, code.data(), code.size() * sizeof(u64));
  154. }
  155. size_t GenericEnvironment::CachedSizeWords() const noexcept {
  156. return CachedSizeBytes() / INST_SIZE;
  157. }
  158. size_t GenericEnvironment::CachedSizeBytes() const noexcept {
  159. return static_cast<size_t>(cached_highest) - cached_lowest + INST_SIZE;
  160. }
  161. size_t GenericEnvironment::ReadSizeBytes() const noexcept {
  162. return read_highest - read_lowest + INST_SIZE;
  163. }
  164. bool GenericEnvironment::CanBeSerialized() const noexcept {
  165. return !has_unbound_instructions;
  166. }
  167. u64 GenericEnvironment::CalculateHash() const {
  168. const size_t size{ReadSizeBytes()};
  169. const auto data{std::make_unique<char[]>(size)};
  170. gpu_memory->ReadBlock(program_base + read_lowest, data.get(), size);
  171. return Common::CityHash64(data.get(), size);
  172. }
  173. void GenericEnvironment::Dump(u64 hash) {
  174. DumpImpl(hash, code.data(), read_highest, read_lowest, initial_offset, stage);
  175. }
  176. void GenericEnvironment::Serialize(std::ofstream& file) const {
  177. const u64 code_size{static_cast<u64>(CachedSizeBytes())};
  178. const u64 num_texture_types{static_cast<u64>(texture_types.size())};
  179. const u64 num_texture_pixel_formats{static_cast<u64>(texture_pixel_formats.size())};
  180. const u64 num_cbuf_values{static_cast<u64>(cbuf_values.size())};
  181. const u64 num_cbuf_replacement_values{static_cast<u64>(cbuf_replacements.size())};
  182. file.write(reinterpret_cast<const char*>(&code_size), sizeof(code_size))
  183. .write(reinterpret_cast<const char*>(&num_texture_types), sizeof(num_texture_types))
  184. .write(reinterpret_cast<const char*>(&num_texture_pixel_formats),
  185. sizeof(num_texture_pixel_formats))
  186. .write(reinterpret_cast<const char*>(&num_cbuf_values), sizeof(num_cbuf_values))
  187. .write(reinterpret_cast<const char*>(&num_cbuf_replacement_values),
  188. sizeof(num_cbuf_replacement_values))
  189. .write(reinterpret_cast<const char*>(&local_memory_size), sizeof(local_memory_size))
  190. .write(reinterpret_cast<const char*>(&texture_bound), sizeof(texture_bound))
  191. .write(reinterpret_cast<const char*>(&start_address), sizeof(start_address))
  192. .write(reinterpret_cast<const char*>(&cached_lowest), sizeof(cached_lowest))
  193. .write(reinterpret_cast<const char*>(&cached_highest), sizeof(cached_highest))
  194. .write(reinterpret_cast<const char*>(&viewport_transform_state),
  195. sizeof(viewport_transform_state))
  196. .write(reinterpret_cast<const char*>(&stage), sizeof(stage))
  197. .write(reinterpret_cast<const char*>(code.data()), code_size);
  198. for (const auto& [key, type] : texture_types) {
  199. file.write(reinterpret_cast<const char*>(&key), sizeof(key))
  200. .write(reinterpret_cast<const char*>(&type), sizeof(type));
  201. }
  202. for (const auto& [key, format] : texture_pixel_formats) {
  203. file.write(reinterpret_cast<const char*>(&key), sizeof(key))
  204. .write(reinterpret_cast<const char*>(&format), sizeof(format));
  205. }
  206. for (const auto& [key, type] : cbuf_values) {
  207. file.write(reinterpret_cast<const char*>(&key), sizeof(key))
  208. .write(reinterpret_cast<const char*>(&type), sizeof(type));
  209. }
  210. for (const auto& [key, type] : cbuf_replacements) {
  211. file.write(reinterpret_cast<const char*>(&key), sizeof(key))
  212. .write(reinterpret_cast<const char*>(&type), sizeof(type));
  213. }
  214. if (stage == Shader::Stage::Compute) {
  215. file.write(reinterpret_cast<const char*>(&workgroup_size), sizeof(workgroup_size))
  216. .write(reinterpret_cast<const char*>(&shared_memory_size), sizeof(shared_memory_size));
  217. } else {
  218. file.write(reinterpret_cast<const char*>(&sph), sizeof(sph));
  219. if (stage == Shader::Stage::Geometry) {
  220. file.write(reinterpret_cast<const char*>(&gp_passthrough_mask),
  221. sizeof(gp_passthrough_mask));
  222. }
  223. }
  224. }
  225. std::optional<u64> GenericEnvironment::TryFindSize() {
  226. static constexpr size_t BLOCK_SIZE = 0x1000;
  227. static constexpr size_t MAXIMUM_SIZE = 0x100000;
  228. static constexpr u64 SELF_BRANCH_A = 0xE2400FFFFF87000FULL;
  229. static constexpr u64 SELF_BRANCH_B = 0xE2400FFFFF07000FULL;
  230. GPUVAddr guest_addr{program_base + start_address};
  231. size_t offset{0};
  232. size_t size{BLOCK_SIZE};
  233. while (size <= MAXIMUM_SIZE) {
  234. code.resize(size / INST_SIZE);
  235. u64* const data = code.data() + offset / INST_SIZE;
  236. gpu_memory->ReadBlock(guest_addr, data, BLOCK_SIZE);
  237. for (size_t index = 0; index < BLOCK_SIZE; index += INST_SIZE) {
  238. const u64 inst = data[index / INST_SIZE];
  239. if (inst == SELF_BRANCH_A || inst == SELF_BRANCH_B) {
  240. return offset + index;
  241. }
  242. }
  243. guest_addr += BLOCK_SIZE;
  244. size += BLOCK_SIZE;
  245. offset += BLOCK_SIZE;
  246. }
  247. return std::nullopt;
  248. }
  249. Tegra::Texture::TICEntry GenericEnvironment::ReadTextureInfo(GPUVAddr tic_addr, u32 tic_limit,
  250. bool via_header_index, u32 raw) {
  251. const auto handle{Tegra::Texture::TexturePair(raw, via_header_index)};
  252. const GPUVAddr descriptor_addr{tic_addr + handle.first * sizeof(Tegra::Texture::TICEntry)};
  253. Tegra::Texture::TICEntry entry;
  254. gpu_memory->ReadBlock(descriptor_addr, &entry, sizeof(entry));
  255. return entry;
  256. }
  257. GraphicsEnvironment::GraphicsEnvironment(Tegra::Engines::Maxwell3D& maxwell3d_,
  258. Tegra::MemoryManager& gpu_memory_,
  259. Maxwell::ShaderType program, GPUVAddr program_base_,
  260. u32 start_address_)
  261. : GenericEnvironment{gpu_memory_, program_base_, start_address_}, maxwell3d{&maxwell3d_} {
  262. gpu_memory->ReadBlock(program_base + start_address, &sph, sizeof(sph));
  263. initial_offset = sizeof(sph);
  264. gp_passthrough_mask = maxwell3d->regs.post_vtg_shader_attrib_skip_mask;
  265. switch (program) {
  266. case Maxwell::ShaderType::VertexA:
  267. stage = Shader::Stage::VertexA;
  268. stage_index = 0;
  269. break;
  270. case Maxwell::ShaderType::VertexB:
  271. stage = Shader::Stage::VertexB;
  272. stage_index = 0;
  273. break;
  274. case Maxwell::ShaderType::TessellationInit:
  275. stage = Shader::Stage::TessellationControl;
  276. stage_index = 1;
  277. break;
  278. case Maxwell::ShaderType::Tessellation:
  279. stage = Shader::Stage::TessellationEval;
  280. stage_index = 2;
  281. break;
  282. case Maxwell::ShaderType::Geometry:
  283. stage = Shader::Stage::Geometry;
  284. stage_index = 3;
  285. break;
  286. case Maxwell::ShaderType::Pixel:
  287. stage = Shader::Stage::Fragment;
  288. stage_index = 4;
  289. break;
  290. default:
  291. ASSERT_MSG(false, "Invalid program={}", program);
  292. break;
  293. }
  294. const u64 local_size{sph.LocalMemorySize()};
  295. ASSERT(local_size <= std::numeric_limits<u32>::max());
  296. local_memory_size = static_cast<u32>(local_size) + sph.common3.shader_local_memory_crs_size;
  297. texture_bound = maxwell3d->regs.bindless_texture_const_buffer_slot;
  298. is_propietary_driver = texture_bound == 2;
  299. has_hle_engine_state =
  300. maxwell3d->engine_state == Tegra::Engines::Maxwell3D::EngineHint::OnHLEMacro;
  301. }
  302. u32 GraphicsEnvironment::ReadCbufValue(u32 cbuf_index, u32 cbuf_offset) {
  303. const auto& cbuf{maxwell3d->state.shader_stages[stage_index].const_buffers[cbuf_index]};
  304. ASSERT(cbuf.enabled);
  305. u32 value{};
  306. if (cbuf_offset < cbuf.size) {
  307. value = gpu_memory->Read<u32>(cbuf.address + cbuf_offset);
  308. }
  309. cbuf_values.emplace(MakeCbufKey(cbuf_index, cbuf_offset), value);
  310. return value;
  311. }
  312. std::optional<Shader::ReplaceConstant> GraphicsEnvironment::GetReplaceConstBuffer(u32 bank,
  313. u32 offset) {
  314. if (!has_hle_engine_state) {
  315. return std::nullopt;
  316. }
  317. const u64 key = (static_cast<u64>(bank) << 32) | static_cast<u64>(offset);
  318. auto it = maxwell3d->replace_table.find(key);
  319. if (it == maxwell3d->replace_table.end()) {
  320. return std::nullopt;
  321. }
  322. const auto converted_value = [](Tegra::Engines::Maxwell3D::HLEReplacementAttributeType name) {
  323. switch (name) {
  324. case Tegra::Engines::Maxwell3D::HLEReplacementAttributeType::BaseVertex:
  325. return Shader::ReplaceConstant::BaseVertex;
  326. case Tegra::Engines::Maxwell3D::HLEReplacementAttributeType::BaseInstance:
  327. return Shader::ReplaceConstant::BaseInstance;
  328. case Tegra::Engines::Maxwell3D::HLEReplacementAttributeType::DrawID:
  329. return Shader::ReplaceConstant::DrawID;
  330. default:
  331. UNREACHABLE();
  332. }
  333. }(it->second);
  334. cbuf_replacements.emplace(key, converted_value);
  335. return converted_value;
  336. }
  337. Shader::TextureType GraphicsEnvironment::ReadTextureType(u32 handle) {
  338. const auto& regs{maxwell3d->regs};
  339. const bool via_header_index{regs.sampler_binding == Maxwell::SamplerBinding::ViaHeaderBinding};
  340. auto entry =
  341. ReadTextureInfo(regs.tex_header.Address(), regs.tex_header.limit, via_header_index, handle);
  342. const Shader::TextureType result{ConvertTextureType(entry)};
  343. texture_types.emplace(handle, result);
  344. return result;
  345. }
  346. Shader::TexturePixelFormat GraphicsEnvironment::ReadTexturePixelFormat(u32 handle) {
  347. const auto& regs{maxwell3d->regs};
  348. const bool via_header_index{regs.sampler_binding == Maxwell::SamplerBinding::ViaHeaderBinding};
  349. auto entry =
  350. ReadTextureInfo(regs.tex_header.Address(), regs.tex_header.limit, via_header_index, handle);
  351. const Shader::TexturePixelFormat result(ConvertTexturePixelFormat(entry));
  352. texture_pixel_formats.emplace(handle, result);
  353. return result;
  354. }
  355. u32 GraphicsEnvironment::ReadViewportTransformState() {
  356. const auto& regs{maxwell3d->regs};
  357. viewport_transform_state = regs.viewport_scale_offset_enabled;
  358. return viewport_transform_state;
  359. }
  360. ComputeEnvironment::ComputeEnvironment(Tegra::Engines::KeplerCompute& kepler_compute_,
  361. Tegra::MemoryManager& gpu_memory_, GPUVAddr program_base_,
  362. u32 start_address_)
  363. : GenericEnvironment{gpu_memory_, program_base_, start_address_}, kepler_compute{
  364. &kepler_compute_} {
  365. const auto& qmd{kepler_compute->launch_description};
  366. stage = Shader::Stage::Compute;
  367. local_memory_size = qmd.local_pos_alloc + qmd.local_crs_alloc;
  368. texture_bound = kepler_compute->regs.tex_cb_index;
  369. is_propietary_driver = texture_bound == 2;
  370. shared_memory_size = qmd.shared_alloc;
  371. workgroup_size = {qmd.block_dim_x, qmd.block_dim_y, qmd.block_dim_z};
  372. }
  373. u32 ComputeEnvironment::ReadCbufValue(u32 cbuf_index, u32 cbuf_offset) {
  374. const auto& qmd{kepler_compute->launch_description};
  375. ASSERT(((qmd.const_buffer_enable_mask.Value() >> cbuf_index) & 1) != 0);
  376. const auto& cbuf{qmd.const_buffer_config[cbuf_index]};
  377. u32 value{};
  378. if (cbuf_offset < cbuf.size) {
  379. value = gpu_memory->Read<u32>(cbuf.Address() + cbuf_offset);
  380. }
  381. cbuf_values.emplace(MakeCbufKey(cbuf_index, cbuf_offset), value);
  382. return value;
  383. }
  384. Shader::TextureType ComputeEnvironment::ReadTextureType(u32 handle) {
  385. const auto& regs{kepler_compute->regs};
  386. const auto& qmd{kepler_compute->launch_description};
  387. auto entry = ReadTextureInfo(regs.tic.Address(), regs.tic.limit, qmd.linked_tsc != 0, handle);
  388. const Shader::TextureType result{ConvertTextureType(entry)};
  389. texture_types.emplace(handle, result);
  390. return result;
  391. }
  392. Shader::TexturePixelFormat ComputeEnvironment::ReadTexturePixelFormat(u32 handle) {
  393. const auto& regs{kepler_compute->regs};
  394. const auto& qmd{kepler_compute->launch_description};
  395. auto entry = ReadTextureInfo(regs.tic.Address(), regs.tic.limit, qmd.linked_tsc != 0, handle);
  396. const Shader::TexturePixelFormat result(ConvertTexturePixelFormat(entry));
  397. texture_pixel_formats.emplace(handle, result);
  398. return result;
  399. }
  400. u32 ComputeEnvironment::ReadViewportTransformState() {
  401. return viewport_transform_state;
  402. }
  403. void FileEnvironment::Deserialize(std::ifstream& file) {
  404. u64 code_size{};
  405. u64 num_texture_types{};
  406. u64 num_texture_pixel_formats{};
  407. u64 num_cbuf_values{};
  408. u64 num_cbuf_replacement_values{};
  409. file.read(reinterpret_cast<char*>(&code_size), sizeof(code_size))
  410. .read(reinterpret_cast<char*>(&num_texture_types), sizeof(num_texture_types))
  411. .read(reinterpret_cast<char*>(&num_texture_pixel_formats),
  412. sizeof(num_texture_pixel_formats))
  413. .read(reinterpret_cast<char*>(&num_cbuf_values), sizeof(num_cbuf_values))
  414. .read(reinterpret_cast<char*>(&num_cbuf_replacement_values),
  415. sizeof(num_cbuf_replacement_values))
  416. .read(reinterpret_cast<char*>(&local_memory_size), sizeof(local_memory_size))
  417. .read(reinterpret_cast<char*>(&texture_bound), sizeof(texture_bound))
  418. .read(reinterpret_cast<char*>(&start_address), sizeof(start_address))
  419. .read(reinterpret_cast<char*>(&read_lowest), sizeof(read_lowest))
  420. .read(reinterpret_cast<char*>(&read_highest), sizeof(read_highest))
  421. .read(reinterpret_cast<char*>(&viewport_transform_state), sizeof(viewport_transform_state))
  422. .read(reinterpret_cast<char*>(&stage), sizeof(stage));
  423. code = std::make_unique<u64[]>(Common::DivCeil(code_size, sizeof(u64)));
  424. file.read(reinterpret_cast<char*>(code.get()), code_size);
  425. for (size_t i = 0; i < num_texture_types; ++i) {
  426. u32 key;
  427. Shader::TextureType type;
  428. file.read(reinterpret_cast<char*>(&key), sizeof(key))
  429. .read(reinterpret_cast<char*>(&type), sizeof(type));
  430. texture_types.emplace(key, type);
  431. }
  432. for (size_t i = 0; i < num_texture_pixel_formats; ++i) {
  433. u32 key;
  434. Shader::TexturePixelFormat format;
  435. file.read(reinterpret_cast<char*>(&key), sizeof(key))
  436. .read(reinterpret_cast<char*>(&format), sizeof(format));
  437. texture_pixel_formats.emplace(key, format);
  438. }
  439. for (size_t i = 0; i < num_cbuf_values; ++i) {
  440. u64 key;
  441. u32 value;
  442. file.read(reinterpret_cast<char*>(&key), sizeof(key))
  443. .read(reinterpret_cast<char*>(&value), sizeof(value));
  444. cbuf_values.emplace(key, value);
  445. }
  446. for (size_t i = 0; i < num_cbuf_replacement_values; ++i) {
  447. u64 key;
  448. Shader::ReplaceConstant value;
  449. file.read(reinterpret_cast<char*>(&key), sizeof(key))
  450. .read(reinterpret_cast<char*>(&value), sizeof(value));
  451. cbuf_replacements.emplace(key, value);
  452. }
  453. if (stage == Shader::Stage::Compute) {
  454. file.read(reinterpret_cast<char*>(&workgroup_size), sizeof(workgroup_size))
  455. .read(reinterpret_cast<char*>(&shared_memory_size), sizeof(shared_memory_size));
  456. initial_offset = 0;
  457. } else {
  458. file.read(reinterpret_cast<char*>(&sph), sizeof(sph));
  459. initial_offset = sizeof(sph);
  460. if (stage == Shader::Stage::Geometry) {
  461. file.read(reinterpret_cast<char*>(&gp_passthrough_mask), sizeof(gp_passthrough_mask));
  462. }
  463. }
  464. is_propietary_driver = texture_bound == 2;
  465. }
  466. void FileEnvironment::Dump(u64 hash) {
  467. DumpImpl(hash, code.get(), read_highest, read_lowest, initial_offset, stage);
  468. }
  469. u64 FileEnvironment::ReadInstruction(u32 address) {
  470. if (address < read_lowest || address > read_highest) {
  471. throw Shader::LogicError("Out of bounds address {}", address);
  472. }
  473. return code[(address - read_lowest) / sizeof(u64)];
  474. }
  475. u32 FileEnvironment::ReadCbufValue(u32 cbuf_index, u32 cbuf_offset) {
  476. const auto it{cbuf_values.find(MakeCbufKey(cbuf_index, cbuf_offset))};
  477. if (it == cbuf_values.end()) {
  478. throw Shader::LogicError("Uncached read texture type");
  479. }
  480. return it->second;
  481. }
  482. Shader::TextureType FileEnvironment::ReadTextureType(u32 handle) {
  483. const auto it{texture_types.find(handle)};
  484. if (it == texture_types.end()) {
  485. throw Shader::LogicError("Uncached read texture type");
  486. }
  487. return it->second;
  488. }
  489. Shader::TexturePixelFormat FileEnvironment::ReadTexturePixelFormat(u32 handle) {
  490. const auto it{texture_pixel_formats.find(handle)};
  491. if (it == texture_pixel_formats.end()) {
  492. throw Shader::LogicError("Uncached read texture pixel format");
  493. }
  494. return it->second;
  495. }
  496. u32 FileEnvironment::ReadViewportTransformState() {
  497. return viewport_transform_state;
  498. }
  499. u32 FileEnvironment::LocalMemorySize() const {
  500. return local_memory_size;
  501. }
  502. u32 FileEnvironment::SharedMemorySize() const {
  503. return shared_memory_size;
  504. }
  505. u32 FileEnvironment::TextureBoundBuffer() const {
  506. return texture_bound;
  507. }
  508. std::array<u32, 3> FileEnvironment::WorkgroupSize() const {
  509. return workgroup_size;
  510. }
  511. std::optional<Shader::ReplaceConstant> FileEnvironment::GetReplaceConstBuffer(u32 bank,
  512. u32 offset) {
  513. const u64 key = (static_cast<u64>(bank) << 32) | static_cast<u64>(offset);
  514. auto it = cbuf_replacements.find(key);
  515. if (it == cbuf_replacements.end()) {
  516. return std::nullopt;
  517. }
  518. return it->second;
  519. }
  520. void SerializePipeline(std::span<const char> key, std::span<const GenericEnvironment* const> envs,
  521. const std::filesystem::path& filename, u32 cache_version) try {
  522. std::ofstream file(filename, std::ios::binary | std::ios::ate | std::ios::app);
  523. file.exceptions(std::ifstream::failbit);
  524. if (!file.is_open()) {
  525. LOG_ERROR(Common_Filesystem, "Failed to open pipeline cache file {}",
  526. Common::FS::PathToUTF8String(filename));
  527. return;
  528. }
  529. if (file.tellp() == 0) {
  530. // Write header
  531. file.write(MAGIC_NUMBER.data(), MAGIC_NUMBER.size())
  532. .write(reinterpret_cast<const char*>(&cache_version), sizeof(cache_version));
  533. }
  534. if (!std::ranges::all_of(envs, &GenericEnvironment::CanBeSerialized)) {
  535. return;
  536. }
  537. const u32 num_envs{static_cast<u32>(envs.size())};
  538. file.write(reinterpret_cast<const char*>(&num_envs), sizeof(num_envs));
  539. for (const GenericEnvironment* const env : envs) {
  540. env->Serialize(file);
  541. }
  542. file.write(key.data(), key.size_bytes());
  543. } catch (const std::ios_base::failure& e) {
  544. LOG_ERROR(Common_Filesystem, "{}", e.what());
  545. if (!Common::FS::RemoveFile(filename)) {
  546. LOG_ERROR(Common_Filesystem, "Failed to delete pipeline cache file {}",
  547. Common::FS::PathToUTF8String(filename));
  548. }
  549. }
  550. void LoadPipelines(
  551. std::stop_token stop_loading, const std::filesystem::path& filename, u32 expected_cache_version,
  552. Common::UniqueFunction<void, std::ifstream&, FileEnvironment> load_compute,
  553. Common::UniqueFunction<void, std::ifstream&, std::vector<FileEnvironment>> load_graphics) try {
  554. std::ifstream file(filename, std::ios::binary | std::ios::ate);
  555. if (!file.is_open()) {
  556. return;
  557. }
  558. file.exceptions(std::ifstream::failbit);
  559. const auto end{file.tellg()};
  560. file.seekg(0, std::ios::beg);
  561. std::array<char, 8> magic_number;
  562. u32 cache_version;
  563. file.read(magic_number.data(), magic_number.size())
  564. .read(reinterpret_cast<char*>(&cache_version), sizeof(cache_version));
  565. if (magic_number != MAGIC_NUMBER || cache_version != expected_cache_version) {
  566. file.close();
  567. if (Common::FS::RemoveFile(filename)) {
  568. if (magic_number != MAGIC_NUMBER) {
  569. LOG_ERROR(Common_Filesystem, "Invalid pipeline cache file");
  570. }
  571. if (cache_version != expected_cache_version) {
  572. LOG_INFO(Common_Filesystem, "Deleting old pipeline cache");
  573. }
  574. } else {
  575. LOG_ERROR(Common_Filesystem,
  576. "Invalid pipeline cache file and failed to delete it in \"{}\"",
  577. Common::FS::PathToUTF8String(filename));
  578. }
  579. return;
  580. }
  581. while (file.tellg() != end) {
  582. if (stop_loading.stop_requested()) {
  583. return;
  584. }
  585. u32 num_envs{};
  586. file.read(reinterpret_cast<char*>(&num_envs), sizeof(num_envs));
  587. std::vector<FileEnvironment> envs(num_envs);
  588. for (FileEnvironment& env : envs) {
  589. env.Deserialize(file);
  590. }
  591. if (envs.front().ShaderStage() == Shader::Stage::Compute) {
  592. load_compute(file, std::move(envs.front()));
  593. } else {
  594. load_graphics(file, std::move(envs));
  595. }
  596. }
  597. } catch (const std::ios_base::failure& e) {
  598. LOG_ERROR(Common_Filesystem, "{}", e.what());
  599. if (!Common::FS::RemoveFile(filename)) {
  600. LOG_ERROR(Common_Filesystem, "Failed to delete pipeline cache file {}",
  601. Common::FS::PathToUTF8String(filename));
  602. }
  603. }
  604. } // namespace VideoCommon