Asynchronous batched scene queries in PhysX 3 (equivalent of NX_SQE_ASYNCHRONOUS from PhysX 2)?


I am currently porting C++ code from using PhysX SDK 2.8.4 to using PhysX SDK 3.2.1 (PC Win32), and I’m having trouble with batched raycasts.

Here is the simple structure of the old code:

NxSceneQueryDesc desc; = mReport;
desc.executeMode = NX_SQE_ASYNCHRONOUS;  // Asynchronous mode.
NxSceneQuery* batch = mScene->createSceneQuery(desc);

batch->raycastAnyShape(/* params (1) */);
batch->raycastAnyShape(/* params (2) */);
batch->raycastClosestShape(/* params (3) */);
// ...

batch->execute();  // Non-blocking call, returns immediately.

foo();  // Do other work
bar();  // while raycasts are being
baz();  // executed in parallel.

batch->finish(true);  // Blocking call (until all queries have been executed).

Indeed, in the docs we had (emphasis mine):

But now in PhysX 3 (where NxSceneQuery has been renamed PxBatchQuery), there is no “PxBatchQueryExecuteMode” nor “PxBatchQuery::finish()” anymore, and it seems that calls to PxBatchQuery::execute() are always blocking.

So here’s my question: How can I get the same old behavior (have batched queries execute asynchronously in a background thread) in PhysX 3.2?

Do I have to inherit a class from pxtask::Task and override run() to call PxBatchQuery::execute() inside or something? (I’m not familiar with the new Task Management system…)

Thank you very much.

Could you please take a look at the SampleSubmarine?

The batched queries are done in Crab::scanForObstacles which is called in the run() function of pxtask::LightCpuTask.

@HaiLocLu Thank you for your answer. Actually that’s precisely why I did eventually when porting the “substepping” part of the old code (even if I find it somewhat overkill compared to the old version).

But now that’s still not really the same behavior as in PhysX 2.8, because the code’s main loop runs at 30 fps but the PhysX simulation at 120 fps, so there are 4 simulate()-fetchResults() substeps per frame, but there is only 1 raycast batch per frame (whereas in SampleSubmarine the Crab executes a batch at each substep). So for now we execute the batch during the first substep, which is thus “heavier” than the subsequent ones…