forked from baldurk/renderdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextureViewer.cs
More file actions
4012 lines (3202 loc) · 142 KB
/
TextureViewer.cs
File metadata and controls
4012 lines (3202 loc) · 142 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-2017 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.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using renderdocui.Code;
using renderdocui.Controls;
using renderdocui.Windows.Dialogs;
using renderdoc;
using System.Threading;
namespace renderdocui.Windows
{
public partial class TextureViewer : DockContent, ILogViewerForm
{
#region Privates
private Core m_Core;
private ReplayOutput m_Output = null;
private Dialogs.TextureGoto m_Goto = null;
private TextureDisplay m_TexDisplay = new TextureDisplay();
private ToolStripControlHost depthStencilToolstrip = null;
private DockContent m_PreviewPanel = null;
private DockContent m_TexlistDockPanel = null;
private FileSystemWatcher m_FSWatcher = null;
private int m_HighWaterStatusLength = 0;
public enum FollowType { OutputColour, OutputDepth, ReadWrite, ReadOnly }
struct Following
{
public FollowType Type;
public ShaderStageType Stage;
public int index;
public int arrayEl;
public static Following Default = new Following(FollowType.OutputColour, ShaderStageType.Pixel, 0, 0);
public Following(FollowType t, ShaderStageType s, int i, int a) { Type = t; Stage = s; index = i; arrayEl = a; }
public override int GetHashCode()
{
return Type.GetHashCode() +
Stage.GetHashCode() +
index.GetHashCode();
}
public override bool Equals(object obj)
{
return obj is Following && this == (Following)obj;
}
public static bool operator ==(Following s1, Following s2)
{
return s1.Type == s2.Type &&
s1.Stage == s2.Stage &&
s1.index == s2.index;
}
public static bool operator !=(Following s1, Following s2)
{
return !(s1 == s2);
}
public static void GetDrawContext(Core core, out bool copy, out bool compute)
{
var curDraw = core.CurDrawcall;
copy = curDraw != null && (curDraw.flags & (DrawcallFlags.Copy | DrawcallFlags.Resolve | DrawcallFlags.Present)) != 0;
compute = curDraw != null && (curDraw.flags & DrawcallFlags.Dispatch) != 0 &&
core.CurPipelineState.GetShader(ShaderStageType.Compute) != ResourceId.Null;
}
public int GetHighestMip(Core core)
{
var curDraw = core.CurDrawcall;
bool copy, compute;
GetDrawContext(core, out copy, out compute);
return GetBoundResource(core, arrayEl).HighestMip;
}
public int GetFirstArraySlice(Core core)
{
var curDraw = core.CurDrawcall;
bool copy, compute;
GetDrawContext(core, out copy, out compute);
return GetBoundResource(core, arrayEl).FirstSlice;
}
public FormatComponentType GetTypeHint(Core core)
{
var curDraw = core.CurDrawcall;
bool copy, compute;
GetDrawContext(core, out copy, out compute);
return GetBoundResource(core, arrayEl).typeHint;
}
public ResourceId GetResourceId(Core core)
{
return GetBoundResource(core, arrayEl).Id;
}
public BoundResource GetBoundResource(Core core, int arrayIdx)
{
BoundResource ret = new BoundResource();
if (Type == FollowType.OutputColour)
{
var outputs = GetOutputTargets(core);
if (index < outputs.Length)
ret = outputs[index];
}
else if (Type == FollowType.OutputDepth)
{
ret = GetDepthTarget(core);
}
else if (Type == FollowType.ReadWrite)
{
var rw = GetReadWriteResources(core);
var mapping = GetMapping(core);
if (index < mapping.ReadWriteResources.Length)
{
var key = mapping.ReadWriteResources[index];
if (rw.ContainsKey(key))
ret = rw[key][arrayIdx];
}
}
else if (Type == FollowType.ReadOnly)
{
var res = GetReadOnlyResources(core);
var mapping = GetMapping(core);
if (index < mapping.ReadOnlyResources.Length)
{
var key = mapping.ReadOnlyResources[index];
if (res.ContainsKey(key))
ret = res[key][arrayIdx];
}
}
return ret;
}
public static BoundResource[] GetOutputTargets(Core core)
{
var curDraw = core.CurDrawcall;
bool copy, compute;
GetDrawContext(core, out copy, out compute);
if (copy)
return new BoundResource[] { new BoundResource(curDraw.copyDestination) };
else if(compute)
return new BoundResource[0];
else
{
var ret = core.CurPipelineState.GetOutputTargets();
if (ret.Length == 0 && curDraw != null && (curDraw.flags & DrawcallFlags.Present) != 0)
{
if (curDraw.copyDestination != ResourceId.Null)
return new BoundResource[] { new BoundResource(curDraw.copyDestination) };
foreach (var t in core.CurTextures)
if ((t.creationFlags & TextureCreationFlags.SwapBuffer) != 0)
return new BoundResource[] { new BoundResource(t.ID) };
}
return ret;
}
}
public static BoundResource GetDepthTarget(Core core)
{
var curDraw = core.CurDrawcall;
bool copy, compute;
GetDrawContext(core, out copy, out compute);
if (copy || compute)
return new BoundResource(ResourceId.Null);
else
return core.CurPipelineState.GetDepthTarget();
}
public Dictionary<BindpointMap, BoundResource[]> GetReadWriteResources(Core core)
{
return GetReadWriteResources(core, Stage);
}
public static Dictionary<BindpointMap, BoundResource[]> GetReadWriteResources(Core core, ShaderStageType stage)
{
var curDraw = core.CurDrawcall;
bool copy, compute;
GetDrawContext(core, out copy, out compute);
if (copy)
{
return new Dictionary<BindpointMap, BoundResource[]>();
}
else if (compute)
{
// only return compute resources for one stage
if (stage == ShaderStageType.Pixel || stage == ShaderStageType.Compute)
return core.CurPipelineState.GetReadWriteResources(ShaderStageType.Compute);
else
return new Dictionary<BindpointMap, BoundResource[]>();
}
else
{
return core.CurPipelineState.GetReadWriteResources(stage);
}
}
public Dictionary<BindpointMap, BoundResource[]> GetReadOnlyResources(Core core)
{
return GetReadOnlyResources(core, Stage);
}
public static Dictionary<BindpointMap, BoundResource[]> GetReadOnlyResources(Core core, ShaderStageType stage)
{
var curDraw = core.CurDrawcall;
bool copy, compute;
GetDrawContext(core, out copy, out compute);
if (copy)
{
var ret = new Dictionary<BindpointMap, BoundResource[]>();
// only return copy source for one stage
if(stage == ShaderStageType.Pixel)
ret.Add(new BindpointMap(0, 0), new BoundResource[] { new BoundResource(curDraw.copySource) });
return ret;
}
else if (compute)
{
// only return compute resources for one stage
if (stage == ShaderStageType.Pixel || stage == ShaderStageType.Compute)
return core.CurPipelineState.GetReadOnlyResources(ShaderStageType.Compute);
else
return new Dictionary<BindpointMap, BoundResource[]>();
}
else
{
return core.CurPipelineState.GetReadOnlyResources(stage);
}
}
public ShaderReflection GetReflection(Core core)
{
return GetReflection(core, Stage);
}
public static ShaderReflection GetReflection(Core core, ShaderStageType stage)
{
var curDraw = core.CurDrawcall;
bool copy, compute;
GetDrawContext(core, out copy, out compute);
if (copy)
return null;
else if (compute)
return core.CurPipelineState.GetShaderReflection(ShaderStageType.Compute);
else
return core.CurPipelineState.GetShaderReflection(stage);
}
public ShaderBindpointMapping GetMapping(Core core)
{
return GetMapping(core, Stage);
}
public static ShaderBindpointMapping GetMapping(Core core, ShaderStageType stage)
{
var curDraw = core.CurDrawcall;
bool copy, compute;
GetDrawContext(core, out copy, out compute);
if (copy)
{
ShaderBindpointMapping mapping = new ShaderBindpointMapping();
mapping.ConstantBlocks = new BindpointMap[0];
mapping.ReadWriteResources = new BindpointMap[0];
mapping.InputAttributes = new int[0];
// for PS only add a single mapping to get the copy source
if (stage == ShaderStageType.Pixel)
mapping.ReadOnlyResources = new BindpointMap[] { new BindpointMap(0, 0) };
else
mapping.ReadOnlyResources = new BindpointMap[0];
return mapping;
}
else if (compute)
{
return core.CurPipelineState.GetBindpointMapping(ShaderStageType.Compute);
}
else
{
return core.CurPipelineState.GetBindpointMapping(stage);
}
}
}
private Following m_Following = Following.Default;
public class TexSettings
{
public TexSettings()
{
r = g = b = true; a = false;
mip = 0; slice = 0;
minrange = 0.0f; maxrange = 1.0f;
typeHint = FormatComponentType.None;
}
public int displayType; // RGBA, RGBM, Custom
public string customShader;
public bool r, g, b, a;
public bool depth, stencil;
public int mip, slice;
public float minrange, maxrange;
public FormatComponentType typeHint;
}
private Dictionary<ResourceId, TexSettings> m_TextureSettings = new Dictionary<ResourceId, TexSettings>();
#endregion
public TextureViewer(Core core)
{
m_Core = core;
InitializeComponent();
if (SystemInformation.HighContrast)
{
dockPanel.Skin = Helpers.MakeHighContrastDockPanelSkin();
zoomStrip.Renderer = new ToolStripSystemRenderer();
overlayStrip.Renderer = new ToolStripSystemRenderer();
subStrip.Renderer = new ToolStripSystemRenderer();
rangeStrip.Renderer = new ToolStripSystemRenderer();
channelStrip.Renderer = new ToolStripSystemRenderer();
actionsStrip.Renderer = new ToolStripSystemRenderer();
}
m_Goto = new Dialogs.TextureGoto(GotoLocation);
textureList.Font =
texturefilter.Font =
rangeBlack.Font =
rangeWhite.Font =
customShader.Font =
hdrMul.Font =
channels.Font =
mipLevel.Font =
sliceFace.Font =
zoomOption.Font =
core.Config.PreferredFont;
Icon = global::renderdocui.Properties.Resources.icon;
textureList.m_Core = core;
textureList.GoIconClick += new EventHandler<GoIconClickEventArgs>(textureList_GoIconClick);
UI_SetupToolstrips();
UI_SetupDocks();
UI_UpdateTextureDetails();
statusLabel.Text = "";
zoomOption.SelectedText = "";
mipLevel.Enabled = false;
sliceFace.Enabled = false;
rangeBlack.ResizeToFit = false;
rangeWhite.ResizeToFit = false;
PixelPicked = false;
mainLayout.Dock = DockStyle.Fill;
saveTex.Enabled = gotoLocationButton.Enabled = viewTexBuffer.Enabled = false;
DockHandler.GetPersistStringCallback = PersistString;
renderContainer.MouseWheelHandler = render_MouseWheel;
renderContainer.MouseDown += render_MouseClick;
renderContainer.MouseMove += render_MouseMove;
RecreateRenderPanel();
RecreateContextPanel();
rangeHistogram.RangeUpdated += new EventHandler<RangeHistogramEventArgs>(rangeHistogram_RangeUpdated);
this.DoubleBuffered = true;
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
channels.SelectedIndex = 0;
FitToWindow = true;
overlay.SelectedIndex = 0;
m_Following = Following.Default;
texturefilter.SelectedIndex = 0;
}
private void UI_SetupDocks()
{
m_PreviewPanel = Helpers.WrapDockContent(dockPanel, renderToolstripContainer, "Current");
m_PreviewPanel.DockState = DockState.Document;
m_PreviewPanel.AllowEndUserDocking = false;
m_PreviewPanel.Show();
m_PreviewPanel.CloseButton = false;
m_PreviewPanel.CloseButtonVisible = false;
m_PreviewPanel.DockHandler.TabPageContextMenuStrip = tabContextMenu;
dockPanel.ActiveDocumentChanged += new EventHandler(dockPanel_ActiveDocumentChanged);
var w3 = Helpers.WrapDockContent(dockPanel, roPanel, "Inputs");
w3.DockAreas &= ~DockAreas.Document;
w3.DockState = DockState.DockRight;
w3.Show();
w3.CloseButton = false;
w3.CloseButtonVisible = false;
var w5 = Helpers.WrapDockContent(dockPanel, rwPanel, "Outputs");
w5.DockAreas &= ~DockAreas.Document;
w5.DockState = DockState.DockRight;
w5.Show(w3.Pane, w3);
w5.CloseButton = false;
w5.CloseButtonVisible = false;
m_TexlistDockPanel = Helpers.WrapDockContent(dockPanel, texlistContainer, "Texture List");
m_TexlistDockPanel.DockAreas &= ~DockAreas.Document;
m_TexlistDockPanel.DockState = DockState.DockLeft;
m_TexlistDockPanel.Hide();
m_TexlistDockPanel.HideOnClose = true;
var w4 = Helpers.WrapDockContent(dockPanel, pixelContextPanel, "Pixel Context");
w4.DockAreas &= ~DockAreas.Document;
w4.Show(w3.Pane, DockAlignment.Bottom, 0.3);
w4.CloseButton = false;
w4.CloseButtonVisible = false;
}
private void UI_SetupToolstrips()
{
int idx = rangeStrip.Items.IndexOf(rangeWhite);
rangeStrip.Items.Insert(idx, new ToolStripControlHost(rangeHistogram));
for (int i = 0; i < channelStrip.Items.Count; i++)
{
if (channelStrip.Items[i] == mulSep)
{
depthStencilToolstrip = new ToolStripControlHost(depthstencilPanel);
channelStrip.Items.Insert(i, depthStencilToolstrip);
break;
}
}
}
public class PersistData
{
public static int currentPersistVersion = 4;
public int persistVersion = currentPersistVersion;
public string panelLayout;
public FloatVector darkBack = new FloatVector(0, 0, 0, 0);
public FloatVector lightBack = new FloatVector(0, 0, 0, 0);
public static PersistData GetDefaults()
{
PersistData data = new PersistData();
data.panelLayout = "";
data.darkBack = new FloatVector(0, 0, 0, 0);
data.lightBack = new FloatVector(0, 0, 0, 0);
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();
}
// fixup old incorrect checkerboard colours
if (data.lightBack.x != data.darkBack.x)
{
TextureDisplay defaults = new TextureDisplay();
data.lightBack = defaults.lightBackgroundColour;
data.darkBack = defaults.darkBackgroundColour;
}
ApplyPersistData(data);
}
private IDockContent GetContentFromPersistString(string persistString)
{
Control[] persistors = {
renderToolstripContainer,
roPanel,
rwPanel,
texlistContainer,
pixelContextPanel
};
foreach(var p in persistors)
if (persistString == p.Name && p.Parent is IDockContent && (p.Parent as DockContent).DockPanel == null)
return p.Parent as IDockContent;
// backwards compatibilty for rename
if(persistString == "texPanel")
return roPanel.Parent as IDockContent;
if(persistString == "rtPanel")
return rwPanel.Parent as IDockContent;
return null;
}
private string onloadLayout = "";
private FloatVector darkBack = new FloatVector(0, 0, 0, 0);
private FloatVector lightBack = new FloatVector(0, 0, 0, 0);
private void ApplyPersistData(PersistData data)
{
onloadLayout = data.panelLayout;
darkBack = data.darkBack;
lightBack = data.lightBack;
}
private void TextureViewer_Load(object sender, EventArgs e)
{
if (onloadLayout.Length > 0)
{
Control[] persistors = {
renderToolstripContainer,
roPanel,
rwPanel,
texlistContainer,
pixelContextPanel
};
foreach (var p in persistors)
(p.Parent as DockContent).DockPanel = null;
var enc = new UnicodeEncoding();
using (var strm = new MemoryStream(enc.GetBytes(onloadLayout)))
{
strm.Flush();
strm.Position = 0;
try
{
dockPanel.LoadFromXml(strm, new DeserializeDockContent(GetContentFromPersistString));
}
catch (System.Exception)
{
// on error, go back to default layout
UI_SetupDocks();
}
}
onloadLayout = "";
}
if (darkBack.x != lightBack.x)
{
backcolorPick.Checked = false;
checkerBack.Checked = true;
}
else
{
backcolorPick.Checked = true;
checkerBack.Checked = false;
colorDialog.Color = Color.FromArgb((int)(255 * darkBack.x),
(int)(255 * darkBack.y),
(int)(255 * darkBack.z));
}
}
private string PersistString()
{
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.darkBack = darkBack;
data.lightBack = lightBack;
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(PersistData));
xs.Serialize(writer, data);
return writer.ToString();
}
#region Public Functions
private Dictionary<ResourceId, DockContent> lockedTabs = new Dictionary<ResourceId, DockContent>();
public void GotoLocation(int x, int y)
{
if(!m_Core.LogLoaded || CurrentTexture == null)
return;
m_PickedPoint = new Point(x, y);
uint mipHeight = Math.Max(1, CurrentTexture.height >> (int)m_TexDisplay.mip);
if (m_Core.APIProps.pipelineType == GraphicsAPI.OpenGL)
m_PickedPoint.Y = (int)(mipHeight - 1) - m_PickedPoint.Y;
if (m_TexDisplay.FlipY)
m_PickedPoint.Y = (int)(mipHeight - 1) - m_PickedPoint.Y;
m_Core.Renderer.BeginInvoke((ReplayRenderer r) =>
{
if (m_Output != null)
RT_PickPixelsAndUpdate(m_PickedPoint.X, m_PickedPoint.Y, true);
RT_UpdateAndDisplay(r);
});
UI_UpdateStatusText();
}
public void ViewTexture(ResourceId ID, bool focus)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new Action(() => { this.ViewTexture(ID, focus); }));
return;
}
TextureViewer_Load(null, null);
if (lockedTabs.ContainsKey(ID))
{
if (!lockedTabs[ID].IsDisposed && !lockedTabs[ID].IsHidden)
{
if (focus)
Show();
lockedTabs[ID].Show();
m_Core.Renderer.BeginInvoke(RT_UpdateAndDisplay);
return;
}
lockedTabs.Remove(ID);
}
for (int i = 0; i < m_Core.CurTextures.Length; i++)
{
if (m_Core.CurTextures[i].ID == ID)
{
FetchTexture current = m_Core.CurTextures[i];
var newPanel = Helpers.WrapDockContent(dockPanel, renderToolstripContainer, current.name);
newPanel.DockState = DockState.Document;
newPanel.AllowEndUserDocking = false;
newPanel.Icon = Icon.FromHandle(global::renderdocui.Properties.Resources.page_white_link.GetHicon());
newPanel.Tag = current;
newPanel.DockHandler.TabPageContextMenuStrip = tabContextMenu;
newPanel.FormClosing += new FormClosingEventHandler(PreviewPanel_FormClosing);
newPanel.Show(m_PreviewPanel.Pane, null);
newPanel.Show();
if (focus)
Show();
lockedTabs.Add(ID, newPanel);
m_Core.Renderer.BeginInvoke(RT_UpdateAndDisplay);
return;
}
}
for (int i = 0; i < m_Core.CurBuffers.Length; i++)
{
if (m_Core.CurBuffers[i].ID == ID)
{
var viewer = new BufferViewer(m_Core, false);
viewer.ViewRawBuffer(true, 0, ulong.MaxValue, ID);
viewer.Show(DockPanel);
return;
}
}
}
#endregion
#region Custom Shader handling
private List<string> m_CustomShadersBusy = new List<string>();
private Dictionary<string, ResourceId> m_CustomShaders = new Dictionary<string, ResourceId>();
private Dictionary<string, ShaderViewer> m_CustomShaderEditor = new Dictionary<string, ShaderViewer>();
private void ReloadCustomShaders(string filter)
{
if (!m_Core.LogLoaded) return;
if (filter.Length > 0)
{
var shaders = m_CustomShaders.Values.ToArray();
m_Core.Renderer.BeginInvoke((ReplayRenderer r) =>
{
foreach (var s in shaders)
r.FreeCustomShader(s);
});
customShader.Items.Clear();
m_CustomShaders.Clear();
}
else
{
var fn = Path.GetFileNameWithoutExtension(filter);
var key = fn.ToUpperInvariant();
if (m_CustomShaders.ContainsKey(key))
{
if (m_CustomShadersBusy.Contains(key))
return;
ResourceId freed = m_CustomShaders[key];
m_Core.Renderer.BeginInvoke((ReplayRenderer r) =>
{
r.FreeCustomShader(freed);
});
m_CustomShaders.Remove(key);
var text = customShader.Text;
for (int i = 0; i < customShader.Items.Count; i++)
{
if (customShader.Items[i].ToString() == fn)
{
customShader.Items.RemoveAt(i);
break;
}
}
customShader.Text = text;
}
}
foreach (var f in Directory.EnumerateFiles(Core.ConfigDirectory, "*" + m_Core.APIProps.ShaderExtension))
{
var fn = Path.GetFileNameWithoutExtension(f);
var key = fn.ToUpperInvariant();
if (!m_CustomShaders.ContainsKey(key) && !m_CustomShadersBusy.Contains(key))
{
try
{
string source = File.ReadAllText(f);
m_CustomShaders.Add(key, ResourceId.Null);
m_CustomShadersBusy.Add(key);
m_Core.Renderer.BeginInvoke((ReplayRenderer r) =>
{
string errors = "";
ResourceId id = r.BuildCustomShader("main", source, 0, ShaderStageType.Pixel, out errors);
if (m_CustomShaderEditor.ContainsKey(key))
{
BeginInvoke((MethodInvoker)delegate
{
m_CustomShaderEditor[key].ShowErrors(errors);
});
}
BeginInvoke((MethodInvoker)delegate
{
customShader.Items.Add(fn);
m_CustomShaders[key] = id;
m_CustomShadersBusy.Remove(key);
customShader.AutoCompleteSource = AutoCompleteSource.None;
customShader.AutoCompleteSource = AutoCompleteSource.ListItems;
UI_UpdateChannels();
});
});
}
catch (System.Exception)
{
// just continue, skip this file
}
}
}
}
private void customCreate_Click(object sender, EventArgs e)
{
if (customShader.Text == null || customShader.Text.Length == 0)
{
MessageBox.Show("No name entered.\nEnter a name in the textbox.", "Error Creating Shader", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (m_CustomShaders.ContainsKey(customShader.Text.ToUpperInvariant()))
{
MessageBox.Show("Selected shader already exists.\nEnter a new name in the textbox.", "Error Creating Shader", MessageBoxButtons.OK, MessageBoxIcon.Error);
customShader.Text = "";
UI_UpdateChannels();
return;
}
var path = Path.Combine(Core.ConfigDirectory, customShader.Text + m_Core.APIProps.ShaderExtension);
string src = "";
if (m_Core.APIProps.pipelineType.IsD3D())
{
src = String.Format(
"float4 main(float4 pos : SV_Position, float4 uv : TEXCOORD0) : SV_Target0{0}" +
"{{{0}" +
" return float4(0,0,0,1);{0}" +
"}}{0}"
, Environment.NewLine);
}
else if (m_Core.APIProps.pipelineType == GraphicsAPI.OpenGL ||
m_Core.APIProps.pipelineType == GraphicsAPI.Vulkan)
{
src = String.Format(
"#version 420 core{0}{0}" +
"layout (location = 0) in vec2 uv;{0}{0}" +
"layout (location = 0) out vec4 color_out;{0}{0}" +
"void main(){0}" +
"{{{0}" +
" color_out = vec4(0,0,0,1);{0}" +
"}}{0}"
, Environment.NewLine);
}
try
{
File.WriteAllText(path, src);
}
catch (System.Exception)
{
// ignore this file
}
// auto-open edit window
customEdit_Click(sender, e);
}
private void customEdit_Click(object sender, EventArgs e)
{
var filename = customShader.Text;
var key = filename.ToUpperInvariant();
string src = "";
try
{
src = File.ReadAllText(Path.Combine(Core.ConfigDirectory, filename + m_Core.APIProps.ShaderExtension));
}
catch (System.Exception ex)
{
MessageBox.Show("Couldn't open file for shader " + filename + Environment.NewLine + ex.ToString(), "Cannot open shader",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var files = new Dictionary<string, string>();
files.Add(filename, src);
ShaderViewer s = new ShaderViewer(m_Core, true, "Custom Shader", files,
// Save Callback
(ShaderViewer viewer, Dictionary<string, string> updatedfiles) =>
{
foreach (var f in updatedfiles)
{
var path = Path.Combine(Core.ConfigDirectory, f.Key + m_Core.APIProps.ShaderExtension);
try
{
File.WriteAllText(path, f.Value);
}
catch (System.Exception ex)
{
MessageBox.Show("Couldn't save file for shader " + filename + Environment.NewLine + ex.ToString(), "Cannot save shader",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
},
// Close Callback
() =>
{
m_CustomShaderEditor.Remove(key);
});
m_CustomShaderEditor[key] = s;
s.Show(this.DockPanel);
}
private void customDelete_Click(object sender, EventArgs e)
{
if (customShader.Text == null || customShader.Text.Length == 0)
{