From b712bf1770aba8bb57f4dc072938edd8fce1fa66 Mon Sep 17 00:00:00 2001 From: Gargaj Date: Fri, 8 Aug 2014 12:46:29 +0200 Subject: [PATCH 1/3] Change to looped writes to avoid struct packing issues --- code/AssbinExporter.cpp | 28 ++++++++++++++++---------- code/AssbinLoader.cpp | 44 +++++++++++++++++++++++++++++++---------- 2 files changed, 52 insertions(+), 20 deletions(-) diff --git a/code/AssbinExporter.cpp b/code/AssbinExporter.cpp index 2a39a5be3..1c697b7e2 100644 --- a/code/AssbinExporter.cpp +++ b/code/AssbinExporter.cpp @@ -207,6 +207,14 @@ inline size_t WriteBounds(IOStream * stream, const T* in, unsigned int size) return t + Write(stream,maxc); } +template +inline size_t WriteArray(IOStream * stream, const T* in, unsigned int size) +{ + size_t n = 0; + for (unsigned int i=0; i(stream,in[i]); + return n; +} + // ---------------------------------------------------------------------------------- /** @class AssbinChunkWriter * @brief Chunk writer mechanism for the .assbin file structure @@ -356,7 +364,7 @@ inline size_t WriteBounds(IOStream * stream, const T* in, unsigned int size) if (shortened) { WriteBounds(&chunk,b->mWeights,b->mNumWeights); } // else write as usual - else chunk.Write(b->mWeights,1,b->mNumWeights*sizeof(aiVertexWeight)); + else WriteArray(&chunk,b->mWeights,b->mNumWeights); } // ----------------------------------------------------------------------------------- @@ -400,13 +408,13 @@ inline size_t WriteBounds(IOStream * stream, const T* in, unsigned int size) if (shortened) { WriteBounds(&chunk,mesh->mVertices,mesh->mNumVertices); } // else write as usual - else chunk.Write(mesh->mVertices,1,12*mesh->mNumVertices); + else WriteArray(&chunk,mesh->mVertices,mesh->mNumVertices); } if (mesh->mNormals) { if (shortened) { WriteBounds(&chunk,mesh->mNormals,mesh->mNumVertices); } // else write as usual - else chunk.Write(mesh->mNormals,1,12*mesh->mNumVertices); + else WriteArray(&chunk,mesh->mNormals,mesh->mNumVertices); } if (mesh->mTangents && mesh->mBitangents) { if (shortened) { @@ -414,8 +422,8 @@ inline size_t WriteBounds(IOStream * stream, const T* in, unsigned int size) WriteBounds(&chunk,mesh->mBitangents,mesh->mNumVertices); } // else write as usual else { - chunk.Write(mesh->mTangents,1,12*mesh->mNumVertices); - chunk.Write(mesh->mBitangents,1,12*mesh->mNumVertices); + WriteArray(&chunk,mesh->mTangents,mesh->mNumVertices); + WriteArray(&chunk,mesh->mBitangents,mesh->mNumVertices); } } for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_COLOR_SETS;++n) { @@ -425,7 +433,7 @@ inline size_t WriteBounds(IOStream * stream, const T* in, unsigned int size) if (shortened) { WriteBounds(&chunk,mesh->mColors[n],mesh->mNumVertices); } // else write as usual - else chunk.Write(mesh->mColors[n],16*mesh->mNumVertices,1); + else WriteArray(&chunk,mesh->mColors[n],mesh->mNumVertices); } for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_TEXTURECOORDS;++n) { if (!mesh->mTextureCoords[n]) @@ -437,7 +445,7 @@ inline size_t WriteBounds(IOStream * stream, const T* in, unsigned int size) if (shortened) { WriteBounds(&chunk,mesh->mTextureCoords[n],mesh->mNumVertices); } // else write as usual - else chunk.Write(mesh->mTextureCoords[n],12*mesh->mNumVertices,1); + else WriteArray(&chunk,mesh->mTextureCoords[n],mesh->mNumVertices); } // write faces. There are no floating-point calculations involved @@ -532,21 +540,21 @@ inline size_t WriteBounds(IOStream * stream, const T* in, unsigned int size) WriteBounds(&chunk,nd->mPositionKeys,nd->mNumPositionKeys); } // else write as usual - else chunk.Write(nd->mPositionKeys,1,nd->mNumPositionKeys*sizeof(aiVectorKey)); + else WriteArray(&chunk,nd->mPositionKeys,nd->mNumPositionKeys); } if (nd->mRotationKeys) { if (shortened) { WriteBounds(&chunk,nd->mRotationKeys,nd->mNumRotationKeys); } // else write as usual - else chunk.Write(nd->mRotationKeys,1,nd->mNumRotationKeys*sizeof(aiQuatKey)); + else WriteArray(&chunk,nd->mRotationKeys,nd->mNumRotationKeys); } if (nd->mScalingKeys) { if (shortened) { WriteBounds(&chunk,nd->mScalingKeys,nd->mNumScalingKeys); } // else write as usual - else chunk.Write(nd->mScalingKeys,1,nd->mNumScalingKeys*sizeof(aiVectorKey)); + else WriteArray(&chunk,nd->mScalingKeys,nd->mNumScalingKeys); } } diff --git a/code/AssbinLoader.cpp b/code/AssbinLoader.cpp index 617355263..9cc725092 100644 --- a/code/AssbinLoader.cpp +++ b/code/AssbinLoader.cpp @@ -121,6 +121,30 @@ aiMatrix4x4 Read(IOStream * stream) return m; } +template <> +aiVectorKey Read(IOStream * stream) +{ + aiVectorKey v; + v.mTime = Read(stream); + v.mValue = Read(stream); + return v; +} + +template <> +aiQuatKey Read(IOStream * stream) +{ + aiQuatKey v; + v.mTime = Read(stream); + v.mValue = Read(stream); + return v; +} + +template +void ReadArray(IOStream * stream, T * out, unsigned int size) +{ + for (unsigned int i=0; i(stream); +} + template void ReadBounds( IOStream * stream, T* p, unsigned int n ) { // not sure what to do here, the data isn't really useful. @@ -176,7 +200,7 @@ void AssbinImporter::ReadBinaryBone( IOStream * stream, aiBone* b ) else { b->mWeights = new aiVertexWeight[b->mNumWeights]; - stream->Read(b->mWeights,1,b->mNumWeights*sizeof(aiVertexWeight)); + ReadArray(stream,b->mWeights,b->mNumWeights); } } @@ -203,7 +227,7 @@ void AssbinImporter::ReadBinaryMesh( IOStream * stream, aiMesh* mesh ) else { mesh->mVertices = new aiVector3D[mesh->mNumVertices]; - stream->Read(mesh->mVertices,1,12*mesh->mNumVertices); + ReadArray(stream,mesh->mVertices,mesh->mNumVertices); } } if (c & ASSBIN_MESH_HAS_NORMALS) @@ -214,7 +238,7 @@ void AssbinImporter::ReadBinaryMesh( IOStream * stream, aiMesh* mesh ) else { mesh->mNormals = new aiVector3D[mesh->mNumVertices]; - stream->Read(mesh->mNormals,1,12*mesh->mNumVertices); + ReadArray(stream,mesh->mNormals,mesh->mNumVertices); } } if (c & ASSBIN_MESH_HAS_TANGENTS_AND_BITANGENTS) @@ -226,9 +250,9 @@ void AssbinImporter::ReadBinaryMesh( IOStream * stream, aiMesh* mesh ) else { mesh->mTangents = new aiVector3D[mesh->mNumVertices]; - stream->Read(mesh->mTangents,1,12*mesh->mNumVertices); + ReadArray(stream,mesh->mTangents,mesh->mNumVertices); mesh->mBitangents = new aiVector3D[mesh->mNumVertices]; - stream->Read(mesh->mBitangents,1,12*mesh->mNumVertices); + ReadArray(stream,mesh->mBitangents,mesh->mNumVertices); } } for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_COLOR_SETS;++n) @@ -243,7 +267,7 @@ void AssbinImporter::ReadBinaryMesh( IOStream * stream, aiMesh* mesh ) else { mesh->mColors[n] = new aiColor4D[mesh->mNumVertices]; - stream->Read(mesh->mColors[n],16*mesh->mNumVertices,1); + ReadArray(stream,mesh->mColors[n],mesh->mNumVertices); } } for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_TEXTURECOORDS;++n) @@ -260,7 +284,7 @@ void AssbinImporter::ReadBinaryMesh( IOStream * stream, aiMesh* mesh ) else { mesh->mTextureCoords[n] = new aiVector3D[mesh->mNumVertices]; - stream->Read(mesh->mTextureCoords[n],12*mesh->mNumVertices,1); + ReadArray(stream,mesh->mTextureCoords[n],mesh->mNumVertices); } } @@ -361,7 +385,7 @@ void AssbinImporter::ReadBinaryNodeAnim(IOStream * stream, aiNodeAnim* nd) } // else write as usual else { nd->mPositionKeys = new aiVectorKey[nd->mNumPositionKeys]; - stream->Read(nd->mPositionKeys,1,nd->mNumPositionKeys*sizeof(aiVectorKey)); + ReadArray(stream,nd->mPositionKeys,nd->mNumPositionKeys); } } if (nd->mNumRotationKeys) { @@ -372,7 +396,7 @@ void AssbinImporter::ReadBinaryNodeAnim(IOStream * stream, aiNodeAnim* nd) else { nd->mRotationKeys = new aiQuatKey[nd->mNumRotationKeys]; - stream->Read(nd->mRotationKeys,1,nd->mNumRotationKeys*sizeof(aiQuatKey)); + ReadArray(stream,nd->mRotationKeys,nd->mNumRotationKeys); } } if (nd->mNumScalingKeys) { @@ -383,7 +407,7 @@ void AssbinImporter::ReadBinaryNodeAnim(IOStream * stream, aiNodeAnim* nd) else { nd->mScalingKeys = new aiVectorKey[nd->mNumScalingKeys]; - stream->Read(nd->mScalingKeys,1,nd->mNumScalingKeys*sizeof(aiVectorKey)); + ReadArray(stream,nd->mScalingKeys,nd->mNumScalingKeys); } } } From fa99aa0d3a9eebd7f9677e3ccac1204ee7971d7c Mon Sep 17 00:00:00 2001 From: Gargaj Date: Fri, 8 Aug 2014 12:56:11 +0200 Subject: [PATCH 2/3] add remaining specializations hopefully i didnt mess this up too bad --- code/AssbinLoader.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/code/AssbinLoader.cpp b/code/AssbinLoader.cpp index 9cc725092..a17d37f9e 100644 --- a/code/AssbinLoader.cpp +++ b/code/AssbinLoader.cpp @@ -100,6 +100,38 @@ T Read(IOStream * stream) return t; } +template <> +aiVector3D Read(IOStream * stream) +{ + aiVector3D v; + v.x = Read(stream); + v.y = Read(stream); + v.z = Read(stream); + return v; +} + +template <> +aiColor4D Read(IOStream * stream) +{ + aiColor4D c; + c.r = Read(stream); + c.g = Read(stream); + c.b = Read(stream); + c.a = Read(stream); + return c; +} + +template <> +aiQuaternion Read(IOStream * stream) +{ + aiQuaternion v; + v.w = Read(stream); + v.x = Read(stream); + v.y = Read(stream); + v.z = Read(stream); + return v; +} + template <> aiString Read(IOStream * stream) { @@ -109,6 +141,15 @@ aiString Read(IOStream * stream) return s; } +template <> +aiVertexWeight Read(IOStream * stream) +{ + aiVertexWeight w; + w.mVertexId = Read(stream); + w.mWeight = Read(stream); + return w; +} + template <> aiMatrix4x4 Read(IOStream * stream) { From 58b78fa2654959cf19a63773762697948abafafc Mon Sep 17 00:00:00 2001 From: Gargaj Date: Fri, 8 Aug 2014 13:10:05 +0200 Subject: [PATCH 3/3] comment --- code/AssbinExporter.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/code/AssbinExporter.cpp b/code/AssbinExporter.cpp index 1c697b7e2..29ebfeee7 100644 --- a/code/AssbinExporter.cpp +++ b/code/AssbinExporter.cpp @@ -207,6 +207,8 @@ inline size_t WriteBounds(IOStream * stream, const T* in, unsigned int size) return t + Write(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 inline size_t WriteArray(IOStream * stream, const T* in, unsigned int size) {