-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathVFXExpressionAbstractValues.cs
More file actions
314 lines (262 loc) · 9.26 KB
/
Copy pathVFXExpressionAbstractValues.cs
File metadata and controls
314 lines (262 loc) · 9.26 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.VFX;
using UnityObject = UnityEngine.Object;
namespace UnityEditor.VFX
{
#pragma warning disable 0659
abstract class VFXValue : VFXExpression
{
public enum Mode
{
Variable, // Variable that should never be folded
FoldableVariable, // Variable that can be folded
Constant, // Immutable value
}
// Syntactic sugar method to create a constant value
static public VFXValue<int> Constant(Texture2D value)
{
return new VFXTexture2DValue(ReferenceEquals(value, null) ? 0 : value.GetInstanceID(), Mode.Constant);
}
static public VFXValue<int> Constant(Texture3D value)
{
return new VFXTexture3DValue(ReferenceEquals(value, null) ? 0 : value.GetInstanceID(), Mode.Constant);
}
static public VFXValue<int> Constant(Cubemap value)
{
return new VFXTextureCubeValue(ReferenceEquals(value, null) ? 0 : value.GetInstanceID(), Mode.Constant);
}
static public VFXValue<int> Constant(Texture2DArray value)
{
return new VFXTexture2DArrayValue(ReferenceEquals(value, null) ? 0 : value.GetInstanceID(), Mode.Constant);
}
static public VFXValue<int> Constant(CubemapArray value)
{
return new VFXTextureCubeArrayValue(ReferenceEquals(value, null) ? 0 : value.GetInstanceID(), Mode.Constant);
}
static public VFXValue<int> Constant(CameraBuffer value)
{
return new VFXCameraBufferValue(value, Mode.Constant);
}
static public VFXValue<T> Constant<T>(T value = default(T))
{
return new VFXValue<T>(value, Mode.Constant);
}
private static Flags GetFlagsFromMode(Mode mode, Flags flags)
{
flags |= Flags.Value;
if (mode != Mode.Variable)
{
flags |= Flags.Foldable;
if (mode == Mode.Constant)
flags |= Flags.Constant;
}
return flags;
}
protected VFXValue(Mode mode, Flags flags)
: base(GetFlagsFromMode(mode, flags))
{
m_Mode = mode;
}
public Mode ValueMode { get { return m_Mode; } }
sealed public override VFXExpressionOperation operation { get { return VFXExpressionOperation.Value; } }
protected sealed override VFXExpression Evaluate(VFXExpression[] constParents)
{
if (m_Mode == Mode.Constant)
return this;
return CopyExpression(Mode.Constant);
}
public override string GetCodeString(string[] parents)
{
if (Is(Flags.InvalidOnGPU) || !Is(Flags.Constant))
throw new InvalidOperationException(string.Format("Type {0} is either not valid on GPU or expression is not constant", valueType));
return VFXShaderWriter.GetValueString(valueType, GetContent());
}
abstract public VFXValue CopyExpression(Mode mode);
public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj))
return true;
if (m_Mode == Mode.Constant)
{
var val = obj as VFXValue;
if (val == null)
return false;
if (val.m_Mode != Mode.Constant)
return false;
if (valueType != val.valueType)
return false;
var content = GetContent();
var otherContent = val.GetContent();
if (content == null)
return otherContent == null;
return content.Equals(otherContent);
}
else
return false;
}
protected override int GetInnerHashCode()
{
if (m_Mode == Mode.Constant)
{
int hashCode = valueType.GetHashCode();
var content = GetContent();
if (content != null)
hashCode ^= content.GetHashCode();
return hashCode;
}
return RuntimeHelpers.GetHashCode(this);
}
public abstract void SetContent(object value);
private Mode m_Mode;
protected object m_Content;
}
class VFXValue<T> : VFXValue
{
protected static Flags GetFlagsFromType(VFXValueType valueType)
{
var flags = Flags.None;
if (!IsTypeValidOnGPU(valueType))
flags |= VFXExpression.Flags.InvalidOnGPU;
if (!IsTypeConstantFoldable(valueType))
flags |= VFXExpression.Flags.InvalidConstant;
return flags;
}
public VFXValue(T content, Mode mode = Mode.FoldableVariable, Flags flag = Flags.None) : base(mode, flag | GetFlagsFromType(ToValueType()))
{
m_Content = content;
}
public override VFXValue CopyExpression(Mode mode)
{
var copy = new VFXValue<T>(Get(), mode);
return copy;
}
private static readonly VFXValue s_Default = new VFXValue<T>(default(T), VFXValue.Mode.Constant);
public static VFXValue Default { get { return s_Default; } }
public T Get()
{
return (T)m_Content;
}
public override object GetContent()
{
return Get();
}
public override void SetContent(object value)
{
m_Content = default(T);
if (value == null)
{
return;
}
var fromType = value.GetType();
var toType = typeof(T);
if (typeof(Texture).IsAssignableFrom(toType) && toType.IsAssignableFrom(fromType))
{
m_Content = (T)value;
}
else if (typeof(Mesh).IsAssignableFrom(toType) && toType.IsAssignableFrom(fromType))
{
m_Content = (T)value;
}
else if (fromType == toType || toType.IsAssignableFrom(fromType))
{
m_Content = (T)Convert.ChangeType(value, toType);
}
else if (toType == typeof(GraphicsBuffer))
{
//We can't serialize a reference of GraphicsBuffer
m_Content = null;
}
else
{
var implicitMethod = fromType.GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public)
.FirstOrDefault(m => m.Name == "op_Implicit" && m.ReturnType == toType);
if (implicitMethod != null)
{
m_Content = (T)implicitMethod.Invoke(null, new object[] { value });
}
else
{
Debug.LogErrorFormat("Cannot cast from {0} to {1}", fromType, toType);
}
}
}
private static VFXValueType ToValueType()
{
Type t = typeof(T);
if (typeof(Texture).IsAssignableFrom(t))
{
return VFXValueType.None;
}
var valueType = GetVFXValueTypeFromType(t);
if (valueType == VFXValueType.None)
throw new ArgumentException("Invalid type");
return valueType;
}
static private readonly VFXValueType s_ValueType = ToValueType();
protected override int[] additionnalOperands
{
get
{
return new int[] { (int)s_ValueType };
}
}
}
class VFXObjectValue : VFXValue<int>
{
public VFXObjectValue(int instanceID, Mode mode, VFXValueType contentType) : base(instanceID, mode, GetFlagsFromType(contentType))
{
m_ContentType = contentType;
}
sealed protected override int[] additionnalOperands
{
get
{
return new int[] { (int)m_ContentType };
}
}
public override VFXValue CopyExpression(Mode mode)
{
var copy = new VFXObjectValue((int)m_Content, mode, m_ContentType);
return copy;
}
public override T Get<T>()
{
if (typeof(T) == typeof(int))
return (T)(object)base.Get();
return (T)(object)EditorUtility.InstanceIDToObject(base.Get());
}
public override object GetContent()
{
return Get();
}
public override void SetContent(object value)
{
if (value == null)
{
m_Content = (int)0;
return;
}
if (value is UnityObject obj)
{
m_Content = obj.GetInstanceID();
return;
}
if (value is CameraBuffer cameraBuffer)
{
m_Content = cameraBuffer;
return;
}
if (value is GraphicsBuffer)
{
m_Content = (int)0;
return;
}
m_Content = (int)value;
}
VFXValueType m_ContentType;
}
#pragma warning restore 0659
}