Skip to content

Modernization #809

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

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Prev Previous commit
Restore the browser compatibility layer of the GL module (my fault)
  • Loading branch information
tq-xyy committed Jan 29, 2023
commit 8057e04d3914f09b480eede05506757806bbcaf8
51 changes: 51 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
dist
node_modules
test/all.html

*.md

# lock files
yarn.lock
pnpm-lock.yaml
package-lock.json

# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# node-waf configuration
.lock-wscript

# Dependency directory
node_modules

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

# intellij
.idea

#yarn
yarn.lock

# OSX .DS_Store
.DS_Store

# build result
dist
test/all.html
36 changes: 18 additions & 18 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"arrowParens": "avoid",
"bracketSpacing": true,
"endOfLine": "lf",
"htmlWhitespaceSensitivity": "css",
"insertPragma": false,
"jsxSingleQuote": false,
"printWidth": 79,
"proseWrap": "preserve",
"quoteProps": "as-needed",
"requirePragma": false,
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5",
"useTabs": false,
"vueIndentScriptAndStyle": false
}
{
"arrowParens": "avoid",
"bracketSpacing": true,
"endOfLine": "lf",
"htmlWhitespaceSensitivity": "css",
"insertPragma": false,
"jsxSingleQuote": false,
"printWidth": 79,
"proseWrap": "preserve",
"quoteProps": "as-needed",
"requirePragma": false,
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5",
"useTabs": false,
"vueIndentScriptAndStyle": false
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"devDependencies": {
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-replace": "^5.0.2",
"@rollup/plugin-terser": "^0.4.0",
Expand All @@ -50,7 +51,7 @@
"test": "qunit test/issues test/internal test/features",
"test:browser": "node scripts/build-tests.js",
"coverage": "c8 qunit test/issues test/internal test/features",
"lint": "prettier -w *.js *.json src test scripts examples",
"lint": "prettier -w . --cache --no-color --loglevel warn",
"build": "rollup -c --bundleConfigAsCjs",
"watch": "pnpm run build --watch"
},
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 38 additions & 12 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { defineConfig } from 'rollup';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import terser from '@rollup/plugin-terser';
import json from '@rollup/plugin-json';

import fs from 'fs';

Expand All @@ -21,6 +22,39 @@ const banner = `/**
* Copyright (c) ${new Date().getFullYear()} gpu.js Team
*/`;

/**
*
* @returns {import('rollup').Plugin}
*/
function pluginReplaceGL() {
const glID = 'gl?replaceEntry';
const content = `export { default } from 'gl/src/javascript/browser-index';`;

return {
resolveId(id) {
if (id === 'gl') {
return glID;
}
},
load(id) {
if (id === glID) {
return content;
}
},
};
}

function commonConfig(plugins = []) {
return defineConfig({
plugins: [resolve(), commonjs(), json(), ...plugins],
onwarn(msg, warn) {
if (!/Circular/.test(msg)) {
warn(msg);
}
},
});
}

function createOutput(name, format, opts, minify = false) {
return {
banner,
Expand All @@ -40,37 +74,29 @@ function buildBrowser(isCore) {

return defineConfig({
input: './src/browser.js',
plugins: [resolve(), commonjs()],
output: [
createOutput(id, 'umd', options),
createOutput(id, 'umd', options, true),
createOutput(id + '.esm', 'esm'),
],
onwarn(msg, warn) {
if (!/Circular/.test(msg)) {
warn(msg);
}
},

external: isCore ? ['acorn'] : [],

...commonConfig([pluginReplaceGL()]),
});
}

function buildNode() {
return defineConfig({
input: './src/index.js',
plugins: [resolve(), commonjs()],
output: [
createOutput('gpu-node', 'cjs'),
createOutput('gpu-node.esm', 'esm'),
],
onwarn(msg, warn) {
if (!/Circular/.test(msg)) {
warn(msg);
}
},

external: Object.keys(pkg.dependencies).filter(v => v !== 'gpu-mock.js'),

...commonConfig(),
});
}

Expand Down
80 changes: 15 additions & 65 deletions src/browser.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,15 @@
import { GPU } from './gpu';
import { alias } from './alias';
import { utils } from './utils';
import { Input, input } from './input';
import { Texture } from './texture';
import { FunctionBuilder } from './backend/function-builder';
import { FunctionNode } from './backend/function-node';
import { CPUFunctionNode } from './backend/cpu/function-node';
import { CPUKernel } from './backend/cpu/kernel';

import { WebGLFunctionNode } from './backend/web-gl/function-node';
import { WebGLKernel } from './backend/web-gl/kernel';
import { kernelValueMaps as webGLKernelValueMaps } from './backend/web-gl/kernel-value-maps';

import { WebGL2FunctionNode } from './backend/web-gl2/function-node';
import { WebGL2Kernel } from './backend/web-gl2/kernel';
import { kernelValueMaps as webGL2KernelValueMaps } from './backend/web-gl2/kernel-value-maps';

import { Kernel } from './backend/kernel';

import { FunctionTracer } from './backend/function-tracer';

import mathRandom from './plugins/math-random-uniformly-distributed';

GPU.alias = alias;
GPU.utils = utils;
GPU.Input = Input;
GPU.input = input;
GPU.Texture = Texture;
GPU.FunctionBuilder = FunctionBuilder;
GPU.FunctionNode = FunctionNode;
GPU.CPUFunctionNode = CPUFunctionNode;
GPU.CPUKernel = CPUKernel;

GPU.WebGLFunctionNode = WebGLFunctionNode;
GPU.WebGLKernel = WebGLKernel;
GPU.webGLKernelValueMaps = webGLKernelValueMaps;

GPU.WebGL2FunctionNode = WebGL2FunctionNode;
GPU.WebGL2Kernel = WebGL2Kernel;
GPU.webGL2KernelValueMaps = webGL2KernelValueMaps;

GPU.Kernel = Kernel;
GPU.FunctionTracer = FunctionTracer;

GPU.plugins = {
mathRandom,
};

Object.defineProperties(GPU, {
GLKernel: {
get() {
console.warn('The browser build does not support GLKernel');
return CPUKernel;
},
},
HeadlessGLKernel: {
get() {
console.warn('The browser build does not support HeadlessGLKernel');
return CPUKernel;
},
},
});

export default GPU;
import * as lib from './index';
const GPU = lib.GPU;

for (const p in lib) {
if (!Object.prototype.hasOwnProperty.call(lib, p)) {
continue;
}
if (p === 'GPU') {
//prevent recursive reference
continue;
}
GPU[p] = lib[p];
}

export default lib;
24 changes: 7 additions & 17 deletions src/gpu.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { gpuMock } from 'gpu-mock.js';
import { utils } from './utils';
import { Kernel } from './backend/kernel';
import { CPUKernel } from './backend/cpu/kernel';
import { WebGL2Kernel } from './backend/web-gl2/kernel';
import { HeadlessGLKernel } from './backend/headless-gl/kernel';
import { Kernel } from './backend/kernel';
import { WebGLKernel } from './backend/web-gl/kernel';
import { WebGL2Kernel } from './backend/web-gl2/kernel';
import { kernelRunShortcut } from './kernel-run-shortcut';

import { utils } from './utils';
/**
*
* @type {Array.<Kernel>}
*/
export const kernelOrder = [WebGL2Kernel, WebGLKernel];
export const kernelOrder = [HeadlessGLKernel, WebGL2Kernel, WebGLKernel];

/**
*
Expand All @@ -19,19 +19,11 @@ export const kernelOrder = [WebGL2Kernel, WebGLKernel];
export const kernelTypes = ['gpu', 'cpu'];

const internalKernels = {
headlessgl: HeadlessGLKernel,
webgl2: WebGL2Kernel,
webgl: WebGLKernel,
};

/**
*
* @param {import('./backend/headless-gl/kernel').HeadlessGLKernel} HeadlessGLKernel
*/
export function setupNode(HeadlessGLKernel) {
kernelOrder.unshift(HeadlessGLKernel);
internalKernels.headlessgl = HeadlessGLKernel;
}

let validate = true;

/**
Expand Down Expand Up @@ -91,9 +83,7 @@ export class GPU {
* @desc TRUE if platform supports HeadlessGL
*/
static get isHeadlessGLSupported() {
return (
'headlessgl' in internalKernels && internalKernels.headlessgl.isSupported
);
return HeadlessGLKernel.isSupported;
}

/**
Expand Down
5 changes: 0 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,3 @@ import mathRandom from './plugins/math-random-uniformly-distributed';
export const plugins = {
mathRandom,
};

import { setupNode } from './gpu';
import { HeadlessGLKernel } from './backend/headless-gl/kernel';

setupNode(HeadlessGLKernel);