track.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <utility>
  6. #include <variant>
  7. #include "common/common_types.h"
  8. #include "video_core/shader/shader_ir.h"
  9. namespace VideoCommon::Shader {
  10. namespace {
  11. std::pair<Node, s64> FindOperation(const NodeBlock& code, s64 cursor,
  12. OperationCode operation_code) {
  13. for (; cursor >= 0; --cursor) {
  14. const Node node = code.at(cursor);
  15. if (const auto operation = std::get_if<OperationNode>(&*node)) {
  16. if (operation->GetCode() == operation_code) {
  17. return {node, cursor};
  18. }
  19. }
  20. if (const auto conditional = std::get_if<ConditionalNode>(&*node)) {
  21. const auto& conditional_code = conditional->GetCode();
  22. const auto [found, internal_cursor] = FindOperation(
  23. conditional_code, static_cast<s64>(conditional_code.size() - 1), operation_code);
  24. if (found) {
  25. return {found, cursor};
  26. }
  27. }
  28. }
  29. return {};
  30. }
  31. } // Anonymous namespace
  32. std::tuple<Node, u32, u32> ShaderIR::TrackCbuf(Node tracked, const NodeBlock& code,
  33. s64 cursor) const {
  34. if (const auto cbuf = std::get_if<CbufNode>(&*tracked)) {
  35. // Constant buffer found, test if it's an immediate
  36. const auto offset = cbuf->GetOffset();
  37. if (const auto immediate = std::get_if<ImmediateNode>(&*offset)) {
  38. return {tracked, cbuf->GetIndex(), immediate->GetValue()};
  39. }
  40. return {};
  41. }
  42. if (const auto gpr = std::get_if<GprNode>(&*tracked)) {
  43. if (gpr->GetIndex() == Tegra::Shader::Register::ZeroIndex) {
  44. return {};
  45. }
  46. // Reduce the cursor in one to avoid infinite loops when the instruction sets the same
  47. // register that it uses as operand
  48. const auto [source, new_cursor] = TrackRegister(gpr, code, cursor - 1);
  49. if (!source) {
  50. return {};
  51. }
  52. return TrackCbuf(source, code, new_cursor);
  53. }
  54. if (const auto operation = std::get_if<OperationNode>(&*tracked)) {
  55. for (std::size_t i = 0; i < operation->GetOperandsCount(); ++i) {
  56. if (auto found = TrackCbuf((*operation)[i], code, cursor); std::get<0>(found)) {
  57. // Cbuf found in operand.
  58. return found;
  59. }
  60. }
  61. return {};
  62. }
  63. if (const auto conditional = std::get_if<ConditionalNode>(&*tracked)) {
  64. const auto& conditional_code = conditional->GetCode();
  65. return TrackCbuf(tracked, conditional_code, static_cast<s64>(conditional_code.size()));
  66. }
  67. return {};
  68. }
  69. std::optional<u32> ShaderIR::TrackImmediate(Node tracked, const NodeBlock& code, s64 cursor) const {
  70. // Reduce the cursor in one to avoid infinite loops when the instruction sets the same register
  71. // that it uses as operand
  72. const auto [found, found_cursor] =
  73. TrackRegister(&std::get<GprNode>(*tracked), code, cursor - 1);
  74. if (!found) {
  75. return {};
  76. }
  77. if (const auto immediate = std::get_if<ImmediateNode>(&*found)) {
  78. return immediate->GetValue();
  79. }
  80. return {};
  81. }
  82. std::pair<Node, s64> ShaderIR::TrackRegister(const GprNode* tracked, const NodeBlock& code,
  83. s64 cursor) const {
  84. for (; cursor >= 0; --cursor) {
  85. const auto [found_node, new_cursor] = FindOperation(code, cursor, OperationCode::Assign);
  86. if (!found_node) {
  87. return {};
  88. }
  89. const auto operation = std::get_if<OperationNode>(&*found_node);
  90. ASSERT(operation);
  91. const auto& target = (*operation)[0];
  92. if (const auto gpr_target = std::get_if<GprNode>(&*target)) {
  93. if (gpr_target->GetIndex() == tracked->GetIndex()) {
  94. return {(*operation)[1], new_cursor};
  95. }
  96. }
  97. }
  98. return {};
  99. }
  100. } // namespace VideoCommon::Shader