@series-inc/rundot-syncplay 6.0.0-rc.2 → 6.0.0-rc.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +133 -0
- package/core/deterministic-math.d.ts +75 -1
- package/core/native/instance_ledger.hpp +1095 -0
- package/core/native/legacy_checkpoint_ids.hpp +107 -0
- package/core/native/physics/abi.cpp +39 -211
- package/core/native/physics/abi.hpp +2 -6
- package/core/native/physics/articulation.cpp +1434 -0
- package/core/native/physics/articulation.hpp +279 -0
- package/core/native/physics/buoyancy.cpp +1588 -0
- package/core/native/physics/buoyancy.hpp +254 -0
- package/core/native/physics/capsule_triangle.cpp +168 -0
- package/core/native/physics/capsule_triangle.hpp +40 -0
- package/core/native/physics/capsule_triangle_native.inc +291 -0
- package/core/native/physics/certified_sum.hpp +218 -0
- package/core/native/physics/character.cpp +638 -0
- package/core/native/physics/character.hpp +169 -0
- package/core/native/physics/checkpoint_schema.hpp +379 -0
- package/core/native/physics/cloth.cpp +1401 -0
- package/core/native/physics/cloth.hpp +357 -0
- package/core/native/physics/constraint_rows.cpp +363 -2
- package/core/native/physics/constraint_rows.hpp +72 -1
- package/core/native/physics/deformable_query.cpp +1630 -0
- package/core/native/physics/deformable_query.hpp +578 -0
- package/core/native/physics/distance.cpp +1166 -0
- package/core/native/physics/distance.hpp +151 -0
- package/core/native/physics/exact/exact-applied.cpp +101 -0
- package/core/native/physics/exact/exact-applied.hpp +112 -0
- package/core/native/physics/exact/exact-dyadic.cpp +691 -0
- package/core/native/physics/exact/exact-dyadic.hpp +58 -0
- package/core/native/physics/exact/exact-operation.hpp +51 -0
- package/core/native/physics/exact/word-core.hpp +262 -0
- package/core/native/physics/extended_abi.cpp +1242 -0
- package/core/native/physics/extended_abi.hpp +355 -0
- package/core/native/physics/extended_abi_characters.cpp +261 -0
- package/core/native/physics/extended_abi_characters.hpp +434 -0
- package/core/native/physics/extended_abi_core_features.cpp +713 -0
- package/core/native/physics/extended_abi_core_features.hpp +226 -0
- package/core/native/physics/extended_abi_deformables.cpp +448 -0
- package/core/native/physics/extended_abi_deformables.hpp +867 -0
- package/core/native/physics/extended_abi_vehicles.cpp +411 -0
- package/core/native/physics/extended_abi_vehicles.hpp +222 -0
- package/core/native/physics/fracture.cpp +1981 -0
- package/core/native/physics/fracture.hpp +460 -0
- package/core/native/physics/gear.cpp +563 -0
- package/core/native/physics/gear.hpp +158 -0
- package/core/native/physics/geometry_memory.hpp +476 -0
- package/core/native/physics/geometry_store.cpp +547 -0
- package/core/native/physics/geometry_store.hpp +134 -0
- package/core/native/physics/lag_compensation.cpp +1626 -0
- package/core/native/physics/lag_compensation.hpp +405 -0
- package/core/native/physics/mass_properties.cpp +449 -0
- package/core/native/physics/mass_properties.hpp +23 -0
- package/core/native/physics/material_pose.hpp +988 -0
- package/core/native/physics/particles.cpp +1065 -0
- package/core/native/physics/particles.hpp +261 -0
- package/core/native/physics/path_joint.cpp +781 -0
- package/core/native/physics/path_joint.hpp +154 -0
- package/core/native/physics/physics.cpp +13043 -1065
- package/core/native/physics/physics.hpp +184 -8
- package/core/native/physics/planar_deformable.cpp +2433 -0
- package/core/native/physics/planar_deformable.hpp +889 -0
- package/core/native/physics/platformer_2d.cpp +1353 -0
- package/core/native/physics/platformer_2d.hpp +340 -0
- package/core/native/physics/powered_ragdoll.cpp +1100 -0
- package/core/native/physics/powered_ragdoll.hpp +279 -0
- package/core/native/physics/powertrain.cpp +586 -0
- package/core/native/physics/powertrain.hpp +180 -0
- package/core/native/physics/pulley.cpp +311 -0
- package/core/native/physics/pulley.hpp +121 -0
- package/core/native/physics/ragdoll.cpp +586 -0
- package/core/native/physics/ragdoll.hpp +221 -0
- package/core/native/physics/rope.cpp +692 -0
- package/core/native/physics/rope.hpp +215 -0
- package/core/native/physics/rounded_convex.cpp +1229 -0
- package/core/native/physics/rounded_convex.hpp +287 -0
- package/core/native/physics/sdf_geometry.cpp +1821 -0
- package/core/native/physics/sdf_geometry.hpp +474 -0
- package/core/native/physics/soft_body.cpp +1449 -0
- package/core/native/physics/soft_body.hpp +328 -0
- package/core/native/physics/soft_collision.cpp +1862 -0
- package/core/native/physics/soft_collision.hpp +561 -0
- package/core/native/physics/surface_velocity.cpp +366 -0
- package/core/native/physics/surface_velocity.hpp +243 -0
- package/core/native/physics/topology.cpp +357 -39
- package/core/native/physics/topology.hpp +97 -6
- package/core/native/physics/tracks.cpp +1179 -0
- package/core/native/physics/tracks.hpp +404 -0
- package/core/native/physics/vehicle.cpp +629 -0
- package/core/native/physics/vehicle.hpp +191 -0
- package/core/native/physics/voxel_cooking.cpp +1929 -0
- package/core/native/physics/voxel_cooking.hpp +395 -0
- package/core/native/physics/world.cpp +5290 -854
- package/core/native/physics/world.hpp +796 -24
- package/core/native/physics/world_abi.cpp +3840 -306
- package/core/native/physics/world_abi.hpp +530 -10
- package/core/native/physics/world_digest_visitor.hpp +277 -0
- package/core/native/physics/world_strong_digest.hpp +557 -0
- package/core/native/physics2d/ccd.cpp +1302 -0
- package/core/native/physics2d/ccd.hpp +141 -0
- package/core/native/physics2d/checkpoint_memory.hpp +349 -0
- package/core/native/physics2d/clock_primitives.hpp +138 -0
- package/core/native/physics2d/commands.cpp +174 -28
- package/core/native/physics2d/commands.hpp +3 -1
- package/core/native/physics2d/compound.cpp +581 -0
- package/core/native/physics2d/compound.hpp +248 -0
- package/core/native/physics2d/contour.cpp +325 -0
- package/core/native/physics2d/contour.hpp +143 -0
- package/core/native/physics2d/frame_scope.hpp +124 -0
- package/core/native/physics2d/joint_phases.hpp +93 -0
- package/core/native/physics2d/joints.cpp +587 -150
- package/core/native/physics2d/joints.hpp +5 -0
- package/core/native/physics2d/memory_private.hpp +125 -0
- package/core/native/physics2d/owned_abi.cpp +1354 -879
- package/core/native/physics2d/owned_abi.hpp +190 -52
- package/core/native/physics2d/phase_test_hooks.hpp +17 -0
- package/core/native/physics2d/physics2d.cpp +2073 -120
- package/core/native/physics2d/physics2d.hpp +154 -2
- package/core/native/physics2d/queries.cpp +1055 -79
- package/core/native/physics2d/queries.hpp +71 -5
- package/core/native/physics2d/world.cpp +1313 -353
- package/core/native/physics2d/world.hpp +124 -13
- package/core/native/sha256/sha256_compress_unsigned.hpp +68 -0
- package/core/native/sha256/sha256_fips_constants.hpp +24 -0
- package/core/native/sha256/sha256_length.hpp +56 -0
- package/core/native/sha256/sha256_memo.hpp +48 -0
- package/core/native/sha256/sha256_sink.hpp +234 -0
- package/core/physics2d-owned.d.ts +466 -126
- package/core/runtime.d.ts +192 -19
- package/core/scripts/build-native.mjs +34 -0
- package/core/scripts/build-wasm-physics.mjs +272 -23
- package/core/scripts/extended-physics-profile.mjs +634 -0
- package/core/scripts/generate-physics2d-vectors.mjs +59 -0
- package/core/src/kinetix-deterministic-math.d.ts +2 -0
- package/core/src/kinetix-deterministic-math.mjs +513 -27
- package/core/src/kinetix-owned-runtime-adapter.mjs +788 -0
- package/core/src/kinetix-runtime-adapter-utils.mjs +54 -6
- package/core/src/kinetix-runtime-contract.mjs +55 -9
- package/core/src/kinetix-runtime-identity.mjs +12 -1
- package/core/src/kinetix-session-config.mjs +7 -2
- package/core/src/kinetix-state-report.mjs +36 -0
- package/core/src/physics/abi-constants.mjs +5 -4
- package/core/src/physics/articulation.d.mts +194 -0
- package/core/src/physics/articulation.mjs +1141 -0
- package/core/src/physics/buoyancy.d.mts +143 -0
- package/core/src/physics/buoyancy.mjs +268 -0
- package/core/src/physics/cloth.d.mts +314 -0
- package/core/src/physics/cloth.mjs +1156 -0
- package/core/src/physics/deformable-query.d.mts +217 -0
- package/core/src/physics/deformable-query.mjs +1412 -0
- package/core/src/physics/distance.d.mts +160 -0
- package/core/src/physics/distance.mjs +175 -0
- package/core/src/physics/extended-wasm-instance.d.mts +5 -0
- package/core/src/physics/extended-wasm-instance.mjs +48 -0
- package/core/src/physics/feature-api.d.mts +74 -0
- package/core/src/physics/feature-api.mjs +98 -0
- package/core/src/physics/fracture.d.mts +133 -0
- package/core/src/physics/fracture.mjs +1324 -0
- package/core/src/physics/gear.d.mts +585 -0
- package/core/src/physics/gear.mjs +955 -0
- package/core/src/physics/lag-compensation.d.mts +101 -0
- package/core/src/physics/lag-compensation.mjs +1100 -0
- package/core/src/physics/mass-properties.d.mts +114 -0
- package/core/src/physics/mass-properties.mjs +137 -468
- package/core/src/physics/particles.d.mts +177 -0
- package/core/src/physics/particles.mjs +758 -0
- package/core/src/physics/path-joint.d.mts +171 -0
- package/core/src/physics/path-joint.mjs +1231 -0
- package/core/src/physics/physics-extended-wasm-module.generated.mjs +13 -0
- package/core/src/physics/physics-profile-wasm-module.generated.mjs +48 -0
- package/core/src/physics/physics-wasm-module.generated.mjs +34 -5
- package/core/src/physics/powered-ragdoll.d.mts +188 -0
- package/core/src/physics/powered-ragdoll.mjs +1043 -0
- package/core/src/physics/powertrain.d.mts +260 -0
- package/core/src/physics/powertrain.mjs +621 -0
- package/core/src/physics/pulley.d.mts +98 -0
- package/core/src/physics/pulley.mjs +686 -0
- package/core/src/physics/ragdoll.d.mts +289 -0
- package/core/src/physics/ragdoll.mjs +1323 -0
- package/core/src/physics/rope.d.mts +241 -0
- package/core/src/physics/rope.mjs +319 -0
- package/core/src/physics/rounded-convex.d.mts +328 -0
- package/core/src/physics/rounded-convex.mjs +1404 -0
- package/core/src/physics/sdf-geometry.d.mts +117 -0
- package/core/src/physics/sdf-geometry.mjs +1204 -0
- package/core/src/physics/soft-body.d.mts +144 -0
- package/core/src/physics/soft-body.mjs +1285 -0
- package/core/src/physics/soft-collision.d.mts +651 -0
- package/core/src/physics/soft-collision.mjs +1883 -0
- package/core/src/physics/surface-velocity.d.mts +156 -0
- package/core/src/physics/surface-velocity.mjs +722 -0
- package/core/src/physics/tracks.d.mts +411 -0
- package/core/src/physics/tracks.mjs +1074 -0
- package/core/src/physics/vehicle.d.mts +3 -0
- package/core/src/physics/vehicle.mjs +172 -0
- package/core/src/physics/voxel-cooking.d.mts +219 -0
- package/core/src/physics/voxel-cooking.mjs +1499 -0
- package/core/src/physics/voxel-swept-quad.mjs +110 -0
- package/core/src/physics/wasm-instance.mjs +160 -23
- package/core/src/physics/world.d.mts +62 -0
- package/core/src/physics/world.mjs +1947 -152
- package/core/src/physics2d/body-state.mjs +218 -15
- package/core/src/physics2d/collision.d.mts +52 -0
- package/core/src/physics2d/collision.mjs +234 -0
- package/core/src/physics2d/compound.mjs +454 -0
- package/core/src/physics2d/contour-state.mjs +771 -0
- package/core/src/physics2d/feature-api.d.mts +211 -0
- package/core/src/physics2d/feature-api.mjs +3 -0
- package/core/src/physics2d/fluid-state.mjs +145 -0
- package/core/src/physics2d/joint-state.mjs +1 -1
- package/core/src/physics2d/owned-queries.mjs +3 -1
- package/core/src/physics2d/owned-transport.mjs +268 -122
- package/core/src/physics2d/owned-world.mjs +353 -230
- package/core/src/physics2d/particle-fluid.d.mts +152 -0
- package/core/src/physics2d/particle-fluid.mjs +233 -0
- package/core/src/physics2d/physics2d-wasm-module.generated.mjs +27 -10
- package/core/src/physics2d/query-codec.mjs +61 -13
- package/core/src/physics2d/submerged-geometry.d.mts +51 -0
- package/core/src/physics2d/submerged-geometry.mjs +186 -0
- package/core/src/runtime.mjs +23 -0
- package/core/src/shared/bytes.d.mts +2 -0
- package/core/src/shared/bytes.mjs +33 -0
- package/core/src/shared/sha256.d.mts +3 -0
- package/core/src/shared/sha256.mjs +72 -22
- package/core/units.d.ts +50 -0
- package/core/world.d.ts +253 -5
- package/dist/authority-room.d.ts +9 -0
- package/dist/authority-room.js +45 -16
- package/dist/browser.d.ts +6 -5
- package/dist/browser.js +4 -3
- package/dist/cadence.d.ts +7 -0
- package/dist/cadence.js +19 -0
- package/dist/canonical.d.ts +1 -0
- package/dist/canonical.js +3 -0
- package/dist/checksum-policy.d.ts +10 -0
- package/dist/checksum-policy.js +14 -0
- package/dist/collider-cooking-hull.d.ts +18 -0
- package/dist/collider-cooking-hull.js +248 -0
- package/dist/collider-cooking.d.ts +140 -0
- package/dist/collider-cooking.js +794 -0
- package/dist/compat/v5.d.ts +121 -0
- package/dist/compat/v5.js +479 -0
- package/dist/creator.d.ts +7 -6
- package/dist/creator.js +4 -3
- package/dist/game-byte-section.d.ts +36 -0
- package/dist/game-byte-section.js +350 -0
- package/dist/index.d.ts +13 -6
- package/dist/index.js +9 -4
- package/dist/instant-replay.d.ts +27 -2
- package/dist/instant-replay.js +91 -16
- package/dist/lag-compensation3d.js +11 -10
- package/dist/match-log.d.ts +21 -4
- package/dist/match-log.js +92 -9
- package/dist/math.d.ts +2 -0
- package/dist/math.js +1 -0
- package/dist/modules/character-3d.d.ts +16 -1
- package/dist/modules/character-3d.js +42 -5
- package/dist/modules/combat.js +6 -5
- package/dist/movement3d-revision1.d.ts +2 -0
- package/dist/movement3d-revision1.js +973 -0
- package/dist/movement3d.d.ts +53 -3
- package/dist/movement3d.js +917 -247
- package/dist/networked-client.d.ts +15 -0
- package/dist/networked-client.js +378 -105
- package/dist/node.d.ts +3 -2
- package/dist/node.js +2 -1
- package/dist/physics/2d.d.ts +38 -4
- package/dist/physics/2d.js +57 -36
- package/dist/physics/3d.d.ts +755 -111
- package/dist/physics/3d.js +2355 -158
- package/dist/physics/edit-result-graph.d.ts +36 -0
- package/dist/physics/edit-result-graph.js +141 -0
- package/dist/physics/game-codec3d.d.ts +41 -0
- package/dist/physics/game-codec3d.js +544 -0
- package/dist/physics/installed-checkpoint3d.d.ts +13 -0
- package/dist/physics/installed-checkpoint3d.js +120 -0
- package/dist/physics/installed-runtime3d.d.ts +51 -0
- package/dist/physics/installed-runtime3d.js +372 -0
- package/dist/physics/installed-state-commitment3d.d.ts +2 -0
- package/dist/physics/installed-state-commitment3d.js +24 -0
- package/dist/physics/units.d.ts +38 -0
- package/dist/physics/units.js +56 -0
- package/dist/physics/vehicle-state3d.d.ts +93 -0
- package/dist/physics/vehicle-state3d.js +1191 -0
- package/dist/physics/voxel-body-state.d.ts +61 -0
- package/dist/physics/voxel-body-state.js +410 -0
- package/dist/r3f-render-adapter.d.ts +59 -1
- package/dist/r3f-render-adapter.js +137 -2
- package/dist/replay.d.ts +13 -0
- package/dist/replay.js +95 -70
- package/dist/run-room-transport.js +5 -1
- package/dist/runner.d.ts +17 -2
- package/dist/runner.js +472 -84
- package/dist/runtime-cleanup.d.ts +54 -0
- package/dist/runtime-cleanup.js +266 -0
- package/dist/runtime-lease.d.ts +11 -1
- package/dist/runtime-lease.js +23 -10
- package/dist/runtime-room-metadata.d.ts +2 -8
- package/dist/runtime-room-metadata.js +11 -14
- package/dist/runtime-session.d.ts +8 -0
- package/dist/runtime-session.js +222 -58
- package/dist/schema-codegen.js +3 -0
- package/dist/schema-dsl-binary.d.ts +10 -4
- package/dist/schema-dsl-binary.js +375 -76
- package/dist/scratch-pool.d.ts +32 -0
- package/dist/scratch-pool.js +60 -0
- package/dist/sdk-session.d.ts +1 -0
- package/dist/sdk-session.js +4 -0
- package/dist/secret-config.js +2 -60
- package/dist/secret-crypto.js +70 -36
- package/dist/session-snapshot.d.ts +15 -4
- package/dist/session-snapshot.js +35 -33
- package/dist/session-wire.js +14 -22
- package/dist/sparse-set.d.ts +1 -0
- package/dist/sparse-set.js +22 -0
- package/dist/spatial/spatial-grid-3d.d.ts +41 -0
- package/dist/spatial/spatial-grid-3d.js +115 -0
- package/dist/synctest.d.ts +9 -0
- package/dist/synctest.js +106 -17
- package/dist/testing-network.d.ts +2 -0
- package/dist/testing-network.js +122 -18
- package/dist/testing.d.ts +27 -6
- package/dist/testing.js +297 -56
- package/dist/tools/index.d.ts +3 -2
- package/dist/tools/index.js +3 -2
- package/dist/tools/module-certification.d.ts +6 -0
- package/dist/tools/module-certification.js +26 -2
- package/package.json +18 -5
- package/core/scripts/run-box3d-benchmark.mjs +0 -350
- package/core/src/physics/index.mjs +0 -3169
|
@@ -0,0 +1,547 @@
|
|
|
1
|
+
#include "geometry_store.hpp"
|
|
2
|
+
#include "checkpoint_schema.hpp"
|
|
3
|
+
#include "sha256/sha256_memo.hpp"
|
|
4
|
+
#include "sha256/sha256_sink.hpp"
|
|
5
|
+
#include <algorithm>
|
|
6
|
+
#include <memory>
|
|
7
|
+
#include <bit>
|
|
8
|
+
#include <cmath>
|
|
9
|
+
#include <limits>
|
|
10
|
+
#include <stdexcept>
|
|
11
|
+
|
|
12
|
+
namespace rundot::kinetix::physics::geometry_private {
|
|
13
|
+
namespace {
|
|
14
|
+
bool nonNegativeInteger(double value, std::uint64_t maximum) {
|
|
15
|
+
return std::isfinite(value) && value >= 0 && value <= static_cast<double>(maximum)
|
|
16
|
+
&& std::floor(value) == value;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
std::uint64_t fingerprint(const GeometryDescriptor& shape, std::span<const double> values) {
|
|
20
|
+
#if defined(RUNDOT_PHYSICS_PHASE_TEST_HOOKS)
|
|
21
|
+
if (geometryScanHook != nullptr) geometryScanHook();
|
|
22
|
+
#endif
|
|
23
|
+
std::uint64_t hash = 14695981039346656037ull;
|
|
24
|
+
auto append = [&](std::uint64_t value) {
|
|
25
|
+
for (unsigned shift = 0; shift < 64; shift += 8) {
|
|
26
|
+
hash = (hash ^ static_cast<std::uint8_t>(value >> shift)) * 1099511628211ull;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
append(static_cast<std::uint32_t>(shape.kind));
|
|
30
|
+
append(shape.count); append(shape.format); append(shape.featureOffset);
|
|
31
|
+
for (double value : values) append(std::bit_cast<std::uint64_t>(value));
|
|
32
|
+
#if defined(RUNDOT_PHYSICS_PHASE_TEST_HOOKS)
|
|
33
|
+
if (syntheticFingerprintArmed) return syntheticFingerprintValue;
|
|
34
|
+
#endif
|
|
35
|
+
return hash;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
std::array<std::uint8_t, 32> digestGeometryRecord(
|
|
39
|
+
const GeometryDescriptor& shape, std::span<const double> values) {
|
|
40
|
+
rundot::sha256::Sha256Sink sink;
|
|
41
|
+
rundot::sha256::LeFieldWriter<rundot::sha256::Sha256Sink> writer(sink);
|
|
42
|
+
if (writer.domainLiteral(kStrongRecordDomain) != rundot::sha256::SinkStatus::Ok
|
|
43
|
+
|| writer.u32(static_cast<std::uint32_t>(shape.kind)) != rundot::sha256::SinkStatus::Ok
|
|
44
|
+
|| writer.u32(shape.count) != rundot::sha256::SinkStatus::Ok
|
|
45
|
+
|| writer.u32(shape.format) != rundot::sha256::SinkStatus::Ok
|
|
46
|
+
|| writer.u32(shape.featureOffset) != rundot::sha256::SinkStatus::Ok
|
|
47
|
+
|| writer.u64(values.size()) != rundot::sha256::SinkStatus::Ok) {
|
|
48
|
+
throw std::logic_error("geometry record digest failed");
|
|
49
|
+
}
|
|
50
|
+
if (!values.empty()) {
|
|
51
|
+
if (values.size() > std::numeric_limits<std::size_t>::max() / sizeof(double)
|
|
52
|
+
|| writer.bytes(std::span<const std::uint8_t>(
|
|
53
|
+
reinterpret_cast<const std::uint8_t*>(values.data()), values.size() * sizeof(double)))
|
|
54
|
+
!= rundot::sha256::SinkStatus::Ok) {
|
|
55
|
+
throw std::logic_error("geometry record digest failed");
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const auto result = sink.final();
|
|
59
|
+
if (result.status != rundot::sha256::SinkStatus::Ok) {
|
|
60
|
+
throw std::logic_error("geometry record digest failed");
|
|
61
|
+
}
|
|
62
|
+
#if defined(RUNDOT_PHYSICS_PHASE_TEST_HOOKS)
|
|
63
|
+
rundot::sha256::gStrongRecordLeafFills.fetch_add(1, std::memory_order_relaxed);
|
|
64
|
+
#endif
|
|
65
|
+
return result.bytes;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
template <class T, class EntryVector, class... Args>
|
|
69
|
+
std::shared_ptr<T> makeLedgerObject(
|
|
70
|
+
const std::shared_ptr<GeometryCacheAnchor>& cache,
|
|
71
|
+
EntryVector& entries,
|
|
72
|
+
const std::shared_ptr<AllocationLedger>& ledger,
|
|
73
|
+
bool publish,
|
|
74
|
+
Args&&... args) {
|
|
75
|
+
LedgerAllocator<T> alloc{ledger};
|
|
76
|
+
T* raw = alloc.allocate(1);
|
|
77
|
+
try {
|
|
78
|
+
std::construct_at(raw, std::forward<Args>(args)...);
|
|
79
|
+
} catch (...) {
|
|
80
|
+
alloc.deallocate(raw, 1);
|
|
81
|
+
throw;
|
|
82
|
+
}
|
|
83
|
+
const std::weak_ptr<GeometryCacheAnchor> anchor = cache;
|
|
84
|
+
std::shared_ptr<T> owner;
|
|
85
|
+
owner.reset(raw, [anchor, slot = &entries, ledger](T* pointer) {
|
|
86
|
+
if (anchor.lock()) std::erase(*slot, static_cast<const T*>(pointer));
|
|
87
|
+
std::destroy_at(pointer);
|
|
88
|
+
LedgerAllocator<T>{ledger}.deallocate(pointer, 1);
|
|
89
|
+
}, MetadataAllocator<T>{});
|
|
90
|
+
if (publish) entries.push_back(raw);
|
|
91
|
+
return owner;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
bool sameValues(std::span<const double> first, std::span<const double> second) {
|
|
95
|
+
return first.size() == second.size() && std::equal(first.begin(), first.end(), second.begin(),
|
|
96
|
+
[](double a, double b) { return std::bit_cast<std::uint64_t>(a) == std::bit_cast<std::uint64_t>(b); });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
namespace {
|
|
101
|
+
bool validPrimitive(ShapeKind kind, const double* shape) {
|
|
102
|
+
switch (kind) {
|
|
103
|
+
case ShapeKind::Box: return shape[0] > 0.0 && shape[1] > 0.0 && shape[2] > 0.0;
|
|
104
|
+
case ShapeKind::Sphere: return shape[0] > 0.0;
|
|
105
|
+
case ShapeKind::Capsule: return shape[0] > 0.0 && shape[1] > 0.0;
|
|
106
|
+
case ShapeKind::Cylinder: return shape[0] > 0.0 && shape[1] > 0.0 && shape[2] >= 0.0;
|
|
107
|
+
case ShapeKind::Cone: return shape[0] > 0.0 && shape[1] > 0.0 && shape[2] >= 0.0;
|
|
108
|
+
case ShapeKind::TaperedCapsule: return shape[0] > 0.0 && shape[1] > 0.0 && shape[2] > 0.0;
|
|
109
|
+
case ShapeKind::RoundedConvex: return shape[0] > 0.0 && shape[1] > 0.0 && shape[2] >= 0.0;
|
|
110
|
+
case ShapeKind::MultiSphere: return shape[0] > 0.0;
|
|
111
|
+
default: return false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
bool validBounds(const double* minimum, const double* maximum, const double* translation = nullptr) {
|
|
116
|
+
for (std::size_t axis = 0; axis < 3; ++axis) {
|
|
117
|
+
if (minimum[axis] > maximum[axis]) return false;
|
|
118
|
+
if (translation != nullptr
|
|
119
|
+
&& (!std::isfinite(translation[axis] + minimum[axis]) || !std::isfinite(translation[axis] + maximum[axis]))) return false;
|
|
120
|
+
}
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
bool validGeometry(std::span<const double> geometry, ShapeKind kind,
|
|
127
|
+
std::uint64_t offset, std::uint64_t count, std::uint32_t format, std::uint32_t depth) {
|
|
128
|
+
if (count == 0 || offset > geometry.size() / 3 || count > geometry.size() / 3 - offset) return false;
|
|
129
|
+
const double* values = geometry.data() + offset * 3;
|
|
130
|
+
switch (kind) {
|
|
131
|
+
case ShapeKind::Hull: return format == 0 && count >= 4;
|
|
132
|
+
case ShapeKind::Mesh: return format == 2 && count >= 3 && count % 3 == 0;
|
|
133
|
+
case ShapeKind::Heightfield: {
|
|
134
|
+
if (format != 3 || count < 5 || !nonNegativeInteger(values[0], count)
|
|
135
|
+
|| !nonNegativeInteger(values[1], count) || values[0] < 2 || values[1] < 2) return false;
|
|
136
|
+
const auto columns = static_cast<std::uint64_t>(values[0]);
|
|
137
|
+
const auto rows = static_cast<std::uint64_t>(values[1]);
|
|
138
|
+
return columns * rows == count - 1 && values[2] == 0;
|
|
139
|
+
}
|
|
140
|
+
case ShapeKind::Compound: break;
|
|
141
|
+
default: return false;
|
|
142
|
+
}
|
|
143
|
+
if (depth >= kGeometryMaxCompoundDepth) return false;
|
|
144
|
+
if (format == 1) {
|
|
145
|
+
if (count % 2 != 0) return false;
|
|
146
|
+
for (std::uint64_t child = 0; child < count / 2; ++child) {
|
|
147
|
+
if (!validBounds(values + child * 6, values + child * 6 + 3)) return false;
|
|
148
|
+
}
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
if (format != 4 && format != 5 && format != 6) return false;
|
|
152
|
+
const std::uint64_t stride = format == 6 ? 8 : (format == 4 ? 5 : 6);
|
|
153
|
+
if (count < stride || (format == 4 && count % stride != 0)) return false;
|
|
154
|
+
if ((format == 5 || format == 6) && (!nonNegativeInteger(values[format == 6 ? 21 : 17], count / stride) || values[format == 6 ? 21 : 17] < 1)) return false;
|
|
155
|
+
const std::uint64_t children = format == 4 ? count / stride : static_cast<std::uint64_t>(values[format == 6 ? 21 : 17]);
|
|
156
|
+
std::uint64_t payloadEnd = children * stride;
|
|
157
|
+
for (std::uint64_t child = 0; child < children; ++child) {
|
|
158
|
+
const double* record = values + child * stride * 3;
|
|
159
|
+
if (!nonNegativeInteger(record[0], static_cast<std::uint32_t>(ShapeKind::MultiSphere))
|
|
160
|
+
|| !validBounds(record + 7, record + 10, record + 1)) return false;
|
|
161
|
+
const auto childKind = static_cast<ShapeKind>(static_cast<std::uint32_t>(record[0]));
|
|
162
|
+
const bool primitive = childKind == ShapeKind::Box
|
|
163
|
+
|| childKind == ShapeKind::Sphere
|
|
164
|
+
|| childKind == ShapeKind::Capsule
|
|
165
|
+
|| childKind == ShapeKind::Cylinder
|
|
166
|
+
|| childKind == ShapeKind::Cone
|
|
167
|
+
|| childKind == ShapeKind::TaperedCapsule
|
|
168
|
+
|| childKind == ShapeKind::RoundedConvex
|
|
169
|
+
|| childKind == ShapeKind::MultiSphere;
|
|
170
|
+
if (primitive && !validPrimitive(childKind, record + 4)) return false;
|
|
171
|
+
if (format == 4) continue;
|
|
172
|
+
if (format == 6) {
|
|
173
|
+
if (!nonNegativeInteger(record[17], count) || !nonNegativeInteger(record[18], count)
|
|
174
|
+
|| !nonNegativeInteger(record[19], 6) || !nonNegativeInteger(record[20], 1)
|
|
175
|
+
|| record[20] != values[20] || record[21] != children) return false;
|
|
176
|
+
const auto childOffset = static_cast<std::uint64_t>(record[17]);
|
|
177
|
+
const auto childCount = static_cast<std::uint64_t>(record[18]);
|
|
178
|
+
if (primitive) {
|
|
179
|
+
if (childOffset != 0 || childCount != 0 || record[19] != 0) return false;
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
if (childOffset < payloadEnd || childCount > count - childOffset) return false;
|
|
183
|
+
if (!validGeometry(geometry, childKind, offset + childOffset, childCount,
|
|
184
|
+
static_cast<std::uint32_t>(record[19]), depth + 1)) return false;
|
|
185
|
+
payloadEnd = childOffset + childCount;
|
|
186
|
+
} else {
|
|
187
|
+
if (!nonNegativeInteger(record[13], count) || !nonNegativeInteger(record[14], count)
|
|
188
|
+
|| !nonNegativeInteger(record[15], 5) || !nonNegativeInteger(record[16], 1)
|
|
189
|
+
|| record[16] != values[16] || record[17] != children) return false;
|
|
190
|
+
const auto childOffset = static_cast<std::uint64_t>(record[13]);
|
|
191
|
+
const auto childCount = static_cast<std::uint64_t>(record[14]);
|
|
192
|
+
if (primitive) {
|
|
193
|
+
if (childOffset != 0 || childCount != 0 || record[15] != 0) return false;
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (childOffset < payloadEnd || childCount > count - childOffset) return false;
|
|
197
|
+
if (!validGeometry(geometry, childKind, offset + childOffset, childCount,
|
|
198
|
+
static_cast<std::uint32_t>(record[15]), depth + 1)) return false;
|
|
199
|
+
payloadEnd = childOffset + childCount;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if ((format == 5 || format == 6) && values[format == 6 ? 20 : 16] == 1) {
|
|
203
|
+
const std::uint64_t recordStride = format == 6 ? 24 : 18;
|
|
204
|
+
for (std::uint64_t child = 0; child < children; ++child) {
|
|
205
|
+
const double* first = values + child * recordStride;
|
|
206
|
+
if (first[0] != static_cast<double>(ShapeKind::Box)
|
|
207
|
+
|| first[13] != 0 || first[14] != 0 || first[15] != 0) return false;
|
|
208
|
+
for (std::uint64_t other = 0; other < child; ++other) {
|
|
209
|
+
const double* second = values + other * recordStride;
|
|
210
|
+
constexpr double kEps = 1e-9;
|
|
211
|
+
const bool overlap = first[1] - first[4] < second[1] + second[4] - kEps
|
|
212
|
+
&& second[1] - second[4] < first[1] + first[4] - kEps
|
|
213
|
+
&& first[2] - first[5] < second[2] + second[5] - kEps
|
|
214
|
+
&& second[2] - second[5] < first[2] + first[5] - kEps
|
|
215
|
+
&& first[3] - first[6] < second[3] + second[6] - kEps
|
|
216
|
+
&& second[3] - second[6] < first[3] + first[6] - kEps;
|
|
217
|
+
if (overlap) return false;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return true;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
GeometryStore::GeometryStore(GeometryStoreLimits limits)
|
|
225
|
+
: limits_(limits),
|
|
226
|
+
ledger_(std::allocate_shared<AllocationLedger>(
|
|
227
|
+
MetadataAllocator<AllocationLedger>{},
|
|
228
|
+
limits.maxLiveBytes,
|
|
229
|
+
&InstanceMemoryLedger::instance(),
|
|
230
|
+
LedgerDomain::Geometry)),
|
|
231
|
+
cache_(std::make_shared<GeometryCacheAnchor>()) {
|
|
232
|
+
if (limits.maxActiveValues > kGeometryMaxValues || limits.maxBindings > kContactTelemetryCapacity) {
|
|
233
|
+
throw std::invalid_argument("native geometry store limits exceed the supported domain");
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
std::uint64_t GeometryStore::anchorCapacityBytes() const noexcept {
|
|
238
|
+
if (!cache_) return 0;
|
|
239
|
+
const auto charge = [](std::size_t capacity) noexcept {
|
|
240
|
+
constexpr std::uint64_t element = sizeof(void*);
|
|
241
|
+
if (static_cast<std::uint64_t>(capacity) > std::numeric_limits<std::uint64_t>::max() / element) {
|
|
242
|
+
return std::numeric_limits<std::uint64_t>::max();
|
|
243
|
+
}
|
|
244
|
+
return static_cast<std::uint64_t>(capacity) * element;
|
|
245
|
+
};
|
|
246
|
+
const auto records = charge(cache_->records.capacity());
|
|
247
|
+
const auto roots = charge(cache_->roots.capacity());
|
|
248
|
+
if (records > std::numeric_limits<std::uint64_t>::max() - roots) {
|
|
249
|
+
return std::numeric_limits<std::uint64_t>::max();
|
|
250
|
+
}
|
|
251
|
+
return records + roots;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
std::uint64_t GeometryStore::nextAnchorGrowthBytes(
|
|
255
|
+
std::uint64_t newRecords, std::uint64_t newRoots) const noexcept {
|
|
256
|
+
if (!cache_) return std::numeric_limits<std::uint64_t>::max();
|
|
257
|
+
const auto grow = [](std::size_t size, std::size_t capacity, std::uint64_t adds) noexcept {
|
|
258
|
+
if (adds == 0) return std::uint64_t{0};
|
|
259
|
+
if (adds > static_cast<std::uint64_t>(std::numeric_limits<std::size_t>::max() - size)) {
|
|
260
|
+
return std::numeric_limits<std::uint64_t>::max();
|
|
261
|
+
}
|
|
262
|
+
const auto need = size + static_cast<std::size_t>(adds);
|
|
263
|
+
if (need <= capacity) return std::uint64_t{0};
|
|
264
|
+
if (capacity > std::numeric_limits<std::size_t>::max() / 2) {
|
|
265
|
+
return std::numeric_limits<std::uint64_t>::max();
|
|
266
|
+
}
|
|
267
|
+
const auto doubled = capacity * 2;
|
|
268
|
+
const auto next = doubled > need ? doubled : need;
|
|
269
|
+
constexpr std::uint64_t element = sizeof(void*);
|
|
270
|
+
if (static_cast<std::uint64_t>(next) > std::numeric_limits<std::uint64_t>::max() / element) {
|
|
271
|
+
return std::numeric_limits<std::uint64_t>::max();
|
|
272
|
+
}
|
|
273
|
+
return static_cast<std::uint64_t>(next) * element;
|
|
274
|
+
};
|
|
275
|
+
const auto recordBytes = grow(cache_->records.size(), cache_->records.capacity(), newRecords);
|
|
276
|
+
const auto rootBytes = grow(cache_->roots.size(), cache_->roots.capacity(), newRoots);
|
|
277
|
+
if (recordBytes == std::numeric_limits<std::uint64_t>::max()
|
|
278
|
+
|| rootBytes == std::numeric_limits<std::uint64_t>::max()
|
|
279
|
+
|| recordBytes > std::numeric_limits<std::uint64_t>::max() - rootBytes) {
|
|
280
|
+
return std::numeric_limits<std::uint64_t>::max();
|
|
281
|
+
}
|
|
282
|
+
return recordBytes + rootBytes;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
GeometryRecord::GeometryRecord(ConstructionKey, std::shared_ptr<AllocationLedger> ledger,
|
|
286
|
+
std::uint64_t recordId, GeometryDescriptor shape, std::uint64_t hash, std::span<const double> values)
|
|
287
|
+
: values_(values.begin(), values.end(), LedgerAllocator<double>{ledger}),
|
|
288
|
+
triangles_(LedgerAllocator<GeometryTriangle>{ledger}),
|
|
289
|
+
triangleNodes_(LedgerAllocator<GeometryTriangleNode>{ledger}),
|
|
290
|
+
triangleSegments_(LedgerAllocator<GeometryTriangleSegment>{std::move(ledger)}),
|
|
291
|
+
id(recordId), descriptor(shape), fingerprint(hash),
|
|
292
|
+
strongLeaf_(digestGeometryRecord(shape, values)) {
|
|
293
|
+
buildTriangleIndex();
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const GeometryTriangleSegment* GeometryRecord::triangleSegment(ShapeKind kind,
|
|
297
|
+
std::uint32_t offset, std::uint32_t count, std::uint32_t format) const noexcept {
|
|
298
|
+
const auto found = std::lower_bound(triangleSegments_.begin(), triangleSegments_.end(), offset,
|
|
299
|
+
[](const GeometryTriangleSegment& segment, std::uint32_t value) { return segment.offset < value; });
|
|
300
|
+
return found != triangleSegments_.end() && found->offset == offset && found->kind == kind
|
|
301
|
+
&& found->count == count && found->format == format ? &*found : nullptr;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
std::uint32_t GeometryRecord::buildTriangleNode(std::uint32_t offset,
|
|
305
|
+
std::uint32_t first, std::uint32_t count) {
|
|
306
|
+
GeometryTriangleNode node{};
|
|
307
|
+
node.minimum.fill(std::numeric_limits<double>::infinity());
|
|
308
|
+
node.maximum.fill(-std::numeric_limits<double>::infinity());
|
|
309
|
+
for (std::uint32_t index = first; index < first + count; ++index) {
|
|
310
|
+
for (const auto point : triangles_[index].points) {
|
|
311
|
+
for (unsigned axis = 0; axis < 3; ++axis) {
|
|
312
|
+
const auto value = values_[(std::size_t(offset) + point) * 3 + axis];
|
|
313
|
+
node.minimum[axis] = std::min(node.minimum[axis], value);
|
|
314
|
+
node.maximum[axis] = std::max(node.maximum[axis], value);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
node.first = first; node.count = count;
|
|
319
|
+
const auto result = static_cast<std::uint32_t>(triangleNodes_.size());
|
|
320
|
+
triangleNodes_.push_back(node);
|
|
321
|
+
if (count <= 16) return result;
|
|
322
|
+
unsigned axis = 0;
|
|
323
|
+
for (unsigned candidate = 1; candidate < 3; ++candidate) {
|
|
324
|
+
if (node.maximum[candidate] - node.minimum[candidate] > node.maximum[axis] - node.minimum[axis]) axis = candidate;
|
|
325
|
+
}
|
|
326
|
+
const auto middle = first + count / 2;
|
|
327
|
+
const auto centre = [&](const GeometryTriangle& triangle) {
|
|
328
|
+
double minimum = std::numeric_limits<double>::infinity(), maximum = -minimum;
|
|
329
|
+
for (const auto point : triangle.points) {
|
|
330
|
+
const auto value = values_[(std::size_t(offset) + point) * 3 + axis];
|
|
331
|
+
minimum = std::min(minimum, value); maximum = std::max(maximum, value);
|
|
332
|
+
}
|
|
333
|
+
return minimum * 0.5 + maximum * 0.5;
|
|
334
|
+
};
|
|
335
|
+
std::nth_element(triangles_.begin() + first, triangles_.begin() + middle,
|
|
336
|
+
triangles_.begin() + first + count, [&](const GeometryTriangle& a, const GeometryTriangle& b) {
|
|
337
|
+
const auto ac = centre(a), bc = centre(b);
|
|
338
|
+
return ac < bc || (ac == bc && a.ordinal < b.ordinal);
|
|
339
|
+
});
|
|
340
|
+
const auto left = buildTriangleNode(offset, first, middle - first);
|
|
341
|
+
const auto right = buildTriangleNode(offset, middle, first + count - middle);
|
|
342
|
+
triangleNodes_[result].count = 0;
|
|
343
|
+
triangleNodes_[result].left = left; triangleNodes_[result].right = right;
|
|
344
|
+
return result;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
void GeometryRecord::buildTriangleIndex() {
|
|
348
|
+
const auto visitSegments = [&](auto&& self, ShapeKind kind, std::uint32_t offset,
|
|
349
|
+
std::uint32_t count, std::uint32_t format, auto&& emit) -> void {
|
|
350
|
+
if (kind == ShapeKind::Mesh || kind == ShapeKind::Heightfield) {
|
|
351
|
+
emit(kind, offset, count, format);
|
|
352
|
+
} else if (kind == ShapeKind::Compound && (format == 5 || format == 6)) {
|
|
353
|
+
const auto cStride = format == 6 ? 24 : 18;
|
|
354
|
+
const auto children = static_cast<std::uint32_t>(values_[std::size_t(offset) * 3 + (format == 6 ? 21 : 17)]);
|
|
355
|
+
for (std::uint32_t child = 0; child < children; ++child) {
|
|
356
|
+
const auto at = std::size_t(offset) * 3 + std::size_t(child) * cStride;
|
|
357
|
+
self(self, static_cast<ShapeKind>(static_cast<std::uint32_t>(values_[at])),
|
|
358
|
+
offset + static_cast<std::uint32_t>(values_[at + (format == 6 ? 17 : 13)]),
|
|
359
|
+
static_cast<std::uint32_t>(values_[at + (format == 6 ? 18 : 14)]),
|
|
360
|
+
static_cast<std::uint32_t>(values_[at + (format == 6 ? 19 : 15)]), emit);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
const auto triangleCount = [&](ShapeKind kind, std::uint32_t offset, std::uint32_t count) {
|
|
365
|
+
if (kind == ShapeKind::Mesh) return count / 3;
|
|
366
|
+
const auto columns = static_cast<std::uint32_t>(values_[std::size_t(offset) * 3]);
|
|
367
|
+
const auto rows = static_cast<std::uint32_t>(values_[std::size_t(offset) * 3 + 1]);
|
|
368
|
+
return 2 * (columns - 1) * (rows - 1);
|
|
369
|
+
};
|
|
370
|
+
const auto nodeCount = [](auto&& self, std::uint32_t count) -> std::uint32_t {
|
|
371
|
+
return count <= 16 ? 1 : 1 + self(self, count / 2) + self(self, count - count / 2);
|
|
372
|
+
};
|
|
373
|
+
std::size_t triangles = 0, nodes = 0, segments = 0;
|
|
374
|
+
visitSegments(visitSegments, descriptor.kind, 0, descriptor.count, descriptor.format,
|
|
375
|
+
[&](ShapeKind kind, std::uint32_t offset, std::uint32_t count, std::uint32_t) {
|
|
376
|
+
const auto size = triangleCount(kind, offset, count);
|
|
377
|
+
triangles += size; nodes += nodeCount(nodeCount, size); ++segments;
|
|
378
|
+
});
|
|
379
|
+
triangles_.reserve(triangles); triangleNodes_.reserve(nodes); triangleSegments_.reserve(segments);
|
|
380
|
+
visitSegments(visitSegments, descriptor.kind, 0, descriptor.count, descriptor.format,
|
|
381
|
+
[&](ShapeKind kind, std::uint32_t offset, std::uint32_t count, std::uint32_t format) {
|
|
382
|
+
const auto first = static_cast<std::uint32_t>(triangles_.size());
|
|
383
|
+
if (kind == ShapeKind::Mesh) {
|
|
384
|
+
for (std::uint32_t point = 0; point < count; point += 3) {
|
|
385
|
+
triangles_.push_back({{point, point + 1, point + 2}, point / 3});
|
|
386
|
+
}
|
|
387
|
+
} else {
|
|
388
|
+
const auto columns = static_cast<std::uint32_t>(values_[std::size_t(offset) * 3]);
|
|
389
|
+
const auto rows = static_cast<std::uint32_t>(values_[std::size_t(offset) * 3 + 1]);
|
|
390
|
+
for (std::uint32_t row = 0; row + 1 < rows; ++row) {
|
|
391
|
+
for (std::uint32_t column = 0; column + 1 < columns; ++column) {
|
|
392
|
+
const auto point = row * columns + column;
|
|
393
|
+
triangles_.push_back({{point + 1, point + columns + 1, point + 2}, 2 * point});
|
|
394
|
+
triangles_.push_back({{point + columns + 2, point + 2, point + columns + 1}, 2 * point + 1});
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
const auto root = buildTriangleNode(offset, first, triangleCount(kind, offset, count));
|
|
399
|
+
triangleSegments_.push_back({kind, offset, count, format, root});
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
GeometryStoreResult GeometryStore::stage(const GeometryRootOwner& source,
|
|
404
|
+
std::span<const GeometryEdit> edits) const {
|
|
405
|
+
if (source && source->ledger_ != ledger_) return GeometryStoreFailure::WrongStore;
|
|
406
|
+
if (edits.size() > kWorldMaxTopologyCommands || (!edits.empty() && edits.data() == nullptr)) {
|
|
407
|
+
return GeometryStoreFailure::InvalidInput;
|
|
408
|
+
}
|
|
409
|
+
if (edits.empty() && source) return source;
|
|
410
|
+
try {
|
|
411
|
+
auto staged = makeLedgerObject<GeometryRoot>(cache_, cache_->roots, ledger_, false,
|
|
412
|
+
GeometryRoot::ConstructionKey{}, ledger_, source.get());
|
|
413
|
+
for (const auto& edit : edits) {
|
|
414
|
+
if (edit.body.value == 0 || (!edit.values.empty() && edit.values.data() == nullptr)) {
|
|
415
|
+
return GeometryStoreFailure::InvalidInput;
|
|
416
|
+
}
|
|
417
|
+
auto binding = std::lower_bound(staged->bindings_.begin(), staged->bindings_.end(), edit.body,
|
|
418
|
+
[](const GeometryBinding& item, BodyId body) { return item.body < body; });
|
|
419
|
+
if (!edit.descriptor) {
|
|
420
|
+
if (!edit.values.empty()) return GeometryStoreFailure::InvalidInput;
|
|
421
|
+
if (binding != staged->bindings_.end() && binding->body == edit.body) staged->bindings_.erase(binding);
|
|
422
|
+
continue;
|
|
423
|
+
}
|
|
424
|
+
const auto& shape = *edit.descriptor;
|
|
425
|
+
if (edit.values.size() > kGeometryMaxValues
|
|
426
|
+
|| std::uint64_t(shape.count) * 3 != edit.values.size()
|
|
427
|
+
|| std::uint64_t(shape.featureOffset) + shape.count > kGeometryMaxValues / 3
|
|
428
|
+
|| !std::all_of(edit.values.begin(), edit.values.end(), [](double value) { return std::isfinite(value); })
|
|
429
|
+
|| !validGeometry(edit.values, shape.kind, 0, shape.count, shape.format)) {
|
|
430
|
+
return GeometryStoreFailure::InvalidInput;
|
|
431
|
+
}
|
|
432
|
+
const auto hash = fingerprint(shape, edit.values);
|
|
433
|
+
GeometryRecordOwner record;
|
|
434
|
+
for (const auto& existing : staged->bindings_) {
|
|
435
|
+
if (existing.record->fingerprint == hash && existing.record->descriptor == shape
|
|
436
|
+
&& sameValues(existing.record->values(), edit.values)) {
|
|
437
|
+
record = existing.record;
|
|
438
|
+
break;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
if (!record) {
|
|
442
|
+
for (const GeometryRecord* existing : cache_->records) {
|
|
443
|
+
if (existing->fingerprint == hash && existing->descriptor == shape
|
|
444
|
+
&& sameValues(existing->values(), edit.values)) {
|
|
445
|
+
record = existing->shared_from_this();
|
|
446
|
+
break;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
if (!record) {
|
|
451
|
+
if (staged->nextRecordId_ == 0 || staged->nextRecordId_ == std::numeric_limits<std::uint64_t>::max()) {
|
|
452
|
+
return GeometryStoreFailure::IdOverflow;
|
|
453
|
+
}
|
|
454
|
+
record = makeLedgerObject<GeometryRecord>(cache_, cache_->records, ledger_, true,
|
|
455
|
+
GeometryRecord::ConstructionKey{}, ledger_, staged->nextRecordId_++, shape, hash, edit.values);
|
|
456
|
+
}
|
|
457
|
+
if (binding != staged->bindings_.end() && binding->body == edit.body) binding->record = std::move(record);
|
|
458
|
+
else staged->bindings_.insert(binding, GeometryBinding{edit.body, std::move(record)});
|
|
459
|
+
}
|
|
460
|
+
if (staged->bindings_.size() > limits_.maxBindings) return GeometryStoreFailure::Capacity;
|
|
461
|
+
std::vector<const GeometryRecord*, LedgerAllocator<const GeometryRecord*>> active{
|
|
462
|
+
LedgerAllocator<const GeometryRecord*>{ledger_}};
|
|
463
|
+
active.reserve(staged->bindings_.size());
|
|
464
|
+
for (const auto& binding : staged->bindings_) active.push_back(binding.record.get());
|
|
465
|
+
std::sort(active.begin(), active.end(), [](const GeometryRecord* a, const GeometryRecord* b) { return a->id < b->id; });
|
|
466
|
+
active.erase(std::unique(active.begin(), active.end()), active.end());
|
|
467
|
+
std::uint64_t count = 0;
|
|
468
|
+
for (const auto* record : active) {
|
|
469
|
+
if (record->values().size() > limits_.maxActiveValues - count) return GeometryStoreFailure::Capacity;
|
|
470
|
+
count += record->values().size();
|
|
471
|
+
}
|
|
472
|
+
staged->activeValues_ = count;
|
|
473
|
+
const auto sameRoot = [&](const GeometryRoot& existing) {
|
|
474
|
+
if (existing.bindings_.size() != staged->bindings_.size()) return false;
|
|
475
|
+
for (std::size_t index = 0; index < existing.bindings_.size(); ++index) {
|
|
476
|
+
const auto& left = existing.bindings_[index];
|
|
477
|
+
const auto& right = staged->bindings_[index];
|
|
478
|
+
if (left.body != right.body || left.record->descriptor != right.record->descriptor
|
|
479
|
+
|| !sameValues(left.record->values(), right.record->values())) return false;
|
|
480
|
+
}
|
|
481
|
+
return true;
|
|
482
|
+
};
|
|
483
|
+
for (const GeometryRoot* existing : cache_->roots) {
|
|
484
|
+
if (sameRoot(*existing)) return existing->shared_from_this();
|
|
485
|
+
}
|
|
486
|
+
#if defined(RUNDOT_PHYSICS_PHASE_TEST_HOOKS)
|
|
487
|
+
if (geometryScanHook != nullptr) geometryScanHook();
|
|
488
|
+
#endif
|
|
489
|
+
std::uint64_t contentDigest = 14695981039346656037ull;
|
|
490
|
+
auto appendU64 = [&](std::uint64_t value) {
|
|
491
|
+
for (unsigned shift = 0; shift < 64; shift += 8) {
|
|
492
|
+
contentDigest = (contentDigest ^ static_cast<std::uint8_t>(value >> shift)) * 1099511628211ull;
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
appendU64(kGeometryContentDigestVersion);
|
|
496
|
+
appendU64(staged->bindings_.size());
|
|
497
|
+
for (const auto& binding : staged->bindings_) {
|
|
498
|
+
appendU64(binding.body.value);
|
|
499
|
+
appendU64(binding.record->fingerprint);
|
|
500
|
+
}
|
|
501
|
+
staged->contentDigest_ = contentDigest;
|
|
502
|
+
if (!staged->sealStrong()) return GeometryStoreFailure::InvalidInput;
|
|
503
|
+
cache_->roots.push_back(staged.get());
|
|
504
|
+
return GeometryRootOwner{std::move(staged)};
|
|
505
|
+
} catch (const AllocationCapacity&) {
|
|
506
|
+
return GeometryStoreFailure::Capacity;
|
|
507
|
+
} catch (const std::bad_alloc&) {
|
|
508
|
+
return GeometryStoreFailure::Allocation;
|
|
509
|
+
} catch (const std::length_error&) {
|
|
510
|
+
return GeometryStoreFailure::Capacity;
|
|
511
|
+
} catch (const std::logic_error&) {
|
|
512
|
+
return GeometryStoreFailure::InvalidInput;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
bool GeometryRoot::sealStrong() {
|
|
517
|
+
rundot::sha256::Sha256Sink sink;
|
|
518
|
+
rundot::sha256::LeFieldWriter<rundot::sha256::Sha256Sink> writer(sink);
|
|
519
|
+
if (writer.domainLiteral(kStrongRootDomain) != rundot::sha256::SinkStatus::Ok
|
|
520
|
+
|| writer.u64(bindings_.size()) != rundot::sha256::SinkStatus::Ok) {
|
|
521
|
+
return false;
|
|
522
|
+
}
|
|
523
|
+
for (const auto& binding : bindings_) {
|
|
524
|
+
if (!binding.record) return false;
|
|
525
|
+
if (writer.u64(binding.body.value) != rundot::sha256::SinkStatus::Ok
|
|
526
|
+
|| writer.bytes(binding.record->strongLeaf()) != rundot::sha256::SinkStatus::Ok) {
|
|
527
|
+
return false;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
const auto result = sink.final();
|
|
531
|
+
if (result.status != rundot::sha256::SinkStatus::Ok) return false;
|
|
532
|
+
strong_ = result.bytes;
|
|
533
|
+
sealed_ = true;
|
|
534
|
+
#if defined(RUNDOT_PHYSICS_PHASE_TEST_HOOKS)
|
|
535
|
+
rundot::sha256::gStrongRootSealFills.fetch_add(1, std::memory_order_relaxed);
|
|
536
|
+
#endif
|
|
537
|
+
return true;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
#if defined(RUNDOT_PHYSICS_PHASE_TEST_HOOKS)
|
|
541
|
+
GeometryRootOwner GeometryStore::unsealedCloneForTest(const GeometryRootOwner& source) const {
|
|
542
|
+
auto staged = makeLedgerObject<GeometryRoot>(cache_, cache_->roots, ledger_, false,
|
|
543
|
+
GeometryRoot::ConstructionKey{}, ledger_, source.get());
|
|
544
|
+
return staged;
|
|
545
|
+
}
|
|
546
|
+
#endif
|
|
547
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
#include "geometry_memory.hpp"
|
|
3
|
+
#include "topology.hpp"
|
|
4
|
+
#include <optional>
|
|
5
|
+
|
|
6
|
+
namespace rundot::kinetix::physics::geometry_private {
|
|
7
|
+
bool validGeometry(std::span<const double> geometry, ShapeKind kind,
|
|
8
|
+
std::uint64_t offset, std::uint64_t count, std::uint32_t format, std::uint32_t depth = 0);
|
|
9
|
+
struct GeometryDescriptor {
|
|
10
|
+
ShapeKind kind;
|
|
11
|
+
std::uint32_t count;
|
|
12
|
+
std::uint32_t format;
|
|
13
|
+
std::uint32_t featureOffset = 0;
|
|
14
|
+
bool operator==(const GeometryDescriptor&) const = default;
|
|
15
|
+
};
|
|
16
|
+
struct GeometryEdit {
|
|
17
|
+
BodyId body;
|
|
18
|
+
std::optional<GeometryDescriptor> descriptor;
|
|
19
|
+
std::span<const double> values;
|
|
20
|
+
};
|
|
21
|
+
struct GeometryStoreLimits {
|
|
22
|
+
std::uint64_t maxActiveValues = kGeometryMaxValues;
|
|
23
|
+
std::uint64_t maxLiveBytes = 512u * 1024u * 1024u;
|
|
24
|
+
std::uint32_t maxBindings = kContactTelemetryCapacity;
|
|
25
|
+
};
|
|
26
|
+
enum class GeometryStoreFailure { InvalidInput, Capacity, Allocation, IdOverflow, WrongStore };
|
|
27
|
+
class GeometryStore;
|
|
28
|
+
struct GeometryTriangle {
|
|
29
|
+
std::array<std::uint32_t, 3> points;
|
|
30
|
+
std::uint32_t ordinal;
|
|
31
|
+
};
|
|
32
|
+
struct GeometryTriangleNode {
|
|
33
|
+
std::array<double, 3> minimum, maximum;
|
|
34
|
+
std::uint32_t first, count, left = 0, right = 0;
|
|
35
|
+
};
|
|
36
|
+
struct GeometryTriangleSegment {
|
|
37
|
+
ShapeKind kind;
|
|
38
|
+
std::uint32_t offset, count, format, root;
|
|
39
|
+
};
|
|
40
|
+
class GeometryRecord : public std::enable_shared_from_this<GeometryRecord> {
|
|
41
|
+
friend class GeometryStore;
|
|
42
|
+
struct ConstructionKey { private: ConstructionKey() = default; friend class GeometryStore; };
|
|
43
|
+
const std::vector<double, LedgerAllocator<double>> values_;
|
|
44
|
+
std::vector<GeometryTriangle, LedgerAllocator<GeometryTriangle>> triangles_;
|
|
45
|
+
std::vector<GeometryTriangleNode, LedgerAllocator<GeometryTriangleNode>> triangleNodes_;
|
|
46
|
+
std::vector<GeometryTriangleSegment, LedgerAllocator<GeometryTriangleSegment>> triangleSegments_;
|
|
47
|
+
void buildTriangleIndex();
|
|
48
|
+
std::uint32_t buildTriangleNode(std::uint32_t offset, std::uint32_t first, std::uint32_t count);
|
|
49
|
+
public:
|
|
50
|
+
const std::uint64_t id;
|
|
51
|
+
const GeometryDescriptor descriptor;
|
|
52
|
+
const std::uint64_t fingerprint;
|
|
53
|
+
const std::array<std::uint8_t, 32> strongLeaf_;
|
|
54
|
+
GeometryRecord(ConstructionKey, std::shared_ptr<AllocationLedger> ledger, std::uint64_t recordId,
|
|
55
|
+
GeometryDescriptor shape, std::uint64_t hash, std::span<const double> values);
|
|
56
|
+
GeometryRecord(const GeometryRecord&) = delete;
|
|
57
|
+
GeometryRecord& operator=(const GeometryRecord&) = delete;
|
|
58
|
+
std::span<const double> values() const noexcept { return values_; }
|
|
59
|
+
std::span<const std::uint8_t, 32> strongLeaf() const noexcept { return strongLeaf_; }
|
|
60
|
+
std::span<const GeometryTriangle> triangles() const noexcept { return triangles_; }
|
|
61
|
+
std::span<const GeometryTriangleNode> triangleNodes() const noexcept { return triangleNodes_; }
|
|
62
|
+
const GeometryTriangleSegment* triangleSegment(ShapeKind kind, std::uint32_t offset,
|
|
63
|
+
std::uint32_t count, std::uint32_t format) const noexcept;
|
|
64
|
+
};
|
|
65
|
+
using GeometryRecordOwner = std::shared_ptr<const GeometryRecord>;
|
|
66
|
+
struct GeometryBinding {
|
|
67
|
+
BodyId body;
|
|
68
|
+
GeometryRecordOwner record;
|
|
69
|
+
};
|
|
70
|
+
constexpr std::uint32_t kGeometryContentDigestVersion = 1;
|
|
71
|
+
|
|
72
|
+
class GeometryRoot : public std::enable_shared_from_this<GeometryRoot> {
|
|
73
|
+
friend class GeometryStore;
|
|
74
|
+
struct ConstructionKey { private: ConstructionKey() = default; friend class GeometryStore; };
|
|
75
|
+
std::shared_ptr<AllocationLedger> ledger_;
|
|
76
|
+
std::vector<GeometryBinding, LedgerAllocator<GeometryBinding>> bindings_;
|
|
77
|
+
std::uint64_t activeValues_ = 0;
|
|
78
|
+
std::uint64_t nextRecordId_ = 1;
|
|
79
|
+
std::uint64_t contentDigest_ = 0;
|
|
80
|
+
std::array<std::uint8_t, 32> strong_{};
|
|
81
|
+
bool sealed_ = false;
|
|
82
|
+
bool sealStrong();
|
|
83
|
+
public:
|
|
84
|
+
GeometryRoot(ConstructionKey, std::shared_ptr<AllocationLedger> ledger, const GeometryRoot* source)
|
|
85
|
+
: ledger_(std::move(ledger)), bindings_(LedgerAllocator<GeometryBinding>{ledger_}) {
|
|
86
|
+
if (source != nullptr) {
|
|
87
|
+
bindings_.assign(source->bindings_.begin(), source->bindings_.end());
|
|
88
|
+
activeValues_ = source->activeValues_;
|
|
89
|
+
nextRecordId_ = source->nextRecordId_;
|
|
90
|
+
contentDigest_ = source->contentDigest_;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
GeometryRoot(const GeometryRoot&) = delete;
|
|
94
|
+
GeometryRoot& operator=(const GeometryRoot&) = delete;
|
|
95
|
+
std::span<const GeometryBinding> bindings() const noexcept { return bindings_; }
|
|
96
|
+
std::uint64_t activeValues() const noexcept { return activeValues_; }
|
|
97
|
+
std::uint64_t contentDigest() const noexcept { return contentDigest_; }
|
|
98
|
+
bool strongSealed() const noexcept { return sealed_; }
|
|
99
|
+
std::span<const std::uint8_t, 32> strongLeaf() const noexcept { return strong_; }
|
|
100
|
+
};
|
|
101
|
+
using GeometryRootOwner = std::shared_ptr<const GeometryRoot>;
|
|
102
|
+
using GeometryStoreResult = std::variant<GeometryRootOwner, GeometryStoreFailure>;
|
|
103
|
+
#if defined(RUNDOT_PHYSICS_PHASE_TEST_HOOKS)
|
|
104
|
+
using GeometryScanHook = void (*)() noexcept;
|
|
105
|
+
inline thread_local GeometryScanHook geometryScanHook = nullptr;
|
|
106
|
+
// Synthetic only. This is not a discovered 64-bit collision.
|
|
107
|
+
inline thread_local bool syntheticFingerprintArmed = false;
|
|
108
|
+
inline thread_local std::uint64_t syntheticFingerprintValue = 0;
|
|
109
|
+
#endif
|
|
110
|
+
|
|
111
|
+
struct GeometryCacheAnchor {
|
|
112
|
+
std::vector<const GeometryRecord*, WorldCacheAllocator<const GeometryRecord*>> records;
|
|
113
|
+
std::vector<const GeometryRoot*, WorldCacheAllocator<const GeometryRoot*>> roots;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
class GeometryStore {
|
|
117
|
+
GeometryStoreLimits limits_;
|
|
118
|
+
std::shared_ptr<AllocationLedger> ledger_;
|
|
119
|
+
std::shared_ptr<GeometryCacheAnchor> cache_;
|
|
120
|
+
public:
|
|
121
|
+
explicit GeometryStore(GeometryStoreLimits limits = {});
|
|
122
|
+
GeometryStore(const GeometryStore&) = delete;
|
|
123
|
+
GeometryStore& operator=(const GeometryStore&) = delete;
|
|
124
|
+
GeometryStore(GeometryStore&&) noexcept = default;
|
|
125
|
+
GeometryStore& operator=(GeometryStore&&) noexcept = default;
|
|
126
|
+
std::uint64_t liveBytes() const noexcept { return ledger_->liveBytes(); }
|
|
127
|
+
std::uint64_t anchorCapacityBytes() const noexcept;
|
|
128
|
+
std::uint64_t nextAnchorGrowthBytes(std::uint64_t newRecords, std::uint64_t newRoots) const noexcept;
|
|
129
|
+
GeometryStoreResult stage(const GeometryRootOwner& source, std::span<const GeometryEdit> edits) const;
|
|
130
|
+
#if defined(RUNDOT_PHYSICS_PHASE_TEST_HOOKS)
|
|
131
|
+
GeometryRootOwner unsealedCloneForTest(const GeometryRootOwner& source) const;
|
|
132
|
+
#endif
|
|
133
|
+
};
|
|
134
|
+
}
|