Some questions about serialization

Post Reply
ed_welch
Posts: 43
Joined: Wed Mar 04, 2015 6:16 pm

Some questions about serialization

Post by ed_welch »

Hi,
I'm looking into the serialization code. I see this code in the samples:

Code: Select all

	if (import.loadFile("myShape.bullet"))
	{
		int numBvh = import.getNumBvhs();
		if (numBvh)
		{
			btOptimizedBvh* bvh = import.getBvhByIndex(0);
			btVector3 aabbMin(-1000,-1000,-1000),aabbMax(1000,1000,1000);
	
			trimeshShape  = new btBvhTriangleMeshShape(m_indexVertexArrays,useQuantizedAabbCompression,aabbMin,aabbMax,false);
			trimeshShape->setOptimizedBvh(bvh);
			//trimeshShape  = new btBvhTriangleMeshShape(m_indexVertexArrays,useQuantizedAabbCompression,aabbMin,aabbMax);
			//trimeshShape->setOptimizedBvh(bvh);
	
		}
		int numShape = import.getNumCollisionShapes();
		if (numShape)
		{
			trimeshShape = (btBvhTriangleMeshShape*)import.getCollisionShapeByIndex(0);
			
			//if you know the name, you can also try to get the shape by name:
			const char* meshName = import.getNameForPointer(trimeshShape);
			if (meshName)
				trimeshShape = (btBvhTriangleMeshShape*)import.getCollisionShapeByName(meshName);
			
		}
	}

It seems that the code above is loading the same thing twice, two different ways, first via bvh, then via getCollisionShapeByIndex.
Why is it doing that? I'm guessing that I should only use one method, but really I have no idea. Is there any documentation for this?
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Some questions about serialization

Post by Flix »

ed_welch wrote:It seems that the code above is loading the same thing twice, two different ways, first via bvh, then via getCollisionShapeByIndex.
Why is it doing that? I'm guessing that I should only use one method, but really I have no idea. Is there any documentation for this?
Not sure if I'm right, but I remember that when serialization was added to Bullet, at first only the BVH was serialized and had to be added manually to the collision shape. If this is the case, that code is simply backward compatible for old Bullet files (provided that if numBvh>0 numShape=0 or viceversa).

But I'm not sure about it...
ed_welch
Posts: 43
Joined: Wed Mar 04, 2015 6:16 pm

Re: Some questions about serialization

Post by ed_welch »

Thanks for the answer Flix.
I noticed that when I write data out using serializeSingleShape it's creating a 32KB file for a single mesh with only 100 vertices. That seems to be abnormally high. I mean the whole scene will be several mega bytes at that rate.
I'm just wondering if this is more of an experimental feature, rather than something that works well.
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: Some questions about serialization

Post by Basroil »

Serialization is meant to be more of a one time use thing I think, it's not exactly optimized for constant read/write.

The file size though seems more like just your disk formatting rather than the file actually being that large. What's the actual file size? (not on-disk size)
ed_welch
Posts: 43
Joined: Wed Mar 04, 2015 6:16 pm

Re: Some questions about serialization

Post by ed_welch »

The size is 31.2K.
Anyway, I'm only using serialization to speed up the creation btBvhTriangleMeshShapes (loading an entire scene takes 2 minutes).
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Some questions about serialization

Post by Erwin Coumans »

The .bullet serialization in btDiscreteDynamicsWorld::serialize stores an encoded version of all data structures for backward and forward compatibility and to resolve differences in endianness, 32/64bit pointers, float/double precision between source (save) and destination (load) platform. That's why even an empty file takes those kilobytes. It would be possible to only store the encoded version of the structures used, that would make files much smaller. At the moment, the serialization is mainly to serialize a whole world with all objects, it is indeed not efficient to store individual elements.

An indeed, the serialization of BVH data predates the newer .bullet serialization, so if you mainly want to speed up the create of BVH structures, just use the btQuantizedBvh::serialize/deSerializeInPlace method.
ed_welch
Posts: 43
Joined: Wed Mar 04, 2015 6:16 pm

Re: Some questions about serialization

Post by ed_welch »

Erwin Coumans wrote:At the moment, the serialization is mainly to serialize a whole world with all objects, it is indeed not efficient to store individual elements.
Thanks for the answer Erwin,
I load about 200 meshes at start up and create a btBvhTriangleMeshShape for most of them, however I do not load them all into the btDiscreteDynamicsWorld at the same time - plus the same mesh maybe loaded many times into the world.
So, if serialization only works for stuff loaded into the world, it would seem that I can't really use it.
Post Reply