Skip to content

Commit

Permalink
Fix webgl variant offset and dotted texture atlas.
Browse files Browse the repository at this point in the history
  • Loading branch information
tisilent committed Aug 21, 2023
1 parent c5b7e16 commit f051c6d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 26 deletions.
3 changes: 1 addition & 2 deletions addons/xterm-addon-webgl/src/RenderModel.ts
Expand Up @@ -7,11 +7,10 @@ import { ICursorRenderModel, IRenderModel } from './Types';
import { ISelectionRenderModel } from 'browser/renderer/shared/Types';
import { createSelectionRenderModel } from 'browser/renderer/shared/SelectionRenderModel';

export const RENDER_MODEL_INDICIES_PER_CELL = 5;
export const RENDER_MODEL_INDICIES_PER_CELL = 4;
export const RENDER_MODEL_BG_OFFSET = 1;
export const RENDER_MODEL_FG_OFFSET = 2;
export const RENDER_MODEL_EXT_OFFSET = 3;
export const RENDER_MODEL_VARIANT_OFFSET = 4;

export const COMBINED_CHAR_BIT_MASK = 0x80000000;

Expand Down
29 changes: 12 additions & 17 deletions addons/xterm-addon-webgl/src/WebglRenderer.ts
Expand Up @@ -14,7 +14,7 @@ import { ICharacterJoinerService, ICharSizeService, ICoreBrowserService, IThemeS
import { ITerminal } from 'browser/Types';
import { AttributeData } from 'common/buffer/AttributeData';
import { CellData } from 'common/buffer/CellData';
import { Attributes, Content, NULL_CELL_CHAR, NULL_CELL_CODE, UnderlineStyle } from 'common/buffer/Constants';
import { Attributes, Content, ExtFlags, NULL_CELL_CHAR, NULL_CELL_CODE, UnderlineStyle } from 'common/buffer/Constants';
import { EventEmitter, forwardEvent } from 'common/EventEmitter';
import { Disposable, getDisposeArrayDisposable, toDisposable } from 'common/Lifecycle';
import { ICoreService, IDecorationService, IOptionsService, IUnicodeService } from 'common/services/Services';
Expand All @@ -25,7 +25,7 @@ import { RectangleRenderer } from './RectangleRenderer';
import { CursorBlinkStateManager } from 'browser/renderer/shared/CursorBlinkStateManager';
import { LinkRenderLayer } from './renderLayer/LinkRenderLayer';
import { IRenderLayer } from './renderLayer/Types';
import { COMBINED_CHAR_BIT_MASK, RenderModel, RENDER_MODEL_BG_OFFSET, RENDER_MODEL_EXT_OFFSET, RENDER_MODEL_FG_OFFSET, RENDER_MODEL_INDICIES_PER_CELL, RENDER_MODEL_VARIANT_OFFSET } from './RenderModel';
import { COMBINED_CHAR_BIT_MASK, RenderModel, RENDER_MODEL_BG_OFFSET, RENDER_MODEL_EXT_OFFSET, RENDER_MODEL_FG_OFFSET, RENDER_MODEL_INDICIES_PER_CELL } from './RenderModel';
import { IWebGL2RenderingContext } from './Types';

export class WebglRenderer extends Disposable implements IRenderer {
Expand Down Expand Up @@ -398,7 +398,6 @@ export class WebglRenderer extends Disposable implements IRenderer {
let x: number;
let j: number;
let variantOffset: number = -1;
let variantIndex: number = -1;
start = clamp(start, terminal.rows - 1, 0);
end = clamp(end, terminal.rows - 1, 0);

Expand All @@ -425,7 +424,6 @@ export class WebglRenderer extends Disposable implements IRenderer {
joinedRanges = this._characterJoinerService.getJoinedCharacters(row);
// row start variant init
variantOffset = 0;
variantIndex = 0;
for (x = 0; x < terminal.cols; x++) {
lastBg = this._cellColorResolver.result.bg;
line.loadCell(x, cell);
Expand Down Expand Up @@ -503,24 +501,24 @@ export class WebglRenderer extends Disposable implements IRenderer {
} else {
if (cell.extended.underlineStyle !== UnderlineStyle.DOTTED) {
variantOffset = 0;
variantIndex = 0;
}
}

this._cellColorResolver.result.ext &= ~ExtFlags.VARIANT_OFFSET;
this._cellColorResolver.result.ext |= (variantOffset << 29) & ExtFlags.VARIANT_OFFSET;

// Nothing has changed, no updates needed
if (this._model.cells[i] === code &&
this._model.cells[i + RENDER_MODEL_BG_OFFSET] === this._cellColorResolver.result.bg &&
this._model.cells[i + RENDER_MODEL_FG_OFFSET] === this._cellColorResolver.result.fg &&
this._model.cells[i + RENDER_MODEL_EXT_OFFSET] === this._cellColorResolver.result.ext &&
this._model.cells[i + RENDER_MODEL_VARIANT_OFFSET] === variantOffset
this._model.cells[i + RENDER_MODEL_EXT_OFFSET] === this._cellColorResolver.result.ext
) {
if (cell.extended.underlineStyle === UnderlineStyle.DOTTED) {
const s = this._charSizeService.width;
variantOffset = ((deviceCellWidth! * chWidth) - variantOffset) % (lineWidth * 2);
variantIndex++;
if (code !== NULL_CELL_CODE) {
variantOffset = ((deviceCellWidth! * chWidth) - ((lineWidth * 2) - variantOffset)) % (lineWidth * 2);
}
} else {
variantOffset = 0;
variantIndex = 0;
}
continue;
}
Expand All @@ -537,16 +535,14 @@ export class WebglRenderer extends Disposable implements IRenderer {
this._model.cells[i + RENDER_MODEL_BG_OFFSET] = this._cellColorResolver.result.bg;
this._model.cells[i + RENDER_MODEL_FG_OFFSET] = this._cellColorResolver.result.fg;
this._model.cells[i + RENDER_MODEL_EXT_OFFSET] = this._cellColorResolver.result.ext;
this._model.cells[i + RENDER_MODEL_VARIANT_OFFSET] = variantOffset;
this._glyphRenderer!.updateCell(x, y, code, this._cellColorResolver.result.bg, this._cellColorResolver.result.fg, this._cellColorResolver.result.ext, chars, lastBg, variantOffset);

if (cell.extended.underlineStyle === UnderlineStyle.DOTTED) {
const s = this._charSizeService.width;
variantOffset = ((deviceCellWidth! * chWidth) - variantOffset) % (lineWidth * 2);
variantIndex++;
if (code !== NULL_CELL_CODE) {
variantOffset = ((deviceCellWidth! * chWidth) - ((lineWidth * 2) - variantOffset)) % (lineWidth * 2);
}
} else {
variantOffset = 0;
variantIndex = 0;
}

if (isJoined) {
Expand All @@ -561,7 +557,6 @@ export class WebglRenderer extends Disposable implements IRenderer {
this._model.cells[j + RENDER_MODEL_BG_OFFSET] = this._cellColorResolver.result.bg;
this._model.cells[j + RENDER_MODEL_FG_OFFSET] = this._cellColorResolver.result.fg;
this._model.cells[j + RENDER_MODEL_EXT_OFFSET] = this._cellColorResolver.result.ext;
this._model.cells[i + RENDER_MODEL_VARIANT_OFFSET] = variantOffset;
}
}
}
Expand Down
25 changes: 20 additions & 5 deletions src/browser/renderer/shared/TextureAtlas.ts
Expand Up @@ -550,7 +550,6 @@ export class TextureAtlas implements ITextureAtlas {
let nextOffset = variantOffset;
for (let i = 0; i < chWidth; i++) {
this._tmpCtx.save();
// console.log('chWidth',chWidth);
const xChLeft = xLeft + i * this._config.deviceCellWidth;
const xChRight = xLeft + (i + 1) * this._config.deviceCellWidth;
const xChMid = xChLeft + this._config.deviceCellWidth / 2;
Expand Down Expand Up @@ -597,10 +596,26 @@ export class TextureAtlas implements ITextureAtlas {
);
break;
case UnderlineStyle.DOTTED:
this._tmpCtx.setLineDash([Math.round(lineWidth), Math.round(lineWidth)]);
this._tmpCtx.moveTo(xChLeft + nextOffset, yTop);
this._tmpCtx.lineTo(xChRight, yTop);
nextOffset = (xChRight - xChLeft - nextOffset) % (Math.round(lineWidth) * 2);
const offsetWidth = nextOffset >= lineWidth ? lineWidth * 2 - nextOffset : lineWidth - nextOffset;
if (offsetWidth === 0) {
this._tmpCtx.setLineDash([Math.round(lineWidth), Math.round(lineWidth)]);
this._tmpCtx.moveTo(xChLeft, yTop);
this._tmpCtx.lineTo(xChRight, yTop);
} else {
const isFull = nextOffset >= lineWidth ? false : true;
if (isFull === false) {
this._tmpCtx.setLineDash([Math.round(lineWidth), Math.round(lineWidth)]);
this._tmpCtx.moveTo(xChLeft + offsetWidth, yTop);
this._tmpCtx.lineTo(xChRight, yTop);
} else {
this._tmpCtx.setLineDash([Math.round(lineWidth), Math.round(lineWidth)]);
this._tmpCtx.moveTo(xChLeft, yTop);
this._tmpCtx.lineTo(xChLeft + offsetWidth, yTop);
this._tmpCtx.moveTo(xChLeft + offsetWidth + lineWidth, yTop);
this._tmpCtx.lineTo(xChRight, yTop);
}
}
nextOffset = (xChRight - xChLeft - ((lineWidth * 2) - nextOffset)) % (Math.round(lineWidth) * 2);
break;
case UnderlineStyle.DASHED:
this._tmpCtx.setLineDash([this._config.devicePixelRatio * 4, this._config.devicePixelRatio * 3]);
Expand Down
9 changes: 7 additions & 2 deletions src/common/buffer/Constants.ts
Expand Up @@ -134,9 +134,14 @@ export const enum BgFlags {

export const enum ExtFlags {
/**
* bit 27..32 (upper 3 unused)
* bit 27..29
*/
UNDERLINE_STYLE = 0x1C000000
UNDERLINE_STYLE = 0x1C000000,

/**
* bit 30..32
*/
VARIANT_OFFSET = 0xE0000000
}

export const enum UnderlineStyle {
Expand Down

0 comments on commit f051c6d

Please sign in to comment.