-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathVFXContextProfilerUI.cs
More file actions
389 lines (341 loc) · 13.9 KB
/
Copy pathVFXContextProfilerUI.cs
File metadata and controls
389 lines (341 loc) · 13.9 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
using System.Collections.Generic;
using System.IO;
using UnityEditor.VFX.Block;
using UnityEngine;
using UnityEngine.Profiling;
using UnityEngine.UIElements;
namespace UnityEditor.VFX.UI
{
class VFXContextProfilerUI : VFXAnchoredProfilerUI
{
public VFXContextProfilerUI(VFXContextUI contextUI)
{
m_ContextUI = contextUI;
if(!m_ContextUI.selected)
AddToClassList(k_HiddenStyleClass);
title = contextUI.controller.model.name.Split(" ")[0];
AddToClassList("VFXContextProfiler");
AnchorTo(m_ContextUI);
m_TextureInfosLabels = new List<SlotLabel>();
m_TextureHashSet = new HashSet<Texture>();
}
private VFXContextUI m_ContextUI;
internal VFXContextUI contextUI => m_ContextUI;
private Label m_ContextAggregateGPUTiming;
public struct SlotLabel
{
public SlotLabel(VFXSlot slot, string text)
{
this.slot = slot;
this.label = new Label(text) { name = "texture-slot" };
}
public VFXSlot slot;
public Label label;
}
private Foldout m_TextureUsageFoldout;
private List<SlotLabel> m_TextureInfosLabels;
private HashSet<Texture> m_TextureHashSet;
private Label m_InfoBadge;
private VisualElement m_PositionBadge;
private VisualElement m_RotationBadge;
private VisualElement m_AgeBadge;
public override bool isCollapsed => ClassListContains(k_HiddenStyleClass);
void CollectAllTextureSlotsRecursive(IVFXSlotContainer slotContainer, HashSet<VFXSlot> allLinkedSlots)
{
foreach (var inputSlot in slotContainer.inputSlots)
{
if(inputSlot is VFXSlotTexture2D or VFXSlotTexture3D or VFXSlotTextureCube or VFXSlotTexture2DArray or VFXSlotTextureCubeArray )
allLinkedSlots.Add(inputSlot);
foreach (var linkedSlot in inputSlot.LinkedSlots)
{
CollectAllTextureSlotsRecursive(linkedSlot.owner, allLinkedSlots);
}
}
}
internal static string GetTextureInformationString(Texture texture)
{
string memorySize = EditorUtility.FormatBytes(TextureUtil.GetStorageMemorySizeLong(texture));
string textureInfo;
if (texture is Texture3D texture3D)
{
textureInfo = $"{texture3D.height}x{texture3D.width}x{texture3D.depth} - {memorySize}";
}
else
{
textureInfo = $"{texture.height}x{texture.width} - {memorySize}";
}
const int kMaxTotalLength = 35;
int lengthLeftForName = kMaxTotalLength - textureInfo.Length;
string textureName;
if (lengthLeftForName > texture.name.Length)
textureName = texture.name;
else
{
int halfLength = lengthLeftForName / 2;
textureName = $"{texture.name.Substring(0, halfLength)}...{texture.name.Substring(texture.name.Length - halfLength, halfLength)}";
}
return $"{textureName} - {textureInfo}";
}
internal List<SlotLabel> GetTextureSlotLabels()
{
return m_TextureInfosLabels;
}
private void RegisterTextureLabel(VFXSlot slot)
{
Texture texture = slot.value as Texture;
m_TextureInfosLabels.Add( new SlotLabel(slot, texture != null ? GetTextureInformationString(texture) : ""));
}
public override void OnUnselected()
{
base.OnUnselected();
if(!m_Locked)
AddToClassList(k_HiddenStyleClass);
}
public override void AnchorTo(VFXContextUI target)
{
base.AnchorTo(target);
target.onSelectionDelegate += OnContextSelection;
}
public override void Detach()
{
base.Detach();
m_ContextUI.onSelectionDelegate -= OnContextSelection;
}
internal void RepositionPanel()
{
RepositionPanel(m_ContextUI);
}
public override void ForceExpand()
{
RemoveFromClassList(k_HiddenStyleClass);
m_Foldout.value = true;
}
public override void ForceClose()
{
AddToClassList(k_HiddenStyleClass);
m_Foldout.value = false;
}
void OnContextSelection(bool isSelected)
{
if (isSelected)
{
RemoveFromClassList(k_HiddenStyleClass);
m_Foldout.value = true;
}
else
{
if(!m_Locked)
AddToClassList(k_HiddenStyleClass);
}
}
protected override void SetupStatsLayout()
{
if (attachedComponent == null)
return;
if (m_ProfilingGPUItemLeaves != null)
{
ClearContent();
m_ProfilingGPUItemLeaves.Clear();
}
else
{
m_ProfilingGPUItemLeaves = new List<VFXProfilingBoard.ProfilingItemLeaf>();
}
if(m_ContextUI.controller.model is VFXBasicSpawner)
SetupEventBadges();
if(m_ContextUI.controller.model is VFXBasicUpdate)
SetupImplicitUpdateBadges();
VFXSystemNames systemNames = m_ContextUI.controller.viewController.graph.systemNames;
string systemName = systemNames.GetUniqueSystemName(m_ContextUI.controller.model.GetData());
m_SystemName = systemName;
Foldout gpuTimeFoldout = new Foldout
{
name = "TimingGPU",
text = "Execution time (GPU)",
tooltip = "Execution time of the context on the GPU, in milliseconds. Number of dispatches/draw calls are in parenthesis."
};
gpuTimeFoldout.value = false;
m_ContextAggregateGPUTiming = AddLabelToFoldout(gpuTimeFoldout);
if (!m_SupportsGPURecorder)
{
m_ContextAggregateGPUTiming.text = "-";
gpuTimeFoldout.tooltip = kGpuRecorderNotSupportedMsg;
}
foreach (var taskId in m_ContextUI.controller.model.GetContextTaskIndices())
{
string markerName = attachedComponent.GetGPUTaskMarkerName(systemName, taskId.taskIndex);
if (string.IsNullOrEmpty(markerName))
continue;
VisualElement labelContainer = new VisualElement
{
style = { flexDirection = FlexDirection.Row, paddingTop = 2, paddingBottom = 2 }
};
Label taskLabel = new Label
{
name = markerName,
text = $"{taskId.taskName} ",
style = { flexGrow = 1 }
};
Label taskTimingLabel = new Label()
{
name = markerName,
style = { flexGrow = 0, alignSelf = Align.FlexEnd}
};
if (!m_SupportsGPURecorder)
{
taskTimingLabel.text = "-";
taskTimingLabel.tooltip = kGpuRecorderNotSupportedMsg;
}
labelContainer.Add(taskLabel);
labelContainer.Add(taskTimingLabel);
gpuTimeFoldout.Add(labelContainer);
var recorder = Recorder.Get(markerName);
VFXProfilingBoard.ProfilingItemLeaf leaf = new VFXProfilingBoard.ProfilingItemLeaf(recorder, taskTimingLabel);
m_ProfilingGPUItemLeaves.Add(leaf);
}
if(gpuTimeFoldout.childCount > 0)
AddContent(gpuTimeFoldout);
SetupTextureSection();
}
private void SetupTextureSection()
{
m_TextureInfosLabels.Clear();
HashSet<VFXSlot> textureSlots = new HashSet<VFXSlot>();
CollectAllTextureSlotsRecursive(m_ContextUI.controller.model, textureSlots);
foreach (var block in m_ContextUI.controller.model.children)
{
CollectAllTextureSlotsRecursive(block, textureSlots);
}
foreach (var slot in textureSlots)
{
RegisterTextureLabel(slot);
}
if (m_TextureInfosLabels.Count > 0)
{
m_TextureUsageFoldout = new Foldout
{
name = "TextureUsage",
text = "Texture Usage"
};
m_TextureUsageFoldout.value = false;
foreach (var textureInfoLabel in m_TextureInfosLabels)
{
m_TextureUsageFoldout.Add(textureInfoLabel.label);
}
AddContent(m_TextureUsageFoldout);
}
}
void SetupEventBadges()
{
VisualElement statusContainer = new VisualElement
{
style = { flexDirection = FlexDirection.Row, paddingTop = 2, paddingBottom = 2 }
};
m_InfoBadge = CreateBadge(null, "Indicates whether the last received event was a Start or a Stop event");
statusContainer.Add(m_InfoBadge);
AddContent(statusContainer);
}
void SetupImplicitUpdateBadges()
{
bool hasPosition = false;
bool hasRotation = false;
bool hasAge = false;
if (m_ContextUI.controller.model is VFXBasicUpdate basicUpdate)
{
foreach (var block in basicUpdate.activeChildrenWithImplicit)
{
if (block is EulerIntegration)
hasPosition = true;
if (block is AngularEulerIntegration)
hasRotation = true;
if (block is Age)
hasAge = true;
}
}
if (hasPosition || hasRotation || hasAge)
{
VisualElement statusContainer = new VisualElement
{
style = { flexDirection = FlexDirection.Row, paddingTop = 2, paddingBottom = 2 },
tooltip = "Attributes implicitly updated during the update context. They can be disabled in update context’s inspector."
};
if (hasPosition)
{
m_PositionBadge = CreateBadge("POSITION", "Particles positions are automatically updated based on their velocity attribute.");
m_PositionBadge.AddToClassList("position");
statusContainer.Add(m_PositionBadge);
}
if (hasRotation)
{
m_RotationBadge = CreateBadge("ROTATION", "Particles rotations are automatically updated based on their angular velocity attribute.");
m_RotationBadge.AddToClassList("rotation");
statusContainer.Add(m_RotationBadge);
}
if (hasAge)
{
m_AgeBadge = CreateBadge("AGE", "Particles ages are automatically incremented with deltaTime.");
m_AgeBadge.AddToClassList("age");
statusContainer.Add(m_AgeBadge);
}
AddContent(statusContainer);
}
}
internal override void UpdateDynamicValues()
{
base.UpdateDynamicValues();
if (m_ContextAggregateGPUTiming != null && m_SupportsGPURecorder)
{
float timeMs = NanoToMilli(GetGPUTimingAggregate());
m_ContextAggregateGPUTiming.text = $"{timeMs:F3} ms";
m_ContextAggregateGPUTiming.ClearClassList();
m_ContextAggregateGPUTiming.AddToClassList(ComputeHeatmapClass(timeMs, m_HeatmapRefValue));
}
if (m_TextureInfosLabels != null && m_TextureInfosLabels.Count > 0)
{
m_TextureHashSet.Clear();
foreach (var slotLabel in m_TextureInfosLabels)
{
Texture texture = slotLabel.slot.value as Texture;
if (m_TextureHashSet.Contains(texture) || texture == null)
{
slotLabel.label.style.display = DisplayStyle.None;
continue;
}
slotLabel.label.style.display = DisplayStyle.Flex;
string textureLabelText = GetTextureInformationString(texture);
m_TextureHashSet.Add(texture);
if (slotLabel.label.text != textureLabelText)
{
slotLabel.label.text = textureLabelText;
}
}
m_TextureUsageFoldout.style.display = m_TextureHashSet.Count == 0 ? DisplayStyle.None : DisplayStyle.Flex;
}
if (m_InfoBadge != null)
{
var spawnerState = m_AttachedComponent.GetSpawnSystemInfo(m_SystemName);
if (spawnerState.playing)
{
m_InfoBadge.text = "PLAYING";
m_InfoBadge.AddToClassList("play");
m_InfoBadge.RemoveFromClassList("stopped");
}
else
{
m_InfoBadge.text = "STOPPED";
m_InfoBadge.AddToClassList("stopped");
m_InfoBadge.RemoveFromClassList("play");
}
}
}
public long GetGPUTimingAggregate()
{
long aggregate = 0L;
foreach (var leaf in m_ProfilingGPUItemLeaves)
{
aggregate += leaf.average;
}
return aggregate;
}
}
}