forked from baldurk/renderdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_manager.h
More file actions
1420 lines (1130 loc) · 42.9 KB
/
resource_manager.h
File metadata and controls
1420 lines (1130 loc) · 42.9 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.
******************************************************************************/
#pragma once
#include <map>
#include <set>
#include "api/replay/renderdoc_replay.h"
#include "common/threading.h"
#include "core/core.h"
#include "os/os_specific.h"
#include "serialise/serialiser.h"
using std::set;
using std::map;
// in what way (read, write, etc) was a resource referenced in a frame -
// used to determine if initial contents are needed and to what degree
enum FrameRefType
{
eFrameRef_Unknown, // for the initial start of frame pipeline state - can't be marked as
// written/read yet until first action.
// Inputs
eFrameRef_Read,
eFrameRef_Write,
// States
eFrameRef_ReadOnly,
eFrameRef_ReadAndWrite,
eFrameRef_ReadBeforeWrite,
};
// verbose prints with IDs of each dirty resource and whether it was prepared,
// and whether it was serialised.
#define VERBOSE_DIRTY_RESOURCES OPTION_OFF
namespace ResourceIDGen
{
ResourceId GetNewUniqueID();
void SetReplayResourceIDs();
};
struct ResourceRecord;
class ResourceRecordHandler
{
public:
virtual void MarkDirtyResource(ResourceId id) = 0;
virtual void MarkCleanResource(ResourceId id) = 0;
virtual void MarkPendingDirty(ResourceId id) = 0;
virtual void RemoveResourceRecord(ResourceId id) = 0;
virtual void MarkResourceFrameReferenced(ResourceId id, FrameRefType refType) = 0;
virtual void DestroyResourceRecord(ResourceRecord *record) = 0;
};
// This is a generic resource record, that APIs can inherit from and use.
// A resource is an API object that gets tracked on its own, has dependencies on other resources
// and has its own stream of chunks.
//
// This is used to track the necessary resources for a frame, and include only those required
// for the captured frame in its log. It also handles anything resource-specific such as
// shadow CPU copies of data.
struct ResourceRecord
{
ResourceRecord(ResourceId id, bool lock)
: RefCount(1),
ResID(id),
UpdateCount(0),
DataInSerialiser(false),
DataPtr(NULL),
DataOffset(0),
Length(0),
DataWritten(false),
SpecialResource(false)
{
m_ChunkLock = NULL;
if(lock)
m_ChunkLock = new Threading::CriticalSection();
}
~ResourceRecord() { SAFE_DELETE(m_ChunkLock); }
void AddParent(ResourceRecord *r)
{
if(Parents.find(r) == Parents.end())
{
r->AddRef();
Parents.insert(r);
}
}
void MarkParentsDirty(ResourceRecordHandler *mgr)
{
for(auto it = Parents.begin(); it != Parents.end(); ++it)
mgr->MarkDirtyResource((*it)->GetResourceID());
}
void MarkParentsReferenced(ResourceRecordHandler *mgr, FrameRefType refType)
{
for(auto it = Parents.begin(); it != Parents.end(); ++it)
mgr->MarkResourceFrameReferenced((*it)->GetResourceID(), refType);
}
void FreeParents(ResourceRecordHandler *mgr)
{
for(auto it = Parents.begin(); it != Parents.end(); ++it)
(*it)->Delete(mgr);
Parents.clear();
}
void MarkDataUnwritten() { DataWritten = false; }
void Insert(map<int32_t, Chunk *> &recordlist)
{
bool dataWritten = DataWritten;
DataWritten = true;
for(auto it = Parents.begin(); it != Parents.end(); ++it)
{
if(!(*it)->DataWritten)
{
(*it)->Insert(recordlist);
}
}
if(!dataWritten)
recordlist.insert(m_Chunks.begin(), m_Chunks.end());
}
void AddRef() { Atomic::Inc32(&RefCount); }
int GetRefCount() const { return RefCount; }
void Delete(ResourceRecordHandler *mgr);
ResourceId GetResourceID() const { return ResID; }
void RemoveChunk(Chunk *chunk)
{
LockChunks();
for(auto it = m_Chunks.begin(); it != m_Chunks.end(); ++it)
{
if(it->second == chunk)
{
m_Chunks.erase(it);
break;
}
}
UnlockChunks();
}
void AddChunk(Chunk *chunk, int32_t ID = 0)
{
LockChunks();
if(ID == 0)
ID = GetID();
m_Chunks[ID] = chunk;
UnlockChunks();
}
void LockChunks()
{
if(m_ChunkLock)
m_ChunkLock->Lock();
}
void UnlockChunks()
{
if(m_ChunkLock)
m_ChunkLock->Unlock();
}
bool HasChunks() const { return !m_Chunks.empty(); }
size_t NumChunks() const { return m_Chunks.size(); }
void SwapChunks(ResourceRecord *other)
{
LockChunks();
other->LockChunks();
m_Chunks.swap(other->m_Chunks);
m_FrameRefs.swap(other->m_FrameRefs);
other->UnlockChunks();
UnlockChunks();
}
void AppendFrom(ResourceRecord *other)
{
LockChunks();
other->LockChunks();
for(auto it = other->m_Chunks.begin(); it != other->m_Chunks.end(); ++it)
AddChunk(it->second->Duplicate());
for(auto it = other->Parents.begin(); it != other->Parents.end(); ++it)
AddParent(*it);
other->UnlockChunks();
UnlockChunks();
}
void DeleteChunks()
{
LockChunks();
for(auto it = m_Chunks.begin(); it != m_Chunks.end(); ++it)
SAFE_DELETE(it->second);
m_Chunks.clear();
UnlockChunks();
}
Chunk *GetLastChunk() const
{
RDCASSERT(HasChunks());
return m_Chunks.rbegin()->second;
}
int32_t GetLastChunkID() const
{
RDCASSERT(HasChunks());
return m_Chunks.rbegin()->first;
}
void PopChunk() { m_Chunks.erase(m_Chunks.rbegin()->first); }
byte *GetDataPtr() { return DataPtr + DataOffset; }
bool HasDataPtr() { return DataPtr != NULL; }
void SetDataOffset(uint64_t offs) { DataOffset = offs; }
void SetDataPtr(byte *ptr) { DataPtr = ptr; }
bool MarkResourceFrameReferenced(ResourceId id, FrameRefType refType);
void AddResourceReferences(ResourceRecordHandler *mgr);
void AddReferencedIDs(std::set<ResourceId> &ids)
{
for(auto it = m_FrameRefs.begin(); it != m_FrameRefs.end(); ++it)
ids.insert(it->first);
}
uint64_t Length;
int UpdateCount;
bool DataInSerialiser;
bool SpecialResource; // like the swap chain back buffers
bool DataWritten;
protected:
volatile int32_t RefCount;
byte *DataPtr;
uint64_t DataOffset;
ResourceId ResID;
std::set<ResourceRecord *> Parents;
int32_t GetID()
{
static volatile int32_t globalIDCounter = 10;
return Atomic::Inc32(&globalIDCounter);
}
std::map<int32_t, Chunk *> m_Chunks;
Threading::CriticalSection *m_ChunkLock;
map<ResourceId, FrameRefType> m_FrameRefs;
};
// the resource manager is a utility class that's not required but is likely wanted by any API
// implementation.
// It keeps track of resource records, which resources are alive and allows you to query for them by
// ID. It tracks
// which resources are marked as dirty (needing their initial contents fetched before capture).
//
// For APIs that wrap their resources it provides tracking for that.
//
// In the replay application it will also track which 'live' resources are representing which
// 'original'
// resources from the application when it was captured.
template <typename WrappedResourceType, typename RealResourceType, typename RecordType>
class ResourceManager : public ResourceRecordHandler
{
public:
ResourceManager(LogState state, Serialiser *ser);
virtual ~ResourceManager();
void Shutdown();
struct InitialContentData
{
InitialContentData(WrappedResourceType r, uint32_t n, byte *b) : resource(r), num(n), blob(b) {}
InitialContentData()
: resource((WrappedResourceType)RecordType::NullResource), num(0), blob(NULL)
{
}
WrappedResourceType resource;
uint32_t num;
byte *blob;
};
bool IsWriting() { return m_State >= WRITING; }
bool IsReading() { return m_State < WRITING; }
///////////////////////////////////////////
// Capture-side methods
// while capturing, resource records containing chunk streams and metadata for resources
RecordType *GetResourceRecord(ResourceId id);
bool HasResourceRecord(ResourceId id);
RecordType *AddResourceRecord(ResourceId id);
inline void RemoveResourceRecord(ResourceId id);
void DestroyResourceRecord(ResourceRecord *record);
// while capturing or replaying, resources and their live IDs
void AddCurrentResource(ResourceId id, WrappedResourceType res);
bool HasCurrentResource(ResourceId id);
WrappedResourceType GetCurrentResource(ResourceId id);
void ReleaseCurrentResource(ResourceId id);
void MarkInFrame(bool inFrame) { m_InFrame = inFrame; }
void ReleaseInFrameResources();
// insert the chunks for the resources referenced in the frame
void InsertReferencedChunks(Serialiser *fileSer);
// mark resource records as unwritten, ready to be written to a new logfile.
void MarkUnwrittenResources();
// clear the list of frame-referenced resources - e.g. if you're about to recapture a frame
void ClearReferencedResources();
// indicates this resource could have been modified by the GPU,
// so it's now suspect and the data we have on it might well be out of date
// and to be correct its contents should be serialised out at the start
// of the frame.
inline void MarkDirtyResource(ResourceId res);
// for use when we might be mid-capture, this will get flushed to dirty state before the
// next frame but is safe to use mid-capture
void MarkPendingDirty(ResourceId res);
void FlushPendingDirty();
// this can be used when the resource is cleared or similar and it's in a known state
void MarkCleanResource(ResourceId res);
// returns if the resource has been marked as dirty
bool IsResourceDirty(ResourceId res);
// call callbacks to prepare initial contents for dirty resources
void PrepareInitialContents();
InitialContentData GetInitialContents(ResourceId id);
void SetInitialContents(ResourceId id, InitialContentData contents);
void SetInitialChunk(ResourceId id, Chunk *chunk);
// generate chunks for initial contents and insert.
void InsertInitialContentsChunks(Serialiser *fileSer);
// Serialise out which resources need initial contents, along with whether their
// initial contents are in the serialised stream (e.g. RTs might still want to be
// cleared on frame init).
void Serialise_InitialContentsNeeded();
// handle marking a resource referenced for read or write and storing RAW access etc.
static bool MarkReferenced(map<ResourceId, FrameRefType> &refs, ResourceId id,
FrameRefType refType);
// mark resource referenced somewhere in the main frame-affecting calls.
// That means this resource should be included in the final serialise out
inline void MarkResourceFrameReferenced(ResourceId id, FrameRefType refType);
// check if this resource was read before being written to - can be used to detect if
// initial states are necessary
bool ReadBeforeWrite(ResourceId id);
///////////////////////////////////////////
// Replay-side methods
// Live resources to replace serialised IDs
void AddLiveResource(ResourceId origid, WrappedResourceType livePtr);
bool HasLiveResource(ResourceId origid);
WrappedResourceType GetLiveResource(ResourceId origid);
void EraseLiveResource(ResourceId origid);
// when asked for a given id, return the resource for a replacement id
void ReplaceResource(ResourceId from, ResourceId to);
bool HasReplacement(ResourceId from);
void RemoveReplacement(ResourceId id);
// fetch original ID for a real ID or vice-versa.
ResourceId GetOriginalID(ResourceId id);
ResourceId GetLiveID(ResourceId id);
// Serialise in which resources need initial contents and set them up.
void CreateInitialContents();
// Free any initial contents that are prepared (for after capture is complete)
void FreeInitialContents();
// Apply the initial contents for the resources that need them, used at the start of a frame
void ApplyInitialContents();
// Resource wrapping, allows for querying and adding/removing of wrapper layers around resources
bool AddWrapper(WrappedResourceType wrap, RealResourceType real);
bool HasWrapper(RealResourceType real);
WrappedResourceType GetWrapper(RealResourceType real);
void RemoveWrapper(RealResourceType real);
protected:
// 'interface' to implement by derived classes
virtual bool SerialisableResource(ResourceId id, RecordType *record) = 0;
virtual ResourceId GetID(WrappedResourceType res) = 0;
virtual bool ResourceTypeRelease(WrappedResourceType res) = 0;
virtual bool Force_InitialState(WrappedResourceType res, bool prepare) = 0;
virtual bool AllowDeletedResource_InitialState() { return false; }
virtual bool Need_InitialStateChunk(WrappedResourceType res) = 0;
virtual bool Prepare_InitialState(WrappedResourceType res) = 0;
virtual bool Serialise_InitialState(ResourceId id, WrappedResourceType res) = 0;
virtual void Create_InitialState(ResourceId id, WrappedResourceType live, bool hasData) = 0;
virtual void Apply_InitialState(WrappedResourceType live, InitialContentData initial) = 0;
LogState m_State;
Serialiser *m_pSerialiser;
Serialiser *GetSerialiser() { return m_pSerialiser; }
bool m_InFrame;
// very coarse lock, protects EVERYTHING. This could certainly be improved and it may be a
// bottleneck
// for performance. Given that the main use cases are write-rarely read-often the lock should be
// optimised
// for that as we only want to make sure we're not modifying the objects together, by far the most
// common
// operation is looking up data.
Threading::CriticalSection m_Lock;
// easy optimisation win - don't use maps everywhere. It's convenient but not optimal, and
// profiling will
// likely prove that some or all of these could be a problem
// used during capture - map from real resource to its wrapper (other way can be done just with an
// Unwrap)
map<RealResourceType, WrappedResourceType> m_WrapperMap;
// used during capture - holds resources referenced in current frame (and how they're referenced)
map<ResourceId, FrameRefType> m_FrameReferencedResources;
// used during capture - holds resources marked as dirty, needing initial contents
set<ResourceId> m_DirtyResources;
set<ResourceId> m_PendingDirtyResources;
// used during capture or replay - holds initial contents
map<ResourceId, InitialContentData> m_InitialContents;
// on capture, if a chunk was prepared in Prepare_InitialContents and added, don't re-serialise.
// Some initial contents may not need the delayed readback.
map<ResourceId, Chunk *> m_InitialChunks;
// used during capture or replay - map of resources currently alive with their real IDs, used in
// capture and replay.
map<ResourceId, WrappedResourceType> m_CurrentResourceMap;
// used during replay - maps back and forth from original id to live id and vice-versa
map<ResourceId, ResourceId> m_OriginalIDs, m_LiveIDs;
// used during replay - holds resources allocated and the original id that they represent
// for a) in-frame creations and b) pre-frame creations respectively.
map<ResourceId, WrappedResourceType> m_InframeResourceMap, m_LiveResourceMap;
// used during capture - holds resource records by id.
map<ResourceId, RecordType *> m_ResourceRecords;
// used during replay - holds current resource replacements
map<ResourceId, ResourceId> m_Replacements;
};
template <typename WrappedResourceType, typename RealResourceType, typename RecordType>
ResourceManager<WrappedResourceType, RealResourceType, RecordType>::ResourceManager(LogState state,
Serialiser *ser)
{
if(RenderDoc::Inst().GetCrashHandler())
RenderDoc::Inst().GetCrashHandler()->RegisterMemoryRegion(this, sizeof(ResourceManager));
m_State = state;
m_pSerialiser = ser;
m_InFrame = false;
}
template <typename WrappedResourceType, typename RealResourceType, typename RecordType>
void ResourceManager<WrappedResourceType, RealResourceType, RecordType>::Shutdown()
{
while(!m_LiveResourceMap.empty())
{
auto it = m_LiveResourceMap.begin();
ResourceId id = it->first;
ResourceTypeRelease(it->second);
auto removeit = m_LiveResourceMap.find(id);
if(removeit != m_LiveResourceMap.end())
m_LiveResourceMap.erase(removeit);
}
while(!m_InframeResourceMap.empty())
{
auto it = m_InframeResourceMap.begin();
ResourceId id = it->first;
ResourceTypeRelease(it->second);
auto removeit = m_InframeResourceMap.find(id);
if(removeit != m_InframeResourceMap.end())
m_InframeResourceMap.erase(removeit);
}
FreeInitialContents();
RDCASSERT(m_ResourceRecords.empty());
}
template <typename WrappedResourceType, typename RealResourceType, typename RecordType>
ResourceManager<WrappedResourceType, RealResourceType, RecordType>::~ResourceManager()
{
RDCASSERT(m_LiveResourceMap.empty());
RDCASSERT(m_InframeResourceMap.empty());
RDCASSERT(m_InitialContents.empty());
RDCASSERT(m_ResourceRecords.empty());
if(RenderDoc::Inst().GetCrashHandler())
RenderDoc::Inst().GetCrashHandler()->UnregisterMemoryRegion(this);
}
template <typename WrappedResourceType, typename RealResourceType, typename RecordType>
bool ResourceManager<WrappedResourceType, RealResourceType, RecordType>::MarkReferenced(
map<ResourceId, FrameRefType> &refs, ResourceId id, FrameRefType refType)
{
if(refs.find(id) == refs.end())
{
if(refType == eFrameRef_Read)
refs[id] = eFrameRef_ReadOnly;
else if(refType == eFrameRef_Write)
refs[id] = eFrameRef_ReadAndWrite;
else // unknown or existing state
refs[id] = refType;
return true;
}
else
{
if(refType == eFrameRef_Unknown)
{
// nothing
}
else if(refType == eFrameRef_ReadBeforeWrite)
{
// special case, explicitly set to ReadBeforeWrite for when
// we know that this use will likely be a partial-write
refs[id] = eFrameRef_ReadBeforeWrite;
}
else if(refs[id] == eFrameRef_Unknown)
{
if(refType == eFrameRef_Read || refType == eFrameRef_ReadOnly)
refs[id] = eFrameRef_ReadOnly;
else
refs[id] = eFrameRef_ReadAndWrite;
}
else if(refs[id] == eFrameRef_ReadOnly && refType == eFrameRef_Write)
{
refs[id] = eFrameRef_ReadBeforeWrite;
}
}
return false;
}
template <typename WrappedResourceType, typename RealResourceType, typename RecordType>
void ResourceManager<WrappedResourceType, RealResourceType, RecordType>::MarkResourceFrameReferenced(
ResourceId id, FrameRefType refType)
{
SCOPED_LOCK(m_Lock);
if(id == ResourceId())
return;
bool newRef = MarkReferenced(m_FrameReferencedResources, id, refType);
if(newRef)
{
RecordType *record = GetResourceRecord(id);
if(record)
record->AddRef();
}
}
template <typename WrappedResourceType, typename RealResourceType, typename RecordType>
bool ResourceManager<WrappedResourceType, RealResourceType, RecordType>::ReadBeforeWrite(ResourceId id)
{
if(m_FrameReferencedResources.find(id) != m_FrameReferencedResources.end())
return m_FrameReferencedResources[id] == eFrameRef_ReadBeforeWrite ||
m_FrameReferencedResources[id] == eFrameRef_ReadOnly;
return false;
}
template <typename WrappedResourceType, typename RealResourceType, typename RecordType>
void ResourceManager<WrappedResourceType, RealResourceType, RecordType>::MarkDirtyResource(ResourceId res)
{
SCOPED_LOCK(m_Lock);
if(res == ResourceId())
return;
m_DirtyResources.insert(res);
}
template <typename WrappedResourceType, typename RealResourceType, typename RecordType>
void ResourceManager<WrappedResourceType, RealResourceType, RecordType>::MarkPendingDirty(ResourceId res)
{
SCOPED_LOCK(m_Lock);
if(res == ResourceId())
return;
m_PendingDirtyResources.insert(res);
}
template <typename WrappedResourceType, typename RealResourceType, typename RecordType>
void ResourceManager<WrappedResourceType, RealResourceType, RecordType>::FlushPendingDirty()
{
SCOPED_LOCK(m_Lock);
m_DirtyResources.insert(m_PendingDirtyResources.begin(), m_PendingDirtyResources.end());
m_PendingDirtyResources.clear();
}
template <typename WrappedResourceType, typename RealResourceType, typename RecordType>
bool ResourceManager<WrappedResourceType, RealResourceType, RecordType>::IsResourceDirty(ResourceId res)
{
SCOPED_LOCK(m_Lock);
if(res == ResourceId())
return false;
return m_DirtyResources.find(res) != m_DirtyResources.end();
}
template <typename WrappedResourceType, typename RealResourceType, typename RecordType>
void ResourceManager<WrappedResourceType, RealResourceType, RecordType>::MarkCleanResource(ResourceId res)
{
SCOPED_LOCK(m_Lock);
if(res == ResourceId())
return;
if(IsResourceDirty(res))
{
m_DirtyResources.erase(res);
}
}
template <typename WrappedResourceType, typename RealResourceType, typename RecordType>
void ResourceManager<WrappedResourceType, RealResourceType, RecordType>::SetInitialContents(
ResourceId id, InitialContentData contents)
{
SCOPED_LOCK(m_Lock);
RDCASSERT(id != ResourceId());
auto it = m_InitialContents.find(id);
if(it != m_InitialContents.end())
{
ResourceTypeRelease(it->second.resource);
Serialiser::FreeAlignedBuffer(it->second.blob);
m_InitialContents.erase(it);
}
m_InitialContents[id] = contents;
}
template <typename WrappedResourceType, typename RealResourceType, typename RecordType>
void ResourceManager<WrappedResourceType, RealResourceType, RecordType>::SetInitialChunk(ResourceId id,
Chunk *chunk)
{
SCOPED_LOCK(m_Lock);
RDCASSERT(id != ResourceId());
auto it = m_InitialChunks.find(id);
RDCASSERT(chunk->GetChunkType() == INITIAL_CONTENTS);
if(it != m_InitialChunks.end())
{
RDCERR("Initial chunk set for ID %llu twice", id);
delete chunk;
return;
}
m_InitialChunks[id] = chunk;
}
template <typename WrappedResourceType, typename RealResourceType, typename RecordType>
typename ResourceManager<WrappedResourceType, RealResourceType, RecordType>::InitialContentData ResourceManager<
WrappedResourceType, RealResourceType, RecordType>::GetInitialContents(ResourceId id)
{
SCOPED_LOCK(m_Lock);
if(id == ResourceId())
return InitialContentData();
if(m_InitialContents.find(id) != m_InitialContents.end())
return m_InitialContents[id];
return InitialContentData();
}
template <typename WrappedResourceType, typename RealResourceType, typename RecordType>
void ResourceManager<WrappedResourceType, RealResourceType, RecordType>::Serialise_InitialContentsNeeded()
{
SCOPED_LOCK(m_Lock);
struct WrittenRecord
{
ResourceId id;
bool written;
};
vector<WrittenRecord> written;
// reasonable estimate, and these records are small
written.reserve(m_FrameReferencedResources.size());
for(auto it = m_FrameReferencedResources.begin(); it != m_FrameReferencedResources.end(); ++it)
{
RecordType *record = GetResourceRecord(it->first);
if(it->second != eFrameRef_ReadOnly && it->second != eFrameRef_Unknown)
{
WrittenRecord wr = {it->first, record ? record->DataInSerialiser : true};
written.push_back(wr);
}
}
for(auto it = m_DirtyResources.begin(); it != m_DirtyResources.end(); ++it)
{
ResourceId id = *it;
auto ref = m_FrameReferencedResources.find(id);
if(ref == m_FrameReferencedResources.end() || ref->second == eFrameRef_ReadOnly)
{
WrittenRecord wr = {id, true};
written.push_back(wr);
}
}
uint32_t numWritten = (uint32_t)written.size();
m_pSerialiser->Serialise("NumWrittenResources", numWritten);
for(auto it = written.begin(); it != written.end(); ++it)
{
m_pSerialiser->Serialise("id", it->id);
m_pSerialiser->Serialise("WrittenData", it->written);
}
}
template <typename WrappedResourceType, typename RealResourceType, typename RecordType>
void ResourceManager<WrappedResourceType, RealResourceType, RecordType>::FreeInitialContents()
{
while(!m_InitialContents.empty())
{
auto it = m_InitialContents.begin();
ResourceTypeRelease(it->second.resource);
Serialiser::FreeAlignedBuffer(it->second.blob);
if(!m_InitialContents.empty())
m_InitialContents.erase(m_InitialContents.begin());
}
}
template <typename WrappedResourceType, typename RealResourceType, typename RecordType>
void ResourceManager<WrappedResourceType, RealResourceType, RecordType>::CreateInitialContents()
{
set<ResourceId> neededInitials;
uint32_t NumWrittenResources = 0;
m_pSerialiser->Serialise("NumWrittenResources", NumWrittenResources);
for(uint32_t i = 0; i < NumWrittenResources; i++)
{
ResourceId id = ResourceId();
bool WrittenData = false;
m_pSerialiser->Serialise("id", id);
m_pSerialiser->Serialise("WrittenData", WrittenData);
neededInitials.insert(id);
if(HasLiveResource(id) && m_InitialContents.find(id) == m_InitialContents.end())
Create_InitialState(id, GetLiveResource(id), WrittenData);
}
for(auto it = m_InitialContents.begin(); it != m_InitialContents.end();)
{
ResourceId id = it->first;
if(neededInitials.find(id) == neededInitials.end())
{
ResourceTypeRelease(it->second.resource);
Serialiser::FreeAlignedBuffer(it->second.blob);
++it;
m_InitialContents.erase(id);
}
else
{
++it;
}
}
}
template <typename WrappedResourceType, typename RealResourceType, typename RecordType>
void ResourceManager<WrappedResourceType, RealResourceType, RecordType>::ApplyInitialContents()
{
RDCDEBUG("Applying initial contents");
uint32_t numContents = 0;
for(auto it = m_InitialContents.begin(); it != m_InitialContents.end(); ++it)
{
ResourceId id = it->first;
if(HasLiveResource(id))
{
WrappedResourceType live = GetLiveResource(id);
numContents++;
Apply_InitialState(live, it->second);
}
}
RDCDEBUG("Applied %d", numContents);
}
template <typename WrappedResourceType, typename RealResourceType, typename RecordType>
void ResourceManager<WrappedResourceType, RealResourceType, RecordType>::MarkUnwrittenResources()
{
SCOPED_LOCK(m_Lock);
for(auto it = m_ResourceRecords.begin(); it != m_ResourceRecords.end(); ++it)
{
it->second->MarkDataUnwritten();
}
}
template <typename WrappedResourceType, typename RealResourceType, typename RecordType>
void ResourceManager<WrappedResourceType, RealResourceType, RecordType>::InsertReferencedChunks(
Serialiser *fileSer)
{
map<int32_t, Chunk *> sortedChunks;
SCOPED_LOCK(m_Lock);
RDCDEBUG("%u frame resource records", (uint32_t)m_FrameReferencedResources.size());
if(RenderDoc::Inst().GetCaptureOptions().RefAllResources)
{
for(auto it = m_ResourceRecords.begin(); it != m_ResourceRecords.end(); ++it)
{
if(!SerialisableResource(it->first, it->second))
continue;
it->second->Insert(sortedChunks);
}
}
else
{
for(auto it = m_FrameReferencedResources.begin(); it != m_FrameReferencedResources.end(); ++it)
{
RecordType *record = GetResourceRecord(it->first);
if(record)
record->Insert(sortedChunks);
}
}
RDCDEBUG("%u frame resource chunks", (uint32_t)sortedChunks.size());
for(auto it = sortedChunks.begin(); it != sortedChunks.end(); it++)
{
fileSer->Insert(it->second);
}
RDCDEBUG("inserted to serialiser");
}
template <typename WrappedResourceType, typename RealResourceType, typename RecordType>
void ResourceManager<WrappedResourceType, RealResourceType, RecordType>::PrepareInitialContents()
{
SCOPED_LOCK(m_Lock);
RDCDEBUG("Preparing up to %u potentially dirty resources", (uint32_t)m_DirtyResources.size());
uint32_t prepared = 0;
for(auto it = m_DirtyResources.begin(); it != m_DirtyResources.end(); ++it)
{
ResourceId id = *it;
if(!HasCurrentResource(id))
continue;
RecordType *record = GetResourceRecord(id);
WrappedResourceType res = GetCurrentResource(id);
if(record == NULL || record->SpecialResource)
continue;
prepared++;
#if ENABLED(VERBOSE_DIRTY_RESOURCES)
RDCDEBUG("Prepare Resource %llu", id);
#endif
Prepare_InitialState(res);
}
RDCDEBUG("Prepared %u dirty resources", prepared);
prepared = 0;
for(auto it = m_CurrentResourceMap.begin(); it != m_CurrentResourceMap.end(); ++it)
{
if(it->second == (WrappedResourceType)RecordType::NullResource)
continue;
if(Force_InitialState(it->second, true))
{
prepared++;
Prepare_InitialState(it->second);
}
}
RDCDEBUG("Force-prepared %u dirty resources", prepared);
}
template <typename WrappedResourceType, typename RealResourceType, typename RecordType>
void ResourceManager<WrappedResourceType, RealResourceType, RecordType>::InsertInitialContentsChunks(
Serialiser *fileSerialiser)
{
SCOPED_LOCK(m_Lock);
uint32_t dirty = 0;
uint32_t skipped = 0;
RDCDEBUG("Checking %u possibly dirty resources", (uint32_t)m_DirtyResources.size());
for(auto it = m_DirtyResources.begin(); it != m_DirtyResources.end(); ++it)
{
ResourceId id = *it;
if(m_FrameReferencedResources.find(id) == m_FrameReferencedResources.end() &&
!RenderDoc::Inst().GetCaptureOptions().RefAllResources)
{
#if ENABLED(VERBOSE_DIRTY_RESOURCES)
RDCDEBUG("Dirty tesource %llu is GPU dirty but not referenced - skipping", id);
#endif
skipped++;
continue;
}
WrappedResourceType res = (WrappedResourceType)RecordType::NullResource;
bool isAlive = HasCurrentResource(id);
if(!AllowDeletedResource_InitialState() && !isAlive)
{
#if ENABLED(VERBOSE_DIRTY_RESOURCES)
RDCDEBUG("Resource %llu no longer exists - skipping", id);
#endif
continue;
}
if(isAlive)
res = GetCurrentResource(id);
RecordType *record = GetResourceRecord(id);
if(record == NULL)
{
#if ENABLED(VERBOSE_DIRTY_RESOURCES)
RDCDEBUG("Resource %llu has no resource record - skipping", id);