main.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <chrono>
  5. #include <filesystem>
  6. #include <fmt/format.h>
  7. #include "shader_recompiler/backend/spirv/emit_spirv.h"
  8. #include "shader_recompiler/file_environment.h"
  9. #include "shader_recompiler/frontend/ir/basic_block.h"
  10. #include "shader_recompiler/frontend/ir/ir_emitter.h"
  11. #include "shader_recompiler/frontend/maxwell/control_flow.h"
  12. #include "shader_recompiler/frontend/maxwell/decode.h"
  13. #include "shader_recompiler/frontend/maxwell/location.h"
  14. #include "shader_recompiler/frontend/maxwell/program.h"
  15. #include "shader_recompiler/frontend/maxwell/translate/translate.h"
  16. using namespace Shader;
  17. using namespace Shader::Maxwell;
  18. template <typename Func>
  19. static void ForEachFile(const std::filesystem::path& path, Func&& func) {
  20. std::filesystem::directory_iterator end;
  21. for (std::filesystem::directory_iterator it{path}; it != end; ++it) {
  22. if (std::filesystem::is_directory(*it)) {
  23. ForEachFile(*it, func);
  24. } else {
  25. func(*it);
  26. }
  27. }
  28. }
  29. void RunDatabase() {
  30. std::vector<std::unique_ptr<FileEnvironment>> map;
  31. ForEachFile("D:\\Shaders\\Database", [&](const std::filesystem::path& path) {
  32. map.emplace_back(std::make_unique<FileEnvironment>(path.string().c_str()));
  33. });
  34. ObjectPool<Flow::Block> block_pool;
  35. using namespace std::chrono;
  36. auto t0 = high_resolution_clock::now();
  37. int N = 1;
  38. int n = 0;
  39. for (int i = 0; i < N; ++i) {
  40. for (auto& env : map) {
  41. ++n;
  42. // fmt::print(stdout, "Decoding {}\n", path.string());
  43. const Location start_address{0};
  44. block_pool.ReleaseContents();
  45. Flow::CFG cfg{*env, block_pool, start_address};
  46. // fmt::print(stdout, "{}\n", cfg->Dot());
  47. // IR::Program program{env, cfg};
  48. // Optimize(program);
  49. // const std::string code{EmitGLASM(program)};
  50. }
  51. }
  52. auto t = high_resolution_clock::now();
  53. fmt::print(stdout, "{} ms", duration_cast<milliseconds>(t - t0).count() / double(N));
  54. }
  55. static constexpr Profile PROFILE{
  56. .unified_descriptor_binding = true,
  57. .support_float_controls = true,
  58. .support_separate_denorm_behavior = true,
  59. .support_separate_rounding_mode = true,
  60. .support_fp16_denorm_preserve = true,
  61. .support_fp32_denorm_preserve = true,
  62. .support_fp16_denorm_flush = true,
  63. .support_fp32_denorm_flush = true,
  64. };
  65. int main() {
  66. // RunDatabase();
  67. ObjectPool<Flow::Block> flow_block_pool;
  68. ObjectPool<IR::Inst> inst_pool;
  69. ObjectPool<IR::Block> block_pool;
  70. // FileEnvironment env{"D:\\Shaders\\Database\\Oninaki\\CS8F146B41DB6BD826.bin"};
  71. FileEnvironment env{"D:\\Shaders\\shader.bin"};
  72. block_pool.ReleaseContents();
  73. inst_pool.ReleaseContents();
  74. flow_block_pool.ReleaseContents();
  75. Flow::CFG cfg{env, flow_block_pool, 0};
  76. fmt::print(stdout, "{}\n", cfg.Dot());
  77. IR::Program program{TranslateProgram(inst_pool, block_pool, env, cfg)};
  78. fmt::print(stdout, "{}\n", IR::DumpProgram(program));
  79. const std::vector<u32> spirv{Backend::SPIRV::EmitSPIRV(PROFILE, env, program)};
  80. std::FILE* const file{std::fopen("D:\\shader.spv", "wb")};
  81. std::fwrite(spirv.data(), spirv.size(), sizeof(u32), file);
  82. std::fclose(file);
  83. std::system("spirv-dis D:\\shader.spv");
  84. }