CmMesh.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #include "CmMesh.h"
  2. #include "CmMeshRTTI.h"
  3. #include "CmMeshData.h"
  4. #include "CmVector2.h"
  5. #include "CmVector3.h"
  6. #include "CmDebug.h"
  7. #include "CmHardwareBufferManager.h"
  8. #include "CmMeshManager.h"
  9. #include "CmCoreThread.h"
  10. #include "CmAsyncOp.h"
  11. #include "CmAABox.h"
  12. #include "CmVertexDataDesc.h"
  13. #include "CmProfiler.h"
  14. namespace CamelotFramework
  15. {
  16. Mesh::Mesh(UINT32 numVertices, UINT32 numIndices, const VertexDataDescPtr& vertexDesc,
  17. MeshBufferType bufferType, IndexBuffer::IndexType indexType)
  18. :mVertexData(nullptr), mIndexData(nullptr), mNumVertices(numVertices), mNumIndices(numIndices),
  19. mVertexDesc(vertexDesc), mBufferType(bufferType), mIndexType(indexType), mNumSubMeshes(0)
  20. { }
  21. Mesh::Mesh(UINT32 numVertices, UINT32 numIndices, const VertexDataDescPtr& vertexDesc,
  22. const MeshDataPtr& initialMeshData, MeshBufferType bufferType, IndexBuffer::IndexType indexType)
  23. :mVertexData(nullptr), mIndexData(nullptr), mNumVertices(numVertices), mNumIndices(numIndices),
  24. mVertexDesc(vertexDesc), mBufferType(bufferType), mIndexType(indexType), mTempInitialMeshData(initialMeshData), mNumSubMeshes(0)
  25. { }
  26. Mesh::Mesh(const MeshDataPtr& initialMeshData, MeshBufferType bufferType)
  27. :mVertexData(nullptr), mIndexData(nullptr), mNumVertices(initialMeshData->getNumVertices()),
  28. mNumIndices(initialMeshData->getNumIndices()), mBufferType(bufferType), mIndexType(initialMeshData->getIndexType()),
  29. mVertexDesc(initialMeshData->getVertexDesc()), mTempInitialMeshData(initialMeshData), mNumSubMeshes(0)
  30. { }
  31. Mesh::Mesh()
  32. :mVertexData(nullptr), mIndexData(nullptr), mNumVertices(0), mNumIndices(0),
  33. mBufferType(MeshBufferType::Static), mIndexType(IndexBuffer::IT_32BIT), mNumSubMeshes(0)
  34. { }
  35. Mesh::~Mesh()
  36. {
  37. }
  38. void Mesh::writeSubresource(UINT32 subresourceIdx, const GpuResourceData& data, bool discardEntireBuffer)
  39. {
  40. THROW_IF_NOT_CORE_THREAD;
  41. if(data.getTypeId() != TID_MeshData)
  42. CM_EXCEPT(InvalidParametersException, "Invalid GpuResourceData type. Only MeshData is supported.");
  43. if(discardEntireBuffer)
  44. {
  45. if(mBufferType == MeshBufferType::Static)
  46. {
  47. LOGWRN("Buffer discard is enabled but buffer was not created as dynamic. Disabling discard.");
  48. discardEntireBuffer = false;
  49. }
  50. }
  51. else
  52. {
  53. if(mBufferType == MeshBufferType::Dynamic)
  54. {
  55. LOGWRN("Buffer discard is not enabled but buffer was created as dynamic. Enabling discard.");
  56. discardEntireBuffer = true;
  57. }
  58. }
  59. const MeshData& meshData = static_cast<const MeshData&>(data);
  60. // Indices
  61. UINT32 indexOffset = meshData.getResourceIndexOffset() * meshData.getIndexElementSize();
  62. UINT32 indicesSize = meshData.getIndexBufferSize();
  63. UINT8* srcIdxData = meshData.getIndexData();
  64. if((indexOffset + indicesSize) > mIndexData->indexBuffer->getSizeInBytes())
  65. CM_EXCEPT(InvalidParametersException, "Index buffer values are being written out of valid range.");
  66. mIndexData->indexBuffer->writeData(indexOffset, indicesSize, srcIdxData, discardEntireBuffer);
  67. // Vertices
  68. for(UINT32 i = 0; i <= meshData.getVertexDesc()->getMaxStreamIdx(); i++)
  69. {
  70. if(!meshData.getVertexDesc()->hasStream(i))
  71. continue;
  72. if(i >= mVertexData->getBufferCount())
  73. CM_EXCEPT(InvalidParametersException, "Attempting to write to a vertex stream that doesn't exist on this mesh.");
  74. VertexBufferPtr vertexBuffer = mVertexData->getBuffer(i);
  75. UINT32 bufferOffset = meshData.getResourceVertexOffset() * meshData.getVertexDesc()->getVertexStride(i);
  76. UINT32 bufferSize = meshData.getStreamSize(i);
  77. UINT8* srcVertBufferData = meshData.getStreamData(i);
  78. if((bufferOffset + bufferSize) > vertexBuffer->getSizeInBytes())
  79. CM_EXCEPT(InvalidParametersException, "Vertex buffer values for stream \"" + toString(i) + "\" are being written out of valid range.");
  80. if(vertexBuffer->vertexColorReqRGBFlip())
  81. {
  82. UINT8* bufferCopy = (UINT8*)cm_alloc(bufferSize);
  83. memcpy(bufferCopy, srcVertBufferData, bufferSize); // TODO Low priority - Attempt to avoid this copy
  84. UINT32 vertexStride = meshData.getVertexDesc()->getVertexStride(i);
  85. for(INT32 semanticIdx = 0; semanticIdx < VertexBuffer::MAX_SEMANTIC_IDX; semanticIdx++)
  86. {
  87. if(!meshData.getVertexDesc()->hasElement(VES_COLOR, semanticIdx, i))
  88. continue;
  89. UINT8* colorData = bufferCopy + meshData.getElementOffset(VES_COLOR, semanticIdx, i);
  90. for(UINT32 j = 0; j < mVertexData->vertexCount; j++)
  91. {
  92. UINT32* curColor = (UINT32*)colorData;
  93. (*curColor) = ((*curColor) & 0xFF00FF00) | ((*curColor >> 16) & 0x000000FF) | ((*curColor << 16) & 0x00FF0000);
  94. colorData += vertexStride;
  95. }
  96. }
  97. vertexBuffer->writeData(bufferOffset, bufferSize, bufferCopy, discardEntireBuffer);
  98. cm_free(bufferCopy);
  99. }
  100. else
  101. {
  102. vertexBuffer->writeData(bufferOffset, bufferSize, srcVertBufferData, discardEntireBuffer);
  103. }
  104. }
  105. // Submeshes
  106. mSubMeshes.clear();
  107. if(meshData.getNumSubmeshes() > 0)
  108. {
  109. for(UINT32 i = 0; i < meshData.getNumSubmeshes(); i++)
  110. {
  111. UINT32 numIndices = meshData.getNumIndices(i);
  112. if(numIndices > 0)
  113. {
  114. mSubMeshes.push_back(SubMesh(meshData.getIndexBufferOffset(i), numIndices, meshData.getDrawOp(i), mVertexData, mIndexData, true));
  115. }
  116. }
  117. }
  118. else // Read it all as one mesh
  119. {
  120. UINT32 numIndices = meshData.getNumIndices();
  121. if(numIndices > 0)
  122. {
  123. mSubMeshes.push_back(SubMesh(0, numIndices, meshData.getDrawOp(), mVertexData, mIndexData, true));
  124. }
  125. }
  126. mNumSubMeshes.store((UINT32)mSubMeshes.size());
  127. }
  128. void Mesh::readSubresource(UINT32 subresourceIdx, GpuResourceData& data)
  129. {
  130. THROW_IF_NOT_CORE_THREAD;
  131. if(data.getTypeId() != TID_MeshData)
  132. CM_EXCEPT(InvalidParametersException, "Invalid GpuResourceData type. Only MeshData is supported.");
  133. IndexBuffer::IndexType indexType = IndexBuffer::IT_32BIT;
  134. if(mIndexData)
  135. indexType = mIndexData->indexBuffer->getType();
  136. MeshData& meshData = static_cast<MeshData&>(data);
  137. if(mIndexData)
  138. {
  139. UINT8* idxData = static_cast<UINT8*>(mIndexData->indexBuffer->lock(GBL_READ_ONLY));
  140. UINT32 idxElemSize = mIndexData->indexBuffer->getIndexSize();
  141. UINT32 indexResourceOffset = meshData.getResourceIndexOffset() * meshData.getIndexElementSize();
  142. for(UINT32 i = 0; i < mSubMeshes.size(); i++)
  143. {
  144. UINT8* indices = nullptr;
  145. if(indexType == IndexBuffer::IT_16BIT)
  146. indices = (UINT8*)meshData.getIndices16(i);
  147. else
  148. indices = (UINT8*)meshData.getIndices32(i);
  149. UINT32 indicesSize = mSubMeshes[i].indexCount * idxElemSize;
  150. UINT32 indicesOffset = meshData.getIndexBufferOffset(i) + indexResourceOffset;
  151. if((indicesOffset + indicesSize) > meshData.getIndexBufferSize())
  152. CM_EXCEPT(InvalidParametersException, "Provided buffer doesn't have enough space to store mesh indices.");
  153. indices += indexResourceOffset;
  154. memcpy(indices, &idxData[mSubMeshes[i].indexOffset * idxElemSize], mSubMeshes[i].indexCount * idxElemSize);
  155. }
  156. mIndexData->indexBuffer->unlock();
  157. }
  158. if(mVertexData)
  159. {
  160. auto vertexBuffers = mVertexData->getBuffers();
  161. UINT32 streamIdx = 0;
  162. for(auto iter = vertexBuffers.begin(); iter != vertexBuffers.end() ; ++iter)
  163. {
  164. if(streamIdx > meshData.getVertexDesc()->getMaxStreamIdx())
  165. continue;
  166. VertexBufferPtr vertexBuffer = iter->second;
  167. UINT32 bufferSize = vertexBuffer->getVertexSize() * vertexBuffer->getNumVertices();
  168. UINT32 vertexResourceOffset = meshData.getResourceVertexOffset() * meshData.getVertexDesc()->getVertexStride(streamIdx);
  169. UINT32 vertexOffset = meshData.getStreamOffset(streamIdx) + vertexResourceOffset;
  170. UINT8* vertDataPtr = static_cast<UINT8*>(vertexBuffer->lock(GBL_READ_ONLY));
  171. if((vertexOffset + bufferSize) > meshData.getStreamSize(streamIdx))
  172. CM_EXCEPT(InvalidParametersException, "Provided buffer doesn't have enough space to store mesh vertices.");
  173. UINT8* dest = meshData.getStreamData(streamIdx) + vertexResourceOffset;
  174. memcpy(dest, vertDataPtr, bufferSize);
  175. vertexBuffer->unlock();
  176. streamIdx++;
  177. }
  178. }
  179. }
  180. MeshDataPtr Mesh::allocateSubresourceBuffer(UINT32 subresourceIdx) const
  181. {
  182. IndexBuffer::IndexType indexType = IndexBuffer::IT_32BIT;
  183. if(mIndexData)
  184. indexType = mIndexData->indexBuffer->getType();
  185. UINT32 numIndices = 0;
  186. if(mIndexData)
  187. {
  188. for(UINT32 i = 0; i < mSubMeshes.size(); i++)
  189. numIndices += mSubMeshes[i].indexCount;
  190. }
  191. MeshDataPtr meshData = cm_shared_ptr<MeshData>(mVertexData->vertexCount, numIndices, mVertexDesc, DOT_TRIANGLE_LIST, indexType);
  192. if(mIndexData)
  193. {
  194. for(UINT32 i = 0; i < mSubMeshes.size(); i++)
  195. meshData->addSubMesh(mSubMeshes[i].indexCount, i);
  196. }
  197. return meshData;
  198. }
  199. const SubMesh& Mesh::getSubMesh(UINT32 subMeshIdx) const
  200. {
  201. THROW_IF_NOT_CORE_THREAD;
  202. if(subMeshIdx < 0 || subMeshIdx >= mSubMeshes.size())
  203. {
  204. CM_EXCEPT(InvalidParametersException, "Invalid sub-mesh index ("
  205. + toString(subMeshIdx) + "). Number of sub-meshes available: " + toString((int)mSubMeshes.size()));
  206. }
  207. return mSubMeshes[subMeshIdx];
  208. }
  209. const AABox& Mesh::getBounds() const
  210. {
  211. // TODO - Retrieve bounds for entire mesh (need to calculate them during creation)
  212. return AABox::BOX_EMPTY;
  213. }
  214. const AABox& Mesh::getBounds(UINT32 submeshIdx) const
  215. {
  216. // TODO - Retrieve bounds a specific sub-mesh (need to calculate them during creation)
  217. return AABox::BOX_EMPTY;
  218. }
  219. void Mesh::initialize_internal()
  220. {
  221. THROW_IF_NOT_CORE_THREAD;
  222. mIndexData = std::shared_ptr<IndexData>(cm_new<IndexData, PoolAlloc>());
  223. mIndexData->indexCount = mNumIndices;
  224. mIndexData->indexBuffer = HardwareBufferManager::instance().createIndexBuffer(
  225. mIndexType,
  226. mIndexData->indexCount,
  227. mBufferType == MeshBufferType::Dynamic ? GBU_DYNAMIC : GBU_STATIC);
  228. mVertexData = std::shared_ptr<VertexData>(cm_new<VertexData, PoolAlloc>());
  229. mVertexData->vertexCount = mNumVertices;
  230. mVertexData->vertexDeclaration = mVertexDesc->createDeclaration();
  231. for(UINT32 i = 0; i <= mVertexDesc->getMaxStreamIdx(); i++)
  232. {
  233. if(!mVertexDesc->hasStream(i))
  234. continue;
  235. VertexBufferPtr vertexBuffer = HardwareBufferManager::instance().createVertexBuffer(
  236. mVertexData->vertexDeclaration->getVertexSize(i),
  237. mVertexData->vertexCount,
  238. mBufferType == MeshBufferType::Dynamic ? GBU_DYNAMIC : GBU_STATIC);
  239. mVertexData->setBuffer(i, vertexBuffer);
  240. }
  241. // TODO Low priority - DX11 (and maybe OpenGL)? allow an optimization that allows you to set
  242. // buffer data upon buffer construction, instead of setting it in a second step like I do here
  243. if(mTempInitialMeshData != nullptr)
  244. {
  245. writeSubresource(0, *mTempInitialMeshData, mBufferType == MeshBufferType::Dynamic);
  246. mTempInitialMeshData = nullptr;
  247. }
  248. Resource::initialize_internal();
  249. }
  250. void Mesh::destroy_internal()
  251. {
  252. THROW_IF_NOT_CORE_THREAD;
  253. Resource::destroy_internal();
  254. }
  255. HMesh Mesh::dummy()
  256. {
  257. return MeshManager::instance().getDummyMesh();
  258. }
  259. /************************************************************************/
  260. /* SERIALIZATION */
  261. /************************************************************************/
  262. RTTITypeBase* Mesh::getRTTIStatic()
  263. {
  264. return MeshRTTI::instance();
  265. }
  266. RTTITypeBase* Mesh::getRTTI() const
  267. {
  268. return Mesh::getRTTIStatic();
  269. }
  270. /************************************************************************/
  271. /* STATICS */
  272. /************************************************************************/
  273. HMesh Mesh::create(UINT32 numVertices, UINT32 numIndices, const VertexDataDescPtr& vertexDesc, MeshBufferType bufferType, IndexBuffer::IndexType indexType)
  274. {
  275. MeshPtr meshPtr = MeshManager::instance().create(numVertices, numIndices, vertexDesc, bufferType, indexType);
  276. return static_resource_cast<Mesh>(Resource::_createResourceHandle(meshPtr));
  277. }
  278. HMesh Mesh::create(UINT32 numVertices, UINT32 numIndices, const VertexDataDescPtr& vertexDesc, const MeshDataPtr& initialMeshData, MeshBufferType bufferType, IndexBuffer::IndexType indexType)
  279. {
  280. MeshPtr meshPtr = MeshManager::instance().create(numVertices, numIndices, vertexDesc, initialMeshData, bufferType, indexType);
  281. return static_resource_cast<Mesh>(Resource::_createResourceHandle(meshPtr));
  282. }
  283. HMesh Mesh::create(const MeshDataPtr& initialMeshData, MeshBufferType bufferType)
  284. {
  285. MeshPtr meshPtr = MeshManager::instance().create(initialMeshData, bufferType);
  286. return static_resource_cast<Mesh>(Resource::_createResourceHandle(meshPtr));
  287. }
  288. }