CmVertexIndexData.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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 "CmHardwareVertexBuffer.h"
  27. #include "CmHardwareIndexBuffer.h"
  28. #include "CmVector3.h"
  29. #include "CmAxisAlignedBox.h"
  30. #include "CmException.h"
  31. #include "CmRenderSystem.h"
  32. namespace CamelotEngine {
  33. //-----------------------------------------------------------------------
  34. VertexData::VertexData(HardwareBufferManagerBase* 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, HardwareBufferManagerBase* mgr) const
  64. {
  65. HardwareBufferManagerBase* 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, HardwareBufferManagerBase* mgr)
  116. {
  117. HardwareBufferManagerBase* 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. HBL_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(HBL_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, HardwareBufferManagerBase* 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. HardwareBuffer::Usage final = static_cast<HardwareBuffer::Usage>(
  239. HardwareBuffer::HBU_STATIC_WRITE_ONLY | HardwareBuffer::HBU_DISCARDABLE);
  240. VertexDeclaration::VertexElementList::iterator v;
  241. for (v = destElems.begin(); v != destElems.end(); ++v)
  242. {
  243. VertexElement& destelem = *v;
  244. // get source
  245. const VertexElement* srcelem =
  246. vertexDeclaration->findElementBySemantic(
  247. destelem.getSemantic(), destelem.getIndex());
  248. // get buffer
  249. HardwareVertexBufferPtr srcbuf =
  250. vertexBufferBinding->getBuffer(srcelem->getSource());
  251. // improve flexibility only
  252. if (srcbuf->getUsage() & HardwareBuffer::HBU_DYNAMIC)
  253. {
  254. // remove static
  255. final = static_cast<HardwareBuffer::Usage>(
  256. final & ~HardwareBuffer::HBU_STATIC);
  257. // add dynamic
  258. final = static_cast<HardwareBuffer::Usage>(
  259. final | HardwareBuffer::HBU_DYNAMIC);
  260. }
  261. if (!(srcbuf->getUsage() & HardwareBuffer::HBU_WRITE_ONLY))
  262. {
  263. // remove write only
  264. final = static_cast<HardwareBuffer::Usage>(
  265. final & ~HardwareBuffer::HBU_WRITE_ONLY);
  266. }
  267. if (!(srcbuf->getUsage() & HardwareBuffer::HBU_DISCARDABLE))
  268. {
  269. // remove discardable
  270. final = static_cast<HardwareBuffer::Usage>(
  271. final & ~HardwareBuffer::HBU_DISCARDABLE);
  272. }
  273. }
  274. usages.push_back(final);
  275. }
  276. // Call specific method
  277. reorganiseBuffers(newDeclaration, usages, mgr);
  278. }
  279. //-----------------------------------------------------------------------
  280. void VertexData::closeGapsInBindings(void)
  281. {
  282. if (!vertexBufferBinding->hasGaps())
  283. return;
  284. // Check for error first
  285. const VertexDeclaration::VertexElementList& allelems =
  286. vertexDeclaration->getElements();
  287. VertexDeclaration::VertexElementList::const_iterator ai;
  288. for (ai = allelems.begin(); ai != allelems.end(); ++ai)
  289. {
  290. const VertexElement& elem = *ai;
  291. if (!vertexBufferBinding->isBufferBound(elem.getSource()))
  292. {
  293. CM_EXCEPT(ItemIdentityException,
  294. "No buffer is bound to that element source.");
  295. }
  296. }
  297. // Close gaps in the vertex buffer bindings
  298. VertexBufferBinding::BindingIndexMap bindingIndexMap;
  299. vertexBufferBinding->closeGaps(bindingIndexMap);
  300. // Modify vertex elements to reference to new buffer index
  301. unsigned short elemIndex = 0;
  302. for (ai = allelems.begin(); ai != allelems.end(); ++ai, ++elemIndex)
  303. {
  304. const VertexElement& elem = *ai;
  305. VertexBufferBinding::BindingIndexMap::const_iterator it =
  306. bindingIndexMap.find(elem.getSource());
  307. assert(it != bindingIndexMap.end());
  308. UINT16 targetSource = it->second;
  309. if (elem.getSource() != targetSource)
  310. {
  311. vertexDeclaration->modifyElement(elemIndex,
  312. targetSource, elem.getOffset(), elem.getType(),
  313. elem.getSemantic(), elem.getIndex());
  314. }
  315. }
  316. }
  317. //-----------------------------------------------------------------------
  318. void VertexData::removeUnusedBuffers(void)
  319. {
  320. set<UINT16>::type usedBuffers;
  321. // Collect used buffers
  322. const VertexDeclaration::VertexElementList& allelems =
  323. vertexDeclaration->getElements();
  324. VertexDeclaration::VertexElementList::const_iterator ai;
  325. for (ai = allelems.begin(); ai != allelems.end(); ++ai)
  326. {
  327. const VertexElement& elem = *ai;
  328. usedBuffers.insert(elem.getSource());
  329. }
  330. // Unset unused buffer bindings
  331. UINT16 count = vertexBufferBinding->getLastBoundIndex();
  332. for (UINT16 index = 0; index < count; ++index)
  333. {
  334. if (usedBuffers.find(index) == usedBuffers.end() &&
  335. vertexBufferBinding->isBufferBound(index))
  336. {
  337. vertexBufferBinding->unsetBinding(index);
  338. }
  339. }
  340. // Close gaps
  341. closeGapsInBindings();
  342. }
  343. //-----------------------------------------------------------------------
  344. void VertexData::convertPackedColour(
  345. VertexElementType srcType, VertexElementType destType)
  346. {
  347. if (destType != VET_COLOUR_ABGR && destType != VET_COLOUR_ARGB)
  348. {
  349. CM_EXCEPT(InvalidParametersException,
  350. "Invalid destType parameter");
  351. }
  352. if (srcType != VET_COLOUR_ABGR && srcType != VET_COLOUR_ARGB)
  353. {
  354. CM_EXCEPT(InvalidParametersException,
  355. "Invalid srcType parameter");
  356. }
  357. const VertexBufferBinding::VertexBufferBindingMap& bindMap =
  358. vertexBufferBinding->getBindings();
  359. VertexBufferBinding::VertexBufferBindingMap::const_iterator bindi;
  360. for (bindi = bindMap.begin(); bindi != bindMap.end(); ++bindi)
  361. {
  362. VertexDeclaration::VertexElementList elems =
  363. vertexDeclaration->findElementsBySource(bindi->first);
  364. bool conversionNeeded = false;
  365. VertexDeclaration::VertexElementList::iterator elemi;
  366. for (elemi = elems.begin(); elemi != elems.end(); ++elemi)
  367. {
  368. VertexElement& elem = *elemi;
  369. if (elem.getType() == VET_COLOUR ||
  370. ((elem.getType() == VET_COLOUR_ABGR || elem.getType() == VET_COLOUR_ARGB)
  371. && elem.getType() != destType))
  372. {
  373. conversionNeeded = true;
  374. }
  375. }
  376. if (conversionNeeded)
  377. {
  378. void* pBase = bindi->second->lock(HBL_READ_WRITE);
  379. for (UINT32 v = 0; v < bindi->second->getNumVertices(); ++v)
  380. {
  381. for (elemi = elems.begin(); elemi != elems.end(); ++elemi)
  382. {
  383. VertexElement& elem = *elemi;
  384. VertexElementType currType = (elem.getType() == VET_COLOUR) ?
  385. srcType : elem.getType();
  386. if (elem.getType() == VET_COLOUR ||
  387. ((elem.getType() == VET_COLOUR_ABGR || elem.getType() == VET_COLOUR_ARGB)
  388. && elem.getType() != destType))
  389. {
  390. UINT32* pRGBA;
  391. elem.baseVertexPointerToElement(pBase, &pRGBA);
  392. VertexElement::convertColourValue(currType, destType, pRGBA);
  393. }
  394. }
  395. pBase = static_cast<void*>(
  396. static_cast<char*>(pBase) + bindi->second->getVertexSize());
  397. }
  398. bindi->second->unlock();
  399. // Modify the elements to reflect the changed type
  400. const VertexDeclaration::VertexElementList& allelems =
  401. vertexDeclaration->getElements();
  402. VertexDeclaration::VertexElementList::const_iterator ai;
  403. unsigned short elemIndex = 0;
  404. for (ai = allelems.begin(); ai != allelems.end(); ++ai, ++elemIndex)
  405. {
  406. const VertexElement& elem = *ai;
  407. if (elem.getType() == VET_COLOUR ||
  408. ((elem.getType() == VET_COLOUR_ABGR || elem.getType() == VET_COLOUR_ARGB)
  409. && elem.getType() != destType))
  410. {
  411. vertexDeclaration->modifyElement(elemIndex,
  412. elem.getSource(), elem.getOffset(), destType,
  413. elem.getSemantic(), elem.getIndex());
  414. }
  415. }
  416. }
  417. } // each buffer
  418. }
  419. //-----------------------------------------------------------------------
  420. //-----------------------------------------------------------------------
  421. IndexData::IndexData()
  422. {
  423. indexCount = 0;
  424. indexStart = 0;
  425. }
  426. //-----------------------------------------------------------------------
  427. IndexData::~IndexData()
  428. {
  429. }
  430. //-----------------------------------------------------------------------
  431. IndexData* IndexData::clone(bool copyData, HardwareBufferManagerBase* mgr) const
  432. {
  433. HardwareBufferManagerBase* pManager = mgr ? mgr : HardwareBufferManager::instancePtr();
  434. IndexData* dest = new IndexData();
  435. if (indexBuffer.get())
  436. {
  437. if (copyData)
  438. {
  439. dest->indexBuffer = pManager->createIndexBuffer(indexBuffer->getType(), indexBuffer->getNumIndexes(),
  440. indexBuffer->getUsage());
  441. dest->indexBuffer->copyData(*indexBuffer, 0, 0, indexBuffer->getSizeInBytes(), true);
  442. }
  443. else
  444. {
  445. dest->indexBuffer = indexBuffer;
  446. }
  447. }
  448. dest->indexCount = indexCount;
  449. dest->indexStart = indexStart;
  450. return dest;
  451. }
  452. //-----------------------------------------------------------------------
  453. //-----------------------------------------------------------------------
  454. // Local Utility class for vertex cache optimizer
  455. class Triangle
  456. {
  457. public:
  458. enum EdgeMatchType {
  459. AB, BC, CA, ANY, NONE
  460. };
  461. UINT32 a, b, c;
  462. inline Triangle()
  463. {
  464. }
  465. inline Triangle( UINT32 ta, UINT32 tb, UINT32 tc )
  466. : a( ta ), b( tb ), c( tc )
  467. {
  468. }
  469. inline Triangle( UINT32 t[3] )
  470. : a( t[0] ), b( t[1] ), c( t[2] )
  471. {
  472. }
  473. inline Triangle( const Triangle& t )
  474. : a( t.a ), b( t.b ), c( t.c )
  475. {
  476. }
  477. inline bool sharesEdge(const Triangle& t) const
  478. {
  479. return( a == t.a && b == t.c ||
  480. a == t.b && b == t.a ||
  481. a == t.c && b == t.b ||
  482. b == t.a && c == t.c ||
  483. b == t.b && c == t.a ||
  484. b == t.c && c == t.b ||
  485. c == t.a && a == t.c ||
  486. c == t.b && a == t.a ||
  487. c == t.c && a == t.b );
  488. }
  489. inline bool sharesEdge(const UINT32 ea, const UINT32 eb, const Triangle& t) const
  490. {
  491. return( ea == t.a && eb == t.c ||
  492. ea == t.b && eb == t.a ||
  493. ea == t.c && eb == t.b );
  494. }
  495. inline bool sharesEdge(const EdgeMatchType edge, const Triangle& t) const
  496. {
  497. if (edge == AB)
  498. return sharesEdge(a, b, t);
  499. else if (edge == BC)
  500. return sharesEdge(b, c, t);
  501. else if (edge == CA)
  502. return sharesEdge(c, a, t);
  503. else
  504. return (edge == ANY) == sharesEdge(t);
  505. }
  506. inline EdgeMatchType endoSharedEdge(const Triangle& t) const
  507. {
  508. if (sharesEdge(a, b, t)) return AB;
  509. if (sharesEdge(b, c, t)) return BC;
  510. if (sharesEdge(c, a, t)) return CA;
  511. return NONE;
  512. }
  513. inline EdgeMatchType exoSharedEdge(const Triangle& t) const
  514. {
  515. return t.endoSharedEdge(*this);
  516. }
  517. inline void shiftClockwise()
  518. {
  519. UINT32 t = a;
  520. a = c;
  521. c = b;
  522. b = t;
  523. }
  524. inline void shiftCounterClockwise()
  525. {
  526. UINT32 t = a;
  527. a = b;
  528. b = c;
  529. c = t;
  530. }
  531. };
  532. //-----------------------------------------------------------------------
  533. void IndexData::optimiseVertexCacheTriList(void)
  534. {
  535. if (indexBuffer->isLocked()) return;
  536. void *buffer = indexBuffer->lock(HBL_READ_WRITE);
  537. Triangle* triangles;
  538. UINT32 *dest;
  539. UINT32 nIndexes = indexCount;
  540. UINT32 nTriangles = nIndexes / 3;
  541. UINT32 i, j;
  542. UINT16 *source = 0;
  543. if (indexBuffer->getType() == HardwareIndexBuffer::IT_16BIT)
  544. {
  545. triangles = (Triangle*) malloc(sizeof(Triangle) * nTriangles);
  546. source = (UINT16 *)buffer;
  547. dest = (UINT32 *)triangles;
  548. for (i = 0; i < nIndexes; ++i) dest[i] = source[i];
  549. }
  550. else
  551. triangles = (Triangle*)buffer;
  552. // sort triangles based on shared edges
  553. UINT32 *destlist = (UINT32*)malloc(sizeof(UINT32) * nTriangles);
  554. unsigned char *visited = (unsigned char*)malloc(sizeof(unsigned char) * nTriangles);
  555. for (i = 0; i < nTriangles; ++i) visited[i] = 0;
  556. UINT32 start = 0, ti = 0, destcount = 0;
  557. bool found = false;
  558. for (i = 0; i < nTriangles; ++i)
  559. {
  560. if (found)
  561. found = false;
  562. else
  563. {
  564. while (visited[start++]);
  565. ti = start - 1;
  566. }
  567. destlist[destcount++] = ti;
  568. visited[ti] = 1;
  569. for (j = start; j < nTriangles; ++j)
  570. {
  571. if (visited[j]) continue;
  572. if (triangles[ti].sharesEdge(triangles[j]))
  573. {
  574. found = true;
  575. ti = static_cast<UINT32>(j);
  576. break;
  577. }
  578. }
  579. }
  580. if (indexBuffer->getType() == HardwareIndexBuffer::IT_16BIT)
  581. {
  582. // reorder the indexbuffer
  583. j = 0;
  584. for (i = 0; i < nTriangles; ++i)
  585. {
  586. Triangle *t = &triangles[destlist[i]];
  587. source[j++] = (UINT16)t->a;
  588. source[j++] = (UINT16)t->b;
  589. source[j++] = (UINT16)t->c;
  590. }
  591. free(triangles);
  592. }
  593. else
  594. {
  595. UINT32 *reflist = (UINT32*)malloc(sizeof(UINT32) * nTriangles);
  596. // fill the referencebuffer
  597. for (i = 0; i < nTriangles; ++i)
  598. reflist[destlist[i]] = static_cast<UINT32>(i);
  599. // reorder the indexbuffer
  600. for (i = 0; i < nTriangles; ++i)
  601. {
  602. j = destlist[i];
  603. if (i == j) continue; // do not move triangle
  604. // swap triangles
  605. Triangle t = triangles[i];
  606. triangles[i] = triangles[j];
  607. triangles[j] = t;
  608. // change reference
  609. destlist[reflist[i]] = static_cast<UINT32>(j);
  610. // destlist[i] = i; // not needed, it will not be used
  611. }
  612. free(reflist);
  613. }
  614. free(destlist);
  615. free(visited);
  616. indexBuffer->unlock();
  617. }
  618. //-----------------------------------------------------------------------
  619. //-----------------------------------------------------------------------
  620. void VertexCacheProfiler::profile(const HardwareIndexBufferPtr& indexBuffer)
  621. {
  622. if (indexBuffer->isLocked()) return;
  623. UINT16 *shortbuffer = (UINT16 *)indexBuffer->lock(HBL_READ_ONLY);
  624. if (indexBuffer->getType() == HardwareIndexBuffer::IT_16BIT)
  625. for (unsigned int i = 0; i < indexBuffer->getNumIndexes(); ++i)
  626. inCache(shortbuffer[i]);
  627. else
  628. {
  629. UINT32 *buffer = (UINT32 *)shortbuffer;
  630. for (unsigned int i = 0; i < indexBuffer->getNumIndexes(); ++i)
  631. inCache(buffer[i]);
  632. }
  633. indexBuffer->unlock();
  634. }
  635. //-----------------------------------------------------------------------
  636. bool VertexCacheProfiler::inCache(unsigned int index)
  637. {
  638. for (unsigned int i = 0; i < buffersize; ++i)
  639. {
  640. if (index == cache[i])
  641. {
  642. hit++;
  643. return true;
  644. }
  645. }
  646. miss++;
  647. cache[tail++] = index;
  648. tail %= size;
  649. if (buffersize < size) buffersize++;
  650. return false;
  651. }
  652. }