Skip to content

Commit

Permalink
fixed cocos#16248: .wasm / .asm.js files are both downloaded on web p…
Browse files Browse the repository at this point in the history
…latform (cocos#16679)
  • Loading branch information
dumganhar authored Jan 11, 2024
1 parent 82ee6a3 commit 75c6f6d
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 132 deletions.
6 changes: 3 additions & 3 deletions cc.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -650,10 +650,10 @@
"value": false,
"internal": true
},
"WASM_SUPPORT_MODE": {
"comment": "Whether support wasm, here we provide 3 options:\n0: The platform doesn't support WASM\n1: The platform supports WASM\n2: The platform may support WASM, especially on Web platform",
"NATIVE_CODE_BUNDLE_MODE": {
"comment": "Native code (wasm/asmjs) bundle mode, 0: asmjs, 1: wasm, 2: both",
"type": "number",
"value": "($HTML5 || $BYTEDANCE) ? 2 : ($NATIVE ? (($OPEN_HARMONY||$IOS)?0:1): ($MINIGAME? ($WECHAT?1:0) :0))",
"value": 2,
"internal": true
},
"WASM_SUBPACKAGE": {
Expand Down
37 changes: 19 additions & 18 deletions cocos/3d/misc/mesh-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import { CULL_MESHOPT, WASM_SUPPORT_MODE } from 'internal:constants';
import { CULL_MESHOPT, NATIVE_CODE_BUNDLE_MODE } from 'internal:constants';
import { ensureWasmModuleReady, instantiateWasm } from 'pal/wasm';

import { sys, logID } from '../../core';
import { sys, logID, error } from '../../core';

import { game } from '../../game';
import { WebAssemblySupportMode } from '../../misc/webassembly-support';
import { NativeCodeBundleMode } from '../../misc/webassembly-support';

export const MeshoptDecoder = {} as any;

Expand Down Expand Up @@ -63,31 +63,32 @@ function initDecoderWASM (wasm_factory: any, wasm_url: string): Promise<void> {
}

function shouldUseWasmModule (): boolean {
if (WASM_SUPPORT_MODE === (WebAssemblySupportMode.MAYBE_SUPPORT as number)) {
if (NATIVE_CODE_BUNDLE_MODE === (NativeCodeBundleMode.BOTH as number)) {
return sys.hasFeature(sys.Feature.WASM);
} else if (WASM_SUPPORT_MODE === (WebAssemblySupportMode.SUPPORT as number)) {
} else if (NATIVE_CODE_BUNDLE_MODE === (NativeCodeBundleMode.WASM as number)) {
return true;
} else {
return false;
}
}

export function InitDecoder (): Promise<void> {
return ensureWasmModuleReady().then(() => Promise.all([
import('external:emscripten/meshopt/meshopt_decoder.asm.js'),
import('external:emscripten/meshopt/meshopt_decoder.wasm.js'),
import('external:emscripten/meshopt/meshopt_decoder.wasm.wasm'),
]).then(([
{ default: meshopt_asm_factory },
{ default: meshopt_wasm_factory },
{ default: meshopt_wasm_url },
]) => {
if (shouldUseWasmModule() && meshopt_wasm_url) {
return initDecoderWASM(meshopt_wasm_factory, meshopt_wasm_url);
const errorReport = (msg: any): void => { error(msg); };
return ensureWasmModuleReady().then(() => {
if (shouldUseWasmModule()) {
return Promise.all([
import('external:emscripten/meshopt/meshopt_decoder.wasm.js'),
import('external:emscripten/meshopt/meshopt_decoder.wasm.wasm'),
]).then(([
{ default: meshopt_wasm_factory },
{ default: meshopt_wasm_url },
]) => initDecoderWASM(meshopt_wasm_factory, meshopt_wasm_url));
} else {
return initDecoderASM(meshopt_asm_factory);
return import('external:emscripten/meshopt/meshopt_decoder.asm.js').then(
({ default: meshopt_asm_factory }) => initDecoderASM(meshopt_asm_factory),
);
}
}));
}).catch(errorReport);
}

if (!CULL_MESHOPT) {
Expand Down
10 changes: 5 additions & 5 deletions cocos/misc/webassembly-support.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* An enum type for WASM_SUPPORT_MODE constant, making the support mode more readable.
* An enum type for NATIVE_CODE_BUNDLE_MODE constant, making the support mode more readable.
*/
export const enum WebAssemblySupportMode {
NONE = 0,
SUPPORT = 1,
MAYBE_SUPPORT = 2,
export const enum NativeCodeBundleMode {
ASMJS = 0,
WASM = 1,
BOTH = 2,
}
34 changes: 17 additions & 17 deletions cocos/physics-2d/box2d-wasm/instantiated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
*/

import { instantiateWasm, ensureWasmModuleReady } from 'pal/wasm';
import { WASM_SUPPORT_MODE } from 'internal:constants';
import { NATIVE_CODE_BUNDLE_MODE } from 'internal:constants';

import { game } from '../../game';
import { error, sys, IVec2Like, log } from '../../core';
import { WebAssemblySupportMode } from '../../misc/webassembly-support';
import { NativeCodeBundleMode } from '../../misc/webassembly-support';

// eslint-disable-next-line import/no-mutable-exports
export let B2 = {} as any;
Expand Down Expand Up @@ -164,9 +164,9 @@ function initAsm (asmFactory): Promise<void> {
}

function shouldUseWasmModule (): boolean {
if (WASM_SUPPORT_MODE === WebAssemblySupportMode.MAYBE_SUPPORT as number) {
if (NATIVE_CODE_BUNDLE_MODE === (NativeCodeBundleMode.BOTH as number)) {
return sys.hasFeature(sys.Feature.WASM);
} else if (WASM_SUPPORT_MODE === WebAssemblySupportMode.SUPPORT as number) {
} else if (NATIVE_CODE_BUNDLE_MODE === (NativeCodeBundleMode.WASM as number)) {
return true;
} else {
return false;
Expand All @@ -175,21 +175,21 @@ function shouldUseWasmModule (): boolean {

export function waitForBox2dWasmInstantiation (): Promise<void> {
const errorReport = (msg: any): void => { error(msg); };
return ensureWasmModuleReady().then(() => Promise.all([
import('external:emscripten/box2d/box2d.release.wasm.js'),
import('external:emscripten/box2d/box2d.release.wasm.wasm'),
import('external:emscripten/box2d/box2d.release.asm.js'),
]).then(([
{ default: wasmFactory },
{ default: wasmUrl },
{ default: asmFactory },
]) => {
if (shouldUseWasmModule() && wasmUrl) {
return initWasm(wasmFactory, wasmUrl);
return ensureWasmModuleReady().then(() => {
if (shouldUseWasmModule()) {
return Promise.all([
import('external:emscripten/box2d/box2d.release.wasm.js'),
import('external:emscripten/box2d/box2d.release.wasm.wasm'),
]).then(([
{ default: wasmFactory },
{ default: wasmUrl },
]) => initWasm(wasmFactory, wasmUrl));
} else {
return initAsm(asmFactory);
return import('external:emscripten/box2d/box2d.release.asm.js').then(
({ default: asmFactory }) => initAsm(asmFactory),
);
}
})).catch(errorReport);
}).catch(errorReport);
}

game.onPostInfrastructureInitDelegate.add(waitForBox2dWasmInstantiation);
36 changes: 17 additions & 19 deletions cocos/physics/bullet/instantiated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
*/

import { ensureWasmModuleReady, instantiateWasm } from 'pal/wasm';
import { WASM_SUPPORT_MODE } from 'internal:constants';
import { NATIVE_CODE_BUNDLE_MODE } from 'internal:constants';
import { game } from '../../game';
import { error, log, sys } from '../../core';
import { WebAssemblySupportMode } from '../../misc/webassembly-support';
import { NativeCodeBundleMode } from '../../misc/webassembly-support';

//corresponds to bulletType in bullet-compile
export enum EBulletType{
Expand Down Expand Up @@ -126,11 +126,9 @@ function initASM (asmFactory): Promise<void> {
}

function shouldUseWasmModule (): boolean {
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
if (WASM_SUPPORT_MODE === WebAssemblySupportMode.MAYBE_SUPPORT) {
if (NATIVE_CODE_BUNDLE_MODE === (NativeCodeBundleMode.BOTH as number)) {
return sys.hasFeature(sys.Feature.WASM);
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
} else if (WASM_SUPPORT_MODE === WebAssemblySupportMode.SUPPORT) {
} else if (NATIVE_CODE_BUNDLE_MODE === (NativeCodeBundleMode.WASM as number)) {
return true;
} else {
return false;
Expand All @@ -139,21 +137,21 @@ function shouldUseWasmModule (): boolean {

export function waitForAmmoInstantiation (): Promise<void> {
const errorReport = (msg: any): void => { error(msg); };
return ensureWasmModuleReady().then(() => Promise.all([
import('external:emscripten/bullet/bullet.release.wasm.js'),
import('external:emscripten/bullet/bullet.release.wasm.wasm'),
import('external:emscripten/bullet/bullet.release.asm.js'),
]).then(([
{ default: bulletWasmFactory },
{ default: bulletWasmUrl },
{ default: bulletAsmFactory },
]) => {
if (shouldUseWasmModule() && bulletWasmUrl) {
return initWASM(bulletWasmFactory, bulletWasmUrl);
return ensureWasmModuleReady().then(() => {
if (shouldUseWasmModule()) {
return Promise.all([
import('external:emscripten/bullet/bullet.release.wasm.js'),
import('external:emscripten/bullet/bullet.release.wasm.wasm'),
]).then(([
{ default: bulletWasmFactory },
{ default: bulletWasmUrl },
]) => initWASM(bulletWasmFactory, bulletWasmUrl));
} else {
return initASM(bulletAsmFactory);
return import('external:emscripten/bullet/bullet.release.asm.js').then(
({ default: bulletAsmFactory }) => initASM(bulletAsmFactory),
);
}
})).catch(errorReport);
}).catch(errorReport);
}

game.onPostInfrastructureInitDelegate.add(waitForAmmoInstantiation);
76 changes: 38 additions & 38 deletions cocos/physics/physx/physx-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
/* eslint-disable no-lonely-if */
/* eslint-disable import/order */

import { WebAssemblySupportMode } from '../../misc/webassembly-support';
import { NativeCodeBundleMode } from '../../misc/webassembly-support';
import { ensureWasmModuleReady, instantiateWasm } from 'pal/wasm';
import { BYTEDANCE, DEBUG, EDITOR, TEST, WASM_SUPPORT_MODE } from 'internal:constants';
import { IQuatLike, IVec3Like, Quat, RecyclePool, Vec3, cclegacy, geometry, Settings, settings, sys, Color } from '../../core';
import { BYTEDANCE, DEBUG, EDITOR, TEST, NATIVE_CODE_BUNDLE_MODE } from 'internal:constants';
import { IQuatLike, IVec3Like, Quat, RecyclePool, Vec3, cclegacy, geometry, Settings, settings, sys, Color, error, IVec3 } from '../../core';
import { shrinkPositions } from '../utils/util';
import { IRaycastOptions } from '../spec/i-physics-world';
import { IPhysicsConfig, PhysicsRayResult, PhysicsSystem, CharacterControllerContact } from '../framework';
Expand Down Expand Up @@ -67,23 +67,22 @@ export function InitPhysXLibs (): Promise<void> {
resolve();
});
} else {
return ensureWasmModuleReady().then(() => Promise.all([
import('external:emscripten/physx/physx.release.wasm.js'),
import('external:emscripten/physx/physx.release.wasm.wasm'),
import('external:emscripten/physx/physx.release.asm.js'),
]).then(([
{ default: physxWasmFactory },
{ default: physxWasmUrl },
{ default: physxAsmFactory },
]) => InitPhysXLibsInternal(physxWasmFactory, physxWasmUrl, physxAsmFactory)));
}
}

function InitPhysXLibsInternal (physxWasmFactory, physxWasmUrl: string, physxAsmFactory): any {
if (shouldUseWasmModule() && physxWasmUrl) {
return initWASM(physxWasmFactory, physxWasmUrl);
} else {
return initASM(physxAsmFactory);
const errorReport = (msg: any): void => { error(msg); };
return ensureWasmModuleReady().then(() => {
if (shouldUseWasmModule()) {
return Promise.all([
import('external:emscripten/physx/physx.release.wasm.js'),
import('external:emscripten/physx/physx.release.wasm.wasm'),
]).then(([
{ default: physxWasmFactory },
{ default: physxWasmUrl },
]) => initWASM(physxWasmFactory, physxWasmUrl));
} else {
return import('external:emscripten/physx/physx.release.asm.js').then(
({ default: physxAsmFactory }) => initASM(physxAsmFactory),
);
}
}).catch(errorReport);
}
}

Expand Down Expand Up @@ -131,9 +130,9 @@ function initWASM (physxWasmFactory, physxWasmUrl: string): any {
}

function shouldUseWasmModule (): boolean {
if (WASM_SUPPORT_MODE === WebAssemblySupportMode.MAYBE_SUPPORT) {
if (NATIVE_CODE_BUNDLE_MODE === (NativeCodeBundleMode.BOTH as number)) {
return sys.hasFeature(sys.Feature.WASM);
} else if (WASM_SUPPORT_MODE === WebAssemblySupportMode.SUPPORT) {
} else if (NATIVE_CODE_BUNDLE_MODE === (NativeCodeBundleMode.WASM as number)) {
return true;
} else {
return false;
Expand Down Expand Up @@ -286,22 +285,22 @@ export function copyPhysXTransform (node: Node, transform: any): void {
const dontUpdate = physXEqualsCocosVec3(transform, wp) && physXEqualsCocosQuat(transform, wr);
if (dontUpdate) return;
if (USE_BYTEDANCE) {
node.setWorldPosition(transform.p);
node.setWorldRotation(transform.q);
node.setWorldPosition(transform.p as Vec3);
node.setWorldRotation(transform.q as Quat);
} else {
node.setWorldPosition(transform.translation);
node.setWorldRotation(transform.rotation);
node.setWorldPosition(transform.translation as Vec3);
node.setWorldRotation(transform.rotation as Quat);
}
}

export function physXEqualsCocosVec3 (trans: any, v3: IVec3Like): boolean {
const pos = USE_BYTEDANCE ? trans.p : trans.translation;
return Vec3.equals(pos, v3, PX.EPSILON);
return Vec3.equals(pos as IVec3Like, v3, PX.EPSILON as number);
}

export function physXEqualsCocosQuat (trans: any, q: IQuatLike): boolean {
const rot = USE_BYTEDANCE ? trans.q : trans.rotation;
return Quat.equals(rot, q, PX.EPSILON);
return Quat.equals(rot as IQuatLike, q, PX.EPSILON as number);
}

export function applyImpulse (isGlobal: boolean, impl: any, vec: IVec3Like, rp: IVec3Like): void {
Expand Down Expand Up @@ -351,13 +350,14 @@ export function getShapeFlags (isTrigger: boolean): any {
return new PX.PxShapeFlags(flag);
}

// eslint-disable-next-line default-param-last
export function getShapeWorldBounds (shape: any, actor: any, i = 1.01, out: geometry.AABB): void {
if (USE_BYTEDANCE) {
const b3 = PX.RigidActorExt.getWorldBounds(shape, actor, i);
geometry.AABB.fromPoints(out, b3.minimum, b3.maximum);
geometry.AABB.fromPoints(out, b3.minimum as IVec3, b3.maximum as IVec3);
} else {
const b3 = shape.getWorldBounds(actor, i);
geometry.AABB.fromPoints(out, b3.minimum, b3.maximum);
geometry.AABB.fromPoints(out, b3.minimum as IVec3, b3.maximum as IVec3);
}
}

Expand Down Expand Up @@ -592,7 +592,7 @@ export function raycastAll (
const block = r[i];
const collider = getWrapShape<PhysXShape>(block.shapeData).collider;
const result = pool.add();
result._assign(block.position, block.distance, collider, block.normal);
result._assign(block.position as IVec3Like, block.distance as number, collider, block.normal as IVec3Like);
results.push(result);
}
return true;
Expand All @@ -619,7 +619,7 @@ export function raycastAll (
const block = blocks.get(i);
const collider = getWrapShape<PhysXShape>(block.getShape()).collider;
const result = pool.add();
result._assign(block.position, block.distance, collider, block.normal);
result._assign(block.position as IVec3Like, block.distance as number, collider, block.normal as IVec3Like);
results.push(result);
}
return true;
Expand Down Expand Up @@ -654,7 +654,7 @@ export function raycastClosest (world: PhysXWorld, worldRay: geometry.Ray, optio
);
if (block) {
const collider = getWrapShape<PhysXShape>(block.shapeData).collider;
result._assign(block.position, block.distance, collider, block.normal);
result._assign(block.position as IVec3Like, block.distance as number, collider, block.normal as IVec3Like);
return true;
}
} else {
Expand All @@ -674,7 +674,7 @@ export function raycastClosest (world: PhysXWorld, worldRay: geometry.Ray, optio
);
if (r) {
const collider = getWrapShape<PhysXShape>(block.getShape()).collider;
result._assign(block.position, block.distance, collider, block.normal);
result._assign(block.position as IVec3Like, block.distance as number, collider, block.normal as IVec3Like);
return true;
}
}
Expand Down Expand Up @@ -722,7 +722,7 @@ export function sweepAll (
const block = blocks.get(i);
const collider = getWrapShape<PhysXShape>(block.getShape()).collider;
const result = pool.add();
result._assign(block.position, block.distance, collider, block.normal);
result._assign(block.position as IVec3Like, block.distance as number, collider, block.normal as IVec3Like);
results.push(result);
}
return true;
Expand Down Expand Up @@ -768,7 +768,7 @@ export function sweepClosest (
);
if (r) {
const collider = getWrapShape<PhysXShape>(block.getShape()).collider;
result._assign(block.position, block.distance, collider, block.normal);
result._assign(block.position as IVec3Like, block.distance as number, collider, block.normal as IVec3Like);
return true;
}

Expand Down Expand Up @@ -846,15 +846,15 @@ export function initializeWorld (world: any): void {
*/
export function getContactPosition (pxContactOrOffset: any, out: IVec3Like, buf: any): void {
if (USE_BYTEDANCE) {
Vec3.fromArray(out, new Float32Array(buf, pxContactOrOffset, 3));
Vec3.fromArray(out, new Float32Array(buf as ArrayBufferLike, pxContactOrOffset as number, 3));
} else {
Vec3.copy(out, pxContactOrOffset.position);
}
}

export function getContactNormal (pxContactOrOffset: any, out: IVec3Like, buf: any): void {
if (USE_BYTEDANCE) {
Vec3.fromArray(out, new Float32Array(buf, (pxContactOrOffset as number) + 12, 3));
Vec3.fromArray(out, new Float32Array(buf as ArrayBufferLike, (pxContactOrOffset as number) + 12, 3));
} else {
Vec3.copy(out, pxContactOrOffset.normal);
}
Expand Down
Loading

0 comments on commit 75c6f6d

Please sign in to comment.