CmVertexData.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "CmIndexData.h"
  25. #include "CmVertexData.h"
  26. #include "CmHardwareBufferManager.h"
  27. #include "CmVertexBuffer.h"
  28. #include "CmVector3.h"
  29. #include "CmException.h"
  30. #include "CmRenderSystem.h"
  31. namespace CamelotEngine
  32. {
  33. VertexData::VertexData(HardwareBufferManager* mgr)
  34. :mOwnsDeclaration(true)
  35. {
  36. mMgr = mgr ? mgr : HardwareBufferManager::instancePtr();
  37. vertexDeclaration = mMgr->createVertexDeclaration();
  38. vertexCount = 0;
  39. }
  40. VertexData::VertexData(VertexDeclarationPtr dcl)
  41. :mOwnsDeclaration(false)
  42. {
  43. // this is a fallback rather than actively used
  44. mMgr = HardwareBufferManager::instancePtr();
  45. vertexDeclaration = dcl;
  46. vertexCount = 0;
  47. }
  48. VertexData::~VertexData()
  49. {
  50. }
  51. void VertexData::setBuffer(UINT32 index, VertexBufferPtr buffer)
  52. {
  53. mVertexBuffers[index] = buffer;
  54. }
  55. VertexBufferPtr VertexData::getBuffer(UINT32 index) const
  56. {
  57. auto iterFind = mVertexBuffers.find(index);
  58. if(iterFind != mVertexBuffers.end())
  59. {
  60. return iterFind->second;
  61. }
  62. return nullptr;
  63. }
  64. bool VertexData::isBufferBound(UINT32 index) const
  65. {
  66. auto iterFind = mVertexBuffers.find(index);
  67. if(iterFind != mVertexBuffers.end())
  68. {
  69. if(iterFind->second != nullptr)
  70. return true;
  71. }
  72. return false;
  73. }
  74. VertexData* VertexData::clone(bool copyData, HardwareBufferManager* mgr) const
  75. {
  76. HardwareBufferManager* pManager = mgr ? mgr : mMgr;
  77. VertexData* dest = new VertexData(mgr);
  78. // Copy vertex buffers in turn
  79. for (auto iter = mVertexBuffers.begin(); iter != mVertexBuffers.end(); ++iter)
  80. {
  81. VertexBufferPtr srcbuf = iter->second;
  82. VertexBufferPtr dstBuf;
  83. if (copyData)
  84. {
  85. // create new buffer with the same settings
  86. dstBuf = pManager->createVertexBuffer(
  87. srcbuf->getVertexSize(), srcbuf->getNumVertices(), srcbuf->getUsage());
  88. // copy data
  89. dstBuf->copyData(*srcbuf, 0, 0, srcbuf->getSizeInBytes(), true);
  90. }
  91. else
  92. {
  93. // don't copy, point at existing buffer
  94. dstBuf = srcbuf;
  95. }
  96. // Copy binding
  97. dest->setBuffer(iter->first, dstBuf);
  98. }
  99. // Basic vertex info
  100. dest->vertexCount = this->vertexCount;
  101. // Copy elements
  102. const VertexDeclaration::VertexElementList elems =
  103. this->vertexDeclaration->getElements();
  104. VertexDeclaration::VertexElementList::const_iterator ei, eiend;
  105. eiend = elems.end();
  106. for (ei = elems.begin(); ei != eiend; ++ei)
  107. {
  108. dest->vertexDeclaration->addElement(
  109. ei->getSource(),
  110. ei->getOffset(),
  111. ei->getType(),
  112. ei->getSemantic(),
  113. ei->getIndex() );
  114. }
  115. return dest;
  116. }
  117. void VertexData::convertPackedColour(
  118. VertexElementType srcType, VertexElementType destType)
  119. {
  120. if (destType != VET_COLOR_ABGR && destType != VET_COLOR_ARGB)
  121. {
  122. CM_EXCEPT(InvalidParametersException,
  123. "Invalid destType parameter");
  124. }
  125. if (srcType != VET_COLOR_ABGR && srcType != VET_COLOR_ARGB)
  126. {
  127. CM_EXCEPT(InvalidParametersException,
  128. "Invalid srcType parameter");
  129. }
  130. for (auto iter = mVertexBuffers.begin(); iter != mVertexBuffers.end(); ++iter)
  131. {
  132. VertexDeclaration::VertexElementList elems =
  133. vertexDeclaration->findElementsBySource(iter->first);
  134. bool conversionNeeded = false;
  135. VertexDeclaration::VertexElementList::iterator elemi;
  136. for (elemi = elems.begin(); elemi != elems.end(); ++elemi)
  137. {
  138. VertexElement& elem = *elemi;
  139. if (elem.getType() == VET_COLOR ||
  140. ((elem.getType() == VET_COLOR_ABGR || elem.getType() == VET_COLOR_ARGB)
  141. && elem.getType() != destType))
  142. {
  143. conversionNeeded = true;
  144. }
  145. }
  146. if (conversionNeeded)
  147. {
  148. void* pBase = iter->second->lock(GBL_READ_WRITE);
  149. for (UINT32 v = 0; v < iter->second->getNumVertices(); ++v)
  150. {
  151. for (elemi = elems.begin(); elemi != elems.end(); ++elemi)
  152. {
  153. VertexElement& elem = *elemi;
  154. VertexElementType currType = (elem.getType() == VET_COLOR) ?
  155. srcType : elem.getType();
  156. if (elem.getType() == VET_COLOR ||
  157. ((elem.getType() == VET_COLOR_ABGR || elem.getType() == VET_COLOR_ARGB)
  158. && elem.getType() != destType))
  159. {
  160. UINT32* pRGBA;
  161. elem.baseVertexPointerToElement(pBase, &pRGBA);
  162. VertexElement::convertColourValue(currType, destType, pRGBA);
  163. }
  164. }
  165. pBase = static_cast<void*>(
  166. static_cast<char*>(pBase) + iter->second->getVertexSize());
  167. }
  168. iter->second->unlock();
  169. // Modify the elements to reflect the changed type
  170. const VertexDeclaration::VertexElementList& allelems =
  171. vertexDeclaration->getElements();
  172. VertexDeclaration::VertexElementList::const_iterator ai;
  173. unsigned short elemIndex = 0;
  174. for (ai = allelems.begin(); ai != allelems.end(); ++ai, ++elemIndex)
  175. {
  176. const VertexElement& elem = *ai;
  177. if (elem.getType() == VET_COLOR ||
  178. ((elem.getType() == VET_COLOR_ABGR || elem.getType() == VET_COLOR_ARGB)
  179. && elem.getType() != destType))
  180. {
  181. vertexDeclaration->modifyElement(elemIndex,
  182. elem.getSource(), elem.getOffset(), destType,
  183. elem.getSemantic(), elem.getIndex());
  184. }
  185. }
  186. }
  187. } // each buffer
  188. }
  189. void VertexCacheProfiler::profile(const IndexBufferPtr& indexBuffer)
  190. {
  191. if (indexBuffer->isLocked()) return;
  192. UINT16 *shortbuffer = (UINT16 *)indexBuffer->lock(GBL_READ_ONLY);
  193. if (indexBuffer->getType() == IndexBuffer::IT_16BIT)
  194. for (unsigned int i = 0; i < indexBuffer->getNumIndexes(); ++i)
  195. inCache(shortbuffer[i]);
  196. else
  197. {
  198. UINT32 *buffer = (UINT32 *)shortbuffer;
  199. for (unsigned int i = 0; i < indexBuffer->getNumIndexes(); ++i)
  200. inCache(buffer[i]);
  201. }
  202. indexBuffer->unlock();
  203. }
  204. bool VertexCacheProfiler::inCache(unsigned int index)
  205. {
  206. for (unsigned int i = 0; i < buffersize; ++i)
  207. {
  208. if (index == cache[i])
  209. {
  210. hit++;
  211. return true;
  212. }
  213. }
  214. miss++;
  215. cache[tail++] = index;
  216. tail %= size;
  217. if (buffersize < size) buffersize++;
  218. return false;
  219. }
  220. }