Skip to content

Instantly share code, notes, and snippets.

@chadaustin
Created September 29, 2014 23:19
Show Gist options
  • Save chadaustin/0ad326c7e06cda799cf7 to your computer and use it in GitHub Desktop.
Save chadaustin/0ad326c7e06cda799cf7 to your computer and use it in GitHub Desktop.
cal3d::SSEArray<CalCoreSubmesh::Vertex> MorphSubmeshCache;
void accumulateMorphTarget(
const cal3d::MorphTarget* morphTarget
) {
// VC++ isn't hoisting this SSE register out of the loop, so do it manually.
CalVector4 weight(morphTarget->weight); // _mm_load1_ps
const CalCoreMorphTarget::VertexOffsetArray& vertexOffsets = morphTarget->coreMorphTarget->vertexOffsets;
const VertexOffset* morphVertex = cal3d::pointerFromVector(vertexOffsets);
const VertexOffset* lastMorphVertex = morphVertex + vertexOffsets.size();
for (; morphVertex != lastMorphVertex; ++morphVertex) {
size_t i = morphVertex->vertexId;
MorphSubmeshCache[i].position += weight * morphVertex->position; // mulps; addps
MorphSubmeshCache[i].normal += weight * morphVertex->normal; // mulps; addps
}
}
const CalCoreSubmesh::Vertex* accumulateMorphTargets(
size_t vertexCount,
const CalCoreSubmesh::Vertex* sourceVertices,
const cal3d::MorphTarget* morphTarget,
const cal3d::MorphTarget* morphTargetEnd
) {
if (vertexCount > MorphSubmeshCache.size()) {
MorphSubmeshCache.destructive_resize(vertexCount);
}
cal3d::verify(morphTarget->weight != 0.0f, "Don't bother accumulating morphs until you have found an active morph target");
// In theory we could initialize the accumulation buffer with
// "morphTarget", since we know it's active. It would reduce
// memory traffic but increase code complexity, and probably
// never matters in practice.
std::copy(sourceVertices, sourceVertices + vertexCount, MorphSubmeshCache.begin());
// Now find active morph targets and accumulate them
while (morphTarget != morphTargetEnd) {
if (morphTarget->weight != 0.0f) {
accumulateMorphTarget(morphTarget);
}
++morphTarget;
}
return cal3d::pointerFromVector(MorphSubmeshCache);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment