Skip to content

Commit

Permalink
fix: #534
Browse files Browse the repository at this point in the history
  • Loading branch information
robertleeplummerjr committed Jan 21, 2020
1 parent db54434 commit b2c9051
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 47 deletions.
56 changes: 42 additions & 14 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.6.3
* @date Tue Jan 21 2020 07:26:16 GMT-0500 (Eastern Standard Time)
* @version 2.6.4
* @date Tue Jan 21 2020 08:05:29 GMT-0500 (Eastern Standard Time)
*
* @license MIT
* The MIT License
Expand Down Expand Up @@ -12665,26 +12665,54 @@ class WebGL2Kernel extends WebGLKernel {
}

getKernelString() {
const result = [];
const result = [this.getKernelResultDeclaration()];
const subKernels = this.subKernels;
if (subKernels !== null) {
result.push(
this.getKernelResultDeclaration(),
'layout(location = 0) out vec4 data0'
);
for (let i = 0; i < subKernels.length; i++) {
const subKernel = subKernels[i];
result.push(
subKernel.returnType === 'Integer' ?
`int subKernelResult_${ subKernel.name } = 0` :
`float subKernelResult_${ subKernel.name } = 0.0`,
`layout(location = ${ i + 1 }) out vec4 data${ i + 1 }`
);
switch (this.returnType) {
case 'Number':
case 'Float':
case 'Integer':
for (let i = 0; i < subKernels.length; i++) {
const subKernel = subKernels[i];
result.push(
subKernel.returnType === 'Integer' ?
`int subKernelResult_${ subKernel.name } = 0` :
`float subKernelResult_${ subKernel.name } = 0.0`,
`layout(location = ${ i + 1 }) out vec4 data${ i + 1 }`
);
}
break;
case 'Array(2)':
for (let i = 0; i < subKernels.length; i++) {
result.push(
`vec2 subKernelResult_${ subKernels[i].name }`,
`layout(location = ${ i + 1 }) out vec4 data${ i + 1 }`
);
}
break;
case 'Array(3)':
for (let i = 0; i < subKernels.length; i++) {
result.push(
`vec3 subKernelResult_${ subKernels[i].name }`,
`layout(location = ${ i + 1 }) out vec4 data${ i + 1 }`
);
}
break;
case 'Array(4)':
for (let i = 0; i < subKernels.length; i++) {
result.push(
`vec4 subKernelResult_${ subKernels[i].name }`,
`layout(location = ${ i + 1 }) out vec4 data${ i + 1 }`
);
}
break;
}
} else {
result.push(
'out vec4 data0',
this.getKernelResultDeclaration()
'out vec4 data0'
);
}

Expand Down
6 changes: 3 additions & 3 deletions dist/gpu-browser-core.min.js

Large diffs are not rendered by default.

56 changes: 42 additions & 14 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.6.3
* @date Tue Jan 21 2020 07:26:16 GMT-0500 (Eastern Standard Time)
* @version 2.6.4
* @date Tue Jan 21 2020 08:05:29 GMT-0500 (Eastern Standard Time)
*
* @license MIT
* The MIT License
Expand Down Expand Up @@ -17099,26 +17099,54 @@ class WebGL2Kernel extends WebGLKernel {
}

getKernelString() {
const result = [];
const result = [this.getKernelResultDeclaration()];
const subKernels = this.subKernels;
if (subKernels !== null) {
result.push(
this.getKernelResultDeclaration(),
'layout(location = 0) out vec4 data0'
);
for (let i = 0; i < subKernels.length; i++) {
const subKernel = subKernels[i];
result.push(
subKernel.returnType === 'Integer' ?
`int subKernelResult_${ subKernel.name } = 0` :
`float subKernelResult_${ subKernel.name } = 0.0`,
`layout(location = ${ i + 1 }) out vec4 data${ i + 1 }`
);
switch (this.returnType) {
case 'Number':
case 'Float':
case 'Integer':
for (let i = 0; i < subKernels.length; i++) {
const subKernel = subKernels[i];
result.push(
subKernel.returnType === 'Integer' ?
`int subKernelResult_${ subKernel.name } = 0` :
`float subKernelResult_${ subKernel.name } = 0.0`,
`layout(location = ${ i + 1 }) out vec4 data${ i + 1 }`
);
}
break;
case 'Array(2)':
for (let i = 0; i < subKernels.length; i++) {
result.push(
`vec2 subKernelResult_${ subKernels[i].name }`,
`layout(location = ${ i + 1 }) out vec4 data${ i + 1 }`
);
}
break;
case 'Array(3)':
for (let i = 0; i < subKernels.length; i++) {
result.push(
`vec3 subKernelResult_${ subKernels[i].name }`,
`layout(location = ${ i + 1 }) out vec4 data${ i + 1 }`
);
}
break;
case 'Array(4)':
for (let i = 0; i < subKernels.length; i++) {
result.push(
`vec4 subKernelResult_${ subKernels[i].name }`,
`layout(location = ${ i + 1 }) out vec4 data${ i + 1 }`
);
}
break;
}
} else {
result.push(
'out vec4 data0',
this.getKernelResultDeclaration()
'out vec4 data0'
);
}

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.6.3",
"version": "2.6.4",
"description": "GPU Accelerated JavaScript",
"engines": {
"node": ">=8.0.0"
Expand Down
52 changes: 40 additions & 12 deletions src/backend/web-gl2/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,26 +401,54 @@ class WebGL2Kernel extends WebGLKernel {
* @returns {String} result
*/
getKernelString() {
const result = [];
const result = [this.getKernelResultDeclaration()];
const subKernels = this.subKernels;
if (subKernels !== null) {
result.push(
this.getKernelResultDeclaration(),
'layout(location = 0) out vec4 data0'
);
for (let i = 0; i < subKernels.length; i++) {
const subKernel = subKernels[i];
result.push(
subKernel.returnType === 'Integer' ?
`int subKernelResult_${ subKernel.name } = 0` :
`float subKernelResult_${ subKernel.name } = 0.0`,
`layout(location = ${ i + 1 }) out vec4 data${ i + 1 }`
);
switch (this.returnType) {
case 'Number':
case 'Float':
case 'Integer':
for (let i = 0; i < subKernels.length; i++) {
const subKernel = subKernels[i];
result.push(
subKernel.returnType === 'Integer' ?
`int subKernelResult_${ subKernel.name } = 0` :
`float subKernelResult_${ subKernel.name } = 0.0`,
`layout(location = ${ i + 1 }) out vec4 data${ i + 1 }`
);
}
break;
case 'Array(2)':
for (let i = 0; i < subKernels.length; i++) {
result.push(
`vec2 subKernelResult_${ subKernels[i].name }`,
`layout(location = ${ i + 1 }) out vec4 data${ i + 1 }`
);
}
break;
case 'Array(3)':
for (let i = 0; i < subKernels.length; i++) {
result.push(
`vec3 subKernelResult_${ subKernels[i].name }`,
`layout(location = ${ i + 1 }) out vec4 data${ i + 1 }`
);
}
break;
case 'Array(4)':
for (let i = 0; i < subKernels.length; i++) {
result.push(
`vec4 subKernelResult_${ subKernels[i].name }`,
`layout(location = ${ i + 1 }) out vec4 data${ i + 1 }`
);
}
break;
}
} else {
result.push(
'out vec4 data0',
this.getKernelResultDeclaration()
'out vec4 data0'
);
}

Expand Down
147 changes: 147 additions & 0 deletions test/features/create-kernel-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,150 @@ function createKernelMap3DAuto(mode) {
test('createKernelMap 3d cpu', () => {
createKernelMap3DAuto('cpu');
});

function createKernelMapArray2(mode) {
const gpu = new GPU({ mode });
const kernel = gpu.createKernelMap(
{
mapFunc: function mapFunc(mapFuncVal) {
return mapFuncVal;
}
},
function main() {
const mapFuncVal = [1, 2];
mapFunc(mapFuncVal);
const returnValue = [3, 4];
return returnValue;
},
{
output: [1],
returnType: 'Array(2)',
}
);
const { result, mapFunc } = kernel();
assert.deepEqual(Array.from(mapFunc[0]), [1, 2]);
assert.deepEqual(Array.from(result[0]), [3, 4]);
gpu.destroy();
}

test('createKernelMap Array(2) auto', () => {
createKernelMapArray2();
});

test('createKernelMap Array(2) gpu', () => {
createKernelMapArray2('gpu');
});

(GPU.isWebGLSupported ? test : skip)('createKernelMap Array(2) webgl', () => {
createKernelMapArray2('webgl');
});

(GPU.isWebGL2Supported ? test : skip)('createKernelMap Array(2) webgl2', () => {
createKernelMapArray2('webgl2');
});

(GPU.isHeadlessGLSupported ? test : skip)('createKernelMap Array(2) headlessgl', () => {
createKernelMapArray2('headlessgl');
});

test('createKernelMap Array(2) cpu', () => {
createKernelMapArray2('cpu');
});

function createKernelMapArray3(mode) {
const gpu = new GPU({ mode });
const kernel = gpu.createKernelMap(
{
mapFunc: function mapFunc(mapFuncVal) {
return mapFuncVal;
}
},
function main() {
const mapFuncVal = [1, 2, 3];
mapFunc(mapFuncVal);
const returnValue = [4, 5, 6];
return returnValue;
},
{
output: [1],
returnType: 'Array(3)',
}
);
const { result, mapFunc } = kernel();
assert.deepEqual(Array.from(mapFunc[0]), [1, 2, 3]);
assert.deepEqual(Array.from(result[0]), [4, 5, 6]);
gpu.destroy();
}

test('createKernelMap Array(3) auto', () => {
createKernelMapArray3();
});

test('createKernelMap Array(3) gpu', () => {
createKernelMapArray3('gpu');
});

(GPU.isWebGLSupported ? test : skip)('createKernelMap Array(3) webgl', () => {
createKernelMapArray3('webgl');
});

(GPU.isWebGL2Supported ? test : skip)('createKernelMap Array(3) webgl2', () => {
createKernelMapArray3('webgl2');
});

(GPU.isHeadlessGLSupported ? test : skip)('createKernelMap Array(3) headlessgl', () => {
createKernelMapArray3('headlessgl');
});

test('createKernelMap Array(3) cpu', () => {
createKernelMapArray3('cpu');
});

function createKernelMapArray4(mode) {
const gpu = new GPU({ mode });
const kernel = gpu.createKernelMap(
{
mapFunc: function mapFunc(mapFuncVal) {
return mapFuncVal;
}
},
function main() {
const mapFuncVal = [1, 2, 3, 4];
mapFunc(mapFuncVal);
const returnValue = [5, 6, 7, 8];
return returnValue;
},
{
output: [1],
returnType: 'Array(4)',
}
);
const { result, mapFunc } = kernel();
assert.deepEqual(Array.from(mapFunc[0]), [1, 2, 3, 4]);
assert.deepEqual(Array.from(result[0]), [5, 6, 7, 8]);
gpu.destroy();
}

test('createKernelMap Array(4) auto', () => {
createKernelMapArray4();
});

test('createKernelMap Array(4) gpu', () => {
createKernelMapArray4('gpu');
});

(GPU.isWebGLSupported ? test : skip)('createKernelMap Array(4) webgl', () => {
createKernelMapArray4('webgl');
});

(GPU.isWebGL2Supported ? test : skip)('createKernelMap Array(4) webgl2', () => {
createKernelMapArray4('webgl2');
});

(GPU.isHeadlessGLSupported ? test : skip)('createKernelMap Array(4) headlessgl', () => {
createKernelMapArray4('headlessgl');
});

test('createKernelMap Array(4) cpu', () => {
createKernelMapArray4('cpu');
});

0 comments on commit b2c9051

Please sign in to comment.