CmVertexIndexData.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. vertexDeclaration = mMgr->createVertexDeclaration();
  38. vertexCount = 0;
  39. }
  40. VertexData::VertexData(VertexDeclarationPtr dcl)
  41. {
  42. // this is a fallback rather than actively used
  43. mMgr = HardwareBufferManager::instancePtr();
  44. vertexDeclaration = dcl;
  45. vertexCount = 0;
  46. }
  47. VertexData::~VertexData()
  48. {
  49. }
  50. void VertexData::setBuffer(UINT32 index, VertexBufferPtr buffer)
  51. {
  52. mVertexBuffers[index] = buffer;
  53. }
  54. VertexBufferPtr VertexData::getBuffer(UINT32 index) const
  55. {
  56. auto iterFind = mVertexBuffers.find(index);
  57. if(iterFind != mVertexBuffers.end())
  58. {
  59. return iterFind->second;
  60. }
  61. return nullptr;
  62. }
  63. bool VertexData::isBufferBound(UINT32 index) const
  64. {
  65. auto iterFind = mVertexBuffers.find(index);
  66. if(iterFind != mVertexBuffers.end())
  67. {
  68. if(iterFind->second != nullptr)
  69. return true;
  70. }
  71. return false;
  72. }
  73. VertexData* VertexData::clone(bool copyData, HardwareBufferManager* mgr) const
  74. {
  75. HardwareBufferManager* pManager = mgr ? mgr : mMgr;
  76. VertexData* dest = new VertexData(mgr);
  77. // Copy vertex buffers in turn
  78. for (auto iter = mVertexBuffers.begin(); iter != mVertexBuffers.end(); ++iter)
  79. {
  80. VertexBufferPtr srcbuf = iter->second;
  81. VertexBufferPtr dstBuf;
  82. if (copyData)
  83. {
  84. // create new buffer with the same settings
  85. dstBuf = pManager->createVertexBuffer(
  86. srcbuf->getVertexSize(), srcbuf->getNumVertices(), srcbuf->getUsage());
  87. // copy data
  88. dstBuf->copyData(*srcbuf, 0, 0, srcbuf->getSizeInBytes(), true);
  89. }
  90. else
  91. {
  92. // don't copy, point at existing buffer
  93. dstBuf = srcbuf;
  94. }
  95. // Copy binding
  96. dest->setBuffer(iter->first, dstBuf);
  97. }
  98. // Basic vertex info
  99. dest->vertexCount = this->vertexCount;
  100. // Copy elements
  101. const VertexDeclaration::VertexElementList elems =
  102. this->vertexDeclaration->getElements();
  103. VertexDeclaration::VertexElementList::const_iterator ei, eiend;
  104. eiend = elems.end();
  105. for (ei = elems.begin(); ei != eiend; ++ei)
  106. {
  107. dest->vertexDeclaration->addElement(
  108. ei->getSource(),
  109. ei->getOffset(),
  110. ei->getType(),
  111. ei->getSemantic(),
  112. ei->getIndex() );
  113. }
  114. return dest;
  115. }
  116. void VertexData::convertPackedColour(
  117. VertexElementType srcType, VertexElementType destType)
  118. {
  119. if (destType != VET_COLOR_ABGR && destType != VET_COLOR_ARGB)
  120. {
  121. CM_EXCEPT(InvalidParametersException,
  122. "Invalid destType parameter");
  123. }
  124. if (srcType != VET_COLOR_ABGR && srcType != VET_COLOR_ARGB)
  125. {
  126. CM_EXCEPT(InvalidParametersException,
  127. "Invalid srcType parameter");
  128. }
  129. for (auto iter = mVertexBuffers.begin(); iter != mVertexBuffers.end(); ++iter)
  130. {
  131. VertexDeclaration::VertexElementList elems =
  132. vertexDeclaration->findElementsBySource(iter->first);
  133. bool conversionNeeded = false;
  134. VertexDeclaration::VertexElementList::iterator elemi;
  135. for (elemi = elems.begin(); elemi != elems.end(); ++elemi)
  136. {
  137. VertexElement& elem = *elemi;
  138. if (elem.getType() == VET_COLOR ||
  139. ((elem.getType() == VET_COLOR_ABGR || elem.getType() == VET_COLOR_ARGB)
  140. && elem.getType() != destType))
  141. {
  142. conversionNeeded = true;
  143. }
  144. }
  145. if (conversionNeeded)
  146. {
  147. void* pBase = iter->second->lock(GBL_READ_WRITE);
  148. for (UINT32 v = 0; v < iter->second->getNumVertices(); ++v)
  149. {
  150. for (elemi = elems.begin(); elemi != elems.end(); ++elemi)
  151. {
  152. VertexElement& elem = *elemi;
  153. VertexElementType currType = (elem.getType() == VET_COLOR) ?
  154. srcType : elem.getType();
  155. if (elem.getType() == VET_COLOR ||
  156. ((elem.getType() == VET_COLOR_ABGR || elem.getType() == VET_COLOR_ARGB)
  157. && elem.getType() != destType))
  158. {
  159. UINT32* pRGBA;
  160. elem.baseVertexPointerToElement(pBase, &pRGBA);
  161. VertexElement::convertColourValue(currType, destType, pRGBA);
  162. }
  163. }
  164. pBase = static_cast<void*>(
  165. static_cast<char*>(pBase) + iter->second->getVertexSize());
  166. }
  167. iter->second->unlock();
  168. // Modify the elements to reflect the changed type
  169. const VertexDeclaration::VertexElementList& allelems =
  170. vertexDeclaration->getElements();
  171. VertexDeclaration::VertexElementList::const_iterator ai;
  172. unsigned short elemIndex = 0;
  173. for (ai = allelems.begin(); ai != allelems.end(); ++ai, ++elemIndex)
  174. {
  175. const VertexElement& elem = *ai;
  176. if (elem.getType() == VET_COLOR ||
  177. ((elem.getType() == VET_COLOR_ABGR || elem.getType() == VET_COLOR_ARGB)
  178. && elem.getType() != destType))
  179. {
  180. vertexDeclaration->modifyElement(elemIndex,
  181. elem.getSource(), elem.getOffset(), destType,
  182. elem.getSemantic(), elem.getIndex());
  183. }
  184. }
  185. }
  186. } // each buffer
  187. }
  188. IndexData::IndexData()
  189. {
  190. indexCount = 0;
  191. indexStart = 0;
  192. }
  193. IndexData::~IndexData()
  194. {
  195. }
  196. IndexData* IndexData::clone(bool copyData, HardwareBufferManager* mgr) const
  197. {
  198. HardwareBufferManager* pManager = mgr ? mgr : HardwareBufferManager::instancePtr();
  199. IndexData* dest = new IndexData();
  200. if (indexBuffer.get())
  201. {
  202. if (copyData)
  203. {
  204. dest->indexBuffer = pManager->createIndexBuffer(indexBuffer->getType(), indexBuffer->getNumIndexes(),
  205. indexBuffer->getUsage());
  206. dest->indexBuffer->copyData(*indexBuffer, 0, 0, indexBuffer->getSizeInBytes(), true);
  207. }
  208. else
  209. {
  210. dest->indexBuffer = indexBuffer;
  211. }
  212. }
  213. dest->indexCount = indexCount;
  214. dest->indexStart = indexStart;
  215. return dest;
  216. }
  217. // Local Utility class for vertex cache optimizer
  218. class Triangle
  219. {
  220. public:
  221. enum EdgeMatchType {
  222. AB, BC, CA, ANY, NONE
  223. };
  224. UINT32 a, b, c;
  225. inline Triangle()
  226. {
  227. }
  228. inline Triangle( UINT32 ta, UINT32 tb, UINT32 tc )
  229. : a( ta ), b( tb ), c( tc )
  230. {
  231. }
  232. inline Triangle( UINT32 t[3] )
  233. : a( t[0] ), b( t[1] ), c( t[2] )
  234. {
  235. }
  236. inline Triangle( const Triangle& t )
  237. : a( t.a ), b( t.b ), c( t.c )
  238. {
  239. }
  240. inline bool sharesEdge(const Triangle& t) const
  241. {
  242. return( a == t.a && b == t.c ||
  243. a == t.b && b == t.a ||
  244. a == t.c && b == t.b ||
  245. b == t.a && c == t.c ||
  246. b == t.b && c == t.a ||
  247. b == t.c && c == t.b ||
  248. c == t.a && a == t.c ||
  249. c == t.b && a == t.a ||
  250. c == t.c && a == t.b );
  251. }
  252. inline bool sharesEdge(const UINT32 ea, const UINT32 eb, const Triangle& t) const
  253. {
  254. return( ea == t.a && eb == t.c ||
  255. ea == t.b && eb == t.a ||
  256. ea == t.c && eb == t.b );
  257. }
  258. inline bool sharesEdge(const EdgeMatchType edge, const Triangle& t) const
  259. {
  260. if (edge == AB)
  261. return sharesEdge(a, b, t);
  262. else if (edge == BC)
  263. return sharesEdge(b, c, t);
  264. else if (edge == CA)
  265. return sharesEdge(c, a, t);
  266. else
  267. return (edge == ANY) == sharesEdge(t);
  268. }
  269. inline EdgeMatchType endoSharedEdge(const Triangle& t) const
  270. {
  271. if (sharesEdge(a, b, t)) return AB;
  272. if (sharesEdge(b, c, t)) return BC;
  273. if (sharesEdge(c, a, t)) return CA;
  274. return NONE;
  275. }
  276. inline EdgeMatchType exoSharedEdge(const Triangle& t) const
  277. {
  278. return t.endoSharedEdge(*this);
  279. }
  280. inline void shiftClockwise()
  281. {
  282. UINT32 t = a;
  283. a = c;
  284. c = b;
  285. b = t;
  286. }
  287. inline void shiftCounterClockwise()
  288. {
  289. UINT32 t = a;
  290. a = b;
  291. b = c;
  292. c = t;
  293. }
  294. };
  295. void IndexData::optimiseVertexCacheTriList(void)
  296. {
  297. if (indexBuffer->isLocked()) return;
  298. void *buffer = indexBuffer->lock(GBL_READ_WRITE);
  299. Triangle* triangles;
  300. UINT32 *dest;
  301. UINT32 nIndexes = indexCount;
  302. UINT32 nTriangles = nIndexes / 3;
  303. UINT32 i, j;
  304. UINT16 *source = 0;
  305. if (indexBuffer->getType() == IndexBuffer::IT_16BIT)
  306. {
  307. triangles = (Triangle*) malloc(sizeof(Triangle) * nTriangles);
  308. source = (UINT16 *)buffer;
  309. dest = (UINT32 *)triangles;
  310. for (i = 0; i < nIndexes; ++i) dest[i] = source[i];
  311. }
  312. else
  313. triangles = (Triangle*)buffer;
  314. // sort triangles based on shared edges
  315. UINT32 *destlist = (UINT32*)malloc(sizeof(UINT32) * nTriangles);
  316. unsigned char *visited = (unsigned char*)malloc(sizeof(unsigned char) * nTriangles);
  317. for (i = 0; i < nTriangles; ++i) visited[i] = 0;
  318. UINT32 start = 0, ti = 0, destcount = 0;
  319. bool found = false;
  320. for (i = 0; i < nTriangles; ++i)
  321. {
  322. if (found)
  323. found = false;
  324. else
  325. {
  326. while (visited[start++]);
  327. ti = start - 1;
  328. }
  329. destlist[destcount++] = ti;
  330. visited[ti] = 1;
  331. for (j = start; j < nTriangles; ++j)
  332. {
  333. if (visited[j]) continue;
  334. if (triangles[ti].sharesEdge(triangles[j]))
  335. {
  336. found = true;
  337. ti = static_cast<UINT32>(j);
  338. break;
  339. }
  340. }
  341. }
  342. if (indexBuffer->getType() == IndexBuffer::IT_16BIT)
  343. {
  344. // reorder the indexbuffer
  345. j = 0;
  346. for (i = 0; i < nTriangles; ++i)
  347. {
  348. Triangle *t = &triangles[destlist[i]];
  349. source[j++] = (UINT16)t->a;
  350. source[j++] = (UINT16)t->b;
  351. source[j++] = (UINT16)t->c;
  352. }
  353. free(triangles);
  354. }
  355. else
  356. {
  357. UINT32 *reflist = (UINT32*)malloc(sizeof(UINT32) * nTriangles);
  358. // fill the referencebuffer
  359. for (i = 0; i < nTriangles; ++i)
  360. reflist[destlist[i]] = static_cast<UINT32>(i);
  361. // reorder the indexbuffer
  362. for (i = 0; i < nTriangles; ++i)
  363. {
  364. j = destlist[i];
  365. if (i == j) continue; // do not move triangle
  366. // swap triangles
  367. Triangle t = triangles[i];
  368. triangles[i] = triangles[j];
  369. triangles[j] = t;
  370. // change reference
  371. destlist[reflist[i]] = static_cast<UINT32>(j);
  372. // destlist[i] = i; // not needed, it will not be used
  373. }
  374. free(reflist);
  375. }
  376. free(destlist);
  377. free(visited);
  378. indexBuffer->unlock();
  379. }
  380. void VertexCacheProfiler::profile(const IndexBufferPtr& indexBuffer)
  381. {
  382. if (indexBuffer->isLocked()) return;
  383. UINT16 *shortbuffer = (UINT16 *)indexBuffer->lock(GBL_READ_ONLY);
  384. if (indexBuffer->getType() == IndexBuffer::IT_16BIT)
  385. for (unsigned int i = 0; i < indexBuffer->getNumIndexes(); ++i)
  386. inCache(shortbuffer[i]);
  387. else
  388. {
  389. UINT32 *buffer = (UINT32 *)shortbuffer;
  390. for (unsigned int i = 0; i < indexBuffer->getNumIndexes(); ++i)
  391. inCache(buffer[i]);
  392. }
  393. indexBuffer->unlock();
  394. }
  395. bool VertexCacheProfiler::inCache(unsigned int index)
  396. {
  397. for (unsigned int i = 0; i < buffersize; ++i)
  398. {
  399. if (index == cache[i])
  400. {
  401. hit++;
  402. return true;
  403. }
  404. }
  405. miss++;
  406. cache[tail++] = index;
  407. tail %= size;
  408. if (buffersize < size) buffersize++;
  409. return false;
  410. }
  411. }