CmVertexData.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 CamelotFramework
  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. void VertexData::convertPackedColour(
  75. VertexElementType srcType, VertexElementType destType)
  76. {
  77. if (destType != VET_COLOR_ABGR && destType != VET_COLOR_ARGB)
  78. {
  79. CM_EXCEPT(InvalidParametersException,
  80. "Invalid destType parameter");
  81. }
  82. if (srcType != VET_COLOR_ABGR && srcType != VET_COLOR_ARGB)
  83. {
  84. CM_EXCEPT(InvalidParametersException,
  85. "Invalid srcType parameter");
  86. }
  87. for (auto iter = mVertexBuffers.begin(); iter != mVertexBuffers.end(); ++iter)
  88. {
  89. VertexDeclaration::VertexElementList elems =
  90. vertexDeclaration->findElementsBySource(iter->first);
  91. bool conversionNeeded = false;
  92. VertexDeclaration::VertexElementList::iterator elemi;
  93. for (elemi = elems.begin(); elemi != elems.end(); ++elemi)
  94. {
  95. VertexElement& elem = *elemi;
  96. if (elem.getType() == VET_COLOR ||
  97. ((elem.getType() == VET_COLOR_ABGR || elem.getType() == VET_COLOR_ARGB)
  98. && elem.getType() != destType))
  99. {
  100. conversionNeeded = true;
  101. }
  102. }
  103. if (conversionNeeded)
  104. {
  105. void* pBase = iter->second->lock(GBL_READ_WRITE);
  106. for (UINT32 v = 0; v < iter->second->getNumVertices(); ++v)
  107. {
  108. for (elemi = elems.begin(); elemi != elems.end(); ++elemi)
  109. {
  110. VertexElement& elem = *elemi;
  111. VertexElementType currType = (elem.getType() == VET_COLOR) ?
  112. srcType : elem.getType();
  113. if (elem.getType() == VET_COLOR ||
  114. ((elem.getType() == VET_COLOR_ABGR || elem.getType() == VET_COLOR_ARGB)
  115. && elem.getType() != destType))
  116. {
  117. UINT32* pRGBA;
  118. elem.baseVertexPointerToElement(pBase, &pRGBA);
  119. VertexElement::convertColourValue(currType, destType, pRGBA);
  120. }
  121. }
  122. pBase = static_cast<void*>(
  123. static_cast<char*>(pBase) + iter->second->getVertexSize());
  124. }
  125. iter->second->unlock();
  126. // Modify the elements to reflect the changed type
  127. const VertexDeclaration::VertexElementList& allelems =
  128. vertexDeclaration->getElements();
  129. VertexDeclaration::VertexElementList::const_iterator ai;
  130. unsigned short elemIndex = 0;
  131. for (ai = allelems.begin(); ai != allelems.end(); ++ai, ++elemIndex)
  132. {
  133. const VertexElement& elem = *ai;
  134. if (elem.getType() == VET_COLOR ||
  135. ((elem.getType() == VET_COLOR_ABGR || elem.getType() == VET_COLOR_ARGB)
  136. && elem.getType() != destType))
  137. {
  138. vertexDeclaration->modifyElement(elemIndex,
  139. elem.getStreamIdx(), elem.getOffset(), destType,
  140. elem.getSemantic(), elem.getSemanticIdx());
  141. }
  142. }
  143. }
  144. } // each buffer
  145. }
  146. void VertexCacheProfiler::profile(const IndexBufferPtr& indexBuffer)
  147. {
  148. if (indexBuffer->isLocked()) return;
  149. UINT16 *shortbuffer = (UINT16 *)indexBuffer->lock(GBL_READ_ONLY);
  150. if (indexBuffer->getType() == IndexBuffer::IT_16BIT)
  151. for (unsigned int i = 0; i < indexBuffer->getNumIndexes(); ++i)
  152. inCache(shortbuffer[i]);
  153. else
  154. {
  155. UINT32 *buffer = (UINT32 *)shortbuffer;
  156. for (unsigned int i = 0; i < indexBuffer->getNumIndexes(); ++i)
  157. inCache(buffer[i]);
  158. }
  159. indexBuffer->unlock();
  160. }
  161. bool VertexCacheProfiler::inCache(unsigned int index)
  162. {
  163. for (unsigned int i = 0; i < buffersize; ++i)
  164. {
  165. if (index == cache[i])
  166. {
  167. hit++;
  168. return true;
  169. }
  170. }
  171. miss++;
  172. cache[tail++] = index;
  173. tail %= size;
  174. if (buffersize < size) buffersize++;
  175. return false;
  176. }
  177. }