Skip to content

Commit

Permalink
[v3.8.6] Optimize code size again. (#18197)
Browse files Browse the repository at this point in the history
* [v3.8.6] Optimize code size again.

* More mangled properties.

* Mark SpriteFrame._calculateUV as engine internal and mangle.
  • Loading branch information
dumganhar authored Jan 17, 2025
1 parent 249d77d commit 1d870a7
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 49 deletions.
30 changes: 21 additions & 9 deletions cocos/2d/assets/sprite-frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -946,23 +946,27 @@ export class SpriteFrame extends Asset {
/**
* Calculate UV for sliced
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
* @engineInternal
* @mangle
*/
public _calculateSlicedUV (): void {
const rect = this._rect;
// const texture = this._getCalculateTarget()!;
const tex = this.texture;
const self = this;
const rect = self._rect;
// const texture = self._getCalculateTarget()!;
const tex = self.texture;
const capInsets = self._capInsets;
const atlasWidth = tex.width;
const atlasHeight = tex.height;
const leftWidth = this._capInsets[INSET_LEFT];
const rightWidth = this._capInsets[INSET_RIGHT];
const leftWidth = capInsets[INSET_LEFT];
const rightWidth = capInsets[INSET_RIGHT];
const centerWidth = rect.width - leftWidth - rightWidth;
const topHeight = this._capInsets[INSET_TOP];
const bottomHeight = this._capInsets[INSET_BOTTOM];
const topHeight = capInsets[INSET_TOP];
const bottomHeight = capInsets[INSET_BOTTOM];
const centerHeight = rect.height - topHeight - bottomHeight;

const uvSliced = this.uvSliced;
const uvSliced = self.uvSliced;
uvSliced.length = 0;
if (this._rotated) {
if (self._rotated) {
temp_uvs[0].u = rect.x / atlasWidth;
temp_uvs[1].u = (rect.x + bottomHeight) / atlasWidth;
temp_uvs[2].u = (rect.x + bottomHeight + centerHeight) / atlasWidth;
Expand Down Expand Up @@ -1012,6 +1016,8 @@ export class SpriteFrame extends Asset {
/**
* Calculate UV
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
* @engineInternal
* @mangle
*/
public _calculateUV (): void {
const arrayFill = js.array.fillItems;
Expand Down Expand Up @@ -1126,6 +1132,8 @@ export class SpriteFrame extends Asset {

/**
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
* @engineInternal
* @mangle
*/
public _setDynamicAtlasFrame (frame): void {
if (!frame) return;
Expand All @@ -1144,6 +1152,8 @@ export class SpriteFrame extends Asset {

/**
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
* @engineInternal
* @mangle
*/
public _resetDynamicAtlasFrame (): void {
if (!this._original) return;
Expand All @@ -1156,6 +1166,8 @@ export class SpriteFrame extends Asset {

/**
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
* @engineInternal
* @mangle
*/
public _checkPackable (): void {
const dynamicAtlas = dynamicAtlasManager;
Expand Down
4 changes: 2 additions & 2 deletions cocos/3d/assets/mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1530,7 +1530,7 @@ export function decodeMesh (mesh: Mesh.ICreateInfo): Mesh.ICreateInfo {
const bound = view.count * view.stride;
const buffer = new Uint8Array(bound);
const vertex = new Uint8Array(mesh.data.buffer, view.offset, view.length);
const res = MeshoptDecoder.decodeVertexBuffer(buffer, view.count, view.stride, vertex) as number;
const res = MeshoptDecoder.decodeVertexBuffer(buffer, view.count, view.stride, vertex);
res_checker(res);

bufferBlob.setNextAlignment(view.stride);
Expand All @@ -1553,7 +1553,7 @@ export function decodeMesh (mesh: Mesh.ICreateInfo): Mesh.ICreateInfo {
const bound = view.count * view.stride;
const buffer = new Uint8Array(bound);
const index = new Uint8Array(mesh.data.buffer, view.offset, view.length);
const res = MeshoptDecoder.decodeIndexBuffer(buffer, view.count, view.stride, index) as number;
const res = MeshoptDecoder.decodeIndexBuffer(buffer, view.count, view.stride, index);
res_checker(res);

bufferBlob.setNextAlignment(view.stride);
Expand Down
1 change: 1 addition & 0 deletions cocos/3d/misc/buffer-blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
THE SOFTWARE.
*/

/** @mangle */
export class BufferBlob {
private _arrayBufferOrPaddings: Array<ArrayBuffer | number> = [];
private _length = 0;
Expand Down
14 changes: 13 additions & 1 deletion cocos/3d/misc/mesh-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,19 @@ import { sys, logID, error } from '../../core';
import { game } from '../../game';
import { NativeCodeBundleMode } from '../../misc/webassembly-support';

export const MeshoptDecoder = {} as any;
/** @mangle */
interface IMeshoptDecoder {
supported: boolean;
ready: Promise<void>;
decodeVertexBuffer: (target: Uint8Array, count: number, size: number, source: Uint8Array) => number;
decodeIndexBuffer: (target: Uint8Array, count: number, size: number, source: Uint8Array) => number;
decodeIndexSequence: (target: Uint8Array, count: number, size: number, source: Uint8Array) => number;
decodeGltfBuffer: (target: Uint8Array, count: number, size: number, source: Uint8Array, mode: string) => void;
useWorkers: boolean;
decodeGltfBufferAsync: (count: number, size: number, source: Uint8Array, mode: string) => Promise<Uint8Array>;
}

export const MeshoptDecoder: IMeshoptDecoder = {} as any;

function initDecoderASM (asm_factory: any): Promise<void> {
return Promise.all([asm_factory.ready]).then(() => {
Expand Down
10 changes: 5 additions & 5 deletions cocos/animation/value-proxy-factories/uniform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { SpriteFrame } from '../../2d/assets/sprite-frame';
import { TextureBase } from '../../asset/assets/texture-base';
import { deviceManager, Type } from '../../gfx';
import { Pass } from '../../render-scene/core/pass';
import { getDefaultFromType, getStringFromType } from '../../render-scene/core/pass-utils';
import { getBindingFromHandle, getDefaultFromType, getStringFromType, getTypeFromHandle, MaterialProperty } from '../../render-scene/core/pass-utils';
import { IValueProxy, IValueProxyFactory } from '../value-proxy';
import { warn, warnID } from '../../core';

Expand Down Expand Up @@ -97,7 +97,7 @@ export class UniformProxyFactory implements IValueProxyFactory {
return undefined;
}

const type = Pass.getTypeFromHandle(handle);
const type = getTypeFromHandle(handle);
if (type < Type.SAMPLER1D) {
const realHandle = channelIndex === undefined ? handle : pass.getHandle(uniformName, channelIndex, Type.FLOAT);
if (!realHandle) {
Expand All @@ -106,18 +106,18 @@ export class UniformProxyFactory implements IValueProxyFactory {
}
if (isUniformArray(pass, uniformName)) {
return {
set: (value: any): void => {
set: (value: MaterialProperty[]): void => {
pass.setUniformArray(realHandle, value);
},
};
}
return {
set: (value: any): void => {
set: (value: MaterialProperty): void => {
pass.setUniform(realHandle, value);
},
};
} else {
const binding = Pass.getBindingFromHandle(handle);
const binding = getBindingFromHandle(handle);
const prop = pass.properties[uniformName];
const texName = prop && prop.value ? `${prop.value as string}${getStringFromType(prop.type)}` : getDefaultFromType(prop.type) as string;
let dftTex = builtinResMgr.get<TextureBase>(texName);
Expand Down
4 changes: 2 additions & 2 deletions cocos/asset/assets/material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { EffectAsset } from './effect-asset';
import { Texture, Type } from '../../gfx';
import { TextureBase } from './texture-base';
import { IPassInfoFull, Pass, PassOverrides } from '../../render-scene/core/pass';
import { MacroRecord, MaterialProperty } from '../../render-scene/core/pass-utils';
import { getTypeFromHandle, MacroRecord, MaterialProperty } from '../../render-scene/core/pass-utils';
import { Color, warnID, Vec4, cclegacy } from '../../core';
import { SRGBToLinear } from '../../rendering/pipeline-funcs';
import { Renderer } from '../../misc/renderer';
Expand Down Expand Up @@ -474,7 +474,7 @@ export class Material extends Asset {
protected _uploadProperty (pass: Pass, name: string, val: MaterialPropertyFull | MaterialPropertyFull[]): boolean {
const handle = pass.getHandle(name);
if (!handle) { return false; }
const type = Pass.getTypeFromHandle(handle);
const type = getTypeFromHandle(handle);
if (type < (Type.SAMPLER1D as number)) {
if (Array.isArray(val)) {
pass.setUniformArray(handle, val as MaterialProperty[]);
Expand Down
7 changes: 5 additions & 2 deletions cocos/core/platform/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@
THE SOFTWARE.
*/

import { BYTEDANCE, WECHAT, WECHAT_MINI_PROGRAM } from 'internal:constants';

import { systemInfo } from 'pal/system-info';
import { screenAdapter } from 'pal/screen-adapter';
import { BYTEDANCE, WECHAT, WECHAT_MINI_PROGRAM } from 'internal:constants';
import { legacyCC } from '../global-exports';
import { Rect } from '../math/rect';
import { Vec2 } from '../math/vec2';
import { warnID, log } from './debug';
import { NetworkType, Language, OS, Platform, BrowserType, Feature } from '../../../pal/system-info/enum-type';
import { screen } from './screen';
import { macro } from './macro';
import type { View } from '../../ui/view';

// TODO: the type Storage conflicts with the one on OH platform.
type Storage = any;
Expand Down Expand Up @@ -265,6 +267,7 @@ export const sys = {

/**
* @engineInternal
* @mangle
*/
__isWebIOS14OrIPadOS14Env: false,

Expand Down Expand Up @@ -366,7 +369,7 @@ export const sys = {
* @return {Rect}
*/
getSafeAreaRect (symmetric: boolean = true): Rect {
const locView = legacyCC.view;
const locView: View = legacyCC.view;
const edge = screenAdapter.safeAreaEdge;
if (symmetric) {
if (screenAdapter.orientation === macro.ORIENTATION_PORTRAIT) {
Expand Down
10 changes: 6 additions & 4 deletions cocos/core/utils/js-typed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,11 @@ export function copyAllProperties (source: any, target: any, excepts: Array<stri
*/
export function addon (object?: Record<string | number, any>, ...sources: any[]): Record<string | number, any> {
object = object || {};
for (const source of sources) {
for (let i = 0; i < sources.length; ++i) {
const source = sources[i];
if (source) {
if (typeof source !== 'object') {
errorID(5402, source);
errorID(5402, source as string);
continue;
}
for (const name in source) {
Expand All @@ -486,10 +487,11 @@ export function addon (object?: Record<string | number, any>, ...sources: any[])
*/
export function mixin (object?: Record<string | number, any>, ...sources: any[]): Record<string | number, any> {
object = object || {};
for (const source of sources) {
for (let i = 0; i < sources.length; ++i) {
const source = sources[i];
if (source) {
if (typeof source !== 'object') {
errorID(5403, source);
errorID(5403, source as string);
continue;
}
for (const name in source) {
Expand Down
14 changes: 7 additions & 7 deletions cocos/particle/renderer/particle-system-renderer-gpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { Component } from '../../scene-graph';
import { AttributeName, Format, Attribute, API, deviceManager, FormatInfos } from '../../gfx';
import { Mat4, Vec2, Vec4, Quat, Vec3, warn } from '../../core';
import { MaterialInstance, IMaterialInstanceInfo } from '../../render-scene/core/material-instance';
import { MacroRecord } from '../../render-scene/core/pass-utils';
import { getBindingFromHandle, MacroRecord } from '../../render-scene/core/pass-utils';
import { ParticleAlignmentSpace, ParticleRenderMode, ParticleSpace } from '../enum';
import { Particle, IParticleModule } from '../particle';
import { packGradientRange } from '../animator/gradient-range';
Expand Down Expand Up @@ -396,7 +396,7 @@ export default class ParticleSystemRendererGPU extends ParticleSystemRendererBas
this._forceTexture = packed.texture;
this._forceData = packed.texdata;
const handle = pass.getHandle('force_over_time_tex0');
const binding = Pass.getBindingFromHandle(handle);
const binding = getBindingFromHandle(handle);
pass.bindSampler(binding, this._forceTexture.getGFXSampler()!);
pass.bindTexture(binding, this._forceTexture.getGFXTexture()!);
const spaceHandle = pass.getHandle('u_force_space');
Expand All @@ -422,7 +422,7 @@ export default class ParticleSystemRendererGPU extends ParticleSystemRendererBas
this._velocityTexture = packed.texture;
this._velocityData = packed.texdata;
const handle = pass.getHandle('velocity_over_time_tex0');
const binding = Pass.getBindingFromHandle(handle);
const binding = getBindingFromHandle(handle);
pass.bindSampler(binding, this._velocityTexture.getGFXSampler()!);
pass.bindTexture(binding, this._velocityTexture.getGFXTexture()!);
const spaceHandle = pass.getHandle('u_velocity_space');
Expand All @@ -440,7 +440,7 @@ export default class ParticleSystemRendererGPU extends ParticleSystemRendererBas
this._colorTexture = packed.texture;
this._colorData = packed.texdata;
const handle = pass.getHandle('color_over_time_tex0');
const binding = Pass.getBindingFromHandle(handle);
const binding = getBindingFromHandle(handle);
pass.bindSampler(binding, this._colorTexture.getGFXSampler()!);
pass.bindTexture(binding, this._colorTexture.getGFXTexture()!);
const modeHandle = pass.getHandle('u_color_mode');
Expand All @@ -463,7 +463,7 @@ export default class ParticleSystemRendererGPU extends ParticleSystemRendererBas
this._rotationData = packed.texdata;
if (this._rotationTexture) {
const handle = pass.getHandle('rotation_over_time_tex0');
const binding = Pass.getBindingFromHandle(handle);
const binding = getBindingFromHandle(handle);
pass.bindSampler(binding, this._rotationTexture.getGFXSampler()!);
pass.bindTexture(binding, this._rotationTexture.getGFXTexture()!);
const modeHandle = pass.getHandle('u_rotation_mode');
Expand All @@ -486,7 +486,7 @@ export default class ParticleSystemRendererGPU extends ParticleSystemRendererBas
this._sizeData = packed.texdata;
if (this._sizeTexture) {
const handle = pass.getHandle('size_over_time_tex0');
const binding = Pass.getBindingFromHandle(handle);
const binding = getBindingFromHandle(handle);
pass.bindSampler(binding, this._sizeTexture.getGFXSampler()!);
pass.bindTexture(binding, this._sizeTexture.getGFXTexture()!);
const modeHandle = pass.getHandle('u_size_mode');
Expand All @@ -504,7 +504,7 @@ export default class ParticleSystemRendererGPU extends ParticleSystemRendererBas
this._animTexture = packed.texture;
this._animData = packed.texdata;
const handle = pass.getHandle('texture_animation_tex0');
const binding = Pass.getBindingFromHandle(handle);
const binding = getBindingFromHandle(handle);
pass.bindSampler(binding, this._animTexture.getGFXSampler()!);
pass.bindTexture(binding, this._animTexture.getGFXTexture()!);
const infoHandle = pass.getHandle('u_anim_info');
Expand Down
32 changes: 16 additions & 16 deletions cocos/render-scene/core/pass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export class Pass {
public getBinding (name: string): number {
const handle = this.getHandle(name);
if (!handle) { return -1; }
return Pass.getBindingFromHandle(handle);
return getBindingFromHandle(handle);
}

/**
Expand All @@ -266,9 +266,9 @@ export class Pass {
* @param value New value
*/
public setUniform (handle: number, value: MaterialProperty): void {
const binding = Pass.getBindingFromHandle(handle);
const type = Pass.getTypeFromHandle(handle);
const ofs = Pass.getOffsetFromHandle(handle);
const binding = getBindingFromHandle(handle);
const type = getTypeFromHandle(handle);
const ofs = getOffsetFromHandle(handle);
const block = this._getBlockView(type, binding);
if (DEBUG) {
const validator = type2validator[type];
Expand All @@ -285,9 +285,9 @@ export class Pass {
* @param out The output property to store the result
*/
public getUniform<T extends MaterialProperty> (handle: number, out: T): T {
const binding = Pass.getBindingFromHandle(handle);
const type = Pass.getTypeFromHandle(handle);
const ofs = Pass.getOffsetFromHandle(handle);
const binding = getBindingFromHandle(handle);
const type = getTypeFromHandle(handle);
const ofs = getOffsetFromHandle(handle);
const block = this._getBlockView(type, binding);
return type2reader[type](block, out, ofs) as T;
}
Expand All @@ -299,11 +299,11 @@ export class Pass {
* @param value New value
*/
public setUniformArray (handle: number, value: MaterialProperty[]): void {
const binding = Pass.getBindingFromHandle(handle);
const type = Pass.getTypeFromHandle(handle);
const binding = getBindingFromHandle(handle);
const type = getTypeFromHandle(handle);
const stride = GetTypeSize(type) >> 2;
const block = this._getBlockView(type, binding);
let ofs = Pass.getOffsetFromHandle(handle);
let ofs = getOffsetFromHandle(handle);
for (let i = 0; i < value.length; i++, ofs += stride) {
if (value[i] === null) { continue; }
type2writer[type](block, value[i], ofs);
Expand Down Expand Up @@ -408,10 +408,10 @@ export class Pass {
public resetUniform (name: string): void {
const handle = this.getHandle(name);
if (!handle) { return; }
const type = Pass.getTypeFromHandle(handle);
const binding = Pass.getBindingFromHandle(handle);
const ofs = Pass.getOffsetFromHandle(handle);
const count = Pass.getCountFromHandle(handle);
const type = getTypeFromHandle(handle);
const binding = getBindingFromHandle(handle);
const ofs = getOffsetFromHandle(handle);
const count = getCountFromHandle(handle);
const block = this._getBlockView(type, binding);
const info = this._properties[name];
const givenDefault = info && info.value;
Expand All @@ -428,8 +428,8 @@ export class Pass {
public resetTexture (name: string, index?: number): void {
const handle = this.getHandle(name);
if (!handle) { return; }
const type = Pass.getTypeFromHandle(handle);
const binding = Pass.getBindingFromHandle(handle);
const type = getTypeFromHandle(handle);
const binding = getBindingFromHandle(handle);
const info = this._properties[name];
const value = info && info.value;
let textureBase: TextureBase;
Expand Down
2 changes: 1 addition & 1 deletion cocos/render-scene/scene/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@ export class Camera {
* Editor specific gizmo camera logic
* @internal
*/
public syncCameraEditor (camera): void {
public syncCameraEditor (camera: Camera): void {
if (EDITOR) {
this.position = camera.position;
this.forward = camera.forward;
Expand Down
1 change: 1 addition & 0 deletions cocos/ui/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ export class View extends Eventify(System) {
/**
* Convert location in Cocos screen coordinate to location in UI space
* @engineInternal
* @mangle
*/
public _convertToUISpace (point: Vec2): void {
const viewport = this._viewportRect;
Expand Down

0 comments on commit 1d870a7

Please sign in to comment.