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

Refactor: opt memory for texture of font atlas #2287

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 55 additions & 8 deletions packages/core/src/2d/atlas/FontAtlas.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Vector2 } from "@galacean/engine-math";
import { Engine } from "../../Engine";
import { ReferResource } from "../../asset/ReferResource";
import { TextureFilterMode, TextureFormat } from "../../texture";
import { Texture2D } from "../../texture/Texture2D";
import { CharInfo } from "../text/CharInfo";

Expand All @@ -15,9 +16,17 @@ export class FontAtlas extends ReferResource {
private _curX: number = 1;
private _curY: number = 1;
private _nextY: number = 1;
private _preSize: number = 0;
private _curSize: number = 128;

constructor(engine: Engine) {
super(engine);
this.isGCIgnored = true;
const size = this._curSize;
const texture = new Texture2D(engine, size, size, TextureFormat.R8G8B8A8, false);
texture.filterMode = TextureFilterMode.Bilinear;
texture.isGCIgnored = true;
this.texture = texture;
}

uploadCharTexture(charInfo: CharInfo): boolean {
Expand All @@ -26,21 +35,30 @@ export class FontAtlas extends ReferResource {
const textureSize = texture.width;
const offsetWidth = width + space;
const offsetHeight = height + space;
if (1 + offsetWidth >= textureSize || 1 + offsetHeight >= textureSize) {
throw Error("The char fontSize is too large.");
}
const tempCurX = this._curX;
const tempNextY = this._nextY;

const endX = this._curX + offsetWidth;
const endX = tempCurX + offsetWidth;
if (endX >= textureSize) {
this._curX = space;
this._curY = this._nextY + space;
this._curX = space + (tempNextY > this._preSize ? 0 : this._preSize);
this._curY = tempNextY + space;
}
const endY = this._curY + offsetHeight;
if (endY > this._nextY) {
if (endY > tempNextY) {
this._nextY = endY;
}
// Exceed cur texture size.
if (endY >= textureSize) {
return false;
if (textureSize >= 1024) {
console.warn("Exceed max size.");
return false;
} else {
this._curX = textureSize + space;
this._curY = space;
this._nextY = space;
this._resizeTexture();
return this.uploadCharTexture(charInfo);
}
}

if (width > 0 && height > 0 && data) {
Expand Down Expand Up @@ -88,4 +106,33 @@ export class FontAtlas extends ReferResource {
this.texture = null;
this._charInfoMap = {};
}

private _resizeTexture(): void {
const curSize = (this._preSize = this._curSize);
const newSize = (this._curSize *= 2);
const resizeTexture = new Texture2D(this.engine, newSize, newSize, TextureFormat.R8G8B8A8, false);
resizeTexture.filterMode = TextureFilterMode.Bilinear;
resizeTexture.isGCIgnored = true;
const texture = this.texture;
this.texture = resizeTexture;

// Write data from texture to resize texture.
const data = new Uint8Array(curSize * curSize * 4);
texture.getPixelBuffer(data);
resizeTexture.setPixelBuffer(data, 0, 0, 0, curSize, curSize);
resizeTexture.generateMipmaps();

// Reset uvs
const map = this._charInfoMap;
for (const key in map) {
const charInfo = map[key];
const uvs = charInfo.uvs;
uvs[0].scale(0.5);
uvs[1].scale(0.5);
uvs[2].scale(0.5);
uvs[3].scale(0.5);
}

texture.destroy();
}
}
6 changes: 1 addition & 5 deletions packages/core/src/2d/text/SubFont.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AssetPromise } from "../../asset/AssetPromise";
import { ContentRestorer } from "../../asset/ContentRestorer";
import { Engine } from "../../Engine";
import { Texture2D, TextureFilterMode, TextureFormat } from "../../texture";
import { Texture2D } from "../../texture";
import { FontAtlas } from "../atlas/FontAtlas";
import { CharInfo } from "./CharInfo";
import { TextUtils } from "./TextUtils";
Expand Down Expand Up @@ -93,10 +93,6 @@ export class SubFont {
private _createFontAtlas(): FontAtlas {
const { _engine: engine } = this;
const fontAtlas = new FontAtlas(engine);
const texture = new Texture2D(engine, 256, 256, TextureFormat.R8G8B8A8, false);
texture.filterMode = TextureFilterMode.Bilinear;
fontAtlas.texture = texture;
fontAtlas.isGCIgnored = texture.isGCIgnored = true;
this._fontAtlases.push(fontAtlas);

const nativeFontString = this.nativeFontString;
Expand Down
Loading