forked from baldurk/renderdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.cpp
More file actions
1160 lines (929 loc) · 30.6 KB
/
core.cpp
File metadata and controls
1160 lines (929 loc) · 30.6 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.
******************************************************************************/
#include "core/core.h"
#include <time.h>
#include <algorithm>
#include "api/replay/version.h"
#include "common/common.h"
#include "hooks/hooks.h"
#include "replay/replay_driver.h"
#include "serialise/rdcfile.h"
#include "serialise/serialiser.h"
#include "strings/string_utils.h"
#include "crash_handler.h"
#include "api/replay/renderdoc_tostr.inl"
#include "replay/renderdoc_serialise.inl"
// this one is done by hand as we format it
template <>
std::string DoStringise(const ResourceId &el)
{
RDCCOMPILE_ASSERT(sizeof(el) == sizeof(uint64_t), "ResourceId is no longer 1:1 with uint64_t");
return StringFormat::Fmt("ResourceId(%llu)", el);
}
BASIC_TYPE_SERIALISE_STRINGIFY(ResourceId, (uint64_t &)el, SDBasic::UnsignedInteger, 8);
INSTANTIATE_SERIALISE_TYPE(ResourceId);
#if ENABLED(RDOC_LINUX) && ENABLED(RDOC_XLIB)
#include <X11/Xlib.h>
#endif
// from image_viewer.cpp
ReplayStatus IMG_CreateReplayDevice(RDCFile *rdc, IReplayDriver **driver);
template <>
std::string DoStringise(const RDCDriver &el)
{
BEGIN_ENUM_STRINGISE(RDCDriver);
{
STRINGISE_ENUM_NAMED(RDC_Unknown, "Unknown");
STRINGISE_ENUM_NAMED(RDC_OpenGL, "OpenGL");
STRINGISE_ENUM_NAMED(RDC_OpenGLES, "OpenGLES");
STRINGISE_ENUM_NAMED(RDC_Mantle, "Mantle");
STRINGISE_ENUM_NAMED(RDC_D3D12, "D3D12");
STRINGISE_ENUM_NAMED(RDC_D3D11, "D3D11");
STRINGISE_ENUM_NAMED(RDC_D3D10, "D3D10");
STRINGISE_ENUM_NAMED(RDC_D3D9, "D3D9");
STRINGISE_ENUM_NAMED(RDC_D3D8, "D3D8");
STRINGISE_ENUM_NAMED(RDC_Image, "Image");
STRINGISE_ENUM_NAMED(RDC_Vulkan, "Vulkan");
}
END_ENUM_STRINGISE();
}
template <>
std::string DoStringise(const ReplayLogType &el)
{
BEGIN_ENUM_STRINGISE(ReplayLogType);
{
STRINGISE_ENUM_CLASS_NAMED(eReplay_Full, "Full replay including draw");
STRINGISE_ENUM_CLASS_NAMED(eReplay_WithoutDraw, "Replay without draw");
STRINGISE_ENUM_CLASS_NAMED(eReplay_OnlyDraw, "Replay only draw");
}
END_ENUM_STRINGISE();
}
template <>
std::string DoStringise(const WindowingSystem &el)
{
BEGIN_ENUM_STRINGISE(WindowingSystem);
{
STRINGISE_ENUM_CLASS(Unknown);
STRINGISE_ENUM_CLASS(Win32);
STRINGISE_ENUM_CLASS(Xlib);
STRINGISE_ENUM_CLASS(XCB);
STRINGISE_ENUM_CLASS(Android);
}
END_ENUM_STRINGISE();
}
template <>
std::string DoStringise(const RENDERDOC_InputButton &el)
{
char alphanumericbuf[2] = {'A', 0};
// enums map straight to ascii
if((el >= eRENDERDOC_Key_A && el <= eRENDERDOC_Key_Z) ||
(el >= eRENDERDOC_Key_0 && el <= eRENDERDOC_Key_9))
{
alphanumericbuf[0] = (char)el;
return alphanumericbuf;
}
BEGIN_ENUM_STRINGISE(RENDERDOC_InputButton);
{
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_Divide, "/");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_Multiply, "*");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_Subtract, "-");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_Plus, "+");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_F1, "F1");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_F2, "F2");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_F3, "F3");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_F4, "F4");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_F5, "F5");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_F6, "F6");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_F7, "F7");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_F8, "F8");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_F9, "F9");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_F10, "F10");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_F11, "F11");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_F12, "F12");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_Home, "Home");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_End, "End");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_Insert, "Insert");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_Delete, "Delete");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_PageUp, "PageUp");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_PageDn, "PageDn");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_Backspace, "Backspace");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_Tab, "Tab");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_PrtScrn, "PrtScrn");
STRINGISE_ENUM_NAMED(eRENDERDOC_Key_Pause, "Pause");
}
END_ENUM_STRINGISE();
}
template <>
std::string DoStringise(const SystemChunk &el)
{
BEGIN_ENUM_STRINGISE(SystemChunk);
{
STRINGISE_ENUM_CLASS_NAMED(DriverInit, "Driver Initialisation Parameters");
STRINGISE_ENUM_CLASS_NAMED(InitialContentsList, "List of Initial Contents Resources");
STRINGISE_ENUM_CLASS_NAMED(InitialContents, "Initial Contents");
}
END_ENUM_STRINGISE();
}
RenderDoc *RenderDoc::m_Inst = NULL;
RenderDoc &RenderDoc::Inst()
{
static RenderDoc realInst;
RenderDoc::m_Inst = &realInst;
return realInst;
}
void RenderDoc::RecreateCrashHandler()
{
UnloadCrashHandler();
#if ENABLED(RDOC_CRASH_HANDLER)
m_ExHandler = new CrashHandler(m_ExHandler);
#endif
if(m_ExHandler)
m_ExHandler->RegisterMemoryRegion(this, sizeof(RenderDoc));
}
void RenderDoc::UnloadCrashHandler()
{
if(m_ExHandler)
m_ExHandler->UnregisterMemoryRegion(this);
SAFE_DELETE(m_ExHandler);
}
RenderDoc::RenderDoc()
{
m_LogFile = "";
m_MarkerIndentLevel = 0;
m_CurrentDriver = RDC_Unknown;
m_CapturesActive = 0;
m_RemoteIdent = 0;
m_RemoteThread = 0;
m_Replay = false;
m_Cap = 0;
m_FocusKeys.clear();
m_FocusKeys.push_back(eRENDERDOC_Key_F11);
m_CaptureKeys.clear();
m_CaptureKeys.push_back(eRENDERDOC_Key_F12);
m_CaptureKeys.push_back(eRENDERDOC_Key_PrtScrn);
m_ProgressPtr = NULL;
m_ExHandler = NULL;
m_Overlay = eRENDERDOC_Overlay_Default;
m_VulkanCheck = NULL;
m_VulkanInstall = NULL;
m_TargetControlThreadShutdown = false;
m_ControlClientThreadShutdown = false;
}
void RenderDoc::Initialise()
{
Callstack::Init();
Network::Init();
Threading::Init();
m_RemoteIdent = 0;
m_RemoteThread = 0;
if(!IsReplayApp())
{
Process::ApplyEnvironmentModification();
uint32_t port = RenderDoc_FirstTargetControlPort;
Network::Socket *sock = Network::CreateServerSocket("0.0.0.0", port & 0xffff, 4);
while(sock == NULL)
{
port++;
if(port > RenderDoc_LastTargetControlPort)
{
m_RemoteIdent = 0;
break;
}
sock = Network::CreateServerSocket("0.0.0.0", port & 0xffff, 4);
}
if(sock)
{
m_RemoteIdent = port;
m_TargetControlThreadShutdown = false;
m_RemoteThread = Threading::CreateThread([sock]() { TargetControlServerThread(sock); });
RDCLOG("Listening for target control on %u", port);
}
else
{
RDCWARN("Couldn't open socket for target control");
}
}
// set default capture log - useful for when hooks aren't setup
// through the UI (and a log file isn't set manually)
{
string capture_filename;
const char *base = "RenderDoc_app";
if(IsReplayApp())
base = "RenderDoc";
FileIO::GetDefaultFiles(base, capture_filename, m_LoggingFilename, m_Target);
if(m_LogFile.empty())
SetLogFile(capture_filename.c_str());
RDCLOGFILE(m_LoggingFilename.c_str());
}
RDCLOG("RenderDoc v%s %s %s (%s) %s", MAJOR_MINOR_VERSION_STRING,
sizeof(uintptr_t) == sizeof(uint64_t) ? "x64" : "x86",
ENABLED(RDOC_RELEASE) ? "Release" : "Development", GIT_COMMIT_HASH,
IsReplayApp() ? "loaded in replay application" : "capturing application");
#if defined(DISTRIBUTION_VERSION)
RDCLOG("Packaged for %s (%s) - %s", DISTRIBUTION_NAME, DISTRIBUTION_VERSION, DISTRIBUTION_CONTACT);
#endif
Keyboard::Init();
m_FrameTimer.InitTimers();
m_ExHandler = NULL;
{
string curFile;
FileIO::GetExecutableFilename(curFile);
string f = strlower(curFile);
// only create crash handler when we're not in renderdoccmd.exe (to prevent infinite loop as
// the crash handler itself launches renderdoccmd.exe)
if(f.find("renderdoccmd.exe") == string::npos)
{
RecreateCrashHandler();
}
}
// begin printing to stdout/stderr after this point, earlier logging is debugging
// cruft that we don't want cluttering output.
// However we don't want to print in captured applications, since they may be outputting important
// information to stdout/stderr and being piped around and processed!
if(IsReplayApp())
RDCLOGOUTPUT();
}
RenderDoc::~RenderDoc()
{
if(m_ExHandler)
{
UnloadCrashHandler();
}
for(auto it = m_ShutdownFunctions.begin(); it != m_ShutdownFunctions.end(); ++it)
(*it)();
for(size_t i = 0; i < m_Captures.size(); i++)
{
if(m_Captures[i].retrieved)
{
RDCLOG("Removing remotely retrieved capture %s", m_Captures[i].path.c_str());
FileIO::Delete(m_Captures[i].path.c_str());
}
else
{
RDCLOG("'Leaking' unretrieved capture %s", m_Captures[i].path.c_str());
}
}
RDCSTOPLOGGING(m_LoggingFilename.c_str());
if(m_RemoteThread)
{
m_TargetControlThreadShutdown = true;
// On windows we can't join to this thread as it could lead to deadlocks, since we're
// performing this destructor in the middle of module unloading. However we want to
// ensure that the thread gets properly tidied up and closes its socket, so wait a little
// while to give it time to notice the shutdown signal and close itself.
Threading::Sleep(50);
Threading::CloseThread(m_RemoteThread);
m_RemoteThread = 0;
}
Network::Shutdown();
Threading::Shutdown();
StringFormat::Shutdown();
}
void RenderDoc::Shutdown()
{
if(m_ExHandler)
{
UnloadCrashHandler();
}
if(m_RemoteThread)
{
// explicitly wait for thread to shutdown, this call is not from module unloading and
// we want to be sure everything is gone before we remove our module & hooks
m_TargetControlThreadShutdown = true;
Threading::JoinThread(m_RemoteThread);
Threading::CloseThread(m_RemoteThread);
m_RemoteThread = 0;
}
}
void RenderDoc::ProcessGlobalEnvironment(GlobalEnvironment env, const std::vector<std::string> &args)
{
m_GlobalEnv = env;
#if ENABLED(RDOC_LINUX) && ENABLED(RDOC_XLIB)
if(!m_GlobalEnv.xlibDisplay)
m_GlobalEnv.xlibDisplay = XOpenDisplay(NULL);
#endif
if(!args.empty())
{
RDCDEBUG("Replay application launched with parameters:");
for(size_t i = 0; i < args.size(); i++)
RDCDEBUG("[%u]: %s", (uint32_t)i, args[i].c_str());
}
}
bool RenderDoc::MatchClosestWindow(void *&dev, void *&wnd)
{
DeviceWnd dw(dev, wnd);
// lower_bound and the DeviceWnd ordering (pointer compares, dev over wnd) means that if either
// element in dw is NULL we can go forward from this iterator and find the first wildcardMatch
// note that if dev is specified and wnd is NULL, this will actually point at the first
// wildcardMatch already and we can use it immediately (since which window of multiple we
// choose is undefined, so up to us). If dev is NULL there is no window ordering (since dev is
// the primary sorting value) so we just iterate through the whole map. It should be small in
// the majority of cases
auto it = m_WindowFrameCapturers.lower_bound(dw);
while(it != m_WindowFrameCapturers.end())
{
if(it->first.wildcardMatch(dw))
break;
++it;
}
if(it != m_WindowFrameCapturers.end())
{
dev = it->first.dev;
wnd = it->first.wnd;
return true;
}
return false;
}
IFrameCapturer *RenderDoc::MatchFrameCapturer(void *dev, void *wnd)
{
DeviceWnd dw(dev, wnd);
// try and find the closest frame capture registered, and update
// the values in dw to point to it precisely
bool exactMatch = MatchClosestWindow(dw.dev, dw.wnd);
if(!exactMatch)
{
// handle off-screen rendering where there are no device/window pairs in
// m_WindowFrameCapturers, instead we use the first matching device frame capturer
if(wnd == NULL)
{
auto defaultit = m_DeviceFrameCapturers.find(dev);
if(defaultit == m_DeviceFrameCapturers.end() && !m_DeviceFrameCapturers.empty())
defaultit = m_DeviceFrameCapturers.begin();
if(defaultit != m_DeviceFrameCapturers.end())
return defaultit->second;
}
RDCERR("Couldn't find matching frame capturer for device %p window %p", dev, wnd);
return NULL;
}
auto it = m_WindowFrameCapturers.find(dw);
if(it == m_WindowFrameCapturers.end())
{
RDCERR("Couldn't find frame capturer after exact match!");
return NULL;
}
return it->second.FrameCapturer;
}
void RenderDoc::StartFrameCapture(void *dev, void *wnd)
{
IFrameCapturer *frameCap = MatchFrameCapturer(dev, wnd);
if(frameCap)
{
frameCap->StartFrameCapture(dev, wnd);
m_CapturesActive++;
}
}
void RenderDoc::SetActiveWindow(void *dev, void *wnd)
{
DeviceWnd dw(dev, wnd);
auto it = m_WindowFrameCapturers.find(dw);
if(it == m_WindowFrameCapturers.end())
{
RDCERR("Couldn't find frame capturer for device %p window %p", dev, wnd);
return;
}
m_ActiveWindow = dw;
}
bool RenderDoc::EndFrameCapture(void *dev, void *wnd)
{
IFrameCapturer *frameCap = MatchFrameCapturer(dev, wnd);
if(frameCap)
{
m_CapturesActive--;
return frameCap->EndFrameCapture(dev, wnd);
}
return false;
}
bool RenderDoc::IsTargetControlConnected()
{
SCOPED_LOCK(RenderDoc::Inst().m_SingleClientLock);
return !RenderDoc::Inst().m_SingleClientName.empty();
}
string RenderDoc::GetTargetControlUsername()
{
SCOPED_LOCK(RenderDoc::Inst().m_SingleClientLock);
return RenderDoc::Inst().m_SingleClientName;
}
void RenderDoc::Tick()
{
static bool prev_focus = false;
static bool prev_cap = false;
bool cur_focus = false;
for(size_t i = 0; i < m_FocusKeys.size(); i++)
cur_focus |= Keyboard::GetKeyState(m_FocusKeys[i]);
bool cur_cap = false;
for(size_t i = 0; i < m_CaptureKeys.size(); i++)
cur_cap |= Keyboard::GetKeyState(m_CaptureKeys[i]);
m_FrameTimer.UpdateTimers();
if(!prev_focus && cur_focus)
{
m_Cap = 0;
// can only shift focus if we have multiple windows
if(m_WindowFrameCapturers.size() > 1)
{
for(auto it = m_WindowFrameCapturers.begin(); it != m_WindowFrameCapturers.end(); ++it)
{
if(it->first == m_ActiveWindow)
{
auto nextit = it;
++nextit;
if(nextit != m_WindowFrameCapturers.end())
m_ActiveWindow = nextit->first;
else
m_ActiveWindow = m_WindowFrameCapturers.begin()->first;
break;
}
}
}
}
if(!prev_cap && cur_cap)
{
TriggerCapture(1);
}
prev_focus = cur_focus;
prev_cap = cur_cap;
}
string RenderDoc::GetOverlayText(RDCDriver driver, uint32_t frameNumber, int flags)
{
const bool activeWindow = (flags & eOverlay_ActiveWindow);
const bool capturesEnabled = (flags & eOverlay_CaptureDisabled) == 0;
uint32_t overlay = GetOverlayBits();
std::string overlayText = ToStr(driver) + ". ";
if(activeWindow)
{
vector<RENDERDOC_InputButton> keys = GetCaptureKeys();
if(capturesEnabled)
{
if(Keyboard::PlatformHasKeyInput())
{
for(size_t i = 0; i < keys.size(); i++)
{
if(i > 0)
overlayText += ", ";
overlayText += ToStr(keys[i]);
}
if(!keys.empty())
overlayText += " to capture.";
}
else
{
if(IsTargetControlConnected())
overlayText += "Connected by " + GetTargetControlUsername() + ".";
else
overlayText += "No remote access connection.";
}
}
if(overlay & eRENDERDOC_Overlay_FrameNumber)
{
overlayText += StringFormat::Fmt(" Frame: %d.", frameNumber);
}
if(overlay & eRENDERDOC_Overlay_FrameRate)
{
overlayText +=
StringFormat::Fmt(" %.2lf ms (%.2lf .. %.2lf) (%.0lf FPS)", m_FrameTimer.GetAvgFrameTime(),
m_FrameTimer.GetMinFrameTime(), m_FrameTimer.GetMaxFrameTime(),
// max with 0.01ms so that we don't divide by zero
1000.0f / RDCMAX(0.01, m_FrameTimer.GetAvgFrameTime()));
}
overlayText += "\n";
if((overlay & eRENDERDOC_Overlay_CaptureList) && capturesEnabled)
{
overlayText += StringFormat::Fmt("%d Captures saved.\n", (uint32_t)m_Captures.size());
uint64_t now = Timing::GetUnixTimestamp();
for(size_t i = 0; i < m_Captures.size(); i++)
{
if(now - m_Captures[i].timestamp < 20)
{
overlayText += StringFormat::Fmt("Captured frame %d.\n", m_Captures[i].frameNumber);
}
}
}
#if ENABLED(RDOC_DEVEL)
overlayText += StringFormat::Fmt("%llu chunks - %.2f MB\n", Chunk::NumLiveChunks(),
float(Chunk::TotalMem()) / 1024.0f / 1024.0f);
#endif
}
else if(capturesEnabled)
{
vector<RENDERDOC_InputButton> keys = GetFocusKeys();
overlayText += "Inactive window.";
for(size_t i = 0; i < keys.size(); i++)
{
if(i == 0)
overlayText += " ";
else
overlayText += ", ";
overlayText += ToStr(keys[i]);
}
if(!keys.empty())
overlayText += " to cycle between windows";
overlayText += "\n";
}
return overlayText;
}
bool RenderDoc::ShouldTriggerCapture(uint32_t frameNumber)
{
bool ret = m_Cap > 0;
if(m_Cap > 0)
m_Cap--;
set<uint32_t> frames;
frames.swap(m_QueuedFrameCaptures);
for(auto it = frames.begin(); it != frames.end(); ++it)
{
if(*it < frameNumber)
{
// discard, this frame is past.
}
else if((*it) - 1 == frameNumber)
{
// we want to capture the next frame
ret = true;
}
else
{
// not hit this yet, keep it around
m_QueuedFrameCaptures.insert(*it);
}
}
return ret;
}
RDCFile *RenderDoc::CreateRDC(uint32_t frameNum, void *thpixels, size_t thlen, uint16_t thwidth,
uint16_t thheight)
{
RDCFile *ret = new RDCFile;
m_CurrentLogFile = StringFormat::Fmt("%s_frame%u.rdc", m_LogFile.c_str(), frameNum);
// make sure we don't stomp another capture if we make multiple captures in the same frame.
{
SCOPED_LOCK(m_CaptureLock);
int altnum = 2;
while(std::find_if(m_Captures.begin(), m_Captures.end(), [this](const CaptureData &o) {
return o.path == m_CurrentLogFile;
}) != m_Captures.end())
{
m_CurrentLogFile = StringFormat::Fmt("%s_frame%u_%d.rdc", m_LogFile.c_str(), frameNum, altnum);
altnum++;
}
}
RDCThumb th;
RDCThumb *thumb = NULL;
if(thpixels)
{
th.len = (uint32_t)thlen;
th.pixels = (const byte *)thpixels;
th.width = thwidth;
th.height = thheight;
thumb = &th;
}
ret->SetData(m_CurrentDriver, m_CurrentDriverName.c_str(), OSUtility::GetMachineIdent(), thumb);
ret->Create(m_CurrentLogFile.c_str());
if(ret->ErrorCode() != ContainerError::NoError)
{
RDCERR("Error creating RDC at '%s'", m_CurrentLogFile.c_str());
SAFE_DELETE(ret);
}
return ret;
}
bool RenderDoc::HasReplayDriver(RDCDriver driver) const
{
// Image driver is handled specially and isn't registered in the map
if(driver == RDC_Image)
return true;
return m_ReplayDriverProviders.find(driver) != m_ReplayDriverProviders.end();
}
bool RenderDoc::HasRemoteDriver(RDCDriver driver) const
{
if(m_RemoteDriverProviders.find(driver) != m_RemoteDriverProviders.end())
return true;
return HasReplayDriver(driver);
}
void RenderDoc::RegisterReplayProvider(RDCDriver driver, const char *name,
ReplayDriverProvider provider)
{
if(HasReplayDriver(driver))
RDCERR("Re-registering provider for %s (was %s)", name, m_DriverNames[driver].c_str());
if(HasRemoteDriver(driver))
RDCWARN("Registering local provider %s for existing remote provider %s", name,
m_DriverNames[driver].c_str());
m_DriverNames[driver] = name;
m_ReplayDriverProviders[driver] = provider;
}
void RenderDoc::RegisterRemoteProvider(RDCDriver driver, const char *name,
RemoteDriverProvider provider)
{
if(HasRemoteDriver(driver))
RDCERR("Re-registering provider for %s (was %s)", name, m_DriverNames[driver].c_str());
if(HasReplayDriver(driver))
RDCWARN("Registering remote provider %s for existing local provider %s", name,
m_DriverNames[driver].c_str());
m_DriverNames[driver] = name;
m_RemoteDriverProviders[driver] = provider;
}
void RenderDoc::RegisterStructuredProcessor(RDCDriver driver, StructuredProcessor provider)
{
RDCASSERT(m_StructProcesssors.find(driver) == m_StructProcesssors.end());
m_StructProcesssors[driver] = provider;
}
void RenderDoc::RegisterCaptureExporter(const char *filetype, const char *description,
CaptureExporter exporter)
{
RDCASSERT(m_ImportExportFormats.find(filetype) == m_ImportExportFormats.end());
m_ImportExportFormats[filetype] = description;
m_Exporters[filetype] = exporter;
}
void RenderDoc::RegisterCaptureImportExporter(const char *filetype, const char *description,
CaptureImporter importer, CaptureExporter exporter)
{
RDCASSERT(m_ImportExportFormats.find(filetype) == m_ImportExportFormats.end());
m_ImportExportFormats[filetype] = description;
m_Importers[filetype] = importer;
m_Exporters[filetype] = exporter;
}
StructuredProcessor RenderDoc::GetStructuredProcessor(RDCDriver driver)
{
auto it = m_StructProcesssors.find(driver);
if(it == m_StructProcesssors.end())
return NULL;
return it->second;
}
CaptureExporter RenderDoc::GetCaptureExporter(const char *filetype)
{
auto it = m_Exporters.find(filetype);
if(it == m_Exporters.end())
return NULL;
return it->second;
}
CaptureImporter RenderDoc::GetCaptureImporter(const char *filetype)
{
auto it = m_Importers.find(filetype);
if(it == m_Importers.end())
return NULL;
return it->second;
}
std::vector<CaptureFileFormat> RenderDoc::GetCaptureFileFormats()
{
std::vector<CaptureFileFormat> ret;
CaptureFileFormat rdc;
rdc.name = "rdc";
rdc.description = "Native RDC capture file format.";
rdc.openSupported = true;
rdc.convertSupported = true;
ret.push_back(rdc);
for(auto it = m_ImportExportFormats.begin(); it != m_ImportExportFormats.end(); ++it)
{
CaptureFileFormat fmt;
fmt.name = it->first;
fmt.description = it->second;
rdc.openSupported = m_Importers.find(it->first) != m_Importers.end();
rdc.convertSupported = m_Exporters.find(it->first) != m_Exporters.end();
RDCASSERT(rdc.openSupported || rdc.convertSupported);
ret.push_back(fmt);
}
return ret;
}
bool RenderDoc::HasReplaySupport(RDCDriver driverType)
{
if(driverType == RDC_Image)
return true;
if(driverType == RDC_Unknown && !m_ReplayDriverProviders.empty())
return true;
return m_ReplayDriverProviders.find(driverType) != m_ReplayDriverProviders.end();
}
ReplayStatus RenderDoc::CreateProxyReplayDriver(RDCDriver proxyDriver, IReplayDriver **driver)
{
// passing RDC_Unknown means 'I don't care, give me a proxy driver of any type'
if(proxyDriver == RDC_Unknown)
{
if(!m_ReplayDriverProviders.empty())
return m_ReplayDriverProviders.begin()->second(NULL, driver);
}
if(m_ReplayDriverProviders.find(proxyDriver) != m_ReplayDriverProviders.end())
return m_ReplayDriverProviders[proxyDriver](NULL, driver);
RDCERR("Unsupported replay driver requested: %s", ToStr(proxyDriver).c_str());
return ReplayStatus::APIUnsupported;
}
ReplayStatus RenderDoc::CreateReplayDriver(RDCFile *rdc, IReplayDriver **driver)
{
if(driver == NULL)
return ReplayStatus::InternalError;
// allows passing NULL rdcfile as 'I don't care, give me a proxy driver of any type'
if(rdc == NULL)
{
if(!m_ReplayDriverProviders.empty())
return m_ReplayDriverProviders.begin()->second(NULL, driver);
RDCERR("Request for proxy replay device, but no replay providers are available.");
return ReplayStatus::InternalError;
}
RDCDriver driverType = rdc->GetDriver();
// image support is special, handle it here
if(driverType == RDC_Image)
return IMG_CreateReplayDevice(rdc, driver);
if(m_ReplayDriverProviders.find(driverType) != m_ReplayDriverProviders.end())
return m_ReplayDriverProviders[driverType](rdc, driver);
RDCERR("Unsupported replay driver requested: %s", ToStr(driverType).c_str());
return ReplayStatus::APIUnsupported;
}
ReplayStatus RenderDoc::CreateRemoteDriver(RDCFile *rdc, IRemoteDriver **driver)
{
if(rdc == NULL || driver == NULL)
return ReplayStatus::InternalError;
RDCDriver driverType = rdc->GetDriver();
if(m_RemoteDriverProviders.find(driverType) != m_RemoteDriverProviders.end())
return m_RemoteDriverProviders[driverType](rdc, driver);
// replay drivers are remote drivers, fall back and try them
if(m_ReplayDriverProviders.find(driverType) != m_ReplayDriverProviders.end())
{
IReplayDriver *dr = NULL;
ReplayStatus status = m_ReplayDriverProviders[driverType](rdc, &dr);
if(status == ReplayStatus::Succeeded)
*driver = (IRemoteDriver *)dr;
else
RDCASSERT(dr == NULL);
return status;
}
RDCERR("Unsupported replay driver requested: %s", ToStr(driverType).c_str());
return ReplayStatus::APIUnsupported;
}
void RenderDoc::SetCurrentDriver(RDCDriver driver)
{
if(!HasReplayDriver(driver) && !HasRemoteDriver(driver))
{
RDCFATAL("Trying to register unsupported driver!");
}
m_CurrentDriver = driver;
m_CurrentDriverName = m_DriverNames[driver];
}
void RenderDoc::GetCurrentDriver(RDCDriver &driver, string &name)
{
driver = m_CurrentDriver;
name = m_CurrentDriverName;
}
map<RDCDriver, string> RenderDoc::GetReplayDrivers()
{
map<RDCDriver, string> ret;
for(auto it = m_ReplayDriverProviders.begin(); it != m_ReplayDriverProviders.end(); ++it)
ret[it->first] = m_DriverNames[it->first];
return ret;
}
map<RDCDriver, string> RenderDoc::GetRemoteDrivers()
{
map<RDCDriver, string> ret;
for(auto it = m_RemoteDriverProviders.begin(); it != m_RemoteDriverProviders.end(); ++it)
ret[it->first] = m_DriverNames[it->first];
// replay drivers are remote drivers.
for(auto it = m_ReplayDriverProviders.begin(); it != m_ReplayDriverProviders.end(); ++it)
ret[it->first] = m_DriverNames[it->first];
return ret;
}
void RenderDoc::SetCaptureOptions(const CaptureOptions &opts)
{
m_Options = opts;
LibraryHooks::GetInstance().OptionsUpdated();
}