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