UNPKG

@awayjs/stage

Version:
148 lines (147 loc) 5.84 kB
import { __extends } from "tslib"; import { TextureBaseWebGL } from './TextureBaseWebGL'; import { UnloadService } from '../managers/UnloadManager'; import { Settings } from '../Settings'; var TextureWebGL = /** @class */ (function (_super) { __extends(TextureWebGL, _super); function TextureWebGL(context, width, height) { var _this = _super.call(this, context) || this; _this.textureType = 'texture2d'; /*internal*/ _this._isFilled = false; /*internal*/ _this._isPMA = false; /*internal*/ _this._isRT = false; /*internal*/ _this._isMipmaped = false; //keepAliveTime: number = 30_000; _this.lastUsedTime = 0; if (!width || !height) { throw new Error("Incorrected size of texture { width: ".concat(width, ", height: ").concat(height, "}")); } _this._width = width; _this._height = height; _this._glTexture = _this._gl.createTexture(); _this._context.stats.counter.texture++; _this._context.stats.memory.texture += width * height * 8; return _this; } TextureWebGL.store = function (tex) { var key = tex._width << 16 | tex._height; var array = TextureWebGL._pool[key] || (TextureWebGL._pool[key] = []); if (array.indexOf(tex) > -1) { tex.lastUsedTime = this.unloadManager.correctedTime; return true; } if (array.length >= this.SIZE_POOL_LIMIT) { return false; } array.push(tex); if (Settings.ENABLE_UNLOAD_TEXTURE) { tex.lastUsedTime = this.unloadManager.correctedTime; this.unloadManager.addTask(tex); // const count = this.unloadManager.execute(); // count && console.debug('[TextureWebGL Experimental] Remove textures persistently', count); } return true; }; TextureWebGL.remove = function (tex) { var key = tex._width << 16 | tex._height; var array = TextureWebGL._pool[key]; if (!array || !array.length) return false; var index = array.indexOf(tex); return index > -1 && !!array.splice(index, 1); }; TextureWebGL.create = function (context, width, height) { // const count = this.unloadManager.execute(); // count && console.debug('[TextureWebGL Experimental] Remove textures persistently', count); var _a; var key = width << 16 | (height | 0); var tex = (_a = TextureWebGL._pool[key]) === null || _a === void 0 ? void 0 : _a.pop(); if (tex) { this.unloadManager.removeTask(tex); return tex; } return new TextureWebGL(context, width, height); }; Object.defineProperty(TextureWebGL.prototype, "canUnload", { get: function () { return this._glTexture && !this._isRT; }, enumerable: false, configurable: true }); Object.defineProperty(TextureWebGL.prototype, "isPOT", { get: function () { return !(this._width & (this._width - 1)) && !(this._height & (this._height - 1)); }, enumerable: false, configurable: true }); Object.defineProperty(TextureWebGL.prototype, "width", { get: function () { return this._width; }, enumerable: false, configurable: true }); Object.defineProperty(TextureWebGL.prototype, "height", { get: function () { return this._height; }, enumerable: false, configurable: true }); /** * @inheritdoc */ TextureWebGL.prototype.dispose = function () { if (Settings.ENABLE_TEXTURE_POOLING && TextureWebGL.store(this)) return; this.unload(); }; TextureWebGL.prototype.unload = function () { TextureWebGL.remove(this); this._context.stats.counter.texture--; this._context.stats.memory.texture -= this.width * this.height * 8; this._context._texContext.disposeTexture(this); this._glTexture = null; this._state.dispose(); }; /* eslint-disable-next-line */ TextureWebGL.prototype.uploadFromArray = function (array, miplevel, premultiplied) { if (miplevel === void 0) { miplevel = 0; } if (premultiplied === void 0) { premultiplied = false; } this._context._texContext.uploadFromArray(this, array, miplevel, premultiplied); this._isFilled = true; this._isPMA = premultiplied; }; TextureWebGL.prototype.uploadFromURL = function (urlRequest, miplevel, premultiplied) { if (miplevel === void 0) { miplevel = 0; } if (premultiplied === void 0) { premultiplied = false; } //dummy code for testing this._context._texContext.uploadFromArray(this, null, miplevel, premultiplied); this._isFilled = true; this._isPMA = premultiplied; }; TextureWebGL.prototype.uploadCompressedTextureFromArray = function (array, offset, async) { if (async === void 0) { async = false; } }; TextureWebGL.prototype.generateMipmaps = function () { // cant be generated on nPOT texture in webgl1 if (!this.isPOT && this._context.glVersion !== 2) { return; } var last = this._context._texContext.bindTexture(this, true); this._gl.generateMipmap(this._gl.TEXTURE_2D); this._context._texContext.bindTexture(last, true); this._isMipmaped = true; }; TextureWebGL.SIZE_POOL_LIMIT = 10; TextureWebGL.unloadManager = UnloadService.createManager({ keepAliveTime: 20000, name: 'TextureWebGL', priority: 1000, // runs after any unloaders }); TextureWebGL._pool = {}; return TextureWebGL; }(TextureBaseWebGL)); export { TextureWebGL };