2
0

BsMesh.cpp 20 KB

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