diff options
| author | 2021-11-17 04:19:29 +0100 | |
|---|---|---|
| committer | 2022-01-04 02:39:00 +0100 | |
| commit | f58ee3f15f7427a8b834286384931bcf821ed771 (patch) | |
| tree | 1ef8367f64cbee6220c4e5600249697e5cd8e826 /src/video_core/shader_environment.cpp | |
| parent | Merge pull request #7648 from bunnei/thread-pinning (diff) | |
| download | yuzu-f58ee3f15f7427a8b834286384931bcf821ed771.tar.gz yuzu-f58ee3f15f7427a8b834286384931bcf821ed771.tar.xz yuzu-f58ee3f15f7427a8b834286384931bcf821ed771.zip | |
ShaderDecompiler: Add a debug option to dump the game's shaders.
Diffstat (limited to 'src/video_core/shader_environment.cpp')
| -rw-r--r-- | src/video_core/shader_environment.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/video_core/shader_environment.cpp b/src/video_core/shader_environment.cpp index 05850afd0..7d3ae0de4 100644 --- a/src/video_core/shader_environment.cpp +++ b/src/video_core/shader_environment.cpp | |||
| @@ -3,6 +3,7 @@ | |||
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <algorithm> | 5 | #include <algorithm> |
| 6 | #include <bit> | ||
| 6 | #include <filesystem> | 7 | #include <filesystem> |
| 7 | #include <fstream> | 8 | #include <fstream> |
| 8 | #include <memory> | 9 | #include <memory> |
| @@ -14,6 +15,7 @@ | |||
| 14 | #include "common/common_types.h" | 15 | #include "common/common_types.h" |
| 15 | #include "common/div_ceil.h" | 16 | #include "common/div_ceil.h" |
| 16 | #include "common/fs/fs.h" | 17 | #include "common/fs/fs.h" |
| 18 | #include "common/fs/path_util.h" | ||
| 17 | #include "common/logging/log.h" | 19 | #include "common/logging/log.h" |
| 18 | #include "shader_recompiler/environment.h" | 20 | #include "shader_recompiler/environment.h" |
| 19 | #include "video_core/engines/kepler_compute.h" | 21 | #include "video_core/engines/kepler_compute.h" |
| @@ -57,6 +59,47 @@ static Shader::TextureType ConvertType(const Tegra::Texture::TICEntry& entry) { | |||
| 57 | } | 59 | } |
| 58 | } | 60 | } |
| 59 | 61 | ||
| 62 | static std::string_view StageToPrefix(Shader::Stage stage) { | ||
| 63 | switch (stage) { | ||
| 64 | case Shader::Stage::VertexB: | ||
| 65 | return "VB"; | ||
| 66 | case Shader::Stage::TessellationControl: | ||
| 67 | return "TC"; | ||
| 68 | case Shader::Stage::TessellationEval: | ||
| 69 | return "TE"; | ||
| 70 | case Shader::Stage::Geometry: | ||
| 71 | return "GS"; | ||
| 72 | case Shader::Stage::Fragment: | ||
| 73 | return "FS"; | ||
| 74 | case Shader::Stage::Compute: | ||
| 75 | return "CS"; | ||
| 76 | case Shader::Stage::VertexA: | ||
| 77 | return "VA"; | ||
| 78 | default: | ||
| 79 | return "UK"; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | static void DumpImpl(u64 hash, const u64* code, u32 read_highest, u32 read_lowest, | ||
| 84 | u32 initial_offset, Shader::Stage stage) { | ||
| 85 | const auto shader_dir{Common::FS::GetYuzuPath(Common::FS::YuzuPath::DumpDir)}; | ||
| 86 | const auto base_dir{shader_dir / "shaders"}; | ||
| 87 | if (!Common::FS::CreateDir(shader_dir) || !Common::FS::CreateDir(base_dir)) { | ||
| 88 | LOG_ERROR(Common_Filesystem, "Failed to create shader dump directories"); | ||
| 89 | return; | ||
| 90 | } | ||
| 91 | const auto prefix = StageToPrefix(stage); | ||
| 92 | const auto name{base_dir / fmt::format("{}{:016x}.ash", prefix, hash)}; | ||
| 93 | const size_t real_size = read_highest - read_lowest + initial_offset; | ||
| 94 | const size_t padding_needed = ((32 - (real_size % 32)) % 32); | ||
| 95 | std::fstream shader_file(name, std::ios::out | std::ios::binary); | ||
| 96 | const size_t jump_index = initial_offset / sizeof(u64); | ||
| 97 | shader_file.write(reinterpret_cast<const char*>(code + jump_index), real_size); | ||
| 98 | for (size_t i = 0; i < padding_needed; i++) { | ||
| 99 | shader_file.put(0); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 60 | GenericEnvironment::GenericEnvironment(Tegra::MemoryManager& gpu_memory_, GPUVAddr program_base_, | 103 | GenericEnvironment::GenericEnvironment(Tegra::MemoryManager& gpu_memory_, GPUVAddr program_base_, |
| 61 | u32 start_address_) | 104 | u32 start_address_) |
| 62 | : gpu_memory{&gpu_memory_}, program_base{program_base_} { | 105 | : gpu_memory{&gpu_memory_}, program_base{program_base_} { |
| @@ -128,6 +171,10 @@ u64 GenericEnvironment::CalculateHash() const { | |||
| 128 | return Common::CityHash64(data.get(), size); | 171 | return Common::CityHash64(data.get(), size); |
| 129 | } | 172 | } |
| 130 | 173 | ||
| 174 | void GenericEnvironment::Dump(u64 hash) { | ||
| 175 | DumpImpl(hash, code.data(), read_highest, read_lowest, initial_offset, stage); | ||
| 176 | } | ||
| 177 | |||
| 131 | void GenericEnvironment::Serialize(std::ofstream& file) const { | 178 | void GenericEnvironment::Serialize(std::ofstream& file) const { |
| 132 | const u64 code_size{static_cast<u64>(CachedSize())}; | 179 | const u64 code_size{static_cast<u64>(CachedSize())}; |
| 133 | const u64 num_texture_types{static_cast<u64>(texture_types.size())}; | 180 | const u64 num_texture_types{static_cast<u64>(texture_types.size())}; |
| @@ -207,6 +254,7 @@ GraphicsEnvironment::GraphicsEnvironment(Tegra::Engines::Maxwell3D& maxwell3d_, | |||
| 207 | u32 start_address_) | 254 | u32 start_address_) |
| 208 | : GenericEnvironment{gpu_memory_, program_base_, start_address_}, maxwell3d{&maxwell3d_} { | 255 | : GenericEnvironment{gpu_memory_, program_base_, start_address_}, maxwell3d{&maxwell3d_} { |
| 209 | gpu_memory->ReadBlock(program_base + start_address, &sph, sizeof(sph)); | 256 | gpu_memory->ReadBlock(program_base + start_address, &sph, sizeof(sph)); |
| 257 | initial_offset = sizeof(sph); | ||
| 210 | gp_passthrough_mask = maxwell3d->regs.gp_passthrough_mask; | 258 | gp_passthrough_mask = maxwell3d->regs.gp_passthrough_mask; |
| 211 | switch (program) { | 259 | switch (program) { |
| 212 | case Maxwell::ShaderProgram::VertexA: | 260 | case Maxwell::ShaderProgram::VertexA: |
| @@ -323,14 +371,20 @@ void FileEnvironment::Deserialize(std::ifstream& file) { | |||
| 323 | if (stage == Shader::Stage::Compute) { | 371 | if (stage == Shader::Stage::Compute) { |
| 324 | file.read(reinterpret_cast<char*>(&workgroup_size), sizeof(workgroup_size)) | 372 | file.read(reinterpret_cast<char*>(&workgroup_size), sizeof(workgroup_size)) |
| 325 | .read(reinterpret_cast<char*>(&shared_memory_size), sizeof(shared_memory_size)); | 373 | .read(reinterpret_cast<char*>(&shared_memory_size), sizeof(shared_memory_size)); |
| 374 | initial_offset = 0; | ||
| 326 | } else { | 375 | } else { |
| 327 | file.read(reinterpret_cast<char*>(&sph), sizeof(sph)); | 376 | file.read(reinterpret_cast<char*>(&sph), sizeof(sph)); |
| 377 | initial_offset = sizeof(sph); | ||
| 328 | if (stage == Shader::Stage::Geometry) { | 378 | if (stage == Shader::Stage::Geometry) { |
| 329 | file.read(reinterpret_cast<char*>(&gp_passthrough_mask), sizeof(gp_passthrough_mask)); | 379 | file.read(reinterpret_cast<char*>(&gp_passthrough_mask), sizeof(gp_passthrough_mask)); |
| 330 | } | 380 | } |
| 331 | } | 381 | } |
| 332 | } | 382 | } |
| 333 | 383 | ||
| 384 | void FileEnvironment::Dump(u64 [[maybe_unused]] hash) { | ||
| 385 | DumpImpl(hash, code.get(), read_highest, read_lowest, initial_offset, stage); | ||
| 386 | } | ||
| 387 | |||
| 334 | u64 FileEnvironment::ReadInstruction(u32 address) { | 388 | u64 FileEnvironment::ReadInstruction(u32 address) { |
| 335 | if (address < read_lowest || address > read_highest) { | 389 | if (address < read_lowest || address > read_highest) { |
| 336 | throw Shader::LogicError("Out of bounds address {}", address); | 390 | throw Shader::LogicError("Out of bounds address {}", address); |