Skip to content

Commit

Permalink
webgl
Browse files Browse the repository at this point in the history
  • Loading branch information
ibgreen committed Apr 30, 2024
1 parent df29722 commit e2aefd8
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 58 deletions.
2 changes: 1 addition & 1 deletion modules/core/src/adapter/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export abstract class Adapter {
abstract type: string;
abstract isSupported(): boolean;
abstract create(props: DeviceProps): Promise<Device>;
abstract attach?(handle: unknown): Device;
abstract attach?(handle: unknown): Promise<Device>;
}
15 changes: 1 addition & 14 deletions modules/core/src/adapter/luma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class Luma {
}

const adapter = adapters.get(type);
const device = adapter?.attach?.(null);
const device = await adapter?.attach?.(null);
if (device) {
return device;
}
Expand Down Expand Up @@ -212,19 +212,6 @@ export class Luma {
}
return map;
}

// DEPRECATED

/** @deprecated Use registerAdapters */
registerDevices(deviceClasses: any[]): void {
log.warn('luma.registerDevices() is deprecated, use luma.registerAdapters() instead');
for (const deviceClass of deviceClasses) {
const adapter = deviceClass.adapter as Adapter;
if (adapter) {
this.preregisteredAdapters.set(adapter.type, adapter);
}
}
}
}

/**
Expand Down
19 changes: 2 additions & 17 deletions modules/core/test/adapter/luma.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Copyright (c) vis.gl contributors

import test from 'tape-promise/tape';
import {nullAdapter, NullDevice} from '@luma.gl/test-utils';
import {nullAdapter} from '@luma.gl/test-utils';
import {luma} from '@luma.gl/core';

test('luma#attachDevice', async t => {
Expand All @@ -22,23 +22,8 @@ test('luma#createDevice', async t => {
t.end();
});

test('luma#registerDevices', async t => {
luma.registerDevices([NullDevice]);
const device = await luma.createDevice({type: 'unknown'});
t.equal(device.type, 'unknown', 'info.vendor ok');
t.equal(device.info.vendor, 'no one', 'info.vendor ok');
t.equal(device.info.renderer, 'none', 'info.renderer ok');
t.end();
});

test('luma#getSupportedDevices', async t => {
luma.registerDevices([NullDevice]);
const types = luma.getSupportedAdapters();
t.ok(types.includes('unknown'), 'null device is supported');
});

test('luma#getBestAvailableDeviceType', async t => {
luma.registerDevices([NullDevice]);
luma.registerAdapters([nullAdapter]);
// Somewhat dummy test, as tests rely on test utils registering webgl and webgpu devices
// But they might not be supported on all devices.
const types = luma.getBestAvailableAdapter();
Expand Down
4 changes: 2 additions & 2 deletions modules/test-utils/src/create-test-device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export function createTestContext(opts: Record<string, any> = {}): WebGL2Renderi
export function createTestDevice(props: DeviceProps = {}): WebGLDevice | null {
try {
props = {...CONTEXT_DEFAULTS, ...props, debug: true};
// TODO - We dont use luma.createDevice since this function currently expect the context to be created synchronously
return webgl2Adapter.createSync(props);
// TODO - We dont use luma.createDevice since createTestDevice currently expect WebGL context to be created synchronously
return new WebGLDevice(props);
} catch (error) {
// eslint-disable-next-line no-console
console.error(`Failed to created device '${props.id}': ${(error as Error).message}`);
Expand Down
2 changes: 1 addition & 1 deletion modules/test-utils/src/null-device/null-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class NullAdapter extends Adapter {
return true;
}

attach(handle: null): NullDevice {
async attach(handle: null): Promise<NullDevice> {
return new NullDevice({});
}

Expand Down
31 changes: 9 additions & 22 deletions modules/webgl/src/adapter/webgl-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors

import type {WebGLDevice} from './webgl-device';
import {Adapter, Device, DeviceProps, CanvasContext, log} from '@luma.gl/core';
import {WebGLDevice} from './webgl-device';
import {loadSpectorJS, DEFAULT_SPECTOR_PROPS} from '../context/debug/spector';
import {loadWebGLDeveloperTools} from '../context/debug/webgl-developer-tools';

Expand All @@ -15,12 +15,8 @@ export class WebGLAdapter extends Adapter {

constructor() {
super();

// Add spector default props to device default props, so that runtime settings are observed
Device.defaultProps = {...Device.defaultProps, ...DEFAULT_SPECTOR_PROPS};

// @ts-ignore DEPRECATED For backwards compatibility luma.registerDevices
WebGLDevice.adapter = this;
}

/** Check if WebGL 2 is available */
Expand All @@ -34,12 +30,13 @@ export class WebGLAdapter extends Adapter {
* @param gl
* @returns
*/
attach(gl: Device | WebGL2RenderingContext): WebGLDevice {
async attach(gl: Device | WebGL2RenderingContext): Promise<WebGLDevice> {
const {WebGLDevice} = await import('./webgl-device');
if (gl instanceof WebGLDevice) {
return gl;
}
// @ts-expect-error
if (gl?.device instanceof Device) {
if (gl?.device instanceof WebGLDevice) {
// @ts-expect-error
return gl.device as WebGLDevice;
}
Expand All @@ -50,6 +47,8 @@ export class WebGLAdapter extends Adapter {
}

async create(props: DeviceProps = {}): Promise<WebGLDevice> {
const {WebGLDevice} = await import('./webgl-device');

log.groupCollapsed(LOG_LEVEL, 'WebGLDevice created')();

const promises: Promise<unknown>[] = [];
Expand All @@ -60,7 +59,7 @@ export class WebGLAdapter extends Adapter {
}

if (props.debugWithSpectorJS) {
promises.push(loadSpectorJS(props));
promises.push(loadSpectorJS({...props, gl: undefined}));
}

// Wait for page to load: if canvas is a string we need to query the DOM for the canvas element.
Expand All @@ -80,20 +79,6 @@ export class WebGLAdapter extends Adapter {

log.probe(LOG_LEVEL + 1, 'DOM is loaded')();

const device = this.createSync(props);
log.groupEnd(LOG_LEVEL)();

return device;
}

/** Create or attach device synchronously. Not supported for all devices. */
createSync(props: DeviceProps = {}): WebGLDevice {
// @ts-expect-error
if (props.gl?.device) {
log.warn('reattaching existing device')();
return this.attach(props.gl);
}

const device = new WebGLDevice(props);

// Log some debug info about the newly created context
Expand All @@ -103,6 +88,8 @@ ${device.info.vendor}, ${device.info.renderer} for canvas: ${device.canvasContex
log.probe(LOG_LEVEL, message)();
log.table(LOG_LEVEL, device.info)();

log.groupEnd(LOG_LEVEL)();

return device;
}
}
Expand Down
2 changes: 1 addition & 1 deletion modules/webgpu/src/adapter/webgpu-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class WebGPUAdapter extends Adapter {
return device;
}

attach(handle: GPUDevice): WebGPUDevice {
attach(handle: GPUDevice): Promise<WebGPUDevice> {
throw new Error('WebGPUAdapter.attach() not implemented');
}
}
Expand Down

0 comments on commit e2aefd8

Please sign in to comment.