Skip to content

Commit

Permalink
Merge pull request #551 from gpujs/550-expose-typings
Browse files Browse the repository at this point in the history
feat: Provide overloading methods with types
  • Loading branch information
robertleeplummerjr authored Dec 17, 2019
2 parents f514e79 + 142f191 commit cc88928
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 133 deletions.
95 changes: 95 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const c = multiplyMatrix(a, b) as number[][];

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.

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

## Demos
GPU.js in the wild, all around the net. Add yours here!
* [Temperature interpolation using GPU.js](https://observablehq.com/@rveciana/temperature-interpolation-using-gpu-js)
* [Julia Set Fractal using GPU.js](https://observablehq.com/@ukabuer/julia-set-fractal-using-gpu-js)
* [Hello, gpu.js v2](https://observablehq.com/@fil/hello-gpu-js-v2)
* [Basic gpu.js canvas example](https://observablehq.com/@rveciana/basic-gpu-js-canvas-example)
* [Raster projection with GPU.js](https://observablehq.com/@fil/raster-projection-with-gpu-js)
* [GPU.js Example: Slow Fade](https://observablehq.com/@robertleeplummerjr/gpu-js-example-slow-fade)
* [GPU.JS CA Proof of Concept](https://observablehq.com/@alexlamb/gpu-js-ca-proof-of-concept)
* [Image Convolution using GPU.js](https://observablehq.com/@ukabuer/image-convolution-using-gpu-js)
* [Leaflet + gpu.js canvas](https://observablehq.com/@rveciana/leaflet-gpu-js-canvas)
* [Image to GPU.js](https://observablehq.com/@fil/image-to-gpu)
* [GPU Accelerated Heatmap using gpu.js](https://observablehq.com/@tracyhenry/gpu-accelerated-heatmap-using-gpu-js)
* [Dijkstra’s algorithm in gpu.js](https://observablehq.com/@fil/dijkstras-algorithm-in-gpu-js)
* [Voronoi with gpu.js](https://observablehq.com/@fil/voronoi-with-gpu-js)
* [The gpu.js loop](https://observablehq.com/@fil/the-gpu-js-loop)
* [GPU.js Example: Mandelbrot Set](https://observablehq.com/@robertleeplummerjr/gpu-js-example-mandelbrot-set)
* [GPU.js Example: Mandelbulb](https://observablehq.com/@robertleeplummerjr/gpu-js-example-mandelbulb)
* [Inverse of the distance with gpu.js](https://observablehq.com/@rveciana/inverse-of-the-distance-with-gpu-js)
* [gpu.js laser detection v2](https://observablehq.com/@robertleeplummerjr/gpu-js-laser-detection-v2)
* [GPU.js Canvas](https://observablehq.com/@hubgit/gpu-js-canvas)
* [Video Convolution using GPU.js](https://observablehq.com/@robertleeplummerjr/video-convolution-using-gpu-js)
* [GPU Rock Paper Scissors](https://observablehq.com/@alexlamb/gpu-rock-paper-scissors)
* [Shaded relief with gpujs and d3js](https://observablehq.com/@rveciana/shaded-relief-with-gpujs-and-d3js/2)

## Installation
On Linux, ensure you have the correct header files installed: `sudo apt install mesa-common-dev libxi-dev` (adjust for your distribution)

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

## Typescript Typings
Typescript is supported! Typings can be found [here](src/index.d.ts)!
For strongly typed kernels:
```ts
import { GPU, IKernelFunctionThis } from './src';
const gpu = new GPU();

function kernelFunction(this: IKernelFunctionThis): number {
return 1 + this.thread.x;
}

const kernelMap = gpu.createKernel<typeof kernelFunction>(kernelFunction)
.setOutput([3,3,3]);

const result = kernelMap();

console.log(result as number[][][]);
```

For strongly typed mapped kernels:
```ts
import { GPU, Texture, IKernelFunctionThis } from './src';
const gpu = new GPU();

function kernelFunction(this: IKernelFunctionThis): [number, number] {
return [1, 1];
}

function subKernel(): [number, number] {
return [1, 1];
}

const kernelMap = gpu.createKernelMap<typeof kernelFunction>({
test: subKernel,
}, kernelFunction)
.setOutput([1])
.setPipeline(true);

const result = kernelMap();

console.log((result.test as Texture).toArray() as [number, number][]);
```

For extending constants:
```ts
import { GPU, IKernelFunctionThis } from './src';
const gpu = new GPU();

interface IConstants {
screen: [number, number];
}

type This = {
constants: IConstants
} & IKernelFunctionThis;

function kernelFunction(this: This): number {
const { screen } = this.constants;
return 1 + screen[0];
}

const kernelMap = gpu.createKernel<typeof kernelFunction>(kernelFunction)
.setOutput([3,3,3])
.setConstants<IConstants>({
screen: [1, 1]
});

const result = kernelMap();

console.log(result as number[][][]);
```

## Destructured Assignments **New in V2!**
Destructured Objects and Arrays work in GPU.js.
Expand Down
6 changes: 3 additions & 3 deletions dist/gpu-browser-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* GPU Accelerated JavaScript
*
* @version 2.3.0
* @date Tue Nov 26 2019 09:51:10 GMT-0500 (Eastern Standard Time)
* @version 2.3.1
* @date Tue Dec 17 2019 09:39:34 GMT-0500 (Eastern Standard Time)
*
* @license MIT
* The MIT License
Expand Down Expand Up @@ -13389,7 +13389,7 @@ const utils = {

getVariableType(value, strictIntegers) {
if (utils.isArray(value)) {
if (value[0].nodeName === 'IMG') {
if (value.length > 0 && value[0].nodeName === 'IMG') {
return 'HTMLImageArray';
}
return 'Array';
Expand Down
6 changes: 3 additions & 3 deletions dist/gpu-browser-core.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/gpu-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* GPU Accelerated JavaScript
*
* @version 2.3.0
* @date Tue Nov 26 2019 09:51:10 GMT-0500 (Eastern Standard Time)
* @version 2.3.1
* @date Tue Dec 17 2019 09:39:34 GMT-0500 (Eastern Standard Time)
*
* @license MIT
* The MIT License
Expand Down Expand Up @@ -17823,7 +17823,7 @@ const utils = {

getVariableType(value, strictIntegers) {
if (utils.isArray(value)) {
if (value[0].nodeName === 'IMG') {
if (value.length > 0 && value[0].nodeName === 'IMG') {
return 'HTMLImageArray';
}
return 'Array';
Expand Down
6 changes: 3 additions & 3 deletions dist/gpu-browser.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gpu.js",
"version": "2.3.0",
"version": "2.3.1",
"description": "GPU Accelerated JavaScript",
"engines": {
"node": ">=8.0.0"
Expand Down
Loading

0 comments on commit cc88928

Please sign in to comment.