summaryrefslogtreecommitdiff
path: root/Runtime/Filters/Mesh/SkinGeneric.h
blob: ef30d816dfa81084268d3530e706ddee7316dd0b (plain)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#ifndef SKINGENERIC_H
#define SKINGENERIC_H

#include "Runtime/Filters/Mesh/VertexData.h"

#if UNITY_PS3
template<TransformInstruction transformInstruction, int bonesPerVertexCount,
bool skinNormal, bool skinTangent>
void SkinGenericStreamed (SkinMeshInfo& info)
{
	const int* influence1 = reinterpret_cast<const int*> (info.compactSkin);
	const BoneInfluence2* influence2 = reinterpret_cast<const BoneInfluence2*> (info.compactSkin);
	const BoneInfluence* influence4 = reinterpret_cast<const BoneInfluence*> (info.compactSkin);

	const Matrix4x4f* bones4x4 = info.cachedPose;

	int count = info.vertexCount;

	int vertexOffset = info.vertexData->GetStream(0).offset;
	const int vertexStride = info.vertexData->GetStream(0).stride;

	int normalOffset = info.vertexData->GetStream(1).offset;
	const int normalStride = info.vertexData->GetStream(1).stride;

	int tangentOffset = info.vertexData->GetStream(2).offset;
	const int tangentStride = info.vertexData->GetStream(2).stride;

	const int copyDataOffset = info.vertexData->GetStream(3).offset;
	const int copyDataSize = info.vertexData->GetStream(3).stride * info.vertexCount;

	const UInt8* inputVertex = (const UInt8*)info.inVertices;
	UInt8* outputVertex = (UInt8*)info.outVertices;

	Matrix4x4f poseBlended;
	const Matrix4x4f* poseToUse;

	for( int v = 0; v < count; v++ )
	{
		ALIGN_LOOP_OPTIMIZATION

			Prefetch(inputVertex + 256);

		// Blend the matrices first, then transform everything with this
		// blended matrix. Gives a small speed boost on XCode/Intel (11.3 to 12.00 FPS
		// in skin4 bench), and a good boost on MSVC/Windows (9.6 to 12.4 FPS).
		if (bonesPerVertexCount == 1)
		{
			poseToUse = &bones4x4[*influence1];
		}
		else if (bonesPerVertexCount == 2)
		{
			float weight0 = influence2->weight[0];
			float weight1 = influence2->weight[1];
			const float* b4x40 = bones4x4[influence2->boneIndex[0]].m_Data;
			const float* b4x41 = bones4x4[influence2->boneIndex[1]].m_Data;
			// we need only 12 components of the matrix
			poseBlended.m_Data[ 0] = b4x40[ 0] * weight0 + b4x41[ 0] * weight1;
			poseBlended.m_Data[ 1] = b4x40[ 1] * weight0 + b4x41[ 1] * weight1;
			poseBlended.m_Data[ 2] = b4x40[ 2] * weight0 + b4x41[ 2] * weight1;
			poseBlended.m_Data[ 4] = b4x40[ 4] * weight0 + b4x41[ 4] * weight1;
			poseBlended.m_Data[ 5] = b4x40[ 5] * weight0 + b4x41[ 5] * weight1;
			poseBlended.m_Data[ 6] = b4x40[ 6] * weight0 + b4x41[ 6] * weight1;
			poseBlended.m_Data[ 8] = b4x40[ 8] * weight0 + b4x41[ 8] * weight1;
			poseBlended.m_Data[ 9] = b4x40[ 9] * weight0 + b4x41[ 9] * weight1;
			poseBlended.m_Data[10] = b4x40[10] * weight0 + b4x41[10] * weight1;
			poseBlended.m_Data[12] = b4x40[12] * weight0 + b4x41[12] * weight1;
			poseBlended.m_Data[13] = b4x40[13] * weight0 + b4x41[13] * weight1;
			poseBlended.m_Data[14] = b4x40[14] * weight0 + b4x41[14] * weight1;
			poseToUse = &poseBlended;
		}
		else if (bonesPerVertexCount == 4)
		{
			float weight0 = influence4->weight[0];
			float weight1 = influence4->weight[1];
			float weight2 = influence4->weight[2];
			float weight3 = influence4->weight[3];

			const float* b4x40 = bones4x4[influence4->boneIndex[0]].m_Data;
			const float* b4x41 = bones4x4[influence4->boneIndex[1]].m_Data;
			const float* b4x42 = bones4x4[influence4->boneIndex[2]].m_Data;
			const float* b4x43 = bones4x4[influence4->boneIndex[3]].m_Data;
			// we need only 12 components of the matrix, so unroll 
			poseBlended.m_Data[ 0] = b4x40[ 0] * weight0 + b4x41[ 0] * weight1 + b4x42[ 0] * weight2 + b4x43[ 0] * weight3;
			poseBlended.m_Data[ 1] = b4x40[ 1] * weight0 + b4x41[ 1] * weight1 + b4x42[ 1] * weight2 + b4x43[ 1] * weight3;
			poseBlended.m_Data[ 2] = b4x40[ 2] * weight0 + b4x41[ 2] * weight1 + b4x42[ 2] * weight2 + b4x43[ 2] * weight3;
			poseBlended.m_Data[ 4] = b4x40[ 4] * weight0 + b4x41[ 4] * weight1 + b4x42[ 4] * weight2 + b4x43[ 4] * weight3;
			poseBlended.m_Data[ 5] = b4x40[ 5] * weight0 + b4x41[ 5] * weight1 + b4x42[ 5] * weight2 + b4x43[ 5] * weight3;
			poseBlended.m_Data[ 6] = b4x40[ 6] * weight0 + b4x41[ 6] * weight1 + b4x42[ 6] * weight2 + b4x43[ 6] * weight3;
			poseBlended.m_Data[ 8] = b4x40[ 8] * weight0 + b4x41[ 8] * weight1 + b4x42[ 8] * weight2 + b4x43[ 8] * weight3;
			poseBlended.m_Data[ 9] = b4x40[ 9] * weight0 + b4x41[ 9] * weight1 + b4x42[ 9] * weight2 + b4x43[ 9] * weight3;
			poseBlended.m_Data[10] = b4x40[10] * weight0 + b4x41[10] * weight1 + b4x42[10] * weight2 + b4x43[10] * weight3;
			poseBlended.m_Data[12] = b4x40[12] * weight0 + b4x41[12] * weight1 + b4x42[12] * weight2 + b4x43[12] * weight3;
			poseBlended.m_Data[13] = b4x40[13] * weight0 + b4x41[13] * weight1 + b4x42[13] * weight2 + b4x43[13] * weight3;
			poseBlended.m_Data[14] = b4x40[14] * weight0 + b4x41[14] * weight1 + b4x42[14] * weight2 + b4x43[14] * weight3;
			poseToUse = &poseBlended;
		}

		// skin components
		Vector3f outVertex, outNormal, outTangent;
		const Vector3f* vertex = reinterpret_cast<const Vector3f*>( inputVertex + vertexOffset);
		const Vector3f* normal = reinterpret_cast<const Vector3f*>( inputVertex + normalOffset );
		const Vector3f* tangent = reinterpret_cast<const Vector3f*>( inputVertex + tangentOffset );
		poseToUse->MultiplyPoint3( *vertex, outVertex );
		if( skinNormal )
		{
			poseToUse->MultiplyVector3( *normal, outNormal );
			if (transformInstruction == kNormalizeFastest)
			{
				float sqr1 = SqrMagnitude( outNormal );
				float invsqrt1 = FastestInvSqrt (sqr1);
				outNormal *= invsqrt1;
			}
			else if (transformInstruction == kNormalizeFast)
			{
				float sqr1 = SqrMagnitude( outNormal );
				float invsqrt1 = FastInvSqrt (sqr1);
				outNormal *= invsqrt1;
			}
		}
		if( skinTangent )
		{
			poseToUse->MultiplyVector3( *tangent, outTangent );
			if (transformInstruction == kNormalizeFastest)
			{
				float sqr1 = SqrMagnitude( outTangent );
				float invsqrt1 = FastestInvSqrt (sqr1);
				outTangent *= invsqrt1;
			}
			else if (transformInstruction == kNormalizeFast)
			{
				float sqr1 = SqrMagnitude( outTangent );
				float invsqrt1 = FastInvSqrt (sqr1);
				outTangent *= invsqrt1;
			}
		}

		// write data out
		*reinterpret_cast<Vector3f*> (outputVertex + vertexOffset) = outVertex;
		if( skinNormal )
		{
			*reinterpret_cast<Vector3f*>( outputVertex + normalOffset ) = outNormal;
		}
		if( skinTangent )
		{
			*reinterpret_cast<Vector3f*>( outputVertex + tangentOffset ) = outTangent;
			*reinterpret_cast<float*>( outputVertex + tangentOffset + sizeof(Vector3f) ) = *reinterpret_cast<const float*>( inputVertex + tangentOffset + sizeof(Vector3f) );
		}

		vertexOffset += vertexStride;
		normalOffset += normalStride;
		tangentOffset += tangentStride;

		if (bonesPerVertexCount == 1)
			influence1++;
		else if (bonesPerVertexCount == 2)
			influence2++;
		if (bonesPerVertexCount == 4)
			influence4++;
	}

	// copy 
	const UInt8* copyDataSrc = inputVertex + copyDataOffset;
	UInt8* copyDataDst = outputVertex + copyDataOffset;
	memcpy(copyDataDst, copyDataSrc, copyDataSize);
}
#endif

template<TransformInstruction transformInstruction, int bonesPerVertexCount,
	bool skinNormal, bool skinTangent>
void SkinGeneric (SkinMeshInfo& info);

template<TransformInstruction transformInstruction, int bonesPerVertexCount,
	bool skinNormal, bool skinTangent>
void SkinGeneric (SkinMeshInfo& info)
{
#if UNITY_PS3
	if(info.vertexData && (info.vertexData->GetActiveStreamCount() > 2))
		return SkinGenericStreamed<transformInstruction, bonesPerVertexCount, skinNormal, skinTangent>(info);
#endif
	const int* influence1 = reinterpret_cast<const int*> (info.compactSkin);
	const BoneInfluence2* influence2 = reinterpret_cast<const BoneInfluence2*> (info.compactSkin);
	const BoneInfluence* influence4 = reinterpret_cast<const BoneInfluence*> (info.compactSkin);
	
	const Matrix4x4f* bones4x4 = info.cachedPose;
	
	const int inStride = info.inStride;
	int outStride = info.outStride;
	int count = info.vertexCount;

	const int normalOffset = info.normalOffset;
	const int tangentOffset = info.tangentOffset;

	const UInt8* inputVertex = (const UInt8*)info.inVertices;
	UInt8* outputVertex = (UInt8*)info.outVertices;
	
	Matrix4x4f poseBlended;
	const Matrix4x4f* poseToUse;

	
#if !ENABLE_MULTITHREADED_SKINNING
	PROFILER_AUTO(gMeshSkinningSlowpath, NULL);
#endif

	//;;printf_console("bonesPerVertexCount: %d, skinNormal: %d, normalOffset: %d, inStride: %d, copyDataSizeInts: %d, count: %d, boneCount: %d, outputVertex: %d\n",
	//			   bonesPerVertexCount, (int)skinNormal, normalOffset, inStride, copyDataSizeInts, count, info.boneCount, (int)outputVertex);
	//;;uint64_t delta = mach_absolute_time();
	
	for( int v = 0; v < count; v++ )
	{
		ALIGN_LOOP_OPTIMIZATION
		
		Prefetch(inputVertex + 256);
		
		// Blend the matrices first, then transform everything with this
		// blended matrix. Gives a small speed boost on XCode/Intel (11.3 to 12.00 FPS
		// in skin4 bench), and a good boost on MSVC/Windows (9.6 to 12.4 FPS).
		if (bonesPerVertexCount == 1)
		{
			poseToUse = &bones4x4[*influence1];
		}
		else if (bonesPerVertexCount == 2)
		{
			float weight0 = influence2->weight[0];
			float weight1 = influence2->weight[1];
			const float* b4x40 = bones4x4[influence2->boneIndex[0]].m_Data;
			const float* b4x41 = bones4x4[influence2->boneIndex[1]].m_Data;
			// we need only 12 components of the matrix
			poseBlended.m_Data[ 0] = b4x40[ 0] * weight0 + b4x41[ 0] * weight1;
			poseBlended.m_Data[ 1] = b4x40[ 1] * weight0 + b4x41[ 1] * weight1;
			poseBlended.m_Data[ 2] = b4x40[ 2] * weight0 + b4x41[ 2] * weight1;
			poseBlended.m_Data[ 4] = b4x40[ 4] * weight0 + b4x41[ 4] * weight1;
			poseBlended.m_Data[ 5] = b4x40[ 5] * weight0 + b4x41[ 5] * weight1;
			poseBlended.m_Data[ 6] = b4x40[ 6] * weight0 + b4x41[ 6] * weight1;
			poseBlended.m_Data[ 8] = b4x40[ 8] * weight0 + b4x41[ 8] * weight1;
			poseBlended.m_Data[ 9] = b4x40[ 9] * weight0 + b4x41[ 9] * weight1;
			poseBlended.m_Data[10] = b4x40[10] * weight0 + b4x41[10] * weight1;
			poseBlended.m_Data[12] = b4x40[12] * weight0 + b4x41[12] * weight1;
			poseBlended.m_Data[13] = b4x40[13] * weight0 + b4x41[13] * weight1;
			poseBlended.m_Data[14] = b4x40[14] * weight0 + b4x41[14] * weight1;
			poseToUse = &poseBlended;
		}
		else if (bonesPerVertexCount == 4)
		{
			float weight0 = influence4->weight[0];
			float weight1 = influence4->weight[1];
			float weight2 = influence4->weight[2];
			float weight3 = influence4->weight[3];
			
			const float* b4x40 = bones4x4[influence4->boneIndex[0]].m_Data;
			const float* b4x41 = bones4x4[influence4->boneIndex[1]].m_Data;
			const float* b4x42 = bones4x4[influence4->boneIndex[2]].m_Data;
			const float* b4x43 = bones4x4[influence4->boneIndex[3]].m_Data;
			// we need only 12 components of the matrix, so unroll 
			poseBlended.m_Data[ 0] = b4x40[ 0] * weight0 + b4x41[ 0] * weight1 + b4x42[ 0] * weight2 + b4x43[ 0] * weight3;
			poseBlended.m_Data[ 1] = b4x40[ 1] * weight0 + b4x41[ 1] * weight1 + b4x42[ 1] * weight2 + b4x43[ 1] * weight3;
			poseBlended.m_Data[ 2] = b4x40[ 2] * weight0 + b4x41[ 2] * weight1 + b4x42[ 2] * weight2 + b4x43[ 2] * weight3;
			poseBlended.m_Data[ 4] = b4x40[ 4] * weight0 + b4x41[ 4] * weight1 + b4x42[ 4] * weight2 + b4x43[ 4] * weight3;
			poseBlended.m_Data[ 5] = b4x40[ 5] * weight0 + b4x41[ 5] * weight1 + b4x42[ 5] * weight2 + b4x43[ 5] * weight3;
			poseBlended.m_Data[ 6] = b4x40[ 6] * weight0 + b4x41[ 6] * weight1 + b4x42[ 6] * weight2 + b4x43[ 6] * weight3;
			poseBlended.m_Data[ 8] = b4x40[ 8] * weight0 + b4x41[ 8] * weight1 + b4x42[ 8] * weight2 + b4x43[ 8] * weight3;
			poseBlended.m_Data[ 9] = b4x40[ 9] * weight0 + b4x41[ 9] * weight1 + b4x42[ 9] * weight2 + b4x43[ 9] * weight3;
			poseBlended.m_Data[10] = b4x40[10] * weight0 + b4x41[10] * weight1 + b4x42[10] * weight2 + b4x43[10] * weight3;
			poseBlended.m_Data[12] = b4x40[12] * weight0 + b4x41[12] * weight1 + b4x42[12] * weight2 + b4x43[12] * weight3;
			poseBlended.m_Data[13] = b4x40[13] * weight0 + b4x41[13] * weight1 + b4x42[13] * weight2 + b4x43[13] * weight3;
			poseBlended.m_Data[14] = b4x40[14] * weight0 + b4x41[14] * weight1 + b4x42[14] * weight2 + b4x43[14] * weight3;
			poseToUse = &poseBlended;
		}

		// skin components
		Vector3f outVertex, outNormal, outTangent;
		const Vector3f* vertex = reinterpret_cast<const Vector3f*>( inputVertex );
		const Vector3f* normal = reinterpret_cast<const Vector3f*>( inputVertex + normalOffset );
		const Vector3f* tangent = reinterpret_cast<const Vector3f*>( inputVertex + tangentOffset );
		poseToUse->MultiplyPoint3( *vertex, outVertex );
		if( skinNormal )
		{
			poseToUse->MultiplyVector3( *normal, outNormal );
			if (transformInstruction == kNormalizeFastest)
			{
				float sqr1 = SqrMagnitude( outNormal );
				float invsqrt1 = FastestInvSqrt (sqr1);
				outNormal *= invsqrt1;
			}
			else if (transformInstruction == kNormalizeFast)
			{
				float sqr1 = SqrMagnitude( outNormal );
				float invsqrt1 = FastInvSqrt (sqr1);
				outNormal *= invsqrt1;
			}
		}
		if( skinTangent )
		{
			poseToUse->MultiplyVector3( *tangent, outTangent );
			if (transformInstruction == kNormalizeFastest)
			{
				float sqr1 = SqrMagnitude( outTangent );
				float invsqrt1 = FastestInvSqrt (sqr1);
				outTangent *= invsqrt1;
			}
			else if (transformInstruction == kNormalizeFast)
			{
				float sqr1 = SqrMagnitude( outTangent );
				float invsqrt1 = FastInvSqrt (sqr1);
				outTangent *= invsqrt1;
			}
		}
	
		// write data out
		*reinterpret_cast<Vector3f*> (outputVertex) = outVertex;
		if( skinNormal )
		{
			*reinterpret_cast<Vector3f*>( outputVertex + normalOffset ) = outNormal;
		}
		
		if( skinTangent )
		{
			*reinterpret_cast<Vector3f*>( outputVertex + tangentOffset ) = outTangent;
			*reinterpret_cast<float*>( outputVertex + tangentOffset + sizeof(Vector3f) ) = *reinterpret_cast<const float*>( inputVertex + tangentOffset + sizeof(Vector3f) );
		}

		outputVertex += outStride;
		inputVertex += inStride;
		
		if (bonesPerVertexCount == 1)
			influence1++;
		else if (bonesPerVertexCount == 2)
			influence2++;
		if (bonesPerVertexCount == 4)
			influence4++;
	}
		
	//;;static int frameCount = 0; frameCount++;
	//delta = mach_absolute_time() - delta;
	//;;static uint64_t deltaAccum = 0; deltaAccum += (int)(delta);
	//;;printf_console("skin-c: %d %d\n", (int)(deltaAccum / frameCount), (int)delta);
}

#endif