handle no-bones case for skeletal animation
[xonotic/darkplaces.git] / mod_skeletal_animatevertices_sse.c
1 #include "mod_skeletal_animatevertices_sse.h"
2
3 #ifdef SSE_POSSIBLE
4
5 #ifdef MATRIX4x4_OPENGLORIENTATION
6 #error "SSE skeletal requires D3D matrix layout"
7 #endif
8
9 #include <xmmintrin.h>
10
11 void Mod_Skeletal_AnimateVertices_SSE(const dp_model_t * RESTRICT model, const frameblend_t * RESTRICT frameblend, const skeleton_t *skeleton, float * RESTRICT vertex3f, float * RESTRICT normal3f, float * RESTRICT svector3f, float * RESTRICT tvector3f)
12 {
13         // vertex weighted skeletal
14         int i, k;
15         int blends;
16         matrix4x4_t *bonepose;
17         matrix4x4_t *boneposerelative;
18         float m[12];
19         matrix4x4_t mm, mm2;
20         const blendweights_t * RESTRICT weights;
21         int num_vertices_minus_one;
22
23         if (!model->surfmesh.num_vertices)
24                 return;
25
26         if (!model->num_bones)
27         {
28                 if (vertex3f) memcpy(vertex3f, model->surfmesh.data_vertex3f, model->surfmesh.num_vertices*sizeof(float[3]));
29                 if (normal3f) memcpy(normal3f, model->surfmesh.data_normal3f, model->surfmesh.num_vertices*sizeof(float[3]));
30                 if (svector3f) memcpy(svector3f, model->surfmesh.data_svector3f, model->surfmesh.num_vertices*sizeof(float[3]));
31                 if (tvector3f) memcpy(tvector3f, model->surfmesh.data_tvector3f, model->surfmesh.num_vertices*sizeof(float[3]));
32                 return;
33         }
34
35         num_vertices_minus_one = model->surfmesh.num_vertices - 1;
36
37         //unsigned long long ts = rdtsc();
38         bonepose = (matrix4x4_t *) Mod_Skeletal_AnimateVertices_AllocBuffers(sizeof(matrix4x4_t) * (model->num_bones*2 + model->surfmesh.num_blends));
39         boneposerelative = bonepose + model->num_bones;
40
41         if (skeleton && !skeleton->relativetransforms)
42                 skeleton = NULL;
43
44         // interpolate matrices
45         if (skeleton)
46         {
47                 for (i = 0;i < model->num_bones;i++)
48                 {
49                         // relativetransforms is in GL column-major order, which is what we need for SSE
50                         // transposed style processing
51                         if (model->data_bones[i].parent >= 0)
52                                 Matrix4x4_Concat(&bonepose[i], &bonepose[model->data_bones[i].parent], &skeleton->relativetransforms[i]);
53                         else
54                                 memcpy(&bonepose[i], &skeleton->relativetransforms[i], sizeof(matrix4x4_t));
55
56                         // create a relative deformation matrix to describe displacement
57                         // from the base mesh, which is used by the actual weighting
58                         Matrix4x4_FromArray12FloatD3D(&mm, model->data_baseboneposeinverse + i * 12); // baseboneposeinverse is 4x3 row-major
59                         Matrix4x4_Concat(&mm2, &bonepose[i], &mm);
60                         Matrix4x4_Transpose(&boneposerelative[i], &mm2); // TODO: Eliminate this transpose
61                 }
62         }
63         else
64         {
65                 float originscale = model->num_posescale;
66                 float x,y,z,w,lerp;
67                 const short * RESTRICT pose6s;
68
69                 for (i = 0;i < model->num_bones;i++)
70                 {
71                         memset(m, 0, sizeof(m));
72                         for (blends = 0;blends < MAX_FRAMEBLENDS && frameblend[blends].lerp > 0;blends++)
73                         {
74                                 pose6s = model->data_poses6s + 6 * (frameblend[blends].subframe * model->num_bones + i);
75                                 lerp = frameblend[blends].lerp;
76                                 x = pose6s[3] * (1.0f / 32767.0f);
77                                 y = pose6s[4] * (1.0f / 32767.0f);
78                                 z = pose6s[5] * (1.0f / 32767.0f);
79                                 w = 1.0f - (x*x+y*y+z*z);
80                                 w = w > 0.0f ? -sqrt(w) : 0.0f;
81                                 m[ 0] += (1-2*(y*y+z*z)) * lerp;
82                                 m[ 1] += (  2*(x*y-z*w)) * lerp;
83                                 m[ 2] += (  2*(x*z+y*w)) * lerp;
84                                 m[ 3] += (pose6s[0] * originscale) * lerp;
85                                 m[ 4] += (  2*(x*y+z*w)) * lerp;
86                                 m[ 5] += (1-2*(x*x+z*z)) * lerp;
87                                 m[ 6] += (  2*(y*z-x*w)) * lerp;
88                                 m[ 7] += (pose6s[1] * originscale) * lerp;
89                                 m[ 8] += (  2*(x*z-y*w)) * lerp;
90                                 m[ 9] += (  2*(y*z+x*w)) * lerp;
91                                 m[10] += (1-2*(x*x+y*y)) * lerp;
92                                 m[11] += (pose6s[2] * originscale) * lerp;
93                         }
94                         VectorNormalize(m       );
95                         VectorNormalize(m + 4);
96                         VectorNormalize(m + 8);
97                         if (i == r_skeletal_debugbone.integer)
98                                 m[r_skeletal_debugbonecomponent.integer % 12] += r_skeletal_debugbonevalue.value;
99                         m[3] *= r_skeletal_debugtranslatex.value;
100                         m[7] *= r_skeletal_debugtranslatey.value;
101                         m[11] *= r_skeletal_debugtranslatez.value;
102                         Matrix4x4_FromArray12FloatD3D(&mm, m);
103                         if (model->data_bones[i].parent >= 0)
104                                 Matrix4x4_Concat(&bonepose[i], &bonepose[model->data_bones[i].parent], &mm);
105                         else
106                                 memcpy(&bonepose[i], &mm, sizeof(mm));
107                         // create a relative deformation matrix to describe displacement
108                         // from the base mesh, which is used by the actual weighting
109                         Matrix4x4_FromArray12FloatD3D(&mm, model->data_baseboneposeinverse + i * 12); // baseboneposeinverse is 4x3 row-major
110                         Matrix4x4_Concat(&mm2, &bonepose[i], &mm);
111                         Matrix4x4_Transpose(&boneposerelative[i], &mm2); // TODO: Eliminate this transpose
112                 }
113         }
114
115         // generate matrices for all blend combinations
116         weights = model->surfmesh.data_blendweights;
117         for (i = 0;i < model->surfmesh.num_blends;i++, weights++)
118         {
119                 float * RESTRICT b = &boneposerelative[model->num_bones + i].m[0][0];
120                 const float * RESTRICT m = &boneposerelative[weights->index[0]].m[0][0];
121                 float f = weights->influence[0] * (1.0f / 255.0f);
122                 __m128 fv = _mm_set_ps1(f);
123                 __m128 b0 = _mm_load_ps(m);
124                 __m128 b1 = _mm_load_ps(m+4);
125                 __m128 b2 = _mm_load_ps(m+8);
126                 __m128 b3 = _mm_load_ps(m+12);
127                 __m128 m0, m1, m2, m3;
128                 b0 = _mm_mul_ps(b0, fv);
129                 b1 = _mm_mul_ps(b1, fv);
130                 b2 = _mm_mul_ps(b2, fv);
131                 b3 = _mm_mul_ps(b3, fv);
132                 for (k = 1;k < 4 && weights->influence[k];k++)
133                 {
134                         m = &boneposerelative[weights->index[k]].m[0][0];
135                         f = weights->influence[k] * (1.0f / 255.0f);
136                         fv = _mm_set_ps1(f);
137                         m0 = _mm_load_ps(m);
138                         m1 = _mm_load_ps(m+4);
139                         m2 = _mm_load_ps(m+8);
140                         m3 = _mm_load_ps(m+12);
141                         m0 = _mm_mul_ps(m0, fv);
142                         m1 = _mm_mul_ps(m1, fv);
143                         m2 = _mm_mul_ps(m2, fv);
144                         m3 = _mm_mul_ps(m3, fv);
145                         b0 = _mm_add_ps(m0, b0);
146                         b1 = _mm_add_ps(m1, b1);
147                         b2 = _mm_add_ps(m2, b2);
148                         b3 = _mm_add_ps(m3, b3);
149                 }
150                 _mm_store_ps(b, b0);
151                 _mm_store_ps(b+4, b1);
152                 _mm_store_ps(b+8, b2);
153                 _mm_store_ps(b+12, b3);
154         }
155
156 #define LOAD_MATRIX_SCALAR() const float * RESTRICT m = &boneposerelative[*b].m[0][0]
157
158 #define LOAD_MATRIX3() \
159         const float * RESTRICT m = &boneposerelative[*b].m[0][0]; \
160         /* bonepose array is 16 byte aligned */ \
161         __m128 m1 = _mm_load_ps((m)); \
162         __m128 m2 = _mm_load_ps((m)+4); \
163         __m128 m3 = _mm_load_ps((m)+8);
164 #define LOAD_MATRIX4() \
165         const float * RESTRICT m = &boneposerelative[*b].m[0][0]; \
166         /* bonepose array is 16 byte aligned */ \
167         __m128 m1 = _mm_load_ps((m)); \
168         __m128 m2 = _mm_load_ps((m)+4); \
169         __m128 m3 = _mm_load_ps((m)+8); \
170         __m128 m4 = _mm_load_ps((m)+12)
171
172         /* Note that matrix is 4x4 and transposed compared to non-USE_SSE codepath */
173 #define TRANSFORM_POSITION_SCALAR(in, out) \
174         (out)[0] = ((in)[0] * m[0] + (in)[1] * m[4] + (in)[2] * m[ 8] + m[12]); \
175         (out)[1] = ((in)[0] * m[1] + (in)[1] * m[5] + (in)[2] * m[ 9] + m[13]); \
176         (out)[2] = ((in)[0] * m[2] + (in)[1] * m[6] + (in)[2] * m[10] + m[14]);
177 #define TRANSFORM_VECTOR_SCALAR(in, out) \
178         (out)[0] = ((in)[0] * m[0] + (in)[1] * m[4] + (in)[2] * m[ 8]); \
179         (out)[1] = ((in)[0] * m[1] + (in)[1] * m[5] + (in)[2] * m[ 9]); \
180         (out)[2] = ((in)[0] * m[2] + (in)[1] * m[6] + (in)[2] * m[10]);
181
182 #define TRANSFORM_POSITION(in, out) { \
183                 __m128 pin = _mm_loadu_ps(in); /* we ignore the value in the last element (x from the next vertex) */ \
184                 __m128 x = _mm_shuffle_ps(pin, pin, 0x0); \
185                 __m128 t1 = _mm_mul_ps(x, m1); \
186                 \
187                 /* y, + x */ \
188                 __m128 y = _mm_shuffle_ps(pin, pin, 0x55); \
189                 __m128 t2 = _mm_mul_ps(y, m2); \
190                 __m128 t3 = _mm_add_ps(t1, t2); \
191                 \
192                 /* z, + (y+x) */ \
193                 __m128 z = _mm_shuffle_ps(pin, pin, 0xaa); \
194                 __m128 t4 = _mm_mul_ps(z, m3); \
195                 __m128 t5 = _mm_add_ps(t3, t4); \
196                 \
197                 /* + m3 */ \
198                 __m128 pout = _mm_add_ps(t5, m4); \
199                 _mm_storeu_ps((out), pout); \
200         }
201
202 #define TRANSFORM_VECTOR(in, out) { \
203                 __m128 vin = _mm_loadu_ps(in); \
204                 \
205                 /* x */ \
206                 __m128 x = _mm_shuffle_ps(vin, vin, 0x0); \
207                 __m128 t1 = _mm_mul_ps(x, m1); \
208                 \
209                 /* y, + x */ \
210                 __m128 y = _mm_shuffle_ps(vin, vin, 0x55); \
211                 __m128 t2 = _mm_mul_ps(y, m2); \
212                 __m128 t3 = _mm_add_ps(t1, t2); \
213                 \
214                 /* nz, + (ny + nx) */ \
215                 __m128 z = _mm_shuffle_ps(vin, vin, 0xaa); \
216                 __m128 t4 = _mm_mul_ps(z, m3); \
217                 __m128 vout = _mm_add_ps(t3, t4); \
218                 _mm_storeu_ps((out), vout); \
219         }
220
221         // transform vertex attributes by blended matrices
222         if (vertex3f)
223         {
224                 const float * RESTRICT v = model->surfmesh.data_vertex3f;
225                 const unsigned short * RESTRICT b = model->surfmesh.blends;
226                 // special case common combinations of attributes to avoid repeated loading of matrices
227                 if (normal3f)
228                 {
229                         const float * RESTRICT n = model->surfmesh.data_normal3f;
230                         if (svector3f && tvector3f)
231                         {
232                                 const float * RESTRICT sv = model->surfmesh.data_svector3f;
233                                 const float * RESTRICT tv = model->surfmesh.data_tvector3f;
234
235                                 // Note that for SSE each iteration stores one element past end, so we break one vertex short
236                                 // and handle that with scalars in that case
237                                 for (i = 0; i < num_vertices_minus_one; i++, v += 3, n += 3, sv += 3, tv += 3, b++,
238                                                 vertex3f += 3, normal3f += 3, svector3f += 3, tvector3f += 3)
239                                 {
240                                         LOAD_MATRIX4();
241                                         TRANSFORM_POSITION(v, vertex3f);
242                                         TRANSFORM_VECTOR(n, normal3f);
243                                         TRANSFORM_VECTOR(sv, svector3f);
244                                         TRANSFORM_VECTOR(tv, tvector3f);
245                                 }
246
247                                 // Last vertex needs to be done with scalars to avoid reading/writing 1 word past end of arrays
248                                 {
249                                         LOAD_MATRIX_SCALAR();
250                                         TRANSFORM_POSITION_SCALAR(v, vertex3f);
251                                         TRANSFORM_VECTOR_SCALAR(n, normal3f);
252                                         TRANSFORM_VECTOR_SCALAR(sv, svector3f);
253                                         TRANSFORM_VECTOR_SCALAR(tv, tvector3f);
254                                 }
255                                 //printf("elapsed ticks: %llu\n", rdtsc() - ts); // XXX
256                                 return;
257                         }
258
259                         for (i = 0;i < num_vertices_minus_one; i++, v += 3, n += 3, b++, vertex3f += 3, normal3f += 3)
260                         {
261                                 LOAD_MATRIX4();
262                                 TRANSFORM_POSITION(v, vertex3f);
263                                 TRANSFORM_VECTOR(n, normal3f);
264                         }
265                         {
266                                 LOAD_MATRIX_SCALAR();
267                                 TRANSFORM_POSITION_SCALAR(v, vertex3f);
268                                 TRANSFORM_VECTOR_SCALAR(n, normal3f);
269                         }
270                 }
271                 else
272                 {
273                         for (i = 0;i < num_vertices_minus_one; i++, v += 3, b++, vertex3f += 3)
274                         {
275                                 LOAD_MATRIX4();
276                                 TRANSFORM_POSITION(v, vertex3f);
277                         }
278                         {
279                                 LOAD_MATRIX_SCALAR();
280                                 TRANSFORM_POSITION_SCALAR(v, vertex3f);
281                         }
282                 }
283         }
284
285         else if (normal3f)
286         {
287                 const float * RESTRICT n = model->surfmesh.data_normal3f;
288                 const unsigned short * RESTRICT b = model->surfmesh.blends;
289                 for (i = 0; i < num_vertices_minus_one; i++, n += 3, b++, normal3f += 3)
290                 {
291                         LOAD_MATRIX3();
292                         TRANSFORM_VECTOR(n, normal3f);
293                 }
294                 {
295                         LOAD_MATRIX_SCALAR();
296                         TRANSFORM_VECTOR_SCALAR(n, normal3f);
297                 }
298         }
299
300         if (svector3f)
301         {
302                 const float * RESTRICT sv = model->surfmesh.data_svector3f;
303                 const unsigned short * RESTRICT b = model->surfmesh.blends;
304                 for (i = 0; i < num_vertices_minus_one; i++, sv += 3, b++, svector3f += 3)
305                 {
306                         LOAD_MATRIX3();
307                         TRANSFORM_VECTOR(sv, svector3f);
308                 }
309                 {
310                         LOAD_MATRIX_SCALAR();
311                         TRANSFORM_VECTOR_SCALAR(sv, svector3f);
312                 }
313         }
314
315         if (tvector3f)
316         {
317                 const float * RESTRICT tv = model->surfmesh.data_tvector3f;
318                 const unsigned short * RESTRICT b = model->surfmesh.blends;
319                 for (i = 0; i < num_vertices_minus_one; i++, tv += 3, b++, tvector3f += 3)
320                 {
321                         LOAD_MATRIX3();
322                         TRANSFORM_VECTOR(tv, tvector3f);
323                 }
324                 {
325                         LOAD_MATRIX_SCALAR();
326                         TRANSFORM_VECTOR_SCALAR(tv, tvector3f);
327                 }
328         }
329
330 #undef LOAD_MATRIX3
331 #undef LOAD_MATRIX4
332 #undef TRANSFORM_POSITION
333 #undef TRANSFORM_VECTOR
334 #undef LOAD_MATRIX_SCALAR
335 #undef TRANSFORM_POSITION_SCALAR
336 #undef TRANSFORM_VECTOR_SCALAR
337 }
338
339 #endif