Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V3.8.2 box2d wasm nodscale opt #16386

Merged
merged 6 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 53 additions & 18 deletions cocos/physics-2d/box2d-wasm/instantiated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ export function getImplPtr (wasmObject: any): number {
return (wasmObject).$$.ptr as number;
}

// type : Fixture, Body, Contact, Joint, ...
export const enum B2ObjectType {
Fixture = 0,
Body,
Contact,
Joint,
}

/**
* mapping wasm-object-ptr to ts-object
* B2.Fixture pointer -->B2Shape2D
Expand All @@ -48,22 +56,35 @@ export function getImplPtr (wasmObject: any): number {
* B2.Joint pointer --> B2Joint
* ...
*/
const WASM_OBJECT_PTR_2_TS_OBJECT = {};
export function addImplPtrReference (TSObject: any, implPtr: number): void {
if (implPtr) { WASM_OBJECT_PTR_2_TS_OBJECT[implPtr] = TSObject; }
}
export function removeImplPtrReference (implPtr: number): void {
const WASM_OBJECT_PTR_2_TS_OBJECT = new Map<B2ObjectType, Map<number, any>>();

export function addImplPtrReference (Type: B2ObjectType, TSObject: any, implPtr: number): void {
if (implPtr) {
delete WASM_OBJECT_PTR_2_TS_OBJECT[implPtr];
let map = WASM_OBJECT_PTR_2_TS_OBJECT.get(Type);
if (!map) {
map = new Map<number, any>();
WASM_OBJECT_PTR_2_TS_OBJECT.set(Type, map);
}
map.set(implPtr, TSObject);
}
}
export function getTSObjectFromWASMObjectPtr<T> (implPtr: number): T {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return WASM_OBJECT_PTR_2_TS_OBJECT[implPtr];

export function removeImplPtrReference (Type: B2ObjectType, implPtr: number): void {
if (implPtr) {
const map = WASM_OBJECT_PTR_2_TS_OBJECT.get(Type);
if (map && map.has(implPtr)) {
map.delete(implPtr);
if (map.size === 0) {
WASM_OBJECT_PTR_2_TS_OBJECT.delete(Type);
}
}
}
}
export function getTSObjectFromWASMObject<T> (impl: any): T {

export function getTSObjectFromWASMObjectPtr<T> (Type: B2ObjectType, implPtr: number): T {
const map = WASM_OBJECT_PTR_2_TS_OBJECT.get(Type);
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return WASM_OBJECT_PTR_2_TS_OBJECT[getImplPtr(impl)];
return map?.get(implPtr);
}

/**
Expand All @@ -74,20 +95,34 @@ export function getTSObjectFromWASMObject<T> (impl: any): T {
* B2.Joint pointer --> B2.Joint
* ...
*/
const WASM_OBJECT_PTR_2_WASM_OBJECT = {};
export function addImplPtrReferenceWASM (WASMObject: any, implPtr: number): void {
if (implPtr) { WASM_OBJECT_PTR_2_WASM_OBJECT[implPtr] = WASMObject; }
const WASM_OBJECT_PTR_2_WASM_OBJECT = new Map<B2ObjectType, Map<number, any>>();
export function addImplPtrReferenceWASM (Type: B2ObjectType, WASMObject: any, implPtr: number): void {
if (implPtr) {
let map = WASM_OBJECT_PTR_2_WASM_OBJECT.get(Type);
if (!map) {
map = new Map<number, any>();
WASM_OBJECT_PTR_2_WASM_OBJECT.set(Type, map);
}
map.set(implPtr, WASMObject);
}
}

export function removeImplPtrReferenceWASM (implPtr: number): void {
export function removeImplPtrReferenceWASM (Type: B2ObjectType, implPtr: number): void {
if (implPtr) {
delete WASM_OBJECT_PTR_2_WASM_OBJECT[implPtr];
const map = WASM_OBJECT_PTR_2_WASM_OBJECT.get(Type);
if (map && map.has(implPtr)) {
map.delete(implPtr);
if (map.size === 0) {
WASM_OBJECT_PTR_2_WASM_OBJECT.delete(Type);
}
}
}
}

export function getWASMObjectFromWASMObjectPtr<T> (implPtr: number): T {
export function getWASMObjectFromWASMObjectPtr<T> (Type: B2ObjectType, implPtr: number): T {
const map = WASM_OBJECT_PTR_2_WASM_OBJECT.get(Type);
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return WASM_OBJECT_PTR_2_WASM_OBJECT[implPtr];
return map?.get(implPtr);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions cocos/physics-2d/box2d-wasm/joints/joint-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
THE SOFTWARE.
*/

import { B2, addImplPtrReference, addImplPtrReferenceWASM, getImplPtr, removeImplPtrReference, removeImplPtrReferenceWASM } from '../instantiated';
import { B2, B2ObjectType, addImplPtrReference, addImplPtrReferenceWASM, getImplPtr, removeImplPtrReference, removeImplPtrReferenceWASM } from '../instantiated';

Check warning on line 25 in cocos/physics-2d/box2d-wasm/joints/joint-2d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 161. Maximum allowed is 150
import { IJoint2D } from '../../spec/i-physics-joint';
import { Joint2D, PhysicsSystem2D, RigidBody2D } from '../../framework';
import { B2PhysicsWorld } from '../physics-world';
Expand Down Expand Up @@ -105,17 +105,17 @@
def.collideConnected = comp.collideConnected;

this._b2joint = (PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld).impl.CreateJoint(def);
addImplPtrReference(this, getImplPtr(this._b2joint));
addImplPtrReferenceWASM(this._b2joint, getImplPtr(this._b2joint));
addImplPtrReference(B2ObjectType.Joint, this, getImplPtr(this._b2joint));
addImplPtrReferenceWASM(B2ObjectType.Joint, this._b2joint, getImplPtr(this._b2joint));

this._inited = true;
}

destroy (): void {
if (!this._inited) return;

removeImplPtrReference(getImplPtr(this._b2joint));
removeImplPtrReferenceWASM(getImplPtr(this._b2joint));
removeImplPtrReference(B2ObjectType.Joint, getImplPtr(this._b2joint));
removeImplPtrReferenceWASM(B2ObjectType.Joint, getImplPtr(this._b2joint));
(PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld).impl.DestroyJoint(this._b2joint!);

this._b2joint = null;
Expand Down
13 changes: 7 additions & 6 deletions cocos/physics-2d/box2d-wasm/physics-contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
THE SOFTWARE.
*/

import { B2, addImplPtrReference, getTSObjectFromWASMObjectPtr, removeImplPtrReference } from './instantiated';
import { B2, B2ObjectType, addImplPtrReference, getTSObjectFromWASMObjectPtr, removeImplPtrReference } from './instantiated';
import { Vec2 } from '../../core';
import { PHYSICS_2D_PTM_RATIO } from '../framework/physics-types';
import { Collider2D, Contact2DType, PhysicsSystem2D } from '../framework';
Expand Down Expand Up @@ -74,7 +74,7 @@ export class PhysicsContact implements IPhysics2DContact {
}

static put (b2contact: number): void {
const c = getTSObjectFromWASMObjectPtr<PhysicsContact>(b2contact);
const c = getTSObjectFromWASMObjectPtr<PhysicsContact>(B2ObjectType.Contact, b2contact);
if (!c) return;

pools.push(c);
Expand All @@ -97,15 +97,16 @@ export class PhysicsContact implements IPhysics2DContact {
}

init (b2contact: number): void {
this.colliderA = (getTSObjectFromWASMObjectPtr<B2Shape2D>(B2.ContactGetFixtureA(b2contact) as number)).collider;
this.colliderB = (getTSObjectFromWASMObjectPtr<B2Shape2D>(B2.ContactGetFixtureB(b2contact) as number)).collider;
const ab = B2.ContactGetFixture(b2contact) as Vec2;
this.colliderA = (getTSObjectFromWASMObjectPtr<B2Shape2D>(B2ObjectType.Fixture, ab.x)).collider;
this.colliderB = (getTSObjectFromWASMObjectPtr<B2Shape2D>(B2ObjectType.Fixture, ab.y)).collider;
this.disabled = false;
this.disabledOnce = false;
this._impulsePtr = 0;
this._inverted = false;

this._implPtr = b2contact;
addImplPtrReference(this, this._implPtr);
addImplPtrReference(B2ObjectType.Contact, this, this._implPtr);
this._b2WorldmanifoldPtr = B2.WorldManifoldNew();
}

Expand All @@ -119,7 +120,7 @@ export class PhysicsContact implements IPhysics2DContact {
this.disabled = false;
this._impulsePtr = 0;

removeImplPtrReference(this._implPtr);
removeImplPtrReference(B2ObjectType.Contact, this._implPtr);
this._implPtr = 0;

B2.WorldManifoldDelete(this._b2WorldmanifoldPtr);
Expand Down
34 changes: 18 additions & 16 deletions cocos/physics-2d/box2d-wasm/physics-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
*/

import { EDITOR_NOT_IN_PREVIEW } from 'internal:constants';
import { B2, getImplPtr, addImplPtrReference, addImplPtrReferenceWASM, getTSObjectFromWASMObject,
getTSObjectFromWASMObjectPtr, removeImplPtrReference, removeImplPtrReferenceWASM } from './instantiated';
import { B2, getImplPtr, addImplPtrReference, addImplPtrReferenceWASM,
getTSObjectFromWASMObjectPtr, removeImplPtrReference, removeImplPtrReferenceWASM, B2ObjectType } from './instantiated';
import { IPhysicsWorld } from '../spec/i-physics-world';
import { IVec2Like, Vec3, Quat, toRadian, Vec2, toDegree, Rect, CCObject, js } from '../../core';
import { PHYSICS_2D_PTM_RATIO, ERaycast2DType, ERigidBody2DType } from '../framework/physics-types';
Expand Down Expand Up @@ -64,6 +64,7 @@ export class B2PhysicsWorld implements IPhysicsWorld {

private _temoBodyDef: B2.BodyDef;
private _tempB2AABB: B2.AABB;
public tempB2FixtureDefPtr: number;

get impl (): B2.World {
return this._world;
Expand All @@ -88,6 +89,7 @@ export class B2PhysicsWorld implements IPhysicsWorld {

this._temoBodyDef = new B2.BodyDef();
this._tempB2AABB = new B2.AABB();
this.tempB2FixtureDefPtr = B2.FixtureDefNew();
}

_debugGraphics: Graphics | null = null;
Expand Down Expand Up @@ -189,7 +191,7 @@ export class B2PhysicsWorld implements IPhysicsWorld {
const results: RaycastResult2D[] = [];
for (let i = 0, l = fixtures.length; i < l; i++) {
const fixture = fixtures[i];
const shape = getTSObjectFromWASMObject<B2Shape2D>(fixture);
const shape = getTSObjectFromWASMObjectPtr<B2Shape2D>(B2ObjectType.Fixture, fixture);
const collider = shape.collider;

if (type === ERaycast2DType.AllClosest) {
Expand Down Expand Up @@ -319,8 +321,8 @@ export class B2PhysicsWorld implements IPhysicsWorld {
bodyDef.angularVelocity = toRadian(compPrivate._angularVelocity as number);

const b2Body = this._world.CreateBody(bodyDef);
addImplPtrReference(body, getImplPtr(b2Body));
addImplPtrReferenceWASM(b2Body, getImplPtr(b2Body));
addImplPtrReference(B2ObjectType.Body, body, getImplPtr(b2Body));
addImplPtrReferenceWASM(B2ObjectType.Body, b2Body, getImplPtr(b2Body));
body._imp = b2Body;

this._bodies.push(body);
Expand All @@ -331,8 +333,8 @@ export class B2PhysicsWorld implements IPhysicsWorld {
return;
}
if (body.impl) {
removeImplPtrReference(getImplPtr(body.impl));
removeImplPtrReferenceWASM(getImplPtr(body.impl));
removeImplPtrReference(B2ObjectType.Body, getImplPtr(body.impl));
removeImplPtrReferenceWASM(B2ObjectType.Body, getImplPtr(body.impl));
this._world.DestroyBody(body.impl);
body._imp = null;
}
Expand All @@ -344,11 +346,11 @@ export class B2PhysicsWorld implements IPhysicsWorld {
}
}

registerContactFixture (fixture: B2.Fixture): void {
this._contactListener.registerContactFixture(getImplPtr(fixture));
registerContactFixture (fixture: number): void { //B2.Fixture ptr
this._contactListener.registerContactFixture(fixture);
}
unregisterContactFixture (fixture: B2.Fixture): void {
this._contactListener.unregisterContactFixture(getImplPtr(fixture));
unregisterContactFixture (fixture: number): void { //B2.Fixture ptr
this._contactListener.unregisterContactFixture(fixture);
}

testPoint (point: Vec2): readonly Collider2D[] {
Expand All @@ -366,7 +368,7 @@ export class B2PhysicsWorld implements IPhysicsWorld {
const fixtures = PhysicsAABBQueryCallback.getFixtures();
testResults.length = 0;
for (let i = 0; i < fixtures.length; i++) {
const collider = getTSObjectFromWASMObject<B2Shape2D>(fixtures[i]).collider;
const collider = getTSObjectFromWASMObjectPtr<B2Shape2D>(B2ObjectType.Fixture, fixtures[i]).collider;
if (!testResults.includes(collider)) {
testResults.push(collider);
}
Expand All @@ -385,7 +387,7 @@ export class B2PhysicsWorld implements IPhysicsWorld {
const fixtures = PhysicsAABBQueryCallback.getFixtures();
testResults.length = 0;
for (let i = 0; i < fixtures.length; i++) {
const collider = getTSObjectFromWASMObject<B2Shape2D>(fixtures[i]).collider;
const collider = getTSObjectFromWASMObjectPtr<B2Shape2D>(B2ObjectType.Fixture, fixtures[i]).collider;
if (!testResults.includes(collider)) {
testResults.push(collider);
}
Expand All @@ -409,7 +411,7 @@ export class B2PhysicsWorld implements IPhysicsWorld {
}

_onEndContact (b2contact: number): void {
const c = getTSObjectFromWASMObjectPtr<PhysicsContact>(b2contact);
const c = getTSObjectFromWASMObjectPtr<PhysicsContact>(B2ObjectType.Contact, b2contact);
if (!c) {
return;
}
Expand All @@ -419,7 +421,7 @@ export class B2PhysicsWorld implements IPhysicsWorld {
}

_onPreSolve (b2contact: number): void {
const c = getTSObjectFromWASMObjectPtr<PhysicsContact>(b2contact);
const c = getTSObjectFromWASMObjectPtr<PhysicsContact>(B2ObjectType.Contact, b2contact);
if (!c) {
return;
}
Expand All @@ -428,7 +430,7 @@ export class B2PhysicsWorld implements IPhysicsWorld {
}

_onPostSolve (b2contact: number, impulse: number): void {
const c = getTSObjectFromWASMObjectPtr<PhysicsContact>(b2contact);
const c = getTSObjectFromWASMObjectPtr<PhysicsContact>(B2ObjectType.Contact, b2contact);
if (!c) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@
THE SOFTWARE.
*/

import { B2, getTSObjectFromWASMObject, getWASMObjectFromWASMObjectPtr } from '../instantiated';
import { B2 } from '../instantiated';
import { Vec2 } from '../../../core';
import { B2RigidBody2D } from '../rigid-body';

export class PhysicsAABBQueryCallback {
static _point = { x: 0, y: 0 };
static _isPoint = false;
static _fixtures: B2.Fixture[] = [];
static _fixtures: number[] = [];//B2.Fixture ptr

static init (point?: Vec2): void {
if (point) {
Expand All @@ -43,9 +42,9 @@ export class PhysicsAABBQueryCallback {
this._fixtures.length = 0;
}

static ReportFixture (fixture: B2.Fixture): boolean {
static ReportFixture (fixture: number): boolean {
if (this._isPoint) {
if (fixture.TestPoint(this._point)) {
if (B2.FixtureTestPoint(fixture, this._point)) {
this._fixtures.push(fixture);
}
} else {
Expand All @@ -56,18 +55,17 @@ export class PhysicsAABBQueryCallback {
return true;
}

static getFixture (): any {
static getFixture (): number {
return this._fixtures[0];
}

static getFixtures (): any[] {
static getFixtures (): number[] {
return this._fixtures;
}

static callback = {
ReportFixture (fixture: number): boolean {
const f = getWASMObjectFromWASMObjectPtr<B2.Fixture>(fixture);
return PhysicsAABBQueryCallback.ReportFixture(f);
return PhysicsAABBQueryCallback.ReportFixture(fixture);
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@
THE SOFTWARE.
*/

import { B2, getTSObjectFromWASMObject, getWASMObjectFromWASMObjectPtr } from '../instantiated';
import { B2 } from '../instantiated';
import { Vec2 } from '../../../core';
import { ERaycast2DType } from '../../framework';
import { B2Shape2D } from '../shapes/shape-2d';
import { B2RigidBody2D } from '../rigid-body';

export class PhysicsRayCastCallback {// extends B2.RayCastCallback {
static _type = ERaycast2DType.Closest;
static _fixtures: B2.Fixture[] = [];
static _fixtures: number[] = [];//B2.Fixture ptr
static _points: Vec2[] = [];
static _normals: Vec2[] = [];
static _fractions: number[] = [];
Expand All @@ -46,8 +44,8 @@ export class PhysicsRayCastCallback {// extends B2.RayCastCallback {
PhysicsRayCastCallback._fractions.length = 0;
}

static ReportFixture (fixture: B2.Fixture, point: B2.Vec2, normal: B2.Vec2, fraction: number): any {
if ((fixture.GetFilterData().categoryBits & PhysicsRayCastCallback._mask) === 0) {
static ReportFixture (fixture: number, point: B2.Vec2, normal: B2.Vec2, fraction: number): any {
if ((B2.FixtureGetFilterData(fixture).categoryBits & PhysicsRayCastCallback._mask) === 0) {
return 0;
}

Expand All @@ -73,7 +71,7 @@ export class PhysicsRayCastCallback {// extends B2.RayCastCallback {
return fraction;
}

static getFixtures (): B2.Fixture[] {
static getFixtures (): number[] {
return PhysicsRayCastCallback._fixtures;
}

Expand All @@ -91,8 +89,7 @@ export class PhysicsRayCastCallback {// extends B2.RayCastCallback {

static callback = {
ReportFixture (fixture: number, point: B2.Vec2, normal: B2.Vec2, fraction: number): any {
const f = getWASMObjectFromWASMObjectPtr<B2.Fixture>(fixture);
return PhysicsRayCastCallback.ReportFixture(f, point, normal, fraction);
return PhysicsRayCastCallback.ReportFixture(fixture, point, normal, fraction);
},
};
}