greljs

1.4.3 • Public • Published

grel

Jun 20, 2018

Javascript library for GPU programming like structured programming

GPU is used here in another form, thanks to adjacency matrix which allow to do relational operations on GPU without CPU outs to know relations.

- Basic demo

let code = `
class C() {
    // header code
    float outVal = 0.0;

    // onInputArg code
    onInputArg(inputArg) {
        if(inputArg instanceOf A) {

        }
        if(inputArg==a) {
            outVal = inputArg0;
        }
    }

    // main code
    main() {
        foutput0 = outVal;
    }
}

A a = new A(0.0);
A b = new A(0.0);
C c = new C();`;

grels = new GRelScript({"code": code});

let render = function() {
    grels.tick();
    requestAnimFrame(render);
};
render();

Quick guide (for graphic mode)

initial position

}

A a = new A(0.0);
a.setPosition(0.1, 1.0, 0.0, 1.0);
A b = new A(0.0);
C c = new C();`;

Node fixed destination

}

A a = new A(0.0);
a.setDestination(0.1, 1.0, 0.0, 1.0);
A b = new A(0.0);
C c = new C();`;

Custom geometry for all nodes

grels = new GRelScript({"code": code,
                        "meshes": [ "../_RESOURCES/meshA.obj"]});

Multiple custom geometry to every node with different vertex length in single buffer geometry (and setting scale/rotation/color on C and color on a and b)

let code = `
class A() {
    onDrawVertex() {
        if(meshId == 0.0) {
            nodeVertexColor = vec4(0.0, 1.0, 0.0, 1.0);
        }
    }
}
class B() {
    onDrawVertex() {
        if(meshId == 1.0) {
            nodeVertexColor = vec4(1.0, 0.0, 0.0, 1.0);
        }
    }
}
class C() {
    float outVal = 0.0;

    onInputArg(inputArg) {
        if(inputArg instanceOf A) {
            outVal = inputArg0;
        }
    }

    onDrawVertex() {
        if(meshId == 2.0) {
            nodeVertexPosition = vec4(nodeVertexPosition.x*0.4, nodeVertexPosition.y*0.4, nodeVertexPosition.z*0.4, 1.0);
            if(foutput0 > 0.0) {
                nodeVertexColor = vec4(0.0, 1.0, 0.0, 1.0);
            } else {
                mat4 mm = rotationMatrix(vec3(1.0,0.0,0.0), (3.1416/2.0)*2.0);
                nodeVertexPosition = nodeVertexPosition*mm;

                nodeVertexColor = vec4(1.0, 0.0, 0.0, 1.0);
            }
        }
    }
                
    main() {
        foutput0 = outVal;
    }
}

A a = new A(0.0);
B b = new B(0.0);
C c = new C();`;

grels = new GRelScript({"code": code,
                        "meshes": [ "../_RESOURCES/meshA.obj",
                                    "../_RESOURCES/meshB.obj",
                                    "../_RESOURCES/meshC.obj"]});

Note: The most of logic must be placed on onInputArg/main and representations on onDrawVertex (inside class).

Code outside is only for the basic initialization but you can write some logic here (next code is an unfunctional example in WebASM mode).


- Logic outside class

float rr = 0.3;
float rrb = 0.3;
float rrc = 0.0;
AB arr[10];
AB arrB[10];
AB arrC[10];
A d = new A(0.2); d.setPosition(-10.0, 0.0, 10.0, 1.0);
for(int n=0;n<3;n++) {
    arr[n] = 0.31+rr; // (it generate extra code. better use it with care)
    arr[n] = 0.27+rr+d;
}
for(int n=0;n<5;n++) {
    rrc = rrc+0.1;
    arrB[n] = 0.51+rrc;
}
for(int n=0;n<10;n++) {
    arr[n].setPosition(-100.0, 0.0, float(n), 1.0); // initial position (setPosition method does not extra code generation)
    arrB[n].setPosition(-70.0, 0.0, float(n), 1.0); // initial position
    arrC[n].setPosition(-30.0, 0.0, float(n), 1.0); // initial position
    arrC[n] = arr[n]+arrB[min(n+2,9)]; // CPU?. it's executed in GPU under the hood! :P (but is better use classes for it)
}
arr[0] = new AB(0.3);
A a = new A(0.0); a.setPosition(-10.0, 0.0, -10.0, 1.0);
A b = new A(0.0); b.setPosition(-10.0, 0.0, -5.0, 1.0);
A c = new A(0.3); c.setPosition(-10.0, 0.0, 5.0, 1.0);

C e = new C(); e.setPosition(10.0, 0.0, -10.0, 1.0);
F f = new F(); f.setPosition(10.0, 0.0, -5.0, 1.0);
F fb = new F(); fb.setPosition(10.0, 0.0, 5.0, 1.0);
F fc = new F(); fc.setPosition(10.0, 0.0, 10.0, 1.0);
F fd = new F(); fd.setPosition(10.0, 0.0, 20.0, 1.0);
F ff = new F(); ff.setPosition(10.0, 0.0, 30.0, 1.0);

A a2 = new A(0.0); a2.setPosition(-10.0, 0.0, 40.0, 1.0);
F f2 = new F(); f2.setPosition(10.0, 0.0, 40.0, 1.0);

c = d;             // set GFunction c with GFunction d (c is the same as c[0] or c.foutput0 (0-4))
e = d;             // set GFunction e with GFunction d
fd = rrb;          // set GFunction fd with real variable rrb
if(a > d) {        // compare two GFunction floats
    e = c;         // set GFunction e with GFunction c
    if(b > 0.5) {  // compare GFunction float with real number
        f = a;     // set GFunction f with GFunction a
        fb = rr;   // set GFunction fb with real variable rr
        fc = 0.4;  // set GFunction fc with real number
        rrb = 0.6; // set real var rrb with real number
        fd = rrb;  // if this is commented fd will keep with initial 0.3 of rrb
    }
}

if(a > 0.0 && a2 > 0.5) {
    f2 = 1.0;
}

// for less internal code you should use the onInputArg event inside classes with an "instanceOf" and
// to handle it in a general form and set the "foutput" depending of status of the input arguments.
ff.onInputArg((inputArg) => { // (it generate much extra code. Use it with care)
    if(inputArg==fd) {
        if(inputArg0 > 0.4) {
            foutput0 = 1.0/2.0;
        }
    }
});

How it works

This is a graph system but not CPU based, it's entirely on GPU (nodes is drawed as single geometry and relations is handled through Adjacency Matrix).

Nodes are like functions which returns an output and this output can be connected to another node (function) like an input (argument). With this we can construct a programming system more closest to another structural programming and make of GPU almost like a fast CPU.

If node functions acts as logical gates, all this gates will be executed at same time.

Sometimes is needed gain access to another "id". This so simple is a bit difficult when someone tries to implement it on GPU and is a bit heavy do the same for every application. Here each node is an "id" and with one relation you will be able to communicate with another "id".

If you need graphical representation, the own node function can be used to customize it according your needs. Nodes can be act as simple logical function, logical function and representable object or a simply representable object.

You can use it to create algorithms that would like to be executed on GPU, but writing it almost like a normal structured language (more easy to carry out). Also can be used for visual applications where many items needs be represented but they depend of the status of another objects.

Max number of nodes is 4096.

More demos

- Two custom geometries with independent logic over one node

- Quantum simulator (in progress)

Built-In variables inside class

onInputArg(inputArg) {}

READ:

inputArg - (to use with: (inputArg instanceof node_type)(checked with internal inputArgNodeType) OR with: (inputArg == node_name)(checked with internal inputArgNodeName)
inputArg0
inputArg1
inputArg2
inputArg3
inputArg4

WRITE:

foutput0
foutput1
foutput2
foutput3
foutput4

READ-WRITE:

currentPos

main() {}

READ:

WRITE:

foutput0
foutput1
foutput2
foutput3
foutput4

READ-WRITE:

currentPos

onDrawVertex() {}

READ:

foutput0
foutput1
foutput2
foutput3
foutput4
meshId

READ-WRITE:

nodeVertexPosition
nodeVertexColor
currentPosition

Installation

npm install greljs
<script src="../../dist/grel/GRel.class.js"></script>
OR
<script src="../../dist/gbrain/grel.min.js"></script>

Slack Group

Readme

Keywords

none

Package Sidebar

Install

npm i greljs

Weekly Downloads

80

Version

1.4.3

License

MIT

Unpacked Size

16.8 MB

Total Files

59

Last publish

Collaborators

  • stormcolor