Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ibgreen committed Sep 22, 2024
1 parent 6c02eb2 commit a229656
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
64 changes: 32 additions & 32 deletions modules/core/src/adapter/luma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,38 @@ export class Luma {
globalThis.luma = this;
}

/** Creates a device. Asynchronously. */
async createDevice(props_: CreateDeviceProps = {}): Promise<Device> {
const props: Required<CreateDeviceProps> = {...Luma.defaultProps, ...props_};

const adapter = this.selectAdapter(props.type, props.adapters);
if (!adapter) {
throw new Error(ERROR_MESSAGE);
}

// Wait for page to load so that CanvasContext's can access the DOM.
if (props.waitForPageLoad) {
await adapter.pageLoaded;
}

return await adapter.create(props);
}

/**
* Attach to an existing GPU API handle (WebGL2RenderingContext or GPUDevice).
* @param handle Externally created WebGL context or WebGPU device
*/
async attachDevice(handle: unknown, props: AttachDeviceProps): Promise<Device> {
const type = this._getTypeFromHandle(handle, props.adapters);

const adapter = type && this.selectAdapter(type, props.adapters);
if (!adapter) {
throw new Error(ERROR_MESSAGE);
}

return await adapter?.attach?.(handle, props);
}

/**
* Global adapter registration.
* @deprecated Use props.adapters instead
Expand Down Expand Up @@ -131,38 +163,6 @@ export class Luma {
return (selectedType && adapterMap.get(selectedType)) || null;
}

/** Creates a device. Asynchronously. */
async createDevice(props_: CreateDeviceProps = {}): Promise<Device> {
const props: Required<CreateDeviceProps> = {...Luma.defaultProps, ...props_};

const adapter = this.selectAdapter(props.type, props.adapters);
if (!adapter) {
throw new Error(ERROR_MESSAGE);
}

// Wait for page to load so that CanvasContext's can access the DOM.
if (props.waitForPageLoad) {
await adapter.pageLoaded;
}

return await adapter.create(props);
}

/**
* Attach to an existing GPU API handle (WebGL2RenderingContext or GPUDevice).
* @param handle Externally created WebGL context or WebGPU device
*/
async attachDevice(handle: unknown, props: AttachDeviceProps): Promise<Device> {
const type = this._getTypeFromHandle(handle, props.adapters);

const adapter = type && this.selectAdapter(type, props.adapters);
if (!adapter) {
throw new Error(ERROR_MESSAGE);
}

return await adapter?.attach?.(handle, props);
}

/**
* Override `HTMLCanvasContext.getCanvas()` to always create WebGL2 contexts with additional WebGL1 compatibility.
* Useful when attaching luma to a context from an external library does not support creating WebGL2 contexts.
Expand Down
10 changes: 5 additions & 5 deletions modules/core/test/adapter/luma.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {nullAdapter} from '@luma.gl/test-utils';
import {luma} from '@luma.gl/core';

test('luma#attachDevice', async t => {
const device = await luma.attachDevice({handle: null, adapters: [nullAdapter]});
const device = await luma.attachDevice(null, {adapters: [nullAdapter]});
t.equal(device.type, 'null', 'info.vendor ok');
t.equal(device.info.vendor, 'no one', 'info.vendor ok');
t.equal(device.info.renderer, 'none', 'info.renderer ok');
Expand All @@ -23,7 +23,7 @@ test('luma#createDevice', async t => {
});

test('luma#registerAdapters', async t => {
luma.registerAdapters([NullDevice]);
luma.registerAdapters([nullAdapter]);
const device = await luma.createDevice({type: 'null'});
t.equal(device.type, 'null', 'info.vendor ok');
t.equal(device.info.vendor, 'no one', 'info.vendor ok');
Expand All @@ -37,12 +37,12 @@ test('luma#getSupportedAdapters', async t => {
t.ok(types.includes('null'), 'null device is supported');
});

test('luma#getBestAvailableDeviceType', async t => {
test('luma#getBestAvailableAdapterType', async t => {
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();
t.ok(typeof types === 'string', 'does not crash');
const type = luma.getBestAvailableAdapterType();
t.ok(typeof type === 'string', 'does not crash');
});

// To suppress @typescript-eslint/unbound-method
Expand Down

0 comments on commit a229656

Please sign in to comment.