forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameDebugger.cs
More file actions
1534 lines (1303 loc) · 62 KB
/
Copy pathFrameDebugger.cs
File metadata and controls
1534 lines (1303 loc) · 62 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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEditorInternal;
using System.Runtime.InteropServices;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
namespace UnityEditorInternal
{
// match enum FrameEventType on C++ side!
// match kFrameEventTypeNames names array!
internal enum FrameEventType
{
// ReSharper disable InconsistentNaming
ClearNone = 0,
ClearColor,
ClearDepth,
ClearColorDepth,
ClearStencil,
ClearColorStencil,
ClearDepthStencil,
ClearAll,
SetRenderTarget,
ResolveRT,
ResolveDepth,
GrabIntoRT,
StaticBatch,
DynamicBatch,
Mesh,
DynamicGeometry,
GLDraw,
SkinOnGPU,
DrawProcedural,
ComputeDispatch,
PluginEvent,
InstancedMesh,
BeginRenderpass,
NextSubpass,
EndSubpass
// ReSharper restore InconsistentNaming
};
internal enum ShowAdditionalInfo
{
Preview,
ShaderProperties
};
// Match C++ ScriptingShaderFloatInfo memory layout!
[StructLayout(LayoutKind.Sequential)]
internal struct ShaderFloatInfo
{
public string name;
public int flags;
public float value;
}
// Match C++ ScriptingShaderVectorInfo memory layout!
[StructLayout(LayoutKind.Sequential)]
internal struct ShaderVectorInfo
{
public string name;
public int flags;
public Vector4 value;
}
// Match C++ ScriptingShaderMatrixInfo memory layout!
[StructLayout(LayoutKind.Sequential)]
internal struct ShaderMatrixInfo
{
public string name;
public int flags;
public Matrix4x4 value;
}
// Match C++ ScriptingShaderTextureInfo memory layout!
[StructLayout(LayoutKind.Sequential)]
internal struct ShaderTextureInfo
{
public string name;
public int flags;
public string textureName;
public Texture value;
}
// Match C++ ScriptingShaderBufferInfo memory layout!
[StructLayout(LayoutKind.Sequential)]
internal struct ShaderBufferInfo
{
public string name;
public int flags;
}
// Match C++ ScriptingShaderProperties memory layout!
[StructLayout(LayoutKind.Sequential)]
internal struct ShaderProperties
{
public ShaderFloatInfo[] floats;
public ShaderVectorInfo[] vectors;
public ShaderMatrixInfo[] matrices;
public ShaderTextureInfo[] textures;
public ShaderBufferInfo[] buffers;
}
// Match C++ ScriptingFrameDebuggerEventData memory layout!
[StructLayout(LayoutKind.Sequential)]
internal struct FrameDebuggerEventData
{
public int frameEventIndex;
public int vertexCount;
public int indexCount;
public int instanceCount;
public int drawCallCount;
public string shaderName;
public string passName;
public string passLightMode;
public int shaderInstanceID;
public int subShaderIndex;
public int shaderPassIndex;
public string shaderKeywords;
public int rendererInstanceID;
public Mesh mesh;
public int meshInstanceID;
public int meshSubset;
// state for compute shader dispatches
public int csInstanceID;
public string csName;
public string csKernel;
public int csThreadGroupsX;
public int csThreadGroupsY;
public int csThreadGroupsZ;
// active render target info
public string rtName;
public int rtWidth;
public int rtHeight;
public int rtFormat;
public int rtDim;
public int rtFace;
public short rtCount;
public short rtHasDepthTexture;
// shader state and properties
public FrameDebuggerBlendState blendState;
public FrameDebuggerRasterState rasterState;
public FrameDebuggerDepthState depthState;
public FrameDebuggerStencilState stencilState;
public int stencilRef;
public int batchBreakCause;
public ShaderProperties shaderProperties;
}
// Match C++ MonoFrameDebuggerEvent memory layout!
[StructLayout(LayoutKind.Sequential)]
internal struct FrameDebuggerEvent
{
public FrameEventType type;
public GameObject gameObject;
}
// Match C++ ScriptingFrameDebuggerBlendState memory layout!
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
internal struct FrameDebuggerBlendState
{
public uint writeMask;
public BlendMode srcBlend;
public BlendMode dstBlend;
public BlendMode srcBlendAlpha;
public BlendMode dstBlendAlpha;
public BlendOp blendOp;
public BlendOp blendOpAlpha;
}
// Match C++ ScriptingFrameDebuggerRasterState memory layout!
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
struct FrameDebuggerRasterState
{
public CullMode cullMode;
public int depthBias;
public bool depthClip;
public float slopeScaledDepthBias;
};
// Match C++ ScriptingFrameDebuggerDepthState memory layout!
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
struct FrameDebuggerDepthState
{
public int depthWrite;
public CompareFunction depthFunc;
};
// Match C++ ScriptingFrameDebuggerStencilState memory layout!
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
struct FrameDebuggerStencilState
{
public bool stencilEnable;
public byte readMask;
public byte writeMask;
public byte padding;
public CompareFunction stencilFuncFront;
public StencilOp stencilPassOpFront;
public StencilOp stencilFailOpFront;
public StencilOp stencilZFailOpFront;
public CompareFunction stencilFuncBack;
public StencilOp stencilPassOpBack;
public StencilOp stencilFailOpBack;
public StencilOp stencilZFailOpBack;
};
}
namespace UnityEditor
{
internal class FrameDebuggerWindow : EditorWindow
{
// match enum FrameEventType on C++ side!
static public readonly string[] s_FrameEventTypeNames = new[]
{
"Clear (nothing)",
"Clear (color)",
"Clear (Z)",
"Clear (color+Z)",
"Clear (stencil)",
"Clear (color+stencil)",
"Clear (Z+stencil)",
"Clear (color+Z+stencil)",
"SetRenderTarget",
"Resolve Color",
"Resolve Depth",
"Grab RenderTexture",
"Static Batch",
"Dynamic Batch",
"Draw Mesh",
"Draw Dynamic",
"Draw GL",
"GPU Skinning",
"Draw Procedural",
"Compute Shader",
"Plugin Event",
"Draw Mesh (instanced)",
"Begin Renderpass",
"Next Subpass",
"End Renderpass"
};
// Cached strings built from FrameDebuggerEventData.
// Only need to rebuild them when event data actually changes.
private struct EventDataStrings
{
public string drawCallCount;
public string shader;
public string pass;
public string stencilRef;
public string stencilReadMask;
public string stencilWriteMask;
public string stencilComp;
public string stencilPass;
public string stencilFail;
public string stencilZFail;
public string[] texturePropertyTooltips;
}
const float kScrollbarWidth = 16;
const float kResizerWidth = 5f;
const float kMinListWidth = 200f;
const float kMinDetailsWidth = 200f;
const float kMinWindowWidth = 240f;
const float kDetailsMargin = 4f;
const float kMinPreviewSize = 64f;
const string kFloatFormat = "g2";
const string kFloatDetailedFormat = "g7";
const float kShaderPropertiesIndention = 15.0f;
const float kNameFieldWidth = 200.0f;
const float kValueFieldWidth = 200.0f;
const float kArrayValuePopupBtnWidth = 25.0f;
// See the comments for BaseParamInfo in FrameDebuggerInternal.h
const int kShaderTypeBits = 6;
const int kArraySizeBitMask = 0x3FF;
// Sometimes when disabling the frame debugger, the UI does not update automatically -
// the repaint happens, but we still think there's zero events present
// (on Mac at least). Haven't figured out why, so whenever changing the
// enable/limit state, just repaint a couple of times. Yeah...
const int kNeedToRepaintFrames = 4;
[SerializeField]
float m_ListWidth = kMinListWidth * 1.5f;
private int m_RepaintFrames = kNeedToRepaintFrames;
// Mesh preview
PreviewRenderUtility m_PreviewUtility;
public Vector2 m_PreviewDir = new Vector2(120, -20);
private Material m_Material;
private Material m_WireMaterial;
// Frame events tree view
[SerializeField]
TreeViewState m_TreeViewState;
[NonSerialized]
FrameDebuggerTreeView m_Tree;
[NonSerialized] private int m_FrameEventsHash;
// Render target view options
[NonSerialized] int m_RTIndex;
[NonSerialized] int m_RTChannel;
[NonSerialized] private float m_RTBlackLevel;
[NonSerialized] private float m_RTWhiteLevel = 1.0f;
private int m_PrevEventsLimit = 0;
private int m_PrevEventsCount = 0;
private FrameDebuggerEventData m_CurEventData;
private uint m_CurEventDataHash = 0;
private EventDataStrings m_CurEventDataStrings;
// Shader Properties
private Vector2 m_ScrollViewShaderProps = Vector2.zero;
private ShowAdditionalInfo m_AdditionalInfo = ShowAdditionalInfo.ShaderProperties;
private GUIContent[] m_AdditionalInfoGuiContents = Enum.GetNames(typeof(ShowAdditionalInfo)).Select(m => new GUIContent(m)).ToArray();
static List<FrameDebuggerWindow> s_FrameDebuggers = new List<FrameDebuggerWindow>();
private AttachProfilerUI m_AttachProfilerUI = new AttachProfilerUI();
[MenuItem("Window/Frame Debugger", false, 2100)]
public static FrameDebuggerWindow ShowFrameDebuggerWindow()
{
var wnd = GetWindow(typeof(FrameDebuggerWindow)) as FrameDebuggerWindow;
if (wnd != null)
{
wnd.titleContent = EditorGUIUtility.TrTextContent("Frame Debug");
}
return wnd;
}
internal static void RepaintAll()
{
foreach (var fd in s_FrameDebuggers)
{
fd.Repaint();
}
}
public FrameDebuggerWindow()
{
position = new Rect(50, 50, (kMinListWidth + kMinDetailsWidth) * 1.5f, 350f);
minSize = new Vector2(kMinListWidth + kMinDetailsWidth, 200);
}
internal void ChangeFrameEventLimit(int newLimit)
{
if (newLimit <= 0 || newLimit > FrameDebuggerUtility.count)
{
return;
}
if (newLimit != FrameDebuggerUtility.limit && newLimit > 0)
{
GameObject go = FrameDebuggerUtility.GetFrameEventGameObject(newLimit - 1);
if (go != null)
EditorGUIUtility.PingObject(go);
}
FrameDebuggerUtility.limit = newLimit;
if (m_Tree != null)
m_Tree.SelectFrameEventIndex(newLimit);
}
static void DisableFrameDebugger()
{
if (FrameDebuggerUtility.IsLocalEnabled())
{
// if it was true before, we disabled and ask the game scene to repaint
EditorApplication.SetSceneRepaintDirty();
}
FrameDebuggerUtility.SetEnabled(false, FrameDebuggerUtility.GetRemotePlayerGUID());
}
internal void OnDidOpenScene()
{
DisableFrameDebugger();
}
void OnPauseStateChanged(PauseState state)
{
RepaintOnLimitChange();
}
void OnPlayModeStateChanged(PlayModeStateChange state)
{
RepaintOnLimitChange();
}
internal void OnEnable()
{
autoRepaintOnSceneChange = true;
s_FrameDebuggers.Add(this);
EditorApplication.pauseStateChanged += OnPauseStateChanged;
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
m_RepaintFrames = kNeedToRepaintFrames;
}
internal void OnDisable()
{
if (m_WireMaterial != null)
{
DestroyImmediate(m_WireMaterial, true);
}
if (m_PreviewUtility != null)
{
m_PreviewUtility.Cleanup();
m_PreviewUtility = null;
}
s_FrameDebuggers.Remove(this);
EditorApplication.pauseStateChanged -= OnPauseStateChanged;
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
DisableFrameDebugger();
}
public void EnableIfNeeded()
{
if (FrameDebuggerUtility.IsLocalEnabled() || FrameDebuggerUtility.IsRemoteEnabled())
return;
m_RTChannel = 0;
m_RTIndex = 0;
m_RTBlackLevel = 0.0f;
m_RTWhiteLevel = 1.0f;
ClickEnableFrameDebugger();
RepaintOnLimitChange();
}
private void ClickEnableFrameDebugger()
{
bool isEnabled = FrameDebuggerUtility.IsLocalEnabled() || FrameDebuggerUtility.IsRemoteEnabled();
bool enablingLocally = !isEnabled && m_AttachProfilerUI.IsEditor();
if (enablingLocally && !FrameDebuggerUtility.locallySupported)
return;
if (enablingLocally)
{
// pause play mode if needed
if (EditorApplication.isPlaying && !EditorApplication.isPaused)
EditorApplication.isPaused = true;
}
if (!isEnabled)
FrameDebuggerUtility.SetEnabled(true, ProfilerDriver.connectedProfiler);
else
FrameDebuggerUtility.SetEnabled(false, FrameDebuggerUtility.GetRemotePlayerGUID());
// Make sure game view is visible when enabling frame debugger locally
if (FrameDebuggerUtility.IsLocalEnabled())
{
GameView gameView = (GameView)WindowLayout.FindEditorWindowOfType(typeof(GameView));
if (gameView)
{
gameView.ShowTab();
}
}
m_PrevEventsLimit = FrameDebuggerUtility.limit;
m_PrevEventsCount = FrameDebuggerUtility.count;
}
// Only call this when m_CurEventData changes.
void BuildCurEventDataStrings()
{
// we will show that only if drawcall count is bigger than one so update unconditionally
m_CurEventDataStrings.drawCallCount = string.Format("{0}", m_CurEventData.drawCallCount);
// shader name & subshader index
m_CurEventDataStrings.shader = string.Format("{0}, SubShader #{1}", m_CurEventData.shaderName, m_CurEventData.subShaderIndex.ToString());
// pass name & LightMode tag
string passName = string.IsNullOrEmpty(m_CurEventData.passName) ? "#" + m_CurEventData.shaderPassIndex.ToString() : m_CurEventData.passName;
string lightMode = string.IsNullOrEmpty(m_CurEventData.passLightMode) ? "" : string.Format(" ({0})", m_CurEventData.passLightMode);
m_CurEventDataStrings.pass = passName + lightMode;
// stencil states
if (m_CurEventData.stencilState.stencilEnable)
{
m_CurEventDataStrings.stencilRef = m_CurEventData.stencilRef.ToString();
if (m_CurEventData.stencilState.readMask != 255)
m_CurEventDataStrings.stencilReadMask = m_CurEventData.stencilState.readMask.ToString();
if (m_CurEventData.stencilState.writeMask != 255)
m_CurEventDataStrings.stencilWriteMask = m_CurEventData.stencilState.writeMask.ToString();
// Only show *Front states when CullMode is set to Back.
// Only show *Back states when CullMode is set to Front.
// Show both *Front and *Back states for two-sided geometry.
if (m_CurEventData.rasterState.cullMode == CullMode.Back)
{
m_CurEventDataStrings.stencilComp = m_CurEventData.stencilState.stencilFuncFront.ToString();
m_CurEventDataStrings.stencilPass = m_CurEventData.stencilState.stencilPassOpFront.ToString();
m_CurEventDataStrings.stencilFail = m_CurEventData.stencilState.stencilFailOpFront.ToString();
m_CurEventDataStrings.stencilZFail = m_CurEventData.stencilState.stencilZFailOpFront.ToString();
}
else if (m_CurEventData.rasterState.cullMode == CullMode.Front)
{
m_CurEventDataStrings.stencilComp = m_CurEventData.stencilState.stencilFuncBack.ToString();
m_CurEventDataStrings.stencilPass = m_CurEventData.stencilState.stencilPassOpBack.ToString();
m_CurEventDataStrings.stencilFail = m_CurEventData.stencilState.stencilFailOpBack.ToString();
m_CurEventDataStrings.stencilZFail = m_CurEventData.stencilState.stencilZFailOpBack.ToString();
}
else
{
m_CurEventDataStrings.stencilComp =
string.Format("{0} {1}", m_CurEventData.stencilState.stencilFuncFront.ToString(), m_CurEventData.stencilState.stencilFuncBack.ToString());
m_CurEventDataStrings.stencilPass =
string.Format("{0} {1}", m_CurEventData.stencilState.stencilPassOpFront.ToString(), m_CurEventData.stencilState.stencilPassOpBack.ToString());
m_CurEventDataStrings.stencilFail =
string.Format("{0} {1}", m_CurEventData.stencilState.stencilFailOpFront.ToString(), m_CurEventData.stencilState.stencilFailOpBack.ToString());
m_CurEventDataStrings.stencilZFail =
string.Format("{0} {1}", m_CurEventData.stencilState.stencilZFailOpFront.ToString(), m_CurEventData.stencilState.stencilZFailOpBack.ToString());
}
}
// texture property tooltips
ShaderTextureInfo[] textureInfoArray = m_CurEventData.shaderProperties.textures;
m_CurEventDataStrings.texturePropertyTooltips = new string[textureInfoArray.Length];
StringBuilder tooltip = new StringBuilder();
for (int i = 0; i < textureInfoArray.Length; ++i)
{
Texture texture = textureInfoArray[i].value;
if (texture == null)
continue;
tooltip.Clear();
tooltip.AppendFormat("Size: {0} x {1}", texture.width.ToString(), texture.height.ToString());
tooltip.AppendFormat("\nDimension: {0}", texture.dimension.ToString());
string formatFormat = "\nFormat: {0}";
string depthFormat = "\nDepth: {0}";
if (texture is Texture2D)
tooltip.AppendFormat(formatFormat, (texture as Texture2D).format.ToString());
else if (texture is Cubemap)
tooltip.AppendFormat(formatFormat, (texture as Cubemap).format.ToString());
else if (texture is Texture2DArray)
{
tooltip.AppendFormat(formatFormat, (texture as Texture2DArray).format.ToString());
tooltip.AppendFormat(depthFormat, (texture as Texture2DArray).depth.ToString());
}
else if (texture is Texture3D)
{
tooltip.AppendFormat(formatFormat, (texture as Texture3D).format.ToString());
tooltip.AppendFormat(depthFormat, (texture as Texture3D).depth.ToString());
}
else if (texture is CubemapArray)
{
tooltip.AppendFormat(formatFormat, (texture as CubemapArray).format.ToString());
tooltip.AppendFormat("\nCubemap Count: {0}", (texture as CubemapArray).cubemapCount.ToString());
}
else if (texture is RenderTexture)
{
tooltip.AppendFormat("\nRT Format: {0}", (texture as RenderTexture).format.ToString());
}
tooltip.Append("\n\nCtrl + Click to show preview");
m_CurEventDataStrings.texturePropertyTooltips[i] = tooltip.ToString();
}
}
// Return true if should repaint
private bool DrawToolbar(FrameDebuggerEvent[] descs)
{
bool repaint = false;
bool isSupported = !m_AttachProfilerUI.IsEditor() || FrameDebuggerUtility.locallySupported;
GUILayout.BeginHorizontal(EditorStyles.toolbar);
// enable toggle
EditorGUI.BeginChangeCheck();
using (new EditorGUI.DisabledScope(!isSupported))
{
GUILayout.Toggle(FrameDebuggerUtility.IsLocalEnabled() || FrameDebuggerUtility.IsRemoteEnabled(), styles.recordButton, EditorStyles.toolbarButton, GUILayout.MinWidth(80));
}
if (EditorGUI.EndChangeCheck())
{
ClickEnableFrameDebugger();
repaint = true;
}
m_AttachProfilerUI.OnGUILayout(this);
bool isAnyEnabled = FrameDebuggerUtility.IsLocalEnabled() || FrameDebuggerUtility.IsRemoteEnabled();
if (isAnyEnabled && ProfilerDriver.connectedProfiler != FrameDebuggerUtility.GetRemotePlayerGUID())
{
// Switch from local to remote debugger or vice versa
FrameDebuggerUtility.SetEnabled(false, FrameDebuggerUtility.GetRemotePlayerGUID());
FrameDebuggerUtility.SetEnabled(true, ProfilerDriver.connectedProfiler);
}
GUI.enabled = isAnyEnabled;
// event limit slider
EditorGUI.BeginChangeCheck();
int newLimit;
using (new EditorGUI.DisabledScope(FrameDebuggerUtility.count <= 1))
{
newLimit = EditorGUILayout.IntSlider(FrameDebuggerUtility.limit, 1, FrameDebuggerUtility.count);
}
if (EditorGUI.EndChangeCheck())
{
ChangeFrameEventLimit(newLimit);
}
GUILayout.Label(" of " + FrameDebuggerUtility.count, EditorStyles.miniLabel);
// prev/next buttons
using (new EditorGUI.DisabledScope(newLimit <= 1))
{
if (GUILayout.Button(styles.prevFrame, EditorStyles.toolbarButton))
{
ChangeFrameEventLimit(newLimit - 1);
}
}
using (new EditorGUI.DisabledScope(newLimit >= FrameDebuggerUtility.count))
{
if (GUILayout.Button(styles.nextFrame, EditorStyles.toolbarButton))
{
ChangeFrameEventLimit(newLimit + 1);
}
// If we had last event selected, and something changed in the scene so that
// number of events is different - then try to keep the last event selected.
if (m_PrevEventsLimit == m_PrevEventsCount)
{
if (FrameDebuggerUtility.count != m_PrevEventsCount && FrameDebuggerUtility.limit == m_PrevEventsLimit)
{
ChangeFrameEventLimit(FrameDebuggerUtility.count);
}
}
m_PrevEventsLimit = FrameDebuggerUtility.limit;
m_PrevEventsCount = FrameDebuggerUtility.count;
}
GUILayout.EndHorizontal();
return repaint;
}
private void DrawMeshPreview(Rect previewRect, Rect meshInfoRect, Mesh mesh, int meshSubset)
{
if (m_PreviewUtility == null)
{
m_PreviewUtility = new PreviewRenderUtility();
m_PreviewUtility.camera.fieldOfView = 30.0f;
}
if (m_Material == null)
m_Material = Material.GetDefaultMaterial();
if (m_WireMaterial == null)
{
m_WireMaterial = ModelInspector.CreateWireframeMaterial();
}
m_PreviewUtility.BeginPreview(previewRect, "preBackground");
ModelInspector.RenderMeshPreview(mesh, m_PreviewUtility, m_Material, m_WireMaterial, m_PreviewDir, meshSubset);
m_PreviewUtility.EndAndDrawPreview(previewRect);
// mesh info
string meshName = mesh.name;
if (string.IsNullOrEmpty(meshName))
meshName = "<no name>";
string info = meshName + " subset " + meshSubset + "\n" + m_CurEventData.vertexCount + " verts, " + m_CurEventData.indexCount + " indices";
if (m_CurEventData.instanceCount > 1)
info += ", " + m_CurEventData.instanceCount + " instances";
EditorGUI.DropShadowLabel(meshInfoRect, info);
}
// Draw any mesh associated with the draw event
// Return false if no mesh.
private bool DrawEventMesh()
{
Mesh mesh = m_CurEventData.mesh;
if (mesh == null)
return false;
Rect previewRect = GUILayoutUtility.GetRect(10, 10, GUILayout.ExpandHeight(true));
if (previewRect.width < kMinPreviewSize || previewRect.height < kMinPreviewSize)
return true;
GameObject go = FrameDebuggerUtility.GetFrameEventGameObject(m_CurEventData.frameEventIndex);
// Info display at bottom (and pings object when clicked)
Rect meshInfoRect = previewRect;
meshInfoRect.yMin = meshInfoRect.yMax - EditorGUIUtility.singleLineHeight * 2;
Rect goInfoRect = meshInfoRect;
meshInfoRect.xMin = meshInfoRect.center.x;
goInfoRect.xMax = goInfoRect.center.x;
if (Event.current.type == EventType.MouseDown)
{
if (meshInfoRect.Contains(Event.current.mousePosition))
{
EditorGUIUtility.PingObject(mesh);
Event.current.Use();
}
if (go != null && goInfoRect.Contains(Event.current.mousePosition))
{
EditorGUIUtility.PingObject(go.GetInstanceID());
Event.current.Use();
}
}
m_PreviewDir = PreviewGUI.Drag2D(m_PreviewDir, previewRect);
if (Event.current.type == EventType.Repaint)
{
int meshSubset = m_CurEventData.meshSubset;
DrawMeshPreview(previewRect, meshInfoRect, mesh, meshSubset);
if (go != null)
{
EditorGUI.DropShadowLabel(goInfoRect, go.name);
}
}
return true;
}
private void DrawRenderTargetControls()
{
FrameDebuggerEventData cur = m_CurEventData;
if (cur.rtWidth <= 0 || cur.rtHeight <= 0)
return;
var isDepthOnlyRT = (cur.rtFormat == (int)RenderTextureFormat.Depth || cur.rtFormat == (int)RenderTextureFormat.Shadowmap);
var hasShowableDepth = (cur.rtHasDepthTexture != 0);
var showableRTCount = cur.rtCount;
if (hasShowableDepth)
showableRTCount++;
EditorGUILayout.LabelField("RenderTarget", cur.rtName);
GUILayout.BeginHorizontal(EditorStyles.toolbar);
// MRT to show
bool rtWasClamped;
EditorGUI.BeginChangeCheck();
using (new EditorGUI.DisabledScope(showableRTCount <= 1))
{
var rtNames = new GUIContent[showableRTCount];
for (var i = 0; i < cur.rtCount; ++i)
{
rtNames[i] = Styles.mrtLabels[i];
}
if (hasShowableDepth)
rtNames[cur.rtCount] = Styles.depthLabel;
var clampedIndex = Mathf.Clamp(m_RTIndex, 0, showableRTCount - 1);
rtWasClamped = (clampedIndex != m_RTIndex);
m_RTIndex = clampedIndex;
m_RTIndex = EditorGUILayout.Popup(m_RTIndex, rtNames, EditorStyles.toolbarPopup, GUILayout.Width(70));
}
// color channels
GUILayout.Space(10);
using (new EditorGUI.DisabledScope(isDepthOnlyRT))
{
GUILayout.Label(Styles.channelHeader, EditorStyles.miniLabel);
m_RTChannel = GUILayout.Toolbar(m_RTChannel, Styles.channelLabels, EditorStyles.toolbarButton);
}
// levels
GUILayout.Space(10);
GUILayout.Label(Styles.levelsHeader, EditorStyles.miniLabel);
EditorGUILayout.MinMaxSlider(ref m_RTBlackLevel, ref m_RTWhiteLevel, 0.0f, 1.0f, GUILayout.MaxWidth(200.0f));
if (EditorGUI.EndChangeCheck() || rtWasClamped)
{
Vector4 mask = Vector4.zero;
if (m_RTChannel == 1)
mask.x = 1f;
else if (m_RTChannel == 2)
mask.y = 1f;
else if (m_RTChannel == 3)
mask.z = 1f;
else if (m_RTChannel == 4)
mask.w = 1f;
else
mask = Vector4.one;
int rtIndexToSet = m_RTIndex;
if (rtIndexToSet >= cur.rtCount)
rtIndexToSet = -1;
FrameDebuggerUtility.SetRenderTargetDisplayOptions(rtIndexToSet, mask, m_RTBlackLevel, m_RTWhiteLevel);
RepaintAllNeededThings();
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.Label(string.Format("{0}x{1} {2}",
cur.rtWidth,
cur.rtHeight,
(RenderTextureFormat)cur.rtFormat));
if (cur.rtDim == (int)UnityEngine.Rendering.TextureDimension.Cube)
GUILayout.Label("Rendering into cubemap");
}
private void DrawEventDrawCallInfo()
{
if (m_CurEventData.drawCallCount > 1)
EditorGUILayout.LabelField("Draw Calls", m_CurEventDataStrings.drawCallCount);
// shader, pass & keyword information
EditorGUILayout.LabelField("Shader", m_CurEventDataStrings.shader);
if (GUI.Button(GUILayoutUtility.GetLastRect(), Styles.selectShaderTooltip, GUI.skin.label))
{
EditorGUIUtility.PingObject(m_CurEventData.shaderInstanceID);
Event.current.Use();
}
EditorGUILayout.LabelField("Pass", m_CurEventDataStrings.pass);
if (!string.IsNullOrEmpty(m_CurEventData.shaderKeywords))
{
EditorGUILayout.LabelField("Keywords", m_CurEventData.shaderKeywords);
if (GUI.Button(GUILayoutUtility.GetLastRect(), Styles.copyToClipboardTooltip, GUI.skin.label))
EditorGUIUtility.systemCopyBuffer = m_CurEventDataStrings.shader + System.Environment.NewLine + m_CurEventData.shaderKeywords;
}
DrawStates();
// Show why this draw call can't batch with the previous one.
if (m_CurEventData.batchBreakCause > 1) // Valid batch break cause enum value on the C++ side starts at 2.
{
GUILayout.Space(10.0f);
GUILayout.Label(Styles.causeOfNewDrawCallLabel, EditorStyles.boldLabel);
GUILayout.Label(styles.batchBreakCauses[m_CurEventData.batchBreakCause], EditorStyles.wordWrappedLabel);
}
GUILayout.Space(15.0f);
// preview / properties
EditorGUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
m_AdditionalInfo = (ShowAdditionalInfo)GUILayout.Toolbar((int)m_AdditionalInfo, m_AdditionalInfoGuiContents, "LargeButton", GUI.ToolbarButtonSize.FitToContents);
GUILayout.FlexibleSpace();
EditorGUILayout.EndHorizontal();
switch (m_AdditionalInfo)
{
case ShowAdditionalInfo.Preview:
// Show mesh preview if possible
if (!DrawEventMesh())
{
// If no mesh preview, then show vertex/index count at least
EditorGUILayout.LabelField("Vertices", m_CurEventData.vertexCount.ToString());
EditorGUILayout.LabelField("Indices", m_CurEventData.indexCount.ToString());
}
break;
case ShowAdditionalInfo.ShaderProperties:
DrawShaderProperties(m_CurEventData.shaderProperties);
break;
}
}
private void DrawEventComputeDispatchInfo()
{
// compute shader & kernel information
EditorGUILayout.LabelField("Compute Shader", m_CurEventData.csName);
if (GUI.Button(GUILayoutUtility.GetLastRect(), GUIContent.none, GUI.skin.label))
{
EditorGUIUtility.PingObject(m_CurEventData.csInstanceID);
Event.current.Use();
}
EditorGUILayout.LabelField("Kernel", m_CurEventData.csKernel);
// dispatch size
string threadGroupsText;
if (m_CurEventData.csThreadGroupsX != 0 || m_CurEventData.csThreadGroupsY != 0 || m_CurEventData.csThreadGroupsZ != 0)
threadGroupsText = string.Format("{0}x{1}x{2}", m_CurEventData.csThreadGroupsX, m_CurEventData.csThreadGroupsY, m_CurEventData.csThreadGroupsZ);
else
threadGroupsText = "indirect dispatch";
EditorGUILayout.LabelField("Thread Groups", threadGroupsText);
}
private void DrawCurrentEvent(Rect rect, FrameDebuggerEvent[] descs)
{
int curEventIndex = FrameDebuggerUtility.limit - 1;
if (curEventIndex < 0 || curEventIndex >= descs.Length)
return;
GUILayout.BeginArea(rect);
uint eventDataHash = FrameDebuggerUtility.eventDataHash;
bool isFrameEventDataValid = curEventIndex == m_CurEventData.frameEventIndex;
if (eventDataHash != 0 && m_CurEventDataHash != eventDataHash)
{
isFrameEventDataValid = FrameDebuggerUtility.GetFrameEventData(curEventIndex, out m_CurEventData);
m_CurEventDataHash = eventDataHash;
BuildCurEventDataStrings();
}
// render target
if (isFrameEventDataValid)
DrawRenderTargetControls();
// event type and draw call info
FrameDebuggerEvent cur = descs[curEventIndex];
GUILayout.Label(string.Format("Event #{0}: {1}", (curEventIndex + 1), s_FrameEventTypeNames[(int)cur.type]), EditorStyles.boldLabel);
if (FrameDebuggerUtility.IsRemoteEnabled() && FrameDebuggerUtility.receivingRemoteFrameEventData)
{
GUILayout.Label("Receiving frame event data...");
}
else if (isFrameEventDataValid) // Is this a draw call?
{
if (m_CurEventData.vertexCount > 0 || m_CurEventData.indexCount > 0)
{
// a draw call, display extra info
DrawEventDrawCallInfo();
}
else if (cur.type == FrameEventType.ComputeDispatch)
{
// a compute dispatch, display extra info
DrawEventComputeDispatchInfo();
}
}
GUILayout.EndArea();
}
void DrawShaderPropertyFlags(int flags)
{
// lowest bits of flags are set for each shader stage that property is used in; matching ShaderType C++ enum
var str = string.Empty;
if ((flags & (1 << 1)) != 0)
str += 'v';
if ((flags & (1 << 2)) != 0)
str += 'f';
if ((flags & (1 << 3)) != 0)
str += 'g';
if ((flags & (1 << 4)) != 0)
str += 'h';
if ((flags & (1 << 5)) != 0)
str += 'd';
GUILayout.Label(str, EditorStyles.miniLabel, GUILayout.MinWidth(20.0f));
}
void ShaderPropertyCopyValueMenu(Rect valueRect, System.Object value)
{
var e = Event.current;
if (e.type == EventType.ContextClick && valueRect.Contains(e.mousePosition))
{
e.Use();
var menu = new GenericMenu();
menu.AddItem(EditorGUIUtility.TrTextContent("Copy value"), false, delegate
{
var str = string.Empty;
if (value is Vector4)
str = ((Vector4)value).ToString(kFloatDetailedFormat);
else if (value is Matrix4x4)
str = ((Matrix4x4)value).ToString(kFloatDetailedFormat);
else if (value is System.Single)
str = ((System.Single)value).ToString(kFloatDetailedFormat);
else
str = value.ToString();
EditorGUIUtility.systemCopyBuffer = str;
});
menu.ShowAsContext();
}