Skip to content

Commit

Permalink
Merge pull request #16223 from minggo/v3.8.2-merge-v3.8.1
Browse files Browse the repository at this point in the history
V3.8.2 merge v3.8.1
  • Loading branch information
minggo authored Sep 8, 2023
2 parents 164aa24 + 5545986 commit dee8822
Show file tree
Hide file tree
Showing 57 changed files with 979 additions and 712 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/lib
lib/
.turbo/
/bin
/web.config
.idea
Expand Down
6 changes: 6 additions & 0 deletions cc.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,12 @@
"type": "boolean",
"value": false,
"internal": true
},
"CULL_MESHOPT": {
"comment": "An internal constant to indicate whether we cull the meshopt wasm module and asm.js module.",
"type": "boolean",
"value": true,
"internal": true
}
},

Expand Down
2 changes: 0 additions & 2 deletions cocos/2d/components/mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,11 +456,9 @@ export class Mask extends Component {

protected _removeMaskNode (): void {
if (this._sprite) {
this._sprite.destroy();
this._sprite = null;
}
if (this._graphics) {
this._graphics.destroy();
this._graphics = null;
}
}
Expand Down
160 changes: 153 additions & 7 deletions cocos/3d/assets/mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import { Asset } from '../../asset/assets/asset';
import { IDynamicGeometry } from '../../primitive/define';
import { BufferBlob } from '../misc/buffer-blob';
import { Skeleton } from './skeleton';
import { geometry, cclegacy, sys, warnID, Mat4, Quat, Vec3, assertIsTrue, murmurhash2_32_gc, errorID } from '../../core';
import { geometry, cclegacy, sys, warnID, Mat4, Quat, Vec3, assertIsTrue, murmurhash2_32_gc, errorID, halfToFloat } from '../../core';
import { RenderingSubMesh } from '../../asset/assets';
import {
Attribute, Device, Buffer, BufferInfo, AttributeName, BufferUsageBit, Feature, Format,
FormatInfos, FormatType, MemoryUsageBit, PrimitiveMode, getTypedArrayConstructor, DrawInfo, FormatInfo, deviceManager,
FormatInfos, FormatType, MemoryUsageBit, PrimitiveMode, getTypedArrayConstructor, DrawInfo, FormatInfo, deviceManager, FormatFeatureBit,
} from '../../gfx';
import { Morph } from './morph';
import { MorphRendering, createMorphRendering } from './morph-rendering';
Expand Down Expand Up @@ -409,6 +409,11 @@ export class Mesh extends Asset {
if (this.struct.encoded) { // decode mesh data
info = decodeMesh(info);
}
if (this.struct.quantized
&& !(deviceManager.gfxDevice.getFormatFeatures(Format.RGB16F) & FormatFeatureBit.VERTEX_ATTRIBUTE)) {
// dequantize mesh data
info = dequantizeMesh(info);
}

this._struct = info.struct;
this._data = info.data;
Expand Down Expand Up @@ -1507,11 +1512,6 @@ export function decodeMesh (mesh: Mesh.ICreateInfo): Mesh.ICreateInfo {
return mesh;
}

// decode the mesh
if (!MeshoptDecoder.supported) {
return mesh;
}

const res_checker = (res: number): void => {
if (res < 0) {
errorID(14204, res);
Expand Down Expand Up @@ -1581,4 +1581,150 @@ export function inflateMesh (mesh: Mesh.ICreateInfo): Mesh.ICreateInfo {
return mesh;
}

export function dequantizeMesh (mesh: Mesh.ICreateInfo): Mesh.ICreateInfo {
const struct = JSON.parse(JSON.stringify(mesh.struct)) as Mesh.IStruct;

const bufferBlob = new BufferBlob();
bufferBlob.setNextAlignment(0);

function transformVertex (
reader: ((offset: number) => number),
writer: ((offset: number, value: number) => void),
count: number,
components: number,
componentSize: number,
readerStride: number,
writerStride: number,
): void {
for (let i = 0; i < count; i++) {
for (let j = 0; j < components; j++) {
const inputOffset = readerStride * i + componentSize * j;
const outputOffset = writerStride * i + componentSize * j;
writer(outputOffset, reader(inputOffset));
}
}
}

function dequantizeHalf (
reader: ((offset: number) => number),
writer: ((offset: number, value: number) => void),
count: number,
components: number,
readerStride: number,
writerStride: number,
): void {
for (let i = 0; i < count; i++) {
for (let j = 0; j < components; j++) {
const inputOffset = readerStride * i + 2 * j;
const outputOffset = writerStride * i + 4 * j;
const value = halfToFloat(reader(inputOffset));
writer(outputOffset, value);
}
}
}

for (let i = 0; i < struct.vertexBundles.length; ++i) {
const bundle = struct.vertexBundles[i];
const view = bundle.view;
const attributes = bundle.attributes;
const oldAttributes = mesh.struct.vertexBundles[i].attributes;
const strides: number[] = [];
const dequantizes: boolean[] = [];
const readers: ((offset: number) => number)[] = [];
for (let j = 0; j < attributes.length; ++j) {
const attr = attributes[j];
const inputView = new DataView(mesh.data.buffer, view.offset + getOffset(oldAttributes, j));
const reader = getReader(inputView, attr.format);
let dequantize = true;
switch (attr.format) {
case Format.R16F:
attr.format = Format.R32F;
break;
case Format.RG16F:
attr.format = Format.RG32F;
break;
case Format.RGB16F:
attr.format = Format.RGB32F;
break;
case Format.RGBA16F:
attr.format = Format.RGBA32F;
break;
default:
dequantize = false;
break;
}
strides.push(FormatInfos[attr.format].size);
dequantizes.push(dequantize);
readers.push(reader!);
}
const netStride = strides.reduce((acc, cur) => acc + cur, 0);
const newBuffer = new Uint8Array(netStride * view.count);
for (let j = 0; j < attributes.length; ++j) {
const attribute = attributes[j];
const reader = readers[j];
const outputView = new DataView(newBuffer.buffer, getOffset(attributes, j));
const writer = getWriter(outputView, attribute.format)!;
const dequantize = dequantizes[j];
const formatInfo = FormatInfos[attribute.format];
if (dequantize) {
dequantizeHalf(
reader,
writer,
view.count,
formatInfo.count,
view.stride,
netStride,
);
} else {
transformVertex(
reader,
writer,
view.count,
formatInfo.count,
formatInfo.size / formatInfo.count,
view.stride,
netStride,
);
}
}

bufferBlob.setNextAlignment(netStride);
const newView: Mesh.IBufferView = {
offset: bufferBlob.getLength(),
length: newBuffer.byteLength,
count: view.count,
stride: netStride,
};
bundle.view = newView;
bufferBlob.addBuffer(newBuffer);
}

// dump index buffer
for (const primitive of struct.primitives) {
if (primitive.indexView === undefined) {
continue;
}
const view = primitive.indexView;
const buffer = new Uint8Array(mesh.data.buffer, view.offset, view.length);
bufferBlob.setNextAlignment(view.stride);
const newView: Mesh.IBufferView = {
offset: bufferBlob.getLength(),
length: buffer.byteLength,
count: view.count,
stride: view.stride,
};
primitive.indexView = newView;
bufferBlob.addBuffer(buffer);
}

const data = new Uint8Array(bufferBlob.getCombined());

struct.quantized = false;

return {
struct,
data,
};
}

// function get
1 change: 1 addition & 0 deletions cocos/3d/framework/mesh-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,7 @@ export class MeshRenderer extends ModelRenderer {
if (this._mesh) {
const meshStruct = this._mesh.struct;
this._model.createBoundingShape(meshStruct.minPosition, meshStruct.maxPosition);
this._model.updateWorldBound();
}
// Initialize lighting map before model initializing
// because the lighting map will influence the model's shader
Expand Down
6 changes: 4 additions & 2 deletions cocos/3d/misc/mesh-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import { WASM_SUPPORT_MODE } from 'internal:constants';
import { CULL_MESHOPT, WASM_SUPPORT_MODE } from 'internal:constants';
import { ensureWasmModuleReady, instantiateWasm } from 'pal/wasm';

import { sys, logID } from '../../core';
Expand Down Expand Up @@ -90,4 +90,6 @@ export function InitDecoder (): Promise<void> {
}));
}

game.onPostInfrastructureInitDelegate.add(InitDecoder);
if (!CULL_MESHOPT) {
game.onPostInfrastructureInitDelegate.add(InitDecoder);
}
6 changes: 3 additions & 3 deletions cocos/misc/camera-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ export class Camera extends Component {
*/
@type(FOVAxis)
@displayOrder(7)
@visible(function (this: Camera): boolean {
@visible(function visible (this: Camera): boolean {
return this._projection === ProjectionType.PERSPECTIVE;
})
@tooltip('i18n:camera.fov_axis')
Expand Down Expand Up @@ -354,10 +354,10 @@ export class Camera extends Component {
* @zh 正交模式下的相机视角高度。
*/
@displayOrder(9)
@visible(function (this: Camera): boolean {
@visible(function visible (this: Camera): boolean {
return this._projection === ProjectionType.ORTHO;
})
@rangeMin(1)
@rangeMin(1e-6)
@tooltip('i18n:camera.ortho_height')
get orthoHeight (): number {
return this._orthoHeight;
Expand Down
3 changes: 2 additions & 1 deletion cocos/physics-2d/box2d-wasm/instantiated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export const B2 = {} as any;

export function getImplPtr (wasmObject: any): number {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return (wasmObject).$$.ptr;
if (!wasmObject) return 0;
return (wasmObject).$$.ptr as number;
}

/**
Expand Down
8 changes: 5 additions & 3 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 } from '../instantiated';
import { B2, addImplPtrReference, addImplPtrReferenceWASM, getImplPtr, removeImplPtrReference, removeImplPtrReferenceWASM } from '../instantiated';
import { IJoint2D } from '../../spec/i-physics-joint';
import { Joint2D, PhysicsSystem2D, RigidBody2D } from '../../framework';
import { B2PhysicsWorld } from '../physics-world';
Expand Down Expand Up @@ -54,7 +54,7 @@ export class B2Joint implements IJoint2D {
}

onDisable (): void {
PhysicsSystem2D.instance._callAfterStep(this, this._destroy);
PhysicsSystem2D.instance._callAfterStep(this, this.destroy);
}

// need init after body and connected body init
Expand Down Expand Up @@ -106,9 +106,11 @@ export class B2Joint implements IJoint2D {
this._inited = true;
}

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

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

this._b2joint = null;
Expand Down
2 changes: 1 addition & 1 deletion cocos/physics-2d/box2d-wasm/joints/mouse-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class B2MouseJoint extends B2Joint implements IMouseJoint {
}

onTouchEnd (event: Touch): void {
this._destroy();
this.destroy();
this._isTouched = false;
}

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

import { B2 } from './instantiated';
import { B2, getTSObjectFromWASMObject, getTSObjectFromWASMObjectPtr } from './instantiated';
import { IRigidBody2D } from '../spec/i-rigid-body';
import { RigidBody2D } from '../framework/components/rigid-body-2d';
import { PhysicsSystem2D } from '../framework/physics-system';
Expand All @@ -31,8 +31,10 @@ import { Vec2, toRadian, Vec3, Quat, IVec2Like, toDegree, TWO_PI, HALF_PI } from
import { PHYSICS_2D_PTM_RATIO, ERigidBody2DType } from '../framework/physics-types';

import { Node } from '../../scene-graph/node';
import { Collider2D } from '../framework';
import { Collider2D, Joint2D } from '../framework';
import { NodeEventType } from '../../scene-graph/node-event';
import { B2Shape2D } from './shapes/shape-2d';
import { B2Joint } from './joints/joint-2d';

const tempVec3 = new Vec3();
const tempVec2_1 = { x: 0, y: 0 };//new B2.Vec2(0, 0);
Expand Down Expand Up @@ -114,6 +116,29 @@ export class B2RigidBody2D implements IRigidBody2D {
_destroy (): void {
if (!this._inited) return;

//collect all fixtures attached to this rigid body and process them
const fixtureList = this.impl?.GetFixtureList();
if (fixtureList) {
let shapeTSObj = getTSObjectFromWASMObject<B2Shape2D>(fixtureList);
while (shapeTSObj && shapeTSObj.impl) {
shapeTSObj.destroy();
const nextFixture = fixtureList.GetNext();
shapeTSObj = getTSObjectFromWASMObject<B2Shape2D>(nextFixture);
}
}

//collect all joints attached to this rigid body and process them
const jointListPtr = this.impl?.GetJointList();
if (jointListPtr) {
let jointWASMPtr = B2.JointEdgeGetJoint(jointListPtr) as number;
let jointTSObj = getTSObjectFromWASMObjectPtr<B2Joint>(jointWASMPtr);
while (jointTSObj) {
jointTSObj.destroy();
jointWASMPtr = B2.JointEdgeGetNext(jointListPtr) as number;
jointTSObj = getTSObjectFromWASMObjectPtr<B2Joint>(jointWASMPtr);
}
}

(PhysicsSystem2D.instance.physicsWorld as B2PhysicsWorld).removeBody(this);

this._inited = false;
Expand Down
6 changes: 3 additions & 3 deletions cocos/physics-2d/box2d-wasm/shapes/shape-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class B2Shape2D implements IBaseShape {
}

onDisable (): void {
PhysicsSystem2D.instance._callAfterStep(this, this._destroy);
PhysicsSystem2D.instance._callAfterStep(this, this.destroy);
}

start (): void {
Expand All @@ -92,7 +92,7 @@ export class B2Shape2D implements IBaseShape {
}

apply (): void {
this._destroy();
this.destroy();
if (this.collider.enabledInHierarchy) {
this._init();
}
Expand Down Expand Up @@ -200,7 +200,7 @@ export class B2Shape2D implements IBaseShape {
this._inited = true;
}

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

const fixtures = this._fixtures;
Expand Down
Loading

0 comments on commit dee8822

Please sign in to comment.