CmVertexIndexData.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmVertexIndexData.h"
  25. #include "CmHardwareBufferManager.h"
  26. #include "CmVertexBuffer.h"
  27. #include "CmIndexBuffer.h"
  28. #include "CmVector3.h"
  29. #include "CmAxisAlignedBox.h"
  30. #include "CmException.h"
  31. #include "CmRenderSystem.h"
  32. namespace CamelotEngine {
  33. //-----------------------------------------------------------------------
  34. VertexData::VertexData(HardwareBufferManager* mgr)
  35. {
  36. mMgr = mgr ? mgr : HardwareBufferManager::instancePtr();
  37. vertexBufferBinding = mMgr->createVertexBufferBinding();
  38. vertexDeclaration = mMgr->createVertexDeclaration();
  39. mDeleteDclBinding = true;
  40. vertexCount = 0;
  41. vertexStart = 0;
  42. }
  43. //---------------------------------------------------------------------
  44. VertexData::VertexData(VertexDeclarationPtr dcl, VertexBufferBinding* bind)
  45. {
  46. // this is a fallback rather than actively used
  47. mMgr = HardwareBufferManager::instancePtr();
  48. vertexDeclaration = dcl;
  49. vertexBufferBinding = bind;
  50. mDeleteDclBinding = false;
  51. vertexCount = 0;
  52. vertexStart = 0;
  53. }
  54. //-----------------------------------------------------------------------
  55. VertexData::~VertexData()
  56. {
  57. if (mDeleteDclBinding)
  58. {
  59. mMgr->destroyVertexBufferBinding(vertexBufferBinding);
  60. }
  61. }
  62. //-----------------------------------------------------------------------
  63. VertexData* VertexData::clone(bool copyData, HardwareBufferManager* mgr) const
  64. {
  65. HardwareBufferManager* pManager = mgr ? mgr : mMgr;
  66. VertexData* dest = new VertexData(mgr);
  67. // Copy vertex buffers in turn
  68. const VertexBufferBinding::VertexBufferBindingMap& bindings =
  69. this->vertexBufferBinding->getBindings();
  70. VertexBufferBinding::VertexBufferBindingMap::const_iterator vbi, vbend;
  71. vbend = bindings.end();
  72. for (vbi = bindings.begin(); vbi != vbend; ++vbi)
  73. {
  74. HardwareVertexBufferPtr srcbuf = vbi->second;
  75. HardwareVertexBufferPtr dstBuf;
  76. if (copyData)
  77. {
  78. // create new buffer with the same settings
  79. dstBuf = pManager->createVertexBuffer(
  80. srcbuf->getVertexSize(), srcbuf->getNumVertices(), srcbuf->getUsage());
  81. // copy data
  82. dstBuf->copyData(*srcbuf, 0, 0, srcbuf->getSizeInBytes(), true);
  83. }
  84. else
  85. {
  86. // don't copy, point at existing buffer
  87. dstBuf = srcbuf;
  88. }
  89. // Copy binding
  90. dest->vertexBufferBinding->setBinding(vbi->first, dstBuf);
  91. }
  92. // Basic vertex info
  93. dest->vertexStart = this->vertexStart;
  94. dest->vertexCount = this->vertexCount;
  95. // Copy elements
  96. const VertexDeclaration::VertexElementList elems =
  97. this->vertexDeclaration->getElements();
  98. VertexDeclaration::VertexElementList::const_iterator ei, eiend;
  99. eiend = elems.end();
  100. for (ei = elems.begin(); ei != eiend; ++ei)
  101. {
  102. dest->vertexDeclaration->addElement(
  103. ei->getSource(),
  104. ei->getOffset(),
  105. ei->getType(),
  106. ei->getSemantic(),
  107. ei->getIndex() );
  108. }
  109. // Copy reference to hardware shadow buffer, no matter whether copy data or not
  110. dest->hardwareShadowVolWBuffer = hardwareShadowVolWBuffer;
  111. return dest;
  112. }
  113. //-----------------------------------------------------------------------
  114. void VertexData::reorganiseBuffers(VertexDeclarationPtr newDeclaration,
  115. const BufferUsageList& bufferUsages, HardwareBufferManager* mgr)
  116. {
  117. HardwareBufferManager* pManager = mgr ? mgr : mMgr;
  118. // Firstly, close up any gaps in the buffer sources which might have arisen
  119. newDeclaration->closeGapsInSource();
  120. // Build up a list of both old and new elements in each buffer
  121. unsigned short buf = 0;
  122. vector<void*>::type oldBufferLocks;
  123. vector<UINT32>::type oldBufferVertexSizes;
  124. vector<void*>::type newBufferLocks;
  125. vector<UINT32>::type newBufferVertexSizes;
  126. VertexBufferBinding* newBinding = pManager->createVertexBufferBinding();
  127. const VertexBufferBinding::VertexBufferBindingMap& oldBindingMap = vertexBufferBinding->getBindings();
  128. VertexBufferBinding::VertexBufferBindingMap::const_iterator itBinding;
  129. // Pre-allocate old buffer locks
  130. if (!oldBindingMap.empty())
  131. {
  132. UINT32 count = oldBindingMap.rbegin()->first + 1;
  133. oldBufferLocks.resize(count);
  134. oldBufferVertexSizes.resize(count);
  135. }
  136. // Lock all the old buffers for reading
  137. for (itBinding = oldBindingMap.begin(); itBinding != oldBindingMap.end(); ++itBinding)
  138. {
  139. assert(itBinding->second->getNumVertices() >= vertexCount);
  140. oldBufferVertexSizes[itBinding->first] =
  141. itBinding->second->getVertexSize();
  142. oldBufferLocks[itBinding->first] =
  143. itBinding->second->lock(
  144. GBL_READ_ONLY);
  145. }
  146. // Create new buffers and lock all for writing
  147. buf = 0;
  148. while (!newDeclaration->findElementsBySource(buf).empty())
  149. {
  150. UINT32 vertexSize = newDeclaration->getVertexSize(buf);
  151. HardwareVertexBufferPtr vbuf =
  152. pManager->createVertexBuffer(
  153. vertexSize,
  154. vertexCount,
  155. bufferUsages[buf]);
  156. newBinding->setBinding(buf, vbuf);
  157. newBufferVertexSizes.push_back(vertexSize);
  158. newBufferLocks.push_back(
  159. vbuf->lock(GBL_WRITE_ONLY_DISCARD));
  160. buf++;
  161. }
  162. // Map from new to old elements
  163. typedef map<const VertexElement*, const VertexElement*>::type NewToOldElementMap;
  164. NewToOldElementMap newToOldElementMap;
  165. const VertexDeclaration::VertexElementList& newElemList = newDeclaration->getElements();
  166. VertexDeclaration::VertexElementList::const_iterator ei, eiend;
  167. eiend = newElemList.end();
  168. for (ei = newElemList.begin(); ei != eiend; ++ei)
  169. {
  170. // Find corresponding old element
  171. const VertexElement* oldElem =
  172. vertexDeclaration->findElementBySemantic(
  173. (*ei).getSemantic(), (*ei).getIndex());
  174. if (!oldElem)
  175. {
  176. // Error, cannot create new elements with this method
  177. CM_EXCEPT(ItemIdentityException,
  178. "Element not found in old vertex declaration");
  179. }
  180. newToOldElementMap[&(*ei)] = oldElem;
  181. }
  182. // Now iterate over the new buffers, pulling data out of the old ones
  183. // For each vertex
  184. for (UINT32 v = 0; v < vertexCount; ++v)
  185. {
  186. // For each (new) element
  187. for (ei = newElemList.begin(); ei != eiend; ++ei)
  188. {
  189. const VertexElement* newElem = &(*ei);
  190. NewToOldElementMap::iterator noi = newToOldElementMap.find(newElem);
  191. const VertexElement* oldElem = noi->second;
  192. unsigned short oldBufferNo = oldElem->getSource();
  193. unsigned short newBufferNo = newElem->getSource();
  194. void* pSrcBase = static_cast<void*>(
  195. static_cast<unsigned char*>(oldBufferLocks[oldBufferNo])
  196. + v * oldBufferVertexSizes[oldBufferNo]);
  197. void* pDstBase = static_cast<void*>(
  198. static_cast<unsigned char*>(newBufferLocks[newBufferNo])
  199. + v * newBufferVertexSizes[newBufferNo]);
  200. void *pSrc, *pDst;
  201. oldElem->baseVertexPointerToElement(pSrcBase, &pSrc);
  202. newElem->baseVertexPointerToElement(pDstBase, &pDst);
  203. memcpy(pDst, pSrc, newElem->getSize());
  204. }
  205. }
  206. // Unlock all buffers
  207. for (itBinding = oldBindingMap.begin(); itBinding != oldBindingMap.end(); ++itBinding)
  208. {
  209. itBinding->second->unlock();
  210. }
  211. for (buf = 0; buf < newBinding->getBufferCount(); ++buf)
  212. {
  213. newBinding->getBuffer(buf)->unlock();
  214. }
  215. // Delete old binding & declaration
  216. if (mDeleteDclBinding)
  217. {
  218. pManager->destroyVertexBufferBinding(vertexBufferBinding);
  219. }
  220. // Assign new binding and declaration
  221. vertexDeclaration = newDeclaration;
  222. vertexBufferBinding = newBinding;
  223. // after this is complete, new manager should be used
  224. mMgr = pManager;
  225. mDeleteDclBinding = true; // because we created these through a manager
  226. }
  227. //-----------------------------------------------------------------------
  228. void VertexData::reorganiseBuffers(VertexDeclarationPtr newDeclaration, HardwareBufferManager* mgr)
  229. {
  230. // Derive the buffer usages from looking at where the source has come
  231. // from
  232. BufferUsageList usages;
  233. for (unsigned short b = 0; b <= newDeclaration->getMaxSource(); ++b)
  234. {
  235. VertexDeclaration::VertexElementList destElems = newDeclaration->findElementsBySource(b);
  236. // Initialise with most restrictive version
  237. // (not really a usable option, but these flags will be removed)
  238. GpuBufferUsage final = static_cast<GpuBufferUsage>(GBU_STATIC);
  239. VertexDeclaration::VertexElementList::iterator v;
  240. for (v = destElems.begin(); v != destElems.end(); ++v)
  241. {
  242. VertexElement& destelem = *v;
  243. // get source
  244. const VertexElement* srcelem =
  245. vertexDeclaration->findElementBySemantic(
  246. destelem.getSemantic(), destelem.getIndex());
  247. // get buffer
  248. HardwareVertexBufferPtr srcbuf =
  249. vertexBufferBinding->getBuffer(srcelem->getSource());
  250. // improve flexibility only
  251. if (srcbuf->getUsage() & GBU_DYNAMIC)
  252. {
  253. // remove static
  254. final = static_cast<GpuBufferUsage>(
  255. final & ~GBU_STATIC);
  256. // add dynamic
  257. final = static_cast<GpuBufferUsage>(
  258. final | GBU_DYNAMIC);
  259. }
  260. }
  261. usages.push_back(final);
  262. }
  263. // Call specific method
  264. reorganiseBuffers(newDeclaration, usages, mgr);
  265. }
  266. //-----------------------------------------------------------------------
  267. void VertexData::closeGapsInBindings(void)
  268. {
  269. if (!vertexBufferBinding->hasGaps())
  270. return;
  271. // Check for error first
  272. const VertexDeclaration::VertexElementList& allelems =
  273. vertexDeclaration->getElements();
  274. VertexDeclaration::VertexElementList::const_iterator ai;
  275. for (ai = allelems.begin(); ai != allelems.end(); ++ai)
  276. {
  277. const VertexElement& elem = *ai;
  278. if (!vertexBufferBinding->isBufferBound(elem.getSource()))
  279. {
  280. CM_EXCEPT(ItemIdentityException,
  281. "No buffer is bound to that element source.");
  282. }
  283. }
  284. // Close gaps in the vertex buffer bindings
  285. VertexBufferBinding::BindingIndexMap bindingIndexMap;
  286. vertexBufferBinding->closeGaps(bindingIndexMap);
  287. // Modify vertex elements to reference to new buffer index
  288. unsigned short elemIndex = 0;
  289. for (ai = allelems.begin(); ai != allelems.end(); ++ai, ++elemIndex)
  290. {
  291. const VertexElement& elem = *ai;
  292. VertexBufferBinding::BindingIndexMap::const_iterator it =
  293. bindingIndexMap.find(elem.getSource());
  294. assert(it != bindingIndexMap.end());
  295. UINT16 targetSource = it->second;
  296. if (elem.getSource() != targetSource)
  297. {
  298. vertexDeclaration->modifyElement(elemIndex,
  299. targetSource, elem.getOffset(), elem.getType(),
  300. elem.getSemantic(), elem.getIndex());
  301. }
  302. }
  303. }
  304. //-----------------------------------------------------------------------
  305. void VertexData::removeUnusedBuffers(void)
  306. {
  307. set<UINT16>::type usedBuffers;
  308. // Collect used buffers
  309. const VertexDeclaration::VertexElementList& allelems =
  310. vertexDeclaration->getElements();
  311. VertexDeclaration::VertexElementList::const_iterator ai;
  312. for (ai = allelems.begin(); ai != allelems.end(); ++ai)
  313. {
  314. const VertexElement& elem = *ai;
  315. usedBuffers.insert(elem.getSource());
  316. }
  317. // Unset unused buffer bindings
  318. UINT16 count = vertexBufferBinding->getLastBoundIndex();
  319. for (UINT16 index = 0; index < count; ++index)
  320. {
  321. if (usedBuffers.find(index) == usedBuffers.end() &&
  322. vertexBufferBinding->isBufferBound(index))
  323. {
  324. vertexBufferBinding->unsetBinding(index);
  325. }
  326. }
  327. // Close gaps
  328. closeGapsInBindings();
  329. }
  330. //-----------------------------------------------------------------------
  331. void VertexData::convertPackedColour(
  332. VertexElementType srcType, VertexElementType destType)
  333. {
  334. if (destType != VET_COLOR_ABGR && destType != VET_COLOR_ARGB)
  335. {
  336. CM_EXCEPT(InvalidParametersException,
  337. "Invalid destType parameter");
  338. }
  339. if (srcType != VET_COLOR_ABGR && srcType != VET_COLOR_ARGB)
  340. {
  341. CM_EXCEPT(InvalidParametersException,
  342. "Invalid srcType parameter");
  343. }
  344. const VertexBufferBinding::VertexBufferBindingMap& bindMap =
  345. vertexBufferBinding->getBindings();
  346. VertexBufferBinding::VertexBufferBindingMap::const_iterator bindi;
  347. for (bindi = bindMap.begin(); bindi != bindMap.end(); ++bindi)
  348. {
  349. VertexDeclaration::VertexElementList elems =
  350. vertexDeclaration->findElementsBySource(bindi->first);
  351. bool conversionNeeded = false;
  352. VertexDeclaration::VertexElementList::iterator elemi;
  353. for (elemi = elems.begin(); elemi != elems.end(); ++elemi)
  354. {
  355. VertexElement& elem = *elemi;
  356. if (elem.getType() == VET_COLOR ||
  357. ((elem.getType() == VET_COLOR_ABGR || elem.getType() == VET_COLOR_ARGB)
  358. && elem.getType() != destType))
  359. {
  360. conversionNeeded = true;
  361. }
  362. }
  363. if (conversionNeeded)
  364. {
  365. void* pBase = bindi->second->lock(GBL_READ_WRITE);
  366. for (UINT32 v = 0; v < bindi->second->getNumVertices(); ++v)
  367. {
  368. for (elemi = elems.begin(); elemi != elems.end(); ++elemi)
  369. {
  370. VertexElement& elem = *elemi;
  371. VertexElementType currType = (elem.getType() == VET_COLOR) ?
  372. srcType : elem.getType();
  373. if (elem.getType() == VET_COLOR ||
  374. ((elem.getType() == VET_COLOR_ABGR || elem.getType() == VET_COLOR_ARGB)
  375. && elem.getType() != destType))
  376. {
  377. UINT32* pRGBA;
  378. elem.baseVertexPointerToElement(pBase, &pRGBA);
  379. VertexElement::convertColourValue(currType, destType, pRGBA);
  380. }
  381. }
  382. pBase = static_cast<void*>(
  383. static_cast<char*>(pBase) + bindi->second->getVertexSize());
  384. }
  385. bindi->second->unlock();
  386. // Modify the elements to reflect the changed type
  387. const VertexDeclaration::VertexElementList& allelems =
  388. vertexDeclaration->getElements();
  389. VertexDeclaration::VertexElementList::const_iterator ai;
  390. unsigned short elemIndex = 0;
  391. for (ai = allelems.begin(); ai != allelems.end(); ++ai, ++elemIndex)
  392. {
  393. const VertexElement& elem = *ai;
  394. if (elem.getType() == VET_COLOR ||
  395. ((elem.getType() == VET_COLOR_ABGR || elem.getType() == VET_COLOR_ARGB)
  396. && elem.getType() != destType))
  397. {
  398. vertexDeclaration->modifyElement(elemIndex,
  399. elem.getSource(), elem.getOffset(), destType,
  400. elem.getSemantic(), elem.getIndex());
  401. }
  402. }
  403. }
  404. } // each buffer
  405. }
  406. //-----------------------------------------------------------------------
  407. //-----------------------------------------------------------------------
  408. IndexData::IndexData()
  409. {
  410. indexCount = 0;
  411. indexStart = 0;
  412. }
  413. //-----------------------------------------------------------------------
  414. IndexData::~IndexData()
  415. {
  416. }
  417. //-----------------------------------------------------------------------
  418. IndexData* IndexData::clone(bool copyData, HardwareBufferManager* mgr) const
  419. {
  420. HardwareBufferManager* pManager = mgr ? mgr : HardwareBufferManager::instancePtr();
  421. IndexData* dest = new IndexData();
  422. if (indexBuffer.get())
  423. {
  424. if (copyData)
  425. {
  426. dest->indexBuffer = pManager->createIndexBuffer(indexBuffer->getType(), indexBuffer->getNumIndexes(),
  427. indexBuffer->getUsage());
  428. dest->indexBuffer->copyData(*indexBuffer, 0, 0, indexBuffer->getSizeInBytes(), true);
  429. }
  430. else
  431. {
  432. dest->indexBuffer = indexBuffer;
  433. }
  434. }
  435. dest->indexCount = indexCount;
  436. dest->indexStart = indexStart;
  437. return dest;
  438. }
  439. //-----------------------------------------------------------------------
  440. //-----------------------------------------------------------------------
  441. // Local Utility class for vertex cache optimizer
  442. class Triangle
  443. {
  444. public:
  445. enum EdgeMatchType {
  446. AB, BC, CA, ANY, NONE
  447. };
  448. UINT32 a, b, c;
  449. inline Triangle()
  450. {
  451. }
  452. inline Triangle( UINT32 ta, UINT32 tb, UINT32 tc )
  453. : a( ta ), b( tb ), c( tc )
  454. {
  455. }
  456. inline Triangle( UINT32 t[3] )
  457. : a( t[0] ), b( t[1] ), c( t[2] )
  458. {
  459. }
  460. inline Triangle( const Triangle& t )
  461. : a( t.a ), b( t.b ), c( t.c )
  462. {
  463. }
  464. inline bool sharesEdge(const Triangle& t) const
  465. {
  466. return( a == t.a && b == t.c ||
  467. a == t.b && b == t.a ||
  468. a == t.c && b == t.b ||
  469. b == t.a && c == t.c ||
  470. b == t.b && c == t.a ||
  471. b == t.c && c == t.b ||
  472. c == t.a && a == t.c ||
  473. c == t.b && a == t.a ||
  474. c == t.c && a == t.b );
  475. }
  476. inline bool sharesEdge(const UINT32 ea, const UINT32 eb, const Triangle& t) const
  477. {
  478. return( ea == t.a && eb == t.c ||
  479. ea == t.b && eb == t.a ||
  480. ea == t.c && eb == t.b );
  481. }
  482. inline bool sharesEdge(const EdgeMatchType edge, const Triangle& t) const
  483. {
  484. if (edge == AB)
  485. return sharesEdge(a, b, t);
  486. else if (edge == BC)
  487. return sharesEdge(b, c, t);
  488. else if (edge == CA)
  489. return sharesEdge(c, a, t);
  490. else
  491. return (edge == ANY) == sharesEdge(t);
  492. }
  493. inline EdgeMatchType endoSharedEdge(const Triangle& t) const
  494. {
  495. if (sharesEdge(a, b, t)) return AB;
  496. if (sharesEdge(b, c, t)) return BC;
  497. if (sharesEdge(c, a, t)) return CA;
  498. return NONE;
  499. }
  500. inline EdgeMatchType exoSharedEdge(const Triangle& t) const
  501. {
  502. return t.endoSharedEdge(*this);
  503. }
  504. inline void shiftClockwise()
  505. {
  506. UINT32 t = a;
  507. a = c;
  508. c = b;
  509. b = t;
  510. }
  511. inline void shiftCounterClockwise()
  512. {
  513. UINT32 t = a;
  514. a = b;
  515. b = c;
  516. c = t;
  517. }
  518. };
  519. //-----------------------------------------------------------------------
  520. void IndexData::optimiseVertexCacheTriList(void)
  521. {
  522. if (indexBuffer->isLocked()) return;
  523. void *buffer = indexBuffer->lock(GBL_READ_WRITE);
  524. Triangle* triangles;
  525. UINT32 *dest;
  526. UINT32 nIndexes = indexCount;
  527. UINT32 nTriangles = nIndexes / 3;
  528. UINT32 i, j;
  529. UINT16 *source = 0;
  530. if (indexBuffer->getType() == IndexBuffer::IT_16BIT)
  531. {
  532. triangles = (Triangle*) malloc(sizeof(Triangle) * nTriangles);
  533. source = (UINT16 *)buffer;
  534. dest = (UINT32 *)triangles;
  535. for (i = 0; i < nIndexes; ++i) dest[i] = source[i];
  536. }
  537. else
  538. triangles = (Triangle*)buffer;
  539. // sort triangles based on shared edges
  540. UINT32 *destlist = (UINT32*)malloc(sizeof(UINT32) * nTriangles);
  541. unsigned char *visited = (unsigned char*)malloc(sizeof(unsigned char) * nTriangles);
  542. for (i = 0; i < nTriangles; ++i) visited[i] = 0;
  543. UINT32 start = 0, ti = 0, destcount = 0;
  544. bool found = false;
  545. for (i = 0; i < nTriangles; ++i)
  546. {
  547. if (found)
  548. found = false;
  549. else
  550. {
  551. while (visited[start++]);
  552. ti = start - 1;
  553. }
  554. destlist[destcount++] = ti;
  555. visited[ti] = 1;
  556. for (j = start; j < nTriangles; ++j)
  557. {
  558. if (visited[j]) continue;
  559. if (triangles[ti].sharesEdge(triangles[j]))
  560. {
  561. found = true;
  562. ti = static_cast<UINT32>(j);
  563. break;
  564. }
  565. }
  566. }
  567. if (indexBuffer->getType() == IndexBuffer::IT_16BIT)
  568. {
  569. // reorder the indexbuffer
  570. j = 0;
  571. for (i = 0; i < nTriangles; ++i)
  572. {
  573. Triangle *t = &triangles[destlist[i]];
  574. source[j++] = (UINT16)t->a;
  575. source[j++] = (UINT16)t->b;
  576. source[j++] = (UINT16)t->c;
  577. }
  578. free(triangles);
  579. }
  580. else
  581. {
  582. UINT32 *reflist = (UINT32*)malloc(sizeof(UINT32) * nTriangles);
  583. // fill the referencebuffer
  584. for (i = 0; i < nTriangles; ++i)
  585. reflist[destlist[i]] = static_cast<UINT32>(i);
  586. // reorder the indexbuffer
  587. for (i = 0; i < nTriangles; ++i)
  588. {
  589. j = destlist[i];
  590. if (i == j) continue; // do not move triangle
  591. // swap triangles
  592. Triangle t = triangles[i];
  593. triangles[i] = triangles[j];
  594. triangles[j] = t;
  595. // change reference
  596. destlist[reflist[i]] = static_cast<UINT32>(j);
  597. // destlist[i] = i; // not needed, it will not be used
  598. }
  599. free(reflist);
  600. }
  601. free(destlist);
  602. free(visited);
  603. indexBuffer->unlock();
  604. }
  605. //-----------------------------------------------------------------------
  606. //-----------------------------------------------------------------------
  607. void VertexCacheProfiler::profile(const HardwareIndexBufferPtr& indexBuffer)
  608. {
  609. if (indexBuffer->isLocked()) return;
  610. UINT16 *shortbuffer = (UINT16 *)indexBuffer->lock(GBL_READ_ONLY);
  611. if (indexBuffer->getType() == IndexBuffer::IT_16BIT)
  612. for (unsigned int i = 0; i < indexBuffer->getNumIndexes(); ++i)
  613. inCache(shortbuffer[i]);
  614. else
  615. {
  616. UINT32 *buffer = (UINT32 *)shortbuffer;
  617. for (unsigned int i = 0; i < indexBuffer->getNumIndexes(); ++i)
  618. inCache(buffer[i]);
  619. }
  620. indexBuffer->unlock();
  621. }
  622. //-----------------------------------------------------------------------
  623. bool VertexCacheProfiler::inCache(unsigned int index)
  624. {
  625. for (unsigned int i = 0; i < buffersize; ++i)
  626. {
  627. if (index == cache[i])
  628. {
  629. hit++;
  630. return true;
  631. }
  632. }
  633. miss++;
  634. cache[tail++] = index;
  635. tail %= size;
  636. if (buffersize < size) buffersize++;
  637. return false;
  638. }
  639. }