summaryrefslogtreecommitdiffstats
path: root/src/video_core/shader/expr.h
blob: 45695c0edcf1a48b890ae845decb8e4e1be5806e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2019 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include <memory>
#include <variant>

#include "video_core/engines/shader_bytecode.h"

namespace VideoCommon::Shader {

using Tegra::Shader::ConditionCode;
using Tegra::Shader::Pred;

class ExprAnd;
class ExprOr;
class ExprNot;
class ExprPredicate;
class ExprCondCode;
class ExprVar;
class ExprBoolean;

using ExprData =
    std::variant<ExprVar, ExprCondCode, ExprPredicate, ExprNot, ExprOr, ExprAnd, ExprBoolean>;
using Expr = std::shared_ptr<ExprData>;

class ExprAnd final {
public:
    explicit ExprAnd(Expr a, Expr b) : operand1{std::move(a)}, operand2{std::move(b)} {}

    bool operator==(const ExprAnd& b) const;
    bool operator!=(const ExprAnd& b) const;

    Expr operand1;
    Expr operand2;
};

class ExprOr final {
public:
    explicit ExprOr(Expr a, Expr b) : operand1{std::move(a)}, operand2{std::move(b)} {}

    bool operator==(const ExprOr& b) const;
    bool operator!=(const ExprOr& b) const;

    Expr operand1;
    Expr operand2;
};

class ExprNot final {
public:
    explicit ExprNot(Expr a) : operand1{std::move(a)} {}

    bool operator==(const ExprNot& b) const;
    bool operator!=(const ExprNot& b) const;

    Expr operand1;
};

class ExprVar final {
public:
    explicit ExprVar(u32 index) : var_index{index} {}

    bool operator==(const ExprVar& b) const {
        return var_index == b.var_index;
    }

    bool operator!=(const ExprVar& b) const {
        return !operator==(b);
    }

    u32 var_index;
};

class ExprPredicate final {
public:
    explicit ExprPredicate(u32 predicate) : predicate{predicate} {}

    bool operator==(const ExprPredicate& b) const {
        return predicate == b.predicate;
    }

    bool operator!=(const ExprPredicate& b) const {
        return !operator==(b);
    }

    u32 predicate;
};

class ExprCondCode final {
public:
    explicit ExprCondCode(ConditionCode cc) : cc{cc} {}

    bool operator==(const ExprCondCode& b) const {
        return cc == b.cc;
    }

    bool operator!=(const ExprCondCode& b) const {
        return !operator==(b);
    }

    ConditionCode cc;
};

class ExprBoolean final {
public:
    explicit ExprBoolean(bool val) : value{val} {}

    bool operator==(const ExprBoolean& b) const {
        return value == b.value;
    }

    bool operator!=(const ExprBoolean& b) const {
        return !operator==(b);
    }

    bool value;
};

template <typename T, typename... Args>
Expr MakeExpr(Args&&... args) {
    static_assert(std::is_convertible_v<T, ExprData>);
    return std::make_shared<ExprData>(T(std::forward<Args>(args)...));
}

bool ExprAreEqual(const Expr& first, const Expr& second);

bool ExprAreOpposite(const Expr& first, const Expr& second);

Expr MakeExprNot(Expr first);

Expr MakeExprAnd(Expr first, Expr second);

Expr MakeExprOr(Expr first, Expr second);

bool ExprIsTrue(const Expr& first);

} // namespace VideoCommon::Shader