1
我做一個遊戲,並有加載3D模型的資源文件格式非常緩慢,紋理和網格等這樣的定義:升壓序列化和文件IO是
/* PackageHeader definition */
struct PackageHeader
{
std::string mSignature;
uint8_t mMajorVersion;
uint8_t mMinorVersion;
PackageHeader();
};
/* PackageMesh definition */
struct PackageMesh
{
std::vector<Vec3> mVertexData;
std::vector<Vec3> mNormalData;
std::vector<Vec2> mTexCoordsData;
std::vector<uint32_t> mIndiceData;
uint16_t mMaterialIndex;
bool mHasMaterial;
PackageMesh();
};
/* PackageTexture definition */
struct PackageTexture
{
std::string mName;
std::vector<uint8_t> mTextureData;
uint32_t mTextureWidth; // width/height in pixels
uint32_t mTextureHeight;
ITexture::TextureFormat mTextureFormat;
ITexture::TextureType mTextureType;
PackageTexture();
};
/* PackageMaterial definition */
struct PackageMaterial
{
std::string mName;
PackageTexture mDiffuseTexture;
Vec3 mDiffuseColor;
Vec3 mAmbientColor;
Vec3 mSpecularColor;
Vec3 mEmissiveColor;
PackageMaterial();
};
/* PackageModel definition */
struct PackageModel
{
std::string mName;
std::vector<PackageModel> mChildren;
std::vector<PackageMesh> mMeshes;
Mat4 mTransform;
PackageModel();
};
/* JonsPackage definition */
struct JonsPackage
{
PackageHeader mHeader;
std::vector<PackageModel> mModels;
std::vector<PackageMaterial> mMaterials;
JonsPackage();
};
我使用升壓序列化保存/從文件系統加載,直到現在一直是絕對令人驚歎的,因爲它幾乎不需要代碼來完成它。
但是進口一些3D模型,然後嘗試再次加載它之後,加載時間是巨大的;從文件系統加載到反序列化需要將近30秒的時間。
這是序列化的代碼/反序列化:
JonsPackagePtr ReadJonsPkg(const std::string& jonsPkgName)
{
std::ifstream jonsPkgStream(jonsPkgName.c_str(), std::ios::in | std::ios::binary); // TODO: support opening of older resource packages
JonsPackagePtr pkg(HeapAllocator::GetDefaultHeapAllocator().AllocateObject<JonsPackage>(), boost::bind(&HeapAllocator::DeallocateObject<JonsPackage>, &HeapAllocator::GetDefaultHeapAllocator(), _1));
if (jonsPkgStream && jonsPkgStream.good() && jonsPkgStream.is_open())
{
std::stringstream buf(std::ios_base::binary | std::ios_base::in | std::ios_base::out);
buf << jonsPkgStream.rdbuf();
buf.seekg(0);
jonsPkgStream.close();
boost::archive::binary_iarchive iar(buf);
iar >> (*pkg.get());
}
jonsPkgStream.close();
return pkg;
}
bool WriteJonsPkg(const std::string& jonsPkgName, const JonsPackagePtr pkg)
{
std::ofstream outStream(jonsPkgName.c_str(), std::ios::out | std::ios::binary | std::ios::trunc);
bool ret = false;
if (outStream.is_open())
{
boost::archive::binary_oarchive oar(outStream);
oar << (*pkg.get());
ret = true;
}
return ret;
}
這裏是VS2012性能analys的圖像: http://postimg.org/image/smdsnu5gl/
我使用的資源文件是上盤約26 MB,包含3個封裝模型和14個封裝紋理。我能做些什麼呢,我的文件格式設計是死衚衕嗎?
VS的運行之外的伎倆,我同樣的問題。它仍然附加調試器,即使在我沒有意識到的發佈中。 – bossbarber