@gkoreli/ghostty-vt-js 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +89 -0
- package/THIRD_PARTY_NOTICES.md +45 -0
- package/dist/browser-terminal/buffer.d.ts +93 -0
- package/dist/browser-terminal/buffer.d.ts.map +1 -0
- package/dist/browser-terminal/buffer.js +337 -0
- package/dist/browser-terminal/buffer.js.map +1 -0
- package/dist/browser-terminal/encoding.d.ts +17 -0
- package/dist/browser-terminal/encoding.d.ts.map +1 -0
- package/dist/browser-terminal/encoding.js +22 -0
- package/dist/browser-terminal/encoding.js.map +1 -0
- package/dist/browser-terminal/event-emitter.d.ts +8 -0
- package/dist/browser-terminal/event-emitter.d.ts.map +1 -0
- package/dist/browser-terminal/event-emitter.js +27 -0
- package/dist/browser-terminal/event-emitter.js.map +1 -0
- package/dist/browser-terminal/frame-hold.d.ts +13 -0
- package/dist/browser-terminal/frame-hold.d.ts.map +1 -0
- package/dist/browser-terminal/frame-hold.js +17 -0
- package/dist/browser-terminal/frame-hold.js.map +1 -0
- package/dist/browser-terminal/geometry.d.ts +229 -0
- package/dist/browser-terminal/geometry.d.ts.map +1 -0
- package/dist/browser-terminal/geometry.js +299 -0
- package/dist/browser-terminal/geometry.js.map +1 -0
- package/dist/browser-terminal/ghostty.d.ts +478 -0
- package/dist/browser-terminal/ghostty.d.ts.map +1 -0
- package/dist/browser-terminal/ghostty.js +1212 -0
- package/dist/browser-terminal/ghostty.js.map +1 -0
- package/dist/browser-terminal/gpu/adapter.d.ts +70 -0
- package/dist/browser-terminal/gpu/adapter.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/adapter.js +260 -0
- package/dist/browser-terminal/gpu/adapter.js.map +1 -0
- package/dist/browser-terminal/gpu/core/canvas-glyph-rasterizer.d.ts +33 -0
- package/dist/browser-terminal/gpu/core/canvas-glyph-rasterizer.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/canvas-glyph-rasterizer.js +159 -0
- package/dist/browser-terminal/gpu/core/canvas-glyph-rasterizer.js.map +1 -0
- package/dist/browser-terminal/gpu/core/context-loss-watchdog.d.ts +35 -0
- package/dist/browser-terminal/gpu/core/context-loss-watchdog.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/context-loss-watchdog.js +73 -0
- package/dist/browser-terminal/gpu/core/context-loss-watchdog.js.map +1 -0
- package/dist/browser-terminal/gpu/core/frame.d.ts +68 -0
- package/dist/browser-terminal/gpu/core/frame.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/frame.js +167 -0
- package/dist/browser-terminal/gpu/core/frame.js.map +1 -0
- package/dist/browser-terminal/gpu/core/glyph-atlas.d.ts +136 -0
- package/dist/browser-terminal/gpu/core/glyph-atlas.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/glyph-atlas.js +281 -0
- package/dist/browser-terminal/gpu/core/glyph-atlas.js.map +1 -0
- package/dist/browser-terminal/gpu/core/procedural-shapes.d.ts +24 -0
- package/dist/browser-terminal/gpu/core/procedural-shapes.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/procedural-shapes.js +316 -0
- package/dist/browser-terminal/gpu/core/procedural-shapes.js.map +1 -0
- package/dist/browser-terminal/gpu/core/webgl2-renderer.d.ts +107 -0
- package/dist/browser-terminal/gpu/core/webgl2-renderer.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/core/webgl2-renderer.js +698 -0
- package/dist/browser-terminal/gpu/core/webgl2-renderer.js.map +1 -0
- package/dist/browser-terminal/gpu/create-renderer.d.ts +15 -0
- package/dist/browser-terminal/gpu/create-renderer.d.ts.map +1 -0
- package/dist/browser-terminal/gpu/create-renderer.js +33 -0
- package/dist/browser-terminal/gpu/create-renderer.js.map +1 -0
- package/dist/browser-terminal/index.d.ts +47 -0
- package/dist/browser-terminal/index.d.ts.map +1 -0
- package/dist/browser-terminal/index.js +75 -0
- package/dist/browser-terminal/index.js.map +1 -0
- package/dist/browser-terminal/input-handler.d.ts +262 -0
- package/dist/browser-terminal/input-handler.d.ts.map +1 -0
- package/dist/browser-terminal/input-handler.js +978 -0
- package/dist/browser-terminal/input-handler.js.map +1 -0
- package/dist/browser-terminal/interfaces.d.ts +230 -0
- package/dist/browser-terminal/interfaces.d.ts.map +1 -0
- package/dist/browser-terminal/interfaces.js +5 -0
- package/dist/browser-terminal/interfaces.js.map +1 -0
- package/dist/browser-terminal/link-detector.d.ts +76 -0
- package/dist/browser-terminal/link-detector.d.ts.map +1 -0
- package/dist/browser-terminal/link-detector.js +165 -0
- package/dist/browser-terminal/link-detector.js.map +1 -0
- package/dist/browser-terminal/providers/osc8-link-provider.d.ts +56 -0
- package/dist/browser-terminal/providers/osc8-link-provider.d.ts.map +1 -0
- package/dist/browser-terminal/providers/osc8-link-provider.js +204 -0
- package/dist/browser-terminal/providers/osc8-link-provider.js.map +1 -0
- package/dist/browser-terminal/providers/url-regex-provider.d.ts +68 -0
- package/dist/browser-terminal/providers/url-regex-provider.d.ts.map +1 -0
- package/dist/browser-terminal/providers/url-regex-provider.js +107 -0
- package/dist/browser-terminal/providers/url-regex-provider.js.map +1 -0
- package/dist/browser-terminal/renderer-interface.d.ts +59 -0
- package/dist/browser-terminal/renderer-interface.d.ts.map +1 -0
- package/dist/browser-terminal/renderer-interface.js +2 -0
- package/dist/browser-terminal/renderer-interface.js.map +1 -0
- package/dist/browser-terminal/renderer.d.ts +254 -0
- package/dist/browser-terminal/renderer.d.ts.map +1 -0
- package/dist/browser-terminal/renderer.js +905 -0
- package/dist/browser-terminal/renderer.js.map +1 -0
- package/dist/browser-terminal/scrollbar-overlay.d.ts +45 -0
- package/dist/browser-terminal/scrollbar-overlay.d.ts.map +1 -0
- package/dist/browser-terminal/scrollbar-overlay.js +89 -0
- package/dist/browser-terminal/scrollbar-overlay.js.map +1 -0
- package/dist/browser-terminal/selection-manager.d.ts +180 -0
- package/dist/browser-terminal/selection-manager.d.ts.map +1 -0
- package/dist/browser-terminal/selection-manager.js +913 -0
- package/dist/browser-terminal/selection-manager.js.map +1 -0
- package/dist/browser-terminal/terminal.d.ts +423 -0
- package/dist/browser-terminal/terminal.d.ts.map +1 -0
- package/dist/browser-terminal/terminal.js +1754 -0
- package/dist/browser-terminal/terminal.js.map +1 -0
- package/dist/browser-terminal/types.d.ts +595 -0
- package/dist/browser-terminal/types.d.ts.map +1 -0
- package/dist/browser-terminal/types.js +467 -0
- package/dist/browser-terminal/types.js.map +1 -0
- package/dist/browser-terminal/viewport-scroller.d.ts +94 -0
- package/dist/browser-terminal/viewport-scroller.d.ts.map +1 -0
- package/dist/browser-terminal/viewport-scroller.js +159 -0
- package/dist/browser-terminal/viewport-scroller.js.map +1 -0
- package/dist/browser-terminal/vt/effects.d.ts +114 -0
- package/dist/browser-terminal/vt/effects.d.ts.map +1 -0
- package/dist/browser-terminal/vt/effects.js +143 -0
- package/dist/browser-terminal/vt/effects.js.map +1 -0
- package/dist/browser-terminal/vt/enums.d.ts +155 -0
- package/dist/browser-terminal/vt/enums.d.ts.map +1 -0
- package/dist/browser-terminal/vt/enums.js +161 -0
- package/dist/browser-terminal/vt/enums.js.map +1 -0
- package/dist/browser-terminal/vt/mouse.d.ts +166 -0
- package/dist/browser-terminal/vt/mouse.d.ts.map +1 -0
- package/dist/browser-terminal/vt/mouse.js +259 -0
- package/dist/browser-terminal/vt/mouse.js.map +1 -0
- package/dist/browser-terminal/vt/paste.d.ts +53 -0
- package/dist/browser-terminal/vt/paste.d.ts.map +1 -0
- package/dist/browser-terminal/vt/paste.js +89 -0
- package/dist/browser-terminal/vt/paste.js.map +1 -0
- package/dist/browser-terminal/vt/render-state.d.ts +208 -0
- package/dist/browser-terminal/vt/render-state.d.ts.map +1 -0
- package/dist/browser-terminal/vt/render-state.js +776 -0
- package/dist/browser-terminal/vt/render-state.js.map +1 -0
- package/dist/browser-terminal/vt/style.d.ts +54 -0
- package/dist/browser-terminal/vt/style.d.ts.map +1 -0
- package/dist/browser-terminal/vt/style.js +92 -0
- package/dist/browser-terminal/vt/style.js.map +1 -0
- package/dist/browser-terminal/wasm-loader.d.ts +39 -0
- package/dist/browser-terminal/wasm-loader.d.ts.map +1 -0
- package/dist/browser-terminal/wasm-loader.js +73 -0
- package/dist/browser-terminal/wasm-loader.js.map +1 -0
- package/dist/browser-terminal/write-buffer.d.ts +40 -0
- package/dist/browser-terminal/write-buffer.d.ts.map +1 -0
- package/dist/browser-terminal/write-buffer.js +144 -0
- package/dist/browser-terminal/write-buffer.js.map +1 -0
- package/dist/protocol/index.d.ts +135 -0
- package/dist/protocol/index.d.ts.map +1 -0
- package/dist/protocol/index.js +76 -0
- package/dist/protocol/index.js.map +1 -0
- package/dist/react/index.d.ts +59 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +146 -0
- package/dist/react/index.js.map +1 -0
- package/dist/server/index.d.ts +130 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +191 -0
- package/dist/server/index.js.map +1 -0
- package/dist/terminal-screen-emulator/emulator.d.ts +66 -0
- package/dist/terminal-screen-emulator/emulator.d.ts.map +1 -0
- package/dist/terminal-screen-emulator/emulator.js +286 -0
- package/dist/terminal-screen-emulator/emulator.js.map +1 -0
- package/dist/terminal-screen-emulator/index.d.ts +82 -0
- package/dist/terminal-screen-emulator/index.d.ts.map +1 -0
- package/dist/terminal-screen-emulator/index.js +92 -0
- package/dist/terminal-screen-emulator/index.js.map +1 -0
- package/dist/terminal-screen-emulator/types.d.ts +382 -0
- package/dist/terminal-screen-emulator/types.d.ts.map +1 -0
- package/dist/terminal-screen-emulator/types.js +91 -0
- package/dist/terminal-screen-emulator/types.js.map +1 -0
- package/dist/terminal-screen-emulator/wasm-loader.d.ts +71 -0
- package/dist/terminal-screen-emulator/wasm-loader.d.ts.map +1 -0
- package/dist/terminal-screen-emulator/wasm-loader.js +162 -0
- package/dist/terminal-screen-emulator/wasm-loader.js.map +1 -0
- package/dist/themes/index.d.ts +6 -0
- package/dist/themes/index.d.ts.map +1 -0
- package/dist/themes/index.js +51 -0
- package/dist/themes/index.js.map +1 -0
- package/dist/wasm/alloc.d.ts +170 -0
- package/dist/wasm/alloc.d.ts.map +1 -0
- package/dist/wasm/alloc.js +307 -0
- package/dist/wasm/alloc.js.map +1 -0
- package/dist/wasm/compile.d.ts +73 -0
- package/dist/wasm/compile.d.ts.map +1 -0
- package/dist/wasm/compile.js +82 -0
- package/dist/wasm/compile.js.map +1 -0
- package/dist/wasm/exports.d.ts +234 -0
- package/dist/wasm/exports.d.ts.map +1 -0
- package/dist/wasm/exports.js +15 -0
- package/dist/wasm/exports.js.map +1 -0
- package/dist/wasm/fn-table.d.ts +119 -0
- package/dist/wasm/fn-table.d.ts.map +1 -0
- package/dist/wasm/fn-table.js +168 -0
- package/dist/wasm/fn-table.js.map +1 -0
- package/dist/wasm/memory.d.ts +59 -0
- package/dist/wasm/memory.d.ts.map +1 -0
- package/dist/wasm/memory.js +89 -0
- package/dist/wasm/memory.js.map +1 -0
- package/dist/wasm/type-layouts.d.ts +61 -0
- package/dist/wasm/type-layouts.d.ts.map +1 -0
- package/dist/wasm/type-layouts.js +32 -0
- package/dist/wasm/type-layouts.js.map +1 -0
- package/package.json +94 -0
- package/wasm/ghostty-vt.wasm +0 -0
|
@@ -0,0 +1,698 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Engine-blind WebGL2 terminal renderer.
|
|
3
|
+
*
|
|
4
|
+
* The instanced unit-quad and double-buffered CPU staging design follows the
|
|
5
|
+
* production-hardened shape of xterm.js addon-webgl's GlyphRenderer (MIT).
|
|
6
|
+
* Texture-array sampling and the matching rectangle/glyph pass structure are
|
|
7
|
+
* informed by restty's GLSL renderer (MIT). Code here is original and tailored
|
|
8
|
+
* to this package's SoA frame and geometry-authority contract.
|
|
9
|
+
*/
|
|
10
|
+
import { CanvasGlyphRasterizer } from './canvas-glyph-rasterizer.js';
|
|
11
|
+
import { ContextLossWatchdog } from './context-loss-watchdog.js';
|
|
12
|
+
import { packRgba, unpackRgba } from './frame.js';
|
|
13
|
+
import { GlyphAtlas } from './glyph-atlas.js';
|
|
14
|
+
import { emitProceduralShape } from './procedural-shapes.js';
|
|
15
|
+
const GLYPH_STRIDE = 40;
|
|
16
|
+
const RECT_STRIDE = 20;
|
|
17
|
+
const MINIMUM_CONTRAST_RATIO = 1.5;
|
|
18
|
+
const LINK_COLOR = packRgba(74, 144, 226);
|
|
19
|
+
export const WEBGL2_CONTEXT_LOSS_TIMEOUT_MS = 10_000;
|
|
20
|
+
export const WEBGL2_CONTEXT_ATTRIBUTES = {
|
|
21
|
+
alpha: false,
|
|
22
|
+
antialias: false,
|
|
23
|
+
depth: false,
|
|
24
|
+
stencil: false,
|
|
25
|
+
premultipliedAlpha: true,
|
|
26
|
+
preserveDrawingBuffer: false,
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Snap both edges from the same grid equation. Adjacent cells therefore share
|
|
30
|
+
* an exact device-pixel boundary even when the CSS cell advance is fractional.
|
|
31
|
+
*/
|
|
32
|
+
export function deviceCellRect(originX, originY, cellWidth, cellHeight, col, row, span = 1) {
|
|
33
|
+
const x = Math.round(originX + col * cellWidth);
|
|
34
|
+
const y = Math.round(originY + row * cellHeight);
|
|
35
|
+
const right = Math.round(originX + (col + span) * cellWidth);
|
|
36
|
+
const bottom = Math.round(originY + (row + 1) * cellHeight);
|
|
37
|
+
return {
|
|
38
|
+
x,
|
|
39
|
+
y,
|
|
40
|
+
width: Math.max(1, right - x),
|
|
41
|
+
height: Math.max(1, bottom - y),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
class DoubleStaging {
|
|
45
|
+
buffers = [new ArrayBuffer(4096), new ArrayBuffer(4096)];
|
|
46
|
+
index = 0;
|
|
47
|
+
next(bytes) {
|
|
48
|
+
this.index ^= 1;
|
|
49
|
+
if (this.buffers[this.index].byteLength < bytes) {
|
|
50
|
+
let capacity = this.buffers[this.index].byteLength;
|
|
51
|
+
while (capacity < bytes)
|
|
52
|
+
capacity *= 2;
|
|
53
|
+
this.buffers[this.index] = new ArrayBuffer(capacity);
|
|
54
|
+
}
|
|
55
|
+
return new DataView(this.buffers[this.index], 0, bytes);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function compileShader(gl, type, source) {
|
|
59
|
+
const shader = gl.createShader(type);
|
|
60
|
+
if (!shader)
|
|
61
|
+
throw new Error('WebGL2 failed to create shader');
|
|
62
|
+
gl.shaderSource(shader, source);
|
|
63
|
+
gl.compileShader(shader);
|
|
64
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
65
|
+
const message = gl.getShaderInfoLog(shader) ?? 'unknown shader compile error';
|
|
66
|
+
gl.deleteShader(shader);
|
|
67
|
+
throw new Error(message);
|
|
68
|
+
}
|
|
69
|
+
return shader;
|
|
70
|
+
}
|
|
71
|
+
function createProgram(gl, vertexSource, fragmentSource) {
|
|
72
|
+
const vertex = compileShader(gl, gl.VERTEX_SHADER, vertexSource);
|
|
73
|
+
const fragment = compileShader(gl, gl.FRAGMENT_SHADER, fragmentSource);
|
|
74
|
+
const program = gl.createProgram();
|
|
75
|
+
if (!program)
|
|
76
|
+
throw new Error('WebGL2 failed to create program');
|
|
77
|
+
gl.attachShader(program, vertex);
|
|
78
|
+
gl.attachShader(program, fragment);
|
|
79
|
+
gl.linkProgram(program);
|
|
80
|
+
gl.deleteShader(vertex);
|
|
81
|
+
gl.deleteShader(fragment);
|
|
82
|
+
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
83
|
+
const message = gl.getProgramInfoLog(program) ?? 'unknown program link error';
|
|
84
|
+
gl.deleteProgram(program);
|
|
85
|
+
throw new Error(message);
|
|
86
|
+
}
|
|
87
|
+
return program;
|
|
88
|
+
}
|
|
89
|
+
function relativeLuminance(color) {
|
|
90
|
+
const [r, g, b] = unpackRgba(color);
|
|
91
|
+
const linear = (channel) => {
|
|
92
|
+
const value = channel / 255;
|
|
93
|
+
return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
|
|
94
|
+
};
|
|
95
|
+
return 0.2126 * linear(r) + 0.7152 * linear(g) + 0.0722 * linear(b);
|
|
96
|
+
}
|
|
97
|
+
function contrastRatio(a, b) {
|
|
98
|
+
const first = relativeLuminance(a);
|
|
99
|
+
const second = relativeLuminance(b);
|
|
100
|
+
return (Math.max(first, second) + 0.05) / (Math.min(first, second) + 0.05);
|
|
101
|
+
}
|
|
102
|
+
/** Resolve low-contrast text at instance-build time, never in the glyph cache. */
|
|
103
|
+
export function ensureMinimumContrast(foreground, background, minimumRatio = MINIMUM_CONTRAST_RATIO) {
|
|
104
|
+
if (contrastRatio(foreground, background) >= minimumRatio)
|
|
105
|
+
return foreground;
|
|
106
|
+
const black = packRgba(0, 0, 0);
|
|
107
|
+
const white = packRgba(255, 255, 255);
|
|
108
|
+
return contrastRatio(black, background) > contrastRatio(white, background) ? black : white;
|
|
109
|
+
}
|
|
110
|
+
function withAlpha(color, alpha) {
|
|
111
|
+
return ((color & 0x00ffffff) | ((alpha & 0xff) << 24)) >>> 0;
|
|
112
|
+
}
|
|
113
|
+
export class WebGl2RendererCore {
|
|
114
|
+
backend = 'webgl2';
|
|
115
|
+
canvas;
|
|
116
|
+
gl;
|
|
117
|
+
geometry;
|
|
118
|
+
fontFamily;
|
|
119
|
+
fontSize;
|
|
120
|
+
maxAtlasPages;
|
|
121
|
+
atlas;
|
|
122
|
+
atlasTexture;
|
|
123
|
+
zeroPage;
|
|
124
|
+
rectProgram;
|
|
125
|
+
glyphProgram;
|
|
126
|
+
rectVao;
|
|
127
|
+
glyphVao;
|
|
128
|
+
rectBuffer;
|
|
129
|
+
glyphBuffer;
|
|
130
|
+
rectStaging = new DoubleStaging();
|
|
131
|
+
glyphStaging = new DoubleStaging();
|
|
132
|
+
contextLost = false;
|
|
133
|
+
restorationFailed = false;
|
|
134
|
+
disposed = false;
|
|
135
|
+
restoreNeedsFrame = true;
|
|
136
|
+
contextLossWatchdog;
|
|
137
|
+
/** False while the browser owns a lost context; callers must retain damage. */
|
|
138
|
+
get contextAvailable() {
|
|
139
|
+
return !this.contextLost && !this.restorationFailed && !this.disposed;
|
|
140
|
+
}
|
|
141
|
+
onContextLost = (event) => {
|
|
142
|
+
event.preventDefault();
|
|
143
|
+
this.contextLost = true;
|
|
144
|
+
this.restorationFailed = false;
|
|
145
|
+
this.contextLossWatchdog.arm();
|
|
146
|
+
};
|
|
147
|
+
onContextRestored = () => {
|
|
148
|
+
this.contextLost = false;
|
|
149
|
+
try {
|
|
150
|
+
this.createResources();
|
|
151
|
+
this.atlas.clear();
|
|
152
|
+
this.restoreNeedsFrame = true;
|
|
153
|
+
this.restorationFailed = false;
|
|
154
|
+
this.contextLossWatchdog.restore();
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
// The browser restored the context, but resource creation can still fail
|
|
158
|
+
// transiently. Retain damage, keep the watchdog armed, and retry from
|
|
159
|
+
// render().
|
|
160
|
+
this.restorationFailed = true;
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
constructor(canvas, options) {
|
|
164
|
+
this.canvas = canvas;
|
|
165
|
+
const gl = canvas.getContext('webgl2', WEBGL2_CONTEXT_ATTRIBUTES);
|
|
166
|
+
if (!gl)
|
|
167
|
+
throw new Error('WebGL2 is unavailable');
|
|
168
|
+
this.gl = gl;
|
|
169
|
+
this.geometry = options.geometry;
|
|
170
|
+
this.fontFamily = options.fontFamily;
|
|
171
|
+
this.fontSize = options.fontSize;
|
|
172
|
+
this.maxAtlasPages = Math.max(1, Math.min(options.atlasPages ?? 4, gl.getParameter(gl.MAX_ARRAY_TEXTURE_LAYERS)));
|
|
173
|
+
const pageSize = Math.min(options.atlasPageSize ?? 512, gl.getParameter(gl.MAX_TEXTURE_SIZE));
|
|
174
|
+
this.zeroPage = new Uint8Array(pageSize * pageSize * 4);
|
|
175
|
+
this.atlas = this.createAtlas(pageSize);
|
|
176
|
+
const contextLossTimeoutMs = options.contextLossTimeoutMs ?? WEBGL2_CONTEXT_LOSS_TIMEOUT_MS;
|
|
177
|
+
const contextLossMessage = `WebGL2 context did not recover within ${contextLossTimeoutMs}ms; ` +
|
|
178
|
+
'remount the terminal to use Canvas fallback.';
|
|
179
|
+
this.contextLossWatchdog = new ContextLossWatchdog({
|
|
180
|
+
timeoutMs: contextLossTimeoutMs,
|
|
181
|
+
schedule: (callback, timeoutMs) => setTimeout(callback, timeoutMs),
|
|
182
|
+
clear: (handle) => clearTimeout(handle),
|
|
183
|
+
onWarning: () => console.warn(`[ghostty-vt] ${contextLossMessage}`),
|
|
184
|
+
onFailure: () => options.onContextLossTimeout?.(contextLossMessage),
|
|
185
|
+
});
|
|
186
|
+
canvas.addEventListener('webglcontextlost', this.onContextLost);
|
|
187
|
+
canvas.addEventListener('webglcontextrestored', this.onContextRestored);
|
|
188
|
+
this.createResources();
|
|
189
|
+
this.resize(options.geometry);
|
|
190
|
+
}
|
|
191
|
+
resize(geometry) {
|
|
192
|
+
this.assertLive();
|
|
193
|
+
this.geometry = geometry;
|
|
194
|
+
this.canvas.style.width = `${geometry.cssWidth}px`;
|
|
195
|
+
this.canvas.style.height = `${geometry.cssHeight}px`;
|
|
196
|
+
if (this.canvas.width !== geometry.deviceWidth)
|
|
197
|
+
this.canvas.width = geometry.deviceWidth;
|
|
198
|
+
if (this.canvas.height !== geometry.deviceHeight)
|
|
199
|
+
this.canvas.height = geometry.deviceHeight;
|
|
200
|
+
this.gl.viewport(0, 0, geometry.deviceWidth, geometry.deviceHeight);
|
|
201
|
+
this.restoreNeedsFrame = true;
|
|
202
|
+
}
|
|
203
|
+
setFont(fontFamily, fontSize, geometry) {
|
|
204
|
+
this.fontFamily = fontFamily;
|
|
205
|
+
this.fontSize = fontSize;
|
|
206
|
+
this.geometry = geometry;
|
|
207
|
+
const pageSize = this.atlas.pageSize;
|
|
208
|
+
this.atlas.dispose();
|
|
209
|
+
this.atlas = this.createAtlas(pageSize);
|
|
210
|
+
this.restoreNeedsFrame = true;
|
|
211
|
+
}
|
|
212
|
+
render(frame, view, force = false) {
|
|
213
|
+
this.assertLive();
|
|
214
|
+
if (this.contextLost)
|
|
215
|
+
return false;
|
|
216
|
+
if (this.restorationFailed) {
|
|
217
|
+
try {
|
|
218
|
+
this.createResources();
|
|
219
|
+
this.atlas.clear();
|
|
220
|
+
this.restoreNeedsFrame = true;
|
|
221
|
+
this.restorationFailed = false;
|
|
222
|
+
this.contextLossWatchdog.restore();
|
|
223
|
+
}
|
|
224
|
+
catch {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (!force && !this.restoreNeedsFrame && !frame.fullRedraw && !frame.dirtyRows.some(Boolean)) {
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
const backgroundRects = [];
|
|
232
|
+
const foregroundRects = [];
|
|
233
|
+
const surfaceRects = [];
|
|
234
|
+
const glyphs = [];
|
|
235
|
+
const g = this.geometry;
|
|
236
|
+
const dpr = g.devicePixelRatio;
|
|
237
|
+
const cellWidth = g.cellWidth * dpr;
|
|
238
|
+
const cellHeight = g.cellHeight * dpr;
|
|
239
|
+
const baseline = g.baseline * dpr;
|
|
240
|
+
const deviceOriginX = g.gridOriginX * dpr;
|
|
241
|
+
const deviceOriginY = g.gridOriginY * dpr;
|
|
242
|
+
const originX = Math.round(deviceOriginX);
|
|
243
|
+
const originY = Math.round(deviceOriginY);
|
|
244
|
+
const cursor = view.viewportY === 0 && view.cursorVisible && frame.cursor.visible ? frame.cursor : null;
|
|
245
|
+
this.atlas.beginFrame();
|
|
246
|
+
try {
|
|
247
|
+
for (let row = 0; row < frame.rows; row++) {
|
|
248
|
+
for (let col = 0; col < frame.cols; col++) {
|
|
249
|
+
const index = row * frame.cols + col;
|
|
250
|
+
const span = frame.widths[index];
|
|
251
|
+
if (span === 0)
|
|
252
|
+
continue;
|
|
253
|
+
const style = frame.styles[index];
|
|
254
|
+
const selected = frame.isSelected(col, row);
|
|
255
|
+
const blockCursor = cursor !== null && view.cursorStyle === 'block' && cursor.x === col && cursor.y === row;
|
|
256
|
+
let foreground = frame.foregrounds[index];
|
|
257
|
+
let background = frame.backgrounds[index];
|
|
258
|
+
if ((style & 16 /* GpuCellFlags.INVERSE */) !== 0) {
|
|
259
|
+
[foreground, background] = [background, foreground];
|
|
260
|
+
}
|
|
261
|
+
if (selected) {
|
|
262
|
+
foreground = frame.selectionForeground;
|
|
263
|
+
background = frame.selectionBackground;
|
|
264
|
+
}
|
|
265
|
+
if (blockCursor) {
|
|
266
|
+
foreground = frame.cursorAccent;
|
|
267
|
+
background = frame.cursorColor;
|
|
268
|
+
}
|
|
269
|
+
foreground = ensureMinimumContrast(foreground, background);
|
|
270
|
+
if ((style & 128 /* GpuCellFlags.FAINT */) !== 0)
|
|
271
|
+
foreground = withAlpha(foreground, 128);
|
|
272
|
+
const cell = deviceCellRect(deviceOriginX, deviceOriginY, cellWidth, cellHeight, col, row, span);
|
|
273
|
+
const { x, y, width, height } = cell;
|
|
274
|
+
if (background !== frame.defaultBackground || selected || blockCursor) {
|
|
275
|
+
backgroundRects.push({ ...cell, color: background });
|
|
276
|
+
}
|
|
277
|
+
const invisible = (style & 32 /* GpuCellFlags.INVISIBLE */) !== 0;
|
|
278
|
+
if (!invisible && frame.codepoints[index] !== 0) {
|
|
279
|
+
const procedural = emitProceduralShape(frame.codepoints[index]);
|
|
280
|
+
if (procedural !== null) {
|
|
281
|
+
for (const shape of procedural) {
|
|
282
|
+
foregroundRects.push({
|
|
283
|
+
x: Math.round(x + shape.x * width),
|
|
284
|
+
y: Math.round(y + shape.y * height),
|
|
285
|
+
width: Math.max(1, Math.round(shape.width * width)),
|
|
286
|
+
height: Math.max(1, Math.round(shape.height * height)),
|
|
287
|
+
color: foreground,
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
const atlasGlyph = this.atlas.get({
|
|
293
|
+
text: frame.cellText(index),
|
|
294
|
+
bold: (style & 1 /* GpuCellFlags.BOLD */) !== 0,
|
|
295
|
+
italic: (style & 2 /* GpuCellFlags.ITALIC */) !== 0,
|
|
296
|
+
cellSpan: span,
|
|
297
|
+
});
|
|
298
|
+
if (atlasGlyph) {
|
|
299
|
+
glyphs.push({
|
|
300
|
+
x: x + atlasGlyph.offsetX,
|
|
301
|
+
y: y + atlasGlyph.offsetY,
|
|
302
|
+
width: atlasGlyph.width,
|
|
303
|
+
height: atlasGlyph.height,
|
|
304
|
+
u0: atlasGlyph.u0,
|
|
305
|
+
v0: atlasGlyph.v0,
|
|
306
|
+
u1: atlasGlyph.u1,
|
|
307
|
+
v1: atlasGlyph.v1,
|
|
308
|
+
color: foreground,
|
|
309
|
+
meta: atlasGlyph.page | (atlasGlyph.colored ? 0x10000 : 0),
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
const lineThickness = Math.max(1, Math.round(dpr));
|
|
315
|
+
if (!invisible && (style & 4 /* GpuCellFlags.UNDERLINE */) !== 0) {
|
|
316
|
+
foregroundRects.push({
|
|
317
|
+
x,
|
|
318
|
+
y: Math.min(y + height - lineThickness, Math.round(y + baseline + 2 * dpr)),
|
|
319
|
+
width,
|
|
320
|
+
height: lineThickness,
|
|
321
|
+
color: foreground,
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
if (!invisible && (style & 8 /* GpuCellFlags.STRIKETHROUGH */) !== 0) {
|
|
325
|
+
foregroundRects.push({
|
|
326
|
+
x,
|
|
327
|
+
y: Math.round(y + height / 2),
|
|
328
|
+
width,
|
|
329
|
+
height: lineThickness,
|
|
330
|
+
color: foreground,
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
if (!invisible &&
|
|
334
|
+
((frame.hyperlinks[index] !== 0 &&
|
|
335
|
+
view.hoveredHyperlinkId !== 0 &&
|
|
336
|
+
frame.hyperlinks[index] === view.hoveredHyperlinkId) ||
|
|
337
|
+
this.inLinkRange(col, row, view.hoveredLinkRange))) {
|
|
338
|
+
foregroundRects.push({
|
|
339
|
+
x,
|
|
340
|
+
y: Math.min(y + height - lineThickness, Math.round(y + baseline + 2 * dpr)),
|
|
341
|
+
width,
|
|
342
|
+
height: lineThickness,
|
|
343
|
+
color: LINK_COLOR,
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
finally {
|
|
350
|
+
this.atlas.endFrame();
|
|
351
|
+
}
|
|
352
|
+
if (cursor && view.cursorStyle !== 'block') {
|
|
353
|
+
const cell = deviceCellRect(deviceOriginX, deviceOriginY, cellWidth, cellHeight, cursor.x, cursor.y);
|
|
354
|
+
const { x, y, width: cursorCellWidth, height: cursorCellHeight } = cell;
|
|
355
|
+
if (view.cursorStyle === 'underline') {
|
|
356
|
+
const height = Math.max(2 * dpr, Math.floor(cursorCellHeight * 0.15));
|
|
357
|
+
foregroundRects.push({
|
|
358
|
+
x,
|
|
359
|
+
y: y + cursorCellHeight - height,
|
|
360
|
+
width: cursorCellWidth,
|
|
361
|
+
height,
|
|
362
|
+
color: frame.cursorColor,
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
else {
|
|
366
|
+
const width = Math.max(2 * dpr, Math.floor(cursorCellWidth * 0.15));
|
|
367
|
+
foregroundRects.push({ x, y, width, height: cursorCellHeight, color: frame.cursorColor });
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
this.buildScrollbar(surfaceRects, frame, view);
|
|
371
|
+
this.uploadAtlasUpdates(this.atlas.drainUpdates());
|
|
372
|
+
const gl = this.gl;
|
|
373
|
+
const [r, green, b] = unpackRgba(frame.defaultBackground);
|
|
374
|
+
gl.disable(gl.SCISSOR_TEST);
|
|
375
|
+
gl.clearColor(r / 255, green / 255, b / 255, 1);
|
|
376
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
377
|
+
const gridRight = Math.round((g.gridOriginX + g.gridCssWidth) * dpr);
|
|
378
|
+
const gridBottom = Math.round((g.gridOriginY + g.gridCssHeight) * dpr);
|
|
379
|
+
const scissorX = originX;
|
|
380
|
+
const scissorY = g.deviceHeight - gridBottom;
|
|
381
|
+
const scissorWidth = gridRight - originX;
|
|
382
|
+
const scissorHeight = gridBottom - originY;
|
|
383
|
+
gl.enable(gl.SCISSOR_TEST);
|
|
384
|
+
gl.scissor(scissorX, scissorY, scissorWidth, scissorHeight);
|
|
385
|
+
this.drawRects(backgroundRects);
|
|
386
|
+
this.drawGlyphs(glyphs);
|
|
387
|
+
this.drawRects(foregroundRects);
|
|
388
|
+
gl.disable(gl.SCISSOR_TEST);
|
|
389
|
+
this.drawRects(surfaceRects);
|
|
390
|
+
this.restoreNeedsFrame = false;
|
|
391
|
+
return true;
|
|
392
|
+
}
|
|
393
|
+
clear(color) {
|
|
394
|
+
if (this.contextLost || this.disposed)
|
|
395
|
+
return;
|
|
396
|
+
const [r, g, b] = unpackRgba(color);
|
|
397
|
+
this.gl.disable(this.gl.SCISSOR_TEST);
|
|
398
|
+
this.gl.clearColor(r / 255, g / 255, b / 255, 1);
|
|
399
|
+
this.gl.clear(this.gl.COLOR_BUFFER_BIT);
|
|
400
|
+
}
|
|
401
|
+
dispose() {
|
|
402
|
+
if (this.disposed)
|
|
403
|
+
return;
|
|
404
|
+
this.disposed = true;
|
|
405
|
+
this.canvas.removeEventListener('webglcontextlost', this.onContextLost);
|
|
406
|
+
this.canvas.removeEventListener('webglcontextrestored', this.onContextRestored);
|
|
407
|
+
this.contextLossWatchdog.dispose();
|
|
408
|
+
this.deleteResources();
|
|
409
|
+
this.atlas.dispose();
|
|
410
|
+
}
|
|
411
|
+
createAtlas(pageSize) {
|
|
412
|
+
const g = this.geometry;
|
|
413
|
+
return new GlyphAtlas({
|
|
414
|
+
rasterizer: new CanvasGlyphRasterizer({
|
|
415
|
+
fontFamily: this.fontFamily,
|
|
416
|
+
fontSize: this.fontSize,
|
|
417
|
+
cellWidth: g.cellWidth,
|
|
418
|
+
cellHeight: g.cellHeight,
|
|
419
|
+
baseline: g.baseline,
|
|
420
|
+
devicePixelRatio: g.devicePixelRatio,
|
|
421
|
+
}),
|
|
422
|
+
pageSize,
|
|
423
|
+
maxTextureSize: this.gl.getParameter(this.gl.MAX_TEXTURE_SIZE),
|
|
424
|
+
maxPages: this.maxAtlasPages,
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
createResources() {
|
|
428
|
+
const gl = this.gl;
|
|
429
|
+
this.deleteResources();
|
|
430
|
+
this.rectProgram = createProgram(gl, RECT_VERTEX, RECT_FRAGMENT);
|
|
431
|
+
this.glyphProgram = createProgram(gl, GLYPH_VERTEX, GLYPH_FRAGMENT);
|
|
432
|
+
this.rectBuffer = gl.createBuffer() ?? undefined;
|
|
433
|
+
this.glyphBuffer = gl.createBuffer() ?? undefined;
|
|
434
|
+
this.rectVao = gl.createVertexArray() ?? undefined;
|
|
435
|
+
this.glyphVao = gl.createVertexArray() ?? undefined;
|
|
436
|
+
this.atlasTexture = gl.createTexture() ?? undefined;
|
|
437
|
+
if (!this.rectBuffer ||
|
|
438
|
+
!this.glyphBuffer ||
|
|
439
|
+
!this.rectVao ||
|
|
440
|
+
!this.glyphVao ||
|
|
441
|
+
!this.atlasTexture) {
|
|
442
|
+
throw new Error('WebGL2 failed to allocate renderer resources');
|
|
443
|
+
}
|
|
444
|
+
gl.bindVertexArray(this.rectVao);
|
|
445
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, this.rectBuffer);
|
|
446
|
+
gl.enableVertexAttribArray(0);
|
|
447
|
+
gl.vertexAttribPointer(0, 4, gl.FLOAT, false, RECT_STRIDE, 0);
|
|
448
|
+
gl.vertexAttribDivisor(0, 1);
|
|
449
|
+
gl.enableVertexAttribArray(1);
|
|
450
|
+
gl.vertexAttribIPointer(1, 1, gl.UNSIGNED_INT, RECT_STRIDE, 16);
|
|
451
|
+
gl.vertexAttribDivisor(1, 1);
|
|
452
|
+
gl.bindVertexArray(this.glyphVao);
|
|
453
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, this.glyphBuffer);
|
|
454
|
+
gl.enableVertexAttribArray(0);
|
|
455
|
+
gl.vertexAttribPointer(0, 4, gl.FLOAT, false, GLYPH_STRIDE, 0);
|
|
456
|
+
gl.vertexAttribDivisor(0, 1);
|
|
457
|
+
gl.enableVertexAttribArray(1);
|
|
458
|
+
gl.vertexAttribPointer(1, 4, gl.FLOAT, false, GLYPH_STRIDE, 16);
|
|
459
|
+
gl.vertexAttribDivisor(1, 1);
|
|
460
|
+
gl.enableVertexAttribArray(2);
|
|
461
|
+
gl.vertexAttribIPointer(2, 1, gl.UNSIGNED_INT, GLYPH_STRIDE, 32);
|
|
462
|
+
gl.vertexAttribDivisor(2, 1);
|
|
463
|
+
gl.enableVertexAttribArray(3);
|
|
464
|
+
gl.vertexAttribIPointer(3, 1, gl.UNSIGNED_INT, GLYPH_STRIDE, 36);
|
|
465
|
+
gl.vertexAttribDivisor(3, 1);
|
|
466
|
+
gl.bindVertexArray(null);
|
|
467
|
+
gl.bindTexture(gl.TEXTURE_2D_ARRAY, this.atlasTexture);
|
|
468
|
+
gl.texStorage3D(gl.TEXTURE_2D_ARRAY, 1, gl.RGBA8, this.atlas.pageSize, this.atlas.pageSize, this.maxAtlasPages);
|
|
469
|
+
gl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
470
|
+
gl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
471
|
+
gl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
472
|
+
gl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
473
|
+
gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
|
|
474
|
+
gl.enable(gl.BLEND);
|
|
475
|
+
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
|
|
476
|
+
}
|
|
477
|
+
deleteResources() {
|
|
478
|
+
const gl = this.gl;
|
|
479
|
+
if (this.rectProgram)
|
|
480
|
+
gl.deleteProgram(this.rectProgram);
|
|
481
|
+
if (this.glyphProgram)
|
|
482
|
+
gl.deleteProgram(this.glyphProgram);
|
|
483
|
+
if (this.rectVao)
|
|
484
|
+
gl.deleteVertexArray(this.rectVao);
|
|
485
|
+
if (this.glyphVao)
|
|
486
|
+
gl.deleteVertexArray(this.glyphVao);
|
|
487
|
+
if (this.rectBuffer)
|
|
488
|
+
gl.deleteBuffer(this.rectBuffer);
|
|
489
|
+
if (this.glyphBuffer)
|
|
490
|
+
gl.deleteBuffer(this.glyphBuffer);
|
|
491
|
+
if (this.atlasTexture)
|
|
492
|
+
gl.deleteTexture(this.atlasTexture);
|
|
493
|
+
this.rectProgram = undefined;
|
|
494
|
+
this.glyphProgram = undefined;
|
|
495
|
+
this.rectVao = undefined;
|
|
496
|
+
this.glyphVao = undefined;
|
|
497
|
+
this.rectBuffer = undefined;
|
|
498
|
+
this.glyphBuffer = undefined;
|
|
499
|
+
this.atlasTexture = undefined;
|
|
500
|
+
}
|
|
501
|
+
uploadAtlasUpdates(updates) {
|
|
502
|
+
if (updates.length === 0)
|
|
503
|
+
return;
|
|
504
|
+
const gl = this.gl;
|
|
505
|
+
gl.bindTexture(gl.TEXTURE_2D_ARRAY, this.atlasTexture);
|
|
506
|
+
for (const update of updates) {
|
|
507
|
+
if (update.kind === 'reset-page') {
|
|
508
|
+
gl.texSubImage3D(gl.TEXTURE_2D_ARRAY, 0, 0, 0, update.page, update.size, update.size, 1, gl.RGBA, gl.UNSIGNED_BYTE, this.zeroPage);
|
|
509
|
+
}
|
|
510
|
+
else {
|
|
511
|
+
gl.texSubImage3D(gl.TEXTURE_2D_ARRAY, 0, update.x, update.y, update.page, update.width, update.height, 1, gl.RGBA, gl.UNSIGNED_BYTE, update.pixels);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
drawRects(rectangles) {
|
|
516
|
+
if (rectangles.length === 0)
|
|
517
|
+
return;
|
|
518
|
+
const gl = this.gl;
|
|
519
|
+
const bytes = rectangles.length * RECT_STRIDE;
|
|
520
|
+
const staging = this.rectStaging.next(bytes);
|
|
521
|
+
let offset = 0;
|
|
522
|
+
for (const rectangle of rectangles) {
|
|
523
|
+
staging.setFloat32(offset, rectangle.x, true);
|
|
524
|
+
staging.setFloat32(offset + 4, rectangle.y, true);
|
|
525
|
+
staging.setFloat32(offset + 8, rectangle.width, true);
|
|
526
|
+
staging.setFloat32(offset + 12, rectangle.height, true);
|
|
527
|
+
staging.setUint32(offset + 16, rectangle.color, true);
|
|
528
|
+
offset += RECT_STRIDE;
|
|
529
|
+
}
|
|
530
|
+
gl.useProgram(this.rectProgram);
|
|
531
|
+
gl.uniform2f(gl.getUniformLocation(this.rectProgram, 'u_surface'), this.geometry.deviceWidth, this.geometry.deviceHeight);
|
|
532
|
+
gl.bindVertexArray(this.rectVao);
|
|
533
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, this.rectBuffer);
|
|
534
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array(staging.buffer, staging.byteOffset, bytes), gl.STREAM_DRAW);
|
|
535
|
+
gl.drawArraysInstanced(gl.TRIANGLE_STRIP, 0, 4, rectangles.length);
|
|
536
|
+
}
|
|
537
|
+
drawGlyphs(glyphs) {
|
|
538
|
+
if (glyphs.length === 0)
|
|
539
|
+
return;
|
|
540
|
+
const gl = this.gl;
|
|
541
|
+
const bytes = glyphs.length * GLYPH_STRIDE;
|
|
542
|
+
const staging = this.glyphStaging.next(bytes);
|
|
543
|
+
let offset = 0;
|
|
544
|
+
for (const glyph of glyphs) {
|
|
545
|
+
staging.setFloat32(offset, glyph.x, true);
|
|
546
|
+
staging.setFloat32(offset + 4, glyph.y, true);
|
|
547
|
+
staging.setFloat32(offset + 8, glyph.width, true);
|
|
548
|
+
staging.setFloat32(offset + 12, glyph.height, true);
|
|
549
|
+
staging.setFloat32(offset + 16, glyph.u0, true);
|
|
550
|
+
staging.setFloat32(offset + 20, glyph.v0, true);
|
|
551
|
+
staging.setFloat32(offset + 24, glyph.u1, true);
|
|
552
|
+
staging.setFloat32(offset + 28, glyph.v1, true);
|
|
553
|
+
staging.setUint32(offset + 32, glyph.color, true);
|
|
554
|
+
staging.setUint32(offset + 36, glyph.meta, true);
|
|
555
|
+
offset += GLYPH_STRIDE;
|
|
556
|
+
}
|
|
557
|
+
gl.useProgram(this.glyphProgram);
|
|
558
|
+
gl.uniform2f(gl.getUniformLocation(this.glyphProgram, 'u_surface'), this.geometry.deviceWidth, this.geometry.deviceHeight);
|
|
559
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
560
|
+
gl.bindTexture(gl.TEXTURE_2D_ARRAY, this.atlasTexture);
|
|
561
|
+
gl.uniform1i(gl.getUniformLocation(this.glyphProgram, 'u_atlas'), 0);
|
|
562
|
+
gl.bindVertexArray(this.glyphVao);
|
|
563
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, this.glyphBuffer);
|
|
564
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array(staging.buffer, staging.byteOffset, bytes), gl.STREAM_DRAW);
|
|
565
|
+
gl.drawArraysInstanced(gl.TRIANGLE_STRIP, 0, 4, glyphs.length);
|
|
566
|
+
}
|
|
567
|
+
buildScrollbar(rectangles, frame, view) {
|
|
568
|
+
if (view.scrollbackLength <= 0)
|
|
569
|
+
return;
|
|
570
|
+
const dpr = this.geometry.devicePixelRatio;
|
|
571
|
+
const width = 8 * dpr;
|
|
572
|
+
const x = this.geometry.deviceWidth - width - 4 * dpr;
|
|
573
|
+
const padding = 4 * dpr;
|
|
574
|
+
const trackHeight = this.geometry.deviceHeight - padding * 2;
|
|
575
|
+
rectangles.push({
|
|
576
|
+
x: x - 2 * dpr,
|
|
577
|
+
y: 0,
|
|
578
|
+
width: width + 6 * dpr,
|
|
579
|
+
height: this.geometry.deviceHeight,
|
|
580
|
+
color: frame.defaultBackground,
|
|
581
|
+
});
|
|
582
|
+
if (view.scrollbarOpacity <= 0)
|
|
583
|
+
return;
|
|
584
|
+
const totalLines = view.scrollbackLength + frame.rows;
|
|
585
|
+
const thumbHeight = Math.max(20 * dpr, (frame.rows / totalLines) * trackHeight);
|
|
586
|
+
const position = view.viewportY / view.scrollbackLength;
|
|
587
|
+
const thumbY = padding + (trackHeight - thumbHeight) * (1 - position);
|
|
588
|
+
rectangles.push({
|
|
589
|
+
x,
|
|
590
|
+
y: padding,
|
|
591
|
+
width,
|
|
592
|
+
height: trackHeight,
|
|
593
|
+
color: packRgba(128, 128, 128, Math.round(255 * 0.1 * view.scrollbarOpacity)),
|
|
594
|
+
});
|
|
595
|
+
rectangles.push({
|
|
596
|
+
x,
|
|
597
|
+
y: thumbY,
|
|
598
|
+
width,
|
|
599
|
+
height: thumbHeight,
|
|
600
|
+
color: packRgba(128, 128, 128, Math.round(255 * (view.viewportY > 0 ? 0.5 : 0.3) * view.scrollbarOpacity)),
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
inLinkRange(x, y, range) {
|
|
604
|
+
if (!range)
|
|
605
|
+
return false;
|
|
606
|
+
return ((y === range.startY && x >= range.startX && (y < range.endY || x <= range.endX)) ||
|
|
607
|
+
(y > range.startY && y < range.endY) ||
|
|
608
|
+
(y === range.endY && x <= range.endX && (y > range.startY || x >= range.startX)));
|
|
609
|
+
}
|
|
610
|
+
assertLive() {
|
|
611
|
+
if (this.disposed)
|
|
612
|
+
throw new Error('WebGl2RendererCore used after dispose()');
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
const QUAD_VERTEX = `
|
|
616
|
+
vec2 corner(int id) {
|
|
617
|
+
if (id == 0) return vec2(0.0, 0.0);
|
|
618
|
+
if (id == 1) return vec2(1.0, 0.0);
|
|
619
|
+
if (id == 2) return vec2(0.0, 1.0);
|
|
620
|
+
return vec2(1.0, 1.0);
|
|
621
|
+
}
|
|
622
|
+
`;
|
|
623
|
+
const COLOR_GLSL = `
|
|
624
|
+
vec4 unpackColor(uint value) {
|
|
625
|
+
return vec4(
|
|
626
|
+
float(value & 255u),
|
|
627
|
+
float((value >> 8u) & 255u),
|
|
628
|
+
float((value >> 16u) & 255u),
|
|
629
|
+
float((value >> 24u) & 255u)
|
|
630
|
+
) / 255.0;
|
|
631
|
+
}
|
|
632
|
+
`;
|
|
633
|
+
const RECT_VERTEX = `#version 300 es
|
|
634
|
+
precision highp float;
|
|
635
|
+
precision highp int;
|
|
636
|
+
layout(location = 0) in vec4 a_rect;
|
|
637
|
+
layout(location = 1) in uint a_color;
|
|
638
|
+
uniform vec2 u_surface;
|
|
639
|
+
flat out uint v_color;
|
|
640
|
+
${QUAD_VERTEX}
|
|
641
|
+
void main() {
|
|
642
|
+
vec2 p = a_rect.xy + corner(gl_VertexID) * a_rect.zw;
|
|
643
|
+
gl_Position = vec4(p.x / u_surface.x * 2.0 - 1.0, 1.0 - p.y / u_surface.y * 2.0, 0.0, 1.0);
|
|
644
|
+
v_color = a_color;
|
|
645
|
+
}
|
|
646
|
+
`;
|
|
647
|
+
const RECT_FRAGMENT = `#version 300 es
|
|
648
|
+
precision highp float;
|
|
649
|
+
precision highp int;
|
|
650
|
+
flat in uint v_color;
|
|
651
|
+
out vec4 outColor;
|
|
652
|
+
${COLOR_GLSL}
|
|
653
|
+
void main() {
|
|
654
|
+
vec4 color = unpackColor(v_color);
|
|
655
|
+
outColor = vec4(color.rgb * color.a, color.a);
|
|
656
|
+
}
|
|
657
|
+
`;
|
|
658
|
+
const GLYPH_VERTEX = `#version 300 es
|
|
659
|
+
precision highp float;
|
|
660
|
+
precision highp int;
|
|
661
|
+
layout(location = 0) in vec4 a_rect;
|
|
662
|
+
layout(location = 1) in vec4 a_uv;
|
|
663
|
+
layout(location = 2) in uint a_color;
|
|
664
|
+
layout(location = 3) in uint a_meta;
|
|
665
|
+
uniform vec2 u_surface;
|
|
666
|
+
out vec2 v_uv;
|
|
667
|
+
flat out uint v_color;
|
|
668
|
+
flat out uint v_meta;
|
|
669
|
+
${QUAD_VERTEX}
|
|
670
|
+
void main() {
|
|
671
|
+
vec2 c = corner(gl_VertexID);
|
|
672
|
+
vec2 p = a_rect.xy + c * a_rect.zw;
|
|
673
|
+
gl_Position = vec4(p.x / u_surface.x * 2.0 - 1.0, 1.0 - p.y / u_surface.y * 2.0, 0.0, 1.0);
|
|
674
|
+
v_uv = mix(a_uv.xy, a_uv.zw, c);
|
|
675
|
+
v_color = a_color;
|
|
676
|
+
v_meta = a_meta;
|
|
677
|
+
}
|
|
678
|
+
`;
|
|
679
|
+
const GLYPH_FRAGMENT = `#version 300 es
|
|
680
|
+
precision highp float;
|
|
681
|
+
precision highp int;
|
|
682
|
+
uniform highp sampler2DArray u_atlas;
|
|
683
|
+
in vec2 v_uv;
|
|
684
|
+
flat in uint v_color;
|
|
685
|
+
flat in uint v_meta;
|
|
686
|
+
out vec4 outColor;
|
|
687
|
+
${COLOR_GLSL}
|
|
688
|
+
void main() {
|
|
689
|
+
uint page = v_meta & 65535u;
|
|
690
|
+
bool colored = (v_meta & 65536u) != 0u;
|
|
691
|
+
vec4 sampleColor = texture(u_atlas, vec3(v_uv, float(page)));
|
|
692
|
+
vec4 foreground = unpackColor(v_color);
|
|
693
|
+
float alpha = sampleColor.a * foreground.a;
|
|
694
|
+
vec3 rgb = colored ? sampleColor.rgb * alpha : foreground.rgb * alpha;
|
|
695
|
+
outColor = vec4(rgb, alpha);
|
|
696
|
+
}
|
|
697
|
+
`;
|
|
698
|
+
//# sourceMappingURL=webgl2-renderer.js.map
|