meshContainer.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. #include "meshContainer.h"
  2. #include "..\..\common_h\FileService.h"
  3. #include "..\..\common_h\physics.h"
  4. #include "..\xrender\Render.h"
  5. #include "gmxService.h"
  6. #ifndef USE_XBOX_INPLACE_GPU_RESOURCES
  7. static byte tailGPUResources[4096];
  8. #endif
  9. #ifdef _XBOX
  10. #include "..\xrender\GraphicsApi.h"
  11. #endif
  12. MeshContainer::MeshContainer(IRender* renderService)
  13. {
  14. pRS = renderService;
  15. const_data_raw = NULL;
  16. const_data = NULL;
  17. refCount = 1;
  18. }
  19. MeshContainer::~MeshContainer()
  20. {
  21. IPhysicsScene::MeshInit * pPhysMeshes = (IPhysicsScene::MeshInit*)msh.pCreatedPhysicMeshes.ptr;
  22. for (dword i = 0; i < msh.dwCreatedPhysicMeshesCount; i++)
  23. {
  24. pPhysMeshes[i].mesh->Release();
  25. }
  26. material * __restrict currentMaterial = msh.pMaterials.ptr;
  27. for (dword i = 0; i < msh.dwMaterialsCount; i++, currentMaterial++)
  28. {
  29. for (dword t = 0; t < GMX_MESH_MAX_TEXTURES_COUNT; t++)
  30. {
  31. IBaseTexture* pTexture = currentMaterial->tex[t];
  32. if (pTexture)
  33. {
  34. pTexture->Release();
  35. }
  36. }
  37. }
  38. meshFree(const_data_raw, _FL_);
  39. const_data_raw = NULL;
  40. const_data = NULL;
  41. assert(msh.collisionDataPlatform == NULL);
  42. #ifdef USE_XBOX_INPLACE_GPU_RESOURCES
  43. if (msh.physicalMemory)
  44. {
  45. XPhysicalFree(msh.physicalMemory);
  46. msh.physicalMemory = NULL;
  47. }
  48. #else
  49. if (msh.indexBuffer)
  50. {
  51. msh.indexBuffer->Release();
  52. msh.indexBuffer = NULL;
  53. }
  54. if (msh.stream0)
  55. {
  56. msh.stream0->Release();
  57. msh.stream0 = NULL;
  58. }
  59. if (msh.stream1)
  60. {
  61. msh.stream1->Release();
  62. msh.stream1 = NULL;
  63. }
  64. #endif
  65. GMXService::pGS->reportMeshContainerRelease(this);
  66. }
  67. void MeshContainer::LoadAsStatic(IDataFile* file)
  68. {
  69. AllocConstantData(msh.dwStaticDataSize);
  70. file->Read(const_data, msh.dwStaticDataSize);
  71. #ifdef USE_XBOX_INPLACE_GPU_RESOURCES
  72. Assert(sizeof(D3DVertexBuffer) == msh.sizeofD3DVertexBuffer);
  73. Assert(sizeof(D3DIndexBuffer) == msh.sizeofD3DIndexBuffer);
  74. dword dwGPUMemorySize = msh.dwIndexBufferDataSize4KAligned + msh.dwVertexBufferDataSize4KAligned0;
  75. if (dwGPUMemorySize > 0)
  76. {
  77. msh.inplaceIB.fixupAsOffset(const_data);
  78. msh.inplaceVBStream0.fixupAsOffset(const_data);
  79. msh.inplaceVBStream1.reset();
  80. msh.physicalMemory = (byte*)XPhysicalAlloc( dwGPUMemorySize, MAXULONG_PTR, 0, PAGE_READWRITE | PAGE_WRITECOMBINE );
  81. file->Read(msh.physicalMemory, dwGPUMemorySize);
  82. XGOffsetResourceAddress( msh.inplaceIB.ptr, msh.physicalMemory );
  83. XGOffsetResourceAddress( msh.inplaceVBStream0.ptr, msh.physicalMemory + msh.dwIndexBufferDataSize4KAligned );
  84. } else
  85. {
  86. msh.inplaceIB.reset();
  87. msh.inplaceVBStream0.reset();
  88. msh.inplaceVBStream1.reset();
  89. }
  90. #else
  91. if (msh.dwIndexBufferDataSize > 0)
  92. {
  93. msh.indexBuffer = pRS->CreateIndexBuffer(msh.dwIndexBufferDataSize, _FL_);
  94. word * indexBuffer_data = (word * )msh.indexBuffer->Lock();
  95. file->Read(indexBuffer_data, msh.dwIndexBufferDataSize);
  96. dword dwTailSize = msh.dwIndexBufferDataSize4KAligned - msh.dwIndexBufferDataSize;
  97. Assert(dwTailSize < 4096);
  98. file->Read(tailGPUResources, dwTailSize);
  99. #ifndef _XBOX
  100. for (dword i = 0; i < msh.dwIndexBufferIndexCount; i++)
  101. {
  102. MeshSwizzleWord(indexBuffer_data[i]);
  103. }
  104. #endif
  105. msh.indexBuffer->Unlock();
  106. } //(msh.dwIndexBufferDataSize > 0)
  107. if (msh.dwVertexBufferDataSize0 > 0)
  108. {
  109. msh.stream0 = pRS->CreateVertexBuffer(msh.dwVertexBufferDataSize0, sizeof(MeshStaticVertex), _FL_);
  110. MeshStaticVertex * stream0_data = (MeshStaticVertex *)msh.stream0->Lock();
  111. file->Read(stream0_data, msh.dwVertexBufferDataSize0);
  112. dword dwTailSize = msh.dwVertexBufferDataSize4KAligned0 - msh.dwVertexBufferDataSize0;
  113. Assert(dwTailSize < 4096);
  114. file->Read(tailGPUResources, dwTailSize);
  115. #ifndef _XBOX
  116. for (dword i = 0; i < msh.dwVertexBufferVertexCount0; i++)
  117. {
  118. MeshSwizzleStaticVertex(stream0_data[i]);
  119. }
  120. #endif
  121. msh.stream0->Unlock();
  122. msh.stream1 = NULL;
  123. } //(msh.dwVertexBufferDataSize0 > 0)
  124. #endif //USE_XBOX_INPLACE_GPU_RESOURCES
  125. #ifdef _XBOX
  126. dword dwCollsionSize = msh.dwNovodexCollisionXBOX360_size;
  127. #else
  128. //на PC загружаем оба коллижена(нет seek в файловой системе), но работаем только с нужным
  129. dword dwCollsionSize = msh.dwNovodexCollisionXBOX360_size + msh.dwNovodexCollisionPC_size;
  130. #endif
  131. msh.collisionDataPlatform = (byte *)meshAlloc(dwCollsionSize, _FL_);
  132. file->Read(msh.collisionDataPlatform, dwCollsionSize);
  133. }
  134. void MeshContainer::LoadAsAnimated(IDataFile* file)
  135. {
  136. AllocConstantData(msh.dwStaticDataSize);
  137. file->Read(const_data, msh.dwStaticDataSize);
  138. #ifdef USE_XBOX_INPLACE_GPU_RESOURCES
  139. Assert(sizeof(D3DVertexBuffer) == msh.sizeofD3DVertexBuffer);
  140. Assert(sizeof(D3DIndexBuffer) == msh.sizeofD3DIndexBuffer);
  141. dword dwGPUMemorySize = msh.dwIndexBufferDataSize4KAligned + msh.dwVertexBufferDataSize4KAligned0 + msh.dwVertexBufferDataSize4KAligned1;
  142. if (dwGPUMemorySize > 0)
  143. {
  144. msh.inplaceIB.fixupAsOffset(const_data);
  145. msh.inplaceVBStream0.fixupAsOffset(const_data);
  146. msh.inplaceVBStream1.fixupAsOffset(const_data);
  147. msh.physicalMemory = (byte*)XPhysicalAlloc( dwGPUMemorySize, MAXULONG_PTR, 0, PAGE_READWRITE | PAGE_WRITECOMBINE );
  148. file->Read(msh.physicalMemory, dwGPUMemorySize);
  149. XGOffsetResourceAddress( msh.inplaceIB.ptr, msh.physicalMemory );
  150. XGOffsetResourceAddress( msh.inplaceVBStream0.ptr, msh.physicalMemory + msh.dwIndexBufferDataSize4KAligned );
  151. XGOffsetResourceAddress( msh.inplaceVBStream1.ptr, msh.physicalMemory + msh.dwIndexBufferDataSize4KAligned + msh.dwVertexBufferDataSize4KAligned0 );
  152. } else
  153. {
  154. msh.inplaceIB.reset();
  155. msh.inplaceVBStream0.reset();
  156. msh.inplaceVBStream1.reset();
  157. }
  158. #else
  159. if (msh.dwIndexBufferDataSize > 0)
  160. {
  161. msh.indexBuffer = pRS->CreateIndexBuffer(msh.dwIndexBufferDataSize, _FL_);
  162. word * indexBuffer_data = (word *)msh.indexBuffer->Lock();
  163. file->Read(indexBuffer_data, msh.dwIndexBufferDataSize);
  164. dword dwTailSize = msh.dwIndexBufferDataSize4KAligned - msh.dwIndexBufferDataSize;
  165. Assert(dwTailSize < 4096);
  166. file->Read(tailGPUResources, dwTailSize);
  167. #ifndef _XBOX
  168. for (dword i = 0; i < msh.dwIndexBufferIndexCount; i++)
  169. {
  170. MeshSwizzleWord(indexBuffer_data[i]);
  171. }
  172. #endif
  173. msh.indexBuffer->Unlock();
  174. } //(msh.dwIndexBufferDataSize > 0)
  175. if (msh.dwVertexBufferDataSize0 > 0)
  176. {
  177. msh.stream0 = pRS->CreateVertexBuffer(msh.dwVertexBufferDataSize0, sizeof(MeshAnimVertex), _FL_);
  178. MeshAnimVertex * stream0_data = (MeshAnimVertex *)msh.stream0->Lock();
  179. file->Read(stream0_data, msh.dwVertexBufferDataSize0);
  180. dword dwTailSize = msh.dwVertexBufferDataSize4KAligned0 - msh.dwVertexBufferDataSize0;
  181. Assert(dwTailSize < 4096);
  182. file->Read(tailGPUResources, dwTailSize);
  183. #ifndef _XBOX
  184. for (dword i = 0; i < msh.dwVertexBufferVertexCount0; i++)
  185. {
  186. MeshSwizzleAnimVertex(stream0_data[i]);
  187. }
  188. #endif
  189. msh.stream0->Unlock();
  190. } //(msh.dwVertexBufferDataSize0 > 0)
  191. if (msh.dwVertexBufferDataSize1 > 0)
  192. {
  193. msh.stream1 = pRS->CreateVertexBuffer(msh.dwVertexBufferDataSize1, sizeof(MeshAnimBoneID), _FL_);
  194. MeshAnimBoneID * stream1_data = (MeshAnimBoneID *)msh.stream1->Lock();
  195. file->Read(stream1_data, msh.dwVertexBufferDataSize1);
  196. dword dwTailSize = msh.dwVertexBufferDataSize4KAligned1 - msh.dwVertexBufferDataSize1;
  197. Assert(dwTailSize < 4096);
  198. file->Read(tailGPUResources, dwTailSize);
  199. #ifndef _XBOX
  200. for (dword i = 0; i < msh.dwVertexBufferVertexCount1; i++)
  201. {
  202. MeshSwizzleAnimBoneID(stream1_data[i]);
  203. }
  204. #endif
  205. msh.stream1->Unlock();
  206. } //if (msh.dwVertexBufferDataSize1 > 0)
  207. #endif
  208. }
  209. void MeshContainer::CreateCollision()
  210. {
  211. if (msh.collisionDataPlatform == NULL)
  212. {
  213. return;
  214. }
  215. IPhysicsScene::MeshInit * pPhysMeshes = (IPhysicsScene::MeshInit*)msh.pCreatedPhysicMeshes.ptr;
  216. mesh * __restrict geom = msh.pMeshes.ptr;
  217. dword dwMeshesCount = msh.dwMeshesCount;
  218. dword dwGlobalIndex = 0;
  219. for (dword i = 0; i < dwMeshesCount; i++, geom++)
  220. {
  221. if (geom->bCollision == 0)
  222. {
  223. continue;
  224. }
  225. dword dwSubsets = geom->dwSubsetCount;
  226. subsetStatic * __restrict subsetStat = geom->pStaticSubsets.ptr;
  227. for (dword j = 0; j < dwSubsets; j++, subsetStat++)
  228. {
  229. #ifdef _XBOX
  230. byte * collisionPtr = subsetStat->pNovodexCollisionXBOX360.ptr;
  231. dword collisionSize = subsetStat->dwNovodexCollisionSizeXBOX360;
  232. byte * pmapPtr = subsetStat->pNovodexPMapXBOX360.ptr;
  233. dword pmapSize = subsetStat->dwNovodexPMapSizeXBOX360;
  234. #else
  235. byte * collisionPtr = subsetStat->pNovodexCollisionPC.ptr;
  236. dword collisionSize = subsetStat->dwNovodexCollisionSizePC;
  237. byte * pmapPtr = subsetStat->pNovodexPMapPC.ptr;
  238. dword pmapSize = subsetStat->dwNovodexPMapSizePC;
  239. #endif
  240. if (collisionSize > 0)
  241. {
  242. Assert(GMXService::pPhysic);
  243. IPhysTriangleMesh * pPhysCollision = GMXService::pPhysic->CreateTriangleMesh(collisionPtr, collisionSize, pmapPtr, pmapSize);
  244. assert(dwGlobalIndex < msh.dwCreatedPhysicMeshesCount);
  245. pPhysMeshes[dwGlobalIndex].mesh = pPhysCollision;
  246. //pPhysMeshes[dwGlobalIndex].mtx.SetIdentity();
  247. dwGlobalIndex++;
  248. }
  249. }
  250. }
  251. meshFree(msh.collisionDataPlatform, _FL_);
  252. msh.collisionDataPlatform = NULL;
  253. }
  254. bool MeshContainer::Load (IDataFile * file, IFileService * pFS, const char * nameForDebugMessages)
  255. {
  256. if (!file)
  257. {
  258. return false;
  259. }
  260. dword dwRead = file->Read(&msh, sizeof(header));
  261. if (dwRead != sizeof(header))
  262. {
  263. //invalid file size
  264. api->Trace("'%s' header too small, incorrect mesh file!", nameForDebugMessages);
  265. return false;
  266. }
  267. #ifndef _XBOX
  268. msh.swizzle(false);
  269. #endif
  270. if (msh.dwHeader != MESH_HEADER)
  271. {
  272. //not mesh
  273. api->Trace("'%s' is not mesh file !", nameForDebugMessages);
  274. return false;
  275. }
  276. if (msh.dwVersion != MESH_VERSION)
  277. {
  278. //invalid version
  279. api->Trace("'%s' invalid version %d, must be %d", nameForDebugMessages, msh.dwVersion, MESH_VERSION);
  280. return false;
  281. }
  282. if (msh.bAnimated)
  283. {
  284. LoadAsAnimated(file);
  285. } else
  286. {
  287. LoadAsStatic(file);
  288. }
  289. //файл больше не нужен...
  290. //----------------------------------------------------------------
  291. FixupPointers();
  292. CreateCollision();
  293. CreateAndLinkMaterials();
  294. return true;
  295. }
  296. void MeshContainer::CreateAndLinkMaterials()
  297. {
  298. material * __restrict currentMaterial = msh.pMaterials.ptr;
  299. for (dword i = 0; i < msh.dwMaterialsCount; i++, currentMaterial++)
  300. {
  301. pRS->GetShaderLightingId(currentMaterial->shaderName.c_str(), currentMaterial->tech);
  302. //Если не diffuse то будет белая текстура
  303. IBaseTexture* defaultTexture = NGRender::pRS->getWhiteTexture();
  304. //
  305. IBaseTexture** __restrict texPtr = &currentMaterial->tex[0];
  306. for (dword texIdx = 0; texIdx < GMX_MESH_MAX_TEXTURES_COUNT; texIdx++)
  307. {
  308. Assert(texIdx < GMX_MESH_MAX_TEXTURES_COUNT);
  309. const char * texName = currentMaterial->textureNames[texIdx].c_str();
  310. IBaseTexture* baseTex = NULL;
  311. if (texName)
  312. {
  313. baseTex = pRS->CreateTexture(_FL_, "%s\\%s", GMXService::pGS->GetTexturePath(), texName);
  314. //Что бы во все незагруженные установился diffuse
  315. //(это все, что бы убрать условия в runtime каждый кадр, что если нет текстуры поставить diffuse, а если нет диффуза, поставить белую)
  316. if (baseTex && texIdx == 0)
  317. {
  318. defaultTexture = baseTex;
  319. }
  320. }
  321. if (baseTex == NULL)
  322. {
  323. baseTex = defaultTexture;
  324. baseTex->AddRef();
  325. }
  326. Assert(baseTex);
  327. texPtr[texIdx] = baseTex;
  328. }
  329. DWORD dwDiffuseIdx = currentMaterial->dwDiffuseSlotIdx;
  330. IBaseTexture * diffuseTex = currentMaterial->tex[dwDiffuseIdx];
  331. if (!diffuseTex) continue;
  332. if (diffuseTex->GetFormat() != FMT_DXT1) continue;
  333. currentMaterial->isNoAlpha = 1;
  334. }
  335. //Что бы избавиться от индирекции при отрисовке...
  336. if (msh.bAnimated)
  337. {
  338. mesh * __restrict geom = msh.pMeshes.ptr;
  339. for (dword i = 0; i < msh.dwMeshesCount; i++, geom++)
  340. {
  341. dword dwSubsets = geom->dwSubsetCount;
  342. subsetAnim * __restrict sub = geom->pAnimSubsets.ptr;
  343. for (dword j = 0; j < dwSubsets; j++, sub++)
  344. {
  345. ApplyMaterialToSubsetAnimated(sub->pMaterial.ptr, sub);
  346. }
  347. }
  348. } else
  349. {
  350. mesh * __restrict geom = msh.pMeshes.ptr;
  351. for (dword i = 0; i < msh.dwMeshesCount; i++, geom++)
  352. {
  353. dword dwSubsets = geom->dwSubsetCount;
  354. subsetStatic * __restrict sub = geom->pStaticSubsets.ptr;
  355. for (dword j = 0; j < dwSubsets; j++, sub++)
  356. {
  357. ApplyMaterialToSubsetStatic(sub->pMaterial.ptr, sub);
  358. }
  359. }
  360. }
  361. }
  362. #ifndef _XBOX
  363. void MeshContainer::SwizzleStaticData()
  364. {
  365. const char * __restrict dataEnd = (const char *)(const_data + msh.dwStringTableSize);
  366. const char * __restrict dataIterator = (const char *)(const_data + sizeof(stringHead));
  367. if (dataIterator < dataEnd)
  368. {
  369. for (;;)
  370. {
  371. stringHead * head = (stringHead*)(dataIterator - sizeof(stringHead));
  372. MeshSwizzleDWord(head->dwHashNoCase);
  373. MeshSwizzleDWord(head->dwLen);
  374. dataIterator += (head->dwLen + sizeof(stringHead)) + sizeof(char);
  375. if (dataIterator >= dataEnd)
  376. {
  377. break;
  378. }
  379. }
  380. }
  381. material * __restrict currentMaterial = msh.pMaterials.ptr;
  382. for (dword i = 0; i < msh.dwMaterialsCount; i++, currentMaterial++)
  383. {
  384. MeshSwizzleDWord(currentMaterial->dwBonesInSkin);
  385. MeshSwizzleDWord(currentMaterial->dwDiffuseSlotIdx);
  386. MeshSwizzleDWord(currentMaterial->dwLevel);
  387. MeshSwizzleDWord(currentMaterial->dwTexCount);
  388. MeshSwizzleDWord(currentMaterial->isNoAlpha);
  389. }
  390. renderOrder * __restrict order = msh.pRenderOrders.ptr;
  391. for (dword i = 0; i < msh.dwOrdersCount; i++, order++)
  392. {
  393. MeshSwizzleDWord(order->dwMeshOrder);
  394. }
  395. locator * __restrict cLocator = msh.pLocators.ptr;
  396. for (dword i = 0; i < msh.dwLocatorsCount; i++, cLocator++)
  397. {
  398. MeshSwizzleDWord(cLocator->desc.dw);
  399. MeshSwizzleDWord(cLocator->dwDynamicTransformCacheIndex);
  400. MeshSwizzleDWord(cLocator->dwParticleCacheIndex);
  401. //Assert(cLocator->dwDynamicTransformCacheIndex != 0xFFFFFFFF);
  402. MeshSwizzleDWord(cLocator->handle.index);
  403. }
  404. mesh * __restrict cMesh = msh.pMeshes.ptr;
  405. for (dword i = 0; i < msh.dwMeshesCount; i++, cMesh++)
  406. {
  407. MeshSwizzleDWord(cMesh->bCollision);
  408. MeshSwizzleDWord(cMesh->bVisible);
  409. MeshSwizzleDWord(cMesh->desc.dw);
  410. MeshSwizzleDWord(cMesh->dwRenderOrder);
  411. MeshSwizzleDWord(cMesh->dwSubsetCount);
  412. MeshSwizzleVector3(cMesh->max);
  413. MeshSwizzleVector3(cMesh->min);
  414. }
  415. nodeTransform * __restrict transfrom = msh.pAllTransformations.ptr;
  416. for (dword i = 0; i < msh.dwTransformationsCount; i++, transfrom++)
  417. {
  418. //MeshSwizzleLong(transfrom->animBoneID);
  419. MeshSwizzleMatrix(transfrom->mtxLocalTransform);
  420. MeshSwizzleMatrix(transfrom->mtxTransform);
  421. MeshSwizzleInt(transfrom->parentIdx);
  422. }
  423. TTABLEITEM * __restrict ttableItem = msh.pTranslateTables.ptr;
  424. for (dword i = 0; i < msh.dwTranslateTableCount; i++, ttableItem++)
  425. {
  426. MeshSwizzleTableItem(*ttableItem);
  427. }
  428. if (msh.bAnimated)
  429. {
  430. subsetAnim * __restrict animSubset = msh.pAnimSubsets.ptr;
  431. for (dword i = 0; i < msh.dwSubsetsCount; i++, animSubset++)
  432. {
  433. //MeshSwizzleDWord(animSubset->bonesGlobalOffset);
  434. //MeshSwizzleDWord(animSubset->dwBonesCount);
  435. MeshSwizzleDWord(animSubset->dwPassesCount);
  436. MeshSwizzleDWord(animSubset->numVertexes);
  437. MeshSwizzleDWord(animSubset->offsetInStream0InBytes);
  438. }
  439. subSubsetAnim * __restrict subSubsetAnm = msh.pSubSubsets.ptr;
  440. for (dword i = 0; i < msh.dwSubSubsetAnimCount; i++, subSubsetAnm++)
  441. {
  442. MeshSwizzleDWord(subSubsetAnm->dwTranslateTableSize);
  443. MeshSwizzleDWord(subSubsetAnm->numVertexes);
  444. MeshSwizzleDWord(subSubsetAnm->offsetInIndexBufferInInds);
  445. MeshSwizzleDWord(subSubsetAnm->offsetInStream1InBytes);
  446. MeshSwizzleDWord(subSubsetAnm->triCount);
  447. }
  448. boneDataReadOnly * __restrict bonesData = msh.pBonesInfo.ptr;
  449. for (dword i = 0; i < msh.dwBonesCount; i++, bonesData++)
  450. {
  451. MeshSwizzleVector3(bonesData->bbMax);
  452. MeshSwizzleVector3(bonesData->bbMin);
  453. MeshSwizzleVector4(bonesData->bbSphereCenterAndRadius);
  454. MeshSwizzleVector4(bonesData->bbSphereRadius);
  455. }
  456. boneMtxInputReadOnly * __restrict bonesMtx = msh.pBonesMatrices.ptr;
  457. for (dword i = 0; i < msh.dwBonesCount; i++, bonesMtx++)
  458. {
  459. MeshSwizzleMatrix(bonesMtx->mtxBindPose);
  460. }
  461. blendShapeBoneTransform * __restrict bsBoneTransform = msh.pBlendShapeBonesTransform.ptr;
  462. for (dword i = 0; i < msh.dwBlendShapeBonesTransformCount; i++, bsBoneTransform++)
  463. {
  464. MeshSwizzleVector3(bsBoneTransform->p);
  465. MeshSwizzleQuaternion(bsBoneTransform->q);
  466. MeshSwizzleFloat(bsBoneTransform->weightScale);
  467. }
  468. /*
  469. blendShapeTarget * __restrict bsMorphTarget = msh.pBlendShapeTargets.ptr;
  470. for (dword i = 0; i < msh.dwBlendShapeTargetsCount; i++, bsMorphTarget++)
  471. {
  472. //bsMorphTarget->frameBones
  473. }
  474. blendShapeBoneDescription * __restrict bsBoneDesc = msh.pBlendShapeBones.ptr;
  475. for (dword i = 0; i < msh.dwBlendShapeBonesCount; i++, bsBoneDesc++)
  476. {
  477. //bsBoneDesc->boneName
  478. }
  479. */
  480. } else
  481. {
  482. subsetStatic * __restrict staticSubset = msh.pStaticSubsets.ptr;
  483. for (dword i = 0; i < msh.dwSubsetsCount; i++, staticSubset++)
  484. {
  485. MeshSwizzleVector3(staticSubset->max);
  486. MeshSwizzleVector3(staticSubset->min);
  487. MeshSwizzleDWord(staticSubset->numVertexes);
  488. MeshSwizzleDWord(staticSubset->offsetInIndexBufferInInds);
  489. MeshSwizzleDWord(staticSubset->offsetInVertexBufferInBytes);
  490. MeshSwizzleVector3(staticSubset->sphereC);
  491. MeshSwizzleFloat(staticSubset->sphereR);
  492. MeshSwizzleDWord(staticSubset->triCount);
  493. MeshSwizzleDWord(staticSubset->dwNovodexCollisionSizePC);
  494. MeshSwizzleDWord(staticSubset->dwNovodexPMapSizePC);
  495. MeshSwizzleDWord(staticSubset->dwNovodexCollisionSizeXBOX360);
  496. MeshSwizzleDWord(staticSubset->dwNovodexPMapSizeXBOX360);
  497. }
  498. }
  499. /*
  500. msh.pAllTransformations.fixupAsOffset(const_data);
  501. msh.pDynamicTransformations.fixupAsOffset(const_data);
  502. */
  503. }
  504. #endif
  505. void MeshContainer::FixupPointers()
  506. {
  507. if (msh.bAnimated)
  508. {
  509. msh.pCreatedPhysicMeshes.reset();
  510. msh.pMaterials.fixupAsOffset(const_data);
  511. msh.pRenderOrders.fixupAsOffset(const_data);
  512. msh.pLocators.fixupAsOffset(const_data);
  513. msh.pMeshes.fixupAsOffset(const_data);
  514. msh.pStaticSubsets.reset();
  515. msh.pAnimSubsets.fixupAsOffset(const_data);
  516. msh.pTranslateTables.fixupAsOffset(const_data);
  517. msh.pSubSubsets.fixupAsOffset(const_data);
  518. msh.pBonesInfo.fixupAsOffset(const_data);
  519. msh.pBonesMatrices.fixupAsOffset(const_data);
  520. msh.pBlendShapeBones.fixupAsOffset(const_data);
  521. msh.pBlendShapeTargets.fixupAsOffset(const_data);
  522. msh.pBlendShapeBonesTransform.fixupAsOffset(const_data);
  523. msh.pAllTransformations.fixupAsOffset(const_data);
  524. msh.pDynamicTransformations.fixupAsOffset(const_data);
  525. } else
  526. {
  527. msh.pCreatedPhysicMeshes.fixupAsOffset(const_data);
  528. msh.pMaterials.fixupAsOffset(const_data);
  529. msh.pRenderOrders.fixupAsOffset(const_data);
  530. msh.pLocators.fixupAsOffset(const_data);
  531. msh.pMeshes.fixupAsOffset(const_data);
  532. msh.pStaticSubsets.fixupAsOffset(const_data);
  533. msh.pAnimSubsets.reset();
  534. msh.pTranslateTables.reset();
  535. msh.pSubSubsets.reset();
  536. msh.pBonesInfo.reset();
  537. msh.pBonesMatrices.reset();
  538. msh.pBlendShapeBones.reset();
  539. msh.pBlendShapeTargets.reset();
  540. msh.pBlendShapeBonesTransform.reset();
  541. msh.pAllTransformations.fixupAsOffset(const_data);
  542. msh.pDynamicTransformations.fixupAsOffset(const_data);
  543. }
  544. MESH_ALIGN_16_CHECK(msh.pBlendShapeBonesTransform.ptr);
  545. for (dword i = 0; i < HASH_TABLE_SIZE; i++)
  546. {
  547. msh.entryTable[i].fixupAsIndex(msh.pLocators.ptr);
  548. }
  549. #ifndef _XBOX
  550. SwizzleStaticData();
  551. #endif
  552. dword dwMaterialsCount = msh.dwMaterialsCount;
  553. material * __restrict currentMaterial = msh.pMaterials.ptr;
  554. for (dword i = 0; i < dwMaterialsCount; i++, currentMaterial++)
  555. {
  556. currentMaterial->shaderName.fixup(const_data);
  557. currentMaterial->textureNames[0].fixup(const_data);
  558. currentMaterial->textureNames[1].fixup(const_data);
  559. currentMaterial->textureNames[2].fixup(const_data);
  560. currentMaterial->textureNames[3].fixup(const_data);
  561. }
  562. dword dwMeshesCount = msh.dwMeshesCount;
  563. mesh * __restrict cMesh = msh.pMeshes.ptr;
  564. for (dword i = 0; i < dwMeshesCount; i++, cMesh++)
  565. {
  566. cMesh->pTransform.fixupAsIndex(msh.pAllTransformations.ptr);
  567. cMesh->pParentTransform.fixupAsIndex(msh.pAllTransformations.ptr);
  568. cMesh->name.fixup(const_data);
  569. }
  570. cMesh = msh.pMeshes.ptr;
  571. dword dwLocatorsCount = msh.dwLocatorsCount;
  572. locator * __restrict cLocator = msh.pLocators.ptr;
  573. for (dword i = 0; i < dwLocatorsCount; i++, cLocator++)
  574. {
  575. cLocator->pTransform.fixupAsIndex(msh.pAllTransformations.ptr);
  576. cLocator->pParentTransform.fixupAsIndex(msh.pAllTransformations.ptr);
  577. cLocator->boneName.fixup(const_data);
  578. cLocator->particleName.fixup(const_data);
  579. cLocator->name.fixup(const_data);
  580. cLocator->pHashTableNext.fixupAsIndex(msh.pLocators.ptr);
  581. }
  582. if (msh.bAnimated)
  583. {
  584. nodeTransform * __restrict transform = msh.pAllTransformations.ptr;
  585. for (dword i = 0; i < msh.dwTransformationsCount; i++, transform++)
  586. {
  587. transform->debugNodeName.fixup(const_data);
  588. transform->boneName.fixup(const_data);
  589. }
  590. boneDataReadOnly * __restrict bonesData = msh.pBonesInfo.ptr;
  591. dword dwBonesCount = msh.dwBonesCount;
  592. for (dword i = 0; i < dwBonesCount; i++, bonesData++)
  593. {
  594. bonesData->name.fixup(const_data);
  595. }
  596. dword dwSubSubsetCount = msh.dwSubSubsetAnimCount;
  597. subSubsetAnim * __restrict subSubsetAnm = msh.pSubSubsets.ptr;
  598. for (dword i = 0; i < dwSubSubsetCount; i++, subSubsetAnm++)
  599. {
  600. subSubsetAnm->pTTableLocalToGlobal.fixupAsIndex(msh.pTranslateTables.ptr);
  601. }
  602. dword dwSubsetCount = msh.dwSubsetsCount;
  603. subsetAnim * __restrict animSubset = msh.pAnimSubsets.ptr;
  604. for (dword i = 0; i < dwSubsetCount; i++, animSubset++)
  605. {
  606. animSubset->pMaterial.fixupAsIndex(msh.pMaterials.ptr);
  607. animSubset->pSubSubsets.fixupAsIndex(msh.pSubSubsets.ptr);
  608. // animSubset->pBonesData.fixupAsIndex(msh.pBonesInfo.ptr);
  609. // animSubset->pBonesMatrices.fixupAsIndex(msh.pBonesMatrices.ptr);
  610. }
  611. for (dword i = 0; i < dwMeshesCount; i++, cMesh++)
  612. {
  613. cMesh->pAnimSubsets.fixupAsIndex(msh.pAnimSubsets.ptr);
  614. }
  615. dword dwOrdersCount = msh.dwOrdersCount;
  616. renderOrder * __restrict order = msh.pRenderOrders.ptr;
  617. for (dword i = 0; i < dwOrdersCount; i++, order++)
  618. {
  619. order->pMesh.fixupAsIndex(msh.pMeshes.ptr);
  620. //т.к. pSubset указатель на void то фиксим руками...
  621. //order->pSubset.fixupAsIndex(order->pMesh.ptr->pAnimSubsets.ptr);
  622. dword dwIdx = order->pSubset.index;
  623. #ifndef _XBOX
  624. MeshSwizzleDWord(dwIdx);
  625. #endif
  626. order->pSubset.ptr = &order->pMesh.ptr->pAnimSubsets.ptr[dwIdx];
  627. }
  628. dword dwBSMorphTargetCount = msh.dwBlendShapeTargetsCount;
  629. blendShapeTarget * __restrict bsFrame = msh.pBlendShapeTargets.ptr;
  630. for (dword i = 0; i < dwBSMorphTargetCount; i++, bsFrame++)
  631. {
  632. bsFrame->frameBones.fixupAsIndex(msh.pBlendShapeBonesTransform.ptr);
  633. }
  634. dword dwBSBonesCount = msh.dwBlendShapeBonesCount;
  635. blendShapeBoneDescription * __restrict bsBoneName = msh.pBlendShapeBones.ptr;
  636. for (dword i = 0; i < dwBSBonesCount; i++, bsBoneName++)
  637. {
  638. bsBoneName->boneName.fixup(const_data);
  639. }
  640. } else
  641. {
  642. for (dword i = 0; i < dwMeshesCount; i++, cMesh++)
  643. {
  644. cMesh->pStaticSubsets.fixupAsIndex(msh.pStaticSubsets.ptr);
  645. }
  646. //---
  647. nodeTransform * __restrict transform = msh.pAllTransformations.ptr;
  648. for (dword i = 0; i < msh.dwTransformationsCount; i++, transform++)
  649. {
  650. transform->debugNodeName.fixup(const_data);
  651. transform->boneName.clear();
  652. }
  653. dword dwSubsetCount = msh.dwSubsetsCount;
  654. subsetStatic * __restrict staticSubset = msh.pStaticSubsets.ptr;
  655. for (dword i = 0; i < dwSubsetCount; i++, staticSubset++)
  656. {
  657. staticSubset->pMaterial.fixupAsIndex(msh.pMaterials.ptr);
  658. //staticSubset->pCollision.fixupAsIndex(NULL);
  659. #ifdef _XBOX
  660. staticSubset->pNovodexCollisionPC.reset();
  661. staticSubset->pNovodexPMapPC.reset();
  662. staticSubset->pNovodexCollisionXBOX360.fixupAsOffset(msh.collisionDataPlatform);
  663. staticSubset->pNovodexPMapXBOX360.fixupAsOffset(msh.collisionDataPlatform);
  664. #else
  665. //Учитываем, что в загруженных данных вначале идут Xbox данные...
  666. byte* collisionDataPC = msh.collisionDataPlatform + msh.dwNovodexCollisionXBOX360_size;
  667. staticSubset->pNovodexCollisionPC.fixupAsOffset(collisionDataPC);
  668. staticSubset->pNovodexPMapPC.fixupAsOffset(collisionDataPC);
  669. staticSubset->pNovodexCollisionXBOX360.reset();
  670. staticSubset->pNovodexPMapXBOX360.reset();
  671. #endif
  672. }
  673. dword dwOrdersCount = msh.dwOrdersCount;
  674. renderOrder * __restrict order = msh.pRenderOrders.ptr;
  675. for (dword i = 0; i < dwOrdersCount; i++, order++)
  676. {
  677. order->pMesh.fixupAsIndex(msh.pMeshes.ptr);
  678. //т.к. pSubset указатель на void то фиксим руками...
  679. //order->pSubset.fixupAsIndex(order->pMesh.ptr->pStaticSubsets.ptr);
  680. dword dwIdx = order->pSubset.index;
  681. #ifndef _XBOX
  682. MeshSwizzleDWord(dwIdx);
  683. #endif
  684. order->pSubset.ptr = &order->pMesh.ptr->pStaticSubsets.ptr[dwIdx];
  685. }
  686. }
  687. }
  688. void MeshContainer::AllocConstantData(dword dwBytesCount)
  689. {
  690. DWORD dwBytesToAllocate = dwBytesCount + 16;
  691. const_data_raw = (byte *)meshAlloc(dwBytesToAllocate, _FL_);
  692. const_data = AlignPtr((const_data_raw));
  693. MESH_ALIGN_16_CHECK(const_data);
  694. //конструктор не нужен этим данным
  695. }
  696. void MeshContainer::ForceRelease ()
  697. {
  698. Assert(refCount > 0);
  699. refCount = 1;
  700. Release ();
  701. }
  702. bool MeshContainer::Release ()
  703. {
  704. Assert(refCount > 0);
  705. refCount--;
  706. if (refCount <= 0)
  707. {
  708. delete this;
  709. return true;
  710. }
  711. return false;
  712. }
  713. void MeshContainer::AddRef ()
  714. {
  715. refCount++;
  716. }