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
|
#include "UnityPrefix.h"
#include "MeshSkinning.h"
#if UNITY_OSX
#include <alloca.h> // this is really deprecated and should be exchanged for stdlib.h
#else
#include <stdlib.h>
#endif
#include "Runtime/Utilities/Utility.h"
#include "Runtime/Utilities/LogAssert.h"
#include "Runtime/Utilities/OptimizationUtility.h"
#include "Runtime/Misc/Allocator.h"
#include "Runtime/Utilities/Prefetch.h"
#include "Runtime/Profiler/TimeHelper.h"
#include "Runtime/Profiler/Profiler.h"
#include "Runtime/Misc/CPUInfo.h"
#include "Runtime/Allocator/MemoryMacros.h"
#include "Runtime/Filters/Mesh/LodMesh.h"
PROFILER_INFORMATION(gMeshSkinningProfile, "MeshSkinning.Skin", kProfilerRender)
PROFILER_INFORMATION(gMeshSkinningSlowpath, "MeshSkinning.SlowPath", kProfilerRender)
#include "MeshSkinningMobile.h"
#include "MeshSkinningSSE2.h"
#include "SkinGeneric.h"
#include "MeshBlendShaping.h"
//===========================================================================================================================================
void SkinMesh(SkinMeshInfo& info)
{
const TransformInstruction NormalizeTransformInstruction =
#if (UNITY_SUPPORTS_NEON && !UNITY_DISABLE_NEON_SKINNING) || UNITY_SUPPORTS_VFP
// NOTE: optimized NEON/VFP routines do not do any normalization
// instead we rely on GPU to do that
kNoNormalize;
#else
//@TODO: fix that "Fast" & "Fastest" crap. Right now "Fastest" is actually a win on PC (1ms saved in Dark Unity)
// so I'm leaving it there for now.
kNormalizeFastest;
#endif
// Instantiates the right skinning template depending on the bone per vertex count
#define PERMUTE_BONES(skinNormal,skinTangent) { \
if (info.bonesPerVertex == 1) \
SkinGeneric<NormalizeTransformInstruction, 1, skinNormal, skinTangent> (info); \
else if (info.bonesPerVertex == 2) \
SkinGeneric<NormalizeTransformInstruction, 2, skinNormal, skinTangent> (info); \
else if (info.bonesPerVertex == 4) \
SkinGeneric<NormalizeTransformInstruction, 4, skinNormal, skinTangent> (info); \
}
if (info.skinNormals && info.skinTangents)
PERMUTE_BONES(true, true)
else if (info.skinNormals)
PERMUTE_BONES(true, false)
else
PERMUTE_BONES(false, false)
}
static void ApplyMeshSkinning (SkinMeshInfo& info)
{
#if UNITY_WII
SkinMeshWii(info);
#else
PROFILER_AUTO(gMeshSkinningProfile, NULL);
if (SkinMeshOptimizedMobile(info))
return;
if (SkinMeshOptimizedSSE2(info))
return;
// fallback to slow generic implementation
{
PROFILER_AUTO(gMeshSkinningSlowpath, NULL);
SkinMesh(info);
}
#endif
}
void DeformSkinnedMesh (SkinMeshInfo& info)
{
const bool hasBlendShapes = info.blendshapeCount != 0;
const bool hasSkin = info.boneCount != 0;
// No actual skinning can be done. Just copy vertex stream.
// TODO: This code can be removed if we render the undeformed mesh in SkinnedMeshRenderer
// when there is no skin and no active blend shapes. See case 557165.
if (!hasBlendShapes && !hasSkin)
{
memcpy (info.outVertices, info.inVertices, info.inStride * info.vertexCount);
return;
}
UInt8* tmpBlendShapes = NULL;
// blend shapes
if (hasBlendShapes)
{
// The final destination might be write-combined memory which is insanely slow to read
// or randomly access, so always allocate a temp buffer for blend shapes (case 554830).
// Skinning can write directly to a VB since it always writes sequentially to memory.
size_t bufferSize = info.inStride * info.vertexCount;
tmpBlendShapes = ALLOC_TEMP_MANUAL(UInt8, bufferSize);
ApplyBlendShapes (info, tmpBlendShapes);
if (hasSkin)
info.inVertices = tmpBlendShapes;
else
memcpy(info.outVertices, tmpBlendShapes, bufferSize);
}
// skinning
if (hasSkin)
ApplyMeshSkinning (info);
if (tmpBlendShapes)
FREE_TEMP_MANUAL(tmpBlendShapes);
}
void* DeformSkinnedMeshJob (void* rawData)
{
SkinMeshInfo* data = reinterpret_cast<SkinMeshInfo*>(rawData);
DeformSkinnedMesh (*data);
return NULL;
}
SkinMeshInfo::SkinMeshInfo()
{
memset(this, 0, sizeof(SkinMeshInfo));
}
void SkinMeshInfo::Allocate()
{
size_t size = boneCount * sizeof(Matrix4x4f) + sizeof(float) * blendshapeCount;
if (size == 0)
return;
allocatedBuffer = (UInt8*)UNITY_MALLOC_ALIGNED(kMemSkinning, size, 64);
UInt8* head = allocatedBuffer;
if (boneCount != 0)
{
cachedPose = reinterpret_cast<Matrix4x4f*> (head);
head += sizeof(Matrix4x4f) * boneCount;
}
if (blendshapeCount != 0)
{
blendshapeWeights = reinterpret_cast<float*> (head);
}
}
void SkinMeshInfo::Release() const
{
if (allocatedBuffer)
UNITY_FREE(kMemSkinning, allocatedBuffer);
}
|