Merge when max atlas pages is reached

This commit is contained in:
Daniel Imms
2022-11-01 10:08:00 -07:00
parent 54ee698ba6
commit c9bacc80d7
2 changed files with 22 additions and 25 deletions
@@ -11,6 +11,7 @@ import { Terminal } from 'xterm';
import { IRasterizedGlyph, IRenderDimensions, ITextureAtlas } from 'browser/renderer/shared/Types';
import { Disposable, toDisposable } from 'common/Lifecycle';
import { throwIfFalsy } from 'browser/renderer/shared/RendererUtils';
import { TextureAtlas } from 'browser/renderer/shared/TextureAtlas';
interface IVertices {
attributes: Float32Array;
@@ -108,8 +109,6 @@ export class GlyphRenderer extends Disposable {
]
};
private static _maxAtlasPages: number | undefined;
constructor(
private readonly _terminal: Terminal,
private readonly _gl: IWebGL2RenderingContext,
@@ -119,11 +118,11 @@ export class GlyphRenderer extends Disposable {
const gl = this._gl;
if (GlyphRenderer._maxAtlasPages === undefined) {
GlyphRenderer._maxAtlasPages = throwIfFalsy(gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS) as number | null);
if (TextureAtlas.maxAtlasPages === undefined) {
TextureAtlas.maxAtlasPages = throwIfFalsy(gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS) as number | null);
}
this._program = throwIfFalsy(createProgram(gl, vertexShaderSource, createFragmentShaderSource(GlyphRenderer._maxAtlasPages)));
this._program = throwIfFalsy(createProgram(gl, vertexShaderSource, createFragmentShaderSource(TextureAtlas.maxAtlasPages)));
this.register(toDisposable(() => gl.deleteProgram(this._program)));
// Uniform locations
@@ -178,8 +177,8 @@ export class GlyphRenderer extends Disposable {
// Setup static uniforms
gl.useProgram(this._program);
const textureUnits = new Int32Array(GlyphRenderer._maxAtlasPages);
for (let i = 0; i < GlyphRenderer._maxAtlasPages; i++) {
const textureUnits = new Int32Array(TextureAtlas.maxAtlasPages);
for (let i = 0; i < TextureAtlas.maxAtlasPages; i++) {
textureUnits[i] = i;
}
gl.uniform1iv(this._textureLocation, textureUnits);
@@ -188,7 +187,7 @@ export class GlyphRenderer extends Disposable {
// Setup 1x1 red pixel textures for all potential atlas pages, if one of these invalid textures
// is ever drawn it will show characters as red rectangles.
this._atlasTextures = [];
for (let i = 0; i < GlyphRenderer._maxAtlasPages; i++) {
for (let i = 0; i < TextureAtlas.maxAtlasPages; i++) {
const texture = throwIfFalsy(gl.createTexture());
this.register(toDisposable(() => gl.deleteTexture(texture)));
gl.activeTexture(gl.TEXTURE0 + i);
+15 -17
View File
@@ -69,6 +69,8 @@ export class TextureAtlas implements ITextureAtlas {
private _textureSize: number = 512;
public static maxAtlasPages: number | undefined;
private readonly _onAddTextureAtlasCanvas = new EventEmitter<HTMLCanvasElement>();
public readonly onAddTextureAtlasCanvas = this._onAddTextureAtlasCanvas.event;
@@ -140,7 +142,7 @@ export class TextureAtlas implements ITextureAtlas {
// this._increaseTextureSize();
// }
if (!this._hasMerged && this._pages.length === 6) {
if (!this._hasMerged && this._pages.length === TextureAtlas.maxAtlasPages) {
this._hasMerged = true;
console.log('try merge');
console.time('merge');
@@ -172,22 +174,7 @@ export class TextureAtlas implements ITextureAtlas {
// TODO: Splice other 3 pages, shifting all other texture page props
for (let i = sortedMergingPagesIndexes.length - 1; i >= 1; i--) {
// TODO: Optimize, this is slow
const mergingPageIndex = sortedMergingPagesIndexes[i];
console.log('splice', mergingPageIndex);
this._pages.splice(mergingPageIndex, 1);
for (let j = mergingPageIndex; j < this._pages.length; j++) {
const adjustingPage = this._pages[j];
console.log('adjust', j);
// if (mergingPages.includes(adjustingPage)) {
// continue;
// }
for (const g of adjustingPage.glyphs) {
g.texturePage--;
}
adjustingPage.hasCanvasChanged = true;
}
this._deletePage(sortedMergingPagesIndexes[i]);
}
this._onAddTextureAtlasCanvas.fire(mergedPage.canvas);
@@ -239,6 +226,17 @@ export class TextureAtlas implements ITextureAtlas {
return mergedPage;
}
private _deletePage(pageIndex: number): void {
this._pages.splice(pageIndex, 1);
for (let j = pageIndex; j < this._pages.length; j++) {
const adjustingPage = this._pages[j];
for (const g of adjustingPage.glyphs) {
g.texturePage--;
}
adjustingPage.hasCanvasChanged = true;
}
}
/**
* Doubles the texture size of new atlas pages if allowed.
*/