-
Notifications
You must be signed in to change notification settings - Fork 5
/
OpcodeBaseTypes.py
296 lines (226 loc) · 10.3 KB
/
OpcodeBaseTypes.py
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
class Opcode():
def __init__(self, OSO, index):
self.OSO = OSO
self.InstructionIndex = index
self.Instuction = self.OSO.Instructions[index]
self.Tag = self.OSO.Instructions[index].Tag
def Instruction(self):
return self.Instruction
def InstructionIndex(self):
return self.InstructionIndex
def NextIndex(self):
return self.InstructionIndex + 1
def Tag(self):
return self.Tag
def Generate(self, nodeGraph):
print("Generate not implemented for %s" % self.Instuction.Opcode)
# Destination Source
class Opcode_DS(Opcode):
def __init__(self, OSO, index):
Opcode.__init__(self, OSO, index)
self.Destination = OSO.GetVariable(self.Instuction.Parameters[0])
self.Source = OSO.GetVariable(self.Instuction.Parameters[1])
def Destination(self):
return self.Destination
def Source(self):
return self.Source
class Opcode_D(Opcode):
def __init__(self, OSO, index):
Opcode.__init__(self, OSO, index)
self.Destination = OSO.GetVariable(self.Instuction.Parameters[0])
def Destination(self):
return self.Destination
# Destination Source1 Source2
class Opcode_DSS(Opcode):
def __init__(self, OSO, index):
Opcode.__init__(self, OSO, index)
self.Destination = OSO.GetVariable(self.Instuction.Parameters[0])
self.Source1 = OSO.GetVariable(self.Instuction.Parameters[1])
self.Source2 = OSO.GetVariable(self.Instuction.Parameters[2])
def Destination(self):
return self.Destination
def Source1(self):
return self.Source1
def Source2(self):
return self.Source2
class Opcode_SDD(Opcode):
def __init__(self, OSO, index):
Opcode.__init__(self, OSO, index)
self.Source = OSO.GetVariable(self.Instuction.Parameters[0])
self.Destination1 = OSO.GetVariable(self.Instuction.Parameters[1])
self.Destination2 = OSO.GetVariable(self.Instuction.Parameters[2])
def Destination1(self):
return self.Destination1
def Destination2(self):
return self.Destination2
def Source(self):
return self.Source
# Destination Source1 Source2
class Opcode_DSSS(Opcode):
def __init__(self, OSO, index):
Opcode.__init__(self, OSO, index)
self.Destination = OSO.GetVariable(self.Instuction.Parameters[0])
self.Source1 = OSO.GetVariable(self.Instuction.Parameters[1])
self.Source2 = OSO.GetVariable(self.Instuction.Parameters[2])
self.Source3 = OSO.GetVariable(self.Instuction.Parameters[3])
def Destination(self):
return self.Destination
def Source1(self):
return self.Source1
def Source2(self):
return self.Source2
def Source3(self):
return self.Source3
# Destination Source1 index
class Opcode_DSI(Opcode):
def __init__(self, OSO, index):
Opcode.__init__(self, OSO, index)
self.Destination = OSO.GetVariable(self.Instuction.Parameters[0])
self.Source = OSO.GetVariable(self.Instuction.Parameters[1])
self.Index = OSO.GetVariable(self.Instuction.Parameters[2])
def Destination(self):
return self.Destination
def Source(self):
return self.Source1
def Index(self):
return self.Index
class Opcode_DIS(Opcode):
def __init__(self, OSO, index):
Opcode.__init__(self, OSO, index)
self.Destination = OSO.GetVariable(self.Instuction.Parameters[0])
self.Index = OSO.GetVariable(self.Instuction.Parameters[1])
self.Source = OSO.GetVariable(self.Instuction.Parameters[2])
def Destination(self):
return self.Destination
def Source(self):
return self.Source
def Index(self):
return self.Index
class Opcode_basicMath(Opcode_DSS):
def __init__(self, OSO, index, operation):
Opcode_DSS.__init__(self, OSO, index)
self.Operation = operation
def GenerateFloatFloat(self, nodeGraph):
node = nodeGraph.CreateNode("ShaderNodeMath")
node.SetProperty("operation", self.Operation)
nodeGraph.AddLink(node, 0, self.Source1)
nodeGraph.AddLink(node, 1, self.Source2)
nodeGraph.SetVar(self.Destination, node, 0)
def GeneratePointPoint(self, nodeGraph):
node = nodeGraph.CreateNode("ShaderNodeVectorMath")
if self.Operation in ['ADD', 'SUBTRACT']:
node.SetProperty("operation", self.Operation)
nodeGraph.AddLink(node, 0, self.Source1)
nodeGraph.AddLink(node, 1, self.Source2)
nodeGraph.SetVar(self.Destination, node, 0)
elif self.Operation in ["MULTIPLY" ,"DIVIDE","MODULO"]:
node1 = nodeGraph.CreateNode('ShaderNodeSeparateXYZ')
nodeGraph.AddLink(node1, 0, self.Source1)
node2 = nodeGraph.CreateNode('ShaderNodeSeparateXYZ')
nodeGraph.AddLink(node2, 0, self.Source2)
nodex = nodeGraph.CreateNode("ShaderNodeMath")
nodex.SetProperty("operation", self.Operation)
nodeGraph.AddNodeLink(nodex, 0, node1, 0)
nodeGraph.AddNodeLink(nodex, 1, node2, 0)
nodey = nodeGraph.CreateNode("ShaderNodeMath")
nodey.SetProperty("operation", self.Operation)
nodeGraph.AddNodeLink(nodey, 0, node1, 1)
nodeGraph.AddNodeLink(nodey, 1, node2, 1)
nodez = nodeGraph.CreateNode("ShaderNodeMath")
nodez.SetProperty("operation", self.Operation)
nodeGraph.AddNodeLink(nodez, 0, node1, 2)
nodeGraph.AddNodeLink(nodez, 1, node2, 2)
nodeOut = nodeGraph.CreateNode('ShaderNodeCombineXYZ')
nodeGraph.AddNodeLink(nodeOut, 0, nodex, 0)
nodeGraph.AddNodeLink(nodeOut, 1, nodey, 0)
nodeGraph.AddNodeLink(nodeOut, 2, nodez, 0)
nodeGraph.SetVar(self.Destination, nodeOut, 0)
else:
raise ValueError(str('Unsupported math operation %s %s %s' % (
self.Source1.dataType, self.Operation, self.Source2.dataType)))
def GeneratePointFloat(self, nodeGraph, vec, flt):
node = nodeGraph.CreateNode('ShaderNodeSeparateXYZ')
nodeGraph.AddLink(node, 0, vec)
nodex = nodeGraph.CreateNode("ShaderNodeMath")
nodex.SetProperty("operation", self.Operation)
nodeGraph.AddNodeLink(nodex, 0, node, 0)
nodeGraph.AddLink(nodex, 1, flt)
nodey = nodeGraph.CreateNode("ShaderNodeMath")
nodey.SetProperty("operation", self.Operation)
nodeGraph.AddNodeLink(nodey, 0, node, 1)
nodeGraph.AddLink(nodey, 1, flt)
nodez = nodeGraph.CreateNode("ShaderNodeMath")
nodez.SetProperty("operation", self.Operation)
nodeGraph.AddNodeLink(nodez, 0, node, 2)
nodeGraph.AddLink(nodez, 1, flt)
nodeOut = nodeGraph.CreateNode('ShaderNodeCombineXYZ')
nodeGraph.AddNodeLink(nodeOut, 0, nodex, 0)
nodeGraph.AddNodeLink(nodeOut, 1, nodey, 0)
nodeGraph.AddNodeLink(nodeOut, 2, nodez, 0)
nodeGraph.SetVar(self.Destination, nodeOut, 0)
def GenerateFloatPoint(self, nodeGraph):
node = nodeGraph.CreateNode('ShaderNodeSeparateXYZ')
nodeGraph.AddLink(node, 0, self.Source2)
nodex = nodeGraph.CreateNode("ShaderNodeMath")
nodex.SetProperty("operation", self.Operation)
nodeGraph.AddNodeLink(nodex, 1, node, 0)
nodeGraph.AddLink(nodex, 0, self.Source1)
nodey = nodeGraph.CreateNode("ShaderNodeMath")
nodey.SetProperty("operation", self.Operation)
nodeGraph.AddNodeLink(nodey, 1, node, 1)
nodeGraph.AddLink(nodey, 0, self.Source1)
nodez = nodeGraph.CreateNode("ShaderNodeMath")
nodez.SetProperty("operation", self.Operation)
nodeGraph.AddNodeLink(nodez, 1, node, 2)
nodeGraph.AddLink(nodez, 0, self.Source1)
nodeOut = nodeGraph.CreateNode('ShaderNodeCombineXYZ')
nodeGraph.AddNodeLink(nodeOut, 0, nodex, 0)
nodeGraph.AddNodeLink(nodeOut, 1, nodey, 0)
nodeGraph.AddNodeLink(nodeOut, 2, nodez, 0)
nodeGraph.SetVar(self.Destination, nodeOut, 0)
def Generate(self, nodeGraph):
if self.Source1.IsNumeric() and self.Source2.IsNumeric():
self.GenerateFloatFloat(nodeGraph)
elif self.Source1.IsNumeric() and self.Source2.IsPointLike():
self.GenerateFloatPoint(nodeGraph)
elif self.Source1.IsPointLike() and self.Source2.IsNumeric():
self.GeneratePointFloat(nodeGraph, self.Source1, self.Source2)
elif self.Source1.IsPointLike() and self.Source2.IsPointLike():
self.GeneratePointPoint(nodeGraph)
else:
raise ValueError(str('Unsupported math operation %s %s %s' % (
self.Source1.dataType, self.Operation, self.Source2.dataType)))
class Opcode_basicMath1(Opcode_DS):
def __init__(self, OSO, index, operation):
Opcode_DS.__init__(self, OSO, index)
self.Operation = operation
def GenerateFloat(self, nodeGraph):
node = nodeGraph.CreateNode("ShaderNodeMath")
node.SetProperty("operation", self.Operation)
nodeGraph.AddLink(node, 0, self.Source)
nodeGraph.SetVar(self.Destination, node, 0)
def GeneratePoint(self, nodeGraph):
node = nodeGraph.CreateNode('ShaderNodeSeparateXYZ')
nodeGraph.AddLink(node, 0, self.Source)
nodex = nodeGraph.CreateNode("ShaderNodeMath")
nodex.SetProperty("operation", self.Operation)
nodeGraph.AddNodeLink(nodex, 0, node, 0)
nodey = nodeGraph.CreateNode("ShaderNodeMath")
nodey.SetProperty("operation", self.Operation)
nodeGraph.AddNodeLink(nodey, 0, node, 1)
nodez = nodeGraph.CreateNode("ShaderNodeMath")
nodez.SetProperty("operation", self.Operation)
nodeGraph.AddNodeLink(nodez, 0, node, 2)
nodeOut = nodeGraph.CreateNode('ShaderNodeCombineXYZ')
nodeGraph.AddNodeLink(nodeOut, 0, nodex, 0)
nodeGraph.AddNodeLink(nodeOut, 1, nodey, 0)
nodeGraph.AddNodeLink(nodeOut, 2, nodez, 0)
nodeGraph.SetVar(self.Destination, nodeOut, 0)
def Generate(self, nodeGraph):
if self.Source.IsNumeric():
self.GenerateFloat(nodeGraph)
elif self.Source.IsPointLike():
self.GeneratePoint(nodeGraph)
else:
raise ValueError(str('Unsupported math operation %s %s' %
(self.Source.dataType, self.Operation)))