This repository was archived by the owner on Aug 28, 2025. It is now read-only.
forked from baldurk/renderdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBufferViewer.cs
More file actions
3621 lines (2894 loc) · 130 KB
/
BufferViewer.cs
File metadata and controls
3621 lines (2894 loc) · 130 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
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2015-2016 Baldur Karlsson
* Copyright (c) 2014 Crytek
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using renderdocui.Code;
using renderdocui.Windows.Dialogs;
using renderdoc;
namespace renderdocui.Windows
{
// since they're quite similar, the BufferViewer class displays both the geometry mesh
// data as well as raw views of buffers (with custom formatting). See the two different constructors.
//
// When we go to fetch data we do that on a separate thread to parse the byte-stream that comes
// back according to the format (since there can be mismatches between pipeline stages, we always take
// the "output" of each stage.
//
// Once we have the data parsed we flag that and the UI then uses the VirtualMode on the datagridview
// to populate rows lazily as and when they're needed.
//
// The threading is messy and with the 'hiding' of threading details behind invokes for the UI I suspect
// this whole setup is fragile and/or not thread-safe in areas. It would be nice to control all the threading
// explicitly myself but the UI interaction makes that murky, so bear in mind that you need to be able to
// handle changing events while a thread is still going and about to populate some data etc, and be able
// to abort that and start anew without anything breaking or racing.
public partial class BufferViewer : DockContent, ILogViewerForm, IBufferFormatProcessor
{
#region Data Privates
// we try to bundle up data so that as much as possible things don't change out from under a thread
// or invoke and get us into an 'impossible' state.
// This class describes the format/structure that we'll use to interpret the byte stream
private class Input
{
public FormatElement[] BufferFormats = null;
public ResourceId[] Buffers = null;
public object[][] GenericValues = null;
public uint[] Strides = null;
public ulong[] Offsets = null;
public PrimitiveTopology Topology = PrimitiveTopology.Unknown;
public FetchDrawcall Drawcall = null;
public ResourceId IndexBuffer = ResourceId.Null;
public ulong IndexOffset = 0;
public bool IndexRestart = true;
public uint IndexRestartValue = uint.MaxValue;
}
// contains the raw bytes (and any state necessary from the drawcall itself)
private class Dataset
{
public uint IndexCount = 0;
public MeshFormat PostVS;
public PrimitiveTopology Topology = PrimitiveTopology.Unknown;
public byte[][] Buffers = null;
public uint[] Indices = null; // 'displayed' indices from index buffer
public uint[] DataIndices = null; // where to find the data, different only for PostVS
}
// we generate a UIState object with everything needed to populate the actual
// visible data in the UI.
private class UIState
{
public UIState(MeshDataStage stage)
{
m_Stage = stage;
}
public Input m_Input = null;
public MeshDataStage m_Stage = MeshDataStage.VSIn;
public Dataset m_Data = null;
public Stream[] m_Stream = null;
public BinaryReader[] m_Reader = null;
public object[][] m_Rows = null;
public byte[] m_RawData = null;
public uint m_RawStride = 0;
public DataGridView m_GridView = null;
public DockContent m_DockContent = null;
public Thread m_DataParseThread = null;
private Object m_ThreadLock = new Object();
public Vec3f[] m_MinBounds = null;
public Vec3f[] m_MaxBounds = null;
public void AbortThread()
{
lock (m_ThreadLock)
{
if (m_DataParseThread != null)
{
if (m_DataParseThread.ThreadState != ThreadState.Aborted &&
m_DataParseThread.ThreadState != ThreadState.Stopped)
{
m_DataParseThread.Abort();
m_DataParseThread.Join();
}
m_DataParseThread = null;
}
}
}
}
// one UI state for each stage
private UIState m_VSIn = new UIState(MeshDataStage.VSIn);
private UIState m_VSOut = new UIState(MeshDataStage.VSOut);
private UIState m_GSOut = new UIState(MeshDataStage.GSOut);
// this points to the 'highlighted'/current UI state.
private UIState m_ContextUIState = null;
private bool m_Loaded = false;
// this becomes a 'cancel' flag for any in-flight invokes
// to set data. Since we can't cancel then wait on an invoke
// from the UI thread synchronously, we can just increment this
// and anything in flight will bail out as soon as it notices this
// is different.
private int m_ReqID = 0;
private const int CellDefaultWidth = 100;
private int CellFloatWidth = CellDefaultWidth;
private BufferFormatSpecifier m_FormatSpecifier = null;
private string m_FormatText = "";
private UIState GetUIState(MeshDataStage type)
{
if (type == MeshDataStage.VSIn)
return m_VSIn;
if (type == MeshDataStage.VSOut)
return m_VSOut;
if (type == MeshDataStage.GSOut)
return m_GSOut;
return null;
}
private UIState GetUIState(object sender)
{
if (sender == vsInBufferView)
return m_VSIn;
if (sender == vsOutBufferView)
return m_VSOut;
if (sender == gsOutBufferView)
return m_GSOut;
return null;
}
#endregion
#region Privates
private Core m_Core;
private ReplayOutput m_Output = null;
private byte[] m_Zeroes = null;
private OutputConfig m_OutConfig = new OutputConfig();
private MeshDisplay m_MeshDisplay = new MeshDisplay();
private IntPtr RenderHandle = IntPtr.Zero;
// Cameras
private TimedUpdate m_Updater = null;
private ArcballCamera m_Arcball = null;
private FlyCamera m_Flycam = null;
private CameraControls m_CurrentCamera = null;
#endregion
public BufferViewer(Core core, bool meshview)
{
InitializeComponent();
if (SystemInformation.HighContrast)
{
dockPanel.Skin = Helpers.MakeHighContrastDockPanelSkin();
toolStrip1.Renderer = new ToolStripSystemRenderer();
toolStrip2.Renderer = new ToolStripSystemRenderer();
}
Icon = global::renderdocui.Properties.Resources.icon;
UI_SetupDocks(meshview);
m_Zeroes = new byte[512];
for (int i = 0; i < 512; i++) m_Zeroes[i] = 0;
m_VSIn.m_GridView = vsInBufferView;
m_VSOut.m_GridView = vsOutBufferView;
m_GSOut.m_GridView = gsOutBufferView;
largeBufferWarning.Visible = false;
byteOffset.Enabled = false;
rowOffset.Font =
byteOffset.Font =
instanceIdxToolitem.Font =
camSpeed.Font =
fovGuess.Font =
aspectGuess.Font =
nearGuess.Font =
farGuess.Font =
core.Config.PreferredFont;
m_ContextUIState = m_VSIn;
DockHandler.GetPersistStringCallback = PersistString;
exportToToolStripMenuItem.Enabled = exportToolItem.Enabled = false;
m_Core = core;
this.DoubleBuffered = true;
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
RecreateRenderPanel();
ResetConfig();
MeshView = meshview;
if (!MeshView)
{
debugVertexToolItem.Visible = debugSep.Visible = false;
instLabel.Visible = instSep.Visible = instanceIdxToolitem.Visible = false;
syncViewsToolItem.Visible = false;
highlightVerts.Visible = false;
byteOffset.Visible = true; byteOffsLab.Visible = true;
rowRange.Visible = true; rowRangeLab.Visible = true;
byteOffset.Text = "0";
rowRange.Text = DefaultMaxRows.ToString();
Text = "Buffer Contents";
// only add log viewer for non-mesh output buffer viewers.
// The mesh viewer is added in Core.GetMeshViewer()
m_Core.AddLogViewer(this);
}
else
{
byteOffset.Visible = false; byteOffsLab.Visible = false;
rowRange.Visible = false; rowRangeLab.Visible = false;
byteOffset.Text = "0";
rowRange.Text = DefaultMaxRows.ToString();
Text = "Mesh Output";
}
}
private void ResetConfig()
{
m_OutConfig.m_Type = OutputType.MeshDisplay;
m_MeshDisplay = new MeshDisplay();
m_MeshDisplay.type = MeshDataStage.VSIn;
m_MeshDisplay.fov = 90.0f;
m_MeshDisplay.solidShadeMode = SolidShadeMode.None;
solidShading.SelectedIndex = 0;
m_MeshDisplay.showPrevInstances = false;
m_MeshDisplay.showAllInstances = false;
m_MeshDisplay.showWholePass = false;
drawRange.SelectedIndex = 0;
if (m_Arcball != null)
m_Arcball.Camera.Shutdown();
if (m_Flycam != null)
m_Flycam.Camera.Shutdown();
m_Arcball = new ArcballCamera();
m_Flycam = new FlyCamera();
m_CurrentCamera = m_Arcball;
m_Updater = new TimedUpdate(10, TimerUpdate);
m_Arcball.SpeedMultiplier = m_Flycam.SpeedMultiplier = (float)camSpeed.Value;
fovGuess.Text = m_MeshDisplay.fov.ToString("G");
controlType.SelectedIndex = 0;
}
private void UI_SetupDocks(bool meshview)
{
if (meshview)
{
var w = Helpers.WrapDockContent(dockPanel, previewTab, "Preview");
w.CloseButton = false;
w.CloseButtonVisible = false;
w.Show(dockPanel, DockState.DockBottom);
m_VSIn.m_DockContent = Helpers.WrapDockContent(dockPanel, vsInBufferView, "VS Input");
m_VSIn.m_DockContent.CloseButton = false;
m_VSIn.m_DockContent.CloseButtonVisible = false;
m_VSIn.m_DockContent.Show(dockPanel, DockState.Document);
m_GSOut.m_DockContent = Helpers.WrapDockContent(dockPanel, gsOutBufferView, "GS/DS Output");
m_GSOut.m_DockContent.CloseButton = false;
m_GSOut.m_DockContent.CloseButtonVisible = false;
m_GSOut.m_DockContent.Show(m_VSIn.m_DockContent.Pane, DockAlignment.Right, 0.5);
m_VSOut.m_DockContent = Helpers.WrapDockContent(dockPanel, vsOutBufferView, "VS Output");
m_VSOut.m_DockContent.CloseButton = false;
m_VSOut.m_DockContent.CloseButtonVisible = false;
m_VSOut.m_DockContent.Show(m_GSOut.m_DockContent.Pane, m_GSOut.m_DockContent);
}
else
{
previewTab.Visible = false;
vsOutBufferView.Visible = false;
gsOutBufferView.Visible = false;
var w = Helpers.WrapDockContent(dockPanel, vsInBufferView, "Buffer Contents");
w.DockState = DockState.Document;
w.Show();
}
}
public class PersistData
{
public static int currentPersistVersion = 3;
public int persistVersion = currentPersistVersion;
public string panelLayout;
public bool meshView;
public static PersistData GetDefaults()
{
PersistData data = new PersistData();
data.panelLayout = "";
data.meshView = true;
return data;
}
}
public void InitFromPersistString(string str)
{
PersistData data = null;
try
{
if (str.Length > GetType().ToString().Length)
{
var reader = new StringReader(str.Substring(GetType().ToString().Length));
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(PersistData));
data = (PersistData)xs.Deserialize(reader);
reader.Close();
}
}
catch (System.Xml.XmlException)
{
}
catch (InvalidOperationException)
{
// don't need to handle it. Leave data null and pick up defaults below
}
if (data == null || data.persistVersion != PersistData.currentPersistVersion)
{
data = PersistData.GetDefaults();
}
ApplyPersistData(data);
}
private string onloadLayout = "";
Control[] LayoutPersistors
{
get
{
return new Control[] {
previewTab,
vsInBufferView,
gsOutBufferView,
vsOutBufferView,
};
}
}
private IDockContent GetContentFromPersistString(string persistString)
{
foreach (var p in LayoutPersistors)
if (persistString == p.Name && p.Parent is IDockContent && (p.Parent as DockContent).DockPanel == null)
return p.Parent as IDockContent;
return null;
}
private void ApplyPersistData(PersistData data)
{
MeshView = data.meshView;
onloadLayout = data.panelLayout;
}
// note that raw buffer viewers do not persist deliberately
private string PersistString()
{
if (!MeshView) return "";
var writer = new StringWriter();
writer.Write(GetType().ToString());
PersistData data = new PersistData();
// passing in a MemoryStream gets disposed - can't see a way to retrieve this
// in-memory.
var enc = new UnicodeEncoding();
var path = Path.GetTempFileName();
dockPanel.SaveAsXml(path, "", enc);
try
{
data.panelLayout = File.ReadAllText(path, enc);
File.Delete(path);
}
catch (System.Exception)
{
// can't recover
return writer.ToString();
}
data.meshView = MeshView;
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(PersistData));
xs.Serialize(writer, data);
return writer.ToString();
}
#region ILogViewerForm
void RecreateRenderPanel()
{
renderTable.Controls.Clear();
render.Dispose();
render = new Controls.NoScrollPanel();
render.Painting = true;
render.BackColor = Color.Black;
render.Dock = DockStyle.Fill;
render.Paint += new PaintEventHandler(render_Paint);
render.MouseClick += new MouseEventHandler(render_MouseClick);
render.MouseDown += new MouseEventHandler(render_MouseDown);
render.MouseMove += new MouseEventHandler(render_MouseMove);
render.MouseWheel += render_MouseWheel;
render.MouseWheelHandler = render_MouseWheel;
render.KeyDown += new KeyEventHandler(render_KeyDown);
render.KeyUp += new KeyEventHandler(render_KeyUp);
RenderHandle = render.Handle;
renderTable.Controls.Add(render, 1, 0);
renderTable.Controls.Add(configCamControls, 0, 0);
}
public void OnLogfileClosed()
{
if (IsDisposed) return;
RecreateRenderPanel();
m_Output = null;
ResetConfig();
ClearStoredData();
exportToToolStripMenuItem.Enabled = exportToolItem.Enabled = false;
}
public void OnLogfileLoaded()
{
ClearStoredData();
RecreateRenderPanel();
exportToToolStripMenuItem.Enabled = exportToolItem.Enabled = true;
var draw = m_Core.CurDrawcall;
previewTab.SelectedIndex = 0;
ulong byteoffs = ByteOffset;
if (MeshView)
{
if (draw == null)
{
m_VSIn.AbortThread();
m_VSOut.AbortThread();
m_GSOut.AbortThread();
return;
}
int curReq = m_ReqID;
m_Core.Renderer.BeginInvoke((ReplayRenderer r) =>
{
if (curReq != m_ReqID)
return;
m_Output = r.CreateOutput(RenderHandle, OutputType.MeshDisplay);
m_Output.SetOutputConfig(m_OutConfig);
RT_UpdateRenderOutput(r);
m_Output.Display(); // pump the display once, this will fetch postvs data
m_VSIn.m_Input = GetCurrentMeshInput(draw, MeshDataStage.VSIn);
m_VSOut.m_Input = GetCurrentMeshInput(draw, MeshDataStage.VSOut);
m_GSOut.m_Input = GetCurrentMeshInput(draw, MeshDataStage.GSOut);
var contentsVSIn = RT_FetchBufferContents(MeshDataStage.VSIn, r, m_VSIn.m_Input, byteoffs);
var contentsVSOut = RT_FetchBufferContents(MeshDataStage.VSOut, r, m_VSOut.m_Input, byteoffs);
var contentsGSOut = RT_FetchBufferContents(MeshDataStage.GSOut, r, m_GSOut.m_Input, byteoffs);
if (curReq != m_ReqID)
return;
this.BeginInvoke(new Action(() =>
{
if (curReq != m_ReqID)
return;
UI_AutoFetchRenderComponents(MeshDataStage.VSIn, true);
UI_AutoFetchRenderComponents(MeshDataStage.VSOut, true);
UI_AutoFetchRenderComponents(MeshDataStage.GSOut, true);
UI_AutoFetchRenderComponents(MeshDataStage.VSIn, false);
UI_AutoFetchRenderComponents(MeshDataStage.VSOut, false);
UI_AutoFetchRenderComponents(MeshDataStage.GSOut, false);
UI_UpdateMeshRenderComponents();
UI_SetAllColumns();
UI_SetRowsData(MeshDataStage.VSIn, contentsVSIn, 0);
if (m_VSOut.m_Input != null)
UI_SetRowsData(MeshDataStage.VSOut, contentsVSOut, 0);
if (m_GSOut.m_Input != null)
UI_SetRowsData(MeshDataStage.GSOut, contentsGSOut, 0);
camGuess_PropChanged();
m_Loaded = true;
}));
});
}
else
{
m_Loaded = true;
m_Core.Renderer.BeginInvoke((ReplayRenderer r) =>
{
if (IsDisposed) return;
m_Output = null;
});
}
}
private void CalcCellFloatWidth()
{
Graphics g = vsInBufferView.CreateGraphics();
Font f = vsInBufferView.DefaultCellStyle.Font;
// measure a few doubles at different extremes to get a max column width
// for floats under the current float formatter settings
// all numbers are negative to account for negative sign
double[] testNums = new double[] {
-1.0, // some default number
-1.2345e-200, // something that will definitely be exponential notation
-123456.7890123456789, // some number with a 'large' value before the decimal, and numbers after the decimal
};
CellFloatWidth = CellDefaultWidth;
foreach (double d in testNums)
{
float stringWidth = g.MeasureString(Formatter.Format(d), f).Width;
CellFloatWidth = Math.Max(CellFloatWidth, (int)stringWidth + 5);
}
g.Dispose();
}
public void OnEventSelected(UInt32 eventID)
{
if (IsDisposed) return;
// ignore OnEventSelected until we've loaded
if (!m_Loaded)
return;
ClearStoredData();
var draw = m_Core.CurDrawcall;
byteOffset.Enabled = false;
instanceIdxToolitem.Enabled = (draw != null && draw.numInstances > 1);
if (!instanceIdxToolitem.Enabled)
instanceIdxToolitem.Text = "0";
if (MeshView && draw == null)
{
m_VSIn.AbortThread();
m_VSOut.AbortThread();
m_GSOut.AbortThread();
return;
}
int[] horizscroll = new int[] {
m_VSIn.m_GridView.HorizontalScrollingOffset,
m_VSOut.m_GridView.HorizontalScrollingOffset,
m_GSOut.m_GridView.HorizontalScrollingOffset,
};
int curReq = m_ReqID;
ulong byteoffs = ByteOffset;
m_Core.Renderer.BeginInvoke((ReplayRenderer r) =>
{
m_VSIn.AbortThread();
m_VSOut.AbortThread();
m_GSOut.AbortThread();
if (curReq != m_ReqID)
return;
if (MeshView)
{
MeshDataStage[] stages = new MeshDataStage[] { MeshDataStage.VSIn, MeshDataStage.VSOut, MeshDataStage.GSOut };
FormatElement[] prevPos = new FormatElement[3];
FormatElement[] prevSecond = new FormatElement[3];
for (int i = 0; i < 3; i++)
{
prevPos[i] = GetPosHighlightFormatElement(stages[i]);
prevSecond[i] = GetSecondHighlightFormatElement(stages[i]);
}
m_VSIn.m_Input = GetCurrentMeshInput(draw, MeshDataStage.VSIn);
m_VSOut.m_Input = GetCurrentMeshInput(draw, MeshDataStage.VSOut);
m_GSOut.m_Input = GetCurrentMeshInput(draw, MeshDataStage.GSOut);
for(int i=0; i < 3; i++)
{
FormatElement curPos = GetPosHighlightFormatElement(stages[i]);
FormatElement curSecond = GetSecondHighlightFormatElement(stages[i]);
if (prevPos[i] == null || prevPos[i] != curPos) UI_AutoFetchRenderComponents(stages[i], true);
if (prevSecond[i] == null || prevSecond[i] != curSecond) UI_AutoFetchRenderComponents(stages[i], false);
}
}
var contentsVSIn = RT_FetchBufferContents(MeshDataStage.VSIn, r, m_VSIn.m_Input, byteoffs);
var contentsVSOut = RT_FetchBufferContents(MeshDataStage.VSOut, r, m_VSOut.m_Input, byteoffs);
var contentsGSOut = RT_FetchBufferContents(MeshDataStage.GSOut, r, m_GSOut.m_Input, byteoffs);
if (curReq != m_ReqID)
return;
this.BeginInvoke(new Action(() =>
{
if (curReq != m_ReqID)
return;
m_VSIn.AbortThread();
m_VSOut.AbortThread();
m_GSOut.AbortThread();
if (m_VSIn.m_Input != null)
UI_SetRowsData(MeshDataStage.VSIn, contentsVSIn, horizscroll[0]);
if (m_VSOut.m_Input != null)
UI_SetRowsData(MeshDataStage.VSOut, contentsVSOut, horizscroll[1]);
if (m_GSOut.m_Input != null)
UI_SetRowsData(MeshDataStage.GSOut, contentsGSOut, horizscroll[2]);
if (MeshView)
UI_UpdateMeshRenderComponents();
UI_SetAllColumns();
camGuess_PropChanged();
render.Invalidate();
}));
});
}
#endregion
#region Data Setting
private void ClearStoredData()
{
UIState[] states = { m_VSIn, m_VSOut, m_GSOut };
m_ReqID++;
foreach (var s in states)
{
s.AbortThread();
if(s.m_Reader != null)
{
for (int i = 0; i < s.m_Reader.Length; i++)
{
if(s.m_Reader[i] != null)
s.m_Reader[i].Dispose();
s.m_Reader[i] = null;
}
}
if (s.m_Stream != null)
{
for (int i = 0; i < s.m_Stream.Length; i++)
{
if (s.m_Reader[i] != null)
s.m_Stream[i].Dispose();
s.m_Stream[i] = null;
}
}
s.m_Stream = null;
s.m_Reader = null;
s.m_RawData = null;
if (s.m_Data != null && s.m_Data.Buffers != null)
{
for (int i = 0; i < s.m_Data.Buffers.Length; i++)
s.m_Data.Buffers[i] = null;
}
if (s.m_Data != null)
{
s.m_Data.DataIndices = null;
}
s.m_Data = null;
s.m_RawStride = 0;
s.m_Rows = null;
s.m_GridView.RowCount = 0;
}
ClearHighlightVerts();
}
public bool MeshView;
public int RowOffset
{
get
{
int row = 0;
int.TryParse(rowOffset.Text, out row);
return row;
}
set
{
rowOffset.Text = value.ToString();
}
}
private uint DefaultMaxRows { get { return 200000; } }
private int MaxRowCount
{
get
{
// for now, don't clamp rows on mesh view
if (IsDisposed || MeshView) return int.MaxValue;
int maxrows = 0;
int.TryParse(rowRange.Text, out maxrows);
return maxrows;
}
}
private ulong ByteOffset
{
get
{
if (IsDisposed) return 0;
ulong offs = 0;
ulong.TryParse(byteOffset.Text, out offs);
return offs;
}
}
#region Get Data Formats/Organisation
public void ViewRawBuffer(bool isBuffer, ulong offset, ulong size, ResourceId id)
{
ViewRawBuffer(isBuffer, offset, size, id, "");
}
public void ViewRawBuffer(bool isBuffer, ulong offset, ulong size, ResourceId id, string formatString)
{
if (m_Core.CurBuffers == null) return;
m_FormatText = formatString;
UInt64 len = 0;
Text = "Buffer Contents";
foreach (var b in m_Core.CurBuffers)
{
if (b.ID == id)
{
Text = b.name + " - Contents";
len = b.length;
break;
}
}
Input input = new Input();
string errors = "";
FormatElement[] elems = FormatElement.ParseFormatString(formatString, len, true, out errors);
input.Strides = new uint[] { elems.Last().offset + elems.Last().ByteSize };
input.Buffers = new ResourceId[] { isBuffer ? id : ResourceId.Null, isBuffer ? ResourceId.Null : id };
input.Offsets = new ulong[] { 0 };
input.IndexBuffer = ResourceId.Null;
input.BufferFormats = elems;
input.IndexOffset = 0;
largeBufferWarning.Visible = false;
byteOffset.Text = offset.ToString();
if (size == ulong.MaxValue)
{
rowRange.Text = DefaultMaxRows.ToString();
}
else
{
uint rows = (uint)(size / input.Strides[0]);
if (rows * input.Strides[0] < size)
rows++;
if (rows > DefaultMaxRows)
rows = DefaultMaxRows;
rowRange.Text = rows.ToString();
}
m_VSIn.m_Input = input;
ShowFormatSpecifier();
m_FormatSpecifier.SetErrors(errors);
ClearStoredData();
m_Core.Renderer.BeginInvoke((ReplayRenderer r) =>
{
if (IsDisposed) return;
var contents = RT_FetchBufferContents(MeshDataStage.VSIn, r, input, offset);
this.BeginInvoke(new Action(() =>
{
if (IsDisposed) return;
UI_SetRowsData(MeshDataStage.VSIn, contents, 0);
UI_SetColumns(MeshDataStage.VSIn, input.BufferFormats);
}));
});
}
// used for the mesh view, to get the format of the mesh input from whichever stage that
// we're looking at
private Input GetCurrentMeshInput(FetchDrawcall draw, MeshDataStage type)
{
if (!MeshView)
return null;
Input ret = new Input();
ret.Drawcall = draw;
ret.Topology = draw != null ? draw.topology : PrimitiveTopology.Unknown;
ResourceId ibuffer = ResourceId.Null;
ulong ioffset = 0;
m_Core.CurPipelineState.GetIBuffer(out ibuffer, out ioffset);
if (draw != null && (draw.flags & DrawcallFlags.UseIBuffer) == 0)
{
ibuffer = ResourceId.Null;
ioffset = 0;
}
ret.IndexBuffer = ibuffer;
ret.IndexOffset = ioffset;
ret.IndexRestart = m_Core.CurPipelineState.IsStripRestartEnabled();
ret.IndexRestartValue = m_Core.CurPipelineState.GetStripRestartIndex(draw != null ? draw.indexByteWidth : 0);
if (type != MeshDataStage.VSIn)
{
ShaderReflection details = null;
if (type == MeshDataStage.VSOut)
details = m_Core.CurPipelineState.GetShaderReflection(ShaderStageType.Vertex);
else if (type == MeshDataStage.GSOut)
{
details = m_Core.CurPipelineState.GetShaderReflection(ShaderStageType.Geometry);
if (details == null)
details = m_Core.CurPipelineState.GetShaderReflection(ShaderStageType.Domain);
}
if (details == null)
return null;
List<FormatElement> f = new List<FormatElement>();
int posidx = -1;
for (int i = 0; i < details.OutputSig.Length; i++)
{
var sig = details.OutputSig[i];
f.Add(new FormatElement());
f[i].buffer = 0;
f[i].name = details.OutputSig[i].varName.Length > 0 ? details.OutputSig[i].varName : details.OutputSig[i].semanticIdxName;
f[i].format.compByteWidth = sizeof(float);
f[i].format.compCount = sig.compCount;
f[i].format.compType = sig.compType;
f[i].format.special = false;
f[i].format.rawType = 0;
f[i].perinstance = false;
f[i].instancerate = 1;
f[i].rowmajor = false;
f[i].matrixdim = 1;
f[i].systemValue = sig.systemValue;
if(f[i].systemValue == SystemAttribute.Position)
posidx = i;
}
// shift position attribute up to first, keeping order otherwise
// the same
if (posidx > 0)
{
FormatElement pos = f[posidx];
f.RemoveAt(posidx);
f.Insert(0, pos);
}