Repository navigation
Expand file tree
/
Copy pathresource_manager.cpp
More file actions
239 lines (209 loc) · 7.43 KB
/
Copy pathresource_manager.cpp
File metadata and controls
239 lines (209 loc) · 7.43 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
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2015-2026 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 "resource_manager.h"
#include <algorithm>
namespace ResourceIDGen
{
static int64_t globalIDCounter = 1;
static ResourceId baseReplayID;
ResourceId GetNewUniqueID()
{
ResourceId ret;
ret.id = Atomic::Inc64(&globalIDCounter);
return ret;
}
bool IsReplayOnlyID(ResourceId id)
{
return baseReplayID < id || (id == ResourceId());
}
void SetReplayResourceIDs()
{
// separate replay-only IDs from captured IDs by adding a value when replaying.
// 1000000000000000000 capture-time IDs before we overlap replay IDs gives
// almost 32 years generating 100000 IDs per frame at 10000 FPS.
// only add this value once (since we're not |'ing on a bit)
if(globalIDCounter < 1000000000000000000LL)
globalIDCounter =
RDCMAX(int64_t(globalIDCounter), int64_t(globalIDCounter + 1000000000000000000LL));
baseReplayID = GetNewUniqueID();
}
};
INSTANTIATE_SERIALISE_TYPE(ResourceManagerInternal::WrittenRecord);
template <>
rdcstr DoStringise(const FrameRefType &el)
{
BEGIN_ENUM_STRINGISE(FrameRefType)
{
STRINGISE_ENUM_CLASS_NAMED(eFrameRef_None, "None");
STRINGISE_ENUM_CLASS_NAMED(eFrameRef_PartialWrite, "Partial Write");
STRINGISE_ENUM_CLASS_NAMED(eFrameRef_CompleteWrite, "Complete Write");
STRINGISE_ENUM_CLASS_NAMED(eFrameRef_Read, "Read");
STRINGISE_ENUM_CLASS_NAMED(eFrameRef_ReadBeforeWrite, "Read Before Write");
STRINGISE_ENUM_CLASS_NAMED(eFrameRef_CompleteWriteAndDiscard, "Complete Write and Discard");
STRINGISE_ENUM_CLASS_NAMED(eFrameRef_Unknown, "Unknown");
}
END_ENUM_STRINGISE()
}
FrameRefType ComposeFrameRefs(FrameRefType first, FrameRefType second)
{
switch(first)
{
case eFrameRef_None:
case eFrameRef_PartialWrite:
if(second == eFrameRef_None)
// A `None` reference after any other reference type does not change
// the first reference type
return first;
else if(first == eFrameRef_PartialWrite && second == eFrameRef_Read)
// a `Read` reference after a partial write means we need to store `WriteBeforeRead` instead
// of just `Read`.
return eFrameRef_WriteBeforeRead;
else
// Otherwise a `None` or `Write` reference before any non-`None` reference type
// does not change the reference type.
return second;
case eFrameRef_Read:
case eFrameRef_WriteBeforeRead:
if(IncludesWrite(second))
// `first` reads before `second` writes
return eFrameRef_ReadBeforeWrite;
else
return first;
case eFrameRef_CompleteWrite:
case eFrameRef_CompleteWriteAndDiscard:
case eFrameRef_ReadBeforeWrite:
// These reference types are both locked in, and cannot be affected by
// later references.
return first;
default: RDCERR("Unknown FrameRefType: %d", first); return eFrameRef_ReadBeforeWrite;
}
}
FrameRefType ComposeFrameRefsUnordered(FrameRefType first, FrameRefType second)
{
if((IncludesRead(first) && IncludesWrite(second)) || (IncludesRead(second) && IncludesWrite(first)))
{
// There is a way to order these references so that the resource is read
// and then written. Since read-before-write is the worst case in terms of
// reset requirements, we conservatively assume this is the case.
return eFrameRef_ReadBeforeWrite;
}
else
{
// first patch CompleteWriteAndDiscard to CompleteWrite so the values are as expected.
if(first == eFrameRef_CompleteWriteAndDiscard)
first = eFrameRef_CompleteWrite;
if(second == eFrameRef_CompleteWriteAndDiscard)
second = eFrameRef_CompleteWrite;
// Otherwise either:
// - first and second are each Read or None, or
// - first and second are each CompleteWrite, PartialWrite or None.
// In either case, Compose(first,second) = Compose(second,first) = max(first,second).
return RDCMAX(first, second);
}
}
FrameRefType ComposeFrameRefsDisjoint(FrameRefType x, FrameRefType y)
{
if(x == eFrameRef_ReadBeforeWrite || y == eFrameRef_ReadBeforeWrite)
{
// If any subresource is `ReadBeforeWrite`, then the whole resource is.
return eFrameRef_ReadBeforeWrite;
}
else
{
// first patch CompleteWriteAndDiscard to CompleteWrite so the values are as expected.
if(x == eFrameRef_CompleteWriteAndDiscard)
x = eFrameRef_CompleteWrite;
if(y == eFrameRef_CompleteWriteAndDiscard)
y = eFrameRef_CompleteWrite;
// For all other cases, just return the larger value.
return RDCMAX(x, y);
}
}
FrameRefType ComposeFrameRefsFirstKnown(FrameRefType first, FrameRefType second)
{
if(eFrameRef_Minimum <= first && first <= eFrameRef_Maximum)
return first;
else
return second;
}
FrameRefType KeepOldFrameRef(FrameRefType first, FrameRefType second)
{
return first;
}
bool IncludesRead(FrameRefType refType)
{
switch(refType)
{
case eFrameRef_Read:
case eFrameRef_WriteBeforeRead:
case eFrameRef_ReadBeforeWrite: return true;
default: return false;
}
}
bool IncludesWrite(FrameRefType refType)
{
switch(refType)
{
case eFrameRef_PartialWrite:
case eFrameRef_CompleteWrite:
case eFrameRef_CompleteWriteAndDiscard:
case eFrameRef_WriteBeforeRead:
case eFrameRef_ReadBeforeWrite: return true;
default: return false;
}
}
bool IsDirtyFrameRef(FrameRefType refType)
{
return (refType != eFrameRef_None && refType != eFrameRef_Read);
}
bool IsCompleteWriteFrameRef(FrameRefType refType)
{
return refType == eFrameRef_CompleteWrite || refType == eFrameRef_CompleteWriteAndDiscard;
}
void ResourceRecord::AddResourceReferences(ResourceRecordHandler *mgr)
{
for(auto it = m_FrameRefs.begin(); it != m_FrameRefs.end(); ++it)
{
mgr->MarkResourceFrameReferenced(it->first, it->second);
}
}
void ResourceRecord::Delete(ResourceRecordHandler *mgr)
{
int32_t ref = Atomic::Dec32(&RefCount);
RDCASSERT(ref >= 0);
if(ref <= 0)
{
rdcarray<ResourceRecord *> ParentsToDelete;
Parents.swap(ParentsToDelete);
Length = 0;
DataPtr = NULL;
DeleteChunks();
if(ResID != ResourceId())
mgr->RemoveResourceRecord(ResID);
mgr->DestroyResourceRecord(this);
for(auto it = ParentsToDelete.begin(); it != ParentsToDelete.end(); ++it)
(*it)->Delete(mgr);
}
}