Skip to content

Commit

Permalink
chore(core): Move out deepEqual, getShaderInfo (#2005)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibgreen committed Mar 6, 2024
1 parent de61062 commit cedc3a1
Show file tree
Hide file tree
Showing 18 changed files with 77 additions and 114 deletions.
2 changes: 1 addition & 1 deletion modules/core/src/adapter/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import {VERSION} from '../init';
import {StatsManager, lumaStats} from '../utils/stats-manager';
import {log} from '../utils/log';
import {uid} from '../utils/utils';
import {uid} from '../utils/uid';
import type {TextureFormat} from '../type-utils//texture-formats';
import type {CanvasContext, CanvasContextProps} from './canvas-context';
import type {BufferProps} from './resources/buffer';
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/adapter/resources/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Copyright (c) vis.gl contributors

import type {Device} from '../device';
import {uid} from '../../utils/utils';
import {uid} from '../../utils/uid';

export type ResourceProps = {
/** Name of resource, mainly for debugging purposes. A unique name will be assigned if not provided */
Expand Down
14 changes: 10 additions & 4 deletions modules/core/src/adapter/resources/shader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import type {Device} from '../device';
import {Resource, ResourceProps} from './resource';
// import { log } from '../../utils/log';
import {uid} from '../../utils/utils';
import {uid} from '../../utils/uid';
import {CompilerMessage} from '../../portable/compiler-log/compiler-message';
import {formatCompilerLog} from '../../portable/compiler-log/format-compiler-log';
import {getShaderInfo} from '../../portable/compiler-log/get-shader-info';

/**
* Properties for a Shader
Expand Down Expand Up @@ -108,7 +107,7 @@ export abstract class Shader extends Resource<ShaderProps> {
return;
}

const shaderName: string = getShaderInfo(this.source).name;
const shaderName: string = getShaderName(this.source);
const shaderTitle: string = `${this.stage} ${shaderName}`;
let htmlLog = formatCompilerLog(messages, this.source, {showSourceCode: 'all', html: true});
// Show translated source if available
Expand Down Expand Up @@ -151,5 +150,12 @@ ${htmlLog}

/** Deduce an id, from shader source, or supplied id, or shader type */
function getShaderIdFromProps(props: ShaderProps): string {
return getShaderInfo(props.source).name || props.id || uid(`unnamed ${props.stage}-shader`);
return getShaderName(props.source) || props.id || uid(`unnamed ${props.stage}-shader`);
}

/** Extracts GLSLIFY style naming of shaders: `#define SHADER_NAME ...` */
function getShaderName(shader: string, defaultName: string = 'unnamed'): string {
const SHADER_NAME_REGEXP = /#define[\s*]SHADER_NAME[\s*]([A-Za-z0-9_-]+)[\s*]/;
const match = SHADER_NAME_REGEXP.exec(shader);
return match ? match[1] : defaultName;
}
8 changes: 4 additions & 4 deletions modules/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,14 @@ export {

export {StatsManager} from './utils/stats-manager';
export {log} from './utils/log';
export {uid, isObjectEmpty} from './utils/utils';
export {isUniformValue, splitUniformsAndBindings} from './portable/uniforms/uniform';
export {setPathPrefix, loadFile, loadImage, loadImageBitmap, loadScript} from './utils/load-file';
export {setPathPrefix, loadImage, loadImageBitmap} from './utils/load-file';
export {getScratchArrayBuffer, getScratchArray, fillArray} from './utils/array-utils-flat';
export {makeRandomNumberGenerator, random} from './utils/random';
export {deepEqual} from './utils/deep-equal';

// SHADER HELPERS../../engine/src/animation-loop/request-animation-frame
export {uid} from './utils/uid';
export {isObjectEmpty} from './utils/is-object-empty';
// export {deepEqual} from './utils/deep-equal';

/**
* Marks GLSL shaders for syntax highlighting: glsl`...`
Expand Down
42 changes: 0 additions & 42 deletions modules/core/src/portable/compiler-log/get-shader-info.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,6 @@
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors

const uidCounters: Record<string, number> = {};

/**
* Returns a UID.
* @param id= - Identifier base name
* @return uid
**/
export function uid(id: string = 'id'): string {
uidCounters[id] = uidCounters[id] || 1;
const count = uidCounters[id]++;
return `${id}-${count}`;
}

/** Returns true if given object is empty, false otherwise. */
export function isObjectEmpty(obj: object): boolean {
let isEmpty = true;
Expand Down
40 changes: 1 addition & 39 deletions modules/core/src/utils/load-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,7 @@ export function setPathPrefix(prefix: string) {
pathPrefix = prefix;
}

/**
* Reads raw file data. Respects setPathPrefix.
*/
export async function loadFile(
url: string,
options?: {dataType?: 'text' | 'arrayBuffer'} & RequestInit
): Promise<any> {
url = url.startsWith('http') ? url : pathPrefix + url;
const dataType = options?.dataType || 'text';
const response = await fetch(url, options);
return await response[dataType]();
}
// TODO - keep only loadImageBitmap

/**
* Loads ImageBitmap asynchronously. Respects setPathPrefix.
Expand Down Expand Up @@ -62,30 +51,3 @@ export async function loadImage(
}
});
}

/**
* Load a script (identified by an url). When the url returns, the
* content of this file is added into a new script element, attached to the DOM (body element)
* @param scriptUrl defines the url of the script to laod
* @param scriptId defines the id of the script element
*/
export async function loadScript(scriptUrl: string, scriptId?: string): Promise<Event> {
const head = document.getElementsByTagName('head')[0];
if (!head) {
throw new Error('loadScript');
}

const script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', scriptUrl);
if (scriptId) {
script.id = scriptId;
}

return new Promise((resolve, reject) => {
script.onload = resolve;
script.onerror = error =>
reject(new Error(`Unable to load script '${scriptUrl}': ${error as string}`));
head.appendChild(script);
});
}
16 changes: 16 additions & 0 deletions modules/core/src/utils/uid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// luma.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors

const uidCounters: Record<string, number> = {};

/**
* Returns a UID.
* @param id= - Identifier base name
* @return uid
**/
export function uid(id: string = 'id'): string {
uidCounters[id] = uidCounters[id] || 1;
const count = uidCounters[id]++;
return `${id}-${count}`;
}
9 changes: 3 additions & 6 deletions modules/core/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
// general utils
import './lib/utils/array-utils-flat.spec';
import './lib/utils/utils.spec';
import './lib/utils/deep-equal.spec';
import './lib/utils/uniform.spec';
import './lib/utils/uid.spec';

// type utils
import './adapter/attribute-utils/get-attribute-from-layout.spec';

import './adapter/type-utils/decode-attribute-type.spec';

import './adapter/type-utils/decode-vertex-format.spec';
import './adapter/type-utils/decode-texture-format.spec';
import './adapter/type-utils/vertex-format-from-attribute.spec';

// adapter
import './adapter/attribute-utils/get-attribute-from-layout.spec';
import './adapter/canvas-context.spec';

// uniforms
import './lib/uniforms/uniform-buffer-layout.spec';
import './lib/uniforms/uniform.spec';

// compiler logs
import './lib/compiler-log/format-compiler-log.spec';
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion modules/engine/src/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {BufferLayout, Shader, VertexArray, TransformFeedback} from '@luma.g
import type {AttributeInfo, Binding, UniformValue, PrimitiveTopology} from '@luma.gl/core';
import {Device, DeviceFeature, Buffer, Texture, TextureView, Sampler} from '@luma.gl/core';
import {RenderPipeline, RenderPass, UniformStore} from '@luma.gl/core';
import {log, uid, deepEqual, isObjectEmpty, splitUniformsAndBindings} from '@luma.gl/core';
import {log, uid, isObjectEmpty, splitUniformsAndBindings} from '@luma.gl/core';
import {getTypedArrayFromDataType, getAttributeInfosFromLayouts} from '@luma.gl/core';

import type {ShaderModule, PlatformInfo} from '@luma.gl/shadertools';
Expand All @@ -21,6 +21,7 @@ import {PipelineFactory} from '../lib/pipeline-factory';
import {ShaderFactory} from '../lib/shader-factory';
import {getDebugTableForShaderLayout} from '../debug/debug-shader-layout';
import {debugFramebuffer} from '../debug/debug-framebuffer';
import {deepEqual} from '../utils/deep-equal';

const LOG_DRAW_PRIORITY = 2;
const LOG_DRAW_TIMEOUT = 10000;
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions modules/engine/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// utils
import './utils/deep-equal.spec';

// model etc
import './lib/model.spec';
import './lib/animation-loop.spec';
import './lib/pipeline-factory.spec';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'tape-promise/tape';
import {deepEqual} from '@luma.gl/core';
import {deepEqual} from '@luma.gl/engine/utils/deep-equal';

const obj = {longitude: -70, latitude: 40.7, zoom: 12};
const TEST_CASES = [
Expand Down
3 changes: 2 additions & 1 deletion modules/webgl/src/context/debug/spector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors

import {log, loadScript} from '@luma.gl/core';
import {log} from '@luma.gl/core';
import {loadScript} from '../../utils/load-script';

/** Spector debug initialization options */
type SpectorProps = {
Expand Down
3 changes: 2 additions & 1 deletion modules/webgl/src/context/debug/webgl-developer-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors

import {log, loadScript} from '@luma.gl/core';
import {log} from '@luma.gl/core';
// Rename constant to prevent inlining. We need the full set of constants for generating debug strings.
import {GL as GLEnum} from '@luma.gl/constants';
import {isBrowser} from '@probe.gl/env';
import {loadScript} from '../../utils/load-script';

const WEBGL_DEBUG_CDN_URL = 'https://unpkg.com/webgl-debug@2.0.1/index.js';

Expand Down
30 changes: 30 additions & 0 deletions modules/webgl/src/utils/load-script.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// luma.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors

/**
* Load a script (identified by an url). When the url returns, the
* content of this file is added into a new script element, attached to the DOM (body element)
* @param scriptUrl defines the url of the script to laod
* @param scriptId defines the id of the script element
*/
export async function loadScript(scriptUrl: string, scriptId?: string): Promise<Event> {
const head = document.getElementsByTagName('head')[0];
if (!head) {
throw new Error('loadScript');
}

const script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', scriptUrl);
if (scriptId) {
script.id = scriptId;
}

return new Promise((resolve, reject) => {
script.onload = resolve;
script.onerror = error =>
reject(new Error(`Unable to load script '${scriptUrl}': ${error as string}`));
head.appendChild(script);
});
}

0 comments on commit cedc3a1

Please sign in to comment.