expr.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <memory>
  5. #include <variant>
  6. #include "video_core/shader/expr.h"
  7. namespace VideoCommon::Shader {
  8. namespace {
  9. bool ExprIsBoolean(const Expr& expr) {
  10. return std::holds_alternative<ExprBoolean>(*expr);
  11. }
  12. bool ExprBooleanGet(const Expr& expr) {
  13. return std::get_if<ExprBoolean>(expr.get())->value;
  14. }
  15. } // Anonymous namespace
  16. bool ExprAnd::operator==(const ExprAnd& b) const {
  17. return (*operand1 == *b.operand1) && (*operand2 == *b.operand2);
  18. }
  19. bool ExprAnd::operator!=(const ExprAnd& b) const {
  20. return !operator==(b);
  21. }
  22. bool ExprOr::operator==(const ExprOr& b) const {
  23. return (*operand1 == *b.operand1) && (*operand2 == *b.operand2);
  24. }
  25. bool ExprOr::operator!=(const ExprOr& b) const {
  26. return !operator==(b);
  27. }
  28. bool ExprNot::operator==(const ExprNot& b) const {
  29. return *operand1 == *b.operand1;
  30. }
  31. bool ExprNot::operator!=(const ExprNot& b) const {
  32. return !operator==(b);
  33. }
  34. Expr MakeExprNot(Expr first) {
  35. if (std::holds_alternative<ExprNot>(*first)) {
  36. return std::get_if<ExprNot>(first.get())->operand1;
  37. }
  38. return MakeExpr<ExprNot>(std::move(first));
  39. }
  40. Expr MakeExprAnd(Expr first, Expr second) {
  41. if (ExprIsBoolean(first)) {
  42. return ExprBooleanGet(first) ? second : first;
  43. }
  44. if (ExprIsBoolean(second)) {
  45. return ExprBooleanGet(second) ? first : second;
  46. }
  47. return MakeExpr<ExprAnd>(std::move(first), std::move(second));
  48. }
  49. Expr MakeExprOr(Expr first, Expr second) {
  50. if (ExprIsBoolean(first)) {
  51. return ExprBooleanGet(first) ? first : second;
  52. }
  53. if (ExprIsBoolean(second)) {
  54. return ExprBooleanGet(second) ? second : first;
  55. }
  56. return MakeExpr<ExprOr>(std::move(first), std::move(second));
  57. }
  58. bool ExprAreEqual(const Expr& first, const Expr& second) {
  59. return (*first) == (*second);
  60. }
  61. bool ExprAreOpposite(const Expr& first, const Expr& second) {
  62. if (std::holds_alternative<ExprNot>(*first)) {
  63. return ExprAreEqual(std::get_if<ExprNot>(first.get())->operand1, second);
  64. }
  65. if (std::holds_alternative<ExprNot>(*second)) {
  66. return ExprAreEqual(std::get_if<ExprNot>(second.get())->operand1, first);
  67. }
  68. return false;
  69. }
  70. bool ExprIsTrue(const Expr& first) {
  71. if (ExprIsBoolean(first)) {
  72. return ExprBooleanGet(first);
  73. }
  74. return false;
  75. }
  76. } // namespace VideoCommon::Shader