BsMesh.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsMesh.h"
  4. #include "BsMeshRTTI.h"
  5. #include "BsMeshData.h"
  6. #include "BsDebug.h"
  7. #include "BsHardwareBufferManager.h"
  8. #include "BsMeshManager.h"
  9. #include "BsCoreThread.h"
  10. #include "BsAsyncOp.h"
  11. #include "BsVertexDataDesc.h"
  12. #include "BsResources.h"
  13. #include "BsRenderAPI.h"
  14. namespace bs
  15. {
  16. MESH_DESC MESH_DESC::DEFAULT = MESH_DESC();
  17. MeshCore::MeshCore(const SPtr<MeshData>& initialMeshData, const MESH_DESC& desc, GpuDeviceFlags deviceMask)
  18. : MeshCoreBase(desc.numVertices, desc.numIndices, desc.subMeshes), mVertexData(nullptr), mIndexBuffer(nullptr)
  19. , mVertexDesc(desc.vertexDesc), mUsage(desc.usage), mIndexType(desc.indexType), mDeviceMask(deviceMask)
  20. , mTempInitialMeshData(initialMeshData), mSkeleton(desc.skeleton), mMorphShapes(desc.morphShapes)
  21. { }
  22. MeshCore::~MeshCore()
  23. {
  24. THROW_IF_NOT_CORE_THREAD;
  25. mVertexData = nullptr;
  26. mIndexBuffer = nullptr;
  27. mVertexDesc = nullptr;
  28. mTempInitialMeshData = nullptr;
  29. }
  30. void MeshCore::initialize()
  31. {
  32. THROW_IF_NOT_CORE_THREAD;
  33. bool isDynamic = (mUsage & MU_DYNAMIC) != 0;
  34. int usage = isDynamic ? GBU_DYNAMIC : GBU_STATIC;
  35. INDEX_BUFFER_DESC ibDesc;
  36. ibDesc.indexType = mIndexType;
  37. ibDesc.numIndices = mProperties.mNumIndices;
  38. ibDesc.usage = (GpuBufferUsage)usage;
  39. mIndexBuffer = IndexBufferCore::create(ibDesc, mDeviceMask);
  40. mVertexData = SPtr<VertexData>(bs_new<VertexData>());
  41. mVertexData->vertexCount = mProperties.mNumVertices;
  42. mVertexData->vertexDeclaration = VertexDeclarationCore::create(mVertexDesc, mDeviceMask);
  43. for (UINT32 i = 0; i <= mVertexDesc->getMaxStreamIdx(); i++)
  44. {
  45. if (!mVertexDesc->hasStream(i))
  46. continue;
  47. VERTEX_BUFFER_DESC vbDesc;
  48. vbDesc.vertexSize = mVertexData->vertexDeclaration->getProperties().getVertexSize(i);
  49. vbDesc.numVerts = mVertexData->vertexCount;
  50. vbDesc.usage = (GpuBufferUsage)usage;
  51. SPtr<VertexBufferCore> vertexBuffer = VertexBufferCore::create(vbDesc, mDeviceMask);
  52. mVertexData->setBuffer(i, vertexBuffer);
  53. }
  54. // TODO Low priority - DX11 (and maybe OpenGL)? allow an optimization that allows you to set
  55. // buffer data upon buffer construction, instead of setting it in a second step like I do here
  56. if (mTempInitialMeshData != nullptr)
  57. {
  58. writeData(*mTempInitialMeshData, isDynamic);
  59. mTempInitialMeshData = nullptr;
  60. }
  61. MeshCoreBase::initialize();
  62. }
  63. SPtr<VertexData> MeshCore::getVertexData() const
  64. {
  65. THROW_IF_NOT_CORE_THREAD;
  66. return mVertexData;
  67. }
  68. SPtr<IndexBufferCore> MeshCore::getIndexBuffer() const
  69. {
  70. THROW_IF_NOT_CORE_THREAD;
  71. return mIndexBuffer;
  72. }
  73. SPtr<VertexDataDesc> MeshCore::getVertexDesc() const
  74. {
  75. THROW_IF_NOT_CORE_THREAD;
  76. return mVertexDesc;
  77. }
  78. void MeshCore::writeData(const MeshData& meshData, bool discardEntireBuffer, bool performUpdateBounds, UINT32 queueIdx)
  79. {
  80. THROW_IF_NOT_CORE_THREAD;
  81. if (discardEntireBuffer)
  82. {
  83. if ((mUsage & MU_STATIC) != 0)
  84. {
  85. LOGWRN("Buffer discard is enabled but buffer was not created as dynamic. Disabling discard.");
  86. discardEntireBuffer = false;
  87. }
  88. }
  89. else
  90. {
  91. if ((mUsage & MU_DYNAMIC) != 0)
  92. {
  93. LOGWRN("Buffer discard is not enabled but buffer was created as dynamic. Enabling discard.");
  94. discardEntireBuffer = true;
  95. }
  96. }
  97. // Indices
  98. const IndexBufferProperties& ibProps = mIndexBuffer->getProperties();
  99. UINT32 indicesSize = meshData.getIndexBufferSize();
  100. UINT8* srcIdxData = meshData.getIndexData();
  101. if (meshData.getIndexElementSize() != ibProps.getIndexSize())
  102. {
  103. LOGERR("Provided index size doesn't match meshes index size. Needed: " +
  104. toString(ibProps.getIndexSize()) + ". Got: " + toString(meshData.getIndexElementSize()));
  105. return;
  106. }
  107. if (indicesSize > mIndexBuffer->getSize())
  108. {
  109. indicesSize = mIndexBuffer->getSize();
  110. LOGERR("Index buffer values are being written out of valid range.");
  111. }
  112. mIndexBuffer->writeData(0, indicesSize, srcIdxData, discardEntireBuffer ? BWT_DISCARD : BWT_NORMAL, queueIdx);
  113. // Vertices
  114. for (UINT32 i = 0; i <= mVertexDesc->getMaxStreamIdx(); i++)
  115. {
  116. if (!mVertexDesc->hasStream(i))
  117. continue;
  118. if (!meshData.getVertexDesc()->hasStream(i))
  119. continue;
  120. // Ensure both have the same sized vertices
  121. UINT32 myVertSize = mVertexDesc->getVertexStride(i);
  122. UINT32 otherVertSize = meshData.getVertexDesc()->getVertexStride(i);
  123. if (myVertSize != otherVertSize)
  124. {
  125. LOGERR("Provided vertex size for stream " + toString(i) + " doesn't match meshes vertex size. Needed: " +
  126. toString(myVertSize) + ". Got: " + toString(otherVertSize));
  127. continue;
  128. }
  129. SPtr<VertexBufferCore> vertexBuffer = mVertexData->getBuffer(i);
  130. UINT32 bufferSize = meshData.getStreamSize(i);
  131. UINT8* srcVertBufferData = meshData.getStreamData(i);
  132. if (bufferSize > vertexBuffer->getSize())
  133. {
  134. bufferSize = vertexBuffer->getSize();
  135. LOGERR("Vertex buffer values for stream \"" + toString(i) + "\" are being written out of valid range.");
  136. }
  137. if (RenderAPICore::instance().getAPIInfo().getVertexColorFlipRequired())
  138. {
  139. UINT8* bufferCopy = (UINT8*)bs_alloc(bufferSize);
  140. memcpy(bufferCopy, srcVertBufferData, bufferSize); // TODO Low priority - Attempt to avoid this copy
  141. UINT32 vertexStride = meshData.getVertexDesc()->getVertexStride(i);
  142. for (INT32 semanticIdx = 0; semanticIdx < VertexBuffer::MAX_SEMANTIC_IDX; semanticIdx++)
  143. {
  144. if (!meshData.getVertexDesc()->hasElement(VES_COLOR, semanticIdx, i))
  145. continue;
  146. UINT8* colorData = bufferCopy + mVertexDesc->getElementOffsetFromStream(VES_COLOR, semanticIdx, i);
  147. for (UINT32 j = 0; j < mVertexData->vertexCount; j++)
  148. {
  149. UINT32* curColor = (UINT32*)colorData;
  150. (*curColor) = ((*curColor) & 0xFF00FF00) | ((*curColor >> 16) & 0x000000FF) | ((*curColor << 16) & 0x00FF0000);
  151. colorData += vertexStride;
  152. }
  153. }
  154. vertexBuffer->writeData(0, bufferSize, bufferCopy, discardEntireBuffer ? BWT_DISCARD : BWT_NORMAL, queueIdx);
  155. bs_free(bufferCopy);
  156. }
  157. else
  158. {
  159. vertexBuffer->writeData(0, bufferSize, srcVertBufferData, discardEntireBuffer ? BWT_DISCARD : BWT_NORMAL,
  160. queueIdx);
  161. }
  162. }
  163. if (performUpdateBounds)
  164. updateBounds(meshData);
  165. }
  166. void MeshCore::readData(MeshData& meshData, UINT32 deviceIdx, UINT32 queueIdx)
  167. {
  168. THROW_IF_NOT_CORE_THREAD;
  169. IndexType indexType = IT_32BIT;
  170. if (mIndexBuffer)
  171. indexType = mIndexBuffer->getProperties().getType();
  172. if (mIndexBuffer)
  173. {
  174. const IndexBufferProperties& ibProps = mIndexBuffer->getProperties();
  175. if (meshData.getIndexElementSize() != ibProps.getIndexSize())
  176. {
  177. LOGERR("Provided index size doesn't match meshes index size. Needed: " +
  178. toString(ibProps.getIndexSize()) + ". Got: " + toString(meshData.getIndexElementSize()));
  179. return;
  180. }
  181. UINT8* idxData = static_cast<UINT8*>(mIndexBuffer->lock(GBL_READ_ONLY, deviceIdx, queueIdx));
  182. UINT32 idxElemSize = ibProps.getIndexSize();
  183. UINT8* indices = nullptr;
  184. if (indexType == IT_16BIT)
  185. indices = (UINT8*)meshData.getIndices16();
  186. else
  187. indices = (UINT8*)meshData.getIndices32();
  188. UINT32 numIndicesToCopy = std::min(mProperties.mNumIndices, meshData.getNumIndices());
  189. UINT32 indicesSize = numIndicesToCopy * idxElemSize;
  190. if (indicesSize > meshData.getIndexBufferSize())
  191. {
  192. LOGERR("Provided buffer doesn't have enough space to store mesh indices.");
  193. return;
  194. }
  195. memcpy(indices, idxData, numIndicesToCopy * idxElemSize);
  196. mIndexBuffer->unlock();
  197. }
  198. if (mVertexData)
  199. {
  200. auto vertexBuffers = mVertexData->getBuffers();
  201. UINT32 streamIdx = 0;
  202. for (auto iter = vertexBuffers.begin(); iter != vertexBuffers.end(); ++iter)
  203. {
  204. if (!meshData.getVertexDesc()->hasStream(streamIdx))
  205. continue;
  206. SPtr<VertexBufferCore> vertexBuffer = iter->second;
  207. const VertexBufferProperties& vbProps = vertexBuffer->getProperties();
  208. // Ensure both have the same sized vertices
  209. UINT32 myVertSize = mVertexDesc->getVertexStride(streamIdx);
  210. UINT32 otherVertSize = meshData.getVertexDesc()->getVertexStride(streamIdx);
  211. if (myVertSize != otherVertSize)
  212. {
  213. LOGERR("Provided vertex size for stream " + toString(streamIdx) + " doesn't match meshes vertex size. Needed: " +
  214. toString(myVertSize) + ". Got: " + toString(otherVertSize));
  215. continue;
  216. }
  217. UINT32 numVerticesToCopy = meshData.getNumVertices();
  218. UINT32 bufferSize = vbProps.getVertexSize() * numVerticesToCopy;
  219. if (bufferSize > vertexBuffer->getSize())
  220. {
  221. LOGERR("Vertex buffer values for stream \"" + toString(streamIdx) + "\" are being read out of valid range.");
  222. continue;
  223. }
  224. UINT8* vertDataPtr = static_cast<UINT8*>(vertexBuffer->lock(GBL_READ_ONLY, deviceIdx, queueIdx));
  225. UINT8* dest = meshData.getStreamData(streamIdx);
  226. memcpy(dest, vertDataPtr, bufferSize);
  227. vertexBuffer->unlock();
  228. streamIdx++;
  229. }
  230. }
  231. }
  232. void MeshCore::updateBounds(const MeshData& meshData)
  233. {
  234. mProperties.mBounds = meshData.calculateBounds();
  235. // TODO - Sync this to sim-thread possibly?
  236. }
  237. SPtr<MeshCore> MeshCore::create(UINT32 numVertices, UINT32 numIndices, const SPtr<VertexDataDesc>& vertexDesc,
  238. int usage, DrawOperationType drawOp, IndexType indexType, GpuDeviceFlags deviceMask)
  239. {
  240. MESH_DESC desc;
  241. desc.numVertices = numVertices;
  242. desc.numIndices = numIndices;
  243. desc.vertexDesc = vertexDesc;
  244. desc.subMeshes.push_back(SubMesh(0, numIndices, drawOp));
  245. desc.usage = usage;
  246. desc.indexType = indexType;
  247. SPtr<MeshCore> mesh = bs_shared_ptr<MeshCore>(new (bs_alloc<MeshCore>()) MeshCore(nullptr, desc, deviceMask));
  248. mesh->_setThisPtr(mesh);
  249. mesh->initialize();
  250. return mesh;
  251. }
  252. SPtr<MeshCore> MeshCore::create(const MESH_DESC& desc, GpuDeviceFlags deviceMask)
  253. {
  254. SPtr<MeshCore> mesh = bs_shared_ptr<MeshCore>(new (bs_alloc<MeshCore>()) MeshCore(nullptr, desc, deviceMask));
  255. mesh->_setThisPtr(mesh);
  256. mesh->initialize();
  257. return mesh;
  258. }
  259. SPtr<MeshCore> MeshCore::create(const SPtr<MeshData>& initialMeshData, const MESH_DESC& desc, GpuDeviceFlags deviceMask)
  260. {
  261. MESH_DESC descCopy = desc;
  262. descCopy.numVertices = initialMeshData->getNumVertices();
  263. descCopy.numIndices = initialMeshData->getNumIndices();
  264. descCopy.vertexDesc = initialMeshData->getVertexDesc();
  265. descCopy.indexType = initialMeshData->getIndexType();
  266. SPtr<MeshCore> mesh =
  267. bs_shared_ptr<MeshCore>(new (bs_alloc<MeshCore>()) MeshCore(initialMeshData, descCopy, deviceMask));
  268. mesh->_setThisPtr(mesh);
  269. mesh->initialize();
  270. return mesh;
  271. }
  272. SPtr<MeshCore> MeshCore::create(const SPtr<MeshData>& initialMeshData, int usage, DrawOperationType drawOp,
  273. GpuDeviceFlags deviceMask)
  274. {
  275. MESH_DESC desc;
  276. desc.numVertices = initialMeshData->getNumVertices();
  277. desc.numIndices = initialMeshData->getNumIndices();
  278. desc.vertexDesc = initialMeshData->getVertexDesc();
  279. desc.indexType = initialMeshData->getIndexType();
  280. desc.subMeshes.push_back(SubMesh(0, initialMeshData->getNumIndices(), drawOp));
  281. desc.usage = usage;
  282. SPtr<MeshCore> mesh =
  283. bs_shared_ptr<MeshCore>(new (bs_alloc<MeshCore>()) MeshCore(initialMeshData, desc, deviceMask));
  284. mesh->_setThisPtr(mesh);
  285. mesh->initialize();
  286. return mesh;
  287. }
  288. Mesh::Mesh(const MESH_DESC& desc)
  289. :MeshBase(desc.numVertices, desc.numIndices, desc.subMeshes), mVertexDesc(desc.vertexDesc), mUsage(desc.usage),
  290. mIndexType(desc.indexType), mSkeleton(desc.skeleton), mMorphShapes(desc.morphShapes)
  291. {
  292. }
  293. Mesh::Mesh(const SPtr<MeshData>& initialMeshData, const MESH_DESC& desc)
  294. :MeshBase(initialMeshData->getNumVertices(), initialMeshData->getNumIndices(), desc.subMeshes),
  295. mCPUData(initialMeshData), mVertexDesc(initialMeshData->getVertexDesc()),
  296. mUsage(desc.usage), mIndexType(initialMeshData->getIndexType()), mSkeleton(desc.skeleton),
  297. mMorphShapes(desc.morphShapes)
  298. {
  299. }
  300. Mesh::Mesh()
  301. :MeshBase(0, 0, DOT_TRIANGLE_LIST), mUsage(MU_STATIC), mIndexType(IT_32BIT)
  302. {
  303. }
  304. Mesh::~Mesh()
  305. {
  306. }
  307. AsyncOp Mesh::writeData(const SPtr<MeshData>& data, bool discardEntireBuffer)
  308. {
  309. updateBounds(*data);
  310. updateCPUBuffer(0, *data);
  311. data->_lock();
  312. std::function<void(const SPtr<MeshCore>&, const SPtr<MeshData>&, bool, AsyncOp&)> func =
  313. [&](const SPtr<MeshCore>& mesh, const SPtr<MeshData>& _meshData, bool _discardEntireBuffer, AsyncOp& asyncOp)
  314. {
  315. mesh->writeData(*_meshData, _discardEntireBuffer, false);
  316. _meshData->_unlock();
  317. asyncOp._completeOperation();
  318. };
  319. return gCoreThread().queueReturnCommand(std::bind(func, getCore(),
  320. data, discardEntireBuffer, std::placeholders::_1));
  321. }
  322. AsyncOp Mesh::readData(const SPtr<MeshData>& data)
  323. {
  324. data->_lock();
  325. std::function<void(const SPtr<MeshCore>&, const SPtr<MeshData>&, AsyncOp&)> func =
  326. [&](const SPtr<MeshCore>& mesh, const SPtr<MeshData>& _meshData, AsyncOp& asyncOp)
  327. {
  328. mesh->readData(*_meshData);
  329. _meshData->_unlock();
  330. asyncOp._completeOperation();
  331. };
  332. return gCoreThread().queueReturnCommand(std::bind(func, getCore(),
  333. data, std::placeholders::_1));
  334. }
  335. SPtr<MeshData> Mesh::allocBuffer() const
  336. {
  337. SPtr<MeshData> meshData = bs_shared_ptr_new<MeshData>(mProperties.mNumVertices, mProperties.mNumIndices,
  338. mVertexDesc, mIndexType);
  339. return meshData;
  340. }
  341. void Mesh::initialize()
  342. {
  343. if (mCPUData != nullptr)
  344. updateBounds(*mCPUData);
  345. MeshBase::initialize();
  346. if ((mUsage & MU_CPUCACHED) != 0 && mCPUData == nullptr)
  347. createCPUBuffer();
  348. }
  349. void Mesh::updateBounds(const MeshData& meshData)
  350. {
  351. mProperties.mBounds = meshData.calculateBounds();
  352. markCoreDirty();
  353. }
  354. SPtr<MeshCore> Mesh::getCore() const
  355. {
  356. return std::static_pointer_cast<MeshCore>(mCoreSpecific);
  357. }
  358. SPtr<CoreObjectCore> Mesh::createCore() const
  359. {
  360. MESH_DESC desc;
  361. desc.numVertices = mProperties.mNumVertices;
  362. desc.numIndices = mProperties.mNumIndices;
  363. desc.vertexDesc = mVertexDesc;
  364. desc.subMeshes = mProperties.mSubMeshes;
  365. desc.usage = mUsage;
  366. desc.indexType = mIndexType;
  367. desc.skeleton = mSkeleton;
  368. desc.morphShapes = mMorphShapes;
  369. MeshCore* obj = new (bs_alloc<MeshCore>()) MeshCore(mCPUData, desc, GDF_DEFAULT);
  370. SPtr<CoreObjectCore> meshCore = bs_shared_ptr<MeshCore>(obj);
  371. meshCore->_setThisPtr(meshCore);
  372. if ((mUsage & MU_CPUCACHED) == 0)
  373. mCPUData = nullptr;
  374. return meshCore;
  375. }
  376. void Mesh::updateCPUBuffer(UINT32 subresourceIdx, const MeshData& pixelData)
  377. {
  378. if ((mUsage & MU_CPUCACHED) == 0)
  379. return;
  380. if (subresourceIdx > 0)
  381. {
  382. LOGERR("Invalid subresource index: " + toString(subresourceIdx) + ". Supported range: 0 .. 1.");
  383. return;
  384. }
  385. if (pixelData.getNumIndices() != mProperties.getNumIndices() ||
  386. pixelData.getNumVertices() != mProperties.getNumVertices() ||
  387. pixelData.getIndexType() != mIndexType ||
  388. pixelData.getVertexDesc()->getVertexStride() != mVertexDesc->getVertexStride())
  389. {
  390. LOGERR("Provided buffer is not of valid dimensions or format in order to update this mesh.");
  391. return;
  392. }
  393. if (mCPUData->getSize() != pixelData.getSize())
  394. BS_EXCEPT(InternalErrorException, "Buffer sizes don't match.");
  395. UINT8* dest = mCPUData->getData();
  396. UINT8* src = pixelData.getData();
  397. memcpy(dest, src, pixelData.getSize());
  398. }
  399. void Mesh::readCachedData(MeshData& dest)
  400. {
  401. if ((mUsage & MU_CPUCACHED) == 0)
  402. {
  403. LOGERR("Attempting to read CPU data from a mesh that is created without CPU caching.");
  404. return;
  405. }
  406. if (dest.getNumIndices() != mProperties.getNumIndices() ||
  407. dest.getNumVertices() != mProperties.getNumVertices() ||
  408. dest.getIndexType() != mIndexType ||
  409. dest.getVertexDesc()->getVertexStride() != mVertexDesc->getVertexStride())
  410. {
  411. LOGERR("Provided buffer is not of valid dimensions or format in order to read from this mesh.");
  412. return;
  413. }
  414. if (mCPUData->getSize() != dest.getSize())
  415. BS_EXCEPT(InternalErrorException, "Buffer sizes don't match.");
  416. UINT8* srcPtr = mCPUData->getData();
  417. UINT8* destPtr = dest.getData();
  418. memcpy(destPtr, srcPtr, dest.getSize());
  419. }
  420. void Mesh::createCPUBuffer()
  421. {
  422. mCPUData = allocBuffer();
  423. }
  424. HMesh Mesh::dummy()
  425. {
  426. return MeshManager::instance().getDummyMesh();
  427. }
  428. /************************************************************************/
  429. /* SERIALIZATION */
  430. /************************************************************************/
  431. RTTITypeBase* Mesh::getRTTIStatic()
  432. {
  433. return MeshRTTI::instance();
  434. }
  435. RTTITypeBase* Mesh::getRTTI() const
  436. {
  437. return Mesh::getRTTIStatic();
  438. }
  439. /************************************************************************/
  440. /* STATICS */
  441. /************************************************************************/
  442. HMesh Mesh::create(UINT32 numVertices, UINT32 numIndices, const SPtr<VertexDataDesc>& vertexDesc,
  443. int usage, DrawOperationType drawOp, IndexType indexType)
  444. {
  445. MESH_DESC desc;
  446. desc.numVertices = numVertices;
  447. desc.numIndices = numIndices;
  448. desc.vertexDesc = vertexDesc;
  449. desc.usage = usage;
  450. desc.subMeshes.push_back(SubMesh(0, numIndices, drawOp));
  451. desc.indexType = indexType;
  452. SPtr<Mesh> meshPtr = _createPtr(desc);
  453. return static_resource_cast<Mesh>(gResources()._createResourceHandle(meshPtr));
  454. }
  455. HMesh Mesh::create(const MESH_DESC& desc)
  456. {
  457. SPtr<Mesh> meshPtr = _createPtr(desc);
  458. return static_resource_cast<Mesh>(gResources()._createResourceHandle(meshPtr));
  459. }
  460. HMesh Mesh::create(const SPtr<MeshData>& initialMeshData, const MESH_DESC& desc)
  461. {
  462. SPtr<Mesh> meshPtr = _createPtr(initialMeshData, desc);
  463. return static_resource_cast<Mesh>(gResources()._createResourceHandle(meshPtr));
  464. }
  465. HMesh Mesh::create(const SPtr<MeshData>& initialMeshData, int usage, DrawOperationType drawOp)
  466. {
  467. SPtr<Mesh> meshPtr = _createPtr(initialMeshData, usage, drawOp);
  468. return static_resource_cast<Mesh>(gResources()._createResourceHandle(meshPtr));
  469. }
  470. SPtr<Mesh> Mesh::_createPtr(const MESH_DESC& desc)
  471. {
  472. SPtr<Mesh> mesh = bs_core_ptr<Mesh>(new (bs_alloc<Mesh>()) Mesh(desc));
  473. mesh->_setThisPtr(mesh);
  474. mesh->initialize();
  475. return mesh;
  476. }
  477. SPtr<Mesh> Mesh::_createPtr(const SPtr<MeshData>& initialMeshData, const MESH_DESC& desc)
  478. {
  479. SPtr<Mesh> mesh = bs_core_ptr<Mesh>(new (bs_alloc<Mesh>()) Mesh(initialMeshData, desc));
  480. mesh->_setThisPtr(mesh);
  481. mesh->initialize();
  482. return mesh;
  483. }
  484. SPtr<Mesh> Mesh::_createPtr(const SPtr<MeshData>& initialMeshData, int usage, DrawOperationType drawOp)
  485. {
  486. MESH_DESC desc;
  487. desc.usage = usage;
  488. desc.subMeshes.push_back(SubMesh(0, initialMeshData->getNumIndices(), drawOp));
  489. SPtr<Mesh> mesh = bs_core_ptr<Mesh>(new (bs_alloc<Mesh>()) Mesh(initialMeshData, desc));
  490. mesh->_setThisPtr(mesh);
  491. mesh->initialize();
  492. return mesh;
  493. }
  494. SPtr<Mesh> Mesh::createEmpty()
  495. {
  496. SPtr<Mesh> mesh = bs_core_ptr<Mesh>(new (bs_alloc<Mesh>()) Mesh());
  497. mesh->_setThisPtr(mesh);
  498. return mesh;
  499. }
  500. }