BsMesh.cpp 19 KB

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