From 49fe53e48dbe386480b30fbf872bbd3711120f64 Mon Sep 17 00:00:00 2001 From: billsonnn Date: Sat, 30 Mar 2024 17:41:12 -0400 Subject: [PATCH] Add TexturePool --- packages/utils/src/GetTexturePool.ts | 5 ++ packages/utils/src/TexturePool.ts | 105 +++++++++++++++++++++++++++ packages/utils/src/index.ts | 2 + src/DevTools.ts | 16 ++-- 4 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 packages/utils/src/GetTexturePool.ts create mode 100644 packages/utils/src/TexturePool.ts diff --git a/packages/utils/src/GetTexturePool.ts b/packages/utils/src/GetTexturePool.ts new file mode 100644 index 00000000..46e84fe1 --- /dev/null +++ b/packages/utils/src/GetTexturePool.ts @@ -0,0 +1,5 @@ +import { TexturePool } from './TexturePool'; + +const texturePool = new TexturePool(); + +export const GetTexturePool = () => texturePool; diff --git a/packages/utils/src/TexturePool.ts b/packages/utils/src/TexturePool.ts new file mode 100644 index 00000000..128679c3 --- /dev/null +++ b/packages/utils/src/TexturePool.ts @@ -0,0 +1,105 @@ +import { NitroLogger, TextureUtils } from '@nitrots/utils'; +import { Texture } from 'pixi.js'; + +export class TexturePool +{ + private static MAX_IDLE: number = 3600; + + private _textures: { [index: string]: { [index: string]: Texture[] } } = {}; + private _totalTextures: number = 0; + private _runCount: number = 0; + + public getTotalTextures(): number + { + let total = 0; + + for(const width in this._textures) + { + for(const height in this._textures[width]) + { + total += this._textures[width][height].length; + } + } + + this._totalTextures = total; + + return this._totalTextures; + } + + public getTexture(width: number, height: number): Texture + { + if(!this._textures[width]) this._textures[width] = {}; + + if(!this._textures[width][height]) this._textures[width][height] = []; + + if(this._textures[width][height].length) + { + const texture = this._textures[width][height].shift(); + + if(texture) + { + this._totalTextures--; + + return texture; + } + } + + return TextureUtils.createRenderTexture(width, height); + } + + public putTexture(texture: Texture) + { + if(!texture) return; + + if(!this._textures[texture.width]) this._textures[texture.width] = {}; + + if(!this._textures[texture.width][texture.height]) this._textures[texture.width][texture.height] = []; + + //@ts-ignore + delete texture.source.hitMap; + + this._textures[texture.width][texture.height].push(texture); + + this._totalTextures++; + } + + public run(): void + { + this._runCount++; + + if(!this._totalTextures) return; + + for(const width in this._textures) + { + for(const height in this._textures[width]) + { + const textures = this._textures[width][height]; + + for(let i = textures.length - 1; i >= 0; i--) + { + const texture = textures[i]; + const source = texture.source; + + if((source._touched > -1) && (this._runCount - source._touched) > TexturePool.MAX_IDLE) + { + //@ts-ignore + delete texture.source.hitMap; + + if(!source.destroyed) texture.destroy(true); + + this._textures[texture.width][texture.height].splice(i, 1); + + this._totalTextures--; + + NitroLogger.log(`[TexturePool] Texture disposed: ${texture.width}x${texture.height}`); + } + } + } + } + } + + public get textures(): { [index: string]: { [index: string]: Texture[] } } + { + return this._textures; + } +} diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 1209a918..a25613d3 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -7,6 +7,7 @@ export * from './FurniId'; export * from './GetPixi'; export * from './GetRenderer'; export * from './GetStage'; +export * from './GetTexturePool'; export * from './GetTicker'; export * from './GetTickerFPS'; export * from './GetTickerTime'; @@ -23,6 +24,7 @@ export * from './Node3D'; export * from './NumberBank'; export * from './PointMath'; export * from './RoomId'; +export * from './TexturePool'; export * from './TextureUtils'; export * from './Vector3d'; export * from './filters'; diff --git a/src/DevTools.ts b/src/DevTools.ts index ec41fb76..ad972b86 100644 --- a/src/DevTools.ts +++ b/src/DevTools.ts @@ -1,6 +1,6 @@ import { GetRoomEngine, RoomEngine } from '@nitrots/room'; -import { GetRenderer } from '@nitrots/utils'; -import { TextureSource } from 'pixi.js'; +import { GetRenderer, GetTexturePool } from '@nitrots/utils'; +import { Texture, TextureGCSystem, TextureSource } from 'pixi.js'; export { }; declare global @@ -9,13 +9,17 @@ declare global { NitroDevTools?: { - getRoomEngine(): RoomEngine; - showTextureCache(): TextureSource[]; + roomEngine(): RoomEngine; + textureCache(): TextureSource[]; + texturePool(): { [index: string]: { [index: string]: Texture[] } }; + textureGC(): TextureGCSystem; }; } } window.NitroDevTools = { - getRoomEngine: () => GetRoomEngine(), - showTextureCache: () => GetRenderer().texture.managedTextures, + roomEngine: () => GetRoomEngine(), + textureCache: () => GetRenderer().texture.managedTextures, + texturePool: () => GetTexturePool().textures, + textureGC: () => GetRenderer().textureGC };