llviewerjointmesh.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /**
  2. * @file llviewerjointmesh.cpp
  3. * @brief Implementation of LLViewerJointMesh class
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "llviewerprecompiledheaders.h"
  33. #include "llviewerjointmesh.h"
  34. #include "imageids.h"
  35. #include "llfasttimer.h"
  36. #include "llgl.h"
  37. #include "llrender.h"
  38. #include "lldrawable.h"
  39. #include "lldrawpoolavatar.h"
  40. #include "lldrawpoolbump.h"
  41. #include "lldynamictexture.h"
  42. #include "llface.h"
  43. #include "llpipeline.h"
  44. #include "llviewercamera.h"
  45. #include "llviewercontrol.h"
  46. #include "llviewershadermgr.h"
  47. #include "llviewertexlayer.h"
  48. #include "llviewertexturelist.h"
  49. #include "llvoavatar.h"
  50. constexpr U32 JOINT_MESH_RENDER_MASK = LLVertexBuffer::MAP_VERTEX |
  51. LLVertexBuffer::MAP_NORMAL |
  52. LLVertexBuffer::MAP_TEXCOORD0;
  53. LLViewerJointMesh::LLViewerJointMesh()
  54. : LLAvatarJointMesh()
  55. {
  56. }
  57. constexpr S32 NUM_AXES = 3;
  58. // Register layout
  59. // rotation X 0-n
  60. // rotation Y 0-n
  61. // rotation Z 0-n
  62. // pivot parent 0-n -- child = n+1
  63. static LLMatrix4 gJointMatUnaligned[32];
  64. static LLMatrix4a gJointMatAligned[32];
  65. static LLMatrix3 gJointRotUnaligned[32];
  66. static LLVector4 gJointPivot[32];
  67. void LLViewerJointMesh::uploadJointMatrices()
  68. {
  69. LLPolyMesh* reference_mesh = mMesh->getReferenceMesh();
  70. if (!reference_mesh) // Paranoia
  71. {
  72. llwarns << "NULL mMesh !" << llendl;
  73. return;
  74. }
  75. S32 joint_count = reference_mesh->mJointRenderData.size();
  76. LLDrawPool* poolp = mFace ? mFace->getPool() : NULL;
  77. bool hardware_skinning = poolp && poolp->getShaderLevel() > 0;
  78. // Calculate joint matrices
  79. for (S32 joint_num = 0; joint_num < joint_count; ++joint_num)
  80. {
  81. LLMatrix4 joint_mat =
  82. *reference_mesh->mJointRenderData[joint_num]->mWorldMatrix;
  83. if (hardware_skinning)
  84. {
  85. joint_mat *= LLDrawPoolAvatar::getModelView();
  86. }
  87. gJointMatUnaligned[joint_num] = joint_mat;
  88. gJointRotUnaligned[joint_num] = joint_mat.getMat3();
  89. }
  90. // Upload joint pivots
  91. bool last_pivot_uploaded = false;
  92. S32 j = 0;
  93. for (S32 joint_num = 0; joint_num < joint_count; ++joint_num)
  94. {
  95. LLSkinJoint* sj =
  96. reference_mesh->mJointRenderData[joint_num]->mSkinJoint;
  97. if (sj)
  98. {
  99. if (!last_pivot_uploaded)
  100. {
  101. LLVector4 parent_pivot(sj->mRootToParentJointSkinOffset);
  102. parent_pivot.mV[VW] = 0.f;
  103. gJointPivot[j++] = parent_pivot;
  104. }
  105. LLVector4 child_pivot(sj->mRootToJointSkinOffset);
  106. child_pivot.mV[VW] = 0.f;
  107. gJointPivot[j++] = child_pivot;
  108. last_pivot_uploaded = true;
  109. }
  110. else
  111. {
  112. last_pivot_uploaded = false;
  113. }
  114. }
  115. // Add pivot point into transform
  116. for (S32 i = 0; i < j; ++i)
  117. {
  118. LLVector3 pivot;
  119. pivot = LLVector3(gJointPivot[i]);
  120. pivot = pivot * gJointRotUnaligned[i];
  121. gJointMatUnaligned[i].translate(pivot);
  122. }
  123. // Upload matrices
  124. if (hardware_skinning)
  125. {
  126. F32 mat[45 * 4];
  127. memset(mat, 0, sizeof(F32) * 45 * 4);
  128. for (S32 joint_num = 0; joint_num < joint_count; ++joint_num)
  129. {
  130. gJointMatUnaligned[joint_num].transpose();
  131. for (S32 axis = 0; axis < NUM_AXES; ++axis)
  132. {
  133. F32* vector = gJointMatUnaligned[joint_num].mMatrix[axis];
  134. U32 offset = LL_CHARACTER_MAX_JOINTS_PER_MESH * axis +
  135. joint_num;
  136. memcpy(mat + offset * 4, vector, sizeof(F32) * 4);
  137. }
  138. }
  139. if (LLGLSLShader::sCurBoundShaderPtr)
  140. {
  141. LLGLSLShader::sCurBoundShaderPtr->uniform4fv(LLShaderMgr::AVATAR_MATRIX,
  142. 45, mat);
  143. }
  144. stop_glerror();
  145. }
  146. else
  147. {
  148. // Load gJointMatUnaligned into gJointMatAligned
  149. for (S32 joint_num = 0; joint_num < joint_count; ++joint_num)
  150. {
  151. gJointMatAligned[joint_num].loadu(gJointMatUnaligned[joint_num]);
  152. }
  153. }
  154. }
  155. // Used by the qsort function to sort the index array
  156. int compare_int(const void* a, const void* b)
  157. {
  158. if (*(U32*)a < *(U32*)b)
  159. {
  160. return -1;
  161. }
  162. else if (*(U32*)a > *(U32*)b)
  163. {
  164. return 1;
  165. }
  166. else
  167. {
  168. return 0;
  169. }
  170. }
  171. //virtual
  172. U32 LLViewerJointMesh::drawShape(bool first_pass, bool is_dummy)
  173. {
  174. if (!mValid || !mMesh || !mFace || !mVisible ||
  175. !mFace->getVertexBuffer() || mMesh->getNumFaces() == 0 ||
  176. !LLGLSLShader::sCurBoundShaderPtr)
  177. {
  178. return 0;
  179. }
  180. LLTexUnit* unit;
  181. if (LLDrawPoolAvatar::sDiffuseChannel > -1)
  182. {
  183. unit = gGL.getTexUnit(LLDrawPoolAvatar::sDiffuseChannel);
  184. }
  185. else
  186. {
  187. unit = NULL;
  188. }
  189. //----------------------------------------------------------------
  190. // Setup current color
  191. //----------------------------------------------------------------
  192. if (is_dummy)
  193. {
  194. gGL.diffuseColor4fv(LLVOAvatar::getDummyColor().mV);
  195. }
  196. else
  197. {
  198. gGL.diffuseColor4fv(mColor.mV);
  199. }
  200. LLGLSSpecular specular(LLColor4(1.f, 1.f, 1.f, 1.f), 0.f);
  201. //----------------------------------------------------------------
  202. // Setup current texture
  203. //----------------------------------------------------------------
  204. llassert(!(mTexture.notNull() && mLayerSet)); // mutually exclusive
  205. LLViewerTexLayerSet* layerset = NULL;
  206. if (mLayerSet)
  207. {
  208. layerset = mLayerSet->asViewerTexLayerSet();
  209. }
  210. if (mTestImageName)
  211. {
  212. if (unit)
  213. {
  214. unit->bindManual(LLTexUnit::TT_TEXTURE, mTestImageName);
  215. }
  216. if (mIsTransparent)
  217. {
  218. gGL.diffuseColor4f(1.f, 1.f, 1.f, 1.f);
  219. }
  220. else
  221. {
  222. gGL.diffuseColor4f(0.7f, 0.6f, 0.3f, 1.f);
  223. }
  224. }
  225. else if (!is_dummy && layerset)
  226. {
  227. if (layerset->hasComposite())
  228. {
  229. if (unit)
  230. {
  231. unit->bind(layerset->getViewerComposite());
  232. }
  233. }
  234. else
  235. {
  236. // This warning will always trigger if you have hacked the avatar
  237. // to show as incomplete. Ignore the warning if this is the case.
  238. static LLCachedControl<bool> render_unloaded_avatar(gSavedSettings,
  239. "RenderUnloadedAvatar");
  240. if (!render_unloaded_avatar)
  241. {
  242. static std::set<S32> mesh_ids;
  243. if (mesh_ids.count(mMeshID) == 0)
  244. {
  245. mesh_ids.emplace(mMeshID);
  246. llwarns << "Layerset without composite for MeshID = "
  247. << mMeshID << llendl;
  248. }
  249. }
  250. if (LLViewerFetchedTexture::sDefaultImagep && unit)
  251. {
  252. unit->bind(LLViewerFetchedTexture::sDefaultImagep);
  253. }
  254. }
  255. }
  256. else if (!is_dummy && mTexture.notNull())
  257. {
  258. if (unit)
  259. {
  260. unit->bind(mTexture);
  261. }
  262. }
  263. else if (LLViewerFetchedTexture::sDefaultImagep && unit)
  264. {
  265. unit->bind(LLViewerFetchedTexture::sDefaultImagep);
  266. }
  267. U32 mask = JOINT_MESH_RENDER_MASK;
  268. U32 start = mMesh->mFaceVertexOffset;
  269. U32 end = start + mMesh->mFaceVertexCount - 1;
  270. U32 count = mMesh->mFaceIndexCount;
  271. U32 offset = mMesh->mFaceIndexOffset;
  272. LLVertexBuffer* buff = mFace->getVertexBuffer();
  273. if (mMesh->hasWeights())
  274. {
  275. if (mFace->getPool()->getShaderLevel() > 0)
  276. {
  277. if (first_pass)
  278. {
  279. uploadJointMatrices();
  280. }
  281. mask |= LLVertexBuffer::MAP_WEIGHT;
  282. if (mFace->getPool()->getShaderLevel() > 1)
  283. {
  284. mask |= LLVertexBuffer::MAP_CLOTHWEIGHT;
  285. }
  286. }
  287. buff->setBuffer(mask);
  288. buff->drawRange(LLRender::TRIANGLES, start, end, count, offset);
  289. }
  290. else
  291. {
  292. gGL.pushMatrix();
  293. LLMatrix4 joint_to_world = getWorldMatrix();
  294. gGL.multMatrix(joint_to_world.getF32ptr());
  295. buff->setBuffer(mask);
  296. buff->drawRange(LLRender::TRIANGLES, start, end, count, offset);
  297. gGL.popMatrix();
  298. }
  299. gPipeline.addTrianglesDrawn(count);
  300. stop_glerror();
  301. return count;
  302. }
  303. void LLViewerJointMesh::updateFaceSizes(U32 &num_vertices, U32& num_indices,
  304. F32 pixel_area)
  305. {
  306. // Bump num_vertices to next multiple of 4
  307. num_vertices = (num_vertices + 0x3) & ~0x3;
  308. // Do a pre-alloc pass to determine sizes of data.
  309. if (mMesh && mValid)
  310. {
  311. mMesh->mFaceVertexOffset = num_vertices;
  312. mMesh->mFaceVertexCount = mMesh->getNumVertices();
  313. mMesh->mFaceIndexOffset = num_indices;
  314. mMesh->mFaceIndexCount = mMesh->getSharedData()->mNumTriangleIndices;
  315. mMesh->getReferenceMesh()->mCurVertexCount = mMesh->mFaceVertexCount;
  316. num_vertices += mMesh->getNumVertices();
  317. num_indices += mMesh->mFaceIndexCount;
  318. }
  319. }
  320. // IF THIS FUNCTION BREAKS, SEE LLPOLYMESH CONSTRUCTOR AND CHECK ALIGNMENT OF
  321. // INPUT ARRAYS
  322. void LLViewerJointMesh::updateFaceData(LLFace* face, F32 pixel_area,
  323. bool damp_wind, bool terse_update)
  324. {
  325. mFace = face;
  326. if (!mFace->getVertexBuffer())
  327. {
  328. return;
  329. }
  330. LLDrawPool* poolp = mFace->getPool();
  331. bool hardware_skinning = poolp && poolp->getShaderLevel() > 0;
  332. if (!hardware_skinning && terse_update)
  333. {
  334. // No need to do terse updates if we're doing software vertex skinning
  335. // since mMesh is being copied into mVertexBuffer every frame
  336. return;
  337. }
  338. LL_FAST_TIMER(FTM_AVATAR_FACE);
  339. LLStrider<LLVector3> verticesp;
  340. LLStrider<LLVector3> normalsp;
  341. LLStrider<LLVector2> tex_coordsp;
  342. LLStrider<F32> vertex_weightsp;
  343. LLStrider<LLVector4a> clothing_weightsp;
  344. LLStrider<U16> indicesp;
  345. // Copy data into the faces from the polymesh data.
  346. if (mMesh && mValid)
  347. {
  348. const U32 num_verts = mMesh->getNumVertices();
  349. if (num_verts)
  350. {
  351. face->getGeometryAvatar(verticesp, normalsp, tex_coordsp,
  352. vertex_weightsp, clothing_weightsp);
  353. if (!face->getVertexBuffer()->getIndexStrider(indicesp))
  354. {
  355. return;
  356. }
  357. verticesp += mMesh->mFaceVertexOffset;
  358. normalsp += mMesh->mFaceVertexOffset;
  359. F32* v = (F32*)verticesp.get();
  360. F32* n = (F32*)normalsp.get();
  361. U32 words = num_verts * 4;
  362. LLVector4a::memcpyNonAliased16(v, (F32*)mMesh->getCoords(),
  363. words * sizeof(F32));
  364. LLVector4a::memcpyNonAliased16(n, (F32*)mMesh->getNormals(),
  365. words * sizeof(F32));
  366. if (!terse_update)
  367. {
  368. vertex_weightsp += mMesh->mFaceVertexOffset;
  369. clothing_weightsp += mMesh->mFaceVertexOffset;
  370. tex_coordsp += mMesh->mFaceVertexOffset;
  371. F32* tc = (F32*)tex_coordsp.get();
  372. F32* vw = (F32*)vertex_weightsp.get();
  373. F32* cw = (F32*)clothing_weightsp.get();
  374. S32 tc_size = (num_verts * 2 * sizeof(F32) + 0xF) & ~0xF;
  375. LLVector4a::memcpyNonAliased16(tc, (F32*)mMesh->getTexCoords(),
  376. tc_size);
  377. S32 vw_size = (num_verts * sizeof(F32) + 0xF) & ~0xF;
  378. LLVector4a::memcpyNonAliased16(vw, (F32*)mMesh->getWeights(),
  379. vw_size);
  380. LLVector4a::memcpyNonAliased16(cw,
  381. (F32*)mMesh->getClothingWeights(),
  382. num_verts * 4 * sizeof(F32));
  383. }
  384. const U32 idx_count = mMesh->getNumFaces() * 3;
  385. indicesp += mMesh->mFaceIndexOffset;
  386. U16* __restrict idx = indicesp.get();
  387. S32* __restrict src_idx = (S32*)mMesh->getFaces();
  388. const S32 offset = (S32)mMesh->mFaceVertexOffset;
  389. for (U32 i = 0; i < idx_count; ++i)
  390. {
  391. *(idx++) = *(src_idx++) + offset;
  392. }
  393. }
  394. }
  395. }
  396. bool LLViewerJointMesh::updateLOD(F32 pixel_area, bool activate)
  397. {
  398. bool valid = mValid;
  399. setValid(activate, true);
  400. return valid != activate;
  401. }
  402. //static
  403. void LLViewerJointMesh::updateGeometry(LLFace* mFace, LLPolyMesh* mMesh)
  404. {
  405. LLStrider<LLVector3> o_vertices;
  406. LLStrider<LLVector3> o_normals;
  407. // Get vertex and normal striders
  408. LLVertexBuffer* buffer = mFace->getVertexBuffer();
  409. if (!buffer->getVertexStrider(o_vertices, 0) ||
  410. !buffer->getNormalStrider(o_normals, 0))
  411. {
  412. return;
  413. }
  414. F32* __restrict vert = o_vertices[0].mV;
  415. F32* __restrict norm = o_normals[0].mV;
  416. const F32* __restrict weights = mMesh->getWeights();
  417. const LLVector4a* __restrict coords = (LLVector4a*)mMesh->getCoords();
  418. const LLVector4a* __restrict normals = (LLVector4a*)mMesh->getNormals();
  419. U32 offset = mMesh->mFaceVertexOffset * 4;
  420. vert += offset;
  421. norm += offset;
  422. for (U32 index = 0, count = mMesh->getNumVertices(); index < count;
  423. ++index)
  424. {
  425. // Equivalent to joint = floorf(weights[index]);
  426. S32 joint = _mm_cvtt_ss2si(_mm_load_ss(weights+index));
  427. F32 w = weights[index] - joint;
  428. LLMatrix4a gBlendMat;
  429. if (w != 0.f)
  430. {
  431. // Blend between matrices and apply
  432. gBlendMat.setLerp(gJointMatAligned[joint + 0],
  433. gJointMatAligned[joint + 1], w);
  434. LLVector4a res;
  435. gBlendMat.affineTransform(coords[index], res);
  436. res.store4a(vert + index * 4);
  437. gBlendMat.rotate(normals[index], res);
  438. res.store4a(norm + index * 4);
  439. }
  440. else
  441. { // No lerp required in this case.
  442. LLVector4a res;
  443. gJointMatAligned[joint].affineTransform(coords[index], res);
  444. res.store4a(vert + index * 4);
  445. gJointMatAligned[joint].rotate(normals[index], res);
  446. res.store4a(norm + index * 4);
  447. }
  448. }
  449. buffer->unmapBuffer();
  450. }
  451. void LLViewerJointMesh::updateJointGeometry()
  452. {
  453. if (!(mValid && mMesh && mFace &&
  454. mMesh->hasWeights() && mFace->getVertexBuffer() &&
  455. gViewerShaderMgrp->getShaderLevel(LLViewerShaderMgr::SHADER_AVATAR) == 0))
  456. {
  457. return;
  458. }
  459. uploadJointMatrices();
  460. updateGeometry(mFace, mMesh);
  461. }
  462. void LLViewerJointMesh::dump()
  463. {
  464. if (mValid)
  465. {
  466. llinfos << "Usable LOD: " << getName() << llendl;
  467. }
  468. }