🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How to save cooked geometry data in PhysX?

Started by
3 comments, last by cgrant 6 years, 11 months ago

In PhysX 3.3.4, convexmesh\trianglemesh\heightfield should be cooked before use, and they could be cooked offline or at runtime.
 

According to Geometry, we can cook a geometry in two manners:

  • offline manner by streaming

PxDefaultMemoryOutputStream writeBuffer;
cooking.cookTriangleMesh(someDescription, writeBuffer);
PxDefaultMemoryInputData readBuffer(writeBuffer.getData(), writeBuffer.getSize());
PxTriangleMesh* aTriangleMesh = physics.createTriangleMesh(readBuffer);
  • or runtime manner by cook api

PxTriangleMesh* aTriangleMesh = theCooking->createTriangleMesh(meshDesc, thePhysics->getPhysicsInsertionCallback());

I should use the offline manner to achieve the best performance. I read the source code of PhysX and found them identical, except for an extra stream in offline manner containing data generated at cook time. So to speed up performance, should I save the stream to disk and recovery the cooked data at runtime to make cook actually "offline"?

However, Serialization says that

Quote

Note: cooking also generates a binary output stream. The primary purpose of cooking, however, is to translate from a user format to a format suitable for the SDK runtime, and so it is not considered a serialization mechanism. Loading a cooked mesh from a stream involves allocation and endian conversion. As a consequence, it is much less efficient than PhysX' binary serialization mechanism. See Shapes for more details about cooking.


So the stream generated in the offline cook manner should not be stored at disk. If that is the case, what does the redundancy stream for?
If I use the PhysX binary serialization mechanism, how to make the cook procedure offline and achieve the best performance?

Advertisement

There seems to be a disjoint in the question you are asking and the information used for illustration. Maybe the following will help clarify what you are trying to achieve.

1. If you are cooking high poly mesh, I would refrain from doing so as a tri-mesh is not optimal for PhysX so I would try using convex meshes where appropriate. With that said I don't recall if PhysX limits the number of vertices in a tri-mesh, but it does for convex meshes. Runtime cooking of a few convex meshes is not that slow, but I would image the cost would add up if you are cooking a lot of meshes. The trade-off here would be whether or not you have a hard load-time requirement.

2. When you cook any representation, be resulting binary stream can be serialized to disk and reloaded. This is where I got confused as the 2 snippets posted just shows 2 ways of creating a triangle mesh representation, not the offline/runtime method as you would think.  The first snippet posted would be the same offline/runtime. The only difference being with offline method, some external tool is used to cook the data using the same method as you would at runtime. However, instead of just turning around and consuming the buffer after cooking, the tool would save the stream to disk. Whenever the app is run, the cooked data is read from disk into a stream and used to create the tri-mesh representation.

See the documentation regarding PxPhysics::createTriangleMesh


Hope that helps clarify the issue.

Thanks for your reply.

I got a bit confused between the stream of cooked data and the binary serialization mechanism before.

Based on your reply, can I interpret this way:

Save/load the stream of cooked data is a local cache mechanism (snippet 1). We can cache the stream (e.g. to a filestream on disk) the first time cooking the geometry, and recover the geometry from the cache file whenever we use, avoiding the cooking procedure. However, due to its platform-dependences, the cached file is better generated and used by one user, its not a good way to distribute or share these cache files with others. 

Snippet 2 does runtime cooking, it is suitable for simple geometries, where runtime performance is not that critical.

The binary serialization mechanism is a global resource exchanging mechanism. It is designed to produce, distribute and share physx resources with others. The only two differences between binary serialization and RepX format are human readability and file size.

Yes..that is correct. As long as the platform doesn't change...ex going from PC to ARM etc..your serialize data should work on the same platform it was cooked on. So I don't understand the comment in regards to the cached file is better generated and used by one user.

 

This topic is closed to new replies.

Advertisement