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