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