1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
;; SkinSSE2.s
;;
;; Created by Kaspar Daugaard on 1/12/11.
;; Copyright 2011 Unity Technologies. All rights reserved.
bits 32
section .text align=32
%define normalOffset 12
%define tangentOffset 24
%macro SkinSSE2_Generic 3
; %1 numBones
; %2 hasNormals
; %3 hasTangents
; [ebp + 8] inVertices
; [ebp + 12] outVertices
; [ebp + 16] numVertices
; [ebp + 20] boneMatrices
; [ebp + 24] weightsAndIndices
; [ebp + 28] inputStride
; [ebp + 32] outputStride
push ebp
mov ebp, esp
pushad
; Local variables (32 byte aligned)
; [esp + 0] MaskW
; [esp + 16] MaskVec3
; [esp + 32] savedEcx
sub esp, 16*3
and esp, ~31
; Create bitmasks on stack
sub eax, eax
mov [esp + 0], eax ; MaskW
mov [esp + 4], eax
mov [esp + 8], eax
dec eax
mov [esp + 12], eax
mov [esp + 16], eax ; MaskVec3
mov [esp + 20], eax
mov [esp + 24], eax
inc eax
mov [esp + 28], eax
mov esi, [ebp + 8] ; inVertices
mov edi, [ebp + 12] ; outVertices
mov ecx, [ebp + 16] ; numVertices
mov edx, [ebp + 24] ; weightsAndIndices
; Prefetch vertices
prefetchnta [edx]
prefetchnta [esi]
prefetchnta [esi + 32]
align 32
%%SkinSSE2_loop:
prefetchnta [esi + 64]
mov ebx, [ebp + 20] ; boneMatrices
mov [esp + 32], ecx ; savedEcx
; Load first bone index
%if %1 == 1
; Single bone, no weight
mov eax, [edx]
shl eax, 6
%else
; Indices come after weights
mov eax, [edx + %1*4]
shl eax, 6
prefetchnta [ebx + eax]
prefetchnta [ebx + eax + 32]
; Load second bone index
mov ecx, [edx + %1*4 + 4]
shl ecx, 6
prefetchnta [ebx + ecx]
prefetchnta [ebx + ecx + 32]
; Load all weights to xmm0
movups xmm0, [edx]
%endif
; Load first matrix to xmm4-xmm7
movaps xmm4, [ebx + eax]
movaps xmm5, [ebx + eax + 16]
movaps xmm6, [ebx + eax + 32]
movaps xmm7, [ebx + eax + 48]
%if %1 >= 2
; Multiply first matrix with first weight
movaps xmm1, xmm0
shufps xmm1, xmm1, 0x00
mulps xmm4, xmm1
mulps xmm5, xmm1
mulps xmm6, xmm1
mulps xmm7, xmm1
%endif
%if %1 >= 3
; Load third bone index
mov eax, [edx + %1*4 + 8]
shl eax, 6
prefetchnta [ebx + eax]
prefetchnta [ebx + eax + 32]
%endif
%if %1 >= 2
; Load first two rows of the second matrix to xmm2-xmm3
movaps xmm2, [ebx + ecx]
movaps xmm3, [ebx + ecx + 16]
; Shuffle second weight to all elements of xmm1
movaps xmm1, xmm0
shufps xmm1, xmm1, 0x55
; Multiply two first rows of second matrix with second weight
mulps xmm2, xmm1
mulps xmm3, xmm1
; Add
addps xmm4, xmm2
addps xmm5, xmm3
; Load last two rows of the second matrix to xmm2-xmm3
movaps xmm2, [ebx + ecx + 32]
movaps xmm3, [ebx + ecx + 48]
; Multiply two last rows of the second matri with second weight
mulps xmm2, xmm1
mulps xmm3, xmm1
; Add
addps xmm6, xmm2
addps xmm7, xmm3
%endif
%if %1 >= 4
; Load fourth bone index
mov ecx, [edx + %1*4 + 12]
shl ecx, 6
prefetchnta [ebx + ecx]
prefetchnta [ebx + ecx + 32]
%endif
%if %1 >= 3
; Load first two rows of the third matrix to xmm2-xmm3
movaps xmm2, [ebx + eax]
movaps xmm3, [ebx + eax + 16]
; Shuffle third weight to all elements of xmm1
movaps xmm1, xmm0
shufps xmm1, xmm1, 0xaa
; Multiply first two rows of third matrix with third weight
mulps xmm2, xmm1
mulps xmm3, xmm1
; Add
addps xmm4, xmm2
addps xmm5, xmm3
; Load last two rows of the third matrix to xmm2-xmm3
movaps xmm2, [ebx + eax + 32]
movaps xmm3, [ebx + eax + 48]
; Multiply last two rows of third matrix with third weight
mulps xmm2, xmm1
mulps xmm3, xmm1
; Add
addps xmm6, xmm2
addps xmm7, xmm3
%endif
%if %1 >= 4
; Load first two rows of the fourth matrix into xmm2-xmm3
movaps xmm2, [ebx + ecx]
movaps xmm3, [ebx + ecx + 16]
; Shuffle fourth weight to all elements of xmm1
movaps xmm1, xmm0
shufps xmm1, xmm1, 0xff
; Multiply first two rows of the fourth matrix with fourth weight
mulps xmm2, xmm1
mulps xmm3, xmm1
; Add
addps xmm4, xmm2
addps xmm5, xmm3
; Load last two rows of the fourth matrix to xmm2-xmm3
movaps xmm2, [ebx + ecx + 32]
movaps xmm3, [ebx + ecx + 48]
; Multiply last two rows of the fourth matrix with fourth weight
mulps xmm2, xmm1
mulps xmm3, xmm1
; Add
addps xmm6, xmm2
addps xmm7, xmm3
%endif
; Matrix is in xmm4-xmm7
; Transform position by 4x4 matrix in xmm4-xmm7
movups xmm0, [esi]
movaps xmm1, xmm0
movaps xmm2, xmm0
shufps xmm1, xmm1, 0x55
shufps xmm2, xmm2, 0xaa
shufps xmm0, xmm0, 0x00
mulps xmm1, xmm5
mulps xmm2, xmm6
mulps xmm0, xmm4
addps xmm1, xmm2
addps xmm0, xmm7
addps xmm0, xmm1
; Store vertex position in outvert
movaps xmm7, [esp + 16] ; MaskVec3
maskmovdqu xmm0, xmm7
%if %2 ; Has normal
; Transform vector by 3x3 matrix in xmm4-xmm6
movups xmm0, [esi + normalOffset]
movaps xmm1, xmm0
movaps xmm2, xmm0
shufps xmm1, xmm1, 0x55
shufps xmm2, xmm2, 0xaa
shufps xmm0, xmm0, 0x00
mulps xmm1, xmm5
mulps xmm2, xmm6
mulps xmm0, xmm4
addps xmm1, xmm2
addps xmm0, xmm1
%endif
%if %3 ; Has tangent
; Transform vector by 3x3 matrix in xmm4-xmm6
movups xmm1, [esi + tangentOffset]
movaps xmm2, xmm1
movaps xmm3, xmm1
shufps xmm2, xmm2, 0x55
shufps xmm3, xmm3, 0xaa
mulps xmm2, xmm5
mulps xmm3, xmm6
movaps xmm6, xmm1 ; Save original tangent's W in xmm6
shufps xmm1, xmm1, 0x00
andps xmm6, [esp + 0] ; MaskW
mulps xmm1, xmm4
addps xmm2, xmm3
addps xmm1, xmm2
%endif
%if %2 || %3 ; Has normal or tangent
; Calculate lengths and normalize
movaps xmm2, xmm0
movaps xmm5, xmm1
mulps xmm2, xmm2
mulps xmm5, xmm5
movaps xmm3, xmm2
movaps xmm4, xmm2
shufps xmm3, xmm5, 0x55
shufps xmm4, xmm5, 0xaa
shufps xmm2, xmm5, 0x00
addps xmm3, xmm4
addps xmm2, xmm3
sqrtps xmm2, xmm2
rcpps xmm2, xmm2
movaps xmm3, xmm2
shufps xmm2, xmm2, 0x00
shufps xmm3, xmm3, 0xaa
mulps xmm0, xmm2
mulps xmm1, xmm3
%endif
%if %2 ; Write normal
add edi, normalOffset
maskmovdqu xmm0, xmm7 ; MaskVec3
sub edi, normalOffset
%endif
%if %3 ; Write tangent
andps xmm1, xmm7 ; MaskVec3
orps xmm1, xmm6 ; Restore original W
movups [edi + tangentOffset], xmm1
%endif
%if %1 == 1
; Indices only
add edx, 4
%else
; Indices and weights
add edx, %1 * 8
%endif
add esi, [ebp + 28] ; inputStride
add edi, [ebp + 32] ; outputStride
mov ecx, [esp + 32] ; savedEcx
dec ecx
jnz %%SkinSSE2_loop
; Remove local variables from stack
lea esp, [ebp-32]
popad
pop ebp
ret
align 16
%endmacro
global SkinSSE2_1Bone_Pos
global SkinSSE2_2Bones_Pos
global SkinSSE2_4Bones_Pos
global SkinSSE2_1Bone_PosNormal
global SkinSSE2_2Bones_PosNormal
global SkinSSE2_4Bones_PosNormal
global SkinSSE2_1Bone_PosNormalTan
global SkinSSE2_2Bones_PosNormalTan
global SkinSSE2_4Bones_PosNormalTan
SkinSSE2_1Bone_Pos: SkinSSE2_Generic 1, 0, 0
SkinSSE2_2Bones_Pos: SkinSSE2_Generic 2, 0, 0
SkinSSE2_4Bones_Pos: SkinSSE2_Generic 4, 0, 0
SkinSSE2_1Bone_PosNormal: SkinSSE2_Generic 1, 1, 0
SkinSSE2_2Bones_PosNormal: SkinSSE2_Generic 2, 1, 0
SkinSSE2_4Bones_PosNormal: SkinSSE2_Generic 4, 1, 0
SkinSSE2_1Bone_PosNormalTan: SkinSSE2_Generic 1, 1, 1
SkinSSE2_2Bones_PosNormalTan: SkinSSE2_Generic 2, 1, 1
SkinSSE2_4Bones_PosNormalTan: SkinSSE2_Generic 4, 1, 1
|