control_flow.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <list>
  6. #include <optional>
  7. #include <set>
  8. #include <variant>
  9. #include "video_core/engines/shader_bytecode.h"
  10. #include "video_core/shader/ast.h"
  11. #include "video_core/shader/compiler_settings.h"
  12. #include "video_core/shader/shader_ir.h"
  13. namespace VideoCommon::Shader {
  14. using Tegra::Shader::ConditionCode;
  15. using Tegra::Shader::Pred;
  16. constexpr s32 exit_branch = -1;
  17. struct Condition {
  18. Pred predicate{Pred::UnusedIndex};
  19. ConditionCode cc{ConditionCode::T};
  20. bool IsUnconditional() const {
  21. return predicate == Pred::UnusedIndex && cc == ConditionCode::T;
  22. }
  23. bool operator==(const Condition& other) const {
  24. return std::tie(predicate, cc) == std::tie(other.predicate, other.cc);
  25. }
  26. bool operator!=(const Condition& other) const {
  27. return !operator==(other);
  28. }
  29. };
  30. class SingleBranch {
  31. public:
  32. SingleBranch() = default;
  33. SingleBranch(Condition condition, s32 address, bool kill, bool is_sync, bool is_brk,
  34. bool ignore)
  35. : condition{condition}, address{address}, kill{kill}, is_sync{is_sync}, is_brk{is_brk},
  36. ignore{ignore} {}
  37. bool operator==(const SingleBranch& b) const {
  38. return std::tie(condition, address, kill, is_sync, is_brk, ignore) ==
  39. std::tie(b.condition, b.address, b.kill, b.is_sync, b.is_brk, b.ignore);
  40. }
  41. bool operator!=(const SingleBranch& b) const {
  42. return !operator==(b);
  43. }
  44. Condition condition{};
  45. s32 address{exit_branch};
  46. bool kill{};
  47. bool is_sync{};
  48. bool is_brk{};
  49. bool ignore{};
  50. };
  51. struct CaseBranch {
  52. CaseBranch(u32 cmp_value, u32 address) : cmp_value{cmp_value}, address{address} {}
  53. u32 cmp_value;
  54. u32 address;
  55. };
  56. class MultiBranch {
  57. public:
  58. MultiBranch(u32 gpr, std::vector<CaseBranch>&& branches)
  59. : gpr{gpr}, branches{std::move(branches)} {}
  60. u32 gpr{};
  61. std::vector<CaseBranch> branches{};
  62. };
  63. using BranchData = std::variant<SingleBranch, MultiBranch>;
  64. using BlockBranchInfo = std::shared_ptr<BranchData>;
  65. bool BlockBranchInfoAreEqual(BlockBranchInfo first, BlockBranchInfo second);
  66. struct ShaderBlock {
  67. u32 start{};
  68. u32 end{};
  69. bool ignore_branch{};
  70. BlockBranchInfo branch{};
  71. bool operator==(const ShaderBlock& sb) const {
  72. return std::tie(start, end, ignore_branch) ==
  73. std::tie(sb.start, sb.end, sb.ignore_branch) &&
  74. BlockBranchInfoAreEqual(branch, sb.branch);
  75. }
  76. bool operator!=(const ShaderBlock& sb) const {
  77. return !operator==(sb);
  78. }
  79. };
  80. struct ShaderCharacteristics {
  81. std::list<ShaderBlock> blocks{};
  82. std::set<u32> labels{};
  83. u32 start{};
  84. u32 end{};
  85. ASTManager manager{true, true};
  86. CompilerSettings settings{};
  87. };
  88. std::unique_ptr<ShaderCharacteristics> ScanFlow(const ProgramCode& program_code, u32 start_address,
  89. const CompilerSettings& settings,
  90. ConstBufferLocker& locker);
  91. } // namespace VideoCommon::Shader