Advertisement Jump to content

How to save cooked geometry data in PhysX?

This topic is 566 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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


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?

Edited by stanleyerror

Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites

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.


Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!