forked from baldurk/renderdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvk_core.cpp
More file actions
3077 lines (2499 loc) · 95.5 KB
/
vk_core.cpp
File metadata and controls
3077 lines (2499 loc) · 95.5 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
*
* 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 "vk_core.h"
#include "jpeg-compressor/jpge.h"
#include "maths/formatpacking.h"
#include "serialise/string_utils.h"
#include "vk_debug.h"
const char *VkChunkNames[] = {
"WrappedVulkan::Initialisation",
"vkCreateInstance",
"vkEnumeratePhysicalDevices",
"vkCreateDevice",
"vkGetDeviceQueue",
"vkAllocMemory",
"vkUnmapMemory",
"vkFlushMappedMemoryRanges",
"vkFreeMemory",
"vkCreateCommandPool",
"vkResetCommandPool",
"vkCreateCommandBuffer",
"vkCreateFramebuffer",
"vkCreateRenderPass",
"vkCreateDescriptorPool",
"vkCreateDescriptorSetLayout",
"vkCreateBuffer",
"vkCreateBufferView",
"vkCreateImage",
"vkCreateImageView",
"vkCreateDepthTargetView",
"vkCreateSampler",
"vkCreateShaderModule",
"vkCreatePipelineLayout",
"vkCreatePipelineCache",
"vkCreateGraphicsPipelines",
"vkCreateComputePipelines",
"vkGetSwapchainImagesKHR",
"vkCreateSemaphore",
"vkCreateFence",
"vkGetFenceStatus",
"vkResetFences",
"vkWaitForFences",
"vkCreateEvent",
"vkGetEventStatus",
"vkSetEvent",
"vkResetEvent",
"vkCreateQueryPool",
"vkAllocDescriptorSets",
"vkUpdateDescriptorSets",
"vkBeginCommandBuffer",
"vkEndCommandBuffer",
"vkQueueWaitIdle",
"vkDeviceWaitIdle",
"vkQueueSubmit",
"vkBindBufferMemory",
"vkBindImageMemory",
"vkQueueBindSparse",
"vkCmdBeginRenderPass",
"vkCmdNextSubpass",
"vkCmdExecuteCommands",
"vkCmdEndRenderPass",
"vkCmdBindPipeline",
"vkCmdSetViewport",
"vkCmdSetScissor",
"vkCmdSetLineWidth",
"vkCmdSetDepthBias",
"vkCmdSetBlendConstants",
"vkCmdSetDepthBounds",
"vkCmdSetStencilCompareMask",
"vkCmdSetStencilWriteMask",
"vkCmdSetStencilReference",
"vkCmdBindDescriptorSet",
"vkCmdBindVertexBuffers",
"vkCmdBindIndexBuffer",
"vkCmdCopyBufferToImage",
"vkCmdCopyImageToBuffer",
"vkCmdCopyBuffer",
"vkCmdCopyImage",
"vkCmdBlitImage",
"vkCmdResolveImage",
"vkCmdUpdateBuffer",
"vkCmdFillBuffer",
"vkCmdPushConstants",
"vkCmdClearColorImage",
"vkCmdClearDepthStencilImage",
"vkCmdClearAttachments",
"vkCmdPipelineBarrier",
"vkCmdWriteTimestamp",
"vkCmdCopyQueryPoolResults",
"vkCmdBeginQuery",
"vkCmdEndQuery",
"vkCmdResetQueryPool",
"vkCmdSetEvent",
"vkCmdResetEvent",
"vkCmdWaitEvents",
"vkCmdDraw",
"vkCmdDrawIndirect",
"vkCmdDrawIndexed",
"vkCmdDrawIndexedIndirect",
"vkCmdDispatch",
"vkCmdDispatchIndirect",
"vkCmdDebugMarkerBeginEXT",
"vkCmdDebugMarkerInsertEXT",
"vkCmdDebugMarkerEndEXT",
"vkDebugMarkerSetObjectNameEXT",
"vkDebugMarkerSetObjectTagEXT",
"vkCreateSwapchainKHR",
"Debug Messages",
"Capture",
"BeginCapture",
"EndCapture",
};
VkInitParams::VkInitParams()
{
SerialiseVersion = VK_SERIALISE_VERSION;
AppVersion = 0;
EngineVersion = 0;
APIVersion = 0;
}
ReplayCreateStatus VkInitParams::Serialise()
{
Serialiser *localSerialiser = GetSerialiser();
SERIALISE_ELEMENT(uint32_t, ver, VK_SERIALISE_VERSION);
SerialiseVersion = ver;
if(ver != VK_SERIALISE_VERSION)
{
RDCERR("Incompatible Vulkan serialise version, expected %d got %d", VK_SERIALISE_VERSION, ver);
return eReplayCreate_APIIncompatibleVersion;
}
localSerialiser->Serialise("AppName", AppName);
localSerialiser->Serialise("EngineName", EngineName);
localSerialiser->Serialise("AppVersion", AppVersion);
localSerialiser->Serialise("EngineVersion", EngineVersion);
localSerialiser->Serialise("APIVersion", APIVersion);
localSerialiser->Serialise("Layers", Layers);
localSerialiser->Serialise("Extensions", Extensions);
localSerialiser->Serialise("InstanceID", InstanceID);
return eReplayCreate_Success;
}
void VkInitParams::Set(const VkInstanceCreateInfo *pCreateInfo, ResourceId inst)
{
RDCASSERT(pCreateInfo);
if(pCreateInfo->pApplicationInfo)
{
// we don't support any extensions on appinfo structure
RDCASSERT(pCreateInfo->pApplicationInfo->pNext == NULL);
AppName = pCreateInfo->pApplicationInfo->pApplicationName
? pCreateInfo->pApplicationInfo->pApplicationName
: "";
EngineName =
pCreateInfo->pApplicationInfo->pEngineName ? pCreateInfo->pApplicationInfo->pEngineName : "";
AppVersion = pCreateInfo->pApplicationInfo->applicationVersion;
EngineVersion = pCreateInfo->pApplicationInfo->engineVersion;
APIVersion = pCreateInfo->pApplicationInfo->apiVersion;
}
else
{
AppName = "";
EngineName = "";
AppVersion = 0;
EngineVersion = 0;
APIVersion = 0;
}
Layers.resize(pCreateInfo->enabledLayerCount);
Extensions.resize(pCreateInfo->enabledExtensionCount);
for(uint32_t i = 0; i < pCreateInfo->enabledLayerCount; i++)
Layers[i] = pCreateInfo->ppEnabledLayerNames[i];
for(uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++)
Extensions[i] = pCreateInfo->ppEnabledExtensionNames[i];
InstanceID = inst;
}
WrappedVulkan::WrappedVulkan(const char *logFilename) : m_RenderState(&m_CreationInfo)
{
#if ENABLED(RDOC_RELEASE)
const bool debugSerialiser = false;
#else
const bool debugSerialiser = true;
#endif
if(RenderDoc::Inst().IsReplayApp())
{
m_State = READING;
if(logFilename)
{
m_pSerialiser = new Serialiser(logFilename, Serialiser::READING, debugSerialiser);
}
else
{
byte dummy[4];
m_pSerialiser = new Serialiser(4, dummy, false);
}
}
else
{
m_State = WRITING_IDLE;
m_pSerialiser = new Serialiser(NULL, Serialiser::WRITING, debugSerialiser);
}
InitSPIRVCompiler();
RenderDoc::Inst().RegisterShutdownFunction(&ShutdownSPIRVCompiler);
m_Replay.SetDriver(this);
m_FrameCounter = 0;
m_AppControlledCapture = false;
threadSerialiserTLSSlot = Threading::AllocateTLSSlot();
tempMemoryTLSSlot = Threading::AllocateTLSSlot();
debugMessageSinkTLSSlot = Threading::AllocateTLSSlot();
m_RootEventID = 1;
m_RootDrawcallID = 1;
m_FirstEventID = 0;
m_LastEventID = ~0U;
m_DrawcallCallback = NULL;
m_CurChunkOffset = 0;
m_AddedDrawcall = false;
m_LastCmdBufferID = ResourceId();
m_DrawcallStack.push_back(&m_ParentDrawcall);
m_SetDeviceLoaderData = NULL;
m_ResourceManager = new VulkanResourceManager(m_State, m_pSerialiser, this);
m_DebugManager = NULL;
m_pSerialiser->SetUserData(m_ResourceManager);
m_RenderState.m_ResourceManager = GetResourceManager();
m_Instance = VK_NULL_HANDLE;
m_PhysicalDevice = VK_NULL_HANDLE;
m_Device = VK_NULL_HANDLE;
m_Queue = VK_NULL_HANDLE;
m_QueueFamilyIdx = 0;
m_SupportedQueueFamily = 0;
m_DbgMsgCallback = VK_NULL_HANDLE;
m_HeaderChunk = NULL;
if(!RenderDoc::Inst().IsReplayApp())
{
m_FrameCaptureRecord = GetResourceManager()->AddResourceRecord(ResourceIDGen::GetNewUniqueID());
m_FrameCaptureRecord->DataInSerialiser = false;
m_FrameCaptureRecord->Length = 0;
m_FrameCaptureRecord->SpecialResource = true;
}
else
{
m_FrameCaptureRecord = NULL;
ResourceIDGen::SetReplayResourceIDs();
}
m_pSerialiser->SetChunkNameLookup(&GetChunkName);
//////////////////////////////////////////////////////////////////////////
// Compile time asserts
RDCCOMPILE_ASSERT(ARRAY_COUNT(VkChunkNames) == NUM_VULKAN_CHUNKS - FIRST_CHUNK_ID,
"Not right number of chunk names");
}
WrappedVulkan::~WrappedVulkan()
{
// records must be deleted before resource manager shutdown
if(m_FrameCaptureRecord)
{
RDCASSERT(m_FrameCaptureRecord->GetRefCount() == 1);
m_FrameCaptureRecord->Delete(GetResourceManager());
m_FrameCaptureRecord = NULL;
}
// in case the application leaked some objects, avoid crashing trying
// to release them ourselves by clearing the resource manager.
// In a well-behaved application, this should be a no-op.
m_ResourceManager->ClearWithoutReleasing();
SAFE_DELETE(m_ResourceManager);
SAFE_DELETE(m_pSerialiser);
for(size_t i = 0; i < m_MemIdxMaps.size(); i++)
delete[] m_MemIdxMaps[i];
for(size_t i = 0; i < m_ThreadSerialisers.size(); i++)
delete m_ThreadSerialisers[i];
for(size_t i = 0; i < m_ThreadTempMem.size(); i++)
{
delete[] m_ThreadTempMem[i]->memory;
delete m_ThreadTempMem[i];
}
}
VkCommandBuffer WrappedVulkan::GetNextCmd()
{
VkCommandBuffer ret;
if(!m_InternalCmds.freecmds.empty())
{
ret = m_InternalCmds.freecmds.back();
m_InternalCmds.freecmds.pop_back();
ObjDisp(ret)->ResetCommandBuffer(Unwrap(ret), 0);
}
else
{
VkCommandBufferAllocateInfo cmdInfo = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, NULL,
Unwrap(m_InternalCmds.cmdpool),
VK_COMMAND_BUFFER_LEVEL_PRIMARY, 1};
VkResult vkr = ObjDisp(m_Device)->AllocateCommandBuffers(Unwrap(m_Device), &cmdInfo, &ret);
if(m_SetDeviceLoaderData)
m_SetDeviceLoaderData(m_Device, ret);
else
SetDispatchTableOverMagicNumber(m_Device, ret);
RDCASSERTEQUAL(vkr, VK_SUCCESS);
GetResourceManager()->WrapResource(Unwrap(m_Device), ret);
}
m_InternalCmds.pendingcmds.push_back(ret);
return ret;
}
void WrappedVulkan::SubmitCmds()
{
// nothing to do
if(m_InternalCmds.pendingcmds.empty())
return;
vector<VkCommandBuffer> cmds = m_InternalCmds.pendingcmds;
for(size_t i = 0; i < cmds.size(); i++)
cmds[i] = Unwrap(cmds[i]);
VkSubmitInfo submitInfo = {
VK_STRUCTURE_TYPE_SUBMIT_INFO,
NULL,
0,
NULL,
NULL, // wait semaphores
(uint32_t)cmds.size(),
&cmds[0], // command buffers
0,
NULL, // signal semaphores
};
// we might have work to do (e.g. debug manager creation command buffer) but
// no queue, if the device is destroyed immediately. In this case we can just
// skip the submit
if(m_Queue != VK_NULL_HANDLE)
{
VkResult vkr = ObjDisp(m_Queue)->QueueSubmit(Unwrap(m_Queue), 1, &submitInfo, VK_NULL_HANDLE);
RDCASSERTEQUAL(vkr, VK_SUCCESS);
}
#if ENABLED(SINGLE_FLUSH_VALIDATE)
FlushQ();
#endif
m_InternalCmds.submittedcmds.insert(m_InternalCmds.submittedcmds.end(),
m_InternalCmds.pendingcmds.begin(),
m_InternalCmds.pendingcmds.end());
m_InternalCmds.pendingcmds.clear();
}
VkSemaphore WrappedVulkan::GetNextSemaphore()
{
VkSemaphore ret;
if(!m_InternalCmds.freesems.empty())
{
ret = m_InternalCmds.freesems.back();
m_InternalCmds.freesems.pop_back();
// assume semaphore is back to unsignaled state after being waited on
}
else
{
VkSemaphoreCreateInfo semInfo = {VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO};
VkResult vkr = ObjDisp(m_Device)->CreateSemaphore(Unwrap(m_Device), &semInfo, NULL, &ret);
RDCASSERTEQUAL(vkr, VK_SUCCESS);
GetResourceManager()->WrapResource(Unwrap(m_Device), ret);
}
m_InternalCmds.pendingsems.push_back(ret);
return ret;
}
void WrappedVulkan::SubmitSemaphores()
{
// nothing to do
if(m_InternalCmds.pendingsems.empty())
return;
// no actual submission, just mark them as 'done with' so they will be
// recycled on next flush
m_InternalCmds.submittedsems.insert(m_InternalCmds.submittedsems.end(),
m_InternalCmds.pendingsems.begin(),
m_InternalCmds.pendingsems.end());
m_InternalCmds.pendingsems.clear();
}
void WrappedVulkan::FlushQ()
{
// VKTODOLOW could do away with the need for this function by keeping
// commands until N presents later, or something, or checking on fences.
// If we do so, then check each use for FlushQ to see if it needs a
// CPU-GPU sync or whether it is just looking to recycle command buffers
// (Particularly the one in vkQueuePresentKHR drawing the overlay)
// see comment in SubmitQ()
if(m_Queue != VK_NULL_HANDLE)
{
ObjDisp(m_Queue)->QueueWaitIdle(Unwrap(m_Queue));
}
#if ENABLED(SINGLE_FLUSH_VALIDATE)
{
ObjDisp(m_Queue)->DeviceWaitIdle(Unwrap(m_Device));
VkResult vkr = ObjDisp(m_Queue)->DeviceWaitIdle(Unwrap(m_Device));
RDCASSERTEQUAL(vkr, VK_SUCCESS);
}
#endif
if(!m_InternalCmds.submittedcmds.empty())
{
m_InternalCmds.freecmds.insert(m_InternalCmds.freecmds.end(),
m_InternalCmds.submittedcmds.begin(),
m_InternalCmds.submittedcmds.end());
m_InternalCmds.submittedcmds.clear();
}
}
uint32_t WrappedVulkan::HandlePreCallback(VkCommandBuffer commandBuffer, DrawcallFlags type,
uint32_t multiDrawOffset)
{
if(!m_DrawcallCallback)
return 0;
// look up the EID this drawcall came from
DrawcallUse use(m_CurChunkOffset, 0);
auto it = std::lower_bound(m_DrawcallUses.begin(), m_DrawcallUses.end(), use);
RDCASSERT(it != m_DrawcallUses.end());
uint32_t eventID = it->eventID;
RDCASSERT(eventID != 0);
// handle all aliases of this drawcall as long as it's not a multidraw
const FetchDrawcall *draw = GetDrawcall(eventID);
if(draw == NULL || (draw->flags & eDraw_MultiDraw) == 0)
{
++it;
while(it != m_DrawcallUses.end() && it->fileOffset == m_CurChunkOffset)
{
m_DrawcallCallback->AliasEvent(eventID, it->eventID);
++it;
}
}
eventID += multiDrawOffset;
if(type == eDraw_Drawcall)
m_DrawcallCallback->PreDraw(eventID, commandBuffer);
else if(type == eDraw_Dispatch)
m_DrawcallCallback->PreDispatch(eventID, commandBuffer);
else
m_DrawcallCallback->PreMisc(eventID, type, commandBuffer);
return eventID;
}
const char *WrappedVulkan::GetChunkName(uint32_t idx)
{
if(idx == CREATE_PARAMS)
return "Create Params";
if(idx == THUMBNAIL_DATA)
return "Thumbnail Data";
if(idx == DRIVER_INIT_PARAMS)
return "Driver Init Params";
if(idx == INITIAL_CONTENTS)
return "Initial Contents";
if(idx < FIRST_CHUNK_ID || idx >= NUM_VULKAN_CHUNKS)
return "<unknown>";
return VkChunkNames[idx - FIRST_CHUNK_ID];
}
template <>
string ToStrHelper<false, VulkanChunkType>::Get(const VulkanChunkType &el)
{
return WrappedVulkan::GetChunkName(el);
}
WrappedVulkan::ScopedDebugMessageSink::ScopedDebugMessageSink(WrappedVulkan *driver)
{
driver->SetDebugMessageSink(this);
m_pDriver = driver;
}
WrappedVulkan::ScopedDebugMessageSink::~ScopedDebugMessageSink()
{
m_pDriver->SetDebugMessageSink(NULL);
}
WrappedVulkan::ScopedDebugMessageSink *WrappedVulkan::GetDebugMessageSink()
{
return (WrappedVulkan::ScopedDebugMessageSink *)Threading::GetTLSValue(debugMessageSinkTLSSlot);
}
void WrappedVulkan::SetDebugMessageSink(WrappedVulkan::ScopedDebugMessageSink *sink)
{
Threading::SetTLSValue(debugMessageSinkTLSSlot, (void *)sink);
}
byte *WrappedVulkan::GetTempMemory(size_t s)
{
TempMem *mem = (TempMem *)Threading::GetTLSValue(tempMemoryTLSSlot);
if(mem && mem->size >= s)
return mem->memory;
// alloc or grow alloc
TempMem *newmem = mem;
if(!newmem)
newmem = new TempMem();
// free old memory, don't need to keep contents
if(newmem->memory)
delete[] newmem->memory;
// alloc new memory
newmem->size = s;
newmem->memory = new byte[s];
Threading::SetTLSValue(tempMemoryTLSSlot, (void *)newmem);
// if this is entirely new, save it for deletion on shutdown
if(!mem)
{
SCOPED_LOCK(m_ThreadTempMemLock);
m_ThreadTempMem.push_back(newmem);
}
return newmem->memory;
}
Serialiser *WrappedVulkan::GetThreadSerialiser()
{
Serialiser *ser = (Serialiser *)Threading::GetTLSValue(threadSerialiserTLSSlot);
if(ser)
return ser;
// slow path, but rare
#if ENABLED(RDOC_RELEASE)
const bool debugSerialiser = false;
#else
const bool debugSerialiser = true;
#endif
ser = new Serialiser(NULL, Serialiser::WRITING, debugSerialiser);
ser->SetUserData(m_ResourceManager);
ser->SetChunkNameLookup(&GetChunkName);
Threading::SetTLSValue(threadSerialiserTLSSlot, (void *)ser);
{
SCOPED_LOCK(m_ThreadSerialisersLock);
m_ThreadSerialisers.push_back(ser);
}
return ser;
}
static VkResult FillPropertyCountAndList(const VkExtensionProperties *src, uint32_t numExts,
uint32_t *dstCount, VkExtensionProperties *dstProps)
{
if(dstCount && !dstProps)
{
// just returning the number of extensions
*dstCount = numExts;
return VK_SUCCESS;
}
else if(dstCount && dstProps)
{
uint32_t dstSpace = *dstCount;
// return the number of extensions.
*dstCount = RDCMIN(numExts, dstSpace);
// copy as much as there's space for, up to how many there are
memcpy(dstProps, src, sizeof(VkExtensionProperties) * RDCMIN(numExts, dstSpace));
// if there was enough space, return success, else incomplete
if(dstSpace >= numExts)
return VK_SUCCESS;
else
return VK_INCOMPLETE;
}
// both parameters were NULL, return incomplete
return VK_INCOMPLETE;
}
bool operator<(const VkExtensionProperties &a, const VkExtensionProperties &b)
{
// assume a given extension name is unique, ie. an implementation won't report the
// same extension with two different spec versions.
return strcmp(a.extensionName, b.extensionName) < 0;
}
// This list must be kept sorted according to the above sort operator!
static const VkExtensionProperties supportedExtensions[] = {
// this extension is 'free' - it just marks SPIR-V extension availability
{
VK_AMD_GCN_SHADER_EXTENSION_NAME, VK_AMD_GCN_SHADER_SPEC_VERSION,
},
// this extension is 'free' - it just marks SPIR-V extension availability
{
VK_AMD_GPU_SHADER_HALF_FLOAT_EXTENSION_NAME, VK_AMD_GPU_SHADER_HALF_FLOAT_SPEC_VERSION,
},
{
VK_AMD_NEGATIVE_VIEWPORT_HEIGHT_EXTENSION_NAME, VK_AMD_NEGATIVE_VIEWPORT_HEIGHT_SPEC_VERSION,
},
// this extension is 'free' - it just marks SPIR-V extension availability
{
VK_AMD_SHADER_BALLOT_EXTENSION_NAME, VK_AMD_SHADER_BALLOT_SPEC_VERSION,
},
// this extension is 'free' - it just marks SPIR-V extension availability
{
VK_AMD_SHADER_EXPLICIT_VERTEX_PARAMETER_EXTENSION_NAME,
VK_AMD_SHADER_EXPLICIT_VERTEX_PARAMETER_SPEC_VERSION,
},
// this extension is 'free' - it just marks SPIR-V extension availability
{
VK_AMD_SHADER_TRINARY_MINMAX_EXTENSION_NAME, VK_AMD_SHADER_TRINARY_MINMAX_SPEC_VERSION,
},
#ifdef VK_EXT_acquire_xlib_display
{
VK_EXT_ACQUIRE_XLIB_DISPLAY_EXTENSION_NAME, VK_EXT_ACQUIRE_XLIB_DISPLAY_SPEC_VERSION,
},
#endif
{
VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
},
{
VK_EXT_DIRECT_MODE_DISPLAY_EXTENSION_NAME, VK_EXT_DIRECT_MODE_DISPLAY_SPEC_VERSION,
},
{
VK_EXT_DISPLAY_CONTROL_EXTENSION_NAME, VK_EXT_DISPLAY_CONTROL_SPEC_VERSION,
},
{
VK_EXT_DISPLAY_SURFACE_COUNTER_EXTENSION_NAME, VK_EXT_DISPLAY_SURFACE_COUNTER_SPEC_VERSION,
},
// this extension is 'free' - it just marks SPIR-V extension availability
{
VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME, VK_EXT_SHADER_SUBGROUP_BALLOT_SPEC_VERSION,
},
// this extension is 'free' - it just marks SPIR-V extension availability
{
VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME, VK_EXT_SHADER_SUBGROUP_BALLOT_SPEC_VERSION,
},
{
VK_EXT_VALIDATION_FLAGS_EXTENSION_NAME, VK_EXT_VALIDATION_FLAGS_SPEC_VERSION,
},
#ifdef VK_KHR_android_surface
{
VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, VK_KHR_ANDROID_SURFACE_SPEC_VERSION,
},
#endif
#ifdef VK_KHR_display
{
VK_KHR_DISPLAY_EXTENSION_NAME, VK_KHR_DISPLAY_SPEC_VERSION,
},
#endif
#ifdef VK_KHR_display_swapchain
{
VK_KHR_DISPLAY_SWAPCHAIN_EXTENSION_NAME, VK_KHR_DISPLAY_SWAPCHAIN_SPEC_VERSION,
},
#endif
{
VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_SPEC_VERSION,
},
{
VK_KHR_MAINTENANCE1_EXTENSION_NAME, VK_KHR_MAINTENANCE1_SPEC_VERSION,
},
{
VK_KHR_SAMPLER_MIRROR_CLAMP_TO_EDGE_EXTENSION_NAME,
VK_KHR_SAMPLER_MIRROR_CLAMP_TO_EDGE_SPEC_VERSION,
},
// this extension is 'free' - it just marks SPIR-V extension availability
{
VK_KHR_SHADER_DRAW_PARAMETERS_EXTENSION_NAME, VK_KHR_SHADER_DRAW_PARAMETERS_SPEC_VERSION,
},
{
VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION,
},
{
VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_KHR_SWAPCHAIN_SPEC_VERSION,
},
#ifdef VK_KHR_win32_surface
{
VK_KHR_WIN32_SURFACE_EXTENSION_NAME, VK_KHR_WIN32_SURFACE_SPEC_VERSION,
},
#endif
#ifdef VK_KHR_xcb_surface
{
VK_KHR_XCB_SURFACE_EXTENSION_NAME, VK_KHR_XCB_SURFACE_SPEC_VERSION,
},
#endif
#ifdef VK_KHR_xlib_surface
{
VK_KHR_XLIB_SURFACE_EXTENSION_NAME, VK_KHR_XLIB_SURFACE_SPEC_VERSION,
},
#endif
{
VK_KHX_EXTERNAL_MEMORY_EXTENSION_NAME, VK_KHX_EXTERNAL_MEMORY_SPEC_VERSION,
},
{
VK_KHX_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME,
VK_KHX_EXTERNAL_MEMORY_CAPABILITIES_SPEC_VERSION,
},
{
VK_KHX_EXTERNAL_MEMORY_FD_EXTENSION_NAME, VK_KHX_EXTERNAL_MEMORY_FD_SPEC_VERSION,
},
#ifdef VK_KHX_external_memory_win32
{
VK_KHX_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME, VK_KHX_EXTERNAL_MEMORY_WIN32_SPEC_VERSION,
},
#endif
{
VK_KHX_EXTERNAL_SEMAPHORE_EXTENSION_NAME, VK_KHX_EXTERNAL_SEMAPHORE_SPEC_VERSION,
},
{
VK_KHX_EXTERNAL_SEMAPHORE_CAPABILITIES_EXTENSION_NAME,
VK_KHX_EXTERNAL_SEMAPHORE_CAPABILITIES_SPEC_VERSION,
},
{
VK_KHX_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, VK_KHX_EXTERNAL_SEMAPHORE_FD_SPEC_VERSION,
},
#ifdef VK_KHX_external_semaphore_win32
{
VK_KHX_EXTERNAL_SEMAPHORE_WIN32_EXTENSION_NAME, VK_KHX_EXTERNAL_SEMAPHORE_WIN32_SPEC_VERSION,
},
#endif
#ifdef VK_KHX_win32_keyed_mutex
{
VK_KHX_WIN32_KEYED_MUTEX_EXTENSION_NAME, VK_KHX_WIN32_KEYED_MUTEX_SPEC_VERSION,
},
#endif
{
VK_NV_DEDICATED_ALLOCATION_EXTENSION_NAME, VK_NV_DEDICATED_ALLOCATION_SPEC_VERSION,
},
{
VK_NV_EXTERNAL_MEMORY_EXTENSION_NAME, VK_NV_EXTERNAL_MEMORY_SPEC_VERSION,
},
{
VK_NV_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME,
VK_NV_EXTERNAL_MEMORY_CAPABILITIES_SPEC_VERSION,
},
#ifdef VK_NV_external_memory_win32
{
VK_NV_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME, VK_NV_EXTERNAL_MEMORY_WIN32_SPEC_VERSION,
},
#endif
#ifdef VK_NV_win32_keyed_mutex
{
VK_NV_WIN32_KEYED_MUTEX_EXTENSION_NAME, VK_NV_WIN32_KEYED_MUTEX_SPEC_VERSION,
},
#endif
};
static void ValidateSupportedExtensionList()
{
// this should be a unit test
#if DISABLED(RDOC_RELEASE)
std::vector<VkExtensionProperties> unsorted;
unsorted.insert(unsorted.begin(), &supportedExtensions[0],
&supportedExtensions[ARRAY_COUNT(supportedExtensions)]);
std::vector<VkExtensionProperties> sorted = unsorted;
std::sort(sorted.begin(), sorted.end());
for(size_t i = 0; i < unsorted.size(); i++)
if(strcmp(unsorted[i].extensionName, sorted[i].extensionName))
RDCFATAL("supportedExtensions list is not sorted");
#endif
}
// this is the list of extensions we provide - regardless of whether the ICD supports them
static const VkExtensionProperties renderdocProvidedExtensions[] = {
{VK_EXT_DEBUG_MARKER_EXTENSION_NAME, VK_EXT_DEBUG_MARKER_SPEC_VERSION},
};
bool WrappedVulkan::IsSupportedExtension(const char *extName)
{
for(size_t i = 0; i < ARRAY_COUNT(supportedExtensions); i++)
if(!strcmp(supportedExtensions[i].extensionName, extName))
return true;
return false;
}
VkResult WrappedVulkan::FilterDeviceExtensionProperties(VkPhysicalDevice physDev,
uint32_t *pPropertyCount,
VkExtensionProperties *pProperties)
{
VkResult vkr;
// first fetch the list of extensions ourselves
uint32_t numExts;
vkr = ObjDisp(physDev)->EnumerateDeviceExtensionProperties(Unwrap(physDev), NULL, &numExts, NULL);
if(vkr != VK_SUCCESS)
return vkr;
vector<VkExtensionProperties> exts(numExts);
vkr = ObjDisp(physDev)->EnumerateDeviceExtensionProperties(Unwrap(physDev), NULL, &numExts,
&exts[0]);
if(vkr != VK_SUCCESS)
return vkr;
// filter the list of extensions to only the ones we support.
// sort the reported extensions
std::sort(exts.begin(), exts.end());
std::vector<VkExtensionProperties> filtered;
filtered.reserve(exts.size());
ValidateSupportedExtensionList();
// now we can step through both lists with two pointers,
// instead of doing an O(N*M) lookup searching through each
// supported extension for each reported extension.
size_t i = 0;
for(auto it = exts.begin(); it != exts.end() && i < ARRAY_COUNT(supportedExtensions);)
{
int nameCompare = strcmp(it->extensionName, supportedExtensions[i].extensionName);
// if neither is less than the other, the extensions are equal
if(nameCompare == 0)
{
// warn on spec version mismatch, but allow it.
if(supportedExtensions[i].specVersion != it->specVersion)
RDCWARN(
"Spec versions of %s are different between supported extension (%d) and reported (%d)!",
it->extensionName, supportedExtensions[i].specVersion, it->specVersion);
filtered.push_back(*it);
++it;
++i;
}
else if(nameCompare < 0)
{
// reported extension was less. It's not supported - skip past it and continue
++it;
}
else if(nameCompare > 0)
{
// supported extension was less. Check the next supported extension
++i;
}
}
// now we can add extensions that we provide ourselves (note this isn't sorted, but we
// don't have to sort the results, the sorting was just so we could filter optimally).
filtered.insert(filtered.end(), &renderdocProvidedExtensions[0],
&renderdocProvidedExtensions[0] + ARRAY_COUNT(renderdocProvidedExtensions));
return FillPropertyCountAndList(&filtered[0], (uint32_t)filtered.size(), pPropertyCount,
pProperties);
}
VkResult WrappedVulkan::GetProvidedExtensionProperties(uint32_t *pPropertyCount,
VkExtensionProperties *pProperties)
{
return FillPropertyCountAndList(renderdocProvidedExtensions,
(uint32_t)ARRAY_COUNT(renderdocProvidedExtensions),
pPropertyCount, pProperties);
}
void WrappedVulkan::Serialise_CaptureScope(uint64_t offset)
{
uint32_t FrameNumber = m_FrameCounter;
// must use main serialiser here to match resource manager below
GetMainSerialiser()->Serialise("FrameNumber", FrameNumber);
if(m_State >= WRITING)
{
GetResourceManager()->Serialise_InitialContentsNeeded();
}
else
{
m_FrameRecord.frameInfo.fileOffset = offset;
m_FrameRecord.frameInfo.firstEvent = 1; // m_pImmediateContext->GetEventID();
m_FrameRecord.frameInfo.frameNumber = FrameNumber;
RDCEraseEl(m_FrameRecord.frameInfo.stats);
GetResourceManager()->CreateInitialContents();
}
}
void WrappedVulkan::EndCaptureFrame(VkImage presentImage)
{
// must use main serialiser here to match resource manager
Serialiser *localSerialiser = GetMainSerialiser();
SCOPED_SERIALISE_CONTEXT(CONTEXT_CAPTURE_FOOTER);
SERIALISE_ELEMENT(ResourceId, bbid, GetResID(presentImage));
bool HasCallstack = RenderDoc::Inst().GetCaptureOptions().CaptureCallstacks != 0;
localSerialiser->Serialise("HasCallstack", HasCallstack);
if(HasCallstack)
{
Callstack::Stackwalk *call = Callstack::Collect();
RDCASSERT(call->NumLevels() < 0xff);
uint64_t numLevels = (uint64_t)call->NumLevels();
uint64_t *stack = (uint64_t *)call->GetAddrs();