GithubHelp home page GithubHelp logo

gpujs / gpu.js Goto Github PK

View Code? Open in Web Editor NEW
15.0K 15.0K 640.0 20.05 MB

GPU Accelerated JavaScript

Home Page: https://gpu.rocks

License: MIT License

JavaScript 98.47% HTML 1.53%
glsl gpgpu gpu javascript math nodejs webgl

gpu.js's People

Contributors

abhisheksoni27 avatar arjunmehta avatar aron123 avatar bddppq avatar bhageena avatar brakdag avatar chrisguttandin avatar coenraads avatar dannycalleri avatar dependabot[bot] avatar dhritzkiv avatar drenata avatar drulac avatar fuzzie360 avatar gnandretta avatar inklabapp avatar jt0dd avatar juniorrojas avatar loyd avatar mluckydwyer avatar nickhingston avatar picocreator avatar robertleeplummerjr avatar sandeepmistry avatar sawman avatar stopyoukid avatar strd6 avatar t-mullen avatar ultrasonicsoft avatar voodoo11 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gpu.js's Issues

GPU mode code does not have JS-like scoping rules

We should implement function level scope for all variables, for more JS-like scoping. What this means is that in codegen we emit all code for variable declarations on top first. This could also mean that we need to decompose all for loops into while loops, because:

func() {
for (var a = 0...) {}
for (var a = 0...) {}
}

is now invalid code.

Using arrays as parameters

Hey there guys,

I'm currently working on shifting big calculations from the CPU to the GPU using GPU.js to increase the performance of my app.

I tried the following:
I created a gpu Kernel which should multiplicate each element from ArrayA, with the corresponding Item from ArrayB:

var gpuMultiplication = gpu.createKernel(function (ArrayA, ArrayB) {
            return ArrayA[this.thread.x] * ArrayB[this.thread.x];
        }).dimensions([50]);

this Code works just fine but only when I'm calling the function using newly created arrays as the parameters. E.g.: gpuMultiplication([1 ,2, 3, 4, ...], [..., 4, 3, 2, 1]);

Once I'm trying to pass an array as a variable into the function (gpuMultiplication(ArrayX, ArrayY);), gpu.js crashes with the following exception:

gpu.js:44 An error occurred compiling the shaders:
ERROR: 0:135: 'user_ArrayA' : undeclared identifier
ERROR: 0:135: 'user_ArrayASize' : undeclared identifier
ERROR: 0:135: 'user_ArrayASize' : left of '[' is not of type array, matrix, or vector
ERROR: 0:135: 'user_ArrayASize' : left of '[' is not of type array, matrix, or vector
ERROR: 0:135: 'user_ArrayADim' : undeclared identifier
ERROR: 0:135: 'user_ArrayADim' : left of '[' is not of type array, matrix, or vector
ERROR: 0:135: 'user_ArrayADim' : left of '[' is not of type array, matrix, or vector
ERROR: 0:135: 'user_ArrayADim' : left of '[' is not of type array, matrix, or vector
ERROR: 0:135: 'get' : no matching overloaded function found
ERROR: 0:135: 'user_ArrayB' : undeclared identifier
ERROR: 0:135: 'user_ArrayBSize' : undeclared identifier
ERROR: 0:135: 'user_ArrayBSize' : left of '[' is not of type array, matrix, or vector
ERROR: 0:135: 'user_ArrayBSize' : left of '[' is not of type array, matrix, or vector
ERROR: 0:135: 'user_ArrayBDim' : undeclared identifier
ERROR: 0:135: 'user_ArrayBDim' : left of '[' is not of type array, matrix, or vector
ERROR: 0:135: 'user_ArrayBDim' : left of '[' is not of type array, matrix, or vector
ERROR: 0:135: 'user_ArrayBDim' : left of '[' is not of type array, matrix, or vector
ERROR: 0:135: 'get' : no matching overloaded function found

Both Arrays are initialized as Float32Array(50) and filled with random values. (Values are only numeric and without any commas)

Am I doing something wrong? Or is it not possible to input arrays like that?
I created my code according to the following example from your website: http://gpu.rocks/examples/

Regards,
Michael

Errors with very large arrays

Hi,

When trying to execute a GPU function on a large array (1920x1080x3 which represents a 1920x1080 picture with 3 color components) it fails with the following error:
"RangeError: too many arguments provided for a function call" on Firefox
"RangeError: Maximum call stack size exceeded" on Chrome.

See attached example, which do has non-op function to exhibit the issue: https://drive.google.com/file/d/0BxAjgoe3PaRwaTFHMUsxWG1xV2c/view?usp=sharing
It works when working on smaller arrays, but fails on large ones.

I'm not sure how big arrays are common in Javascript (I feel like Firefox or Chrome have issues handling them anyway) but such arrays don't sound that big to me, particularly for taking full advantage of GPU operations by working on big data set.

Texture Object example?

The docs say that the flag outputToTexture can be used to get a Texture Object instead of an array, and that it can be fed as an input to a new Kernel Function to avoid the round-trip penalty between kernels. Is there an example of two kernels working this way?

Coloring canvas pixels using other than this.color

In the demo, coloring a canvas is done using this.color(r,g,b,a) for each individual pixel.
First, something like var i=0, and then in the kernel function doing this.color(0,i,0,1); i++; gives errors.
Is the only way possible to style a pixel is with fixed numeric values? Can we use variables or equate pixels to other pixels from the browser for example?

Optimizing the Dragons

Ever since we fixed computations on Intel and AMD gpus, calculations are not as fast as they can be. I'm going to explore how to improve/optimize this.

Polyfilled (fake) asynchronous kernel API support

#5 requires asynchronous API but is currently blocked by #4.

We could implement a fake asynchronous API by returning a Promise that resolves synchronously but the downside is it might be confusing and unintuitive to users.

Canvas image output support

Would be useful for image processing like convolution filters and etc. Might require #7 to store rgba but its possible to use partial support before vec are implemented as its a very specific case for output.

Break and Continue statements are not implemented

for example:

var y = gpu.createKernel(function() {
        for (i =0; i <10; i++) {
            break;
        }
    }, opt);

It seems the ast_BreakStatement and ast_ContinueStatement functions are not implemented in functionnode_webgl.js

more es6 style

I see a lot of:

var GPU = (function() {
	var GPU = GPUCore;

if this were more es6-ish, we'd get a very pretty:

class GPU {}

Array Support

We want to support fix sized arrays. To ensure that, we check for modifications to arrays beyond initialization. Not very JS-like, but we need to either initialise or annotate the array var declaration for the type inference to work

decimal precision?

I was curious on how decimal precision is obtained in gpu.js. 64, 32, 24, or less? Can we have that put into documentation as well?

Support for function calls in the kernel

We want to support calling functions declared in the environment outside the kernel. This means that we will use reflection to import the function from the environment when we detect a call in the code generator, and compile the function automatically.

Using webgl scripts

What if along with the function inputs we could use a string that has a webgl script and has access to all the inputs. That would make it more reliable seeing as the compiling is not perfect yet.

Using functions as a constant.

An error occurred compiling the shaders: ERROR: 0:135: 'constants' : undeclared identifier
ERROR: 0:135: 'constants_pixelMultiplier' : undeclared identifier
ERROR: 0:135: 'constants_scale' : undeclared identifier
ERROR: 0:135: '' : methods supported in GLSL ES 3.00 and above only
ERROR: 0:135: 'func' : invalid method
ERROR: 0:135: 'assign' : cannot convert from 'const int' to 'highp float'

Here is my code.

let gpu = new GPU();
function generateArrayWithFunction(w, h, scale, f) {
	/*let output = [];
	for (let x = 0; x < w * pixelMultiplier; x++) {
		output.push([]);
		for (let y = 0; y < h * pixelMultiplier; y++) {
			output[x][y] = f(x / pixelMultiplier * scale, y / pixelMultiplier * scale, 0);
		}
	}

	return output;*/
	let g = gpu.createKernel(function() {
		return this.constants.func(this.thread.x / this.constants.pixelMultiplier * this.constants.scale, this.thread.y / this.constants.pixelMultiplier * this.constants.scale, 0);
	}).dimensions({dimensions: [w, h], constants: {func: f, pixelMultiplier: pixelMultiplier, scale: scale}});
	return g();
}

Safari support?

When I try to run on safari I get:
WebGL: INVALID_ENUM: readPixels: invalid type

WebWorker backend support

Use the WebWorker API to run the kernel on CPU cores.

Challenges:

  • WebWorkers are purely asynchronous. Blocked by #4 for API parity among all backends which might break graceful fallback.

Actually release v1.0.0

This project needs to adopt semver 2.0.0. So:

  1. Current API will be frozen until 2.0.0.
  2. Additional API will increase minor version number.

Let's do a quick once over the following advertised functionality:

Basic

  • Accept numbers as arguments to kernel functions
  • Accept 1D array as arguments to kernel functions
  • Accept 2D array as arguments to kernel functions
  • Accept 3D array as arguments to kernel functions
  • Produce output in 1D
  • Produce output in 2D
  • Produce output in 3D

Settings

  • this.constants support
  • Debug flag support
  • Graphical mode in CPU
  • Graphical mode in GPU
  • hardcodeConstants support
  • outputToTexture support
  • loopMaxIterations support
  • Mode switching support
  • Add function support

Syntax

  • Arithmetic operations
  • Context constants
  • Argument array read
  • Return
  • if, else if, else branches
  • While and for loops
  • Fixed sized for loops
  • Internal functions
  • Math functions
  • Color function

Platforms:

  • Runs on Chrome
  • Runs on Firefox
  • Runs on Safari
  • Runs on IE12
  • Runs on Edge
  • Runs on Mobile Chrome (iOS)
  • Runs on Mobile Chrome (Android)
  • Runs on Mobile Safari

Functions support

Hello. How I can passthrough JS functions (i. e. if can be universal) in gpu.js?
Also, how I can use variants of grid values?

  • [0...1] float
  • [0...width] int

Because I want to make graphical color picker, for HSV, HSL, HCG.

int support

We want to investigate how to implement int type. This would mean emitting different code depending on which mode we run the compiler in.

In place Array oporations

Currently argument arrays are read only. However this limits the kind of operations that are possible.

For instance it is not possible to define FFT like algorithms as you have to map to the array output, necessitating an N^2 operation.

That is it is not possible to separate the size and shape of the kernel from the size and shape of the outputted/modified array.

Would it be possible to support this mode of operation? What would be needed in gpu_core.js to achieve this?

Nested variable declarations in for loop difference (of CPU vs GPU)

Kudos for @staceytay for doing up a ray tracer demo

http://staceytay.com/raytracer/

Sadly for the GPU.JS team, it also shows that we may have a canvas related bug for rendering mode (floating point accuracy?). Specifically for "Lambertian reflectance"

Maybe its his code, or our code (most probably ours), but yea =(

@todo : investigate and fix

To replicate, switch between CPU and GPU with Lambertian reflectance "on" and pay close attentions to the shadow shading

vec2, vec3, vec4 support

Vectors are very useful for graphical computations so it would be very useful to support vec variables and function arguments and function returns.

When it's implemented it could look like:

function kernel() {
    var v = this.vec2( 1, 2 );
    var mag = Math.sqrt(v.x*v.x + v.y*v.y);
}

The "this" context variable can provide the vec2 function in fallback mode just like this.thread.x and etc.

Challenges:

  • Appear to need type inference for variable v which might not be feasible. Either that or commented annotations like: /*vec3*/var v = ... which might look very ugly.

Userland function name safety

Two options

  1. Prefix all the function with a "user_" voodoo

  2. Have a blacklist of function names. Throw an exception on GPU.addFunction call if function name is illegal.

Personally i vote for 2. Makes the shader code have less voodoo

Multiple function support

Support for multiple functions, allowing nested calls. Etc.

  1. Prototype function register builder, for webgl
  2. JS to webgl function register
  3. Ship it, break things

Should no argument kernel be supported?

Referencing an old (commented out) failing test case inside if_else.js

function booleanBranch( mode ) {
    var f = GPU(function() {
        var ret = 0.0;
        if(true) {
            ret = 4.0;
        } else {
            ret = 2.0;
        }
        return ret;
    }, {
        thread : [1],
        block : [1],
        mode : mode
    });

    QUnit.ok( f !== null, "function generated test");
    QUnit.close(f(), 42.0, 0.01, "basic return function test");
}

QUnit.test( "booleanBranch (auto)", function() {
    booleanBranch(null);
});

QUnit.test( "booleanBranch (GPU)", function() {
    booleanBranch("gpu");
});

QUnit.test( "booleanBranch (CPU)", function() {
    booleanBranch("cpu");
});

Which currently fails, presumingly cause our entire system expects an argument for data input.

Soooo..... should this be fixed?

To replicate: just comment back in those test case, its inside test/src/features/if_else.js

OpenGL code generation support

Long term practical application, that may have commercial usage.

  1. Upload "big data" set
  2. Get sample of data set in browser
  3. Experiment with local scripts, in webgl
  4. Run it on scale / grid in opengl

Compiler optimisations

Some basic compile-time optimisations would be nice.

Already implemented:

  • Inlining constant uniform variables

Possible optimisations:

  • Compiler may generate something like decode32(encode32(x)), can easily be replaced as x
  • Rewrite control structures to prevent branch. e.g. if (x == 1) y += 1; to y += x*1;

Fix ===

=== is the recommended way to compare values in javascript, but it throws a compiler error in, for example:

var y = gpu.createKernel(function() {
            if (1 === 1) {

            }
    });

Truly asynchronous kernel API support

Currently all APIs are blocking and will freeze the main thread if the kernel is computationally large.

Challenges:

  • No asynchronous API for WebGL exists (Blocker)

Full type inference

We want to support full type inference for all variables and function parameters. In order to do that however, we need to ensure that userland functions all obey the language rules of GPU.js in order for the type inference to work.

Types we need to support:
float
vec2/3/4
array of

Types we plan to support:
int
object

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.