Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add editor gpu rendering fallback #228414

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/vs/editor/browser/gpu/gpuDisposable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@ import type { IReference } from '../../../base/common/lifecycle.js';
import { isFunction } from '../../../base/common/types.js';

export namespace GPULifecycle {
export async function requestDevice(): Promise<IReference<GPUDevice>> {
if (!navigator.gpu) {
throw new Error('This browser does not support WebGPU');
export async function requestDevice(fallback?: (message: string) => void): Promise<IReference<GPUDevice>> {
try {
if (!navigator.gpu) {
throw new Error('This browser does not support WebGPU');
}
const adapter = (await navigator.gpu.requestAdapter())!;
if (!adapter) {
throw new Error('This browser supports WebGPU but it appears to be disabled');
}
return wrapDestroyableInDisposable(await adapter.requestDevice());
} catch (e) {
if (fallback) {
fallback(e.message);
}
throw e;
}
const adapter = (await navigator.gpu.requestAdapter())!;
if (!adapter) {
throw new Error('This browser supports WebGPU but it appears to be disabled');
}
return wrapDestroyableInDisposable(await adapter.requestDevice());
}

export function createBuffer(device: GPUDevice, descriptor: GPUBufferDescriptor, initialValues?: Float32Array | (() => Float32Array)): IReference<GPUBuffer> {
Expand Down
15 changes: 13 additions & 2 deletions src/vs/editor/browser/gpu/viewGpuContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as nls from '../../../nls.js';
import { addDisposableListener, getActiveWindow } from '../../../base/browser/dom.js';
import { createFastDomNode, type FastDomNode } from '../../../base/browser/fastDomNode.js';
import { Disposable } from '../../../base/common/lifecycle.js';
Expand All @@ -12,6 +13,8 @@ import { observableValue, runOnChange, type IObservable } from '../../../base/co
import { IInstantiationService } from '../../../platform/instantiation/common/instantiation.js';
import { ViewLinesGpu } from '../viewParts/viewLinesGpu/viewLinesGpu.js';
import { TextureAtlas } from './atlas/textureAtlas.js';
import { IConfigurationService } from '../../../platform/configuration/common/configuration.js';
import { INotificationService, IPromptChoice, Severity } from '../../../platform/notification/common/notification.js';
import { GPULifecycle } from './gpuDisposable.js';
import { ensureNonNullable, observeDevicePixelDimensions } from './gpuUtils.js';

Expand All @@ -26,7 +29,9 @@ export class ViewGpuContext extends Disposable {
readonly devicePixelRatio: IObservable<number>;

constructor(
@IInstantiationService private readonly _instantiationService: IInstantiationService
@IInstantiationService private readonly _instantiationService: IInstantiationService,
@INotificationService private readonly _notificationService: INotificationService,
@IConfigurationService private readonly configurationService: IConfigurationService,
) {
super();

Expand All @@ -35,7 +40,13 @@ export class ViewGpuContext extends Disposable {

this.ctx = ensureNonNullable(this.canvas.domNode.getContext('webgpu'));

this.device = GPULifecycle.requestDevice().then(ref => this._register(ref).object);
this.device = GPULifecycle.requestDevice((message) => {
const choices: IPromptChoice[] = [{
label: nls.localize('editor.dom.render', "Use DOM-based rendering"),
run: () => this.configurationService.updateValue('editor.experimentalGpuAcceleration', 'off'),
}];
this._notificationService.prompt(Severity.Warning, message, choices);
}).then(ref => this._register(ref).object);
this.device.then(device => {
if (!ViewLinesGpu.atlas) {
ViewLinesGpu.atlas = this._instantiationService.createInstance(TextureAtlas, device.limits.maxTextureDimension2D, undefined);
Expand Down
14 changes: 8 additions & 6 deletions src/vs/editor/browser/viewParts/viewLinesGpu/viewLinesGpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ export class ViewLinesGpu extends ViewPart {
}));

// Rerender when the texture atlas deletes glyphs
this._register(ViewLinesGpu.atlas.onDidDeleteGlyphs(() => {
this._atlasGpuTextureVersions.length = 0;
this._atlasGpuTextureVersions[0] = 0;
this._atlasGpuTextureVersions[1] = 0;
this._renderStrategy.reset();
}));
if (ViewLinesGpu.atlas) {
this._register(ViewLinesGpu.atlas.onDidDeleteGlyphs(() => {
this._atlasGpuTextureVersions.length = 0;
this._atlasGpuTextureVersions[0] = 0;
this._atlasGpuTextureVersions[1] = 0;
this._renderStrategy.reset();
}));
}

this.initWebgpu();
}
Expand Down
Loading