diff --git a/modules/core/src/adapter/device.ts b/modules/core/src/adapter/device.ts index f54b764f9..f009d871c 100644 --- a/modules/core/src/adapter/device.ts +++ b/modules/core/src/adapter/device.ts @@ -2,7 +2,6 @@ // SPDX-License-Identifier: MIT // Copyright (c) vis.gl contributors -import {VERSION} from '../init'; import {StatsManager, lumaStats} from '../utils/stats-manager'; import {log} from '../utils/log'; import {uid} from '../utils/uid'; @@ -294,8 +293,6 @@ export abstract class Device { return 'Device'; } - static VERSION = VERSION; - constructor(props: DeviceProps) { this.props = {...Device.defaultProps, ...props}; this.id = this.props.id || uid(this[Symbol.toStringTag].toLowerCase()); diff --git a/modules/core/src/adapter/luma.ts b/modules/core/src/adapter/luma.ts index b48ce66a5..6a92cbcea 100644 --- a/modules/core/src/adapter/luma.ts +++ b/modules/core/src/adapter/luma.ts @@ -9,6 +9,13 @@ import {StatsManager} from '../utils/stats-manager'; import {lumaStats} from '../utils/stats-manager'; import {log} from '../utils/log'; +declare global { + // eslint-disable-next-line no-var + var luma: Luma; +} + +const STARTUP_MESSAGE = 'set luma.log.level=1 (or higher) to trace rendering'; + const ERROR_MESSAGE = 'No matching device found. Ensure `@luma.gl/webgl` and/or `@luma.gl/webgpu` modules are imported.'; @@ -43,11 +50,39 @@ export class Luma { /** Global stats for all devices */ readonly stats: StatsManager = lumaStats; - /** Global log */ + /** + * Global log + * + * Assign luma.log.level in console to control logging: \ + * 0: none, 1: minimal, 2: verbose, 3: attribute/uniforms, 4: gl logs + * luma.log.break[], set to gl funcs, luma.log.profile[] set to model names`; + */ readonly log: Log = log; + /** Version of luma.gl */ + readonly VERSION: string = + // Version detection using build plugin + // @ts-expect-error no-undef + typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'running from source'; + protected deviceMap = new Map(); + constructor() { + if (globalThis.luma) { + if (globalThis.luma.VERSION !== this.VERSION) { + log.error(`Found luma.gl ${globalThis.luma.VERSION} while initialzing ${this.VERSION}`)(); + log.error(`'yarn why @luma.gl/core' can help identify the source of the conflict`)(); + throw new Error(`luma.gl - multiple versions detected: see console log`); + } + + log.error('This version of luma.gl has already been initialized')(); + } + + log.log(1, `${this.VERSION} - ${STARTUP_MESSAGE}`)(); + + globalThis.luma = this; + } + registerDevices(deviceClasses: DeviceFactory[]): void { for (const deviceClass of deviceClasses) { this.deviceMap.set(deviceClass.type, deviceClass); diff --git a/modules/core/src/index.ts b/modules/core/src/index.ts index 94edd113a..64506b1a5 100644 --- a/modules/core/src/index.ts +++ b/modules/core/src/index.ts @@ -2,8 +2,6 @@ // SPDX-License-Identifier: MIT // Copyright (c) vis.gl contributors -export {VERSION} from './init'; - // MAIN API ACCESS POINT export {luma} from './adapter/luma'; diff --git a/modules/core/src/init.ts b/modules/core/src/init.ts deleted file mode 100644 index 15194488f..000000000 --- a/modules/core/src/init.ts +++ /dev/null @@ -1,53 +0,0 @@ -// luma.gl -// SPDX-License-Identifier: MIT -// Copyright (c) vis.gl contributors - -import {isBrowser} from '@probe.gl/env'; -import {log} from './utils/log'; -import {lumaStats} from './utils/stats-manager'; - -declare global { - // eslint-disable-next-line no-var - var luma: any; -} - -/** - * By adding the result of init() to Device.VERSION we guarantee it will be called - * @returns version - */ -function initializeLuma(): string { - // Version detection using babel plugin - // @ts-expect-error - const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'running from source'; - - const STARTUP_MESSAGE = 'set luma.log.level=1 (or higher) to trace rendering'; - // Assign luma.log.level in console to control logging: \ - // 0: none, 1: minimal, 2: verbose, 3: attribute/uniforms, 4: gl logs - // luma.log.break[], set to gl funcs, luma.log.profile[] set to model names`; - - if (globalThis.luma && globalThis.luma.VERSION !== VERSION) { - throw new Error( - `luma.gl - multiple VERSIONs detected: ${globalThis.luma.VERSION} vs ${VERSION}` - ); - } - - if (!globalThis.luma) { - if (isBrowser()) { - log.log(1, `${VERSION} - ${STARTUP_MESSAGE}`)(); - } - - globalThis.luma = globalThis.luma || { - VERSION, - version: VERSION, - log, - - // A global stats object that various components can add information to - // E.g. see webgl/resource.js - stats: lumaStats - }; - } - - return VERSION; -} - -export const VERSION = initializeLuma(); diff --git a/modules/webgl/src/context/debug/spector.ts b/modules/webgl/src/context/debug/spector.ts index 95f3282d8..a394feb7a 100644 --- a/modules/webgl/src/context/debug/spector.ts +++ b/modules/webgl/src/context/debug/spector.ts @@ -52,7 +52,7 @@ export function initializeSpectorJS(props?: SpectorProps) { log.probe(LOG_LEVEL, 'SPECTOR found and initialized')(); spector = new globalThis.SPECTOR.Spector(); if (globalThis.luma) { - globalThis.luma.spector = spector; + (globalThis.luma as any).spector = spector; } }