/indra/newview/llviewerjointmesh.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 847 lines · 541 code · 140 blank · 166 comment · 86 complexity · 3249125e53b57486c1be3cdc66521062 MD5 · raw file

  1. /**
  2. * @file llviewerjointmesh.cpp
  3. * @brief Implementation of LLViewerJointMesh class
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. //-----------------------------------------------------------------------------
  27. // Header Files
  28. //-----------------------------------------------------------------------------
  29. #include "llviewerprecompiledheaders.h"
  30. #include "imageids.h"
  31. #include "llfasttimer.h"
  32. #include "llrender.h"
  33. #include "llapr.h"
  34. #include "llbox.h"
  35. #include "lldrawable.h"
  36. #include "lldrawpoolavatar.h"
  37. #include "lldrawpoolbump.h"
  38. #include "lldynamictexture.h"
  39. #include "llface.h"
  40. #include "llgldbg.h"
  41. #include "llglheaders.h"
  42. #include "lltexlayer.h"
  43. #include "llviewercamera.h"
  44. #include "llviewercontrol.h"
  45. #include "llviewertexturelist.h"
  46. #include "llviewerjointmesh.h"
  47. #include "llvoavatar.h"
  48. #include "llsky.h"
  49. #include "pipeline.h"
  50. #include "llviewershadermgr.h"
  51. #include "llmath.h"
  52. #include "v4math.h"
  53. #include "m3math.h"
  54. #include "m4math.h"
  55. #include "llmatrix4a.h"
  56. #if !LL_DARWIN && !LL_LINUX && !LL_SOLARIS
  57. extern PFNGLWEIGHTPOINTERARBPROC glWeightPointerARB;
  58. extern PFNGLWEIGHTFVARBPROC glWeightfvARB;
  59. extern PFNGLVERTEXBLENDARBPROC glVertexBlendARB;
  60. #endif
  61. static LLPointer<LLVertexBuffer> sRenderBuffer = NULL;
  62. static const U32 sRenderMask = LLVertexBuffer::MAP_VERTEX |
  63. LLVertexBuffer::MAP_NORMAL |
  64. LLVertexBuffer::MAP_TEXCOORD0;
  65. //-----------------------------------------------------------------------------
  66. //-----------------------------------------------------------------------------
  67. // LLViewerJointMesh::LLSkinJoint
  68. //-----------------------------------------------------------------------------
  69. //-----------------------------------------------------------------------------
  70. //-----------------------------------------------------------------------------
  71. // LLSkinJoint
  72. //-----------------------------------------------------------------------------
  73. LLSkinJoint::LLSkinJoint()
  74. {
  75. mJoint = NULL;
  76. }
  77. //-----------------------------------------------------------------------------
  78. // ~LLSkinJoint
  79. //-----------------------------------------------------------------------------
  80. LLSkinJoint::~LLSkinJoint()
  81. {
  82. mJoint = NULL;
  83. }
  84. //-----------------------------------------------------------------------------
  85. // LLSkinJoint::setupSkinJoint()
  86. //-----------------------------------------------------------------------------
  87. BOOL LLSkinJoint::setupSkinJoint( LLViewerJoint *joint)
  88. {
  89. // find the named joint
  90. mJoint = joint;
  91. if ( !mJoint )
  92. {
  93. llinfos << "Can't find joint" << llendl;
  94. }
  95. // compute the inverse root skin matrix
  96. mRootToJointSkinOffset.clearVec();
  97. LLVector3 rootSkinOffset;
  98. while (joint)
  99. {
  100. rootSkinOffset += joint->getSkinOffset();
  101. joint = (LLViewerJoint*)joint->getParent();
  102. }
  103. mRootToJointSkinOffset = -rootSkinOffset;
  104. mRootToParentJointSkinOffset = mRootToJointSkinOffset;
  105. mRootToParentJointSkinOffset += mJoint->getSkinOffset();
  106. return TRUE;
  107. }
  108. //-----------------------------------------------------------------------------
  109. //-----------------------------------------------------------------------------
  110. // LLViewerJointMesh
  111. //-----------------------------------------------------------------------------
  112. //-----------------------------------------------------------------------------
  113. BOOL LLViewerJointMesh::sPipelineRender = FALSE;
  114. EAvatarRenderPass LLViewerJointMesh::sRenderPass = AVATAR_RENDER_PASS_SINGLE;
  115. U32 LLViewerJointMesh::sClothingMaskImageName = 0;
  116. LLColor4 LLViewerJointMesh::sClothingInnerColor;
  117. //-----------------------------------------------------------------------------
  118. // LLViewerJointMesh()
  119. //-----------------------------------------------------------------------------
  120. LLViewerJointMesh::LLViewerJointMesh()
  121. :
  122. mTexture( NULL ),
  123. mLayerSet( NULL ),
  124. mTestImageName( 0 ),
  125. mFaceIndexCount(0),
  126. mIsTransparent(FALSE)
  127. {
  128. mColor[0] = 1.0f;
  129. mColor[1] = 1.0f;
  130. mColor[2] = 1.0f;
  131. mColor[3] = 1.0f;
  132. mShiny = 0.0f;
  133. mCullBackFaces = TRUE;
  134. mMesh = NULL;
  135. mNumSkinJoints = 0;
  136. mSkinJoints = NULL;
  137. mFace = NULL;
  138. mMeshID = 0;
  139. mUpdateXform = FALSE;
  140. mValid = FALSE;
  141. }
  142. //-----------------------------------------------------------------------------
  143. // ~LLViewerJointMesh()
  144. // Class Destructor
  145. //-----------------------------------------------------------------------------
  146. LLViewerJointMesh::~LLViewerJointMesh()
  147. {
  148. mMesh = NULL;
  149. mTexture = NULL;
  150. freeSkinData();
  151. }
  152. //-----------------------------------------------------------------------------
  153. // LLViewerJointMesh::allocateSkinData()
  154. //-----------------------------------------------------------------------------
  155. BOOL LLViewerJointMesh::allocateSkinData( U32 numSkinJoints )
  156. {
  157. mSkinJoints = new LLSkinJoint[ numSkinJoints ];
  158. mNumSkinJoints = numSkinJoints;
  159. return TRUE;
  160. }
  161. //-----------------------------------------------------------------------------
  162. // LLViewerJointMesh::freeSkinData()
  163. //-----------------------------------------------------------------------------
  164. void LLViewerJointMesh::freeSkinData()
  165. {
  166. mNumSkinJoints = 0;
  167. delete [] mSkinJoints;
  168. mSkinJoints = NULL;
  169. }
  170. //--------------------------------------------------------------------
  171. // LLViewerJointMesh::getColor()
  172. //--------------------------------------------------------------------
  173. void LLViewerJointMesh::getColor( F32 *red, F32 *green, F32 *blue, F32 *alpha )
  174. {
  175. *red = mColor[0];
  176. *green = mColor[1];
  177. *blue = mColor[2];
  178. *alpha = mColor[3];
  179. }
  180. //--------------------------------------------------------------------
  181. // LLViewerJointMesh::setColor()
  182. //--------------------------------------------------------------------
  183. void LLViewerJointMesh::setColor( F32 red, F32 green, F32 blue, F32 alpha )
  184. {
  185. mColor[0] = red;
  186. mColor[1] = green;
  187. mColor[2] = blue;
  188. mColor[3] = alpha;
  189. }
  190. //--------------------------------------------------------------------
  191. // LLViewerJointMesh::getTexture()
  192. //--------------------------------------------------------------------
  193. //LLViewerTexture *LLViewerJointMesh::getTexture()
  194. //{
  195. // return mTexture;
  196. //}
  197. //--------------------------------------------------------------------
  198. // LLViewerJointMesh::setTexture()
  199. //--------------------------------------------------------------------
  200. void LLViewerJointMesh::setTexture( LLViewerTexture *texture )
  201. {
  202. mTexture = texture;
  203. // texture and dynamic_texture are mutually exclusive
  204. if( texture )
  205. {
  206. mLayerSet = NULL;
  207. //texture->bindTexture(0);
  208. //texture->setClamp(TRUE, TRUE);
  209. }
  210. }
  211. //--------------------------------------------------------------------
  212. // LLViewerJointMesh::setLayerSet()
  213. // Sets the shape texture (takes precedence over normal texture)
  214. //--------------------------------------------------------------------
  215. void LLViewerJointMesh::setLayerSet( LLTexLayerSet* layer_set )
  216. {
  217. mLayerSet = layer_set;
  218. // texture and dynamic_texture are mutually exclusive
  219. if( layer_set )
  220. {
  221. mTexture = NULL;
  222. }
  223. }
  224. //--------------------------------------------------------------------
  225. // LLViewerJointMesh::getMesh()
  226. //--------------------------------------------------------------------
  227. LLPolyMesh *LLViewerJointMesh::getMesh()
  228. {
  229. return mMesh;
  230. }
  231. //-----------------------------------------------------------------------------
  232. // LLViewerJointMesh::setMesh()
  233. //-----------------------------------------------------------------------------
  234. void LLViewerJointMesh::setMesh( LLPolyMesh *mesh )
  235. {
  236. // set the mesh pointer
  237. mMesh = mesh;
  238. // release any existing skin joints
  239. freeSkinData();
  240. if ( mMesh == NULL )
  241. {
  242. return;
  243. }
  244. // acquire the transform from the mesh object
  245. setPosition( mMesh->getPosition() );
  246. setRotation( mMesh->getRotation() );
  247. setScale( mMesh->getScale() );
  248. // create skin joints if necessary
  249. if ( mMesh->hasWeights() && !mMesh->isLOD())
  250. {
  251. U32 numJointNames = mMesh->getNumJointNames();
  252. allocateSkinData( numJointNames );
  253. std::string *jointNames = mMesh->getJointNames();
  254. U32 jn;
  255. for (jn = 0; jn < numJointNames; jn++)
  256. {
  257. //llinfos << "Setting up joint " << jointNames[jn] << llendl;
  258. LLViewerJoint* joint = (LLViewerJoint*)(getRoot()->findJoint(jointNames[jn]) );
  259. mSkinJoints[jn].setupSkinJoint( joint );
  260. }
  261. }
  262. // setup joint array
  263. if (!mMesh->isLOD())
  264. {
  265. setupJoint((LLViewerJoint*)getRoot());
  266. }
  267. // llinfos << "joint render entries: " << mMesh->mJointRenderData.count() << llendl;
  268. }
  269. //-----------------------------------------------------------------------------
  270. // setupJoint()
  271. //-----------------------------------------------------------------------------
  272. void LLViewerJointMesh::setupJoint(LLViewerJoint* current_joint)
  273. {
  274. // llinfos << "Mesh: " << getName() << llendl;
  275. // S32 joint_count = 0;
  276. U32 sj;
  277. for (sj=0; sj<mNumSkinJoints; sj++)
  278. {
  279. LLSkinJoint &js = mSkinJoints[sj];
  280. if (js.mJoint != current_joint)
  281. {
  282. continue;
  283. }
  284. // we've found a skinjoint for this joint..
  285. // is the last joint in the array our parent?
  286. if(mMesh->mJointRenderData.count() && mMesh->mJointRenderData[mMesh->mJointRenderData.count() - 1]->mWorldMatrix == &current_joint->getParent()->getWorldMatrix())
  287. {
  288. // ...then just add ourselves
  289. LLViewerJoint* jointp = js.mJoint;
  290. mMesh->mJointRenderData.put(new LLJointRenderData(&jointp->getWorldMatrix(), &js));
  291. // llinfos << "joint " << joint_count << js.mJoint->getName() << llendl;
  292. // joint_count++;
  293. }
  294. // otherwise add our parent and ourselves
  295. else
  296. {
  297. mMesh->mJointRenderData.put(new LLJointRenderData(&current_joint->getParent()->getWorldMatrix(), NULL));
  298. // llinfos << "joint " << joint_count << current_joint->getParent()->getName() << llendl;
  299. // joint_count++;
  300. mMesh->mJointRenderData.put(new LLJointRenderData(&current_joint->getWorldMatrix(), &js));
  301. // llinfos << "joint " << joint_count << current_joint->getName() << llendl;
  302. // joint_count++;
  303. }
  304. }
  305. // depth-first traversal
  306. for (LLJoint::child_list_t::iterator iter = current_joint->mChildren.begin();
  307. iter != current_joint->mChildren.end(); ++iter)
  308. {
  309. LLViewerJoint* child_joint = (LLViewerJoint*)(*iter);
  310. setupJoint(child_joint);
  311. }
  312. }
  313. const S32 NUM_AXES = 3;
  314. // register layoud
  315. // rotation X 0-n
  316. // rotation Y 0-n
  317. // rotation Z 0-n
  318. // pivot parent 0-n -- child = n+1
  319. static LLMatrix4 gJointMatUnaligned[32];
  320. static LLMatrix4a gJointMatAligned[32];
  321. static LLMatrix3 gJointRotUnaligned[32];
  322. static LLVector4 gJointPivot[32];
  323. //-----------------------------------------------------------------------------
  324. // uploadJointMatrices()
  325. //-----------------------------------------------------------------------------
  326. void LLViewerJointMesh::uploadJointMatrices()
  327. {
  328. S32 joint_num;
  329. LLPolyMesh *reference_mesh = mMesh->getReferenceMesh();
  330. LLDrawPool *poolp = mFace ? mFace->getPool() : NULL;
  331. BOOL hardware_skinning = (poolp && poolp->getVertexShaderLevel() > 0) ? TRUE : FALSE;
  332. //calculate joint matrices
  333. for (joint_num = 0; joint_num < reference_mesh->mJointRenderData.count(); joint_num++)
  334. {
  335. LLMatrix4 joint_mat = *reference_mesh->mJointRenderData[joint_num]->mWorldMatrix;
  336. if (hardware_skinning)
  337. {
  338. joint_mat *= LLDrawPoolAvatar::getModelView();
  339. }
  340. gJointMatUnaligned[joint_num] = joint_mat;
  341. gJointRotUnaligned[joint_num] = joint_mat.getMat3();
  342. }
  343. BOOL last_pivot_uploaded = FALSE;
  344. S32 j = 0;
  345. //upload joint pivots
  346. for (joint_num = 0; joint_num < reference_mesh->mJointRenderData.count(); joint_num++)
  347. {
  348. LLSkinJoint *sj = reference_mesh->mJointRenderData[joint_num]->mSkinJoint;
  349. if (sj)
  350. {
  351. if (!last_pivot_uploaded)
  352. {
  353. LLVector4 parent_pivot(sj->mRootToParentJointSkinOffset);
  354. parent_pivot.mV[VW] = 0.f;
  355. gJointPivot[j++] = parent_pivot;
  356. }
  357. LLVector4 child_pivot(sj->mRootToJointSkinOffset);
  358. child_pivot.mV[VW] = 0.f;
  359. gJointPivot[j++] = child_pivot;
  360. last_pivot_uploaded = TRUE;
  361. }
  362. else
  363. {
  364. last_pivot_uploaded = FALSE;
  365. }
  366. }
  367. //add pivot point into transform
  368. for (S32 i = 0; i < j; i++)
  369. {
  370. LLVector3 pivot;
  371. pivot = LLVector3(gJointPivot[i]);
  372. pivot = pivot * gJointRotUnaligned[i];
  373. gJointMatUnaligned[i].translate(pivot);
  374. }
  375. // upload matrices
  376. if (hardware_skinning)
  377. {
  378. GLfloat mat[45*4];
  379. memset(mat, 0, sizeof(GLfloat)*45*4);
  380. for (joint_num = 0; joint_num < reference_mesh->mJointRenderData.count(); joint_num++)
  381. {
  382. gJointMatUnaligned[joint_num].transpose();
  383. for (S32 axis = 0; axis < NUM_AXES; axis++)
  384. {
  385. F32* vector = gJointMatUnaligned[joint_num].mMatrix[axis];
  386. U32 offset = LL_CHARACTER_MAX_JOINTS_PER_MESH*axis+joint_num;
  387. memcpy(mat+offset*4, vector, sizeof(GLfloat)*4);
  388. }
  389. }
  390. stop_glerror();
  391. if (LLGLSLShader::sCurBoundShaderPtr)
  392. {
  393. LLGLSLShader::sCurBoundShaderPtr->uniform4fv(LLViewerShaderMgr::AVATAR_MATRIX, 45, mat);
  394. }
  395. stop_glerror();
  396. }
  397. else
  398. {
  399. //load gJointMatUnaligned into gJointMatAligned
  400. for (joint_num = 0; joint_num < reference_mesh->mJointRenderData.count(); ++joint_num)
  401. {
  402. gJointMatAligned[joint_num].loadu(gJointMatUnaligned[joint_num]);
  403. }
  404. }
  405. }
  406. //--------------------------------------------------------------------
  407. // LLViewerJointMesh::drawBone()
  408. //--------------------------------------------------------------------
  409. void LLViewerJointMesh::drawBone()
  410. {
  411. }
  412. //--------------------------------------------------------------------
  413. // LLViewerJointMesh::isTransparent()
  414. //--------------------------------------------------------------------
  415. BOOL LLViewerJointMesh::isTransparent()
  416. {
  417. return mIsTransparent;
  418. }
  419. //--------------------------------------------------------------------
  420. // DrawElementsBLEND and utility code
  421. //--------------------------------------------------------------------
  422. // compate_int is used by the qsort function to sort the index array
  423. int compare_int(const void *a, const void *b)
  424. {
  425. if (*(U32*)a < *(U32*)b)
  426. {
  427. return -1;
  428. }
  429. else if (*(U32*)a > *(U32*)b)
  430. {
  431. return 1;
  432. }
  433. else return 0;
  434. }
  435. //--------------------------------------------------------------------
  436. // LLViewerJointMesh::drawShape()
  437. //--------------------------------------------------------------------
  438. U32 LLViewerJointMesh::drawShape( F32 pixelArea, BOOL first_pass, BOOL is_dummy)
  439. {
  440. if (!mValid || !mMesh || !mFace || !mVisible ||
  441. !mFace->getVertexBuffer() ||
  442. mMesh->getNumFaces() == 0 ||
  443. (LLGLSLShader::sNoFixedFunction && LLGLSLShader::sCurBoundShaderPtr == NULL))
  444. {
  445. return 0;
  446. }
  447. U32 triangle_count = 0;
  448. S32 diffuse_channel = LLDrawPoolAvatar::sDiffuseChannel;
  449. stop_glerror();
  450. //----------------------------------------------------------------
  451. // setup current color
  452. //----------------------------------------------------------------
  453. if (is_dummy)
  454. gGL.diffuseColor4fv(LLVOAvatar::getDummyColor().mV);
  455. else
  456. gGL.diffuseColor4fv(mColor.mV);
  457. stop_glerror();
  458. LLGLSSpecular specular(LLColor4(1.f,1.f,1.f,1.f), (mFace->getPool()->getVertexShaderLevel() > 0 || LLGLSLShader::sNoFixedFunction) ? 0.f : mShiny);
  459. //----------------------------------------------------------------
  460. // setup current texture
  461. //----------------------------------------------------------------
  462. llassert( !(mTexture.notNull() && mLayerSet) ); // mutually exclusive
  463. LLTexUnit::eTextureAddressMode old_mode = LLTexUnit::TAM_WRAP;
  464. if (mTestImageName)
  465. {
  466. gGL.getTexUnit(diffuse_channel)->bindManual(LLTexUnit::TT_TEXTURE, mTestImageName);
  467. if (mIsTransparent)
  468. {
  469. gGL.diffuseColor4f(1.f, 1.f, 1.f, 1.f);
  470. }
  471. else
  472. {
  473. gGL.diffuseColor4f(0.7f, 0.6f, 0.3f, 1.f);
  474. gGL.getTexUnit(diffuse_channel)->setTextureColorBlend(LLTexUnit::TBO_LERP_TEX_ALPHA, LLTexUnit::TBS_TEX_COLOR, LLTexUnit::TBS_PREV_COLOR);
  475. }
  476. }
  477. else if( !is_dummy && mLayerSet )
  478. {
  479. if( mLayerSet->hasComposite() )
  480. {
  481. gGL.getTexUnit(diffuse_channel)->bind(mLayerSet->getComposite());
  482. }
  483. else
  484. {
  485. gGL.getTexUnit(diffuse_channel)->bind(LLViewerTextureManager::getFetchedTexture(IMG_DEFAULT));
  486. }
  487. }
  488. else
  489. if ( !is_dummy && mTexture.notNull() )
  490. {
  491. if(mTexture->hasGLTexture())
  492. {
  493. old_mode = mTexture->getAddressMode();
  494. }
  495. gGL.getTexUnit(diffuse_channel)->bind(mTexture.get());
  496. gGL.getTexUnit(diffuse_channel)->bind(mTexture);
  497. gGL.getTexUnit(diffuse_channel)->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  498. }
  499. else
  500. {
  501. gGL.getTexUnit(diffuse_channel)->bind(LLViewerTextureManager::getFetchedTexture(IMG_DEFAULT));
  502. }
  503. U32 mask = sRenderMask;
  504. U32 start = mMesh->mFaceVertexOffset;
  505. U32 end = start + mMesh->mFaceVertexCount - 1;
  506. U32 count = mMesh->mFaceIndexCount;
  507. U32 offset = mMesh->mFaceIndexOffset;
  508. LLVertexBuffer* buff = mFace->getVertexBuffer();
  509. if (mMesh->hasWeights())
  510. {
  511. if ((mFace->getPool()->getVertexShaderLevel() > 0))
  512. {
  513. if (first_pass)
  514. {
  515. uploadJointMatrices();
  516. }
  517. mask = mask | LLVertexBuffer::MAP_WEIGHT;
  518. if (mFace->getPool()->getVertexShaderLevel() > 1)
  519. {
  520. mask = mask | LLVertexBuffer::MAP_CLOTHWEIGHT;
  521. }
  522. }
  523. buff->setBuffer(mask);
  524. buff->drawRange(LLRender::TRIANGLES, start, end, count, offset);
  525. }
  526. else
  527. {
  528. gGL.pushMatrix();
  529. LLMatrix4 jointToWorld = getWorldMatrix();
  530. gGL.multMatrix((GLfloat*)jointToWorld.mMatrix);
  531. buff->setBuffer(mask);
  532. buff->drawRange(LLRender::TRIANGLES, start, end, count, offset);
  533. gGL.popMatrix();
  534. }
  535. gPipeline.addTrianglesDrawn(count);
  536. triangle_count += count;
  537. if (mTestImageName)
  538. {
  539. gGL.getTexUnit(diffuse_channel)->setTextureBlendType(LLTexUnit::TB_MULT);
  540. }
  541. if (mTexture.notNull() && !is_dummy)
  542. {
  543. gGL.getTexUnit(diffuse_channel)->bind(mTexture);
  544. gGL.getTexUnit(diffuse_channel)->setTextureAddressMode(old_mode);
  545. }
  546. return triangle_count;
  547. }
  548. //-----------------------------------------------------------------------------
  549. // updateFaceSizes()
  550. //-----------------------------------------------------------------------------
  551. void LLViewerJointMesh::updateFaceSizes(U32 &num_vertices, U32& num_indices, F32 pixel_area)
  552. {
  553. //bump num_vertices to next multiple of 4
  554. num_vertices = (num_vertices + 0x3) & ~0x3;
  555. // Do a pre-alloc pass to determine sizes of data.
  556. if (mMesh && mValid)
  557. {
  558. mMesh->mFaceVertexOffset = num_vertices;
  559. mMesh->mFaceVertexCount = mMesh->getNumVertices();
  560. mMesh->mFaceIndexOffset = num_indices;
  561. mMesh->mFaceIndexCount = mMesh->getSharedData()->mNumTriangleIndices;
  562. mMesh->getReferenceMesh()->mCurVertexCount = mMesh->mFaceVertexCount;
  563. num_vertices += mMesh->getNumVertices();
  564. num_indices += mMesh->mFaceIndexCount;
  565. }
  566. }
  567. //-----------------------------------------------------------------------------
  568. // updateFaceData()
  569. //-----------------------------------------------------------------------------
  570. static LLFastTimer::DeclareTimer FTM_AVATAR_FACE("Avatar Face");
  571. void LLViewerJointMesh::updateFaceData(LLFace *face, F32 pixel_area, BOOL damp_wind, bool terse_update)
  572. {
  573. //IF THIS FUNCTION BREAKS, SEE LLPOLYMESH CONSTRUCTOR AND CHECK ALIGNMENT OF INPUT ARRAYS
  574. mFace = face;
  575. if (!mFace->getVertexBuffer())
  576. {
  577. return;
  578. }
  579. LLDrawPool *poolp = mFace->getPool();
  580. BOOL hardware_skinning = (poolp && poolp->getVertexShaderLevel() > 0) ? TRUE : FALSE;
  581. if (!hardware_skinning && terse_update)
  582. { //no need to do terse updates if we're doing software vertex skinning
  583. // since mMesh is being copied into mVertexBuffer every frame
  584. return;
  585. }
  586. LLFastTimer t(FTM_AVATAR_FACE);
  587. LLStrider<LLVector3> verticesp;
  588. LLStrider<LLVector3> normalsp;
  589. LLStrider<LLVector2> tex_coordsp;
  590. LLStrider<F32> vertex_weightsp;
  591. LLStrider<LLVector4> clothing_weightsp;
  592. LLStrider<U16> indicesp;
  593. // Copy data into the faces from the polymesh data.
  594. if (mMesh && mValid)
  595. {
  596. const U32 num_verts = mMesh->getNumVertices();
  597. if (num_verts)
  598. {
  599. face->getVertexBuffer()->getIndexStrider(indicesp);
  600. face->getGeometryAvatar(verticesp, normalsp, tex_coordsp, vertex_weightsp, clothing_weightsp);
  601. verticesp += mMesh->mFaceVertexOffset;
  602. normalsp += mMesh->mFaceVertexOffset;
  603. F32* v = (F32*) verticesp.get();
  604. F32* n = (F32*) normalsp.get();
  605. U32 words = num_verts*4;
  606. LLVector4a::memcpyNonAliased16(v, (F32*) mMesh->getCoords(), words*sizeof(F32));
  607. LLVector4a::memcpyNonAliased16(n, (F32*) mMesh->getNormals(), words*sizeof(F32));
  608. if (!terse_update)
  609. {
  610. vertex_weightsp += mMesh->mFaceVertexOffset;
  611. clothing_weightsp += mMesh->mFaceVertexOffset;
  612. tex_coordsp += mMesh->mFaceVertexOffset;
  613. F32* tc = (F32*) tex_coordsp.get();
  614. F32* vw = (F32*) vertex_weightsp.get();
  615. F32* cw = (F32*) clothing_weightsp.get();
  616. LLVector4a::memcpyNonAliased16(tc, (F32*) mMesh->getTexCoords(), num_verts*2*sizeof(F32));
  617. LLVector4a::memcpyNonAliased16(vw, (F32*) mMesh->getWeights(), num_verts*sizeof(F32));
  618. LLVector4a::memcpyNonAliased16(cw, (F32*) mMesh->getClothingWeights(), num_verts*4*sizeof(F32));
  619. }
  620. const U32 idx_count = mMesh->getNumFaces()*3;
  621. indicesp += mMesh->mFaceIndexOffset;
  622. U16* __restrict idx = indicesp.get();
  623. S32* __restrict src_idx = (S32*) mMesh->getFaces();
  624. const S32 offset = (S32) mMesh->mFaceVertexOffset;
  625. for (S32 i = 0; i < idx_count; ++i)
  626. {
  627. *(idx++) = *(src_idx++)+offset;
  628. }
  629. }
  630. }
  631. }
  632. //-----------------------------------------------------------------------------
  633. // updateLOD()
  634. //-----------------------------------------------------------------------------
  635. BOOL LLViewerJointMesh::updateLOD(F32 pixel_area, BOOL activate)
  636. {
  637. BOOL valid = mValid;
  638. setValid(activate, TRUE);
  639. return (valid != activate);
  640. }
  641. // static
  642. void LLViewerJointMesh::updateGeometry(LLFace *mFace, LLPolyMesh *mMesh)
  643. {
  644. LLStrider<LLVector3> o_vertices;
  645. LLStrider<LLVector3> o_normals;
  646. //get vertex and normal striders
  647. LLVertexBuffer* buffer = mFace->getVertexBuffer();
  648. buffer->getVertexStrider(o_vertices, 0);
  649. buffer->getNormalStrider(o_normals, 0);
  650. F32* __restrict vert = o_vertices[0].mV;
  651. F32* __restrict norm = o_normals[0].mV;
  652. const F32* __restrict weights = mMesh->getWeights();
  653. const LLVector4a* __restrict coords = (LLVector4a*) mMesh->getCoords();
  654. const LLVector4a* __restrict normals = (LLVector4a*) mMesh->getNormals();
  655. U32 offset = mMesh->mFaceVertexOffset*4;
  656. vert += offset;
  657. norm += offset;
  658. for (U32 index = 0; index < mMesh->getNumVertices(); index++)
  659. {
  660. // equivalent to joint = floorf(weights[index]);
  661. S32 joint = _mm_cvtt_ss2si(_mm_load_ss(weights+index));
  662. F32 w = weights[index] - joint;
  663. LLMatrix4a gBlendMat;
  664. if (w != 0.f)
  665. {
  666. // blend between matrices and apply
  667. gBlendMat.setLerp(gJointMatAligned[joint+0],
  668. gJointMatAligned[joint+1], w);
  669. LLVector4a res;
  670. gBlendMat.affineTransform(coords[index], res);
  671. res.store4a(vert+index*4);
  672. gBlendMat.rotate(normals[index], res);
  673. res.store4a(norm+index*4);
  674. }
  675. else
  676. { // No lerp required in this case.
  677. LLVector4a res;
  678. gJointMatAligned[joint].affineTransform(coords[index], res);
  679. res.store4a(vert+index*4);
  680. gJointMatAligned[joint].rotate(normals[index], res);
  681. res.store4a(norm+index*4);
  682. }
  683. }
  684. buffer->flush();
  685. }
  686. void LLViewerJointMesh::updateJointGeometry()
  687. {
  688. if (!(mValid
  689. && mMesh
  690. && mFace
  691. && mMesh->hasWeights()
  692. && mFace->getVertexBuffer()
  693. && LLViewerShaderMgr::instance()->getVertexShaderLevel(LLViewerShaderMgr::SHADER_AVATAR) == 0))
  694. {
  695. return;
  696. }
  697. uploadJointMatrices();
  698. updateGeometry(mFace, mMesh);
  699. }
  700. void LLViewerJointMesh::dump()
  701. {
  702. if (mValid)
  703. {
  704. llinfos << "Usable LOD " << mName << llendl;
  705. }
  706. }
  707. // End