Skip to content

Commit 0e4c049

Browse files
feat: Update for offscreen canvas and retool for cleaner CPU kernel backend
fix toString for kernels as well Added documentation Updated release year
1 parent 62d03ff commit 0e4c049

35 files changed

+852
-362
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License
22

3-
Copyright (c) 2017 gpu.js Team
3+
Copyright (c) 2018 gpu.js Team
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
[![Logo](http://gpu.rocks/img/ogimage.png)](http://gpu.rocks/)
22

33

4-
# gpu.js
5-
gpu.js is a JavaScript Acceleration library for GPGPU (General purpose computing on GPUs) in JavaScript. gpu.js will automatically compile simple JavaScript functions into shader language and run them on the GPU. In case a GPU is not available, the functions will still run in regular JavaScript.
4+
# GPU.js
5+
GPU.js is a JavaScript Acceleration library for GPGPU (General purpose computing on GPUs) in JavaScript. GPU.js will automatically compile simple JavaScript functions into shader language and run them on the GPU. In case a GPU is not available, the functions will still run in regular JavaScript.
66

77

88
[![Join the chat at https://gitter.im/gpujs/gpu.js](https://badges.gitter.im/gpujs/gpu.js.svg)](https://gitter.im/gpujs/gpu.js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
99
[![Slack](https://slack.bri.im/badge.svg)](https://slack.bri.im)
1010

1111
# What is this sorcery?
1212

13-
Matrix multiplication written in gpu.js:
13+
Matrix multiplication written in GPU.js:
1414

1515
```js
1616
const gpu = new GPU();
@@ -50,6 +50,7 @@ Or alternatively you can experiment around with the [kernel playground here](htt
5050
* [Adding Custom Functions Directly to Kernel](#adding-custom-functions-directly-to-kernel)
5151
* [Loops](#loops)
5252
* [Pipelining](#pipelining)
53+
* [Offscreen Canvas](#offscreen-canvas)
5354
* [Cleanup](#cleanup)
5455
* [Flattened typed array support](#flattened-typed-array-support)
5556
* [Supported Math functions](#supported-math-functions)
@@ -78,7 +79,7 @@ yarn add gpu.js
7879

7980
### Browser
8081

81-
Download the latest version of gpu.js and include the files in your HTML page using the following tags:
82+
Download the latest version of GPU.js and include the files in your HTML page using the following tags:
8283

8384
```html
8485
<script src="/path/to/js/gpu.min.js"></script>
@@ -405,12 +406,46 @@ const matMult = gpu.createKernel(function(a, b) {
405406
[Pipeline](https://en.wikipedia.org/wiki/Pipeline_(computing)) is a feature where values are sent directly from kernel to kernel via a texture.
406407
This results in extremely fast computing. This is achieved with the kernel option `outputToTexture: boolean` option or by calling `kernel.setOutputToTexture(true)`
407408

409+
## Offscreen Canvas
410+
GPU.js supports offscreen canvas where available. Here is an example of how to use it with two files, `gpu-worker.js`, and `index.js`:
411+
412+
file: `gpu-worker.js`
413+
```js
414+
importScripts('path/to/gpu.js');
415+
onmessage = function() {
416+
// define gpu instance
417+
const gpu = new GPU();
418+
419+
// input values
420+
const a = [1,2,3];
421+
const b = [3,2,1];
422+
423+
// setup kernel
424+
const kernel = gpu.createKernel(function(a, b) {
425+
return a[this.thread.x] - b[this.thread.x];
426+
})
427+
.setOutput([3]);
428+
429+
// output some results!
430+
postMessage(kernel(a, b));
431+
};
432+
```
433+
434+
file: `index.js`
435+
```js
436+
var worker = new Worker('gpu-worker.js');
437+
worker.onmessage = function(e) {
438+
var result = e.data;
439+
console.log(result);
440+
};
441+
```
442+
408443
## Cleanup
409444
* for instances of `GPU` use the `destroy` method. Example: `gpu.destroy()`
410445
* for instances of `Kernel` use the `destroy` method. Example: `kernel.destroy()`
411446

412447
## Flattened typed array support
413-
To use the useful `x`, `y`, `z` `thread` lookup api inside of gpu.js, and yet use flattened arrays, there is the `Input` type.
448+
To use the useful `x`, `y`, `z` `thread` lookup api inside of GPU.js, and yet use flattened arrays, there is the `Input` type.
414449
This is generally much faster for when sending values to the gpu, especially with larger data sets. Usage example:
415450
```js
416451
import GPU, { input } from 'gpu.js';
@@ -490,7 +525,7 @@ We promise never to pass off your code as ours.
490525

491526
The MIT License
492527

493-
Copyright (c) 2017 gpu.js Team
528+
Copyright (c) 2018 GPU.js Team
494529

495530
Permission is hereby granted, free of charge, to any person obtaining a copy
496531
of this software and associated documentation files (the "Software"), to deal

bin/gpu-core.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* GPU Accelerated JavaScript
66
*
7-
* @version 1.8.1
8-
* @date Fri Sep 21 2018 06:53:44 GMT-0400 (EDT)
7+
* @version 1.9.0
8+
* @date Wed Oct 24 2018 14:09:46 GMT-0400 (EDT)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -88,7 +88,7 @@ var UtilsCore = function () {
8888

8989

9090
value: function isCanvas(canvasObj) {
91-
return canvasObj !== null && canvasObj.nodeName && canvasObj.getContext && canvasObj.nodeName.toUpperCase() === 'CANVAS';
91+
return canvasObj !== null && (canvasObj.nodeName && canvasObj.getContext && canvasObj.nodeName.toUpperCase() === 'CANVAS' || typeof OffscreenCanvas !== 'undefined' && canvasObj instanceof OffscreenCanvas);
9292
}
9393

9494

@@ -106,7 +106,7 @@ var UtilsCore = function () {
106106
return null;
107107
}
108108

109-
var canvas = document.createElement('canvas');
109+
var canvas = typeof document !== 'undefined' ? document.createElement('canvas') : new OffscreenCanvas(0, 0);
110110

111111
canvas.width = 2;
112112
canvas.height = 2;
@@ -124,11 +124,25 @@ var UtilsCore = function () {
124124
}
125125

126126

127+
}, {
128+
key: 'isWebGl2',
129+
value: function isWebGl2(webGl2Obj) {
130+
return webGl2Obj && typeof WebGL2RenderingContext !== 'undefined' && webGl2Obj instanceof WebGL2RenderingContext;
131+
}
132+
133+
127134
}, {
128135
key: 'isWebGlSupported',
129136
value: function isWebGlSupported() {
130137
return _isWebGlSupported;
131138
}
139+
140+
141+
}, {
142+
key: 'isWebGl2Supported',
143+
value: function isWebGl2Supported() {
144+
return _isWebGl2Supported;
145+
}
132146
}, {
133147
key: 'isWebGlDrawBuffersSupported',
134148
value: function isWebGlDrawBuffersSupported() {
@@ -161,7 +175,16 @@ var UtilsCore = function () {
161175
throw new Error('Invalid canvas object - ' + canvasObj);
162176
}
163177

164-
var webGl = canvasObj.getContext('experimental-webgl', UtilsCore.initWebGlDefaultOptions()) || canvasObj.getContext('webgl', UtilsCore.initWebGlDefaultOptions());
178+
var webGl = null;
179+
var defaultOptions = UtilsCore.initWebGlDefaultOptions();
180+
try {
181+
webGl = canvasObj.getContext('experimental-webgl', defaultOptions);
182+
} catch (e) {
183+
}
184+
185+
if (webGl === null) {
186+
webGl = canvasObj.getContext('webgl2', defaultOptions) || canvasObj.getContext('webgl', defaultOptions);
187+
}
165188

166189
if (webGl) {
167190
webGl.OES_texture_float = webGl.getExtension('OES_texture_float');
@@ -194,6 +217,7 @@ var UtilsCore = function () {
194217
}, {
195218
key: 'checkOutput',
196219
value: function checkOutput(output) {
220+
if (!output || !Array.isArray(output)) throw new Error('kernel.output not an array');
197221
for (var i = 0; i < output.length; i++) {
198222
if (isNaN(output[i]) || output[i] < 1) {
199223
throw new Error('kernel.output[' + i + '] incorrectly defined as `' + output[i] + '`, needs to be numeric, and greater than 0');
@@ -206,9 +230,11 @@ var UtilsCore = function () {
206230
}();
207231

208232

209-
var _isCanvasSupported = typeof document !== 'undefined' ? UtilsCore.isCanvas(document.createElement('canvas')) : false;
233+
var _isCanvasSupported = typeof document !== 'undefined' ? UtilsCore.isCanvas(document.createElement('canvas')) : typeof OffscreenCanvas !== 'undefined';
210234
var _testingWebGl = UtilsCore.initWebGl(UtilsCore.initCanvas());
235+
var _testingWebGl2 = UtilsCore.initWebGl2(UtilsCore.initCanvas());
211236
var _isWebGlSupported = UtilsCore.isWebGl(_testingWebGl);
237+
var _isWebGl2Supported = UtilsCore.isWebGl2(_testingWebGl2);
212238
var _isWebGlDrawBuffersSupported = _isWebGlSupported && Boolean(_testingWebGl.getExtension('WEBGL_draw_buffers'));
213239

214240
if (_isWebGlSupported) {

bin/gpu-core.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)