-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstants.py
More file actions
233 lines (209 loc) · 5.72 KB
/
constants.py
File metadata and controls
233 lines (209 loc) · 5.72 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import ast
import math
from dataclasses import dataclass
from functools import cache
BAD_MATH_AST_NODES = (
ast.BoolOp,
ast.NamedExpr,
ast.Lambda,
ast.IfExp,
ast.Dict,
ast.Set,
ast.ListComp,
ast.SetComp,
ast.DictComp,
ast.GeneratorExp,
ast.Await,
ast.Yield,
ast.YieldFrom,
ast.FormattedValue,
ast.JoinedStr,
ast.Attribute,
ast.Subscript,
ast.Starred,
ast.List,
ast.Tuple,
ast.Slice,
)
# same as BAD_MATH_AST_NODES but without tuple
BAD_VECTOR_MATH_AST_NODES = (
ast.BoolOp,
ast.NamedExpr,
ast.Lambda,
ast.IfExp,
ast.Dict,
ast.Set,
ast.ListComp,
ast.SetComp,
ast.DictComp,
ast.GeneratorExp,
ast.Await,
ast.Yield,
ast.YieldFrom,
ast.FormattedValue,
ast.JoinedStr,
ast.Attribute,
ast.Subscript,
ast.Starred,
ast.List,
ast.Slice,
)
TYPICAL_VAR_NAMES = ("x", "y", "z", "a", "b", "c")
@dataclass(frozen=True)
class Function:
input_nums: tuple[int, ...]
enum_value: str
printable_args: str | None = None
input_types: tuple[type, ...] | None = None
@cache
def __str__(self):
if self.printable_args is None:
# generate args from the number of inputs
s = ", ".join(
var
for var, _ in zip(
TYPICAL_VAR_NAMES,
range(self.input_nums[0]),
)
)
return f"({s})"
if "(" in self.printable_args:
return self.printable_args
else:
return f"({self.printable_args})"
PrintRepresent = str
def F(
nums: int | tuple[int, ...],
enum_value: str,
printable_args: str | None = None,
input_types: tuple[type, ...] | None = None,
):
if isinstance(nums, int):
nums = (nums,)
return Function(nums, enum_value, printable_args, input_types)
SHADER_MATH_CALLS: dict[str, dict[str, Function | PrintRepresent]] = {
"Functions": {
# ADD, SUBTRACT, MULTIPLY, DIVIDE have dedicated operators
"+": "x + y",
"-": "x - y",
"*": "x * y",
"/": "x / y",
# MULTIPLY_ADD: Special case (a * b + c) or (a + b * c),
# POWER has a dedicated operator
"**": "x ** y",
"log": F((1, 2), "LOGARITHM", "x[, base]"),
"sqrt": F(1, "SQRT"),
# INVERSE_SQRT: Special case ( 1 / sqrt(x) )
"abs": F(1, "ABSOLUTE"),
"exp": F(1, "EXPONENT", "(x) (or e ** x)"),
},
"Comparison": {
"min": F(2, "MINIMUM"),
"max": F(2, "MAXIMUM"),
# LESS_THAN, GREATER_THAN have dedicated operators
"<": "x < y",
">": "x > y",
"sign": F(1, "SIGN"),
"cmp": F(3, "COMPARE"),
"smin": F(3, "SMOOTH_MIN"),
"smax": F(3, "SMOOTH_MAX"),
},
"Rounding": {
"round": F(1, "ROUND"),
"floor": F(1, "FLOOR"),
"ceil": F(1, "CEIL"),
"trunc": F(1, "TRUNC"),
"int": F(1, "TRUNC"), # alias for truncate
"frac": F(1, "FRACT"),
# MODULO has a dedicated operator
"%": "x % y",
"fmod": F(2, "FLOORED_MODULO"),
"wrap": F(3, "WRAP"),
"snap": F(2, "SNAP"),
"pingpong": F(2, "PINGPONG"),
},
"Trigonometric": {
"sin": F(1, "SINE"),
"cos": F(1, "COSINE"),
"tan": F(1, "TANGENT"),
"asin": F(1, "ARCSINE"),
"acos": F(1, "ARCCOSINE"),
"atan": F(1, "ARCTANGENT"),
"atan2": F(2, "ARCTAN2"),
"sinh": F(1, "SINH"),
"cosh": F(1, "COSH"),
"tanh": F(1, "TANH"),
},
"Conversion": {
"rad": F(1, "RADIANS"),
"deg": F(1, "DEGREES"),
},
}
VALID_MATH_FUNCTIONS: dict[str, Function] = {
k: v for _, group in SHADER_MATH_CALLS.items() for k, v in group.items() if isinstance(v, Function)
}
VECTOR_MATH_CALLS: dict[str, dict[str, Function | PrintRepresent]] = {
"Functions": {
# ADD, SUBTRACT, MULTIPLY, DIVIDE have dedicated operators
"+": "x + y",
"-": "x - y",
"*": "x * y",
"/": "x / y",
# MULTIPLY_ADD: Special case (a * b + c) or (a + b * c),
},
"1": {
"cross": F((2,), "CROSS_PRODUCT"),
"project": F((2,), "PROJECT"),
"reflect": F((2,), "REFLECT"),
"refract": F((2, 3), "REFRACT", "x, y[, IOR]"),
"ffw": F((2, 3), "FACE_FORWARD", "x, y[, z]"),
"dot": F((2,), "DOT_PRODUCT"),
},
"Calculations": {
"dist": F((2,), "DISTANCE"),
"len": F((1,), "LENGTH"),
"scale": F((2,), "SCALE", "x, scale", (tuple, float)),
"normalize": F((1,), "NORMALIZE"),
},
"Wrappings": {
"abs": F((1,), "ABSOLUTE"),
"min": F((2,), "MINIMUM"),
"max": F((2,), "MAXIMUM"),
"floor": F((1,), "FLOOR"),
"ceil": F((1,), "CEIL"),
"frac": F((1,), "FRACTION"),
"%": "x % y",
"wrap": F((3,), "WRAP"),
"snap": F((2,), "SNAP"),
},
"Trigonometric": {
"sin": F((1,), "SINE"),
"cos": F((1,), "COSINE"),
"tan": F((1,), "TANGENT"),
},
}
SHADER_NODE_BASIC_OPS: dict[type[ast.operator], str] = {
ast.Add: "ADD",
ast.Sub: "SUBTRACT",
ast.Mult: "MULTIPLY",
ast.Div: "DIVIDE",
ast.Mod: "MODULO",
ast.Pow: "POWER",
# Floordiv is covered
# MatMult, LShift, RShift, BitOr, BitXor, BitAnd will not be implemented
}
SHADER_VECTOR_NODE_BASIC_OPS: dict[type[ast.operator], str] = {
# Add is covered
ast.Sub: "SUBTRACT",
# Mult is covered
# Div is covered
ast.Mod: "MODULO",
# Floordiv is covered
# Pow, MatMult, LShift, RShift, BitOr, BitXor, BitAnd will not be implemented
}
VARIABLE_NAME = str
ASSUMABLE_CONSTANTS = {
"pi": math.pi,
"e": math.e,
"tau": math.tau,
}