Merge pull request #328 from Gargaj/assbin-arraywrite

Use specialized writes for arrays
pull/334/head
Kim Kulling 2014-08-08 13:59:30 +02:00
commit f55ef85578
2 changed files with 95 additions and 20 deletions

View File

@ -207,6 +207,16 @@ inline size_t WriteBounds(IOStream * stream, const T* in, unsigned int size)
return t + Write<T>(stream,maxc); return t + Write<T>(stream,maxc);
} }
// We use this to write out non-byte arrays so that we write using the specializations.
// This way we avoid writing out extra bytes that potentially come from struct alignment.
template <typename T>
inline size_t WriteArray(IOStream * stream, const T* in, unsigned int size)
{
size_t n = 0;
for (unsigned int i=0; i<size; i++) n += Write<T>(stream,in[i]);
return n;
}
// ---------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------
/** @class AssbinChunkWriter /** @class AssbinChunkWriter
* @brief Chunk writer mechanism for the .assbin file structure * @brief Chunk writer mechanism for the .assbin file structure
@ -356,7 +366,7 @@ inline size_t WriteBounds(IOStream * stream, const T* in, unsigned int size)
if (shortened) { if (shortened) {
WriteBounds(&chunk,b->mWeights,b->mNumWeights); WriteBounds(&chunk,b->mWeights,b->mNumWeights);
} // else write as usual } // else write as usual
else chunk.Write(b->mWeights,1,b->mNumWeights*sizeof(aiVertexWeight)); else WriteArray<aiVertexWeight>(&chunk,b->mWeights,b->mNumWeights);
} }
// ----------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------
@ -400,13 +410,13 @@ inline size_t WriteBounds(IOStream * stream, const T* in, unsigned int size)
if (shortened) { if (shortened) {
WriteBounds(&chunk,mesh->mVertices,mesh->mNumVertices); WriteBounds(&chunk,mesh->mVertices,mesh->mNumVertices);
} // else write as usual } // else write as usual
else chunk.Write(mesh->mVertices,1,12*mesh->mNumVertices); else WriteArray<aiVector3D>(&chunk,mesh->mVertices,mesh->mNumVertices);
} }
if (mesh->mNormals) { if (mesh->mNormals) {
if (shortened) { if (shortened) {
WriteBounds(&chunk,mesh->mNormals,mesh->mNumVertices); WriteBounds(&chunk,mesh->mNormals,mesh->mNumVertices);
} // else write as usual } // else write as usual
else chunk.Write(mesh->mNormals,1,12*mesh->mNumVertices); else WriteArray<aiVector3D>(&chunk,mesh->mNormals,mesh->mNumVertices);
} }
if (mesh->mTangents && mesh->mBitangents) { if (mesh->mTangents && mesh->mBitangents) {
if (shortened) { if (shortened) {
@ -414,8 +424,8 @@ inline size_t WriteBounds(IOStream * stream, const T* in, unsigned int size)
WriteBounds(&chunk,mesh->mBitangents,mesh->mNumVertices); WriteBounds(&chunk,mesh->mBitangents,mesh->mNumVertices);
} // else write as usual } // else write as usual
else { else {
chunk.Write(mesh->mTangents,1,12*mesh->mNumVertices); WriteArray<aiVector3D>(&chunk,mesh->mTangents,mesh->mNumVertices);
chunk.Write(mesh->mBitangents,1,12*mesh->mNumVertices); WriteArray<aiVector3D>(&chunk,mesh->mBitangents,mesh->mNumVertices);
} }
} }
for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_COLOR_SETS;++n) { for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_COLOR_SETS;++n) {
@ -425,7 +435,7 @@ inline size_t WriteBounds(IOStream * stream, const T* in, unsigned int size)
if (shortened) { if (shortened) {
WriteBounds(&chunk,mesh->mColors[n],mesh->mNumVertices); WriteBounds(&chunk,mesh->mColors[n],mesh->mNumVertices);
} // else write as usual } // else write as usual
else chunk.Write(mesh->mColors[n],16*mesh->mNumVertices,1); else WriteArray<aiColor4D>(&chunk,mesh->mColors[n],mesh->mNumVertices);
} }
for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_TEXTURECOORDS;++n) { for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_TEXTURECOORDS;++n) {
if (!mesh->mTextureCoords[n]) if (!mesh->mTextureCoords[n])
@ -437,7 +447,7 @@ inline size_t WriteBounds(IOStream * stream, const T* in, unsigned int size)
if (shortened) { if (shortened) {
WriteBounds(&chunk,mesh->mTextureCoords[n],mesh->mNumVertices); WriteBounds(&chunk,mesh->mTextureCoords[n],mesh->mNumVertices);
} // else write as usual } // else write as usual
else chunk.Write(mesh->mTextureCoords[n],12*mesh->mNumVertices,1); else WriteArray<aiVector3D>(&chunk,mesh->mTextureCoords[n],mesh->mNumVertices);
} }
// write faces. There are no floating-point calculations involved // write faces. There are no floating-point calculations involved
@ -532,21 +542,21 @@ inline size_t WriteBounds(IOStream * stream, const T* in, unsigned int size)
WriteBounds(&chunk,nd->mPositionKeys,nd->mNumPositionKeys); WriteBounds(&chunk,nd->mPositionKeys,nd->mNumPositionKeys);
} // else write as usual } // else write as usual
else chunk.Write(nd->mPositionKeys,1,nd->mNumPositionKeys*sizeof(aiVectorKey)); else WriteArray<aiVectorKey>(&chunk,nd->mPositionKeys,nd->mNumPositionKeys);
} }
if (nd->mRotationKeys) { if (nd->mRotationKeys) {
if (shortened) { if (shortened) {
WriteBounds(&chunk,nd->mRotationKeys,nd->mNumRotationKeys); WriteBounds(&chunk,nd->mRotationKeys,nd->mNumRotationKeys);
} // else write as usual } // else write as usual
else chunk.Write(nd->mRotationKeys,1,nd->mNumRotationKeys*sizeof(aiQuatKey)); else WriteArray<aiQuatKey>(&chunk,nd->mRotationKeys,nd->mNumRotationKeys);
} }
if (nd->mScalingKeys) { if (nd->mScalingKeys) {
if (shortened) { if (shortened) {
WriteBounds(&chunk,nd->mScalingKeys,nd->mNumScalingKeys); WriteBounds(&chunk,nd->mScalingKeys,nd->mNumScalingKeys);
} // else write as usual } // else write as usual
else chunk.Write(nd->mScalingKeys,1,nd->mNumScalingKeys*sizeof(aiVectorKey)); else WriteArray<aiVectorKey>(&chunk,nd->mScalingKeys,nd->mNumScalingKeys);
} }
} }

View File

@ -100,6 +100,38 @@ T Read(IOStream * stream)
return t; return t;
} }
template <>
aiVector3D Read<aiVector3D>(IOStream * stream)
{
aiVector3D v;
v.x = Read<float>(stream);
v.y = Read<float>(stream);
v.z = Read<float>(stream);
return v;
}
template <>
aiColor4D Read<aiColor4D>(IOStream * stream)
{
aiColor4D c;
c.r = Read<float>(stream);
c.g = Read<float>(stream);
c.b = Read<float>(stream);
c.a = Read<float>(stream);
return c;
}
template <>
aiQuaternion Read<aiQuaternion>(IOStream * stream)
{
aiQuaternion v;
v.w = Read<float>(stream);
v.x = Read<float>(stream);
v.y = Read<float>(stream);
v.z = Read<float>(stream);
return v;
}
template <> template <>
aiString Read<aiString>(IOStream * stream) aiString Read<aiString>(IOStream * stream)
{ {
@ -109,6 +141,15 @@ aiString Read<aiString>(IOStream * stream)
return s; return s;
} }
template <>
aiVertexWeight Read<aiVertexWeight>(IOStream * stream)
{
aiVertexWeight w;
w.mVertexId = Read<unsigned int>(stream);
w.mWeight = Read<float>(stream);
return w;
}
template <> template <>
aiMatrix4x4 Read<aiMatrix4x4>(IOStream * stream) aiMatrix4x4 Read<aiMatrix4x4>(IOStream * stream)
{ {
@ -121,6 +162,30 @@ aiMatrix4x4 Read<aiMatrix4x4>(IOStream * stream)
return m; return m;
} }
template <>
aiVectorKey Read<aiVectorKey>(IOStream * stream)
{
aiVectorKey v;
v.mTime = Read<double>(stream);
v.mValue = Read<aiVector3D>(stream);
return v;
}
template <>
aiQuatKey Read<aiQuatKey>(IOStream * stream)
{
aiQuatKey v;
v.mTime = Read<double>(stream);
v.mValue = Read<aiQuaternion>(stream);
return v;
}
template <typename T>
void ReadArray(IOStream * stream, T * out, unsigned int size)
{
for (unsigned int i=0; i<size; i++) out[i] = Read<T>(stream);
}
template <typename T> void ReadBounds( IOStream * stream, T* p, unsigned int n ) template <typename T> void ReadBounds( IOStream * stream, T* p, unsigned int n )
{ {
// not sure what to do here, the data isn't really useful. // not sure what to do here, the data isn't really useful.
@ -176,7 +241,7 @@ void AssbinImporter::ReadBinaryBone( IOStream * stream, aiBone* b )
else else
{ {
b->mWeights = new aiVertexWeight[b->mNumWeights]; b->mWeights = new aiVertexWeight[b->mNumWeights];
stream->Read(b->mWeights,1,b->mNumWeights*sizeof(aiVertexWeight)); ReadArray<aiVertexWeight>(stream,b->mWeights,b->mNumWeights);
} }
} }
@ -203,7 +268,7 @@ void AssbinImporter::ReadBinaryMesh( IOStream * stream, aiMesh* mesh )
else else
{ {
mesh->mVertices = new aiVector3D[mesh->mNumVertices]; mesh->mVertices = new aiVector3D[mesh->mNumVertices];
stream->Read(mesh->mVertices,1,12*mesh->mNumVertices); ReadArray<aiVector3D>(stream,mesh->mVertices,mesh->mNumVertices);
} }
} }
if (c & ASSBIN_MESH_HAS_NORMALS) if (c & ASSBIN_MESH_HAS_NORMALS)
@ -214,7 +279,7 @@ void AssbinImporter::ReadBinaryMesh( IOStream * stream, aiMesh* mesh )
else else
{ {
mesh->mNormals = new aiVector3D[mesh->mNumVertices]; mesh->mNormals = new aiVector3D[mesh->mNumVertices];
stream->Read(mesh->mNormals,1,12*mesh->mNumVertices); ReadArray<aiVector3D>(stream,mesh->mNormals,mesh->mNumVertices);
} }
} }
if (c & ASSBIN_MESH_HAS_TANGENTS_AND_BITANGENTS) if (c & ASSBIN_MESH_HAS_TANGENTS_AND_BITANGENTS)
@ -226,9 +291,9 @@ void AssbinImporter::ReadBinaryMesh( IOStream * stream, aiMesh* mesh )
else else
{ {
mesh->mTangents = new aiVector3D[mesh->mNumVertices]; mesh->mTangents = new aiVector3D[mesh->mNumVertices];
stream->Read(mesh->mTangents,1,12*mesh->mNumVertices); ReadArray<aiVector3D>(stream,mesh->mTangents,mesh->mNumVertices);
mesh->mBitangents = new aiVector3D[mesh->mNumVertices]; mesh->mBitangents = new aiVector3D[mesh->mNumVertices];
stream->Read(mesh->mBitangents,1,12*mesh->mNumVertices); ReadArray<aiVector3D>(stream,mesh->mBitangents,mesh->mNumVertices);
} }
} }
for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_COLOR_SETS;++n) for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_COLOR_SETS;++n)
@ -243,7 +308,7 @@ void AssbinImporter::ReadBinaryMesh( IOStream * stream, aiMesh* mesh )
else else
{ {
mesh->mColors[n] = new aiColor4D[mesh->mNumVertices]; mesh->mColors[n] = new aiColor4D[mesh->mNumVertices];
stream->Read(mesh->mColors[n],16*mesh->mNumVertices,1); ReadArray<aiColor4D>(stream,mesh->mColors[n],mesh->mNumVertices);
} }
} }
for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_TEXTURECOORDS;++n) for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_TEXTURECOORDS;++n)
@ -260,7 +325,7 @@ void AssbinImporter::ReadBinaryMesh( IOStream * stream, aiMesh* mesh )
else else
{ {
mesh->mTextureCoords[n] = new aiVector3D[mesh->mNumVertices]; mesh->mTextureCoords[n] = new aiVector3D[mesh->mNumVertices];
stream->Read(mesh->mTextureCoords[n],12*mesh->mNumVertices,1); ReadArray<aiVector3D>(stream,mesh->mTextureCoords[n],mesh->mNumVertices);
} }
} }
@ -361,7 +426,7 @@ void AssbinImporter::ReadBinaryNodeAnim(IOStream * stream, aiNodeAnim* nd)
} // else write as usual } // else write as usual
else { else {
nd->mPositionKeys = new aiVectorKey[nd->mNumPositionKeys]; nd->mPositionKeys = new aiVectorKey[nd->mNumPositionKeys];
stream->Read(nd->mPositionKeys,1,nd->mNumPositionKeys*sizeof(aiVectorKey)); ReadArray<aiVectorKey>(stream,nd->mPositionKeys,nd->mNumPositionKeys);
} }
} }
if (nd->mNumRotationKeys) { if (nd->mNumRotationKeys) {
@ -372,7 +437,7 @@ void AssbinImporter::ReadBinaryNodeAnim(IOStream * stream, aiNodeAnim* nd)
else else
{ {
nd->mRotationKeys = new aiQuatKey[nd->mNumRotationKeys]; nd->mRotationKeys = new aiQuatKey[nd->mNumRotationKeys];
stream->Read(nd->mRotationKeys,1,nd->mNumRotationKeys*sizeof(aiQuatKey)); ReadArray<aiQuatKey>(stream,nd->mRotationKeys,nd->mNumRotationKeys);
} }
} }
if (nd->mNumScalingKeys) { if (nd->mNumScalingKeys) {
@ -383,7 +448,7 @@ void AssbinImporter::ReadBinaryNodeAnim(IOStream * stream, aiNodeAnim* nd)
else else
{ {
nd->mScalingKeys = new aiVectorKey[nd->mNumScalingKeys]; nd->mScalingKeys = new aiVectorKey[nd->mNumScalingKeys];
stream->Read(nd->mScalingKeys,1,nd->mNumScalingKeys*sizeof(aiVectorKey)); ReadArray<aiVectorKey>(stream,nd->mScalingKeys,nd->mNumScalingKeys);
} }
} }
} }