Native Serialized BVH not compatible between Windows/Linux

Empire-Phoenix
Posts: 24
Joined: Sat Nov 07, 2009 7:57 pm

Native Serialized BVH not compatible between Windows/Linux

Post by Empire-Phoenix »

Hi,

related: https://github.com/jMonkeyEngine/jmonke ... issues/254

I use large MeshCollisionShapes that I save using the serialization mechanisms of bullet.
If i load it on a same OS, they work flawless.

However if I load them on a Window if generated on Linux or vice vesa, a native crash occures.
The question is, is this expected by design? Is this a mistake in my wrapping code?

Here is the code doing the actual serisialisation:

https://github.com/jMonkeyEngine/jmonke ... nShape.cpp

Code: Select all

//mesh is a btBvhTriangleMeshShape
 btOptimizedBvh* bvh = mesh->getOptimizedBvh();
       unsigned int ssize = bvh->calculateSerializeBufferSize();
       char* buffer = (char*)btAlignedAlloc(ssize, 16);
       bool success = bvh->serialize(buffer, ssize, true);
 if(!success){
//error handling for java side
      jclass newExc = env->FindClass("java/lang/RuntimeException");
      env->ThrowNew(newExc, "Unableto Serialize, native error reported");
    }
//copy data to java side
         jbyteArray byteArray = env->NewByteArray(ssize);
         env->SetByteArrayRegion(byteArray, 0, ssize , (jbyte*) buffer);
//free the buffer after it is copied
   btAlignedFree(buffer);
The restoring part is:

Code: Select all

void* buffer = btAlignedAlloc(len, 16);
//just copy back from java side
        env->GetByteArrayRegion (bytearray, 0, len, reinterpret_cast<jbyte*>(buffer));

  btOptimizedBvh* bhv = btOptimizedBvh::deSerializeInPlace(buffer, len, true);
//on java side the objects are simply a long, so need to cast to actual type
  btBvhTriangleMeshShape* mesh = reinterpret_cast<btBvhTriangleMeshShape*>(meshobj);
  mesh->setOptimizedBvh(bhv);
(I never tested on 32bit systems, so might work there or not.)
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Native Serialized BVH not compatible between Windows/Lin

Post by Erwin Coumans »

The btOptimizedBvh::deSerializeInPlace is not a proper part of the Bullet serialization,
it was some contribution that is not well tested.

The proper way of serializing data using Bullet is described here:
http://bulletphysics.org/mediawiki-1.5. ... ialization
If you follow the description in this wiki page, the serialization/deserialization is cross-platform and platforms with different endianness, 32-bit versus 64bit, double vs single precision float gets automatically resolved.

Not all data might be serialized yet using the proper Bullet serialization, it might be that the acceleration structures (btOptimizedBvh) need to be rebuild from scratch after loading (I don;t recall).

Thanks!
Erwin
Empire-Phoenix
Posts: 24
Joined: Sat Nov 07, 2009 7:57 pm

Re: Native Serialized BVH not compatible between Windows/Lin

Post by Empire-Phoenix »

Hi,

thank you for your answer, I thinkthis will work,
according to the dna the acceleration bvh structures are actuall saved and imported in the loader.

What I kinda struggle a bit with is,
how can i actuall get the stored object from the importer?

Eg i save a btBvhTriangleMeshShape to a buffer.
I load the buffer via a newly allocated btBulletWorldImporter

no how can i get the loaded object(s) from the btBulletWorldImporter?
Sadly the linked documentation is not explaining this part.

-> I don't want to directly load it into a physicspace

Is this the right approach?

(ignore the probably compile errors, the main approch is more what i want to validate here :) )

Code: Select all

btBulletWorldImporter* importer = new btBulletWorldImporter();
	fileLoader->loadFileFromMemory(buffer,len);
 	
	btBvhTriangleMeshShape* mesh = reinterpret_cast<btBvhTriangleMeshShape*>(meshobj);
  	int loadedbvh = importer->getNumBvhs();
	btOptimizedBvh* bvh -> importer->getBvhByIndex();
	mesh->setOptimizedBvh(bhv);