-
Notifications
You must be signed in to change notification settings - Fork 867
Expand file tree
/
Copy pathRenderGraphViewer.cs
More file actions
2273 lines (1927 loc) · 101 KB
/
RenderGraphViewer.cs
File metadata and controls
2273 lines (1927 loc) · 101 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using UnityEditor.Networking.PlayerConnection;
using UnityEditor.Rendering.Analytics;
using UnityEditor.Toolbars;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.RenderGraphModule;
using UnityEngine.Scripting.APIUpdating;
using UnityEngine.UIElements;
namespace UnityEditor.Rendering
{
/// <summary>
/// Editor window class for the Render Graph Viewer
/// </summary>
[MovedFrom("")]
[CoreRPHelpURL(packageName: "com.unity.render-pipelines.universal", pageName: "render-graph-view")]
public partial class RenderGraphViewer : EditorWindowWithHelpButton
{
internal static partial class Names
{
public const string kAutoPauseToggle = "auto-pause-toggle";
public const string kCurrentGraphDropdown = "current-graph-dropdown";
public const string kConnectionDropdown = "connection-dropdown";
public const string kCurrentExecutionToolbarMenu = "current-execution-toolbar-menu";
public const string kPassFilterField = "pass-filter-field";
public const string kResourceFilterField = "resource-filter-field";
public const string kViewOptionsField = "view-options-field";
public const string kContentContainer = "content-container";
public const string kMainContainer = "main-container";
public const string kPassList = "pass-list";
public const string kPassListScrollView = "pass-list-scroll-view";
public const string kResourceListScrollView = "resource-list-scroll-view";
public const string kResourceGridScrollView = "resource-grid-scroll-view";
public const string kResourceGrid = "resource-grid";
public const string kGridlineContainer = "grid-line-container";
public const string kHoverOverlay = "hover-overlay";
public const string kEmptyStateMessage = "empty-state-message";
public const string kPassListCornerOccluder = "pass-list-corner-occluder";
public const string kStatusLabel = "status-label";
}
internal static partial class Classes
{
public const string kPassListItem = "pass-list__item";
public const string kPassTitle = "pass-title";
public const string kPassBlock = "pass-block";
public const string kPassBlockScriptLink = "pass-block-script-link";
public const string kPassBlockCulledPass = "pass-block--culled";
public const string kPassBlockAsyncPass = "pass-block--async";
public const string kPassHighlight = "pass--highlight";
public const string kPassHoverHighlight = "pass--hover-highlight";
public const string kPassHighlightBorder = "pass--highlight-border";
public const string kPassMergeIndicator = "pass-merge-indicator";
public const string kPassCompatibilityMessageIndicator = "pass-compatibility-message-indicator";
public const string kPassCompatibilityMessageIndicatorAnimation = "pass-compatibility-message-indicator--anim";
public const string kPassCompatibilityMessageIndicatorCompatible = "pass-compatibility-message-indicator--compatible";
public const string kPassSynchronizationMessageIndicator = "pass-synchronization-message-indicator";
public const string kResourceListItem = "resource-list__item";
public const string kResourceListItemHighlight = "resource-list__item--highlight";
public const string kResourceListPaddingItem = "resource-list-padding-item";
public const string kResourceIconContainer = "resource-icon-container";
public const string kResourceIcon = "resource-icon";
public const string kResourceIconImported = "resource-icon--imported";
public const string kResourceIconMultipleUsage = "resource-icon--multiple-usage";
public const string kResourceIconGlobalDark = "resource-icon--global-dark";
public const string kResourceIconGlobalLight = "resource-icon--global-light";
public const string kResourceIconFbfetch = "resource-icon--fbfetch";
public const string kResourceIconTexture = "resource-icon--texture";
public const string kResourceIconMemorylessTexture = "resource-icon--memoryless-texture";
public const string kResourceIconBuffer = "resource-icon--buffer";
public const string kResourceIconAccelerationStructure = "resource-icon--acceleration-structure";
public const string kResourceGridRow = "resource-grid__row";
public const string kResourceGridFocusOverlay = "resource-grid-focus-overlay";
public const string kResourceHelperLine = "resource-helper-line";
public const string kResourceHelperLineHighlight = "resource-helper-line--highlight";
public const string kResourceUsageRangeBlock = "usage-range-block";
public const string kResourceUsageRangeBlockHighlight = "usage-range-block--highlight";
public const string kResourceDependencyBlock = "dependency-block";
public const string kResourceDependencyBlockRead = "dependency-block-read";
public const string kResourceDependencyBlockWrite = "dependency-block-write";
public const string kResourceDependencyBlockReadWrite = "dependency-block-readwrite";
public const string kGridLine = "grid-line";
public const string kGridLineHighlight = "grid-line--highlight";
public const string kLoadAction = "dependency-block-load-action";
public const string kLoadActionLoad = "load-action-load";
public const string kLoadActionClear = "load-action-clear";
public const string kLoadActionDontCare = "load-action-dont-care";
public const string kStoreAction = "dependency-block-store-action";
public const string kStoreActionStore = "store-action-store";
public const string kStoreActionResolve = "store-action-resolve";
public const string kStoreActionStoreAndResolve = "store-action-store-resolve";
public const string kStoreActionDontCare = "store-action-dont-care";
public const string kLoadActionBorder = "dependency-block-load-action-border";
public const string kStoreActionBorder = "dependency-block-store-action-border";
}
const string k_TemplatePath = "Packages/com.unity.render-pipelines.core/Editor/UXML/RenderGraphViewer.uxml";
const string k_DarkStylePath = "Packages/com.unity.render-pipelines.core/Editor/StyleSheets/RenderGraphViewerDark.uss";
const string k_LightStylePath = "Packages/com.unity.render-pipelines.core/Editor/StyleSheets/RenderGraphViewerLight.uss";
const string k_ResourceListIconPath = "Packages/com.unity.render-pipelines.core/Editor/Icons/RenderGraphViewer/{0}Resources@2x.png";
const string k_PassListIconPath = "Packages/com.unity.render-pipelines.core/Editor/Icons/RenderGraphViewer/{0}PassInspector@2x.png";
const string k_EditorName ="Editor";
// keep in sync with .uss
const int kPassWidthPx = 26;
const int kResourceRowHeightPx = 30;
const int kResourceColumnWidth = 220;
const int kResourceIconSize = 16;
const int kResourceGridMarginTopPx = 6;
const int kDependencyBlockHeightPx = 26;
const int kDependencyBlockWidthPx = kPassWidthPx;
const int kPassTitleAllowanceMargin = 120;
const int kHeaderContainerHeightPx = 24;
static readonly Color kReadWriteBlockFillColorDark = new Color32(0xA9, 0xD1, 0x36, 255);
static readonly Color kReadWriteBlockFillColorLight = new Color32(0x67, 0x9C, 0x33, 255);
internal RenderGraph.DebugData m_CurrentDebugData;
PlayerConnection m_PlayerConnection;
bool m_Paused = false;
static int s_EditorWindowInstanceId;
DateTime m_LastDataCaptureTime = DateTime.MinValue;
string m_ConnectedDeviceName = "Local Editor";
bool m_IsDeviceConnected = true;
bool HasValidDebugData => m_CurrentDebugData != null && m_CurrentDebugData.valid;
Foldout m_ResourcesList;
Foldout m_PassList;
PanManipulator m_PanManipulator;
Texture2D m_ResourceListIcon;
Texture2D m_PassListIcon;
readonly List<PassElementInfo> m_PassElementsInfo = new(); // Indexed using visiblePassIndex
readonly List<ResourceElementInfo> m_ResourceElementsInfo = new(); // Indexed using visibleResourceIndex
readonly Dictionary<int, int> m_PassIdToVisiblePassIndex = new();
readonly Dictionary<int, int> m_VisiblePassIndexToPassId = new();
int m_CurrentHoveredVisiblePassIndex = -1;
int m_CurrentHoveredVisibleResourceIndex = -1;
int m_CurrentSelectedVisiblePassIndex = -1;
const string kPassFilterLegacyEditorPrefsKey = "RenderGraphViewer.PassFilterLegacy";
const string kPassFilterEditorPrefsKey = "RenderGraphViewer.PassFilter";
const string kResourceFilterEditorPrefsKey = "RenderGraphViewer.ResourceFilter";
const string kSelectedExecutionEditorPrefsKey = "RenderGraphViewer.SelectedExecution";
const string kViewOptionsEditorPrefsKey = "RenderGraphViewer.ViewOptions";
IVisualElementScheduledItem m_StoreSelectedExecutionDelayed;
IVisualElementScheduledItem m_RefreshUIDelayed;
PassFilter m_PassFilter = PassFilter.CulledPasses | PassFilter.RasterPasses | PassFilter.UnsafePasses | PassFilter.ComputePasses;
PassFilterLegacy m_PassFilterLegacy = PassFilterLegacy.CulledPasses;
ResourceFilter m_ResourceFilter =
ResourceFilter.ImportedResources | ResourceFilter.Textures |
ResourceFilter.Buffers | ResourceFilter.AccelerationStructures;
ViewOptions m_ViewOptions = ViewOptions.LoadStoreActions;
bool m_PassFilterEnabled = true;
bool m_ResourceFilterEnabled = true;
bool m_ViewOptionsEnabled = false;
enum EmptyStateReason
{
None = 0,
NoGraphRegistered,
WaitingForCameraRender,
EmptyPassFilterResult,
EmptyResourceFilterResult,
IncompatibleDataReceived
};
static readonly string[] kEmptyStateMessages =
{
"",
L10n.Tr("No Render Graph has been registered. The Render Graph Viewer is only functional when Render Graph API is in use."),
L10n.Tr("Waiting for the selected camera to render. Depending on the camera, you may need to trigger rendering by selecting the Scene or Game view.\n\nEnsure Render Graph is not disabled in Project Settings > Graphics."),
L10n.Tr("No passes to display. Select a different Pass Filter to display contents."),
L10n.Tr("No resources to display. Select a different Resource Filter to display contents."),
L10n.Tr("Editor received incompatible data. Rebuild the player with this version of the editor, or switch to an older version of the editor."),
};
private static readonly string[] kLoadActionNames =
{
"",
L10n.Tr("Load"),
L10n.Tr("Clear"),
L10n.Tr("Don't Care"),
};
private static readonly string[] kStoreActionNames =
{
"",
L10n.Tr("Store"),
L10n.Tr("Resolve"),
L10n.Tr("Store and Resolve"),
L10n.Tr("Don't Care"),
};
static readonly string kOpenInCSharpEditorTooltip = L10n.Tr("Click to open <b>{0}</b> definition in C# editor.");
[MenuItem("Window/Analysis/Render Graph Viewer", false, 10006)]
static void Init()
{
var window = GetWindow<RenderGraphViewer>();
window.titleContent = new GUIContent("Render Graph Viewer");
window.minSize = new Vector2(880f, 300f);
}
[Flags]
enum PassFilterLegacy
{
CulledPasses = 1 << 0,
}
[Flags]
enum PassFilter
{
CulledPasses = 1 << 0,
RasterPasses = 1 << 1,
UnsafePasses = 1 << 2,
ComputePasses = 1 << 3,
}
[Flags]
enum ResourceFilter
{
ImportedResources = 1 << 0,
Textures = 1 << 1,
Buffers = 1 << 2,
AccelerationStructures = 1 << 3,
}
[Flags]
enum ViewOptions
{
LoadStoreActions = 1 << 0,
}
class ResourceElementInfo
{
public RenderGraphResourceType type;
public int index;
public VisualElement usageRangeBlock;
public VisualElement resourceListItem;
public VisualElement resourceHelperLine;
public int firstPassId;
public int lastPassId;
}
class ResourceRWBlock
{
[Flags]
public enum UsageFlags
{
None = 0,
UpdatesGlobalResource = 1 << 0,
FramebufferFetch = 1 << 1,
}
public enum LoadAction
{
None,
Load,
Clear,
DontCare,
}
public enum StoreAction
{
None,
Store,
Resolve,
StoreAndResolve,
DontCare,
}
public VisualElement element;
public string tooltip;
public int visibleResourceIndex;
public bool read;
public bool write;
public bool memoryless;
public LoadAction load;
public StoreAction store;
public UsageFlags usage;
public bool HasMultipleUsageFlags()
{
// Check if usage is a power of 2, meaning only one bit is set
return usage != 0 && (usage & (usage - 1)) != 0;
}
}
class PassElementInfo
{
public int passId;
public VisualElement passBlock;
public PassTitleLabel passTitle;
public bool isCulled;
public bool isAsync;
// List of resource blocks read/written to by this pass
public readonly List<ResourceRWBlock> resourceBlocks = new();
public VisualElement leftGridLine;
public VisualElement rightGridLine;
public bool hasPassCompatibilityTooltip;
public bool isPassCompatibleToMerge;
public bool hasAsyncDependencyTooltip;
}
void AppendToTooltipIfNotEmpty(VisualElement elem, string append)
{
if (elem.tooltip.Length > 0)
elem.tooltip += append;
}
void ResetPassBlockState()
{
foreach (var info in m_PassElementsInfo)
{
info.hasPassCompatibilityTooltip = false;
info.isPassCompatibleToMerge = false;
info.hasAsyncDependencyTooltip = false;
info.passBlock.RemoveFromClassList(Classes.kPassBlockCulledPass);
info.passBlock.tooltip = string.Empty;
if (info.isCulled)
{
info.passBlock.AddToClassList(Classes.kPassBlockCulledPass);
info.passBlock.tooltip = "Culled pass";
}
info.passBlock.RemoveFromClassList(Classes.kPassBlockAsyncPass);
if (info.isAsync)
{
info.passBlock.AddToClassList(Classes.kPassBlockAsyncPass);
AppendToTooltipIfNotEmpty(info.passBlock, $"{Environment.NewLine}");
info.passBlock.tooltip += "Async Compute Pass";
}
var groupedIds = GetGroupedPassIds(info.passId);
if (groupedIds.Count > 1)
{
AppendToTooltipIfNotEmpty(info.passBlock, $"{Environment.NewLine}");
info.passBlock.tooltip += $"{groupedIds.Count} Raster Render Passes merged into a single Native Render Pass";
}
AppendToTooltipIfNotEmpty(info.passBlock, $"{Environment.NewLine}{Environment.NewLine}");
info.passBlock.tooltip += string.Format(kOpenInCSharpEditorTooltip, info.passTitle.text);
}
}
void UpdatePassBlocksToSelectedState(List<int> selectedPassIds)
{
// Hide culled/async pass indicators when a block is selected
foreach (var info in m_PassElementsInfo)
{
info.passBlock.RemoveFromClassList(Classes.kPassBlockCulledPass);
info.passBlock.RemoveFromClassList(Classes.kPassBlockAsyncPass);
info.passBlock.tooltip = string.Empty;
}
foreach (var passIdInGroup in selectedPassIds)
{
var pass = m_CurrentDebugData.passList[passIdInGroup];
// Native pass compatibility
if (m_CurrentDebugData.isNRPCompiler)
{
if (pass.nrpInfo.nativePassInfo != null && pass.nrpInfo.nativePassInfo.passCompatibility.Count > 0)
{
foreach (var msg in pass.nrpInfo.nativePassInfo.passCompatibility)
{
int linkedPassId = msg.Key;
string compatibilityMessage = msg.Value.message;
var linkedPassGroup = GetGroupedPassIds(linkedPassId);
foreach (var passIdInLinkedPassGroup in linkedPassGroup)
{
if (selectedPassIds.Contains(passIdInLinkedPassGroup))
continue; // Don't show compatibility info among passes that are merged
if (m_PassIdToVisiblePassIndex.TryGetValue(passIdInLinkedPassGroup,
out int visiblePassIndexInLinkedPassGroup))
{
var info = m_PassElementsInfo[visiblePassIndexInLinkedPassGroup];
info.hasPassCompatibilityTooltip = true;
info.isPassCompatibleToMerge = msg.Value.isCompatible;
info.passBlock.tooltip = compatibilityMessage;
}
}
}
// Each native pass has compatibility messages, it's enough to process the first one
break;
}
}
// Async compute dependencies
if (m_PassIdToVisiblePassIndex.TryGetValue(pass.syncFromPassIndex, out int visibleSyncFromPassIndex))
{
var syncFromPassInfo = m_PassElementsInfo[visibleSyncFromPassIndex];
syncFromPassInfo.hasAsyncDependencyTooltip = true;
AppendToTooltipIfNotEmpty(syncFromPassInfo.passBlock, $"{Environment.NewLine}");
syncFromPassInfo.passBlock.tooltip +=
"Currently selected Async Compute Pass inserts a GraphicsFence, which this pass waits on.";
}
if (m_PassIdToVisiblePassIndex.TryGetValue(pass.syncToPassIndex, out int visibleSyncToPassIndex))
{
var syncToPassInfo = m_PassElementsInfo[visibleSyncToPassIndex];
syncToPassInfo.hasAsyncDependencyTooltip = true;
AppendToTooltipIfNotEmpty(syncToPassInfo.passBlock, $"{Environment.NewLine}");
syncToPassInfo.passBlock.tooltip +=
"Currently selected Async Compute Pass waits on a GraphicsFence inserted after this pass.";
}
}
foreach (var info in m_PassElementsInfo)
{
AppendToTooltipIfNotEmpty(info.passBlock, $"{Environment.NewLine}{Environment.NewLine}");
info.passBlock.tooltip += string.Format(kOpenInCSharpEditorTooltip, info.passTitle.text);
}
}
[Flags]
enum ResourceHighlightOptions
{
None = 1 << 0,
ResourceListItem = 1 << 1,
ResourceUsageRangeBorder = 1 << 2,
ResourceHelperLine = 1 << 3,
All = ResourceListItem | ResourceUsageRangeBorder | ResourceHelperLine
}
void ClearResourceHighlight(ResourceHighlightOptions highlightOptions = ResourceHighlightOptions.All)
{
if (highlightOptions.HasFlag(ResourceHighlightOptions.ResourceListItem))
{
rootVisualElement.Query(classes: Classes.kResourceListItem).ForEach(elem =>
{
elem.RemoveFromClassList(Classes.kResourceListItemHighlight);
});
}
if (highlightOptions.HasFlag(ResourceHighlightOptions.ResourceUsageRangeBorder))
{
rootVisualElement.Query(classes: Classes.kResourceUsageRangeBlockHighlight).ForEach(elem =>
{
elem.RemoveFromHierarchy();
});
}
if (highlightOptions.HasFlag(ResourceHighlightOptions.ResourceHelperLine))
{
rootVisualElement.Query(classes: Classes.kResourceHelperLineHighlight).ForEach(elem =>
{
elem.RemoveFromClassList(Classes.kResourceHelperLineHighlight);
});
}
}
void SetResourceHighlight(ResourceElementInfo info, int visibleResourceIndex, ResourceHighlightOptions highlightOptions)
{
if (highlightOptions.HasFlag(ResourceHighlightOptions.ResourceListItem))
{
info.resourceListItem.AddToClassList(Classes.kResourceListItemHighlight);
}
if (highlightOptions.HasFlag(ResourceHighlightOptions.ResourceUsageRangeBorder))
{
var usageRangeHighlightBlock = new VisualElement();
usageRangeHighlightBlock.style.left = info.usageRangeBlock.style.left.value.value;
usageRangeHighlightBlock.style.width = info.usageRangeBlock.style.width.value.value + 1.0f;
usageRangeHighlightBlock.style.top = visibleResourceIndex * kResourceRowHeightPx;
usageRangeHighlightBlock.pickingMode = PickingMode.Ignore;
usageRangeHighlightBlock.AddToClassList(Classes.kResourceUsageRangeBlockHighlight);
rootVisualElement.Q<VisualElement>(Names.kResourceGrid).parent.Add(usageRangeHighlightBlock);
usageRangeHighlightBlock.PlaceInFront(rootVisualElement.Q<VisualElement>(Names.kGridlineContainer));
}
if (highlightOptions.HasFlag(ResourceHighlightOptions.ResourceHelperLine))
{
info.resourceHelperLine.AddToClassList(Classes.kResourceHelperLineHighlight);
}
}
[Flags]
enum PassHighlightOptions
{
None = 1 << 0,
PassBlockBorder = 1 << 1,
PassBlockFill = 1 << 2,
PassTitle = 1 << 3,
PassGridLines = 1 << 4,
ResourceRWBlocks = 1 << 5,
PassesWithCompatibilityMessage = 1 << 6,
PassesWithSynchronizationMessage = 1 << 7,
ResourceGridFocusOverlay = 1 << 8,
PassTitleHover = 1 << 9,
All = PassBlockBorder | PassBlockFill | PassTitle | PassGridLines | ResourceRWBlocks |
PassesWithCompatibilityMessage | PassesWithSynchronizationMessage | ResourceGridFocusOverlay | PassTitleHover
}
void ClearPassHighlight(PassHighlightOptions highlightOptions = PassHighlightOptions.All)
{
// Remove pass block & title highlight
foreach (var info in m_PassElementsInfo)
{
if (highlightOptions.HasFlag(PassHighlightOptions.PassTitle))
info.passTitle.RemoveFromClassList(Classes.kPassHighlight);
if (highlightOptions.HasFlag(PassHighlightOptions.PassTitleHover))
info.passTitle.RemoveFromClassList(Classes.kPassHoverHighlight);
if (highlightOptions.HasFlag(PassHighlightOptions.PassBlockBorder))
info.passBlock.RemoveFromClassList(Classes.kPassHighlightBorder);
if (highlightOptions.HasFlag(PassHighlightOptions.PassBlockFill))
info.passBlock.RemoveFromClassList(Classes.kPassHighlight);
if (highlightOptions.HasFlag(PassHighlightOptions.PassesWithCompatibilityMessage))
{
info.passBlock.RemoveFromClassList(Classes.kPassCompatibilityMessageIndicator);
info.passBlock.RemoveFromClassList(Classes.kPassCompatibilityMessageIndicatorAnimation);
info.passBlock.RemoveFromClassList(Classes.kPassCompatibilityMessageIndicatorCompatible);
info.passBlock.UnregisterCallback<TransitionEndEvent, VisualElement>(ToggleCompatiblePassAnimation);
}
if (highlightOptions.HasFlag(PassHighlightOptions.PassesWithSynchronizationMessage))
info.passBlock.RemoveFromClassList(Classes.kPassSynchronizationMessageIndicator);
}
// Remove grid line highlight
if (highlightOptions.HasFlag(PassHighlightOptions.PassGridLines))
{
rootVisualElement.Query(classes: Classes.kGridLine).ForEach(elem =>
{
elem.RemoveFromClassList(Classes.kGridLineHighlight);
});
}
// Remove focus overlay
if (highlightOptions.HasFlag(PassHighlightOptions.ResourceGridFocusOverlay))
{
rootVisualElement.Query(classes: Classes.kResourceGridFocusOverlay).ForEach(elem =>
{
elem.RemoveFromHierarchy();
});
}
}
void SetPassHighlight(int visiblePassIndex, PassHighlightOptions highlightOptions)
{
if (!m_VisiblePassIndexToPassId.TryGetValue(visiblePassIndex, out int passId))
return;
var groupedPassIds = GetGroupedPassIds(passId);
// Add pass block & title highlight
List<PassElementInfo> visiblePassInfos = new();
foreach (int groupedPassId in groupedPassIds)
{
if (m_PassIdToVisiblePassIndex.TryGetValue(groupedPassId, out int groupedVisiblePassIndex))
{
var info = m_PassElementsInfo[groupedVisiblePassIndex];
if (highlightOptions.HasFlag(PassHighlightOptions.PassTitle))
info.passTitle.AddToClassList(Classes.kPassHighlight);
if (highlightOptions.HasFlag(PassHighlightOptions.PassTitleHover))
info.passTitle.AddToClassList(Classes.kPassHoverHighlight);
if (highlightOptions.HasFlag(PassHighlightOptions.PassBlockBorder))
info.passBlock.AddToClassList(Classes.kPassHighlightBorder);
if (highlightOptions.HasFlag(PassHighlightOptions.PassBlockFill))
info.passBlock.AddToClassList(Classes.kPassHighlight);
visiblePassInfos.Add(info);
}
}
foreach (var info in m_PassElementsInfo)
{
if (groupedPassIds.Contains(info.passId))
continue;
if (highlightOptions.HasFlag(PassHighlightOptions.PassesWithCompatibilityMessage) &&
info.hasPassCompatibilityTooltip)
{
info.passBlock.AddToClassList(Classes.kPassCompatibilityMessageIndicator);
if (info.isPassCompatibleToMerge)
{
info.passBlock.schedule.Execute(() =>
{
info.passBlock.AddToClassList(Classes.kPassCompatibilityMessageIndicatorAnimation);
info.passBlock.AddToClassList(Classes.kPassCompatibilityMessageIndicatorCompatible);
}).StartingIn(100);
info.passBlock.RegisterCallback<TransitionEndEvent, VisualElement>(
ToggleCompatiblePassAnimation, info.passBlock);
}
}
if (highlightOptions.HasFlag(PassHighlightOptions.PassesWithSynchronizationMessage) &&
info.hasAsyncDependencyTooltip)
info.passBlock.AddToClassList(Classes.kPassSynchronizationMessageIndicator);
}
// Add grid line highlight
if (highlightOptions.HasFlag(PassHighlightOptions.PassGridLines))
{
var firstVisiblePassInfo = visiblePassInfos[0];
firstVisiblePassInfo.leftGridLine.AddToClassList(Classes.kGridLineHighlight);
int nextVisiblePassIndex = FindNextVisiblePassIndex(visiblePassInfos[^1].passId + 1);
if (nextVisiblePassIndex != -1)
m_PassElementsInfo[nextVisiblePassIndex].leftGridLine.AddToClassList(Classes.kGridLineHighlight);
else
rootVisualElement.Query(classes: Classes.kGridLine).Last()
.AddToClassList(Classes.kGridLineHighlight);
}
if (highlightOptions.HasFlag(PassHighlightOptions.ResourceGridFocusOverlay))
{
int firstPassIndex = FindNextVisiblePassIndex(groupedPassIds[0]);
int afterLastPassIndex = FindNextVisiblePassIndex(groupedPassIds[^1] + 1);
int focusOverlayHeightPx = m_ResourceElementsInfo.Count * kResourceRowHeightPx + kResourceGridMarginTopPx;
int leftWidth = firstPassIndex * kPassWidthPx;
int rightWidth = (m_PassElementsInfo.Count - afterLastPassIndex) * kPassWidthPx;
VisualElement left = new VisualElement();
left.AddToClassList(Classes.kResourceGridFocusOverlay);
left.style.marginTop = kResourceGridMarginTopPx;
left.style.width = leftWidth;
left.style.height = focusOverlayHeightPx;
left.pickingMode = PickingMode.Ignore;
VisualElement right = new VisualElement();
right.AddToClassList(Classes.kResourceGridFocusOverlay);
right.style.marginTop = kResourceGridMarginTopPx;
right.style.marginLeft = afterLastPassIndex * kPassWidthPx;
right.style.width = rightWidth;
right.style.height = focusOverlayHeightPx;
right.pickingMode = PickingMode.Ignore;
var resourceGridScrollView = rootVisualElement.Q<ScrollView>(Names.kResourceGridScrollView);
resourceGridScrollView.Add(left);
resourceGridScrollView.Add(right);
}
}
void ToggleCompatiblePassAnimation(TransitionEndEvent _, VisualElement element)
{
element.ToggleInClassList(Classes.kPassCompatibilityMessageIndicatorCompatible);
}
// Returns a list of passes containing the pass itself and potentially others (if merging happened)
List<int> GetGroupedPassIds(int passId)
{
var pass = m_CurrentDebugData.passList[passId];
return pass.nrpInfo?.nativePassInfo?.mergedPassIds ?? new List<int> { passId };
}
bool SelectResource(int visibleResourceIndex, int visiblePassIndex)
{
bool validResourceIndex = visibleResourceIndex >= 0 && visibleResourceIndex < m_ResourceElementsInfo.Count;
if (!validResourceIndex)
return false;
var resInfo = m_ResourceElementsInfo[visibleResourceIndex];
if (m_VisiblePassIndexToPassId.TryGetValue(visiblePassIndex, out int passId))
{
if (passId >= resInfo.firstPassId && passId <= resInfo.lastPassId)
{
ScrollToResource(visibleResourceIndex);
return true;
}
}
return false;
}
void SelectPass(int visiblePassIndex)
{
m_CurrentSelectedVisiblePassIndex = visiblePassIndex;
const PassHighlightOptions opts = PassHighlightOptions.PassTitle |
PassHighlightOptions.PassBlockFill |
PassHighlightOptions.PassGridLines |
PassHighlightOptions.PassBlockBorder |
PassHighlightOptions.ResourceRWBlocks |
PassHighlightOptions.PassesWithCompatibilityMessage |
PassHighlightOptions.PassesWithSynchronizationMessage |
PassHighlightOptions.ResourceGridFocusOverlay;
ClearPassHighlight(opts);
ResetPassBlockState();
if (m_VisiblePassIndexToPassId.TryGetValue(visiblePassIndex, out int passId))
{
var selectedPassIds = GetGroupedPassIds(passId);
UpdatePassBlocksToSelectedState(selectedPassIds);
SetPassHighlight(visiblePassIndex, opts);
ScrollToPass(m_PassIdToVisiblePassIndex[selectedPassIds[0]]);
}
}
void HoverPass(int visiblePassIndex, int visibleResourceIndex)
{
if (m_CurrentHoveredVisiblePassIndex != visiblePassIndex ||
m_CurrentHoveredVisibleResourceIndex != visibleResourceIndex)
{
var highlight = PassHighlightOptions.PassBlockBorder |
PassHighlightOptions.PassGridLines |
PassHighlightOptions.PassBlockFill |
PassHighlightOptions.PassTitleHover;
if (m_CurrentSelectedVisiblePassIndex != -1)
{
// Don't highlight or clear these when a pass is selected
highlight &= ~(PassHighlightOptions.PassTitle | PassHighlightOptions.PassBlockFill |
PassHighlightOptions.PassBlockBorder | PassHighlightOptions.PassGridLines);
}
if (m_CurrentHoveredVisiblePassIndex != -1)
ClearPassHighlight(highlight);
if (visibleResourceIndex != -1) // Don't highlight these while mouse is on resource grid
highlight &= ~(PassHighlightOptions.PassBlockFill | PassHighlightOptions.PassTitleHover);
if (visiblePassIndex != -1)
SetPassHighlight(visiblePassIndex, highlight);
}
}
void HoverResourceByIndex(int visibleResourceIndex, int visiblePassIndex)
{
if (m_CurrentHoveredVisibleResourceIndex != visibleResourceIndex ||
m_CurrentHoveredVisiblePassIndex != visiblePassIndex)
{
var highlight = ResourceHighlightOptions.ResourceUsageRangeBorder | ResourceHighlightOptions.ResourceHelperLine;
if (m_CurrentHoveredVisibleResourceIndex >= 0 &&
m_CurrentHoveredVisibleResourceIndex < m_ResourceElementsInfo.Count)
{
ClearResourceHighlight(highlight);
rootVisualElement.Q(Names.kHoverOverlay).AddToClassList(PanManipulator.k_ContentPanClassName);
m_PanManipulator.canStartDragging = true;
}
if (visibleResourceIndex != -1)
{
var info = m_ResourceElementsInfo[visibleResourceIndex];
if (m_VisiblePassIndexToPassId.TryGetValue(visiblePassIndex, out int passId))
{
bool disablePanning = false;
var passInfo = m_PassElementsInfo[visiblePassIndex];
foreach (var res in passInfo.resourceBlocks)
{
if (res.visibleResourceIndex == visibleResourceIndex &&
res.usage.HasFlag(ResourceRWBlock.UsageFlags.UpdatesGlobalResource))
{
disablePanning = true;
}
}
if (passId >= info.firstPassId && passId <= info.lastPassId)
{
SetResourceHighlight(info, visibleResourceIndex, highlight);
disablePanning = true;
}
if (disablePanning)
{
rootVisualElement.Q(Names.kHoverOverlay)
.RemoveFromClassList(PanManipulator.k_ContentPanClassName);
m_PanManipulator.canStartDragging = false;
}
}
}
}
}
void HoverResourceGrid(int visiblePassIndex, int visibleResourceIndex)
{
if (m_PanManipulator is { dragActive: true })
{
visiblePassIndex = -1;
visibleResourceIndex = -1;
}
HoverPass(visiblePassIndex, visibleResourceIndex);
HoverResourceByIndex(visibleResourceIndex, visiblePassIndex);
m_CurrentHoveredVisiblePassIndex = visiblePassIndex;
m_CurrentHoveredVisibleResourceIndex = visibleResourceIndex;
}
void GetVisiblePassAndResourceIndex(Vector2 pos, out int visiblePassIndex, out int visibleResourceIndex)
{
visiblePassIndex = Math.Min(Mathf.FloorToInt(pos.x / kPassWidthPx), m_PassElementsInfo.Count - 1);
visibleResourceIndex = Math.Min(Mathf.FloorToInt(pos.y / kResourceRowHeightPx),
m_ResourceElementsInfo.Count - 1);
}
void ResourceGridHovered(MouseMoveEvent evt)
{
GetVisiblePassAndResourceIndex(evt.localMousePosition, out int visiblePassIndex,
out int visibleResourceIndex);
HoverResourceGrid(visiblePassIndex, visibleResourceIndex);
}
void ResourceGridClicked(ClickEvent evt)
{
GetVisiblePassAndResourceIndex(evt.localPosition, out int visiblePassIndex, out int visibleResourceIndex);
bool selectedResource = SelectResource(visibleResourceIndex, visiblePassIndex);
// Also select pass when clicking on resource
if (selectedResource)
SelectPass(visiblePassIndex);
// Clicked grid background, clear selection
if (!selectedResource)
DeselectPass();
evt.StopImmediatePropagation(); // Required because to root element click deselects
}
void PassBlockClicked(ClickEvent evt, int visiblePassIndex)
{
SelectPass(visiblePassIndex);
evt.StopImmediatePropagation(); // Required because to root element click deselects
}
void ResourceGridTooltipDisplayed(TooltipEvent evt)
{
evt.tooltip = string.Empty;
if (m_CurrentHoveredVisibleResourceIndex != -1 && m_CurrentHoveredVisiblePassIndex != -1)
{
var passInfo = m_PassElementsInfo[m_CurrentHoveredVisiblePassIndex];
var resourceInfo = m_ResourceElementsInfo[m_CurrentHoveredVisibleResourceIndex];
var passId = m_VisiblePassIndexToPassId[m_CurrentHoveredVisiblePassIndex];
foreach (var rwBlock in passInfo.resourceBlocks)
{
if (rwBlock.visibleResourceIndex == m_CurrentHoveredVisibleResourceIndex)
{
evt.tooltip = rwBlock.tooltip;
evt.rect = rwBlock.element.worldBound;
break;
}
}
if (evt.tooltip == string.Empty &&
passId >= resourceInfo.firstPassId && passId <= resourceInfo.lastPassId)
{
evt.tooltip = "Resource is alive but not used by this pass.";
evt.rect = resourceInfo.usageRangeBlock.worldBound;
}
}
evt.StopPropagation();
}
void DeselectPass()
{
SelectPass(-1);
m_CurrentHoveredVisiblePassIndex = -1;
m_CurrentHoveredVisibleResourceIndex = -1;
}
void KeyPressed(KeyUpEvent evt)
{
if (evt.keyCode == KeyCode.Escape)
DeselectPass();
}
void ClearEmptyStateMessage()
{
rootVisualElement.Q<VisualElement>(Names.kContentContainer).style.display = DisplayStyle.Flex;
rootVisualElement.Q<VisualElement>(Names.kEmptyStateMessage).style.display = DisplayStyle.None;
}
void SetEmptyStateMessage(EmptyStateReason reason)
{
rootVisualElement.Q<VisualElement>(Names.kContentContainer).style.display = DisplayStyle.None;
var emptyStateElement = rootVisualElement.Q<VisualElement>(Names.kEmptyStateMessage);
emptyStateElement.style.display = DisplayStyle.Flex;
if (emptyStateElement[0] is TextElement emptyStateText)
emptyStateText.text = $"{kEmptyStateMessages[(int) reason]}";
}
void OnAutoPlayStatusChanged(ChangeEvent<bool> evt)
{
var autoPlayToggle = rootVisualElement.Q<ToolbarToggle>(Names.kAutoPauseToggle);
autoPlayToggle.text = evt.newValue ? L10n.Tr("Auto Update") : L10n.Tr("Pause");
m_Paused = evt.newValue;
if (!m_Paused && !m_IsDeviceConnected && m_ConnectedDeviceName != k_EditorName)
{
ConnectDebugSession<RenderGraphEditorLocalDebugSession>();
}
UpdateStatusLabel();
// Force update when unpausing
if (!m_Paused)
UpdateCurrentDebugData();
}
// Helper method to check if an enum value has a specific flag.
bool HasFlag<T>(T value, T flag) where T : Enum
{
return (Convert.ToInt32(value) & Convert.ToInt32(flag)) == Convert.ToInt32(flag);
}
// Need to keep track of the ToggleDropdown event handlers to be able to remove them when rebuilding the UI
readonly Dictionary<ToggleDropdown, Action<bool>> m_ToggleHandlers = new();
readonly Dictionary<ToggleDropdown, Action<int[]>> m_SelectionHandlers = new();
void BuildEnumFlagsToggleDropdown<T>(ToggleDropdown dropdown, T currentValue, string prefsKey, Action<T> setValue, bool defaultEnabled = true) where T : Enum
{
if (!HasValidDebugData)
{
dropdown.style.display = DisplayStyle.None;
return;
}
dropdown.style.display = DisplayStyle.Flex;
// Unsubscribe old handlers if present
if (m_ToggleHandlers.TryGetValue(dropdown, out var oldToggleChanged))
dropdown.toggleChanged -= oldToggleChanged;
if (m_SelectionHandlers.TryGetValue(dropdown, out var oldSelectionChanged))
dropdown.selectionChanged -= oldSelectionChanged;
Array enumValues = Enum.GetValues(typeof(T));
List<string> optionNames = new List<string>();
List<T> values = new List<T>();
for (int i = 0; i < enumValues.Length; i++)
{
T value = (T)enumValues.GetValue(i);
int intValue = Convert.ToInt32(value);
if (intValue != 0)
{
values.Add(value);
optionNames.Add(ObjectNames.NicifyVariableName(value.ToString()));
}
}
dropdown.SetOptions(optionNames.ToArray());
var selectedIndices = new List<int>();
for (int i = 0; i < values.Count; i++)
{
if (HasFlag(currentValue, values[i]))
{
selectedIndices.Add(i);
}
}
dropdown.SetSelectedIndices(selectedIndices.ToArray());
bool isEnabled = GetFilterEnabledState(prefsKey, defaultEnabled);
dropdown.SetEnabled(isEnabled);
UpdateFilterEnabledState(prefsKey, isEnabled);
Action<bool> toggleChanged = enabled =>
{
UpdateFilterEnabledState(prefsKey, enabled);
SaveFilterEnabledState(prefsKey, enabled);
RebuildGraphViewerUI();
};
Action<int[]> selectionChanged = indices =>
{
int newValueInt = 0;
foreach (int index in indices)
{
if (index >= 0 && index < values.Count)
{
newValueInt |= Convert.ToInt32(values[index]);
}
}
var newValue = (T)Enum.ToObject(typeof(T), newValueInt);
setValue(newValue);
EditorPrefs.SetInt(prefsKey, newValueInt);
if (dropdown.value)
{
RebuildGraphViewerUI();
}
};
m_ToggleHandlers[dropdown] = toggleChanged;