Repository navigation
Is there a lower-cost API for registering a single new rigid body at runtime? #500
SummaryWe spawn/despawn individual rigid bodies at runtime through the ovstage population API. Every add or remove goes through the full USD population round-trip, and ovphysx_update_from_ovstage alone accounts for ~92-93% of that round-trip's cost, regardless of how small the added/removed content is (a single 1m cube). This holds whether we go through our own wrapper code or call the raw API directly with nothing else in the process. Per spawn/despawn cycle: Measured breakdownThrough our own wrapper, averaged over 12 spawn/despawn cycles in our actual application
With zero wrapper code involved a standalone executable linking only
Code Snippet for measurements#include <chrono>
#include <cstdio>
#include <string>
#include <ovphysx/ovphysx.h>
#include <ovstage/ovstage.h>
#include <ovstage/ovstage_population.h>
using Clock = std::chrono::steady_clock;
static double us(Clock::time_point t0) { return std::chrono::duration<double, std::micro>(Clock::now() - t0).count(); }
static bool wait_pop(ovstage_instance_t* s, ovstage_population_enqueue_result_t r) {
if (r.status != OVSTAGE_OK) return false;
ovstage_population_op_wait_result_t w{};
return ovstage_population_wait_op(s, r.op_index, OVSTAGE_TIMEOUT_INFINITE, &w) == OVSTAGE_OK;
}
static bool floor(ovstage_instance_t* s, ovstage_ordinal_t o) {
ovstage_write_floor_desc_t d{};
d.ordinal = o;
d.scope = OVSTAGE_SCOPE_ALL;
ovstage_enqueue_result_t r = ovstage_advance_write_floor(s, &d);
if (r.status != OVSTAGE_OK) return false;
ovstage_op_wait_result_t w{};
return ovstage_wait_op(s, r.op_index, OVSTAGE_TIMEOUT_INFINITE, &w) == OVSTAGE_OK;
}
static std::string cube(const std::string& n) {
return "#usda 1.0\n(\n defaultPrim = \"" + n + "\"\n)\n\n"
"def Cube \"" + n + "\" (\n"
" prepend apiSchemas = [\"PhysicsRigidBodyAPI\", \"PhysicsCollisionAPI\", \"PhysicsMassAPI\"]\n)\n{\n"
" double size = 1\n"
" double3 xformOp:translate = (0, 3, 0)\n"
" uniform token[] xformOpOrder = [\"xformOp:translate\"]\n"
" float physics:mass = 1\n"
"}\n";
}
static bool add_ref(ovstage_instance_t* s, const std::string& usda, const std::string& tgt,
ovstage_population_usd_reference_handle_t* h) {
ovx_string_t u{usda.c_str(), usda.size()};
ovx_string_t t{tgt.c_str(), tgt.size()};
return wait_pop(s, ovstage_population_add_usd_reference_from_string(s, u, t, h));
}
int main() {
ovphysx_initialize();
ovphysx_handle_t handle = 0;
ovphysx_create_args args = OVPHYSX_CREATE_ARGS_DEFAULT;
if (ovphysx_create_instance(&args, &handle).status != OVPHYSX_API_SUCCESS) return 1;
ovstage_instance_desc_t d{};
d.name = "bench";
ovstage_instance_t* stage = nullptr;
if (ovstage_create_instance(&d, &stage) != OVSTAGE_OK) return 1;
ovstage_ordinal_t ord = 1;
std::string meta = "#usda 1.0\n(\n upAxis = \"Y\"\n metersPerUnit = 1\n)\n";
ovx_string_t metaStr{meta.c_str(), meta.size()};
if (!wait_pop(stage, ovstage_population_open_usd_from_string(stage, metaStr, ord, 0.0, OVSTAGE_POPULATION_DOMAIN_PHYSICS))) return 1;
if (!floor(stage, ord)) return 1;
++ord;
ovstage_population_usd_reference_handle_t h{};
if (!add_ref(stage, cube("Product_0"), "/Product_0", &h)) return 1;
if (!wait_pop(stage, ovstage_population_apply_usd_changes(stage, ord))) return 1;
if (!floor(stage, ord)) return 1;
if (ovphysx_attach_ovstage(handle, stage, ord).status != OVPHYSX_API_SUCCESS) return 1;
++ord;
constexpr int N = 12;
double update_us = 0, other_us = 0;
for (int i = 1; i <= N; ++i) {
Clock::time_point t0 = Clock::now();
if (!wait_pop(stage, ovstage_population_remove_usd_reference(stage, h))) return 1;
std::string name = "Product_" + std::to_string(i);
if (!add_ref(stage, cube(name), "/" + name, &h)) return 1;
if (!wait_pop(stage, ovstage_population_apply_usd_changes(stage, ord))) return 1;
if (!floor(stage, ord)) return 1;
other_us += us(t0);
t0 = Clock::now();
ovstage_ordinal_range_t range{0, ord, false};
if (ovphysx_update_from_ovstage(handle, range).status != OVPHYSX_API_SUCCESS) return 1;
update_us += us(t0);
++ord;
}
double total = update_us + other_us;
printf("update_from_ovstage: %.0fus/call (%.1f%%)\nother: %.0fus/call (%.1f%%)\n",
update_us / N, 100.0 * update_us / total, other_us / N, 100.0 * other_us / total);
return 0;
}
QuestionIs there a supported, lower-cost way to register a single new rigid body against a running
|
Replies: 1 comment 2 replies
|
Hi, Having said that this first version of ovphysx working with ovstage is not yet optimized, I think the issue you raised in the other ticket are probably resolved in update that should be available hopefully tomorrow. Let me check that, but in ideal case we are able to fix the perf so that there is no concern about the perf cost. Regards, |
Hi,
no there is no plan to expose a light-weight entry point, for that there is PhysX SDK itself. With ovphysx objects should be created through ovstage so that things work well with the rest of the ov ecosystem thats the rationale.
Having said that this first version of ovphysx working with ovstage is not yet optimized, I think the issue you raised in the other ticket are probably resolved in update that should be available hopefully tomorrow. Let me check that, but in ideal case we are able to fix the perf so that there is no concern about the perf cost.
Regards,
Ales