1 #include "mod_skeletal_animatevertices_sse.h"
5 #ifdef MATRIX4x4_OPENGLORIENTATION
6 #error "SSE skeletal requires D3D matrix layout"
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)
13 // vertex weighted skeletal
16 matrix4x4_t *bonepose;
17 matrix4x4_t *boneposerelative;
20 const blendweights_t * RESTRICT weights;
21 int num_vertices_minus_one;
23 if (!model->surfmesh.num_vertices)
26 num_vertices_minus_one = model->surfmesh.num_vertices - 1;
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;
32 if (skeleton && !skeleton->relativetransforms)
35 // interpolate matrices
38 for (i = 0;i < model->num_bones;i++)
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]);
45 memcpy(&bonepose[i], &skeleton->relativetransforms[i], sizeof(matrix4x4_t));
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(&boneposerelative[i], &bonepose[i], &mm);
55 float originscale = model->num_posescale;
57 const short * RESTRICT pose6s;
59 for (i = 0;i < model->num_bones;i++)
61 memset(m, 0, sizeof(m));
62 for (blends = 0;blends < MAX_FRAMEBLENDS && frameblend[blends].lerp > 0;blends++)
64 pose6s = model->data_poses6s + 6 * (frameblend[blends].subframe * model->num_bones + i);
65 lerp = frameblend[blends].lerp;
66 x = pose6s[3] * (1.0f / 32767.0f);
67 y = pose6s[4] * (1.0f / 32767.0f);
68 z = pose6s[5] * (1.0f / 32767.0f);
69 w = 1.0f - (x*x+y*y+z*z);
70 w = w > 0.0f ? -sqrt(w) : 0.0f;
71 m[ 0] += (1-2*(y*y+z*z)) * lerp;
72 m[ 1] += ( 2*(x*y-z*w)) * lerp;
73 m[ 2] += ( 2*(x*z+y*w)) * lerp;
74 m[ 3] += (pose6s[0] * originscale) * lerp;
75 m[ 4] += ( 2*(x*y+z*w)) * lerp;
76 m[ 5] += (1-2*(x*x+z*z)) * lerp;
77 m[ 6] += ( 2*(y*z-x*w)) * lerp;
78 m[ 7] += (pose6s[1] * originscale) * lerp;
79 m[ 8] += ( 2*(x*z-y*w)) * lerp;
80 m[ 9] += ( 2*(y*z+x*w)) * lerp;
81 m[10] += (1-2*(x*x+y*y)) * lerp;
82 m[11] += (pose6s[2] * originscale) * lerp;
85 VectorNormalize(m + 4);
86 VectorNormalize(m + 8);
87 if (i == r_skeletal_debugbone.integer)
88 m[r_skeletal_debugbonecomponent.integer % 12] += r_skeletal_debugbonevalue.value;
89 m[3] *= r_skeletal_debugtranslatex.value;
90 m[7] *= r_skeletal_debugtranslatey.value;
91 m[11] *= r_skeletal_debugtranslatez.value;
92 Matrix4x4_FromArray12FloatD3D(&mm, m);
93 if (model->data_bones[i].parent >= 0)
94 Matrix4x4_Concat(&bonepose[i], &bonepose[model->data_bones[i].parent], &mm);
96 memcpy(&bonepose[i], &mm, sizeof(mm));
97 // create a relative deformation matrix to describe displacement
98 // from the base mesh, which is used by the actual weighting
99 Matrix4x4_FromArray12FloatD3D(&mm, model->data_baseboneposeinverse + i * 12); // baseboneposeinverse is 4x3 row-major
100 Matrix4x4_Concat(&mm2, &bonepose[i], &mm);
101 Matrix4x4_Transpose(&boneposerelative[i], &mm2); // TODO: Eliminate this transpose
105 // generate matrices for all blend combinations
106 weights = model->surfmesh.data_blendweights;
107 for (i = 0;i < model->surfmesh.num_blends;i++, weights++)
109 float * RESTRICT b = &boneposerelative[model->num_bones + i].m[0][0];
110 const float * RESTRICT m = &boneposerelative[weights->index[0]].m[0][0];
111 float f = weights->influence[0] * (1.0f / 255.0f);
112 __m128 fv = _mm_set_ps1(f);
113 __m128 b0 = _mm_load_ps(m);
114 __m128 b1 = _mm_load_ps(m+4);
115 __m128 b2 = _mm_load_ps(m+8);
116 __m128 b3 = _mm_load_ps(m+12);
117 __m128 m0, m1, m2, m3;
118 b0 = _mm_mul_ps(b0, fv);
119 b1 = _mm_mul_ps(b1, fv);
120 b2 = _mm_mul_ps(b2, fv);
121 b3 = _mm_mul_ps(b3, fv);
122 for (k = 1;k < 4 && weights->influence[k];k++)
124 m = &boneposerelative[weights->index[k]].m[0][0];
125 f = weights->influence[k] * (1.0f / 255.0f);
128 m1 = _mm_load_ps(m+4);
129 m2 = _mm_load_ps(m+8);
130 m3 = _mm_load_ps(m+12);
131 m0 = _mm_mul_ps(m0, fv);
132 m1 = _mm_mul_ps(m1, fv);
133 m2 = _mm_mul_ps(m2, fv);
134 m3 = _mm_mul_ps(m3, fv);
135 b0 = _mm_add_ps(m0, b0);
136 b1 = _mm_add_ps(m1, b1);
137 b2 = _mm_add_ps(m2, b2);
138 b3 = _mm_add_ps(m3, b3);
141 _mm_store_ps(b+4, b1);
142 _mm_store_ps(b+8, b2);
143 _mm_store_ps(b+12, b3);
146 #define LOAD_MATRIX_SCALAR() const float * RESTRICT m = &boneposerelative[*b].m[0][0]
148 #define LOAD_MATRIX3() \
149 const float * RESTRICT m = &boneposerelative[*b].m[0][0]; \
150 /* bonepose array is 16 byte aligned */ \
151 __m128 m1 = _mm_load_ps((m)); \
152 __m128 m2 = _mm_load_ps((m)+4); \
153 __m128 m3 = _mm_load_ps((m)+8);
154 #define LOAD_MATRIX4() \
155 const float * RESTRICT m = &boneposerelative[*b].m[0][0]; \
156 /* bonepose array is 16 byte aligned */ \
157 __m128 m1 = _mm_load_ps((m)); \
158 __m128 m2 = _mm_load_ps((m)+4); \
159 __m128 m3 = _mm_load_ps((m)+8); \
160 __m128 m4 = _mm_load_ps((m)+12)
162 /* Note that matrix is 4x4 and transposed compared to non-USE_SSE codepath */
163 #define TRANSFORM_POSITION_SCALAR(in, out) \
164 (out)[0] = ((in)[0] * m[0] + (in)[1] * m[4] + (in)[2] * m[ 8] + m[12]); \
165 (out)[1] = ((in)[0] * m[1] + (in)[1] * m[5] + (in)[2] * m[ 9] + m[13]); \
166 (out)[2] = ((in)[0] * m[2] + (in)[1] * m[6] + (in)[2] * m[10] + m[14]);
167 #define TRANSFORM_VECTOR_SCALAR(in, out) \
168 (out)[0] = ((in)[0] * m[0] + (in)[1] * m[4] + (in)[2] * m[ 8]); \
169 (out)[1] = ((in)[0] * m[1] + (in)[1] * m[5] + (in)[2] * m[ 9]); \
170 (out)[2] = ((in)[0] * m[2] + (in)[1] * m[6] + (in)[2] * m[10]);
172 #define TRANSFORM_POSITION(in, out) { \
173 __m128 pin = _mm_loadu_ps(in); /* we ignore the value in the last element (x from the next vertex) */ \
174 __m128 x = _mm_shuffle_ps(pin, pin, 0x0); \
175 __m128 t1 = _mm_mul_ps(x, m1); \
178 __m128 y = _mm_shuffle_ps(pin, pin, 0x55); \
179 __m128 t2 = _mm_mul_ps(y, m2); \
180 __m128 t3 = _mm_add_ps(t1, t2); \
183 __m128 z = _mm_shuffle_ps(pin, pin, 0xaa); \
184 __m128 t4 = _mm_mul_ps(z, m3); \
185 __m128 t5 = _mm_add_ps(t3, t4); \
188 __m128 pout = _mm_add_ps(t5, m4); \
189 _mm_storeu_ps((out), pout); \
192 #define TRANSFORM_VECTOR(in, out) { \
193 __m128 vin = _mm_loadu_ps(in); \
196 __m128 x = _mm_shuffle_ps(vin, vin, 0x0); \
197 __m128 t1 = _mm_mul_ps(x, m1); \
200 __m128 y = _mm_shuffle_ps(vin, vin, 0x55); \
201 __m128 t2 = _mm_mul_ps(y, m2); \
202 __m128 t3 = _mm_add_ps(t1, t2); \
204 /* nz, + (ny + nx) */ \
205 __m128 z = _mm_shuffle_ps(vin, vin, 0xaa); \
206 __m128 t4 = _mm_mul_ps(z, m3); \
207 __m128 vout = _mm_add_ps(t3, t4); \
208 _mm_storeu_ps((out), vout); \
211 // transform vertex attributes by blended matrices
214 const float * RESTRICT v = model->surfmesh.data_vertex3f;
215 const unsigned short * RESTRICT b = model->surfmesh.blends;
216 // special case common combinations of attributes to avoid repeated loading of matrices
219 const float * RESTRICT n = model->surfmesh.data_normal3f;
220 if (svector3f && tvector3f)
222 const float * RESTRICT sv = model->surfmesh.data_svector3f;
223 const float * RESTRICT tv = model->surfmesh.data_tvector3f;
225 // Note that for SSE each iteration stores one element past end, so we break one vertex short
226 // and handle that with scalars in that case
227 for (i = 0; i < num_vertices_minus_one; i++, v += 3, n += 3, sv += 3, tv += 3, b++,
228 vertex3f += 3, normal3f += 3, svector3f += 3, tvector3f += 3)
231 TRANSFORM_POSITION(v, vertex3f);
232 TRANSFORM_VECTOR(n, normal3f);
233 TRANSFORM_VECTOR(sv, svector3f);
234 TRANSFORM_VECTOR(tv, tvector3f);
237 // Last vertex needs to be done with scalars to avoid reading/writing 1 word past end of arrays
239 LOAD_MATRIX_SCALAR();
240 TRANSFORM_POSITION_SCALAR(v, vertex3f);
241 TRANSFORM_VECTOR_SCALAR(n, normal3f);
242 TRANSFORM_VECTOR_SCALAR(sv, svector3f);
243 TRANSFORM_VECTOR_SCALAR(tv, tvector3f);
245 //printf("elapsed ticks: %llu\n", rdtsc() - ts); // XXX
249 for (i = 0;i < num_vertices_minus_one; i++, v += 3, n += 3, b++, vertex3f += 3, normal3f += 3)
252 TRANSFORM_POSITION(v, vertex3f);
253 TRANSFORM_VECTOR(n, normal3f);
256 LOAD_MATRIX_SCALAR();
257 TRANSFORM_POSITION_SCALAR(v, vertex3f);
258 TRANSFORM_VECTOR_SCALAR(n, normal3f);
263 for (i = 0;i < num_vertices_minus_one; i++, v += 3, b++, vertex3f += 3)
266 TRANSFORM_POSITION(v, vertex3f);
269 LOAD_MATRIX_SCALAR();
270 TRANSFORM_POSITION_SCALAR(v, vertex3f);
277 const float * RESTRICT n = model->surfmesh.data_normal3f;
278 const unsigned short * RESTRICT b = model->surfmesh.blends;
279 for (i = 0; i < num_vertices_minus_one; i++, n += 3, b++, normal3f += 3)
282 TRANSFORM_VECTOR(n, normal3f);
285 LOAD_MATRIX_SCALAR();
286 TRANSFORM_VECTOR_SCALAR(n, normal3f);
292 const float * RESTRICT sv = model->surfmesh.data_svector3f;
293 const unsigned short * RESTRICT b = model->surfmesh.blends;
294 for (i = 0; i < num_vertices_minus_one; i++, sv += 3, b++, svector3f += 3)
297 TRANSFORM_VECTOR(sv, svector3f);
300 LOAD_MATRIX_SCALAR();
301 TRANSFORM_VECTOR_SCALAR(sv, svector3f);
307 const float * RESTRICT tv = model->surfmesh.data_tvector3f;
308 const unsigned short * RESTRICT b = model->surfmesh.blends;
309 for (i = 0; i < num_vertices_minus_one; i++, tv += 3, b++, tvector3f += 3)
312 TRANSFORM_VECTOR(tv, tvector3f);
315 LOAD_MATRIX_SCALAR();
316 TRANSFORM_VECTOR_SCALAR(tv, tvector3f);
322 #undef TRANSFORM_POSITION
323 #undef TRANSFORM_VECTOR
324 #undef LOAD_MATRIX_SCALAR
325 #undef TRANSFORM_POSITION_SCALAR
326 #undef TRANSFORM_VECTOR_SCALAR