Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/browser/gpu/renderStrategy/viewportRenderStrategy.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 { Emitter } from '../../../../base/common/event.js';
10
import { CursorColumns } from '../../../common/core/cursorColumns.js';
11
import type { IViewLineTokens } from '../../../common/tokens/lineTokens.js';
12
import { type ViewConfigurationChangedEvent, type ViewDecorationsChangedEvent, type ViewLineMappingChangedEvent, type ViewLinesChangedEvent, type ViewLinesDeletedEvent, type ViewLinesInsertedEvent, type ViewScrollChangedEvent, type ViewThemeChangedEvent, type ViewTokensChangedEvent, type ViewZonesChangedEvent } from '../../../common/viewEvents.js';
13
import type { ViewportData } from '../../../common/viewLayout/viewLinesViewportData.js';
14
import type { ViewLineRenderingData } from '../../../common/viewModel.js';
15
import { InlineDecoration } from '../../../common/viewModel/inlineDecorations.js';
16
import type { ViewContext } from '../../../common/viewModel/viewContext.js';
17
import type { ViewLineOptions } from '../../viewParts/viewLines/viewLineOptions.js';
18
import type { ITextureAtlasPageGlyph } from '../atlas/atlas.js';
19
import { createContentSegmenter, type IContentSegmenter } from '../contentSegmenter.js';
20
import { BindingId } from '../gpu.js';
21
import { GPULifecycle } from '../gpuDisposable.js';
22
import { quadVertices } from '../gpuUtils.js';
23
import { GlyphRasterizer } from '../raster/glyphRasterizer.js';
24
import { ViewGpuContext } from '../viewGpuContext.js';
25
import { BaseRenderStrategy } from './baseRenderStrategy.js';
26
import { fullFileRenderStrategyWgsl } from './fullFileRenderStrategy.wgsl.js';
27
28
const enum Constants {
29
IndicesPerCell = 6,
30
CellBindBufferCapacityIncrement = 32,
31
CellBindBufferInitialCapacity = 63, // Will be rounded up to nearest increment
32
}
33
34
const enum CellBufferInfo {
35
FloatsPerEntry = 6,
36
BytesPerEntry = CellBufferInfo.FloatsPerEntry * 4,
37
Offset_X = 0,
38
Offset_Y = 1,
39
Offset_Unused1 = 2,
40
Offset_Unused2 = 3,
41
GlyphIndex = 4,
42
TextureIndex = 5,
43
}
44
45
/**
46
* A render strategy that uploads the content of the entire viewport every frame.
47
*/
48
export class ViewportRenderStrategy extends BaseRenderStrategy {
49
/**
50
* The hard cap for line columns that can be rendered by the GPU renderer.
51
*/
52
static readonly maxSupportedColumns = 2000;
53
54
readonly type = 'viewport';
55
readonly wgsl: string = fullFileRenderStrategyWgsl;
56
57
private _cellBindBufferLineCapacity = Constants.CellBindBufferInitialCapacity;
58
private _cellBindBuffer!: GPUBuffer;
59
60
/**
61
* The cell value buffers, these hold the cells and their glyphs. It's double buffers such that
62
* the thread doesn't block when one is being uploaded to the GPU.
63
*/
64
private _cellValueBuffers!: [ArrayBuffer, ArrayBuffer];
65
private _activeDoubleBufferIndex: 0 | 1 = 0;
66
67
private _visibleObjectCount: number = 0;
68
69
private _scrollOffsetBindBuffer: GPUBuffer;
70
private _scrollOffsetValueBuffer: Float32Array;
71
private _scrollInitialized: boolean = false;
72
73
get bindGroupEntries(): GPUBindGroupEntry[] {
74
return [
75
{ binding: BindingId.Cells, resource: { buffer: this._cellBindBuffer } },
76
{ binding: BindingId.ScrollOffset, resource: { buffer: this._scrollOffsetBindBuffer } }
77
];
78
}
79
80
private readonly _onDidChangeBindGroupEntries = this._register(new Emitter<void>());
81
readonly onDidChangeBindGroupEntries = this._onDidChangeBindGroupEntries.event;
82
83
constructor(
84
context: ViewContext,
85
viewGpuContext: ViewGpuContext,
86
device: GPUDevice,
87
glyphRasterizer: { value: GlyphRasterizer },
88
) {
89
super(context, viewGpuContext, device, glyphRasterizer);
90
91
this._rebuildCellBuffer(this._cellBindBufferLineCapacity);
92
93
const scrollOffsetBufferSize = 2;
94
this._scrollOffsetBindBuffer = this._register(GPULifecycle.createBuffer(this._device, {
95
label: 'Monaco scroll offset buffer',
96
size: scrollOffsetBufferSize * Float32Array.BYTES_PER_ELEMENT,
97
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
98
})).object;
99
this._scrollOffsetValueBuffer = new Float32Array(scrollOffsetBufferSize);
100
}
101
102
private _rebuildCellBuffer(lineCount: number) {
103
this._cellBindBuffer?.destroy();
104
105
// Increase in chunks so resizing a window by hand doesn't keep allocating and throwing away
106
const lineCountWithIncrement = (Math.floor(lineCount / Constants.CellBindBufferCapacityIncrement) + 1) * Constants.CellBindBufferCapacityIncrement;
107
108
const bufferSize = lineCountWithIncrement * ViewportRenderStrategy.maxSupportedColumns * Constants.IndicesPerCell * Float32Array.BYTES_PER_ELEMENT;
109
this._cellBindBuffer = this._register(GPULifecycle.createBuffer(this._device, {
110
label: 'Monaco full file cell buffer',
111
size: bufferSize,
112
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
113
})).object;
114
this._cellValueBuffers = [
115
new ArrayBuffer(bufferSize),
116
new ArrayBuffer(bufferSize),
117
];
118
this._cellBindBufferLineCapacity = lineCountWithIncrement;
119
120
this._onDidChangeBindGroupEntries.fire();
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
return true;
135
}
136
137
public override onDecorationsChanged(e: ViewDecorationsChangedEvent): boolean {
138
return true;
139
}
140
141
public override onTokensChanged(e: ViewTokensChangedEvent): boolean {
142
return true;
143
}
144
145
public override onLinesDeleted(e: ViewLinesDeletedEvent): boolean {
146
return true;
147
}
148
149
public override onLinesInserted(e: ViewLinesInsertedEvent): boolean {
150
return true;
151
}
152
153
public override onLinesChanged(e: ViewLinesChangedEvent): boolean {
154
return true;
155
}
156
157
public override onScrollChanged(e?: ViewScrollChangedEvent): boolean {
158
const dpr = getActiveWindow().devicePixelRatio;
159
this._scrollOffsetValueBuffer[0] = (e?.scrollLeft ?? this._context.viewLayout.getCurrentScrollLeft()) * dpr;
160
this._scrollOffsetValueBuffer[1] = (e?.scrollTop ?? this._context.viewLayout.getCurrentScrollTop()) * dpr;
161
this._device.queue.writeBuffer(this._scrollOffsetBindBuffer, 0, this._scrollOffsetValueBuffer as Float32Array<ArrayBuffer>);
162
return true;
163
}
164
165
public override onThemeChanged(e: ViewThemeChangedEvent): boolean {
166
return true;
167
}
168
169
public override onLineMappingChanged(e: ViewLineMappingChangedEvent): boolean {
170
return true;
171
}
172
173
public override onZonesChanged(e: ViewZonesChangedEvent): boolean {
174
return true;
175
}
176
177
// #endregion
178
179
reset() {
180
for (const bufferIndex of [0, 1]) {
181
// Zero out buffer and upload to GPU to prevent stale rows from rendering
182
const buffer = new Float32Array(this._cellValueBuffers[bufferIndex]);
183
buffer.fill(0, 0, buffer.length);
184
this._device.queue.writeBuffer(this._cellBindBuffer, 0, buffer.buffer, 0, buffer.byteLength);
185
}
186
}
187
188
update(viewportData: ViewportData, viewLineOptions: ViewLineOptions): number {
189
// IMPORTANT: This is a hot function. Variables are pre-allocated and shared within the
190
// loop. This is done so we don't need to trust the JIT compiler to do this optimization to
191
// avoid potential additional blocking time in garbage collector which is a common cause of
192
// dropped frames.
193
194
let chars = '';
195
let segment: string | undefined;
196
let charWidth = 0;
197
let y = 0;
198
let x = 0;
199
let absoluteOffsetX = 0;
200
let absoluteOffsetY = 0;
201
let tabXOffset = 0;
202
let glyph: Readonly<ITextureAtlasPageGlyph>;
203
let cellIndex = 0;
204
205
let tokenStartIndex = 0;
206
let tokenEndIndex = 0;
207
let tokenMetadata = 0;
208
209
let decorationStyleSetBold: boolean | undefined;
210
let decorationStyleSetColor: number | undefined;
211
let decorationStyleSetOpacity: number | undefined;
212
213
let lineData: ViewLineRenderingData;
214
let decoration: InlineDecoration;
215
let fillStartIndex = 0;
216
let fillEndIndex = 0;
217
218
let tokens: IViewLineTokens;
219
220
const dpr = getActiveWindow().devicePixelRatio;
221
let contentSegmenter: IContentSegmenter;
222
223
if (!this._scrollInitialized) {
224
this.onScrollChanged();
225
this._scrollInitialized = true;
226
}
227
228
// Zero out cell buffer or rebuild if needed
229
if (this._cellBindBufferLineCapacity < viewportData.endLineNumber - viewportData.startLineNumber + 1) {
230
this._rebuildCellBuffer(viewportData.endLineNumber - viewportData.startLineNumber + 1);
231
}
232
const cellBuffer = new Float32Array(this._cellValueBuffers[this._activeDoubleBufferIndex]);
233
cellBuffer.fill(0);
234
235
const lineIndexCount = ViewportRenderStrategy.maxSupportedColumns * Constants.IndicesPerCell;
236
237
for (y = viewportData.startLineNumber; y <= viewportData.endLineNumber; y++) {
238
239
// Only attempt to render lines that the GPU renderer can handle
240
if (!this._viewGpuContext.canRender(viewLineOptions, viewportData, y)) {
241
continue;
242
}
243
244
lineData = viewportData.getViewLineRenderingData(y);
245
tabXOffset = 0;
246
247
contentSegmenter = createContentSegmenter(lineData, viewLineOptions);
248
charWidth = viewLineOptions.spaceWidth * dpr;
249
absoluteOffsetX = 0;
250
251
tokens = lineData.tokens;
252
tokenStartIndex = lineData.minColumn - 1;
253
tokenEndIndex = 0;
254
for (let tokenIndex = 0, tokensLen = tokens.getCount(); tokenIndex < tokensLen; tokenIndex++) {
255
tokenEndIndex = tokens.getEndOffset(tokenIndex);
256
if (tokenEndIndex <= tokenStartIndex) {
257
// The faux indent part of the line should have no token type
258
continue;
259
}
260
261
tokenMetadata = tokens.getMetadata(tokenIndex);
262
263
for (x = tokenStartIndex; x < tokenEndIndex; x++) {
264
// Only render lines that do not exceed maximum columns
265
if (x > ViewportRenderStrategy.maxSupportedColumns) {
266
break;
267
}
268
segment = contentSegmenter.getSegmentAtIndex(x);
269
if (segment === undefined) {
270
continue;
271
}
272
chars = segment;
273
274
if (!(lineData.isBasicASCII && viewLineOptions.useMonospaceOptimizations)) {
275
charWidth = this.glyphRasterizer.getTextMetrics(chars).width;
276
}
277
278
decorationStyleSetColor = undefined;
279
decorationStyleSetBold = undefined;
280
decorationStyleSetOpacity = undefined;
281
282
// Apply supported inline decoration styles to the cell metadata
283
for (decoration of lineData.inlineDecorations) {
284
// This is Range.strictContainsPosition except it works at the cell level,
285
// it's also inlined to avoid overhead.
286
if (
287
(y < decoration.range.startLineNumber || y > decoration.range.endLineNumber) ||
288
(y === decoration.range.startLineNumber && x < decoration.range.startColumn - 1) ||
289
(y === decoration.range.endLineNumber && x >= decoration.range.endColumn - 1)
290
) {
291
continue;
292
}
293
294
const rules = ViewGpuContext.decorationCssRuleExtractor.getStyleRules(this._viewGpuContext.canvas.domNode, decoration.inlineClassName);
295
for (const rule of rules) {
296
for (const r of rule.style) {
297
const value = rule.styleMap.get(r)?.toString() ?? '';
298
switch (r) {
299
case 'color': {
300
// TODO: This parsing and error handling should move into canRender so fallback
301
// to DOM works
302
const parsedColor = Color.Format.CSS.parse(value);
303
if (!parsedColor) {
304
throw new BugIndicatingError('Invalid color format ' + value);
305
}
306
decorationStyleSetColor = parsedColor.toNumber32Bit();
307
break;
308
}
309
case 'font-weight': {
310
const parsedValue = parseCssFontWeight(value);
311
if (parsedValue >= 400) {
312
decorationStyleSetBold = true;
313
// TODO: Set bold (https://github.com/microsoft/vscode/issues/237584)
314
} else {
315
decorationStyleSetBold = false;
316
// TODO: Set normal (https://github.com/microsoft/vscode/issues/237584)
317
}
318
break;
319
}
320
case 'opacity': {
321
const parsedValue = parseCssOpacity(value);
322
decorationStyleSetOpacity = parsedValue;
323
break;
324
}
325
default: throw new BugIndicatingError('Unexpected inline decoration style');
326
}
327
}
328
}
329
}
330
331
if (chars === ' ' || chars === '\t') {
332
// Zero out glyph to ensure it doesn't get rendered
333
cellIndex = ((y - 1) * ViewportRenderStrategy.maxSupportedColumns + x) * Constants.IndicesPerCell;
334
cellBuffer.fill(0, cellIndex, cellIndex + CellBufferInfo.FloatsPerEntry);
335
// Adjust xOffset for tab stops
336
if (chars === '\t') {
337
// Find the pixel offset between the current position and the next tab stop
338
const offsetBefore = x + tabXOffset;
339
tabXOffset = CursorColumns.nextRenderTabStop(x + tabXOffset, lineData.tabSize);
340
absoluteOffsetX += charWidth * (tabXOffset - offsetBefore);
341
// Convert back to offset excluding x and the current character
342
tabXOffset -= x + 1;
343
} else {
344
absoluteOffsetX += charWidth;
345
}
346
continue;
347
}
348
349
const decorationStyleSetId = ViewGpuContext.decorationStyleCache.getOrCreateEntry(decorationStyleSetColor, decorationStyleSetBold, decorationStyleSetOpacity);
350
glyph = this._viewGpuContext.atlas.getGlyph(this.glyphRasterizer, chars, tokenMetadata, decorationStyleSetId, absoluteOffsetX);
351
352
absoluteOffsetY = Math.round(
353
// Top of layout box (includes line height)
354
viewportData.relativeVerticalOffset[y - viewportData.startLineNumber] * dpr +
355
356
// Delta from top of layout box (includes line height) to top of the inline box (no line height)
357
Math.floor((viewportData.lineHeight * dpr - (glyph.fontBoundingBoxAscent + glyph.fontBoundingBoxDescent)) / 2) +
358
359
// Delta from top of inline box (no line height) to top of glyph origin. If the glyph was drawn
360
// with a top baseline for example, this ends up drawing the glyph correctly using the alphabetical
361
// baseline.
362
glyph.fontBoundingBoxAscent
363
);
364
365
cellIndex = ((y - viewportData.startLineNumber) * ViewportRenderStrategy.maxSupportedColumns + x) * Constants.IndicesPerCell;
366
cellBuffer[cellIndex + CellBufferInfo.Offset_X] = Math.floor(absoluteOffsetX);
367
cellBuffer[cellIndex + CellBufferInfo.Offset_Y] = absoluteOffsetY;
368
cellBuffer[cellIndex + CellBufferInfo.GlyphIndex] = glyph.glyphIndex;
369
cellBuffer[cellIndex + CellBufferInfo.TextureIndex] = glyph.pageIndex;
370
371
// Adjust the x pixel offset for the next character
372
absoluteOffsetX += charWidth;
373
}
374
375
tokenStartIndex = tokenEndIndex;
376
}
377
378
// Clear to end of line
379
fillStartIndex = ((y - viewportData.startLineNumber) * ViewportRenderStrategy.maxSupportedColumns + tokenEndIndex) * Constants.IndicesPerCell;
380
fillEndIndex = ((y - viewportData.startLineNumber) * ViewportRenderStrategy.maxSupportedColumns) * Constants.IndicesPerCell;
381
cellBuffer.fill(0, fillStartIndex, fillEndIndex);
382
}
383
384
const visibleObjectCount = (viewportData.endLineNumber - viewportData.startLineNumber + 1) * lineIndexCount;
385
386
// This render strategy always uploads the whole viewport
387
this._device.queue.writeBuffer(
388
this._cellBindBuffer,
389
0,
390
cellBuffer.buffer,
391
0,
392
(viewportData.endLineNumber - viewportData.startLineNumber) * lineIndexCount * Float32Array.BYTES_PER_ELEMENT
393
);
394
395
this._activeDoubleBufferIndex = this._activeDoubleBufferIndex ? 0 : 1;
396
397
this._visibleObjectCount = visibleObjectCount;
398
399
return visibleObjectCount;
400
}
401
402
draw(pass: GPURenderPassEncoder, viewportData: ViewportData): void {
403
if (this._visibleObjectCount <= 0) {
404
throw new BugIndicatingError('Attempt to draw 0 objects');
405
}
406
pass.draw(quadVertices.length / 2, this._visibleObjectCount);
407
}
408
}
409
410
function parseCssFontWeight(value: string) {
411
switch (value) {
412
case 'lighter':
413
case 'normal': return 400;
414
case 'bolder':
415
case 'bold': return 700;
416
}
417
return parseInt(value);
418
}
419
420
function parseCssOpacity(value: string): number {
421
if (value.endsWith('%')) {
422
return parseFloat(value.substring(0, value.length - 1)) / 100;
423
}
424
if (value.match(/^\d+(?:\.\d*)/)) {
425
return parseFloat(value);
426
}
427
return 1;
428
}
429
430