1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
/**
* @file llskinningutil.cpp
* @brief Functions for mesh object skinning
* @author vir@lindenlab.com
*
* $LicenseInfo:firstyear=2015&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2015, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
#include "llviewerprecompiledheaders.h"
#include "llskinningutil.h"
#include "llvoavatar.h"
#include "llviewercontrol.h"
#include "llmeshrepository.h"
#include "llvolume.h"
#include "llrigginginfo.h"
void LLSkinningUtil::initClass()
{
}
U32 LLSkinningUtil::getMaxJointCount()
{
U32 result = LL_MAX_JOINTS_PER_MESH_OBJECT;
return result;
}
U32 LLSkinningUtil::getMeshJointCount(const LLMeshSkinInfo *skin)
{
return llmin((U32)getMaxJointCount(), (U32)skin->mJointNames.size());
}
void LLSkinningUtil::scrubInvalidJoints(LLVOAvatar *avatar, LLMeshSkinInfo* skin)
{
if (skin->mInvalidJointsScrubbed)
{
return;
}
for (U32 j = 0; j < skin->mJointNames.size(); ++j)
{
// Fix invalid names to "mPelvis". Currently meshes with
// invalid names will be blocked on upload, so this is just
// needed for handling of any legacy bad data.
if (!avatar->getJoint(skin->mJointNames[j]))
{
LL_DEBUGS("Avatar") << "Mesh rigged to invalid joint" << skin->mJointNames[j] << LL_ENDL;
LL_WARNS_ONCE("Avatar") << "Mesh rigged to invalid joint" << skin->mJointNames[j] << LL_ENDL;
skin->mJointNames[j] = "mPelvis";
}
}
skin->mInvalidJointsScrubbed = true;
}
void LLSkinningUtil::initSkinningMatrixPalette(
LLMatrix4* mat,
S32 count,
const LLMeshSkinInfo* skin,
LLVOAvatar *avatar)
{
initJointNums(const_cast<LLMeshSkinInfo*>(skin), avatar);
for (U32 j = 0; j < count; ++j)
{
LLJoint *joint = avatar->getJoint(skin->mJointNums[j]);
if (joint)
{
#define MAT_USE_SSE
#ifdef MAT_USE_SSE
LLMatrix4a bind, world, res;
bind.loadu(skin->mInvBindMatrix[j]);
world.loadu(joint->getWorldMatrix());
matMul(bind,world,res);
memcpy(mat[j].mMatrix,res.mMatrix,16*sizeof(float));
#else
mat[j] = skin->mInvBindMatrix[j];
mat[j] *= joint->getWorldMatrix();
#endif
}
else
{
mat[j] = skin->mInvBindMatrix[j];
// This shouldn't happen - in mesh upload, skinned
// rendering should be disabled unless all joints are
// valid. In other cases of skinned rendering, invalid
// joints should already have been removed during scrubInvalidJoints().
LL_WARNS_ONCE("Avatar") << "Rigged to invalid joint name " << skin->mJointNames[j] << LL_ENDL;
LL_WARNS_ONCE() << "avatar build state: isBuilt() " << avatar->isBuilt() << " mInitFlags " << avatar->mInitFlags << LL_ENDL;
}
}
}
void LLSkinningUtil::checkSkinWeights(LLVector4a* weights, U32 num_vertices, const LLMeshSkinInfo* skin)
{
#ifdef SHOW_ASSERT // same condition that controls llassert()
const S32 max_joints = skin->mJointNames.size();
for (U32 j=0; j<num_vertices; j++)
{
F32 *w = weights[j].getF32ptr();
F32 wsum = 0.0;
for (U32 k=0; k<4; ++k)
{
S32 i = llfloor(w[k]);
llassert(i>=0);
llassert(i<max_joints);
wsum += w[k]-i;
}
llassert(wsum > 0.0f);
}
#endif
}
void LLSkinningUtil::scrubSkinWeights(LLVector4a* weights, U32 num_vertices, const LLMeshSkinInfo* skin)
{
const S32 max_joints = skin->mJointNames.size();
for (U32 j=0; j<num_vertices; j++)
{
F32 *w = weights[j].getF32ptr();
for (U32 k=0; k<4; ++k)
{
S32 i = llfloor(w[k]);
F32 f = w[k]-i;
i = llclamp(i,0,max_joints-1);
w[k] = i + f;
}
}
checkSkinWeights(weights, num_vertices, skin);
}
void LLSkinningUtil::getPerVertexSkinMatrix(
F32* weights,
LLMatrix4a* mat,
bool handle_bad_scale,
LLMatrix4a& final_mat,
U32 max_joints)
{
bool valid_weights = true;
final_mat.clear();
S32 idx[4];
LLVector4 wght;
F32 scale = 0.f;
for (U32 k = 0; k < 4; k++)
{
F32 w = weights[k];
// BENTO potential optimizations
// - Do clamping in unpackVolumeFaces() (once instead of every time)
// - int vs floor: if we know w is
// >= 0.0, we can use int instead of floorf; the latter
// allegedly has a lot of overhead due to ieeefp error
// checking which we should not need.
idx[k] = llclamp((S32) floorf(w), (S32)0, (S32)max_joints-1);
wght[k] = w - floorf(w);
scale += wght[k];
}
if (handle_bad_scale && scale <= 0.f)
{
wght = LLVector4(1.0f, 0.0f, 0.0f, 0.0f);
valid_weights = false;
}
else
{
// This is enforced in unpackVolumeFaces()
llassert(scale>0.f);
wght *= 1.f/scale;
}
for (U32 k = 0; k < 4; k++)
{
F32 w = wght[k];
LLMatrix4a src;
src.setMul(mat[idx[k]], w);
final_mat.add(src);
}
// SL-366 - with weight validation/cleanup code, it should no longer be
// possible to hit the bad scale case.
llassert(valid_weights);
}
void LLSkinningUtil::initJointNums(LLMeshSkinInfo* skin, LLVOAvatar *avatar)
{
if (!skin->mJointNumsInitialized)
{
for (U32 j = 0; j < skin->mJointNames.size(); ++j)
{
LLJoint *joint = NULL;
if (skin->mJointNums[j] == -1)
{
joint = avatar->getJoint(skin->mJointNames[j]);
if (joint)
{
skin->mJointNums[j] = joint->getJointNum();
if (skin->mJointNums[j] < 0)
{
LL_WARNS_ONCE() << "joint has unusual number " << skin->mJointNames[j] << ": " << skin->mJointNums[j] << LL_ENDL;
LL_WARNS_ONCE() << "avatar build state: isBuilt() " << avatar->isBuilt() << " mInitFlags " << avatar->mInitFlags << LL_ENDL;
}
}
else
{
LL_WARNS_ONCE() << "unable to find joint " << skin->mJointNames[j] << LL_ENDL;
LL_WARNS_ONCE() << "avatar build state: isBuilt() " << avatar->isBuilt() << " mInitFlags " << avatar->mInitFlags << LL_ENDL;
}
}
}
skin->mJointNumsInitialized = true;
}
}
static LLTrace::BlockTimerStatHandle FTM_FACE_RIGGING_INFO("Face Rigging Info");
void LLSkinningUtil::updateRiggingInfo(const LLMeshSkinInfo* skin, LLVOAvatar *avatar, LLVolumeFace& vol_face)
{
LL_RECORD_BLOCK_TIME(FTM_FACE_RIGGING_INFO);
if (vol_face.mJointRiggingInfoTab.needsUpdate())
{
S32 num_verts = vol_face.mNumVertices;
if (num_verts>0 && vol_face.mWeights && (skin->mJointNames.size()>0))
{
initJointNums(const_cast<LLMeshSkinInfo*>(skin), avatar);
if (vol_face.mJointRiggingInfoTab.size()==0)
{
//std::set<S32> active_joints;
//S32 active_verts = 0;
vol_face.mJointRiggingInfoTab.resize(LL_CHARACTER_MAX_ANIMATED_JOINTS);
LLJointRiggingInfoTab &rig_info_tab = vol_face.mJointRiggingInfoTab;
for (S32 i=0; i<vol_face.mNumVertices; i++)
{
LLVector4a& pos = vol_face.mPositions[i];
F32 *weights = vol_face.mWeights[i].getF32ptr();
LLVector4 wght;
S32 idx[4];
F32 scale = 0.0f;
// AXON unpacking of weights should be pulled into a common function and optimized if possible.
for (U32 k = 0; k < 4; k++)
{
F32 w = weights[k];
idx[k] = llclamp((S32) floorf(w), (S32)0, (S32)LL_CHARACTER_MAX_ANIMATED_JOINTS-1);
wght[k] = w - idx[k];
scale += wght[k];
}
if (scale > 0.0f)
{
for (U32 k=0; k<4; ++k)
{
wght[k] /= scale;
}
}
for (U32 k=0; k<4; ++k)
{
S32 joint_index = idx[k];
if (wght[k] > 0.0f)
{
S32 joint_num = skin->mJointNums[joint_index];
if (joint_num >= 0 && joint_num < LL_CHARACTER_MAX_ANIMATED_JOINTS)
{
rig_info_tab[joint_num].setIsRiggedTo(true);
// AXON can precompute these matMuls.
LLMatrix4a bind_shape;
bind_shape.loadu(skin->mBindShapeMatrix);
LLMatrix4a inv_bind;
inv_bind.loadu(skin->mInvBindMatrix[joint_index]);
LLMatrix4a mat;
matMul(bind_shape, inv_bind, mat);
LLVector4a pos_joint_space;
mat.affineTransform(pos, pos_joint_space);
pos_joint_space.mul(wght[k]);
LLVector4a *extents = rig_info_tab[joint_num].getRiggedExtents();
update_min_max(extents[0], extents[1], pos_joint_space);
}
}
}
}
//LL_DEBUGS("RigSpammish") << "built rigging info for vf " << &vol_face
// << " num_verts " << vol_face.mNumVertices
// << " active joints " << active_joints.size()
// << " active verts " << active_verts
// << LL_ENDL;
vol_face.mJointRiggingInfoTab.setNeedsUpdate(false);
}
}
if (vol_face.mJointRiggingInfoTab.size()!=0)
{
LL_DEBUGS("RigSpammish") << "we have rigging info for vf " << &vol_face
<< " num_verts " << vol_face.mNumVertices << LL_ENDL;
}
else
{
LL_DEBUGS("RigSpammish") << "no rigging info for vf " << &vol_face
<< " num_verts " << vol_face.mNumVertices << LL_ENDL;
}
}
}
|