Skip to content

Commit

Permalink
Optimize code size for draw-batch.ts and batcher-2d.ts, remove needle…
Browse files Browse the repository at this point in the history
…ss setter/getter. (#18238)

* Optimize code size for draw-batch.ts and batcher-2d.ts, remove needless setter/getter.

* Add comment for changing getter to function
  • Loading branch information
dumganhar authored Jan 24, 2025
1 parent 01040a3 commit 8de922e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 31 deletions.
13 changes: 10 additions & 3 deletions cocos/2d/renderer/batcher-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,7 @@ export class Batcher2D implements IBatcher {
// by legacyCC.director.root.batcher2D._releaseDescriptorSetCache
/**
* @engineInternal
* @mangle
*/
public _releaseDescriptorSetCache (textureHash: number | Texture | null, sampler: Sampler | null = null): void {
if (JSB) {
Expand Down Expand Up @@ -962,6 +963,7 @@ export class Batcher2D implements IBatcher {
}
}

/** @mangle */
class LocalDescriptorSet {
private _descriptorSet: DescriptorSet | null = null;
private _transform: Node | null = null;
Expand All @@ -971,7 +973,11 @@ class LocalDescriptorSet {
private _transformUpdate = true;
private declare _localData: Float32Array | null;

public get descriptorSet (): DescriptorSet | null {
// NOTE: Internal modules should avoid using getter/setter accessors since we're using babel to convert TS to JS
// and terser minifier could not handle the getter/setter generated JS code correctly.
// See the issue: https://github.com/terser/terser/issues/322
// Change get descriptorSet() to getDescriptorSet() in v3.8.6.
public getDescriptorSet (): DescriptorSet | null {
return this._descriptorSet;
}

Expand Down Expand Up @@ -1063,6 +1069,7 @@ class LocalDescriptorSet {
}
}

/** @mangle */
class DescriptorSetCache {
private _descriptorSetCache = new Map<number, DescriptorSet>();
private _dsCacheHashByTexture = new Map<number, number>();
Expand All @@ -1080,13 +1087,13 @@ class DescriptorSetCache {
for (let i = 0, len = caches.length; i < len; i++) {
const cache: LocalDescriptorSet = caches[i];
if (cache.equals(batch.useLocalData, batch.textureHash, batch.samplerHash)) {
return cache.descriptorSet!;
return cache.getDescriptorSet()!;
}
}
const localDs = this._localCachePool.alloc();
localDs.initialize(batch);
this._localDescriptorSetCache.push(localDs);
return localDs.descriptorSet!;
return localDs.getDescriptorSet()!;
} else {
const hash = batch.textureHash ^ batch.samplerHash;
if (this._descriptorSetCache.has(hash)) {
Expand Down
35 changes: 7 additions & 28 deletions cocos/2d/renderer/draw-batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,9 @@ import { IBatcher } from './i-batcher';
import type { Root } from '../../root';

const UI_VIS_FLAG = Layers.Enum.NONE | Layers.Enum.UI_3D;
export class DrawBatch2D {
public get inputAssembler (): InputAssembler | null {
return this._inputAssembler;
}

public set inputAssembler (ia: InputAssembler | null) {
this._inputAssembler = ia;
}

public get descriptorSet (): DescriptorSet | null {
return this._descriptorSet;
}

public set descriptorSet (ds: DescriptorSet | null) {
this._descriptorSet = ds;
}

public get visFlags (): number {
return this._visFlags;
}
public set visFlags (vis) {
this._visFlags = vis;
}

/** @mangle */
export class DrawBatch2D {
get passes (): Pass[] {
return this._passes;
}
Expand All @@ -77,9 +56,9 @@ export class DrawBatch2D {
public samplerHash = 0;
private _passes: Pass[] = [];
private _shaders: Shader[] = [];
private _visFlags: number = UI_VIS_FLAG;
private _inputAssembler: InputAssembler | null = null;
private _descriptorSet: DescriptorSet | null = null;
public visFlags: number = UI_VIS_FLAG;
public inputAssembler: InputAssembler | null = null;
public descriptorSet: DescriptorSet | null = null;
//private declare _nativeObj: any;

public destroy (ui: IBatcher): void {
Expand All @@ -88,8 +67,8 @@ export class DrawBatch2D {

public clear (): void {
// this.bufferBatch = null;
this._inputAssembler = null;
this._descriptorSet = null;
this.inputAssembler = null;
this.descriptorSet = null;
// this.camera = null;
this.texture = null;
this.sampler = null;
Expand Down

0 comments on commit 8de922e

Please sign in to comment.