Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/browser/gpu/renderStrategy/fullFileRenderStrategy.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { getActiveWindow } from '../../../../base/browser/dom.js';
7
import { Color } from '../../../../base/common/color.js';
8
import { BugIndicatingError } from '../../../../base/common/errors.js';
9
import { CursorColumns } from '../../../common/core/cursorColumns.js';
10
import type { IViewLineTokens } from '../../../common/tokens/lineTokens.js';
11
import { ViewEventType, type ViewConfigurationChangedEvent, type ViewDecorationsChangedEvent, type ViewLineMappingChangedEvent, type ViewLinesChangedEvent, type ViewLinesDeletedEvent, type ViewLinesInsertedEvent, type ViewScrollChangedEvent, type ViewThemeChangedEvent, type ViewTokensChangedEvent, type ViewZonesChangedEvent } from '../../../common/viewEvents.js';
12
import type { ViewportData } from '../../../common/viewLayout/viewLinesViewportData.js';
13
import type { ViewLineRenderingData } from '../../../common/viewModel.js';
14
import type { ViewContext } from '../../../common/viewModel/viewContext.js';
15
import type { ViewLineOptions } from '../../viewParts/viewLines/viewLineOptions.js';
16
import type { ITextureAtlasPageGlyph } from '../atlas/atlas.js';
17
import { createContentSegmenter, type IContentSegmenter } from '../contentSegmenter.js';
18
import { fullFileRenderStrategyWgsl } from './fullFileRenderStrategy.wgsl.js';
19
import { BindingId } from '../gpu.js';
20
import { GPULifecycle } from '../gpuDisposable.js';
21
import { quadVertices } from '../gpuUtils.js';
22
import { GlyphRasterizer } from '../raster/glyphRasterizer.js';
23
import { ViewGpuContext } from '../viewGpuContext.js';
24
import { BaseRenderStrategy } from './baseRenderStrategy.js';
25
import { InlineDecoration } from '../../../common/viewModel/inlineDecorations.js';
26
27
const enum Constants {
28
IndicesPerCell = 6,
29
}
30
31
const enum CellBufferInfo {
32
FloatsPerEntry = 6,
33
BytesPerEntry = CellBufferInfo.FloatsPerEntry * 4,
34
Offset_X = 0,
35
Offset_Y = 1,
36
Offset_Unused1 = 2,
37
Offset_Unused2 = 3,
38
GlyphIndex = 4,
39
TextureIndex = 5,
40
}
41
42
type QueuedBufferEvent = (
43
ViewConfigurationChangedEvent |
44
ViewLineMappingChangedEvent |
45
ViewLinesDeletedEvent |
46
ViewZonesChangedEvent
47
);
48
49
/**
50
* A render strategy that tracks a large buffer, uploading only dirty lines as they change and
51
* leveraging heavy caching. This is the most performant strategy but has limitations around long
52
* lines and too many lines.
53
*/
54
export class FullFileRenderStrategy extends BaseRenderStrategy {
55
56
/**
57
* The hard cap for line count that can be rendered by the GPU renderer.
58
*/
59
static readonly maxSupportedLines = 3000;
60
61
/**
62
* The hard cap for line columns that can be rendered by the GPU renderer.
63
*/
64
static readonly maxSupportedColumns = 200;
65
66
readonly type = 'fullfile';
67
readonly wgsl: string = fullFileRenderStrategyWgsl;
68
69
private _cellBindBuffer!: GPUBuffer;
70
71
/**
72
* The cell value buffers, these hold the cells and their glyphs. It's double buffers such that
73
* the thread doesn't block when one is being uploaded to the GPU.
74
*/
75
private _cellValueBuffers!: [ArrayBuffer, ArrayBuffer];
76
private _activeDoubleBufferIndex: 0 | 1 = 0;
77
78
private readonly _upToDateLines: [Set<number>, Set<number>] = [new Set(), new Set()];
79
private _visibleObjectCount: number = 0;
80
private _finalRenderedLine: number = 0;
81
82
private _scrollOffsetBindBuffer: GPUBuffer;
83
private _scrollOffsetValueBuffer: Float32Array;
84
private _scrollInitialized: boolean = false;
85
86
private readonly _queuedBufferUpdates: [QueuedBufferEvent[], QueuedBufferEvent[]] = [[], []];
87
88
get bindGroupEntries(): GPUBindGroupEntry[] {
89
return [
90
{ binding: BindingId.Cells, resource: { buffer: this._cellBindBuffer } },
91
{ binding: BindingId.ScrollOffset, resource: { buffer: this._scrollOffsetBindBuffer } }
92
];
93
}
94
95
constructor(
96
context: ViewContext,
97
viewGpuContext: ViewGpuContext,
98
device: GPUDevice,
99
glyphRasterizer: { value: GlyphRasterizer },
100
) {
101
super(context, viewGpuContext, device, glyphRasterizer);
102
103
const bufferSize = FullFileRenderStrategy.maxSupportedLines * FullFileRenderStrategy.maxSupportedColumns * Constants.IndicesPerCell * Float32Array.BYTES_PER_ELEMENT;
104
this._cellBindBuffer = this._register(GPULifecycle.createBuffer(this._device, {
105
label: 'Monaco full file cell buffer',
106
size: bufferSize,
107
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
108
})).object;
109
this._cellValueBuffers = [
110
new ArrayBuffer(bufferSize),
111
new ArrayBuffer(bufferSize),
112
];
113
114
const scrollOffsetBufferSize = 2;
115
this._scrollOffsetBindBuffer = this._register(GPULifecycle.createBuffer(this._device, {
116
label: 'Monaco scroll offset buffer',
117
size: scrollOffsetBufferSize * Float32Array.BYTES_PER_ELEMENT,
118
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
119
})).object;
120
this._scrollOffsetValueBuffer = new Float32Array(scrollOffsetBufferSize);
121
}
122
123
// #region Event handlers
124
125
// The primary job of these handlers is to:
126
// 1. Invalidate the up to date line cache, which will cause the line to be re-rendered when
127
// it's _within the viewport_.
128
// 2. Pass relevant events on to the render function so it can force certain line ranges to be
129
// re-rendered even if they're not in the viewport. For example when a view zone is added,
130
// there are lines that used to be visible but are no longer, so those ranges must be
131
// cleared and uploaded to the GPU.
132
133
public override onConfigurationChanged(e: ViewConfigurationChangedEvent): boolean {
134
this._invalidateAllLines();
135
this._queueBufferUpdate(e);
136
return true;
137
}
138
139
public override onDecorationsChanged(e: ViewDecorationsChangedEvent): boolean {
140
this._invalidateAllLines();
141
return true;
142
}
143
144
public override onTokensChanged(e: ViewTokensChangedEvent): boolean {
145
// TODO: This currently fires for the entire viewport whenever scrolling stops
146
// https://github.com/microsoft/vscode/issues/233942
147
for (const range of e.ranges) {
148
this._invalidateLineRange(range.fromLineNumber, range.toLineNumber);
149
}
150
return true;
151
}
152
153
public override onLinesDeleted(e: ViewLinesDeletedEvent): boolean {
154
// TODO: This currently invalidates everything after the deleted line, it could shift the
155
// line data up to retain some up to date lines
156
// TODO: This does not invalidate lines that are no longer in the file
157
this._invalidateLinesFrom(e.fromLineNumber);
158
this._queueBufferUpdate(e);
159
return true;
160
}
161
162
public override onLinesInserted(e: ViewLinesInsertedEvent): boolean {
163
// TODO: This currently invalidates everything after the deleted line, it could shift the
164
// line data up to retain some up to date lines
165
this._invalidateLinesFrom(e.fromLineNumber);
166
return true;
167
}
168
169
public override onLinesChanged(e: ViewLinesChangedEvent): boolean {
170
this._invalidateLineRange(e.fromLineNumber, e.fromLineNumber + e.count);
171
return true;
172
}
173
174
public override onScrollChanged(e?: ViewScrollChangedEvent): boolean {
175
const dpr = getActiveWindow().devicePixelRatio;
176
this._scrollOffsetValueBuffer[0] = (e?.scrollLeft ?? this._context.viewLayout.getCurrentScrollLeft()) * dpr;
177
this._scrollOffsetValueBuffer[1] = (e?.scrollTop ?? this._context.viewLayout.getCurrentScrollTop()) * dpr;
178
this._device.queue.writeBuffer(this._scrollOffsetBindBuffer, 0, this._scrollOffsetValueBuffer as Float32Array<ArrayBuffer>);
179
return true;
180
}
181
182
public override onThemeChanged(e: ViewThemeChangedEvent): boolean {
183
this._invalidateAllLines();
184
return true;
185
}
186
187
public override onLineMappingChanged(e: ViewLineMappingChangedEvent): boolean {
188
this._invalidateAllLines();
189
this._queueBufferUpdate(e);
190
return true;
191
}
192
193
public override onZonesChanged(e: ViewZonesChangedEvent): boolean {
194
this._invalidateAllLines();
195
this._queueBufferUpdate(e);
196
197
return true;
198
}
199
200
// #endregion
201
202
private _invalidateAllLines(): void {
203
this._upToDateLines[0].clear();
204
this._upToDateLines[1].clear();
205
}
206
207
private _invalidateLinesFrom(lineNumber: number): void {
208
for (const i of [0, 1]) {
209
const upToDateLines = this._upToDateLines[i];
210
for (const upToDateLine of upToDateLines) {
211
if (upToDateLine >= lineNumber) {
212
upToDateLines.delete(upToDateLine);
213
}
214
}
215
}
216
}
217
218
private _invalidateLineRange(fromLineNumber: number, toLineNumber: number): void {
219
for (let i = fromLineNumber; i <= toLineNumber; i++) {
220
this._upToDateLines[0].delete(i);
221
this._upToDateLines[1].delete(i);
222
}
223
}
224
225
reset() {
226
this._invalidateAllLines();
227
for (const bufferIndex of [0, 1]) {
228
// Zero out buffer and upload to GPU to prevent stale rows from rendering
229
const buffer = new Float32Array(this._cellValueBuffers[bufferIndex]);
230
buffer.fill(0, 0, buffer.length);
231
this._device.queue.writeBuffer(this._cellBindBuffer, 0, buffer.buffer, 0, buffer.byteLength);
232
}
233
this._finalRenderedLine = 0;
234
}
235
236
update(viewportData: ViewportData, viewLineOptions: ViewLineOptions): number {
237
// IMPORTANT: This is a hot function. Variables are pre-allocated and shared within the
238
// loop. This is done so we don't need to trust the JIT compiler to do this optimization to
239
// avoid potential additional blocking time in garbage collector which is a common cause of
240
// dropped frames.
241
242
let chars = '';
243
let segment: string | undefined;
244
let charWidth = 0;
245
let y = 0;
246
let x = 0;
247
let absoluteOffsetX = 0;
248
let absoluteOffsetY = 0;
249
let tabXOffset = 0;
250
let glyph: Readonly<ITextureAtlasPageGlyph>;
251
let cellIndex = 0;
252
253
let tokenStartIndex = 0;
254
let tokenEndIndex = 0;
255
let tokenMetadata = 0;
256
257
let decorationStyleSetBold: boolean | undefined;
258
let decorationStyleSetColor: number | undefined;
259
let decorationStyleSetOpacity: number | undefined;
260
261
let lineData: ViewLineRenderingData;
262
let decoration: InlineDecoration;
263
let fillStartIndex = 0;
264
let fillEndIndex = 0;
265
266
let tokens: IViewLineTokens;
267
268
const dpr = getActiveWindow().devicePixelRatio;
269
let contentSegmenter: IContentSegmenter;
270
271
if (!this._scrollInitialized) {
272
this.onScrollChanged();
273
this._scrollInitialized = true;
274
}
275
276
// Update cell data
277
const cellBuffer = new Float32Array(this._cellValueBuffers[this._activeDoubleBufferIndex]);
278
const lineIndexCount = FullFileRenderStrategy.maxSupportedColumns * Constants.IndicesPerCell;
279
280
const upToDateLines = this._upToDateLines[this._activeDoubleBufferIndex];
281
let dirtyLineStart = 3000;
282
let dirtyLineEnd = 0;
283
284
// Handle any queued buffer updates
285
const queuedBufferUpdates = this._queuedBufferUpdates[this._activeDoubleBufferIndex];
286
while (queuedBufferUpdates.length) {
287
const e = queuedBufferUpdates.shift()!;
288
switch (e.type) {
289
// TODO: Refine these cases so we're not throwing away everything
290
case ViewEventType.ViewConfigurationChanged:
291
case ViewEventType.ViewLineMappingChanged:
292
case ViewEventType.ViewZonesChanged: {
293
cellBuffer.fill(0);
294
295
dirtyLineStart = 1;
296
dirtyLineEnd = Math.max(dirtyLineEnd, this._finalRenderedLine);
297
this._finalRenderedLine = 0;
298
break;
299
}
300
case ViewEventType.ViewLinesDeleted: {
301
// Shift content below deleted line up
302
const deletedLineContentStartIndex = (e.fromLineNumber - 1) * FullFileRenderStrategy.maxSupportedColumns * Constants.IndicesPerCell;
303
const deletedLineContentEndIndex = (e.toLineNumber) * FullFileRenderStrategy.maxSupportedColumns * Constants.IndicesPerCell;
304
const nullContentStartIndex = (this._finalRenderedLine - (e.toLineNumber - e.fromLineNumber + 1)) * FullFileRenderStrategy.maxSupportedColumns * Constants.IndicesPerCell;
305
cellBuffer.set(cellBuffer.subarray(deletedLineContentEndIndex), deletedLineContentStartIndex);
306
307
// Zero out content on lines that are no longer valid
308
cellBuffer.fill(0, nullContentStartIndex);
309
310
// Update dirty lines and final rendered line
311
dirtyLineStart = Math.min(dirtyLineStart, e.fromLineNumber);
312
dirtyLineEnd = Math.max(dirtyLineEnd, this._finalRenderedLine);
313
this._finalRenderedLine -= e.toLineNumber - e.fromLineNumber + 1;
314
break;
315
}
316
}
317
}
318
319
for (y = viewportData.startLineNumber; y <= viewportData.endLineNumber; y++) {
320
321
// Only attempt to render lines that the GPU renderer can handle
322
if (!this._viewGpuContext.canRender(viewLineOptions, viewportData, y)) {
323
fillStartIndex = ((y - 1) * FullFileRenderStrategy.maxSupportedColumns) * Constants.IndicesPerCell;
324
fillEndIndex = (y * FullFileRenderStrategy.maxSupportedColumns) * Constants.IndicesPerCell;
325
cellBuffer.fill(0, fillStartIndex, fillEndIndex);
326
327
dirtyLineStart = Math.min(dirtyLineStart, y);
328
dirtyLineEnd = Math.max(dirtyLineEnd, y);
329
330
continue;
331
}
332
333
// Skip updating the line if it's already up to date
334
if (upToDateLines.has(y)) {
335
continue;
336
}
337
338
dirtyLineStart = Math.min(dirtyLineStart, y);
339
dirtyLineEnd = Math.max(dirtyLineEnd, y);
340
341
lineData = viewportData.getViewLineRenderingData(y);
342
tabXOffset = 0;
343
344
contentSegmenter = createContentSegmenter(lineData, viewLineOptions);
345
charWidth = viewLineOptions.spaceWidth * dpr;
346
absoluteOffsetX = 0;
347
348
tokens = lineData.tokens;
349
tokenStartIndex = lineData.minColumn - 1;
350
tokenEndIndex = 0;
351
for (let tokenIndex = 0, tokensLen = tokens.getCount(); tokenIndex < tokensLen; tokenIndex++) {
352
tokenEndIndex = tokens.getEndOffset(tokenIndex);
353
if (tokenEndIndex <= tokenStartIndex) {
354
// The faux indent part of the line should have no token type
355
continue;
356
}
357
358
tokenMetadata = tokens.getMetadata(tokenIndex);
359
360
for (x = tokenStartIndex; x < tokenEndIndex; x++) {
361
// Only render lines that do not exceed maximum columns
362
if (x > FullFileRenderStrategy.maxSupportedColumns) {
363
break;
364
}
365
segment = contentSegmenter.getSegmentAtIndex(x);
366
if (segment === undefined) {
367
continue;
368
}
369
chars = segment;
370
371
if (!(lineData.isBasicASCII && viewLineOptions.useMonospaceOptimizations)) {
372
charWidth = this.glyphRasterizer.getTextMetrics(chars).width;
373
}
374
375
decorationStyleSetColor = undefined;
376
decorationStyleSetBold = undefined;
377
decorationStyleSetOpacity = undefined;
378
379
// Apply supported inline decoration styles to the cell metadata
380
for (decoration of lineData.inlineDecorations) {
381
// This is Range.strictContainsPosition except it works at the cell level,
382
// it's also inlined to avoid overhead.
383
if (
384
(y < decoration.range.startLineNumber || y > decoration.range.endLineNumber) ||
385
(y === decoration.range.startLineNumber && x < decoration.range.startColumn - 1) ||
386
(y === decoration.range.endLineNumber && x >= decoration.range.endColumn - 1)
387
) {
388
continue;
389
}
390
391
const rules = ViewGpuContext.decorationCssRuleExtractor.getStyleRules(this._viewGpuContext.canvas.domNode, decoration.inlineClassName);
392
for (const rule of rules) {
393
for (const r of rule.style) {
394
const value = rule.styleMap.get(r)?.toString() ?? '';
395
switch (r) {
396
case 'color': {
397
// TODO: This parsing and error handling should move into canRender so fallback
398
// to DOM works
399
const parsedColor = Color.Format.CSS.parse(value);
400
if (!parsedColor) {
401
throw new BugIndicatingError('Invalid color format ' + value);
402
}
403
decorationStyleSetColor = parsedColor.toNumber32Bit();
404
break;
405
}
406
case 'font-weight': {
407
const parsedValue = parseCssFontWeight(value);
408
if (parsedValue >= 400) {
409
decorationStyleSetBold = true;
410
// TODO: Set bold (https://github.com/microsoft/vscode/issues/237584)
411
} else {
412
decorationStyleSetBold = false;
413
// TODO: Set normal (https://github.com/microsoft/vscode/issues/237584)
414
}
415
break;
416
}
417
case 'opacity': {
418
const parsedValue = parseCssOpacity(value);
419
decorationStyleSetOpacity = parsedValue;
420
break;
421
}
422
default: throw new BugIndicatingError('Unexpected inline decoration style');
423
}
424
}
425
}
426
}
427
428
if (chars === ' ' || chars === '\t') {
429
// Zero out glyph to ensure it doesn't get rendered
430
cellIndex = ((y - 1) * FullFileRenderStrategy.maxSupportedColumns + x) * Constants.IndicesPerCell;
431
cellBuffer.fill(0, cellIndex, cellIndex + CellBufferInfo.FloatsPerEntry);
432
// Adjust xOffset for tab stops
433
if (chars === '\t') {
434
// Find the pixel offset between the current position and the next tab stop
435
const offsetBefore = x + tabXOffset;
436
tabXOffset = CursorColumns.nextRenderTabStop(x + tabXOffset, lineData.tabSize);
437
absoluteOffsetX += charWidth * (tabXOffset - offsetBefore);
438
// Convert back to offset excluding x and the current character
439
tabXOffset -= x + 1;
440
} else {
441
absoluteOffsetX += charWidth;
442
}
443
continue;
444
}
445
446
const decorationStyleSetId = ViewGpuContext.decorationStyleCache.getOrCreateEntry(decorationStyleSetColor, decorationStyleSetBold, decorationStyleSetOpacity);
447
glyph = this._viewGpuContext.atlas.getGlyph(this.glyphRasterizer, chars, tokenMetadata, decorationStyleSetId, absoluteOffsetX);
448
449
absoluteOffsetY = Math.round(
450
// Top of layout box (includes line height)
451
viewportData.relativeVerticalOffset[y - viewportData.startLineNumber] * dpr +
452
453
// Delta from top of layout box (includes line height) to top of the inline box (no line height)
454
Math.floor((viewportData.lineHeight * dpr - (glyph.fontBoundingBoxAscent + glyph.fontBoundingBoxDescent)) / 2) +
455
456
// Delta from top of inline box (no line height) to top of glyph origin. If the glyph was drawn
457
// with a top baseline for example, this ends up drawing the glyph correctly using the alphabetical
458
// baseline.
459
glyph.fontBoundingBoxAscent
460
);
461
462
cellIndex = ((y - 1) * FullFileRenderStrategy.maxSupportedColumns + x) * Constants.IndicesPerCell;
463
cellBuffer[cellIndex + CellBufferInfo.Offset_X] = Math.floor(absoluteOffsetX);
464
cellBuffer[cellIndex + CellBufferInfo.Offset_Y] = absoluteOffsetY;
465
cellBuffer[cellIndex + CellBufferInfo.GlyphIndex] = glyph.glyphIndex;
466
cellBuffer[cellIndex + CellBufferInfo.TextureIndex] = glyph.pageIndex;
467
468
// Adjust the x pixel offset for the next character
469
absoluteOffsetX += charWidth;
470
}
471
472
tokenStartIndex = tokenEndIndex;
473
}
474
475
// Clear to end of line
476
fillStartIndex = ((y - 1) * FullFileRenderStrategy.maxSupportedColumns + tokenEndIndex) * Constants.IndicesPerCell;
477
fillEndIndex = (y * FullFileRenderStrategy.maxSupportedColumns) * Constants.IndicesPerCell;
478
cellBuffer.fill(0, fillStartIndex, fillEndIndex);
479
480
upToDateLines.add(y);
481
}
482
483
const visibleObjectCount = (viewportData.endLineNumber - viewportData.startLineNumber + 1) * lineIndexCount;
484
485
// Only write when there is changed data
486
dirtyLineStart = Math.min(dirtyLineStart, FullFileRenderStrategy.maxSupportedLines);
487
dirtyLineEnd = Math.min(dirtyLineEnd, FullFileRenderStrategy.maxSupportedLines);
488
if (dirtyLineStart <= dirtyLineEnd) {
489
// Write buffer and swap it out to unblock writes
490
this._device.queue.writeBuffer(
491
this._cellBindBuffer,
492
(dirtyLineStart - 1) * lineIndexCount * Float32Array.BYTES_PER_ELEMENT,
493
cellBuffer.buffer,
494
(dirtyLineStart - 1) * lineIndexCount * Float32Array.BYTES_PER_ELEMENT,
495
(dirtyLineEnd - dirtyLineStart + 1) * lineIndexCount * Float32Array.BYTES_PER_ELEMENT
496
);
497
}
498
499
this._finalRenderedLine = Math.max(this._finalRenderedLine, dirtyLineEnd);
500
501
this._activeDoubleBufferIndex = this._activeDoubleBufferIndex ? 0 : 1;
502
503
this._visibleObjectCount = visibleObjectCount;
504
505
return visibleObjectCount;
506
}
507
508
draw(pass: GPURenderPassEncoder, viewportData: ViewportData): void {
509
if (this._visibleObjectCount <= 0) {
510
throw new BugIndicatingError('Attempt to draw 0 objects');
511
}
512
pass.draw(
513
quadVertices.length / 2,
514
this._visibleObjectCount,
515
undefined,
516
(viewportData.startLineNumber - 1) * FullFileRenderStrategy.maxSupportedColumns
517
);
518
}
519
520
/**
521
* Queue updates that need to happen on the active buffer, not just the cache. This will be
522
* deferred to when the actual cell buffer is changed since the active buffer could be locked by
523
* the GPU which would block the main thread.
524
*/
525
private _queueBufferUpdate(e: QueuedBufferEvent) {
526
this._queuedBufferUpdates[0].push(e);
527
this._queuedBufferUpdates[1].push(e);
528
}
529
}
530
531
function parseCssFontWeight(value: string) {
532
switch (value) {
533
case 'lighter':
534
case 'normal': return 400;
535
case 'bolder':
536
case 'bold': return 700;
537
}
538
return parseInt(value);
539
}
540
541
function parseCssOpacity(value: string): number {
542
if (value.endsWith('%')) {
543
return parseFloat(value.substring(0, value.length - 1)) / 100;
544
}
545
if (value.match(/^\d+(?:\.\d*)/)) {
546
return parseFloat(value);
547
}
548
return 1;
549
}
550
551