Fix MSVC++ 2015 warnings about variable scope and some narrowing conversions without...
[xonotic/darkplaces.git] / mod_skeletal_animatevertices_generic.c
1 #include "mod_skeletal_animatevertices_generic.h"
2
3 void Mod_Skeletal_AnimateVertices_Generic(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)
4 {
5         // vertex weighted skeletal
6         int i, k;
7         float *bonepose;
8         float *boneposerelative;
9         const blendweights_t * RESTRICT weights;
10
11         //unsigned long long ts = rdtsc();
12         bonepose = (float *) Mod_Skeletal_AnimateVertices_AllocBuffers(sizeof(float[12]) * (model->num_bones*2 + model->surfmesh.num_blends));
13         boneposerelative = bonepose + model->num_bones * 12;
14
15         Mod_Skeletal_BuildTransforms(model, frameblend, skeleton, bonepose, boneposerelative);
16
17         // generate matrices for all blend combinations
18         weights = model->surfmesh.data_blendweights;
19         for (i = 0;i < model->surfmesh.num_blends;i++, weights++)
20         {
21                 float * RESTRICT b = boneposerelative + 12 * (model->num_bones + i);
22                 const float * RESTRICT m = boneposerelative + 12 * (unsigned int)weights->index[0];
23                 float f = weights->influence[0] * (1.0f / 255.0f);
24                 b[ 0] = f*m[ 0]; b[ 1] = f*m[ 1]; b[ 2] = f*m[ 2]; b[ 3] = f*m[ 3];
25                 b[ 4] = f*m[ 4]; b[ 5] = f*m[ 5]; b[ 6] = f*m[ 6]; b[ 7] = f*m[ 7];
26                 b[ 8] = f*m[ 8]; b[ 9] = f*m[ 9]; b[10] = f*m[10]; b[11] = f*m[11];
27                 for (k = 1;k < 4 && weights->influence[k];k++)
28                 {
29                         m = boneposerelative + 12 * (unsigned int)weights->index[k];
30                         f = weights->influence[k] * (1.0f / 255.0f);
31                         b[ 0] += f*m[ 0]; b[ 1] += f*m[ 1]; b[ 2] += f*m[ 2]; b[ 3] += f*m[ 3];
32                         b[ 4] += f*m[ 4]; b[ 5] += f*m[ 5]; b[ 6] += f*m[ 6]; b[ 7] += f*m[ 7];
33                         b[ 8] += f*m[ 8]; b[ 9] += f*m[ 9]; b[10] += f*m[10]; b[11] += f*m[11];
34                 }
35         }
36
37 #define LOAD_MATRIX_SCALAR() const float * RESTRICT m = boneposerelative + 12 * (unsigned int)*b
38
39 #define LOAD_MATRIX3() \
40         LOAD_MATRIX_SCALAR()
41 #define LOAD_MATRIX4() \
42         LOAD_MATRIX_SCALAR()
43
44 #define TRANSFORM_POSITION_SCALAR(in, out) \
45         (out)[0] = ((in)[0] * m[0] + (in)[1] * m[1] + (in)[2] * m[ 2] + m[3]); \
46         (out)[1] = ((in)[0] * m[4] + (in)[1] * m[5] + (in)[2] * m[ 6] + m[7]); \
47         (out)[2] = ((in)[0] * m[8] + (in)[1] * m[9] + (in)[2] * m[10] + m[11]);
48 #define TRANSFORM_VECTOR_SCALAR(in, out) \
49         (out)[0] = ((in)[0] * m[0] + (in)[1] * m[1] + (in)[2] * m[ 2]); \
50         (out)[1] = ((in)[0] * m[4] + (in)[1] * m[5] + (in)[2] * m[ 6]); \
51         (out)[2] = ((in)[0] * m[8] + (in)[1] * m[9] + (in)[2] * m[10]);
52
53 #define TRANSFORM_POSITION(in, out) \
54         TRANSFORM_POSITION_SCALAR(in, out)
55 #define TRANSFORM_VECTOR(in, out) \
56         TRANSFORM_VECTOR_SCALAR(in, out)
57
58         // transform vertex attributes by blended matrices
59         if (vertex3f)
60         {
61                 const float * RESTRICT v = model->surfmesh.data_vertex3f;
62                 const unsigned short * RESTRICT b = model->surfmesh.blends;
63                 // special case common combinations of attributes to avoid repeated loading of matrices
64                 if (normal3f)
65                 {
66                         const float * RESTRICT n = model->surfmesh.data_normal3f;
67                         if (svector3f && tvector3f)
68                         {
69                                 const float * RESTRICT svec = model->surfmesh.data_svector3f;
70                                 const float * RESTRICT tvec = model->surfmesh.data_tvector3f;
71
72                                 // Note that for SSE each iteration stores one element past end, so we break one vertex short
73                                 // and handle that with scalars in that case
74                                 for (i = 0; i < model->surfmesh.num_vertices; i++, v += 3, n += 3, svec += 3, tvec += 3, b++,
75                                                 vertex3f += 3, normal3f += 3, svector3f += 3, tvector3f += 3)
76                                 {
77                                         LOAD_MATRIX4();
78                                         TRANSFORM_POSITION(v, vertex3f);
79                                         TRANSFORM_VECTOR(n, normal3f);
80                                         TRANSFORM_VECTOR(svec, svector3f);
81                                         TRANSFORM_VECTOR(tvec, tvector3f);
82                                 }
83
84                                 return;
85                         }
86
87                         for (i = 0;i < model->surfmesh.num_vertices; i++, v += 3, n += 3, b++, vertex3f += 3, normal3f += 3)
88                         {
89                                 LOAD_MATRIX4();
90                                 TRANSFORM_POSITION(v, vertex3f);
91                                 TRANSFORM_VECTOR(n, normal3f);
92                         }
93                 }
94                 else
95                 {
96                         for (i = 0;i < model->surfmesh.num_vertices; i++, v += 3, b++, vertex3f += 3)
97                         {
98                                 LOAD_MATRIX4();
99                                 TRANSFORM_POSITION(v, vertex3f);
100                         }
101                 }
102         }
103
104         else if (normal3f)
105         {
106                 const float * RESTRICT n = model->surfmesh.data_normal3f;
107                 const unsigned short * RESTRICT b = model->surfmesh.blends;
108                 for (i = 0; i < model->surfmesh.num_vertices; i++, n += 3, b++, normal3f += 3)
109                 {
110                         LOAD_MATRIX3();
111                         TRANSFORM_VECTOR(n, normal3f);
112                 }
113         }
114
115         if (svector3f)
116         {
117                 const float * RESTRICT svec = model->surfmesh.data_svector3f;
118                 const unsigned short * RESTRICT b = model->surfmesh.blends;
119                 for (i = 0; i < model->surfmesh.num_vertices; i++, svec += 3, b++, svector3f += 3)
120                 {
121                         LOAD_MATRIX3();
122                         TRANSFORM_VECTOR(svec, svector3f);
123                 }
124         }
125
126         if (tvector3f)
127         {
128                 const float * RESTRICT tvec = model->surfmesh.data_tvector3f;
129                 const unsigned short * RESTRICT b = model->surfmesh.blends;
130                 for (i = 0; i < model->surfmesh.num_vertices; i++, tvec += 3, b++, tvector3f += 3)
131                 {
132                         LOAD_MATRIX3();
133                         TRANSFORM_VECTOR(tvec, tvector3f);
134                 }
135         }
136 }