Skip to content

Commit cc88928

Browse files
Merge pull request #551 from gpujs/550-expose-typings
feat: Provide overloading methods with types
2 parents f514e79 + 142f191 commit cc88928

File tree

7 files changed

+234
-133
lines changed

7 files changed

+234
-133
lines changed

README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const c = multiplyMatrix(a, b) as number[][];
7777

7878
NOTE: documentation is slightly out of date for the upcoming release of v2. We will fix it! In the mean time, if you'd like to assist (PLEASE) let us know.
7979

80+
* [Demos](#demos)
8081
* [Installation](#installation)
8182
* [`GPU` Settings](#gpu-settings)
8283
* [`gpu.createKernel` Settings](#gpucreatekernel-settings)
@@ -112,6 +113,31 @@ NOTE: documentation is slightly out of date for the upcoming release of v2. We
112113
* [Terms Explained](#terms-explained)
113114
* [License](#license)
114115

116+
## Demos
117+
GPU.js in the wild, all around the net. Add yours here!
118+
* [Temperature interpolation using GPU.js](https://observablehq.com/@rveciana/temperature-interpolation-using-gpu-js)
119+
* [Julia Set Fractal using GPU.js](https://observablehq.com/@ukabuer/julia-set-fractal-using-gpu-js)
120+
* [Hello, gpu.js v2](https://observablehq.com/@fil/hello-gpu-js-v2)
121+
* [Basic gpu.js canvas example](https://observablehq.com/@rveciana/basic-gpu-js-canvas-example)
122+
* [Raster projection with GPU.js](https://observablehq.com/@fil/raster-projection-with-gpu-js)
123+
* [GPU.js Example: Slow Fade](https://observablehq.com/@robertleeplummerjr/gpu-js-example-slow-fade)
124+
* [GPU.JS CA Proof of Concept](https://observablehq.com/@alexlamb/gpu-js-ca-proof-of-concept)
125+
* [Image Convolution using GPU.js](https://observablehq.com/@ukabuer/image-convolution-using-gpu-js)
126+
* [Leaflet + gpu.js canvas](https://observablehq.com/@rveciana/leaflet-gpu-js-canvas)
127+
* [Image to GPU.js](https://observablehq.com/@fil/image-to-gpu)
128+
* [GPU Accelerated Heatmap using gpu.js](https://observablehq.com/@tracyhenry/gpu-accelerated-heatmap-using-gpu-js)
129+
* [Dijkstra’s algorithm in gpu.js](https://observablehq.com/@fil/dijkstras-algorithm-in-gpu-js)
130+
* [Voronoi with gpu.js](https://observablehq.com/@fil/voronoi-with-gpu-js)
131+
* [The gpu.js loop](https://observablehq.com/@fil/the-gpu-js-loop)
132+
* [GPU.js Example: Mandelbrot Set](https://observablehq.com/@robertleeplummerjr/gpu-js-example-mandelbrot-set)
133+
* [GPU.js Example: Mandelbulb](https://observablehq.com/@robertleeplummerjr/gpu-js-example-mandelbulb)
134+
* [Inverse of the distance with gpu.js](https://observablehq.com/@rveciana/inverse-of-the-distance-with-gpu-js)
135+
* [gpu.js laser detection v2](https://observablehq.com/@robertleeplummerjr/gpu-js-laser-detection-v2)
136+
* [GPU.js Canvas](https://observablehq.com/@hubgit/gpu-js-canvas)
137+
* [Video Convolution using GPU.js](https://observablehq.com/@robertleeplummerjr/video-convolution-using-gpu-js)
138+
* [GPU Rock Paper Scissors](https://observablehq.com/@alexlamb/gpu-rock-paper-scissors)
139+
* [Shaded relief with gpujs and d3js](https://observablehq.com/@rveciana/shaded-relief-with-gpujs-and-d3js/2)
140+
115141
## Installation
116142
On Linux, ensure you have the correct header files installed: `sudo apt install mesa-common-dev libxi-dev` (adjust for your distribution)
117143

@@ -957,6 +983,75 @@ To assist with mostly unit tests, but perhaps in scenarios outside of GPU.js, th
957983

958984
## Typescript Typings
959985
Typescript is supported! Typings can be found [here](src/index.d.ts)!
986+
For strongly typed kernels:
987+
```ts
988+
import { GPU, IKernelFunctionThis } from './src';
989+
const gpu = new GPU();
990+
991+
function kernelFunction(this: IKernelFunctionThis): number {
992+
return 1 + this.thread.x;
993+
}
994+
995+
const kernelMap = gpu.createKernel<typeof kernelFunction>(kernelFunction)
996+
.setOutput([3,3,3]);
997+
998+
const result = kernelMap();
999+
1000+
console.log(result as number[][][]);
1001+
```
1002+
1003+
For strongly typed mapped kernels:
1004+
```ts
1005+
import { GPU, Texture, IKernelFunctionThis } from './src';
1006+
const gpu = new GPU();
1007+
1008+
function kernelFunction(this: IKernelFunctionThis): [number, number] {
1009+
return [1, 1];
1010+
}
1011+
1012+
function subKernel(): [number, number] {
1013+
return [1, 1];
1014+
}
1015+
1016+
const kernelMap = gpu.createKernelMap<typeof kernelFunction>({
1017+
test: subKernel,
1018+
}, kernelFunction)
1019+
.setOutput([1])
1020+
.setPipeline(true);
1021+
1022+
const result = kernelMap();
1023+
1024+
console.log((result.test as Texture).toArray() as [number, number][]);
1025+
```
1026+
1027+
For extending constants:
1028+
```ts
1029+
import { GPU, IKernelFunctionThis } from './src';
1030+
const gpu = new GPU();
1031+
1032+
interface IConstants {
1033+
screen: [number, number];
1034+
}
1035+
1036+
type This = {
1037+
constants: IConstants
1038+
} & IKernelFunctionThis;
1039+
1040+
function kernelFunction(this: This): number {
1041+
const { screen } = this.constants;
1042+
return 1 + screen[0];
1043+
}
1044+
1045+
const kernelMap = gpu.createKernel<typeof kernelFunction>(kernelFunction)
1046+
.setOutput([3,3,3])
1047+
.setConstants<IConstants>({
1048+
screen: [1, 1]
1049+
});
1050+
1051+
const result = kernelMap();
1052+
1053+
console.log(result as number[][][]);
1054+
```
9601055

9611056
## Destructured Assignments **New in V2!**
9621057
Destructured Objects and Arrays work in GPU.js.

dist/gpu-browser-core.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* GPU Accelerated JavaScript
66
*
7-
* @version 2.3.0
8-
* @date Tue Nov 26 2019 09:51:10 GMT-0500 (Eastern Standard Time)
7+
* @version 2.3.1
8+
* @date Tue Dec 17 2019 09:39:34 GMT-0500 (Eastern Standard Time)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -13389,7 +13389,7 @@ const utils = {
1338913389

1339013390
getVariableType(value, strictIntegers) {
1339113391
if (utils.isArray(value)) {
13392-
if (value[0].nodeName === 'IMG') {
13392+
if (value.length > 0 && value[0].nodeName === 'IMG') {
1339313393
return 'HTMLImageArray';
1339413394
}
1339513395
return 'Array';

dist/gpu-browser-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.

dist/gpu-browser.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* GPU Accelerated JavaScript
66
*
7-
* @version 2.3.0
8-
* @date Tue Nov 26 2019 09:51:10 GMT-0500 (Eastern Standard Time)
7+
* @version 2.3.1
8+
* @date Tue Dec 17 2019 09:39:34 GMT-0500 (Eastern Standard Time)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -17823,7 +17823,7 @@ const utils = {
1782317823

1782417824
getVariableType(value, strictIntegers) {
1782517825
if (utils.isArray(value)) {
17826-
if (value[0].nodeName === 'IMG') {
17826+
if (value.length > 0 && value[0].nodeName === 'IMG') {
1782717827
return 'HTMLImageArray';
1782817828
}
1782917829
return 'Array';

dist/gpu-browser.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.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gpu.js",
3-
"version": "2.3.0",
3+
"version": "2.3.1",
44
"description": "GPU Accelerated JavaScript",
55
"engines": {
66
"node": ">=8.0.0"

0 commit comments

Comments
 (0)