Skip to content

Commit 3fcf354

Browse files
committed
expose physx step start/finish
1 parent e029ca3 commit 3fcf354

File tree

7 files changed

+42
-9
lines changed

7 files changed

+42
-9
lines changed

include/sapien/physx/physx_default.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ class PhysxDefault {
2121
uint32_t totalAggregatePairsCapacity);
2222
static ::physx::PxgDynamicsMemoryConfig const &getGpuMemoryConfig();
2323

24+
static void setCpuWorkers(uint32_t count);
25+
static uint32_t getCpuWorkers();
26+
2427
// enable GPU simulation, may not be disabled
2528
static void EnableGPU();
2629
static bool GetGPUEnabled();

include/sapien/physx/physx_system.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ class PhysxSystemGpu : public PhysxSystem {
153153
getArticulationLinkComponents() const override;
154154

155155
void step() override;
156+
157+
void stepStart();
158+
void stepFinish();
159+
156160
bool isGpu() const override { return true; }
157161

158162
void gpuInit();

python/py_package/__init__.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,5 @@ def set_cuda_tensor_backend(backend: str) -> None:
6262
"""
6363
def set_log_level(level: str) -> None:
6464
pass
65-
__version__ = '3.0.0.dev20240110+ee6427de'
65+
__version__ = '3.0.0.dev20240110+e029ca37'
6666
SceneConfig = sapien.pysapien.physx.PhysxSceneConfig

python/py_package/pysapien/physx/__init__.pyi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ __all__ = [
3636
"PhysxRigidStaticComponent",
3737
"PhysxSceneConfig",
3838
"PhysxSystem",
39+
"get_cpu_workers",
3940
"get_default_material",
4041
"is_gpu_enabled",
42+
"set_cpu_workers",
4143
"set_default_material",
4244
"set_gpu_memory_config"
4345
]
@@ -811,6 +813,8 @@ class PhysxGpuSystem(PhysxSystem, sapien.pysapien.System):
811813
position `[1, 1, 1]` will be at position `[1, 1, 1] + [2, 1, 0] = [3, 2, 1]` in
812814
PhysX scene.
813815
"""
816+
def step_finish(self) -> None: ...
817+
def step_start(self) -> None: ...
814818
def sync_poses_gpu_to_cpu(self) -> None:
815819
"""
816820
Warning: this function is super slow and for debug only. Download all poses from the GPU and copy to SAPIEN entities.
@@ -1273,10 +1277,14 @@ class PhysxCpuSystem(PhysxSystem, sapien.pysapien.System):
12731277
pass
12741278
def _enable_gpu() -> None:
12751279
pass
1280+
def get_cpu_workers() -> int:
1281+
pass
12761282
def get_default_material() -> PhysxMaterial:
12771283
pass
12781284
def is_gpu_enabled() -> bool:
12791285
pass
1286+
def set_cpu_workers(count: int) -> None:
1287+
pass
12801288
def set_default_material(static_friction: float, dynamic_friction: float, restitution: float) -> None:
12811289
pass
12821290
def set_gpu_memory_config(temp_buffer_capacity: int = 16777216, max_rigid_contact_count: int = 524288, max_rigid_patch_count: int = 81920, heap_capacity: int = 67108864, found_lost_pairs_capacity: int = 262144, found_lost_aggregate_pairs_capacity: int = 1024, total_aggregate_pairs_capacity: int = 1024) -> None:

python/pybind/physx.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,10 @@ kernels will be launched to the provided stream.
448448

449449
.def("sync_poses_gpu_to_cpu", &PhysxSystemGpu::syncPosesGpuToCpu,
450450
"Warning: this function is super slow and for debug only. Download all poses from the "
451-
"GPU and copy to SAPIEN entities.");
451+
"GPU and copy to SAPIEN entities.")
452+
453+
.def("step_start", &PhysxSystemGpu::stepStart)
454+
.def("step_finish", &PhysxSystemGpu::stepFinish);
452455

453456
PyPhysxMaterial
454457
.def(py::init<float, float, float>(), py::arg("static_friction"),
@@ -1001,7 +1004,9 @@ set some motion axes of the dynamic rigid body to be locked
10011004
py::arg("heap_capacity") = 64 * 1024 * 1024,
10021005
py::arg("found_lost_pairs_capacity") = 256 * 1024,
10031006
py::arg("found_lost_aggregate_pairs_capacity") = 1024,
1004-
py::arg("total_aggregate_pairs_capacity") = 1024);
1007+
py::arg("total_aggregate_pairs_capacity") = 1024)
1008+
.def("set_cpu_workers", &PhysxDefault::setCpuWorkers, py::arg("count"))
1009+
.def("get_cpu_workers", &PhysxDefault::getCpuWorkers);
10051010

10061011
////////// end global //////////
10071012

src/physx/physx_default.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ static float gDynamicFriction{0.3};
1010
static float gRestitution{0.1};
1111
static std::weak_ptr<PhysxMaterial> gDefaultMaterial;
1212
static bool gGPUEnabled{false};
13+
static uint32_t gCpuWorkers{0};
1314

1415
static ::physx::PxgDynamicsMemoryConfig gGpuMemoryConfig{};
1516

@@ -49,6 +50,9 @@ ::physx::PxgDynamicsMemoryConfig const &PhysxDefault::getGpuMemoryConfig() {
4950
return gGpuMemoryConfig;
5051
}
5152

53+
void PhysxDefault::setCpuWorkers(uint32_t count) { gCpuWorkers = count; }
54+
uint32_t PhysxDefault::getCpuWorkers() { return gCpuWorkers; }
55+
5256
void PhysxDefault::EnableGPU() {
5357
if (PhysxEngine::GetIfExists()) {
5458
throw std::runtime_error(

src/physx/physx_system.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ PhysxSystemCpu::PhysxSystemCpu(PhysxSceneConfig const &config) : PhysxSystem(con
5252

5353
sceneDesc.flags = sceneFlags;
5454

55-
mPxCPUDispatcher = PxDefaultCpuDispatcherCreate(0);
55+
mPxCPUDispatcher = PxDefaultCpuDispatcherCreate(PhysxDefault::getCpuWorkers());
5656
if (!mPxCPUDispatcher) {
5757
throw std::runtime_error("PhysX system creation failed: failed to create CPU dispatcher");
5858
}
@@ -95,7 +95,7 @@ PhysxSystemGpu::PhysxSystemGpu(PhysxSceneConfig const &config) : PhysxSystem(con
9595

9696
sceneDesc.flags = sceneFlags;
9797

98-
mPxCPUDispatcher = PxDefaultCpuDispatcherCreate(0);
98+
mPxCPUDispatcher = PxDefaultCpuDispatcherCreate(PhysxDefault::getCpuWorkers());
9999
if (!mPxCPUDispatcher) {
100100
throw std::runtime_error("PhysX system creation failed: failed to create CPU dispatcher");
101101
}
@@ -190,8 +190,7 @@ std::unique_ptr<PhysxHitInfo> PhysxSystemCpu::raycast(Vec3 const &origin, Vec3 c
190190

191191
void PhysxSystemCpu::step() {
192192
mPxScene->simulate(mTimestep);
193-
while (!mPxScene->fetchResults(true)) {
194-
}
193+
mPxScene->fetchResults(true);
195194
for (auto c : mRigidStaticComponents) {
196195
c->syncPoseToEntity();
197196
}
@@ -210,11 +209,21 @@ void PhysxSystemGpu::step() {
210209

211210
++mTotalSteps;
212211
mPxScene->simulate(mTimestep);
213-
while (!mPxScene->fetchResults(true)) {
214-
}
212+
mPxScene->fetchResults(true);
215213
// TODO: does the GPU API require fetch results?
216214
}
217215

216+
void PhysxSystemGpu::stepStart() {
217+
if (!mGpuInitialized) {
218+
throw std::runtime_error("failed to step: gpu simulation is not initialized.");
219+
}
220+
221+
++mTotalSteps;
222+
mPxScene->simulate(mTimestep);
223+
}
224+
225+
void PhysxSystemGpu::stepFinish() { mPxScene->fetchResults(true); }
226+
218227
std::string PhysxSystemCpu::packState() const {
219228
std::ostringstream ss;
220229
for (auto &actor : mRigidDynamicComponents) {

0 commit comments

Comments
 (0)