forked from OGRECave/ogre
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OgreHardwareBufferManager.cpp
534 lines (496 loc) · 21.5 KB
/
OgreHardwareBufferManager.cpp
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
/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/
Copyright (c) 2000-2014 Torus Knot Software Ltd
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 "OgreStableHeaders.h"
#include "OgreVertexIndexData.h"
namespace Ogre {
//-----------------------------------------------------------------------
template<> HardwareBufferManager* Singleton<HardwareBufferManager>::msSingleton = 0;
HardwareBufferManager* HardwareBufferManager::getSingletonPtr(void)
{
return msSingleton;
}
HardwareBufferManager& HardwareBufferManager::getSingleton(void)
{
assert( msSingleton ); return ( *msSingleton );
}
//---------------------------------------------------------------------
HardwareBufferManager::HardwareBufferManager()
{
}
//---------------------------------------------------------------------
HardwareBufferManager::~HardwareBufferManager()
{
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
// Free temporary vertex buffers every 5 minutes on 100fps
const size_t HardwareBufferManagerBase::UNDER_USED_FRAME_THRESHOLD = 30000;
const size_t HardwareBufferManagerBase::EXPIRED_DELAY_FRAME_THRESHOLD = 5;
//-----------------------------------------------------------------------
HardwareBufferManagerBase::HardwareBufferManagerBase()
: mUnderUsedFrameCount(0)
{
}
//-----------------------------------------------------------------------
HardwareBufferManagerBase::~HardwareBufferManagerBase()
{
// Destroy everything
destroyAllDeclarations();
destroyAllBindings();
// No need to destroy main buffers - they will be destroyed by removal of bindings
// No need to destroy temp buffers - they will be destroyed automatically.
}
//-----------------------------------------------------------------------
VertexDeclaration* HardwareBufferManagerBase::createVertexDeclaration(void)
{
VertexDeclaration* decl = createVertexDeclarationImpl();
OGRE_LOCK_MUTEX(mVertexDeclarationsMutex);
mVertexDeclarations.insert(decl);
return decl;
}
//-----------------------------------------------------------------------
void HardwareBufferManagerBase::destroyVertexDeclaration(VertexDeclaration* decl)
{
OGRE_LOCK_MUTEX(mVertexDeclarationsMutex);
OgreAssertDbg(mVertexDeclarations.find(decl) != mVertexDeclarations.end(), "unknown decl");
mVertexDeclarations.erase(decl);
destroyVertexDeclarationImpl(decl);
}
//-----------------------------------------------------------------------
VertexBufferBinding* HardwareBufferManagerBase::createVertexBufferBinding(void)
{
VertexBufferBinding* ret = createVertexBufferBindingImpl();
OGRE_LOCK_MUTEX(mVertexBufferBindingsMutex);
mVertexBufferBindings.insert(ret);
return ret;
}
//-----------------------------------------------------------------------
void HardwareBufferManagerBase::destroyVertexBufferBinding(VertexBufferBinding* binding)
{
OGRE_LOCK_MUTEX(mVertexBufferBindingsMutex);
OgreAssertDbg(mVertexBufferBindings.find(binding) != mVertexBufferBindings.end(),
"unknown binding");
mVertexBufferBindings.erase(binding);
destroyVertexBufferBindingImpl(binding);
}
//-----------------------------------------------------------------------
VertexDeclaration* HardwareBufferManagerBase::createVertexDeclarationImpl(void)
{
return OGRE_NEW VertexDeclaration();
}
//-----------------------------------------------------------------------
void HardwareBufferManagerBase::destroyVertexDeclarationImpl(VertexDeclaration* decl)
{
OGRE_DELETE decl;
}
//-----------------------------------------------------------------------
VertexBufferBinding* HardwareBufferManagerBase::createVertexBufferBindingImpl(void)
{
return OGRE_NEW VertexBufferBinding();
}
//-----------------------------------------------------------------------
void HardwareBufferManagerBase::destroyVertexBufferBindingImpl(VertexBufferBinding* binding)
{
OGRE_DELETE binding;
}
//-----------------------------------------------------------------------
void HardwareBufferManagerBase::destroyAllDeclarations(void)
{
OGRE_LOCK_MUTEX(mVertexDeclarationsMutex);
VertexDeclarationList::iterator decl;
for (decl = mVertexDeclarations.begin(); decl != mVertexDeclarations.end(); ++decl)
{
destroyVertexDeclarationImpl(*decl);
}
mVertexDeclarations.clear();
}
//-----------------------------------------------------------------------
void HardwareBufferManagerBase::destroyAllBindings(void)
{
OGRE_LOCK_MUTEX(mVertexBufferBindingsMutex);
VertexBufferBindingList::iterator bind;
for (bind = mVertexBufferBindings.begin(); bind != mVertexBufferBindings.end(); ++bind)
{
destroyVertexBufferBindingImpl(*bind);
}
mVertexBufferBindings.clear();
}
//-----------------------------------------------------------------------
void HardwareBufferManagerBase::registerVertexBufferSourceAndCopy(
const HardwareVertexBufferSharedPtr& sourceBuffer,
const HardwareVertexBufferSharedPtr& copy)
{
OGRE_LOCK_MUTEX(mTempBuffersMutex);
// Add copy to free temporary vertex buffers
mFreeTempVertexBufferMap.emplace(sourceBuffer.get(), copy);
}
//-----------------------------------------------------------------------
HardwareVertexBufferSharedPtr
HardwareBufferManagerBase::allocateVertexBufferCopy(
const HardwareVertexBufferSharedPtr& sourceBuffer,
BufferLicenseType licenseType, HardwareBufferLicensee* licensee,
bool copyData)
{
// pre-lock the mVertexBuffers mutex, which would usually get locked in
// makeBufferCopy / createVertexBuffer
// this prevents a deadlock in _notifyVertexBufferDestroyed
// which locks the same mutexes (via other methods) but in reverse order
OGRE_LOCK_MUTEX(mVertexBuffersMutex);
{
OGRE_LOCK_MUTEX(mTempBuffersMutex);
HardwareVertexBufferSharedPtr vbuf;
// Locate existing buffer copy in temporary vertex buffers
FreeTemporaryVertexBufferMap::iterator i =
mFreeTempVertexBufferMap.find(sourceBuffer.get());
if (i == mFreeTempVertexBufferMap.end())
{
// copy buffer, use shadow buffer and make dynamic
vbuf = makeBufferCopy(
sourceBuffer,
HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE,
true);
}
else
{
// Allocate existing copy
vbuf = i->second;
mFreeTempVertexBufferMap.erase(i);
}
// Copy data?
if (copyData)
{
vbuf->copyData(*(sourceBuffer.get()), 0, 0, sourceBuffer->getSizeInBytes(), true);
}
// Insert copy into licensee list
mTempVertexBufferLicenses.emplace(
vbuf.get(),
VertexBufferLicense(sourceBuffer.get(), licenseType, EXPIRED_DELAY_FRAME_THRESHOLD, vbuf, licensee));
return vbuf;
}
}
//-----------------------------------------------------------------------
void HardwareBufferManagerBase::releaseVertexBufferCopy(
const HardwareVertexBufferSharedPtr& bufferCopy)
{
OGRE_LOCK_MUTEX(mTempBuffersMutex);
TemporaryVertexBufferLicenseMap::iterator i =
mTempVertexBufferLicenses.find(bufferCopy.get());
if (i != mTempVertexBufferLicenses.end())
{
const VertexBufferLicense& vbl = i->second;
vbl.licensee->licenseExpired(vbl.buffer.get());
mFreeTempVertexBufferMap.emplace(vbl.originalBufferPtr, vbl.buffer);
mTempVertexBufferLicenses.erase(i);
}
}
//-----------------------------------------------------------------------
void HardwareBufferManagerBase::touchVertexBufferCopy(
const HardwareVertexBufferSharedPtr& bufferCopy)
{
OGRE_LOCK_MUTEX(mTempBuffersMutex);
TemporaryVertexBufferLicenseMap::iterator i =
mTempVertexBufferLicenses.find(bufferCopy.get());
if (i != mTempVertexBufferLicenses.end())
{
VertexBufferLicense& vbl = i->second;
assert(vbl.licenseType == BLT_AUTOMATIC_RELEASE);
vbl.expiredDelay = EXPIRED_DELAY_FRAME_THRESHOLD;
}
}
//-----------------------------------------------------------------------
void HardwareBufferManagerBase::_freeUnusedBufferCopies(void)
{
OGRE_LOCK_MUTEX(mTempBuffersMutex);
size_t numFreed = 0;
// Free unused temporary buffers
FreeTemporaryVertexBufferMap::iterator i;
i = mFreeTempVertexBufferMap.begin();
while (i != mFreeTempVertexBufferMap.end())
{
FreeTemporaryVertexBufferMap::iterator icur = i++;
// Free the temporary buffer that referenced by ourself only.
// TODO: Some temporary buffers are bound to vertex buffer bindings
// but not checked out, need to sort out method to unbind them.
if (icur->second.use_count() <= 1)
{
++numFreed;
mFreeTempVertexBufferMap.erase(icur);
}
}
StringStream str;
if (numFreed)
{
str << "HardwareBufferManager: Freed " << numFreed << " unused temporary vertex buffers.";
}
else
{
str << "HardwareBufferManager: No unused temporary vertex buffers found.";
}
LogManager::getSingleton().logMessage(str.str(), LML_TRIVIAL);
}
//-----------------------------------------------------------------------
void HardwareBufferManagerBase::_releaseBufferCopies(bool forceFreeUnused)
{
OGRE_LOCK_MUTEX(mTempBuffersMutex);
size_t numUnused = mFreeTempVertexBufferMap.size();
size_t numUsed = mTempVertexBufferLicenses.size();
// Erase the copies which are automatic licensed out
TemporaryVertexBufferLicenseMap::iterator i;
i = mTempVertexBufferLicenses.begin();
while (i != mTempVertexBufferLicenses.end())
{
TemporaryVertexBufferLicenseMap::iterator icur = i++;
VertexBufferLicense& vbl = icur->second;
if (vbl.licenseType == BLT_AUTOMATIC_RELEASE &&
(forceFreeUnused || --vbl.expiredDelay <= 0))
{
vbl.licensee->licenseExpired(vbl.buffer.get());
mFreeTempVertexBufferMap.emplace(vbl.originalBufferPtr, vbl.buffer);
mTempVertexBufferLicenses.erase(icur);
}
}
// Check whether or not free unused temporary vertex buffers.
if (forceFreeUnused)
{
_freeUnusedBufferCopies();
mUnderUsedFrameCount = 0;
}
else
{
if (numUsed < numUnused)
{
// Free temporary vertex buffers if too many unused for a long time.
// Do overall temporary vertex buffers instead of per source buffer
// to avoid overhead.
++mUnderUsedFrameCount;
if (mUnderUsedFrameCount >= UNDER_USED_FRAME_THRESHOLD)
{
_freeUnusedBufferCopies();
mUnderUsedFrameCount = 0;
}
}
else
{
mUnderUsedFrameCount = 0;
}
}
}
//-----------------------------------------------------------------------
void HardwareBufferManagerBase::_forceReleaseBufferCopies(
const HardwareVertexBufferSharedPtr& sourceBuffer)
{
_forceReleaseBufferCopies(sourceBuffer.get());
}
//-----------------------------------------------------------------------
void HardwareBufferManagerBase::_forceReleaseBufferCopies(
HardwareVertexBuffer* sourceBuffer)
{
OGRE_LOCK_MUTEX(mTempBuffersMutex);
// Erase the copies which are licensed out
TemporaryVertexBufferLicenseMap::iterator i;
i = mTempVertexBufferLicenses.begin();
while (i != mTempVertexBufferLicenses.end())
{
TemporaryVertexBufferLicenseMap::iterator icur = i++;
const VertexBufferLicense& vbl = icur->second;
if (vbl.originalBufferPtr == sourceBuffer)
{
// Just tell the owner that this is being released
vbl.licensee->licenseExpired(vbl.buffer.get());
mTempVertexBufferLicenses.erase(icur);
}
}
// Erase the free copies
//
// Why we need this unusual code? It's for resolve reenter problem.
//
// Using mFreeTempVertexBufferMap.erase(sourceBuffer) directly will
// cause reenter into here because vertex buffer destroyed notify.
// In most time there are no problem. But when sourceBuffer is the
// last item of the mFreeTempVertexBufferMap, some STL multimap
// implementation (VC and STLport) will call to clear(), which will
// causing intermediate state of mFreeTempVertexBufferMap, in that
// time destroyed notify back to here cause illegal accessing in
// the end.
//
// For safely reason, use following code to resolve reenter problem.
//
typedef FreeTemporaryVertexBufferMap::iterator _Iter;
std::pair<_Iter, _Iter> range = mFreeTempVertexBufferMap.equal_range(sourceBuffer);
if (range.first != range.second)
{
std::list<HardwareVertexBufferSharedPtr> holdForDelayDestroy;
for (_Iter it = range.first; it != range.second; ++it)
{
if (it->second.use_count() <= 1)
{
holdForDelayDestroy.push_back(it->second);
}
}
mFreeTempVertexBufferMap.erase(range.first, range.second);
// holdForDelayDestroy will destroy auto.
}
}
//-----------------------------------------------------------------------
void HardwareBufferManagerBase::_notifyVertexBufferDestroyed(HardwareVertexBuffer* buf)
{
OGRE_LOCK_MUTEX(mVertexBuffersMutex);
VertexBufferList::iterator i = mVertexBuffers.find(buf);
if (i != mVertexBuffers.end())
{
// release vertex buffer copies
mVertexBuffers.erase(i);
_forceReleaseBufferCopies(buf);
}
}
RenderToVertexBufferSharedPtr HardwareBufferManagerBase::createRenderToVertexBuffer()
{
OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "not supported by RenderSystem");
}
HardwareBufferPtr HardwareBufferManagerBase::createUniformBuffer(size_t sizeBytes,
HardwareBufferUsage usage,
bool useShadowBuffer)
{
OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "not supported by RenderSystem");
}
//-----------------------------------------------------------------------
HardwareVertexBufferSharedPtr
HardwareBufferManagerBase::makeBufferCopy(
const HardwareVertexBufferSharedPtr& source,
HardwareBuffer::Usage usage, bool useShadowBuffer)
{
return this->createVertexBuffer(
source->getVertexSize(),
source->getNumVertices(),
usage, useShadowBuffer);
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
TempBlendedBufferInfo::~TempBlendedBufferInfo(void)
{
// check that temp buffers have been released
if (destPositionBuffer)
destPositionBuffer->getManager()->releaseVertexBufferCopy(destPositionBuffer);
if (destNormalBuffer)
destNormalBuffer->getManager()->releaseVertexBufferCopy(destNormalBuffer);
}
//-----------------------------------------------------------------------------
void TempBlendedBufferInfo::extractFrom(const VertexData* sourceData)
{
// Release old buffer copies first
if (destPositionBuffer)
{
destPositionBuffer->getManager()->releaseVertexBufferCopy(destPositionBuffer);
assert(!destPositionBuffer);
}
if (destNormalBuffer)
{
destNormalBuffer->getManager()->releaseVertexBufferCopy(destNormalBuffer);
assert(!destNormalBuffer);
}
VertexDeclaration* decl = sourceData->vertexDeclaration;
VertexBufferBinding* bind = sourceData->vertexBufferBinding;
const VertexElement *posElem = decl->findElementBySemantic(VES_POSITION);
const VertexElement *normElem = decl->findElementBySemantic(VES_NORMAL);
assert(posElem && "Positions are required");
posBindIndex = posElem->getSource();
srcPositionBuffer = bind->getBuffer(posBindIndex);
if (!normElem)
{
posNormalShareBuffer = false;
srcNormalBuffer.reset();
}
else
{
normBindIndex = normElem->getSource();
if (normBindIndex == posBindIndex)
{
posNormalShareBuffer = true;
srcNormalBuffer.reset();
}
else
{
posNormalShareBuffer = false;
srcNormalBuffer = bind->getBuffer(normBindIndex);
}
}
}
//-----------------------------------------------------------------------------
void TempBlendedBufferInfo::checkoutTempCopies(bool positions, bool normals)
{
bindPositions = positions;
bindNormals = normals;
if (positions && !destPositionBuffer)
{
destPositionBuffer = srcPositionBuffer->getManager()->allocateVertexBufferCopy(srcPositionBuffer,
HardwareBufferManagerBase::BLT_AUTOMATIC_RELEASE, this);
}
if (normals && !posNormalShareBuffer && srcNormalBuffer && !destNormalBuffer)
{
destNormalBuffer = srcNormalBuffer->getManager()->allocateVertexBufferCopy(srcNormalBuffer,
HardwareBufferManagerBase::BLT_AUTOMATIC_RELEASE, this);
}
}
//-----------------------------------------------------------------------------
bool TempBlendedBufferInfo::buffersCheckedOut(bool positions, bool normals) const
{
if (positions || (normals && posNormalShareBuffer))
{
if (!destPositionBuffer)
return false;
destPositionBuffer->getManager()->touchVertexBufferCopy(destPositionBuffer);
}
if (normals && !posNormalShareBuffer)
{
if (!destNormalBuffer)
return false;
destNormalBuffer->getManager()->touchVertexBufferCopy(destNormalBuffer);
}
return true;
}
//-----------------------------------------------------------------------------
void TempBlendedBufferInfo::bindTempCopies(VertexData* targetData, bool suppressHardwareUpload)
{
this->destPositionBuffer->suppressHardwareUpdate(suppressHardwareUpload);
targetData->vertexBufferBinding->setBinding(
this->posBindIndex, this->destPositionBuffer);
if (bindNormals && !posNormalShareBuffer && destNormalBuffer)
{
this->destNormalBuffer->suppressHardwareUpdate(suppressHardwareUpload);
targetData->vertexBufferBinding->setBinding(
this->normBindIndex, this->destNormalBuffer);
}
}
//-----------------------------------------------------------------------------
void TempBlendedBufferInfo::licenseExpired(HardwareBuffer* buffer)
{
assert(buffer == destPositionBuffer.get()
|| buffer == destNormalBuffer.get());
if (buffer == destPositionBuffer.get())
destPositionBuffer.reset();
if (buffer == destNormalBuffer.get())
destNormalBuffer.reset();
}
}